Conner Labs
Posts: 45
Joined: Fri Jan 11, 2013 2:45 pm
Location: Glasgow
Contact: Website

Using RTC with non-standard I2C port

Wed Jun 28, 2017 3:57 pm

Hi all

I have a PCF2127 RTC chip connected to my Compute Module 3. I am running the latest Raspbian Jessie Lite official image.

I am using the i2c-gpio overlay to drive it through a software emulated I2C port, to save the real I2C port for something more important.

If I use dtoverlay=i2c-rtc,pcf2127, it only looks for the RTC on i2c-1, and fails, as the emulated I2C port is i2c-3.

If I remove the dtoverlay line, and issue the following commands:
modprobe i2c-dev
modprobe rtc-pcf2127
echo pcf2127 0x51 >> /sys/bus/i2c/devices/i2c-3/new_device;

the driver loads successfully and the hwclock command talks to the RTC chip as it should.

But I can't figure out which file to put these commands in to make it work automatically at boot time. I also can't figure out how to make the overlay work with i2c-3.

Any help appreciated.

Steve Conner

DirkS
Posts: 9937
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Using RTC with non-standard I2C port

Wed Jun 28, 2017 4:46 pm

I am using the i2c-gpio overlay to drive it through a software emulated I2C port, to save the real I2C port for something more important.
Why? Do you see problems when you use the RTC on i2c1?

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

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 8:28 am

Rather like the other current question on using an ads7846 on spi1, this shows up a hole in the DT overlay mechanism - it is almost impossible to parameterise an overlay for different I2C or SPI busses. For now you will have to copy the standard i2c-rtc overlay and modify it to reference i2c-3 instead. Actually it would be better if you make it an i2c-rtc-gpio overlay instead - a combination of i2c-gpio and i2c-rtc.

Conner Labs
Posts: 45
Joined: Fri Jan 11, 2013 2:45 pm
Location: Glasgow
Contact: Website

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 9:24 am

I had no problems using the RTC on i2c-1. It's just that our product has an array of data acquisition boards connected to i2c-1. I want a real hardware I2C port for these as it's faster, and I want the RTC on a different port so the RTC driver doesn't conflict with our data acquisition driver.

(I have no idea if they would in fact conflict, I just don't even want to go there, and I've already signed off on the PCB design.)

It worked fine on the CM1 which had 2 real I2C ports. :? On the CM3, i2c-0 is actually used by the GPU for something.

PhilE: I suspected this was the case. I am aiming to develop a script that customises the standard Jessie Lite image into the image for our product. So this means I need to make the script compile a DTB overlay, and have it break every time you change device tree, which seems to be quite often?

If I developed this i2c-rtc-gpio overlay and got it working, would you let me push it into the official distribution?

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

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 9:46 am

Yes of course - and I'm happy to help if you get stuck.

Conner Labs
Posts: 45
Joined: Fri Jan 11, 2013 2:45 pm
Location: Glasgow
Contact: Website

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 12:04 pm

OK, so I tried what seemed to me the obvious approach. I got the relevant .dts files from the 4.9.y branch on github, pasted the contents of i2c-rtc-overlay.dts and i2c-gpio-overlay.dts together into a file called i2c-gpio-rtc-overlay.dts, replaced "i2c_arm" for "i2c_gpio", and changed the fragment numbers about.

And... it worked :D

How would I go about pushing this upstream?

Code: Select all

// Definitions for several I2C based Real Time Clocks
// Available through i2c-gpio
/dts-v1/;
/plugin/;

