oscargomezf
Posts: 23
Joined: Thu Mar 10, 2016 3:46 pm

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

Wed Jul 05, 2017 2:31 pm

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";

	fragment@0 {
		target = <0xdeadbeef>;

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

			ds1307@68 {
				compatible = "maxim,ds1307";
				reg = <0x68>;
				status = "disable";
				linux,phandle = <0x1>;
				phandle = <0x1>;
			};

			ds3231@68 {
				compatible = "maxim,ds3231";
				reg = <0x68>;
				status = "disable";
				linux,phandle = <0x2>;
				phandle = <0x2>;
			};

			mcp7941x@6f {
				compatible = "microchip,mcp7941x";
				reg = <0x6f>;
				status = "disable";
				linux,phandle = <0x3>;
				phandle = <0x3>;
			};

			pcf2127@51 {
				compatible = "nxp,pcf2127";
				reg = <0x51>;
				status = "disable";
				linux,phandle = <0x4>;
				phandle = <0x4>;
			};

			pcf8523@68 {
				compatible = "nxp,pcf8523";
				reg = <0x68>;
				status = "disable";
				linux,phandle = <0x5>;
				phandle = <0x5>;
			};

			pcf8563@51 {
				compatible = "nxp,pcf8563";
				reg = <0x51>;
				status = "disable";
				linux,phandle = <0x6>;
				phandle = <0x6>;
			};

			m41t62@68 {
				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 = "/fragment@0/__overlay__/ds1307@68";
		mcp7941x = "/fragment@0/__overlay__/mcp7941x@6f";
		ds3231 = "/fragment@0/__overlay__/ds3231@68";
		pcf2127 = "/fragment@0/__overlay__/pcf2127@51";
		pcf8523 = "/fragment@0/__overlay__/pcf8523@68";
		pcf8563 = "/fragment@0/__overlay__/pcf8563@51";
		m41t62 = "/fragment@0/__overlay__/m41t62@68";
	};

	__fixups__ {
		i2c_arm = "/fragment@0: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.

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

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

Wed Jul 05, 2017 2:41 pm

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.

oscargomezf
Posts: 23
Joined: Thu Mar 10, 2016 3:46 pm

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

Wed Jul 05, 2017 3:14 pm

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.

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

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

Wed Jul 05, 2017 3:18 pm

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.

ericcooper
Posts: 63
Joined: Sat Apr 08, 2017 6:23 pm

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

Tue Jul 25, 2017 12:51 am

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?

ericcooper
Posts: 63
Joined: Sat Apr 08, 2017 6:23 pm

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

Tue Aug 01, 2017 4:35 pm

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.

djlegge
Posts: 11
Joined: Tue Jul 07, 2015 1:55 pm

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

Fri Oct 20, 2017 1:10 pm

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.

ericcooper
Posts: 63
Joined: Sat Apr 08, 2017 6:23 pm

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

Sat Nov 04, 2017 9:55 pm

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

Return to “Device Tree”

Who is online

Users browsing this forum: No registered users and 1 guest