diff mbox

[U-Boot,v2,07/12] sunxi: Add i2c support

Message ID 1401824522-11353-8-git-send-email-hdegoede@redhat.com
State Superseded
Delegated to: Ian Campbell
Headers show

Commit Message

Hans de Goede June 3, 2014, 7:41 p.m. UTC
Add support for the i2c controller found on all Allwinner sunxi SoCs,
this is the same controller as found on the Marvell orion5x and kirkwood
SoC families, with a slightly different register layout, so this patch uses
the existing mvtwsi code.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Acked-by: Ian Campbell <ijc@hellion.org.uk>
---
 arch/arm/cpu/armv7/sunxi/board.c      |  6 ++++++
 arch/arm/include/asm/arch-sunxi/i2c.h | 15 +++++++++++++++
 board/sunxi/board.c                   |  7 +++++++
 drivers/i2c/Makefile                  |  1 +
 drivers/i2c/mvtwsi.c                  | 18 ++++++++++++++++++
 include/configs/sunxi-common.h        |  9 +++++++++
 6 files changed, 56 insertions(+)
 create mode 100644 arch/arm/include/asm/arch-sunxi/i2c.h

Comments

Heiko Schocher June 4, 2014, 4:34 a.m. UTC | #1
Hello Hans,

Am 03.06.2014 21:41, schrieb Hans de Goede:
> Add support for the i2c controller found on all Allwinner sunxi SoCs,
> this is the same controller as found on the Marvell orion5x and kirkwood
> SoC families, with a slightly different register layout, so this patch uses
> the existing mvtwsi code.
>
> Signed-off-by: Hans de Goede<hdegoede@redhat.com>
> Acked-by: Ian Campbell<ijc@hellion.org.uk>
> ---
>   arch/arm/cpu/armv7/sunxi/board.c      |  6 ++++++
>   arch/arm/include/asm/arch-sunxi/i2c.h | 15 +++++++++++++++
>   board/sunxi/board.c                   |  7 +++++++
>   drivers/i2c/Makefile                  |  1 +
>   drivers/i2c/mvtwsi.c                  | 18 ++++++++++++++++++
>   include/configs/sunxi-common.h        |  9 +++++++++
>   6 files changed, 56 insertions(+)
>   create mode 100644 arch/arm/include/asm/arch-sunxi/i2c.h
[...]
> diff --git a/include/configs/sunxi-common.h b/include/configs/sunxi-common.h
> index 1d1c87d..2db083c 100644
> --- a/include/configs/sunxi-common.h
> +++ b/include/configs/sunxi-common.h
> @@ -161,6 +161,15 @@
>   #undef CONFIG_CMD_NET
>   #undef CONFIG_CMD_NFS
>
> +/* I2C */
> +#define CONFIG_SPL_I2C_SUPPORT
> +/* No CONFIG_SYS_I2C as we use the non converted mvtwsi driver */
> +#define CONFIG_HARD_I2C

NACK. Goal is to get rid of HARD_I2C define ... please convert this
driver to fit with CONFIG_SYS_I2C framework.
A fast look in this driver, this should not to be to hard, as this
driver does not support multi busses...

A good example for porting a i2c driver with no multibus support is:

commit 0bdffe71fddeaa46768a39305797e4512dee0f15
Author: Heiko Schocher <hs@denx.de>
Date:   Fri Nov 8 07:30:53 2013 +0100

     i2c, zynq: convert zynq i2c driver to new multibus/multiadapter framework

> +#define CONFIG_SYS_I2C_SUNXI
> +#define CONFIG_SYS_I2C_SPEED		400000
> +#define CONFIG_SYS_I2C_SLAVE		0x7f
> +#define CONFIG_CMD_I2C
> +
>   #ifndef CONFIG_CONS_INDEX
>   #define CONFIG_CONS_INDEX              1       /* UART0 */
>   #endif

bye,
Heiko
Hans de Goede June 9, 2014, 2:48 p.m. UTC | #2
Hi,

