help with sxitwi on allwinner H3

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

help with sxitwi on allwinner H3

Stephen Graf
I am trying to get an i2c driver working on orange pi one (H3).
Reading the H3 datasheet would lead me to believe that the switwi driver
should work.  The H3 data is the same except for 2 additional extended
feature registers that should not be needed.

I modified the dtb and the sxitwi driver to set up H3 compatibility, rebuilt
the kernel and got the following in the dmesg:

sxitwi0 at simplebus0sxiccmu_ccu_enable: 0x0000003b

iic0 at sxitwi0
sxitwi1 at simplebus0sxiccmu_ccu_enable: 0x0000003c

iic1 at sxitwi1
sxitwi2 at simplebus0sxiccmu_ccu_enable: 0x0000003d

iic2 at sxitwi2

Now I am stuck trying to write some code to test the driver. I have a device
on the i2c bus iic1 at address 76, a bme280 temperature, pressure and
humidity sensor.
I am not an accomplished c programmer and would dearly like to see an
example of using iic to read and write a device on the i2c bus.

Note: I do not see nor can create iicx or switwi devices.  

Reply | Threaded
Open this post in threaded view
|

Re: help with sxitwi on allwinner H3

Patrick Wildt-3
On Wed, Sep 13, 2017 at 10:50:28AM -0700, Stephen Graf wrote:

> I am trying to get an i2c driver working on orange pi one (H3).
> Reading the H3 datasheet would lead me to believe that the switwi driver
> should work.  The H3 data is the same except for 2 additional extended
> feature registers that should not be needed.
>
> I modified the dtb and the sxitwi driver to set up H3 compatibility, rebuilt
> the kernel and got the following in the dmesg:
>
> sxitwi0 at simplebus0sxiccmu_ccu_enable: 0x0000003b
>
> iic0 at sxitwi0
> sxitwi1 at simplebus0sxiccmu_ccu_enable: 0x0000003c
>
> iic1 at sxitwi1
> sxitwi2 at simplebus0sxiccmu_ccu_enable: 0x0000003d
>
> iic2 at sxitwi2
>
> Now I am stuck trying to write some code to test the driver. I have a device
> on the i2c bus iic1 at address 76, a bme280 temperature, pressure and
> humidity sensor.
> I am not an accomplished c programmer and would dearly like to see an
> example of using iic to read and write a device on the i2c bus.
>
> Note: I do not see nor can create iicx or switwi devices.  
>

Looks like you need to implement support for H3's I2C clock in
sxiccmu, so it's powered on properly.  That could help.  Could.

Patrick

Reply | Threaded
Open this post in threaded view
|

Re: help with sxitwi on allwinner H3

Artturi Alm
In reply to this post by Stephen Graf
On Wed, Sep 13, 2017 at 10:50:28AM -0700, Stephen Graf wrote:

> I am trying to get an i2c driver working on orange pi one (H3).
> Reading the H3 datasheet would lead me to believe that the switwi driver
> should work.  The H3 data is the same except for 2 additional extended
> feature registers that should not be needed.
>
> I modified the dtb and the sxitwi driver to set up H3 compatibility, rebuilt
> the kernel and got the following in the dmesg:
>
> sxitwi0 at simplebus0sxiccmu_ccu_enable: 0x0000003b
>
> iic0 at sxitwi0
> sxitwi1 at simplebus0sxiccmu_ccu_enable: 0x0000003c
>
> iic1 at sxitwi1
> sxitwi2 at simplebus0sxiccmu_ccu_enable: 0x0000003d
>
> iic2 at sxitwi2
>
> Now I am stuck trying to write some code to test the driver. I have a device
> on the i2c bus iic1 at address 76, a bme280 temperature, pressure and
> humidity sensor.
> I am not an accomplished c programmer and would dearly like to see an
> example of using iic to read and write a device on the i2c bus.
>
> Note: I do not see nor can create iicx or switwi devices.  
>

Hi,

