0
votes

Hi I am new to kernel driver developement. I am using the raspberry pi as my hostI am trying to create an I2C driver for a custom board we have. The custom board will act as the slave. I am confused about how do I go about entering the devices slave address. From what I understand

  1. You need to either have a board setup file which I dont since its a custom board.
  2. You can edit the device tree
  3. Or you can do it in the user space application.

I am not sure where exactly to edit the device tree if I go with the second option. More over I would like to somehow register the slave address in the I2C driver itself. That way I donot need to rebuild the kernel. One method i was looking at was to set the i2c client from the driver code but that was advised by commentators I am not sure why. Any help would be appreciated.

Instantiating Drivers

1
Device Tree is correct approach. Or in case the platform supports ACPI can do similar.0andriy

1 Answers

0
votes

So I have finally a working way in which I can bind the I2C device without needing a kernel rebuild. I create two driver files(.ko files). One for registering and one for the actual driver.

The way I did it is I got the bus number to which the device was connected. (You can look into i2c user space code. i2cdetect -y (busnumber) will help you detect which bus number it is)

Once I knew that I created a driver file which would register my device by getting access to the adapter and then registering it. My bus number was 1 and slave address 0x10

static struct i2c_board_info board_info[] __initdata = 
{
    {
         I2C_BOARD_INFO("my_device", 0x10),
     },
};

And in the init function of the driver I register the device by

i2c_new_device(i2c_get_adapter(1), board_info[0])

Thats it. Now once you build this insmod the ko file before insmoding the actual driver file and everything should work.