Add third party device driver to device tree

Hello,
I am working on hts221 temperature and humidity sensor. I got two device drivers for that

  1. GitHub - STMicroelectronics/hts221: hts221 platform independent driver based on Standard C language and compliant with MISRA standard
  2. GitHub - jbroutier/HTS221-driver: Driver for the HTS221 humidity and temperature sensor used in the Raspberry Pi Sense Hat board.

How can I add these device drivers to the device tree.
I am new to device drivers any help is much appreciated.
Thank you

Computer on Module - Apalis iMX8
Carrier Board - Ixora Carrier Board
Computer on Module OS - Torizon
Development PC OS - Linux

Greetings @bhagwat_99,

First of all, in order to add these new driver to TorizonCore please see the article here: Building External Kernel Modules With Torizon

In terms of “adding” them to the device tree, I’m not entirely sure. These drivers don’t seem to be organized the same as other peripheral drivers in the Linux kernel. Seems like you just invoke the driver header files in your code. This will most likely be something you’ll need to experiment with to see how these drivers work.

Best Regards,
Jeremias

Greeting @jeremias.tx
Thank you for help.

Glad I could provide some guidance.

Hello @jeremias.tx
I want to interface opt3001 light sensor to the i2c bus of apalis imx8qm. Linux driver for the same is already present. linux/opt3001.c at master · torvalds/linux · GitHub
I changed i2c node in device tree as follows

&i2c2 {
	status = "okay";

	atmel_mxt_ts: atmel_mxt_ts@4a {
		compatible = "atmel,maxtouch";
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_gpio5>, <&pinctrl_gpio6>;
		reg = <0x4a>;
		interrupt-parent = <&lsio_gpio4>;
		interrupts = <1 IRQ_TYPE_EDGE_FALLING>;    /* Apalis GPIO5 */
		reset-gpios = <&lsio_gpio4 2 GPIO_ACTIVE_HIGH>; /* Apalis GPIO6 */
		status = "disabled";
	};

	/* M41T0M6 real time clock on carrier board */
	rtc_i2c: rtc@68 {
		compatible = "st,m41t0";
		reg = <0x68>;
	};

	eeprom: eeprom@50 {
		compatible = "atmel,24c02";
		reg = <0x50>;
		pagesize = <16>;
	};

	light_sensor: opt3001@44 {
		compatible = "ti,opt3001";
		reg = <0x44>;
	};

        temp_sensor: hts221@5f {
		compatible = "st,hts221";
		reg = <0x5f>;
	};

};

I got these devices

apalis-imx8-06852506:~$ /sys/class/i2c-dev/i2c-4/device/      
4-0044/    4-0050/    4-005f/    4-0068/    device/    i2c-dev/   of_node/   power/     subsystem/

apalis-imx8-06852506:~$ ls -la /sys/class/i2c-dev/i2c-4/device/4-0050
total 0
drwxr-xr-x 4 root root    0 Jan  1  1970 .
drwxr-xr-x 8 root root    0 Jan  1  1970 ..
drwxr-xr-x 3 root root    0 Jan  1  1970 4-00500
-r--r--r-- 1 root root 4096 Feb 17 09:33 consumers
lrwxrwxrwx 1 root root    0 Jan  1  1970 driver -> ../../../../../../bus/i2c/drivers/at24
-rw------- 1 root root  256 Feb 17 09:42 eeprom
-r--r--r-- 1 root root 4096 Feb 17 09:33 modalias
-r--r--r-- 1 root root 4096 Jan  1  1970 name
lrwxrwxrwx 1 root root    0 Feb 17 09:33 of_node -> ../../../../../../firmware/devicetree/base/bus@5a000000/i2c@5a820000/eeprom@50
drwxr-xr-x 2 root root    0 Feb 17 09:33 power
lrwxrwxrwx 1 root root    0 Jan  1  1970 subsystem -> ../../../../../../bus/i2c
-r--r--r-- 1 root root 4096 Feb 17 09:33 suppliers
-rw-r--r-- 1 root root 4096 Jan  1  1970 uevent
apalis-imx8-06852506:~$ ls -la /sys/class/i2c-dev/i2c-4/device/4-005f
total 0
drwxr-xr-x 3 root root    0 Jan  1  1970 .
drwxr-xr-x 8 root root    0 Jan  1  1970 ..
-r--r--r-- 1 root root 4096 Feb 17 09:29 consumers
-r--r--r-- 1 root root 4096 Feb 17 09:29 modalias
-r--r--r-- 1 root root 4096 Jan  1  1970 name
lrwxrwxrwx 1 root root    0 Feb 17 09:29 of_node -> ../../../../../../firmware/devicetree/base/bus@5a000000/i2c@5a820000/hts221@5f
drwxr-xr-x 2 root root    0 Feb 17 09:29 power
lrwxrwxrwx 1 root root    0 Jan  1  1970 subsystem -> ../../../../../../bus/i2c
-r--r--r-- 1 root root 4096 Feb 17 09:29 suppliers
-rw-r--r-- 1 root root 4096 Jan  1  1970 uevent
apalis-imx8-06852506:~$ ls -la /sys/class/i2c-dev/i2c-4/device/4-0044
total 0
drwxr-xr-x 3 root root    0 Jan  1  1970 .
drwxr-xr-x 8 root root    0 Jan  1  1970 ..
-r--r--r-- 1 root root 4096 Feb 17 09:37 consumers
-r--r--r-- 1 root root 4096 Feb 17 09:37 modalias
-r--r--r-- 1 root root 4096 Jan  1  1970 name
lrwxrwxrwx 1 root root    0 Feb 17 09:37 of_node -> ../../../../../../firmware/devicetree/base/bus@5a000000/i2c@5a820000/opt3001@44
drwxr-xr-x 2 root root    0 Feb 17 09:37 power
lrwxrwxrwx 1 root root    0 Jan  1  1970 subsystem -> ../../../../../../bus/i2c
-r--r--r-- 1 root root 4096 Feb 17 09:37 suppliers
-rw-r--r-- 1 root root 4096 Jan  1  1970 uevent

driver is assigned for already present eeprom but not for added hts221 and opt3001.
Do we have to add driver as a module and not in device tree?

In the device tree you use the compatible property to reference a driver to be used in that node. However this action does not enable drivers. You need to rebuild the Linux Kernel with this driver enabled if you want to make use of it. The reason this works for the EEPROM is because the driver is already enabled and present in our Linux kernel. This driver for the light sensor is not.

Best Regards,
Jeremias

Hello @jeremias.tx
Can you give some idea about how to rebuild the kernel with driver enabled. How to enable the driver?

You can build the kernel yourself and customize it as shown here: https://developer.toradex.com/knowledge-base/build-u-boot-and-linux-kernel-from-source-code

Or, since this particular driver is in in the Linux kernel, I could ask the team to enable it by default. However this isn’t guaranteed and it may take some time before it does get enabled by our team.

Best Regards,
Jeremias