after you've finished what patrick@ wrote about, you might want to take
a look at i/sys/dev/fdt/axp20x.c, it could very well be the simplest
driver on iic-bus in tree, and consequently it does attach to sxitwi* :)

-Artturi

Reply | Threaded
Open this post in threaded view
|

Re: help with sxitwi on allwinner H3

Artturi Alm
On Wed, Sep 13, 2017 at 09:23:05PM +0300, Artturi Alm wrote:

> On Wed, Sep 13, 2017 at 10:50:28AM -0700, Stephen Graf wrote:
> > I am trying to get an i2c driver working on orange pi one (H3).
> > Reading the H3 datasheet would lead me to believe that the switwi driver
> > should work.  The H3 data is the same except for 2 additional extended
> > feature registers that should not be needed.
> >
> > I modified the dtb and the sxitwi driver to set up H3 compatibility, rebuilt
> > the kernel and got the following in the dmesg:
> >
> > sxitwi0 at simplebus0sxiccmu_ccu_enable: 0x0000003b
> >
> > iic0 at sxitwi0
> > sxitwi1 at simplebus0sxiccmu_ccu_enable: 0x0000003c
> >
> > iic1 at sxitwi1
> > sxitwi2 at simplebus0sxiccmu_ccu_enable: 0x0000003d
> >
> > iic2 at sxitwi2
> >
> > Now I am stuck trying to write some code to test the driver. I have a device
> > on the i2c bus iic1 at address 76, a bme280 temperature, pressure and
> > humidity sensor.
> > I am not an accomplished c programmer and would dearly like to see an
> > example of using iic to read and write a device on the i2c bus.
> >
> > Note: I do not see nor can create iicx or switwi devices.  
> >
>
> Hi,
>
> after you've finished what patrick@ wrote about, you might want to take
> a look at i/sys/dev/fdt/axp20x.c, it could very well be the simplest
> driver on iic-bus in tree, and consequently it does attach to sxitwi* :)
>
> -Artturi

So i was bored for a while; could work, but i don't have hw to test w/..

-Artturi


