xref: /openbmc/linux/drivers/i2c/busses/i2c-davinci.c (revision cc8de9a68599b261244ea453b38678229f06ada7)
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>
36e5353765SLinus Walleij #include <linux/gpio/consumer.h>
375c3d8a46SHeiko Schocher #include <linux/of_device.h>
38ec2a0833SArnd Bergmann #include <linux/platform_data/i2c-davinci.h>
3917f88151SFranklin S Cooper Jr #include <linux/pm_runtime.h>
4095a7f10eSVladimir Barinov 
4195a7f10eSVladimir Barinov /* ----- global defines ----------------------------------------------- */
4295a7f10eSVladimir Barinov 
4395a7f10eSVladimir Barinov #define DAVINCI_I2C_TIMEOUT	(1*HZ)
448574faf9SPhilby John #define DAVINCI_I2C_MAX_TRIES	2
4541c8d452SAlexander Sverdlin #define DAVINCI_I2C_OWN_ADDRESS	0x08
4641c8d452SAlexander Sverdlin #define I2C_DAVINCI_INTR_ALL    (DAVINCI_I2C_IMR_SCD | \
4795a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_ARDY | \
4895a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_NACK | \
4995a7f10eSVladimir Barinov 				 DAVINCI_I2C_IMR_AL)
5095a7f10eSVladimir Barinov 
5195a7f10eSVladimir Barinov #define DAVINCI_I2C_OAR_REG	0x00
5295a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_REG	0x04
5395a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_REG	0x08
5495a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKL_REG	0x0c
5595a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKH_REG	0x10
5695a7f10eSVladimir Barinov #define DAVINCI_I2C_CNT_REG	0x14
5795a7f10eSVladimir Barinov #define DAVINCI_I2C_DRR_REG	0x18
5895a7f10eSVladimir Barinov #define DAVINCI_I2C_SAR_REG	0x1c
5995a7f10eSVladimir Barinov #define DAVINCI_I2C_DXR_REG	0x20
6095a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_REG	0x24
6195a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_REG	0x28
6295a7f10eSVladimir Barinov #define DAVINCI_I2C_EMDR_REG	0x2c
6395a7f10eSVladimir Barinov #define DAVINCI_I2C_PSC_REG	0x30
647ef97e9aSGrygorii Strashko #define DAVINCI_I2C_FUNC_REG	0x48
657ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_REG	0x4c
667ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_REG	0x50
677ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DOUT_REG	0x54
687ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_REG	0x58
697ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_REG	0x5c
7095a7f10eSVladimir Barinov 
7195a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AAS	0x07
7295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_SCD	0x06
7395a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_XRDY	0x05
7495a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_RDR	0x04
7595a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_ARDY	0x03
7695a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_NACK	0x02
7795a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AL	0x01
7895a7f10eSVladimir Barinov 
79c062a251SChaithrika U S #define DAVINCI_I2C_STR_BB	BIT(12)
80c062a251SChaithrika U S #define DAVINCI_I2C_STR_RSFULL	BIT(11)
81c062a251SChaithrika U S #define DAVINCI_I2C_STR_SCD	BIT(5)
82c062a251SChaithrika U S #define DAVINCI_I2C_STR_ARDY	BIT(2)
83c062a251SChaithrika U S #define DAVINCI_I2C_STR_NACK	BIT(1)
84c062a251SChaithrika U S #define DAVINCI_I2C_STR_AL	BIT(0)
8595a7f10eSVladimir Barinov 
86c062a251SChaithrika U S #define DAVINCI_I2C_MDR_NACK	BIT(15)
87c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STT	BIT(13)
88c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STP	BIT(11)
89c062a251SChaithrika U S #define DAVINCI_I2C_MDR_MST	BIT(10)
90c062a251SChaithrika U S #define DAVINCI_I2C_MDR_TRX	BIT(9)
91c062a251SChaithrika U S #define DAVINCI_I2C_MDR_XA	BIT(8)
92c062a251SChaithrika U S #define DAVINCI_I2C_MDR_RM	BIT(7)
93c062a251SChaithrika U S #define DAVINCI_I2C_MDR_IRS	BIT(5)
9495a7f10eSVladimir Barinov 
95c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AAS	BIT(6)
96c062a251SChaithrika U S #define DAVINCI_I2C_IMR_SCD	BIT(5)
97c062a251SChaithrika U S #define DAVINCI_I2C_IMR_XRDY	BIT(4)
98c062a251SChaithrika U S #define DAVINCI_I2C_IMR_RRDY	BIT(3)
99c062a251SChaithrika U S #define DAVINCI_I2C_IMR_ARDY	BIT(2)
100c062a251SChaithrika U S #define DAVINCI_I2C_IMR_NACK	BIT(1)
101c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AL	BIT(0)
10295a7f10eSVladimir Barinov 
1037ef97e9aSGrygorii Strashko /* set SDA and SCL as GPIO */
1047ef97e9aSGrygorii Strashko #define DAVINCI_I2C_FUNC_PFUNC0	BIT(0)
1057ef97e9aSGrygorii Strashko 
1067ef97e9aSGrygorii Strashko /* set SCL as output when used as GPIO*/
1077ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_PDIR0	BIT(0)
1087ef97e9aSGrygorii Strashko /* set SDA as output when used as GPIO*/
1097ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_PDIR1	BIT(1)
1107ef97e9aSGrygorii Strashko 
1117ef97e9aSGrygorii Strashko /* read SCL GPIO level */
1127ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_PDIN0 BIT(0)
1137ef97e9aSGrygorii Strashko /* read SDA GPIO level */
1147ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_PDIN1 BIT(1)
1157ef97e9aSGrygorii Strashko 
1167ef97e9aSGrygorii Strashko /*set the SCL GPIO high */
1177ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_PDSET0	BIT(0)
1187ef97e9aSGrygorii Strashko /*set the SDA GPIO high */
1197ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_PDSET1	BIT(1)
1207ef97e9aSGrygorii Strashko 
1217ef97e9aSGrygorii Strashko /* set the SCL GPIO low */
1227ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_PDCLR0	BIT(0)
1237ef97e9aSGrygorii Strashko /* set the SDA GPIO low */
1247ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_PDCLR1	BIT(1)
1257ef97e9aSGrygorii Strashko 
12617f88151SFranklin S Cooper Jr /* timeout for pm runtime autosuspend */
12717f88151SFranklin S Cooper Jr #define DAVINCI_I2C_PM_TIMEOUT	1000	/* ms */
12817f88151SFranklin S Cooper Jr 
12995a7f10eSVladimir Barinov struct davinci_i2c_dev {
13095a7f10eSVladimir Barinov 	struct device           *dev;
13195a7f10eSVladimir Barinov 	void __iomem		*base;
13295a7f10eSVladimir Barinov 	struct completion	cmd_complete;
13395a7f10eSVladimir Barinov 	struct clk              *clk;
13495a7f10eSVladimir Barinov 	int			cmd_err;
13595a7f10eSVladimir Barinov 	u8			*buf;
13695a7f10eSVladimir Barinov 	size_t			buf_len;
13795a7f10eSVladimir Barinov 	int			irq;
138c6c7c729SDirk Behme 	int			stop;
1395a0d5f5fSTroy Kisky 	u8			terminate;
14095a7f10eSVladimir Barinov 	struct i2c_adapter	adapter;
14182c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ
14282c0de11SChaithrika U S 	struct notifier_block	freq_transition;
14382c0de11SChaithrika U S #endif
1445c3d8a46SHeiko Schocher 	struct davinci_i2c_platform_data *pdata;
14595a7f10eSVladimir Barinov };
14695a7f10eSVladimir Barinov 
14795a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */
14895a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
14995a7f10eSVladimir Barinov 	.bus_freq	= 100,
15095a7f10eSVladimir Barinov 	.bus_delay	= 0,
15195a7f10eSVladimir Barinov };
15295a7f10eSVladimir Barinov 
15395a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
15495a7f10eSVladimir Barinov 					 int reg, u16 val)
15595a7f10eSVladimir Barinov {
156a238dcfaSTaras Kondratiuk 	writew_relaxed(val, i2c_dev->base + reg);
15795a7f10eSVladimir Barinov }
15895a7f10eSVladimir Barinov 
15995a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
16095a7f10eSVladimir Barinov {
161a238dcfaSTaras Kondratiuk 	return readw_relaxed(i2c_dev->base + reg);
16295a7f10eSVladimir Barinov }
16395a7f10eSVladimir Barinov 
1645ae5b113SChaithrika U S static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
1655ae5b113SChaithrika U S 								int val)
1665ae5b113SChaithrika U S {
1675ae5b113SChaithrika U S 	u16 w;
1685ae5b113SChaithrika U S 
1695ae5b113SChaithrika U S 	w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
1705ae5b113SChaithrika U S 	if (!val)	/* put I2C into reset */
1715ae5b113SChaithrika U S 		w &= ~DAVINCI_I2C_MDR_IRS;
1725ae5b113SChaithrika U S 	else		/* take I2C out of reset */
1735ae5b113SChaithrika U S 		w |= DAVINCI_I2C_MDR_IRS;
1745ae5b113SChaithrika U S 
1755ae5b113SChaithrika U S 	davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
1765ae5b113SChaithrika U S }
1775ae5b113SChaithrika U S 
1785ae5b113SChaithrika U S static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
17995a7f10eSVladimir Barinov {
1805c3d8a46SHeiko Schocher 	struct davinci_i2c_platform_data *pdata = dev->pdata;
18195a7f10eSVladimir Barinov 	u16 psc;
18295a7f10eSVladimir Barinov 	u32 clk;
183cc99ff70STroy Kisky 	u32 d;
18495a7f10eSVladimir Barinov 	u32 clkh;
18595a7f10eSVladimir Barinov 	u32 clkl;
18695a7f10eSVladimir Barinov 	u32 input_clock = clk_get_rate(dev->clk);
18735780e86SAlexander Sverdlin 	struct device_node *of_node = dev->dev->of_node;
18895a7f10eSVladimir Barinov 
18995a7f10eSVladimir Barinov 	/* NOTE: I2C Clock divider programming info
19095a7f10eSVladimir Barinov 	 * As per I2C specs the following formulas provide prescaler
19195a7f10eSVladimir Barinov 	 * and low/high divider values
19295a7f10eSVladimir Barinov 	 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
19395a7f10eSVladimir Barinov 	 *                       module clk
19495a7f10eSVladimir Barinov 	 *
19595a7f10eSVladimir Barinov 	 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
19695a7f10eSVladimir Barinov 	 *
19795a7f10eSVladimir Barinov 	 * Thus,
19895a7f10eSVladimir Barinov 	 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
19995a7f10eSVladimir Barinov 	 *
20095a7f10eSVladimir Barinov 	 * where if PSC == 0, d = 7,
20195a7f10eSVladimir Barinov 	 *       if PSC == 1, d = 6
20295a7f10eSVladimir Barinov 	 *       if PSC > 1 , d = 5
20335780e86SAlexander Sverdlin 	 *
20435780e86SAlexander Sverdlin 	 * Note:
20535780e86SAlexander Sverdlin 	 * d is always 6 on Keystone I2C controller
20695a7f10eSVladimir Barinov 	 */
20795a7f10eSVladimir Barinov 
20887cb5b42SAlexander Sverdlin 	/*
20987cb5b42SAlexander Sverdlin 	 * Both Davinci and current Keystone User Guides recommend a value
21087cb5b42SAlexander Sverdlin 	 * between 7MHz and 12MHz. In reality 7MHz module clock doesn't
21187cb5b42SAlexander Sverdlin 	 * always produce enough margin between SDA and SCL transitions.
21287cb5b42SAlexander Sverdlin 	 * Measurements show that the higher the module clock is, the
21387cb5b42SAlexander Sverdlin 	 * bigger is the margin, providing more reliable communication.
21487cb5b42SAlexander Sverdlin 	 * So we better target for 12MHz.
21587cb5b42SAlexander Sverdlin 	 */
21687cb5b42SAlexander Sverdlin 	psc = (input_clock / 12000000) - 1;
217cc99ff70STroy Kisky 	if ((input_clock / (psc + 1)) > 12000000)
218cc99ff70STroy Kisky 		psc++;	/* better to run under spec than over */
219cc99ff70STroy Kisky 	d = (psc >= 2) ? 5 : 7 - psc;
22095a7f10eSVladimir Barinov 
22135780e86SAlexander Sverdlin 	if (of_node && of_device_is_compatible(of_node, "ti,keystone-i2c"))
22235780e86SAlexander Sverdlin 		d = 6;
22335780e86SAlexander Sverdlin 
224955fc950SAlexander Sverdlin 	clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000));
225955fc950SAlexander Sverdlin 	/* Avoid driving the bus too fast because of rounding errors above */
226955fc950SAlexander Sverdlin 	if (input_clock / (psc + 1) / clk > pdata->bus_freq * 1000)
227955fc950SAlexander Sverdlin 		clk++;
228955fc950SAlexander Sverdlin 	/*
229955fc950SAlexander Sverdlin 	 * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at
230955fc950SAlexander Sverdlin 	 * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH
231955fc950SAlexander Sverdlin 	 * to LOW ratio as 1 to 2 is more safe.
232955fc950SAlexander Sverdlin 	 */
233955fc950SAlexander Sverdlin 	if (pdata->bus_freq > 100)
234955fc950SAlexander Sverdlin 		clkl = (clk << 1) / 3;
235955fc950SAlexander Sverdlin 	else
236955fc950SAlexander Sverdlin 		clkl = (clk >> 1);
237955fc950SAlexander Sverdlin 	/*
238955fc950SAlexander Sverdlin 	 * It's not always possible to have 1 to 2 ratio when d=7, so fall back
239955fc950SAlexander Sverdlin 	 * to minimal possible clkh in this case.
240*cc8de9a6SAlexander Sverdlin 	 *
241*cc8de9a6SAlexander Sverdlin 	 * Note:
242*cc8de9a6SAlexander Sverdlin 	 * CLKH is not allowed to be 0, in this case I2C clock is not generated
243*cc8de9a6SAlexander Sverdlin 	 * at all
244955fc950SAlexander Sverdlin 	 */
245*cc8de9a6SAlexander Sverdlin 	if (clk > clkl + d) {
246955fc950SAlexander Sverdlin 		clkh = clk - clkl - d;
247955fc950SAlexander Sverdlin 		clkl -= d;
248955fc950SAlexander Sverdlin 	} else {
249*cc8de9a6SAlexander Sverdlin 		clkh = 1;
250955fc950SAlexander Sverdlin 		clkl = clk - (d << 1);
251955fc950SAlexander Sverdlin 	}
25295a7f10eSVladimir Barinov 
25395a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
25495a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
25595a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
25695a7f10eSVladimir Barinov 
2575ae5b113SChaithrika U S 	dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
2585ae5b113SChaithrika U S }
2595ae5b113SChaithrika U S 
2605ae5b113SChaithrika U S /*
2615ae5b113SChaithrika U S  * This function configures I2C and brings I2C out of reset.
2625ae5b113SChaithrika U S  * This function is called during I2C init function. This function
2635ae5b113SChaithrika U S  * also gets called if I2C encounters any errors.
2645ae5b113SChaithrika U S  */
2655ae5b113SChaithrika U S static int i2c_davinci_init(struct davinci_i2c_dev *dev)
2665ae5b113SChaithrika U S {
2675c3d8a46SHeiko Schocher 	struct davinci_i2c_platform_data *pdata = dev->pdata;
2685ae5b113SChaithrika U S 
2695ae5b113SChaithrika U S 	/* put I2C into reset */
2705ae5b113SChaithrika U S 	davinci_i2c_reset_ctrl(dev, 0);
2715ae5b113SChaithrika U S 
2725ae5b113SChaithrika U S 	/* compute clock dividers */
2735ae5b113SChaithrika U S 	i2c_davinci_calc_clk_dividers(dev);
2745ae5b113SChaithrika U S 
2757605fa3bSDavid Brownell 	/* Respond at reserved "SMBus Host" slave address" (and zero);
2767605fa3bSDavid Brownell 	 * we seem to have no option to not respond...
2777605fa3bSDavid Brownell 	 */
27841c8d452SAlexander Sverdlin 	davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, DAVINCI_I2C_OWN_ADDRESS);
2797605fa3bSDavid Brownell 
28095a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "PSC  = %d\n",
28195a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
28295a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKL = %d\n",
28395a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
28495a7f10eSVladimir Barinov 	dev_dbg(dev->dev, "CLKH = %d\n",
28595a7f10eSVladimir Barinov 		davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
286cc99ff70STroy Kisky 	dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
287cc99ff70STroy Kisky 		pdata->bus_freq, pdata->bus_delay);
28895a7f10eSVladimir Barinov 
2895c3d8a46SHeiko Schocher 
29095a7f10eSVladimir Barinov 	/* Take the I2C module out of reset: */
2915ae5b113SChaithrika U S 	davinci_i2c_reset_ctrl(dev, 1);
29295a7f10eSVladimir Barinov 
29395a7f10eSVladimir Barinov 	/* Enable interrupts */
29495a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
29595a7f10eSVladimir Barinov 
29695a7f10eSVladimir Barinov 	return 0;
29795a7f10eSVladimir Barinov }
29895a7f10eSVladimir Barinov 
29995a7f10eSVladimir Barinov /*
300cd2428c3SPhil Reid  * This routine does i2c bus recovery by using i2c_generic_scl_recovery
3012e65676fSGrygorii Strashko  * which is provided by I2C Bus recovery infrastructure.
3022e65676fSGrygorii Strashko  */
3032e65676fSGrygorii Strashko static void davinci_i2c_prepare_recovery(struct i2c_adapter *adap)
3042e65676fSGrygorii Strashko {
3052e65676fSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3062e65676fSGrygorii Strashko 
3072e65676fSGrygorii Strashko 	/* Disable interrupts */
3082e65676fSGrygorii Strashko 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, 0);
3092e65676fSGrygorii Strashko 
3102e65676fSGrygorii Strashko 	/* put I2C into reset */
3112e65676fSGrygorii Strashko 	davinci_i2c_reset_ctrl(dev, 0);
3122e65676fSGrygorii Strashko }
3132e65676fSGrygorii Strashko 
3142e65676fSGrygorii Strashko static void davinci_i2c_unprepare_recovery(struct i2c_adapter *adap)
3152e65676fSGrygorii Strashko {
3162e65676fSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3172e65676fSGrygorii Strashko 
3182e65676fSGrygorii Strashko 	i2c_davinci_init(dev);
3192e65676fSGrygorii Strashko }
3202e65676fSGrygorii Strashko 
3212e65676fSGrygorii Strashko static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info = {
322cd2428c3SPhil Reid 	.recover_bus = i2c_generic_scl_recovery,
3232e65676fSGrygorii Strashko 	.prepare_recovery = davinci_i2c_prepare_recovery,
3242e65676fSGrygorii Strashko 	.unprepare_recovery = davinci_i2c_unprepare_recovery,
3252e65676fSGrygorii Strashko };
3262e65676fSGrygorii Strashko 
3277ef97e9aSGrygorii Strashko static void davinci_i2c_set_scl(struct i2c_adapter *adap, int val)
3287ef97e9aSGrygorii Strashko {
3297ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3307ef97e9aSGrygorii Strashko 
3317ef97e9aSGrygorii Strashko 	if (val)
3327ef97e9aSGrygorii Strashko 		davinci_i2c_write_reg(dev, DAVINCI_I2C_DSET_REG,
3337ef97e9aSGrygorii Strashko 				      DAVINCI_I2C_DSET_PDSET0);
3347ef97e9aSGrygorii Strashko 	else
3357ef97e9aSGrygorii Strashko 		davinci_i2c_write_reg(dev, DAVINCI_I2C_DCLR_REG,
3367ef97e9aSGrygorii Strashko 				      DAVINCI_I2C_DCLR_PDCLR0);
3377ef97e9aSGrygorii Strashko }
3387ef97e9aSGrygorii Strashko 
3397ef97e9aSGrygorii Strashko static int davinci_i2c_get_scl(struct i2c_adapter *adap)
3407ef97e9aSGrygorii Strashko {
3417ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3427ef97e9aSGrygorii Strashko 	int val;
3437ef97e9aSGrygorii Strashko 
3447ef97e9aSGrygorii Strashko 	/* read the state of SCL */
3457ef97e9aSGrygorii Strashko 	val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
3467ef97e9aSGrygorii Strashko 	return val & DAVINCI_I2C_DIN_PDIN0;
3477ef97e9aSGrygorii Strashko }
3487ef97e9aSGrygorii Strashko 
3497ef97e9aSGrygorii Strashko static int davinci_i2c_get_sda(struct i2c_adapter *adap)
3507ef97e9aSGrygorii Strashko {
3517ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3527ef97e9aSGrygorii Strashko 	int val;
3537ef97e9aSGrygorii Strashko 
3547ef97e9aSGrygorii Strashko 	/* read the state of SDA */
3557ef97e9aSGrygorii Strashko 	val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
3567ef97e9aSGrygorii Strashko 	return val & DAVINCI_I2C_DIN_PDIN1;
3577ef97e9aSGrygorii Strashko }
3587ef97e9aSGrygorii Strashko 
3597ef97e9aSGrygorii Strashko static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter *adap)
3607ef97e9aSGrygorii Strashko {
3617ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3627ef97e9aSGrygorii Strashko 
3637ef97e9aSGrygorii Strashko 	davinci_i2c_prepare_recovery(adap);
3647ef97e9aSGrygorii Strashko 
3657ef97e9aSGrygorii Strashko 	/* SCL output, SDA input */
3667ef97e9aSGrygorii Strashko 	davinci_i2c_write_reg(dev, DAVINCI_I2C_DIR_REG, DAVINCI_I2C_DIR_PDIR0);
3677ef97e9aSGrygorii Strashko 
3687ef97e9aSGrygorii Strashko 	/* change to GPIO mode */
3697ef97e9aSGrygorii Strashko 	davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG,
3707ef97e9aSGrygorii Strashko 			      DAVINCI_I2C_FUNC_PFUNC0);
3717ef97e9aSGrygorii Strashko }
3727ef97e9aSGrygorii Strashko 
3737ef97e9aSGrygorii Strashko static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter *adap)
3747ef97e9aSGrygorii Strashko {
3757ef97e9aSGrygorii Strashko 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3767ef97e9aSGrygorii Strashko 
3777ef97e9aSGrygorii Strashko 	/* change back to I2C mode */
3787ef97e9aSGrygorii Strashko 	davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG, 0);
3797ef97e9aSGrygorii Strashko 
3807ef97e9aSGrygorii Strashko 	davinci_i2c_unprepare_recovery(adap);
3817ef97e9aSGrygorii Strashko }
3827ef97e9aSGrygorii Strashko 
3837ef97e9aSGrygorii Strashko static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info = {
3847ef97e9aSGrygorii Strashko 	.recover_bus = i2c_generic_scl_recovery,
3857ef97e9aSGrygorii Strashko 	.set_scl = davinci_i2c_set_scl,
3867ef97e9aSGrygorii Strashko 	.get_scl = davinci_i2c_get_scl,
3877ef97e9aSGrygorii Strashko 	.get_sda = davinci_i2c_get_sda,
3887ef97e9aSGrygorii Strashko 	.prepare_recovery = davinci_i2c_scl_prepare_recovery,
3897ef97e9aSGrygorii Strashko 	.unprepare_recovery = davinci_i2c_scl_unprepare_recovery,
3907ef97e9aSGrygorii Strashko };
3917ef97e9aSGrygorii Strashko 
3922e65676fSGrygorii Strashko /*
39395a7f10eSVladimir Barinov  * Waiting for bus not busy
39495a7f10eSVladimir Barinov  */
39572a956c5SAlexander Sverdlin static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev)
39695a7f10eSVladimir Barinov {
39772a956c5SAlexander Sverdlin 	unsigned long timeout = jiffies + dev->adapter.timeout;
39895a7f10eSVladimir Barinov 
39972a956c5SAlexander Sverdlin 	do {
40072a956c5SAlexander Sverdlin 		if (!(davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB))
40172a956c5SAlexander Sverdlin 			return 0;
40272a956c5SAlexander Sverdlin 		schedule_timeout_uninterruptible(1);
40372a956c5SAlexander Sverdlin 	} while (time_before_eq(jiffies, timeout));
40472a956c5SAlexander Sverdlin 
40572a956c5SAlexander Sverdlin 	dev_warn(dev->dev, "timeout waiting for bus ready\n");
4062e65676fSGrygorii Strashko 	i2c_recover_bus(&dev->adapter);
40772a956c5SAlexander Sverdlin 
40872a956c5SAlexander Sverdlin 	/*
40972a956c5SAlexander Sverdlin 	 * if bus is still "busy" here, it's most probably a HW problem like
41072a956c5SAlexander Sverdlin 	 * short-circuit
41172a956c5SAlexander Sverdlin 	 */
41272a956c5SAlexander Sverdlin 	if (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB)
41372a956c5SAlexander Sverdlin 		return -EIO;
41495a7f10eSVladimir Barinov 
41595a7f10eSVladimir Barinov 	return 0;
41695a7f10eSVladimir Barinov }
41795a7f10eSVladimir Barinov 
41895a7f10eSVladimir Barinov /*
41995a7f10eSVladimir Barinov  * Low level master read/write transaction. This function is called
42095a7f10eSVladimir Barinov  * from i2c_davinci_xfer.
42195a7f10eSVladimir Barinov  */
42295a7f10eSVladimir Barinov static int
42395a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
42495a7f10eSVladimir Barinov {
42595a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
4265c3d8a46SHeiko Schocher 	struct davinci_i2c_platform_data *pdata = dev->pdata;
42795a7f10eSVladimir Barinov 	u32 flag;
42895a7f10eSVladimir Barinov 	u16 w;
429d9e1f441SNicholas Mc Guire 	unsigned long time_left;
43095a7f10eSVladimir Barinov 
43141c8d452SAlexander Sverdlin 	if (msg->addr == DAVINCI_I2C_OWN_ADDRESS) {
43241c8d452SAlexander Sverdlin 		dev_warn(dev->dev, "transfer to own address aborted\n");
43341c8d452SAlexander Sverdlin 		return -EADDRNOTAVAIL;
43441c8d452SAlexander Sverdlin 	}
43541c8d452SAlexander Sverdlin 
43695a7f10eSVladimir Barinov 	/* Introduce a delay, required for some boards (e.g Davinci EVM) */
43795a7f10eSVladimir Barinov 	if (pdata->bus_delay)
43895a7f10eSVladimir Barinov 		udelay(pdata->bus_delay);
43995a7f10eSVladimir Barinov 
44095a7f10eSVladimir Barinov 	/* set the slave address */
44195a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
44295a7f10eSVladimir Barinov 
44395a7f10eSVladimir Barinov 	dev->buf = msg->buf;
44495a7f10eSVladimir Barinov 	dev->buf_len = msg->len;
445c6c7c729SDirk Behme 	dev->stop = stop;
44695a7f10eSVladimir Barinov 
44795a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
44895a7f10eSVladimir Barinov 
44916735d02SWolfram Sang 	reinit_completion(&dev->cmd_complete);
45095a7f10eSVladimir Barinov 	dev->cmd_err = 0;
45195a7f10eSVladimir Barinov 
452c5b4afecSJon Povey 	/* Take I2C out of reset and configure it as master */
453c5b4afecSJon Povey 	flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
45495a7f10eSVladimir Barinov 
45595a7f10eSVladimir Barinov 	/* if the slave address is ten bit address, enable XA bit */
45695a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_TEN)
45795a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_XA;
45895a7f10eSVladimir Barinov 	if (!(msg->flags & I2C_M_RD))
45995a7f10eSVladimir Barinov 		flag |= DAVINCI_I2C_MDR_TRX;
460c5b4afecSJon Povey 	if (msg->len == 0)
461c6c7c729SDirk Behme 		flag |= DAVINCI_I2C_MDR_RM;
46295a7f10eSVladimir Barinov 
46395a7f10eSVladimir Barinov 	/* Enable receive or transmit interrupts */
46495a7f10eSVladimir Barinov 	w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
46595a7f10eSVladimir Barinov 	if (msg->flags & I2C_M_RD)
466c062a251SChaithrika U S 		w |= DAVINCI_I2C_IMR_RRDY;
46795a7f10eSVladimir Barinov 	else
468c062a251SChaithrika U S 		w |= DAVINCI_I2C_IMR_XRDY;
46995a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
47095a7f10eSVladimir Barinov 
4715a0d5f5fSTroy Kisky 	dev->terminate = 0;
472c6c7c729SDirk Behme 
473c6c7c729SDirk Behme 	/*
474c5b4afecSJon Povey 	 * Write mode register first as needed for correct behaviour
475c5b4afecSJon Povey 	 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
47625985edcSLucas De Marchi 	 * occurring before we have loaded DXR
477c5b4afecSJon Povey 	 */
478c5b4afecSJon Povey 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
479c5b4afecSJon Povey 
480c5b4afecSJon Povey 	/*
481c6c7c729SDirk Behme 	 * First byte should be set here, not after interrupt,
482c6c7c729SDirk Behme 	 * because transmit-data-ready interrupt can come before
483c6c7c729SDirk Behme 	 * NACK-interrupt during sending of previous message and
484c6c7c729SDirk Behme 	 * ICDXR may have wrong data
485c5b4afecSJon Povey 	 * It also saves us one interrupt, slightly faster
486c6c7c729SDirk Behme 	 */
487c6c7c729SDirk Behme 	if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
488c6c7c729SDirk Behme 		davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
489c6c7c729SDirk Behme 		dev->buf_len--;
490c6c7c729SDirk Behme 	}
491c6c7c729SDirk Behme 
492c5b4afecSJon Povey 	/* Set STT to begin transmit now DXR is loaded */
493c5b4afecSJon Povey 	flag |= DAVINCI_I2C_MDR_STT;
494c5b4afecSJon Povey 	if (stop && msg->len != 0)
495c5b4afecSJon Povey 		flag |= DAVINCI_I2C_MDR_STP;
4964bba0fd8SJon Povey 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
4974bba0fd8SJon Povey 
498d9e1f441SNicholas Mc Guire 	time_left = wait_for_completion_timeout(&dev->cmd_complete,
499d9e1f441SNicholas Mc Guire 						dev->adapter.timeout);
500d9e1f441SNicholas Mc Guire 	if (!time_left) {
50195a7f10eSVladimir Barinov 		dev_err(dev->dev, "controller timed out\n");
5022e65676fSGrygorii Strashko 		i2c_recover_bus(adap);
5035a0d5f5fSTroy Kisky 		dev->buf_len = 0;
50495a7f10eSVladimir Barinov 		return -ETIMEDOUT;
50595a7f10eSVladimir Barinov 	}
5065a0d5f5fSTroy Kisky 	if (dev->buf_len) {
5075a0d5f5fSTroy Kisky 		/* This should be 0 if all bytes were transferred
5085a0d5f5fSTroy Kisky 		 * or dev->cmd_err denotes an error.
5095a0d5f5fSTroy Kisky 		 */
510631de7a4SWolfram Sang 		dev_err(dev->dev, "abnormal termination buf_len=%zu\n",
5115a0d5f5fSTroy Kisky 			dev->buf_len);
5125a0d5f5fSTroy Kisky 		dev->terminate = 1;
5135a0d5f5fSTroy Kisky 		wmb();
5145a0d5f5fSTroy Kisky 		dev->buf_len = 0;
515d9e1f441SNicholas Mc Guire 		return -EREMOTEIO;
5165a0d5f5fSTroy Kisky 	}
51795a7f10eSVladimir Barinov 
51895a7f10eSVladimir Barinov 	/* no error */
51995a7f10eSVladimir Barinov 	if (likely(!dev->cmd_err))
52095a7f10eSVladimir Barinov 		return msg->len;
52195a7f10eSVladimir Barinov 
52295a7f10eSVladimir Barinov 	/* We have an error */
52395a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
52495a7f10eSVladimir Barinov 		i2c_davinci_init(dev);
52595a7f10eSVladimir Barinov 		return -EIO;
52695a7f10eSVladimir Barinov 	}
52795a7f10eSVladimir Barinov 
52895a7f10eSVladimir Barinov 	if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
52995a7f10eSVladimir Barinov 		if (msg->flags & I2C_M_IGNORE_NAK)
53095a7f10eSVladimir Barinov 			return msg->len;
53195a7f10eSVladimir Barinov 		w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
532c062a251SChaithrika U S 		w |= DAVINCI_I2C_MDR_STP;
53395a7f10eSVladimir Barinov 		davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
53495a7f10eSVladimir Barinov 		return -EREMOTEIO;
53595a7f10eSVladimir Barinov 	}
53695a7f10eSVladimir Barinov 	return -EIO;
53795a7f10eSVladimir Barinov }
53895a7f10eSVladimir Barinov 
53995a7f10eSVladimir Barinov /*
54095a7f10eSVladimir Barinov  * Prepare controller for a transaction and call i2c_davinci_xfer_msg
54195a7f10eSVladimir Barinov  */
54295a7f10eSVladimir Barinov static int
54395a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
54495a7f10eSVladimir Barinov {
54595a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
54695a7f10eSVladimir Barinov 	int i;
54795a7f10eSVladimir Barinov 	int ret;
54895a7f10eSVladimir Barinov 
54908882d20SHarvey Harrison 	dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
55095a7f10eSVladimir Barinov 
55117f88151SFranklin S Cooper Jr 	ret = pm_runtime_get_sync(dev->dev);
55217f88151SFranklin S Cooper Jr 	if (ret < 0) {
55317f88151SFranklin S Cooper Jr 		dev_err(dev->dev, "Failed to runtime_get device: %d\n", ret);
55417f88151SFranklin S Cooper Jr 		pm_runtime_put_noidle(dev->dev);
55517f88151SFranklin S Cooper Jr 		return ret;
55617f88151SFranklin S Cooper Jr 	}
55717f88151SFranklin S Cooper Jr 
55872a956c5SAlexander Sverdlin 	ret = i2c_davinci_wait_bus_not_busy(dev);
55995a7f10eSVladimir Barinov 	if (ret < 0) {
56095a7f10eSVladimir Barinov 		dev_warn(dev->dev, "timeout waiting for bus ready\n");
56117f88151SFranklin S Cooper Jr 		goto out;
56295a7f10eSVladimir Barinov 	}
56395a7f10eSVladimir Barinov 
56495a7f10eSVladimir Barinov 	for (i = 0; i < num; i++) {
56595a7f10eSVladimir Barinov 		ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
566d868caa1STroy Kisky 		dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
567d868caa1STroy Kisky 			ret);
56895a7f10eSVladimir Barinov 		if (ret < 0)
56917f88151SFranklin S Cooper Jr 			goto out;
57095a7f10eSVladimir Barinov 	}
57182c0de11SChaithrika U S 
57217f88151SFranklin S Cooper Jr 	ret = num;
57382c0de11SChaithrika U S 
57417f88151SFranklin S Cooper Jr out:
57517f88151SFranklin S Cooper Jr 	pm_runtime_mark_last_busy(dev->dev);
57617f88151SFranklin S Cooper Jr 	pm_runtime_put_autosuspend(dev->dev);
57717f88151SFranklin S Cooper Jr 
57817f88151SFranklin S Cooper Jr 	return ret;
57995a7f10eSVladimir Barinov }
58095a7f10eSVladimir Barinov 
58195a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap)
58295a7f10eSVladimir Barinov {
583c6c7c729SDirk Behme 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
58495a7f10eSVladimir Barinov }
58595a7f10eSVladimir Barinov 
5865a0d5f5fSTroy Kisky static void terminate_read(struct davinci_i2c_dev *dev)
5875a0d5f5fSTroy Kisky {
5885a0d5f5fSTroy Kisky 	u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
5895a0d5f5fSTroy Kisky 	w |= DAVINCI_I2C_MDR_NACK;
5905a0d5f5fSTroy Kisky 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
5915a0d5f5fSTroy Kisky 
5925a0d5f5fSTroy Kisky 	/* Throw away data */
5935a0d5f5fSTroy Kisky 	davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
5945a0d5f5fSTroy Kisky 	if (!dev->terminate)
5955a0d5f5fSTroy Kisky 		dev_err(dev->dev, "RDR IRQ while no data requested\n");
5965a0d5f5fSTroy Kisky }
5975a0d5f5fSTroy Kisky static void terminate_write(struct davinci_i2c_dev *dev)
5985a0d5f5fSTroy Kisky {
5995a0d5f5fSTroy Kisky 	u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
6005a0d5f5fSTroy Kisky 	w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
6015a0d5f5fSTroy Kisky 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
6025a0d5f5fSTroy Kisky 
6035a0d5f5fSTroy Kisky 	if (!dev->terminate)
6047605fa3bSDavid Brownell 		dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
6055a0d5f5fSTroy Kisky }
6065a0d5f5fSTroy Kisky 
60795a7f10eSVladimir Barinov /*
60895a7f10eSVladimir Barinov  * Interrupt service routine. This gets called whenever an I2C interrupt
60995a7f10eSVladimir Barinov  * occurs.
61095a7f10eSVladimir Barinov  */
61195a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
61295a7f10eSVladimir Barinov {
61395a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = dev_id;
61495a7f10eSVladimir Barinov 	u32 stat;
61595a7f10eSVladimir Barinov 	int count = 0;
61695a7f10eSVladimir Barinov 	u16 w;
61795a7f10eSVladimir Barinov 
61817f88151SFranklin S Cooper Jr 	if (pm_runtime_suspended(dev->dev))
61917f88151SFranklin S Cooper Jr 		return IRQ_NONE;
62017f88151SFranklin S Cooper Jr 
62195a7f10eSVladimir Barinov 	while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
62208882d20SHarvey Harrison 		dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
62395a7f10eSVladimir Barinov 		if (count++ == 100) {
62495a7f10eSVladimir Barinov 			dev_warn(dev->dev, "Too much work in one IRQ\n");
62595a7f10eSVladimir Barinov 			break;
62695a7f10eSVladimir Barinov 		}
62795a7f10eSVladimir Barinov 
62895a7f10eSVladimir Barinov 		switch (stat) {
62995a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AL:
6305a0d5f5fSTroy Kisky 			/* Arbitration lost, must retry */
63195a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_AL;
6325a0d5f5fSTroy Kisky 			dev->buf_len = 0;
63395a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
63495a7f10eSVladimir Barinov 			break;
63595a7f10eSVladimir Barinov 
63695a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_NACK:
63795a7f10eSVladimir Barinov 			dev->cmd_err |= DAVINCI_I2C_STR_NACK;
6385a0d5f5fSTroy Kisky 			dev->buf_len = 0;
63995a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
64095a7f10eSVladimir Barinov 			break;
64195a7f10eSVladimir Barinov 
64295a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_ARDY:
643b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
644b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
645c6c7c729SDirk Behme 			if (((dev->buf_len == 0) && (dev->stop != 0)) ||
646c6c7c729SDirk Behme 			    (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
647c6c7c729SDirk Behme 				w = davinci_i2c_read_reg(dev,
648c6c7c729SDirk Behme 							 DAVINCI_I2C_MDR_REG);
649c6c7c729SDirk Behme 				w |= DAVINCI_I2C_MDR_STP;
650c6c7c729SDirk Behme 				davinci_i2c_write_reg(dev,
651c6c7c729SDirk Behme 						      DAVINCI_I2C_MDR_REG, w);
652c6c7c729SDirk Behme 			}
65395a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
65495a7f10eSVladimir Barinov 			break;
65595a7f10eSVladimir Barinov 
65695a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_RDR:
65795a7f10eSVladimir Barinov 			if (dev->buf_len) {
65895a7f10eSVladimir Barinov 				*dev->buf++ =
65995a7f10eSVladimir Barinov 				    davinci_i2c_read_reg(dev,
66095a7f10eSVladimir Barinov 							 DAVINCI_I2C_DRR_REG);
66195a7f10eSVladimir Barinov 				dev->buf_len--;
66295a7f10eSVladimir Barinov 				if (dev->buf_len)
66395a7f10eSVladimir Barinov 					continue;
66495a7f10eSVladimir Barinov 
66595a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
66695a7f10eSVladimir Barinov 					DAVINCI_I2C_STR_REG,
667b73a9aecSTroy Kisky 					DAVINCI_I2C_IMR_RRDY);
6685a0d5f5fSTroy Kisky 			} else {
6695a0d5f5fSTroy Kisky 				/* signal can terminate transfer */
6705a0d5f5fSTroy Kisky 				terminate_read(dev);
6715a0d5f5fSTroy Kisky 			}
67295a7f10eSVladimir Barinov 			break;
67395a7f10eSVladimir Barinov 
67495a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_XRDY:
67595a7f10eSVladimir Barinov 			if (dev->buf_len) {
67695a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
67795a7f10eSVladimir Barinov 						      *dev->buf++);
67895a7f10eSVladimir Barinov 				dev->buf_len--;
67995a7f10eSVladimir Barinov 				if (dev->buf_len)
68095a7f10eSVladimir Barinov 					continue;
68195a7f10eSVladimir Barinov 
68295a7f10eSVladimir Barinov 				w = davinci_i2c_read_reg(dev,
68395a7f10eSVladimir Barinov 							 DAVINCI_I2C_IMR_REG);
684c062a251SChaithrika U S 				w &= ~DAVINCI_I2C_IMR_XRDY;
68595a7f10eSVladimir Barinov 				davinci_i2c_write_reg(dev,
68695a7f10eSVladimir Barinov 						      DAVINCI_I2C_IMR_REG,
68795a7f10eSVladimir Barinov 						      w);
6885a0d5f5fSTroy Kisky 			} else {
6895a0d5f5fSTroy Kisky 				/* signal can terminate transfer */
6905a0d5f5fSTroy Kisky 				terminate_write(dev);
6915a0d5f5fSTroy Kisky 			}
69295a7f10eSVladimir Barinov 			break;
69395a7f10eSVladimir Barinov 
69495a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_SCD:
695b73a9aecSTroy Kisky 			davinci_i2c_write_reg(dev,
696b73a9aecSTroy Kisky 				DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
69795a7f10eSVladimir Barinov 			complete(&dev->cmd_complete);
69895a7f10eSVladimir Barinov 			break;
69995a7f10eSVladimir Barinov 
70095a7f10eSVladimir Barinov 		case DAVINCI_I2C_IVR_AAS:
7017605fa3bSDavid Brownell 			dev_dbg(dev->dev, "Address as slave interrupt\n");
7027605fa3bSDavid Brownell 			break;
7037605fa3bSDavid Brownell 
7047605fa3bSDavid Brownell 		default:
7057605fa3bSDavid Brownell 			dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
7067605fa3bSDavid Brownell 			break;
7077605fa3bSDavid Brownell 		}
7087605fa3bSDavid Brownell 	}
70995a7f10eSVladimir Barinov 
71095a7f10eSVladimir Barinov 	return count ? IRQ_HANDLED : IRQ_NONE;
71195a7f10eSVladimir Barinov }
71295a7f10eSVladimir Barinov 
71382c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ
71482c0de11SChaithrika U S static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
71582c0de11SChaithrika U S 				     unsigned long val, void *data)
71682c0de11SChaithrika U S {
71782c0de11SChaithrika U S 	struct davinci_i2c_dev *dev;
71882c0de11SChaithrika U S 
71982c0de11SChaithrika U S 	dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
72019cfcafdSBartosz Golaszewski 
72119cfcafdSBartosz Golaszewski 	i2c_lock_adapter(&dev->adapter);
72282c0de11SChaithrika U S 	if (val == CPUFREQ_PRECHANGE) {
72382c0de11SChaithrika U S 		davinci_i2c_reset_ctrl(dev, 0);
72482c0de11SChaithrika U S 	} else if (val == CPUFREQ_POSTCHANGE) {
72582c0de11SChaithrika U S 		i2c_davinci_calc_clk_dividers(dev);
72682c0de11SChaithrika U S 		davinci_i2c_reset_ctrl(dev, 1);
72782c0de11SChaithrika U S 	}
72819cfcafdSBartosz Golaszewski 	i2c_unlock_adapter(&dev->adapter);
72982c0de11SChaithrika U S 
73082c0de11SChaithrika U S 	return 0;
73182c0de11SChaithrika U S }
73282c0de11SChaithrika U S 
73382c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
73482c0de11SChaithrika U S {
73582c0de11SChaithrika U S 	dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
73682c0de11SChaithrika U S 
73782c0de11SChaithrika U S 	return cpufreq_register_notifier(&dev->freq_transition,
73882c0de11SChaithrika U S 					 CPUFREQ_TRANSITION_NOTIFIER);
73982c0de11SChaithrika U S }
74082c0de11SChaithrika U S 
74182c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
74282c0de11SChaithrika U S {
74382c0de11SChaithrika U S 	cpufreq_unregister_notifier(&dev->freq_transition,
74482c0de11SChaithrika U S 				    CPUFREQ_TRANSITION_NOTIFIER);
74582c0de11SChaithrika U S }
74682c0de11SChaithrika U S #else
74782c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
74882c0de11SChaithrika U S {
74982c0de11SChaithrika U S 	return 0;
75082c0de11SChaithrika U S }
75182c0de11SChaithrika U S 
75282c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
75382c0de11SChaithrika U S {
75482c0de11SChaithrika U S }
75582c0de11SChaithrika U S #endif
75682c0de11SChaithrika U S 
7578dc0f8c7SGustavo A. R. Silva static const struct i2c_algorithm i2c_davinci_algo = {
75895a7f10eSVladimir Barinov 	.master_xfer	= i2c_davinci_xfer,
75995a7f10eSVladimir Barinov 	.functionality	= i2c_davinci_func,
76095a7f10eSVladimir Barinov };
76195a7f10eSVladimir Barinov 
7625c3d8a46SHeiko Schocher static const struct of_device_id davinci_i2c_of_match[] = {
7635c3d8a46SHeiko Schocher 	{.compatible = "ti,davinci-i2c", },
76435780e86SAlexander Sverdlin 	{.compatible = "ti,keystone-i2c", },
7655c3d8a46SHeiko Schocher 	{},
7665c3d8a46SHeiko Schocher };
7675c3d8a46SHeiko Schocher MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
7685c3d8a46SHeiko Schocher 
76995a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev)
77095a7f10eSVladimir Barinov {
77195a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev;
77295a7f10eSVladimir Barinov 	struct i2c_adapter *adap;
7732c6ef04fSGrygorii Strashko 	struct resource *mem;
774cd2428c3SPhil Reid 	struct i2c_bus_recovery_info *rinfo;
7752c6ef04fSGrygorii Strashko 	int r, irq;
77695a7f10eSVladimir Barinov 
7772c6ef04fSGrygorii Strashko 	irq = platform_get_irq(pdev, 0);
7782c6ef04fSGrygorii Strashko 	if (irq <= 0) {
7792c6ef04fSGrygorii Strashko 		if (!irq)
7802c6ef04fSGrygorii Strashko 			irq = -ENXIO;
7812c6ef04fSGrygorii Strashko 		if (irq != -EPROBE_DEFER)
7822c6ef04fSGrygorii Strashko 			dev_err(&pdev->dev,
7832c6ef04fSGrygorii Strashko 				"can't get irq resource ret=%d\n", irq);
7842c6ef04fSGrygorii Strashko 		return irq;
78595a7f10eSVladimir Barinov 	}
78695a7f10eSVladimir Barinov 
78785796843SVishwanathrao Badarkhe, Manish 	dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev),
78885796843SVishwanathrao Badarkhe, Manish 			GFP_KERNEL);
78995a7f10eSVladimir Barinov 	if (!dev) {
79085796843SVishwanathrao Badarkhe, Manish 		dev_err(&pdev->dev, "Memory allocation failed\n");
79185796843SVishwanathrao Badarkhe, Manish 		return -ENOMEM;
79295a7f10eSVladimir Barinov 	}
79395a7f10eSVladimir Barinov 
7942e743787STroy Kisky 	init_completion(&dev->cmd_complete);
79519cfcafdSBartosz Golaszewski 
796c4df5000SWolfram Sang 	dev->dev = &pdev->dev;
7972c6ef04fSGrygorii Strashko 	dev->irq = irq;
7981e2c2390SOlof Johansson 	dev->pdata = dev_get_platdata(&pdev->dev);
79995a7f10eSVladimir Barinov 	platform_set_drvdata(pdev, dev);
80095a7f10eSVladimir Barinov 
8015c3d8a46SHeiko Schocher 	if (!dev->pdata && pdev->dev.of_node) {
8025c3d8a46SHeiko Schocher 		u32 prop;
8035c3d8a46SHeiko Schocher 
8045c3d8a46SHeiko Schocher 		dev->pdata = devm_kzalloc(&pdev->dev,
8055c3d8a46SHeiko Schocher 			sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
806c4df5000SWolfram Sang 		if (!dev->pdata)
807c4df5000SWolfram Sang 			return -ENOMEM;
808c4df5000SWolfram Sang 
8095c3d8a46SHeiko Schocher 		memcpy(dev->pdata, &davinci_i2c_platform_data_default,
8105c3d8a46SHeiko Schocher 			sizeof(struct davinci_i2c_platform_data));
8115c3d8a46SHeiko Schocher 		if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
8125c3d8a46SHeiko Schocher 			&prop))
8135c3d8a46SHeiko Schocher 			dev->pdata->bus_freq = prop / 1000;
8147ef97e9aSGrygorii Strashko 
8157ef97e9aSGrygorii Strashko 		dev->pdata->has_pfunc =
8167ef97e9aSGrygorii Strashko 			of_property_read_bool(pdev->dev.of_node,
8177ef97e9aSGrygorii Strashko 					      "ti,has-pfunc");
8185c3d8a46SHeiko Schocher 	} else if (!dev->pdata) {
8195c3d8a46SHeiko Schocher 		dev->pdata = &davinci_i2c_platform_data_default;
8205c3d8a46SHeiko Schocher 	}
8215c3d8a46SHeiko Schocher 
82285796843SVishwanathrao Badarkhe, Manish 	dev->clk = devm_clk_get(&pdev->dev, NULL);
823c4df5000SWolfram Sang 	if (IS_ERR(dev->clk))
824cc209ac8SFranklin S Cooper Jr 		return PTR_ERR(dev->clk);
82595a7f10eSVladimir Barinov 
8263cc2d009SWolfram Sang 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
82785796843SVishwanathrao Badarkhe, Manish 	dev->base = devm_ioremap_resource(&pdev->dev, mem);
82885796843SVishwanathrao Badarkhe, Manish 	if (IS_ERR(dev->base)) {
82917f88151SFranklin S Cooper Jr 		return PTR_ERR(dev->base);
83017f88151SFranklin S Cooper Jr 	}
83117f88151SFranklin S Cooper Jr 
83217f88151SFranklin S Cooper Jr 	pm_runtime_set_autosuspend_delay(dev->dev,
83317f88151SFranklin S Cooper Jr 					 DAVINCI_I2C_PM_TIMEOUT);
83417f88151SFranklin S Cooper Jr 	pm_runtime_use_autosuspend(dev->dev);
83517f88151SFranklin S Cooper Jr 
83617f88151SFranklin S Cooper Jr 	pm_runtime_enable(dev->dev);
83717f88151SFranklin S Cooper Jr 
83817f88151SFranklin S Cooper Jr 	r = pm_runtime_get_sync(dev->dev);
83917f88151SFranklin S Cooper Jr 	if (r < 0) {
84017f88151SFranklin S Cooper Jr 		dev_err(dev->dev, "failed to runtime_get device: %d\n", r);
84117f88151SFranklin S Cooper Jr 		pm_runtime_put_noidle(dev->dev);
84217f88151SFranklin S Cooper Jr 		return r;
843c062a251SChaithrika U S 	}
844c062a251SChaithrika U S 
84595a7f10eSVladimir Barinov 	i2c_davinci_init(dev);
84695a7f10eSVladimir Barinov 
84785796843SVishwanathrao Badarkhe, Manish 	r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
84885796843SVishwanathrao Badarkhe, Manish 			pdev->name, dev);
84995a7f10eSVladimir Barinov 	if (r) {
85095a7f10eSVladimir Barinov 		dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
85195a7f10eSVladimir Barinov 		goto err_unuse_clocks;
85295a7f10eSVladimir Barinov 	}
85395a7f10eSVladimir Barinov 
85482c0de11SChaithrika U S 	r = i2c_davinci_cpufreq_register(dev);
85582c0de11SChaithrika U S 	if (r) {
85682c0de11SChaithrika U S 		dev_err(&pdev->dev, "failed to register cpufreq\n");
85785796843SVishwanathrao Badarkhe, Manish 		goto err_unuse_clocks;
85882c0de11SChaithrika U S 	}
85982c0de11SChaithrika U S 
86095a7f10eSVladimir Barinov 	adap = &dev->adapter;
86195a7f10eSVladimir Barinov 	i2c_set_adapdata(adap, dev);
86295a7f10eSVladimir Barinov 	adap->owner = THIS_MODULE;
863adcb82a9SWolfram Sang 	adap->class = I2C_CLASS_DEPRECATED;
86495a7f10eSVladimir Barinov 	strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
86595a7f10eSVladimir Barinov 	adap->algo = &i2c_davinci_algo;
86695a7f10eSVladimir Barinov 	adap->dev.parent = &pdev->dev;
86798a679caSJean Delvare 	adap->timeout = DAVINCI_I2C_TIMEOUT;
8685c3d8a46SHeiko Schocher 	adap->dev.of_node = pdev->dev.of_node;
86995a7f10eSVladimir Barinov 
8707ef97e9aSGrygorii Strashko 	if (dev->pdata->has_pfunc)
8717ef97e9aSGrygorii Strashko 		adap->bus_recovery_info = &davinci_i2c_scl_recovery_info;
872e5353765SLinus Walleij 	else if (dev->pdata->gpio_recovery) {
873cd2428c3SPhil Reid 		rinfo =  &davinci_i2c_gpio_recovery_info;
874cd2428c3SPhil Reid 		adap->bus_recovery_info = rinfo;
875e5353765SLinus Walleij 		rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl",
876e5353765SLinus Walleij 						  GPIOD_OUT_HIGH_OPEN_DRAIN);
877e5353765SLinus Walleij 		if (IS_ERR(rinfo->scl_gpiod)) {
878e5353765SLinus Walleij 			r = PTR_ERR(rinfo->scl_gpiod);
879cd2428c3SPhil Reid 			goto err_unuse_clocks;
880e5353765SLinus Walleij 		}
881e5353765SLinus Walleij 		rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN);
882e5353765SLinus Walleij 		if (IS_ERR(rinfo->sda_gpiod)) {
883e5353765SLinus Walleij 			r = PTR_ERR(rinfo->sda_gpiod);
884cd2428c3SPhil Reid 			goto err_unuse_clocks;
885e5353765SLinus Walleij 		}
8862e65676fSGrygorii Strashko 	}
8872e65676fSGrygorii Strashko 
88895a7f10eSVladimir Barinov 	adap->nr = pdev->id;
88995a7f10eSVladimir Barinov 	r = i2c_add_numbered_adapter(adap);
890ea734404SWolfram Sang 	if (r)
89185796843SVishwanathrao Badarkhe, Manish 		goto err_unuse_clocks;
89295a7f10eSVladimir Barinov 
89317f88151SFranklin S Cooper Jr 	pm_runtime_mark_last_busy(dev->dev);
89417f88151SFranklin S Cooper Jr 	pm_runtime_put_autosuspend(dev->dev);
89517f88151SFranklin S Cooper Jr 
89695a7f10eSVladimir Barinov 	return 0;
89795a7f10eSVladimir Barinov 
89895a7f10eSVladimir Barinov err_unuse_clocks:
89917f88151SFranklin S Cooper Jr 	pm_runtime_dont_use_autosuspend(dev->dev);
90017f88151SFranklin S Cooper Jr 	pm_runtime_put_sync(dev->dev);
90117f88151SFranklin S Cooper Jr 	pm_runtime_disable(dev->dev);
90217f88151SFranklin S Cooper Jr 
90395a7f10eSVladimir Barinov 	return r;
90495a7f10eSVladimir Barinov }
90595a7f10eSVladimir Barinov 
90695a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev)
90795a7f10eSVladimir Barinov {
90895a7f10eSVladimir Barinov 	struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
90917f88151SFranklin S Cooper Jr 	int ret;
91095a7f10eSVladimir Barinov 
91182c0de11SChaithrika U S 	i2c_davinci_cpufreq_deregister(dev);
91282c0de11SChaithrika U S 
91395a7f10eSVladimir Barinov 	i2c_del_adapter(&dev->adapter);
91495a7f10eSVladimir Barinov 
91517f88151SFranklin S Cooper Jr 	ret = pm_runtime_get_sync(&pdev->dev);
91617f88151SFranklin S Cooper Jr 	if (ret < 0) {
91717f88151SFranklin S Cooper Jr 		pm_runtime_put_noidle(&pdev->dev);
91817f88151SFranklin S Cooper Jr 		return ret;
91917f88151SFranklin S Cooper Jr 	}
92095a7f10eSVladimir Barinov 
92195a7f10eSVladimir Barinov 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
92295a7f10eSVladimir Barinov 
92317f88151SFranklin S Cooper Jr 	pm_runtime_dont_use_autosuspend(dev->dev);
92417f88151SFranklin S Cooper Jr 	pm_runtime_put_sync(dev->dev);
92517f88151SFranklin S Cooper Jr 	pm_runtime_disable(dev->dev);
92617f88151SFranklin S Cooper Jr 
92795a7f10eSVladimir Barinov 	return 0;
92895a7f10eSVladimir Barinov }
92995a7f10eSVladimir Barinov 
93068f15de9SChaithrika U S #ifdef CONFIG_PM
93168f15de9SChaithrika U S static int davinci_i2c_suspend(struct device *dev)
93268f15de9SChaithrika U S {
9339242e72aSMasahiro Yamada 	struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev);
93468f15de9SChaithrika U S 
93568f15de9SChaithrika U S 	/* put I2C into reset */
93668f15de9SChaithrika U S 	davinci_i2c_reset_ctrl(i2c_dev, 0);
93768f15de9SChaithrika U S 
93868f15de9SChaithrika U S 	return 0;
93968f15de9SChaithrika U S }
94068f15de9SChaithrika U S 
94168f15de9SChaithrika U S static int davinci_i2c_resume(struct device *dev)
94268f15de9SChaithrika U S {
9439242e72aSMasahiro Yamada 	struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev);
94468f15de9SChaithrika U S 
94568f15de9SChaithrika U S 	/* take I2C out of reset */
94668f15de9SChaithrika U S 	davinci_i2c_reset_ctrl(i2c_dev, 1);
94768f15de9SChaithrika U S 
94868f15de9SChaithrika U S 	return 0;
94968f15de9SChaithrika U S }
95068f15de9SChaithrika U S 
95168f15de9SChaithrika U S static const struct dev_pm_ops davinci_i2c_pm = {
95268f15de9SChaithrika U S 	.suspend        = davinci_i2c_suspend,
95368f15de9SChaithrika U S 	.resume         = davinci_i2c_resume,
95417f88151SFranklin S Cooper Jr 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
95517f88151SFranklin S Cooper Jr 				      pm_runtime_force_resume)
95668f15de9SChaithrika U S };
95768f15de9SChaithrika U S 
95868f15de9SChaithrika U S #define davinci_i2c_pm_ops (&davinci_i2c_pm)
95968f15de9SChaithrika U S #else
96068f15de9SChaithrika U S #define davinci_i2c_pm_ops NULL
96168f15de9SChaithrika U S #endif
96268f15de9SChaithrika U S 
963add8eda7SKay Sievers /* work with hotplug and coldplug */
964add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci");
965add8eda7SKay Sievers 
96695a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = {
96795a7f10eSVladimir Barinov 	.probe		= davinci_i2c_probe,
96895a7f10eSVladimir Barinov 	.remove		= davinci_i2c_remove,
96995a7f10eSVladimir Barinov 	.driver		= {
97095a7f10eSVladimir Barinov 		.name	= "i2c_davinci",
97168f15de9SChaithrika U S 		.pm	= davinci_i2c_pm_ops,
9724e905323SSachin Kamat 		.of_match_table = davinci_i2c_of_match,
97395a7f10eSVladimir Barinov 	},
97495a7f10eSVladimir Barinov };
97595a7f10eSVladimir Barinov 
97695a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */
97795a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void)
97895a7f10eSVladimir Barinov {
97995a7f10eSVladimir Barinov 	return platform_driver_register(&davinci_i2c_driver);
98095a7f10eSVladimir Barinov }
98195a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver);
98295a7f10eSVladimir Barinov 
98395a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void)
98495a7f10eSVladimir Barinov {
98595a7f10eSVladimir Barinov 	platform_driver_unregister(&davinci_i2c_driver);
98695a7f10eSVladimir Barinov }
98795a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver);
98895a7f10eSVladimir Barinov 
98995a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India");
99095a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
99195a7f10eSVladimir Barinov MODULE_LICENSE("GPL");
992