195a7f10eSVladimir Barinov /* 295a7f10eSVladimir Barinov * TI DAVINCI I2C adapter driver. 395a7f10eSVladimir Barinov * 495a7f10eSVladimir Barinov * Copyright (C) 2006 Texas Instruments. 595a7f10eSVladimir Barinov * Copyright (C) 2007 MontaVista Software Inc. 695a7f10eSVladimir Barinov * 795a7f10eSVladimir Barinov * Updated by Vinod & Sudhakar Feb 2005 895a7f10eSVladimir Barinov * 995a7f10eSVladimir Barinov * ---------------------------------------------------------------------------- 1095a7f10eSVladimir Barinov * 1195a7f10eSVladimir Barinov * This program is free software; you can redistribute it and/or modify 1295a7f10eSVladimir Barinov * it under the terms of the GNU General Public License as published by 1395a7f10eSVladimir Barinov * the Free Software Foundation; either version 2 of the License, or 1495a7f10eSVladimir Barinov * (at your option) any later version. 1595a7f10eSVladimir Barinov * 1695a7f10eSVladimir Barinov * This program is distributed in the hope that it will be useful, 1795a7f10eSVladimir Barinov * but WITHOUT ANY WARRANTY; without even the implied warranty of 1895a7f10eSVladimir Barinov * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1995a7f10eSVladimir Barinov * GNU General Public License for more details. 2095a7f10eSVladimir Barinov * ---------------------------------------------------------------------------- 2195a7f10eSVladimir Barinov * 2295a7f10eSVladimir Barinov */ 2395a7f10eSVladimir Barinov #include <linux/kernel.h> 2495a7f10eSVladimir Barinov #include <linux/module.h> 2595a7f10eSVladimir Barinov #include <linux/delay.h> 2695a7f10eSVladimir Barinov #include <linux/i2c.h> 2795a7f10eSVladimir Barinov #include <linux/clk.h> 2895a7f10eSVladimir Barinov #include <linux/errno.h> 2995a7f10eSVladimir Barinov #include <linux/sched.h> 3095a7f10eSVladimir Barinov #include <linux/err.h> 3195a7f10eSVladimir Barinov #include <linux/interrupt.h> 3295a7f10eSVladimir Barinov #include <linux/platform_device.h> 3395a7f10eSVladimir Barinov #include <linux/io.h> 345a0e3ad6STejun Heo #include <linux/slab.h> 3582c0de11SChaithrika U S #include <linux/cpufreq.h> 368574faf9SPhilby John #include <linux/gpio.h> 375c3d8a46SHeiko Schocher #include <linux/of_device.h> 38ec2a0833SArnd Bergmann #include <linux/platform_data/i2c-davinci.h> 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 completion xfr_complete; 14382c0de11SChaithrika U S struct notifier_block freq_transition; 14482c0de11SChaithrika U S #endif 1455c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata; 14695a7f10eSVladimir Barinov }; 14795a7f10eSVladimir Barinov 14895a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */ 14995a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = { 15095a7f10eSVladimir Barinov .bus_freq = 100, 15195a7f10eSVladimir Barinov .bus_delay = 0, 15295a7f10eSVladimir Barinov }; 15395a7f10eSVladimir Barinov 15495a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev, 15595a7f10eSVladimir Barinov int reg, u16 val) 15695a7f10eSVladimir Barinov { 157a238dcfaSTaras Kondratiuk writew_relaxed(val, i2c_dev->base + reg); 15895a7f10eSVladimir Barinov } 15995a7f10eSVladimir Barinov 16095a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg) 16195a7f10eSVladimir Barinov { 162a238dcfaSTaras Kondratiuk return readw_relaxed(i2c_dev->base + reg); 16395a7f10eSVladimir Barinov } 16495a7f10eSVladimir Barinov 1655ae5b113SChaithrika U S static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev, 1665ae5b113SChaithrika U S int val) 1675ae5b113SChaithrika U S { 1685ae5b113SChaithrika U S u16 w; 1695ae5b113SChaithrika U S 1705ae5b113SChaithrika U S w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG); 1715ae5b113SChaithrika U S if (!val) /* put I2C into reset */ 1725ae5b113SChaithrika U S w &= ~DAVINCI_I2C_MDR_IRS; 1735ae5b113SChaithrika U S else /* take I2C out of reset */ 1745ae5b113SChaithrika U S w |= DAVINCI_I2C_MDR_IRS; 1755ae5b113SChaithrika U S 1765ae5b113SChaithrika U S davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w); 1775ae5b113SChaithrika U S } 1785ae5b113SChaithrika U S 1795ae5b113SChaithrika U S static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev) 18095a7f10eSVladimir Barinov { 1815c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 18295a7f10eSVladimir Barinov u16 psc; 18395a7f10eSVladimir Barinov u32 clk; 184cc99ff70STroy Kisky u32 d; 18595a7f10eSVladimir Barinov u32 clkh; 18695a7f10eSVladimir Barinov u32 clkl; 18795a7f10eSVladimir Barinov u32 input_clock = clk_get_rate(dev->clk); 18835780e86SAlexander Sverdlin struct device_node *of_node = dev->dev->of_node; 18995a7f10eSVladimir Barinov 19095a7f10eSVladimir Barinov /* NOTE: I2C Clock divider programming info 19195a7f10eSVladimir Barinov * As per I2C specs the following formulas provide prescaler 19295a7f10eSVladimir Barinov * and low/high divider values 19395a7f10eSVladimir Barinov * input clk --> PSC Div -----------> ICCL/H Div --> output clock 19495a7f10eSVladimir Barinov * module clk 19595a7f10eSVladimir Barinov * 19695a7f10eSVladimir Barinov * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ] 19795a7f10eSVladimir Barinov * 19895a7f10eSVladimir Barinov * Thus, 19995a7f10eSVladimir Barinov * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d; 20095a7f10eSVladimir Barinov * 20195a7f10eSVladimir Barinov * where if PSC == 0, d = 7, 20295a7f10eSVladimir Barinov * if PSC == 1, d = 6 20395a7f10eSVladimir Barinov * if PSC > 1 , d = 5 20435780e86SAlexander Sverdlin * 20535780e86SAlexander Sverdlin * Note: 20635780e86SAlexander Sverdlin * d is always 6 on Keystone I2C controller 20795a7f10eSVladimir Barinov */ 20895a7f10eSVladimir Barinov 20987cb5b42SAlexander Sverdlin /* 21087cb5b42SAlexander Sverdlin * Both Davinci and current Keystone User Guides recommend a value 21187cb5b42SAlexander Sverdlin * between 7MHz and 12MHz. In reality 7MHz module clock doesn't 21287cb5b42SAlexander Sverdlin * always produce enough margin between SDA and SCL transitions. 21387cb5b42SAlexander Sverdlin * Measurements show that the higher the module clock is, the 21487cb5b42SAlexander Sverdlin * bigger is the margin, providing more reliable communication. 21587cb5b42SAlexander Sverdlin * So we better target for 12MHz. 21687cb5b42SAlexander Sverdlin */ 21787cb5b42SAlexander Sverdlin psc = (input_clock / 12000000) - 1; 218cc99ff70STroy Kisky if ((input_clock / (psc + 1)) > 12000000) 219cc99ff70STroy Kisky psc++; /* better to run under spec than over */ 220cc99ff70STroy Kisky d = (psc >= 2) ? 5 : 7 - psc; 22195a7f10eSVladimir Barinov 22235780e86SAlexander Sverdlin if (of_node && of_device_is_compatible(of_node, "ti,keystone-i2c")) 22335780e86SAlexander Sverdlin d = 6; 22435780e86SAlexander Sverdlin 225955fc950SAlexander Sverdlin clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)); 226955fc950SAlexander Sverdlin /* Avoid driving the bus too fast because of rounding errors above */ 227955fc950SAlexander Sverdlin if (input_clock / (psc + 1) / clk > pdata->bus_freq * 1000) 228955fc950SAlexander Sverdlin clk++; 229955fc950SAlexander Sverdlin /* 230955fc950SAlexander Sverdlin * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at 231955fc950SAlexander Sverdlin * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH 232955fc950SAlexander Sverdlin * to LOW ratio as 1 to 2 is more safe. 233955fc950SAlexander Sverdlin */ 234955fc950SAlexander Sverdlin if (pdata->bus_freq > 100) 235955fc950SAlexander Sverdlin clkl = (clk << 1) / 3; 236955fc950SAlexander Sverdlin else 237955fc950SAlexander Sverdlin clkl = (clk >> 1); 238955fc950SAlexander Sverdlin /* 239955fc950SAlexander Sverdlin * It's not always possible to have 1 to 2 ratio when d=7, so fall back 240955fc950SAlexander Sverdlin * to minimal possible clkh in this case. 241955fc950SAlexander Sverdlin */ 242955fc950SAlexander Sverdlin if (clk >= clkl + d) { 243955fc950SAlexander Sverdlin clkh = clk - clkl - d; 244955fc950SAlexander Sverdlin clkl -= d; 245955fc950SAlexander Sverdlin } else { 246955fc950SAlexander Sverdlin clkh = 0; 247955fc950SAlexander Sverdlin clkl = clk - (d << 1); 248955fc950SAlexander Sverdlin } 24995a7f10eSVladimir Barinov 25095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc); 25195a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh); 25295a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl); 25395a7f10eSVladimir Barinov 2545ae5b113SChaithrika U S dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk); 2555ae5b113SChaithrika U S } 2565ae5b113SChaithrika U S 2575ae5b113SChaithrika U S /* 2585ae5b113SChaithrika U S * This function configures I2C and brings I2C out of reset. 2595ae5b113SChaithrika U S * This function is called during I2C init function. This function 2605ae5b113SChaithrika U S * also gets called if I2C encounters any errors. 2615ae5b113SChaithrika U S */ 2625ae5b113SChaithrika U S static int i2c_davinci_init(struct davinci_i2c_dev *dev) 2635ae5b113SChaithrika U S { 2645c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 2655ae5b113SChaithrika U S 2665ae5b113SChaithrika U S /* put I2C into reset */ 2675ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 0); 2685ae5b113SChaithrika U S 2695ae5b113SChaithrika U S /* compute clock dividers */ 2705ae5b113SChaithrika U S i2c_davinci_calc_clk_dividers(dev); 2715ae5b113SChaithrika U S 2727605fa3bSDavid Brownell /* Respond at reserved "SMBus Host" slave address" (and zero); 2737605fa3bSDavid Brownell * we seem to have no option to not respond... 2747605fa3bSDavid Brownell */ 27541c8d452SAlexander Sverdlin davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, DAVINCI_I2C_OWN_ADDRESS); 2767605fa3bSDavid Brownell 27795a7f10eSVladimir Barinov dev_dbg(dev->dev, "PSC = %d\n", 27895a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG)); 27995a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKL = %d\n", 28095a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG)); 28195a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKH = %d\n", 28295a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG)); 283cc99ff70STroy Kisky dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n", 284cc99ff70STroy Kisky pdata->bus_freq, pdata->bus_delay); 28595a7f10eSVladimir Barinov 2865c3d8a46SHeiko Schocher 28795a7f10eSVladimir Barinov /* Take the I2C module out of reset: */ 2885ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 1); 28995a7f10eSVladimir Barinov 29095a7f10eSVladimir Barinov /* Enable interrupts */ 29195a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL); 29295a7f10eSVladimir Barinov 29395a7f10eSVladimir Barinov return 0; 29495a7f10eSVladimir Barinov } 29595a7f10eSVladimir Barinov 29695a7f10eSVladimir Barinov /* 2972e65676fSGrygorii Strashko * This routine does i2c bus recovery by using i2c_generic_gpio_recovery 2982e65676fSGrygorii Strashko * which is provided by I2C Bus recovery infrastructure. 2992e65676fSGrygorii Strashko */ 3002e65676fSGrygorii Strashko static void davinci_i2c_prepare_recovery(struct i2c_adapter *adap) 3012e65676fSGrygorii Strashko { 3022e65676fSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3032e65676fSGrygorii Strashko 3042e65676fSGrygorii Strashko /* Disable interrupts */ 3052e65676fSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, 0); 3062e65676fSGrygorii Strashko 3072e65676fSGrygorii Strashko /* put I2C into reset */ 3082e65676fSGrygorii Strashko davinci_i2c_reset_ctrl(dev, 0); 3092e65676fSGrygorii Strashko } 3102e65676fSGrygorii Strashko 3112e65676fSGrygorii Strashko static void davinci_i2c_unprepare_recovery(struct i2c_adapter *adap) 3122e65676fSGrygorii Strashko { 3132e65676fSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3142e65676fSGrygorii Strashko 3152e65676fSGrygorii Strashko i2c_davinci_init(dev); 3162e65676fSGrygorii Strashko } 3172e65676fSGrygorii Strashko 3182e65676fSGrygorii Strashko static struct i2c_bus_recovery_info davinci_i2c_gpio_recovery_info = { 3192e65676fSGrygorii Strashko .recover_bus = i2c_generic_gpio_recovery, 3202e65676fSGrygorii Strashko .prepare_recovery = davinci_i2c_prepare_recovery, 3212e65676fSGrygorii Strashko .unprepare_recovery = davinci_i2c_unprepare_recovery, 3222e65676fSGrygorii Strashko }; 3232e65676fSGrygorii Strashko 3247ef97e9aSGrygorii Strashko static void davinci_i2c_set_scl(struct i2c_adapter *adap, int val) 3257ef97e9aSGrygorii Strashko { 3267ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3277ef97e9aSGrygorii Strashko 3287ef97e9aSGrygorii Strashko if (val) 3297ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_DSET_REG, 3307ef97e9aSGrygorii Strashko DAVINCI_I2C_DSET_PDSET0); 3317ef97e9aSGrygorii Strashko else 3327ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_DCLR_REG, 3337ef97e9aSGrygorii Strashko DAVINCI_I2C_DCLR_PDCLR0); 3347ef97e9aSGrygorii Strashko } 3357ef97e9aSGrygorii Strashko 3367ef97e9aSGrygorii Strashko static int davinci_i2c_get_scl(struct i2c_adapter *adap) 3377ef97e9aSGrygorii Strashko { 3387ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3397ef97e9aSGrygorii Strashko int val; 3407ef97e9aSGrygorii Strashko 3417ef97e9aSGrygorii Strashko /* read the state of SCL */ 3427ef97e9aSGrygorii Strashko val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG); 3437ef97e9aSGrygorii Strashko return val & DAVINCI_I2C_DIN_PDIN0; 3447ef97e9aSGrygorii Strashko } 3457ef97e9aSGrygorii Strashko 3467ef97e9aSGrygorii Strashko static int davinci_i2c_get_sda(struct i2c_adapter *adap) 3477ef97e9aSGrygorii Strashko { 3487ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3497ef97e9aSGrygorii Strashko int val; 3507ef97e9aSGrygorii Strashko 3517ef97e9aSGrygorii Strashko /* read the state of SDA */ 3527ef97e9aSGrygorii Strashko val = davinci_i2c_read_reg(dev, DAVINCI_I2C_DIN_REG); 3537ef97e9aSGrygorii Strashko return val & DAVINCI_I2C_DIN_PDIN1; 3547ef97e9aSGrygorii Strashko } 3557ef97e9aSGrygorii Strashko 3567ef97e9aSGrygorii Strashko static void davinci_i2c_scl_prepare_recovery(struct i2c_adapter *adap) 3577ef97e9aSGrygorii Strashko { 3587ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3597ef97e9aSGrygorii Strashko 3607ef97e9aSGrygorii Strashko davinci_i2c_prepare_recovery(adap); 3617ef97e9aSGrygorii Strashko 3627ef97e9aSGrygorii Strashko /* SCL output, SDA input */ 3637ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_DIR_REG, DAVINCI_I2C_DIR_PDIR0); 3647ef97e9aSGrygorii Strashko 3657ef97e9aSGrygorii Strashko /* change to GPIO mode */ 3667ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG, 3677ef97e9aSGrygorii Strashko DAVINCI_I2C_FUNC_PFUNC0); 3687ef97e9aSGrygorii Strashko } 3697ef97e9aSGrygorii Strashko 3707ef97e9aSGrygorii Strashko static void davinci_i2c_scl_unprepare_recovery(struct i2c_adapter *adap) 3717ef97e9aSGrygorii Strashko { 3727ef97e9aSGrygorii Strashko struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3737ef97e9aSGrygorii Strashko 3747ef97e9aSGrygorii Strashko /* change back to I2C mode */ 3757ef97e9aSGrygorii Strashko davinci_i2c_write_reg(dev, DAVINCI_I2C_FUNC_REG, 0); 3767ef97e9aSGrygorii Strashko 3777ef97e9aSGrygorii Strashko davinci_i2c_unprepare_recovery(adap); 3787ef97e9aSGrygorii Strashko } 3797ef97e9aSGrygorii Strashko 3807ef97e9aSGrygorii Strashko static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info = { 3817ef97e9aSGrygorii Strashko .recover_bus = i2c_generic_scl_recovery, 3827ef97e9aSGrygorii Strashko .set_scl = davinci_i2c_set_scl, 3837ef97e9aSGrygorii Strashko .get_scl = davinci_i2c_get_scl, 3847ef97e9aSGrygorii Strashko .get_sda = davinci_i2c_get_sda, 3857ef97e9aSGrygorii Strashko .prepare_recovery = davinci_i2c_scl_prepare_recovery, 3867ef97e9aSGrygorii Strashko .unprepare_recovery = davinci_i2c_scl_unprepare_recovery, 3877ef97e9aSGrygorii Strashko }; 3887ef97e9aSGrygorii Strashko 3892e65676fSGrygorii Strashko /* 39095a7f10eSVladimir Barinov * Waiting for bus not busy 39195a7f10eSVladimir Barinov */ 39272a956c5SAlexander Sverdlin static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev) 39395a7f10eSVladimir Barinov { 39472a956c5SAlexander Sverdlin unsigned long timeout = jiffies + dev->adapter.timeout; 39595a7f10eSVladimir Barinov 39672a956c5SAlexander Sverdlin do { 39772a956c5SAlexander Sverdlin if (!(davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB)) 39872a956c5SAlexander Sverdlin return 0; 39972a956c5SAlexander Sverdlin schedule_timeout_uninterruptible(1); 40072a956c5SAlexander Sverdlin } while (time_before_eq(jiffies, timeout)); 40172a956c5SAlexander Sverdlin 40272a956c5SAlexander Sverdlin dev_warn(dev->dev, "timeout waiting for bus ready\n"); 4032e65676fSGrygorii Strashko i2c_recover_bus(&dev->adapter); 40472a956c5SAlexander Sverdlin 40572a956c5SAlexander Sverdlin /* 40672a956c5SAlexander Sverdlin * if bus is still "busy" here, it's most probably a HW problem like 40772a956c5SAlexander Sverdlin * short-circuit 40872a956c5SAlexander Sverdlin */ 40972a956c5SAlexander Sverdlin if (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB) 41072a956c5SAlexander Sverdlin return -EIO; 41195a7f10eSVladimir Barinov 41295a7f10eSVladimir Barinov return 0; 41395a7f10eSVladimir Barinov } 41495a7f10eSVladimir Barinov 41595a7f10eSVladimir Barinov /* 41695a7f10eSVladimir Barinov * Low level master read/write transaction. This function is called 41795a7f10eSVladimir Barinov * from i2c_davinci_xfer. 41895a7f10eSVladimir Barinov */ 41995a7f10eSVladimir Barinov static int 42095a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop) 42195a7f10eSVladimir Barinov { 42295a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 4235c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 42495a7f10eSVladimir Barinov u32 flag; 42595a7f10eSVladimir Barinov u16 w; 426d9e1f441SNicholas Mc Guire unsigned long time_left; 42795a7f10eSVladimir Barinov 42841c8d452SAlexander Sverdlin if (msg->addr == DAVINCI_I2C_OWN_ADDRESS) { 42941c8d452SAlexander Sverdlin dev_warn(dev->dev, "transfer to own address aborted\n"); 43041c8d452SAlexander Sverdlin return -EADDRNOTAVAIL; 43141c8d452SAlexander Sverdlin } 43241c8d452SAlexander Sverdlin 43395a7f10eSVladimir Barinov /* Introduce a delay, required for some boards (e.g Davinci EVM) */ 43495a7f10eSVladimir Barinov if (pdata->bus_delay) 43595a7f10eSVladimir Barinov udelay(pdata->bus_delay); 43695a7f10eSVladimir Barinov 43795a7f10eSVladimir Barinov /* set the slave address */ 43895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr); 43995a7f10eSVladimir Barinov 44095a7f10eSVladimir Barinov dev->buf = msg->buf; 44195a7f10eSVladimir Barinov dev->buf_len = msg->len; 442c6c7c729SDirk Behme dev->stop = stop; 44395a7f10eSVladimir Barinov 44495a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len); 44595a7f10eSVladimir Barinov 44616735d02SWolfram Sang reinit_completion(&dev->cmd_complete); 44795a7f10eSVladimir Barinov dev->cmd_err = 0; 44895a7f10eSVladimir Barinov 449c5b4afecSJon Povey /* Take I2C out of reset and configure it as master */ 450c5b4afecSJon Povey flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST; 45195a7f10eSVladimir Barinov 45295a7f10eSVladimir Barinov /* if the slave address is ten bit address, enable XA bit */ 45395a7f10eSVladimir Barinov if (msg->flags & I2C_M_TEN) 45495a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_XA; 45595a7f10eSVladimir Barinov if (!(msg->flags & I2C_M_RD)) 45695a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_TRX; 457c5b4afecSJon Povey if (msg->len == 0) 458c6c7c729SDirk Behme flag |= DAVINCI_I2C_MDR_RM; 45995a7f10eSVladimir Barinov 46095a7f10eSVladimir Barinov /* Enable receive or transmit interrupts */ 46195a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG); 46295a7f10eSVladimir Barinov if (msg->flags & I2C_M_RD) 463c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_RRDY; 46495a7f10eSVladimir Barinov else 465c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_XRDY; 46695a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w); 46795a7f10eSVladimir Barinov 4685a0d5f5fSTroy Kisky dev->terminate = 0; 469c6c7c729SDirk Behme 470c6c7c729SDirk Behme /* 471c5b4afecSJon Povey * Write mode register first as needed for correct behaviour 472c5b4afecSJon Povey * on OMAP-L138, but don't set STT yet to avoid a race with XRDY 47325985edcSLucas De Marchi * occurring before we have loaded DXR 474c5b4afecSJon Povey */ 475c5b4afecSJon Povey davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 476c5b4afecSJon Povey 477c5b4afecSJon Povey /* 478c6c7c729SDirk Behme * First byte should be set here, not after interrupt, 479c6c7c729SDirk Behme * because transmit-data-ready interrupt can come before 480c6c7c729SDirk Behme * NACK-interrupt during sending of previous message and 481c6c7c729SDirk Behme * ICDXR may have wrong data 482c5b4afecSJon Povey * It also saves us one interrupt, slightly faster 483c6c7c729SDirk Behme */ 484c6c7c729SDirk Behme if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) { 485c6c7c729SDirk Behme davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++); 486c6c7c729SDirk Behme dev->buf_len--; 487c6c7c729SDirk Behme } 488c6c7c729SDirk Behme 489c5b4afecSJon Povey /* Set STT to begin transmit now DXR is loaded */ 490c5b4afecSJon Povey flag |= DAVINCI_I2C_MDR_STT; 491c5b4afecSJon Povey if (stop && msg->len != 0) 492c5b4afecSJon Povey flag |= DAVINCI_I2C_MDR_STP; 4934bba0fd8SJon Povey davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 4944bba0fd8SJon Povey 495d9e1f441SNicholas Mc Guire time_left = wait_for_completion_timeout(&dev->cmd_complete, 496d9e1f441SNicholas Mc Guire dev->adapter.timeout); 497d9e1f441SNicholas Mc Guire if (!time_left) { 49895a7f10eSVladimir Barinov dev_err(dev->dev, "controller timed out\n"); 4992e65676fSGrygorii Strashko i2c_recover_bus(adap); 5005a0d5f5fSTroy Kisky dev->buf_len = 0; 50195a7f10eSVladimir Barinov return -ETIMEDOUT; 50295a7f10eSVladimir Barinov } 5035a0d5f5fSTroy Kisky if (dev->buf_len) { 5045a0d5f5fSTroy Kisky /* This should be 0 if all bytes were transferred 5055a0d5f5fSTroy Kisky * or dev->cmd_err denotes an error. 5065a0d5f5fSTroy Kisky */ 507*631de7a4SWolfram Sang dev_err(dev->dev, "abnormal termination buf_len=%zu\n", 5085a0d5f5fSTroy Kisky dev->buf_len); 5095a0d5f5fSTroy Kisky dev->terminate = 1; 5105a0d5f5fSTroy Kisky wmb(); 5115a0d5f5fSTroy Kisky dev->buf_len = 0; 512d9e1f441SNicholas Mc Guire return -EREMOTEIO; 5135a0d5f5fSTroy Kisky } 51495a7f10eSVladimir Barinov 51595a7f10eSVladimir Barinov /* no error */ 51695a7f10eSVladimir Barinov if (likely(!dev->cmd_err)) 51795a7f10eSVladimir Barinov return msg->len; 51895a7f10eSVladimir Barinov 51995a7f10eSVladimir Barinov /* We have an error */ 52095a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_AL) { 52195a7f10eSVladimir Barinov i2c_davinci_init(dev); 52295a7f10eSVladimir Barinov return -EIO; 52395a7f10eSVladimir Barinov } 52495a7f10eSVladimir Barinov 52595a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_NACK) { 52695a7f10eSVladimir Barinov if (msg->flags & I2C_M_IGNORE_NAK) 52795a7f10eSVladimir Barinov return msg->len; 52895a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 529c062a251SChaithrika U S w |= DAVINCI_I2C_MDR_STP; 53095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 53195a7f10eSVladimir Barinov return -EREMOTEIO; 53295a7f10eSVladimir Barinov } 53395a7f10eSVladimir Barinov return -EIO; 53495a7f10eSVladimir Barinov } 53595a7f10eSVladimir Barinov 53695a7f10eSVladimir Barinov /* 53795a7f10eSVladimir Barinov * Prepare controller for a transaction and call i2c_davinci_xfer_msg 53895a7f10eSVladimir Barinov */ 53995a7f10eSVladimir Barinov static int 54095a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) 54195a7f10eSVladimir Barinov { 54295a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 54395a7f10eSVladimir Barinov int i; 54495a7f10eSVladimir Barinov int ret; 54595a7f10eSVladimir Barinov 54608882d20SHarvey Harrison dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); 54795a7f10eSVladimir Barinov 54817f88151SFranklin S Cooper Jr ret = pm_runtime_get_sync(dev->dev); 54917f88151SFranklin S Cooper Jr if (ret < 0) { 55017f88151SFranklin S Cooper Jr dev_err(dev->dev, "Failed to runtime_get device: %d\n", ret); 55117f88151SFranklin S Cooper Jr pm_runtime_put_noidle(dev->dev); 55217f88151SFranklin S Cooper Jr return ret; 55317f88151SFranklin S Cooper Jr } 55417f88151SFranklin S Cooper Jr 55572a956c5SAlexander Sverdlin ret = i2c_davinci_wait_bus_not_busy(dev); 55695a7f10eSVladimir Barinov if (ret < 0) { 55795a7f10eSVladimir Barinov dev_warn(dev->dev, "timeout waiting for bus ready\n"); 55817f88151SFranklin S Cooper Jr goto out; 55995a7f10eSVladimir Barinov } 56095a7f10eSVladimir Barinov 56195a7f10eSVladimir Barinov for (i = 0; i < num; i++) { 56295a7f10eSVladimir Barinov ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1))); 563d868caa1STroy Kisky dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num, 564d868caa1STroy Kisky ret); 56595a7f10eSVladimir Barinov if (ret < 0) 56617f88151SFranklin S Cooper Jr goto out; 56795a7f10eSVladimir Barinov } 56882c0de11SChaithrika U S 56917f88151SFranklin S Cooper Jr ret = num; 57082c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 57182c0de11SChaithrika U S complete(&dev->xfr_complete); 57282c0de11SChaithrika U S #endif 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); 72082c0de11SChaithrika U S if (val == CPUFREQ_PRECHANGE) { 72182c0de11SChaithrika U S wait_for_completion(&dev->xfr_complete); 72282c0de11SChaithrika U S davinci_i2c_reset_ctrl(dev, 0); 72382c0de11SChaithrika U S } else if (val == CPUFREQ_POSTCHANGE) { 72482c0de11SChaithrika U S i2c_davinci_calc_clk_dividers(dev); 72582c0de11SChaithrika U S davinci_i2c_reset_ctrl(dev, 1); 72682c0de11SChaithrika U S } 72782c0de11SChaithrika U S 72882c0de11SChaithrika U S return 0; 72982c0de11SChaithrika U S } 73082c0de11SChaithrika U S 73182c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev) 73282c0de11SChaithrika U S { 73382c0de11SChaithrika U S dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition; 73482c0de11SChaithrika U S 73582c0de11SChaithrika U S return cpufreq_register_notifier(&dev->freq_transition, 73682c0de11SChaithrika U S CPUFREQ_TRANSITION_NOTIFIER); 73782c0de11SChaithrika U S } 73882c0de11SChaithrika U S 73982c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev) 74082c0de11SChaithrika U S { 74182c0de11SChaithrika U S cpufreq_unregister_notifier(&dev->freq_transition, 74282c0de11SChaithrika U S CPUFREQ_TRANSITION_NOTIFIER); 74382c0de11SChaithrika U S } 74482c0de11SChaithrika U S #else 74582c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev) 74682c0de11SChaithrika U S { 74782c0de11SChaithrika U S return 0; 74882c0de11SChaithrika U S } 74982c0de11SChaithrika U S 75082c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev) 75182c0de11SChaithrika U S { 75282c0de11SChaithrika U S } 75382c0de11SChaithrika U S #endif 75482c0de11SChaithrika U S 7558dc0f8c7SGustavo A. R. Silva static const struct i2c_algorithm i2c_davinci_algo = { 75695a7f10eSVladimir Barinov .master_xfer = i2c_davinci_xfer, 75795a7f10eSVladimir Barinov .functionality = i2c_davinci_func, 75895a7f10eSVladimir Barinov }; 75995a7f10eSVladimir Barinov 7605c3d8a46SHeiko Schocher static const struct of_device_id davinci_i2c_of_match[] = { 7615c3d8a46SHeiko Schocher {.compatible = "ti,davinci-i2c", }, 76235780e86SAlexander Sverdlin {.compatible = "ti,keystone-i2c", }, 7635c3d8a46SHeiko Schocher {}, 7645c3d8a46SHeiko Schocher }; 7655c3d8a46SHeiko Schocher MODULE_DEVICE_TABLE(of, davinci_i2c_of_match); 7665c3d8a46SHeiko Schocher 76795a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev) 76895a7f10eSVladimir Barinov { 76995a7f10eSVladimir Barinov struct davinci_i2c_dev *dev; 77095a7f10eSVladimir Barinov struct i2c_adapter *adap; 7712c6ef04fSGrygorii Strashko struct resource *mem; 7722c6ef04fSGrygorii Strashko int r, irq; 77395a7f10eSVladimir Barinov 7742c6ef04fSGrygorii Strashko irq = platform_get_irq(pdev, 0); 7752c6ef04fSGrygorii Strashko if (irq <= 0) { 7762c6ef04fSGrygorii Strashko if (!irq) 7772c6ef04fSGrygorii Strashko irq = -ENXIO; 7782c6ef04fSGrygorii Strashko if (irq != -EPROBE_DEFER) 7792c6ef04fSGrygorii Strashko dev_err(&pdev->dev, 7802c6ef04fSGrygorii Strashko "can't get irq resource ret=%d\n", irq); 7812c6ef04fSGrygorii Strashko return irq; 78295a7f10eSVladimir Barinov } 78395a7f10eSVladimir Barinov 78485796843SVishwanathrao Badarkhe, Manish dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev), 78585796843SVishwanathrao Badarkhe, Manish GFP_KERNEL); 78695a7f10eSVladimir Barinov if (!dev) { 78785796843SVishwanathrao Badarkhe, Manish dev_err(&pdev->dev, "Memory allocation failed\n"); 78885796843SVishwanathrao Badarkhe, Manish return -ENOMEM; 78995a7f10eSVladimir Barinov } 79095a7f10eSVladimir Barinov 7912e743787STroy Kisky init_completion(&dev->cmd_complete); 79282c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 79382c0de11SChaithrika U S init_completion(&dev->xfr_complete); 79482c0de11SChaithrika U S #endif 795c4df5000SWolfram Sang dev->dev = &pdev->dev; 7962c6ef04fSGrygorii Strashko dev->irq = irq; 7971e2c2390SOlof Johansson dev->pdata = dev_get_platdata(&pdev->dev); 79895a7f10eSVladimir Barinov platform_set_drvdata(pdev, dev); 79995a7f10eSVladimir Barinov 8005c3d8a46SHeiko Schocher if (!dev->pdata && pdev->dev.of_node) { 8015c3d8a46SHeiko Schocher u32 prop; 8025c3d8a46SHeiko Schocher 8035c3d8a46SHeiko Schocher dev->pdata = devm_kzalloc(&pdev->dev, 8045c3d8a46SHeiko Schocher sizeof(struct davinci_i2c_platform_data), GFP_KERNEL); 805c4df5000SWolfram Sang if (!dev->pdata) 806c4df5000SWolfram Sang return -ENOMEM; 807c4df5000SWolfram Sang 8085c3d8a46SHeiko Schocher memcpy(dev->pdata, &davinci_i2c_platform_data_default, 8095c3d8a46SHeiko Schocher sizeof(struct davinci_i2c_platform_data)); 8105c3d8a46SHeiko Schocher if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency", 8115c3d8a46SHeiko Schocher &prop)) 8125c3d8a46SHeiko Schocher dev->pdata->bus_freq = prop / 1000; 8137ef97e9aSGrygorii Strashko 8147ef97e9aSGrygorii Strashko dev->pdata->has_pfunc = 8157ef97e9aSGrygorii Strashko of_property_read_bool(pdev->dev.of_node, 8167ef97e9aSGrygorii Strashko "ti,has-pfunc"); 8175c3d8a46SHeiko Schocher } else if (!dev->pdata) { 8185c3d8a46SHeiko Schocher dev->pdata = &davinci_i2c_platform_data_default; 8195c3d8a46SHeiko Schocher } 8205c3d8a46SHeiko Schocher 82185796843SVishwanathrao Badarkhe, Manish dev->clk = devm_clk_get(&pdev->dev, NULL); 822c4df5000SWolfram Sang if (IS_ERR(dev->clk)) 823cc209ac8SFranklin S Cooper Jr return PTR_ERR(dev->clk); 82495a7f10eSVladimir Barinov 8253cc2d009SWolfram Sang mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 82685796843SVishwanathrao Badarkhe, Manish dev->base = devm_ioremap_resource(&pdev->dev, mem); 82785796843SVishwanathrao Badarkhe, Manish if (IS_ERR(dev->base)) { 82817f88151SFranklin S Cooper Jr return PTR_ERR(dev->base); 82917f88151SFranklin S Cooper Jr } 83017f88151SFranklin S Cooper Jr 83117f88151SFranklin S Cooper Jr pm_runtime_set_autosuspend_delay(dev->dev, 83217f88151SFranklin S Cooper Jr DAVINCI_I2C_PM_TIMEOUT); 83317f88151SFranklin S Cooper Jr pm_runtime_use_autosuspend(dev->dev); 83417f88151SFranklin S Cooper Jr 83517f88151SFranklin S Cooper Jr pm_runtime_enable(dev->dev); 83617f88151SFranklin S Cooper Jr 83717f88151SFranklin S Cooper Jr r = pm_runtime_get_sync(dev->dev); 83817f88151SFranklin S Cooper Jr if (r < 0) { 83917f88151SFranklin S Cooper Jr dev_err(dev->dev, "failed to runtime_get device: %d\n", r); 84017f88151SFranklin S Cooper Jr pm_runtime_put_noidle(dev->dev); 84117f88151SFranklin S Cooper Jr return r; 842c062a251SChaithrika U S } 843c062a251SChaithrika U S 84495a7f10eSVladimir Barinov i2c_davinci_init(dev); 84595a7f10eSVladimir Barinov 84685796843SVishwanathrao Badarkhe, Manish r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0, 84785796843SVishwanathrao Badarkhe, Manish pdev->name, dev); 84895a7f10eSVladimir Barinov if (r) { 84995a7f10eSVladimir Barinov dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); 85095a7f10eSVladimir Barinov goto err_unuse_clocks; 85195a7f10eSVladimir Barinov } 85295a7f10eSVladimir Barinov 85382c0de11SChaithrika U S r = i2c_davinci_cpufreq_register(dev); 85482c0de11SChaithrika U S if (r) { 85582c0de11SChaithrika U S dev_err(&pdev->dev, "failed to register cpufreq\n"); 85685796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks; 85782c0de11SChaithrika U S } 85882c0de11SChaithrika U S 85995a7f10eSVladimir Barinov adap = &dev->adapter; 86095a7f10eSVladimir Barinov i2c_set_adapdata(adap, dev); 86195a7f10eSVladimir Barinov adap->owner = THIS_MODULE; 862adcb82a9SWolfram Sang adap->class = I2C_CLASS_DEPRECATED; 86395a7f10eSVladimir Barinov strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name)); 86495a7f10eSVladimir Barinov adap->algo = &i2c_davinci_algo; 86595a7f10eSVladimir Barinov adap->dev.parent = &pdev->dev; 86698a679caSJean Delvare adap->timeout = DAVINCI_I2C_TIMEOUT; 8675c3d8a46SHeiko Schocher adap->dev.of_node = pdev->dev.of_node; 86895a7f10eSVladimir Barinov 8697ef97e9aSGrygorii Strashko if (dev->pdata->has_pfunc) 8707ef97e9aSGrygorii Strashko adap->bus_recovery_info = &davinci_i2c_scl_recovery_info; 8717ef97e9aSGrygorii Strashko else if (dev->pdata->scl_pin) { 8722e65676fSGrygorii Strashko adap->bus_recovery_info = &davinci_i2c_gpio_recovery_info; 8732e65676fSGrygorii Strashko adap->bus_recovery_info->scl_gpio = dev->pdata->scl_pin; 8742e65676fSGrygorii Strashko adap->bus_recovery_info->sda_gpio = dev->pdata->sda_pin; 8752e65676fSGrygorii Strashko } 8762e65676fSGrygorii Strashko 87795a7f10eSVladimir Barinov adap->nr = pdev->id; 87895a7f10eSVladimir Barinov r = i2c_add_numbered_adapter(adap); 879ea734404SWolfram Sang if (r) 88085796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks; 88195a7f10eSVladimir Barinov 88217f88151SFranklin S Cooper Jr pm_runtime_mark_last_busy(dev->dev); 88317f88151SFranklin S Cooper Jr pm_runtime_put_autosuspend(dev->dev); 88417f88151SFranklin S Cooper Jr 88595a7f10eSVladimir Barinov return 0; 88695a7f10eSVladimir Barinov 88795a7f10eSVladimir Barinov err_unuse_clocks: 88817f88151SFranklin S Cooper Jr pm_runtime_dont_use_autosuspend(dev->dev); 88917f88151SFranklin S Cooper Jr pm_runtime_put_sync(dev->dev); 89017f88151SFranklin S Cooper Jr pm_runtime_disable(dev->dev); 89117f88151SFranklin S Cooper Jr 89295a7f10eSVladimir Barinov return r; 89395a7f10eSVladimir Barinov } 89495a7f10eSVladimir Barinov 89595a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev) 89695a7f10eSVladimir Barinov { 89795a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = platform_get_drvdata(pdev); 89817f88151SFranklin S Cooper Jr int ret; 89995a7f10eSVladimir Barinov 90082c0de11SChaithrika U S i2c_davinci_cpufreq_deregister(dev); 90182c0de11SChaithrika U S 90295a7f10eSVladimir Barinov i2c_del_adapter(&dev->adapter); 90395a7f10eSVladimir Barinov 90417f88151SFranklin S Cooper Jr ret = pm_runtime_get_sync(&pdev->dev); 90517f88151SFranklin S Cooper Jr if (ret < 0) { 90617f88151SFranklin S Cooper Jr pm_runtime_put_noidle(&pdev->dev); 90717f88151SFranklin S Cooper Jr return ret; 90817f88151SFranklin S Cooper Jr } 90995a7f10eSVladimir Barinov 91095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0); 91195a7f10eSVladimir Barinov 91217f88151SFranklin S Cooper Jr pm_runtime_dont_use_autosuspend(dev->dev); 91317f88151SFranklin S Cooper Jr pm_runtime_put_sync(dev->dev); 91417f88151SFranklin S Cooper Jr pm_runtime_disable(dev->dev); 91517f88151SFranklin S Cooper Jr 91695a7f10eSVladimir Barinov return 0; 91795a7f10eSVladimir Barinov } 91895a7f10eSVladimir Barinov 91968f15de9SChaithrika U S #ifdef CONFIG_PM 92068f15de9SChaithrika U S static int davinci_i2c_suspend(struct device *dev) 92168f15de9SChaithrika U S { 9229242e72aSMasahiro Yamada struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev); 92368f15de9SChaithrika U S 92468f15de9SChaithrika U S /* put I2C into reset */ 92568f15de9SChaithrika U S davinci_i2c_reset_ctrl(i2c_dev, 0); 92668f15de9SChaithrika U S 92768f15de9SChaithrika U S return 0; 92868f15de9SChaithrika U S } 92968f15de9SChaithrika U S 93068f15de9SChaithrika U S static int davinci_i2c_resume(struct device *dev) 93168f15de9SChaithrika U S { 9329242e72aSMasahiro Yamada struct davinci_i2c_dev *i2c_dev = dev_get_drvdata(dev); 93368f15de9SChaithrika U S 93468f15de9SChaithrika U S /* take I2C out of reset */ 93568f15de9SChaithrika U S davinci_i2c_reset_ctrl(i2c_dev, 1); 93668f15de9SChaithrika U S 93768f15de9SChaithrika U S return 0; 93868f15de9SChaithrika U S } 93968f15de9SChaithrika U S 94068f15de9SChaithrika U S static const struct dev_pm_ops davinci_i2c_pm = { 94168f15de9SChaithrika U S .suspend = davinci_i2c_suspend, 94268f15de9SChaithrika U S .resume = davinci_i2c_resume, 94317f88151SFranklin S Cooper Jr SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 94417f88151SFranklin S Cooper Jr pm_runtime_force_resume) 94568f15de9SChaithrika U S }; 94668f15de9SChaithrika U S 94768f15de9SChaithrika U S #define davinci_i2c_pm_ops (&davinci_i2c_pm) 94868f15de9SChaithrika U S #else 94968f15de9SChaithrika U S #define davinci_i2c_pm_ops NULL 95068f15de9SChaithrika U S #endif 95168f15de9SChaithrika U S 952add8eda7SKay Sievers /* work with hotplug and coldplug */ 953add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci"); 954add8eda7SKay Sievers 95595a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = { 95695a7f10eSVladimir Barinov .probe = davinci_i2c_probe, 95795a7f10eSVladimir Barinov .remove = davinci_i2c_remove, 95895a7f10eSVladimir Barinov .driver = { 95995a7f10eSVladimir Barinov .name = "i2c_davinci", 96068f15de9SChaithrika U S .pm = davinci_i2c_pm_ops, 9614e905323SSachin Kamat .of_match_table = davinci_i2c_of_match, 96295a7f10eSVladimir Barinov }, 96395a7f10eSVladimir Barinov }; 96495a7f10eSVladimir Barinov 96595a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */ 96695a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void) 96795a7f10eSVladimir Barinov { 96895a7f10eSVladimir Barinov return platform_driver_register(&davinci_i2c_driver); 96995a7f10eSVladimir Barinov } 97095a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver); 97195a7f10eSVladimir Barinov 97295a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void) 97395a7f10eSVladimir Barinov { 97495a7f10eSVladimir Barinov platform_driver_unregister(&davinci_i2c_driver); 97595a7f10eSVladimir Barinov } 97695a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver); 97795a7f10eSVladimir Barinov 97895a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India"); 97995a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter"); 98095a7f10eSVladimir Barinov MODULE_LICENSE("GPL"); 981