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