xref: /openbmc/linux/drivers/i2c/busses/i2c-davinci.c (revision d868caa177d4487ce1935926498f542a8f67c1cf)
195a7f10eSVladimir Barinov /*
295a7f10eSVladimir Barinov  * TI DAVINCI I2C adapter driver.
395a7f10eSVladimir Barinov  *
495a7f10eSVladimir Barinov  * Copyright (C) 2006 Texas Instruments.
595a7f10eSVladimir Barinov  * Copyright (C) 2007 MontaVista Software Inc.
695a7f10eSVladimir Barinov  *
795a7f10eSVladimir Barinov  * Updated by Vinod & Sudhakar Feb 2005
895a7f10eSVladimir Barinov  *
995a7f10eSVladimir Barinov  * ----------------------------------------------------------------------------
1095a7f10eSVladimir Barinov  *
1195a7f10eSVladimir Barinov  * This program is free software; you can redistribute it and/or modify
1295a7f10eSVladimir Barinov  * it under the terms of the GNU General Public License as published by
1395a7f10eSVladimir Barinov  * the Free Software Foundation; either version 2 of the License, or
1495a7f10eSVladimir Barinov  * (at your option) any later version.
1595a7f10eSVladimir Barinov  *
1695a7f10eSVladimir Barinov  * This program is distributed in the hope that it will be useful,
1795a7f10eSVladimir Barinov  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1895a7f10eSVladimir Barinov  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1995a7f10eSVladimir Barinov  * GNU General Public License for more details.
2095a7f10eSVladimir Barinov  *
2195a7f10eSVladimir Barinov  * You should have received a copy of the GNU General Public License
2295a7f10eSVladimir Barinov  * along with this program; if not, write to the Free Software
2395a7f10eSVladimir Barinov  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2495a7f10eSVladimir Barinov  * ----------------------------------------------------------------------------
2595a7f10eSVladimir Barinov  *
2695a7f10eSVladimir Barinov  */
2795a7f10eSVladimir Barinov #include <linux/kernel.h>
2895a7f10eSVladimir Barinov #include <linux/module.h>
2995a7f10eSVladimir Barinov #include <linux/delay.h>
3095a7f10eSVladimir Barinov #include <linux/i2c.h>
3195a7f10eSVladimir Barinov #include <linux/clk.h>
3295a7f10eSVladimir Barinov #include <linux/errno.h>
3395a7f10eSVladimir Barinov #include <linux/sched.h>
3495a7f10eSVladimir Barinov #include <linux/err.h>
3595a7f10eSVladimir Barinov #include <linux/interrupt.h>
3695a7f10eSVladimir Barinov #include <linux/platform_device.h>
3795a7f10eSVladimir Barinov #include <linux/io.h>
3895a7f10eSVladimir Barinov 
3995a7f10eSVladimir Barinov #include <asm/hardware.h>
4095a7f10eSVladimir Barinov #include <asm/mach-types.h>
4195a7f10eSVladimir Barinov 
4295a7f10eSVladimir Barinov #include <asm/arch/i2c.h>
4395a7f10eSVladimir Barinov 
4495a7f10eSVladimir Barinov /* ----- global defines ----------------------------------------------- */
4595a7f10eSVladimir Barinov 
4695a7f10eSVladimir Barinov #define DAVINCI_I2C_TIMEOUT	(1*HZ)
4795a7f10eSVladimir Barinov #define I2C_DAVINCI_INTR_ALL    (DAVINCI_I2C_IMR_AAS | \
4895a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_SCD | \
4995a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_ARDY | \
5095a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_NACK | \
5195a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_AL)
5295a7f10eSVladimir Barinov 
5395a7f10eSVladimir Barinov #define DAVINCI_I2C_OAR_REG	0x00
5495a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_REG	0x04
5595a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_REG	0x08
5695a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKL_REG	0x0c
5795a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKH_REG	0x10
5895a7f10eSVladimir Barinov #define DAVINCI_I2C_CNT_REG	0x14
5995a7f10eSVladimir Barinov #define DAVINCI_I2C_DRR_REG	0x18
6095a7f10eSVladimir Barinov #define DAVINCI_I2C_SAR_REG	0x1c
6195a7f10eSVladimir Barinov #define DAVINCI_I2C_DXR_REG	0x20
6295a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_REG	0x24
6395a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_REG	0x28
6495a7f10eSVladimir Barinov #define DAVINCI_I2C_EMDR_REG	0x2c
6595a7f10eSVladimir Barinov #define DAVINCI_I2C_PSC_REG	0x30
6695a7f10eSVladimir Barinov 
6795a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AAS	0x07
6895a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_SCD	0x06
6995a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_XRDY	0x05
7095a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_RDR	0x04
7195a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_ARDY	0x03
7295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_NACK	0x02
7395a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AL	0x01
7495a7f10eSVladimir Barinov 
7595a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_BB	(1 << 12)
7695a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_RSFULL	(1 << 11)
7795a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_SCD	(1 << 5)
7895a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_ARDY	(1 << 2)
7995a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_NACK	(1 << 1)
8095a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_AL	(1 << 0)
8195a7f10eSVladimir Barinov 
8295a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_NACK	(1 << 15)
8395a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_STT	(1 << 13)
8495a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_STP	(1 << 11)
8595a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_MST	(1 << 10)
8695a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_TRX	(1 << 9)
8795a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_XA	(1 << 8)
8895a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_IRS	(1 << 5)
8995a7f10eSVladimir Barinov 
9095a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_AAS	(1 << 6)
9195a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_SCD	(1 << 5)
9295a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_XRDY	(1 << 4)
9395a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_RRDY	(1 << 3)
9495a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_ARDY	(1 << 2)
9595a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_NACK	(1 << 1)
9695a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_AL	(1 << 0)
9795a7f10eSVladimir Barinov 
9895a7f10eSVladimir Barinov #define MOD_REG_BIT(val, mask, set) do { \
9995a7f10eSVladimir Barinov 	if (set) { \
10095a7f10eSVladimir Barinov 		val |= mask; \
10195a7f10eSVladimir Barinov 	} else { \
10295a7f10eSVladimir Barinov 		val &= ~mask; \
10395a7f10eSVladimir Barinov 	} \
10495a7f10eSVladimir Barinov } while (0)
10595a7f10eSVladimir Barinov 
10695a7f10eSVladimir Barinov struct davinci_i2c_dev {
10795a7f10eSVladimir Barinov 	struct device           *dev;
10895a7f10eSVladimir Barinov 	void __iomem		*base;
10995a7f10eSVladimir Barinov 	struct completion	cmd_complete;
11095a7f10eSVladimir Barinov 	struct clk              *clk;
11195a7f10eSVladimir Barinov 	int			cmd_err;
11295a7f10eSVladimir Barinov 	u8			*buf;
11395a7f10eSVladimir Barinov 	size_t			buf_len;
11495a7f10eSVladimir Barinov 	int			irq;
11595a7f10eSVladimir Barinov 	struct i2c_adapter	adapter;
11695a7f10eSVladimir Barinov };
11795a7f10eSVladimir Barinov 
11895a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */
11995a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
12095a7f10eSVladimir Barinov 	.bus_freq	= 100,
12195a7f10eSVladimir Barinov 	.bus_delay	= 0,
12295a7f10eSVladimir Barinov };
12395a7f10eSVladimir Barinov 
12495a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
12595a7f10eSVladimir Barinov 					 int reg, u16 val)
12695a7f10eSVladimir Barinov {
12795a7f10eSVladimir Barinov 	__raw_writew(val, i2c_dev->base + reg);
12895a7f10eSVladimir Barinov }
12995a7f10eSVladimir Barinov 
13095a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
13195a7f10eSVladimir Barinov {
13295a7f10eSVladimir Barinov 	return __raw_readw(i2c_dev->base + reg);
13395a7f10eSVladimir Barinov }
13495a7f10eSVladimir Barinov 
13595a7f10eSVladimir Barinov /*
13695a7f10eSVladimir Barinov  * This functions configures I2C and brings I2C out of reset.
13795a7f10eSVladimir Barinov  * This function is called during I2C init function. This function
13895a7f10eSVladimir Barinov  * also gets called if I2C encounters any errors.
13995a7f10eSVladimir Barinov  */
14095a7f10eSVladimir Barinov static int i2c_davinci_init(struct davinci_i2c_dev *dev)
14195a7f10eSVladimir Barinov {
14295a7f10eSVladimir Barinov 	struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
14395a7f10eSVladimir Barinov 	u16 psc;
14495a7f10eSVladimir Barinov 	u32 clk;
145cc99ff70STroy Kisky 	u32 d;
14695a7f10eSVladimir Barinov 	u32 clkh;
14795a7f10eSVladimir Barinov 	u32 clkl;
14895a7f10eSVladimir Barinov 	u32 input_clock = clk_get_rate(dev->clk);
14995a7f10eSVladimir Barinov 	u16 w;
15095a7f10eSVladimir Barinov 
15195a7f10eSVladimir Barinov 	if (!pdata)
15295a7f10eSVladimir Barinov 		pdata = &davinci_i2c_platform_data_default;
15395a7f10eSVladimir Barinov 
15495a7f10eSVladimir Barinov 	/* put I2C into reset */
15595a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
15695a7f10eSVladimir Barinov 	MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 0);
15795a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
15895a7f10eSVladimir Barinov 
15995a7f10eSVladimir Barinov 	/* NOTE: I2C Clock divider programming info
16095a7f10eSVladimir Barinov 	 * As per I2C specs the following formulas provide prescaler
16195a7f10eSVladimir Barinov 	 * and low/high divider values
16295a7f10eSVladimir Barinov 	 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
16395a7f10eSVladimir Barinov 	 *                       module clk
16495a7f10eSVladimir Barinov 	 *
16595a7f10eSVladimir Barinov 	 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
16695a7f10eSVladimir Barinov 	 *
16795a7f10eSVladimir Barinov 	 * Thus,
16895a7f10eSVladimir Barinov 	 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
16995a7f10eSVladimir Barinov 	 *
17095a7f10eSVladimir Barinov 	 * where if PSC == 0, d = 7,
17195a7f10eSVladimir Barinov 	 *       if PSC == 1, d = 6
17295a7f10eSVladimir Barinov 	 *       if PSC > 1 , d = 5
17395a7f10eSVladimir Barinov 	 */
17495a7f10eSVladimir Barinov 
175cc99ff70STroy Kisky 	/* get minimum of 7 MHz clock, but max of 12 MHz */
176cc99ff70STroy Kisky 	psc = (input_clock / 7000000) - 1;
177cc99ff70STroy Kisky 	if ((input_clock / (psc + 1)) > 12000000)
178cc99ff70STroy Kisky 		psc++;	/* better to run under spec than over */
179cc99ff70STroy Kisky 	d = (psc >= 2) ? 5 : 7 - psc;
18095a7f10eSVladimir Barinov 
181cc99ff70STroy Kisky 	clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
182cc99ff70STroy Kisky 	clkh = clk >> 1;
18395a7f10eSVladimir Barinov 	clkl = clk - clkh;
18495a7f10eSVladimir Barinov 
18595a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
18695a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
18795a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
18895a7f10eSVladimir Barinov 
189cc99ff70STroy Kisky 	dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
19095a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "PSC  = %d\n",
19195a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
19295a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKL = %d\n",
19395a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
19495a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKH = %d\n",
19595a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
196cc99ff70STroy Kisky 	dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
197cc99ff70STroy Kisky 		pdata->bus_freq, pdata->bus_delay);
19895a7f10eSVladimir Barinov 
19995a7f10eSVladimir Barinov 	/* Take the I2C module out of reset: */
20095a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
20195a7f10eSVladimir Barinov 	MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 1);
20295a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
20395a7f10eSVladimir Barinov 
20495a7f10eSVladimir Barinov 	/* Enable interrupts */
20595a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
20695a7f10eSVladimir Barinov 
20795a7f10eSVladimir Barinov 	return 0;
20895a7f10eSVladimir Barinov }
20995a7f10eSVladimir Barinov 
21095a7f10eSVladimir Barinov /*
21195a7f10eSVladimir Barinov  * Waiting for bus not busy
21295a7f10eSVladimir Barinov  */
21395a7f10eSVladimir Barinov static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
21495a7f10eSVladimir Barinov 					 char allow_sleep)
21595a7f10eSVladimir Barinov {
21695a7f10eSVladimir Barinov 	unsigned long timeout;
21795a7f10eSVladimir Barinov 
21895a7f10eSVladimir Barinov 	timeout = jiffies + DAVINCI_I2C_TIMEOUT;
21995a7f10eSVladimir Barinov 	while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
22095a7f10eSVladimir Barinov 	       & DAVINCI_I2C_STR_BB) {
22195a7f10eSVladimir Barinov 		if (time_after(jiffies, timeout)) {
22295a7f10eSVladimir Barinov 			dev_warn(dev->dev,
22395a7f10eSVladimir Barinov 				 "timeout waiting for bus ready\n");
22495a7f10eSVladimir Barinov 			return -ETIMEDOUT;
22595a7f10eSVladimir Barinov 		}
22695a7f10eSVladimir Barinov 		if (allow_sleep)
22795a7f10eSVladimir Barinov 			schedule_timeout(1);
22895a7f10eSVladimir Barinov 	}
22995a7f10eSVladimir Barinov 
23095a7f10eSVladimir Barinov 	return 0;
23195a7f10eSVladimir Barinov }
23295a7f10eSVladimir Barinov 
23395a7f10eSVladimir Barinov /*
23495a7f10eSVladimir Barinov  * Low level master read/write transaction. This function is called
23595a7f10eSVladimir Barinov  * from i2c_davinci_xfer.
23695a7f10eSVladimir Barinov  */
23795a7f10eSVladimir Barinov static int
23895a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
23995a7f10eSVladimir Barinov {
24095a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
24195a7f10eSVladimir Barinov 	struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
24295a7f10eSVladimir Barinov 	u32 flag;
24395a7f10eSVladimir Barinov 	u32 stat;
24495a7f10eSVladimir Barinov 	u16 w;
24595a7f10eSVladimir Barinov 	int r;
24695a7f10eSVladimir Barinov 
24795a7f10eSVladimir Barinov 	if (msg->len == 0)
24895a7f10eSVladimir Barinov 		return -EINVAL;
24995a7f10eSVladimir Barinov 
25095a7f10eSVladimir Barinov 	if (!pdata)
25195a7f10eSVladimir Barinov 		pdata = &davinci_i2c_platform_data_default;
25295a7f10eSVladimir Barinov 	/* Introduce a delay, required for some boards (e.g Davinci EVM) */
25395a7f10eSVladimir Barinov 	if (pdata->bus_delay)
25495a7f10eSVladimir Barinov 		udelay(pdata->bus_delay);
25595a7f10eSVladimir Barinov 
25695a7f10eSVladimir Barinov 	/* set the slave address */
25795a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
25895a7f10eSVladimir Barinov 
25995a7f10eSVladimir Barinov 	dev->buf = msg->buf;
26095a7f10eSVladimir Barinov 	dev->buf_len = msg->len;
26195a7f10eSVladimir Barinov 
26295a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
26395a7f10eSVladimir Barinov 
26495a7f10eSVladimir Barinov 	init_completion(&dev->cmd_complete);
26595a7f10eSVladimir Barinov 	dev->cmd_err = 0;
26695a7f10eSVladimir Barinov 
26795a7f10eSVladimir Barinov 	/* Clear any pending interrupts by reading the IVR */
26895a7f10eSVladimir Barinov 	stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG);
26995a7f10eSVladimir Barinov 
27095a7f10eSVladimir Barinov 	/* Take I2C out of reset, configure it as master and set the
27195a7f10eSVladimir Barinov 	 * start bit */
27295a7f10eSVladimir Barinov 	flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST | DAVINCI_I2C_MDR_STT;
27395a7f10eSVladimir Barinov 
27495a7f10eSVladimir Barinov 	/* if the slave address is ten bit address, enable XA bit */
27595a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_TEN)
27695a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_XA;
27795a7f10eSVladimir Barinov 	if (!(msg->flags & I2C_M_RD))
27895a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_TRX;
27995a7f10eSVladimir Barinov 	if (stop)
28095a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_STP;
28195a7f10eSVladimir Barinov 
28295a7f10eSVladimir Barinov 	/* Enable receive or transmit interrupts */
28395a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
28495a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_RD)
28595a7f10eSVladimir Barinov 		MOD_REG_BIT(w, DAVINCI_I2C_IMR_RRDY, 1);
28695a7f10eSVladimir Barinov 	else
28795a7f10eSVladimir Barinov 		MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 1);
28895a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
28995a7f10eSVladimir Barinov 
29095a7f10eSVladimir Barinov 	/* write the data into mode register */
29195a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
29295a7f10eSVladimir Barinov 
29395a7f10eSVladimir Barinov 	r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
29495a7f10eSVladimir Barinov 						      DAVINCI_I2C_TIMEOUT);
29595a7f10eSVladimir Barinov 	dev->buf_len = 0;
29695a7f10eSVladimir Barinov 	if (r < 0)
29795a7f10eSVladimir Barinov 		return r;
29895a7f10eSVladimir Barinov 
29995a7f10eSVladimir Barinov 	if (r == 0) {
30095a7f10eSVladimir Barinov 		dev_err(dev->dev, "controller timed out\n");
30195a7f10eSVladimir Barinov 		i2c_davinci_init(dev);
30295a7f10eSVladimir Barinov 		return -ETIMEDOUT;
30395a7f10eSVladimir Barinov 	}
30495a7f10eSVladimir Barinov 
30595a7f10eSVladimir Barinov 	/* no error */
30695a7f10eSVladimir Barinov 	if (likely(!dev->cmd_err))
30795a7f10eSVladimir Barinov 		return msg->len;
30895a7f10eSVladimir Barinov 
30995a7f10eSVladimir Barinov 	/* We have an error */
31095a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
31195a7f10eSVladimir Barinov 		i2c_davinci_init(dev);
31295a7f10eSVladimir Barinov 		return -EIO;
31395a7f10eSVladimir Barinov 	}
31495a7f10eSVladimir Barinov 
31595a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
31695a7f10eSVladimir Barinov 		if (msg->flags & I2C_M_IGNORE_NAK)
31795a7f10eSVladimir Barinov 			return msg->len;
31895a7f10eSVladimir Barinov 		if (stop) {
31995a7f10eSVladimir Barinov 			w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
32095a7f10eSVladimir Barinov 			MOD_REG_BIT(w, DAVINCI_I2C_MDR_STP, 1);
32195a7f10eSVladimir Barinov 			davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
32295a7f10eSVladimir Barinov 		}
32395a7f10eSVladimir Barinov 		return -EREMOTEIO;
32495a7f10eSVladimir Barinov 	}
32595a7f10eSVladimir Barinov 	return -EIO;
32695a7f10eSVladimir Barinov }
32795a7f10eSVladimir Barinov 
32895a7f10eSVladimir Barinov /*
32995a7f10eSVladimir Barinov  * Prepare controller for a transaction and call i2c_davinci_xfer_msg
33095a7f10eSVladimir Barinov  */
33195a7f10eSVladimir Barinov static int
33295a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
33395a7f10eSVladimir Barinov {
33495a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
33595a7f10eSVladimir Barinov 	int i;
33695a7f10eSVladimir Barinov 	int ret;
33795a7f10eSVladimir Barinov 
33808882d20SHarvey Harrison 	dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
33995a7f10eSVladimir Barinov 
34095a7f10eSVladimir Barinov 	ret = i2c_davinci_wait_bus_not_busy(dev, 1);
34195a7f10eSVladimir Barinov 	if (ret < 0) {
34295a7f10eSVladimir Barinov 		dev_warn(dev->dev, "timeout waiting for bus ready\n");
34395a7f10eSVladimir Barinov 		return ret;
34495a7f10eSVladimir Barinov 	}
34595a7f10eSVladimir Barinov 
34695a7f10eSVladimir Barinov 	for (i = 0; i < num; i++) {
34795a7f10eSVladimir Barinov 		ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
348*d868caa1STroy Kisky 		dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
349*d868caa1STroy Kisky 			ret);
35095a7f10eSVladimir Barinov 		if (ret < 0)
35195a7f10eSVladimir Barinov 			return ret;
35295a7f10eSVladimir Barinov 	}
35395a7f10eSVladimir Barinov 	return num;
35495a7f10eSVladimir Barinov }
35595a7f10eSVladimir Barinov 
35695a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap)
35795a7f10eSVladimir Barinov {
35895a7f10eSVladimir Barinov 	return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
35995a7f10eSVladimir Barinov }
36095a7f10eSVladimir Barinov 
36195a7f10eSVladimir Barinov /*
36295a7f10eSVladimir Barinov  * Interrupt service routine. This gets called whenever an I2C interrupt
36395a7f10eSVladimir Barinov  * occurs.
36495a7f10eSVladimir Barinov  */
36595a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
36695a7f10eSVladimir Barinov {
36795a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = dev_id;
36895a7f10eSVladimir Barinov 	u32 stat;
36995a7f10eSVladimir Barinov 	int count = 0;
37095a7f10eSVladimir Barinov 	u16 w;
37195a7f10eSVladimir Barinov 
37295a7f10eSVladimir Barinov 	while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
37308882d20SHarvey Harrison 		dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
37495a7f10eSVladimir Barinov 		if (count++ == 100) {
37595a7f10eSVladimir Barinov 			dev_warn(dev->dev, "Too much work in one IRQ\n");
37695a7f10eSVladimir Barinov 			break;
37795a7f10eSVladimir Barinov 		}
37895a7f10eSVladimir Barinov 
37995a7f10eSVladimir Barinov 		switch (stat) {
38095a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AL:
38195a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_AL;
38295a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
38395a7f10eSVladimir Barinov 			break;
38495a7f10eSVladimir Barinov 
38595a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_NACK:
38695a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_NACK;
38795a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
38895a7f10eSVladimir Barinov 			break;
38995a7f10eSVladimir Barinov 
39095a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_ARDY:
391b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
392b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
39395a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
39495a7f10eSVladimir Barinov 			break;
39595a7f10eSVladimir Barinov 
39695a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_RDR:
39795a7f10eSVladimir Barinov 			if (dev->buf_len) {
39895a7f10eSVladimir Barinov 				*dev->buf++ =
39995a7f10eSVladimir Barinov 				    davinci_i2c_read_reg(dev,
40095a7f10eSVladimir Barinov 							 DAVINCI_I2C_DRR_REG);
40195a7f10eSVladimir Barinov 				dev->buf_len--;
40295a7f10eSVladimir Barinov 				if (dev->buf_len)
40395a7f10eSVladimir Barinov 					continue;
40495a7f10eSVladimir Barinov 
40595a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
40695a7f10eSVladimir Barinov 					DAVINCI_I2C_STR_REG,
407b73a9aecSTroy Kisky 					DAVINCI_I2C_IMR_RRDY);
40895a7f10eSVladimir Barinov 			} else
40995a7f10eSVladimir Barinov 				dev_err(dev->dev, "RDR IRQ while no "
41095a7f10eSVladimir Barinov 					"data requested\n");
41195a7f10eSVladimir Barinov 			break;
41295a7f10eSVladimir Barinov 
41395a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_XRDY:
41495a7f10eSVladimir Barinov 			if (dev->buf_len) {
41595a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
41695a7f10eSVladimir Barinov 						      *dev->buf++);
41795a7f10eSVladimir Barinov 				dev->buf_len--;
41895a7f10eSVladimir Barinov 				if (dev->buf_len)
41995a7f10eSVladimir Barinov 					continue;
42095a7f10eSVladimir Barinov 
42195a7f10eSVladimir Barinov 				w = davinci_i2c_read_reg(dev,
42295a7f10eSVladimir Barinov 							 DAVINCI_I2C_IMR_REG);
42395a7f10eSVladimir Barinov 				MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 0);
42495a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
42595a7f10eSVladimir Barinov 						      DAVINCI_I2C_IMR_REG,
42695a7f10eSVladimir Barinov 						      w);
42795a7f10eSVladimir Barinov 			} else
42895a7f10eSVladimir Barinov 				dev_err(dev->dev, "TDR IRQ while no data to "
42995a7f10eSVladimir Barinov 					"send\n");
43095a7f10eSVladimir Barinov 			break;
43195a7f10eSVladimir Barinov 
43295a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_SCD:
433b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
434b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
43595a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
43695a7f10eSVladimir Barinov 			break;
43795a7f10eSVladimir Barinov 
43895a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AAS:
43995a7f10eSVladimir Barinov 			dev_warn(dev->dev, "Address as slave interrupt\n");
44095a7f10eSVladimir Barinov 		}/* switch */
44195a7f10eSVladimir Barinov 	}/* while */
44295a7f10eSVladimir Barinov 
44395a7f10eSVladimir Barinov 	return count ? IRQ_HANDLED : IRQ_NONE;
44495a7f10eSVladimir Barinov }
44595a7f10eSVladimir Barinov 
44695a7f10eSVladimir Barinov static struct i2c_algorithm i2c_davinci_algo = {
44795a7f10eSVladimir Barinov 	.master_xfer	= i2c_davinci_xfer,
44895a7f10eSVladimir Barinov 	.functionality	= i2c_davinci_func,
44995a7f10eSVladimir Barinov };
45095a7f10eSVladimir Barinov 
45195a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev)
45295a7f10eSVladimir Barinov {
45395a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev;
45495a7f10eSVladimir Barinov 	struct i2c_adapter *adap;
45595a7f10eSVladimir Barinov 	struct resource *mem, *irq, *ioarea;
45695a7f10eSVladimir Barinov 	int r;
45795a7f10eSVladimir Barinov 
45895a7f10eSVladimir Barinov 	/* NOTE: driver uses the static register mapping */
45995a7f10eSVladimir Barinov 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
46095a7f10eSVladimir Barinov 	if (!mem) {
46195a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "no mem resource?\n");
46295a7f10eSVladimir Barinov 		return -ENODEV;
46395a7f10eSVladimir Barinov 	}
46495a7f10eSVladimir Barinov 
46595a7f10eSVladimir Barinov 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
46695a7f10eSVladimir Barinov 	if (!irq) {
46795a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "no irq resource?\n");
46895a7f10eSVladimir Barinov 		return -ENODEV;
46995a7f10eSVladimir Barinov 	}
47095a7f10eSVladimir Barinov 
47195a7f10eSVladimir Barinov 	ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
47295a7f10eSVladimir Barinov 				    pdev->name);
47395a7f10eSVladimir Barinov 	if (!ioarea) {
47495a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "I2C region already claimed\n");
47595a7f10eSVladimir Barinov 		return -EBUSY;
47695a7f10eSVladimir Barinov 	}
47795a7f10eSVladimir Barinov 
47895a7f10eSVladimir Barinov 	dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL);
47995a7f10eSVladimir Barinov 	if (!dev) {
48095a7f10eSVladimir Barinov 		r = -ENOMEM;
48195a7f10eSVladimir Barinov 		goto err_release_region;
48295a7f10eSVladimir Barinov 	}
48395a7f10eSVladimir Barinov 
48495a7f10eSVladimir Barinov 	dev->dev = get_device(&pdev->dev);
48595a7f10eSVladimir Barinov 	dev->irq = irq->start;
48695a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, dev);
48795a7f10eSVladimir Barinov 
48895a7f10eSVladimir Barinov 	dev->clk = clk_get(&pdev->dev, "I2CCLK");
48995a7f10eSVladimir Barinov 	if (IS_ERR(dev->clk)) {
49095a7f10eSVladimir Barinov 		r = -ENODEV;
49195a7f10eSVladimir Barinov 		goto err_free_mem;
49295a7f10eSVladimir Barinov 	}
49395a7f10eSVladimir Barinov 	clk_enable(dev->clk);
49495a7f10eSVladimir Barinov 
49595a7f10eSVladimir Barinov 	dev->base = (void __iomem *)IO_ADDRESS(mem->start);
49695a7f10eSVladimir Barinov 	i2c_davinci_init(dev);
49795a7f10eSVladimir Barinov 
49895a7f10eSVladimir Barinov 	r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev);
49995a7f10eSVladimir Barinov 	if (r) {
50095a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
50195a7f10eSVladimir Barinov 		goto err_unuse_clocks;
50295a7f10eSVladimir Barinov 	}
50395a7f10eSVladimir Barinov 
50495a7f10eSVladimir Barinov 	adap = &dev->adapter;
50595a7f10eSVladimir Barinov 	i2c_set_adapdata(adap, dev);
50695a7f10eSVladimir Barinov 	adap->owner = THIS_MODULE;
50795a7f10eSVladimir Barinov 	adap->class = I2C_CLASS_HWMON;
50895a7f10eSVladimir Barinov 	strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
50995a7f10eSVladimir Barinov 	adap->algo = &i2c_davinci_algo;
51095a7f10eSVladimir Barinov 	adap->dev.parent = &pdev->dev;
51195a7f10eSVladimir Barinov 
51295a7f10eSVladimir Barinov 	/* FIXME */
51395a7f10eSVladimir Barinov 	adap->timeout = 1;
51495a7f10eSVladimir Barinov 
51595a7f10eSVladimir Barinov 	adap->nr = pdev->id;
51695a7f10eSVladimir Barinov 	r = i2c_add_numbered_adapter(adap);
51795a7f10eSVladimir Barinov 	if (r) {
51895a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure adding adapter\n");
51995a7f10eSVladimir Barinov 		goto err_free_irq;
52095a7f10eSVladimir Barinov 	}
52195a7f10eSVladimir Barinov 
52295a7f10eSVladimir Barinov 	return 0;
52395a7f10eSVladimir Barinov 
52495a7f10eSVladimir Barinov err_free_irq:
52595a7f10eSVladimir Barinov 	free_irq(dev->irq, dev);
52695a7f10eSVladimir Barinov err_unuse_clocks:
52795a7f10eSVladimir Barinov 	clk_disable(dev->clk);
52895a7f10eSVladimir Barinov 	clk_put(dev->clk);
52995a7f10eSVladimir Barinov 	dev->clk = NULL;
53095a7f10eSVladimir Barinov err_free_mem:
53195a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, NULL);
53295a7f10eSVladimir Barinov 	put_device(&pdev->dev);
53395a7f10eSVladimir Barinov 	kfree(dev);
53495a7f10eSVladimir Barinov err_release_region:
53595a7f10eSVladimir Barinov 	release_mem_region(mem->start, (mem->end - mem->start) + 1);
53695a7f10eSVladimir Barinov 
53795a7f10eSVladimir Barinov 	return r;
53895a7f10eSVladimir Barinov }
53995a7f10eSVladimir Barinov 
54095a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev)
54195a7f10eSVladimir Barinov {
54295a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
54395a7f10eSVladimir Barinov 	struct resource *mem;
54495a7f10eSVladimir Barinov 
54595a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, NULL);
54695a7f10eSVladimir Barinov 	i2c_del_adapter(&dev->adapter);
54795a7f10eSVladimir Barinov 	put_device(&pdev->dev);
54895a7f10eSVladimir Barinov 
54995a7f10eSVladimir Barinov 	clk_disable(dev->clk);
55095a7f10eSVladimir Barinov 	clk_put(dev->clk);
55195a7f10eSVladimir Barinov 	dev->clk = NULL;
55295a7f10eSVladimir Barinov 
55395a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
55495a7f10eSVladimir Barinov 	free_irq(IRQ_I2C, dev);
55595a7f10eSVladimir Barinov 	kfree(dev);
55695a7f10eSVladimir Barinov 
55795a7f10eSVladimir Barinov 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
55895a7f10eSVladimir Barinov 	release_mem_region(mem->start, (mem->end - mem->start) + 1);
55995a7f10eSVladimir Barinov 	return 0;
56095a7f10eSVladimir Barinov }
56195a7f10eSVladimir Barinov 
562add8eda7SKay Sievers /* work with hotplug and coldplug */
563add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci");
564add8eda7SKay Sievers 
56595a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = {
56695a7f10eSVladimir Barinov 	.probe		= davinci_i2c_probe,
56795a7f10eSVladimir Barinov 	.remove		= davinci_i2c_remove,
56895a7f10eSVladimir Barinov 	.driver		= {
56995a7f10eSVladimir Barinov 		.name	= "i2c_davinci",
57095a7f10eSVladimir Barinov 		.owner	= THIS_MODULE,
57195a7f10eSVladimir Barinov 	},
57295a7f10eSVladimir Barinov };
57395a7f10eSVladimir Barinov 
57495a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */
57595a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void)
57695a7f10eSVladimir Barinov {
57795a7f10eSVladimir Barinov 	return platform_driver_register(&davinci_i2c_driver);
57895a7f10eSVladimir Barinov }
57995a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver);
58095a7f10eSVladimir Barinov 
58195a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void)
58295a7f10eSVladimir Barinov {
58395a7f10eSVladimir Barinov 	platform_driver_unregister(&davinci_i2c_driver);
58495a7f10eSVladimir Barinov }
58595a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver);
58695a7f10eSVladimir Barinov 
58795a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India");
58895a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
58995a7f10eSVladimir Barinov MODULE_LICENSE("GPL");
590