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