xref: /openbmc/linux/drivers/i2c/busses/i2c-davinci.c (revision e164ddeeb82920c5b1470b6585767a000c8b0e45)
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 
39a09e64fbSRussell King #include <mach/hardware.h>
4095a7f10eSVladimir Barinov 
41a09e64fbSRussell King #include <mach/i2c.h>
4295a7f10eSVladimir Barinov 
4395a7f10eSVladimir Barinov /* ----- global defines ----------------------------------------------- */
4495a7f10eSVladimir Barinov 
4595a7f10eSVladimir Barinov #define DAVINCI_I2C_TIMEOUT	(1*HZ)
4695a7f10eSVladimir Barinov #define I2C_DAVINCI_INTR_ALL    (DAVINCI_I2C_IMR_AAS | \
4795a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_SCD | \
4895a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_ARDY | \
4995a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_NACK | \
5095a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_AL)
5195a7f10eSVladimir Barinov 
5295a7f10eSVladimir Barinov #define DAVINCI_I2C_OAR_REG	0x00
5395a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_REG	0x04
5495a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_REG	0x08
5595a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKL_REG	0x0c
5695a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKH_REG	0x10
5795a7f10eSVladimir Barinov #define DAVINCI_I2C_CNT_REG	0x14
5895a7f10eSVladimir Barinov #define DAVINCI_I2C_DRR_REG	0x18
5995a7f10eSVladimir Barinov #define DAVINCI_I2C_SAR_REG	0x1c
6095a7f10eSVladimir Barinov #define DAVINCI_I2C_DXR_REG	0x20
6195a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_REG	0x24
6295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_REG	0x28
6395a7f10eSVladimir Barinov #define DAVINCI_I2C_EMDR_REG	0x2c
6495a7f10eSVladimir Barinov #define DAVINCI_I2C_PSC_REG	0x30
6595a7f10eSVladimir Barinov 
6695a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AAS	0x07
6795a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_SCD	0x06
6895a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_XRDY	0x05
6995a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_RDR	0x04
7095a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_ARDY	0x03
7195a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_NACK	0x02
7295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AL	0x01
7395a7f10eSVladimir Barinov 
7495a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_BB	(1 << 12)
7595a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_RSFULL	(1 << 11)
7695a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_SCD	(1 << 5)
7795a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_ARDY	(1 << 2)
7895a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_NACK	(1 << 1)
7995a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_AL	(1 << 0)
8095a7f10eSVladimir Barinov 
8195a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_NACK	(1 << 15)
8295a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_STT	(1 << 13)
8395a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_STP	(1 << 11)
8495a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_MST	(1 << 10)
8595a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_TRX	(1 << 9)
8695a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_XA	(1 << 8)
875a0d5f5fSTroy Kisky #define DAVINCI_I2C_MDR_RM	(1 << 7)
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;
1155a0d5f5fSTroy Kisky 	u8			terminate;
11695a7f10eSVladimir Barinov 	struct i2c_adapter	adapter;
11795a7f10eSVladimir Barinov };
11895a7f10eSVladimir Barinov 
11995a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */
12095a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
12195a7f10eSVladimir Barinov 	.bus_freq	= 100,
12295a7f10eSVladimir Barinov 	.bus_delay	= 0,
12395a7f10eSVladimir Barinov };
12495a7f10eSVladimir Barinov 
12595a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
12695a7f10eSVladimir Barinov 					 int reg, u16 val)
12795a7f10eSVladimir Barinov {
12895a7f10eSVladimir Barinov 	__raw_writew(val, i2c_dev->base + reg);
12995a7f10eSVladimir Barinov }
13095a7f10eSVladimir Barinov 
13195a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
13295a7f10eSVladimir Barinov {
13395a7f10eSVladimir Barinov 	return __raw_readw(i2c_dev->base + reg);
13495a7f10eSVladimir Barinov }
13595a7f10eSVladimir Barinov 
13695a7f10eSVladimir Barinov /*
13795a7f10eSVladimir Barinov  * This functions configures I2C and brings I2C out of reset.
13895a7f10eSVladimir Barinov  * This function is called during I2C init function. This function
13995a7f10eSVladimir Barinov  * also gets called if I2C encounters any errors.
14095a7f10eSVladimir Barinov  */
14195a7f10eSVladimir Barinov static int i2c_davinci_init(struct davinci_i2c_dev *dev)
14295a7f10eSVladimir Barinov {
14395a7f10eSVladimir Barinov 	struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
14495a7f10eSVladimir Barinov 	u16 psc;
14595a7f10eSVladimir Barinov 	u32 clk;
146cc99ff70STroy Kisky 	u32 d;
14795a7f10eSVladimir Barinov 	u32 clkh;
14895a7f10eSVladimir Barinov 	u32 clkl;
14995a7f10eSVladimir Barinov 	u32 input_clock = clk_get_rate(dev->clk);
15095a7f10eSVladimir Barinov 	u16 w;
15195a7f10eSVladimir Barinov 
15295a7f10eSVladimir Barinov 	if (!pdata)
15395a7f10eSVladimir Barinov 		pdata = &davinci_i2c_platform_data_default;
15495a7f10eSVladimir Barinov 
15595a7f10eSVladimir Barinov 	/* put I2C into reset */
15695a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
15795a7f10eSVladimir Barinov 	MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 0);
15895a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
15995a7f10eSVladimir Barinov 
16095a7f10eSVladimir Barinov 	/* NOTE: I2C Clock divider programming info
16195a7f10eSVladimir Barinov 	 * As per I2C specs the following formulas provide prescaler
16295a7f10eSVladimir Barinov 	 * and low/high divider values
16395a7f10eSVladimir Barinov 	 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
16495a7f10eSVladimir Barinov 	 *                       module clk
16595a7f10eSVladimir Barinov 	 *
16695a7f10eSVladimir Barinov 	 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
16795a7f10eSVladimir Barinov 	 *
16895a7f10eSVladimir Barinov 	 * Thus,
16995a7f10eSVladimir Barinov 	 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
17095a7f10eSVladimir Barinov 	 *
17195a7f10eSVladimir Barinov 	 * where if PSC == 0, d = 7,
17295a7f10eSVladimir Barinov 	 *       if PSC == 1, d = 6
17395a7f10eSVladimir Barinov 	 *       if PSC > 1 , d = 5
17495a7f10eSVladimir Barinov 	 */
17595a7f10eSVladimir Barinov 
176cc99ff70STroy Kisky 	/* get minimum of 7 MHz clock, but max of 12 MHz */
177cc99ff70STroy Kisky 	psc = (input_clock / 7000000) - 1;
178cc99ff70STroy Kisky 	if ((input_clock / (psc + 1)) > 12000000)
179cc99ff70STroy Kisky 		psc++;	/* better to run under spec than over */
180cc99ff70STroy Kisky 	d = (psc >= 2) ? 5 : 7 - psc;
18195a7f10eSVladimir Barinov 
182cc99ff70STroy Kisky 	clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
183cc99ff70STroy Kisky 	clkh = clk >> 1;
18495a7f10eSVladimir Barinov 	clkl = clk - clkh;
18595a7f10eSVladimir Barinov 
18695a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
18795a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
18895a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
18995a7f10eSVladimir Barinov 
190cc99ff70STroy Kisky 	dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
19195a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "PSC  = %d\n",
19295a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
19395a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKL = %d\n",
19495a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
19595a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKH = %d\n",
19695a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
197cc99ff70STroy Kisky 	dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
198cc99ff70STroy Kisky 		pdata->bus_freq, pdata->bus_delay);
19995a7f10eSVladimir Barinov 
20095a7f10eSVladimir Barinov 	/* Take the I2C module out of reset: */
20195a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
20295a7f10eSVladimir Barinov 	MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 1);
20395a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
20495a7f10eSVladimir Barinov 
20595a7f10eSVladimir Barinov 	/* Enable interrupts */
20695a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
20795a7f10eSVladimir Barinov 
20895a7f10eSVladimir Barinov 	return 0;
20995a7f10eSVladimir Barinov }
21095a7f10eSVladimir Barinov 
21195a7f10eSVladimir Barinov /*
21295a7f10eSVladimir Barinov  * Waiting for bus not busy
21395a7f10eSVladimir Barinov  */
21495a7f10eSVladimir Barinov static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
21595a7f10eSVladimir Barinov 					 char allow_sleep)
21695a7f10eSVladimir Barinov {
21795a7f10eSVladimir Barinov 	unsigned long timeout;
21895a7f10eSVladimir Barinov 
21998a679caSJean Delvare 	timeout = jiffies + dev->adapter.timeout;
22095a7f10eSVladimir Barinov 	while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
22195a7f10eSVladimir Barinov 	       & DAVINCI_I2C_STR_BB) {
22295a7f10eSVladimir Barinov 		if (time_after(jiffies, timeout)) {
22395a7f10eSVladimir Barinov 			dev_warn(dev->dev,
22495a7f10eSVladimir Barinov 				 "timeout waiting for bus ready\n");
22595a7f10eSVladimir Barinov 			return -ETIMEDOUT;
22695a7f10eSVladimir Barinov 		}
22795a7f10eSVladimir Barinov 		if (allow_sleep)
22895a7f10eSVladimir Barinov 			schedule_timeout(1);
22995a7f10eSVladimir Barinov 	}
23095a7f10eSVladimir Barinov 
23195a7f10eSVladimir Barinov 	return 0;
23295a7f10eSVladimir Barinov }
23395a7f10eSVladimir Barinov 
23495a7f10eSVladimir Barinov /*
23595a7f10eSVladimir Barinov  * Low level master read/write transaction. This function is called
23695a7f10eSVladimir Barinov  * from i2c_davinci_xfer.
23795a7f10eSVladimir Barinov  */
23895a7f10eSVladimir Barinov static int
23995a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
24095a7f10eSVladimir Barinov {
24195a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
24295a7f10eSVladimir Barinov 	struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
24395a7f10eSVladimir Barinov 	u32 flag;
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 
2642e743787STroy Kisky 	INIT_COMPLETION(dev->cmd_complete);
26595a7f10eSVladimir Barinov 	dev->cmd_err = 0;
26695a7f10eSVladimir Barinov 
26795a7f10eSVladimir Barinov 	/* Take I2C out of reset, configure it as master and set the
26895a7f10eSVladimir Barinov 	 * start bit */
26995a7f10eSVladimir Barinov 	flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST | DAVINCI_I2C_MDR_STT;
27095a7f10eSVladimir Barinov 
27195a7f10eSVladimir Barinov 	/* if the slave address is ten bit address, enable XA bit */
27295a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_TEN)
27395a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_XA;
27495a7f10eSVladimir Barinov 	if (!(msg->flags & I2C_M_RD))
27595a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_TRX;
27695a7f10eSVladimir Barinov 	if (stop)
27795a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_STP;
27895a7f10eSVladimir Barinov 
27995a7f10eSVladimir Barinov 	/* Enable receive or transmit interrupts */
28095a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
28195a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_RD)
28295a7f10eSVladimir Barinov 		MOD_REG_BIT(w, DAVINCI_I2C_IMR_RRDY, 1);
28395a7f10eSVladimir Barinov 	else
28495a7f10eSVladimir Barinov 		MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 1);
28595a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
28695a7f10eSVladimir Barinov 
2875a0d5f5fSTroy Kisky 	dev->terminate = 0;
28895a7f10eSVladimir Barinov 	/* write the data into mode register */
28995a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
29095a7f10eSVladimir Barinov 
29195a7f10eSVladimir Barinov 	r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
29298a679caSJean Delvare 						      dev->adapter.timeout);
29395a7f10eSVladimir Barinov 	if (r == 0) {
29495a7f10eSVladimir Barinov 		dev_err(dev->dev, "controller timed out\n");
29595a7f10eSVladimir Barinov 		i2c_davinci_init(dev);
2965a0d5f5fSTroy Kisky 		dev->buf_len = 0;
29795a7f10eSVladimir Barinov 		return -ETIMEDOUT;
29895a7f10eSVladimir Barinov 	}
2995a0d5f5fSTroy Kisky 	if (dev->buf_len) {
3005a0d5f5fSTroy Kisky 		/* This should be 0 if all bytes were transferred
3015a0d5f5fSTroy Kisky 		 * or dev->cmd_err denotes an error.
3025a0d5f5fSTroy Kisky 		 * A signal may have aborted the transfer.
3035a0d5f5fSTroy Kisky 		 */
3045a0d5f5fSTroy Kisky 		if (r >= 0) {
3055a0d5f5fSTroy Kisky 			dev_err(dev->dev, "abnormal termination buf_len=%i\n",
3065a0d5f5fSTroy Kisky 				dev->buf_len);
3075a0d5f5fSTroy Kisky 			r = -EREMOTEIO;
3085a0d5f5fSTroy Kisky 		}
3095a0d5f5fSTroy Kisky 		dev->terminate = 1;
3105a0d5f5fSTroy Kisky 		wmb();
3115a0d5f5fSTroy Kisky 		dev->buf_len = 0;
3125a0d5f5fSTroy Kisky 	}
3135a0d5f5fSTroy Kisky 	if (r < 0)
3145a0d5f5fSTroy Kisky 		return r;
31595a7f10eSVladimir Barinov 
31695a7f10eSVladimir Barinov 	/* no error */
31795a7f10eSVladimir Barinov 	if (likely(!dev->cmd_err))
31895a7f10eSVladimir Barinov 		return msg->len;
31995a7f10eSVladimir Barinov 
32095a7f10eSVladimir Barinov 	/* We have an error */
32195a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
32295a7f10eSVladimir Barinov 		i2c_davinci_init(dev);
32395a7f10eSVladimir Barinov 		return -EIO;
32495a7f10eSVladimir Barinov 	}
32595a7f10eSVladimir Barinov 
32695a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
32795a7f10eSVladimir Barinov 		if (msg->flags & I2C_M_IGNORE_NAK)
32895a7f10eSVladimir Barinov 			return msg->len;
32995a7f10eSVladimir Barinov 		if (stop) {
33095a7f10eSVladimir Barinov 			w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
33195a7f10eSVladimir Barinov 			MOD_REG_BIT(w, DAVINCI_I2C_MDR_STP, 1);
33295a7f10eSVladimir Barinov 			davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
33395a7f10eSVladimir Barinov 		}
33495a7f10eSVladimir Barinov 		return -EREMOTEIO;
33595a7f10eSVladimir Barinov 	}
33695a7f10eSVladimir Barinov 	return -EIO;
33795a7f10eSVladimir Barinov }
33895a7f10eSVladimir Barinov 
33995a7f10eSVladimir Barinov /*
34095a7f10eSVladimir Barinov  * Prepare controller for a transaction and call i2c_davinci_xfer_msg
34195a7f10eSVladimir Barinov  */
34295a7f10eSVladimir Barinov static int
34395a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
34495a7f10eSVladimir Barinov {
34595a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
34695a7f10eSVladimir Barinov 	int i;
34795a7f10eSVladimir Barinov 	int ret;
34895a7f10eSVladimir Barinov 
34908882d20SHarvey Harrison 	dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
35095a7f10eSVladimir Barinov 
35195a7f10eSVladimir Barinov 	ret = i2c_davinci_wait_bus_not_busy(dev, 1);
35295a7f10eSVladimir Barinov 	if (ret < 0) {
35395a7f10eSVladimir Barinov 		dev_warn(dev->dev, "timeout waiting for bus ready\n");
35495a7f10eSVladimir Barinov 		return ret;
35595a7f10eSVladimir Barinov 	}
35695a7f10eSVladimir Barinov 
35795a7f10eSVladimir Barinov 	for (i = 0; i < num; i++) {
35895a7f10eSVladimir Barinov 		ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
359d868caa1STroy Kisky 		dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
360d868caa1STroy Kisky 			ret);
36195a7f10eSVladimir Barinov 		if (ret < 0)
36295a7f10eSVladimir Barinov 			return ret;
36395a7f10eSVladimir Barinov 	}
36495a7f10eSVladimir Barinov 	return num;
36595a7f10eSVladimir Barinov }
36695a7f10eSVladimir Barinov 
36795a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap)
36895a7f10eSVladimir Barinov {
36995a7f10eSVladimir Barinov 	return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
37095a7f10eSVladimir Barinov }
37195a7f10eSVladimir Barinov 
3725a0d5f5fSTroy Kisky static void terminate_read(struct davinci_i2c_dev *dev)
3735a0d5f5fSTroy Kisky {
3745a0d5f5fSTroy Kisky 	u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
3755a0d5f5fSTroy Kisky 	w |= DAVINCI_I2C_MDR_NACK;
3765a0d5f5fSTroy Kisky 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
3775a0d5f5fSTroy Kisky 
3785a0d5f5fSTroy Kisky 	/* Throw away data */
3795a0d5f5fSTroy Kisky 	davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
3805a0d5f5fSTroy Kisky 	if (!dev->terminate)
3815a0d5f5fSTroy Kisky 		dev_err(dev->dev, "RDR IRQ while no data requested\n");
3825a0d5f5fSTroy Kisky }
3835a0d5f5fSTroy Kisky static void terminate_write(struct davinci_i2c_dev *dev)
3845a0d5f5fSTroy Kisky {
3855a0d5f5fSTroy Kisky 	u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
3865a0d5f5fSTroy Kisky 	w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
3875a0d5f5fSTroy Kisky 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
3885a0d5f5fSTroy Kisky 
3895a0d5f5fSTroy Kisky 	if (!dev->terminate)
3905a0d5f5fSTroy Kisky 		dev_err(dev->dev, "TDR IRQ while no data to send\n");
3915a0d5f5fSTroy Kisky }
3925a0d5f5fSTroy Kisky 
39395a7f10eSVladimir Barinov /*
39495a7f10eSVladimir Barinov  * Interrupt service routine. This gets called whenever an I2C interrupt
39595a7f10eSVladimir Barinov  * occurs.
39695a7f10eSVladimir Barinov  */
39795a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
39895a7f10eSVladimir Barinov {
39995a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = dev_id;
40095a7f10eSVladimir Barinov 	u32 stat;
40195a7f10eSVladimir Barinov 	int count = 0;
40295a7f10eSVladimir Barinov 	u16 w;
40395a7f10eSVladimir Barinov 
40495a7f10eSVladimir Barinov 	while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
40508882d20SHarvey Harrison 		dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
40695a7f10eSVladimir Barinov 		if (count++ == 100) {
40795a7f10eSVladimir Barinov 			dev_warn(dev->dev, "Too much work in one IRQ\n");
40895a7f10eSVladimir Barinov 			break;
40995a7f10eSVladimir Barinov 		}
41095a7f10eSVladimir Barinov 
41195a7f10eSVladimir Barinov 		switch (stat) {
41295a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AL:
4135a0d5f5fSTroy Kisky 			/* Arbitration lost, must retry */
41495a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_AL;
4155a0d5f5fSTroy Kisky 			dev->buf_len = 0;
41695a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
41795a7f10eSVladimir Barinov 			break;
41895a7f10eSVladimir Barinov 
41995a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_NACK:
42095a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_NACK;
4215a0d5f5fSTroy Kisky 			dev->buf_len = 0;
42295a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
42395a7f10eSVladimir Barinov 			break;
42495a7f10eSVladimir Barinov 
42595a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_ARDY:
426b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
427b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
42895a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
42995a7f10eSVladimir Barinov 			break;
43095a7f10eSVladimir Barinov 
43195a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_RDR:
43295a7f10eSVladimir Barinov 			if (dev->buf_len) {
43395a7f10eSVladimir Barinov 				*dev->buf++ =
43495a7f10eSVladimir Barinov 				    davinci_i2c_read_reg(dev,
43595a7f10eSVladimir Barinov 							 DAVINCI_I2C_DRR_REG);
43695a7f10eSVladimir Barinov 				dev->buf_len--;
43795a7f10eSVladimir Barinov 				if (dev->buf_len)
43895a7f10eSVladimir Barinov 					continue;
43995a7f10eSVladimir Barinov 
44095a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
44195a7f10eSVladimir Barinov 					DAVINCI_I2C_STR_REG,
442b73a9aecSTroy Kisky 					DAVINCI_I2C_IMR_RRDY);
4435a0d5f5fSTroy Kisky 			} else {
4445a0d5f5fSTroy Kisky 				/* signal can terminate transfer */
4455a0d5f5fSTroy Kisky 				terminate_read(dev);
4465a0d5f5fSTroy Kisky 			}
44795a7f10eSVladimir Barinov 			break;
44895a7f10eSVladimir Barinov 
44995a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_XRDY:
45095a7f10eSVladimir Barinov 			if (dev->buf_len) {
45195a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
45295a7f10eSVladimir Barinov 						      *dev->buf++);
45395a7f10eSVladimir Barinov 				dev->buf_len--;
45495a7f10eSVladimir Barinov 				if (dev->buf_len)
45595a7f10eSVladimir Barinov 					continue;
45695a7f10eSVladimir Barinov 
45795a7f10eSVladimir Barinov 				w = davinci_i2c_read_reg(dev,
45895a7f10eSVladimir Barinov 							 DAVINCI_I2C_IMR_REG);
45995a7f10eSVladimir Barinov 				MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 0);
46095a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
46195a7f10eSVladimir Barinov 						      DAVINCI_I2C_IMR_REG,
46295a7f10eSVladimir Barinov 						      w);
4635a0d5f5fSTroy Kisky 			} else {
4645a0d5f5fSTroy Kisky 				/* signal can terminate transfer */
4655a0d5f5fSTroy Kisky 				terminate_write(dev);
4665a0d5f5fSTroy Kisky 			}
46795a7f10eSVladimir Barinov 			break;
46895a7f10eSVladimir Barinov 
46995a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_SCD:
470b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
471b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
47295a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
47395a7f10eSVladimir Barinov 			break;
47495a7f10eSVladimir Barinov 
47595a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AAS:
47695a7f10eSVladimir Barinov 			dev_warn(dev->dev, "Address as slave interrupt\n");
47795a7f10eSVladimir Barinov 		}/* switch */
47895a7f10eSVladimir Barinov 	}/* while */
47995a7f10eSVladimir Barinov 
48095a7f10eSVladimir Barinov 	return count ? IRQ_HANDLED : IRQ_NONE;
48195a7f10eSVladimir Barinov }
48295a7f10eSVladimir Barinov 
48395a7f10eSVladimir Barinov static struct i2c_algorithm i2c_davinci_algo = {
48495a7f10eSVladimir Barinov 	.master_xfer	= i2c_davinci_xfer,
48595a7f10eSVladimir Barinov 	.functionality	= i2c_davinci_func,
48695a7f10eSVladimir Barinov };
48795a7f10eSVladimir Barinov 
48895a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev)
48995a7f10eSVladimir Barinov {
49095a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev;
49195a7f10eSVladimir Barinov 	struct i2c_adapter *adap;
49295a7f10eSVladimir Barinov 	struct resource *mem, *irq, *ioarea;
49395a7f10eSVladimir Barinov 	int r;
49495a7f10eSVladimir Barinov 
49595a7f10eSVladimir Barinov 	/* NOTE: driver uses the static register mapping */
49695a7f10eSVladimir Barinov 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
49795a7f10eSVladimir Barinov 	if (!mem) {
49895a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "no mem resource?\n");
49995a7f10eSVladimir Barinov 		return -ENODEV;
50095a7f10eSVladimir Barinov 	}
50195a7f10eSVladimir Barinov 
50295a7f10eSVladimir Barinov 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
50395a7f10eSVladimir Barinov 	if (!irq) {
50495a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "no irq resource?\n");
50595a7f10eSVladimir Barinov 		return -ENODEV;
50695a7f10eSVladimir Barinov 	}
50795a7f10eSVladimir Barinov 
50895a7f10eSVladimir Barinov 	ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
50995a7f10eSVladimir Barinov 				    pdev->name);
51095a7f10eSVladimir Barinov 	if (!ioarea) {
51195a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "I2C region already claimed\n");
51295a7f10eSVladimir Barinov 		return -EBUSY;
51395a7f10eSVladimir Barinov 	}
51495a7f10eSVladimir Barinov 
51595a7f10eSVladimir Barinov 	dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL);
51695a7f10eSVladimir Barinov 	if (!dev) {
51795a7f10eSVladimir Barinov 		r = -ENOMEM;
51895a7f10eSVladimir Barinov 		goto err_release_region;
51995a7f10eSVladimir Barinov 	}
52095a7f10eSVladimir Barinov 
5212e743787STroy Kisky 	init_completion(&dev->cmd_complete);
52295a7f10eSVladimir Barinov 	dev->dev = get_device(&pdev->dev);
52395a7f10eSVladimir Barinov 	dev->irq = irq->start;
52495a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, dev);
52595a7f10eSVladimir Barinov 
526*e164ddeeSKevin Hilman 	dev->clk = clk_get(&pdev->dev, NULL);
52795a7f10eSVladimir Barinov 	if (IS_ERR(dev->clk)) {
52895a7f10eSVladimir Barinov 		r = -ENODEV;
52995a7f10eSVladimir Barinov 		goto err_free_mem;
53095a7f10eSVladimir Barinov 	}
53195a7f10eSVladimir Barinov 	clk_enable(dev->clk);
53295a7f10eSVladimir Barinov 
53395a7f10eSVladimir Barinov 	dev->base = (void __iomem *)IO_ADDRESS(mem->start);
53495a7f10eSVladimir Barinov 	i2c_davinci_init(dev);
53595a7f10eSVladimir Barinov 
53695a7f10eSVladimir Barinov 	r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev);
53795a7f10eSVladimir Barinov 	if (r) {
53895a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
53995a7f10eSVladimir Barinov 		goto err_unuse_clocks;
54095a7f10eSVladimir Barinov 	}
54195a7f10eSVladimir Barinov 
54295a7f10eSVladimir Barinov 	adap = &dev->adapter;
54395a7f10eSVladimir Barinov 	i2c_set_adapdata(adap, dev);
54495a7f10eSVladimir Barinov 	adap->owner = THIS_MODULE;
54595a7f10eSVladimir Barinov 	adap->class = I2C_CLASS_HWMON;
54695a7f10eSVladimir Barinov 	strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
54795a7f10eSVladimir Barinov 	adap->algo = &i2c_davinci_algo;
54895a7f10eSVladimir Barinov 	adap->dev.parent = &pdev->dev;
54998a679caSJean Delvare 	adap->timeout = DAVINCI_I2C_TIMEOUT;
55095a7f10eSVladimir Barinov 
55195a7f10eSVladimir Barinov 	adap->nr = pdev->id;
55295a7f10eSVladimir Barinov 	r = i2c_add_numbered_adapter(adap);
55395a7f10eSVladimir Barinov 	if (r) {
55495a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure adding adapter\n");
55595a7f10eSVladimir Barinov 		goto err_free_irq;
55695a7f10eSVladimir Barinov 	}
55795a7f10eSVladimir Barinov 
55895a7f10eSVladimir Barinov 	return 0;
55995a7f10eSVladimir Barinov 
56095a7f10eSVladimir Barinov err_free_irq:
56195a7f10eSVladimir Barinov 	free_irq(dev->irq, dev);
56295a7f10eSVladimir Barinov err_unuse_clocks:
56395a7f10eSVladimir Barinov 	clk_disable(dev->clk);
56495a7f10eSVladimir Barinov 	clk_put(dev->clk);
56595a7f10eSVladimir Barinov 	dev->clk = NULL;
56695a7f10eSVladimir Barinov err_free_mem:
56795a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, NULL);
56895a7f10eSVladimir Barinov 	put_device(&pdev->dev);
56995a7f10eSVladimir Barinov 	kfree(dev);
57095a7f10eSVladimir Barinov err_release_region:
57195a7f10eSVladimir Barinov 	release_mem_region(mem->start, (mem->end - mem->start) + 1);
57295a7f10eSVladimir Barinov 
57395a7f10eSVladimir Barinov 	return r;
57495a7f10eSVladimir Barinov }
57595a7f10eSVladimir Barinov 
57695a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev)
57795a7f10eSVladimir Barinov {
57895a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
57995a7f10eSVladimir Barinov 	struct resource *mem;
58095a7f10eSVladimir Barinov 
58195a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, NULL);
58295a7f10eSVladimir Barinov 	i2c_del_adapter(&dev->adapter);
58395a7f10eSVladimir Barinov 	put_device(&pdev->dev);
58495a7f10eSVladimir Barinov 
58595a7f10eSVladimir Barinov 	clk_disable(dev->clk);
58695a7f10eSVladimir Barinov 	clk_put(dev->clk);
58795a7f10eSVladimir Barinov 	dev->clk = NULL;
58895a7f10eSVladimir Barinov 
58995a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
59095a7f10eSVladimir Barinov 	free_irq(IRQ_I2C, dev);
59195a7f10eSVladimir Barinov 	kfree(dev);
59295a7f10eSVladimir Barinov 
59395a7f10eSVladimir Barinov 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
59495a7f10eSVladimir Barinov 	release_mem_region(mem->start, (mem->end - mem->start) + 1);
59595a7f10eSVladimir Barinov 	return 0;
59695a7f10eSVladimir Barinov }
59795a7f10eSVladimir Barinov 
598add8eda7SKay Sievers /* work with hotplug and coldplug */
599add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci");
600add8eda7SKay Sievers 
60195a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = {
60295a7f10eSVladimir Barinov 	.probe		= davinci_i2c_probe,
60395a7f10eSVladimir Barinov 	.remove		= davinci_i2c_remove,
60495a7f10eSVladimir Barinov 	.driver		= {
60595a7f10eSVladimir Barinov 		.name	= "i2c_davinci",
60695a7f10eSVladimir Barinov 		.owner	= THIS_MODULE,
60795a7f10eSVladimir Barinov 	},
60895a7f10eSVladimir Barinov };
60995a7f10eSVladimir Barinov 
61095a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */
61195a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void)
61295a7f10eSVladimir Barinov {
61395a7f10eSVladimir Barinov 	return platform_driver_register(&davinci_i2c_driver);
61495a7f10eSVladimir Barinov }
61595a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver);
61695a7f10eSVladimir Barinov 
61795a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void)
61895a7f10eSVladimir Barinov {
61995a7f10eSVladimir Barinov 	platform_driver_unregister(&davinci_i2c_driver);
62095a7f10eSVladimir Barinov }
62195a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver);
62295a7f10eSVladimir Barinov 
62395a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India");
62495a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
62595a7f10eSVladimir Barinov MODULE_LICENSE("GPL");
626