srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 1 | /* |
Linus Walleij | 1804edd | 2010-09-23 09:03:40 +0200 | [diff] [blame] | 2 | * Copyright (C) 2009 ST-Ericsson SA |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 3 | * Copyright (C) 2009 STMicroelectronics |
| 4 | * |
| 5 | * I2C master mode controller driver, used in Nomadik 8815 |
| 6 | * and Ux500 platforms. |
| 7 | * |
| 8 | * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com> |
| 9 | * Author: Sachin Verma <sachin.verma@st.com> |
| 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 version 2, as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/io.h> |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 24 | #include <linux/regulator/consumer.h> |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 26 | |
| 27 | #include <plat/i2c.h> |
| 28 | |
| 29 | #define DRIVER_NAME "nmk-i2c" |
| 30 | |
| 31 | /* I2C Controller register offsets */ |
| 32 | #define I2C_CR (0x000) |
| 33 | #define I2C_SCR (0x004) |
| 34 | #define I2C_HSMCR (0x008) |
| 35 | #define I2C_MCR (0x00C) |
| 36 | #define I2C_TFR (0x010) |
| 37 | #define I2C_SR (0x014) |
| 38 | #define I2C_RFR (0x018) |
| 39 | #define I2C_TFTR (0x01C) |
| 40 | #define I2C_RFTR (0x020) |
| 41 | #define I2C_DMAR (0x024) |
| 42 | #define I2C_BRCR (0x028) |
| 43 | #define I2C_IMSCR (0x02C) |
| 44 | #define I2C_RISR (0x030) |
| 45 | #define I2C_MISR (0x034) |
| 46 | #define I2C_ICR (0x038) |
| 47 | |
| 48 | /* Control registers */ |
| 49 | #define I2C_CR_PE (0x1 << 0) /* Peripheral Enable */ |
| 50 | #define I2C_CR_OM (0x3 << 1) /* Operating mode */ |
| 51 | #define I2C_CR_SAM (0x1 << 3) /* Slave addressing mode */ |
| 52 | #define I2C_CR_SM (0x3 << 4) /* Speed mode */ |
| 53 | #define I2C_CR_SGCM (0x1 << 6) /* Slave general call mode */ |
| 54 | #define I2C_CR_FTX (0x1 << 7) /* Flush Transmit */ |
| 55 | #define I2C_CR_FRX (0x1 << 8) /* Flush Receive */ |
| 56 | #define I2C_CR_DMA_TX_EN (0x1 << 9) /* DMA Tx enable */ |
| 57 | #define I2C_CR_DMA_RX_EN (0x1 << 10) /* DMA Rx Enable */ |
| 58 | #define I2C_CR_DMA_SLE (0x1 << 11) /* DMA sync. logic enable */ |
| 59 | #define I2C_CR_LM (0x1 << 12) /* Loopback mode */ |
| 60 | #define I2C_CR_FON (0x3 << 13) /* Filtering on */ |
| 61 | #define I2C_CR_FS (0x3 << 15) /* Force stop enable */ |
| 62 | |
| 63 | /* Master controller (MCR) register */ |
| 64 | #define I2C_MCR_OP (0x1 << 0) /* Operation */ |
| 65 | #define I2C_MCR_A7 (0x7f << 1) /* 7-bit address */ |
| 66 | #define I2C_MCR_EA10 (0x7 << 8) /* 10-bit Extended address */ |
| 67 | #define I2C_MCR_SB (0x1 << 11) /* Extended address */ |
| 68 | #define I2C_MCR_AM (0x3 << 12) /* Address type */ |
| 69 | #define I2C_MCR_STOP (0x1 << 14) /* Stop condition */ |
| 70 | #define I2C_MCR_LENGTH (0x7ff << 15) /* Transaction length */ |
| 71 | |
| 72 | /* Status register (SR) */ |
| 73 | #define I2C_SR_OP (0x3 << 0) /* Operation */ |
| 74 | #define I2C_SR_STATUS (0x3 << 2) /* controller status */ |
| 75 | #define I2C_SR_CAUSE (0x7 << 4) /* Abort cause */ |
| 76 | #define I2C_SR_TYPE (0x3 << 7) /* Receive type */ |
| 77 | #define I2C_SR_LENGTH (0x7ff << 9) /* Transfer length */ |
| 78 | |
| 79 | /* Interrupt mask set/clear (IMSCR) bits */ |
| 80 | #define I2C_IT_TXFE (0x1 << 0) |
| 81 | #define I2C_IT_TXFNE (0x1 << 1) |
| 82 | #define I2C_IT_TXFF (0x1 << 2) |
| 83 | #define I2C_IT_TXFOVR (0x1 << 3) |
| 84 | #define I2C_IT_RXFE (0x1 << 4) |
| 85 | #define I2C_IT_RXFNF (0x1 << 5) |
| 86 | #define I2C_IT_RXFF (0x1 << 6) |
| 87 | #define I2C_IT_RFSR (0x1 << 16) |
| 88 | #define I2C_IT_RFSE (0x1 << 17) |
| 89 | #define I2C_IT_WTSR (0x1 << 18) |
| 90 | #define I2C_IT_MTD (0x1 << 19) |
| 91 | #define I2C_IT_STD (0x1 << 20) |
| 92 | #define I2C_IT_MAL (0x1 << 24) |
| 93 | #define I2C_IT_BERR (0x1 << 25) |
| 94 | #define I2C_IT_MTDWS (0x1 << 28) |
| 95 | |
| 96 | #define GEN_MASK(val, mask, sb) (((val) << (sb)) & (mask)) |
| 97 | |
| 98 | /* some bits in ICR are reserved */ |
| 99 | #define I2C_CLEAR_ALL_INTS 0x131f007f |
| 100 | |
| 101 | /* first three msb bits are reserved */ |
| 102 | #define IRQ_MASK(mask) (mask & 0x1fffffff) |
| 103 | |
| 104 | /* maximum threshold value */ |
| 105 | #define MAX_I2C_FIFO_THRESHOLD 15 |
| 106 | |
| 107 | enum i2c_status { |
| 108 | I2C_NOP, |
| 109 | I2C_ON_GOING, |
| 110 | I2C_OK, |
| 111 | I2C_ABORT |
| 112 | }; |
| 113 | |
| 114 | /* operation */ |
| 115 | enum i2c_operation { |
| 116 | I2C_NO_OPERATION = 0xff, |
| 117 | I2C_WRITE = 0x00, |
| 118 | I2C_READ = 0x01 |
| 119 | }; |
| 120 | |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 121 | /** |
| 122 | * struct i2c_nmk_client - client specific data |
| 123 | * @slave_adr: 7-bit slave address |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 124 | * @count: no. bytes to be transferred |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 125 | * @buffer: client data buffer |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 126 | * @xfer_bytes: bytes transferred till now |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 127 | * @operation: current I2C operation |
| 128 | */ |
| 129 | struct i2c_nmk_client { |
| 130 | unsigned short slave_adr; |
| 131 | unsigned long count; |
| 132 | unsigned char *buffer; |
| 133 | unsigned long xfer_bytes; |
| 134 | enum i2c_operation operation; |
| 135 | }; |
| 136 | |
| 137 | /** |
| 138 | * struct nmk_i2c_dev - private data structure of the controller |
| 139 | * @pdev: parent platform device |
| 140 | * @adap: corresponding I2C adapter |
| 141 | * @irq: interrupt line for the controller |
| 142 | * @virtbase: virtual io memory area |
| 143 | * @clk: hardware i2c block clock |
| 144 | * @cfg: machine provided controller configuration |
| 145 | * @cli: holder of client specific data |
| 146 | * @stop: stop condition |
| 147 | * @xfer_complete: acknowledge completion for a I2C message |
| 148 | * @result: controller propogated result |
Linus Walleij | c8d4763 | 2011-08-09 20:17:29 +0200 | [diff] [blame] | 149 | * @regulator: pointer to i2c regulator |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 150 | * @busy: Busy doing transfer |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 151 | */ |
| 152 | struct nmk_i2c_dev { |
| 153 | struct platform_device *pdev; |
| 154 | struct i2c_adapter adap; |
| 155 | int irq; |
| 156 | void __iomem *virtbase; |
| 157 | struct clk *clk; |
| 158 | struct nmk_i2c_controller cfg; |
| 159 | struct i2c_nmk_client cli; |
| 160 | int stop; |
| 161 | struct completion xfer_complete; |
| 162 | int result; |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 163 | struct regulator *regulator; |
| 164 | bool busy; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | /* controller's abort causes */ |
| 168 | static const char *abort_causes[] = { |
| 169 | "no ack received after address transmission", |
| 170 | "no ack received during data phase", |
| 171 | "ack received after xmission of master code", |
| 172 | "master lost arbitration", |
| 173 | "slave restarts", |
| 174 | "slave reset", |
| 175 | "overflow, maxsize is 2047 bytes", |
| 176 | }; |
| 177 | |
| 178 | static inline void i2c_set_bit(void __iomem *reg, u32 mask) |
| 179 | { |
| 180 | writel(readl(reg) | mask, reg); |
| 181 | } |
| 182 | |
| 183 | static inline void i2c_clr_bit(void __iomem *reg, u32 mask) |
| 184 | { |
| 185 | writel(readl(reg) & ~mask, reg); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * flush_i2c_fifo() - This function flushes the I2C FIFO |
| 190 | * @dev: private data of I2C Driver |
| 191 | * |
| 192 | * This function flushes the I2C Tx and Rx FIFOs. It returns |
| 193 | * 0 on successful flushing of FIFO |
| 194 | */ |
| 195 | static int flush_i2c_fifo(struct nmk_i2c_dev *dev) |
| 196 | { |
| 197 | #define LOOP_ATTEMPTS 10 |
| 198 | int i; |
| 199 | unsigned long timeout; |
| 200 | |
| 201 | /* |
| 202 | * flush the transmit and receive FIFO. The flushing |
| 203 | * operation takes several cycles before to be completed. |
| 204 | * On the completion, the I2C internal logic clears these |
| 205 | * bits, until then no one must access Tx, Rx FIFO and |
| 206 | * should poll on these bits waiting for the completion. |
| 207 | */ |
| 208 | writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR); |
| 209 | |
| 210 | for (i = 0; i < LOOP_ATTEMPTS; i++) { |
Virupax Sadashivpetimath | cd20e4fa | 2011-05-13 12:29:46 +0200 | [diff] [blame] | 211 | timeout = jiffies + dev->adap.timeout; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 212 | |
| 213 | while (!time_after(jiffies, timeout)) { |
| 214 | if ((readl(dev->virtbase + I2C_CR) & |
| 215 | (I2C_CR_FTX | I2C_CR_FRX)) == 0) |
| 216 | return 0; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | dev_err(&dev->pdev->dev, "flushing operation timed out " |
| 221 | "giving up after %d attempts", LOOP_ATTEMPTS); |
| 222 | |
| 223 | return -ETIMEDOUT; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * disable_all_interrupts() - Disable all interrupts of this I2c Bus |
| 228 | * @dev: private data of I2C Driver |
| 229 | */ |
| 230 | static void disable_all_interrupts(struct nmk_i2c_dev *dev) |
| 231 | { |
| 232 | u32 mask = IRQ_MASK(0); |
| 233 | writel(mask, dev->virtbase + I2C_IMSCR); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * clear_all_interrupts() - Clear all interrupts of I2C Controller |
| 238 | * @dev: private data of I2C Driver |
| 239 | */ |
| 240 | static void clear_all_interrupts(struct nmk_i2c_dev *dev) |
| 241 | { |
| 242 | u32 mask; |
| 243 | mask = IRQ_MASK(I2C_CLEAR_ALL_INTS); |
| 244 | writel(mask, dev->virtbase + I2C_ICR); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * init_hw() - initialize the I2C hardware |
| 249 | * @dev: private data of I2C Driver |
| 250 | */ |
| 251 | static int init_hw(struct nmk_i2c_dev *dev) |
| 252 | { |
| 253 | int stat; |
| 254 | |
| 255 | stat = flush_i2c_fifo(dev); |
| 256 | if (stat) |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 257 | goto exit; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 258 | |
| 259 | /* disable the controller */ |
| 260 | i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE); |
| 261 | |
| 262 | disable_all_interrupts(dev); |
| 263 | |
| 264 | clear_all_interrupts(dev); |
| 265 | |
| 266 | dev->cli.operation = I2C_NO_OPERATION; |
| 267 | |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 268 | exit: |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 269 | return stat; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* enable peripheral, master mode operation */ |
| 273 | #define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE) |
| 274 | |
| 275 | /** |
| 276 | * load_i2c_mcr_reg() - load the MCR register |
| 277 | * @dev: private data of controller |
| 278 | */ |
| 279 | static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev) |
| 280 | { |
| 281 | u32 mcr = 0; |
| 282 | |
| 283 | /* 7-bit address transaction */ |
| 284 | mcr |= GEN_MASK(1, I2C_MCR_AM, 12); |
| 285 | mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1); |
| 286 | |
| 287 | /* start byte procedure not applied */ |
| 288 | mcr |= GEN_MASK(0, I2C_MCR_SB, 11); |
| 289 | |
| 290 | /* check the operation, master read/write? */ |
| 291 | if (dev->cli.operation == I2C_WRITE) |
| 292 | mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0); |
| 293 | else |
| 294 | mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0); |
| 295 | |
| 296 | /* stop or repeated start? */ |
| 297 | if (dev->stop) |
| 298 | mcr |= GEN_MASK(1, I2C_MCR_STOP, 14); |
| 299 | else |
| 300 | mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14)); |
| 301 | |
| 302 | mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15); |
| 303 | |
| 304 | return mcr; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * setup_i2c_controller() - setup the controller |
| 309 | * @dev: private data of controller |
| 310 | */ |
| 311 | static void setup_i2c_controller(struct nmk_i2c_dev *dev) |
| 312 | { |
| 313 | u32 brcr1, brcr2; |
| 314 | u32 i2c_clk, div; |
| 315 | |
| 316 | writel(0x0, dev->virtbase + I2C_CR); |
| 317 | writel(0x0, dev->virtbase + I2C_HSMCR); |
| 318 | writel(0x0, dev->virtbase + I2C_TFTR); |
| 319 | writel(0x0, dev->virtbase + I2C_RFTR); |
| 320 | writel(0x0, dev->virtbase + I2C_DMAR); |
| 321 | |
| 322 | /* |
| 323 | * set the slsu: |
| 324 | * |
| 325 | * slsu defines the data setup time after SCL clock |
| 326 | * stretching in terms of i2c clk cycles. The |
| 327 | * needed setup time for the three modes are 250ns, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 328 | * 100ns, 10ns respectively thus leading to the values |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 329 | * of 14, 6, 2 for a 48 MHz i2c clk. |
| 330 | */ |
| 331 | writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR); |
| 332 | |
| 333 | i2c_clk = clk_get_rate(dev->clk); |
| 334 | |
| 335 | /* fallback to std. mode if machine has not provided it */ |
| 336 | if (dev->cfg.clk_freq == 0) |
| 337 | dev->cfg.clk_freq = 100000; |
| 338 | |
| 339 | /* |
| 340 | * The spec says, in case of std. mode the divider is |
| 341 | * 2 whereas it is 3 for fast and fastplus mode of |
| 342 | * operation. TODO - high speed support. |
| 343 | */ |
| 344 | div = (dev->cfg.clk_freq > 100000) ? 3 : 2; |
| 345 | |
| 346 | /* |
| 347 | * generate the mask for baud rate counters. The controller |
| 348 | * has two baud rate counters. One is used for High speed |
| 349 | * operation, and the other is for std, fast mode, fast mode |
| 350 | * plus operation. Currently we do not supprt high speed mode |
| 351 | * so set brcr1 to 0. |
| 352 | */ |
| 353 | brcr1 = 0 << 16; |
| 354 | brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff; |
| 355 | |
| 356 | /* set the baud rate counter register */ |
| 357 | writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR); |
| 358 | |
| 359 | /* |
| 360 | * set the speed mode. Currently we support |
| 361 | * only standard and fast mode of operation |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 362 | * TODO - support for fast mode plus (up to 1Mb/s) |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 363 | * and high speed (up to 3.4 Mb/s) |
| 364 | */ |
| 365 | if (dev->cfg.sm > I2C_FREQ_MODE_FAST) { |
| 366 | dev_err(&dev->pdev->dev, "do not support this mode " |
| 367 | "defaulting to std. mode\n"); |
| 368 | brcr2 = i2c_clk/(100000 * 2) & 0xffff; |
| 369 | writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR); |
| 370 | writel(I2C_FREQ_MODE_STANDARD << 4, |
| 371 | dev->virtbase + I2C_CR); |
| 372 | } |
| 373 | writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR); |
| 374 | |
| 375 | /* set the Tx and Rx FIFO threshold */ |
| 376 | writel(dev->cfg.tft, dev->virtbase + I2C_TFTR); |
| 377 | writel(dev->cfg.rft, dev->virtbase + I2C_RFTR); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * read_i2c() - Read from I2C client device |
| 382 | * @dev: private data of I2C Driver |
| 383 | * |
| 384 | * This function reads from i2c client device when controller is in |
| 385 | * master mode. There is a completion timeout. If there is no transfer |
| 386 | * before timeout error is returned. |
| 387 | */ |
| 388 | static int read_i2c(struct nmk_i2c_dev *dev) |
| 389 | { |
| 390 | u32 status = 0; |
| 391 | u32 mcr; |
| 392 | u32 irq_mask = 0; |
| 393 | int timeout; |
| 394 | |
| 395 | mcr = load_i2c_mcr_reg(dev); |
| 396 | writel(mcr, dev->virtbase + I2C_MCR); |
| 397 | |
| 398 | /* load the current CR value */ |
| 399 | writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR, |
| 400 | dev->virtbase + I2C_CR); |
| 401 | |
| 402 | /* enable the controller */ |
| 403 | i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE); |
| 404 | |
| 405 | init_completion(&dev->xfer_complete); |
| 406 | |
| 407 | /* enable interrupts by setting the mask */ |
| 408 | irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF | |
| 409 | I2C_IT_MAL | I2C_IT_BERR); |
| 410 | |
| 411 | if (dev->stop) |
| 412 | irq_mask |= I2C_IT_MTD; |
| 413 | else |
| 414 | irq_mask |= I2C_IT_MTDWS; |
| 415 | |
| 416 | irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask); |
| 417 | |
| 418 | writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask, |
| 419 | dev->virtbase + I2C_IMSCR); |
| 420 | |
srinidhi kasagar | 4b723a4 | 2011-08-09 20:17:22 +0200 | [diff] [blame] | 421 | timeout = wait_for_completion_timeout( |
Virupax Sadashivpetimath | cd20e4fa | 2011-05-13 12:29:46 +0200 | [diff] [blame] | 422 | &dev->xfer_complete, dev->adap.timeout); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 423 | |
| 424 | if (timeout < 0) { |
| 425 | dev_err(&dev->pdev->dev, |
srinidhi kasagar | 4b723a4 | 2011-08-09 20:17:22 +0200 | [diff] [blame] | 426 | "wait_for_completion_timeout" |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 427 | "returned %d waiting for event\n", timeout); |
| 428 | status = timeout; |
| 429 | } |
| 430 | |
| 431 | if (timeout == 0) { |
Virupax Sadashivpetimath | 0511f64 | 2011-05-13 12:30:53 +0200 | [diff] [blame] | 432 | /* Controller timed out */ |
Virupax Sadashivpetimath | 4cb3f53 | 2011-05-13 12:29:55 +0200 | [diff] [blame] | 433 | dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n", |
| 434 | dev->cli.slave_adr); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 435 | status = -ETIMEDOUT; |
| 436 | } |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 437 | return status; |
| 438 | } |
| 439 | |
Virupax Sadashivpetimath | 5535534 | 2011-05-13 12:30:34 +0200 | [diff] [blame] | 440 | static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes) |
| 441 | { |
| 442 | int count; |
| 443 | |
| 444 | for (count = (no_bytes - 2); |
| 445 | (count > 0) && |
| 446 | (dev->cli.count != 0); |
| 447 | count--) { |
| 448 | /* write to the Tx FIFO */ |
| 449 | writeb(*dev->cli.buffer, |
| 450 | dev->virtbase + I2C_TFR); |
| 451 | dev->cli.buffer++; |
| 452 | dev->cli.count--; |
| 453 | dev->cli.xfer_bytes++; |
| 454 | } |
| 455 | |
| 456 | } |
| 457 | |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 458 | /** |
| 459 | * write_i2c() - Write data to I2C client. |
| 460 | * @dev: private data of I2C Driver |
| 461 | * |
| 462 | * This function writes data to I2C client |
| 463 | */ |
| 464 | static int write_i2c(struct nmk_i2c_dev *dev) |
| 465 | { |
| 466 | u32 status = 0; |
| 467 | u32 mcr; |
| 468 | u32 irq_mask = 0; |
| 469 | int timeout; |
| 470 | |
| 471 | mcr = load_i2c_mcr_reg(dev); |
| 472 | |
| 473 | writel(mcr, dev->virtbase + I2C_MCR); |
| 474 | |
| 475 | /* load the current CR value */ |
| 476 | writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR, |
| 477 | dev->virtbase + I2C_CR); |
| 478 | |
| 479 | /* enable the controller */ |
| 480 | i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE); |
| 481 | |
| 482 | init_completion(&dev->xfer_complete); |
| 483 | |
| 484 | /* enable interrupts by settings the masks */ |
Virupax Sadashivpetimath | 5535534 | 2011-05-13 12:30:34 +0200 | [diff] [blame] | 485 | irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR); |
| 486 | |
| 487 | /* Fill the TX FIFO with transmit data */ |
| 488 | fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD); |
| 489 | |
| 490 | if (dev->cli.count != 0) |
| 491 | irq_mask |= I2C_IT_TXFNE; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 492 | |
| 493 | /* |
| 494 | * check if we want to transfer a single or multiple bytes, if so |
| 495 | * set the MTDWS bit (Master Transaction Done Without Stop) |
| 496 | * to start repeated start operation |
| 497 | */ |
| 498 | if (dev->stop) |
| 499 | irq_mask |= I2C_IT_MTD; |
| 500 | else |
| 501 | irq_mask |= I2C_IT_MTDWS; |
| 502 | |
| 503 | irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask); |
| 504 | |
| 505 | writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask, |
| 506 | dev->virtbase + I2C_IMSCR); |
| 507 | |
srinidhi kasagar | 4b723a4 | 2011-08-09 20:17:22 +0200 | [diff] [blame] | 508 | timeout = wait_for_completion_timeout( |
Virupax Sadashivpetimath | cd20e4fa | 2011-05-13 12:29:46 +0200 | [diff] [blame] | 509 | &dev->xfer_complete, dev->adap.timeout); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 510 | |
| 511 | if (timeout < 0) { |
| 512 | dev_err(&dev->pdev->dev, |
Linus Walleij | c8d4763 | 2011-08-09 20:17:29 +0200 | [diff] [blame] | 513 | "wait_for_completion_timeout " |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 514 | "returned %d waiting for event\n", timeout); |
| 515 | status = timeout; |
| 516 | } |
| 517 | |
| 518 | if (timeout == 0) { |
Virupax Sadashivpetimath | 0511f64 | 2011-05-13 12:30:53 +0200 | [diff] [blame] | 519 | /* Controller timed out */ |
Virupax Sadashivpetimath | 4cb3f53 | 2011-05-13 12:29:55 +0200 | [diff] [blame] | 520 | dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n", |
| 521 | dev->cli.slave_adr); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 522 | status = -ETIMEDOUT; |
| 523 | } |
| 524 | |
| 525 | return status; |
| 526 | } |
| 527 | |
| 528 | /** |
Linus Walleij | 82a4413 | 2011-05-13 12:31:01 +0200 | [diff] [blame] | 529 | * nmk_i2c_xfer_one() - transmit a single I2C message |
| 530 | * @dev: device with a message encoded into it |
| 531 | * @flags: message flags |
| 532 | */ |
| 533 | static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags) |
| 534 | { |
| 535 | int status; |
| 536 | |
| 537 | if (flags & I2C_M_RD) { |
| 538 | /* read operation */ |
| 539 | dev->cli.operation = I2C_READ; |
| 540 | status = read_i2c(dev); |
| 541 | } else { |
| 542 | /* write operation */ |
| 543 | dev->cli.operation = I2C_WRITE; |
| 544 | status = write_i2c(dev); |
| 545 | } |
| 546 | |
| 547 | if (status || (dev->result)) { |
| 548 | u32 i2c_sr; |
| 549 | u32 cause; |
| 550 | |
| 551 | i2c_sr = readl(dev->virtbase + I2C_SR); |
| 552 | /* |
| 553 | * Check if the controller I2C operation status |
| 554 | * is set to ABORT(11b). |
| 555 | */ |
| 556 | if (((i2c_sr >> 2) & 0x3) == 0x3) { |
| 557 | /* get the abort cause */ |
| 558 | cause = (i2c_sr >> 4) & 0x7; |
| 559 | dev_err(&dev->pdev->dev, "%s\n", cause |
| 560 | >= ARRAY_SIZE(abort_causes) ? |
| 561 | "unknown reason" : |
| 562 | abort_causes[cause]); |
| 563 | } |
| 564 | |
| 565 | (void) init_hw(dev); |
| 566 | |
| 567 | status = status ? status : dev->result; |
| 568 | } |
| 569 | |
| 570 | return status; |
| 571 | } |
| 572 | |
| 573 | /** |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 574 | * nmk_i2c_xfer() - I2C transfer function used by kernel framework |
Linus Walleij | 1804edd | 2010-09-23 09:03:40 +0200 | [diff] [blame] | 575 | * @i2c_adap: Adapter pointer to the controller |
| 576 | * @msgs: Pointer to data to be written. |
| 577 | * @num_msgs: Number of messages to be executed |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 578 | * |
| 579 | * This is the function called by the generic kernel i2c_transfer() |
| 580 | * or i2c_smbus...() API calls. Note that this code is protected by the |
| 581 | * semaphore set in the kernel i2c_transfer() function. |
| 582 | * |
| 583 | * NOTE: |
| 584 | * READ TRANSFER : We impose a restriction of the first message to be the |
| 585 | * index message for any read transaction. |
| 586 | * - a no index is coded as '0', |
| 587 | * - 2byte big endian index is coded as '3' |
| 588 | * !!! msg[0].buf holds the actual index. |
| 589 | * This is compatible with generic messages of smbus emulator |
| 590 | * that send a one byte index. |
| 591 | * eg. a I2C transation to read 2 bytes from index 0 |
| 592 | * idx = 0; |
| 593 | * msg[0].addr = client->addr; |
| 594 | * msg[0].flags = 0x0; |
| 595 | * msg[0].len = 1; |
| 596 | * msg[0].buf = &idx; |
| 597 | * |
| 598 | * msg[1].addr = client->addr; |
| 599 | * msg[1].flags = I2C_M_RD; |
| 600 | * msg[1].len = 2; |
| 601 | * msg[1].buf = rd_buff |
| 602 | * i2c_transfer(adap, msg, 2); |
| 603 | * |
| 604 | * WRITE TRANSFER : The I2C standard interface interprets all data as payload. |
| 605 | * If you want to emulate an SMBUS write transaction put the |
| 606 | * index as first byte(or first and second) in the payload. |
| 607 | * eg. a I2C transation to write 2 bytes from index 1 |
| 608 | * wr_buff[0] = 0x1; |
| 609 | * wr_buff[1] = 0x23; |
| 610 | * wr_buff[2] = 0x46; |
| 611 | * msg[0].flags = 0x0; |
| 612 | * msg[0].len = 3; |
| 613 | * msg[0].buf = wr_buff; |
| 614 | * i2c_transfer(adap, msg, 1); |
| 615 | * |
| 616 | * To read or write a block of data (multiple bytes) using SMBUS emulation |
| 617 | * please use the i2c_smbus_read_i2c_block_data() |
| 618 | * or i2c_smbus_write_i2c_block_data() API |
| 619 | */ |
| 620 | static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, |
| 621 | struct i2c_msg msgs[], int num_msgs) |
| 622 | { |
| 623 | int status; |
| 624 | int i; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 625 | struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap); |
Virupax Sadashivpetimath | ebd10e0 | 2011-05-13 12:30:23 +0200 | [diff] [blame] | 626 | int j; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 627 | |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 628 | dev->busy = true; |
| 629 | |
| 630 | if (dev->regulator) |
| 631 | regulator_enable(dev->regulator); |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 632 | pm_runtime_get_sync(&dev->pdev->dev); |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 633 | |
Linus Walleij | 8ef4f4e | 2010-09-23 09:03:55 +0200 | [diff] [blame] | 634 | clk_enable(dev->clk); |
| 635 | |
Virupax Sadashivpetimath | ebd10e0 | 2011-05-13 12:30:23 +0200 | [diff] [blame] | 636 | status = init_hw(dev); |
| 637 | if (status) |
| 638 | goto out; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 639 | |
Linus Walleij | 82a4413 | 2011-05-13 12:31:01 +0200 | [diff] [blame] | 640 | /* Attempt three times to send the message queue */ |
Virupax Sadashivpetimath | ebd10e0 | 2011-05-13 12:30:23 +0200 | [diff] [blame] | 641 | for (j = 0; j < 3; j++) { |
| 642 | /* setup the i2c controller */ |
| 643 | setup_i2c_controller(dev); |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 644 | |
Virupax Sadashivpetimath | ebd10e0 | 2011-05-13 12:30:23 +0200 | [diff] [blame] | 645 | for (i = 0; i < num_msgs; i++) { |
| 646 | if (unlikely(msgs[i].flags & I2C_M_TEN)) { |
| 647 | dev_err(&dev->pdev->dev, "10 bit addressing" |
| 648 | "not supported\n"); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 649 | |
Virupax Sadashivpetimath | ebd10e0 | 2011-05-13 12:30:23 +0200 | [diff] [blame] | 650 | status = -EINVAL; |
| 651 | goto out; |
| 652 | } |
| 653 | dev->cli.slave_adr = msgs[i].addr; |
| 654 | dev->cli.buffer = msgs[i].buf; |
| 655 | dev->cli.count = msgs[i].len; |
| 656 | dev->stop = (i < (num_msgs - 1)) ? 0 : 1; |
| 657 | dev->result = 0; |
| 658 | |
Linus Walleij | 82a4413 | 2011-05-13 12:31:01 +0200 | [diff] [blame] | 659 | status = nmk_i2c_xfer_one(dev, msgs[i].flags); |
| 660 | if (status != 0) |
Virupax Sadashivpetimath | ebd10e0 | 2011-05-13 12:30:23 +0200 | [diff] [blame] | 661 | break; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 662 | } |
Virupax Sadashivpetimath | ebd10e0 | 2011-05-13 12:30:23 +0200 | [diff] [blame] | 663 | if (status == 0) |
| 664 | break; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 665 | } |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 666 | |
| 667 | out: |
Linus Walleij | 8ef4f4e | 2010-09-23 09:03:55 +0200 | [diff] [blame] | 668 | clk_disable(dev->clk); |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 669 | pm_runtime_put_sync(&dev->pdev->dev); |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 670 | if (dev->regulator) |
| 671 | regulator_disable(dev->regulator); |
| 672 | |
| 673 | dev->busy = false; |
Linus Walleij | 8ef4f4e | 2010-09-23 09:03:55 +0200 | [diff] [blame] | 674 | |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 675 | /* return the no. messages processed */ |
| 676 | if (status) |
| 677 | return status; |
| 678 | else |
| 679 | return num_msgs; |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * disable_interrupts() - disable the interrupts |
| 684 | * @dev: private data of controller |
Linus Walleij | 1804edd | 2010-09-23 09:03:40 +0200 | [diff] [blame] | 685 | * @irq: interrupt number |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 686 | */ |
| 687 | static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq) |
| 688 | { |
| 689 | irq = IRQ_MASK(irq); |
| 690 | writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq), |
| 691 | dev->virtbase + I2C_IMSCR); |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * i2c_irq_handler() - interrupt routine |
| 697 | * @irq: interrupt number |
| 698 | * @arg: data passed to the handler |
| 699 | * |
| 700 | * This is the interrupt handler for the i2c driver. Currently |
| 701 | * it handles the major interrupts like Rx & Tx FIFO management |
| 702 | * interrupts, master transaction interrupts, arbitration and |
| 703 | * bus error interrupts. The rest of the interrupts are treated as |
| 704 | * unhandled. |
| 705 | */ |
| 706 | static irqreturn_t i2c_irq_handler(int irq, void *arg) |
| 707 | { |
| 708 | struct nmk_i2c_dev *dev = arg; |
| 709 | u32 tft, rft; |
| 710 | u32 count; |
| 711 | u32 misr; |
| 712 | u32 src = 0; |
| 713 | |
| 714 | /* load Tx FIFO and Rx FIFO threshold values */ |
| 715 | tft = readl(dev->virtbase + I2C_TFTR); |
| 716 | rft = readl(dev->virtbase + I2C_RFTR); |
| 717 | |
| 718 | /* read interrupt status register */ |
| 719 | misr = readl(dev->virtbase + I2C_MISR); |
| 720 | |
| 721 | src = __ffs(misr); |
| 722 | switch ((1 << src)) { |
| 723 | |
| 724 | /* Transmit FIFO nearly empty interrupt */ |
| 725 | case I2C_IT_TXFNE: |
| 726 | { |
| 727 | if (dev->cli.operation == I2C_READ) { |
| 728 | /* |
| 729 | * in read operation why do we care for writing? |
| 730 | * so disable the Transmit FIFO interrupt |
| 731 | */ |
| 732 | disable_interrupts(dev, I2C_IT_TXFNE); |
| 733 | } else { |
Virupax Sadashivpetimath | 5535534 | 2011-05-13 12:30:34 +0200 | [diff] [blame] | 734 | fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft)); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 735 | /* |
| 736 | * if done, close the transfer by disabling the |
| 737 | * corresponding TXFNE interrupt |
| 738 | */ |
| 739 | if (dev->cli.count == 0) |
| 740 | disable_interrupts(dev, I2C_IT_TXFNE); |
| 741 | } |
| 742 | } |
| 743 | break; |
| 744 | |
| 745 | /* |
| 746 | * Rx FIFO nearly full interrupt. |
| 747 | * This is set when the numer of entries in Rx FIFO is |
| 748 | * greater or equal than the threshold value programmed |
| 749 | * in RFT |
| 750 | */ |
| 751 | case I2C_IT_RXFNF: |
| 752 | for (count = rft; count > 0; count--) { |
| 753 | /* Read the Rx FIFO */ |
| 754 | *dev->cli.buffer = readb(dev->virtbase + I2C_RFR); |
| 755 | dev->cli.buffer++; |
| 756 | } |
| 757 | dev->cli.count -= rft; |
| 758 | dev->cli.xfer_bytes += rft; |
| 759 | break; |
| 760 | |
| 761 | /* Rx FIFO full */ |
| 762 | case I2C_IT_RXFF: |
| 763 | for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) { |
| 764 | *dev->cli.buffer = readb(dev->virtbase + I2C_RFR); |
| 765 | dev->cli.buffer++; |
| 766 | } |
| 767 | dev->cli.count -= MAX_I2C_FIFO_THRESHOLD; |
| 768 | dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD; |
| 769 | break; |
| 770 | |
| 771 | /* Master Transaction Done with/without stop */ |
| 772 | case I2C_IT_MTD: |
| 773 | case I2C_IT_MTDWS: |
| 774 | if (dev->cli.operation == I2C_READ) { |
Rabin Vincent | 1df3ab1 | 2010-04-27 10:31:08 +0530 | [diff] [blame] | 775 | while (!(readl(dev->virtbase + I2C_RISR) |
| 776 | & I2C_IT_RXFE)) { |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 777 | if (dev->cli.count == 0) |
| 778 | break; |
| 779 | *dev->cli.buffer = |
| 780 | readb(dev->virtbase + I2C_RFR); |
| 781 | dev->cli.buffer++; |
| 782 | dev->cli.count--; |
| 783 | dev->cli.xfer_bytes++; |
| 784 | } |
| 785 | } |
| 786 | |
Virupax Sadashivpetimath | b5e890f | 2011-05-13 12:30:42 +0200 | [diff] [blame] | 787 | disable_all_interrupts(dev); |
| 788 | clear_all_interrupts(dev); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 789 | |
| 790 | if (dev->cli.count) { |
Virupax Sadashivpetimath | 99381be | 2011-05-13 12:29:28 +0200 | [diff] [blame] | 791 | dev->result = -EIO; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 792 | dev_err(&dev->pdev->dev, "%lu bytes still remain to be" |
| 793 | "xfered\n", dev->cli.count); |
| 794 | (void) init_hw(dev); |
| 795 | } |
| 796 | complete(&dev->xfer_complete); |
| 797 | |
| 798 | break; |
| 799 | |
| 800 | /* Master Arbitration lost interrupt */ |
| 801 | case I2C_IT_MAL: |
Virupax Sadashivpetimath | 99381be | 2011-05-13 12:29:28 +0200 | [diff] [blame] | 802 | dev->result = -EIO; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 803 | (void) init_hw(dev); |
| 804 | |
| 805 | i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL); |
| 806 | complete(&dev->xfer_complete); |
| 807 | |
| 808 | break; |
| 809 | |
| 810 | /* |
| 811 | * Bus Error interrupt. |
| 812 | * This happens when an unexpected start/stop condition occurs |
| 813 | * during the transaction. |
| 814 | */ |
| 815 | case I2C_IT_BERR: |
Virupax Sadashivpetimath | 99381be | 2011-05-13 12:29:28 +0200 | [diff] [blame] | 816 | dev->result = -EIO; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 817 | /* get the status */ |
| 818 | if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT) |
| 819 | (void) init_hw(dev); |
| 820 | |
| 821 | i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR); |
| 822 | complete(&dev->xfer_complete); |
| 823 | |
| 824 | break; |
| 825 | |
| 826 | /* |
| 827 | * Tx FIFO overrun interrupt. |
| 828 | * This is set when a write operation in Tx FIFO is performed and |
| 829 | * the Tx FIFO is full. |
| 830 | */ |
| 831 | case I2C_IT_TXFOVR: |
Virupax Sadashivpetimath | 99381be | 2011-05-13 12:29:28 +0200 | [diff] [blame] | 832 | dev->result = -EIO; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 833 | (void) init_hw(dev); |
| 834 | |
| 835 | dev_err(&dev->pdev->dev, "Tx Fifo Over run\n"); |
| 836 | complete(&dev->xfer_complete); |
| 837 | |
| 838 | break; |
| 839 | |
| 840 | /* unhandled interrupts by this driver - TODO*/ |
| 841 | case I2C_IT_TXFE: |
| 842 | case I2C_IT_TXFF: |
| 843 | case I2C_IT_RXFE: |
| 844 | case I2C_IT_RFSR: |
| 845 | case I2C_IT_RFSE: |
| 846 | case I2C_IT_WTSR: |
| 847 | case I2C_IT_STD: |
| 848 | dev_err(&dev->pdev->dev, "unhandled Interrupt\n"); |
| 849 | break; |
| 850 | default: |
| 851 | dev_err(&dev->pdev->dev, "spurious Interrupt..\n"); |
| 852 | break; |
| 853 | } |
| 854 | |
| 855 | return IRQ_HANDLED; |
| 856 | } |
| 857 | |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 858 | |
| 859 | #ifdef CONFIG_PM |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 860 | static int nmk_i2c_suspend(struct device *dev) |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 861 | { |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 862 | struct platform_device *pdev = to_platform_device(dev); |
| 863 | struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev); |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 864 | |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 865 | if (nmk_i2c->busy) |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 866 | return -EBUSY; |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 867 | |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static int nmk_i2c_resume(struct device *dev) |
| 872 | { |
| 873 | return 0; |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 874 | } |
| 875 | #else |
| 876 | #define nmk_i2c_suspend NULL |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 877 | #define nmk_i2c_resume NULL |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 878 | #endif |
| 879 | |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 880 | /* |
| 881 | * We use noirq so that we suspend late and resume before the wakeup interrupt |
| 882 | * to ensure that we do the !pm_runtime_suspended() check in resume before |
| 883 | * there has been a regular pm runtime resume (via pm_runtime_get_sync()). |
| 884 | */ |
| 885 | static const struct dev_pm_ops nmk_i2c_pm = { |
| 886 | .suspend_noirq = nmk_i2c_suspend, |
| 887 | .resume_noirq = nmk_i2c_resume, |
| 888 | }; |
| 889 | |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 890 | static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap) |
| 891 | { |
Linus Walleij | 5680bc6e | 2010-09-23 09:04:03 +0200 | [diff] [blame] | 892 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static const struct i2c_algorithm nmk_i2c_algo = { |
| 896 | .master_xfer = nmk_i2c_xfer, |
| 897 | .functionality = nmk_i2c_functionality |
| 898 | }; |
| 899 | |
| 900 | static int __devinit nmk_i2c_probe(struct platform_device *pdev) |
| 901 | { |
| 902 | int ret = 0; |
| 903 | struct resource *res; |
| 904 | struct nmk_i2c_controller *pdata = |
| 905 | pdev->dev.platform_data; |
| 906 | struct nmk_i2c_dev *dev; |
| 907 | struct i2c_adapter *adap; |
| 908 | |
| 909 | dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL); |
| 910 | if (!dev) { |
| 911 | dev_err(&pdev->dev, "cannot allocate memory\n"); |
| 912 | ret = -ENOMEM; |
| 913 | goto err_no_mem; |
| 914 | } |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 915 | dev->busy = false; |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 916 | dev->pdev = pdev; |
| 917 | platform_set_drvdata(pdev, dev); |
| 918 | |
| 919 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 920 | if (!res) { |
| 921 | ret = -ENOENT; |
| 922 | goto err_no_resource; |
| 923 | } |
| 924 | |
| 925 | if (request_mem_region(res->start, resource_size(res), |
| 926 | DRIVER_NAME "I/O region") == NULL) { |
| 927 | ret = -EBUSY; |
| 928 | goto err_no_region; |
| 929 | } |
| 930 | |
| 931 | dev->virtbase = ioremap(res->start, resource_size(res)); |
| 932 | if (!dev->virtbase) { |
| 933 | ret = -ENOMEM; |
| 934 | goto err_no_ioremap; |
| 935 | } |
| 936 | |
| 937 | dev->irq = platform_get_irq(pdev, 0); |
| 938 | ret = request_irq(dev->irq, i2c_irq_handler, IRQF_DISABLED, |
| 939 | DRIVER_NAME, dev); |
| 940 | if (ret) { |
| 941 | dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq); |
| 942 | goto err_irq; |
| 943 | } |
| 944 | |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 945 | dev->regulator = regulator_get(&pdev->dev, "v-i2c"); |
| 946 | if (IS_ERR(dev->regulator)) { |
| 947 | dev_warn(&pdev->dev, "could not get i2c regulator\n"); |
| 948 | dev->regulator = NULL; |
| 949 | } |
| 950 | |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 951 | pm_suspend_ignore_children(&pdev->dev, true); |
| 952 | pm_runtime_enable(&pdev->dev); |
| 953 | |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 954 | dev->clk = clk_get(&pdev->dev, NULL); |
| 955 | if (IS_ERR(dev->clk)) { |
| 956 | dev_err(&pdev->dev, "could not get i2c clock\n"); |
| 957 | ret = PTR_ERR(dev->clk); |
| 958 | goto err_no_clk; |
| 959 | } |
| 960 | |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 961 | adap = &dev->adap; |
| 962 | adap->dev.parent = &pdev->dev; |
| 963 | adap->owner = THIS_MODULE; |
| 964 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
| 965 | adap->algo = &nmk_i2c_algo; |
Virupax Sadashivpetimath | cd20e4fa | 2011-05-13 12:29:46 +0200 | [diff] [blame] | 966 | adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) : |
| 967 | msecs_to_jiffies(20000); |
Linus Walleij | 6d779a4 | 2010-11-30 16:59:29 +0100 | [diff] [blame] | 968 | snprintf(adap->name, sizeof(adap->name), |
| 969 | "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 970 | |
| 971 | /* fetch the controller id */ |
| 972 | adap->nr = pdev->id; |
| 973 | |
| 974 | /* fetch the controller configuration from machine */ |
| 975 | dev->cfg.clk_freq = pdata->clk_freq; |
| 976 | dev->cfg.slsu = pdata->slsu; |
| 977 | dev->cfg.tft = pdata->tft; |
| 978 | dev->cfg.rft = pdata->rft; |
| 979 | dev->cfg.sm = pdata->sm; |
| 980 | |
| 981 | i2c_set_adapdata(adap, dev); |
| 982 | |
Linus Walleij | 6d779a4 | 2010-11-30 16:59:29 +0100 | [diff] [blame] | 983 | dev_info(&pdev->dev, "initialize %s on virtual " |
| 984 | "base %p\n", adap->name, dev->virtbase); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 985 | |
| 986 | ret = i2c_add_numbered_adapter(adap); |
| 987 | if (ret) { |
| 988 | dev_err(&pdev->dev, "failed to add adapter\n"); |
| 989 | goto err_add_adap; |
| 990 | } |
| 991 | |
| 992 | return 0; |
| 993 | |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 994 | err_add_adap: |
| 995 | clk_put(dev->clk); |
| 996 | err_no_clk: |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 997 | if (dev->regulator) |
| 998 | regulator_put(dev->regulator); |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 999 | pm_runtime_disable(&pdev->dev); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 1000 | free_irq(dev->irq, dev); |
| 1001 | err_irq: |
| 1002 | iounmap(dev->virtbase); |
| 1003 | err_no_ioremap: |
| 1004 | release_mem_region(res->start, resource_size(res)); |
| 1005 | err_no_region: |
| 1006 | platform_set_drvdata(pdev, NULL); |
| 1007 | err_no_resource: |
| 1008 | kfree(dev); |
| 1009 | err_no_mem: |
| 1010 | |
| 1011 | return ret; |
| 1012 | } |
| 1013 | |
| 1014 | static int __devexit nmk_i2c_remove(struct platform_device *pdev) |
| 1015 | { |
Rabin Vincent | a1c2767 | 2010-04-27 10:31:07 +0530 | [diff] [blame] | 1016 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 1017 | struct nmk_i2c_dev *dev = platform_get_drvdata(pdev); |
| 1018 | |
| 1019 | i2c_del_adapter(&dev->adap); |
| 1020 | flush_i2c_fifo(dev); |
| 1021 | disable_all_interrupts(dev); |
| 1022 | clear_all_interrupts(dev); |
| 1023 | /* disable the controller */ |
| 1024 | i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE); |
| 1025 | free_irq(dev->irq, dev); |
| 1026 | iounmap(dev->virtbase); |
Rabin Vincent | a1c2767 | 2010-04-27 10:31:07 +0530 | [diff] [blame] | 1027 | if (res) |
| 1028 | release_mem_region(res->start, resource_size(res)); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 1029 | clk_put(dev->clk); |
Jonas Aberg | a20d239 | 2011-05-13 12:29:02 +0200 | [diff] [blame] | 1030 | if (dev->regulator) |
| 1031 | regulator_put(dev->regulator); |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 1032 | pm_runtime_disable(&pdev->dev); |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 1033 | platform_set_drvdata(pdev, NULL); |
| 1034 | kfree(dev); |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | static struct platform_driver nmk_i2c_driver = { |
| 1040 | .driver = { |
| 1041 | .owner = THIS_MODULE, |
| 1042 | .name = DRIVER_NAME, |
Rabin Vincent | b0e751a | 2011-05-13 12:30:07 +0200 | [diff] [blame] | 1043 | .pm = &nmk_i2c_pm, |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 1044 | }, |
| 1045 | .probe = nmk_i2c_probe, |
| 1046 | .remove = __devexit_p(nmk_i2c_remove), |
srinidhi kasagar | 3f9900f | 2010-02-01 19:44:54 +0530 | [diff] [blame] | 1047 | }; |
| 1048 | |
| 1049 | static int __init nmk_i2c_init(void) |
| 1050 | { |
| 1051 | return platform_driver_register(&nmk_i2c_driver); |
| 1052 | } |
| 1053 | |
| 1054 | static void __exit nmk_i2c_exit(void) |
| 1055 | { |
| 1056 | platform_driver_unregister(&nmk_i2c_driver); |
| 1057 | } |
| 1058 | |
| 1059 | subsys_initcall(nmk_i2c_init); |
| 1060 | module_exit(nmk_i2c_exit); |
| 1061 | |
| 1062 | MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR"); |
| 1063 | MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver"); |
| 1064 | MODULE_LICENSE("GPL"); |
| 1065 | MODULE_ALIAS("platform:" DRIVER_NAME); |