On 06/04/2014 06:34 AM, Heiko Schocher wrote:
> Hello Hans,
> 
> Am 03.06.2014 21:41, schrieb Hans de Goede:
>> Add support for the i2c controller found on all Allwinner sunxi SoCs,
>> this is the same controller as found on the Marvell orion5x and kirkwood
>> SoC families, with a slightly different register layout, so this patch uses
>> the existing mvtwsi code.
>>
>> Signed-off-by: Hans de Goede<hdegoede@redhat.com>
>> Acked-by: Ian Campbell<ijc@hellion.org.uk>
>> ---
>>   arch/arm/cpu/armv7/sunxi/board.c      |  6 ++++++
>>   arch/arm/include/asm/arch-sunxi/i2c.h | 15 +++++++++++++++
>>   board/sunxi/board.c                   |  7 +++++++
>>   drivers/i2c/Makefile                  |  1 +
>>   drivers/i2c/mvtwsi.c                  | 18 ++++++++++++++++++
>>   include/configs/sunxi-common.h        |  9 +++++++++
>>   6 files changed, 56 insertions(+)
>>   create mode 100644 arch/arm/include/asm/arch-sunxi/i2c.h
> [...]
>> diff --git a/include/configs/sunxi-common.h b/include/configs/sunxi-common.h
>> index 1d1c87d..2db083c 100644
>> --- a/include/configs/sunxi-common.h
>> +++ b/include/configs/sunxi-common.h
>> @@ -161,6 +161,15 @@
>>   #undef CONFIG_CMD_NET
>>   #undef CONFIG_CMD_NFS
>>
>> +/* I2C */
>> +#define CONFIG_SPL_I2C_SUPPORT
>> +/* No CONFIG_SYS_I2C as we use the non converted mvtwsi driver */
>> +#define CONFIG_HARD_I2C
> 
> NACK. Goal is to get rid of HARD_I2C define ... please convert this
> driver to fit with CONFIG_SYS_I2C framework.
> A fast look in this driver, this should not to be to hard, as this
> driver does not support multi busses...
> 
> A good example for porting a i2c driver with no multibus support is:
> 
> commit 0bdffe71fddeaa46768a39305797e4512dee0f15
> Author: Heiko Schocher <hs@denx.de>
> Date:   Fri Nov 8 07:30:53 2013 +0100
> 
>     i2c, zynq: convert zynq i2c driver to new multibus/multiadapter framework

Ok, one patch converting the mvtwsi driver to CONFIG_SYS_I2C coming up.

Note I've tested this on multiple sunxi boards / SoC generations, but
I don't have any kirkwood or orion5x boards, so I cannot guarantee that
this patch won't cause regressions there (I've compile tested the patch
with kirkwood and orion5x board configs).

Regards,

Hans
diff mbox

Patch

diff --git a/arch/arm/cpu/armv7/sunxi/board.c b/arch/arm/cpu/armv7/sunxi/board.c
index 0118f5b..7d86409 100644
--- a/arch/arm/cpu/armv7/sunxi/board.c
+++ b/arch/arm/cpu/armv7/sunxi/board.c
@@ -11,6 +11,7 @@ 
  */
 
 #include <common.h>
+#include <i2c.h>
 #include <netdev.h>
 #include <miiphy.h>
 #include <serial.h>
@@ -91,11 +92,16 @@  void s_init(void)
 	clock_init();
 	timer_init();
 	gpio_init();
+	i2c_init_board();
 
 #ifdef CONFIG_SPL_BUILD
 	gd = &gdata;
 	preloader_console_init();
 
+#ifdef CONFIG_SPL_I2C_SUPPORT
+	/* Needed early by sunxi_board_init if PMU is enabled */
+	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
+#endif
 	sunxi_board_init();
 #endif
 }
