I2C: AT91RM9200 bus driver
Subsystems (2)
PATCH 1/1 I2C: AT91RM9200 bus driver
hi,
This patch adds support for the I2C (Two-wire interface) controller build into the Atmel AT91RM9200 processor.
Could it please be reviewed for submission to the mainline kernel. Thanks.
Regards,
Andrew Victor
drivers/i2c/busses/Kconfig +7 -0
@@ -199,6 +199,13 @@ This driver can also be built as a module. If so, the module will be called i2c-iop3xx. +config I2C_AT91 + tristate "Atmel AT91RM9200 I2C Two-Wire interface (TWI)" + depends on I2C && ARCH_AT91RM9200 + help + This supports the use of the I2C interface on the Atmel + AT91RM9200 processor. + config I2C_ISA tristate depends on I2C
drivers/i2c/busses/Makefile +1 -0
@@ -42,6 +42,7 @@ obj-$(CONFIG_I2C_VOODOO3) += i2c-voodoo3.o obj-$(CONFIG_SCx200_ACB) += scx200_acb.o obj-$(CONFIG_SCx200_I2C) += scx200_i2c.o +obj-$(CONFIG_I2C_AT91) += i2c-at91.o ifeq ($(CONFIG_I2C_DEBUG_BUS),y) EXTRA_CFLAGS += -DDEBUG
drivers/i2c/busses/i2c-at91.c +279 -0
279 lines not on this page · raw message
include/asm-arm/arch-at91rm9200/at91rm9200_twi.h +57 -0
57 lines not on this page · raw message
PATCH 1/1 I2C: AT91RM9200 bus driver
Hello,
Quoted 2 lines
Could it please be reviewed for submission to the mainline kernel. Thanks.
Yep I will do the review and when done Jean can push it to mainline.
Please check my comments in the code. Please replace all printk with dev_err etc infrastructure.
One error when nobody replies to smbus write quick should be treated as dev_dbg only so users will not have a full log of errors when chip drivers are probing empty addresses.
You should also move the hw init just before the i2c_add_adapter.
Regards
Rudolf
Quoted 21 lines
diff -urN linux-2.6.16-rc4.orig/drivers/i2c/busses/Kconfig linux-2.6.16-rc4/drivers/i2c/busses/Kconfig
drivers/i2c/busses/Kconfig +7
-0@@ -199,6 +199,13 @@ This driver can also be built as a module. If so, the module will be called i2c-iop3xx. +config I2C_AT91 + tristate "Atmel AT91RM9200 I2C Two-Wire interface (TWI)" + depends on I2C && ARCH_AT91RM9200 + help + This supports the use of the I2C interface on the Atmel + AT91RM9200 processor. + config I2C_ISA tristate depends on I2Cdiff -urN linux-2.6.16-rc4.orig/drivers/i2c/busses/Makefile linux-2.6.16-rc4/drivers/i2c/busses/Makefile
drivers/i2c/busses/Makefile line 45
Quoted 4 lines
drivers/i2c/busses/Makefile +1
-0@@ -42,6 +42,7 @@ obj-$(CONFIG_I2C_VOODOO3) += i2c-voodoo3.o obj-$(CONFIG_SCx200_ACB) += scx200_acb.o obj-$(CONFIG_SCx200_I2C) += scx200_i2c.o +obj-$(CONFIG_I2C_AT91) += i2c-at91.o
The rest of file seems to follow alphabetic order, please fix it.
drivers/i2c/busses/i2c-at91.c line 4
Quoted 10 lines
drivers/i2c/busses/Makefile +0
-0ifeq ($(CONFIG_I2C_DEBUG_BUS),y) EXTRA_CFLAGS += -DDEBUGdiff -urN
linux-2.6.16-rc4.orig/drivers/i2c/busses/i2c-at91.clinux-2.6.16-rc4/drivers/i2c/busses/i2c-at91.cdrivers/i2c/busses/i2c-at91.c +4
-0@@ -0,0 +1,279 @@ +/* +i2c Support for Atmel's AT91RM9200 Two-Wire Interface + +Copyright (C) Rick Bronson
No year?
Quoted 36 lines
drivers/i2c/busses/i2c-at91.c +36
-0+ +Borrowed heavily from original work by: +Copyright (C) 2000 Philip Edelbrock <phil at stimpy.netroedge.com> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include <linux/module.h> +#include <linux/version.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/pci.h> +#include <linux/types.h> +#include <linux/delay.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/clk.h> +#include <linux/platform_device.h> + +#include <asm/io.h> + +#include <asm/arch/at91rm9200_twi.h> +#include <asm/arch/board.h>
drivers/i2c/busses/i2c-at91.c line 50
Quoted 10 lines
drivers/i2c/busses/i2c-at91.c +10
-0+ +#define AT91_TWI_CLOCK 100000 +#define AT91_TWI_CKDIV1 (2 << 16)/* TWI clock divider. NOTE: see Errata #22 */ + + +#define DBG(x...) do {\ + if (debug > 0) \ + printk(KERN_DEBUG "i2c:" x); \ + } while(0) +
there is infrastructure like dev_warn dev_debug dev_err please follow it. (take a look to other i2c driver please)
drivers/i2c/busses/i2c-at91.c line 51
Quoted 1 line
drivers/i2c/busses/i2c-at91.c +1
-0+static int debug =0 ;
DOnt need it
Quoted 58 lines
drivers/i2c/busses/i2c-at91.c +58
-0+ + +static struct clk *twi_clk; + +/* +* Read from a TWI register. +*/ +static inline unsigned long at91_twi_read (unsigned int reg) +{ +void __iomem *twi_base = (void __iomem *)AT91_VA_BASE_TWI ; + +return __raw_readl (twi_base + reg); +} + +/* +* Write to a TWI register. +*/ +static inline void at91_twi_write (unsigned int reg,unsigned long value) +{ +void __iomem *twi_base = (void __iomem *)AT91_VA_BASE_TWI ; + +__raw_writel (value, twi_base + reg); +} + +static void at91_twi_hwinit (void ) +{ +unsigned int sclock, cldiv2, cldiv3; + +at91_twi_write (AT91_TWI_IDR ,0x1ff );/* Disable all interrupts */ +at91_twi_write (AT91_TWI_CR ,AT91_TWI_SWRST );/* Reset peripheral */ +at91_twi_write (AT91_TWI_CR ,AT91_TWI_MSEN );/* Set Master mode */ + + sclock = (10 *clk_get_rate (twi_clk)) /AT91_TWI_CLOCK ; + cldiv2 = (sclock /10 ) -5 ; +if ((sclock %10 ) <5 ) + cldiv2 -=1 ; + cldiv3 = (cldiv2 + (4 - cldiv2 %4 )) >>2 ; + +/* Here, CKDIV = 1 and CHDIV=CLDIV => CLDIV = CHDIV = 1/4*((Fmclk/FTWI) -6) */ +at91_twi_write (AT91_TWI_CWGR ,AT91_TWI_CKDIV1 | cldiv3 | (cldiv3 <<8 )); +} + +/* +* Poll the i2c status register until the specified bit is set. +* Returns 0 if timed out (100 msec) +*/ +static short at91_poll_status (unsigned long bit) +{ +int loop_cntr =10000 ; + +do { +udelay (10 ); + }while (!(at91_twi_read (AT91_TWI_SR ) & bit) && (--loop_cntr >0 )); + +return (loop_cntr >0 ); +} + +/*
drivers/i2c/busses/i2c-at91.c line 119
Quoted 10 lines
drivers/i2c/busses/i2c-at91.c +10
-0+* Generic i2c master transfer entrypoint +*/ +static int at91_xfer (struct i2c_adapter *adap,struct i2c_msg msgs[],int num) +{ +struct i2c_msg *pmsg; +int length; +unsigned char *buf; +int i; + +DBG ("at91_xfer: processing %d messages:\n" , num);
dev_dbg
drivers/i2c/busses/i2c-at91.c line 128
Quoted 9 lines
drivers/i2c/busses/i2c-at91.c +9
-0+ + pmsg = msgs;/* get 1st message */ + +for (i =0 ; i < num; i++) { +DBG (" #%d: %sing %d byte%s %s 0x%02x ..." , i, + pmsg->flags &I2C_M_RD ?"read" :"write" , + pmsg->len, pmsg->len >1 ?"s" :"" , + pmsg->flags &I2C_M_RD ?"from" :"to" , pmsg->addr); +
here too
Quoted 12 lines
drivers/i2c/busses/i2c-at91.c +12
-0+/* +* Set the TWI Master Mode Register: +* We do _not_ use Atmel's feature of storing the "internal device address" +* in TWI_IADR. Thus the IADRSZ bits in TWI_MMR are set to zero. +* Instead the "internal device address" has to be written using a seperate +* i2c message. +* See http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2004-September/024411.html +*/ +at91_twi_write (AT91_TWI_MMR , (pmsg->addr <<16 ) | (pmsg->flags &I2C_M_RD ?AT91_TWI_MREAD :0 )); + + length = pmsg->len; + buf = pmsg->buf;
drivers/i2c/busses/i2c-at91.c line 150
Quoted 10 lines
drivers/i2c/busses/i2c-at91.c +10
-0+if (length && buf) {/* sanity check */ +if (pmsg->flags &I2C_M_RD ) { +at91_twi_write (AT91_TWI_CR ,AT91_TWI_START ); +while (length--) { +if (!length) +at91_twi_write (AT91_TWI_CR ,AT91_TWI_STOP ); +/* Wait until transfer is finished */ +if (!at91_poll_status (AT91_TWI_RXRDY )) { +printk (KERN_ERR "at91_i2c: timeout 1\n" ); +return 0 ;
And also -1 should be signaled as error + dev_err
drivers/i2c/busses/i2c-at91.c line 156
Quoted 6 lines
drivers/i2c/busses/i2c-at91.c +6
-0+ } + *buf++ = (at91_twi_read (AT91_TWI_RHR ) &0xff ); + } +if (!at91_poll_status (AT91_TWI_TXCOMP )) { +printk (KERN_ERR "at91_i2c: timeout 2\n" ); +return 0 ;
-1 and dev_err
drivers/i2c/busses/i2c-at91.c line 165
Quoted 9 lines
drivers/i2c/busses/i2c-at91.c +9
-0+ } + }else { +at91_twi_write (AT91_TWI_CR ,AT91_TWI_START ); +while (length--) { +at91_twi_write (AT91_TWI_THR , *buf++); +if (!length) +at91_twi_write (AT91_TWI_CR ,AT91_TWI_STOP ); +if (!at91_poll_status (AT91_TWI_TXRDY )) { +printk (KERN_ERR "at91_i2c: timeout 3\n" );
dev_err or dev_dbg it depends on situation. If there is no device on requested address this error should not be displeayed as dev_err but as dev_dbg (so people dont panic when the chip drivers is looking for the chip on empty adresses when their are checking the logs. the detection is done via A Addr Rd/Wr [A] P
drivers/i2c/busses/i2c-at91.c line 166
Quoted 1 line
drivers/i2c/busses/i2c-at91.c +1
-0+return 0 ;
and -1
drivers/i2c/busses/i2c-at91.c line 172
Quoted 6 lines
drivers/i2c/busses/i2c-at91.c +6
-0+ } + } +/* Wait until transfer is finished */ +if (!at91_poll_status (AT91_TWI_TXCOMP )) { +printk (KERN_ERR "at91_i2c: timeout 4\n" ); +return 0 ;
dev_err and -1
Quoted 24 lines
drivers/i2c/busses/i2c-at91.c +24
-0+ } + } + } +DBG ("ok\n" ); + pmsg++;/* go to next message */ + } +return i; +} + +/* +* Return list of supported functionality +*/ +static u32 at91_func (struct i2c_adapter *adapter) +{ +return I2C_FUNC_I2C |I2C_FUNC_SMBUS_EMUL ; +} + +/* For now, we only handle combined mode (smbus) */ +static struct i2c_algorithm at91_algorithm = { + .master_xfer = at91_xfer, + .functionality = at91_func, +}; + +/*
drivers/i2c/busses/i2c-at91.c line 206
Quoted 10 lines
drivers/i2c/busses/i2c-at91.c +10
-0+* Main initialization routine +*/ +static int __init at91_i2c_probe (struct platform_device *pdev) +{ +struct i2c_adapter *adapter; +int rc; + + twi_clk =clk_get (NULL ,"twi_clk" ); +if (!twi_clk) { +printk (KERN_ERR "at91_i2c: no clock defined\n" );
dev_err
drivers/i2c/busses/i2c-at91.c line 211
Quoted 5 lines
drivers/i2c/busses/i2c-at91.c +5
-0+return -ENODEV ; + } +clk_enable (twi_clk);/* enable peripheral clock */ + +at91_twi_hwinit ();/* initialize TWI controller */
move it down
drivers/i2c/busses/i2c-at91.c line 215
Quoted 4 lines
drivers/i2c/busses/i2c-at91.c +4
-0+ + adapter =kmalloc (sizeof (struct i2c_adapter ),GFP_KERNEL ); +if (adapter =NULL ) { +printk (KERN_ERR "at91_i2c: can't allocate inteface!\n" );
dev_err
drivers/i2c/busses/i2c-at91.c line 216
Quoted 1 line
drivers/i2c/busses/i2c-at91.c +1
-0+return -ENOMEM ;
If the HW init is not called you can quit like this. Otherwise not.
drivers/i2c/busses/i2c-at91.c line 219
Quoted 3 lines
drivers/i2c/busses/i2c-at91.c +3
-0+ } +memset (adapter,0 ,sizeof (struct i2c_adapter )); +
kzalloc can do that.
drivers/i2c/busses/i2c-at91.c line 225
Quoted 7 lines
drivers/i2c/busses/i2c-at91.c +6
-0+sprintf (adapter->name,"AT91RM9200" ); + adapter->algo = &at91_algorithm; + adapter->algo_data =NULL ; + adapter->client_register =NULL ; + adapter->client_unregister =NULL ; + adapter->class =I2C_CLASS_HWMON ;
So this NULL are zeros.....
drivers/i2c/busses/i2c-at91.c line 228
Quoted 2 lines
drivers/i2c/busses/i2c-at91.c +2
-0+platform_set_drvdata (pdev, adapter); +
Here should be HW init called
drivers/i2c/busses/i2c-at91.c line 232
Quoted 4 lines
drivers/i2c/busses/i2c-at91.c +4
-0+ rc =i2c_add_adapter (adapter); +if (rc) { +printk (KERN_ERR "at91_i2c: Adapter %s registration failed\n" , adapter->name); +kfree (adapter);
dev_err
and HW deinit
drivers/i2c/busses/i2c-at91.c line 235
Quoted 3 lines
drivers/i2c/busses/i2c-at91.c +3
-0+ } +else +printk (KERN_INFO "AT91 i2c bus driver.\n" );
dev_info
Quoted 113 lines
drivers/i2c/busses/i2c-at91.c +44
-0+ +return rc; +} + +static int __devexit at91_i2c_remove(struct platform_device *pdev) +{ +struct i2c_adapter *adapter =platform_get_drvdata (pdev); +int res; + + res =i2c_del_adapter (adapter); +platform_set_drvdata (pdev,NULL ); + +clk_disable (twi_clk);/* disable peripheral clock */ +clk_put (twi_clk); + +return res; +} + +static struct platform_driver at91_i2c_driver = { + .probe = at91_i2c_probe, + .remove = at91_i2c_remove, + .driver = { + .name ="at91_i2c" , + .owner =THIS_MODULE , + }, +}; + +static int __init at91_i2c_init (void ) +{ +return platform_driver_register (&at91_i2c_driver); +} + +static void __exit at91_i2c_exit(void) +{ +platform_driver_unregister (&at91_i2c_driver); +} + + +module_init (at91_i2c_init); +module_exit (at91_i2c_exit); + +MODULE_AUTHOR ("Rick Bronson" ); +MODULE_DESCRIPTION ("I2C driver for Atmel AT91RM9200" ); +MODULE_LICENSE ("GPL" );diff -urN
linux-2.6.16-rc4.orig/include/asm-arm/arch-at91rm9200/at91rm9200_twi.hlinux-2.6.16-rc4/include/asm-arm/arch-at91rm9200/at91rm9200_twi.hinclude/asm-arm/arch-at91rm9200/at91rm9200_twi.h +57
-0@@ -0,0 +1,57 @@ +/* +* include/asm-arm/arch-at91rm9200/at91rm9200_twi.h +* +* Copyright (C) 2005 Ivan Kokshaysky +* Copyright (C) SAN People +* +* Two-wire Interface (TWI) registers. +* Based on AT91RM9200 datasheet revision E. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +*/ + +#ifndef AT91RM9200_TWI_H +#define AT91RM9200_TWI_H + +#define AT91_TWI_CR 0x00/* Control Register */ +#define AT91_TWI_START (1 << 0)/* Send a Start Condition */ +#define AT91_TWI_STOP (1 << 1)/* Send a Stop Condition */ +#define AT91_TWI_MSEN (1 << 2)/* Master Transfer Enable */ +#define AT91_TWI_MSDIS (1 << 3)/* Master Transfer Disable */ +#define AT91_TWI_SWRST (1 << 7)/* Software Reset */ + +#define AT91_TWI_MMR 0x04/* Master Mode Register */ +#define AT91_TWI_IADRSZ (3 << 8)/* Internal Device Address Size */ +#define AT91_TWI_IADRSZ_NO (0 << 8) +#define AT91_TWI_IADRSZ_1 (1 << 8) +#define AT91_TWI_IADRSZ_2 (2 << 8) +#define AT91_TWI_IADRSZ_3 (3 << 8) +#define AT91_TWI_MREAD (1 << 12)/* Master Read Direction */ +#define AT91_TWI_DADR (0x7f << 16)/* Device Address */ + +#define AT91_TWI_IADR 0x0c/* Internal Address Register */ + +#define AT91_TWI_CWGR 0x10/* Clock Waveform Generator Register */ +#define AT91_TWI_CLDIV (0xff << 0)/* Clock Low Divisor */ +#define AT91_TWI_CHDIV (0xff << 8)/* Clock High Divisor */ +#define AT91_TWI_CKDIV (7 << 16)/* Clock Divider */ + +#define AT91_TWI_SR 0x20/* Status Register */ +#define AT91_TWI_TXCOMP (1 << 0)/* Transmission Complete */ +#define AT91_TWI_RXRDY (1 << 1)/* Receive Holding Register Ready */ +#define AT91_TWI_TXRDY (1 << 2)/* Transmit Holding Register Ready */ +#define AT91_TWI_OVRE (1 << 6)/* Overrun Error */ +#define AT91_TWI_UNRE (1 << 7)/* Underrun Error */ +#define AT91_TWI_NACK (1 << 8)/* Not Acknowledged */ + +#define AT91_TWI_IER 0x24/* Interrupt Enable Register */ +#define AT91_TWI_IDR 0x28/* Interrupt Disable Register */ +#define AT91_TWI_IMR 0x2c/* Interrupt Mask Register */ +#define AT91_TWI_RHR 0x30/* Receive Holding Register */ +#define AT91_TWI_THR 0x34/* Transmit Holding Register */ + +#endif +_______________________________________________
lm-sensors mailing list
lm-sensors at lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH 1/1 I2C: AT91RM9200 bus driver
In reply to Andrew Victorhi,
Quoted 1 line
Yep I will do the review and when done Jean can push it to mainline.
The changes you recommended have been made. See the appended patch.
Quoted 1 line
Please replace all printk with
dev_erretc infrastructure.
Those seems to generate strange messages on this platform.
at91_i2c at91_i2c: AT91 i2c bus driver.
ie, device name is doubled.
Regards,
Andrew Victor
drivers/i2c/busses/Kconfig +7 -0
@@ -199,6 +199,13 @@ This driver can also be built as a module. If so, the module will be called i2c-iop3xx. +config I2C_AT91 + tristate "Atmel AT91RM9200 I2C Two-Wire interface (TWI)" + depends on I2C && ARCH_AT91RM9200 + help + This supports the use of the I2C interface on the AT91RM9200 + processor. + config I2C_ISA tristate depends on I2C
drivers/i2c/busses/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_I2C_AMD756) += i2c-amd756.o obj-$(CONFIG_I2C_AMD756_S4882) += i2c-amd756-s4882.o obj-$(CONFIG_I2C_AMD8111) += i2c-amd8111.o +obj-$(CONFIG_I2C_AT91) += i2c-at91.o obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o obj-$(CONFIG_I2C_ELEKTOR) += i2c-elektor.o obj-$(CONFIG_I2C_HYDRA) += i2c-hydra.o
drivers/i2c/busses/i2c-at91.c +268 -0
268 lines not on this page · raw message
include/asm-arm/arch-at91rm9200/at91rm9200_twi.h +57 -0
57 lines not on this page · raw message
PATCH 1/1 I2C: AT91RM9200 bus driver
In reply to Andrew VictorHi Andrew,
Quoted 5 lines · Andrew Victor
Quoted 1 line
Please replace all printk with
dev_erretc infrastructure.Those seems to generate strange messages on this platform.
at91_i2cat91_i2c: AT91 i2c bus driver.
ie, device name is doubled.
This is a known problem with the current platform driver core implementation. In fact what you see isn't a doubled device name but a device name followed by the device id (if I remember correctly). It happens to be almost always the same for platform drivers so the output looks a bit weird.
This would need to be "fixed" at the platform driver core level but I'm not too sure what to do here, as the dev_* macros are generic ones and not platform-driver specific.
--
Jean Delvare