diff --git a/sys/dev/fdt/sxiccmu_clocks.h b/sys/dev/fdt/sxiccmu_clocks.h
index 8b25ac42bd4..f82613e0125 100644
--- a/sys/dev/fdt/sxiccmu_clocks.h
+++ b/sys/dev/fdt/sxiccmu_clocks.h
@@ -95,6 +95,10 @@ struct sxiccmu_ccu_bit sun50i_a64_gates[] = {
 
 #define H3_CLK_BUS_PIO 54
 
+#define H3_CLK_BUS_I2C0 59
+#define H3_CLK_BUS_I2C1 60
+#define H3_CLK_BUS_I2C2 61
+
 #define H3_CLK_BUS_UART0 62
 #define H3_CLK_BUS_UART1 63
 #define H3_CLK_BUS_UART2 64
@@ -128,6 +132,9 @@ struct sxiccmu_ccu_bit sun8i_h3_gates[] = {
  [H3_CLK_BUS_OHCI2] = { 0x0060, 30 },
  [H3_CLK_BUS_OHCI3] = { 0x0060, 31 },
  [H3_CLK_BUS_PIO]   = { 0x0068, 5 },
+ [H3_CLK_BUS_I2C0] = { 0x006c, 0, H3_CLK_APB2 },
+ [H3_CLK_BUS_I2C1] = { 0x006c, 1, H3_CLK_APB2 },
+ [H3_CLK_BUS_I2C2] = { 0x006c, 2, H3_CLK_APB2 },
  [H3_CLK_BUS_UART0] = { 0x006c, 16, H3_CLK_APB2 },
  [H3_CLK_BUS_UART1] = { 0x006c, 17, H3_CLK_APB2 },
  [H3_CLK_BUS_UART2] = { 0x006c, 18, H3_CLK_APB2 },
@@ -195,6 +202,10 @@ struct sxiccmu_ccu_bit sun50i_a64_resets[] = {
 
 #define H3_RST_BUS_EPHY 39
 
+#define H3_RST_BUS_I2C0 46
+#define H3_RST_BUS_I2C1 47
+#define H3_RST_BUS_I2C2 48
+
 struct sxiccmu_ccu_bit sun8i_h3_resets[] = {
  [H3_RST_USB_PHY0] =  { 0x00cc, 0 },
  [H3_RST_USB_PHY1] =  { 0x00cc, 1 },
@@ -213,4 +224,7 @@ struct sxiccmu_ccu_bit sun8i_h3_resets[] = {
  [H3_RST_BUS_OHCI2] = { 0x02c0, 30 },
  [H3_RST_BUS_OHCI3] = { 0x02c0, 31 },
  [H3_RST_BUS_EPHY]  = { 0x02c8, 2 },
+ [H3_RST_BUS_I2C0] =  { 0x02d8, 0 },
+ [H3_RST_BUS_I2C1] =  { 0x02d8, 1 },
+ [H3_RST_BUS_I2C2] =  { 0x02d8, 2 },
 };
diff --git a/sys/dev/fdt/sxitwi.c b/sys/dev/fdt/sxitwi.c
index 260f6f1de57..d05f67150ed 100644
--- a/sys/dev/fdt/sxitwi.c
+++ b/sys/dev/fdt/sxitwi.c
@@ -178,8 +178,9 @@ sxitwi_match(struct device *parent, void *match, void *aux)
 {
  struct fdt_attach_args *faa = aux;
 
- return (OF_is_compatible(faa->fa_node, "allwinner,sun4i-a10-i2c") |
-    OF_is_compatible(faa->fa_node, "allwinner,sun7i-a20-i2c"));
+ return (OF_is_compatible(faa->fa_node, "allwinner,sun4i-a10-i2c") ||
+    OF_is_compatible(faa->fa_node, "allwinner,sun7i-a20-i2c") ||
+    OF_is_compatible(faa->fa_node, "allwinner,sun6i-a31-i2c"));
 }
 
 void

Reply | Threaded
Open this post in threaded view
|

Re: help with sxitwi on allwinner H3

Stephen Graf
In reply to this post by Stephen Graf
I looked at the sxiccmu and it seems to be set up for H3 in both the dtb and
driver.   There is a " sxiccmu1 at simplebus0" in dmesg (attached). What
does the message " sxiccmu_ccu_enable: 0x0000003b" in dmesg mean?

I would like to continue with the sxitwi driver as almost all the bits and
bytes in the driver match the H3 datasheet for TWI. The only exception I see
are two enhanced feature registers that I don't think will make any
difference.  The default is for the features to be off.

Can someone point me to code that does iic attach, detach, read and write
that I could use as an example to follow. I can't seem to get my head around
iic(9), setting up the parameters. Copying some existing code is much more
what I can handle.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of
Artturi Alm
Sent: Wednesday, September 13, 2017 11:23 AM
To: Stephen Graf <[hidden email]>
Cc: Patrick Wildt <[hidden email]>; [hidden email]
Subject: Re: help with sxitwi on allwinner H3

On Wed, Sep 13, 2017 at 10:50:28AM -0700, Stephen Graf wrote:

> I am trying to get an i2c driver working on orange pi one (H3).
> Reading the H3 datasheet would lead me to believe that the
> driver should work.  The H3 data is the same except for 2 additional
> extended feature registers that should not be needed.
>
> I modified the dtb and the sxitwi driver to set up H3 compatibility,
> rebuilt the kernel and got the following in the dmesg:
>
> sxitwi0 at simplebus0sxiccmu_ccu_enable: 0x0000003b
>
> iic0 at sxitwi0
> sxitwi1 at simplebus0sxiccmu_ccu_enable: 0x0000003c
>
> iic1 at sxitwi1
> sxitwi2 at simplebus0sxiccmu_ccu_enable: 0x0000003d
>
> iic2 at sxitwi2
>
> Now I am stuck trying to write some code to test the driver. I have a
> device on the i2c bus iic1 at address 76, a bme280 temperature,
> pressure and humidity sensor.
> I am not an accomplished c programmer and would dearly like to see an
> example of using iic to read and write a device on the i2c bus.
>
> Note: I do not see nor can create iicx or switwi devices.  
>
Hi,

after you've finished what patrick@ wrote about, you might want to take a
look at i/sys/dev/fdt/axp20x.c, it could very well be the simplest driver on
iic-bus in tree, and consequently it does attach to sxitwi* :)

-Artturi


dmesg.txt.txt (2K) Download Attachment
dts.txt (32K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: help with sxitwi on allwinner H3

Stephen Graf
In reply to this post by Artturi Alm
Thank you for the changes.  That is not something I could have managed.
Attached is the dmesg with the new kernel.

Now I need to test.  I have a device on i2c1 at address 76.  Is there any
sample code I can copy to read it?

-----Original Message-----
From: Artturi Alm [mailto:[hidden email]]
Sent: Wednesday, September 13, 2017 1:05 PM
To: Stephen Graf <[hidden email]>
Subject: Re: help with sxitwi on allwinner H3

On Wed, Sep 13, 2017 at 12:59:05PM -0700, Stephen Graf wrote:

> Thank you.  I will give it a try.
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Artturi Alm
> Sent: Wednesday, September 13, 2017 12:35 PM
> To: Stephen Graf <[hidden email]>
> Cc: [hidden email]
> Subject: Re: help with sxitwi on allwinner H3
>
> On Wed, Sep 13, 2017 at 09:23:05PM +0300, Artturi Alm wrote:
> > On Wed, Sep 13, 2017 at 10:50:28AM -0700, Stephen Graf wrote:
> > > I am trying to get an i2c driver working on orange pi one (H3).
> > > Reading the H3 datasheet would lead me to believe that the switwi
> > > driver should work.  The H3 data is the same except for 2
> > > additional extended feature registers that should not be needed.
> > >
> > > I modified the dtb and the sxitwi driver to set up H3
> > > compatibility, rebuilt the kernel and got the following in the dmesg:
> > >
> > > sxitwi0 at simplebus0sxiccmu_ccu_enable: 0x0000003b
> > >
> > > iic0 at sxitwi0
> > > sxitwi1 at simplebus0sxiccmu_ccu_enable: 0x0000003c
> > >
> > > iic1 at sxitwi1
> > > sxitwi2 at simplebus0sxiccmu_ccu_enable: 0x0000003d
> > >
> > > iic2 at sxitwi2
> > >
> > > Now I am stuck trying to write some code to test the driver. I
> > > have a device on the i2c bus iic1 at address 76, a bme280
> > > temperature, pressure and humidity sensor.
> > > I am not an accomplished c programmer and would dearly like to see
> > > an example of using iic to read and write a device on the i2c bus.
> > >
> > > Note: I do not see nor can create iicx or switwi devices.  
> > >
> >
> > Hi,
> >
> > after you've finished what patrick@ wrote about, you might want to
> > take a look at i/sys/dev/fdt/axp20x.c, it could very well be the
> > simplest driver on iic-bus in tree, and consequently it does attach
> > to
> > sxitwi* :)
> >
> > -Artturi
>
> So i was bored for a while; could work, but i don't have hw to test w/..
>
> -Artturi
>
>
> diff --git a/sys/dev/fdt/sxiccmu_clocks.h
> b/sys/dev/fdt/sxiccmu_clocks.h index 8b25ac42bd4..f82613e0125 100644
> --- a/sys/dev/fdt/sxiccmu_clocks.h
> +++ b/sys/dev/fdt/sxiccmu_clocks.h
> @@ -95,6 +95,10 @@ struct sxiccmu_ccu_bit sun50i_a64_gates[] = {
>  
>  #define H3_CLK_BUS_PIO 54
>  
> +#define H3_CLK_BUS_I2C0 59
> +#define H3_CLK_BUS_I2C1 60
> +#define H3_CLK_BUS_I2C2 61
> +
>  #define H3_CLK_BUS_UART0 62
>  #define H3_CLK_BUS_UART1 63
>  #define H3_CLK_BUS_UART2 64
> @@ -128,6 +132,9 @@ struct sxiccmu_ccu_bit sun8i_h3_gates[] = {
>   [H3_CLK_BUS_OHCI2] = { 0x0060, 30 },
>   [H3_CLK_BUS_OHCI3] = { 0x0060, 31 },
>   [H3_CLK_BUS_PIO]   = { 0x0068, 5 },
> + [H3_CLK_BUS_I2C0] = { 0x006c, 0, H3_CLK_APB2 },
> + [H3_CLK_BUS_I2C1] = { 0x006c, 1, H3_CLK_APB2 },
> + [H3_CLK_BUS_I2C2] = { 0x006c, 2, H3_CLK_APB2 },
>   [H3_CLK_BUS_UART0] = { 0x006c, 16, H3_CLK_APB2 },
>   [H3_CLK_BUS_UART1] = { 0x006c, 17, H3_CLK_APB2 },
>   [H3_CLK_BUS_UART2] = { 0x006c, 18, H3_CLK_APB2 }, @@ -195,6 +202,10
> @@ struct sxiccmu_ccu_bit sun50i_a64_resets[] = {
>  
>  #define H3_RST_BUS_EPHY 39
>  
> +#define H3_RST_BUS_I2C0 46
> +#define H3_RST_BUS_I2C1 47
> +#define H3_RST_BUS_I2C2 48
> +
>  struct sxiccmu_ccu_bit sun8i_h3_resets[] = {
>   [H3_RST_USB_PHY0] =  { 0x00cc, 0 },
>   [H3_RST_USB_PHY1] =  { 0x00cc, 1 },
> @@ -213,4 +224,7 @@ struct sxiccmu_ccu_bit sun8i_h3_resets[] = {
>   [H3_RST_BUS_OHCI2] = { 0x02c0, 30 },
>   [H3_RST_BUS_OHCI3] = { 0x02c0, 31 },
>   [H3_RST_BUS_EPHY]  = { 0x02c8, 2 },
> + [H3_RST_BUS_I2C0] =  { 0x02d8, 0 },
> + [H3_RST_BUS_I2C1] =  { 0x02d8, 1 },
> + [H3_RST_BUS_I2C2] =  { 0x02d8, 2 },
>  };
> diff --git a/sys/dev/fdt/sxitwi.c b/sys/dev/fdt/sxitwi.c index
> 260f6f1de57..d05f67150ed 100644
> --- a/sys/dev/fdt/sxitwi.c
> +++ b/sys/dev/fdt/sxitwi.c
> @@ -178,8 +178,9 @@ sxitwi_match(struct device *parent, void *match,
> void
> *aux)  {
>   struct fdt_attach_args *faa = aux;
>  
> - return (OF_is_compatible(faa->fa_node, "allwinner,sun4i-a10-i2c") |
> -    OF_is_compatible(faa->fa_node, "allwinner,sun7i-a20-i2c"));
> + return (OF_is_compatible(faa->fa_node, "allwinner,sun4i-a10-i2c") ||
> +    OF_is_compatible(faa->fa_node, "allwinner,sun7i-a20-i2c") ||
> +    OF_is_compatible(faa->fa_node, "allwinner,sun6i-a31-i2c"));
>  }
>  
>  void
>
>

I just noticed i forgot to add the call in diff below.

-Artturi


diff --git a/sys/dev/fdt/sxitwi.c b/sys/dev/fdt/sxitwi.c index
d05f67150ed..d10e6a9db5e 100644
--- a/sys/dev/fdt/sxitwi.c
+++ b/sys/dev/fdt/sxitwi.c
@@ -222,6 +222,8 @@ sxitwi_attach(struct device *parent, struct device
*self, void *aux)
  /* Enable clock */
  clock_enable(faa->fa_node, NULL);
 
+ reset_deassert_all(faa->fa_node);
+
  /*
  * Set clock rate to 100kHz. From the datasheet:
  *   For 100Khz standard speed 2Wire, CLK_N=2, CLK_M=11

dmesg.txt (2K) Download Attachment