1
votes

I have a thermo3 click which has tmp102 sensor.

To read data from it, I have written a user-space application using I2C as follows:

fd = open("/dev/i2c-0", O_RDWR))
ioctl(fd, I2C_SLAVE, 0x48)
read(fd, &buf, 2)

But then I found its driver in linux source code.

So I am wondering:

1) Can I write a user-space application which uses this driver's API's (means using driver's device file)?

2) How I2C user-space application works without using this driver (because this driver is not enabled in my kernel when I was using i2c user-space application) or This is the way I2C works?

2

2 Answers

1
votes

1) This is an hwmon device, you don't have a device file to use. Instead the api is in /sys/class/hwmon. In particular, you want to read the /sys/class/hwmon/hwmonX/tempX_input files

2) By using /dev/i2c-0, your are directly communicating on the i2c bus, effectively bypassing any kernel driver. You then have to know the details for any device you use (registers to read and their meaning...) instead of using the generic Linux kernel abstraction.

1
votes

User Level Application can not interact with hardware. They can communicate by system call (ioctl, open, read and write etc) and Sysfs(sysfs is a virtual file system).

1 :- Your device is hwmom device. Your Driver is "tmp102". Driver Expose these three temp1_input, temp1_max_hyst and temp1_max sysfs entry for user-level application.

 SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0);
 SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,  tmp102_set_temp, 1);
 SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
          tmp102_set_temp, 2);

You can read /sys/class/hwmom/tmp102/temp1_input sysfs file. you can read and write these two sysfs file /sys/class/hwmom/tmp102/temp1_max_hyst and sys/class/hwmom/tmp102/temp1_max.

2 :- The files in /dev are actual devices files which UDEV creates at run time.A device file is an interface for a device driver that appears in a file system as if it were an ordinary file. Your driver has expose some other device file. thermal_zone_of_sensor_register(hwmon_dev, 0,hwmon_dev, &tmp102_of_thermal_ops). You have /dev/hwmom_dev node.

User-Level application can't directly communicate any of i2c-device or hardware. Linux kernel has limitation. Application need driver as interface to controle any of device.