0
votes

I am learning about how to develop a Linux I2C kernel driver, and I learn from websites below.
How to instantiate I2C devices
I2C Driver for Linux Based Embedded System
...
Next, I found a sample that shows how to implement a I2C touchpad driver, but it really confused me.
linux/drivers/input/mouse/synaptics_i2c.c

My question is, how Linux kernel bind this driver to correctly device?
This driver don't provide 'detect' callback, no specify the I2C slave address via i2c_driver.address_list, and there is seems no anyone to call i2c_board_info to register the address info (I grep a whole Linux codebase).
I thought the driver MUST be specify the slave address or provide the 'detect' callback, just like
drivers/hwmon/adc128d818.c
or linux/drivers/rtc/rtc-ds1307.c (it will be register by i2c_board_info)

Please let me know what I missed, thanks.

2
Short answer: compatible string in your driver is matching corresponding compatible string from your device tree file (.dts). Then matching happens, and probe() function being called. See this and this for details.Sam Protsenko
Thanks for your answer, you mean that "synaptics_i2c.c" rely on device tree? Even I don't declare '.of_match_table = of_match_ptr(synaptics_i2c_of_match),'?? In "synaptics_i2c.c", the point that confuse me is, if you are not defined CONFIG_OF, it seems not to declare .compatible, in this case, how it work?Lak Fu
Look at i2c_device_match() function. It tries to do 3 kind of matches: first device tree match (OF), then ACPI match, and if both failed -- then I2C match. So if you neither use Device Tree or ACPI, matching will happen using your i2c_device_table->name, in i2c_match_id() function.Sam Protsenko
See also this answer for details.Sam Protsenko
Oh, that exactly is my question - I2C match. It looks like a string match mechanism. In driver, we provide the name by filling i2c_device_id struct, but kernel how to know a device's name? Without OP and ACPI, how I2C match happen? Is it rely on I2C_BOARD_INFO? If I want my device bind to my driver by I2C match, what I need to prepare? Modify kernel and call I2C_BOARD_INFO to add my device info, right? Anyway, thank you so much!!Lak Fu

2 Answers

0
votes

The i2c device declaration is start from device tree.

Declare the i2c devices in device tree.

Example:

i2c1: i2c@400a0000 {
    /* ... master properties skipped ... */
    clock-frequency = <100000>;

    flash@50 {
        compatible = "atmel,24c256";
        reg = <0x50>;
    };

    pca9532: gpio@60 {
        compatible = "nxp,pca9532";
        gpio-controller;
        #gpio-cells = <2>;
        reg = <0x60>;
    };
};

Where,

1) 400a000 is a i2c bus address 2) pca9532 and flash are driver names 3) @50 and @60 are slave address 4) the attribute “compatible” to find and map device with the driver 5) other attribute which is inside cell of each entries are specific to the driver which will be used for device initialization during the probe

https://www.kernel.org/doc/Documentation/i2c/instantiating-devices

0
votes

I finally figured out my question.
Refer to http://www.embedded-bits.co.uk/2009/i2c-in-the-2632-linux-kernel/
There is need to register my I2C device with i2c_new_probed_device() or i2c_new_device on Kernel to let it have a mapping table about slave address and device name.