blob: abd93f3a05a03f2a083b68a7ab92c4c97f80c32b [file] [log] [blame]
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001/*
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 Desroches60937b22012-11-23 10:09:04 +010022#include <linux/dma-mapping.h>
23#include <linux/dmaengine.h>
Nikolaus Vossfac368a2011-11-08 11:49:46 +010024#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 Desroches70d46a22012-09-12 08:42:14 +020029#include <linux/of.h>
30#include <linux/of_device.h>
Nikolaus Vossfac368a2011-11-08 11:49:46 +010031#include <linux/platform_device.h>
32#include <linux/slab.h>
Ludovic Desroches60937b22012-11-23 10:09:04 +010033#include <linux/platform_data/dma-atmel.h>
Wenyou Yangd64a8182014-10-24 14:50:15 +080034#include <linux/pm_runtime.h>
Wenyou Yang62d10c42014-11-10 09:55:52 +080035#include <linux/pinctrl/consumer.h>
Nikolaus Vossfac368a2011-11-08 11:49:46 +010036
Marek Roszko75b6c4b2014-03-11 00:25:38 -040037#define DEFAULT_TWI_CLK_HZ 100000 /* max 400 Kbits/s */
Nikolaus Vossfac368a2011-11-08 11:49:46 +010038#define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */
Ludovic Desroches60937b22012-11-23 10:09:04 +010039#define AT91_I2C_DMA_THRESHOLD 8 /* enable DMA if transfer size is bigger than this threshold */
Wenyou Yangd64a8182014-10-24 14:50:15 +080040#define AUTOSUSPEND_TIMEOUT 2000
Nikolaus Vossfac368a2011-11-08 11:49:46 +010041
42/* AT91 TWI register definitions */
43#define AT91_TWI_CR 0x0000 /* Control Register */
Cyrille Pitchene84cf8f2015-06-09 18:22:15 +020044#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 */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +020052#define AT91_TWI_ACMEN BIT(16) /* Alternative Command Mode Enable */
53#define AT91_TWI_ACMDIS BIT(17) /* Alternative Command Mode Disable */
54#define AT91_TWI_THRCLR BIT(24) /* Transmit Holding Register Clear */
55#define AT91_TWI_RHRCLR BIT(25) /* Receive Holding Register Clear */
56#define AT91_TWI_LOCKCLR BIT(26) /* Lock Clear */
Nikolaus Vossfac368a2011-11-08 11:49:46 +010057
58#define AT91_TWI_MMR 0x0004 /* Master Mode Register */
59#define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */
Cyrille Pitchene84cf8f2015-06-09 18:22:15 +020060#define AT91_TWI_MREAD BIT(12) /* Master Read Direction */
Nikolaus Vossfac368a2011-11-08 11:49:46 +010061
62#define AT91_TWI_IADR 0x000c /* Internal Address Register */
63
64#define AT91_TWI_CWGR 0x0010 /* Clock Waveform Generator Reg */
65
66#define AT91_TWI_SR 0x0020 /* Status Register */
Cyrille Pitchene84cf8f2015-06-09 18:22:15 +020067#define AT91_TWI_TXCOMP BIT(0) /* Transmission Complete */
68#define AT91_TWI_RXRDY BIT(1) /* Receive Holding Register Ready */
69#define AT91_TWI_TXRDY BIT(2) /* Transmit Holding Register Ready */
70#define AT91_TWI_OVRE BIT(6) /* Overrun Error */
71#define AT91_TWI_UNRE BIT(7) /* Underrun Error */
72#define AT91_TWI_NACK BIT(8) /* Not Acknowledged */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +020073#define AT91_TWI_LOCK BIT(23) /* TWI Lock due to Frame Errors */
Nikolaus Vossfac368a2011-11-08 11:49:46 +010074
Cyrille Pitchen93563a62015-06-09 18:22:14 +020075#define AT91_TWI_INT_MASK \
76 (AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY | AT91_TWI_NACK)
77
Nikolaus Vossfac368a2011-11-08 11:49:46 +010078#define AT91_TWI_IER 0x0024 /* Interrupt Enable Register */
79#define AT91_TWI_IDR 0x0028 /* Interrupt Disable Register */
80#define AT91_TWI_IMR 0x002c /* Interrupt Mask Register */
81#define AT91_TWI_RHR 0x0030 /* Receive Holding Register */
82#define AT91_TWI_THR 0x0034 /* Transmit Holding Register */
83
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +020084#define AT91_TWI_ACR 0x0040 /* Alternative Command Register */
85#define AT91_TWI_ACR_DATAL(len) ((len) & 0xff)
86#define AT91_TWI_ACR_DIR BIT(8)
87
Cyrille Pitchen6ce461e2015-06-09 18:22:18 +020088#define AT91_TWI_VER 0x00fc /* Version Register */
89
Nikolaus Vossfac368a2011-11-08 11:49:46 +010090struct at91_twi_pdata {
Ludovic Desroches5f433812012-11-23 10:09:03 +010091 unsigned clk_max_div;
92 unsigned clk_offset;
93 bool has_unre_flag;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +020094 bool has_alt_cmd;
Ludovic Desroches60937b22012-11-23 10:09:04 +010095 struct at_dma_slave dma_slave;
96};
97
98struct at91_twi_dma {
99 struct dma_chan *chan_rx;
100 struct dma_chan *chan_tx;
101 struct scatterlist sg;
102 struct dma_async_tx_descriptor *data_desc;
103 enum dma_data_direction direction;
104 bool buf_mapped;
105 bool xfer_in_progress;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100106};
107
108struct at91_twi_dev {
Ludovic Desroches5f433812012-11-23 10:09:03 +0100109 struct device *dev;
110 void __iomem *base;
111 struct completion cmd_complete;
112 struct clk *clk;
113 u8 *buf;
114 size_t buf_len;
115 struct i2c_msg *msg;
116 int irq;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100117 unsigned imr;
Ludovic Desroches5f433812012-11-23 10:09:03 +0100118 unsigned transfer_status;
119 struct i2c_adapter adapter;
120 unsigned twi_cwgr_reg;
121 struct at91_twi_pdata *pdata;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100122 bool use_dma;
Marek Roszko75b81f32014-08-20 21:39:41 -0400123 bool recv_len_abort;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100124 struct at91_twi_dma dma;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100125};
126
127static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg)
128{
129 return readl_relaxed(dev->base + reg);
130}
131
132static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val)
133{
134 writel_relaxed(val, dev->base + reg);
135}
136
137static void at91_disable_twi_interrupts(struct at91_twi_dev *dev)
138{
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200139 at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_INT_MASK);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100140}
141
Ludovic Desroches60937b22012-11-23 10:09:04 +0100142static void at91_twi_irq_save(struct at91_twi_dev *dev)
143{
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200144 dev->imr = at91_twi_read(dev, AT91_TWI_IMR) & AT91_TWI_INT_MASK;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100145 at91_disable_twi_interrupts(dev);
146}
147
148static void at91_twi_irq_restore(struct at91_twi_dev *dev)
149{
150 at91_twi_write(dev, AT91_TWI_IER, dev->imr);
151}
152
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100153static void at91_init_twi_bus(struct at91_twi_dev *dev)
154{
155 at91_disable_twi_interrupts(dev);
156 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
157 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
158 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
159 at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
160}
161
162/*
163 * Calculate symmetric clock as stated in datasheet:
164 * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
165 */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500166static void at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100167{
168 int ckdiv, cdiv, div;
169 struct at91_twi_pdata *pdata = dev->pdata;
170 int offset = pdata->clk_offset;
171 int max_ckdiv = pdata->clk_max_div;
172
173 div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
174 2 * twi_clk) - offset);
175 ckdiv = fls(div >> 8);
176 cdiv = div >> ckdiv;
177
178 if (ckdiv > max_ckdiv) {
179 dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
180 ckdiv, max_ckdiv);
181 ckdiv = max_ckdiv;
182 cdiv = 255;
183 }
184
185 dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv;
186 dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv);
187}
188
Ludovic Desroches60937b22012-11-23 10:09:04 +0100189static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
190{
191 struct at91_twi_dma *dma = &dev->dma;
192
193 at91_twi_irq_save(dev);
194
195 if (dma->xfer_in_progress) {
196 if (dma->direction == DMA_FROM_DEVICE)
197 dmaengine_terminate_all(dma->chan_rx);
198 else
199 dmaengine_terminate_all(dma->chan_tx);
200 dma->xfer_in_progress = false;
201 }
202 if (dma->buf_mapped) {
203 dma_unmap_single(dev->dev, sg_dma_address(&dma->sg),
204 dev->buf_len, dma->direction);
205 dma->buf_mapped = false;
206 }
207
208 at91_twi_irq_restore(dev);
209}
210
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100211static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
212{
213 if (dev->buf_len <= 0)
214 return;
215
216 at91_twi_write(dev, AT91_TWI_THR, *dev->buf);
217
218 /* send stop when last byte has been written */
219 if (--dev->buf_len == 0)
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200220 if (!dev->pdata->has_alt_cmd)
221 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100222
223 dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len);
224
225 ++dev->buf;
226}
227
Ludovic Desroches60937b22012-11-23 10:09:04 +0100228static void at91_twi_write_data_dma_callback(void *data)
229{
230 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
231
232 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg),
Wolfram Sang28772ac2014-07-21 11:42:03 +0200233 dev->buf_len, DMA_TO_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100234
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200235 /*
236 * When this callback is called, THR/TX FIFO is likely not to be empty
237 * yet. So we have to wait for TXCOMP or NACK bits to be set into the
238 * Status Register to be sure that the STOP bit has been sent and the
239 * transfer is completed. The NACK interrupt has already been enabled,
240 * we just have to enable TXCOMP one.
241 */
242 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200243 if (!dev->pdata->has_alt_cmd)
244 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100245}
246
247static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
248{
249 dma_addr_t dma_addr;
250 struct dma_async_tx_descriptor *txdesc;
251 struct at91_twi_dma *dma = &dev->dma;
252 struct dma_chan *chan_tx = dma->chan_tx;
253
254 if (dev->buf_len <= 0)
255 return;
256
257 dma->direction = DMA_TO_DEVICE;
258
259 at91_twi_irq_save(dev);
260 dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
261 DMA_TO_DEVICE);
262 if (dma_mapping_error(dev->dev, dma_addr)) {
263 dev_err(dev->dev, "dma map failed\n");
264 return;
265 }
266 dma->buf_mapped = true;
267 at91_twi_irq_restore(dev);
268 sg_dma_len(&dma->sg) = dev->buf_len;
269 sg_dma_address(&dma->sg) = dma_addr;
270
271 txdesc = dmaengine_prep_slave_sg(chan_tx, &dma->sg, 1, DMA_MEM_TO_DEV,
272 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
273 if (!txdesc) {
274 dev_err(dev->dev, "dma prep slave sg failed\n");
275 goto error;
276 }
277
278 txdesc->callback = at91_twi_write_data_dma_callback;
279 txdesc->callback_param = dev;
280
281 dma->xfer_in_progress = true;
282 dmaengine_submit(txdesc);
283 dma_async_issue_pending(chan_tx);
284
285 return;
286
287error:
288 at91_twi_dma_cleanup(dev);
289}
290
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100291static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
292{
293 if (dev->buf_len <= 0)
294 return;
295
296 *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff;
297 --dev->buf_len;
298
Marek Roszko75b81f32014-08-20 21:39:41 -0400299 /* return if aborting, we only needed to read RHR to clear RXRDY*/
300 if (dev->recv_len_abort)
301 return;
302
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100303 /* handle I2C_SMBUS_BLOCK_DATA */
304 if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
Marek Roszko75b81f32014-08-20 21:39:41 -0400305 /* ensure length byte is a valid value */
306 if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
307 dev->msg->flags &= ~I2C_M_RECV_LEN;
308 dev->buf_len += *dev->buf;
309 dev->msg->len = dev->buf_len + 1;
310 dev_dbg(dev->dev, "received block length %d\n",
311 dev->buf_len);
312 } else {
313 /* abort and send the stop by reading one more byte */
314 dev->recv_len_abort = true;
315 dev->buf_len = 1;
316 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100317 }
318
319 /* send stop if second but last byte has been read */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200320 if (!dev->pdata->has_alt_cmd && dev->buf_len == 1)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100321 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
322
323 dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len);
324
325 ++dev->buf;
326}
327
Ludovic Desroches60937b22012-11-23 10:09:04 +0100328static void at91_twi_read_data_dma_callback(void *data)
329{
330 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200331 unsigned ier = AT91_TWI_TXCOMP;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100332
333 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg),
Wolfram Sang28772ac2014-07-21 11:42:03 +0200334 dev->buf_len, DMA_FROM_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100335
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200336 if (!dev->pdata->has_alt_cmd) {
337 /* The last two bytes have to be read without using dma */
338 dev->buf += dev->buf_len - 2;
339 dev->buf_len = 2;
340 ier |= AT91_TWI_RXRDY;
341 }
342 at91_twi_write(dev, AT91_TWI_IER, ier);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100343}
344
345static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
346{
347 dma_addr_t dma_addr;
348 struct dma_async_tx_descriptor *rxdesc;
349 struct at91_twi_dma *dma = &dev->dma;
350 struct dma_chan *chan_rx = dma->chan_rx;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200351 size_t buf_len;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100352
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200353 buf_len = (dev->pdata->has_alt_cmd) ? dev->buf_len : dev->buf_len - 2;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100354 dma->direction = DMA_FROM_DEVICE;
355
356 /* Keep in mind that we won't use dma to read the last two bytes */
357 at91_twi_irq_save(dev);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200358 dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100359 if (dma_mapping_error(dev->dev, dma_addr)) {
360 dev_err(dev->dev, "dma map failed\n");
361 return;
362 }
363 dma->buf_mapped = true;
364 at91_twi_irq_restore(dev);
365 dma->sg.dma_address = dma_addr;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200366 sg_dma_len(&dma->sg) = buf_len;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100367
368 rxdesc = dmaengine_prep_slave_sg(chan_rx, &dma->sg, 1, DMA_DEV_TO_MEM,
369 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
370 if (!rxdesc) {
371 dev_err(dev->dev, "dma prep slave sg failed\n");
372 goto error;
373 }
374
375 rxdesc->callback = at91_twi_read_data_dma_callback;
376 rxdesc->callback_param = dev;
377
378 dma->xfer_in_progress = true;
379 dmaengine_submit(rxdesc);
380 dma_async_issue_pending(dma->chan_rx);
381
382 return;
383
384error:
385 at91_twi_dma_cleanup(dev);
386}
387
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100388static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
389{
390 struct at91_twi_dev *dev = dev_id;
391 const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
392 const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
393
394 if (!irqstatus)
395 return IRQ_NONE;
396 else if (irqstatus & AT91_TWI_RXRDY)
397 at91_twi_read_next_byte(dev);
398 else if (irqstatus & AT91_TWI_TXRDY)
399 at91_twi_write_next_byte(dev);
400
401 /* catch error flags */
402 dev->transfer_status |= status;
403
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200404 if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100405 at91_disable_twi_interrupts(dev);
406 complete(&dev->cmd_complete);
407 }
408
409 return IRQ_HANDLED;
410}
411
412static int at91_do_twi_transfer(struct at91_twi_dev *dev)
413{
414 int ret;
Nicholas Mc Guire1c42aca2015-02-08 11:12:07 -0500415 unsigned long time_left;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100416 bool has_unre_flag = dev->pdata->has_unre_flag;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200417 bool has_alt_cmd = dev->pdata->has_alt_cmd;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100418
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200419 /*
420 * WARNING: the TXCOMP bit in the Status Register is NOT a clear on
421 * read flag but shows the state of the transmission at the time the
422 * Status Register is read. According to the programmer datasheet,
423 * TXCOMP is set when both holding register and internal shifter are
424 * empty and STOP condition has been sent.
425 * Consequently, we should enable NACK interrupt rather than TXCOMP to
426 * detect transmission failure.
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200427 * Indeed let's take the case of an i2c write command using DMA.
428 * Whenever the slave doesn't acknowledge a byte, the LOCK, NACK and
429 * TXCOMP bits are set together into the Status Register.
430 * LOCK is a clear on write bit, which is set to prevent the DMA
431 * controller from sending new data on the i2c bus after a NACK
432 * condition has happened. Once locked, this i2c peripheral stops
433 * triggering the DMA controller for new data but it is more than
434 * likely that a new DMA transaction is already in progress, writing
435 * into the Transmit Holding Register. Since the peripheral is locked,
436 * these new data won't be sent to the i2c bus but they will remain
437 * into the Transmit Holding Register, so TXCOMP bit is cleared.
438 * Then when the interrupt handler is called, the Status Register is
439 * read: the TXCOMP bit is clear but NACK bit is still set. The driver
440 * manage the error properly, without waiting for timeout.
441 * This case can be reproduced easyly when writing into an at24 eeprom.
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200442 *
443 * Besides, the TXCOMP bit is already set before the i2c transaction
444 * has been started. For read transactions, this bit is cleared when
445 * writing the START bit into the Control Register. So the
446 * corresponding interrupt can safely be enabled just after.
447 * However for write transactions managed by the CPU, we first write
448 * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP
449 * interrupt. If TXCOMP interrupt were enabled before writing into THR,
450 * the interrupt handler would be called immediately and the i2c command
451 * would be reported as completed.
452 * Also when a write transaction is managed by the DMA controller,
453 * enabling the TXCOMP interrupt in this function may lead to a race
454 * condition since we don't know whether the TXCOMP interrupt is enabled
455 * before or after the DMA has started to write into THR. So the TXCOMP
456 * interrupt is enabled later by at91_twi_write_data_dma_callback().
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200457 * Immediately after in that DMA callback, if the alternative command
458 * mode is not used, we still need to send the STOP condition manually
459 * writing the corresponding bit into the Control Register.
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200460 */
461
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100462 dev_dbg(dev->dev, "transfer: %s %d bytes.\n",
463 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
464
Wolfram Sang16735d02013-11-14 14:32:02 -0800465 reinit_completion(&dev->cmd_complete);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100466 dev->transfer_status = 0;
Ludovic Desroches7c3fe642012-11-13 16:43:21 +0100467
468 if (!dev->buf_len) {
469 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
470 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
471 } else if (dev->msg->flags & I2C_M_RD) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100472 unsigned start_flags = AT91_TWI_START;
473
474 if (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY) {
475 dev_err(dev->dev, "RXRDY still set!");
476 at91_twi_read(dev, AT91_TWI_RHR);
477 }
478
479 /* if only one byte is to be read, immediately stop transfer */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200480 if (!has_alt_cmd && dev->buf_len <= 1 &&
481 !(dev->msg->flags & I2C_M_RECV_LEN))
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100482 start_flags |= AT91_TWI_STOP;
483 at91_twi_write(dev, AT91_TWI_CR, start_flags);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100484 /*
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200485 * When using dma without alternative command mode, the last
486 * byte has to be read manually in order to not send the stop
487 * command too late and then to receive extra data.
488 * In practice, there are some issues if you use the dma to
489 * read n-1 bytes because of latency.
Ludovic Desroches60937b22012-11-23 10:09:04 +0100490 * Reading n-2 bytes with dma and the two last ones manually
491 * seems to be the best solution.
492 */
493 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200494 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100495 at91_twi_read_data_dma(dev);
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200496 } else {
Ludovic Desroches60937b22012-11-23 10:09:04 +0100497 at91_twi_write(dev, AT91_TWI_IER,
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200498 AT91_TWI_TXCOMP |
499 AT91_TWI_NACK |
500 AT91_TWI_RXRDY);
501 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100502 } else {
Ludovic Desroches60937b22012-11-23 10:09:04 +0100503 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200504 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100505 at91_twi_write_data_dma(dev);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100506 } else {
507 at91_twi_write_next_byte(dev);
508 at91_twi_write(dev, AT91_TWI_IER,
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200509 AT91_TWI_TXCOMP |
510 AT91_TWI_NACK |
511 AT91_TWI_TXRDY);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100512 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100513 }
514
Nicholas Mc Guire1c42aca2015-02-08 11:12:07 -0500515 time_left = wait_for_completion_timeout(&dev->cmd_complete,
516 dev->adapter.timeout);
517 if (time_left == 0) {
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200518 dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100519 dev_err(dev->dev, "controller timed out\n");
520 at91_init_twi_bus(dev);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100521 ret = -ETIMEDOUT;
522 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100523 }
524 if (dev->transfer_status & AT91_TWI_NACK) {
525 dev_dbg(dev->dev, "received nack\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100526 ret = -EREMOTEIO;
527 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100528 }
529 if (dev->transfer_status & AT91_TWI_OVRE) {
530 dev_err(dev->dev, "overrun while reading\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100531 ret = -EIO;
532 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100533 }
534 if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
535 dev_err(dev->dev, "underrun while writing\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100536 ret = -EIO;
537 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100538 }
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200539 if (has_alt_cmd && (dev->transfer_status & AT91_TWI_LOCK)) {
540 dev_err(dev->dev, "tx locked\n");
541 ret = -EIO;
542 goto error;
543 }
Marek Roszko75b81f32014-08-20 21:39:41 -0400544 if (dev->recv_len_abort) {
545 dev_err(dev->dev, "invalid smbus block length recvd\n");
546 ret = -EPROTO;
547 goto error;
548 }
549
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100550 dev_dbg(dev->dev, "transfer complete\n");
551
552 return 0;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100553
554error:
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200555 /* first stop DMA transfer if still in progress */
Ludovic Desroches60937b22012-11-23 10:09:04 +0100556 at91_twi_dma_cleanup(dev);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200557 /* then flush THR/FIFO and unlock TX if locked */
558 if (has_alt_cmd && (dev->transfer_status & AT91_TWI_LOCK)) {
559 dev_dbg(dev->dev, "unlock tx\n");
560 at91_twi_write(dev, AT91_TWI_CR,
561 AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
562 }
Ludovic Desroches60937b22012-11-23 10:09:04 +0100563 return ret;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100564}
565
566static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
567{
568 struct at91_twi_dev *dev = i2c_get_adapdata(adap);
569 int ret;
570 unsigned int_addr_flag = 0;
571 struct i2c_msg *m_start = msg;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200572 bool is_read, use_alt_cmd = false;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100573
574 dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
575
Wenyou Yangd64a8182014-10-24 14:50:15 +0800576 ret = pm_runtime_get_sync(dev->dev);
577 if (ret < 0)
578 goto out;
579
Wolfram Sanga7405842015-01-07 12:24:10 +0100580 if (num == 2) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100581 int internal_address = 0;
582 int i;
583
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100584 /* 1st msg is put into the internal address, start with 2nd */
585 m_start = &msg[1];
586 for (i = 0; i < msg->len; ++i) {
587 const unsigned addr = msg->buf[msg->len - 1 - i];
588
589 internal_address |= addr << (8 * i);
590 int_addr_flag += AT91_TWI_IADRSZ_1;
591 }
592 at91_twi_write(dev, AT91_TWI_IADR, internal_address);
593 }
594
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200595 is_read = (m_start->flags & I2C_M_RD);
596 if (dev->pdata->has_alt_cmd) {
597 if (m_start->len > 0) {
598 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN);
599 at91_twi_write(dev, AT91_TWI_ACR,
600 AT91_TWI_ACR_DATAL(m_start->len) |
601 ((is_read) ? AT91_TWI_ACR_DIR : 0));
602 use_alt_cmd = true;
603 } else {
604 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS);
605 }
606 }
607
608 at91_twi_write(dev, AT91_TWI_MMR,
609 (m_start->addr << 16) |
610 int_addr_flag |
611 ((!use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0));
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100612
613 dev->buf_len = m_start->len;
614 dev->buf = m_start->buf;
615 dev->msg = m_start;
Marek Roszko75b81f32014-08-20 21:39:41 -0400616 dev->recv_len_abort = false;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100617
618 ret = at91_do_twi_transfer(dev);
619
Wenyou Yangd64a8182014-10-24 14:50:15 +0800620 ret = (ret < 0) ? ret : num;
621out:
622 pm_runtime_mark_last_busy(dev->dev);
623 pm_runtime_put_autosuspend(dev->dev);
624
625 return ret;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100626}
627
Wolfram Sanga7405842015-01-07 12:24:10 +0100628/*
629 * The hardware can handle at most two messages concatenated by a
630 * repeated start via it's internal address feature.
631 */
632static struct i2c_adapter_quirks at91_twi_quirks = {
633 .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
634 .max_comb_1st_msg_len = 3,
635};
636
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100637static u32 at91_twi_func(struct i2c_adapter *adapter)
638{
639 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
640 | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
641}
642
643static struct i2c_algorithm at91_twi_algorithm = {
644 .master_xfer = at91_twi_xfer,
645 .functionality = at91_twi_func,
646};
647
648static struct at91_twi_pdata at91rm9200_config = {
649 .clk_max_div = 5,
650 .clk_offset = 3,
651 .has_unre_flag = true,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200652 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100653};
654
655static struct at91_twi_pdata at91sam9261_config = {
656 .clk_max_div = 5,
657 .clk_offset = 4,
658 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200659 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100660};
661
662static struct at91_twi_pdata at91sam9260_config = {
663 .clk_max_div = 7,
664 .clk_offset = 4,
665 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200666 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100667};
668
669static struct at91_twi_pdata at91sam9g20_config = {
670 .clk_max_div = 7,
671 .clk_offset = 4,
672 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200673 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100674};
675
676static struct at91_twi_pdata at91sam9g10_config = {
677 .clk_max_div = 7,
678 .clk_offset = 4,
679 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200680 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100681};
682
683static const struct platform_device_id at91_twi_devtypes[] = {
684 {
685 .name = "i2c-at91rm9200",
686 .driver_data = (unsigned long) &at91rm9200_config,
687 }, {
688 .name = "i2c-at91sam9261",
689 .driver_data = (unsigned long) &at91sam9261_config,
690 }, {
691 .name = "i2c-at91sam9260",
692 .driver_data = (unsigned long) &at91sam9260_config,
693 }, {
694 .name = "i2c-at91sam9g20",
695 .driver_data = (unsigned long) &at91sam9g20_config,
696 }, {
697 .name = "i2c-at91sam9g10",
698 .driver_data = (unsigned long) &at91sam9g10_config,
699 }, {
700 /* sentinel */
701 }
702};
703
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200704#if defined(CONFIG_OF)
Joachim Eastwood4182b432013-02-09 19:14:00 +0100705static struct at91_twi_pdata at91sam9x5_config = {
706 .clk_max_div = 7,
707 .clk_offset = 4,
708 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200709 .has_alt_cmd = false,
710};
711
712static struct at91_twi_pdata sama5d2_config = {
713 .clk_max_div = 7,
714 .clk_offset = 4,
715 .has_unre_flag = true,
716 .has_alt_cmd = true,
Joachim Eastwood4182b432013-02-09 19:14:00 +0100717};
718
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200719static const struct of_device_id atmel_twi_dt_ids[] = {
720 {
Joachim Eastwood631056c2012-12-05 22:42:12 +0100721 .compatible = "atmel,at91rm9200-i2c",
722 .data = &at91rm9200_config,
723 } , {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200724 .compatible = "atmel,at91sam9260-i2c",
725 .data = &at91sam9260_config,
726 } , {
jean-jacques hiblotd9a3afc2014-01-15 14:17:13 +0100727 .compatible = "atmel,at91sam9261-i2c",
728 .data = &at91sam9261_config,
729 } , {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200730 .compatible = "atmel,at91sam9g20-i2c",
731 .data = &at91sam9g20_config,
732 } , {
733 .compatible = "atmel,at91sam9g10-i2c",
734 .data = &at91sam9g10_config,
735 }, {
736 .compatible = "atmel,at91sam9x5-i2c",
737 .data = &at91sam9x5_config,
738 }, {
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200739 .compatible = "atmel,sama5d2-i2c",
740 .data = &sama5d2_config,
741 }, {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200742 /* sentinel */
743 }
744};
745MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids);
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200746#endif
747
Bill Pemberton0b255e92012-11-27 15:59:38 -0500748static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
Ludovic Desroches60937b22012-11-23 10:09:04 +0100749{
750 int ret = 0;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100751 struct dma_slave_config slave_config;
752 struct at91_twi_dma *dma = &dev->dma;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100753
754 memset(&slave_config, 0, sizeof(slave_config));
755 slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
756 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
757 slave_config.src_maxburst = 1;
758 slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
759 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
760 slave_config.dst_maxburst = 1;
761 slave_config.device_fc = false;
762
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100763 dma->chan_tx = dma_request_slave_channel_reason(dev->dev, "tx");
764 if (IS_ERR(dma->chan_tx)) {
765 ret = PTR_ERR(dma->chan_tx);
766 dma->chan_tx = NULL;
Ludovic Desrochesd877a722013-04-15 02:16:56 +0000767 goto error;
768 }
769
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100770 dma->chan_rx = dma_request_slave_channel_reason(dev->dev, "rx");
771 if (IS_ERR(dma->chan_rx)) {
772 ret = PTR_ERR(dma->chan_rx);
773 dma->chan_rx = NULL;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100774 goto error;
775 }
776
777 slave_config.direction = DMA_MEM_TO_DEV;
778 if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
779 dev_err(dev->dev, "failed to configure tx channel\n");
780 ret = -EINVAL;
781 goto error;
782 }
783
784 slave_config.direction = DMA_DEV_TO_MEM;
785 if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
786 dev_err(dev->dev, "failed to configure rx channel\n");
787 ret = -EINVAL;
788 goto error;
789 }
790
791 sg_init_table(&dma->sg, 1);
792 dma->buf_mapped = false;
793 dma->xfer_in_progress = false;
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100794 dev->use_dma = true;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100795
796 dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
797 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
798
799 return ret;
800
801error:
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100802 if (ret != -EPROBE_DEFER)
803 dev_info(dev->dev, "can't use DMA, error %d\n", ret);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100804 if (dma->chan_rx)
805 dma_release_channel(dma->chan_rx);
806 if (dma->chan_tx)
807 dma_release_channel(dma->chan_tx);
808 return ret;
809}
810
Bill Pemberton0b255e92012-11-27 15:59:38 -0500811static struct at91_twi_pdata *at91_twi_get_driver_data(
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200812 struct platform_device *pdev)
813{
814 if (pdev->dev.of_node) {
815 const struct of_device_id *match;
816 match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node);
817 if (!match)
818 return NULL;
Ludovic Desrochescd32e6c2012-11-23 17:03:16 +0100819 return (struct at91_twi_pdata *)match->data;
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200820 }
821 return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data;
822}
823
Bill Pemberton0b255e92012-11-27 15:59:38 -0500824static int at91_twi_probe(struct platform_device *pdev)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100825{
826 struct at91_twi_dev *dev;
827 struct resource *mem;
828 int rc;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100829 u32 phy_addr;
Marek Roszko75b6c4b2014-03-11 00:25:38 -0400830 u32 bus_clk_rate;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100831
832 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
833 if (!dev)
834 return -ENOMEM;
835 init_completion(&dev->cmd_complete);
836 dev->dev = &pdev->dev;
837
838 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
839 if (!mem)
840 return -ENODEV;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100841 phy_addr = mem->start;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100842
843 dev->pdata = at91_twi_get_driver_data(pdev);
844 if (!dev->pdata)
845 return -ENODEV;
846
Thierry Reding84dbf802013-01-21 11:09:03 +0100847 dev->base = devm_ioremap_resource(&pdev->dev, mem);
848 if (IS_ERR(dev->base))
849 return PTR_ERR(dev->base);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100850
851 dev->irq = platform_get_irq(pdev, 0);
852 if (dev->irq < 0)
853 return dev->irq;
854
855 rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
856 dev_name(dev->dev), dev);
857 if (rc) {
858 dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
859 return rc;
860 }
861
862 platform_set_drvdata(pdev, dev);
863
864 dev->clk = devm_clk_get(dev->dev, NULL);
865 if (IS_ERR(dev->clk)) {
866 dev_err(dev->dev, "no clock defined\n");
867 return -ENODEV;
868 }
869 clk_prepare_enable(dev->clk);
870
Arnd Bergmanndc6df6e2014-11-21 14:44:31 +0100871 if (dev->dev->of_node) {
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100872 rc = at91_twi_configure_dma(dev, phy_addr);
873 if (rc == -EPROBE_DEFER)
874 return rc;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100875 }
876
Marek Roszko75b6c4b2014-03-11 00:25:38 -0400877 rc = of_property_read_u32(dev->dev->of_node, "clock-frequency",
878 &bus_clk_rate);
879 if (rc)
880 bus_clk_rate = DEFAULT_TWI_CLK_HZ;
881
882 at91_calc_twi_clock(dev, bus_clk_rate);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100883 at91_init_twi_bus(dev);
884
885 snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
886 i2c_set_adapdata(&dev->adapter, dev);
887 dev->adapter.owner = THIS_MODULE;
Wolfram Sangb8505792014-07-10 13:46:22 +0200888 dev->adapter.class = I2C_CLASS_DEPRECATED;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100889 dev->adapter.algo = &at91_twi_algorithm;
Wolfram Sanga7405842015-01-07 12:24:10 +0100890 dev->adapter.quirks = &at91_twi_quirks;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100891 dev->adapter.dev.parent = dev->dev;
892 dev->adapter.nr = pdev->id;
893 dev->adapter.timeout = AT91_I2C_TIMEOUT;
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200894 dev->adapter.dev.of_node = pdev->dev.of_node;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100895
Wenyou Yangd64a8182014-10-24 14:50:15 +0800896 pm_runtime_set_autosuspend_delay(dev->dev, AUTOSUSPEND_TIMEOUT);
897 pm_runtime_use_autosuspend(dev->dev);
898 pm_runtime_set_active(dev->dev);
899 pm_runtime_enable(dev->dev);
900
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100901 rc = i2c_add_numbered_adapter(&dev->adapter);
902 if (rc) {
903 dev_err(dev->dev, "Adapter %s registration failed\n",
904 dev->adapter.name);
905 clk_disable_unprepare(dev->clk);
Wenyou Yangd64a8182014-10-24 14:50:15 +0800906
907 pm_runtime_disable(dev->dev);
908 pm_runtime_set_suspended(dev->dev);
909
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100910 return rc;
911 }
912
Cyrille Pitchen6ce461e2015-06-09 18:22:18 +0200913 dev_info(dev->dev, "AT91 i2c bus driver (hw version: %#x).\n",
914 at91_twi_read(dev, AT91_TWI_VER));
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100915 return 0;
916}
917
Bill Pemberton0b255e92012-11-27 15:59:38 -0500918static int at91_twi_remove(struct platform_device *pdev)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100919{
920 struct at91_twi_dev *dev = platform_get_drvdata(pdev);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100921
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000922 i2c_del_adapter(&dev->adapter);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100923 clk_disable_unprepare(dev->clk);
924
Wenyou Yangd64a8182014-10-24 14:50:15 +0800925 pm_runtime_disable(dev->dev);
926 pm_runtime_set_suspended(dev->dev);
927
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000928 return 0;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100929}
930
931#ifdef CONFIG_PM
932
933static int at91_twi_runtime_suspend(struct device *dev)
934{
935 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
936
Wenyou Yangd64a8182014-10-24 14:50:15 +0800937 clk_disable_unprepare(twi_dev->clk);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100938
Wenyou Yang62d10c42014-11-10 09:55:52 +0800939 pinctrl_pm_select_sleep_state(dev);
940
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100941 return 0;
942}
943
944static int at91_twi_runtime_resume(struct device *dev)
945{
946 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
947
Wenyou Yang62d10c42014-11-10 09:55:52 +0800948 pinctrl_pm_select_default_state(dev);
949
Wenyou Yangd64a8182014-10-24 14:50:15 +0800950 return clk_prepare_enable(twi_dev->clk);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100951}
952
Wenyou Yang36765292014-10-24 14:50:16 +0800953static int at91_twi_suspend_noirq(struct device *dev)
954{
955 if (!pm_runtime_status_suspended(dev))
956 at91_twi_runtime_suspend(dev);
957
958 return 0;
959}
960
961static int at91_twi_resume_noirq(struct device *dev)
962{
963 int ret;
964
965 if (!pm_runtime_status_suspended(dev)) {
966 ret = at91_twi_runtime_resume(dev);
967 if (ret)
968 return ret;
969 }
970
971 pm_runtime_mark_last_busy(dev);
972 pm_request_autosuspend(dev);
973
974 return 0;
975}
976
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100977static const struct dev_pm_ops at91_twi_pm = {
Wenyou Yang36765292014-10-24 14:50:16 +0800978 .suspend_noirq = at91_twi_suspend_noirq,
979 .resume_noirq = at91_twi_resume_noirq,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100980 .runtime_suspend = at91_twi_runtime_suspend,
981 .runtime_resume = at91_twi_runtime_resume,
982};
983
984#define at91_twi_pm_ops (&at91_twi_pm)
985#else
986#define at91_twi_pm_ops NULL
987#endif
988
989static struct platform_driver at91_twi_driver = {
990 .probe = at91_twi_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500991 .remove = at91_twi_remove,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100992 .id_table = at91_twi_devtypes,
993 .driver = {
994 .name = "at91_i2c",
Sachin Kamat600abea2013-03-14 00:13:03 +0000995 .of_match_table = of_match_ptr(atmel_twi_dt_ids),
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100996 .pm = at91_twi_pm_ops,
997 },
998};
999
1000static int __init at91_twi_init(void)
1001{
1002 return platform_driver_register(&at91_twi_driver);
1003}
1004
1005static void __exit at91_twi_exit(void)
1006{
1007 platform_driver_unregister(&at91_twi_driver);
1008}
1009
1010subsys_initcall(at91_twi_init);
1011module_exit(at91_twi_exit);
1012
1013MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
1014MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
1015MODULE_LICENSE("GPL");
1016MODULE_ALIAS("platform:at91_i2c");