Page 1 of 1

Adding a new RTC to i2c-rtc-overlay.dts

Posted: Wed Jul 05, 2017 2:31 pm
by oscargomezf
Hi everyone,

I'm trying to add new RTC driver, the ST41M62. I've added this code to the dts file i2c-rtc-overlay.dts:

Code: Select all

/dts-v1/;

/ {
	compatible = "brcm,bcm2708";

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

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

			[email protected] {
				compatible = "maxim,ds1307";
				reg = <0x68>;
				status = "disable";
				linux,phandle = <0x1>;
				phandle = <0x1>;
			};

			[email protected] {
				compatible = "maxim,ds3231";
				reg = <0x68>;
				status = "disable";
				linux,phandle = <0x2>;
				phandle = <0x2>;
			};

			[email protected] {
				compatible = "microchip,mcp7941x";
				reg = <0x6f>;
				status = "disable";
				linux,phandle = <0x3>;
				phandle = <0x3>;
			};

			[email protected] {
				compatible = "nxp,pcf2127";
				reg = <0x51>;
				status = "disable";
				linux,phandle = <0x4>;
				phandle = <0x4>;
			};

			[email protected] {
				compatible = "nxp,pcf8523";
				reg = <0x68>;
				status = "disable";
				linux,phandle = <0x5>;
				phandle = <0x5>;
			};

			[email protected] {
				compatible = "nxp,pcf8563";
				reg = <0x51>;
				status = "disable";
				linux,phandle = <0x6>;
				phandle = <0x6>;
			};

			[email protected] {
				compatible = "st,m41t62";
				reg = <0x68>;
				status = "disable";
				linux,phandle = <0x7>;
				phandle = <0x7>;
			};
		};
	};

	__overrides__ {
		ds1307 = [00 00 00 01 73 74 61 74 75 73 00];
		ds3231 = [00 00 00 02 73 74 61 74 75 73 00];
		mcp7941x = [00 00 00 03 73 74 61 74 75 73 00];
		pcf2127 = [00 00 00 04 73 74 61 74 75 73 00];
		pcf8523 = [00 00 00 05 73 74 61 74 75 73 00];
		pcf8563 = [00 00 00 06 73 74 61 74 75 73 00];
		m41t62 = [00 00 00 07 73 74 61 74 75 73 00];
	};

	__symbols__ {
		ds1307 = "/[email protected]/__overlay__/[email protected]";
		mcp7941x = "/[email protected]/__overlay__/[email protected]";
		ds3231 = "/[email protected]/__overlay__/[email protected]";
		pcf2127 = "/[email protected]/__overlay__/[email protected]";
		pcf8523 = "/[email protected]/__overlay__/[email protected]";
		pcf8563 = "/[email protected]/__overlay__/[email protected]";
		m41t62 = "/[email protected]/__overlay__/[email protected]";
	};

	__fixups__ {
		i2c_arm = "/[email protected]:target:0";
	};

	__local_fixups__ {
		fixup = "/__overrides__:ds1307:0", "/__overrides__:ds3231:0", "/__overrides__:mcp7941x:0", "/__overrides__:pcf2127:0", "/__overrides__:pcf8523:0", "/__overrides__:pcf8563:0", "/__overrides__:m41t62:0";
	};
};
And I had to add the line to the config.txt:
dtoverlay=i2c-rtc,m41t62
The driver is working fine, but the RTC has an initial value that it seems to be wrong:

Code: Select all

# hwclock -r
hwclock: RTC_RD_TIME: Invalid argument
After introducing these commands, it works fine:

Code: Select all

# date
Thu Jan  1 00:00:31 UTC 1970
# hwclock -r
hwclock: RTC_RD_TIME: Invalid argument
# hwclock -w
# hwclock -r
Wed Dec 31 23:59:59 1969  0.000000 seconds
# date -s "2017-07-05 15:48:00"
Wed Jul  5 15:48:00 UTC 2017
# hwclock -w
# hwclock -r
Wed Jul  5 15:48:03 2017  0.000000 seconds
Does anyone know if we can fix it from the dts file?

Best reagrds.

Re: Adding a new RTC to i2c-rtc-overlay.dts

Posted: Wed Jul 05, 2017 2:41 pm
by PhilE
Device Tree just declares that some hardware exists, with enough information to allow the kernel to locate a driver. You are expecting it to declare that once we have an accurate time source to program the RTC, which is a much higher level operation - it just doesn't fit the DT model.