diff --git a/arch/arm/include/asm/arch-sunxi/i2c.h b/arch/arm/include/asm/arch-sunxi/i2c.h
new file mode 100644
index 0000000..dc5406b
--- /dev/null
+++ b/arch/arm/include/asm/arch-sunxi/i2c.h
@@ -0,0 +1,15 @@ 
+/*
+ * Copyright 2014 - Hans de Goede <hdegoede@redhat.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+#ifndef _SUNXI_I2C_H_
+#define _SUNXI_I2C_H_
+
+#include <asm/arch/cpu.h>
+
+#define CONFIG_I2C_MVTWSI_BASE	SUNXI_TWI0_BASE
+/* This is abp0-clk on sun4i/5i/7i / abp1-clk on sun6i/sun8i which is 24MHz */
+#define CONFIG_SYS_TCLK		24000000
+
+#endif
diff --git a/board/sunxi/board.c b/board/sunxi/board.c
index b05d0b9..543b809 100644
--- a/board/sunxi/board.c
+++ b/board/sunxi/board.c
@@ -106,6 +106,13 @@  int board_mmc_init(bd_t *bis)
 }
 #endif
 
+void i2c_init_board(void)
+{
+	sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUNXI_GPB0_TWI0);
+	sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUNXI_GPB0_TWI0);
+	clock_twi_onoff(0, 1);
+}
+
 #ifdef CONFIG_SPL_BUILD
 void sunxi_board_init(void)
 {
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
index e33586d..494c5c7 100644
--- a/drivers/i2c/Makefile
+++ b/drivers/i2c/Makefile
@@ -27,5 +27,6 @@  obj-$(CONFIG_SYS_I2C_RCAR) += rcar_i2c.o
 obj-$(CONFIG_SYS_I2C_S3C24X0) += s3c24x0_i2c.o
 obj-$(CONFIG_SYS_I2C_SH) += sh_i2c.o
 obj-$(CONFIG_SYS_I2C_SOFT) += soft_i2c.o
+obj-$(CONFIG_SYS_I2C_SUNXI) += mvtwsi.o
 obj-$(CONFIG_SYS_I2C_TEGRA) += tegra_i2c.o
 obj-$(CONFIG_SYS_I2C_ZYNQ) += zynq_i2c.o
diff --git a/drivers/i2c/mvtwsi.c b/drivers/i2c/mvtwsi.c
index 5ba0e03..e670515 100644
--- a/drivers/i2c/mvtwsi.c
+++ b/drivers/i2c/mvtwsi.c
@@ -22,6 +22,8 @@ 
 #include <asm/arch/orion5x.h>
 #elif defined(CONFIG_KIRKWOOD)
 #include <asm/arch/kirkwood.h>
+#elif defined(CONFIG_SUNXI)
+#include <asm/arch/i2c.h>
 #else
 #error Driver mvtwsi not supported by SoC or board
 #endif
@@ -30,6 +32,20 @@ 
  * TWSI register structure
  */
 
+#ifdef CONFIG_SUNXI
+
+struct  mvtwsi_registers {
+	u32 slave_address;
+	u32 xtnd_slave_addr;
+	u32 data;
+	u32 control;
+	u32 status;
+	u32 baudrate;
+	u32 soft_reset;
+};
+
+#else
+
 struct  mvtwsi_registers {
 	u32 slave_address;
 	u32 data;
@@ -43,6 +59,8 @@  struct  mvtwsi_registers {
 	u32 soft_reset;
 };
 
+#endif
+
 /*
  * Control register fields
  */
diff --git a/include/configs/sunxi-common.h b/include/configs/sunxi-common.h
index 1d1c87d..2db083c 100644
--- a/include/configs/sunxi-common.h
+++ b/include/configs/sunxi-common.h
@@ -161,6 +161,15 @@ 
 #undef CONFIG_CMD_NET
 #undef CONFIG_CMD_NFS
 
+/* I2C */
+#define CONFIG_SPL_I2C_SUPPORT
+/* No CONFIG_SYS_I2C as we use the non converted mvtwsi driver */
+#define CONFIG_HARD_I2C
+#define CONFIG_SYS_I2C_SUNXI
+#define CONFIG_SYS_I2C_SPEED		400000
+#define CONFIG_SYS_I2C_SLAVE		0x7f
+#define CONFIG_CMD_I2C
+
 #ifndef CONFIG_CONS_INDEX
 #define CONFIG_CONS_INDEX              1       /* UART0 */
 #endif