Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * TI DAVINCI I2C adapter driver. |
| 3 | * |
| 4 | * Copyright (C) 2006 Texas Instruments. |
| 5 | * Copyright (C) 2007 MontaVista Software Inc. |
| 6 | * |
| 7 | * Updated by Vinod & Sudhakar Feb 2005 |
| 8 | * |
| 9 | * ---------------------------------------------------------------------------- |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 20 | * ---------------------------------------------------------------------------- |
| 21 | * |
| 22 | */ |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/clk.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/sched.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Chaithrika U S | 82c0de1 | 2010-01-06 14:55:00 +0530 | [diff] [blame] | 35 | #include <linux/cpufreq.h> |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 36 | #include <linux/gpio.h> |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 37 | #include <linux/of_device.h> |
Arnd Bergmann | ec2a083 | 2012-08-24 15:11:34 +0200 | [diff] [blame] | 38 | #include <linux/platform_data/i2c-davinci.h> |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 39 | |
| 40 | /* ----- global defines ----------------------------------------------- */ |
| 41 | |
| 42 | #define DAVINCI_I2C_TIMEOUT (1*HZ) |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 43 | #define DAVINCI_I2C_MAX_TRIES 2 |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 44 | #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \ |
| 45 | DAVINCI_I2C_IMR_SCD | \ |
| 46 | DAVINCI_I2C_IMR_ARDY | \ |
| 47 | DAVINCI_I2C_IMR_NACK | \ |
| 48 | DAVINCI_I2C_IMR_AL) |
| 49 | |
| 50 | #define DAVINCI_I2C_OAR_REG 0x00 |
| 51 | #define DAVINCI_I2C_IMR_REG 0x04 |
| 52 | #define DAVINCI_I2C_STR_REG 0x08 |
| 53 | #define DAVINCI_I2C_CLKL_REG 0x0c |
| 54 | #define DAVINCI_I2C_CLKH_REG 0x10 |
| 55 | #define DAVINCI_I2C_CNT_REG 0x14 |
| 56 | #define DAVINCI_I2C_DRR_REG 0x18 |
| 57 | #define DAVINCI_I2C_SAR_REG 0x1c |
| 58 | #define DAVINCI_I2C_DXR_REG 0x20 |
| 59 | #define DAVINCI_I2C_MDR_REG 0x24 |
| 60 | #define DAVINCI_I2C_IVR_REG 0x28 |
| 61 | #define DAVINCI_I2C_EMDR_REG 0x2c |
| 62 | #define DAVINCI_I2C_PSC_REG 0x30 |
| 63 | |
| 64 | #define DAVINCI_I2C_IVR_AAS 0x07 |
| 65 | #define DAVINCI_I2C_IVR_SCD 0x06 |
| 66 | #define DAVINCI_I2C_IVR_XRDY 0x05 |
| 67 | #define DAVINCI_I2C_IVR_RDR 0x04 |
| 68 | #define DAVINCI_I2C_IVR_ARDY 0x03 |
| 69 | #define DAVINCI_I2C_IVR_NACK 0x02 |
| 70 | #define DAVINCI_I2C_IVR_AL 0x01 |
| 71 | |
Chaithrika U S | c062a25 | 2010-01-06 14:54:57 +0530 | [diff] [blame] | 72 | #define DAVINCI_I2C_STR_BB BIT(12) |
| 73 | #define DAVINCI_I2C_STR_RSFULL BIT(11) |
| 74 | #define DAVINCI_I2C_STR_SCD BIT(5) |
| 75 | #define DAVINCI_I2C_STR_ARDY BIT(2) |
| 76 | #define DAVINCI_I2C_STR_NACK BIT(1) |
| 77 | #define DAVINCI_I2C_STR_AL BIT(0) |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 78 | |
Chaithrika U S | c062a25 | 2010-01-06 14:54:57 +0530 | [diff] [blame] | 79 | #define DAVINCI_I2C_MDR_NACK BIT(15) |
| 80 | #define DAVINCI_I2C_MDR_STT BIT(13) |
| 81 | #define DAVINCI_I2C_MDR_STP BIT(11) |
| 82 | #define DAVINCI_I2C_MDR_MST BIT(10) |
| 83 | #define DAVINCI_I2C_MDR_TRX BIT(9) |
| 84 | #define DAVINCI_I2C_MDR_XA BIT(8) |
| 85 | #define DAVINCI_I2C_MDR_RM BIT(7) |
| 86 | #define DAVINCI_I2C_MDR_IRS BIT(5) |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 87 | |
Chaithrika U S | c062a25 | 2010-01-06 14:54:57 +0530 | [diff] [blame] | 88 | #define DAVINCI_I2C_IMR_AAS BIT(6) |
| 89 | #define DAVINCI_I2C_IMR_SCD BIT(5) |
| 90 | #define DAVINCI_I2C_IMR_XRDY BIT(4) |
| 91 | #define DAVINCI_I2C_IMR_RRDY BIT(3) |
| 92 | #define DAVINCI_I2C_IMR_ARDY BIT(2) |
| 93 | #define DAVINCI_I2C_IMR_NACK BIT(1) |
| 94 | #define DAVINCI_I2C_IMR_AL BIT(0) |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 95 | |
| 96 | struct davinci_i2c_dev { |
| 97 | struct device *dev; |
| 98 | void __iomem *base; |
| 99 | struct completion cmd_complete; |
| 100 | struct clk *clk; |
| 101 | int cmd_err; |
| 102 | u8 *buf; |
| 103 | size_t buf_len; |
| 104 | int irq; |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 105 | int stop; |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 106 | u8 terminate; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 107 | struct i2c_adapter adapter; |
Chaithrika U S | 82c0de1 | 2010-01-06 14:55:00 +0530 | [diff] [blame] | 108 | #ifdef CONFIG_CPU_FREQ |
| 109 | struct completion xfr_complete; |
| 110 | struct notifier_block freq_transition; |
| 111 | #endif |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 112 | struct davinci_i2c_platform_data *pdata; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /* default platform data to use if not supplied in the platform_device */ |
| 116 | static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = { |
| 117 | .bus_freq = 100, |
| 118 | .bus_delay = 0, |
| 119 | }; |
| 120 | |
| 121 | static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev, |
| 122 | int reg, u16 val) |
| 123 | { |
Taras Kondratiuk | a238dcf | 2013-11-20 20:23:44 +0200 | [diff] [blame] | 124 | writew_relaxed(val, i2c_dev->base + reg); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg) |
| 128 | { |
Taras Kondratiuk | a238dcf | 2013-11-20 20:23:44 +0200 | [diff] [blame] | 129 | return readw_relaxed(i2c_dev->base + reg); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 132 | /* Generate a pulse on the i2c clock pin. */ |
Wolfram Sang | adf68ac | 2013-03-29 11:23:41 +0100 | [diff] [blame] | 133 | static void davinci_i2c_clock_pulse(unsigned int scl_pin) |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 134 | { |
| 135 | u16 i; |
| 136 | |
| 137 | if (scl_pin) { |
| 138 | /* Send high and low on the SCL line */ |
| 139 | for (i = 0; i < 9; i++) { |
| 140 | gpio_set_value(scl_pin, 0); |
| 141 | udelay(20); |
| 142 | gpio_set_value(scl_pin, 1); |
| 143 | udelay(20); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* This routine does i2c bus recovery as specified in the |
| 149 | * i2c protocol Rev. 03 section 3.16 titled "Bus clear" |
| 150 | */ |
Wolfram Sang | adf68ac | 2013-03-29 11:23:41 +0100 | [diff] [blame] | 151 | static void davinci_i2c_recover_bus(struct davinci_i2c_dev *dev) |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 152 | { |
| 153 | u32 flag = 0; |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 154 | struct davinci_i2c_platform_data *pdata = dev->pdata; |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 155 | |
| 156 | dev_err(dev->dev, "initiating i2c bus recovery\n"); |
| 157 | /* Send NACK to the slave */ |
| 158 | flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); |
| 159 | flag |= DAVINCI_I2C_MDR_NACK; |
| 160 | /* write the data into mode register */ |
| 161 | davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); |
Wolfram Sang | adf68ac | 2013-03-29 11:23:41 +0100 | [diff] [blame] | 162 | davinci_i2c_clock_pulse(pdata->scl_pin); |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 163 | /* Send STOP */ |
| 164 | flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); |
| 165 | flag |= DAVINCI_I2C_MDR_STP; |
| 166 | davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); |
| 167 | } |
| 168 | |
Chaithrika U S | 5ae5b11 | 2010-01-06 14:54:58 +0530 | [diff] [blame] | 169 | static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev, |
| 170 | int val) |
| 171 | { |
| 172 | u16 w; |
| 173 | |
| 174 | w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG); |
| 175 | if (!val) /* put I2C into reset */ |
| 176 | w &= ~DAVINCI_I2C_MDR_IRS; |
| 177 | else /* take I2C out of reset */ |
| 178 | w |= DAVINCI_I2C_MDR_IRS; |
| 179 | |
| 180 | davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w); |
| 181 | } |
| 182 | |
| 183 | static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev) |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 184 | { |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 185 | struct davinci_i2c_platform_data *pdata = dev->pdata; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 186 | u16 psc; |
| 187 | u32 clk; |
Troy Kisky | cc99ff7 | 2008-07-14 22:38:20 +0200 | [diff] [blame] | 188 | u32 d; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 189 | u32 clkh; |
| 190 | u32 clkl; |
| 191 | u32 input_clock = clk_get_rate(dev->clk); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 192 | |
| 193 | /* NOTE: I2C Clock divider programming info |
| 194 | * As per I2C specs the following formulas provide prescaler |
| 195 | * and low/high divider values |
| 196 | * input clk --> PSC Div -----------> ICCL/H Div --> output clock |
| 197 | * module clk |
| 198 | * |
| 199 | * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ] |
| 200 | * |
| 201 | * Thus, |
| 202 | * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d; |
| 203 | * |
| 204 | * where if PSC == 0, d = 7, |
| 205 | * if PSC == 1, d = 6 |
| 206 | * if PSC > 1 , d = 5 |
| 207 | */ |
| 208 | |
Troy Kisky | cc99ff7 | 2008-07-14 22:38:20 +0200 | [diff] [blame] | 209 | /* get minimum of 7 MHz clock, but max of 12 MHz */ |
| 210 | psc = (input_clock / 7000000) - 1; |
| 211 | if ((input_clock / (psc + 1)) > 12000000) |
| 212 | psc++; /* better to run under spec than over */ |
| 213 | d = (psc >= 2) ? 5 : 7 - psc; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 214 | |
Troy Kisky | cc99ff7 | 2008-07-14 22:38:20 +0200 | [diff] [blame] | 215 | clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1); |
| 216 | clkh = clk >> 1; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 217 | clkl = clk - clkh; |
| 218 | |
| 219 | davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc); |
| 220 | davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh); |
| 221 | davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl); |
| 222 | |
Chaithrika U S | 5ae5b11 | 2010-01-06 14:54:58 +0530 | [diff] [blame] | 223 | dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk); |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * This function configures I2C and brings I2C out of reset. |
| 228 | * This function is called during I2C init function. This function |
| 229 | * also gets called if I2C encounters any errors. |
| 230 | */ |
| 231 | static int i2c_davinci_init(struct davinci_i2c_dev *dev) |
| 232 | { |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 233 | struct davinci_i2c_platform_data *pdata = dev->pdata; |
Chaithrika U S | 5ae5b11 | 2010-01-06 14:54:58 +0530 | [diff] [blame] | 234 | |
| 235 | /* put I2C into reset */ |
| 236 | davinci_i2c_reset_ctrl(dev, 0); |
| 237 | |
| 238 | /* compute clock dividers */ |
| 239 | i2c_davinci_calc_clk_dividers(dev); |
| 240 | |
David Brownell | 7605fa3 | 2009-07-06 15:48:36 -0700 | [diff] [blame] | 241 | /* Respond at reserved "SMBus Host" slave address" (and zero); |
| 242 | * we seem to have no option to not respond... |
| 243 | */ |
| 244 | davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08); |
| 245 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 246 | dev_dbg(dev->dev, "PSC = %d\n", |
| 247 | davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG)); |
| 248 | dev_dbg(dev->dev, "CLKL = %d\n", |
| 249 | davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG)); |
| 250 | dev_dbg(dev->dev, "CLKH = %d\n", |
| 251 | davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG)); |
Troy Kisky | cc99ff7 | 2008-07-14 22:38:20 +0200 | [diff] [blame] | 252 | dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n", |
| 253 | pdata->bus_freq, pdata->bus_delay); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 254 | |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 255 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 256 | /* Take the I2C module out of reset: */ |
Chaithrika U S | 5ae5b11 | 2010-01-06 14:54:58 +0530 | [diff] [blame] | 257 | davinci_i2c_reset_ctrl(dev, 1); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 258 | |
| 259 | /* Enable interrupts */ |
| 260 | davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Waiting for bus not busy |
| 267 | */ |
| 268 | static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev, |
| 269 | char allow_sleep) |
| 270 | { |
| 271 | unsigned long timeout; |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 272 | static u16 to_cnt; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 273 | |
Jean Delvare | 98a679c | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 274 | timeout = jiffies + dev->adapter.timeout; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 275 | while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) |
| 276 | & DAVINCI_I2C_STR_BB) { |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 277 | if (to_cnt <= DAVINCI_I2C_MAX_TRIES) { |
| 278 | if (time_after(jiffies, timeout)) { |
| 279 | dev_warn(dev->dev, |
| 280 | "timeout waiting for bus ready\n"); |
| 281 | to_cnt++; |
| 282 | return -ETIMEDOUT; |
| 283 | } else { |
| 284 | to_cnt = 0; |
Wolfram Sang | adf68ac | 2013-03-29 11:23:41 +0100 | [diff] [blame] | 285 | davinci_i2c_recover_bus(dev); |
Philby John | 8574faf | 2010-01-11 22:39:44 +0530 | [diff] [blame] | 286 | i2c_davinci_init(dev); |
| 287 | } |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 288 | } |
| 289 | if (allow_sleep) |
| 290 | schedule_timeout(1); |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Low level master read/write transaction. This function is called |
| 298 | * from i2c_davinci_xfer. |
| 299 | */ |
| 300 | static int |
| 301 | i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop) |
| 302 | { |
| 303 | struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 304 | struct davinci_i2c_platform_data *pdata = dev->pdata; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 305 | u32 flag; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 306 | u16 w; |
| 307 | int r; |
| 308 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 309 | /* Introduce a delay, required for some boards (e.g Davinci EVM) */ |
| 310 | if (pdata->bus_delay) |
| 311 | udelay(pdata->bus_delay); |
| 312 | |
| 313 | /* set the slave address */ |
| 314 | davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr); |
| 315 | |
| 316 | dev->buf = msg->buf; |
| 317 | dev->buf_len = msg->len; |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 318 | dev->stop = stop; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 319 | |
| 320 | davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len); |
| 321 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 322 | reinit_completion(&dev->cmd_complete); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 323 | dev->cmd_err = 0; |
| 324 | |
Jon Povey | c5b4afe | 2010-10-12 13:47:05 +0900 | [diff] [blame] | 325 | /* Take I2C out of reset and configure it as master */ |
| 326 | flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 327 | |
| 328 | /* if the slave address is ten bit address, enable XA bit */ |
| 329 | if (msg->flags & I2C_M_TEN) |
| 330 | flag |= DAVINCI_I2C_MDR_XA; |
| 331 | if (!(msg->flags & I2C_M_RD)) |
| 332 | flag |= DAVINCI_I2C_MDR_TRX; |
Jon Povey | c5b4afe | 2010-10-12 13:47:05 +0900 | [diff] [blame] | 333 | if (msg->len == 0) |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 334 | flag |= DAVINCI_I2C_MDR_RM; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 335 | |
| 336 | /* Enable receive or transmit interrupts */ |
| 337 | w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG); |
| 338 | if (msg->flags & I2C_M_RD) |
Chaithrika U S | c062a25 | 2010-01-06 14:54:57 +0530 | [diff] [blame] | 339 | w |= DAVINCI_I2C_IMR_RRDY; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 340 | else |
Chaithrika U S | c062a25 | 2010-01-06 14:54:57 +0530 | [diff] [blame] | 341 | w |= DAVINCI_I2C_IMR_XRDY; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 342 | davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w); |
| 343 | |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 344 | dev->terminate = 0; |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 345 | |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 346 | /* |
Jon Povey | c5b4afe | 2010-10-12 13:47:05 +0900 | [diff] [blame] | 347 | * Write mode register first as needed for correct behaviour |
| 348 | * on OMAP-L138, but don't set STT yet to avoid a race with XRDY |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 349 | * occurring before we have loaded DXR |
Jon Povey | c5b4afe | 2010-10-12 13:47:05 +0900 | [diff] [blame] | 350 | */ |
| 351 | davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); |
| 352 | |
| 353 | /* |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 354 | * First byte should be set here, not after interrupt, |
| 355 | * because transmit-data-ready interrupt can come before |
| 356 | * NACK-interrupt during sending of previous message and |
| 357 | * ICDXR may have wrong data |
Jon Povey | c5b4afe | 2010-10-12 13:47:05 +0900 | [diff] [blame] | 358 | * It also saves us one interrupt, slightly faster |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 359 | */ |
| 360 | if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) { |
| 361 | davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++); |
| 362 | dev->buf_len--; |
| 363 | } |
| 364 | |
Jon Povey | c5b4afe | 2010-10-12 13:47:05 +0900 | [diff] [blame] | 365 | /* Set STT to begin transmit now DXR is loaded */ |
| 366 | flag |= DAVINCI_I2C_MDR_STT; |
| 367 | if (stop && msg->len != 0) |
| 368 | flag |= DAVINCI_I2C_MDR_STP; |
Jon Povey | 4bba0fd | 2010-09-17 12:02:11 +0900 | [diff] [blame] | 369 | davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag); |
| 370 | |
Mike Looijmans | 900ef80 | 2014-03-14 07:42:28 +0100 | [diff] [blame] | 371 | r = wait_for_completion_timeout(&dev->cmd_complete, dev->adapter.timeout); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 372 | if (r == 0) { |
| 373 | dev_err(dev->dev, "controller timed out\n"); |
Wolfram Sang | adf68ac | 2013-03-29 11:23:41 +0100 | [diff] [blame] | 374 | davinci_i2c_recover_bus(dev); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 375 | i2c_davinci_init(dev); |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 376 | dev->buf_len = 0; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 377 | return -ETIMEDOUT; |
| 378 | } |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 379 | if (dev->buf_len) { |
| 380 | /* This should be 0 if all bytes were transferred |
| 381 | * or dev->cmd_err denotes an error. |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 382 | */ |
| 383 | if (r >= 0) { |
| 384 | dev_err(dev->dev, "abnormal termination buf_len=%i\n", |
| 385 | dev->buf_len); |
| 386 | r = -EREMOTEIO; |
| 387 | } |
| 388 | dev->terminate = 1; |
| 389 | wmb(); |
| 390 | dev->buf_len = 0; |
| 391 | } |
| 392 | if (r < 0) |
| 393 | return r; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 394 | |
| 395 | /* no error */ |
| 396 | if (likely(!dev->cmd_err)) |
| 397 | return msg->len; |
| 398 | |
| 399 | /* We have an error */ |
| 400 | if (dev->cmd_err & DAVINCI_I2C_STR_AL) { |
| 401 | i2c_davinci_init(dev); |
| 402 | return -EIO; |
| 403 | } |
| 404 | |
| 405 | if (dev->cmd_err & DAVINCI_I2C_STR_NACK) { |
| 406 | if (msg->flags & I2C_M_IGNORE_NAK) |
| 407 | return msg->len; |
Grygorii Strashko | 9ea359f | 2014-12-01 17:34:04 +0200 | [diff] [blame] | 408 | w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); |
| 409 | w |= DAVINCI_I2C_MDR_STP; |
| 410 | davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 411 | return -EREMOTEIO; |
| 412 | } |
| 413 | return -EIO; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Prepare controller for a transaction and call i2c_davinci_xfer_msg |
| 418 | */ |
| 419 | static int |
| 420 | i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 421 | { |
| 422 | struct davinci_i2c_dev *dev = i2c_get_adapdata(adap); |
| 423 | int i; |
| 424 | int ret; |
| 425 | |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 426 | dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 427 | |
| 428 | ret = i2c_davinci_wait_bus_not_busy(dev, 1); |
| 429 | if (ret < 0) { |
| 430 | dev_warn(dev->dev, "timeout waiting for bus ready\n"); |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | for (i = 0; i < num; i++) { |
| 435 | ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1))); |
Troy Kisky | d868caa | 2008-07-14 22:38:20 +0200 | [diff] [blame] | 436 | dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num, |
| 437 | ret); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 438 | if (ret < 0) |
| 439 | return ret; |
| 440 | } |
Chaithrika U S | 82c0de1 | 2010-01-06 14:55:00 +0530 | [diff] [blame] | 441 | |
| 442 | #ifdef CONFIG_CPU_FREQ |
| 443 | complete(&dev->xfr_complete); |
| 444 | #endif |
| 445 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 446 | return num; |
| 447 | } |
| 448 | |
| 449 | static u32 i2c_davinci_func(struct i2c_adapter *adap) |
| 450 | { |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 451 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 454 | static void terminate_read(struct davinci_i2c_dev *dev) |
| 455 | { |
| 456 | u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); |
| 457 | w |= DAVINCI_I2C_MDR_NACK; |
| 458 | davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); |
| 459 | |
| 460 | /* Throw away data */ |
| 461 | davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG); |
| 462 | if (!dev->terminate) |
| 463 | dev_err(dev->dev, "RDR IRQ while no data requested\n"); |
| 464 | } |
| 465 | static void terminate_write(struct davinci_i2c_dev *dev) |
| 466 | { |
| 467 | u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG); |
| 468 | w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP; |
| 469 | davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w); |
| 470 | |
| 471 | if (!dev->terminate) |
David Brownell | 7605fa3 | 2009-07-06 15:48:36 -0700 | [diff] [blame] | 472 | dev_dbg(dev->dev, "TDR IRQ while no data to send\n"); |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 475 | /* |
| 476 | * Interrupt service routine. This gets called whenever an I2C interrupt |
| 477 | * occurs. |
| 478 | */ |
| 479 | static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id) |
| 480 | { |
| 481 | struct davinci_i2c_dev *dev = dev_id; |
| 482 | u32 stat; |
| 483 | int count = 0; |
| 484 | u16 w; |
| 485 | |
| 486 | while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) { |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 487 | dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 488 | if (count++ == 100) { |
| 489 | dev_warn(dev->dev, "Too much work in one IRQ\n"); |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | switch (stat) { |
| 494 | case DAVINCI_I2C_IVR_AL: |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 495 | /* Arbitration lost, must retry */ |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 496 | dev->cmd_err |= DAVINCI_I2C_STR_AL; |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 497 | dev->buf_len = 0; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 498 | complete(&dev->cmd_complete); |
| 499 | break; |
| 500 | |
| 501 | case DAVINCI_I2C_IVR_NACK: |
| 502 | dev->cmd_err |= DAVINCI_I2C_STR_NACK; |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 503 | dev->buf_len = 0; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 504 | complete(&dev->cmd_complete); |
| 505 | break; |
| 506 | |
| 507 | case DAVINCI_I2C_IVR_ARDY: |
Troy Kisky | b73a9ae | 2008-04-11 12:07:05 +0200 | [diff] [blame] | 508 | davinci_i2c_write_reg(dev, |
| 509 | DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY); |
Dirk Behme | c6c7c72 | 2008-03-28 06:16:12 +0100 | [diff] [blame] | 510 | if (((dev->buf_len == 0) && (dev->stop != 0)) || |
| 511 | (dev->cmd_err & DAVINCI_I2C_STR_NACK)) { |
| 512 | w = davinci_i2c_read_reg(dev, |
| 513 | DAVINCI_I2C_MDR_REG); |
| 514 | w |= DAVINCI_I2C_MDR_STP; |
| 515 | davinci_i2c_write_reg(dev, |
| 516 | DAVINCI_I2C_MDR_REG, w); |
| 517 | } |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 518 | complete(&dev->cmd_complete); |
| 519 | break; |
| 520 | |
| 521 | case DAVINCI_I2C_IVR_RDR: |
| 522 | if (dev->buf_len) { |
| 523 | *dev->buf++ = |
| 524 | davinci_i2c_read_reg(dev, |
| 525 | DAVINCI_I2C_DRR_REG); |
| 526 | dev->buf_len--; |
| 527 | if (dev->buf_len) |
| 528 | continue; |
| 529 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 530 | davinci_i2c_write_reg(dev, |
Troy Kisky | b73a9ae | 2008-04-11 12:07:05 +0200 | [diff] [blame] | 531 | DAVINCI_I2C_STR_REG, |
| 532 | DAVINCI_I2C_IMR_RRDY); |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 533 | } else { |
| 534 | /* signal can terminate transfer */ |
| 535 | terminate_read(dev); |
| 536 | } |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 537 | break; |
| 538 | |
| 539 | case DAVINCI_I2C_IVR_XRDY: |
| 540 | if (dev->buf_len) { |
| 541 | davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, |
| 542 | *dev->buf++); |
| 543 | dev->buf_len--; |
| 544 | if (dev->buf_len) |
| 545 | continue; |
| 546 | |
| 547 | w = davinci_i2c_read_reg(dev, |
| 548 | DAVINCI_I2C_IMR_REG); |
Chaithrika U S | c062a25 | 2010-01-06 14:54:57 +0530 | [diff] [blame] | 549 | w &= ~DAVINCI_I2C_IMR_XRDY; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 550 | davinci_i2c_write_reg(dev, |
| 551 | DAVINCI_I2C_IMR_REG, |
| 552 | w); |
Troy Kisky | 5a0d5f5 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 553 | } else { |
| 554 | /* signal can terminate transfer */ |
| 555 | terminate_write(dev); |
| 556 | } |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 557 | break; |
| 558 | |
| 559 | case DAVINCI_I2C_IVR_SCD: |
Troy Kisky | b73a9ae | 2008-04-11 12:07:05 +0200 | [diff] [blame] | 560 | davinci_i2c_write_reg(dev, |
| 561 | DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 562 | complete(&dev->cmd_complete); |
| 563 | break; |
| 564 | |
| 565 | case DAVINCI_I2C_IVR_AAS: |
David Brownell | 7605fa3 | 2009-07-06 15:48:36 -0700 | [diff] [blame] | 566 | dev_dbg(dev->dev, "Address as slave interrupt\n"); |
| 567 | break; |
| 568 | |
| 569 | default: |
| 570 | dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat); |
| 571 | break; |
| 572 | } |
| 573 | } |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 574 | |
| 575 | return count ? IRQ_HANDLED : IRQ_NONE; |
| 576 | } |
| 577 | |
Chaithrika U S | 82c0de1 | 2010-01-06 14:55:00 +0530 | [diff] [blame] | 578 | #ifdef CONFIG_CPU_FREQ |
| 579 | static int i2c_davinci_cpufreq_transition(struct notifier_block *nb, |
| 580 | unsigned long val, void *data) |
| 581 | { |
| 582 | struct davinci_i2c_dev *dev; |
| 583 | |
| 584 | dev = container_of(nb, struct davinci_i2c_dev, freq_transition); |
| 585 | if (val == CPUFREQ_PRECHANGE) { |
| 586 | wait_for_completion(&dev->xfr_complete); |
| 587 | davinci_i2c_reset_ctrl(dev, 0); |
| 588 | } else if (val == CPUFREQ_POSTCHANGE) { |
| 589 | i2c_davinci_calc_clk_dividers(dev); |
| 590 | davinci_i2c_reset_ctrl(dev, 1); |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev) |
| 597 | { |
| 598 | dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition; |
| 599 | |
| 600 | return cpufreq_register_notifier(&dev->freq_transition, |
| 601 | CPUFREQ_TRANSITION_NOTIFIER); |
| 602 | } |
| 603 | |
| 604 | static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev) |
| 605 | { |
| 606 | cpufreq_unregister_notifier(&dev->freq_transition, |
| 607 | CPUFREQ_TRANSITION_NOTIFIER); |
| 608 | } |
| 609 | #else |
| 610 | static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev) |
| 611 | { |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev) |
| 616 | { |
| 617 | } |
| 618 | #endif |
| 619 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 620 | static struct i2c_algorithm i2c_davinci_algo = { |
| 621 | .master_xfer = i2c_davinci_xfer, |
| 622 | .functionality = i2c_davinci_func, |
| 623 | }; |
| 624 | |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 625 | static const struct of_device_id davinci_i2c_of_match[] = { |
| 626 | {.compatible = "ti,davinci-i2c", }, |
| 627 | {}, |
| 628 | }; |
| 629 | MODULE_DEVICE_TABLE(of, davinci_i2c_of_match); |
| 630 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 631 | static int davinci_i2c_probe(struct platform_device *pdev) |
| 632 | { |
| 633 | struct davinci_i2c_dev *dev; |
| 634 | struct i2c_adapter *adap; |
Grygorii Strashko | 2c6ef04 | 2014-12-01 17:34:03 +0200 | [diff] [blame] | 635 | struct resource *mem; |
| 636 | int r, irq; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 637 | |
Grygorii Strashko | 2c6ef04 | 2014-12-01 17:34:03 +0200 | [diff] [blame] | 638 | irq = platform_get_irq(pdev, 0); |
| 639 | if (irq <= 0) { |
| 640 | if (!irq) |
| 641 | irq = -ENXIO; |
| 642 | if (irq != -EPROBE_DEFER) |
| 643 | dev_err(&pdev->dev, |
| 644 | "can't get irq resource ret=%d\n", irq); |
| 645 | return irq; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 646 | } |
| 647 | |
Vishwanathrao Badarkhe, Manish | 8579684 | 2013-03-05 01:44:45 +0000 | [diff] [blame] | 648 | dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev), |
| 649 | GFP_KERNEL); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 650 | if (!dev) { |
Vishwanathrao Badarkhe, Manish | 8579684 | 2013-03-05 01:44:45 +0000 | [diff] [blame] | 651 | dev_err(&pdev->dev, "Memory allocation failed\n"); |
| 652 | return -ENOMEM; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 653 | } |
| 654 | |
Troy Kisky | 2e74378 | 2008-07-14 22:38:21 +0200 | [diff] [blame] | 655 | init_completion(&dev->cmd_complete); |
Chaithrika U S | 82c0de1 | 2010-01-06 14:55:00 +0530 | [diff] [blame] | 656 | #ifdef CONFIG_CPU_FREQ |
| 657 | init_completion(&dev->xfr_complete); |
| 658 | #endif |
Wolfram Sang | c4df500 | 2013-04-18 15:43:07 +0200 | [diff] [blame] | 659 | dev->dev = &pdev->dev; |
Grygorii Strashko | 2c6ef04 | 2014-12-01 17:34:03 +0200 | [diff] [blame] | 660 | dev->irq = irq; |
Olof Johansson | 1e2c239 | 2013-09-05 12:23:44 -0700 | [diff] [blame] | 661 | dev->pdata = dev_get_platdata(&pdev->dev); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 662 | platform_set_drvdata(pdev, dev); |
| 663 | |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 664 | if (!dev->pdata && pdev->dev.of_node) { |
| 665 | u32 prop; |
| 666 | |
| 667 | dev->pdata = devm_kzalloc(&pdev->dev, |
| 668 | sizeof(struct davinci_i2c_platform_data), GFP_KERNEL); |
Wolfram Sang | c4df500 | 2013-04-18 15:43:07 +0200 | [diff] [blame] | 669 | if (!dev->pdata) |
| 670 | return -ENOMEM; |
| 671 | |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 672 | memcpy(dev->pdata, &davinci_i2c_platform_data_default, |
| 673 | sizeof(struct davinci_i2c_platform_data)); |
| 674 | if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency", |
| 675 | &prop)) |
| 676 | dev->pdata->bus_freq = prop / 1000; |
| 677 | } else if (!dev->pdata) { |
| 678 | dev->pdata = &davinci_i2c_platform_data_default; |
| 679 | } |
| 680 | |
Vishwanathrao Badarkhe, Manish | 8579684 | 2013-03-05 01:44:45 +0000 | [diff] [blame] | 681 | dev->clk = devm_clk_get(&pdev->dev, NULL); |
Wolfram Sang | c4df500 | 2013-04-18 15:43:07 +0200 | [diff] [blame] | 682 | if (IS_ERR(dev->clk)) |
| 683 | return -ENODEV; |
Murali Karicheri | 2bdbfa9 | 2012-08-30 18:10:36 +0000 | [diff] [blame] | 684 | clk_prepare_enable(dev->clk); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 685 | |
Wolfram Sang | 3cc2d00 | 2013-05-10 10:16:54 +0200 | [diff] [blame] | 686 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Vishwanathrao Badarkhe, Manish | 8579684 | 2013-03-05 01:44:45 +0000 | [diff] [blame] | 687 | dev->base = devm_ioremap_resource(&pdev->dev, mem); |
| 688 | if (IS_ERR(dev->base)) { |
| 689 | r = PTR_ERR(dev->base); |
| 690 | goto err_unuse_clocks; |
Chaithrika U S | c062a25 | 2010-01-06 14:54:57 +0530 | [diff] [blame] | 691 | } |
| 692 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 693 | i2c_davinci_init(dev); |
| 694 | |
Vishwanathrao Badarkhe, Manish | 8579684 | 2013-03-05 01:44:45 +0000 | [diff] [blame] | 695 | r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0, |
| 696 | pdev->name, dev); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 697 | if (r) { |
| 698 | dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); |
| 699 | goto err_unuse_clocks; |
| 700 | } |
| 701 | |
Chaithrika U S | 82c0de1 | 2010-01-06 14:55:00 +0530 | [diff] [blame] | 702 | r = i2c_davinci_cpufreq_register(dev); |
| 703 | if (r) { |
| 704 | dev_err(&pdev->dev, "failed to register cpufreq\n"); |
Vishwanathrao Badarkhe, Manish | 8579684 | 2013-03-05 01:44:45 +0000 | [diff] [blame] | 705 | goto err_unuse_clocks; |
Chaithrika U S | 82c0de1 | 2010-01-06 14:55:00 +0530 | [diff] [blame] | 706 | } |
| 707 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 708 | adap = &dev->adapter; |
| 709 | i2c_set_adapdata(adap, dev); |
| 710 | adap->owner = THIS_MODULE; |
Wolfram Sang | adcb82a | 2014-07-10 13:46:25 +0200 | [diff] [blame] | 711 | adap->class = I2C_CLASS_DEPRECATED; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 712 | strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name)); |
| 713 | adap->algo = &i2c_davinci_algo; |
| 714 | adap->dev.parent = &pdev->dev; |
Jean Delvare | 98a679c | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 715 | adap->timeout = DAVINCI_I2C_TIMEOUT; |
Heiko Schocher | 5c3d8a4 | 2012-07-30 07:21:12 +0000 | [diff] [blame] | 716 | adap->dev.of_node = pdev->dev.of_node; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 717 | |
| 718 | adap->nr = pdev->id; |
| 719 | r = i2c_add_numbered_adapter(adap); |
| 720 | if (r) { |
| 721 | dev_err(&pdev->dev, "failure adding adapter\n"); |
Vishwanathrao Badarkhe, Manish | 8579684 | 2013-03-05 01:44:45 +0000 | [diff] [blame] | 722 | goto err_unuse_clocks; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | return 0; |
| 726 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 727 | err_unuse_clocks: |
Murali Karicheri | 2bdbfa9 | 2012-08-30 18:10:36 +0000 | [diff] [blame] | 728 | clk_disable_unprepare(dev->clk); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 729 | dev->clk = NULL; |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 730 | return r; |
| 731 | } |
| 732 | |
| 733 | static int davinci_i2c_remove(struct platform_device *pdev) |
| 734 | { |
| 735 | struct davinci_i2c_dev *dev = platform_get_drvdata(pdev); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 736 | |
Chaithrika U S | 82c0de1 | 2010-01-06 14:55:00 +0530 | [diff] [blame] | 737 | i2c_davinci_cpufreq_deregister(dev); |
| 738 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 739 | i2c_del_adapter(&dev->adapter); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 740 | |
Murali Karicheri | 2bdbfa9 | 2012-08-30 18:10:36 +0000 | [diff] [blame] | 741 | clk_disable_unprepare(dev->clk); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 742 | dev->clk = NULL; |
| 743 | |
| 744 | davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0); |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 745 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 746 | return 0; |
| 747 | } |
| 748 | |
Chaithrika U S | 68f15de | 2010-01-06 14:54:59 +0530 | [diff] [blame] | 749 | #ifdef CONFIG_PM |
| 750 | static int davinci_i2c_suspend(struct device *dev) |
| 751 | { |
| 752 | struct platform_device *pdev = to_platform_device(dev); |
| 753 | struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev); |
| 754 | |
| 755 | /* put I2C into reset */ |
| 756 | davinci_i2c_reset_ctrl(i2c_dev, 0); |
Murali Karicheri | 2bdbfa9 | 2012-08-30 18:10:36 +0000 | [diff] [blame] | 757 | clk_disable_unprepare(i2c_dev->clk); |
Chaithrika U S | 68f15de | 2010-01-06 14:54:59 +0530 | [diff] [blame] | 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static int davinci_i2c_resume(struct device *dev) |
| 763 | { |
| 764 | struct platform_device *pdev = to_platform_device(dev); |
| 765 | struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev); |
| 766 | |
Murali Karicheri | 2bdbfa9 | 2012-08-30 18:10:36 +0000 | [diff] [blame] | 767 | clk_prepare_enable(i2c_dev->clk); |
Chaithrika U S | 68f15de | 2010-01-06 14:54:59 +0530 | [diff] [blame] | 768 | /* take I2C out of reset */ |
| 769 | davinci_i2c_reset_ctrl(i2c_dev, 1); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static const struct dev_pm_ops davinci_i2c_pm = { |
| 775 | .suspend = davinci_i2c_suspend, |
| 776 | .resume = davinci_i2c_resume, |
| 777 | }; |
| 778 | |
| 779 | #define davinci_i2c_pm_ops (&davinci_i2c_pm) |
| 780 | #else |
| 781 | #define davinci_i2c_pm_ops NULL |
| 782 | #endif |
| 783 | |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 784 | /* work with hotplug and coldplug */ |
| 785 | MODULE_ALIAS("platform:i2c_davinci"); |
| 786 | |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 787 | static struct platform_driver davinci_i2c_driver = { |
| 788 | .probe = davinci_i2c_probe, |
| 789 | .remove = davinci_i2c_remove, |
| 790 | .driver = { |
| 791 | .name = "i2c_davinci", |
Chaithrika U S | 68f15de | 2010-01-06 14:54:59 +0530 | [diff] [blame] | 792 | .pm = davinci_i2c_pm_ops, |
Sachin Kamat | 4e90532 | 2013-09-30 09:04:25 +0530 | [diff] [blame] | 793 | .of_match_table = davinci_i2c_of_match, |
Vladimir Barinov | 95a7f10 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 794 | }, |
| 795 | }; |
| 796 | |
| 797 | /* I2C may be needed to bring up other drivers */ |
| 798 | static int __init davinci_i2c_init_driver(void) |
| 799 | { |
| 800 | return platform_driver_register(&davinci_i2c_driver); |
| 801 | } |
| 802 | subsys_initcall(davinci_i2c_init_driver); |
| 803 | |
| 804 | static void __exit davinci_i2c_exit_driver(void) |
| 805 | { |
| 806 | platform_driver_unregister(&davinci_i2c_driver); |
| 807 | } |
| 808 | module_exit(davinci_i2c_exit_driver); |
| 809 | |
| 810 | MODULE_AUTHOR("Texas Instruments India"); |
| 811 | MODULE_DESCRIPTION("TI DaVinci I2C bus adapter"); |
| 812 | MODULE_LICENSE("GPL"); |