1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
295a7f10eSVladimir Barinov /*
395a7f10eSVladimir Barinov * TI DAVINCI I2C adapter driver.
495a7f10eSVladimir Barinov *
595a7f10eSVladimir Barinov * Copyright (C) 2006 Texas Instruments.
695a7f10eSVladimir Barinov * Copyright (C) 2007 MontaVista Software Inc.
795a7f10eSVladimir Barinov *
895a7f10eSVladimir Barinov * Updated by Vinod & Sudhakar Feb 2005
995a7f10eSVladimir Barinov *
1095a7f10eSVladimir Barinov * ----------------------------------------------------------------------------
1195a7f10eSVladimir Barinov *
1295a7f10eSVladimir Barinov * ----------------------------------------------------------------------------
1395a7f10eSVladimir Barinov */
1495a7f10eSVladimir Barinov #include <linux/kernel.h>
1595a7f10eSVladimir Barinov #include <linux/module.h>
1695a7f10eSVladimir Barinov #include <linux/delay.h>
1795a7f10eSVladimir Barinov #include <linux/i2c.h>
1895a7f10eSVladimir Barinov #include <linux/clk.h>
1995a7f10eSVladimir Barinov #include <linux/errno.h>
2095a7f10eSVladimir Barinov #include <linux/sched.h>
2195a7f10eSVladimir Barinov #include <linux/err.h>
2295a7f10eSVladimir Barinov #include <linux/interrupt.h>
2395a7f10eSVladimir Barinov #include <linux/platform_device.h>
2495a7f10eSVladimir Barinov #include <linux/io.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
2682c0de11SChaithrika U S #include <linux/cpufreq.h>
27e5353765SLinus Walleij #include <linux/gpio/consumer.h>
28*59738ab2SRob Herring #include <linux/of.h>
29ec2a0833SArnd Bergmann #include <linux/platform_data/i2c-davinci.h>
3017f88151SFranklin S Cooper Jr #include <linux/pm_runtime.h>
3195a7f10eSVladimir Barinov
3295a7f10eSVladimir Barinov /* ----- global defines ----------------------------------------------- */
3395a7f10eSVladimir Barinov
3495a7f10eSVladimir Barinov #define DAVINCI_I2C_TIMEOUT (1*HZ)
358574faf9SPhilby John #define DAVINCI_I2C_MAX_TRIES 2
3641c8d452SAlexander Sverdlin #define DAVINCI_I2C_OWN_ADDRESS 0x08
3741c8d452SAlexander Sverdlin #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_SCD | \
3895a7f10eSVladimir Barinov DAVINCI_I2C_IMR_ARDY | \
3995a7f10eSVladimir Barinov DAVINCI_I2C_IMR_NACK | \
4095a7f10eSVladimir Barinov DAVINCI_I2C_IMR_AL)
4195a7f10eSVladimir Barinov
4295a7f10eSVladimir Barinov #define DAVINCI_I2C_OAR_REG 0x00
4395a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_REG 0x04
4495a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_REG 0x08
4595a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKL_REG 0x0c
4695a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKH_REG 0x10
4795a7f10eSVladimir Barinov #define DAVINCI_I2C_CNT_REG 0x14
4895a7f10eSVladimir Barinov #define DAVINCI_I2C_DRR_REG 0x18
4995a7f10eSVladimir Barinov #define DAVINCI_I2C_SAR_REG 0x1c
5095a7f10eSVladimir Barinov #define DAVINCI_I2C_DXR_REG 0x20
5195a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_REG 0x24
5295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_REG 0x28
5395a7f10eSVladimir Barinov #define DAVINCI_I2C_EMDR_REG 0x2c
5495a7f10eSVladimir Barinov #define DAVINCI_I2C_PSC_REG 0x30
557ef97e9aSGrygorii Strashko #define DAVINCI_I2C_FUNC_REG 0x48
567ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_REG 0x4c
577ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_REG 0x50
587ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DOUT_REG 0x54
597ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_REG 0x58
607ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_REG 0x5c
6195a7f10eSVladimir Barinov
6295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AAS 0x07
6395a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_SCD 0x06
6495a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_XRDY 0x05
6595a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_RDR 0x04
6695a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_ARDY 0x03
6795a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_NACK 0x02
6895a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AL 0x01
6995a7f10eSVladimir Barinov
70c062a251SChaithrika U S #define DAVINCI_I2C_STR_BB BIT(12)
71c062a251SChaithrika U S #define DAVINCI_I2C_STR_RSFULL BIT(11)
72c062a251SChaithrika U S #define DAVINCI_I2C_STR_SCD BIT(5)
73c062a251SChaithrika U S #define DAVINCI_I2C_STR_ARDY BIT(2)
74c062a251SChaithrika U S #define DAVINCI_I2C_STR_NACK BIT(1)
75c062a251SChaithrika U S #define DAVINCI_I2C_STR_AL BIT(0)
7695a7f10eSVladimir Barinov
77c062a251SChaithrika U S #define DAVINCI_I2C_MDR_NACK BIT(15)
78c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STT BIT(13)
79c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STP BIT(11)
80c062a251SChaithrika U S #define DAVINCI_I2C_MDR_MST BIT(10)
81c062a251SChaithrika U S #define DAVINCI_I2C_MDR_TRX BIT(9)
82c062a251SChaithrika U S #define DAVINCI_I2C_MDR_XA BIT(8)
83c062a251SChaithrika U S #define DAVINCI_I2C_MDR_RM BIT(7)
84c062a251SChaithrika U S #define DAVINCI_I2C_MDR_IRS BIT(5)
8595a7f10eSVladimir Barinov
86c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AAS BIT(6)
87c062a251SChaithrika U S #define DAVINCI_I2C_IMR_SCD BIT(5)
88c062a251SChaithrika U S #define DAVINCI_I2C_IMR_XRDY BIT(4)
89c062a251SChaithrika U S #define DAVINCI_I2C_IMR_RRDY BIT(3)
90c062a251SChaithrika U S #define DAVINCI_I2C_IMR_ARDY BIT(2)
91c062a251SChaithrika U S #define DAVINCI_I2C_IMR_NACK BIT(1)
92c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AL BIT(0)
9395a7f10eSVladimir Barinov
947ef97e9aSGrygorii Strashko /* set SDA and SCL as GPIO */
957ef97e9aSGrygorii Strashko #define DAVINCI_I2C_FUNC_PFUNC0 BIT(0)
967ef97e9aSGrygorii Strashko
977ef97e9aSGrygorii Strashko /* set SCL as output when used as GPIO*/
987ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_PDIR0 BIT(0)
997ef97e9aSGrygorii Strashko /* set SDA as output when used as GPIO*/
1007ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIR_PDIR1 BIT(1)
1017ef97e9aSGrygorii Strashko
1027ef97e9aSGrygorii Strashko /* read SCL GPIO level */
1037ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_PDIN0 BIT(0)
1047ef97e9aSGrygorii Strashko /* read SDA GPIO level */
1057ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DIN_PDIN1 BIT(1)
1067ef97e9aSGrygorii Strashko
1077ef97e9aSGrygorii Strashko /*set the SCL GPIO high */
1087ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_PDSET0 BIT(0)
1097ef97e9aSGrygorii Strashko /*set the SDA GPIO high */
1107ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DSET_PDSET1 BIT(1)
1117ef97e9aSGrygorii Strashko
1127ef97e9aSGrygorii Strashko /* set the SCL GPIO low */
1137ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_PDCLR0 BIT(0)
1147ef97e9aSGrygorii Strashko /* set the SDA GPIO low */
1157ef97e9aSGrygorii Strashko #define DAVINCI_I2C_DCLR_PDCLR1 BIT(1)
1167ef97e9aSGrygorii Strashko
11717f88151SFranklin S Cooper Jr /* timeout for pm runtime autosuspend */
11817f88151SFranklin S Cooper Jr #define DAVINCI_I2C_PM_TIMEOUT 1000 /* ms */
11917f88151SFranklin S Cooper Jr
12095a7f10eSVladimir Barinov struct davinci_i2c_dev {
12195a7f10eSVladimir Barinov struct device *dev;
12295a7f10eSVladimir Barinov void __iomem *base;
12395a7f10eSVladimir Barinov struct completion cmd_complete;
12495a7f10eSVladimir Barinov struct clk *clk;
12595a7f10eSVladimir Barinov int cmd_err;
12695a7f10eSVladimir Barinov u8 *buf;
12795a7f10eSVladimir Barinov size_t buf_len;
12895a7f10eSVladimir Barinov int irq;
129c6c7c729SDirk Behme int stop;
1305a0d5f5fSTroy Kisky u8 terminate;
13195a7f10eSVladimir Barinov struct i2c_adapter adapter;
13282c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ
13382c0de11SChaithrika U S struct notifier_block freq_transition;
13482c0de11SChaithrika U S #endif
1355c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata;
13695a7f10eSVladimir Barinov };
13795a7f10eSVladimir Barinov
13895a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */
13995a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
14095a7f10eSVladimir Barinov .bus_freq = 100,
14195a7f10eSVladimir Barinov .bus_delay = 0,
14295a7f10eSVladimir Barinov };
14395a7f10eSVladimir Barinov
davinci_i2c_write_reg(struct davinci_i2c_dev * i2c_dev,int reg,u16 val)14495a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
14595a7f10eSVladimir Barinov int reg, u16 val)
14695a7f10eSVladimir Barinov {
147a238dcfaSTaras Kondratiuk writew_relaxed(val, i2c_dev->base + reg);
14895a7f10eSVladimir Barinov }
14995a7f10eSVladimir Barinov
davinci_i2c_read_reg(struct davinci_i2c_dev * i2c_dev,int reg)15095a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
15195a7f10eSVladimir Barinov {
152a238dcfaSTaras Kondratiuk return readw_relaxed(i2c_dev->base + reg);
15395a7f10eSVladimir Barinov }
15495a7f10eSVladimir Barinov
davinci_i2c_reset_ctrl(struct davinci_i2c_dev * i2c_dev,int val)1555ae5b113SChaithrika U S static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
1565ae5b113SChaithrika U S int val)
1575ae5b113SChaithrika U S {
1585ae5b113SChaithrika U S u16 w;
1595ae5b113SChaithrika U S
1605ae5b113SChaithrika U S w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
1615ae5b113SChaithrika U S if (!val) /* put I2C into reset */
1625ae5b113SChaithrika U S w &= ~DAVINCI_I2C_MDR_IRS;
1635ae5b113SChaithrika U S else /* take I2C out of reset */
1645ae5b113SChaithrika U S w |= DAVINCI_I2C_MDR_IRS;
1655ae5b113SChaithrika U S
1665ae5b113SChaithrika U S davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
1675ae5b113SChaithrika U S }
1685ae5b113SChaithrika U S
i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev * dev)1695ae5b113SChaithrika U S static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
17095a7f10eSVladimir Barinov {
1715c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata;
17295a7f10eSVladimir Barinov u16 psc;
17395a7f10eSVladimir Barinov u32 clk;
174cc99ff70STroy Kisky u32 d;
17595a7f10eSVladimir Barinov u32 clkh;
17695a7f10eSVladimir Barinov u32 clkl;
17795a7f10eSVladimir Barinov u32 input_clock = clk_get_rate(dev->clk);
17835780e86SAlexander Sverdlin struct device_node *of_node = dev->dev->of_node;
17995a7f10eSVladimir Barinov
18095a7f10eSVladimir Barinov /* NOTE: I2C Clock divider programming info
18195a7f10eSVladimir Barinov * As per I2C specs the following formulas provide prescaler
18295a7f10eSVladimir Barinov * and low/high divider values
18395a7f10eSVladimir Barinov * input clk --> PSC Div -----------> ICCL/H Div --> output clock
18495a7f10eSVladimir Barinov * module clk
18595a7f10eSVladimir Barinov *
18695a7f10eSVladimir Barinov * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
18795a7f10eSVladimir Barinov *
18895a7f10eSVladimir Barinov * Thus,
18995a7f10eSVladimir Barinov * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
19095a7f10eSVladimir Barinov *
19195a7f10eSVladimir Barinov * where if PSC == 0, d = 7,
19295a7f10eSVladimir Barinov * if PSC == 1, d = 6
19395a7f10eSVladimir Barinov * if PSC > 1 , d = 5
19435780e86SAlexander Sverdlin *
19535780e86SAlexander Sverdlin * Note:
19635780e86SAlexander Sverdlin * d is always 6 on Keystone I2C controller
19795a7f10eSVladimir Barinov */
19895a7f10eSVladimir Barinov
19987cb5b42SAlexander Sverdlin /*
20087cb5b42SAlexander Sverdlin * Both Davinci and current Keystone User Guides recommend a value
20187cb5b42SAlexander Sverdlin * between 7MHz and 12MHz. In reality 7MHz module clock doesn't
20287cb5b42SAlexander Sverdlin * always produce enough margin between SDA and SCL transitions.
20387cb5b42SAlexander Sverdlin * Measurements show that the higher the module clock is, the
20487cb5b42SAlexander Sverdlin * bigger is the margin, providing more reliable communication.
20587cb5b42SAlexander Sverdlin * So we better target for 12MHz.
20687cb5b42SAlexander Sverdlin */
20787cb5b42SAlexander Sverdlin psc = (input_clock / 12000000) - 1;
208cc99ff70STroy Kisky if ((input_clock / (psc + 1)) > 12000000)
209cc99ff70STroy Kisky psc++; /* better to run under spec than over */
210cc99ff70STroy Kisky d = (psc >= 2) ? 5 : 7 - psc;
21195a7f10eSVladimir Barinov
21235780e86SAlexander Sverdlin if (of_node && of_device_is_compatible(of_node, "ti,keystone-i2c"))
21335780e86SAlexander Sverdlin d = 6;
21435780e86SAlexander Sverdlin
215955fc950SAlexander Sverdlin clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000));
216955fc950SAlexander Sverdlin /* Avoid driving the bus too fast because of rounding errors above */
217955fc950SAlexander Sverdlin if (input_clock / (psc + 1) / clk > pdata->bus_freq * 1000)
218955fc950SAlexander Sverdlin clk++;
219955fc950SAlexander Sverdlin /*
220955fc950SAlexander Sverdlin * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at
221955fc950SAlexander Sverdlin * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH
222955fc950SAlexander Sverdlin * to LOW ratio as 1 to 2 is more safe.
223955fc950SAlexander Sverdlin */
224955fc950SAlexander Sverdlin if (pdata->bus_freq > 100)
225955fc950SAlexander Sverdlin clkl = (clk << 1) / 3;
226955fc950SAlexander Sverdlin else
227955fc950SAlexander Sverdlin clkl = (clk >> 1);
228955fc950SAlexander Sverdlin /*
229955fc950SAlexander Sverdlin * It's not always possible to have 1 to 2 ratio when d=7, so fall back
230955fc950SAlexander Sverdlin * to minimal possible clkh in this case.
231cc8de9a6SAlexander Sverdlin *
232cc8de9a6SAlexander Sverdlin * Note:
233cc8de9a6SAlexander Sverdlin * CLKH is not allowed to be 0, in this case I2C clock is not generated
234cc8de9a6SAlexander Sverdlin * at all
235955fc950SAlexander Sverdlin */
236cc8de9a6SAlexander Sverdlin if (clk > clkl + d) {
237955fc950SAlexander Sverdlin clkh = clk - clkl - d;
238955fc950SAlexander Sverdlin clkl -= d;
239955fc950SAlexander Sverdlin } else {
240cc8de9a6SAlexander Sverdlin clkh = 1;
241955fc950SAlexander Sverdlin clkl = clk - (d << 1);
242955fc950SAlexander Sverdlin }
24395a7f10eSVladimir Barinov
24495a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
24595a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
24695a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
24795a7f10eSVladimir Barinov
2485ae5b113SChaithrika U S dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
2495ae5b113SChaithrika U S }
2505ae5b113SChaithrika U S
2515ae5b113SChaithrika U S /*
2525ae5b113SChaithrika U S * This function configures I2C and brings I2C out of reset.
2535ae5b113SChaithrika U S * This function is called during I2C init function. This function
2545ae5b113SChaithrika U S * also gets called if I2C encounters any errors.
2555ae5b113SChaithrika U S */
i2c_davinci_init(struct davinci_i2c_dev * dev)2565ae5b113SChaithrika U S static int i2c_davinci_init(struct davinci_i2c_dev *dev)
2575ae5b113SChaithrika U S {
2585c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata;
2595ae5b113SChaithrika U S
2605ae5b113SChaithrika U S /* put I2C into reset */
2615ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 0);
2625ae5b113SChaithrika U S
2635ae5b113SChaithrika U S /* compute clock dividers */
2645ae5b113SChaithrika U S i2c_davinci_calc_clk_dividers(dev);
2655ae5b113SChaithrika U S
2667605fa3bSDavid Brownell /* Respond at reserved "SMBus Host" slave address" (and zero);
2677605fa3bSDavid Brownell * we seem to have no option to not respond...
2687605fa3bSDavid Brownell */
26941c8d452SAlexander Sverdlin davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, DAVINCI_I2C_OWN_ADDRESS);
2707605fa3bSDavid Brownell
27195a7f10eSVladimir Barinov dev_dbg(dev->dev, "PSC = %d\n",
27295a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
27395a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKL = %d\n",
27495a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
27595a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKH = %d\n",
27695a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
277cc99ff70STroy Kisky dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
278cc99ff70STroy Kisky pdata->bus_freq, pdata->bus_delay);
27995a7f10eSVladimir Barinov
2805c3d8a46SHeiko Schocher
28195a7f10eSVladimir Barinov /* Take the I2C module out of reset: */
2825ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 1);
28395a7f10eSVladimir Barinov
28495a7f10eSVladimir Barinov /* Enable interrupts */
28595a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
28695a7f10eSVladimir Barinov
28795a7f10eSVladimir Barinov return 0;
28895a7f10eSVladimir Barinov }
28995a7f10eSVladimir Barinov
29095a7f10eSVladimir Barinov /*
291cd2428c3SPhil Reid * This routine does i2c bus recovery by using i2c_generic_scl_recovery
2922e65676fSGrygorii Strashko * which is provided by I2C Bus recovery infrastructure.
2932e65676fSGrygorii Strashko */
davinci_i2c_prepare_recovery(struct i2c_adapter * adap)2942e65676fSGrygorii Strashko static void davinci_i2c_prepare_recovery(struct i2c_adapter *adap)
2952e65676fSGrygorii Strashko {
2962e65676fSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
2972e65676fSGrygorii Strashko
2982e65676fSGrygorii Strashko /* Disable interrupts */
2992e65676fSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, 0);
3002e65676fSGrygorii Strashko
3012e65676fSGrygorii Strashko /* put I2C into reset */
3022e65676fSGrygorii Strashko davinci_i2c_reset_ctrl(dev, 0);
3032e65676fSGrygorii Strashko }
3042e65676fSGrygorii Strashko
davinci_i2c_unprepare_recovery(struct i2c_adapter * adap)3052e65676fSGrygorii Strashko static void davinci_i2c_unprepare_recovery(struct i2c_adapter *adap)
3062e65676fSGrygorii Strashko {
3072e65676fSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3082e65676fSGrygorii Strashko
3092e65676fSGrygorii Strashko i2c_davinci_init(dev);
3102e65676fSGrygorii Strashko }
3112e65676fSGrygorii Strashko
3122e65676fSGrygorii Strashko static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info = {
313cd2428c3SPhil Reid .recover_bus = i2c_generic_scl_recovery,
3142e65676fSGrygorii Strashko .prepare_recovery = davinci_i2c_prepare_recovery,
3152e65676fSGrygorii Strashko .unprepare_recovery = davinci_i2c_unprepare_recovery,
3162e65676fSGrygorii Strashko };
3172e65676fSGrygorii Strashko
davinci_i2c_set_scl(struct i2c_adapter * adap,int val)3187ef97e9aSGrygorii Strashko static void davinci_i2c_set_scl(struct i2c_adapter *adap, int val)
3197ef97e9aSGrygorii Strashko {
3207ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3217ef97e9aSGrygorii Strashko
3227ef97e9aSGrygorii Strashko if (val)
3237ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_DSET_REG,
3247ef97e9aSGrygorii Strashko DAVINCI_I2C_DSET_PDSET0);
3257ef97e9aSGrygorii Strashko else
3267ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_DCLR_REG,
3277ef97e9aSGrygorii Strashko DAVINCI_I2C_DCLR_PDCLR0);
3287ef97e9aSGrygorii Strashko }
3297ef97e9aSGrygorii Strashko
davinci_i2c_get_scl(struct i2c_adapter * adap)3307ef97e9aSGrygorii Strashko static int davinci_i2c_get_scl(struct i2c_adapter *adap)
3317ef97e9aSGrygorii Strashko {
3327ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3337ef97e9aSGrygorii Strashko int val;
3347ef97e9aSGrygorii Strashko
3357ef97e9aSGrygorii Strashko /* read the state of SCL */
3367ef97e9aSGrygorii Strashko val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
3377ef97e9aSGrygorii Strashko return val & DAVINCI_I2C_DIN_PDIN0;
3387ef97e9aSGrygorii Strashko }
3397ef97e9aSGrygorii Strashko
davinci_i2c_get_sda(struct i2c_adapter * adap)3407ef97e9aSGrygorii Strashko static int davinci_i2c_get_sda(struct i2c_adapter *adap)
3417ef97e9aSGrygorii Strashko {
3427ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3437ef97e9aSGrygorii Strashko int val;
3447ef97e9aSGrygorii Strashko
3457ef97e9aSGrygorii Strashko /* read the state of SDA */
3467ef97e9aSGrygorii Strashko val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG);
3477ef97e9aSGrygorii Strashko return val & DAVINCI_I2C_DIN_PDIN1;
3487ef97e9aSGrygorii Strashko }
3497ef97e9aSGrygorii Strashko
davinci_i2c_scl_prepare_recovery(struct i2c_adapter * adap)3507ef97e9aSGrygorii Strashko static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter *adap)
3517ef97e9aSGrygorii Strashko {
3527ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3537ef97e9aSGrygorii Strashko
3547ef97e9aSGrygorii Strashko davinci_i2c_prepare_recovery(adap);
3557ef97e9aSGrygorii Strashko
3567ef97e9aSGrygorii Strashko /* SCL output, SDA input */
3577ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_DIR_REG, DAVINCI_I2C_DIR_PDIR0);
3587ef97e9aSGrygorii Strashko
3597ef97e9aSGrygorii Strashko /* change to GPIO mode */
3607ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG,
3617ef97e9aSGrygorii Strashko DAVINCI_I2C_FUNC_PFUNC0);
3627ef97e9aSGrygorii Strashko }
3637ef97e9aSGrygorii Strashko
davinci_i2c_scl_unprepare_recovery(struct i2c_adapter * adap)3647ef97e9aSGrygorii Strashko static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter *adap)
3657ef97e9aSGrygorii Strashko {
3667ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
3677ef97e9aSGrygorii Strashko
3687ef97e9aSGrygorii Strashko /* change back to I2C mode */
3697ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG, 0);
3707ef97e9aSGrygorii Strashko
3717ef97e9aSGrygorii Strashko davinci_i2c_unprepare_recovery(adap);
3727ef97e9aSGrygorii Strashko }
3737ef97e9aSGrygorii Strashko
3747ef97e9aSGrygorii Strashko static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info = {
3757ef97e9aSGrygorii Strashko .recover_bus = i2c_generic_scl_recovery,
3767ef97e9aSGrygorii Strashko .set_scl = davinci_i2c_set_scl,
3777ef97e9aSGrygorii Strashko .get_scl = davinci_i2c_get_scl,
3787ef97e9aSGrygorii Strashko .get_sda = davinci_i2c_get_sda,
3797ef97e9aSGrygorii Strashko .prepare_recovery = davinci_i2c_scl_prepare_recovery,
3807ef97e9aSGrygorii Strashko .unprepare_recovery = davinci_i2c_scl_unprepare_recovery,
3817ef97e9aSGrygorii Strashko };
3827ef97e9aSGrygorii Strashko
3832e65676fSGrygorii Strashko /*
38495a7f10eSVladimir Barinov * Waiting for bus not busy
38595a7f10eSVladimir Barinov */
i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev * dev)38672a956c5SAlexander Sverdlin static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev)
38795a7f10eSVladimir Barinov {
38872a956c5SAlexander Sverdlin unsigned long timeout = jiffies + dev->adapter.timeout;
38995a7f10eSVladimir Barinov
39072a956c5SAlexander Sverdlin do {
39172a956c5SAlexander Sverdlin if (!(davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB))
39272a956c5SAlexander Sverdlin return 0;
39372a956c5SAlexander Sverdlin schedule_timeout_uninterruptible(1);
39472a956c5SAlexander Sverdlin } while (time_before_eq(jiffies, timeout));
39572a956c5SAlexander Sverdlin
39672a956c5SAlexander Sverdlin dev_warn(dev->dev, "timeout waiting for bus ready\n");
3972e65676fSGrygorii Strashko i2c_recover_bus(&dev->adapter);
39872a956c5SAlexander Sverdlin
39972a956c5SAlexander Sverdlin /*
40072a956c5SAlexander Sverdlin * if bus is still "busy" here, it's most probably a HW problem like
40172a956c5SAlexander Sverdlin * short-circuit
40272a956c5SAlexander Sverdlin */
40372a956c5SAlexander Sverdlin if (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB)
40472a956c5SAlexander Sverdlin return -EIO;
40595a7f10eSVladimir Barinov
40695a7f10eSVladimir Barinov return 0;
40795a7f10eSVladimir Barinov }
40895a7f10eSVladimir Barinov
40995a7f10eSVladimir Barinov /*
41095a7f10eSVladimir Barinov * Low level master read/write transaction. This function is called
41195a7f10eSVladimir Barinov * from i2c_davinci_xfer.
41295a7f10eSVladimir Barinov */
41395a7f10eSVladimir Barinov static int
i2c_davinci_xfer_msg(struct i2c_adapter * adap,struct i2c_msg * msg,int stop)41495a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
41595a7f10eSVladimir Barinov {
41695a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
4175c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata;
41895a7f10eSVladimir Barinov u32 flag;
41995a7f10eSVladimir Barinov u16 w;
420d9e1f441SNicholas Mc Guire unsigned long time_left;
42195a7f10eSVladimir Barinov
42241c8d452SAlexander Sverdlin if (msg->addr == DAVINCI_I2C_OWN_ADDRESS) {
42341c8d452SAlexander Sverdlin dev_warn(dev->dev, "transfer to own address aborted\n");
42441c8d452SAlexander Sverdlin return -EADDRNOTAVAIL;
42541c8d452SAlexander Sverdlin }
42641c8d452SAlexander Sverdlin
42795a7f10eSVladimir Barinov /* Introduce a delay, required for some boards (e.g Davinci EVM) */
42895a7f10eSVladimir Barinov if (pdata->bus_delay)
42995a7f10eSVladimir Barinov udelay(pdata->bus_delay);
43095a7f10eSVladimir Barinov
43195a7f10eSVladimir Barinov /* set the slave address */
43295a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
43395a7f10eSVladimir Barinov
43495a7f10eSVladimir Barinov dev->buf = msg->buf;
43595a7f10eSVladimir Barinov dev->buf_len = msg->len;
436c6c7c729SDirk Behme dev->stop = stop;
43795a7f10eSVladimir Barinov
43895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
43995a7f10eSVladimir Barinov
44016735d02SWolfram Sang reinit_completion(&dev->cmd_complete);
44195a7f10eSVladimir Barinov dev->cmd_err = 0;
44295a7f10eSVladimir Barinov
443c5b4afecSJon Povey /* Take I2C out of reset and configure it as master */
444c5b4afecSJon Povey flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
44595a7f10eSVladimir Barinov
44695a7f10eSVladimir Barinov /* if the slave address is ten bit address, enable XA bit */
44795a7f10eSVladimir Barinov if (msg->flags & I2C_M_TEN)
44895a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_XA;
44995a7f10eSVladimir Barinov if (!(msg->flags & I2C_M_RD))
45095a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_TRX;
451c5b4afecSJon Povey if (msg->len == 0)
452c6c7c729SDirk Behme flag |= DAVINCI_I2C_MDR_RM;
45395a7f10eSVladimir Barinov
45495a7f10eSVladimir Barinov /* Enable receive or transmit interrupts */
45595a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
45695a7f10eSVladimir Barinov if (msg->flags & I2C_M_RD)
457c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_RRDY;
45895a7f10eSVladimir Barinov else
459c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_XRDY;
46095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
46195a7f10eSVladimir Barinov
4625a0d5f5fSTroy Kisky dev->terminate = 0;
463c6c7c729SDirk Behme
464c6c7c729SDirk Behme /*
465c5b4afecSJon Povey * Write mode register first as needed for correct behaviour
466c5b4afecSJon Povey * on OMAP-L138, but don't set STT yet to avoid a race with XRDY
46725985edcSLucas De Marchi * occurring before we have loaded DXR
468c5b4afecSJon Povey */
469c5b4afecSJon Povey davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
470c5b4afecSJon Povey
471c5b4afecSJon Povey /*
472c6c7c729SDirk Behme * First byte should be set here, not after interrupt,
473c6c7c729SDirk Behme * because transmit-data-ready interrupt can come before
474c6c7c729SDirk Behme * NACK-interrupt during sending of previous message and
475c6c7c729SDirk Behme * ICDXR may have wrong data
476c5b4afecSJon Povey * It also saves us one interrupt, slightly faster
477c6c7c729SDirk Behme */
478c6c7c729SDirk Behme if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
479c6c7c729SDirk Behme davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
480c6c7c729SDirk Behme dev->buf_len--;
481c6c7c729SDirk Behme }
482c6c7c729SDirk Behme
483c5b4afecSJon Povey /* Set STT to begin transmit now DXR is loaded */
484c5b4afecSJon Povey flag |= DAVINCI_I2C_MDR_STT;
485c5b4afecSJon Povey if (stop && msg->len != 0)
486c5b4afecSJon Povey flag |= DAVINCI_I2C_MDR_STP;
4874bba0fd8SJon Povey davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
4884bba0fd8SJon Povey
489d9e1f441SNicholas Mc Guire time_left = wait_for_completion_timeout(&dev->cmd_complete,
490d9e1f441SNicholas Mc Guire dev->adapter.timeout);
491d9e1f441SNicholas Mc Guire if (!time_left) {
49295a7f10eSVladimir Barinov dev_err(dev->dev, "controller timed out\n");
4932e65676fSGrygorii Strashko i2c_recover_bus(adap);
4945a0d5f5fSTroy Kisky dev->buf_len = 0;
49595a7f10eSVladimir Barinov return -ETIMEDOUT;
49695a7f10eSVladimir Barinov }
4975a0d5f5fSTroy Kisky if (dev->buf_len) {
4985a0d5f5fSTroy Kisky /* This should be 0 if all bytes were transferred
4995a0d5f5fSTroy Kisky * or dev->cmd_err denotes an error.
5005a0d5f5fSTroy Kisky */
501631de7a4SWolfram Sang dev_err(dev->dev, "abnormal termination buf_len=%zu\n",
5025a0d5f5fSTroy Kisky dev->buf_len);
5035a0d5f5fSTroy Kisky dev->terminate = 1;
5045a0d5f5fSTroy Kisky wmb();
5055a0d5f5fSTroy Kisky dev->buf_len = 0;
506d9e1f441SNicholas Mc Guire return -EREMOTEIO;
5075a0d5f5fSTroy Kisky }
50895a7f10eSVladimir Barinov
50995a7f10eSVladimir Barinov /* no error */
51095a7f10eSVladimir Barinov if (likely(!dev->cmd_err))
51195a7f10eSVladimir Barinov return msg->len;
51295a7f10eSVladimir Barinov
51395a7f10eSVladimir Barinov /* We have an error */
51495a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
51595a7f10eSVladimir Barinov i2c_davinci_init(dev);
51695a7f10eSVladimir Barinov return -EIO;
51795a7f10eSVladimir Barinov }
51895a7f10eSVladimir Barinov
51995a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
52095a7f10eSVladimir Barinov if (msg->flags & I2C_M_IGNORE_NAK)
52195a7f10eSVladimir Barinov return msg->len;
52295a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
523c062a251SChaithrika U S w |= DAVINCI_I2C_MDR_STP;
52495a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
52595a7f10eSVladimir Barinov return -EREMOTEIO;
52695a7f10eSVladimir Barinov }
52795a7f10eSVladimir Barinov return -EIO;
52895a7f10eSVladimir Barinov }
52995a7f10eSVladimir Barinov
53095a7f10eSVladimir Barinov /*
53195a7f10eSVladimir Barinov * Prepare controller for a transaction and call i2c_davinci_xfer_msg
53295a7f10eSVladimir Barinov */
53395a7f10eSVladimir Barinov static int
i2c_davinci_xfer(struct i2c_adapter * adap,struct i2c_msg msgs[],int num)53495a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
53595a7f10eSVladimir Barinov {
53695a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
53795a7f10eSVladimir Barinov int i;
53895a7f10eSVladimir Barinov int ret;
53995a7f10eSVladimir Barinov
54008882d20SHarvey Harrison dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
54195a7f10eSVladimir Barinov
542a181b8d1SMinghao Chi ret = pm_runtime_resume_and_get(dev->dev);
54317f88151SFranklin S Cooper Jr if (ret < 0) {
54417f88151SFranklin S Cooper Jr dev_err(dev->dev, "Failed to runtime_get device: %d\n", ret);
54517f88151SFranklin S Cooper Jr return ret;
54617f88151SFranklin S Cooper Jr }
54717f88151SFranklin S Cooper Jr
54872a956c5SAlexander Sverdlin ret = i2c_davinci_wait_bus_not_busy(dev);
54995a7f10eSVladimir Barinov if (ret < 0) {
55095a7f10eSVladimir Barinov dev_warn(dev->dev, "timeout waiting for bus ready\n");
55117f88151SFranklin S Cooper Jr goto out;
55295a7f10eSVladimir Barinov }
55395a7f10eSVladimir Barinov
55495a7f10eSVladimir Barinov for (i = 0; i < num; i++) {
55595a7f10eSVladimir Barinov ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
556d868caa1STroy Kisky dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
557d868caa1STroy Kisky ret);
55895a7f10eSVladimir Barinov if (ret < 0)
55917f88151SFranklin S Cooper Jr goto out;
56095a7f10eSVladimir Barinov }
56182c0de11SChaithrika U S
56217f88151SFranklin S Cooper Jr ret = num;
56382c0de11SChaithrika U S
56417f88151SFranklin S Cooper Jr out:
56517f88151SFranklin S Cooper Jr pm_runtime_mark_last_busy(dev->dev);
56617f88151SFranklin S Cooper Jr pm_runtime_put_autosuspend(dev->dev);
56717f88151SFranklin S Cooper Jr
56817f88151SFranklin S Cooper Jr return ret;
56995a7f10eSVladimir Barinov }
57095a7f10eSVladimir Barinov
i2c_davinci_func(struct i2c_adapter * adap)57195a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap)
57295a7f10eSVladimir Barinov {
573c6c7c729SDirk Behme return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
57495a7f10eSVladimir Barinov }
57595a7f10eSVladimir Barinov
terminate_read(struct davinci_i2c_dev * dev)5765a0d5f5fSTroy Kisky static void terminate_read(struct davinci_i2c_dev *dev)
5775a0d5f5fSTroy Kisky {
5785a0d5f5fSTroy Kisky u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
5795a0d5f5fSTroy Kisky w |= DAVINCI_I2C_MDR_NACK;
5805a0d5f5fSTroy Kisky davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
5815a0d5f5fSTroy Kisky
5825a0d5f5fSTroy Kisky /* Throw away data */
5835a0d5f5fSTroy Kisky davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
5845a0d5f5fSTroy Kisky if (!dev->terminate)
5855a0d5f5fSTroy Kisky dev_err(dev->dev, "RDR IRQ while no data requested\n");
5865a0d5f5fSTroy Kisky }
terminate_write(struct davinci_i2c_dev * dev)5875a0d5f5fSTroy Kisky static void terminate_write(struct davinci_i2c_dev *dev)
5885a0d5f5fSTroy Kisky {
5895a0d5f5fSTroy Kisky u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
5905a0d5f5fSTroy Kisky w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
5915a0d5f5fSTroy Kisky davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
5925a0d5f5fSTroy Kisky
5935a0d5f5fSTroy Kisky if (!dev->terminate)
5947605fa3bSDavid Brownell dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
5955a0d5f5fSTroy Kisky }
5965a0d5f5fSTroy Kisky
59795a7f10eSVladimir Barinov /*
59895a7f10eSVladimir Barinov * Interrupt service routine. This gets called whenever an I2C interrupt
59995a7f10eSVladimir Barinov * occurs.
60095a7f10eSVladimir Barinov */
i2c_davinci_isr(int this_irq,void * dev_id)60195a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
60295a7f10eSVladimir Barinov {
60395a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = dev_id;
60495a7f10eSVladimir Barinov u32 stat;
60595a7f10eSVladimir Barinov int count = 0;
60695a7f10eSVladimir Barinov u16 w;
60795a7f10eSVladimir Barinov
60817f88151SFranklin S Cooper Jr if (pm_runtime_suspended(dev->dev))
60917f88151SFranklin S Cooper Jr return IRQ_NONE;
61017f88151SFranklin S Cooper Jr
61195a7f10eSVladimir Barinov while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
61208882d20SHarvey Harrison dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
61395a7f10eSVladimir Barinov if (count++ == 100) {
61495a7f10eSVladimir Barinov dev_warn(dev->dev, "Too much work in one IRQ\n");
61595a7f10eSVladimir Barinov break;
61695a7f10eSVladimir Barinov }
61795a7f10eSVladimir Barinov
61895a7f10eSVladimir Barinov switch (stat) {
61995a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_AL:
6205a0d5f5fSTroy Kisky /* Arbitration lost, must retry */
62195a7f10eSVladimir Barinov dev->cmd_err |= DAVINCI_I2C_STR_AL;
6225a0d5f5fSTroy Kisky dev->buf_len = 0;
62395a7f10eSVladimir Barinov complete(&dev->cmd_complete);
62495a7f10eSVladimir Barinov break;
62595a7f10eSVladimir Barinov
62695a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_NACK:
62795a7f10eSVladimir Barinov dev->cmd_err |= DAVINCI_I2C_STR_NACK;
6285a0d5f5fSTroy Kisky dev->buf_len = 0;
62995a7f10eSVladimir Barinov complete(&dev->cmd_complete);
63095a7f10eSVladimir Barinov break;
63195a7f10eSVladimir Barinov
63295a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_ARDY:
633b73a9aecSTroy Kisky davinci_i2c_write_reg(dev,
634b73a9aecSTroy Kisky DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
635c6c7c729SDirk Behme if (((dev->buf_len == 0) && (dev->stop != 0)) ||
636c6c7c729SDirk Behme (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
637c6c7c729SDirk Behme w = davinci_i2c_read_reg(dev,
638c6c7c729SDirk Behme DAVINCI_I2C_MDR_REG);
639c6c7c729SDirk Behme w |= DAVINCI_I2C_MDR_STP;
640c6c7c729SDirk Behme davinci_i2c_write_reg(dev,
641c6c7c729SDirk Behme DAVINCI_I2C_MDR_REG, w);
642c6c7c729SDirk Behme }
64395a7f10eSVladimir Barinov complete(&dev->cmd_complete);
64495a7f10eSVladimir Barinov break;
64595a7f10eSVladimir Barinov
64695a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_RDR:
64795a7f10eSVladimir Barinov if (dev->buf_len) {
64895a7f10eSVladimir Barinov *dev->buf++ =
64995a7f10eSVladimir Barinov davinci_i2c_read_reg(dev,
65095a7f10eSVladimir Barinov DAVINCI_I2C_DRR_REG);
65195a7f10eSVladimir Barinov dev->buf_len--;
65295a7f10eSVladimir Barinov if (dev->buf_len)
65395a7f10eSVladimir Barinov continue;
65495a7f10eSVladimir Barinov
65595a7f10eSVladimir Barinov davinci_i2c_write_reg(dev,
65695a7f10eSVladimir Barinov DAVINCI_I2C_STR_REG,
657b73a9aecSTroy Kisky DAVINCI_I2C_IMR_RRDY);
6585a0d5f5fSTroy Kisky } else {
6595a0d5f5fSTroy Kisky /* signal can terminate transfer */
6605a0d5f5fSTroy Kisky terminate_read(dev);
6615a0d5f5fSTroy Kisky }
66295a7f10eSVladimir Barinov break;
66395a7f10eSVladimir Barinov
66495a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_XRDY:
66595a7f10eSVladimir Barinov if (dev->buf_len) {
66695a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
66795a7f10eSVladimir Barinov *dev->buf++);
66895a7f10eSVladimir Barinov dev->buf_len--;
66995a7f10eSVladimir Barinov if (dev->buf_len)
67095a7f10eSVladimir Barinov continue;
67195a7f10eSVladimir Barinov
67295a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev,
67395a7f10eSVladimir Barinov DAVINCI_I2C_IMR_REG);
674c062a251SChaithrika U S w &= ~DAVINCI_I2C_IMR_XRDY;
67595a7f10eSVladimir Barinov davinci_i2c_write_reg(dev,
67695a7f10eSVladimir Barinov DAVINCI_I2C_IMR_REG,
67795a7f10eSVladimir Barinov w);
6785a0d5f5fSTroy Kisky } else {
6795a0d5f5fSTroy Kisky /* signal can terminate transfer */
6805a0d5f5fSTroy Kisky terminate_write(dev);
6815a0d5f5fSTroy Kisky }
68295a7f10eSVladimir Barinov break;
68395a7f10eSVladimir Barinov
68495a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_SCD:
685b73a9aecSTroy Kisky davinci_i2c_write_reg(dev,
686b73a9aecSTroy Kisky DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
68795a7f10eSVladimir Barinov complete(&dev->cmd_complete);
68895a7f10eSVladimir Barinov break;
68995a7f10eSVladimir Barinov
69095a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_AAS:
6917605fa3bSDavid Brownell dev_dbg(dev->dev, "Address as slave interrupt\n");
6927605fa3bSDavid Brownell break;
6937605fa3bSDavid Brownell
6947605fa3bSDavid Brownell default:
6957605fa3bSDavid Brownell dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
6967605fa3bSDavid Brownell break;
6977605fa3bSDavid Brownell }
6987605fa3bSDavid Brownell }
69995a7f10eSVladimir Barinov
70095a7f10eSVladimir Barinov return count ? IRQ_HANDLED : IRQ_NONE;
70195a7f10eSVladimir Barinov }
70295a7f10eSVladimir Barinov
70382c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ
i2c_davinci_cpufreq_transition(struct notifier_block * nb,unsigned long val,void * data)70482c0de11SChaithrika U S static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
70582c0de11SChaithrika U S unsigned long val, void *data)
70682c0de11SChaithrika U S {
70782c0de11SChaithrika U S struct davinci_i2c_dev *dev;
70882c0de11SChaithrika U S
70982c0de11SChaithrika U S dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
71019cfcafdSBartosz Golaszewski
7113f3a89e1SPeter Rosin i2c_lock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER);
71282c0de11SChaithrika U S if (val == CPUFREQ_PRECHANGE) {
71382c0de11SChaithrika U S davinci_i2c_reset_ctrl(dev, 0);
71482c0de11SChaithrika U S } else if (val == CPUFREQ_POSTCHANGE) {
71582c0de11SChaithrika U S i2c_davinci_calc_clk_dividers(dev);
71682c0de11SChaithrika U S davinci_i2c_reset_ctrl(dev, 1);
71782c0de11SChaithrika U S }
7183f3a89e1SPeter Rosin i2c_unlock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER);
71982c0de11SChaithrika U S
72082c0de11SChaithrika U S return 0;
72182c0de11SChaithrika U S }
72282c0de11SChaithrika U S
i2c_davinci_cpufreq_register(struct davinci_i2c_dev * dev)72382c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
72482c0de11SChaithrika U S {
72582c0de11SChaithrika U S dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
72682c0de11SChaithrika U S
72782c0de11SChaithrika U S return cpufreq_register_notifier(&dev->freq_transition,
72882c0de11SChaithrika U S CPUFREQ_TRANSITION_NOTIFIER);
72982c0de11SChaithrika U S }
73082c0de11SChaithrika U S
i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev * dev)73182c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
73282c0de11SChaithrika U S {
73382c0de11SChaithrika U S cpufreq_unregister_notifier(&dev->freq_transition,
73482c0de11SChaithrika U S CPUFREQ_TRANSITION_NOTIFIER);
73582c0de11SChaithrika U S }
73682c0de11SChaithrika U S #else
i2c_davinci_cpufreq_register(struct davinci_i2c_dev * dev)73782c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
73882c0de11SChaithrika U S {
73982c0de11SChaithrika U S return 0;
74082c0de11SChaithrika U S }
74182c0de11SChaithrika U S
i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev * dev)74282c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
74382c0de11SChaithrika U S {
74482c0de11SChaithrika U S }
74582c0de11SChaithrika U S #endif
74682c0de11SChaithrika U S
7478dc0f8c7SGustavo A. R. Silva static const struct i2c_algorithm i2c_davinci_algo = {
74895a7f10eSVladimir Barinov .master_xfer = i2c_davinci_xfer,
74995a7f10eSVladimir Barinov .functionality = i2c_davinci_func,
75095a7f10eSVladimir Barinov };
75195a7f10eSVladimir Barinov
7525c3d8a46SHeiko Schocher static const struct of_device_id davinci_i2c_of_match[] = {
7535c3d8a46SHeiko Schocher {.compatible = "ti,davinci-i2c", },
75435780e86SAlexander Sverdlin {.compatible = "ti,keystone-i2c", },
7555c3d8a46SHeiko Schocher {},
7565c3d8a46SHeiko Schocher };
7575c3d8a46SHeiko Schocher MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
7585c3d8a46SHeiko Schocher
davinci_i2c_probe(struct platform_device * pdev)75995a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev)
76095a7f10eSVladimir Barinov {
76195a7f10eSVladimir Barinov struct davinci_i2c_dev *dev;
76295a7f10eSVladimir Barinov struct i2c_adapter *adap;
763cd2428c3SPhil Reid struct i2c_bus_recovery_info *rinfo;
7642c6ef04fSGrygorii Strashko int r, irq;
76595a7f10eSVladimir Barinov
7662c6ef04fSGrygorii Strashko irq = platform_get_irq(pdev, 0);
767cbffe6b3SDongliang Mu if (irq < 0)
768adcf6eaeSZhu Wang return irq;
76995a7f10eSVladimir Barinov
7701ebafd5fSAndrew Davis dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
7711ebafd5fSAndrew Davis if (!dev)
77285796843SVishwanathrao Badarkhe, Manish return -ENOMEM;
77395a7f10eSVladimir Barinov
7742e743787STroy Kisky init_completion(&dev->cmd_complete);
77519cfcafdSBartosz Golaszewski
776c4df5000SWolfram Sang dev->dev = &pdev->dev;
7772c6ef04fSGrygorii Strashko dev->irq = irq;
7781e2c2390SOlof Johansson dev->pdata = dev_get_platdata(&pdev->dev);
77995a7f10eSVladimir Barinov platform_set_drvdata(pdev, dev);
78095a7f10eSVladimir Barinov
7815c3d8a46SHeiko Schocher if (!dev->pdata && pdev->dev.of_node) {
7825c3d8a46SHeiko Schocher u32 prop;
7835c3d8a46SHeiko Schocher
7845c3d8a46SHeiko Schocher dev->pdata = devm_kzalloc(&pdev->dev,
7855c3d8a46SHeiko Schocher sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
786c4df5000SWolfram Sang if (!dev->pdata)
787c4df5000SWolfram Sang return -ENOMEM;
788c4df5000SWolfram Sang
7895c3d8a46SHeiko Schocher memcpy(dev->pdata, &davinci_i2c_platform_data_default,
7905c3d8a46SHeiko Schocher sizeof(struct davinci_i2c_platform_data));
7915c3d8a46SHeiko Schocher if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
7925c3d8a46SHeiko Schocher &prop))
7935c3d8a46SHeiko Schocher dev->pdata->bus_freq = prop / 1000;
7947ef97e9aSGrygorii Strashko
7957ef97e9aSGrygorii Strashko dev->pdata->has_pfunc =
7967ef97e9aSGrygorii Strashko of_property_read_bool(pdev->dev.of_node,
7977ef97e9aSGrygorii Strashko "ti,has-pfunc");
7985c3d8a46SHeiko Schocher } else if (!dev->pdata) {
7995c3d8a46SHeiko Schocher dev->pdata = &davinci_i2c_platform_data_default;
8005c3d8a46SHeiko Schocher }
8015c3d8a46SHeiko Schocher
80285796843SVishwanathrao Badarkhe, Manish dev->clk = devm_clk_get(&pdev->dev, NULL);
803c4df5000SWolfram Sang if (IS_ERR(dev->clk))
804cc209ac8SFranklin S Cooper Jr return PTR_ERR(dev->clk);
80595a7f10eSVladimir Barinov
806e0442d76SDejin Zheng dev->base = devm_platform_ioremap_resource(pdev, 0);
80785796843SVishwanathrao Badarkhe, Manish if (IS_ERR(dev->base)) {
80817f88151SFranklin S Cooper Jr return PTR_ERR(dev->base);
80917f88151SFranklin S Cooper Jr }
81017f88151SFranklin S Cooper Jr
81117f88151SFranklin S Cooper Jr pm_runtime_set_autosuspend_delay(dev->dev,
81217f88151SFranklin S Cooper Jr DAVINCI_I2C_PM_TIMEOUT);
81317f88151SFranklin S Cooper Jr pm_runtime_use_autosuspend(dev->dev);
81417f88151SFranklin S Cooper Jr
81517f88151SFranklin S Cooper Jr pm_runtime_enable(dev->dev);
81617f88151SFranklin S Cooper Jr
817a181b8d1SMinghao Chi r = pm_runtime_resume_and_get(dev->dev);
81817f88151SFranklin S Cooper Jr if (r < 0) {
81917f88151SFranklin S Cooper Jr dev_err(dev->dev, "failed to runtime_get device: %d\n", r);
820e2062df7SZhang Qilong goto err_pm;
821c062a251SChaithrika U S }
822c062a251SChaithrika U S
82395a7f10eSVladimir Barinov i2c_davinci_init(dev);
82495a7f10eSVladimir Barinov
82585796843SVishwanathrao Badarkhe, Manish r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
82685796843SVishwanathrao Badarkhe, Manish pdev->name, dev);
82795a7f10eSVladimir Barinov if (r) {
82895a7f10eSVladimir Barinov dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
82995a7f10eSVladimir Barinov goto err_unuse_clocks;
83095a7f10eSVladimir Barinov }
83195a7f10eSVladimir Barinov
83282c0de11SChaithrika U S r = i2c_davinci_cpufreq_register(dev);
83382c0de11SChaithrika U S if (r) {
83482c0de11SChaithrika U S dev_err(&pdev->dev, "failed to register cpufreq\n");
83585796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks;
83682c0de11SChaithrika U S }
83782c0de11SChaithrika U S
83895a7f10eSVladimir Barinov adap = &dev->adapter;
83995a7f10eSVladimir Barinov i2c_set_adapdata(adap, dev);
84095a7f10eSVladimir Barinov adap->owner = THIS_MODULE;
841adcb82a9SWolfram Sang adap->class = I2C_CLASS_DEPRECATED;
842ea1558ceSWolfram Sang strscpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
84395a7f10eSVladimir Barinov adap->algo = &i2c_davinci_algo;
84495a7f10eSVladimir Barinov adap->dev.parent = &pdev->dev;
84598a679caSJean Delvare adap->timeout = DAVINCI_I2C_TIMEOUT;
8465c3d8a46SHeiko Schocher adap->dev.of_node = pdev->dev.of_node;
84795a7f10eSVladimir Barinov
8487ef97e9aSGrygorii Strashko if (dev->pdata->has_pfunc)
8497ef97e9aSGrygorii Strashko adap->bus_recovery_info = &davinci_i2c_scl_recovery_info;
850e5353765SLinus Walleij else if (dev->pdata->gpio_recovery) {
851cd2428c3SPhil Reid rinfo = &davinci_i2c_gpio_recovery_info;
852cd2428c3SPhil Reid adap->bus_recovery_info = rinfo;
853e5353765SLinus Walleij rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl",
854e5353765SLinus Walleij GPIOD_OUT_HIGH_OPEN_DRAIN);
855e5353765SLinus Walleij if (IS_ERR(rinfo->scl_gpiod)) {
856e5353765SLinus Walleij r = PTR_ERR(rinfo->scl_gpiod);
857cd2428c3SPhil Reid goto err_unuse_clocks;
858e5353765SLinus Walleij }
859e5353765SLinus Walleij rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN);
860e5353765SLinus Walleij if (IS_ERR(rinfo->sda_gpiod)) {
861e5353765SLinus Walleij r = PTR_ERR(rinfo->sda_gpiod);
862cd2428c3SPhil Reid goto err_unuse_clocks;
863e5353765SLinus Walleij }
8642e65676fSGrygorii Strashko }
8652e65676fSGrygorii Strashko
86695a7f10eSVladimir Barinov adap->nr = pdev->id;
86795a7f10eSVladimir Barinov r = i2c_add_numbered_adapter(adap);
868ea734404SWolfram Sang if (r)
86985796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks;
87095a7f10eSVladimir Barinov
87117f88151SFranklin S Cooper Jr pm_runtime_mark_last_busy(dev->dev);
87217f88151SFranklin S Cooper Jr pm_runtime_put_autosuspend(dev->dev);
87317f88151SFranklin S Cooper Jr
87495a7f10eSVladimir Barinov return 0;
87595a7f10eSVladimir Barinov
87695a7f10eSVladimir Barinov err_unuse_clocks:
87717f88151SFranklin S Cooper Jr pm_runtime_dont_use_autosuspend(dev->dev);
87817f88151SFranklin S Cooper Jr pm_runtime_put_sync(dev->dev);
879e2062df7SZhang Qilong err_pm:
88017f88151SFranklin S Cooper Jr pm_runtime_disable(dev->dev);
88117f88151SFranklin S Cooper Jr
88295a7f10eSVladimir Barinov return r;
88395a7f10eSVladimir Barinov }
88495a7f10eSVladimir Barinov
davinci_i2c_remove(struct platform_device * pdev)885e190a0c3SUwe Kleine-König static void davinci_i2c_remove(struct platform_device *pdev)
88695a7f10eSVladimir Barinov {
88795a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
88817f88151SFranklin S Cooper Jr int ret;
88995a7f10eSVladimir Barinov
89082c0de11SChaithrika U S i2c_davinci_cpufreq_deregister(dev);
89182c0de11SChaithrika U S
89295a7f10eSVladimir Barinov i2c_del_adapter(&dev->adapter);
89395a7f10eSVladimir Barinov
894e190a0c3SUwe Kleine-König ret = pm_runtime_get_sync(&pdev->dev);
895a181b8d1SMinghao Chi if (ret < 0)
896e190a0c3SUwe Kleine-König dev_err(&pdev->dev, "Failed to resume device\n");
897e190a0c3SUwe Kleine-König else
89895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
89995a7f10eSVladimir Barinov
90017f88151SFranklin S Cooper Jr pm_runtime_dont_use_autosuspend(dev->dev);
90117f88151SFranklin S Cooper Jr pm_runtime_put_sync(dev->dev);
90217f88151SFranklin S Cooper Jr pm_runtime_disable(dev->dev);
90395a7f10eSVladimir Barinov }
90495a7f10eSVladimir Barinov
davinci_i2c_suspend(struct device * dev)90568f15de9SChaithrika U S static int davinci_i2c_suspend(struct device *dev)
90668f15de9SChaithrika U S {
9079242e72aSMasahiro Yamada struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev);
90868f15de9SChaithrika U S
90968f15de9SChaithrika U S /* put I2C into reset */
91068f15de9SChaithrika U S davinci_i2c_reset_ctrl(i2c_dev, 0);
91168f15de9SChaithrika U S
91268f15de9SChaithrika U S return 0;
91368f15de9SChaithrika U S }
91468f15de9SChaithrika U S
davinci_i2c_resume(struct device * dev)91568f15de9SChaithrika U S static int davinci_i2c_resume(struct device *dev)
91668f15de9SChaithrika U S {
9179242e72aSMasahiro Yamada struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev);
91868f15de9SChaithrika U S
91968f15de9SChaithrika U S /* take I2C out of reset */
92068f15de9SChaithrika U S davinci_i2c_reset_ctrl(i2c_dev, 1);
92168f15de9SChaithrika U S
92268f15de9SChaithrika U S return 0;
92368f15de9SChaithrika U S }
92468f15de9SChaithrika U S
92568f15de9SChaithrika U S static const struct dev_pm_ops davinci_i2c_pm = {
92668f15de9SChaithrika U S .suspend = davinci_i2c_suspend,
92768f15de9SChaithrika U S .resume = davinci_i2c_resume,
928a6624009SPaul Cercueil NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
92917f88151SFranklin S Cooper Jr pm_runtime_force_resume)
93068f15de9SChaithrika U S };
93168f15de9SChaithrika U S
932dcb31e8dSAndrew Davis static const struct platform_device_id davinci_i2c_driver_ids[] = {
933dcb31e8dSAndrew Davis { .name = "i2c_davinci", },
934dcb31e8dSAndrew Davis { /* sentinel */ }
935dcb31e8dSAndrew Davis };
936dcb31e8dSAndrew Davis MODULE_DEVICE_TABLE(platform, davinci_i2c_driver_ids);
937add8eda7SKay Sievers
93895a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = {
93995a7f10eSVladimir Barinov .probe = davinci_i2c_probe,
940e190a0c3SUwe Kleine-König .remove_new = davinci_i2c_remove,
941dcb31e8dSAndrew Davis .id_table = davinci_i2c_driver_ids,
94295a7f10eSVladimir Barinov .driver = {
94395a7f10eSVladimir Barinov .name = "i2c_davinci",
944a6624009SPaul Cercueil .pm = pm_sleep_ptr(&davinci_i2c_pm),
9454e905323SSachin Kamat .of_match_table = davinci_i2c_of_match,
94695a7f10eSVladimir Barinov },
94795a7f10eSVladimir Barinov };
94895a7f10eSVladimir Barinov
94995a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */
davinci_i2c_init_driver(void)95095a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void)
95195a7f10eSVladimir Barinov {
95295a7f10eSVladimir Barinov return platform_driver_register(&davinci_i2c_driver);
95395a7f10eSVladimir Barinov }
95495a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver);
95595a7f10eSVladimir Barinov
davinci_i2c_exit_driver(void)95695a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void)
95795a7f10eSVladimir Barinov {
95895a7f10eSVladimir Barinov platform_driver_unregister(&davinci_i2c_driver);
95995a7f10eSVladimir Barinov }
96095a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver);
96195a7f10eSVladimir Barinov
96295a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India");
96395a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
96495a7f10eSVladimir Barinov MODULE_LICENSE("GPL");
965