Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * i2c Support for Atmel's AT91 Two-Wire Interface (TWI) |
| 3 | * |
| 4 | * Copyright (C) 2011 Weinmann Medical GmbH |
| 5 | * Author: Nikolaus Voss <n.voss@weinmann.de> |
| 6 | * |
| 7 | * Evolved from original work by: |
| 8 | * Copyright (C) 2004 Rick Bronson |
| 9 | * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com> |
| 10 | * |
| 11 | * Borrowed heavily from original work by: |
| 12 | * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/clk.h> |
| 21 | #include <linux/completion.h> |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 22 | #include <linux/dma-mapping.h> |
| 23 | #include <linux/dmaengine.h> |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 24 | #include <linux/err.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/module.h> |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 29 | #include <linux/of.h> |
| 30 | #include <linux/of_device.h> |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/slab.h> |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 33 | #include <linux/platform_data/dma-atmel.h> |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 34 | #include <linux/pm_runtime.h> |
Wenyou Yang | 62d10c4 | 2014-11-10 09:55:52 +0800 | [diff] [blame] | 35 | #include <linux/pinctrl/consumer.h> |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 36 | |
Marek Roszko | 75b6c4b | 2014-03-11 00:25:38 -0400 | [diff] [blame] | 37 | #define DEFAULT_TWI_CLK_HZ 100000 /* max 400 Kbits/s */ |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 38 | #define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */ |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 39 | #define AT91_I2C_DMA_THRESHOLD 8 /* enable DMA if transfer size is bigger than this threshold */ |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 40 | #define AUTOSUSPEND_TIMEOUT 2000 |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 41 | |
| 42 | /* AT91 TWI register definitions */ |
| 43 | #define AT91_TWI_CR 0x0000 /* Control Register */ |
Cyrille Pitchen | e84cf8f | 2015-06-09 18:22:15 +0200 | [diff] [blame] | 44 | #define AT91_TWI_START BIT(0) /* Send a Start Condition */ |
| 45 | #define AT91_TWI_STOP BIT(1) /* Send a Stop Condition */ |
| 46 | #define AT91_TWI_MSEN BIT(2) /* Master Transfer Enable */ |
| 47 | #define AT91_TWI_MSDIS BIT(3) /* Master Transfer Disable */ |
| 48 | #define AT91_TWI_SVEN BIT(4) /* Slave Transfer Enable */ |
| 49 | #define AT91_TWI_SVDIS BIT(5) /* Slave Transfer Disable */ |
| 50 | #define AT91_TWI_QUICK BIT(6) /* SMBus quick command */ |
| 51 | #define AT91_TWI_SWRST BIT(7) /* Software Reset */ |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 52 | |
| 53 | #define AT91_TWI_MMR 0x0004 /* Master Mode Register */ |
| 54 | #define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */ |
Cyrille Pitchen | e84cf8f | 2015-06-09 18:22:15 +0200 | [diff] [blame] | 55 | #define AT91_TWI_MREAD BIT(12) /* Master Read Direction */ |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 56 | |
| 57 | #define AT91_TWI_IADR 0x000c /* Internal Address Register */ |
| 58 | |
| 59 | #define AT91_TWI_CWGR 0x0010 /* Clock Waveform Generator Reg */ |
| 60 | |
| 61 | #define AT91_TWI_SR 0x0020 /* Status Register */ |
Cyrille Pitchen | e84cf8f | 2015-06-09 18:22:15 +0200 | [diff] [blame] | 62 | #define AT91_TWI_TXCOMP BIT(0) /* Transmission Complete */ |
| 63 | #define AT91_TWI_RXRDY BIT(1) /* Receive Holding Register Ready */ |
| 64 | #define AT91_TWI_TXRDY BIT(2) /* Transmit Holding Register Ready */ |
| 65 | #define AT91_TWI_OVRE BIT(6) /* Overrun Error */ |
| 66 | #define AT91_TWI_UNRE BIT(7) /* Underrun Error */ |
| 67 | #define AT91_TWI_NACK BIT(8) /* Not Acknowledged */ |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 68 | |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 69 | #define AT91_TWI_INT_MASK \ |
| 70 | (AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY | AT91_TWI_NACK) |
| 71 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 72 | #define AT91_TWI_IER 0x0024 /* Interrupt Enable Register */ |
| 73 | #define AT91_TWI_IDR 0x0028 /* Interrupt Disable Register */ |
| 74 | #define AT91_TWI_IMR 0x002c /* Interrupt Mask Register */ |
| 75 | #define AT91_TWI_RHR 0x0030 /* Receive Holding Register */ |
| 76 | #define AT91_TWI_THR 0x0034 /* Transmit Holding Register */ |
| 77 | |
| 78 | struct at91_twi_pdata { |
Ludovic Desroches | 5f43381 | 2012-11-23 10:09:03 +0100 | [diff] [blame] | 79 | unsigned clk_max_div; |
| 80 | unsigned clk_offset; |
| 81 | bool has_unre_flag; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 82 | struct at_dma_slave dma_slave; |
| 83 | }; |
| 84 | |
| 85 | struct at91_twi_dma { |
| 86 | struct dma_chan *chan_rx; |
| 87 | struct dma_chan *chan_tx; |
| 88 | struct scatterlist sg; |
| 89 | struct dma_async_tx_descriptor *data_desc; |
| 90 | enum dma_data_direction direction; |
| 91 | bool buf_mapped; |
| 92 | bool xfer_in_progress; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | struct at91_twi_dev { |
Ludovic Desroches | 5f43381 | 2012-11-23 10:09:03 +0100 | [diff] [blame] | 96 | struct device *dev; |
| 97 | void __iomem *base; |
| 98 | struct completion cmd_complete; |
| 99 | struct clk *clk; |
| 100 | u8 *buf; |
| 101 | size_t buf_len; |
| 102 | struct i2c_msg *msg; |
| 103 | int irq; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 104 | unsigned imr; |
Ludovic Desroches | 5f43381 | 2012-11-23 10:09:03 +0100 | [diff] [blame] | 105 | unsigned transfer_status; |
| 106 | struct i2c_adapter adapter; |
| 107 | unsigned twi_cwgr_reg; |
| 108 | struct at91_twi_pdata *pdata; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 109 | bool use_dma; |
Marek Roszko | 75b81f3 | 2014-08-20 21:39:41 -0400 | [diff] [blame] | 110 | bool recv_len_abort; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 111 | struct at91_twi_dma dma; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg) |
| 115 | { |
| 116 | return readl_relaxed(dev->base + reg); |
| 117 | } |
| 118 | |
| 119 | static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val) |
| 120 | { |
| 121 | writel_relaxed(val, dev->base + reg); |
| 122 | } |
| 123 | |
| 124 | static void at91_disable_twi_interrupts(struct at91_twi_dev *dev) |
| 125 | { |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 126 | at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_INT_MASK); |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 129 | static void at91_twi_irq_save(struct at91_twi_dev *dev) |
| 130 | { |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 131 | dev->imr = at91_twi_read(dev, AT91_TWI_IMR) & AT91_TWI_INT_MASK; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 132 | at91_disable_twi_interrupts(dev); |
| 133 | } |
| 134 | |
| 135 | static void at91_twi_irq_restore(struct at91_twi_dev *dev) |
| 136 | { |
| 137 | at91_twi_write(dev, AT91_TWI_IER, dev->imr); |
| 138 | } |
| 139 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 140 | static void at91_init_twi_bus(struct at91_twi_dev *dev) |
| 141 | { |
| 142 | at91_disable_twi_interrupts(dev); |
| 143 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST); |
| 144 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN); |
| 145 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS); |
| 146 | at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Calculate symmetric clock as stated in datasheet: |
| 151 | * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset)) |
| 152 | */ |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 153 | static void at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk) |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 154 | { |
| 155 | int ckdiv, cdiv, div; |
| 156 | struct at91_twi_pdata *pdata = dev->pdata; |
| 157 | int offset = pdata->clk_offset; |
| 158 | int max_ckdiv = pdata->clk_max_div; |
| 159 | |
| 160 | div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk), |
| 161 | 2 * twi_clk) - offset); |
| 162 | ckdiv = fls(div >> 8); |
| 163 | cdiv = div >> ckdiv; |
| 164 | |
| 165 | if (ckdiv > max_ckdiv) { |
| 166 | dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n", |
| 167 | ckdiv, max_ckdiv); |
| 168 | ckdiv = max_ckdiv; |
| 169 | cdiv = 255; |
| 170 | } |
| 171 | |
| 172 | dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv; |
| 173 | dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv); |
| 174 | } |
| 175 | |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 176 | static void at91_twi_dma_cleanup(struct at91_twi_dev *dev) |
| 177 | { |
| 178 | struct at91_twi_dma *dma = &dev->dma; |
| 179 | |
| 180 | at91_twi_irq_save(dev); |
| 181 | |
| 182 | if (dma->xfer_in_progress) { |
| 183 | if (dma->direction == DMA_FROM_DEVICE) |
| 184 | dmaengine_terminate_all(dma->chan_rx); |
| 185 | else |
| 186 | dmaengine_terminate_all(dma->chan_tx); |
| 187 | dma->xfer_in_progress = false; |
| 188 | } |
| 189 | if (dma->buf_mapped) { |
| 190 | dma_unmap_single(dev->dev, sg_dma_address(&dma->sg), |
| 191 | dev->buf_len, dma->direction); |
| 192 | dma->buf_mapped = false; |
| 193 | } |
| 194 | |
| 195 | at91_twi_irq_restore(dev); |
| 196 | } |
| 197 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 198 | static void at91_twi_write_next_byte(struct at91_twi_dev *dev) |
| 199 | { |
| 200 | if (dev->buf_len <= 0) |
| 201 | return; |
| 202 | |
| 203 | at91_twi_write(dev, AT91_TWI_THR, *dev->buf); |
| 204 | |
| 205 | /* send stop when last byte has been written */ |
| 206 | if (--dev->buf_len == 0) |
| 207 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); |
| 208 | |
| 209 | dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len); |
| 210 | |
| 211 | ++dev->buf; |
| 212 | } |
| 213 | |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 214 | static void at91_twi_write_data_dma_callback(void *data) |
| 215 | { |
| 216 | struct at91_twi_dev *dev = (struct at91_twi_dev *)data; |
| 217 | |
| 218 | dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg), |
Wolfram Sang | 28772ac | 2014-07-21 11:42:03 +0200 | [diff] [blame] | 219 | dev->buf_len, DMA_TO_DEVICE); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 220 | |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 221 | /* |
| 222 | * When this callback is called, THR/TX FIFO is likely not to be empty |
| 223 | * yet. So we have to wait for TXCOMP or NACK bits to be set into the |
| 224 | * Status Register to be sure that the STOP bit has been sent and the |
| 225 | * transfer is completed. The NACK interrupt has already been enabled, |
| 226 | * we just have to enable TXCOMP one. |
| 227 | */ |
| 228 | at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 229 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); |
| 230 | } |
| 231 | |
| 232 | static void at91_twi_write_data_dma(struct at91_twi_dev *dev) |
| 233 | { |
| 234 | dma_addr_t dma_addr; |
| 235 | struct dma_async_tx_descriptor *txdesc; |
| 236 | struct at91_twi_dma *dma = &dev->dma; |
| 237 | struct dma_chan *chan_tx = dma->chan_tx; |
| 238 | |
| 239 | if (dev->buf_len <= 0) |
| 240 | return; |
| 241 | |
| 242 | dma->direction = DMA_TO_DEVICE; |
| 243 | |
| 244 | at91_twi_irq_save(dev); |
| 245 | dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len, |
| 246 | DMA_TO_DEVICE); |
| 247 | if (dma_mapping_error(dev->dev, dma_addr)) { |
| 248 | dev_err(dev->dev, "dma map failed\n"); |
| 249 | return; |
| 250 | } |
| 251 | dma->buf_mapped = true; |
| 252 | at91_twi_irq_restore(dev); |
| 253 | sg_dma_len(&dma->sg) = dev->buf_len; |
| 254 | sg_dma_address(&dma->sg) = dma_addr; |
| 255 | |
| 256 | txdesc = dmaengine_prep_slave_sg(chan_tx, &dma->sg, 1, DMA_MEM_TO_DEV, |
| 257 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 258 | if (!txdesc) { |
| 259 | dev_err(dev->dev, "dma prep slave sg failed\n"); |
| 260 | goto error; |
| 261 | } |
| 262 | |
| 263 | txdesc->callback = at91_twi_write_data_dma_callback; |
| 264 | txdesc->callback_param = dev; |
| 265 | |
| 266 | dma->xfer_in_progress = true; |
| 267 | dmaengine_submit(txdesc); |
| 268 | dma_async_issue_pending(chan_tx); |
| 269 | |
| 270 | return; |
| 271 | |
| 272 | error: |
| 273 | at91_twi_dma_cleanup(dev); |
| 274 | } |
| 275 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 276 | static void at91_twi_read_next_byte(struct at91_twi_dev *dev) |
| 277 | { |
| 278 | if (dev->buf_len <= 0) |
| 279 | return; |
| 280 | |
| 281 | *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff; |
| 282 | --dev->buf_len; |
| 283 | |
Marek Roszko | 75b81f3 | 2014-08-20 21:39:41 -0400 | [diff] [blame] | 284 | /* return if aborting, we only needed to read RHR to clear RXRDY*/ |
| 285 | if (dev->recv_len_abort) |
| 286 | return; |
| 287 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 288 | /* handle I2C_SMBUS_BLOCK_DATA */ |
| 289 | if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) { |
Marek Roszko | 75b81f3 | 2014-08-20 21:39:41 -0400 | [diff] [blame] | 290 | /* ensure length byte is a valid value */ |
| 291 | if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) { |
| 292 | dev->msg->flags &= ~I2C_M_RECV_LEN; |
| 293 | dev->buf_len += *dev->buf; |
| 294 | dev->msg->len = dev->buf_len + 1; |
| 295 | dev_dbg(dev->dev, "received block length %d\n", |
| 296 | dev->buf_len); |
| 297 | } else { |
| 298 | /* abort and send the stop by reading one more byte */ |
| 299 | dev->recv_len_abort = true; |
| 300 | dev->buf_len = 1; |
| 301 | } |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* send stop if second but last byte has been read */ |
| 305 | if (dev->buf_len == 1) |
| 306 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); |
| 307 | |
| 308 | dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len); |
| 309 | |
| 310 | ++dev->buf; |
| 311 | } |
| 312 | |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 313 | static void at91_twi_read_data_dma_callback(void *data) |
| 314 | { |
| 315 | struct at91_twi_dev *dev = (struct at91_twi_dev *)data; |
| 316 | |
| 317 | dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg), |
Wolfram Sang | 28772ac | 2014-07-21 11:42:03 +0200 | [diff] [blame] | 318 | dev->buf_len, DMA_FROM_DEVICE); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 319 | |
| 320 | /* The last two bytes have to be read without using dma */ |
| 321 | dev->buf += dev->buf_len - 2; |
| 322 | dev->buf_len = 2; |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 323 | at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_RXRDY | AT91_TWI_TXCOMP); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void at91_twi_read_data_dma(struct at91_twi_dev *dev) |
| 327 | { |
| 328 | dma_addr_t dma_addr; |
| 329 | struct dma_async_tx_descriptor *rxdesc; |
| 330 | struct at91_twi_dma *dma = &dev->dma; |
| 331 | struct dma_chan *chan_rx = dma->chan_rx; |
| 332 | |
| 333 | dma->direction = DMA_FROM_DEVICE; |
| 334 | |
| 335 | /* Keep in mind that we won't use dma to read the last two bytes */ |
| 336 | at91_twi_irq_save(dev); |
| 337 | dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len - 2, |
| 338 | DMA_FROM_DEVICE); |
| 339 | if (dma_mapping_error(dev->dev, dma_addr)) { |
| 340 | dev_err(dev->dev, "dma map failed\n"); |
| 341 | return; |
| 342 | } |
| 343 | dma->buf_mapped = true; |
| 344 | at91_twi_irq_restore(dev); |
| 345 | dma->sg.dma_address = dma_addr; |
| 346 | sg_dma_len(&dma->sg) = dev->buf_len - 2; |
| 347 | |
| 348 | rxdesc = dmaengine_prep_slave_sg(chan_rx, &dma->sg, 1, DMA_DEV_TO_MEM, |
| 349 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 350 | if (!rxdesc) { |
| 351 | dev_err(dev->dev, "dma prep slave sg failed\n"); |
| 352 | goto error; |
| 353 | } |
| 354 | |
| 355 | rxdesc->callback = at91_twi_read_data_dma_callback; |
| 356 | rxdesc->callback_param = dev; |
| 357 | |
| 358 | dma->xfer_in_progress = true; |
| 359 | dmaengine_submit(rxdesc); |
| 360 | dma_async_issue_pending(dma->chan_rx); |
| 361 | |
| 362 | return; |
| 363 | |
| 364 | error: |
| 365 | at91_twi_dma_cleanup(dev); |
| 366 | } |
| 367 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 368 | static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id) |
| 369 | { |
| 370 | struct at91_twi_dev *dev = dev_id; |
| 371 | const unsigned status = at91_twi_read(dev, AT91_TWI_SR); |
| 372 | const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR); |
| 373 | |
| 374 | if (!irqstatus) |
| 375 | return IRQ_NONE; |
| 376 | else if (irqstatus & AT91_TWI_RXRDY) |
| 377 | at91_twi_read_next_byte(dev); |
| 378 | else if (irqstatus & AT91_TWI_TXRDY) |
| 379 | at91_twi_write_next_byte(dev); |
| 380 | |
| 381 | /* catch error flags */ |
| 382 | dev->transfer_status |= status; |
| 383 | |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 384 | if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) { |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 385 | at91_disable_twi_interrupts(dev); |
| 386 | complete(&dev->cmd_complete); |
| 387 | } |
| 388 | |
| 389 | return IRQ_HANDLED; |
| 390 | } |
| 391 | |
| 392 | static int at91_do_twi_transfer(struct at91_twi_dev *dev) |
| 393 | { |
| 394 | int ret; |
Nicholas Mc Guire | 1c42aca | 2015-02-08 11:12:07 -0500 | [diff] [blame] | 395 | unsigned long time_left; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 396 | bool has_unre_flag = dev->pdata->has_unre_flag; |
| 397 | |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 398 | /* |
| 399 | * WARNING: the TXCOMP bit in the Status Register is NOT a clear on |
| 400 | * read flag but shows the state of the transmission at the time the |
| 401 | * Status Register is read. According to the programmer datasheet, |
| 402 | * TXCOMP is set when both holding register and internal shifter are |
| 403 | * empty and STOP condition has been sent. |
| 404 | * Consequently, we should enable NACK interrupt rather than TXCOMP to |
| 405 | * detect transmission failure. |
| 406 | * |
| 407 | * Besides, the TXCOMP bit is already set before the i2c transaction |
| 408 | * has been started. For read transactions, this bit is cleared when |
| 409 | * writing the START bit into the Control Register. So the |
| 410 | * corresponding interrupt can safely be enabled just after. |
| 411 | * However for write transactions managed by the CPU, we first write |
| 412 | * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP |
| 413 | * interrupt. If TXCOMP interrupt were enabled before writing into THR, |
| 414 | * the interrupt handler would be called immediately and the i2c command |
| 415 | * would be reported as completed. |
| 416 | * Also when a write transaction is managed by the DMA controller, |
| 417 | * enabling the TXCOMP interrupt in this function may lead to a race |
| 418 | * condition since we don't know whether the TXCOMP interrupt is enabled |
| 419 | * before or after the DMA has started to write into THR. So the TXCOMP |
| 420 | * interrupt is enabled later by at91_twi_write_data_dma_callback(). |
| 421 | * Immediately after in that DMA callback, we still need to send the |
| 422 | * STOP condition manually writing the corresponding bit into the |
| 423 | * Control Register. |
| 424 | */ |
| 425 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 426 | dev_dbg(dev->dev, "transfer: %s %d bytes.\n", |
| 427 | (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len); |
| 428 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 429 | reinit_completion(&dev->cmd_complete); |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 430 | dev->transfer_status = 0; |
Ludovic Desroches | 7c3fe64 | 2012-11-13 16:43:21 +0100 | [diff] [blame] | 431 | |
| 432 | if (!dev->buf_len) { |
| 433 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK); |
| 434 | at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP); |
| 435 | } else if (dev->msg->flags & I2C_M_RD) { |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 436 | unsigned start_flags = AT91_TWI_START; |
| 437 | |
| 438 | if (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY) { |
| 439 | dev_err(dev->dev, "RXRDY still set!"); |
| 440 | at91_twi_read(dev, AT91_TWI_RHR); |
| 441 | } |
| 442 | |
| 443 | /* if only one byte is to be read, immediately stop transfer */ |
| 444 | if (dev->buf_len <= 1 && !(dev->msg->flags & I2C_M_RECV_LEN)) |
| 445 | start_flags |= AT91_TWI_STOP; |
| 446 | at91_twi_write(dev, AT91_TWI_CR, start_flags); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 447 | /* |
| 448 | * When using dma, the last byte has to be read manually in |
| 449 | * order to not send the stop command too late and then |
| 450 | * to receive extra data. In practice, there are some issues |
| 451 | * if you use the dma to read n-1 bytes because of latency. |
| 452 | * Reading n-2 bytes with dma and the two last ones manually |
| 453 | * seems to be the best solution. |
| 454 | */ |
| 455 | if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) { |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 456 | at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 457 | at91_twi_read_data_dma(dev); |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 458 | } else { |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 459 | at91_twi_write(dev, AT91_TWI_IER, |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 460 | AT91_TWI_TXCOMP | |
| 461 | AT91_TWI_NACK | |
| 462 | AT91_TWI_RXRDY); |
| 463 | } |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 464 | } else { |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 465 | if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) { |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 466 | at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 467 | at91_twi_write_data_dma(dev); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 468 | } else { |
| 469 | at91_twi_write_next_byte(dev); |
| 470 | at91_twi_write(dev, AT91_TWI_IER, |
Cyrille Pitchen | 93563a6 | 2015-06-09 18:22:14 +0200 | [diff] [blame] | 471 | AT91_TWI_TXCOMP | |
| 472 | AT91_TWI_NACK | |
| 473 | AT91_TWI_TXRDY); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 474 | } |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Nicholas Mc Guire | 1c42aca | 2015-02-08 11:12:07 -0500 | [diff] [blame] | 477 | time_left = wait_for_completion_timeout(&dev->cmd_complete, |
| 478 | dev->adapter.timeout); |
| 479 | if (time_left == 0) { |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 480 | dev_err(dev->dev, "controller timed out\n"); |
| 481 | at91_init_twi_bus(dev); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 482 | ret = -ETIMEDOUT; |
| 483 | goto error; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 484 | } |
| 485 | if (dev->transfer_status & AT91_TWI_NACK) { |
| 486 | dev_dbg(dev->dev, "received nack\n"); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 487 | ret = -EREMOTEIO; |
| 488 | goto error; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 489 | } |
| 490 | if (dev->transfer_status & AT91_TWI_OVRE) { |
| 491 | dev_err(dev->dev, "overrun while reading\n"); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 492 | ret = -EIO; |
| 493 | goto error; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 494 | } |
| 495 | if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) { |
| 496 | dev_err(dev->dev, "underrun while writing\n"); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 497 | ret = -EIO; |
| 498 | goto error; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 499 | } |
Marek Roszko | 75b81f3 | 2014-08-20 21:39:41 -0400 | [diff] [blame] | 500 | if (dev->recv_len_abort) { |
| 501 | dev_err(dev->dev, "invalid smbus block length recvd\n"); |
| 502 | ret = -EPROTO; |
| 503 | goto error; |
| 504 | } |
| 505 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 506 | dev_dbg(dev->dev, "transfer complete\n"); |
| 507 | |
| 508 | return 0; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 509 | |
| 510 | error: |
| 511 | at91_twi_dma_cleanup(dev); |
| 512 | return ret; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num) |
| 516 | { |
| 517 | struct at91_twi_dev *dev = i2c_get_adapdata(adap); |
| 518 | int ret; |
| 519 | unsigned int_addr_flag = 0; |
| 520 | struct i2c_msg *m_start = msg; |
| 521 | |
| 522 | dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num); |
| 523 | |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 524 | ret = pm_runtime_get_sync(dev->dev); |
| 525 | if (ret < 0) |
| 526 | goto out; |
| 527 | |
Wolfram Sang | a740584 | 2015-01-07 12:24:10 +0100 | [diff] [blame] | 528 | if (num == 2) { |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 529 | int internal_address = 0; |
| 530 | int i; |
| 531 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 532 | /* 1st msg is put into the internal address, start with 2nd */ |
| 533 | m_start = &msg[1]; |
| 534 | for (i = 0; i < msg->len; ++i) { |
| 535 | const unsigned addr = msg->buf[msg->len - 1 - i]; |
| 536 | |
| 537 | internal_address |= addr << (8 * i); |
| 538 | int_addr_flag += AT91_TWI_IADRSZ_1; |
| 539 | } |
| 540 | at91_twi_write(dev, AT91_TWI_IADR, internal_address); |
| 541 | } |
| 542 | |
| 543 | at91_twi_write(dev, AT91_TWI_MMR, (m_start->addr << 16) | int_addr_flag |
| 544 | | ((m_start->flags & I2C_M_RD) ? AT91_TWI_MREAD : 0)); |
| 545 | |
| 546 | dev->buf_len = m_start->len; |
| 547 | dev->buf = m_start->buf; |
| 548 | dev->msg = m_start; |
Marek Roszko | 75b81f3 | 2014-08-20 21:39:41 -0400 | [diff] [blame] | 549 | dev->recv_len_abort = false; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 550 | |
| 551 | ret = at91_do_twi_transfer(dev); |
| 552 | |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 553 | ret = (ret < 0) ? ret : num; |
| 554 | out: |
| 555 | pm_runtime_mark_last_busy(dev->dev); |
| 556 | pm_runtime_put_autosuspend(dev->dev); |
| 557 | |
| 558 | return ret; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Wolfram Sang | a740584 | 2015-01-07 12:24:10 +0100 | [diff] [blame] | 561 | /* |
| 562 | * The hardware can handle at most two messages concatenated by a |
| 563 | * repeated start via it's internal address feature. |
| 564 | */ |
| 565 | static struct i2c_adapter_quirks at91_twi_quirks = { |
| 566 | .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR, |
| 567 | .max_comb_1st_msg_len = 3, |
| 568 | }; |
| 569 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 570 | static u32 at91_twi_func(struct i2c_adapter *adapter) |
| 571 | { |
| 572 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
| 573 | | I2C_FUNC_SMBUS_READ_BLOCK_DATA; |
| 574 | } |
| 575 | |
| 576 | static struct i2c_algorithm at91_twi_algorithm = { |
| 577 | .master_xfer = at91_twi_xfer, |
| 578 | .functionality = at91_twi_func, |
| 579 | }; |
| 580 | |
| 581 | static struct at91_twi_pdata at91rm9200_config = { |
| 582 | .clk_max_div = 5, |
| 583 | .clk_offset = 3, |
| 584 | .has_unre_flag = true, |
| 585 | }; |
| 586 | |
| 587 | static struct at91_twi_pdata at91sam9261_config = { |
| 588 | .clk_max_div = 5, |
| 589 | .clk_offset = 4, |
| 590 | .has_unre_flag = false, |
| 591 | }; |
| 592 | |
| 593 | static struct at91_twi_pdata at91sam9260_config = { |
| 594 | .clk_max_div = 7, |
| 595 | .clk_offset = 4, |
| 596 | .has_unre_flag = false, |
| 597 | }; |
| 598 | |
| 599 | static struct at91_twi_pdata at91sam9g20_config = { |
| 600 | .clk_max_div = 7, |
| 601 | .clk_offset = 4, |
| 602 | .has_unre_flag = false, |
| 603 | }; |
| 604 | |
| 605 | static struct at91_twi_pdata at91sam9g10_config = { |
| 606 | .clk_max_div = 7, |
| 607 | .clk_offset = 4, |
| 608 | .has_unre_flag = false, |
| 609 | }; |
| 610 | |
| 611 | static const struct platform_device_id at91_twi_devtypes[] = { |
| 612 | { |
| 613 | .name = "i2c-at91rm9200", |
| 614 | .driver_data = (unsigned long) &at91rm9200_config, |
| 615 | }, { |
| 616 | .name = "i2c-at91sam9261", |
| 617 | .driver_data = (unsigned long) &at91sam9261_config, |
| 618 | }, { |
| 619 | .name = "i2c-at91sam9260", |
| 620 | .driver_data = (unsigned long) &at91sam9260_config, |
| 621 | }, { |
| 622 | .name = "i2c-at91sam9g20", |
| 623 | .driver_data = (unsigned long) &at91sam9g20_config, |
| 624 | }, { |
| 625 | .name = "i2c-at91sam9g10", |
| 626 | .driver_data = (unsigned long) &at91sam9g10_config, |
| 627 | }, { |
| 628 | /* sentinel */ |
| 629 | } |
| 630 | }; |
| 631 | |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 632 | #if defined(CONFIG_OF) |
Joachim Eastwood | 4182b43 | 2013-02-09 19:14:00 +0100 | [diff] [blame] | 633 | static struct at91_twi_pdata at91sam9x5_config = { |
| 634 | .clk_max_div = 7, |
| 635 | .clk_offset = 4, |
| 636 | .has_unre_flag = false, |
Joachim Eastwood | 4182b43 | 2013-02-09 19:14:00 +0100 | [diff] [blame] | 637 | }; |
| 638 | |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 639 | static const struct of_device_id atmel_twi_dt_ids[] = { |
| 640 | { |
Joachim Eastwood | 631056c | 2012-12-05 22:42:12 +0100 | [diff] [blame] | 641 | .compatible = "atmel,at91rm9200-i2c", |
| 642 | .data = &at91rm9200_config, |
| 643 | } , { |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 644 | .compatible = "atmel,at91sam9260-i2c", |
| 645 | .data = &at91sam9260_config, |
| 646 | } , { |
jean-jacques hiblot | d9a3afc | 2014-01-15 14:17:13 +0100 | [diff] [blame] | 647 | .compatible = "atmel,at91sam9261-i2c", |
| 648 | .data = &at91sam9261_config, |
| 649 | } , { |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 650 | .compatible = "atmel,at91sam9g20-i2c", |
| 651 | .data = &at91sam9g20_config, |
| 652 | } , { |
| 653 | .compatible = "atmel,at91sam9g10-i2c", |
| 654 | .data = &at91sam9g10_config, |
| 655 | }, { |
| 656 | .compatible = "atmel,at91sam9x5-i2c", |
| 657 | .data = &at91sam9x5_config, |
| 658 | }, { |
| 659 | /* sentinel */ |
| 660 | } |
| 661 | }; |
| 662 | MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids); |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 663 | #endif |
| 664 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 665 | static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr) |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 666 | { |
| 667 | int ret = 0; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 668 | struct dma_slave_config slave_config; |
| 669 | struct at91_twi_dma *dma = &dev->dma; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 670 | |
| 671 | memset(&slave_config, 0, sizeof(slave_config)); |
| 672 | slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR; |
| 673 | slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 674 | slave_config.src_maxburst = 1; |
| 675 | slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR; |
| 676 | slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 677 | slave_config.dst_maxburst = 1; |
| 678 | slave_config.device_fc = false; |
| 679 | |
Ludovic Desroches | 727f9c2 | 2014-11-21 14:44:32 +0100 | [diff] [blame] | 680 | dma->chan_tx = dma_request_slave_channel_reason(dev->dev, "tx"); |
| 681 | if (IS_ERR(dma->chan_tx)) { |
| 682 | ret = PTR_ERR(dma->chan_tx); |
| 683 | dma->chan_tx = NULL; |
Ludovic Desroches | d877a72 | 2013-04-15 02:16:56 +0000 | [diff] [blame] | 684 | goto error; |
| 685 | } |
| 686 | |
Ludovic Desroches | 727f9c2 | 2014-11-21 14:44:32 +0100 | [diff] [blame] | 687 | dma->chan_rx = dma_request_slave_channel_reason(dev->dev, "rx"); |
| 688 | if (IS_ERR(dma->chan_rx)) { |
| 689 | ret = PTR_ERR(dma->chan_rx); |
| 690 | dma->chan_rx = NULL; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 691 | goto error; |
| 692 | } |
| 693 | |
| 694 | slave_config.direction = DMA_MEM_TO_DEV; |
| 695 | if (dmaengine_slave_config(dma->chan_tx, &slave_config)) { |
| 696 | dev_err(dev->dev, "failed to configure tx channel\n"); |
| 697 | ret = -EINVAL; |
| 698 | goto error; |
| 699 | } |
| 700 | |
| 701 | slave_config.direction = DMA_DEV_TO_MEM; |
| 702 | if (dmaengine_slave_config(dma->chan_rx, &slave_config)) { |
| 703 | dev_err(dev->dev, "failed to configure rx channel\n"); |
| 704 | ret = -EINVAL; |
| 705 | goto error; |
| 706 | } |
| 707 | |
| 708 | sg_init_table(&dma->sg, 1); |
| 709 | dma->buf_mapped = false; |
| 710 | dma->xfer_in_progress = false; |
Ludovic Desroches | 727f9c2 | 2014-11-21 14:44:32 +0100 | [diff] [blame] | 711 | dev->use_dma = true; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 712 | |
| 713 | dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n", |
| 714 | dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx)); |
| 715 | |
| 716 | return ret; |
| 717 | |
| 718 | error: |
Ludovic Desroches | 727f9c2 | 2014-11-21 14:44:32 +0100 | [diff] [blame] | 719 | if (ret != -EPROBE_DEFER) |
| 720 | dev_info(dev->dev, "can't use DMA, error %d\n", ret); |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 721 | if (dma->chan_rx) |
| 722 | dma_release_channel(dma->chan_rx); |
| 723 | if (dma->chan_tx) |
| 724 | dma_release_channel(dma->chan_tx); |
| 725 | return ret; |
| 726 | } |
| 727 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 728 | static struct at91_twi_pdata *at91_twi_get_driver_data( |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 729 | struct platform_device *pdev) |
| 730 | { |
| 731 | if (pdev->dev.of_node) { |
| 732 | const struct of_device_id *match; |
| 733 | match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node); |
| 734 | if (!match) |
| 735 | return NULL; |
Ludovic Desroches | cd32e6c | 2012-11-23 17:03:16 +0100 | [diff] [blame] | 736 | return (struct at91_twi_pdata *)match->data; |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 737 | } |
| 738 | return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data; |
| 739 | } |
| 740 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 741 | static int at91_twi_probe(struct platform_device *pdev) |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 742 | { |
| 743 | struct at91_twi_dev *dev; |
| 744 | struct resource *mem; |
| 745 | int rc; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 746 | u32 phy_addr; |
Marek Roszko | 75b6c4b | 2014-03-11 00:25:38 -0400 | [diff] [blame] | 747 | u32 bus_clk_rate; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 748 | |
| 749 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
| 750 | if (!dev) |
| 751 | return -ENOMEM; |
| 752 | init_completion(&dev->cmd_complete); |
| 753 | dev->dev = &pdev->dev; |
| 754 | |
| 755 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 756 | if (!mem) |
| 757 | return -ENODEV; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 758 | phy_addr = mem->start; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 759 | |
| 760 | dev->pdata = at91_twi_get_driver_data(pdev); |
| 761 | if (!dev->pdata) |
| 762 | return -ENODEV; |
| 763 | |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 764 | dev->base = devm_ioremap_resource(&pdev->dev, mem); |
| 765 | if (IS_ERR(dev->base)) |
| 766 | return PTR_ERR(dev->base); |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 767 | |
| 768 | dev->irq = platform_get_irq(pdev, 0); |
| 769 | if (dev->irq < 0) |
| 770 | return dev->irq; |
| 771 | |
| 772 | rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0, |
| 773 | dev_name(dev->dev), dev); |
| 774 | if (rc) { |
| 775 | dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc); |
| 776 | return rc; |
| 777 | } |
| 778 | |
| 779 | platform_set_drvdata(pdev, dev); |
| 780 | |
| 781 | dev->clk = devm_clk_get(dev->dev, NULL); |
| 782 | if (IS_ERR(dev->clk)) { |
| 783 | dev_err(dev->dev, "no clock defined\n"); |
| 784 | return -ENODEV; |
| 785 | } |
| 786 | clk_prepare_enable(dev->clk); |
| 787 | |
Arnd Bergmann | dc6df6e | 2014-11-21 14:44:31 +0100 | [diff] [blame] | 788 | if (dev->dev->of_node) { |
Ludovic Desroches | 727f9c2 | 2014-11-21 14:44:32 +0100 | [diff] [blame] | 789 | rc = at91_twi_configure_dma(dev, phy_addr); |
| 790 | if (rc == -EPROBE_DEFER) |
| 791 | return rc; |
Ludovic Desroches | 60937b2 | 2012-11-23 10:09:04 +0100 | [diff] [blame] | 792 | } |
| 793 | |
Marek Roszko | 75b6c4b | 2014-03-11 00:25:38 -0400 | [diff] [blame] | 794 | rc = of_property_read_u32(dev->dev->of_node, "clock-frequency", |
| 795 | &bus_clk_rate); |
| 796 | if (rc) |
| 797 | bus_clk_rate = DEFAULT_TWI_CLK_HZ; |
| 798 | |
| 799 | at91_calc_twi_clock(dev, bus_clk_rate); |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 800 | at91_init_twi_bus(dev); |
| 801 | |
| 802 | snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91"); |
| 803 | i2c_set_adapdata(&dev->adapter, dev); |
| 804 | dev->adapter.owner = THIS_MODULE; |
Wolfram Sang | b850579 | 2014-07-10 13:46:22 +0200 | [diff] [blame] | 805 | dev->adapter.class = I2C_CLASS_DEPRECATED; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 806 | dev->adapter.algo = &at91_twi_algorithm; |
Wolfram Sang | a740584 | 2015-01-07 12:24:10 +0100 | [diff] [blame] | 807 | dev->adapter.quirks = &at91_twi_quirks; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 808 | dev->adapter.dev.parent = dev->dev; |
| 809 | dev->adapter.nr = pdev->id; |
| 810 | dev->adapter.timeout = AT91_I2C_TIMEOUT; |
Ludovic Desroches | 70d46a2 | 2012-09-12 08:42:14 +0200 | [diff] [blame] | 811 | dev->adapter.dev.of_node = pdev->dev.of_node; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 812 | |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 813 | pm_runtime_set_autosuspend_delay(dev->dev, AUTOSUSPEND_TIMEOUT); |
| 814 | pm_runtime_use_autosuspend(dev->dev); |
| 815 | pm_runtime_set_active(dev->dev); |
| 816 | pm_runtime_enable(dev->dev); |
| 817 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 818 | rc = i2c_add_numbered_adapter(&dev->adapter); |
| 819 | if (rc) { |
| 820 | dev_err(dev->dev, "Adapter %s registration failed\n", |
| 821 | dev->adapter.name); |
| 822 | clk_disable_unprepare(dev->clk); |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 823 | |
| 824 | pm_runtime_disable(dev->dev); |
| 825 | pm_runtime_set_suspended(dev->dev); |
| 826 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 827 | return rc; |
| 828 | } |
| 829 | |
| 830 | dev_info(dev->dev, "AT91 i2c bus driver.\n"); |
| 831 | return 0; |
| 832 | } |
| 833 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 834 | static int at91_twi_remove(struct platform_device *pdev) |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 835 | { |
| 836 | struct at91_twi_dev *dev = platform_get_drvdata(pdev); |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 837 | |
Lars-Peter Clausen | bf51a8c | 2013-03-09 08:16:46 +0000 | [diff] [blame] | 838 | i2c_del_adapter(&dev->adapter); |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 839 | clk_disable_unprepare(dev->clk); |
| 840 | |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 841 | pm_runtime_disable(dev->dev); |
| 842 | pm_runtime_set_suspended(dev->dev); |
| 843 | |
Lars-Peter Clausen | bf51a8c | 2013-03-09 08:16:46 +0000 | [diff] [blame] | 844 | return 0; |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | #ifdef CONFIG_PM |
| 848 | |
| 849 | static int at91_twi_runtime_suspend(struct device *dev) |
| 850 | { |
| 851 | struct at91_twi_dev *twi_dev = dev_get_drvdata(dev); |
| 852 | |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 853 | clk_disable_unprepare(twi_dev->clk); |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 854 | |
Wenyou Yang | 62d10c4 | 2014-11-10 09:55:52 +0800 | [diff] [blame] | 855 | pinctrl_pm_select_sleep_state(dev); |
| 856 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | static int at91_twi_runtime_resume(struct device *dev) |
| 861 | { |
| 862 | struct at91_twi_dev *twi_dev = dev_get_drvdata(dev); |
| 863 | |
Wenyou Yang | 62d10c4 | 2014-11-10 09:55:52 +0800 | [diff] [blame] | 864 | pinctrl_pm_select_default_state(dev); |
| 865 | |
Wenyou Yang | d64a818 | 2014-10-24 14:50:15 +0800 | [diff] [blame] | 866 | return clk_prepare_enable(twi_dev->clk); |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 867 | } |
| 868 | |
Wenyou Yang | 3676529 | 2014-10-24 14:50:16 +0800 | [diff] [blame] | 869 | static int at91_twi_suspend_noirq(struct device *dev) |
| 870 | { |
| 871 | if (!pm_runtime_status_suspended(dev)) |
| 872 | at91_twi_runtime_suspend(dev); |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | static int at91_twi_resume_noirq(struct device *dev) |
| 878 | { |
| 879 | int ret; |
| 880 | |
| 881 | if (!pm_runtime_status_suspended(dev)) { |
| 882 | ret = at91_twi_runtime_resume(dev); |
| 883 | if (ret) |
| 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | pm_runtime_mark_last_busy(dev); |
| 888 | pm_request_autosuspend(dev); |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 893 | static const struct dev_pm_ops at91_twi_pm = { |
Wenyou Yang | 3676529 | 2014-10-24 14:50:16 +0800 | [diff] [blame] | 894 | .suspend_noirq = at91_twi_suspend_noirq, |
| 895 | .resume_noirq = at91_twi_resume_noirq, |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 896 | .runtime_suspend = at91_twi_runtime_suspend, |
| 897 | .runtime_resume = at91_twi_runtime_resume, |
| 898 | }; |
| 899 | |
| 900 | #define at91_twi_pm_ops (&at91_twi_pm) |
| 901 | #else |
| 902 | #define at91_twi_pm_ops NULL |
| 903 | #endif |
| 904 | |
| 905 | static struct platform_driver at91_twi_driver = { |
| 906 | .probe = at91_twi_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 907 | .remove = at91_twi_remove, |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 908 | .id_table = at91_twi_devtypes, |
| 909 | .driver = { |
| 910 | .name = "at91_i2c", |
Sachin Kamat | 600abea | 2013-03-14 00:13:03 +0000 | [diff] [blame] | 911 | .of_match_table = of_match_ptr(atmel_twi_dt_ids), |
Nikolaus Voss | fac368a | 2011-11-08 11:49:46 +0100 | [diff] [blame] | 912 | .pm = at91_twi_pm_ops, |
| 913 | }, |
| 914 | }; |
| 915 | |
| 916 | static int __init at91_twi_init(void) |
| 917 | { |
| 918 | return platform_driver_register(&at91_twi_driver); |
| 919 | } |
| 920 | |
| 921 | static void __exit at91_twi_exit(void) |
| 922 | { |
| 923 | platform_driver_unregister(&at91_twi_driver); |
| 924 | } |
| 925 | |
| 926 | subsys_initcall(at91_twi_init); |
| 927 | module_exit(at91_twi_exit); |
| 928 | |
| 929 | MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>"); |
| 930 | MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91"); |
| 931 | MODULE_LICENSE("GPL"); |
| 932 | MODULE_ALIAS("platform:at91_i2c"); |