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 * You should have received a copy of the GNU General Public License 2295a7f10eSVladimir Barinov * along with this program; if not, write to the Free Software 2395a7f10eSVladimir Barinov * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 2495a7f10eSVladimir Barinov * ---------------------------------------------------------------------------- 2595a7f10eSVladimir Barinov * 2695a7f10eSVladimir Barinov */ 2795a7f10eSVladimir Barinov #include <linux/kernel.h> 2895a7f10eSVladimir Barinov #include <linux/module.h> 2995a7f10eSVladimir Barinov #include <linux/delay.h> 3095a7f10eSVladimir Barinov #include <linux/i2c.h> 3195a7f10eSVladimir Barinov #include <linux/clk.h> 3295a7f10eSVladimir Barinov #include <linux/errno.h> 3395a7f10eSVladimir Barinov #include <linux/sched.h> 3495a7f10eSVladimir Barinov #include <linux/err.h> 3595a7f10eSVladimir Barinov #include <linux/interrupt.h> 3695a7f10eSVladimir Barinov #include <linux/platform_device.h> 3795a7f10eSVladimir Barinov #include <linux/io.h> 385a0e3ad6STejun Heo #include <linux/slab.h> 3982c0de11SChaithrika U S #include <linux/cpufreq.h> 408574faf9SPhilby John #include <linux/gpio.h> 415c3d8a46SHeiko Schocher #include <linux/of_device.h> 42ec2a0833SArnd Bergmann #include <linux/platform_data/i2c-davinci.h> 4395a7f10eSVladimir Barinov 4495a7f10eSVladimir Barinov /* ----- global defines ----------------------------------------------- */ 4595a7f10eSVladimir Barinov 4695a7f10eSVladimir Barinov #define DAVINCI_I2C_TIMEOUT (1*HZ) 478574faf9SPhilby John #define DAVINCI_I2C_MAX_TRIES 2 4895a7f10eSVladimir Barinov #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \ 4995a7f10eSVladimir Barinov DAVINCI_I2C_IMR_SCD | \ 5095a7f10eSVladimir Barinov DAVINCI_I2C_IMR_ARDY | \ 5195a7f10eSVladimir Barinov DAVINCI_I2C_IMR_NACK | \ 5295a7f10eSVladimir Barinov DAVINCI_I2C_IMR_AL) 5395a7f10eSVladimir Barinov 5495a7f10eSVladimir Barinov #define DAVINCI_I2C_OAR_REG 0x00 5595a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_REG 0x04 5695a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_REG 0x08 5795a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKL_REG 0x0c 5895a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKH_REG 0x10 5995a7f10eSVladimir Barinov #define DAVINCI_I2C_CNT_REG 0x14 6095a7f10eSVladimir Barinov #define DAVINCI_I2C_DRR_REG 0x18 6195a7f10eSVladimir Barinov #define DAVINCI_I2C_SAR_REG 0x1c 6295a7f10eSVladimir Barinov #define DAVINCI_I2C_DXR_REG 0x20 6395a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_REG 0x24 6495a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_REG 0x28 6595a7f10eSVladimir Barinov #define DAVINCI_I2C_EMDR_REG 0x2c 6695a7f10eSVladimir Barinov #define DAVINCI_I2C_PSC_REG 0x30 6795a7f10eSVladimir Barinov 6895a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AAS 0x07 6995a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_SCD 0x06 7095a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_XRDY 0x05 7195a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_RDR 0x04 7295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_ARDY 0x03 7395a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_NACK 0x02 7495a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AL 0x01 7595a7f10eSVladimir Barinov 76c062a251SChaithrika U S #define DAVINCI_I2C_STR_BB BIT(12) 77c062a251SChaithrika U S #define DAVINCI_I2C_STR_RSFULL BIT(11) 78c062a251SChaithrika U S #define DAVINCI_I2C_STR_SCD BIT(5) 79c062a251SChaithrika U S #define DAVINCI_I2C_STR_ARDY BIT(2) 80c062a251SChaithrika U S #define DAVINCI_I2C_STR_NACK BIT(1) 81c062a251SChaithrika U S #define DAVINCI_I2C_STR_AL BIT(0) 8295a7f10eSVladimir Barinov 83c062a251SChaithrika U S #define DAVINCI_I2C_MDR_NACK BIT(15) 84c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STT BIT(13) 85c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STP BIT(11) 86c062a251SChaithrika U S #define DAVINCI_I2C_MDR_MST BIT(10) 87c062a251SChaithrika U S #define DAVINCI_I2C_MDR_TRX BIT(9) 88c062a251SChaithrika U S #define DAVINCI_I2C_MDR_XA BIT(8) 89c062a251SChaithrika U S #define DAVINCI_I2C_MDR_RM BIT(7) 90c062a251SChaithrika U S #define DAVINCI_I2C_MDR_IRS BIT(5) 9195a7f10eSVladimir Barinov 92c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AAS BIT(6) 93c062a251SChaithrika U S #define DAVINCI_I2C_IMR_SCD BIT(5) 94c062a251SChaithrika U S #define DAVINCI_I2C_IMR_XRDY BIT(4) 95c062a251SChaithrika U S #define DAVINCI_I2C_IMR_RRDY BIT(3) 96c062a251SChaithrika U S #define DAVINCI_I2C_IMR_ARDY BIT(2) 97c062a251SChaithrika U S #define DAVINCI_I2C_IMR_NACK BIT(1) 98c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AL BIT(0) 9995a7f10eSVladimir Barinov 10095a7f10eSVladimir Barinov struct davinci_i2c_dev { 10195a7f10eSVladimir Barinov struct device *dev; 10295a7f10eSVladimir Barinov void __iomem *base; 10395a7f10eSVladimir Barinov struct completion cmd_complete; 10495a7f10eSVladimir Barinov struct clk *clk; 10595a7f10eSVladimir Barinov int cmd_err; 10695a7f10eSVladimir Barinov u8 *buf; 10795a7f10eSVladimir Barinov size_t buf_len; 10895a7f10eSVladimir Barinov int irq; 109c6c7c729SDirk Behme int stop; 1105a0d5f5fSTroy Kisky u8 terminate; 11195a7f10eSVladimir Barinov struct i2c_adapter adapter; 11282c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 11382c0de11SChaithrika U S struct completion xfr_complete; 11482c0de11SChaithrika U S struct notifier_block freq_transition; 11582c0de11SChaithrika U S #endif 1165c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata; 11795a7f10eSVladimir Barinov }; 11895a7f10eSVladimir Barinov 11995a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */ 12095a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = { 12195a7f10eSVladimir Barinov .bus_freq = 100, 12295a7f10eSVladimir Barinov .bus_delay = 0, 12395a7f10eSVladimir Barinov }; 12495a7f10eSVladimir Barinov 12595a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev, 12695a7f10eSVladimir Barinov int reg, u16 val) 12795a7f10eSVladimir Barinov { 128a238dcfaSTaras Kondratiuk writew_relaxed(val, i2c_dev->base + reg); 12995a7f10eSVladimir Barinov } 13095a7f10eSVladimir Barinov 13195a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg) 13295a7f10eSVladimir Barinov { 133a238dcfaSTaras Kondratiuk return readw_relaxed(i2c_dev->base + reg); 13495a7f10eSVladimir Barinov } 13595a7f10eSVladimir Barinov 1368574faf9SPhilby John /* Generate a pulse on the i2c clock pin. */ 137adf68acfSWolfram Sang static void davinci_i2c_clock_pulse(unsigned int scl_pin) 1388574faf9SPhilby John { 1398574faf9SPhilby John u16 i; 1408574faf9SPhilby John 1418574faf9SPhilby John if (scl_pin) { 1428574faf9SPhilby John /* Send high and low on the SCL line */ 1438574faf9SPhilby John for (i = 0; i < 9; i++) { 1448574faf9SPhilby John gpio_set_value(scl_pin, 0); 1458574faf9SPhilby John udelay(20); 1468574faf9SPhilby John gpio_set_value(scl_pin, 1); 1478574faf9SPhilby John udelay(20); 1488574faf9SPhilby John } 1498574faf9SPhilby John } 1508574faf9SPhilby John } 1518574faf9SPhilby John 1528574faf9SPhilby John /* This routine does i2c bus recovery as specified in the 1538574faf9SPhilby John * i2c protocol Rev. 03 section 3.16 titled "Bus clear" 1548574faf9SPhilby John */ 155adf68acfSWolfram Sang static void davinci_i2c_recover_bus(struct davinci_i2c_dev *dev) 1568574faf9SPhilby John { 1578574faf9SPhilby John u32 flag = 0; 1585c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 1598574faf9SPhilby John 1608574faf9SPhilby John dev_err(dev->dev, "initiating i2c bus recovery\n"); 1618574faf9SPhilby John /* Send NACK to the slave */ 1628574faf9SPhilby John flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 1638574faf9SPhilby John flag |= DAVINCI_I2C_MDR_NACK; 1648574faf9SPhilby John /* write the data into mode register */ 1658574faf9SPhilby John davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 166adf68acfSWolfram Sang davinci_i2c_clock_pulse(pdata->scl_pin); 1678574faf9SPhilby John /* Send STOP */ 1688574faf9SPhilby John flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 1698574faf9SPhilby John flag |= DAVINCI_I2C_MDR_STP; 1708574faf9SPhilby John davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 1718574faf9SPhilby John } 1728574faf9SPhilby John 1735ae5b113SChaithrika U S static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev, 1745ae5b113SChaithrika U S int val) 1755ae5b113SChaithrika U S { 1765ae5b113SChaithrika U S u16 w; 1775ae5b113SChaithrika U S 1785ae5b113SChaithrika U S w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG); 1795ae5b113SChaithrika U S if (!val) /* put I2C into reset */ 1805ae5b113SChaithrika U S w &= ~DAVINCI_I2C_MDR_IRS; 1815ae5b113SChaithrika U S else /* take I2C out of reset */ 1825ae5b113SChaithrika U S w |= DAVINCI_I2C_MDR_IRS; 1835ae5b113SChaithrika U S 1845ae5b113SChaithrika U S davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w); 1855ae5b113SChaithrika U S } 1865ae5b113SChaithrika U S 1875ae5b113SChaithrika U S static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev) 18895a7f10eSVladimir Barinov { 1895c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 19095a7f10eSVladimir Barinov u16 psc; 19195a7f10eSVladimir Barinov u32 clk; 192cc99ff70STroy Kisky u32 d; 19395a7f10eSVladimir Barinov u32 clkh; 19495a7f10eSVladimir Barinov u32 clkl; 19595a7f10eSVladimir Barinov u32 input_clock = clk_get_rate(dev->clk); 19695a7f10eSVladimir Barinov 19795a7f10eSVladimir Barinov /* NOTE: I2C Clock divider programming info 19895a7f10eSVladimir Barinov * As per I2C specs the following formulas provide prescaler 19995a7f10eSVladimir Barinov * and low/high divider values 20095a7f10eSVladimir Barinov * input clk --> PSC Div -----------> ICCL/H Div --> output clock 20195a7f10eSVladimir Barinov * module clk 20295a7f10eSVladimir Barinov * 20395a7f10eSVladimir Barinov * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ] 20495a7f10eSVladimir Barinov * 20595a7f10eSVladimir Barinov * Thus, 20695a7f10eSVladimir Barinov * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d; 20795a7f10eSVladimir Barinov * 20895a7f10eSVladimir Barinov * where if PSC == 0, d = 7, 20995a7f10eSVladimir Barinov * if PSC == 1, d = 6 21095a7f10eSVladimir Barinov * if PSC > 1 , d = 5 21195a7f10eSVladimir Barinov */ 21295a7f10eSVladimir Barinov 213cc99ff70STroy Kisky /* get minimum of 7 MHz clock, but max of 12 MHz */ 214cc99ff70STroy Kisky psc = (input_clock / 7000000) - 1; 215cc99ff70STroy Kisky if ((input_clock / (psc + 1)) > 12000000) 216cc99ff70STroy Kisky psc++; /* better to run under spec than over */ 217cc99ff70STroy Kisky d = (psc >= 2) ? 5 : 7 - psc; 21895a7f10eSVladimir Barinov 219cc99ff70STroy Kisky clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1); 220cc99ff70STroy Kisky clkh = clk >> 1; 22195a7f10eSVladimir Barinov clkl = clk - clkh; 22295a7f10eSVladimir Barinov 22395a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc); 22495a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh); 22595a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl); 22695a7f10eSVladimir Barinov 2275ae5b113SChaithrika U S dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk); 2285ae5b113SChaithrika U S } 2295ae5b113SChaithrika U S 2305ae5b113SChaithrika U S /* 2315ae5b113SChaithrika U S * This function configures I2C and brings I2C out of reset. 2325ae5b113SChaithrika U S * This function is called during I2C init function. This function 2335ae5b113SChaithrika U S * also gets called if I2C encounters any errors. 2345ae5b113SChaithrika U S */ 2355ae5b113SChaithrika U S static int i2c_davinci_init(struct davinci_i2c_dev *dev) 2365ae5b113SChaithrika U S { 2375c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 2385ae5b113SChaithrika U S 2395ae5b113SChaithrika U S /* put I2C into reset */ 2405ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 0); 2415ae5b113SChaithrika U S 2425ae5b113SChaithrika U S /* compute clock dividers */ 2435ae5b113SChaithrika U S i2c_davinci_calc_clk_dividers(dev); 2445ae5b113SChaithrika U S 2457605fa3bSDavid Brownell /* Respond at reserved "SMBus Host" slave address" (and zero); 2467605fa3bSDavid Brownell * we seem to have no option to not respond... 2477605fa3bSDavid Brownell */ 2487605fa3bSDavid Brownell davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08); 2497605fa3bSDavid Brownell 25095a7f10eSVladimir Barinov dev_dbg(dev->dev, "PSC = %d\n", 25195a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG)); 25295a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKL = %d\n", 25395a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG)); 25495a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKH = %d\n", 25595a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG)); 256cc99ff70STroy Kisky dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n", 257cc99ff70STroy Kisky pdata->bus_freq, pdata->bus_delay); 25895a7f10eSVladimir Barinov 2595c3d8a46SHeiko Schocher 26095a7f10eSVladimir Barinov /* Take the I2C module out of reset: */ 2615ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 1); 26295a7f10eSVladimir Barinov 26395a7f10eSVladimir Barinov /* Enable interrupts */ 26495a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL); 26595a7f10eSVladimir Barinov 26695a7f10eSVladimir Barinov return 0; 26795a7f10eSVladimir Barinov } 26895a7f10eSVladimir Barinov 26995a7f10eSVladimir Barinov /* 27095a7f10eSVladimir Barinov * Waiting for bus not busy 27195a7f10eSVladimir Barinov */ 27295a7f10eSVladimir Barinov static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev, 27395a7f10eSVladimir Barinov char allow_sleep) 27495a7f10eSVladimir Barinov { 27595a7f10eSVladimir Barinov unsigned long timeout; 2768574faf9SPhilby John static u16 to_cnt; 27795a7f10eSVladimir Barinov 27898a679caSJean Delvare timeout = jiffies + dev->adapter.timeout; 27995a7f10eSVladimir Barinov while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) 28095a7f10eSVladimir Barinov & DAVINCI_I2C_STR_BB) { 2818574faf9SPhilby John if (to_cnt <= DAVINCI_I2C_MAX_TRIES) { 28295a7f10eSVladimir Barinov if (time_after(jiffies, timeout)) { 28395a7f10eSVladimir Barinov dev_warn(dev->dev, 28495a7f10eSVladimir Barinov "timeout waiting for bus ready\n"); 2858574faf9SPhilby John to_cnt++; 28695a7f10eSVladimir Barinov return -ETIMEDOUT; 2878574faf9SPhilby John } else { 2888574faf9SPhilby John to_cnt = 0; 289adf68acfSWolfram Sang davinci_i2c_recover_bus(dev); 2908574faf9SPhilby John i2c_davinci_init(dev); 2918574faf9SPhilby John } 29295a7f10eSVladimir Barinov } 29395a7f10eSVladimir Barinov if (allow_sleep) 29495a7f10eSVladimir Barinov schedule_timeout(1); 29595a7f10eSVladimir Barinov } 29695a7f10eSVladimir Barinov 29795a7f10eSVladimir Barinov return 0; 29895a7f10eSVladimir Barinov } 29995a7f10eSVladimir Barinov 30095a7f10eSVladimir Barinov /* 30195a7f10eSVladimir Barinov * Low level master read/write transaction. This function is called 30295a7f10eSVladimir Barinov * from i2c_davinci_xfer. 30395a7f10eSVladimir Barinov */ 30495a7f10eSVladimir Barinov static int 30595a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop) 30695a7f10eSVladimir Barinov { 30795a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 3085c3d8a46SHeiko Schocher struct davinci_i2c_platform_data *pdata = dev->pdata; 30995a7f10eSVladimir Barinov u32 flag; 31095a7f10eSVladimir Barinov u16 w; 31195a7f10eSVladimir Barinov int r; 31295a7f10eSVladimir Barinov 31395a7f10eSVladimir Barinov /* Introduce a delay, required for some boards (e.g Davinci EVM) */ 31495a7f10eSVladimir Barinov if (pdata->bus_delay) 31595a7f10eSVladimir Barinov udelay(pdata->bus_delay); 31695a7f10eSVladimir Barinov 31795a7f10eSVladimir Barinov /* set the slave address */ 31895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr); 31995a7f10eSVladimir Barinov 32095a7f10eSVladimir Barinov dev->buf = msg->buf; 32195a7f10eSVladimir Barinov dev->buf_len = msg->len; 322c6c7c729SDirk Behme dev->stop = stop; 32395a7f10eSVladimir Barinov 32495a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len); 32595a7f10eSVladimir Barinov 32616735d02SWolfram Sang reinit_completion(&dev->cmd_complete); 32795a7f10eSVladimir Barinov dev->cmd_err = 0; 32895a7f10eSVladimir Barinov 329c5b4afecSJon Povey /* Take I2C out of reset and configure it as master */ 330c5b4afecSJon Povey flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST; 33195a7f10eSVladimir Barinov 33295a7f10eSVladimir Barinov /* if the slave address is ten bit address, enable XA bit */ 33395a7f10eSVladimir Barinov if (msg->flags & I2C_M_TEN) 33495a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_XA; 33595a7f10eSVladimir Barinov if (!(msg->flags & I2C_M_RD)) 33695a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_TRX; 337c5b4afecSJon Povey if (msg->len == 0) 338c6c7c729SDirk Behme flag |= DAVINCI_I2C_MDR_RM; 33995a7f10eSVladimir Barinov 34095a7f10eSVladimir Barinov /* Enable receive or transmit interrupts */ 34195a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG); 34295a7f10eSVladimir Barinov if (msg->flags & I2C_M_RD) 343c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_RRDY; 34495a7f10eSVladimir Barinov else 345c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_XRDY; 34695a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w); 34795a7f10eSVladimir Barinov 3485a0d5f5fSTroy Kisky dev->terminate = 0; 349c6c7c729SDirk Behme 350c6c7c729SDirk Behme /* 351c5b4afecSJon Povey * Write mode register first as needed for correct behaviour 352c5b4afecSJon Povey * on OMAP-L138, but don't set STT yet to avoid a race with XRDY 35325985edcSLucas De Marchi * occurring before we have loaded DXR 354c5b4afecSJon Povey */ 355c5b4afecSJon Povey davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 356c5b4afecSJon Povey 357c5b4afecSJon Povey /* 358c6c7c729SDirk Behme * First byte should be set here, not after interrupt, 359c6c7c729SDirk Behme * because transmit-data-ready interrupt can come before 360c6c7c729SDirk Behme * NACK-interrupt during sending of previous message and 361c6c7c729SDirk Behme * ICDXR may have wrong data 362c5b4afecSJon Povey * It also saves us one interrupt, slightly faster 363c6c7c729SDirk Behme */ 364c6c7c729SDirk Behme if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) { 365c6c7c729SDirk Behme davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++); 366c6c7c729SDirk Behme dev->buf_len--; 367c6c7c729SDirk Behme } 368c6c7c729SDirk Behme 369c5b4afecSJon Povey /* Set STT to begin transmit now DXR is loaded */ 370c5b4afecSJon Povey flag |= DAVINCI_I2C_MDR_STT; 371c5b4afecSJon Povey if (stop && msg->len != 0) 372c5b4afecSJon Povey flag |= DAVINCI_I2C_MDR_STP; 3734bba0fd8SJon Povey davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 3744bba0fd8SJon Povey 37595a7f10eSVladimir Barinov r = wait_for_completion_interruptible_timeout(&dev->cmd_complete, 37698a679caSJean Delvare dev->adapter.timeout); 37795a7f10eSVladimir Barinov if (r == 0) { 37895a7f10eSVladimir Barinov dev_err(dev->dev, "controller timed out\n"); 379adf68acfSWolfram Sang davinci_i2c_recover_bus(dev); 38095a7f10eSVladimir Barinov i2c_davinci_init(dev); 3815a0d5f5fSTroy Kisky dev->buf_len = 0; 38295a7f10eSVladimir Barinov return -ETIMEDOUT; 38395a7f10eSVladimir Barinov } 3845a0d5f5fSTroy Kisky if (dev->buf_len) { 3855a0d5f5fSTroy Kisky /* This should be 0 if all bytes were transferred 3865a0d5f5fSTroy Kisky * or dev->cmd_err denotes an error. 3875a0d5f5fSTroy Kisky * A signal may have aborted the transfer. 3885a0d5f5fSTroy Kisky */ 3895a0d5f5fSTroy Kisky if (r >= 0) { 3905a0d5f5fSTroy Kisky dev_err(dev->dev, "abnormal termination buf_len=%i\n", 3915a0d5f5fSTroy Kisky dev->buf_len); 3925a0d5f5fSTroy Kisky r = -EREMOTEIO; 3935a0d5f5fSTroy Kisky } 3945a0d5f5fSTroy Kisky dev->terminate = 1; 3955a0d5f5fSTroy Kisky wmb(); 3965a0d5f5fSTroy Kisky dev->buf_len = 0; 3975a0d5f5fSTroy Kisky } 3985a0d5f5fSTroy Kisky if (r < 0) 3995a0d5f5fSTroy Kisky return r; 40095a7f10eSVladimir Barinov 40195a7f10eSVladimir Barinov /* no error */ 40295a7f10eSVladimir Barinov if (likely(!dev->cmd_err)) 40395a7f10eSVladimir Barinov return msg->len; 40495a7f10eSVladimir Barinov 40595a7f10eSVladimir Barinov /* We have an error */ 40695a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_AL) { 40795a7f10eSVladimir Barinov i2c_davinci_init(dev); 40895a7f10eSVladimir Barinov return -EIO; 40995a7f10eSVladimir Barinov } 41095a7f10eSVladimir Barinov 41195a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_NACK) { 41295a7f10eSVladimir Barinov if (msg->flags & I2C_M_IGNORE_NAK) 41395a7f10eSVladimir Barinov return msg->len; 41495a7f10eSVladimir Barinov if (stop) { 41595a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 416c062a251SChaithrika U S w |= DAVINCI_I2C_MDR_STP; 41795a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 41895a7f10eSVladimir Barinov } 41995a7f10eSVladimir Barinov return -EREMOTEIO; 42095a7f10eSVladimir Barinov } 42195a7f10eSVladimir Barinov return -EIO; 42295a7f10eSVladimir Barinov } 42395a7f10eSVladimir Barinov 42495a7f10eSVladimir Barinov /* 42595a7f10eSVladimir Barinov * Prepare controller for a transaction and call i2c_davinci_xfer_msg 42695a7f10eSVladimir Barinov */ 42795a7f10eSVladimir Barinov static int 42895a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) 42995a7f10eSVladimir Barinov { 43095a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 43195a7f10eSVladimir Barinov int i; 43295a7f10eSVladimir Barinov int ret; 43395a7f10eSVladimir Barinov 43408882d20SHarvey Harrison dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); 43595a7f10eSVladimir Barinov 43695a7f10eSVladimir Barinov ret = i2c_davinci_wait_bus_not_busy(dev, 1); 43795a7f10eSVladimir Barinov if (ret < 0) { 43895a7f10eSVladimir Barinov dev_warn(dev->dev, "timeout waiting for bus ready\n"); 43995a7f10eSVladimir Barinov return ret; 44095a7f10eSVladimir Barinov } 44195a7f10eSVladimir Barinov 44295a7f10eSVladimir Barinov for (i = 0; i < num; i++) { 44395a7f10eSVladimir Barinov ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1))); 444d868caa1STroy Kisky dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num, 445d868caa1STroy Kisky ret); 44695a7f10eSVladimir Barinov if (ret < 0) 44795a7f10eSVladimir Barinov return ret; 44895a7f10eSVladimir Barinov } 44982c0de11SChaithrika U S 45082c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 45182c0de11SChaithrika U S complete(&dev->xfr_complete); 45282c0de11SChaithrika U S #endif 45382c0de11SChaithrika U S 45495a7f10eSVladimir Barinov return num; 45595a7f10eSVladimir Barinov } 45695a7f10eSVladimir Barinov 45795a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap) 45895a7f10eSVladimir Barinov { 459c6c7c729SDirk Behme return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 46095a7f10eSVladimir Barinov } 46195a7f10eSVladimir Barinov 4625a0d5f5fSTroy Kisky static void terminate_read(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_NACK; 4665a0d5f5fSTroy Kisky davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 4675a0d5f5fSTroy Kisky 4685a0d5f5fSTroy Kisky /* Throw away data */ 4695a0d5f5fSTroy Kisky davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG); 4705a0d5f5fSTroy Kisky if (!dev->terminate) 4715a0d5f5fSTroy Kisky dev_err(dev->dev, "RDR IRQ while no data requested\n"); 4725a0d5f5fSTroy Kisky } 4735a0d5f5fSTroy Kisky static void terminate_write(struct davinci_i2c_dev *dev) 4745a0d5f5fSTroy Kisky { 4755a0d5f5fSTroy Kisky u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 4765a0d5f5fSTroy Kisky w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP; 4775a0d5f5fSTroy Kisky davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 4785a0d5f5fSTroy Kisky 4795a0d5f5fSTroy Kisky if (!dev->terminate) 4807605fa3bSDavid Brownell dev_dbg(dev->dev, "TDR IRQ while no data to send\n"); 4815a0d5f5fSTroy Kisky } 4825a0d5f5fSTroy Kisky 48395a7f10eSVladimir Barinov /* 48495a7f10eSVladimir Barinov * Interrupt service routine. This gets called whenever an I2C interrupt 48595a7f10eSVladimir Barinov * occurs. 48695a7f10eSVladimir Barinov */ 48795a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id) 48895a7f10eSVladimir Barinov { 48995a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = dev_id; 49095a7f10eSVladimir Barinov u32 stat; 49195a7f10eSVladimir Barinov int count = 0; 49295a7f10eSVladimir Barinov u16 w; 49395a7f10eSVladimir Barinov 49495a7f10eSVladimir Barinov while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) { 49508882d20SHarvey Harrison dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat); 49695a7f10eSVladimir Barinov if (count++ == 100) { 49795a7f10eSVladimir Barinov dev_warn(dev->dev, "Too much work in one IRQ\n"); 49895a7f10eSVladimir Barinov break; 49995a7f10eSVladimir Barinov } 50095a7f10eSVladimir Barinov 50195a7f10eSVladimir Barinov switch (stat) { 50295a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_AL: 5035a0d5f5fSTroy Kisky /* Arbitration lost, must retry */ 50495a7f10eSVladimir Barinov dev->cmd_err |= DAVINCI_I2C_STR_AL; 5055a0d5f5fSTroy Kisky dev->buf_len = 0; 50695a7f10eSVladimir Barinov complete(&dev->cmd_complete); 50795a7f10eSVladimir Barinov break; 50895a7f10eSVladimir Barinov 50995a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_NACK: 51095a7f10eSVladimir Barinov dev->cmd_err |= DAVINCI_I2C_STR_NACK; 5115a0d5f5fSTroy Kisky dev->buf_len = 0; 51295a7f10eSVladimir Barinov complete(&dev->cmd_complete); 51395a7f10eSVladimir Barinov break; 51495a7f10eSVladimir Barinov 51595a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_ARDY: 516b73a9aecSTroy Kisky davinci_i2c_write_reg(dev, 517b73a9aecSTroy Kisky DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY); 518c6c7c729SDirk Behme if (((dev->buf_len == 0) && (dev->stop != 0)) || 519c6c7c729SDirk Behme (dev->cmd_err & DAVINCI_I2C_STR_NACK)) { 520c6c7c729SDirk Behme w = davinci_i2c_read_reg(dev, 521c6c7c729SDirk Behme DAVINCI_I2C_MDR_REG); 522c6c7c729SDirk Behme w |= DAVINCI_I2C_MDR_STP; 523c6c7c729SDirk Behme davinci_i2c_write_reg(dev, 524c6c7c729SDirk Behme DAVINCI_I2C_MDR_REG, w); 525c6c7c729SDirk Behme } 52695a7f10eSVladimir Barinov complete(&dev->cmd_complete); 52795a7f10eSVladimir Barinov break; 52895a7f10eSVladimir Barinov 52995a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_RDR: 53095a7f10eSVladimir Barinov if (dev->buf_len) { 53195a7f10eSVladimir Barinov *dev->buf++ = 53295a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, 53395a7f10eSVladimir Barinov DAVINCI_I2C_DRR_REG); 53495a7f10eSVladimir Barinov dev->buf_len--; 53595a7f10eSVladimir Barinov if (dev->buf_len) 53695a7f10eSVladimir Barinov continue; 53795a7f10eSVladimir Barinov 53895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, 53995a7f10eSVladimir Barinov DAVINCI_I2C_STR_REG, 540b73a9aecSTroy Kisky DAVINCI_I2C_IMR_RRDY); 5415a0d5f5fSTroy Kisky } else { 5425a0d5f5fSTroy Kisky /* signal can terminate transfer */ 5435a0d5f5fSTroy Kisky terminate_read(dev); 5445a0d5f5fSTroy Kisky } 54595a7f10eSVladimir Barinov break; 54695a7f10eSVladimir Barinov 54795a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_XRDY: 54895a7f10eSVladimir Barinov if (dev->buf_len) { 54995a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, 55095a7f10eSVladimir Barinov *dev->buf++); 55195a7f10eSVladimir Barinov dev->buf_len--; 55295a7f10eSVladimir Barinov if (dev->buf_len) 55395a7f10eSVladimir Barinov continue; 55495a7f10eSVladimir Barinov 55595a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, 55695a7f10eSVladimir Barinov DAVINCI_I2C_IMR_REG); 557c062a251SChaithrika U S w &= ~DAVINCI_I2C_IMR_XRDY; 55895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, 55995a7f10eSVladimir Barinov DAVINCI_I2C_IMR_REG, 56095a7f10eSVladimir Barinov w); 5615a0d5f5fSTroy Kisky } else { 5625a0d5f5fSTroy Kisky /* signal can terminate transfer */ 5635a0d5f5fSTroy Kisky terminate_write(dev); 5645a0d5f5fSTroy Kisky } 56595a7f10eSVladimir Barinov break; 56695a7f10eSVladimir Barinov 56795a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_SCD: 568b73a9aecSTroy Kisky davinci_i2c_write_reg(dev, 569b73a9aecSTroy Kisky DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD); 57095a7f10eSVladimir Barinov complete(&dev->cmd_complete); 57195a7f10eSVladimir Barinov break; 57295a7f10eSVladimir Barinov 57395a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_AAS: 5747605fa3bSDavid Brownell dev_dbg(dev->dev, "Address as slave interrupt\n"); 5757605fa3bSDavid Brownell break; 5767605fa3bSDavid Brownell 5777605fa3bSDavid Brownell default: 5787605fa3bSDavid Brownell dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat); 5797605fa3bSDavid Brownell break; 5807605fa3bSDavid Brownell } 5817605fa3bSDavid Brownell } 58295a7f10eSVladimir Barinov 58395a7f10eSVladimir Barinov return count ? IRQ_HANDLED : IRQ_NONE; 58495a7f10eSVladimir Barinov } 58595a7f10eSVladimir Barinov 58682c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 58782c0de11SChaithrika U S static int i2c_davinci_cpufreq_transition(struct notifier_block *nb, 58882c0de11SChaithrika U S unsigned long val, void *data) 58982c0de11SChaithrika U S { 59082c0de11SChaithrika U S struct davinci_i2c_dev *dev; 59182c0de11SChaithrika U S 59282c0de11SChaithrika U S dev = container_of(nb, struct davinci_i2c_dev, freq_transition); 59382c0de11SChaithrika U S if (val == CPUFREQ_PRECHANGE) { 59482c0de11SChaithrika U S wait_for_completion(&dev->xfr_complete); 59582c0de11SChaithrika U S davinci_i2c_reset_ctrl(dev, 0); 59682c0de11SChaithrika U S } else if (val == CPUFREQ_POSTCHANGE) { 59782c0de11SChaithrika U S i2c_davinci_calc_clk_dividers(dev); 59882c0de11SChaithrika U S davinci_i2c_reset_ctrl(dev, 1); 59982c0de11SChaithrika U S } 60082c0de11SChaithrika U S 60182c0de11SChaithrika U S return 0; 60282c0de11SChaithrika U S } 60382c0de11SChaithrika U S 60482c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev) 60582c0de11SChaithrika U S { 60682c0de11SChaithrika U S dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition; 60782c0de11SChaithrika U S 60882c0de11SChaithrika U S return cpufreq_register_notifier(&dev->freq_transition, 60982c0de11SChaithrika U S CPUFREQ_TRANSITION_NOTIFIER); 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 cpufreq_unregister_notifier(&dev->freq_transition, 61582c0de11SChaithrika U S CPUFREQ_TRANSITION_NOTIFIER); 61682c0de11SChaithrika U S } 61782c0de11SChaithrika U S #else 61882c0de11SChaithrika U S static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev) 61982c0de11SChaithrika U S { 62082c0de11SChaithrika U S return 0; 62182c0de11SChaithrika U S } 62282c0de11SChaithrika U S 62382c0de11SChaithrika U S static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev) 62482c0de11SChaithrika U S { 62582c0de11SChaithrika U S } 62682c0de11SChaithrika U S #endif 62782c0de11SChaithrika U S 62895a7f10eSVladimir Barinov static struct i2c_algorithm i2c_davinci_algo = { 62995a7f10eSVladimir Barinov .master_xfer = i2c_davinci_xfer, 63095a7f10eSVladimir Barinov .functionality = i2c_davinci_func, 63195a7f10eSVladimir Barinov }; 63295a7f10eSVladimir Barinov 6335c3d8a46SHeiko Schocher static const struct of_device_id davinci_i2c_of_match[] = { 6345c3d8a46SHeiko Schocher {.compatible = "ti,davinci-i2c", }, 6355c3d8a46SHeiko Schocher {}, 6365c3d8a46SHeiko Schocher }; 6375c3d8a46SHeiko Schocher MODULE_DEVICE_TABLE(of, davinci_i2c_of_match); 6385c3d8a46SHeiko Schocher 63995a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev) 64095a7f10eSVladimir Barinov { 64195a7f10eSVladimir Barinov struct davinci_i2c_dev *dev; 64295a7f10eSVladimir Barinov struct i2c_adapter *adap; 64385796843SVishwanathrao Badarkhe, Manish struct resource *mem, *irq; 64495a7f10eSVladimir Barinov int r; 64595a7f10eSVladimir Barinov 64695a7f10eSVladimir Barinov irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 64795a7f10eSVladimir Barinov if (!irq) { 64895a7f10eSVladimir Barinov dev_err(&pdev->dev, "no irq resource?\n"); 64995a7f10eSVladimir Barinov return -ENODEV; 65095a7f10eSVladimir Barinov } 65195a7f10eSVladimir Barinov 65285796843SVishwanathrao Badarkhe, Manish dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev), 65385796843SVishwanathrao Badarkhe, Manish GFP_KERNEL); 65495a7f10eSVladimir Barinov if (!dev) { 65585796843SVishwanathrao Badarkhe, Manish dev_err(&pdev->dev, "Memory allocation failed\n"); 65685796843SVishwanathrao Badarkhe, Manish return -ENOMEM; 65795a7f10eSVladimir Barinov } 65895a7f10eSVladimir Barinov 6592e743787STroy Kisky init_completion(&dev->cmd_complete); 66082c0de11SChaithrika U S #ifdef CONFIG_CPU_FREQ 66182c0de11SChaithrika U S init_completion(&dev->xfr_complete); 66282c0de11SChaithrika U S #endif 663c4df5000SWolfram Sang dev->dev = &pdev->dev; 66495a7f10eSVladimir Barinov dev->irq = irq->start; 6651e2c2390SOlof Johansson dev->pdata = dev_get_platdata(&pdev->dev); 66695a7f10eSVladimir Barinov platform_set_drvdata(pdev, dev); 66795a7f10eSVladimir Barinov 6685c3d8a46SHeiko Schocher if (!dev->pdata && pdev->dev.of_node) { 6695c3d8a46SHeiko Schocher u32 prop; 6705c3d8a46SHeiko Schocher 6715c3d8a46SHeiko Schocher dev->pdata = devm_kzalloc(&pdev->dev, 6725c3d8a46SHeiko Schocher sizeof(struct davinci_i2c_platform_data), GFP_KERNEL); 673c4df5000SWolfram Sang if (!dev->pdata) 674c4df5000SWolfram Sang return -ENOMEM; 675c4df5000SWolfram Sang 6765c3d8a46SHeiko Schocher memcpy(dev->pdata, &davinci_i2c_platform_data_default, 6775c3d8a46SHeiko Schocher sizeof(struct davinci_i2c_platform_data)); 6785c3d8a46SHeiko Schocher if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency", 6795c3d8a46SHeiko Schocher &prop)) 6805c3d8a46SHeiko Schocher dev->pdata->bus_freq = prop / 1000; 6815c3d8a46SHeiko Schocher } else if (!dev->pdata) { 6825c3d8a46SHeiko Schocher dev->pdata = &davinci_i2c_platform_data_default; 6835c3d8a46SHeiko Schocher } 6845c3d8a46SHeiko Schocher 68585796843SVishwanathrao Badarkhe, Manish dev->clk = devm_clk_get(&pdev->dev, NULL); 686c4df5000SWolfram Sang if (IS_ERR(dev->clk)) 687c4df5000SWolfram Sang return -ENODEV; 6882bdbfa9cSMurali Karicheri clk_prepare_enable(dev->clk); 68995a7f10eSVladimir Barinov 6903cc2d009SWolfram Sang mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 69185796843SVishwanathrao Badarkhe, Manish dev->base = devm_ioremap_resource(&pdev->dev, mem); 69285796843SVishwanathrao Badarkhe, Manish if (IS_ERR(dev->base)) { 69385796843SVishwanathrao Badarkhe, Manish r = PTR_ERR(dev->base); 69485796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks; 695c062a251SChaithrika U S } 696c062a251SChaithrika U S 69795a7f10eSVladimir Barinov i2c_davinci_init(dev); 69895a7f10eSVladimir Barinov 69985796843SVishwanathrao Badarkhe, Manish r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0, 70085796843SVishwanathrao Badarkhe, Manish pdev->name, dev); 70195a7f10eSVladimir Barinov if (r) { 70295a7f10eSVladimir Barinov dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); 70395a7f10eSVladimir Barinov goto err_unuse_clocks; 70495a7f10eSVladimir Barinov } 70595a7f10eSVladimir Barinov 70682c0de11SChaithrika U S r = i2c_davinci_cpufreq_register(dev); 70782c0de11SChaithrika U S if (r) { 70882c0de11SChaithrika U S dev_err(&pdev->dev, "failed to register cpufreq\n"); 70985796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks; 71082c0de11SChaithrika U S } 71182c0de11SChaithrika U S 71295a7f10eSVladimir Barinov adap = &dev->adapter; 71395a7f10eSVladimir Barinov i2c_set_adapdata(adap, dev); 71495a7f10eSVladimir Barinov adap->owner = THIS_MODULE; 715*adcb82a9SWolfram Sang adap->class = I2C_CLASS_DEPRECATED; 71695a7f10eSVladimir Barinov strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name)); 71795a7f10eSVladimir Barinov adap->algo = &i2c_davinci_algo; 71895a7f10eSVladimir Barinov adap->dev.parent = &pdev->dev; 71998a679caSJean Delvare adap->timeout = DAVINCI_I2C_TIMEOUT; 7205c3d8a46SHeiko Schocher adap->dev.of_node = pdev->dev.of_node; 72195a7f10eSVladimir Barinov 72295a7f10eSVladimir Barinov adap->nr = pdev->id; 72395a7f10eSVladimir Barinov r = i2c_add_numbered_adapter(adap); 72495a7f10eSVladimir Barinov if (r) { 72595a7f10eSVladimir Barinov dev_err(&pdev->dev, "failure adding adapter\n"); 72685796843SVishwanathrao Badarkhe, Manish goto err_unuse_clocks; 72795a7f10eSVladimir Barinov } 72895a7f10eSVladimir Barinov 72995a7f10eSVladimir Barinov return 0; 73095a7f10eSVladimir Barinov 73195a7f10eSVladimir Barinov err_unuse_clocks: 7322bdbfa9cSMurali Karicheri clk_disable_unprepare(dev->clk); 73395a7f10eSVladimir Barinov dev->clk = NULL; 73495a7f10eSVladimir Barinov return r; 73595a7f10eSVladimir Barinov } 73695a7f10eSVladimir Barinov 73795a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev) 73895a7f10eSVladimir Barinov { 73995a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = platform_get_drvdata(pdev); 74095a7f10eSVladimir Barinov 74182c0de11SChaithrika U S i2c_davinci_cpufreq_deregister(dev); 74282c0de11SChaithrika U S 74395a7f10eSVladimir Barinov i2c_del_adapter(&dev->adapter); 74495a7f10eSVladimir Barinov 7452bdbfa9cSMurali Karicheri clk_disable_unprepare(dev->clk); 74695a7f10eSVladimir Barinov dev->clk = NULL; 74795a7f10eSVladimir Barinov 74895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0); 74995a7f10eSVladimir Barinov 75095a7f10eSVladimir Barinov return 0; 75195a7f10eSVladimir Barinov } 75295a7f10eSVladimir Barinov 75368f15de9SChaithrika U S #ifdef CONFIG_PM 75468f15de9SChaithrika U S static int davinci_i2c_suspend(struct device *dev) 75568f15de9SChaithrika U S { 75668f15de9SChaithrika U S struct platform_device *pdev = to_platform_device(dev); 75768f15de9SChaithrika U S struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 75868f15de9SChaithrika U S 75968f15de9SChaithrika U S /* put I2C into reset */ 76068f15de9SChaithrika U S davinci_i2c_reset_ctrl(i2c_dev, 0); 7612bdbfa9cSMurali Karicheri clk_disable_unprepare(i2c_dev->clk); 76268f15de9SChaithrika U S 76368f15de9SChaithrika U S return 0; 76468f15de9SChaithrika U S } 76568f15de9SChaithrika U S 76668f15de9SChaithrika U S static int davinci_i2c_resume(struct device *dev) 76768f15de9SChaithrika U S { 76868f15de9SChaithrika U S struct platform_device *pdev = to_platform_device(dev); 76968f15de9SChaithrika U S struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 77068f15de9SChaithrika U S 7712bdbfa9cSMurali Karicheri clk_prepare_enable(i2c_dev->clk); 77268f15de9SChaithrika U S /* take I2C out of reset */ 77368f15de9SChaithrika U S davinci_i2c_reset_ctrl(i2c_dev, 1); 77468f15de9SChaithrika U S 77568f15de9SChaithrika U S return 0; 77668f15de9SChaithrika U S } 77768f15de9SChaithrika U S 77868f15de9SChaithrika U S static const struct dev_pm_ops davinci_i2c_pm = { 77968f15de9SChaithrika U S .suspend = davinci_i2c_suspend, 78068f15de9SChaithrika U S .resume = davinci_i2c_resume, 78168f15de9SChaithrika U S }; 78268f15de9SChaithrika U S 78368f15de9SChaithrika U S #define davinci_i2c_pm_ops (&davinci_i2c_pm) 78468f15de9SChaithrika U S #else 78568f15de9SChaithrika U S #define davinci_i2c_pm_ops NULL 78668f15de9SChaithrika U S #endif 78768f15de9SChaithrika U S 788add8eda7SKay Sievers /* work with hotplug and coldplug */ 789add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci"); 790add8eda7SKay Sievers 79195a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = { 79295a7f10eSVladimir Barinov .probe = davinci_i2c_probe, 79395a7f10eSVladimir Barinov .remove = davinci_i2c_remove, 79495a7f10eSVladimir Barinov .driver = { 79595a7f10eSVladimir Barinov .name = "i2c_davinci", 79695a7f10eSVladimir Barinov .owner = THIS_MODULE, 79768f15de9SChaithrika U S .pm = davinci_i2c_pm_ops, 7984e905323SSachin Kamat .of_match_table = davinci_i2c_of_match, 79995a7f10eSVladimir Barinov }, 80095a7f10eSVladimir Barinov }; 80195a7f10eSVladimir Barinov 80295a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */ 80395a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void) 80495a7f10eSVladimir Barinov { 80595a7f10eSVladimir Barinov return platform_driver_register(&davinci_i2c_driver); 80695a7f10eSVladimir Barinov } 80795a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver); 80895a7f10eSVladimir Barinov 80995a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void) 81095a7f10eSVladimir Barinov { 81195a7f10eSVladimir Barinov platform_driver_unregister(&davinci_i2c_driver); 81295a7f10eSVladimir Barinov } 81395a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver); 81495a7f10eSVladimir Barinov 81595a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India"); 81695a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter"); 81795a7f10eSVladimir Barinov MODULE_LICENSE("GPL"); 818