How does the Linux kernel now what to put in platform_data?

63 Views Asked by At

I'm analyzing the device driver for e.g. TI TCA6408 device, the driver is gpio-pca953x. In the ->probe() function there is a part when the platform data is retrieved from the device:

static int pca953x_probe(struct i2c_client *const struct i2c_device_id *i2c_id)
{
    [...]
    int irq_base = 0;
    struct pca953x_platform_data *pdata;
    pdata = dev_get_platdata(&client->dev);

    irq_base = pdata->irq_base;
    [...]
}

The structure looks as follows:

struct pca953x_platform_data {
    /* number of the first GPIO */
    unsigned    gpio_base;

    /* initial polarity inversion setting */
    u32     invert;

    /* interrupt base */
    int     irq_base;

    void        *context;   /* param to setup/teardown */

    int     (*setup)(struct i2c_client *client,
                unsigned gpio, unsigned ngpio,
                void *context);
    int     (*teardown)(struct i2c_client *client,
                unsigned gpio, unsigned ngpio,
                void *context);
    const char  *const *names;
};

My question is - as I do not see any assignments to any of the pca953x_platform_data's fields, how the hell does this irq_base field (from pca953x_platform_data) get its value? I'm completely puzzeld...

The call to: dev_get_platdata() just returns what is stored in dev->platform_data.

The example device tree that is:

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/interrupt-controller/irq.h>

i2c0 {
    #address-cells = <1>;
    #size-cells = <0>;

    gpio@20 {
        compatible = "nxp,pca9505";
        reg = <0x20>;
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_pca9505>;
        gpio-controller;
        #gpio-cells = <2>;
        interrupt-parent = <&gpio3>;
        interrupts = <23 IRQ_TYPE_LEVEL_LOW>;

        usb3-sata-sel-hog {
            gpio-hog;
            gpios = <4 GPIO_ACTIVE_HIGH>;
            output-low;
            line-name = "usb3_sata_sel";
        };
    };
};

I could imagine that in some part of the code, somebody allocates memory for pca953x_platform_data struct and then assigns values for each field, however nothing like this happens within this driver.

1

There are 1 best solutions below

0
0andriy On

First of all, you forgot to mention the Linux kernel version you are asking about. The latest version of struct pca953x_platform_data definition has only two fields left (mostly due to old unsupported code removal during last Linux kernel releases.

Now, if you run

git grep -n -w 'struct pca953x_platform_data'

in the Git repository of Linux kernel sources, you will see that the only three boards that are using this data type are still in kernel. The EP93xx is going to be converted to the Device Tree model (there is patch series). And the rest can be easily converted to use software node approach.

That said, your question can be easily answered.

  • For the above mentioned boards it comes from the respective so called board files (see 2).
  • For the rest it can come via ACPI or Device Tree, but actually the driver hard codes it.

In the first case the data defined statically and hence no memory allocations are performed or needed. It simply passed to the struct i2c_board_info which I²C framework takes care on.