Re: [PATCH v2] i2c/nuc900: add i2c driver support for nuc900

From: Wan ZongShun
Date: Mon Jun 28 2010 - 01:54:29 EST


Hi Ben,

How about this patch? Can it be applied?

Thanks!

> This patch is to add i2c driver support for nuc900.
>
> v1->v2 changes:
> Address the comments of Ben Dooks
>
> (1) Document registers Macro definitions.
> (1) Modify some documents apply to kernel doc style.
> (2) clk_get shouldn't need the second argument, so remove it.
> (3) clk_get_rate() should be used.
> (5) Add irq IRQF_SHARED flag.
> (6) change i2s_nuc900_irq_nextbyte() declaration type from 'int' to 'void'
> Â Âit does not need return value.
> (7) To enable irq easier.
>
> Signed-off-by: Wan ZongShun <mcuos.com@xxxxxxxxx>
> Reviewed-by: Marek Vasut <marek.vasut@xxxxxxxxx>
> Reviewed-by: Baruch Siach <baruch@xxxxxxxxxx>
> ---
> Âarch/arm/mach-w90x900/include/mach/i2c.h | Â Â9 +
> Âdrivers/i2c/busses/Kconfig        |  Â7 +
> Âdrivers/i2c/busses/Makefile       Â|  Â1 +
> Âdrivers/i2c/busses/i2c-nuc900.c     Â| Â709 ++++++++++++++++++++++++++++++
> Â4 files changed, 726 insertions(+), 0 deletions(-)
> Âcreate mode 100644 arch/arm/mach-w90x900/include/mach/i2c.h
> Âcreate mode 100644 drivers/i2c/busses/i2c-nuc900.c
>
> diff --git a/arch/arm/mach-w90x900/include/mach/i2c.h b/arch/arm/mach-w90x900/include/mach/i2c.h
> new file mode 100644
> index 0000000..9ffb12d
> --- /dev/null
> +++ b/arch/arm/mach-w90x900/include/mach/i2c.h
> @@ -0,0 +1,9 @@
> +#ifndef __ASM_ARCH_NUC900_I2C_H
> +#define __ASM_ARCH_NUC900_I2C_H
> +
> +struct nuc900_platform_i2c {
> +    int       bus_num;
> +    unsigned long  bus_freq;
> +};
> +
> +#endif /* __ASM_ARCH_NUC900_I2C_H */
> diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
> index bceafbf..ee284d2 100644
> --- a/drivers/i2c/busses/Kconfig
> +++ b/drivers/i2c/busses/Kconfig
> @@ -448,6 +448,13 @@ config I2C_NOMADIK
> Â Â Â Â ÂIf you say yes to this option, support will be included for the
> Â Â Â Â ÂI2C interface from ST-Ericsson's Nomadik and Ux500 architectures.
>
> +config I2C_NUC900
> + Â Â Â tristate "NUC900 I2C Driver"
> + Â Â Â depends on ARCH_W90X900
> + Â Â Â help
> + Â Â Â Â Say Y here to include support for I2C controller in the
> + Â Â Â Â Winbond/Nuvoton NUC900 based System-on-Chip devices.
> +
> Âconfig I2C_OCORES
> Â Â Â Âtristate "OpenCores I2C Controller"
> Â Â Â Âdepends on EXPERIMENTAL
> diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
> index 936880b..c3ef492 100644
> --- a/drivers/i2c/busses/Makefile
> +++ b/drivers/i2c/busses/Makefile
> @@ -43,6 +43,7 @@ obj-$(CONFIG_I2C_IXP2000) Â Â += i2c-ixp2000.o
> Âobj-$(CONFIG_I2C_MPC) Â Â Â Â Â+= i2c-mpc.o
> Âobj-$(CONFIG_I2C_MV64XXX) Â Â Â+= i2c-mv64xxx.o
> Âobj-$(CONFIG_I2C_NOMADIK) Â Â Â+= i2c-nomadik.o
> +obj-$(CONFIG_I2C_NUC900) Â Â Â += i2c-nuc900.o
> Âobj-$(CONFIG_I2C_OCORES) Â Â Â += i2c-ocores.o
> Âobj-$(CONFIG_I2C_OMAP) Â Â Â Â += i2c-omap.o
> Âobj-$(CONFIG_I2C_PASEMI) Â Â Â += i2c-pasemi.o
> diff --git a/drivers/i2c/busses/i2c-nuc900.c b/drivers/i2c/busses/i2c-nuc900.c
> new file mode 100644
> index 0000000..7a13b28
> --- /dev/null
> +++ b/drivers/i2c/busses/i2c-nuc900.c
> @@ -0,0 +1,709 @@
> +/*
> + * linux/drivers/i2c/busses/i2c-nuc900.c
> + *
> + * Copyright (c) 2010 Nuvoton technology corporation.
> + *
> + * This driver based on S3C2410 I2C driver of Ben Dooks <ben@xxxxxxxxxxxx>.
> + * Written by Wan ZongShun <mcuos.com@xxxxxxxxx>
> + *
> + * 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;version 2 of the License.
> + *
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +
> +#include <linux/i2c.h>
> +#include <linux/i2c-id.h>
> +#include <linux/init.h>
> +#include <linux/time.h>
> +#include <linux/interrupt.h>
> +#include <linux/delay.h>
> +#include <linux/errno.h>
> +#include <linux/err.h>
> +#include <linux/platform_device.h>
> +#include <linux/clk.h>
> +#include <linux/cpufreq.h>
> +#include <linux/slab.h>
> +#include <linux/io.h>
> +
> +#include <mach/mfp.h>
> +#include <mach/i2c.h>
> +
> +/* nuc900 i2c registers offset */
> +
> +#define CSR Â Â Â Â Â Â0x00
> +#define DIVIDER Â Â Â Â Â Â Â Â0x04
> +#define CMDR Â Â Â Â Â 0x08
> +#define SWR Â Â Â Â Â Â0x0C
> +#define RXR Â Â Â Â Â Â0x10
> +#define TXR Â Â Â Â Â Â0x14
> +
> +/* nuc900 i2c CSR register bits */
> +
> +#define IRQEN Â Â Â Â Â0x003
> +#define I2CBUSY Â Â Â Â Â Â Â Â0x400
> +#define I2CSTART Â Â Â 0x018
> +#define IRQFLAG Â Â Â Â Â Â Â Â0x004
> +#define ARBIT_LOST Â Â 0x200
> +#define SLAVE_ACK Â Â Â0x800
> +
> +/* nuc900 i2c CMDR register bits */
> +
> +#define I2C_CMD_START Â0x10
> +#define I2C_CMD_STOP Â 0x08
> +#define I2C_CMD_READ Â 0x04
> +#define I2C_CMD_WRITE Â0x02
> +#define I2C_CMD_NACK Â 0x01
> +
> +/* i2c controller state */
> +
> +enum nuc900_i2c_state {
> + Â Â Â STATE_IDLE,
> + Â Â Â STATE_START,
> + Â Â Â STATE_READ,
> + Â Â Â STATE_WRITE,
> + Â Â Â STATE_STOP
> +};
> +
> +/* i2c controller private data */
> +
> +struct nuc900_i2c {
> +    spinlock_t       Âlock;
> +    wait_queue_head_t    wait;
> +
> +    struct i2c_msg     Â*msg;
> +    unsigned int      Âmsg_num;
> +    unsigned int      Âmsg_idx;
> +    unsigned int      Âmsg_ptr;
> +    unsigned int      Âirq;
> +
> +    enum nuc900_i2c_state  state;
> +
> +    void __iomem      Â*regs;
> +    struct clk       Â*clk;
> +    struct device      *dev;
> +    struct resource     *ioarea;
> +    struct i2c_adapter   Âadap;
> +};
> +
> +/* nuc900_i2c_master_complete
> + *
> + * complete the message and wake up the caller, using the given return code,
> + * or zero to mean ok.
> +*/
> +
> +static inline void nuc900_i2c_master_complete(struct nuc900_i2c *i2c, int ret)
> +{
> + Â Â Â dev_dbg(i2c->dev, "master_complete %d\n", ret);
> +
> + Â Â Â i2c->msg_ptr = 0;
> + Â Â Â i2c->msg = NULL;
> + Â Â Â i2c->msg_idx++;
> + Â Â Â i2c->msg_num = 0;
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â i2c->msg_idx = ret;
> +
> + Â Â Â wake_up(&i2c->wait);
> +}
> +
> +/* irq enable/disable functions */
> +
> +static inline void nuc900_i2c_disable_irq(struct nuc900_i2c *i2c)
> +{
> + Â Â Â unsigned long tmp;
> +
> + Â Â Â tmp = readl(i2c->regs + CSR);
> + Â Â Â writel(tmp & ~IRQEN, i2c->regs + CSR);
> +}
> +
> +static inline void nuc900_i2c_enable_irq(struct nuc900_i2c *i2c)
> +{
> + Â Â Â unsigned long tmp;
> +
> + Â Â Â tmp = readl(i2c->regs + CSR);
> + Â Â Â writel(tmp | IRQEN, i2c->regs + CSR);
> +}
> +
> +
> +/* nuc900_i2c_message_start
> + *
> + * put the start of a message onto the bus
> +*/
> +
> +static void nuc900_i2c_message_start(struct nuc900_i2c *i2c,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct i2c_msg *msg)
> +{
> + Â Â Â unsigned int addr = (msg->addr & 0x7f) << 1;
> +
> + Â Â Â if (msg->flags & I2C_M_RD)
> + Â Â Â Â Â Â Â addr |= 0x1;
> + Â Â Â writel(addr & 0xff, i2c->regs + TXR);
> + Â Â Â writel(I2C_CMD_START | I2C_CMD_WRITE, i2c->regs + CMDR);
> +}
> +
> +static inline void nuc900_i2c_stop(struct nuc900_i2c *i2c, int ret)
> +{
> +
> + Â Â Â dev_dbg(i2c->dev, "STOP\n");
> +
> + Â Â Â /* stop the transfer */
> + Â Â Â i2c->state = STATE_STOP;
> + Â Â Â writel(I2C_CMD_STOP, i2c->regs + CMDR);
> +
> + Â Â Â nuc900_i2c_master_complete(i2c, ret);
> + Â Â Â nuc900_i2c_disable_irq(i2c);
> +}
> +
> +/* helper functions to determine the current state in the set of
> + * messages we are sending
> +*/
> +
> +/* is_lastmsg()
> + *
> + * returns TRUE if the current message is the last in the set
> +*/
> +
> +static inline int is_lastmsg(struct nuc900_i2c *i2c)
> +{
> + Â Â Â return i2c->msg_idx >= (i2c->msg_num - 1);
> +}
> +
> +/* is_msglast
> + *
> + * returns TRUE if we this is the last byte in the current message
> +*/
> +
> +static inline int is_msglast(struct nuc900_i2c *i2c)
> +{
> + Â Â Â return i2c->msg_ptr == i2c->msg->len-1;
> +}
> +
> +/* is_msgend
> + *
> + * returns TRUE if we reached the end of the current message
> +*/
> +
> +static inline int is_msgend(struct nuc900_i2c *i2c)
> +{
> + Â Â Â return i2c->msg_ptr >= i2c->msg->len;
> +}
> +
> +/* i2c_nuc900_irq_nextbyte
> + *
> + * process an interrupt and work out what to do
> + */
> +
> +static void i2c_nuc900_irq_nextbyte(struct nuc900_i2c *i2c,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned long iicstat)
> +{
> + Â Â Â unsigned char byte;
> +
> + Â Â Â switch (i2c->state) {
> +
> + Â Â Â case STATE_IDLE:
> + Â Â Â Â Â Â Â dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
> + Â Â Â Â Â Â Â break;
> +
> + Â Â Â case STATE_STOP:
> + Â Â Â Â Â Â Â dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
> + Â Â Â Â Â Â Â nuc900_i2c_disable_irq(i2c);
> + Â Â Â Â Â Â Â break;
> +
> + Â Â Â case STATE_START:
> + Â Â Â Â Â Â Â /* last thing we did was send a start condition on the
> + Â Â Â Â Â Â Â Â* bus, or started a new i2c message
> + Â Â Â Â Â Â Â Â*/
> +
> + Â Â Â Â Â Â Â if (iicstat & SLAVE_ACK &&
> + Â Â Â Â Â Â Â Â Â !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
> + Â Â Â Â Â Â Â Â Â Â Â /* ack was not received... */
> +
> + Â Â Â Â Â Â Â Â Â Â Â dev_dbg(i2c->dev, "ack was not received\n");
> + Â Â Â Â Â Â Â Â Â Â Â nuc900_i2c_stop(i2c, -ENXIO);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â if (i2c->msg->flags & I2C_M_RD)
> + Â Â Â Â Â Â Â Â Â Â Â i2c->state = STATE_READ;
> + Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â i2c->state = STATE_WRITE;
> +
> + Â Â Â Â Â Â Â /* terminate the transfer if there is nothing to do
> + Â Â Â Â Â Â Â Â* as this is used by the i2c probe to find devices.
> + Â Â Â Â Â Â Â */
> +
> + Â Â Â Â Â Â Â if (is_lastmsg(i2c) && i2c->msg->len == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â nuc900_i2c_stop(i2c, 0);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â if (i2c->state == STATE_READ)
> + Â Â Â Â Â Â Â Â Â Â Â goto prepare_read;
> +
> + Â Â Â Â Â Â Â /* fall through to the write state, as we will need to
> + Â Â Â Â Â Â Â Â* send a byte as well
> + Â Â Â Â Â Â Â */
> +
> + Â Â Â case STATE_WRITE:
> + Â Â Â Â Â Â Â /* we are writing data to the device... check for the
> + Â Â Â Â Â Â Â Â* end of the message, and if so, work out what to do
> + Â Â Â Â Â Â Â Â*/
> +
> + Â Â Â Â Â Â Â if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
> + Â Â Â Â Â Â Â Â Â Â Â if (iicstat & SLAVE_ACK) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dev_dbg(i2c->dev, "WRITE: No Ack\n");
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â nuc900_i2c_stop(i2c, -ECONNREFUSED);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â }
> +
> +retry_write:
> +
> + Â Â Â Â Â Â Â if (!is_msgend(i2c)) {
> + Â Â Â Â Â Â Â Â Â Â Â byte = i2c->msg->buf[i2c->msg_ptr++];
> + Â Â Â Â Â Â Â Â Â Â Â writeb(byte, i2c->regs + TXR);
> + Â Â Â Â Â Â Â Â Â Â Â writel(I2C_CMD_WRITE, i2c->regs + CMDR);
> +
> + Â Â Â Â Â Â Â } else if (!is_lastmsg(i2c)) {
> + Â Â Â Â Â Â Â Â Â Â Â /* we need to go to the next i2c message */
> +
> + Â Â Â Â Â Â Â Â Â Â Â dev_dbg(i2c->dev, "WRITE: Next Message\n");
> +
> + Â Â Â Â Â Â Â Â Â Â Â i2c->msg_ptr = 0;
> + Â Â Â Â Â Â Â Â Â Â Â i2c->msg_idx++;
> + Â Â Â Â Â Â Â Â Â Â Â i2c->msg++;
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* check to see if we need to do another message */
> + Â Â Â Â Â Â Â Â Â Â Â if (i2c->msg->flags & I2C_M_NOSTART) {
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (i2c->msg->flags & I2C_M_RD) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* cannot do this, the controller
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â* forces us to send a new START
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â* when we change direction
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â */
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â nuc900_i2c_stop(i2c, -EINVAL);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto retry_write;
> + Â Â Â Â Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* send the new start */
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â nuc900_i2c_message_start(i2c, i2c->msg);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â i2c->state = STATE_START;
> + Â Â Â Â Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â /* send stop */
> +
> + Â Â Â Â Â Â Â Â Â Â Â nuc900_i2c_stop(i2c, 0);
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â break;
> +
> + Â Â Â case STATE_READ:
> + Â Â Â Â Â Â Â /* we have a byte of data in the data register, do
> + Â Â Â Â Â Â Â Â* something with it, and then work out wether we are
> + Â Â Â Â Â Â Â Â* going to do any more read/write
> + Â Â Â Â Â Â Â Â*/
> +
> + Â Â Â Â Â Â Â byte = readb(i2c->regs + RXR);
> + Â Â Â Â Â Â Â i2c->msg->buf[i2c->msg_ptr++] = byte;
> +
> +prepare_read:
> + Â Â Â Â Â Â Â if (is_msglast(i2c)) {
> + Â Â Â Â Â Â Â Â Â Â Â /* last byte of buffer */
> +
> + Â Â Â Â Â Â Â Â Â Â Â if (is_lastmsg(i2c))
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â writel(I2C_CMD_READ | I2C_CMD_NACK,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â i2c->regs + CMDR);
> +
> + Â Â Â Â Â Â Â } else if (is_msgend(i2c)) {
> + Â Â Â Â Â Â Â Â Â Â Â /* ok, we've read the entire buffer, see if there
> + Â Â Â Â Â Â Â Â Â Â Â Â* is anything else we need to do
> + Â Â Â Â Â Â Â Â Â Â Â */
> +
> + Â Â Â Â Â Â Â Â Â Â Â if (is_lastmsg(i2c)) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* last message, send stop and complete */
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dev_dbg(i2c->dev, "READ: Send Stop\n");
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â nuc900_i2c_stop(i2c, 0);
> + Â Â Â Â Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* go to the next transfer */
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dev_dbg(i2c->dev, "READ: Next Transfer\n");
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â i2c->msg_ptr = 0;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â i2c->msg_idx++;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â i2c->msg++;
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â writel(I2C_CMD_READ, i2c->regs + CMDR);
> + Â Â Â Â Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â writel(I2C_CMD_READ, i2c->regs + CMDR);
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â break;
> + Â Â Â }
> +}
> +
> +/* nuc900_i2c_irq
> + *
> + * top level IRQ servicing routine
> +*/
> +
> +static irqreturn_t nuc900_i2c_irq(int irqno, void *dev_id)
> +{
> + Â Â Â struct nuc900_i2c *i2c = dev_id;
> + Â Â Â unsigned long status;
> +
> + Â Â Â status = readl(i2c->regs + CSR);
> + Â Â Â writel(status | IRQFLAG, i2c->regs + CSR);
> +
> + Â Â Â if (status & ARBIT_LOST) {
> + Â Â Â Â Â Â Â /* deal with arbitration loss */
> + Â Â Â Â Â Â Â dev_err(i2c->dev, "deal with arbitration loss\n");
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â if (i2c->state == STATE_IDLE) {
> + Â Â Â Â Â Â Â dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â /* pretty much this leaves us with the fact that we've
> + Â Â Â Â* transmitted or received whatever byte we last sent
> + Â Â Â */
> +
> + Â Â Â i2c_nuc900_irq_nextbyte(i2c, status);
> +
> + out:
> + Â Â Â return IRQ_HANDLED;
> +}
> +
> +
> +/* nuc900_i2c_set_master
> + *
> + * get the i2c bus for a master transaction
> +*/
> +
> +static int nuc900_i2c_set_master(struct nuc900_i2c *i2c)
> +{
> + Â Â Â int timeout = 400;
> +
> + Â Â Â while (timeout-- > 0) {
> + Â Â Â Â Â Â Â if (((readl(i2c->regs + SWR) & I2CSTART) == I2CSTART) &&
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((readl(i2c->regs + CSR) & I2CBUSY) == 0)) {
> + Â Â Â Â Â Â Â Â Â Â Â return 0;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â msleep(1);
> + Â Â Â }
> +
> + Â Â Â return -ETIMEDOUT;
> +}
> +
> +/* nuc900_i2c_doxfer
> + *
> + * this starts an i2c transfer
> +*/
> +
> +static int nuc900_i2c_doxfer(struct nuc900_i2c *i2c,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct i2c_msg *msgs, int num)
> +{
> + Â Â Â unsigned long iicstat, timeout;
> + Â Â Â int spins = 20;
> + Â Â Â int ret;
> +
> + Â Â Â ret = nuc900_i2c_set_master(i2c);
> + Â Â Â if (ret != 0) {
> + Â Â Â Â Â Â Â dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
> + Â Â Â Â Â Â Â ret = -EAGAIN;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â spin_lock_irq(&i2c->lock);
> +
> +    i2c->msg   = msgs;
> + Â Â Â i2c->msg_num = num;
> + Â Â Â i2c->msg_ptr = 0;
> + Â Â Â i2c->msg_idx = 0;
> +    i2c->state  = STATE_START;
> +
> + Â Â Â nuc900_i2c_message_start(i2c, msgs);
> + Â Â Â spin_unlock_irq(&i2c->lock);
> +
> + Â Â Â timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
> +
> + Â Â Â ret = i2c->msg_idx;
> +
> + Â Â Â /* having these next two as dev_err() makes life very
> + Â Â Â Â* noisy when doing an i2cdetect
> + Â Â Â */
> +
> + Â Â Â if (timeout == 0)
> + Â Â Â Â Â Â Â dev_dbg(i2c->dev, "timeout\n");
> + Â Â Â else if (ret != num)
> + Â Â Â Â Â Â Â dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
> +
> + Â Â Â /* ensure the stop has been through the bus */
> +
> + Â Â Â dev_dbg(i2c->dev, "waiting for bus idle\n");
> +
> + Â Â Â /* first, try busy waiting briefly */
> + Â Â Â do {
> + Â Â Â Â Â Â Â iicstat = readl(i2c->regs + CSR);
> + Â Â Â } while ((iicstat & I2CBUSY) && --spins);
> +
> + Â Â Â /* if that timed out sleep */
> + Â Â Â if (!spins) {
> + Â Â Â Â Â Â Â msleep(1);
> + Â Â Â Â Â Â Â iicstat = readl(i2c->regs + CSR);
> + Â Â Â }
> +
> + Â Â Â if (iicstat & I2CBUSY)
> + Â Â Â Â Â Â Â dev_warn(i2c->dev, "timeout waiting for bus idle\n");
> +
> + out:
> + Â Â Â return ret;
> +}
> +
> +/* nuc900_i2c_xfer
> + *
> + * first port of call from the i2c bus code when an message needs
> + * transferring across the i2c bus.
> +*/
> +
> +static int nuc900_i2c_xfer(struct i2c_adapter *adap,
> + Â Â Â Â Â Â Â Â Â Â Â struct i2c_msg *msgs, int num)
> +{
> + Â Â Â struct nuc900_i2c *i2c = (struct nuc900_i2c *)adap->algo_data;
> + Â Â Â int retry;
> + Â Â Â int ret;
> +
> + Â Â Â nuc900_i2c_enable_irq(i2c);
> +
> + Â Â Â for (retry = 0; retry < adap->retries; retry++) {
> +
> + Â Â Â Â Â Â Â ret = nuc900_i2c_doxfer(i2c, msgs, num);
> +
> + Â Â Â Â Â Â Â if (ret != -EAGAIN)
> + Â Â Â Â Â Â Â Â Â Â Â return ret;
> +
> + Â Â Â Â Â Â Â dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
> +
> + Â Â Â Â Â Â Â udelay(100);
> + Â Â Â }
> +
> + Â Â Â return -EREMOTEIO;
> +}
> +
> +/* declare our i2c functionality */
> +static u32 nuc900_i2c_func(struct i2c_adapter *adap)
> +{
> + Â Â Â return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
> +}
> +
> +/* i2c bus registration info */
> +
> +static const struct i2c_algorithm nuc900_i2c_algorithm = {
> +    .master_xfer      Â= nuc900_i2c_xfer,
> +    .functionality     Â= nuc900_i2c_func,
> +};
> +
> +/* nuc900_i2c_probe
> + *
> + * called by the bus driver when a suitable device is found
> +*/
> +
> +static int __devinit nuc900_i2c_probe(struct platform_device *pdev)
> +{
> + Â Â Â struct nuc900_i2c *i2c;
> + Â Â Â struct nuc900_platform_i2c *pdata;
> + Â Â Â struct resource *res;
> + Â Â Â int ret;
> +
> + Â Â Â pdata = pdev->dev.platform_data;
> + Â Â Â if (!pdata) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "no platform data\n");
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â }
> +
> + Â Â Â i2c = kzalloc(sizeof(struct nuc900_i2c), GFP_KERNEL);
> + Â Â Â if (!i2c) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "no memory for state\n");
> + Â Â Â Â Â Â Â return -ENOMEM;
> + Â Â Â }
> +
> + Â Â Â strlcpy(i2c->adap.name, "nuc900-i2c0", sizeof(i2c->adap.name));
> +    i2c->adap.owner  = THIS_MODULE;
> +    i2c->adap.algo  Â= &nuc900_i2c_algorithm;
> + Â Â Â i2c->adap.retries = 2;
> +    i2c->adap.class  = I2C_CLASS_HWMON | I2C_CLASS_SPD;
> +
> + Â Â Â spin_lock_init(&i2c->lock);
> + Â Â Â init_waitqueue_head(&i2c->wait);
> +
> + Â Â Â /* find the clock and enable it */
> +
> + Â Â Â i2c->dev = &pdev->dev;
> + Â Â Â i2c->clk = clk_get(&pdev->dev, NULL);
> + Â Â Â if (IS_ERR(i2c->clk)) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "cannot get clock\n");
> + Â Â Â Â Â Â Â ret = -ENOENT;
> + Â Â Â Â Â Â Â goto err_noclk;
> + Â Â Â }
> +
> + Â Â Â dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
> +
> + Â Â Â clk_enable(i2c->clk);
> +
> + Â Â Â /* map the registers */
> +
> + Â Â Â res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + Â Â Â if (res == NULL) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "cannot find IO resource\n");
> + Â Â Â Â Â Â Â ret = -ENOENT;
> + Â Â Â Â Â Â Â goto err_clk;
> + Â Â Â }
> +
> + Â Â Â i2c->ioarea = request_mem_region(res->start, resource_size(res),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âpdev->name);
> +
> + Â Â Â if (i2c->ioarea == NULL) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "cannot request IO\n");
> + Â Â Â Â Â Â Â ret = -ENXIO;
> + Â Â Â Â Â Â Â goto err_clk;
> + Â Â Â }
> +
> + Â Â Â i2c->regs = ioremap(res->start, resource_size(res));
> +
> + Â Â Â if (i2c->regs == NULL) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "cannot map IO\n");
> + Â Â Â Â Â Â Â ret = -ENXIO;
> + Â Â Â Â Â Â Â goto err_ioarea;
> + Â Â Â }
> +
> + Â Â Â dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
> + Â Â Â Â Â Â Â i2c->regs, i2c->ioarea, res);
> +
> + Â Â Â /* setup info block for the i2c core */
> +
> + Â Â Â i2c->adap.algo_data = i2c;
> + Â Â Â i2c->adap.dev.parent = &pdev->dev;
> +
> + Â Â Â mfp_set_groupg(&pdev->dev);
> +
> + Â Â Â clk_get_rate(i2c->clk);
> +
> + Â Â Â ret = (i2c->clk.apbfreq)/(pdata->bus_freq * 5) - 1;
> + Â Â Â writel(ret & 0xffff, i2c->regs + DIVIDER);
> +
> + Â Â Â /* find the IRQ for this unit (note, this relies on the init call to
> + Â Â Â Â* ensure no current IRQs pending
> + Â Â Â Â*/
> +
> + Â Â Â i2c->irq = ret = platform_get_irq(pdev, 0);
> + Â Â Â if (ret <= 0) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "cannot find IRQ\n");
> + Â Â Â Â Â Â Â goto err_iomap;
> + Â Â Â }
> +
> + Â Â Â ret = request_irq(i2c->irq, nuc900_i2c_irq, IRQF_DISABLED | IRQF_SHARED,
> + Â Â Â Â Â Â Â Â Â Â Â Â dev_name(&pdev->dev), i2c);
> +
> + Â Â Â if (ret != 0) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
> + Â Â Â Â Â Â Â goto err_iomap;
> + Â Â Â }
> +
> + Â Â Â /* Note, previous versions of the driver used i2c_add_adapter()
> + Â Â Â Â* to add the bus at any number. We now pass the bus number via
> + Â Â Â Â* the platform data, so if unset it will now default to always
> + Â Â Â Â* being bus 0.
> + Â Â Â Â*/
> +
> + Â Â Â i2c->adap.nr = pdata->bus_num;
> +
> + Â Â Â ret = i2c_add_numbered_adapter(&i2c->adap);
> + Â Â Â if (ret < 0) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "failed to add bus to i2c core\n");
> + Â Â Â Â Â Â Â goto err_irq;
> + Â Â Â }
> +
> + Â Â Â platform_set_drvdata(pdev, i2c);
> +
> + Â Â Â dev_info(&pdev->dev, "%s: NUC900 I2C adapter\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dev_name(&i2c->adap.dev));
> + Â Â Â return 0;
> +
> + err_irq:
> + Â Â Â free_irq(i2c->irq, i2c);
> +
> + err_iomap:
> + Â Â Â iounmap(i2c->regs);
> +
> + err_ioarea:
> + Â Â Â release_resource(i2c->ioarea);
> + Â Â Â kfree(i2c->ioarea);
> +
> + err_clk:
> + Â Â Â clk_disable(i2c->clk);
> + Â Â Â clk_put(i2c->clk);
> +
> + err_noclk:
> + Â Â Â kfree(i2c);
> + Â Â Â return ret;
> +}
> +
> +/* nuc900_i2c_remove
> + *
> + * called when device is removed from the bus
> +*/
> +
> +static int __devexit nuc900_i2c_remove(struct platform_device *pdev)
> +{
> + Â Â Â struct nuc900_i2c *i2c = platform_get_drvdata(pdev);
> +
> + Â Â Â i2c_del_adapter(&i2c->adap);
> + Â Â Â free_irq(i2c->irq, i2c);
> +
> + Â Â Â clk_disable(i2c->clk);
> + Â Â Â clk_put(i2c->clk);
> +
> + Â Â Â iounmap(i2c->regs);
> +
> + Â Â Â release_resource(i2c->ioarea);
> + Â Â Â kfree(i2c->ioarea);
> + Â Â Â kfree(i2c);
> +
> + Â Â Â return 0;
> +}
> +
> +static struct platform_driver nuc900_i2c_driver = {
> +    .probe     Â= nuc900_i2c_probe,
> +    .remove     = __devexit_p(nuc900_i2c_remove),
> +    .driver     = {
> + Â Â Â Â Â Â Â .owner Â= THIS_MODULE,
> +        .name  = "nuc900-i2c0",
> + Â Â Â },
> +};
> +
> +static int __init i2c_adap_nuc900_init(void)
> +{
> + Â Â Â return platform_driver_register(&nuc900_i2c_driver);
> +}
> +
> +static void __exit i2c_adap_nuc900_exit(void)
> +{
> + Â Â Â platform_driver_unregister(&nuc900_i2c_driver);
> +}
> +subsys_initcall(i2c_adap_nuc900_init);
> +module_exit(i2c_adap_nuc900_exit);
> +
> +MODULE_DESCRIPTION("NUC900 I2C Bus driver");
> +MODULE_AUTHOR("Wan ZongShun, <mcuos.com@xxxxxxxxx>");
> +MODULE_LICENSE("GPL");
> +MODULE_ALIAS("platform:nuc900-i2c0");
> --
> 1.6.3.3
>



--
*linux-arm-kernel mailing list
mail addr:linux-arm-kernel@xxxxxxxxxxxxxxxxxxx
you can subscribe by:
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

* linux-arm-NUC900 mailing list
mail addr:NUC900@xxxxxxxxxxxxxxxx
main web: https://groups.google.com/group/NUC900
you can subscribe it by sending me mail:
mcuos.com@xxxxxxxxx
¢éì®&Þ~º&¶¬–+-±éÝ¥Šw®žË±Êâmébžìdz¹Þ)í…æèw*jg¬±¨¶‰šŽŠÝj/êäz¹ÞŠà2ŠÞ¨è­Ú&¢)ß«a¶Úþø®G«éh®æj:+v‰¨Šwè†Ù>Wš±êÞiÛaxPjØm¶Ÿÿà -»+ƒùdš_