Search code examples
linux-device-driverembedded-linuxi2c

What is the right function to have chip specific initialization in Linux i2c drivers


I am trying to initialize a status register of a ADT746x chip. The Linux kernel has hwmon drivers for this chip, but it is not configured.

I need to initialize few sets of register as soon as device is ready to accept i2c commands via sys fs. These registers configure the temperature and voltage sensors as valid and marks them for monitoring.

What is the right place to have this init code? probe() or detect() or anywhere else in userland?


Solution

  • There are few drivers for this family of sensors in the vanilla kernel, i.e. drivers/hwmon/adt7462.c (in kernel documentation), drivers/hwmon/ln85.c (in kernel documentation), drivers/hwmon/lm90.c (in kernel documentation) and drivers/macintosh/therm_adt746x.c.

    All of them have corresponding interface via sysfs: for hwmon it's a generic one, and specific one for Macintosh driver.

    To achieve what you are asking the following options are possible:

    • Black list modules (if they are loaded automatically) and load them thru the script which will apply necessary parameters through sysfs interface
    • Hack the driver by hard coding necessary calls.

    For the second variant the best option is to place the code in the ->probe() callback. For hwmon drivers it makes sense to do it before calling hwmon_device_register() since the last one will expose the sysfs attributes that user space may immediately use.

    ->detect() is dedicated to allow I²C core check if the driver sees any of supported devices on the bus. At this stage it's not known that device in question is connected. Even after detection some mandatory initialization steps may be missed, and thus it's not recommended to communicate with device at that stage.