xref: /openbmc/linux/drivers/i2c/busses/i2c-davinci.c (revision add8eda7f2be781af0224241e870715cf0cfd75a)
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;
14595a7f10eSVladimir Barinov 	u32 clkh;
14695a7f10eSVladimir Barinov 	u32 clkl;
14795a7f10eSVladimir Barinov 	u32 input_clock = clk_get_rate(dev->clk);
14895a7f10eSVladimir Barinov 	u16 w;
14995a7f10eSVladimir Barinov 
15095a7f10eSVladimir Barinov 	if (!pdata)
15195a7f10eSVladimir Barinov 		pdata = &davinci_i2c_platform_data_default;
15295a7f10eSVladimir Barinov 
15395a7f10eSVladimir Barinov 	/* put I2C into reset */
15495a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
15595a7f10eSVladimir Barinov 	MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 0);
15695a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
15795a7f10eSVladimir Barinov 
15895a7f10eSVladimir Barinov 	/* NOTE: I2C Clock divider programming info
15995a7f10eSVladimir Barinov 	 * As per I2C specs the following formulas provide prescaler
16095a7f10eSVladimir Barinov 	 * and low/high divider values
16195a7f10eSVladimir Barinov 	 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
16295a7f10eSVladimir Barinov 	 *                       module clk
16395a7f10eSVladimir Barinov 	 *
16495a7f10eSVladimir Barinov 	 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
16595a7f10eSVladimir Barinov 	 *
16695a7f10eSVladimir Barinov 	 * Thus,
16795a7f10eSVladimir Barinov 	 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
16895a7f10eSVladimir Barinov 	 *
16995a7f10eSVladimir Barinov 	 * where if PSC == 0, d = 7,
17095a7f10eSVladimir Barinov 	 *       if PSC == 1, d = 6
17195a7f10eSVladimir Barinov 	 *       if PSC > 1 , d = 5
17295a7f10eSVladimir Barinov 	 */
17395a7f10eSVladimir Barinov 
17495a7f10eSVladimir Barinov 	psc = 26; /* To get 1MHz clock */
17595a7f10eSVladimir Barinov 
17695a7f10eSVladimir Barinov 	clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - 10;
17795a7f10eSVladimir Barinov 	clkh = (50 * clk) / 100;
17895a7f10eSVladimir Barinov 	clkl = clk - clkh;
17995a7f10eSVladimir Barinov 
18095a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
18195a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
18295a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
18395a7f10eSVladimir Barinov 
18495a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLK  = %d\n", clk);
18595a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "PSC  = %d\n",
18695a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
18795a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKL = %d\n",
18895a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
18995a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKH = %d\n",
19095a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
19195a7f10eSVladimir Barinov 
19295a7f10eSVladimir Barinov 	/* Take the I2C module out of reset: */
19395a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
19495a7f10eSVladimir Barinov 	MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 1);
19595a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
19695a7f10eSVladimir Barinov 
19795a7f10eSVladimir Barinov 	/* Enable interrupts */
19895a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
19995a7f10eSVladimir Barinov 
20095a7f10eSVladimir Barinov 	return 0;
20195a7f10eSVladimir Barinov }
20295a7f10eSVladimir Barinov 
20395a7f10eSVladimir Barinov /*
20495a7f10eSVladimir Barinov  * Waiting for bus not busy
20595a7f10eSVladimir Barinov  */
20695a7f10eSVladimir Barinov static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
20795a7f10eSVladimir Barinov 					 char allow_sleep)
20895a7f10eSVladimir Barinov {
20995a7f10eSVladimir Barinov 	unsigned long timeout;
21095a7f10eSVladimir Barinov 
21195a7f10eSVladimir Barinov 	timeout = jiffies + DAVINCI_I2C_TIMEOUT;
21295a7f10eSVladimir Barinov 	while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
21395a7f10eSVladimir Barinov 	       & DAVINCI_I2C_STR_BB) {
21495a7f10eSVladimir Barinov 		if (time_after(jiffies, timeout)) {
21595a7f10eSVladimir Barinov 			dev_warn(dev->dev,
21695a7f10eSVladimir Barinov 				 "timeout waiting for bus ready\n");
21795a7f10eSVladimir Barinov 			return -ETIMEDOUT;
21895a7f10eSVladimir Barinov 		}
21995a7f10eSVladimir Barinov 		if (allow_sleep)
22095a7f10eSVladimir Barinov 			schedule_timeout(1);
22195a7f10eSVladimir Barinov 	}
22295a7f10eSVladimir Barinov 
22395a7f10eSVladimir Barinov 	return 0;
22495a7f10eSVladimir Barinov }
22595a7f10eSVladimir Barinov 
22695a7f10eSVladimir Barinov /*
22795a7f10eSVladimir Barinov  * Low level master read/write transaction. This function is called
22895a7f10eSVladimir Barinov  * from i2c_davinci_xfer.
22995a7f10eSVladimir Barinov  */
23095a7f10eSVladimir Barinov static int
23195a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
23295a7f10eSVladimir Barinov {
23395a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
23495a7f10eSVladimir Barinov 	struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
23595a7f10eSVladimir Barinov 	u32 flag;
23695a7f10eSVladimir Barinov 	u32 stat;
23795a7f10eSVladimir Barinov 	u16 w;
23895a7f10eSVladimir Barinov 	int r;
23995a7f10eSVladimir Barinov 
24095a7f10eSVladimir Barinov 	if (msg->len == 0)
24195a7f10eSVladimir Barinov 		return -EINVAL;
24295a7f10eSVladimir Barinov 
24395a7f10eSVladimir Barinov 	if (!pdata)
24495a7f10eSVladimir Barinov 		pdata = &davinci_i2c_platform_data_default;
24595a7f10eSVladimir Barinov 	/* Introduce a delay, required for some boards (e.g Davinci EVM) */
24695a7f10eSVladimir Barinov 	if (pdata->bus_delay)
24795a7f10eSVladimir Barinov 		udelay(pdata->bus_delay);
24895a7f10eSVladimir Barinov 
24995a7f10eSVladimir Barinov 	/* set the slave address */
25095a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
25195a7f10eSVladimir Barinov 
25295a7f10eSVladimir Barinov 	dev->buf = msg->buf;
25395a7f10eSVladimir Barinov 	dev->buf_len = msg->len;
25495a7f10eSVladimir Barinov 
25595a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
25695a7f10eSVladimir Barinov 
25795a7f10eSVladimir Barinov 	init_completion(&dev->cmd_complete);
25895a7f10eSVladimir Barinov 	dev->cmd_err = 0;
25995a7f10eSVladimir Barinov 
26095a7f10eSVladimir Barinov 	/* Clear any pending interrupts by reading the IVR */
26195a7f10eSVladimir Barinov 	stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG);
26295a7f10eSVladimir Barinov 
26395a7f10eSVladimir Barinov 	/* Take I2C out of reset, configure it as master and set the
26495a7f10eSVladimir Barinov 	 * start bit */
26595a7f10eSVladimir Barinov 	flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST | DAVINCI_I2C_MDR_STT;
26695a7f10eSVladimir Barinov 
26795a7f10eSVladimir Barinov 	/* if the slave address is ten bit address, enable XA bit */
26895a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_TEN)
26995a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_XA;
27095a7f10eSVladimir Barinov 	if (!(msg->flags & I2C_M_RD))
27195a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_TRX;
27295a7f10eSVladimir Barinov 	if (stop)
27395a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_STP;
27495a7f10eSVladimir Barinov 
27595a7f10eSVladimir Barinov 	/* Enable receive or transmit interrupts */
27695a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
27795a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_RD)
27895a7f10eSVladimir Barinov 		MOD_REG_BIT(w, DAVINCI_I2C_IMR_RRDY, 1);
27995a7f10eSVladimir Barinov 	else
28095a7f10eSVladimir Barinov 		MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 1);
28195a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
28295a7f10eSVladimir Barinov 
28395a7f10eSVladimir Barinov 	/* write the data into mode register */
28495a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
28595a7f10eSVladimir Barinov 
28695a7f10eSVladimir Barinov 	r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
28795a7f10eSVladimir Barinov 						      DAVINCI_I2C_TIMEOUT);
28895a7f10eSVladimir Barinov 	dev->buf_len = 0;
28995a7f10eSVladimir Barinov 	if (r < 0)
29095a7f10eSVladimir Barinov 		return r;
29195a7f10eSVladimir Barinov 
29295a7f10eSVladimir Barinov 	if (r == 0) {
29395a7f10eSVladimir Barinov 		dev_err(dev->dev, "controller timed out\n");
29495a7f10eSVladimir Barinov 		i2c_davinci_init(dev);
29595a7f10eSVladimir Barinov 		return -ETIMEDOUT;
29695a7f10eSVladimir Barinov 	}
29795a7f10eSVladimir Barinov 
29895a7f10eSVladimir Barinov 	/* no error */
29995a7f10eSVladimir Barinov 	if (likely(!dev->cmd_err))
30095a7f10eSVladimir Barinov 		return msg->len;
30195a7f10eSVladimir Barinov 
30295a7f10eSVladimir Barinov 	/* We have an error */
30395a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
30495a7f10eSVladimir Barinov 		i2c_davinci_init(dev);
30595a7f10eSVladimir Barinov 		return -EIO;
30695a7f10eSVladimir Barinov 	}
30795a7f10eSVladimir Barinov 
30895a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
30995a7f10eSVladimir Barinov 		if (msg->flags & I2C_M_IGNORE_NAK)
31095a7f10eSVladimir Barinov 			return msg->len;
31195a7f10eSVladimir Barinov 		if (stop) {
31295a7f10eSVladimir Barinov 			w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
31395a7f10eSVladimir Barinov 			MOD_REG_BIT(w, DAVINCI_I2C_MDR_STP, 1);
31495a7f10eSVladimir Barinov 			davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
31595a7f10eSVladimir Barinov 		}
31695a7f10eSVladimir Barinov 		return -EREMOTEIO;
31795a7f10eSVladimir Barinov 	}
31895a7f10eSVladimir Barinov 	return -EIO;
31995a7f10eSVladimir Barinov }
32095a7f10eSVladimir Barinov 
32195a7f10eSVladimir Barinov /*
32295a7f10eSVladimir Barinov  * Prepare controller for a transaction and call i2c_davinci_xfer_msg
32395a7f10eSVladimir Barinov  */
32495a7f10eSVladimir Barinov static int
32595a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
32695a7f10eSVladimir Barinov {
32795a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
32895a7f10eSVladimir Barinov 	int i;
32995a7f10eSVladimir Barinov 	int ret;
33095a7f10eSVladimir Barinov 
33108882d20SHarvey Harrison 	dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
33295a7f10eSVladimir Barinov 
33395a7f10eSVladimir Barinov 	ret = i2c_davinci_wait_bus_not_busy(dev, 1);
33495a7f10eSVladimir Barinov 	if (ret < 0) {
33595a7f10eSVladimir Barinov 		dev_warn(dev->dev, "timeout waiting for bus ready\n");
33695a7f10eSVladimir Barinov 		return ret;
33795a7f10eSVladimir Barinov 	}
33895a7f10eSVladimir Barinov 
33995a7f10eSVladimir Barinov 	for (i = 0; i < num; i++) {
34095a7f10eSVladimir Barinov 		ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
34195a7f10eSVladimir Barinov 		if (ret < 0)
34295a7f10eSVladimir Barinov 			return ret;
34395a7f10eSVladimir Barinov 	}
34495a7f10eSVladimir Barinov 
34508882d20SHarvey Harrison 	dev_dbg(dev->dev, "%s:%d ret: %d\n", __func__, __LINE__, ret);
34695a7f10eSVladimir Barinov 
34795a7f10eSVladimir Barinov 	return num;
34895a7f10eSVladimir Barinov }
34995a7f10eSVladimir Barinov 
35095a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap)
35195a7f10eSVladimir Barinov {
35295a7f10eSVladimir Barinov 	return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
35395a7f10eSVladimir Barinov }
35495a7f10eSVladimir Barinov 
35595a7f10eSVladimir Barinov /*
35695a7f10eSVladimir Barinov  * Interrupt service routine. This gets called whenever an I2C interrupt
35795a7f10eSVladimir Barinov  * occurs.
35895a7f10eSVladimir Barinov  */
35995a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
36095a7f10eSVladimir Barinov {
36195a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = dev_id;
36295a7f10eSVladimir Barinov 	u32 stat;
36395a7f10eSVladimir Barinov 	int count = 0;
36495a7f10eSVladimir Barinov 	u16 w;
36595a7f10eSVladimir Barinov 
36695a7f10eSVladimir Barinov 	while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
36708882d20SHarvey Harrison 		dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
36895a7f10eSVladimir Barinov 		if (count++ == 100) {
36995a7f10eSVladimir Barinov 			dev_warn(dev->dev, "Too much work in one IRQ\n");
37095a7f10eSVladimir Barinov 			break;
37195a7f10eSVladimir Barinov 		}
37295a7f10eSVladimir Barinov 
37395a7f10eSVladimir Barinov 		switch (stat) {
37495a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AL:
37595a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_AL;
37695a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
37795a7f10eSVladimir Barinov 			break;
37895a7f10eSVladimir Barinov 
37995a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_NACK:
38095a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_NACK;
38195a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
38295a7f10eSVladimir Barinov 			break;
38395a7f10eSVladimir Barinov 
38495a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_ARDY:
385b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
386b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
38795a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
38895a7f10eSVladimir Barinov 			break;
38995a7f10eSVladimir Barinov 
39095a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_RDR:
39195a7f10eSVladimir Barinov 			if (dev->buf_len) {
39295a7f10eSVladimir Barinov 				*dev->buf++ =
39395a7f10eSVladimir Barinov 				    davinci_i2c_read_reg(dev,
39495a7f10eSVladimir Barinov 							 DAVINCI_I2C_DRR_REG);
39595a7f10eSVladimir Barinov 				dev->buf_len--;
39695a7f10eSVladimir Barinov 				if (dev->buf_len)
39795a7f10eSVladimir Barinov 					continue;
39895a7f10eSVladimir Barinov 
39995a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
40095a7f10eSVladimir Barinov 					DAVINCI_I2C_STR_REG,
401b73a9aecSTroy Kisky 					DAVINCI_I2C_IMR_RRDY);
40295a7f10eSVladimir Barinov 			} else
40395a7f10eSVladimir Barinov 				dev_err(dev->dev, "RDR IRQ while no "
40495a7f10eSVladimir Barinov 					"data requested\n");
40595a7f10eSVladimir Barinov 			break;
40695a7f10eSVladimir Barinov 
40795a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_XRDY:
40895a7f10eSVladimir Barinov 			if (dev->buf_len) {
40995a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
41095a7f10eSVladimir Barinov 						      *dev->buf++);
41195a7f10eSVladimir Barinov 				dev->buf_len--;
41295a7f10eSVladimir Barinov 				if (dev->buf_len)
41395a7f10eSVladimir Barinov 					continue;
41495a7f10eSVladimir Barinov 
41595a7f10eSVladimir Barinov 				w = davinci_i2c_read_reg(dev,
41695a7f10eSVladimir Barinov 							 DAVINCI_I2C_IMR_REG);
41795a7f10eSVladimir Barinov 				MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 0);
41895a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
41995a7f10eSVladimir Barinov 						      DAVINCI_I2C_IMR_REG,
42095a7f10eSVladimir Barinov 						      w);
42195a7f10eSVladimir Barinov 			} else
42295a7f10eSVladimir Barinov 				dev_err(dev->dev, "TDR IRQ while no data to "
42395a7f10eSVladimir Barinov 					"send\n");
42495a7f10eSVladimir Barinov 			break;
42595a7f10eSVladimir Barinov 
42695a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_SCD:
427b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
428b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
42995a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
43095a7f10eSVladimir Barinov 			break;
43195a7f10eSVladimir Barinov 
43295a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AAS:
43395a7f10eSVladimir Barinov 			dev_warn(dev->dev, "Address as slave interrupt\n");
43495a7f10eSVladimir Barinov 		}/* switch */
43595a7f10eSVladimir Barinov 	}/* while */
43695a7f10eSVladimir Barinov 
43795a7f10eSVladimir Barinov 	return count ? IRQ_HANDLED : IRQ_NONE;
43895a7f10eSVladimir Barinov }
43995a7f10eSVladimir Barinov 
44095a7f10eSVladimir Barinov static struct i2c_algorithm i2c_davinci_algo = {
44195a7f10eSVladimir Barinov 	.master_xfer	= i2c_davinci_xfer,
44295a7f10eSVladimir Barinov 	.functionality	= i2c_davinci_func,
44395a7f10eSVladimir Barinov };
44495a7f10eSVladimir Barinov 
44595a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev)
44695a7f10eSVladimir Barinov {
44795a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev;
44895a7f10eSVladimir Barinov 	struct i2c_adapter *adap;
44995a7f10eSVladimir Barinov 	struct resource *mem, *irq, *ioarea;
45095a7f10eSVladimir Barinov 	int r;
45195a7f10eSVladimir Barinov 
45295a7f10eSVladimir Barinov 	/* NOTE: driver uses the static register mapping */
45395a7f10eSVladimir Barinov 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
45495a7f10eSVladimir Barinov 	if (!mem) {
45595a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "no mem resource?\n");
45695a7f10eSVladimir Barinov 		return -ENODEV;
45795a7f10eSVladimir Barinov 	}
45895a7f10eSVladimir Barinov 
45995a7f10eSVladimir Barinov 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
46095a7f10eSVladimir Barinov 	if (!irq) {
46195a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "no irq resource?\n");
46295a7f10eSVladimir Barinov 		return -ENODEV;
46395a7f10eSVladimir Barinov 	}
46495a7f10eSVladimir Barinov 
46595a7f10eSVladimir Barinov 	ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
46695a7f10eSVladimir Barinov 				    pdev->name);
46795a7f10eSVladimir Barinov 	if (!ioarea) {
46895a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "I2C region already claimed\n");
46995a7f10eSVladimir Barinov 		return -EBUSY;
47095a7f10eSVladimir Barinov 	}
47195a7f10eSVladimir Barinov 
47295a7f10eSVladimir Barinov 	dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL);
47395a7f10eSVladimir Barinov 	if (!dev) {
47495a7f10eSVladimir Barinov 		r = -ENOMEM;
47595a7f10eSVladimir Barinov 		goto err_release_region;
47695a7f10eSVladimir Barinov 	}
47795a7f10eSVladimir Barinov 
47895a7f10eSVladimir Barinov 	dev->dev = get_device(&pdev->dev);
47995a7f10eSVladimir Barinov 	dev->irq = irq->start;
48095a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, dev);
48195a7f10eSVladimir Barinov 
48295a7f10eSVladimir Barinov 	dev->clk = clk_get(&pdev->dev, "I2CCLK");
48395a7f10eSVladimir Barinov 	if (IS_ERR(dev->clk)) {
48495a7f10eSVladimir Barinov 		r = -ENODEV;
48595a7f10eSVladimir Barinov 		goto err_free_mem;
48695a7f10eSVladimir Barinov 	}
48795a7f10eSVladimir Barinov 	clk_enable(dev->clk);
48895a7f10eSVladimir Barinov 
48995a7f10eSVladimir Barinov 	dev->base = (void __iomem *)IO_ADDRESS(mem->start);
49095a7f10eSVladimir Barinov 	i2c_davinci_init(dev);
49195a7f10eSVladimir Barinov 
49295a7f10eSVladimir Barinov 	r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev);
49395a7f10eSVladimir Barinov 	if (r) {
49495a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
49595a7f10eSVladimir Barinov 		goto err_unuse_clocks;
49695a7f10eSVladimir Barinov 	}
49795a7f10eSVladimir Barinov 
49895a7f10eSVladimir Barinov 	adap = &dev->adapter;
49995a7f10eSVladimir Barinov 	i2c_set_adapdata(adap, dev);
50095a7f10eSVladimir Barinov 	adap->owner = THIS_MODULE;
50195a7f10eSVladimir Barinov 	adap->class = I2C_CLASS_HWMON;
50295a7f10eSVladimir Barinov 	strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
50395a7f10eSVladimir Barinov 	adap->algo = &i2c_davinci_algo;
50495a7f10eSVladimir Barinov 	adap->dev.parent = &pdev->dev;
50595a7f10eSVladimir Barinov 
50695a7f10eSVladimir Barinov 	/* FIXME */
50795a7f10eSVladimir Barinov 	adap->timeout = 1;
50895a7f10eSVladimir Barinov 
50995a7f10eSVladimir Barinov 	adap->nr = pdev->id;
51095a7f10eSVladimir Barinov 	r = i2c_add_numbered_adapter(adap);
51195a7f10eSVladimir Barinov 	if (r) {
51295a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure adding adapter\n");
51395a7f10eSVladimir Barinov 		goto err_free_irq;
51495a7f10eSVladimir Barinov 	}
51595a7f10eSVladimir Barinov 
51695a7f10eSVladimir Barinov 	return 0;
51795a7f10eSVladimir Barinov 
51895a7f10eSVladimir Barinov err_free_irq:
51995a7f10eSVladimir Barinov 	free_irq(dev->irq, dev);
52095a7f10eSVladimir Barinov err_unuse_clocks:
52195a7f10eSVladimir Barinov 	clk_disable(dev->clk);
52295a7f10eSVladimir Barinov 	clk_put(dev->clk);
52395a7f10eSVladimir Barinov 	dev->clk = NULL;
52495a7f10eSVladimir Barinov err_free_mem:
52595a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, NULL);
52695a7f10eSVladimir Barinov 	put_device(&pdev->dev);
52795a7f10eSVladimir Barinov 	kfree(dev);
52895a7f10eSVladimir Barinov err_release_region:
52995a7f10eSVladimir Barinov 	release_mem_region(mem->start, (mem->end - mem->start) + 1);
53095a7f10eSVladimir Barinov 
53195a7f10eSVladimir Barinov 	return r;
53295a7f10eSVladimir Barinov }
53395a7f10eSVladimir Barinov 
53495a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev)
53595a7f10eSVladimir Barinov {
53695a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
53795a7f10eSVladimir Barinov 	struct resource *mem;
53895a7f10eSVladimir Barinov 
53995a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, NULL);
54095a7f10eSVladimir Barinov 	i2c_del_adapter(&dev->adapter);
54195a7f10eSVladimir Barinov 	put_device(&pdev->dev);
54295a7f10eSVladimir Barinov 
54395a7f10eSVladimir Barinov 	clk_disable(dev->clk);
54495a7f10eSVladimir Barinov 	clk_put(dev->clk);
54595a7f10eSVladimir Barinov 	dev->clk = NULL;
54695a7f10eSVladimir Barinov 
54795a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
54895a7f10eSVladimir Barinov 	free_irq(IRQ_I2C, dev);
54995a7f10eSVladimir Barinov 	kfree(dev);
55095a7f10eSVladimir Barinov 
55195a7f10eSVladimir Barinov 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
55295a7f10eSVladimir Barinov 	release_mem_region(mem->start, (mem->end - mem->start) + 1);
55395a7f10eSVladimir Barinov 	return 0;
55495a7f10eSVladimir Barinov }
55595a7f10eSVladimir Barinov 
556*add8eda7SKay Sievers /* work with hotplug and coldplug */
557*add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci");
558*add8eda7SKay Sievers 
55995a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = {
56095a7f10eSVladimir Barinov 	.probe		= davinci_i2c_probe,
56195a7f10eSVladimir Barinov 	.remove		= davinci_i2c_remove,
56295a7f10eSVladimir Barinov 	.driver		= {
56395a7f10eSVladimir Barinov 		.name	= "i2c_davinci",
56495a7f10eSVladimir Barinov 		.owner	= THIS_MODULE,
56595a7f10eSVladimir Barinov 	},
56695a7f10eSVladimir Barinov };
56795a7f10eSVladimir Barinov 
56895a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */
56995a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void)
57095a7f10eSVladimir Barinov {
57195a7f10eSVladimir Barinov 	return platform_driver_register(&davinci_i2c_driver);
57295a7f10eSVladimir Barinov }
57395a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver);
57495a7f10eSVladimir Barinov 
57595a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void)
57695a7f10eSVladimir Barinov {
57795a7f10eSVladimir Barinov 	platform_driver_unregister(&davinci_i2c_driver);
57895a7f10eSVladimir Barinov }
57995a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver);
58095a7f10eSVladimir Barinov 
58195a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India");
58295a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
58395a7f10eSVladimir Barinov MODULE_LICENSE("GPL");
584