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> 3995a7f10eSVladimir Barinov 40a09e64fbSRussell King #include <mach/hardware.h> 41a09e64fbSRussell King #include <mach/i2c.h> 4295a7f10eSVladimir Barinov 4395a7f10eSVladimir Barinov /* ----- global defines ----------------------------------------------- */ 4495a7f10eSVladimir Barinov 4595a7f10eSVladimir Barinov #define DAVINCI_I2C_TIMEOUT (1*HZ) 4695a7f10eSVladimir Barinov #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \ 4795a7f10eSVladimir Barinov DAVINCI_I2C_IMR_SCD | \ 4895a7f10eSVladimir Barinov DAVINCI_I2C_IMR_ARDY | \ 4995a7f10eSVladimir Barinov DAVINCI_I2C_IMR_NACK | \ 5095a7f10eSVladimir Barinov DAVINCI_I2C_IMR_AL) 5195a7f10eSVladimir Barinov 5295a7f10eSVladimir Barinov #define DAVINCI_I2C_OAR_REG 0x00 5395a7f10eSVladimir Barinov #define DAVINCI_I2C_IMR_REG 0x04 5495a7f10eSVladimir Barinov #define DAVINCI_I2C_STR_REG 0x08 5595a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKL_REG 0x0c 5695a7f10eSVladimir Barinov #define DAVINCI_I2C_CLKH_REG 0x10 5795a7f10eSVladimir Barinov #define DAVINCI_I2C_CNT_REG 0x14 5895a7f10eSVladimir Barinov #define DAVINCI_I2C_DRR_REG 0x18 5995a7f10eSVladimir Barinov #define DAVINCI_I2C_SAR_REG 0x1c 6095a7f10eSVladimir Barinov #define DAVINCI_I2C_DXR_REG 0x20 6195a7f10eSVladimir Barinov #define DAVINCI_I2C_MDR_REG 0x24 6295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_REG 0x28 6395a7f10eSVladimir Barinov #define DAVINCI_I2C_EMDR_REG 0x2c 6495a7f10eSVladimir Barinov #define DAVINCI_I2C_PSC_REG 0x30 6595a7f10eSVladimir Barinov 6695a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AAS 0x07 6795a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_SCD 0x06 6895a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_XRDY 0x05 6995a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_RDR 0x04 7095a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_ARDY 0x03 7195a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_NACK 0x02 7295a7f10eSVladimir Barinov #define DAVINCI_I2C_IVR_AL 0x01 7395a7f10eSVladimir Barinov 74c062a251SChaithrika U S #define DAVINCI_I2C_STR_BB BIT(12) 75c062a251SChaithrika U S #define DAVINCI_I2C_STR_RSFULL BIT(11) 76c062a251SChaithrika U S #define DAVINCI_I2C_STR_SCD BIT(5) 77c062a251SChaithrika U S #define DAVINCI_I2C_STR_ARDY BIT(2) 78c062a251SChaithrika U S #define DAVINCI_I2C_STR_NACK BIT(1) 79c062a251SChaithrika U S #define DAVINCI_I2C_STR_AL BIT(0) 8095a7f10eSVladimir Barinov 81c062a251SChaithrika U S #define DAVINCI_I2C_MDR_NACK BIT(15) 82c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STT BIT(13) 83c062a251SChaithrika U S #define DAVINCI_I2C_MDR_STP BIT(11) 84c062a251SChaithrika U S #define DAVINCI_I2C_MDR_MST BIT(10) 85c062a251SChaithrika U S #define DAVINCI_I2C_MDR_TRX BIT(9) 86c062a251SChaithrika U S #define DAVINCI_I2C_MDR_XA BIT(8) 87c062a251SChaithrika U S #define DAVINCI_I2C_MDR_RM BIT(7) 88c062a251SChaithrika U S #define DAVINCI_I2C_MDR_IRS BIT(5) 8995a7f10eSVladimir Barinov 90c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AAS BIT(6) 91c062a251SChaithrika U S #define DAVINCI_I2C_IMR_SCD BIT(5) 92c062a251SChaithrika U S #define DAVINCI_I2C_IMR_XRDY BIT(4) 93c062a251SChaithrika U S #define DAVINCI_I2C_IMR_RRDY BIT(3) 94c062a251SChaithrika U S #define DAVINCI_I2C_IMR_ARDY BIT(2) 95c062a251SChaithrika U S #define DAVINCI_I2C_IMR_NACK BIT(1) 96c062a251SChaithrika U S #define DAVINCI_I2C_IMR_AL BIT(0) 9795a7f10eSVladimir Barinov 9895a7f10eSVladimir Barinov struct davinci_i2c_dev { 9995a7f10eSVladimir Barinov struct device *dev; 10095a7f10eSVladimir Barinov void __iomem *base; 10195a7f10eSVladimir Barinov struct completion cmd_complete; 10295a7f10eSVladimir Barinov struct clk *clk; 10395a7f10eSVladimir Barinov int cmd_err; 10495a7f10eSVladimir Barinov u8 *buf; 10595a7f10eSVladimir Barinov size_t buf_len; 10695a7f10eSVladimir Barinov int irq; 107c6c7c729SDirk Behme int stop; 1085a0d5f5fSTroy Kisky u8 terminate; 10995a7f10eSVladimir Barinov struct i2c_adapter adapter; 11095a7f10eSVladimir Barinov }; 11195a7f10eSVladimir Barinov 11295a7f10eSVladimir Barinov /* default platform data to use if not supplied in the platform_device */ 11395a7f10eSVladimir Barinov static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = { 11495a7f10eSVladimir Barinov .bus_freq = 100, 11595a7f10eSVladimir Barinov .bus_delay = 0, 11695a7f10eSVladimir Barinov }; 11795a7f10eSVladimir Barinov 11895a7f10eSVladimir Barinov static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev, 11995a7f10eSVladimir Barinov int reg, u16 val) 12095a7f10eSVladimir Barinov { 12195a7f10eSVladimir Barinov __raw_writew(val, i2c_dev->base + reg); 12295a7f10eSVladimir Barinov } 12395a7f10eSVladimir Barinov 12495a7f10eSVladimir Barinov static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg) 12595a7f10eSVladimir Barinov { 12695a7f10eSVladimir Barinov return __raw_readw(i2c_dev->base + reg); 12795a7f10eSVladimir Barinov } 12895a7f10eSVladimir Barinov 129*5ae5b113SChaithrika U S static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev, 130*5ae5b113SChaithrika U S int val) 131*5ae5b113SChaithrika U S { 132*5ae5b113SChaithrika U S u16 w; 133*5ae5b113SChaithrika U S 134*5ae5b113SChaithrika U S w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG); 135*5ae5b113SChaithrika U S if (!val) /* put I2C into reset */ 136*5ae5b113SChaithrika U S w &= ~DAVINCI_I2C_MDR_IRS; 137*5ae5b113SChaithrika U S else /* take I2C out of reset */ 138*5ae5b113SChaithrika U S w |= DAVINCI_I2C_MDR_IRS; 139*5ae5b113SChaithrika U S 140*5ae5b113SChaithrika U S davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w); 141*5ae5b113SChaithrika U S } 142*5ae5b113SChaithrika U S 143*5ae5b113SChaithrika U S static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev) 14495a7f10eSVladimir Barinov { 14595a7f10eSVladimir Barinov struct davinci_i2c_platform_data *pdata = dev->dev->platform_data; 14695a7f10eSVladimir Barinov u16 psc; 14795a7f10eSVladimir Barinov u32 clk; 148cc99ff70STroy Kisky u32 d; 14995a7f10eSVladimir Barinov u32 clkh; 15095a7f10eSVladimir Barinov u32 clkl; 15195a7f10eSVladimir Barinov u32 input_clock = clk_get_rate(dev->clk); 15295a7f10eSVladimir Barinov 15395a7f10eSVladimir Barinov /* NOTE: I2C Clock divider programming info 15495a7f10eSVladimir Barinov * As per I2C specs the following formulas provide prescaler 15595a7f10eSVladimir Barinov * and low/high divider values 15695a7f10eSVladimir Barinov * input clk --> PSC Div -----------> ICCL/H Div --> output clock 15795a7f10eSVladimir Barinov * module clk 15895a7f10eSVladimir Barinov * 15995a7f10eSVladimir Barinov * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ] 16095a7f10eSVladimir Barinov * 16195a7f10eSVladimir Barinov * Thus, 16295a7f10eSVladimir Barinov * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d; 16395a7f10eSVladimir Barinov * 16495a7f10eSVladimir Barinov * where if PSC == 0, d = 7, 16595a7f10eSVladimir Barinov * if PSC == 1, d = 6 16695a7f10eSVladimir Barinov * if PSC > 1 , d = 5 16795a7f10eSVladimir Barinov */ 16895a7f10eSVladimir Barinov 169cc99ff70STroy Kisky /* get minimum of 7 MHz clock, but max of 12 MHz */ 170cc99ff70STroy Kisky psc = (input_clock / 7000000) - 1; 171cc99ff70STroy Kisky if ((input_clock / (psc + 1)) > 12000000) 172cc99ff70STroy Kisky psc++; /* better to run under spec than over */ 173cc99ff70STroy Kisky d = (psc >= 2) ? 5 : 7 - psc; 17495a7f10eSVladimir Barinov 175cc99ff70STroy Kisky clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1); 176cc99ff70STroy Kisky clkh = clk >> 1; 17795a7f10eSVladimir Barinov clkl = clk - clkh; 17895a7f10eSVladimir Barinov 17995a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc); 18095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh); 18195a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl); 18295a7f10eSVladimir Barinov 183*5ae5b113SChaithrika U S dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk); 184*5ae5b113SChaithrika U S } 185*5ae5b113SChaithrika U S 186*5ae5b113SChaithrika U S /* 187*5ae5b113SChaithrika U S * This function configures I2C and brings I2C out of reset. 188*5ae5b113SChaithrika U S * This function is called during I2C init function. This function 189*5ae5b113SChaithrika U S * also gets called if I2C encounters any errors. 190*5ae5b113SChaithrika U S */ 191*5ae5b113SChaithrika U S static int i2c_davinci_init(struct davinci_i2c_dev *dev) 192*5ae5b113SChaithrika U S { 193*5ae5b113SChaithrika U S struct davinci_i2c_platform_data *pdata = dev->dev->platform_data; 194*5ae5b113SChaithrika U S 195*5ae5b113SChaithrika U S if (!pdata) 196*5ae5b113SChaithrika U S pdata = &davinci_i2c_platform_data_default; 197*5ae5b113SChaithrika U S 198*5ae5b113SChaithrika U S /* put I2C into reset */ 199*5ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 0); 200*5ae5b113SChaithrika U S 201*5ae5b113SChaithrika U S /* compute clock dividers */ 202*5ae5b113SChaithrika U S i2c_davinci_calc_clk_dividers(dev); 203*5ae5b113SChaithrika U S 2047605fa3bSDavid Brownell /* Respond at reserved "SMBus Host" slave address" (and zero); 2057605fa3bSDavid Brownell * we seem to have no option to not respond... 2067605fa3bSDavid Brownell */ 2077605fa3bSDavid Brownell davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08); 2087605fa3bSDavid Brownell 20995a7f10eSVladimir Barinov dev_dbg(dev->dev, "PSC = %d\n", 21095a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG)); 21195a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKL = %d\n", 21295a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG)); 21395a7f10eSVladimir Barinov dev_dbg(dev->dev, "CLKH = %d\n", 21495a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG)); 215cc99ff70STroy Kisky dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n", 216cc99ff70STroy Kisky pdata->bus_freq, pdata->bus_delay); 21795a7f10eSVladimir Barinov 21895a7f10eSVladimir Barinov /* Take the I2C module out of reset: */ 219*5ae5b113SChaithrika U S davinci_i2c_reset_ctrl(dev, 1); 22095a7f10eSVladimir Barinov 22195a7f10eSVladimir Barinov /* Enable interrupts */ 22295a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL); 22395a7f10eSVladimir Barinov 22495a7f10eSVladimir Barinov return 0; 22595a7f10eSVladimir Barinov } 22695a7f10eSVladimir Barinov 22795a7f10eSVladimir Barinov /* 22895a7f10eSVladimir Barinov * Waiting for bus not busy 22995a7f10eSVladimir Barinov */ 23095a7f10eSVladimir Barinov static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev, 23195a7f10eSVladimir Barinov char allow_sleep) 23295a7f10eSVladimir Barinov { 23395a7f10eSVladimir Barinov unsigned long timeout; 23495a7f10eSVladimir Barinov 23598a679caSJean Delvare timeout = jiffies + dev->adapter.timeout; 23695a7f10eSVladimir Barinov while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) 23795a7f10eSVladimir Barinov & DAVINCI_I2C_STR_BB) { 23895a7f10eSVladimir Barinov if (time_after(jiffies, timeout)) { 23995a7f10eSVladimir Barinov dev_warn(dev->dev, 24095a7f10eSVladimir Barinov "timeout waiting for bus ready\n"); 24195a7f10eSVladimir Barinov return -ETIMEDOUT; 24295a7f10eSVladimir Barinov } 24395a7f10eSVladimir Barinov if (allow_sleep) 24495a7f10eSVladimir Barinov schedule_timeout(1); 24595a7f10eSVladimir Barinov } 24695a7f10eSVladimir Barinov 24795a7f10eSVladimir Barinov return 0; 24895a7f10eSVladimir Barinov } 24995a7f10eSVladimir Barinov 25095a7f10eSVladimir Barinov /* 25195a7f10eSVladimir Barinov * Low level master read/write transaction. This function is called 25295a7f10eSVladimir Barinov * from i2c_davinci_xfer. 25395a7f10eSVladimir Barinov */ 25495a7f10eSVladimir Barinov static int 25595a7f10eSVladimir Barinov i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop) 25695a7f10eSVladimir Barinov { 25795a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 25895a7f10eSVladimir Barinov struct davinci_i2c_platform_data *pdata = dev->dev->platform_data; 25995a7f10eSVladimir Barinov u32 flag; 26095a7f10eSVladimir Barinov u16 w; 26195a7f10eSVladimir Barinov int r; 26295a7f10eSVladimir Barinov 26395a7f10eSVladimir Barinov if (!pdata) 26495a7f10eSVladimir Barinov pdata = &davinci_i2c_platform_data_default; 26595a7f10eSVladimir Barinov /* Introduce a delay, required for some boards (e.g Davinci EVM) */ 26695a7f10eSVladimir Barinov if (pdata->bus_delay) 26795a7f10eSVladimir Barinov udelay(pdata->bus_delay); 26895a7f10eSVladimir Barinov 26995a7f10eSVladimir Barinov /* set the slave address */ 27095a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr); 27195a7f10eSVladimir Barinov 27295a7f10eSVladimir Barinov dev->buf = msg->buf; 27395a7f10eSVladimir Barinov dev->buf_len = msg->len; 274c6c7c729SDirk Behme dev->stop = stop; 27595a7f10eSVladimir Barinov 27695a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len); 27795a7f10eSVladimir Barinov 2782e743787STroy Kisky INIT_COMPLETION(dev->cmd_complete); 27995a7f10eSVladimir Barinov dev->cmd_err = 0; 28095a7f10eSVladimir Barinov 28195a7f10eSVladimir Barinov /* Take I2C out of reset, configure it as master and set the 28295a7f10eSVladimir Barinov * start bit */ 28395a7f10eSVladimir Barinov flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST | DAVINCI_I2C_MDR_STT; 28495a7f10eSVladimir Barinov 28595a7f10eSVladimir Barinov /* if the slave address is ten bit address, enable XA bit */ 28695a7f10eSVladimir Barinov if (msg->flags & I2C_M_TEN) 28795a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_XA; 28895a7f10eSVladimir Barinov if (!(msg->flags & I2C_M_RD)) 28995a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_TRX; 29095a7f10eSVladimir Barinov if (stop) 29195a7f10eSVladimir Barinov flag |= DAVINCI_I2C_MDR_STP; 292c6c7c729SDirk Behme if (msg->len == 0) { 293c6c7c729SDirk Behme flag |= DAVINCI_I2C_MDR_RM; 294c6c7c729SDirk Behme flag &= ~DAVINCI_I2C_MDR_STP; 295c6c7c729SDirk Behme } 29695a7f10eSVladimir Barinov 29795a7f10eSVladimir Barinov /* Enable receive or transmit interrupts */ 29895a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG); 29995a7f10eSVladimir Barinov if (msg->flags & I2C_M_RD) 300c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_RRDY; 30195a7f10eSVladimir Barinov else 302c062a251SChaithrika U S w |= DAVINCI_I2C_IMR_XRDY; 30395a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w); 30495a7f10eSVladimir Barinov 3055a0d5f5fSTroy Kisky dev->terminate = 0; 306c6c7c729SDirk Behme 30795a7f10eSVladimir Barinov /* write the data into mode register */ 30895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); 30995a7f10eSVladimir Barinov 310c6c7c729SDirk Behme /* 311c6c7c729SDirk Behme * First byte should be set here, not after interrupt, 312c6c7c729SDirk Behme * because transmit-data-ready interrupt can come before 313c6c7c729SDirk Behme * NACK-interrupt during sending of previous message and 314c6c7c729SDirk Behme * ICDXR may have wrong data 315c6c7c729SDirk Behme */ 316c6c7c729SDirk Behme if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) { 317c6c7c729SDirk Behme davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++); 318c6c7c729SDirk Behme dev->buf_len--; 319c6c7c729SDirk Behme } 320c6c7c729SDirk Behme 32195a7f10eSVladimir Barinov r = wait_for_completion_interruptible_timeout(&dev->cmd_complete, 32298a679caSJean Delvare dev->adapter.timeout); 32395a7f10eSVladimir Barinov if (r == 0) { 32495a7f10eSVladimir Barinov dev_err(dev->dev, "controller timed out\n"); 32595a7f10eSVladimir Barinov i2c_davinci_init(dev); 3265a0d5f5fSTroy Kisky dev->buf_len = 0; 32795a7f10eSVladimir Barinov return -ETIMEDOUT; 32895a7f10eSVladimir Barinov } 3295a0d5f5fSTroy Kisky if (dev->buf_len) { 3305a0d5f5fSTroy Kisky /* This should be 0 if all bytes were transferred 3315a0d5f5fSTroy Kisky * or dev->cmd_err denotes an error. 3325a0d5f5fSTroy Kisky * A signal may have aborted the transfer. 3335a0d5f5fSTroy Kisky */ 3345a0d5f5fSTroy Kisky if (r >= 0) { 3355a0d5f5fSTroy Kisky dev_err(dev->dev, "abnormal termination buf_len=%i\n", 3365a0d5f5fSTroy Kisky dev->buf_len); 3375a0d5f5fSTroy Kisky r = -EREMOTEIO; 3385a0d5f5fSTroy Kisky } 3395a0d5f5fSTroy Kisky dev->terminate = 1; 3405a0d5f5fSTroy Kisky wmb(); 3415a0d5f5fSTroy Kisky dev->buf_len = 0; 3425a0d5f5fSTroy Kisky } 3435a0d5f5fSTroy Kisky if (r < 0) 3445a0d5f5fSTroy Kisky return r; 34595a7f10eSVladimir Barinov 34695a7f10eSVladimir Barinov /* no error */ 34795a7f10eSVladimir Barinov if (likely(!dev->cmd_err)) 34895a7f10eSVladimir Barinov return msg->len; 34995a7f10eSVladimir Barinov 35095a7f10eSVladimir Barinov /* We have an error */ 35195a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_AL) { 35295a7f10eSVladimir Barinov i2c_davinci_init(dev); 35395a7f10eSVladimir Barinov return -EIO; 35495a7f10eSVladimir Barinov } 35595a7f10eSVladimir Barinov 35695a7f10eSVladimir Barinov if (dev->cmd_err & DAVINCI_I2C_STR_NACK) { 35795a7f10eSVladimir Barinov if (msg->flags & I2C_M_IGNORE_NAK) 35895a7f10eSVladimir Barinov return msg->len; 35995a7f10eSVladimir Barinov if (stop) { 36095a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 361c062a251SChaithrika U S w |= DAVINCI_I2C_MDR_STP; 36295a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 36395a7f10eSVladimir Barinov } 36495a7f10eSVladimir Barinov return -EREMOTEIO; 36595a7f10eSVladimir Barinov } 36695a7f10eSVladimir Barinov return -EIO; 36795a7f10eSVladimir Barinov } 36895a7f10eSVladimir Barinov 36995a7f10eSVladimir Barinov /* 37095a7f10eSVladimir Barinov * Prepare controller for a transaction and call i2c_davinci_xfer_msg 37195a7f10eSVladimir Barinov */ 37295a7f10eSVladimir Barinov static int 37395a7f10eSVladimir Barinov i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) 37495a7f10eSVladimir Barinov { 37595a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); 37695a7f10eSVladimir Barinov int i; 37795a7f10eSVladimir Barinov int ret; 37895a7f10eSVladimir Barinov 37908882d20SHarvey Harrison dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); 38095a7f10eSVladimir Barinov 38195a7f10eSVladimir Barinov ret = i2c_davinci_wait_bus_not_busy(dev, 1); 38295a7f10eSVladimir Barinov if (ret < 0) { 38395a7f10eSVladimir Barinov dev_warn(dev->dev, "timeout waiting for bus ready\n"); 38495a7f10eSVladimir Barinov return ret; 38595a7f10eSVladimir Barinov } 38695a7f10eSVladimir Barinov 38795a7f10eSVladimir Barinov for (i = 0; i < num; i++) { 38895a7f10eSVladimir Barinov ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1))); 389d868caa1STroy Kisky dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num, 390d868caa1STroy Kisky ret); 39195a7f10eSVladimir Barinov if (ret < 0) 39295a7f10eSVladimir Barinov return ret; 39395a7f10eSVladimir Barinov } 39495a7f10eSVladimir Barinov return num; 39595a7f10eSVladimir Barinov } 39695a7f10eSVladimir Barinov 39795a7f10eSVladimir Barinov static u32 i2c_davinci_func(struct i2c_adapter *adap) 39895a7f10eSVladimir Barinov { 399c6c7c729SDirk Behme return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 40095a7f10eSVladimir Barinov } 40195a7f10eSVladimir Barinov 4025a0d5f5fSTroy Kisky static void terminate_read(struct davinci_i2c_dev *dev) 4035a0d5f5fSTroy Kisky { 4045a0d5f5fSTroy Kisky u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 4055a0d5f5fSTroy Kisky w |= DAVINCI_I2C_MDR_NACK; 4065a0d5f5fSTroy Kisky davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 4075a0d5f5fSTroy Kisky 4085a0d5f5fSTroy Kisky /* Throw away data */ 4095a0d5f5fSTroy Kisky davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG); 4105a0d5f5fSTroy Kisky if (!dev->terminate) 4115a0d5f5fSTroy Kisky dev_err(dev->dev, "RDR IRQ while no data requested\n"); 4125a0d5f5fSTroy Kisky } 4135a0d5f5fSTroy Kisky static void terminate_write(struct davinci_i2c_dev *dev) 4145a0d5f5fSTroy Kisky { 4155a0d5f5fSTroy Kisky u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); 4165a0d5f5fSTroy Kisky w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP; 4175a0d5f5fSTroy Kisky davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); 4185a0d5f5fSTroy Kisky 4195a0d5f5fSTroy Kisky if (!dev->terminate) 4207605fa3bSDavid Brownell dev_dbg(dev->dev, "TDR IRQ while no data to send\n"); 4215a0d5f5fSTroy Kisky } 4225a0d5f5fSTroy Kisky 42395a7f10eSVladimir Barinov /* 42495a7f10eSVladimir Barinov * Interrupt service routine. This gets called whenever an I2C interrupt 42595a7f10eSVladimir Barinov * occurs. 42695a7f10eSVladimir Barinov */ 42795a7f10eSVladimir Barinov static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id) 42895a7f10eSVladimir Barinov { 42995a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = dev_id; 43095a7f10eSVladimir Barinov u32 stat; 43195a7f10eSVladimir Barinov int count = 0; 43295a7f10eSVladimir Barinov u16 w; 43395a7f10eSVladimir Barinov 43495a7f10eSVladimir Barinov while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) { 43508882d20SHarvey Harrison dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat); 43695a7f10eSVladimir Barinov if (count++ == 100) { 43795a7f10eSVladimir Barinov dev_warn(dev->dev, "Too much work in one IRQ\n"); 43895a7f10eSVladimir Barinov break; 43995a7f10eSVladimir Barinov } 44095a7f10eSVladimir Barinov 44195a7f10eSVladimir Barinov switch (stat) { 44295a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_AL: 4435a0d5f5fSTroy Kisky /* Arbitration lost, must retry */ 44495a7f10eSVladimir Barinov dev->cmd_err |= DAVINCI_I2C_STR_AL; 4455a0d5f5fSTroy Kisky dev->buf_len = 0; 44695a7f10eSVladimir Barinov complete(&dev->cmd_complete); 44795a7f10eSVladimir Barinov break; 44895a7f10eSVladimir Barinov 44995a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_NACK: 45095a7f10eSVladimir Barinov dev->cmd_err |= DAVINCI_I2C_STR_NACK; 4515a0d5f5fSTroy Kisky dev->buf_len = 0; 45295a7f10eSVladimir Barinov complete(&dev->cmd_complete); 45395a7f10eSVladimir Barinov break; 45495a7f10eSVladimir Barinov 45595a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_ARDY: 456b73a9aecSTroy Kisky davinci_i2c_write_reg(dev, 457b73a9aecSTroy Kisky DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY); 458c6c7c729SDirk Behme if (((dev->buf_len == 0) && (dev->stop != 0)) || 459c6c7c729SDirk Behme (dev->cmd_err & DAVINCI_I2C_STR_NACK)) { 460c6c7c729SDirk Behme w = davinci_i2c_read_reg(dev, 461c6c7c729SDirk Behme DAVINCI_I2C_MDR_REG); 462c6c7c729SDirk Behme w |= DAVINCI_I2C_MDR_STP; 463c6c7c729SDirk Behme davinci_i2c_write_reg(dev, 464c6c7c729SDirk Behme DAVINCI_I2C_MDR_REG, w); 465c6c7c729SDirk Behme } 46695a7f10eSVladimir Barinov complete(&dev->cmd_complete); 46795a7f10eSVladimir Barinov break; 46895a7f10eSVladimir Barinov 46995a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_RDR: 47095a7f10eSVladimir Barinov if (dev->buf_len) { 47195a7f10eSVladimir Barinov *dev->buf++ = 47295a7f10eSVladimir Barinov davinci_i2c_read_reg(dev, 47395a7f10eSVladimir Barinov DAVINCI_I2C_DRR_REG); 47495a7f10eSVladimir Barinov dev->buf_len--; 47595a7f10eSVladimir Barinov if (dev->buf_len) 47695a7f10eSVladimir Barinov continue; 47795a7f10eSVladimir Barinov 47895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, 47995a7f10eSVladimir Barinov DAVINCI_I2C_STR_REG, 480b73a9aecSTroy Kisky DAVINCI_I2C_IMR_RRDY); 4815a0d5f5fSTroy Kisky } else { 4825a0d5f5fSTroy Kisky /* signal can terminate transfer */ 4835a0d5f5fSTroy Kisky terminate_read(dev); 4845a0d5f5fSTroy Kisky } 48595a7f10eSVladimir Barinov break; 48695a7f10eSVladimir Barinov 48795a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_XRDY: 48895a7f10eSVladimir Barinov if (dev->buf_len) { 48995a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, 49095a7f10eSVladimir Barinov *dev->buf++); 49195a7f10eSVladimir Barinov dev->buf_len--; 49295a7f10eSVladimir Barinov if (dev->buf_len) 49395a7f10eSVladimir Barinov continue; 49495a7f10eSVladimir Barinov 49595a7f10eSVladimir Barinov w = davinci_i2c_read_reg(dev, 49695a7f10eSVladimir Barinov DAVINCI_I2C_IMR_REG); 497c062a251SChaithrika U S w &= ~DAVINCI_I2C_IMR_XRDY; 49895a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, 49995a7f10eSVladimir Barinov DAVINCI_I2C_IMR_REG, 50095a7f10eSVladimir Barinov w); 5015a0d5f5fSTroy Kisky } else { 5025a0d5f5fSTroy Kisky /* signal can terminate transfer */ 5035a0d5f5fSTroy Kisky terminate_write(dev); 5045a0d5f5fSTroy Kisky } 50595a7f10eSVladimir Barinov break; 50695a7f10eSVladimir Barinov 50795a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_SCD: 508b73a9aecSTroy Kisky davinci_i2c_write_reg(dev, 509b73a9aecSTroy Kisky DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD); 51095a7f10eSVladimir Barinov complete(&dev->cmd_complete); 51195a7f10eSVladimir Barinov break; 51295a7f10eSVladimir Barinov 51395a7f10eSVladimir Barinov case DAVINCI_I2C_IVR_AAS: 5147605fa3bSDavid Brownell dev_dbg(dev->dev, "Address as slave interrupt\n"); 5157605fa3bSDavid Brownell break; 5167605fa3bSDavid Brownell 5177605fa3bSDavid Brownell default: 5187605fa3bSDavid Brownell dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat); 5197605fa3bSDavid Brownell break; 5207605fa3bSDavid Brownell } 5217605fa3bSDavid Brownell } 52295a7f10eSVladimir Barinov 52395a7f10eSVladimir Barinov return count ? IRQ_HANDLED : IRQ_NONE; 52495a7f10eSVladimir Barinov } 52595a7f10eSVladimir Barinov 52695a7f10eSVladimir Barinov static struct i2c_algorithm i2c_davinci_algo = { 52795a7f10eSVladimir Barinov .master_xfer = i2c_davinci_xfer, 52895a7f10eSVladimir Barinov .functionality = i2c_davinci_func, 52995a7f10eSVladimir Barinov }; 53095a7f10eSVladimir Barinov 53195a7f10eSVladimir Barinov static int davinci_i2c_probe(struct platform_device *pdev) 53295a7f10eSVladimir Barinov { 53395a7f10eSVladimir Barinov struct davinci_i2c_dev *dev; 53495a7f10eSVladimir Barinov struct i2c_adapter *adap; 53595a7f10eSVladimir Barinov struct resource *mem, *irq, *ioarea; 53695a7f10eSVladimir Barinov int r; 53795a7f10eSVladimir Barinov 53895a7f10eSVladimir Barinov /* NOTE: driver uses the static register mapping */ 53995a7f10eSVladimir Barinov mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 54095a7f10eSVladimir Barinov if (!mem) { 54195a7f10eSVladimir Barinov dev_err(&pdev->dev, "no mem resource?\n"); 54295a7f10eSVladimir Barinov return -ENODEV; 54395a7f10eSVladimir Barinov } 54495a7f10eSVladimir Barinov 54595a7f10eSVladimir Barinov irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 54695a7f10eSVladimir Barinov if (!irq) { 54795a7f10eSVladimir Barinov dev_err(&pdev->dev, "no irq resource?\n"); 54895a7f10eSVladimir Barinov return -ENODEV; 54995a7f10eSVladimir Barinov } 55095a7f10eSVladimir Barinov 55159330825SJulia Lawall ioarea = request_mem_region(mem->start, resource_size(mem), 55295a7f10eSVladimir Barinov pdev->name); 55395a7f10eSVladimir Barinov if (!ioarea) { 55495a7f10eSVladimir Barinov dev_err(&pdev->dev, "I2C region already claimed\n"); 55595a7f10eSVladimir Barinov return -EBUSY; 55695a7f10eSVladimir Barinov } 55795a7f10eSVladimir Barinov 55895a7f10eSVladimir Barinov dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL); 55995a7f10eSVladimir Barinov if (!dev) { 56095a7f10eSVladimir Barinov r = -ENOMEM; 56195a7f10eSVladimir Barinov goto err_release_region; 56295a7f10eSVladimir Barinov } 56395a7f10eSVladimir Barinov 5642e743787STroy Kisky init_completion(&dev->cmd_complete); 56595a7f10eSVladimir Barinov dev->dev = get_device(&pdev->dev); 56695a7f10eSVladimir Barinov dev->irq = irq->start; 56795a7f10eSVladimir Barinov platform_set_drvdata(pdev, dev); 56895a7f10eSVladimir Barinov 569e164ddeeSKevin Hilman dev->clk = clk_get(&pdev->dev, NULL); 57095a7f10eSVladimir Barinov if (IS_ERR(dev->clk)) { 57195a7f10eSVladimir Barinov r = -ENODEV; 57295a7f10eSVladimir Barinov goto err_free_mem; 57395a7f10eSVladimir Barinov } 57495a7f10eSVladimir Barinov clk_enable(dev->clk); 57595a7f10eSVladimir Barinov 576c062a251SChaithrika U S dev->base = ioremap(mem->start, resource_size(mem)); 577c062a251SChaithrika U S if (!dev->base) { 578c062a251SChaithrika U S r = -EBUSY; 579c062a251SChaithrika U S goto err_mem_ioremap; 580c062a251SChaithrika U S } 581c062a251SChaithrika U S 58295a7f10eSVladimir Barinov i2c_davinci_init(dev); 58395a7f10eSVladimir Barinov 58495a7f10eSVladimir Barinov r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev); 58595a7f10eSVladimir Barinov if (r) { 58695a7f10eSVladimir Barinov dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); 58795a7f10eSVladimir Barinov goto err_unuse_clocks; 58895a7f10eSVladimir Barinov } 58995a7f10eSVladimir Barinov 59095a7f10eSVladimir Barinov adap = &dev->adapter; 59195a7f10eSVladimir Barinov i2c_set_adapdata(adap, dev); 59295a7f10eSVladimir Barinov adap->owner = THIS_MODULE; 59395a7f10eSVladimir Barinov adap->class = I2C_CLASS_HWMON; 59495a7f10eSVladimir Barinov strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name)); 59595a7f10eSVladimir Barinov adap->algo = &i2c_davinci_algo; 59695a7f10eSVladimir Barinov adap->dev.parent = &pdev->dev; 59798a679caSJean Delvare adap->timeout = DAVINCI_I2C_TIMEOUT; 59895a7f10eSVladimir Barinov 59995a7f10eSVladimir Barinov adap->nr = pdev->id; 60095a7f10eSVladimir Barinov r = i2c_add_numbered_adapter(adap); 60195a7f10eSVladimir Barinov if (r) { 60295a7f10eSVladimir Barinov dev_err(&pdev->dev, "failure adding adapter\n"); 60395a7f10eSVladimir Barinov goto err_free_irq; 60495a7f10eSVladimir Barinov } 60595a7f10eSVladimir Barinov 60695a7f10eSVladimir Barinov return 0; 60795a7f10eSVladimir Barinov 60895a7f10eSVladimir Barinov err_free_irq: 60995a7f10eSVladimir Barinov free_irq(dev->irq, dev); 61095a7f10eSVladimir Barinov err_unuse_clocks: 611c062a251SChaithrika U S iounmap(dev->base); 612c062a251SChaithrika U S err_mem_ioremap: 61395a7f10eSVladimir Barinov clk_disable(dev->clk); 61495a7f10eSVladimir Barinov clk_put(dev->clk); 61595a7f10eSVladimir Barinov dev->clk = NULL; 61695a7f10eSVladimir Barinov err_free_mem: 61795a7f10eSVladimir Barinov platform_set_drvdata(pdev, NULL); 61895a7f10eSVladimir Barinov put_device(&pdev->dev); 61995a7f10eSVladimir Barinov kfree(dev); 62095a7f10eSVladimir Barinov err_release_region: 62159330825SJulia Lawall release_mem_region(mem->start, resource_size(mem)); 62295a7f10eSVladimir Barinov 62395a7f10eSVladimir Barinov return r; 62495a7f10eSVladimir Barinov } 62595a7f10eSVladimir Barinov 62695a7f10eSVladimir Barinov static int davinci_i2c_remove(struct platform_device *pdev) 62795a7f10eSVladimir Barinov { 62895a7f10eSVladimir Barinov struct davinci_i2c_dev *dev = platform_get_drvdata(pdev); 62995a7f10eSVladimir Barinov struct resource *mem; 63095a7f10eSVladimir Barinov 63195a7f10eSVladimir Barinov platform_set_drvdata(pdev, NULL); 63295a7f10eSVladimir Barinov i2c_del_adapter(&dev->adapter); 63395a7f10eSVladimir Barinov put_device(&pdev->dev); 63495a7f10eSVladimir Barinov 63595a7f10eSVladimir Barinov clk_disable(dev->clk); 63695a7f10eSVladimir Barinov clk_put(dev->clk); 63795a7f10eSVladimir Barinov dev->clk = NULL; 63895a7f10eSVladimir Barinov 63995a7f10eSVladimir Barinov davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0); 64095a7f10eSVladimir Barinov free_irq(IRQ_I2C, dev); 641c062a251SChaithrika U S iounmap(dev->base); 64295a7f10eSVladimir Barinov kfree(dev); 64395a7f10eSVladimir Barinov 64495a7f10eSVladimir Barinov mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 64559330825SJulia Lawall release_mem_region(mem->start, resource_size(mem)); 64695a7f10eSVladimir Barinov return 0; 64795a7f10eSVladimir Barinov } 64895a7f10eSVladimir Barinov 649add8eda7SKay Sievers /* work with hotplug and coldplug */ 650add8eda7SKay Sievers MODULE_ALIAS("platform:i2c_davinci"); 651add8eda7SKay Sievers 65295a7f10eSVladimir Barinov static struct platform_driver davinci_i2c_driver = { 65395a7f10eSVladimir Barinov .probe = davinci_i2c_probe, 65495a7f10eSVladimir Barinov .remove = davinci_i2c_remove, 65595a7f10eSVladimir Barinov .driver = { 65695a7f10eSVladimir Barinov .name = "i2c_davinci", 65795a7f10eSVladimir Barinov .owner = THIS_MODULE, 65895a7f10eSVladimir Barinov }, 65995a7f10eSVladimir Barinov }; 66095a7f10eSVladimir Barinov 66195a7f10eSVladimir Barinov /* I2C may be needed to bring up other drivers */ 66295a7f10eSVladimir Barinov static int __init davinci_i2c_init_driver(void) 66395a7f10eSVladimir Barinov { 66495a7f10eSVladimir Barinov return platform_driver_register(&davinci_i2c_driver); 66595a7f10eSVladimir Barinov } 66695a7f10eSVladimir Barinov subsys_initcall(davinci_i2c_init_driver); 66795a7f10eSVladimir Barinov 66895a7f10eSVladimir Barinov static void __exit davinci_i2c_exit_driver(void) 66995a7f10eSVladimir Barinov { 67095a7f10eSVladimir Barinov platform_driver_unregister(&davinci_i2c_driver); 67195a7f10eSVladimir Barinov } 67295a7f10eSVladimir Barinov module_exit(davinci_i2c_exit_driver); 67395a7f10eSVladimir Barinov 67495a7f10eSVladimir Barinov MODULE_AUTHOR("Texas Instruments India"); 67595a7f10eSVladimir Barinov MODULE_DESCRIPTION("TI DaVinci I2C bus adapter"); 67695a7f10eSVladimir Barinov MODULE_LICENSE("GPL"); 677