195a7f10eSVladimir Barinov /* 295a7f10eSVladimir Barinov * TI DAVINCI I2C adapter driver. 395a7f10eSVladimir Barinov * 495a7f10eSVladimir Barinov * Copyright (C) 2006 Texas Instruments. 595a7f10eSVladimir Barinov * Copyright (C) 2007 MontaVista Software Inc. 695a7f10eSVladimir Barinov * 795a7f10eSVladimir Barinov * Updated by Vinod & Sudhakar Feb 2005 895a7f10eSVladimir Barinov * 995a7f10eSVladimir Barinov * ---------------------------------------------------------------------------- 1095a7f10eSVladimir Barinov * 1195a7f10eSVladimir Barinov * This program is free software; you can redistribute it and/or modify 1295a7f10eSVladimir Barinov * it under the terms of the GNU General Public License as published by 1395a7f10eSVladimir Barinov * the Free Software Foundation; either version 2 of the License, or 1495a7f10eSVladimir Barinov * (at your option) any later version. 1595a7f10eSVladimir Barinov * 1695a7f10eSVladimir Barinov * This program is distributed in the hope that it will be useful, 1795a7f10eSVladimir Barinov * but WITHOUT ANY WARRANTY; without even the implied warranty of 1895a7f10eSVladimir Barinov * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1995a7f10eSVladimir Barinov * GNU General Public License for more details. 2095a7f10eSVladimir Barinov * ---------------------------------------------------------------------------- 2195a7f10eSVladimir Barinov * 2295a7f10eSVladimir Barinov */ 2395a7f10eSVladimir Barinov #include <linux/kernel.h> 2495a7f10eSVladimir Barinov #include <linux/module.h> 2595a7f10eSVladimir Barinov #include <linux/delay.h> 2695a7f10eSVladimir Barinov #include <linux/i2c.h> 2795a7f10eSVladimir Barinov #include <linux/clk.h> 2895a7f10eSVladimir Barinov #include <linux/errno.h> 2995a7f10eSVladimir Barinov #include <linux/sched.h> 3095a7f10eSVladimir Barinov #include <linux/err.h> 3195a7f10eSVladimir Barinov #include <linux/interrupt.h> 3295a7f10eSVladimir Barinov #include <linux/platform_device.h> 3395a7f10eSVladimir Barinov #include <linux/io.h> 345a0e3ad6STejun Heo #include <linux/slab.h> 3582c0de11SChaithrika U S #include <linux/cpufreq.h> 368574faf9SPhilby John #include <linux/gpio.h> 375c3d8a46SHeiko Schocher #include <linux/of_device.h> 38ec2a0833SArnd Bergmann #include <linux/platform_data/i2c-davinci.h> 3995a7f10eSVladimir Barinov 4095a7f10eSVladimir Barinov /* ----- global defines ----------------------------------------------- */ 4195a7f10eSVladimir Barinov 4295a7f10eSVladimir Barinov #define DAVINCI_I2C_TIMEOUT (1*HZ) 438574faf9SPhilby John #define DAVINCI_I2C_MAX_TRIES 2 4495a7f10eSVladimir Barinov #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \ 4595a7f10eSVladimir Barinov DAVINCI_I2C_IMR_SCD | \ 4695a7f10eSVladimir Barinov DAVINCI_I2C_IMR_ARDY | \ 4795a7f10eSVladimir Barinov DAVINCI_I2C_IMR_NACK | \ 4895a7f10eSVladimir Barinov DAVINCI_I2C_IMR_AL) 4995a7f10eSVladimir Barinov 5095a7f10eSVladimir Barinov #define DAVINCI_I2C_OAR_REG 0x00 5195a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_REG 0x04 5295a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_REG 0x08 5395a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKL_REG 0x0c 5495a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKH_REG 0x10 5595a7f10eSVladimir Barinov #define DAVINCI_I2C_CNT_REG 0x14 5695a7f10eSVladimir Barinov #define DAVINCI_I2C_DRR_REG 0x18 5795a7f10eSVladimir Barinov #define DAVINCI_I2C_SAR_REG 0x1c 5895a7f10eSVladimir Barinov #define DAVINCI_I2C_DXR_REG 0x20 5995a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_REG 0x24 6095a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_REG 0x28 6195a7f10eSVladimir Barinov #define DAVINCI_I2C_EMDR_REG 0x2c 6295a7f10eSVladimir Barinov #define DAVINCI_I2C_PSC_REG 0x30 6395a7f10eSVladimir Barinov 6495a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AAS 0x07 6595a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_SCD 0x06 6695a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_XRDY 0x05 6795a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_RDR 0x04 6895a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_ARDY 0x03 6995a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_NACK 0x02 7095a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AL 0x01 7195a7f10eSVladimir Barinov 72c062a251SChaithrika U S #define DAVINCI_I2C_STR_BB BIT(12) 73c062a251SChaithrika U S #define DAVINCI_I2C_STR_RSFULL BIT(11) 74c062a251SChaithrika U S #define DAVINCI_I2C_STR_SCD BIT(5) 75c062a251SChaithrika U S #define DAVINCI_I2C_STR_ARDY BIT(2) 76c062a251SChaithrika U S #define DAVINCI_I2C_STR_NACK BIT(1) 77c062a251SChaithrika U S #define DAVINCI_I2C_STR_AL BIT(0) 7895a7f10eSVladimir Barinov 79c062a251SChaithrika U S #define DAVINCI_I2C_MDR_NACK BIT(15) 80c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STT BIT(13) 81c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STP BIT(11) 82c062a251SChaithrika U S #define DAVINCI_I2C_MDR_MST BIT(10) 83c062a251SChaithrika U S #define DAVINCI_I2C_MDR_TRX BIT(9) 84c062a251SChaithrika U S #define DAVINCI_I2C_MDR_XA BIT(8) 85c062a251SChaithrika U S #define DAVINCI_I2C_MDR_RM BIT(7) 86c062a251SChaithrika U S #define DAVINCI_I2C_MDR_IRS BIT(5) 8795a7f10eSVladimir Barinov 88c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AAS BIT(6) 89c062a251SChaithrika U S #define DAVINCI_I2C_IMR_SCD BIT(5) 90c062a251SChaithrika U S #define DAVINCI_I2C_IMR_XRDY BIT(4) 91c062a251SChaithrika U S #define DAVINCI_I2C_IMR_RRDY BIT(3) 92c062a251SChaithrika U S #define DAVINCI_I2C_IMR_ARDY BIT(2) 93c062a251SChaithrika U S #define DAVINCI_I2C_IMR_NACK BIT(1) 94c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AL BIT(0) 9595a7f10eSVladimir Barinov 9695a7f10eSVladimir Barinov struct davinci_i2c_dev { 9795a7f10eSVladimir Barinov struct device *dev; 9895a7f10eSVladimir Barinov void __iomem *base; 9995a7f10eSVladimir Barinov struct completion cmd_complete; 10095a7f10eSVladimir Barinov struct clk *clk; 10195a7f10eSVladimir Barinov int cmd_err; 10295a7f10eSVladimir Barinov u8 *buf; 10395a7f10eSVladimir Barinov size_t buf_len; 10495a7f10eSVladimir Barinov int irq; 105c6c7c729SDirk Behme int stop; 1065a0d5f5fSTroy Kisky u8 terminate; 10795a7f10eSVladimir Barinov struct i2c_adapter adapter; 10882c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 10982c0de11SChaithrika U S struct completion xfr_complete; 11082c0de11SChaithrika U S struct notifier_block freq_transition; 11182c0de11SChaithrika U S #endif 1125c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata; 11395a7f10eSVladimir Barinov }; 11495a7f10eSVladimir Barinov 11595a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */ 11695a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = { 11795a7f10eSVladimir Barinov .bus_freq = 100, 11895a7f10eSVladimir Barinov .bus_delay = 0, 11995a7f10eSVladimir Barinov }; 12095a7f10eSVladimir Barinov 12195a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev, 12295a7f10eSVladimir Barinov int reg, u16 val) 12395a7f10eSVladimir Barinov { 124a238dcfaSTaras Kondratiuk writew_relaxed(val, i2c_dev->base + reg); 12595a7f10eSVladimir Barinov } 12695a7f10eSVladimir Barinov 12795a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg) 12895a7f10eSVladimir Barinov { 129a238dcfaSTaras Kondratiuk return readw_relaxed(i2c_dev->base + reg); 13095a7f10eSVladimir Barinov } 13195a7f10eSVladimir Barinov 1328574faf9SPhilby John /* Generate a pulse on the i2c clock pin. */ 133adf68acfSWolfram Sang static void davinci_i2c_clock_pulse(unsigned int scl_pin) 1348574faf9SPhilby John { 1358574faf9SPhilby John u16 i; 1368574faf9SPhilby John 1378574faf9SPhilby John if (scl_pin) { 1388574faf9SPhilby John /* Send high and low on the SCL line */ 1398574faf9SPhilby John for (i = 0; i < 9; i++) { 1408574faf9SPhilby John gpio_set_value(scl_pin, 0); 1418574faf9SPhilby John udelay(20); 1428574faf9SPhilby John gpio_set_value(scl_pin, 1); 1438574faf9SPhilby John udelay(20); 1448574faf9SPhilby John } 1458574faf9SPhilby John } 1468574faf9SPhilby John } 1478574faf9SPhilby John 1488574faf9SPhilby John /* This routine does i2c bus recovery as specified in the 1498574faf9SPhilby John * i2c protocol Rev. 03 section 3.16 titled "Bus clear" 1508574faf9SPhilby John */ 151adf68acfSWolfram Sang static void davinci_i2c_recover_bus(struct davinci_i2c_dev *dev) 1528574faf9SPhilby John { 1538574faf9SPhilby John u32 flag = 0; 1545c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 1558574faf9SPhilby John 1568574faf9SPhilby John dev_err(dev->dev, "initiating i2c bus recovery\n"); 1578574faf9SPhilby John /* Send NACK to the slave */ 1588574faf9SPhilby John flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 1598574faf9SPhilby John flag |= DAVINCI_I2C_MDR_NACK; 1608574faf9SPhilby John /* write the data into mode register */ 1618574faf9SPhilby John davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 162adf68acfSWolfram Sang davinci_i2c_clock_pulse(pdata->scl_pin); 1638574faf9SPhilby John /* Send STOP */ 1648574faf9SPhilby John flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 1658574faf9SPhilby John flag |= DAVINCI_I2C_MDR_STP; 1668574faf9SPhilby John davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 1678574faf9SPhilby John } 1688574faf9SPhilby John 1695ae5b113SChaithrika U S static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev, 1705ae5b113SChaithrika U S int val) 1715ae5b113SChaithrika U S { 1725ae5b113SChaithrika U S u16 w; 1735ae5b113SChaithrika U S 1745ae5b113SChaithrika U S w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG); 1755ae5b113SChaithrika U S if (!val) /* put I2C into reset */ 1765ae5b113SChaithrika U S w &= ~DAVINCI_I2C_MDR_IRS; 1775ae5b113SChaithrika U S else /* take I2C out of reset */ 1785ae5b113SChaithrika U S w |= DAVINCI_I2C_MDR_IRS; 1795ae5b113SChaithrika U S 1805ae5b113SChaithrika U S davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w); 1815ae5b113SChaithrika U S } 1825ae5b113SChaithrika U S 1835ae5b113SChaithrika U S static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev) 18495a7f10eSVladimir Barinov { 1855c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 18695a7f10eSVladimir Barinov u16 psc; 18795a7f10eSVladimir Barinov u32 clk; 188cc99ff70STroy Kisky u32 d; 18995a7f10eSVladimir Barinov u32 clkh; 19095a7f10eSVladimir Barinov u32 clkl; 19195a7f10eSVladimir Barinov u32 input_clock = clk_get_rate(dev->clk); 19295a7f10eSVladimir Barinov 19395a7f10eSVladimir Barinov /* NOTE: I2C Clock divider programming info 19495a7f10eSVladimir Barinov * As per I2C specs the following formulas provide prescaler 19595a7f10eSVladimir Barinov * and low/high divider values 19695a7f10eSVladimir Barinov * input clk --> PSC Div -----------> ICCL/H Div --> output clock 19795a7f10eSVladimir Barinov * module clk 19895a7f10eSVladimir Barinov * 19995a7f10eSVladimir Barinov * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ] 20095a7f10eSVladimir Barinov * 20195a7f10eSVladimir Barinov * Thus, 20295a7f10eSVladimir Barinov * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d; 20395a7f10eSVladimir Barinov * 20495a7f10eSVladimir Barinov * where if PSC == 0, d = 7, 20595a7f10eSVladimir Barinov * if PSC == 1, d = 6 20695a7f10eSVladimir Barinov * if PSC > 1 , d = 5 20795a7f10eSVladimir Barinov */ 20895a7f10eSVladimir Barinov 209cc99ff70STroy Kisky /* get minimum of 7 MHz clock, but max of 12 MHz */ 210cc99ff70STroy Kisky psc = (input_clock / 7000000) - 1; 211cc99ff70STroy Kisky if ((input_clock / (psc + 1)) > 12000000) 212cc99ff70STroy Kisky psc++; /* better to run under spec than over */ 213cc99ff70STroy Kisky d = (psc >= 2) ? 5 : 7 - psc; 21495a7f10eSVladimir Barinov 215cc99ff70STroy Kisky clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1); 216cc99ff70STroy Kisky clkh = clk >> 1; 21795a7f10eSVladimir Barinov clkl = clk - clkh; 21895a7f10eSVladimir Barinov 21995a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc); 22095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh); 22195a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl); 22295a7f10eSVladimir Barinov 2235ae5b113SChaithrika U S dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk); 2245ae5b113SChaithrika U S } 2255ae5b113SChaithrika U S 2265ae5b113SChaithrika U S /* 2275ae5b113SChaithrika U S * This function configures I2C and brings I2C out of reset. 2285ae5b113SChaithrika U S * This function is called during I2C init function. This function 2295ae5b113SChaithrika U S * also gets called if I2C encounters any errors. 2305ae5b113SChaithrika U S */ 2315ae5b113SChaithrika U S static int i2c_davinci_init(struct davinci_i2c_dev *dev) 2325ae5b113SChaithrika U S { 2335c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 2345ae5b113SChaithrika U S 2355ae5b113SChaithrika U S /* put I2C into reset */ 2365ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 0); 2375ae5b113SChaithrika U S 2385ae5b113SChaithrika U S /* compute clock dividers */ 2395ae5b113SChaithrika U S i2c_davinci_calc_clk_dividers(dev); 2405ae5b113SChaithrika U S 2417605fa3bSDavid Brownell /* Respond at reserved "SMBus Host" slave address" (and zero); 2427605fa3bSDavid Brownell * we seem to have no option to not respond... 2437605fa3bSDavid Brownell */ 2447605fa3bSDavid Brownell davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08); 2457605fa3bSDavid Brownell 24695a7f10eSVladimir Barinov dev_dbg(dev->dev, "PSC = %d\n", 24795a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG)); 24895a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKL = %d\n", 24995a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG)); 25095a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKH = %d\n", 25195a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG)); 252cc99ff70STroy Kisky dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n", 253cc99ff70STroy Kisky pdata->bus_freq, pdata->bus_delay); 25495a7f10eSVladimir Barinov 2555c3d8a46SHeiko Schocher 25695a7f10eSVladimir Barinov /* Take the I2C module out of reset: */ 2575ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 1); 25895a7f10eSVladimir Barinov 25995a7f10eSVladimir Barinov /* Enable interrupts */ 26095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL); 26195a7f10eSVladimir Barinov 26295a7f10eSVladimir Barinov return 0; 26395a7f10eSVladimir Barinov } 26495a7f10eSVladimir Barinov 26595a7f10eSVladimir Barinov /* 26695a7f10eSVladimir Barinov * Waiting for bus not busy 26795a7f10eSVladimir Barinov */ 26895a7f10eSVladimir Barinov static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev, 26995a7f10eSVladimir Barinov char allow_sleep) 27095a7f10eSVladimir Barinov { 27195a7f10eSVladimir Barinov unsigned long timeout; 2728574faf9SPhilby John static u16 to_cnt; 27395a7f10eSVladimir Barinov 27498a679caSJean Delvare timeout = jiffies + dev->adapter.timeout; 27595a7f10eSVladimir Barinov while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) 27695a7f10eSVladimir Barinov & DAVINCI_I2C_STR_BB) { 2778574faf9SPhilby John if (to_cnt <= DAVINCI_I2C_MAX_TRIES) { 27895a7f10eSVladimir Barinov if (time_after(jiffies, timeout)) { 27995a7f10eSVladimir Barinov dev_warn(dev->dev, 28095a7f10eSVladimir Barinov "timeout waiting for bus ready\n"); 2818574faf9SPhilby John to_cnt++; 28295a7f10eSVladimir Barinov return -ETIMEDOUT; 2838574faf9SPhilby John } else { 2848574faf9SPhilby John to_cnt = 0; 285adf68acfSWolfram Sang davinci_i2c_recover_bus(dev); 2868574faf9SPhilby John i2c_davinci_init(dev); 2878574faf9SPhilby John } 28895a7f10eSVladimir Barinov } 28995a7f10eSVladimir Barinov if (allow_sleep) 29095a7f10eSVladimir Barinov schedule_timeout(1); 29195a7f10eSVladimir Barinov } 29295a7f10eSVladimir Barinov 29395a7f10eSVladimir Barinov return 0; 29495a7f10eSVladimir Barinov } 29595a7f10eSVladimir Barinov 29695a7f10eSVladimir Barinov /* 29795a7f10eSVladimir Barinov * Low level master read/write transaction. This function is called 29895a7f10eSVladimir Barinov * from i2c_davinci_xfer. 29995a7f10eSVladimir Barinov */ 30095a7f10eSVladimir Barinov static int 30195a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop) 30295a7f10eSVladimir Barinov { 30395a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3045c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 30595a7f10eSVladimir Barinov u32 flag; 30695a7f10eSVladimir Barinov u16 w; 307*d9e1f441SNicholas Mc Guire unsigned long time_left; 30895a7f10eSVladimir Barinov 30995a7f10eSVladimir Barinov /* Introduce a delay, required for some boards (e.g Davinci EVM) */ 31095a7f10eSVladimir Barinov if (pdata->bus_delay) 31195a7f10eSVladimir Barinov udelay(pdata->bus_delay); 31295a7f10eSVladimir Barinov 31395a7f10eSVladimir Barinov /* set the slave address */ 31495a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr); 31595a7f10eSVladimir Barinov 31695a7f10eSVladimir Barinov dev->buf = msg->buf; 31795a7f10eSVladimir Barinov dev->buf_len = msg->len; 318c6c7c729SDirk Behme dev->stop = stop; 31995a7f10eSVladimir Barinov 32095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len); 32195a7f10eSVladimir Barinov 32216735d02SWolfram Sang reinit_completion(&dev->cmd_complete); 32395a7f10eSVladimir Barinov dev->cmd_err = 0; 32495a7f10eSVladimir Barinov 325c5b4afecSJon Povey /* Take I2C out of reset and configure it as master */ 326c5b4afecSJon Povey flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST; 32795a7f10eSVladimir Barinov 32895a7f10eSVladimir Barinov /* if the slave address is ten bit address, enable XA bit */ 32995a7f10eSVladimir Barinov if (msg->flags & I2C_M_TEN) 33095a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_XA; 33195a7f10eSVladimir Barinov if (!(msg->flags & I2C_M_RD)) 33295a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_TRX; 333c5b4afecSJon Povey if (msg->len == 0) 334c6c7c729SDirk Behme flag |= DAVINCI_I2C_MDR_RM; 33595a7f10eSVladimir Barinov 33695a7f10eSVladimir Barinov /* Enable receive or transmit interrupts */ 33795a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG); 33895a7f10eSVladimir Barinov if (msg->flags & I2C_M_RD) 339c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_RRDY; 34095a7f10eSVladimir Barinov else 341c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_XRDY; 34295a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w); 34395a7f10eSVladimir Barinov 3445a0d5f5fSTroy Kisky dev->terminate = 0; 345c6c7c729SDirk Behme 346c6c7c729SDirk Behme /* 347c5b4afecSJon Povey * Write mode register first as needed for correct behaviour 348c5b4afecSJon Povey * on OMAP-L138, but don't set STT yet to avoid a race with XRDY 34925985edcSLucas De Marchi * occurring before we have loaded DXR 350c5b4afecSJon Povey */ 351c5b4afecSJon Povey davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 352c5b4afecSJon Povey 353c5b4afecSJon Povey /* 354c6c7c729SDirk Behme * First byte should be set here, not after interrupt, 355c6c7c729SDirk Behme * because transmit-data-ready interrupt can come before 356c6c7c729SDirk Behme * NACK-interrupt during sending of previous message and 357c6c7c729SDirk Behme * ICDXR may have wrong data 358c5b4afecSJon Povey * It also saves us one interrupt, slightly faster 359c6c7c729SDirk Behme */ 360c6c7c729SDirk Behme if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) { 361c6c7c729SDirk Behme davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++); 362c6c7c729SDirk Behme dev->buf_len--; 363c6c7c729SDirk Behme } 364c6c7c729SDirk Behme 365c5b4afecSJon Povey /* Set STT to begin transmit now DXR is loaded */ 366c5b4afecSJon Povey flag |= DAVINCI_I2C_MDR_STT; 367c5b4afecSJon Povey if (stop && msg->len != 0) 368c5b4afecSJon Povey flag |= DAVINCI_I2C_MDR_STP; 3694bba0fd8SJon Povey davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 3704bba0fd8SJon Povey 371*d9e1f441SNicholas Mc Guire time_left = wait_for_completion_timeout(&dev->cmd_complete, 372*d9e1f441SNicholas Mc Guire dev->adapter.timeout); 373*d9e1f441SNicholas Mc Guire if (!time_left) { 37495a7f10eSVladimir Barinov dev_err(dev->dev, "controller timed out\n"); 375adf68acfSWolfram Sang davinci_i2c_recover_bus(dev); 37695a7f10eSVladimir Barinov i2c_davinci_init(dev); 3775a0d5f5fSTroy Kisky dev->buf_len = 0; 37895a7f10eSVladimir Barinov return -ETIMEDOUT; 37995a7f10eSVladimir Barinov } 3805a0d5f5fSTroy Kisky if (dev->buf_len) { 3815a0d5f5fSTroy Kisky /* This should be 0 if all bytes were transferred 3825a0d5f5fSTroy Kisky * or dev->cmd_err denotes an error. 3835a0d5f5fSTroy Kisky */ 3845a0d5f5fSTroy Kisky dev_err(dev->dev, "abnormal termination buf_len=%i\n", 3855a0d5f5fSTroy Kisky dev->buf_len); 3865a0d5f5fSTroy Kisky dev->terminate = 1; 3875a0d5f5fSTroy Kisky wmb(); 3885a0d5f5fSTroy Kisky dev->buf_len = 0; 389*d9e1f441SNicholas Mc Guire return -EREMOTEIO; 3905a0d5f5fSTroy Kisky } 39195a7f10eSVladimir Barinov 39295a7f10eSVladimir Barinov /* no error */ 39395a7f10eSVladimir Barinov if (likely(!dev->cmd_err)) 39495a7f10eSVladimir Barinov return msg->len; 39595a7f10eSVladimir Barinov 39695a7f10eSVladimir Barinov /* We have an error */ 39795a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_AL) { 39895a7f10eSVladimir Barinov i2c_davinci_init(dev); 39995a7f10eSVladimir Barinov return -EIO; 40095a7f10eSVladimir Barinov } 40195a7f10eSVladimir Barinov 40295a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_NACK) { 40395a7f10eSVladimir Barinov if (msg->flags & I2C_M_IGNORE_NAK) 40495a7f10eSVladimir Barinov return msg->len; 40595a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 406c062a251SChaithrika U S w |= DAVINCI_I2C_MDR_STP; 40795a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 40895a7f10eSVladimir Barinov return -EREMOTEIO; 40995a7f10eSVladimir Barinov } 41095a7f10eSVladimir Barinov return -EIO; 41195a7f10eSVladimir Barinov } 41295a7f10eSVladimir Barinov 41395a7f10eSVladimir Barinov /* 41495a7f10eSVladimir Barinov * Prepare controller for a transaction and call i2c_davinci_xfer_msg 41595a7f10eSVladimir Barinov */ 41695a7f10eSVladimir Barinov static int 41795a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) 41895a7f10eSVladimir Barinov { 41995a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 42095a7f10eSVladimir Barinov int i; 42195a7f10eSVladimir Barinov int ret; 42295a7f10eSVladimir Barinov 42308882d20SHarvey Harrison dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); 42495a7f10eSVladimir Barinov 42595a7f10eSVladimir Barinov ret = i2c_davinci_wait_bus_not_busy(dev, 1); 42695a7f10eSVladimir Barinov if (ret < 0) { 42795a7f10eSVladimir Barinov dev_warn(dev->dev, "timeout waiting for bus ready\n"); 42895a7f10eSVladimir Barinov return ret; 42995a7f10eSVladimir Barinov } 43095a7f10eSVladimir Barinov 43195a7f10eSVladimir Barinov for (i = 0; i < num; i++) { 43295a7f10eSVladimir Barinov ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1))); 433d868caa1STroy Kisky dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num, 434d868caa1STroy Kisky ret); 43595a7f10eSVladimir Barinov if (ret < 0) 43695a7f10eSVladimir Barinov return ret; 43795a7f10eSVladimir Barinov } 43882c0de11SChaithrika U S 43982c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 44082c0de11SChaithrika U S complete(&dev->xfr_complete); 44182c0de11SChaithrika U S #endif 44282c0de11SChaithrika U S 44395a7f10eSVladimir Barinov return num; 44495a7f10eSVladimir Barinov } 44595a7f10eSVladimir Barinov 44695a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap) 44795a7f10eSVladimir Barinov { 448c6c7c729SDirk Behme return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 44995a7f10eSVladimir Barinov } 45095a7f10eSVladimir Barinov 4515a0d5f5fSTroy Kisky static void terminate_read(struct davinci_i2c_dev *dev) 4525a0d5f5fSTroy Kisky { 4535a0d5f5fSTroy Kisky u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 4545a0d5f5fSTroy Kisky w |= DAVINCI_I2C_MDR_NACK; 4555a0d5f5fSTroy Kisky davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 4565a0d5f5fSTroy Kisky 4575a0d5f5fSTroy Kisky /* Throw away data */ 4585a0d5f5fSTroy Kisky davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG); 4595a0d5f5fSTroy Kisky if (!dev->terminate) 4605a0d5f5fSTroy Kisky dev_err(dev->dev, "RDR IRQ while no data requested\n"); 4615a0d5f5fSTroy Kisky } 4625a0d5f5fSTroy Kisky static void terminate_write(struct davinci_i2c_dev *dev) 4635a0d5f5fSTroy Kisky { 4645a0d5f5fSTroy Kisky u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 4655a0d5f5fSTroy Kisky w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP; 4665a0d5f5fSTroy Kisky davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 4675a0d5f5fSTroy Kisky 4685a0d5f5fSTroy Kisky if (!dev->terminate) 4697605fa3bSDavid Brownell dev_dbg(dev->dev, "TDR IRQ while no data to send\n"); 4705a0d5f5fSTroy Kisky } 4715a0d5f5fSTroy Kisky 47295a7f10eSVladimir Barinov /* 47395a7f10eSVladimir Barinov * Interrupt service routine. This gets called whenever an I2C interrupt 47495a7f10eSVladimir Barinov * occurs. 47595a7f10eSVladimir Barinov */ 47695a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id) 47795a7f10eSVladimir Barinov { 47895a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = dev_id; 47995a7f10eSVladimir Barinov u32 stat; 48095a7f10eSVladimir Barinov int count = 0; 48195a7f10eSVladimir Barinov u16 w; 48295a7f10eSVladimir Barinov 48395a7f10eSVladimir Barinov while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) { 48408882d20SHarvey Harrison dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat); 48595a7f10eSVladimir Barinov if (count++ == 100) { 48695a7f10eSVladimir Barinov dev_warn(dev->dev, "Too much work in one IRQ\n"); 48795a7f10eSVladimir Barinov break; 48895a7f10eSVladimir Barinov } 48995a7f10eSVladimir Barinov 49095a7f10eSVladimir Barinov switch (stat) { 49195a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_AL: 4925a0d5f5fSTroy Kisky /* Arbitration lost, must retry */ 49395a7f10eSVladimir Barinov dev->cmd_err |= DAVINCI_I2C_STR_AL; 4945a0d5f5fSTroy Kisky dev->buf_len = 0; 49595a7f10eSVladimir Barinov complete(&dev->cmd_complete); 49695a7f10eSVladimir Barinov break; 49795a7f10eSVladimir Barinov 49895a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_NACK: 49995a7f10eSVladimir Barinov dev->cmd_err |= DAVINCI_I2C_STR_NACK; 5005a0d5f5fSTroy Kisky dev->buf_len = 0; 50195a7f10eSVladimir Barinov complete(&dev->cmd_complete); 50295a7f10eSVladimir Barinov break; 50395a7f10eSVladimir Barinov 50495a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_ARDY: 505b73a9aecSTroy Kisky davinci_i2c_write_reg(dev, 506b73a9aecSTroy Kisky DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY); 507c6c7c729SDirk Behme if (((dev->buf_len == 0) && (dev->stop != 0)) || 508c6c7c729SDirk Behme (dev->cmd_err & DAVINCI_I2C_STR_NACK)) { 509c6c7c729SDirk Behme w = davinci_i2c_read_reg(dev, 510c6c7c729SDirk Behme DAVINCI_I2C_MDR_REG); 511c6c7c729SDirk Behme w |= DAVINCI_I2C_MDR_STP; 512c6c7c729SDirk Behme davinci_i2c_write_reg(dev, 513c6c7c729SDirk Behme DAVINCI_I2C_MDR_REG, w); 514c6c7c729SDirk Behme } 51595a7f10eSVladimir Barinov complete(&dev->cmd_complete); 51695a7f10eSVladimir Barinov break; 51795a7f10eSVladimir Barinov 51895a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_RDR: 51995a7f10eSVladimir Barinov if (dev->buf_len) { 52095a7f10eSVladimir Barinov *dev->buf++ = 52195a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, 52295a7f10eSVladimir Barinov DAVINCI_I2C_DRR_REG); 52395a7f10eSVladimir Barinov dev->buf_len--; 52495a7f10eSVladimir Barinov if (dev->buf_len) 52595a7f10eSVladimir Barinov continue; 52695a7f10eSVladimir Barinov 52795a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, 52895a7f10eSVladimir Barinov DAVINCI_I2C_STR_REG, 529b73a9aecSTroy Kisky DAVINCI_I2C_IMR_RRDY); 5305a0d5f5fSTroy Kisky } else { 5315a0d5f5fSTroy Kisky /* signal can terminate transfer */ 5325a0d5f5fSTroy Kisky terminate_read(dev); 5335a0d5f5fSTroy Kisky } 53495a7f10eSVladimir Barinov break; 53595a7f10eSVladimir Barinov 53695a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_XRDY: 53795a7f10eSVladimir Barinov if (dev->buf_len) { 53895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, 53995a7f10eSVladimir Barinov *dev->buf++); 54095a7f10eSVladimir Barinov dev->buf_len--; 54195a7f10eSVladimir Barinov if (dev->buf_len) 54295a7f10eSVladimir Barinov continue; 54395a7f10eSVladimir Barinov 54495a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, 54595a7f10eSVladimir Barinov DAVINCI_I2C_IMR_REG); 546c062a251SChaithrika U S w &= ~DAVINCI_I2C_IMR_XRDY; 54795a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, 54895a7f10eSVladimir Barinov DAVINCI_I2C_IMR_REG, 54995a7f10eSVladimir Barinov w); 5505a0d5f5fSTroy Kisky } else { 5515a0d5f5fSTroy Kisky /* signal can terminate transfer */ 5525a0d5f5fSTroy Kisky terminate_write(dev); 5535a0d5f5fSTroy Kisky } 55495a7f10eSVladimir Barinov break; 55595a7f10eSVladimir Barinov 55695a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_SCD: 557b73a9aecSTroy Kisky davinci_i2c_write_reg(dev, 558b73a9aecSTroy Kisky DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD); 55995a7f10eSVladimir Barinov complete(&dev->cmd_complete); 56095a7f10eSVladimir Barinov break; 56195a7f10eSVladimir Barinov 56295a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_AAS: 5637605fa3bSDavid Brownell dev_dbg(dev->dev, "Address as slave interrupt\n"); 5647605fa3bSDavid Brownell break; 5657605fa3bSDavid Brownell 5667605fa3bSDavid Brownell default: 5677605fa3bSDavid Brownell dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat); 5687605fa3bSDavid Brownell break; 5697605fa3bSDavid Brownell } 5707605fa3bSDavid Brownell } 57195a7f10eSVladimir Barinov 57295a7f10eSVladimir Barinov return count ? IRQ_HANDLED : IRQ_NONE; 57395a7f10eSVladimir Barinov } 57495a7f10eSVladimir Barinov 57582c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 57682c0de11SChaithrika U S static int i2c_davinci_cpufreq_transition(struct notifier_block *nb, 57782c0de11SChaithrika U S unsigned long val, void *data) 57882c0de11SChaithrika U S { 57982c0de11SChaithrika U S struct davinci_i2c_dev *dev; 58082c0de11SChaithrika U S 58182c0de11SChaithrika U S dev = container_of(nb, struct davinci_i2c_dev, freq_transition); 58282c0de11SChaithrika U S if (val == CPUFREQ_PRECHANGE) { 58382c0de11SChaithrika U S wait_for_completion(&dev->xfr_complete); 58482c0de11SChaithrika U S davinci_i2c_reset_ctrl(dev, 0); 58582c0de11SChaithrika U S } else if (val == CPUFREQ_POSTCHANGE) { 58682c0de11SChaithrika U S i2c_davinci_calc_clk_dividers(dev); 58782c0de11SChaithrika U S davinci_i2c_reset_ctrl(dev, 1); 58882c0de11SChaithrika U S } 58982c0de11SChaithrika U S 59082c0de11SChaithrika U S return 0; 59182c0de11SChaithrika U S } 59282c0de11SChaithrika U S 59382c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev) 59482c0de11SChaithrika U S { 59582c0de11SChaithrika U S dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition; 59682c0de11SChaithrika U S 59782c0de11SChaithrika U S return cpufreq_register_notifier(&dev->freq_transition, 59882c0de11SChaithrika U S CPUFREQ_TRANSITION_NOTIFIER); 59982c0de11SChaithrika U S } 60082c0de11SChaithrika U S 60182c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev) 60282c0de11SChaithrika U S { 60382c0de11SChaithrika U S cpufreq_unregister_notifier(&dev->freq_transition, 60482c0de11SChaithrika U S CPUFREQ_TRANSITION_NOTIFIER); 60582c0de11SChaithrika U S } 60682c0de11SChaithrika U S #else 60782c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev) 60882c0de11SChaithrika U S { 60982c0de11SChaithrika U S return 0; 61082c0de11SChaithrika U S } 61182c0de11SChaithrika U S 61282c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev) 61382c0de11SChaithrika U S { 61482c0de11SChaithrika U S } 61582c0de11SChaithrika U S #endif 61682c0de11SChaithrika U S 61795a7f10eSVladimir Barinov static struct i2c_algorithm i2c_davinci_algo = { 61895a7f10eSVladimir Barinov .master_xfer = i2c_davinci_xfer, 61995a7f10eSVladimir Barinov .functionality = i2c_davinci_func, 62095a7f10eSVladimir Barinov }; 62195a7f10eSVladimir Barinov 6225c3d8a46SHeiko Schocher static const struct of_device_id davinci_i2c_of_match[] = { 6235c3d8a46SHeiko Schocher {.compatible = "ti,davinci-i2c", }, 6245c3d8a46SHeiko Schocher {}, 6255c3d8a46SHeiko Schocher }; 6265c3d8a46SHeiko Schocher MODULE_DEVICE_TABLE(of, davinci_i2c_of_match); 6275c3d8a46SHeiko Schocher 62895a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev) 62995a7f10eSVladimir Barinov { 63095a7f10eSVladimir Barinov struct davinci_i2c_dev *dev; 63195a7f10eSVladimir Barinov struct i2c_adapter *adap; 6322c6ef04fSGrygorii Strashko struct resource *mem; 6332c6ef04fSGrygorii Strashko int r, irq; 63495a7f10eSVladimir Barinov 6352c6ef04fSGrygorii Strashko irq = platform_get_irq(pdev, 0); 6362c6ef04fSGrygorii Strashko if (irq <= 0) { 6372c6ef04fSGrygorii Strashko if (!irq) 6382c6ef04fSGrygorii Strashko irq = -ENXIO; 6392c6ef04fSGrygorii Strashko if (irq != -EPROBE_DEFER) 6402c6ef04fSGrygorii Strashko dev_err(&pdev->dev, 6412c6ef04fSGrygorii Strashko "can't get irq resource ret=%d\n", irq); 6422c6ef04fSGrygorii Strashko return irq; 64395a7f10eSVladimir Barinov } 64495a7f10eSVladimir Barinov 64585796843SVishwanathrao Badarkhe, Manish dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev), 64685796843SVishwanathrao Badarkhe, Manish GFP_KERNEL); 64795a7f10eSVladimir Barinov if (!dev) { 64885796843SVishwanathrao Badarkhe, Manish dev_err(&pdev->dev, "Memory allocation failed\n"); 64985796843SVishwanathrao Badarkhe, Manish return -ENOMEM; 65095a7f10eSVladimir Barinov } 65195a7f10eSVladimir Barinov 6522e743787STroy Kisky init_completion(&dev->cmd_complete); 65382c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 65482c0de11SChaithrika U S init_completion(&dev->xfr_complete); 65582c0de11SChaithrika U S #endif 656c4df5000SWolfram Sang dev->dev = &pdev->dev; 6572c6ef04fSGrygorii Strashko dev->irq = irq; 6581e2c2390SOlof Johansson dev->pdata = dev_get_platdata(&pdev->dev); 65995a7f10eSVladimir Barinov platform_set_drvdata(pdev, dev); 66095a7f10eSVladimir Barinov 6615c3d8a46SHeiko Schocher if (!dev->pdata && pdev->dev.of_node) { 6625c3d8a46SHeiko Schocher u32 prop; 6635c3d8a46SHeiko Schocher 6645c3d8a46SHeiko Schocher dev->pdata = devm_kzalloc(&pdev->dev, 6655c3d8a46SHeiko Schocher sizeof(struct davinci_i2c_platform_data), GFP_KERNEL); 666c4df5000SWolfram Sang if (!dev->pdata) 667c4df5000SWolfram Sang return -ENOMEM; 668c4df5000SWolfram Sang 6695c3d8a46SHeiko Schocher memcpy(dev->pdata, &davinci_i2c_platform_data_default, 6705c3d8a46SHeiko Schocher sizeof(struct davinci_i2c_platform_data)); 6715c3d8a46SHeiko Schocher if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency", 6725c3d8a46SHeiko Schocher &prop)) 6735c3d8a46SHeiko Schocher dev->pdata->bus_freq = prop / 1000; 6745c3d8a46SHeiko Schocher } else if (!dev->pdata) { 6755c3d8a46SHeiko Schocher dev->pdata = &davinci_i2c_platform_data_default; 6765c3d8a46SHeiko Schocher } 6775c3d8a46SHeiko Schocher 67885796843SVishwanathrao Badarkhe, Manish dev->clk = devm_clk_get(&pdev->dev, NULL); 679c4df5000SWolfram Sang if (IS_ERR(dev->clk)) 680c4df5000SWolfram Sang return -ENODEV; 6812bdbfa9cSMurali Karicheri clk_prepare_enable(dev->clk); 68295a7f10eSVladimir Barinov 6833cc2d009SWolfram Sang mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 68485796843SVishwanathrao Badarkhe, Manish dev->base = devm_ioremap_resource(&pdev->dev, mem); 68585796843SVishwanathrao Badarkhe, Manish if (IS_ERR(dev->base)) { 68685796843SVishwanathrao Badarkhe, Manish r = PTR_ERR(dev->base); 68785796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks; 688c062a251SChaithrika U S } 689c062a251SChaithrika U S 69095a7f10eSVladimir Barinov i2c_davinci_init(dev); 69195a7f10eSVladimir Barinov 69285796843SVishwanathrao Badarkhe, Manish r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0, 69385796843SVishwanathrao Badarkhe, Manish pdev->name, dev); 69495a7f10eSVladimir Barinov if (r) { 69595a7f10eSVladimir Barinov dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); 69695a7f10eSVladimir Barinov goto err_unuse_clocks; 69795a7f10eSVladimir Barinov } 69895a7f10eSVladimir Barinov 69982c0de11SChaithrika U S r = i2c_davinci_cpufreq_register(dev); 70082c0de11SChaithrika U S if (r) { 70182c0de11SChaithrika U S dev_err(&pdev->dev, "failed to register cpufreq\n"); 70285796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks; 70382c0de11SChaithrika U S } 70482c0de11SChaithrika U S 70595a7f10eSVladimir Barinov adap = &dev->adapter; 70695a7f10eSVladimir Barinov i2c_set_adapdata(adap, dev); 70795a7f10eSVladimir Barinov adap->owner = THIS_MODULE; 708adcb82a9SWolfram Sang adap->class = I2C_CLASS_DEPRECATED; 70995a7f10eSVladimir Barinov strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name)); 71095a7f10eSVladimir Barinov adap->algo = &i2c_davinci_algo; 71195a7f10eSVladimir Barinov adap->dev.parent = &pdev->dev; 71298a679caSJean Delvare adap->timeout = DAVINCI_I2C_TIMEOUT; 7135c3d8a46SHeiko Schocher adap->dev.of_node = pdev->dev.of_node; 71495a7f10eSVladimir Barinov 71595a7f10eSVladimir Barinov adap->nr = pdev->id; 71695a7f10eSVladimir Barinov r = i2c_add_numbered_adapter(adap); 71795a7f10eSVladimir Barinov if (r) { 71895a7f10eSVladimir Barinov dev_err(&pdev->dev, "failure adding adapter\n"); 71985796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks; 72095a7f10eSVladimir Barinov } 72195a7f10eSVladimir Barinov 72295a7f10eSVladimir Barinov return 0; 72395a7f10eSVladimir Barinov 72495a7f10eSVladimir Barinov err_unuse_clocks: 7252bdbfa9cSMurali Karicheri clk_disable_unprepare(dev->clk); 72695a7f10eSVladimir Barinov dev->clk = NULL; 72795a7f10eSVladimir Barinov return r; 72895a7f10eSVladimir Barinov } 72995a7f10eSVladimir Barinov 73095a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev) 73195a7f10eSVladimir Barinov { 73295a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = platform_get_drvdata(pdev); 73395a7f10eSVladimir Barinov 73482c0de11SChaithrika U S i2c_davinci_cpufreq_deregister(dev); 73582c0de11SChaithrika U S 73695a7f10eSVladimir Barinov i2c_del_adapter(&dev->adapter); 73795a7f10eSVladimir Barinov 7382bdbfa9cSMurali Karicheri clk_disable_unprepare(dev->clk); 73995a7f10eSVladimir Barinov dev->clk = NULL; 74095a7f10eSVladimir Barinov 74195a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0); 74295a7f10eSVladimir Barinov 74395a7f10eSVladimir Barinov return 0; 74495a7f10eSVladimir Barinov } 74595a7f10eSVladimir Barinov 74668f15de9SChaithrika U S #ifdef CONFIG_PM 74768f15de9SChaithrika U S static int davinci_i2c_suspend(struct device *dev) 74868f15de9SChaithrika U S { 74968f15de9SChaithrika U S struct platform_device *pdev = to_platform_device(dev); 75068f15de9SChaithrika U S struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 75168f15de9SChaithrika U S 75268f15de9SChaithrika U S /* put I2C into reset */ 75368f15de9SChaithrika U S davinci_i2c_reset_ctrl(i2c_dev, 0); 7542bdbfa9cSMurali Karicheri clk_disable_unprepare(i2c_dev->clk); 75568f15de9SChaithrika U S 75668f15de9SChaithrika U S return 0; 75768f15de9SChaithrika U S } 75868f15de9SChaithrika U S 75968f15de9SChaithrika U S static int davinci_i2c_resume(struct device *dev) 76068f15de9SChaithrika U S { 76168f15de9SChaithrika U S struct platform_device *pdev = to_platform_device(dev); 76268f15de9SChaithrika U S struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 76368f15de9SChaithrika U S 7642bdbfa9cSMurali Karicheri clk_prepare_enable(i2c_dev->clk); 76568f15de9SChaithrika U S /* take I2C out of reset */ 76668f15de9SChaithrika U S davinci_i2c_reset_ctrl(i2c_dev, 1); 76768f15de9SChaithrika U S 76868f15de9SChaithrika U S return 0; 76968f15de9SChaithrika U S } 77068f15de9SChaithrika U S 77168f15de9SChaithrika U S static const struct dev_pm_ops davinci_i2c_pm = { 77268f15de9SChaithrika U S .suspend = davinci_i2c_suspend, 77368f15de9SChaithrika U S .resume = davinci_i2c_resume, 77468f15de9SChaithrika U S }; 77568f15de9SChaithrika U S 77668f15de9SChaithrika U S #define davinci_i2c_pm_ops (&davinci_i2c_pm) 77768f15de9SChaithrika U S #else 77868f15de9SChaithrika U S #define davinci_i2c_pm_ops NULL 77968f15de9SChaithrika U S #endif 78068f15de9SChaithrika U S 781add8eda7SKay Sievers /* work with hotplug and coldplug */ 782add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci"); 783add8eda7SKay Sievers 78495a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = { 78595a7f10eSVladimir Barinov .probe = davinci_i2c_probe, 78695a7f10eSVladimir Barinov .remove = davinci_i2c_remove, 78795a7f10eSVladimir Barinov .driver = { 78895a7f10eSVladimir Barinov .name = "i2c_davinci", 78968f15de9SChaithrika U S .pm = davinci_i2c_pm_ops, 7904e905323SSachin Kamat .of_match_table = davinci_i2c_of_match, 79195a7f10eSVladimir Barinov }, 79295a7f10eSVladimir Barinov }; 79395a7f10eSVladimir Barinov 79495a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */ 79595a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void) 79695a7f10eSVladimir Barinov { 79795a7f10eSVladimir Barinov return platform_driver_register(&davinci_i2c_driver); 79895a7f10eSVladimir Barinov } 79995a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver); 80095a7f10eSVladimir Barinov 80195a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void) 80295a7f10eSVladimir Barinov { 80395a7f10eSVladimir Barinov platform_driver_unregister(&davinci_i2c_driver); 80495a7f10eSVladimir Barinov } 80595a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver); 80695a7f10eSVladimir Barinov 80795a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India"); 80895a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter"); 80995a7f10eSVladimir Barinov MODULE_LICENSE("GPL"); 810