i2c--2.6.34 Document: How to enumerate the generated I2C_CLIENT__I2C

Source: Internet
Author: User

According to their own understanding, the http://lxr.linux.no/linux+v2.6.34/Documentation/i2c/instantiating-devices is translated into the document about enumerating and establishing i2c_client. Have objections or questions please refer to the original, after all, the core of the document is the true essence.

Method 1: Use the bus number to declare the device. defines the device's information in the initialization of the kernel. The prerequisite is that the kernel compiles to determine which I2C devices and their addresses, as well as the number of connected buses. For example, in/arch/arm/mach-xxxx/board_xxxx.c you can have such a piece of code to register the I2C device information.[CPP]  View Plain Copy static struct i2c_board_info __initdata h4_i2c_board_info[] =  {              {                     i2c_board_info ("Isp1301_omap", 0x2d),                     .irq            =  OMAP_GPIO_IRQ (,    )          },               {        /* EEPROM on mainboard */                       i2c_board_info ("24C01",  0x52),    &NBsp                  . platform_data  = &m24c01,              },              {        /* EEPROM on cpu card */                      i2c_board_info ("24c01",  0x57),                       .platform_data  = &m24c01,               },    };           static void __init omap_h4_init (void)      {                (...)              i2c_register_board_info (1,h4_i2c_board _info,                           array_size (H4_i2c_board_info));                (...)     }    
After registering, I2c_adapter will scan all registered 2c_board_info and establish a i2c_client for connecting to I2C devices. In this way, when registering with the same name I2c_driver in 2c_board_info, I2c_client is bound to I2c_driver and I2c_driver's probe function is invoked.
Method 2: Enumerate the devices. use I2c_new_device () or I2c_new_probed_device ()Method 1 has many limitations and must be aware of the I2C bus number and physical connection when compiling the kernel. Sometimes developers are faced with an existing system that cannot modify the kernel. Or when the kernel developer porting the system, you don't know what I2C devices are or how many I2C buses there are. In this case, you need to use I2c_new_device (). Its prototype is: struct i2c_client * i2c_new_device (struct i2c_adapter *adap, struct i2c_board_info const *info); This function will use the information provided by info to create a i2c_client and bind to the i2c_adapter that the first parameter points to. The returned parameter is a i2c_client pointer. The driver can communicate directly with the device using the i2c_client pointer. This method is a relatively simple method.


The function to get the i2c_adapter pointer is: struct i2c_adapter* i2c_get_adapter (int id);//Its argument is the I2C bus number. Use to release: void I2c_put_adapter (struct i2c_adapter *adap), if the address of the I2C device is not fixed, or even have different addresses on different boards, you can provide an address list for system detection. The function that should be used at this time is i2c_new_probe_device. Usage is as follows:
[CPP] View Plain copy example  (from the pnx4008 ohci driver):     static  const unsigned short normal_i2c[] = { 0x2c, 0x2d, i2c_client_end  };     Static int __devinit usb_hcd_pnx4008_probe (struct platform _device *pdev)      {                  (...)                  struct  i2c_adapter *i2c_adap;                  struct i2c_board_info i2c_info;                   (...)                  i2c_adap  = i2c_get_adapter (2);                  memset (&i2c_info,  0, sizeof (struct i2c_board_info));                  strlcpy (i2c_info.name,  "Isp1301_pnx",  i2c_name_size);                  isp1301_i2c_client =  i2c_new_probed_device (i2c_adap, &i2c_info,                                                               NORMAL_I2C);                  i2c_put_adapter (i2c_ ADAP);                   (...)        }    

I2c_new_probed_device's prototype is:

struct i2c_client * i2c_new_probed_device (struct i2c_adapter *adap,struct i2c_board_info *info,unsigned Short const * Addr_list);

This function will detect the address in addr_list on the specified bus, assign the first address with ACK feedback to info->addr, and then invoke I2c_new_device with the first two arguments. Its return value is also an available i2c_client pointer. I2c_unregister_device () can unregister the i2c_client of the I2c_new_device ()/i2c_new_probed_device () application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.