/ {
	compatible = "brcm,bcm2708";

  [email protected] {
		target-path = "/";
		__overlay__ {
			i2c_gpio: [email protected] {
				compatible = "i2c-gpio";
				gpios = <&gpio 23 0 /* sda */
					 &gpio 24 0 /* scl */
					>;
				i2c-gpio,delay-us = <2>;        /* ~100 kHz */
				#address-cells = <1>;
				#size-cells = <0>;
			};
		};
	};

	[email protected] {
		target-path = "/aliases";
		__overlay__ {
			i2c_gpio = "/[email protected]";
		};
	};

	[email protected] {
		target-path = "/__symbols__";
		__overlay__ {
			i2c_gpio = "/[email protected]";
		};
	};



	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			abx80x: [email protected] {
				compatible = "abracon,abx80x";
				reg = <0x69>;
				abracon,tc-diode = "standard";
				abracon,tc-resistor = <0>;
				status = "okay";
			};
		};
	};

	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			ds1307: [email protected] {
				compatible = "maxim,ds1307";
				reg = <0x68>;
				status = "okay";
			};
		};
	};

	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			ds1339: [email protected] {
				compatible = "dallas,ds1339";
				trickle-resistor-ohms = <0>;
				reg = <0x68>;
				status = "okay";
			};
		};
	};

	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			ds3231: [email protected] {
				compatible = "maxim,ds3231";
				reg = <0x68>;
				status = "okay";
			};
		};
	};

	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			mcp7940x: [email protected] {
				compatible = "microchip,mcp7940x";
				reg = <0x6f>;
				status = "okay";
			};
		};
	};

	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			mcp7941x: [email protected] {
				compatible = "microchip,mcp7941x";
				reg = <0x6f>;
				status = "okay";
			};
		};
	};

	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			pcf2127: [email protected] {
				compatible = "nxp,pcf2127";
				reg = <0x51>;
				status = "okay";
			};
		};
	};

	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			pcf8523: [email protected] {
				compatible = "nxp,pcf8523";
				reg = <0x68>;
				status = "okay";
			};
		};
	};

	[email protected] {
		target = <&i2c_gpio>;
		__dormant__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";

			pcf8563: [email protected] {
				compatible = "nxp,pcf8563";
				reg = <0x51>;
				status = "okay";
			};
		};
	};

	__overrides__ {
		abx80x = <0>,"+3";
		ds1307 = <0>,"+4";
		ds1339 = <0>,"+5";
		ds3231 = <0>,"+6";
		mcp7940x = <0>,"+7";
		mcp7941x = <0>,"+8";
		pcf2127 = <0>,"+9";
		pcf8523 = <0>,"+10";
		pcf8563 = <0>,"+11";
		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?";
    i2c_gpio_sda = <&i2c_gpio>,"gpios:4";
    i2c_gpio_scl = <&i2c_gpio>,"gpios:16";
    i2c_gpio_delay_us = <&i2c_gpio>,"i2c-gpio,delay-us:0";
	};
};

This compiled ok (I'm a Real Programmer, warnings don't count ;) )

Code: Select all

[email protected]:~/i2c-gpio-rtc $ dtc [email protected] -I dts -O dtb -o i2c-gpio-rtc.dtbo i2c-gpio-rtc-overlay.dts
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected]/__overlay__/[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property
I instantiated it with

Code: Select all

dtoverlay=i2c-gpio-rtc,pcf2127,i2c_gpio_sda=5,i2c_gpio_scl=6

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

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 12:15 pm

Nice job. The correct way to get it into the kernel is to create a Pull Request in the raspberrypi/linux GitHub repo:
https://github.com/raspberrypi/linux/pulls

Using the GitHub GUI you will be able to create new files and modify others, then turn those commits into a Pull Request. The overlay sources belong in: https://github.com/raspberrypi/linux/tr ... s/overlays
You'll also need to edit the Makefile and README in that directory. Be careful with alphabetical ordering and formatting of the README file - no TABs, same indents, two blank lines between entries.

If that sounds too much, create an issue instead (https://github.com/raspberrypi/linux/issues), including or linking to your overlay and asking for it to be included. Either way, it will be answered by my GH alter ego.

Conner Labs
Posts: 45
Joined: Fri Jan 11, 2013 2:45 pm
Location: Glasgow
Contact: Website

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 12:25 pm

I'll give it a go thanks. What branch should I be raising the pull request on? rpi-4.9.y?

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

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 12:26 pm

Yes - that's what we're currently building against. I can take care of merging into the newer branches.

Conner Labs
Posts: 45
Joined: Fri Jan 11, 2013 2:45 pm
Location: Glasgow
Contact: Website

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 1:30 pm

OK, I've submitted the pull request. I don't normally use Git so hopefully I haven't done anything too dreadful.

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

Re: Using RTC with non-standard I2C port

Thu Jun 29, 2017 1:45 pm

No, it looks good. I have some relatively minor feedback, but once that's addressed it can be merged.

unclef1
Posts: 1
Joined: Mon Aug 19, 2019 10:04 am

Re: Using RTC with non-standard I2C port

Mon Aug 19, 2019 10:24 am

Hi.
I have a similar problem.
DS1307 on USB-I2C Tiny converter I2C-3. How can i use overlay on I2C-3?

Return to “Device Tree”