xref: /openbmc/linux/drivers/i2c/busses/i2c-davinci.c (revision 72a956c556b68aa2a9c2c64f5a7b9e50f7bf67e1)
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  *
2295a7f10eSVladimir Barinov  */
2395a7f10eSVladimir Barinov #include <linux/kernel.h>
2495a7f10eSVladimir Barinov #include <linux/module.h>
2595a7f10eSVladimir Barinov #include <linux/delay.h>
2695a7f10eSVladimir Barinov #include <linux/i2c.h>
2795a7f10eSVladimir Barinov #include <linux/clk.h>
2895a7f10eSVladimir Barinov #include <linux/errno.h>
2995a7f10eSVladimir Barinov #include <linux/sched.h>
3095a7f10eSVladimir Barinov #include <linux/err.h>
3195a7f10eSVladimir Barinov #include <linux/interrupt.h>
3295a7f10eSVladimir Barinov #include <linux/platform_device.h>
3395a7f10eSVladimir Barinov #include <linux/io.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
3582c0de11SChaithrika U S #include <linux/cpufreq.h>
368574faf9SPhilby John #include <linux/gpio.h>
375c3d8a46SHeiko Schocher #include <linux/of_device.h>
38ec2a0833SArnd Bergmann #include <linux/platform_data/i2c-davinci.h>
3995a7f10eSVladimir Barinov 
4095a7f10eSVladimir Barinov /* ----- global defines ----------------------------------------------- */
4195a7f10eSVladimir Barinov 
4295a7f10eSVladimir Barinov #define DAVINCI_I2C_TIMEOUT	(1*HZ)
438574faf9SPhilby John #define DAVINCI_I2C_MAX_TRIES	2
4495a7f10eSVladimir Barinov #define I2C_DAVINCI_INTR_ALL    (DAVINCI_I2C_IMR_AAS | \
4595a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_SCD | \
4695a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_ARDY | \
4795a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_NACK | \
4895a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_AL)
4995a7f10eSVladimir Barinov 
5095a7f10eSVladimir Barinov #define DAVINCI_I2C_OAR_REG	0x00
5195a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_REG	0x04
5295a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_REG	0x08
5395a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKL_REG	0x0c
5495a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKH_REG	0x10
5595a7f10eSVladimir Barinov #define DAVINCI_I2C_CNT_REG	0x14
5695a7f10eSVladimir Barinov #define DAVINCI_I2C_DRR_REG	0x18
5795a7f10eSVladimir Barinov #define DAVINCI_I2C_SAR_REG	0x1c
5895a7f10eSVladimir Barinov #define DAVINCI_I2C_DXR_REG	0x20
5995a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_REG	0x24
6095a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_REG	0x28
6195a7f10eSVladimir Barinov #define DAVINCI_I2C_EMDR_REG	0x2c
6295a7f10eSVladimir Barinov #define DAVINCI_I2C_PSC_REG	0x30
637ef97e9aSGrygorii Strashko #define DAVINCI_I2C_FUNC_REG	0x48
647ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_REG	0x4c
657ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_REG	0x50
667ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DOUT_REG	0x54
677ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_REG	0x58
687ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_REG	0x5c
6995a7f10eSVladimir Barinov 
7095a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AAS	0x07
7195a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_SCD	0x06
7295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_XRDY	0x05
7395a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_RDR	0x04
7495a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_ARDY	0x03
7595a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_NACK	0x02
7695a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AL	0x01
7795a7f10eSVladimir Barinov 
78c062a251SChaithrika U S #define DAVINCI_I2C_STR_BB	BIT(12)
79c062a251SChaithrika U S #define DAVINCI_I2C_STR_RSFULL	BIT(11)
80c062a251SChaithrika U S #define DAVINCI_I2C_STR_SCD	BIT(5)
81c062a251SChaithrika U S #define DAVINCI_I2C_STR_ARDY	BIT(2)
82c062a251SChaithrika U S #define DAVINCI_I2C_STR_NACK	BIT(1)
83c062a251SChaithrika U S #define DAVINCI_I2C_STR_AL	BIT(0)
8495a7f10eSVladimir Barinov 
85c062a251SChaithrika U S #define DAVINCI_I2C_MDR_NACK	BIT(15)
86c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STT	BIT(13)
87c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STP	BIT(11)
88c062a251SChaithrika U S #define DAVINCI_I2C_MDR_MST	BIT(10)
89c062a251SChaithrika U S #define DAVINCI_I2C_MDR_TRX	BIT(9)
90c062a251SChaithrika U S #define DAVINCI_I2C_MDR_XA	BIT(8)
91c062a251SChaithrika U S #define DAVINCI_I2C_MDR_RM	BIT(7)
92c062a251SChaithrika U S #define DAVINCI_I2C_MDR_IRS	BIT(5)
9395a7f10eSVladimir Barinov 
94c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AAS	BIT(6)
95c062a251SChaithrika U S #define DAVINCI_I2C_IMR_SCD	BIT(5)
96c062a251SChaithrika U S #define DAVINCI_I2C_IMR_XRDY	BIT(4)
97c062a251SChaithrika U S #define DAVINCI_I2C_IMR_RRDY	BIT(3)
98c062a251SChaithrika U S #define DAVINCI_I2C_IMR_ARDY	BIT(2)
99c062a251SChaithrika U S #define DAVINCI_I2C_IMR_NACK	BIT(1)
100c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AL	BIT(0)
10195a7f10eSVladimir Barinov 
1027ef97e9aSGrygorii Strashko /* set SDA and SCL as GPIO */
1037ef97e9aSGrygorii Strashko #define DAVINCI_I2C_FUNC_PFUNC0	BIT(0)
1047ef97e9aSGrygorii Strashko 
1057ef97e9aSGrygorii Strashko /* set SCL as output when used as GPIO*/
1067ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_PDIR0	BIT(0)
1077ef97e9aSGrygorii Strashko /* set SDA as output when used as GPIO*/
1087ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_PDIR1	BIT(1)
1097ef97e9aSGrygorii Strashko 
1107ef97e9aSGrygorii Strashko /* read SCL GPIO level */
1117ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_PDIN0 BIT(0)
1127ef97e9aSGrygorii Strashko /* read SDA GPIO level */
1137ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_PDIN1 BIT(1)
1147ef97e9aSGrygorii Strashko 
1157ef97e9aSGrygorii Strashko /*set the SCL GPIO high */
1167ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_PDSET0	BIT(0)
1177ef97e9aSGrygorii Strashko /*set the SDA GPIO high */
1187ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_PDSET1	BIT(1)
1197ef97e9aSGrygorii Strashko 
1207ef97e9aSGrygorii Strashko /* set the SCL GPIO low */
1217ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_PDCLR0	BIT(0)
1227ef97e9aSGrygorii Strashko /* set the SDA GPIO low */
1237ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_PDCLR1	BIT(1)
1247ef97e9aSGrygorii Strashko 
12595a7f10eSVladimir Barinov struct davinci_i2c_dev {
12695a7f10eSVladimir Barinov 	struct device           *dev;
12795a7f10eSVladimir Barinov 	void __iomem		*base;
12895a7f10eSVladimir Barinov 	struct completion	cmd_complete;
12995a7f10eSVladimir Barinov 	struct clk              *clk;
13095a7f10eSVladimir Barinov 	int			cmd_err;
13195a7f10eSVladimir Barinov 	u8			*buf;
13295a7f10eSVladimir Barinov 	size_t			buf_len;
13395a7f10eSVladimir Barinov 	int			irq;
134c6c7c729SDirk Behme 	int			stop;
1355a0d5f5fSTroy Kisky 	u8			terminate;
13695a7f10eSVladimir Barinov 	struct i2c_adapter	adapter;
13782c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ
13882c0de11SChaithrika U S 	struct completion	xfr_complete;
13982c0de11SChaithrika U S 	struct notifier_block	freq_transition;
14082c0de11SChaithrika U S #endif
1415c3d8a46SHeiko Schocher 	struct davinci_i2c_platform_data *pdata;
14295a7f10eSVladimir Barinov };
14395a7f10eSVladimir Barinov 
14495a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */
14595a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
14695a7f10eSVladimir Barinov 	.bus_freq	= 100,
14795a7f10eSVladimir Barinov 	.bus_delay	= 0,
14895a7f10eSVladimir Barinov };
14995a7f10eSVladimir Barinov 
15095a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
15195a7f10eSVladimir Barinov 					 int reg, u16 val)
15295a7f10eSVladimir Barinov {
153a238dcfaSTaras Kondratiuk 	writew_relaxed(val, i2c_dev->base + reg);
15495a7f10eSVladimir Barinov }
15595a7f10eSVladimir Barinov 
15695a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
15795a7f10eSVladimir Barinov {
158a238dcfaSTaras Kondratiuk 	return readw_relaxed(i2c_dev->base + reg);
15995a7f10eSVladimir Barinov }
16095a7f10eSVladimir Barinov 
1615ae5b113SChaithrika U S static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
1625ae5b113SChaithrika U S 								int val)
1635ae5b113SChaithrika U S {
1645ae5b113SChaithrika U S 	u16 w;
1655ae5b113SChaithrika U S 
1665ae5b113SChaithrika U S 	w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
1675ae5b113SChaithrika U S 	if (!val)	/* put I2C into reset */
1685ae5b113SChaithrika U S 		w &= ~DAVINCI_I2C_MDR_IRS;
1695ae5b113SChaithrika U S 	else		/* take I2C out of reset */
1705ae5b113SChaithrika U S 		w |= DAVINCI_I2C_MDR_IRS;
1715ae5b113SChaithrika U S 
1725ae5b113SChaithrika U S 	davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
1735ae5b113SChaithrika U S }
1745ae5b113SChaithrika U S 
1755ae5b113SChaithrika U S static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
17695a7f10eSVladimir Barinov {
1775c3d8a46SHeiko Schocher 	struct davinci_i2c_platform_data *pdata = dev->pdata;
17895a7f10eSVladimir Barinov 	u16 psc;
17995a7f10eSVladimir Barinov 	u32 clk;
180cc99ff70STroy Kisky 	u32 d;
18195a7f10eSVladimir Barinov 	u32 clkh;
18295a7f10eSVladimir Barinov 	u32 clkl;
18395a7f10eSVladimir Barinov 	u32 input_clock = clk_get_rate(dev->clk);
18495a7f10eSVladimir Barinov 
18595a7f10eSVladimir Barinov 	/* NOTE: I2C Clock divider programming info
18695a7f10eSVladimir Barinov 	 * As per I2C specs the following formulas provide prescaler
18795a7f10eSVladimir Barinov 	 * and low/high divider values
18895a7f10eSVladimir Barinov 	 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
18995a7f10eSVladimir Barinov 	 *                       module clk
19095a7f10eSVladimir Barinov 	 *
19195a7f10eSVladimir Barinov 	 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
19295a7f10eSVladimir Barinov 	 *
19395a7f10eSVladimir Barinov 	 * Thus,
19495a7f10eSVladimir Barinov 	 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
19595a7f10eSVladimir Barinov 	 *
19695a7f10eSVladimir Barinov 	 * where if PSC == 0, d = 7,
19795a7f10eSVladimir Barinov 	 *       if PSC == 1, d = 6
19895a7f10eSVladimir Barinov 	 *       if PSC > 1 , d = 5
19995a7f10eSVladimir Barinov 	 */
20095a7f10eSVladimir Barinov 
201cc99ff70STroy Kisky 	/* get minimum of 7 MHz clock, but max of 12 MHz */
202cc99ff70STroy Kisky 	psc = (input_clock / 7000000) - 1;
203cc99ff70STroy Kisky 	if ((input_clock / (psc + 1)) > 12000000)
204cc99ff70STroy Kisky 		psc++;	/* better to run under spec than over */
205cc99ff70STroy Kisky 	d = (psc >= 2) ? 5 : 7 - psc;
20695a7f10eSVladimir Barinov 
207cc99ff70STroy Kisky 	clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
208cc99ff70STroy Kisky 	clkh = clk >> 1;
20995a7f10eSVladimir Barinov 	clkl = clk - clkh;
21095a7f10eSVladimir Barinov 
21195a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
21295a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
21395a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
21495a7f10eSVladimir Barinov 
2155ae5b113SChaithrika U S 	dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
2165ae5b113SChaithrika U S }
2175ae5b113SChaithrika U S 
2185ae5b113SChaithrika U S /*
2195ae5b113SChaithrika U S  * This function configures I2C and brings I2C out of reset.
2205ae5b113SChaithrika U S  * This function is called during I2C init function. This function
2215ae5b113SChaithrika U S  * also gets called if I2C encounters any errors.
2225ae5b113SChaithrika U S  */
2235ae5b113SChaithrika U S static int i2c_davinci_init(struct davinci_i2c_dev *dev)
2245ae5b113SChaithrika U S {
2255c3d8a46SHeiko Schocher 	struct davinci_i2c_platform_data *pdata = dev->pdata;
2265ae5b113SChaithrika U S 
2275ae5b113SChaithrika U S 	/* put I2C into reset */
2285ae5b113SChaithrika U S 	davinci_i2c_reset_ctrl(dev, 0);
2295ae5b113SChaithrika U S 
2305ae5b113SChaithrika U S 	/* compute clock dividers */
2315ae5b113SChaithrika U S 	i2c_davinci_calc_clk_dividers(dev);
2325ae5b113SChaithrika U S 
2337605fa3bSDavid Brownell 	/* Respond at reserved "SMBus Host" slave address" (and zero);
2347605fa3bSDavid Brownell 	 * we seem to have no option to not respond...
2357605fa3bSDavid Brownell 	 */
2367605fa3bSDavid Brownell 	davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08);
2377605fa3bSDavid Brownell 
23895a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "PSC  = %d\n",
23995a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
24095a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKL = %d\n",
24195a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
24295a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKH = %d\n",
24395a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
244cc99ff70STroy Kisky 	dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
245cc99ff70STroy Kisky 		pdata->bus_freq, pdata->bus_delay);
24695a7f10eSVladimir Barinov 
2475c3d8a46SHeiko Schocher 
24895a7f10eSVladimir Barinov 	/* Take the I2C module out of reset: */
2495ae5b113SChaithrika U S 	davinci_i2c_reset_ctrl(dev, 1);
25095a7f10eSVladimir Barinov 
25195a7f10eSVladimir Barinov 	/* Enable interrupts */
25295a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
25395a7f10eSVladimir Barinov 
25495a7f10eSVladimir Barinov 	return 0;
25595a7f10eSVladimir Barinov }
25695a7f10eSVladimir Barinov 
25795a7f10eSVladimir Barinov /*
2582e65676fSGrygorii Strashko  * This routine does i2c bus recovery by using i2c_generic_gpio_recovery
2592e65676fSGrygorii Strashko  * which is provided by I2C Bus recovery infrastructure.
2602e65676fSGrygorii Strashko  */
2612e65676fSGrygorii Strashko static void davinci_i2c_prepare_recovery(struct i2c_adapter *adap)
2622e65676fSGrygorii Strashko {
2632e65676fSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
2642e65676fSGrygorii Strashko 
2652e65676fSGrygorii Strashko 	/* Disable interrupts */
2662e65676fSGrygorii Strashko 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, 0);
2672e65676fSGrygorii Strashko 
2682e65676fSGrygorii Strashko 	/* put I2C into reset */
2692e65676fSGrygorii Strashko 	davinci_i2c_reset_ctrl(dev, 0);
2702e65676fSGrygorii Strashko }
2712e65676fSGrygorii Strashko 
2722e65676fSGrygorii Strashko static void davinci_i2c_unprepare_recovery(struct i2c_adapter *adap)
2732e65676fSGrygorii Strashko {
2742e65676fSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
2752e65676fSGrygorii Strashko 
2762e65676fSGrygorii Strashko 	i2c_davinci_init(dev);
2772e65676fSGrygorii Strashko }
2782e65676fSGrygorii Strashko 
2792e65676fSGrygorii Strashko static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info = {
2802e65676fSGrygorii Strashko 	.recover_bus = i2c_generic_gpio_recovery,
2812e65676fSGrygorii Strashko 	.prepare_recovery = davinci_i2c_prepare_recovery,
2822e65676fSGrygorii Strashko 	.unprepare_recovery = davinci_i2c_unprepare_recovery,
2832e65676fSGrygorii Strashko };
2842e65676fSGrygorii Strashko 
2857ef97e9aSGrygorii Strashko static void davinci_i2c_set_scl(struct i2c_adapter *adap, int val)
2867ef97e9aSGrygorii Strashko {
2877ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
2887ef97e9aSGrygorii Strashko 
2897ef97e9aSGrygorii Strashko 	if (val)
2907ef97e9aSGrygorii Strashko 		davinci_i2c_write_reg(dev, DAVINCI_I2C_DSET_REG,
2917ef97e9aSGrygorii Strashko 				      DAVINCI_I2C_DSET_PDSET0);
2927ef97e9aSGrygorii Strashko 	else
2937ef97e9aSGrygorii Strashko 		davinci_i2c_write_reg(dev, DAVINCI_I2C_DCLR_REG,
2947ef97e9aSGrygorii Strashko 				      DAVINCI_I2C_DCLR_PDCLR0);
2957ef97e9aSGrygorii Strashko }
2967ef97e9aSGrygorii Strashko 
2977ef97e9aSGrygorii Strashko static int davinci_i2c_get_scl(struct i2c_adapter *adap)
2987ef97e9aSGrygorii Strashko {
2997ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3007ef97e9aSGrygorii Strashko 	int val;
3017ef97e9aSGrygorii Strashko 
3027ef97e9aSGrygorii Strashko 	/* read the state of SCL */
3037ef97e9aSGrygorii Strashko 	val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
3047ef97e9aSGrygorii Strashko 	return val & DAVINCI_I2C_DIN_PDIN0;
3057ef97e9aSGrygorii Strashko }
3067ef97e9aSGrygorii Strashko 
3077ef97e9aSGrygorii Strashko static int davinci_i2c_get_sda(struct i2c_adapter *adap)
3087ef97e9aSGrygorii Strashko {
3097ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3107ef97e9aSGrygorii Strashko 	int val;
3117ef97e9aSGrygorii Strashko 
3127ef97e9aSGrygorii Strashko 	/* read the state of SDA */
3137ef97e9aSGrygorii Strashko 	val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
3147ef97e9aSGrygorii Strashko 	return val & DAVINCI_I2C_DIN_PDIN1;
3157ef97e9aSGrygorii Strashko }
3167ef97e9aSGrygorii Strashko 
3177ef97e9aSGrygorii Strashko static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter *adap)
3187ef97e9aSGrygorii Strashko {
3197ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3207ef97e9aSGrygorii Strashko 
3217ef97e9aSGrygorii Strashko 	davinci_i2c_prepare_recovery(adap);
3227ef97e9aSGrygorii Strashko 
3237ef97e9aSGrygorii Strashko 	/* SCL output, SDA input */
3247ef97e9aSGrygorii Strashko 	davinci_i2c_write_reg(dev, DAVINCI_I2C_DIR_REG, DAVINCI_I2C_DIR_PDIR0);
3257ef97e9aSGrygorii Strashko 
3267ef97e9aSGrygorii Strashko 	/* change to GPIO mode */
3277ef97e9aSGrygorii Strashko 	davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG,
3287ef97e9aSGrygorii Strashko 			      DAVINCI_I2C_FUNC_PFUNC0);
3297ef97e9aSGrygorii Strashko }
3307ef97e9aSGrygorii Strashko 
3317ef97e9aSGrygorii Strashko static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter *adap)
3327ef97e9aSGrygorii Strashko {
3337ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3347ef97e9aSGrygorii Strashko 
3357ef97e9aSGrygorii Strashko 	/* change back to I2C mode */
3367ef97e9aSGrygorii Strashko 	davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG, 0);
3377ef97e9aSGrygorii Strashko 
3387ef97e9aSGrygorii Strashko 	davinci_i2c_unprepare_recovery(adap);
3397ef97e9aSGrygorii Strashko }
3407ef97e9aSGrygorii Strashko 
3417ef97e9aSGrygorii Strashko static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info = {
3427ef97e9aSGrygorii Strashko 	.recover_bus = i2c_generic_scl_recovery,
3437ef97e9aSGrygorii Strashko 	.set_scl = davinci_i2c_set_scl,
3447ef97e9aSGrygorii Strashko 	.get_scl = davinci_i2c_get_scl,
3457ef97e9aSGrygorii Strashko 	.get_sda = davinci_i2c_get_sda,
3467ef97e9aSGrygorii Strashko 	.prepare_recovery = davinci_i2c_scl_prepare_recovery,
3477ef97e9aSGrygorii Strashko 	.unprepare_recovery = davinci_i2c_scl_unprepare_recovery,
3487ef97e9aSGrygorii Strashko };
3497ef97e9aSGrygorii Strashko 
3502e65676fSGrygorii Strashko /*
35195a7f10eSVladimir Barinov  * Waiting for bus not busy
35295a7f10eSVladimir Barinov  */
353*72a956c5SAlexander Sverdlin static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev)
35495a7f10eSVladimir Barinov {
355*72a956c5SAlexander Sverdlin 	unsigned long timeout = jiffies + dev->adapter.timeout;
35695a7f10eSVladimir Barinov 
357*72a956c5SAlexander Sverdlin 	do {
358*72a956c5SAlexander Sverdlin 		if (!(davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB))
359*72a956c5SAlexander Sverdlin 			return 0;
360*72a956c5SAlexander Sverdlin 		schedule_timeout_uninterruptible(1);
361*72a956c5SAlexander Sverdlin 	} while (time_before_eq(jiffies, timeout));
362*72a956c5SAlexander Sverdlin 
363*72a956c5SAlexander Sverdlin 	dev_warn(dev->dev, "timeout waiting for bus ready\n");
3642e65676fSGrygorii Strashko 	i2c_recover_bus(&dev->adapter);
365*72a956c5SAlexander Sverdlin 
366*72a956c5SAlexander Sverdlin 	/*
367*72a956c5SAlexander Sverdlin 	 * if bus is still "busy" here, it's most probably a HW problem like
368*72a956c5SAlexander Sverdlin 	 * short-circuit
369*72a956c5SAlexander Sverdlin 	 */
370*72a956c5SAlexander Sverdlin 	if (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB)
371*72a956c5SAlexander Sverdlin 		return -EIO;
37295a7f10eSVladimir Barinov 
37395a7f10eSVladimir Barinov 	return 0;
37495a7f10eSVladimir Barinov }
37595a7f10eSVladimir Barinov 
37695a7f10eSVladimir Barinov /*
37795a7f10eSVladimir Barinov  * Low level master read/write transaction. This function is called
37895a7f10eSVladimir Barinov  * from i2c_davinci_xfer.
37995a7f10eSVladimir Barinov  */
38095a7f10eSVladimir Barinov static int
38195a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
38295a7f10eSVladimir Barinov {
38395a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3845c3d8a46SHeiko Schocher 	struct davinci_i2c_platform_data *pdata = dev->pdata;
38595a7f10eSVladimir Barinov 	u32 flag;
38695a7f10eSVladimir Barinov 	u16 w;
387d9e1f441SNicholas Mc Guire 	unsigned long time_left;
38895a7f10eSVladimir Barinov 
38995a7f10eSVladimir Barinov 	/* Introduce a delay, required for some boards (e.g Davinci EVM) */
39095a7f10eSVladimir Barinov 	if (pdata->bus_delay)
39195a7f10eSVladimir Barinov 		udelay(pdata->bus_delay);
39295a7f10eSVladimir Barinov 
39395a7f10eSVladimir Barinov 	/* set the slave address */
39495a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
39595a7f10eSVladimir Barinov 
39695a7f10eSVladimir Barinov 	dev->buf = msg->buf;
39795a7f10eSVladimir Barinov 	dev->buf_len = msg->len;
398c6c7c729SDirk Behme 	dev->stop = stop;
39995a7f10eSVladimir Barinov 
40095a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
40195a7f10eSVladimir Barinov 
40216735d02SWolfram Sang 	reinit_completion(&dev->cmd_complete);
40395a7f10eSVladimir Barinov 	dev->cmd_err = 0;
40495a7f10eSVladimir Barinov 
405c5b4afecSJon Povey 	/* Take I2C out of reset and configure it as master */
406c5b4afecSJon Povey 	flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
40795a7f10eSVladimir Barinov 
40895a7f10eSVladimir Barinov 	/* if the slave address is ten bit address, enable XA bit */
40995a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_TEN)
41095a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_XA;
41195a7f10eSVladimir Barinov 	if (!(msg->flags & I2C_M_RD))
41295a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_TRX;
413c5b4afecSJon Povey 	if (msg->len == 0)
414c6c7c729SDirk Behme 		flag |= DAVINCI_I2C_MDR_RM;
41595a7f10eSVladimir Barinov 
41695a7f10eSVladimir Barinov 	/* Enable receive or transmit interrupts */
41795a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
41895a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_RD)
419c062a251SChaithrika U S 		w |= DAVINCI_I2C_IMR_RRDY;
42095a7f10eSVladimir Barinov 	else
421c062a251SChaithrika U S 		w |= DAVINCI_I2C_IMR_XRDY;
42295a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
42395a7f10eSVladimir Barinov 
4245a0d5f5fSTroy Kisky 	dev->terminate = 0;
425c6c7c729SDirk Behme 
426c6c7c729SDirk Behme 	/*
427c5b4afecSJon Povey 	 * Write mode register first as needed for correct behaviour
428c5b4afecSJon Povey 	 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
42925985edcSLucas De Marchi 	 * occurring before we have loaded DXR
430c5b4afecSJon Povey 	 */
431c5b4afecSJon Povey 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
432c5b4afecSJon Povey 
433c5b4afecSJon Povey 	/*
434c6c7c729SDirk Behme 	 * First byte should be set here, not after interrupt,
435c6c7c729SDirk Behme 	 * because transmit-data-ready interrupt can come before
436c6c7c729SDirk Behme 	 * NACK-interrupt during sending of previous message and
437c6c7c729SDirk Behme 	 * ICDXR may have wrong data
438c5b4afecSJon Povey 	 * It also saves us one interrupt, slightly faster
439c6c7c729SDirk Behme 	 */
440c6c7c729SDirk Behme 	if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
441c6c7c729SDirk Behme 		davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
442c6c7c729SDirk Behme 		dev->buf_len--;
443c6c7c729SDirk Behme 	}
444c6c7c729SDirk Behme 
445c5b4afecSJon Povey 	/* Set STT to begin transmit now DXR is loaded */
446c5b4afecSJon Povey 	flag |= DAVINCI_I2C_MDR_STT;
447c5b4afecSJon Povey 	if (stop && msg->len != 0)
448c5b4afecSJon Povey 		flag |= DAVINCI_I2C_MDR_STP;
4494bba0fd8SJon Povey 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
4504bba0fd8SJon Povey 
451d9e1f441SNicholas Mc Guire 	time_left = wait_for_completion_timeout(&dev->cmd_complete,
452d9e1f441SNicholas Mc Guire 						dev->adapter.timeout);
453d9e1f441SNicholas Mc Guire 	if (!time_left) {
45495a7f10eSVladimir Barinov 		dev_err(dev->dev, "controller timed out\n");
4552e65676fSGrygorii Strashko 		i2c_recover_bus(adap);
4565a0d5f5fSTroy Kisky 		dev->buf_len = 0;
45795a7f10eSVladimir Barinov 		return -ETIMEDOUT;
45895a7f10eSVladimir Barinov 	}
4595a0d5f5fSTroy Kisky 	if (dev->buf_len) {
4605a0d5f5fSTroy Kisky 		/* This should be 0 if all bytes were transferred
4615a0d5f5fSTroy Kisky 		 * or dev->cmd_err denotes an error.
4625a0d5f5fSTroy Kisky 		 */
4635a0d5f5fSTroy Kisky 		dev_err(dev->dev, "abnormal termination buf_len=%i\n",
4645a0d5f5fSTroy Kisky 			dev->buf_len);
4655a0d5f5fSTroy Kisky 		dev->terminate = 1;
4665a0d5f5fSTroy Kisky 		wmb();
4675a0d5f5fSTroy Kisky 		dev->buf_len = 0;
468d9e1f441SNicholas Mc Guire 		return -EREMOTEIO;
4695a0d5f5fSTroy Kisky 	}
47095a7f10eSVladimir Barinov 
47195a7f10eSVladimir Barinov 	/* no error */
47295a7f10eSVladimir Barinov 	if (likely(!dev->cmd_err))
47395a7f10eSVladimir Barinov 		return msg->len;
47495a7f10eSVladimir Barinov 
47595a7f10eSVladimir Barinov 	/* We have an error */
47695a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
47795a7f10eSVladimir Barinov 		i2c_davinci_init(dev);
47895a7f10eSVladimir Barinov 		return -EIO;
47995a7f10eSVladimir Barinov 	}
48095a7f10eSVladimir Barinov 
48195a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
48295a7f10eSVladimir Barinov 		if (msg->flags & I2C_M_IGNORE_NAK)
48395a7f10eSVladimir Barinov 			return msg->len;
48495a7f10eSVladimir Barinov 		w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
485c062a251SChaithrika U S 		w |= DAVINCI_I2C_MDR_STP;
48695a7f10eSVladimir Barinov 		davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
48795a7f10eSVladimir Barinov 		return -EREMOTEIO;
48895a7f10eSVladimir Barinov 	}
48995a7f10eSVladimir Barinov 	return -EIO;
49095a7f10eSVladimir Barinov }
49195a7f10eSVladimir Barinov 
49295a7f10eSVladimir Barinov /*
49395a7f10eSVladimir Barinov  * Prepare controller for a transaction and call i2c_davinci_xfer_msg
49495a7f10eSVladimir Barinov  */
49595a7f10eSVladimir Barinov static int
49695a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
49795a7f10eSVladimir Barinov {
49895a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
49995a7f10eSVladimir Barinov 	int i;
50095a7f10eSVladimir Barinov 	int ret;
50195a7f10eSVladimir Barinov 
50208882d20SHarvey Harrison 	dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
50395a7f10eSVladimir Barinov 
504*72a956c5SAlexander Sverdlin 	ret = i2c_davinci_wait_bus_not_busy(dev);
50595a7f10eSVladimir Barinov 	if (ret < 0) {
50695a7f10eSVladimir Barinov 		dev_warn(dev->dev, "timeout waiting for bus ready\n");
50795a7f10eSVladimir Barinov 		return ret;
50895a7f10eSVladimir Barinov 	}
50995a7f10eSVladimir Barinov 
51095a7f10eSVladimir Barinov 	for (i = 0; i < num; i++) {
51195a7f10eSVladimir Barinov 		ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
512d868caa1STroy Kisky 		dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
513d868caa1STroy Kisky 			ret);
51495a7f10eSVladimir Barinov 		if (ret < 0)
51595a7f10eSVladimir Barinov 			return ret;
51695a7f10eSVladimir Barinov 	}
51782c0de11SChaithrika U S 
51882c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ
51982c0de11SChaithrika U S 	complete(&dev->xfr_complete);
52082c0de11SChaithrika U S #endif
52182c0de11SChaithrika U S 
52295a7f10eSVladimir Barinov 	return num;
52395a7f10eSVladimir Barinov }
52495a7f10eSVladimir Barinov 
52595a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap)
52695a7f10eSVladimir Barinov {
527c6c7c729SDirk Behme 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
52895a7f10eSVladimir Barinov }
52995a7f10eSVladimir Barinov 
5305a0d5f5fSTroy Kisky static void terminate_read(struct davinci_i2c_dev *dev)
5315a0d5f5fSTroy Kisky {
5325a0d5f5fSTroy Kisky 	u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
5335a0d5f5fSTroy Kisky 	w |= DAVINCI_I2C_MDR_NACK;
5345a0d5f5fSTroy Kisky 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
5355a0d5f5fSTroy Kisky 
5365a0d5f5fSTroy Kisky 	/* Throw away data */
5375a0d5f5fSTroy Kisky 	davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
5385a0d5f5fSTroy Kisky 	if (!dev->terminate)
5395a0d5f5fSTroy Kisky 		dev_err(dev->dev, "RDR IRQ while no data requested\n");
5405a0d5f5fSTroy Kisky }
5415a0d5f5fSTroy Kisky static void terminate_write(struct davinci_i2c_dev *dev)
5425a0d5f5fSTroy Kisky {
5435a0d5f5fSTroy Kisky 	u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
5445a0d5f5fSTroy Kisky 	w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
5455a0d5f5fSTroy Kisky 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
5465a0d5f5fSTroy Kisky 
5475a0d5f5fSTroy Kisky 	if (!dev->terminate)
5487605fa3bSDavid Brownell 		dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
5495a0d5f5fSTroy Kisky }
5505a0d5f5fSTroy Kisky 
55195a7f10eSVladimir Barinov /*
55295a7f10eSVladimir Barinov  * Interrupt service routine. This gets called whenever an I2C interrupt
55395a7f10eSVladimir Barinov  * occurs.
55495a7f10eSVladimir Barinov  */
55595a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
55695a7f10eSVladimir Barinov {
55795a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = dev_id;
55895a7f10eSVladimir Barinov 	u32 stat;
55995a7f10eSVladimir Barinov 	int count = 0;
56095a7f10eSVladimir Barinov 	u16 w;
56195a7f10eSVladimir Barinov 
56295a7f10eSVladimir Barinov 	while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
56308882d20SHarvey Harrison 		dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
56495a7f10eSVladimir Barinov 		if (count++ == 100) {
56595a7f10eSVladimir Barinov 			dev_warn(dev->dev, "Too much work in one IRQ\n");
56695a7f10eSVladimir Barinov 			break;
56795a7f10eSVladimir Barinov 		}
56895a7f10eSVladimir Barinov 
56995a7f10eSVladimir Barinov 		switch (stat) {
57095a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AL:
5715a0d5f5fSTroy Kisky 			/* Arbitration lost, must retry */
57295a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_AL;
5735a0d5f5fSTroy Kisky 			dev->buf_len = 0;
57495a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
57595a7f10eSVladimir Barinov 			break;
57695a7f10eSVladimir Barinov 
57795a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_NACK:
57895a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_NACK;
5795a0d5f5fSTroy Kisky 			dev->buf_len = 0;
58095a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
58195a7f10eSVladimir Barinov 			break;
58295a7f10eSVladimir Barinov 
58395a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_ARDY:
584b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
585b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
586c6c7c729SDirk Behme 			if (((dev->buf_len == 0) && (dev->stop != 0)) ||
587c6c7c729SDirk Behme 			    (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
588c6c7c729SDirk Behme 				w = davinci_i2c_read_reg(dev,
589c6c7c729SDirk Behme 							 DAVINCI_I2C_MDR_REG);
590c6c7c729SDirk Behme 				w |= DAVINCI_I2C_MDR_STP;
591c6c7c729SDirk Behme 				davinci_i2c_write_reg(dev,
592c6c7c729SDirk Behme 						      DAVINCI_I2C_MDR_REG, w);
593c6c7c729SDirk Behme 			}
59495a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
59595a7f10eSVladimir Barinov 			break;
59695a7f10eSVladimir Barinov 
59795a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_RDR:
59895a7f10eSVladimir Barinov 			if (dev->buf_len) {
59995a7f10eSVladimir Barinov 				*dev->buf++ =
60095a7f10eSVladimir Barinov 				    davinci_i2c_read_reg(dev,
60195a7f10eSVladimir Barinov 							 DAVINCI_I2C_DRR_REG);
60295a7f10eSVladimir Barinov 				dev->buf_len--;
60395a7f10eSVladimir Barinov 				if (dev->buf_len)
60495a7f10eSVladimir Barinov 					continue;
60595a7f10eSVladimir Barinov 
60695a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
60795a7f10eSVladimir Barinov 					DAVINCI_I2C_STR_REG,
608b73a9aecSTroy Kisky 					DAVINCI_I2C_IMR_RRDY);
6095a0d5f5fSTroy Kisky 			} else {
6105a0d5f5fSTroy Kisky 				/* signal can terminate transfer */
6115a0d5f5fSTroy Kisky 				terminate_read(dev);
6125a0d5f5fSTroy Kisky 			}
61395a7f10eSVladimir Barinov 			break;
61495a7f10eSVladimir Barinov 
61595a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_XRDY:
61695a7f10eSVladimir Barinov 			if (dev->buf_len) {
61795a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
61895a7f10eSVladimir Barinov 						      *dev->buf++);
61995a7f10eSVladimir Barinov 				dev->buf_len--;
62095a7f10eSVladimir Barinov 				if (dev->buf_len)
62195a7f10eSVladimir Barinov 					continue;
62295a7f10eSVladimir Barinov 
62395a7f10eSVladimir Barinov 				w = davinci_i2c_read_reg(dev,
62495a7f10eSVladimir Barinov 							 DAVINCI_I2C_IMR_REG);
625c062a251SChaithrika U S 				w &= ~DAVINCI_I2C_IMR_XRDY;
62695a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
62795a7f10eSVladimir Barinov 						      DAVINCI_I2C_IMR_REG,
62895a7f10eSVladimir Barinov 						      w);
6295a0d5f5fSTroy Kisky 			} else {
6305a0d5f5fSTroy Kisky 				/* signal can terminate transfer */
6315a0d5f5fSTroy Kisky 				terminate_write(dev);
6325a0d5f5fSTroy Kisky 			}
63395a7f10eSVladimir Barinov 			break;
63495a7f10eSVladimir Barinov 
63595a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_SCD:
636b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
637b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
63895a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
63995a7f10eSVladimir Barinov 			break;
64095a7f10eSVladimir Barinov 
64195a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AAS:
6427605fa3bSDavid Brownell 			dev_dbg(dev->dev, "Address as slave interrupt\n");
6437605fa3bSDavid Brownell 			break;
6447605fa3bSDavid Brownell 
6457605fa3bSDavid Brownell 		default:
6467605fa3bSDavid Brownell 			dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
6477605fa3bSDavid Brownell 			break;
6487605fa3bSDavid Brownell 		}
6497605fa3bSDavid Brownell 	}
65095a7f10eSVladimir Barinov 
65195a7f10eSVladimir Barinov 	return count ? IRQ_HANDLED : IRQ_NONE;
65295a7f10eSVladimir Barinov }
65395a7f10eSVladimir Barinov 
65482c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ
65582c0de11SChaithrika U S static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
65682c0de11SChaithrika U S 				     unsigned long val, void *data)
65782c0de11SChaithrika U S {
65882c0de11SChaithrika U S 	struct davinci_i2c_dev *dev;
65982c0de11SChaithrika U S 
66082c0de11SChaithrika U S 	dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
66182c0de11SChaithrika U S 	if (val == CPUFREQ_PRECHANGE) {
66282c0de11SChaithrika U S 		wait_for_completion(&dev->xfr_complete);
66382c0de11SChaithrika U S 		davinci_i2c_reset_ctrl(dev, 0);
66482c0de11SChaithrika U S 	} else if (val == CPUFREQ_POSTCHANGE) {
66582c0de11SChaithrika U S 		i2c_davinci_calc_clk_dividers(dev);
66682c0de11SChaithrika U S 		davinci_i2c_reset_ctrl(dev, 1);
66782c0de11SChaithrika U S 	}
66882c0de11SChaithrika U S 
66982c0de11SChaithrika U S 	return 0;
67082c0de11SChaithrika U S }
67182c0de11SChaithrika U S 
67282c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
67382c0de11SChaithrika U S {
67482c0de11SChaithrika U S 	dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
67582c0de11SChaithrika U S 
67682c0de11SChaithrika U S 	return cpufreq_register_notifier(&dev->freq_transition,
67782c0de11SChaithrika U S 					 CPUFREQ_TRANSITION_NOTIFIER);
67882c0de11SChaithrika U S }
67982c0de11SChaithrika U S 
68082c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
68182c0de11SChaithrika U S {
68282c0de11SChaithrika U S 	cpufreq_unregister_notifier(&dev->freq_transition,
68382c0de11SChaithrika U S 				    CPUFREQ_TRANSITION_NOTIFIER);
68482c0de11SChaithrika U S }
68582c0de11SChaithrika U S #else
68682c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
68782c0de11SChaithrika U S {
68882c0de11SChaithrika U S 	return 0;
68982c0de11SChaithrika U S }
69082c0de11SChaithrika U S 
69182c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
69282c0de11SChaithrika U S {
69382c0de11SChaithrika U S }
69482c0de11SChaithrika U S #endif
69582c0de11SChaithrika U S 
69695a7f10eSVladimir Barinov static struct i2c_algorithm i2c_davinci_algo = {
69795a7f10eSVladimir Barinov 	.master_xfer	= i2c_davinci_xfer,
69895a7f10eSVladimir Barinov 	.functionality	= i2c_davinci_func,
69995a7f10eSVladimir Barinov };
70095a7f10eSVladimir Barinov 
7015c3d8a46SHeiko Schocher static const struct of_device_id davinci_i2c_of_match[] = {
7025c3d8a46SHeiko Schocher 	{.compatible = "ti,davinci-i2c", },
7035c3d8a46SHeiko Schocher 	{},
7045c3d8a46SHeiko Schocher };
7055c3d8a46SHeiko Schocher MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
7065c3d8a46SHeiko Schocher 
70795a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev)
70895a7f10eSVladimir Barinov {
70995a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev;
71095a7f10eSVladimir Barinov 	struct i2c_adapter *adap;
7112c6ef04fSGrygorii Strashko 	struct resource *mem;
7122c6ef04fSGrygorii Strashko 	int r, irq;
71395a7f10eSVladimir Barinov 
7142c6ef04fSGrygorii Strashko 	irq = platform_get_irq(pdev, 0);
7152c6ef04fSGrygorii Strashko 	if (irq <= 0) {
7162c6ef04fSGrygorii Strashko 		if (!irq)
7172c6ef04fSGrygorii Strashko 			irq = -ENXIO;
7182c6ef04fSGrygorii Strashko 		if (irq != -EPROBE_DEFER)
7192c6ef04fSGrygorii Strashko 			dev_err(&pdev->dev,
7202c6ef04fSGrygorii Strashko 				"can't get irq resource ret=%d\n", irq);
7212c6ef04fSGrygorii Strashko 		return irq;
72295a7f10eSVladimir Barinov 	}
72395a7f10eSVladimir Barinov 
72485796843SVishwanathrao Badarkhe, Manish 	dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev),
72585796843SVishwanathrao Badarkhe, Manish 			GFP_KERNEL);
72695a7f10eSVladimir Barinov 	if (!dev) {
72785796843SVishwanathrao Badarkhe, Manish 		dev_err(&pdev->dev, "Memory allocation failed\n");
72885796843SVishwanathrao Badarkhe, Manish 		return -ENOMEM;
72995a7f10eSVladimir Barinov 	}
73095a7f10eSVladimir Barinov 
7312e743787STroy Kisky 	init_completion(&dev->cmd_complete);
73282c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ
73382c0de11SChaithrika U S 	init_completion(&dev->xfr_complete);
73482c0de11SChaithrika U S #endif
735c4df5000SWolfram Sang 	dev->dev = &pdev->dev;
7362c6ef04fSGrygorii Strashko 	dev->irq = irq;
7371e2c2390SOlof Johansson 	dev->pdata = dev_get_platdata(&pdev->dev);
73895a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, dev);
73995a7f10eSVladimir Barinov 
7405c3d8a46SHeiko Schocher 	if (!dev->pdata && pdev->dev.of_node) {
7415c3d8a46SHeiko Schocher 		u32 prop;
7425c3d8a46SHeiko Schocher 
7435c3d8a46SHeiko Schocher 		dev->pdata = devm_kzalloc(&pdev->dev,
7445c3d8a46SHeiko Schocher 			sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
745c4df5000SWolfram Sang 		if (!dev->pdata)
746c4df5000SWolfram Sang 			return -ENOMEM;
747c4df5000SWolfram Sang 
7485c3d8a46SHeiko Schocher 		memcpy(dev->pdata, &davinci_i2c_platform_data_default,
7495c3d8a46SHeiko Schocher 			sizeof(struct davinci_i2c_platform_data));
7505c3d8a46SHeiko Schocher 		if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
7515c3d8a46SHeiko Schocher 			&prop))
7525c3d8a46SHeiko Schocher 			dev->pdata->bus_freq = prop / 1000;
7537ef97e9aSGrygorii Strashko 
7547ef97e9aSGrygorii Strashko 		dev->pdata->has_pfunc =
7557ef97e9aSGrygorii Strashko 			of_property_read_bool(pdev->dev.of_node,
7567ef97e9aSGrygorii Strashko 					      "ti,has-pfunc");
7575c3d8a46SHeiko Schocher 	} else if (!dev->pdata) {
7585c3d8a46SHeiko Schocher 		dev->pdata = &davinci_i2c_platform_data_default;
7595c3d8a46SHeiko Schocher 	}
7605c3d8a46SHeiko Schocher 
76185796843SVishwanathrao Badarkhe, Manish 	dev->clk = devm_clk_get(&pdev->dev, NULL);
762c4df5000SWolfram Sang 	if (IS_ERR(dev->clk))
763c4df5000SWolfram Sang 		return -ENODEV;
7642bdbfa9cSMurali Karicheri 	clk_prepare_enable(dev->clk);
76595a7f10eSVladimir Barinov 
7663cc2d009SWolfram Sang 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
76785796843SVishwanathrao Badarkhe, Manish 	dev->base = devm_ioremap_resource(&pdev->dev, mem);
76885796843SVishwanathrao Badarkhe, Manish 	if (IS_ERR(dev->base)) {
76985796843SVishwanathrao Badarkhe, Manish 		r = PTR_ERR(dev->base);
77085796843SVishwanathrao Badarkhe, Manish 		goto err_unuse_clocks;
771c062a251SChaithrika U S 	}
772c062a251SChaithrika U S 
77395a7f10eSVladimir Barinov 	i2c_davinci_init(dev);
77495a7f10eSVladimir Barinov 
77585796843SVishwanathrao Badarkhe, Manish 	r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
77685796843SVishwanathrao Badarkhe, Manish 			pdev->name, dev);
77795a7f10eSVladimir Barinov 	if (r) {
77895a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
77995a7f10eSVladimir Barinov 		goto err_unuse_clocks;
78095a7f10eSVladimir Barinov 	}
78195a7f10eSVladimir Barinov 
78282c0de11SChaithrika U S 	r = i2c_davinci_cpufreq_register(dev);
78382c0de11SChaithrika U S 	if (r) {
78482c0de11SChaithrika U S 		dev_err(&pdev->dev, "failed to register cpufreq\n");
78585796843SVishwanathrao Badarkhe, Manish 		goto err_unuse_clocks;
78682c0de11SChaithrika U S 	}
78782c0de11SChaithrika U S 
78895a7f10eSVladimir Barinov 	adap = &dev->adapter;
78995a7f10eSVladimir Barinov 	i2c_set_adapdata(adap, dev);
79095a7f10eSVladimir Barinov 	adap->owner = THIS_MODULE;
791adcb82a9SWolfram Sang 	adap->class = I2C_CLASS_DEPRECATED;
79295a7f10eSVladimir Barinov 	strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
79395a7f10eSVladimir Barinov 	adap->algo = &i2c_davinci_algo;
79495a7f10eSVladimir Barinov 	adap->dev.parent = &pdev->dev;
79598a679caSJean Delvare 	adap->timeout = DAVINCI_I2C_TIMEOUT;
7965c3d8a46SHeiko Schocher 	adap->dev.of_node = pdev->dev.of_node;
79795a7f10eSVladimir Barinov 
7987ef97e9aSGrygorii Strashko 	if (dev->pdata->has_pfunc)
7997ef97e9aSGrygorii Strashko 		adap->bus_recovery_info = &davinci_i2c_scl_recovery_info;
8007ef97e9aSGrygorii Strashko 	else if (dev->pdata->scl_pin) {
8012e65676fSGrygorii Strashko 		adap->bus_recovery_info = &davinci_i2c_gpio_recovery_info;
8022e65676fSGrygorii Strashko 		adap->bus_recovery_info->scl_gpio = dev->pdata->scl_pin;
8032e65676fSGrygorii Strashko 		adap->bus_recovery_info->sda_gpio = dev->pdata->sda_pin;
8042e65676fSGrygorii Strashko 	}
8052e65676fSGrygorii Strashko 
80695a7f10eSVladimir Barinov 	adap->nr = pdev->id;
80795a7f10eSVladimir Barinov 	r = i2c_add_numbered_adapter(adap);
80895a7f10eSVladimir Barinov 	if (r) {
80995a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure adding adapter\n");
81085796843SVishwanathrao Badarkhe, Manish 		goto err_unuse_clocks;
81195a7f10eSVladimir Barinov 	}
81295a7f10eSVladimir Barinov 
81395a7f10eSVladimir Barinov 	return 0;
81495a7f10eSVladimir Barinov 
81595a7f10eSVladimir Barinov err_unuse_clocks:
8162bdbfa9cSMurali Karicheri 	clk_disable_unprepare(dev->clk);
81795a7f10eSVladimir Barinov 	dev->clk = NULL;
81895a7f10eSVladimir Barinov 	return r;
81995a7f10eSVladimir Barinov }
82095a7f10eSVladimir Barinov 
82195a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev)
82295a7f10eSVladimir Barinov {
82395a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
82495a7f10eSVladimir Barinov 
82582c0de11SChaithrika U S 	i2c_davinci_cpufreq_deregister(dev);
82682c0de11SChaithrika U S 
82795a7f10eSVladimir Barinov 	i2c_del_adapter(&dev->adapter);
82895a7f10eSVladimir Barinov 
8292bdbfa9cSMurali Karicheri 	clk_disable_unprepare(dev->clk);
83095a7f10eSVladimir Barinov 	dev->clk = NULL;
83195a7f10eSVladimir Barinov 
83295a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
83395a7f10eSVladimir Barinov 
83495a7f10eSVladimir Barinov 	return 0;
83595a7f10eSVladimir Barinov }
83695a7f10eSVladimir Barinov 
83768f15de9SChaithrika U S #ifdef CONFIG_PM
83868f15de9SChaithrika U S static int davinci_i2c_suspend(struct device *dev)
83968f15de9SChaithrika U S {
84068f15de9SChaithrika U S 	struct platform_device *pdev = to_platform_device(dev);
84168f15de9SChaithrika U S 	struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
84268f15de9SChaithrika U S 
84368f15de9SChaithrika U S 	/* put I2C into reset */
84468f15de9SChaithrika U S 	davinci_i2c_reset_ctrl(i2c_dev, 0);
8452bdbfa9cSMurali Karicheri 	clk_disable_unprepare(i2c_dev->clk);
84668f15de9SChaithrika U S 
84768f15de9SChaithrika U S 	return 0;
84868f15de9SChaithrika U S }
84968f15de9SChaithrika U S 
85068f15de9SChaithrika U S static int davinci_i2c_resume(struct device *dev)
85168f15de9SChaithrika U S {
85268f15de9SChaithrika U S 	struct platform_device *pdev = to_platform_device(dev);
85368f15de9SChaithrika U S 	struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
85468f15de9SChaithrika U S 
8552bdbfa9cSMurali Karicheri 	clk_prepare_enable(i2c_dev->clk);
85668f15de9SChaithrika U S 	/* take I2C out of reset */
85768f15de9SChaithrika U S 	davinci_i2c_reset_ctrl(i2c_dev, 1);
85868f15de9SChaithrika U S 
85968f15de9SChaithrika U S 	return 0;
86068f15de9SChaithrika U S }
86168f15de9SChaithrika U S 
86268f15de9SChaithrika U S static const struct dev_pm_ops davinci_i2c_pm = {
86368f15de9SChaithrika U S 	.suspend        = davinci_i2c_suspend,
86468f15de9SChaithrika U S 	.resume         = davinci_i2c_resume,
86568f15de9SChaithrika U S };
86668f15de9SChaithrika U S 
86768f15de9SChaithrika U S #define davinci_i2c_pm_ops (&davinci_i2c_pm)
86868f15de9SChaithrika U S #else
86968f15de9SChaithrika U S #define davinci_i2c_pm_ops NULL
87068f15de9SChaithrika U S #endif
87168f15de9SChaithrika U S 
872add8eda7SKay Sievers /* work with hotplug and coldplug */
873add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci");
874add8eda7SKay Sievers 
87595a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = {
87695a7f10eSVladimir Barinov 	.probe		= davinci_i2c_probe,
87795a7f10eSVladimir Barinov 	.remove		= davinci_i2c_remove,
87895a7f10eSVladimir Barinov 	.driver		= {
87995a7f10eSVladimir Barinov 		.name	= "i2c_davinci",
88068f15de9SChaithrika U S 		.pm	= davinci_i2c_pm_ops,
8814e905323SSachin Kamat 		.of_match_table = davinci_i2c_of_match,
88295a7f10eSVladimir Barinov 	},
88395a7f10eSVladimir Barinov };
88495a7f10eSVladimir Barinov 
88595a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */
88695a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void)
88795a7f10eSVladimir Barinov {
88895a7f10eSVladimir Barinov 	return platform_driver_register(&davinci_i2c_driver);
88995a7f10eSVladimir Barinov }
89095a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver);
89195a7f10eSVladimir Barinov 
89295a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void)
89395a7f10eSVladimir Barinov {
89495a7f10eSVladimir Barinov 	platform_driver_unregister(&davinci_i2c_driver);
89595a7f10eSVladimir Barinov }
89695a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver);
89795a7f10eSVladimir Barinov 
89895a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India");
89995a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
90095a7f10eSVladimir Barinov MODULE_LICENSE("GPL");
901