knightstreet
Posts: 9
Joined: Fri May 13, 2016 7:32 am

mcp9808 sensor device tree entry

Thu Sep 13, 2018 10:01 am

Hi,

I'm attempting to modify my pi device tree to detect the mcp9808 temperature sensor. Ihave successfully done the procedure manually as follows:

Jc42 driver is a module, so modprobe it:

Code: Select all

modprobe jc42
Then add my address 18 device:

Code: Select all

echo jc42 0x18 > /sys/bus/i2c/devices/i2c-0/new_device
Verify all is ok in dmesg:

Code: Select all

[  116.054090] i2c i2c-0: new_device: Instantiated device jc42 at 0x18
Verify module is in control of device:

i2cdetect -y 0

Code: Select all

# i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- 45 -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- 64 -- -- -- -- UU -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
Read my temperature:

Code: Select all

cat /sys/devices/platform/soc/3f205000.i2c/i2c-0/0-0018/hwmon/hwmon0/temp1_input
All ok.

So I'd like to get my device tree to do this for me automagically on boot. I need an entry in my device tree which makes it compatible with the microchip mcp9808

jc42 does have this device and can recognise it by name but I'm not sure what my tree entry should be, my effort is as follows:

Code: Select all

/dts-v1/;

/ {
	compatible = "brcm,bcm2708";

	[email protected] {
		target = <0xffffffff>;

		__overlay__ {
			#address-cells = <0x1>;
			#size-cells = <0x0>;
			status = "okay";

			[email protected] {
				compatible = "microchip,mcp9808";
				reg = <0x18>;
			};
		};
	};

};
I'm not sure about the compatible = "microchip... bit how does it associate witj the jc42 driver?

Cheers,

Chris.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1814
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 10:12 am

Although I2C drivers can have full compatible strings, most don't. Instead the second part of the compatible string (after the comma) is looked up as an I2C driver name - the first part is ignored.

In your case this would look like (I think - this is untested):

Code: Select all

/dts-v1/;

/ {
	compatible = "brcm,bcm2708";

	[email protected] {
		target = <&i2c0>;

		__overlay__ {
			#address-cells = <0x1>;
			#size-cells = <0x0>;
			status = "okay";

			[email protected] {
				compatible = "microchip,jc42";
				reg = <0x18>;
			};
		};
	};

};
Note that I also replaced the target with a reference to the label on the i2c0 node. The 0xffffffff you used is intended to be overwritten by the loader after resolving the label references, but in your case the necessary information is missing from the overlay. In general it is better to locate the source of an overlay if you want to make changes to it, rather than decompile and reverse engineer it.

knightstreet
Posts: 9
Joined: Fri May 13, 2016 7:32 am

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 10:26 am

Hi Phil,

Thanks for the reply, much appreciated.

I get an error on compilation of the dts on it's own for testing -

Code: Select all

ERROR (phandle_references): Reference to non-existent node or label "i2c0"
I get what the error is saying - it doesn't have the alias for i2c0, what's the best way to correct this?

Cheers,

Chris.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1814
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 10:31 am

You need to tell the dtc compiler to generate symbol information for unresolved symbols, rather than reporting an error. That is what the '[email protected]' command line parameter does:

Code: Select all

[email protected]:~$ dtc [email protected] -I dts -O dtb -o mcp9808.dtbo mcp9808-overlay.dts
You may still see some warnings from dtc, but these are expected (it still doesn't properly understand the slightly odd overlay syntax).

knightstreet
Posts: 9
Joined: Fri May 13, 2016 7:32 am

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 10:40 am

Thanks Phil - I think I need to properly digest the dtc rules and regulations :geek:

knightstreet
Posts: 9
Joined: Fri May 13, 2016 7:32 am

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 11:08 am

The @ symbol didn't fix the error unfortunately.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1814
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 11:10 am

Copy and paste your complete command line (no retyping).

knightstreet
Posts: 9
Joined: Fri May 13, 2016 7:32 am

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 11:13 am

Code: Select all

# dtc [email protected] -I dts -O dtb -o mcp9808.dtbo mcp9808-overlay.dts

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1814
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 11:22 am

You're right - the overlay is missing the "/plugin/;" tag at the top, beneath "/dts-v1/;". This tag is actually what tells the compiler to allow unresolved symbols, but without the "[email protected]" parameter it won't generate the necessary symbol and fixup information to be able to resolve the symbols, making it fairly useless unless both are used together. Go figure.

knightstreet
Posts: 9
Joined: Fri May 13, 2016 7:32 am

Re: mcp9808 sensor device tree entry

Thu Sep 13, 2018 11:31 am

Nice one, always good to know why - I re-did the dtc and got the mistake just now. Thanks for taking the time to have a look. My complete file is:

Code: Select all

/dts-v1/;
/plugin/;

/ {
	compatible = "brcm,bcm2708", "brcm,bcm2709";

	[email protected] {
		target = <&i2c0>;
		__overlay__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";
			[email protected] {
				compatible = "microchip,jc42";
				reg = <0x18>;
			};
		};
	};
};

Return to “Device Tree”

Who is online

Users browsing this forum: No registered users and 2 guests