If the "invalid argument" error only occurs the first time you use the RTC then I think you are going to have to go through a commissioning process that sets the clock. If instead it is occurring every boot then that suggests that either the RTC isn't holding its value (no battery backup) or that the content is being corrupted. Either way this is no longer a DT problem and you would be better off asking on a different forum - Interfacing or Advanced Users, perhaps.

Re: Adding a new RTC to i2c-rtc-overlay.dts

Posted: Wed Jul 05, 2017 3:14 pm
by oscargomezf
Hi PhilE,

First of all, thank you for your help and your quick reply.

Do you know what means this part of the .dts file? Probably, they are init values for the RTC, but I couldn't find any documentation about that:
...
__overrides__ {
ds1307 = [00 00 00 01 73 74 61 74 75 73 00];
ds3231 = [00 00 00 02 73 74 61 74 75 73 00];
mcp7941x = [00 00 00 03 73 74 61 74 75 73 00];
pcf2127 = [00 00 00 04 73 74 61 74 75 73 00];
pcf8523 = [00 00 00 05 73 74 61 74 75 73 00];
pcf8563 = [00 00 00 06 73 74 61 74 75 73 00];
m41t62 = [00 00 00 07 73 74 61 74 75 73 00];
};
...
Best regards.

Re: Adding a new RTC to i2c-rtc-overlay.dts

Posted: Wed Jul 05, 2017 3:18 pm
by PhilE
It makes more sense if you read the original source - compiling and decompiling is a lossy process:

Code: Select all

	__overrides__ {
		abx80x = <0>,"+0";
		ds1307 = <0>,"+1";
		ds1339 = <0>,"+2";
		ds3231 = <0>,"+3";
		mcp7940x = <0>,"+4";
		mcp7941x = <0>,"+5";
		pcf2127 = <0>,"+6";
		pcf8523 = <0>,"+7";
		pcf8563 = <0>,"+8";
		trickle-diode-type = <&abx80x>,"abracon,tc-diode";
		trickle-resistor-ohms = <&ds1339>,"trickle-resistor-ohms:0",
					<&abx80x>,"abracon,tc-resistor";
		wakeup-source = <&ds1339>,"wakeup-source?",
				<&ds3231>,"wakeup-source?",
				<&mcp7940x>,"wakeup-source?",
				<&mcp7941x>,"wakeup-source?";
	};
The overrides mechanism is documented here.

Re: Adding a new RTC to i2c-rtc-overlay.dts

Posted: Tue Jul 25, 2017 12:51 am
by ericcooper
I was just about to ask about adding support for the M41T62, glad to see you've already done it.

In addition to reading and writing the RTC with hwclock, I also want to program its interrupt (to fire at a given interval or absolute time in the future). Is that supported using this overlay (presumably through /dev/rtc), and if not, where should I look for pointers on how to add that?

Re: Adding a new RTC to i2c-rtc-overlay.dts

Posted: Tue Aug 01, 2017 4:35 pm
by ericcooper
Answering my own question: the current rtc-m41t80 driver doesn't support the "wakealarm" entry in /sys/class/rtc/. I submitted a small patch to the driver to enable this when the "wakeup-source" property is specified along with "m41t62". Then you can write the time in seconds to that file and the alarm interrupt will occur at that time.

Re: Adding a new RTC to i2c-rtc-overlay.dts

Posted: Fri Oct 20, 2017 1:10 pm
by djlegge
I don't seem to be able to get any 'wakealarm' entry - should this be available now ?
I'm using the very latest raspbian stretch, used rpi-update to get the latest kernel :
Linux raspberrypi 4.9.57+ #1045 Fri Oct 20 11:38:56 BST 2017 armv6l GNU/Linux

In config.txt I have tried many different combinations but currently this :
dtoverlay=i2c-rtc,m41t62
dtparam=wakeup-source

I get /sys/class/rtc/rtc0 and everything works fine - just nothing called wakealarm.

I'm probably doing something wrong so any pointers would be nice....
Thanks.

Re: Adding a new RTC to i2c-rtc-overlay.dts

Posted: Sat Nov 04, 2017 9:55 pm
by ericcooper
I don't think that patch has made it into the RPi kernel yet. But it's in linux-next and Linus has pulled it into his tree, so should be out soon. It's here if you want to cherry-pick it and build your own kernel: https://github.com/torvalds/linux/commi ... bd628f884f