blob: 817ae69cd1fbf5071c31d8f60d8c19027305f085 [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
Nikolaus Vossfac368a2011-11-08 11:49:46 +010088struct at91_twi_pdata {
Ludovic Desroches5f433812012-11-23 10:09:03 +010089 unsigned clk_max_div;
90 unsigned clk_offset;
91 bool has_unre_flag;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +020092 bool has_alt_cmd;
Ludovic Desroches60937b22012-11-23 10:09:04 +010093 struct at_dma_slave dma_slave;
94};
95
96struct at91_twi_dma {
97 struct dma_chan *chan_rx;
98 struct dma_chan *chan_tx;
99 struct scatterlist sg;
100 struct dma_async_tx_descriptor *data_desc;
101 enum dma_data_direction direction;
102 bool buf_mapped;
103 bool xfer_in_progress;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100104};
105
106struct at91_twi_dev {
Ludovic Desroches5f433812012-11-23 10:09:03 +0100107 struct device *dev;
108 void __iomem *base;
109 struct completion cmd_complete;
110 struct clk *clk;
111 u8 *buf;
112 size_t buf_len;
113 struct i2c_msg *msg;
114 int irq;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100115 unsigned imr;
Ludovic Desroches5f433812012-11-23 10:09:03 +0100116 unsigned transfer_status;
117 struct i2c_adapter adapter;
118 unsigned twi_cwgr_reg;
119 struct at91_twi_pdata *pdata;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100120 bool use_dma;
Marek Roszko75b81f32014-08-20 21:39:41 -0400121 bool recv_len_abort;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100122 struct at91_twi_dma dma;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100123};
124
125static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg)
126{
127 return readl_relaxed(dev->base + reg);
128}
129
130static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val)
131{
132 writel_relaxed(val, dev->base + reg);
133}
134
135static void at91_disable_twi_interrupts(struct at91_twi_dev *dev)
136{
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200137 at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_INT_MASK);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100138}
139
Ludovic Desroches60937b22012-11-23 10:09:04 +0100140static void at91_twi_irq_save(struct at91_twi_dev *dev)
141{
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200142 dev->imr = at91_twi_read(dev, AT91_TWI_IMR) & AT91_TWI_INT_MASK;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100143 at91_disable_twi_interrupts(dev);
144}
145
146static void at91_twi_irq_restore(struct at91_twi_dev *dev)
147{
148 at91_twi_write(dev, AT91_TWI_IER, dev->imr);
149}
150
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100151static void at91_init_twi_bus(struct at91_twi_dev *dev)
152{
153 at91_disable_twi_interrupts(dev);
154 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
155 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
156 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
157 at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
158}
159
160/*
161 * Calculate symmetric clock as stated in datasheet:
162 * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
163 */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500164static void at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100165{
166 int ckdiv, cdiv, div;
167 struct at91_twi_pdata *pdata = dev->pdata;
168 int offset = pdata->clk_offset;
169 int max_ckdiv = pdata->clk_max_div;
170
171 div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
172 2 * twi_clk) - offset);
173 ckdiv = fls(div >> 8);
174 cdiv = div >> ckdiv;
175
176 if (ckdiv > max_ckdiv) {
177 dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
178 ckdiv, max_ckdiv);
179 ckdiv = max_ckdiv;
180 cdiv = 255;
181 }
182
183 dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv;
184 dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv);
185}
186
Ludovic Desroches60937b22012-11-23 10:09:04 +0100187static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
188{
189 struct at91_twi_dma *dma = &dev->dma;
190
191 at91_twi_irq_save(dev);
192
193 if (dma->xfer_in_progress) {
194 if (dma->direction == DMA_FROM_DEVICE)
195 dmaengine_terminate_all(dma->chan_rx);
196 else
197 dmaengine_terminate_all(dma->chan_tx);
198 dma->xfer_in_progress = false;
199 }
200 if (dma->buf_mapped) {
201 dma_unmap_single(dev->dev, sg_dma_address(&dma->sg),
202 dev->buf_len, dma->direction);
203 dma->buf_mapped = false;
204 }
205
206 at91_twi_irq_restore(dev);
207}
208
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100209static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
210{
211 if (dev->buf_len <= 0)
212 return;
213
214 at91_twi_write(dev, AT91_TWI_THR, *dev->buf);
215
216 /* send stop when last byte has been written */
217 if (--dev->buf_len == 0)
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200218 if (!dev->pdata->has_alt_cmd)
219 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100220
221 dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len);
222
223 ++dev->buf;
224}
225
Ludovic Desroches60937b22012-11-23 10:09:04 +0100226static void at91_twi_write_data_dma_callback(void *data)
227{
228 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
229
230 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg),
Wolfram Sang28772ac2014-07-21 11:42:03 +0200231 dev->buf_len, DMA_TO_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100232
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200233 /*
234 * When this callback is called, THR/TX FIFO is likely not to be empty
235 * yet. So we have to wait for TXCOMP or NACK bits to be set into the
236 * Status Register to be sure that the STOP bit has been sent and the
237 * transfer is completed. The NACK interrupt has already been enabled,
238 * we just have to enable TXCOMP one.
239 */
240 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200241 if (!dev->pdata->has_alt_cmd)
242 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100243}
244
245static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
246{
247 dma_addr_t dma_addr;
248 struct dma_async_tx_descriptor *txdesc;
249 struct at91_twi_dma *dma = &dev->dma;
250 struct dma_chan *chan_tx = dma->chan_tx;
251
252 if (dev->buf_len <= 0)
253 return;
254
255 dma->direction = DMA_TO_DEVICE;
256
257 at91_twi_irq_save(dev);
258 dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
259 DMA_TO_DEVICE);
260 if (dma_mapping_error(dev->dev, dma_addr)) {
261 dev_err(dev->dev, "dma map failed\n");
262 return;
263 }
264 dma->buf_mapped = true;
265 at91_twi_irq_restore(dev);
266 sg_dma_len(&dma->sg) = dev->buf_len;
267 sg_dma_address(&dma->sg) = dma_addr;
268
269 txdesc = dmaengine_prep_slave_sg(chan_tx, &dma->sg, 1, DMA_MEM_TO_DEV,
270 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
271 if (!txdesc) {
272 dev_err(dev->dev, "dma prep slave sg failed\n");
273 goto error;
274 }
275
276 txdesc->callback = at91_twi_write_data_dma_callback;
277 txdesc->callback_param = dev;
278
279 dma->xfer_in_progress = true;
280 dmaengine_submit(txdesc);
281 dma_async_issue_pending(chan_tx);
282
283 return;
284
285error:
286 at91_twi_dma_cleanup(dev);
287}
288
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100289static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
290{
291 if (dev->buf_len <= 0)
292 return;
293
294 *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff;
295 --dev->buf_len;
296
Marek Roszko75b81f32014-08-20 21:39:41 -0400297 /* return if aborting, we only needed to read RHR to clear RXRDY*/
298 if (dev->recv_len_abort)
299 return;
300
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100301 /* handle I2C_SMBUS_BLOCK_DATA */
302 if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
Marek Roszko75b81f32014-08-20 21:39:41 -0400303 /* ensure length byte is a valid value */
304 if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
305 dev->msg->flags &= ~I2C_M_RECV_LEN;
306 dev->buf_len += *dev->buf;
307 dev->msg->len = dev->buf_len + 1;
308 dev_dbg(dev->dev, "received block length %d\n",
309 dev->buf_len);
310 } else {
311 /* abort and send the stop by reading one more byte */
312 dev->recv_len_abort = true;
313 dev->buf_len = 1;
314 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100315 }
316
317 /* send stop if second but last byte has been read */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200318 if (!dev->pdata->has_alt_cmd && dev->buf_len == 1)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100319 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
320
321 dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len);
322
323 ++dev->buf;
324}
325
Ludovic Desroches60937b22012-11-23 10:09:04 +0100326static void at91_twi_read_data_dma_callback(void *data)
327{
328 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200329 unsigned ier = AT91_TWI_TXCOMP;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100330
331 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg),
Wolfram Sang28772ac2014-07-21 11:42:03 +0200332 dev->buf_len, DMA_FROM_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100333
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200334 if (!dev->pdata->has_alt_cmd) {
335 /* The last two bytes have to be read without using dma */
336 dev->buf += dev->buf_len - 2;
337 dev->buf_len = 2;
338 ier |= AT91_TWI_RXRDY;
339 }
340 at91_twi_write(dev, AT91_TWI_IER, ier);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100341}
342
343static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
344{
345 dma_addr_t dma_addr;
346 struct dma_async_tx_descriptor *rxdesc;
347 struct at91_twi_dma *dma = &dev->dma;
348 struct dma_chan *chan_rx = dma->chan_rx;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200349 size_t buf_len;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100350
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200351 buf_len = (dev->pdata->has_alt_cmd) ? dev->buf_len : dev->buf_len - 2;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100352 dma->direction = DMA_FROM_DEVICE;
353
354 /* Keep in mind that we won't use dma to read the last two bytes */
355 at91_twi_irq_save(dev);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200356 dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100357 if (dma_mapping_error(dev->dev, dma_addr)) {
358 dev_err(dev->dev, "dma map failed\n");
359 return;
360 }
361 dma->buf_mapped = true;
362 at91_twi_irq_restore(dev);
363 dma->sg.dma_address = dma_addr;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200364 sg_dma_len(&dma->sg) = buf_len;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100365
366 rxdesc = dmaengine_prep_slave_sg(chan_rx, &dma->sg, 1, DMA_DEV_TO_MEM,
367 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
368 if (!rxdesc) {
369 dev_err(dev->dev, "dma prep slave sg failed\n");
370 goto error;
371 }
372
373 rxdesc->callback = at91_twi_read_data_dma_callback;
374 rxdesc->callback_param = dev;
375
376 dma->xfer_in_progress = true;
377 dmaengine_submit(rxdesc);
378 dma_async_issue_pending(dma->chan_rx);
379
380 return;
381
382error:
383 at91_twi_dma_cleanup(dev);
384}
385
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100386static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
387{
388 struct at91_twi_dev *dev = dev_id;
389 const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
390 const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
391
392 if (!irqstatus)
393 return IRQ_NONE;
394 else if (irqstatus & AT91_TWI_RXRDY)
395 at91_twi_read_next_byte(dev);
396 else if (irqstatus & AT91_TWI_TXRDY)
397 at91_twi_write_next_byte(dev);
398
399 /* catch error flags */
400 dev->transfer_status |= status;
401
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200402 if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100403 at91_disable_twi_interrupts(dev);
404 complete(&dev->cmd_complete);
405 }
406
407 return IRQ_HANDLED;
408}
409
410static int at91_do_twi_transfer(struct at91_twi_dev *dev)
411{
412 int ret;
Nicholas Mc Guire1c42aca2015-02-08 11:12:07 -0500413 unsigned long time_left;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100414 bool has_unre_flag = dev->pdata->has_unre_flag;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200415 bool has_alt_cmd = dev->pdata->has_alt_cmd;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100416
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200417 /*
418 * WARNING: the TXCOMP bit in the Status Register is NOT a clear on
419 * read flag but shows the state of the transmission at the time the
420 * Status Register is read. According to the programmer datasheet,
421 * TXCOMP is set when both holding register and internal shifter are
422 * empty and STOP condition has been sent.
423 * Consequently, we should enable NACK interrupt rather than TXCOMP to
424 * detect transmission failure.
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200425 * Indeed let's take the case of an i2c write command using DMA.
426 * Whenever the slave doesn't acknowledge a byte, the LOCK, NACK and
427 * TXCOMP bits are set together into the Status Register.
428 * LOCK is a clear on write bit, which is set to prevent the DMA
429 * controller from sending new data on the i2c bus after a NACK
430 * condition has happened. Once locked, this i2c peripheral stops
431 * triggering the DMA controller for new data but it is more than
432 * likely that a new DMA transaction is already in progress, writing
433 * into the Transmit Holding Register. Since the peripheral is locked,
434 * these new data won't be sent to the i2c bus but they will remain
435 * into the Transmit Holding Register, so TXCOMP bit is cleared.
436 * Then when the interrupt handler is called, the Status Register is
437 * read: the TXCOMP bit is clear but NACK bit is still set. The driver
438 * manage the error properly, without waiting for timeout.
439 * This case can be reproduced easyly when writing into an at24 eeprom.
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200440 *
441 * Besides, the TXCOMP bit is already set before the i2c transaction
442 * has been started. For read transactions, this bit is cleared when
443 * writing the START bit into the Control Register. So the
444 * corresponding interrupt can safely be enabled just after.
445 * However for write transactions managed by the CPU, we first write
446 * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP
447 * interrupt. If TXCOMP interrupt were enabled before writing into THR,
448 * the interrupt handler would be called immediately and the i2c command
449 * would be reported as completed.
450 * Also when a write transaction is managed by the DMA controller,
451 * enabling the TXCOMP interrupt in this function may lead to a race
452 * condition since we don't know whether the TXCOMP interrupt is enabled
453 * before or after the DMA has started to write into THR. So the TXCOMP
454 * interrupt is enabled later by at91_twi_write_data_dma_callback().
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200455 * Immediately after in that DMA callback, if the alternative command
456 * mode is not used, we still need to send the STOP condition manually
457 * writing the corresponding bit into the Control Register.
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200458 */
459
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100460 dev_dbg(dev->dev, "transfer: %s %d bytes.\n",
461 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
462
Wolfram Sang16735d02013-11-14 14:32:02 -0800463 reinit_completion(&dev->cmd_complete);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100464 dev->transfer_status = 0;
Ludovic Desroches7c3fe642012-11-13 16:43:21 +0100465
466 if (!dev->buf_len) {
467 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
468 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
469 } else if (dev->msg->flags & I2C_M_RD) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100470 unsigned start_flags = AT91_TWI_START;
471
472 if (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY) {
473 dev_err(dev->dev, "RXRDY still set!");
474 at91_twi_read(dev, AT91_TWI_RHR);
475 }
476
477 /* if only one byte is to be read, immediately stop transfer */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200478 if (!has_alt_cmd && dev->buf_len <= 1 &&
479 !(dev->msg->flags & I2C_M_RECV_LEN))
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100480 start_flags |= AT91_TWI_STOP;
481 at91_twi_write(dev, AT91_TWI_CR, start_flags);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100482 /*
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200483 * When using dma without alternative command mode, the last
484 * byte has to be read manually in order to not send the stop
485 * command too late and then to receive extra data.
486 * In practice, there are some issues if you use the dma to
487 * read n-1 bytes because of latency.
Ludovic Desroches60937b22012-11-23 10:09:04 +0100488 * Reading n-2 bytes with dma and the two last ones manually
489 * seems to be the best solution.
490 */
491 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200492 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100493 at91_twi_read_data_dma(dev);
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200494 } else {
Ludovic Desroches60937b22012-11-23 10:09:04 +0100495 at91_twi_write(dev, AT91_TWI_IER,
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200496 AT91_TWI_TXCOMP |
497 AT91_TWI_NACK |
498 AT91_TWI_RXRDY);
499 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100500 } else {
Ludovic Desroches60937b22012-11-23 10:09:04 +0100501 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200502 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100503 at91_twi_write_data_dma(dev);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100504 } else {
505 at91_twi_write_next_byte(dev);
506 at91_twi_write(dev, AT91_TWI_IER,
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200507 AT91_TWI_TXCOMP |
508 AT91_TWI_NACK |
509 AT91_TWI_TXRDY);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100510 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100511 }
512
Nicholas Mc Guire1c42aca2015-02-08 11:12:07 -0500513 time_left = wait_for_completion_timeout(&dev->cmd_complete,
514 dev->adapter.timeout);
515 if (time_left == 0) {
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200516 dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100517 dev_err(dev->dev, "controller timed out\n");
518 at91_init_twi_bus(dev);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100519 ret = -ETIMEDOUT;
520 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100521 }
522 if (dev->transfer_status & AT91_TWI_NACK) {
523 dev_dbg(dev->dev, "received nack\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100524 ret = -EREMOTEIO;
525 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100526 }
527 if (dev->transfer_status & AT91_TWI_OVRE) {
528 dev_err(dev->dev, "overrun while reading\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100529 ret = -EIO;
530 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100531 }
532 if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
533 dev_err(dev->dev, "underrun while writing\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100534 ret = -EIO;
535 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100536 }
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200537 if (has_alt_cmd && (dev->transfer_status & AT91_TWI_LOCK)) {
538 dev_err(dev->dev, "tx locked\n");
539 ret = -EIO;
540 goto error;
541 }
Marek Roszko75b81f32014-08-20 21:39:41 -0400542 if (dev->recv_len_abort) {
543 dev_err(dev->dev, "invalid smbus block length recvd\n");
544 ret = -EPROTO;
545 goto error;
546 }
547
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100548 dev_dbg(dev->dev, "transfer complete\n");
549
550 return 0;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100551
552error:
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200553 /* first stop DMA transfer if still in progress */
Ludovic Desroches60937b22012-11-23 10:09:04 +0100554 at91_twi_dma_cleanup(dev);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200555 /* then flush THR/FIFO and unlock TX if locked */
556 if (has_alt_cmd && (dev->transfer_status & AT91_TWI_LOCK)) {
557 dev_dbg(dev->dev, "unlock tx\n");
558 at91_twi_write(dev, AT91_TWI_CR,
559 AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
560 }
Ludovic Desroches60937b22012-11-23 10:09:04 +0100561 return ret;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100562}
563
564static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
565{
566 struct at91_twi_dev *dev = i2c_get_adapdata(adap);
567 int ret;
568 unsigned int_addr_flag = 0;
569 struct i2c_msg *m_start = msg;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200570 bool is_read, use_alt_cmd = false;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100571
572 dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
573
Wenyou Yangd64a8182014-10-24 14:50:15 +0800574 ret = pm_runtime_get_sync(dev->dev);
575 if (ret < 0)
576 goto out;
577
Wolfram Sanga7405842015-01-07 12:24:10 +0100578 if (num == 2) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100579 int internal_address = 0;
580 int i;
581
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100582 /* 1st msg is put into the internal address, start with 2nd */
583 m_start = &msg[1];
584 for (i = 0; i < msg->len; ++i) {
585 const unsigned addr = msg->buf[msg->len - 1 - i];
586
587 internal_address |= addr << (8 * i);
588 int_addr_flag += AT91_TWI_IADRSZ_1;
589 }
590 at91_twi_write(dev, AT91_TWI_IADR, internal_address);
591 }
592
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200593 is_read = (m_start->flags & I2C_M_RD);
594 if (dev->pdata->has_alt_cmd) {
595 if (m_start->len > 0) {
596 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN);
597 at91_twi_write(dev, AT91_TWI_ACR,
598 AT91_TWI_ACR_DATAL(m_start->len) |
599 ((is_read) ? AT91_TWI_ACR_DIR : 0));
600 use_alt_cmd = true;
601 } else {
602 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS);
603 }
604 }
605
606 at91_twi_write(dev, AT91_TWI_MMR,
607 (m_start->addr << 16) |
608 int_addr_flag |
609 ((!use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0));
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100610
611 dev->buf_len = m_start->len;
612 dev->buf = m_start->buf;
613 dev->msg = m_start;
Marek Roszko75b81f32014-08-20 21:39:41 -0400614 dev->recv_len_abort = false;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100615
616 ret = at91_do_twi_transfer(dev);
617
Wenyou Yangd64a8182014-10-24 14:50:15 +0800618 ret = (ret < 0) ? ret : num;
619out:
620 pm_runtime_mark_last_busy(dev->dev);
621 pm_runtime_put_autosuspend(dev->dev);
622
623 return ret;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100624}
625
Wolfram Sanga7405842015-01-07 12:24:10 +0100626/*
627 * The hardware can handle at most two messages concatenated by a
628 * repeated start via it's internal address feature.
629 */
630static struct i2c_adapter_quirks at91_twi_quirks = {
631 .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
632 .max_comb_1st_msg_len = 3,
633};
634
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100635static u32 at91_twi_func(struct i2c_adapter *adapter)
636{
637 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
638 | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
639}
640
641static struct i2c_algorithm at91_twi_algorithm = {
642 .master_xfer = at91_twi_xfer,
643 .functionality = at91_twi_func,
644};
645
646static struct at91_twi_pdata at91rm9200_config = {
647 .clk_max_div = 5,
648 .clk_offset = 3,
649 .has_unre_flag = true,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200650 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100651};
652
653static struct at91_twi_pdata at91sam9261_config = {
654 .clk_max_div = 5,
655 .clk_offset = 4,
656 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200657 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100658};
659
660static struct at91_twi_pdata at91sam9260_config = {
661 .clk_max_div = 7,
662 .clk_offset = 4,
663 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200664 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100665};
666
667static struct at91_twi_pdata at91sam9g20_config = {
668 .clk_max_div = 7,
669 .clk_offset = 4,
670 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200671 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100672};
673
674static struct at91_twi_pdata at91sam9g10_config = {
675 .clk_max_div = 7,
676 .clk_offset = 4,
677 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200678 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100679};
680
681static const struct platform_device_id at91_twi_devtypes[] = {
682 {
683 .name = "i2c-at91rm9200",
684 .driver_data = (unsigned long) &at91rm9200_config,
685 }, {
686 .name = "i2c-at91sam9261",
687 .driver_data = (unsigned long) &at91sam9261_config,
688 }, {
689 .name = "i2c-at91sam9260",
690 .driver_data = (unsigned long) &at91sam9260_config,
691 }, {
692 .name = "i2c-at91sam9g20",
693 .driver_data = (unsigned long) &at91sam9g20_config,
694 }, {
695 .name = "i2c-at91sam9g10",
696 .driver_data = (unsigned long) &at91sam9g10_config,
697 }, {
698 /* sentinel */
699 }
700};
701
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200702#if defined(CONFIG_OF)
Joachim Eastwood4182b432013-02-09 19:14:00 +0100703static struct at91_twi_pdata at91sam9x5_config = {
704 .clk_max_div = 7,
705 .clk_offset = 4,
706 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200707 .has_alt_cmd = false,
708};
709
710static struct at91_twi_pdata sama5d2_config = {
711 .clk_max_div = 7,
712 .clk_offset = 4,
713 .has_unre_flag = true,
714 .has_alt_cmd = true,
Joachim Eastwood4182b432013-02-09 19:14:00 +0100715};
716
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200717static const struct of_device_id atmel_twi_dt_ids[] = {
718 {
Joachim Eastwood631056c2012-12-05 22:42:12 +0100719 .compatible = "atmel,at91rm9200-i2c",
720 .data = &at91rm9200_config,
721 } , {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200722 .compatible = "atmel,at91sam9260-i2c",
723 .data = &at91sam9260_config,
724 } , {
jean-jacques hiblotd9a3afc2014-01-15 14:17:13 +0100725 .compatible = "atmel,at91sam9261-i2c",
726 .data = &at91sam9261_config,
727 } , {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200728 .compatible = "atmel,at91sam9g20-i2c",
729 .data = &at91sam9g20_config,
730 } , {
731 .compatible = "atmel,at91sam9g10-i2c",
732 .data = &at91sam9g10_config,
733 }, {
734 .compatible = "atmel,at91sam9x5-i2c",
735 .data = &at91sam9x5_config,
736 }, {
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200737 .compatible = "atmel,sama5d2-i2c",
738 .data = &sama5d2_config,
739 }, {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200740 /* sentinel */
741 }
742};
743MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids);
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200744#endif
745
Bill Pemberton0b255e92012-11-27 15:59:38 -0500746static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
Ludovic Desroches60937b22012-11-23 10:09:04 +0100747{
748 int ret = 0;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100749 struct dma_slave_config slave_config;
750 struct at91_twi_dma *dma = &dev->dma;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100751
752 memset(&slave_config, 0, sizeof(slave_config));
753 slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
754 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
755 slave_config.src_maxburst = 1;
756 slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
757 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
758 slave_config.dst_maxburst = 1;
759 slave_config.device_fc = false;
760
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100761 dma->chan_tx = dma_request_slave_channel_reason(dev->dev, "tx");
762 if (IS_ERR(dma->chan_tx)) {
763 ret = PTR_ERR(dma->chan_tx);
764 dma->chan_tx = NULL;
Ludovic Desrochesd877a722013-04-15 02:16:56 +0000765 goto error;
766 }
767
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100768 dma->chan_rx = dma_request_slave_channel_reason(dev->dev, "rx");
769 if (IS_ERR(dma->chan_rx)) {
770 ret = PTR_ERR(dma->chan_rx);
771 dma->chan_rx = NULL;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100772 goto error;
773 }
774
775 slave_config.direction = DMA_MEM_TO_DEV;
776 if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
777 dev_err(dev->dev, "failed to configure tx channel\n");
778 ret = -EINVAL;
779 goto error;
780 }
781
782 slave_config.direction = DMA_DEV_TO_MEM;
783 if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
784 dev_err(dev->dev, "failed to configure rx channel\n");
785 ret = -EINVAL;
786 goto error;
787 }
788
789 sg_init_table(&dma->sg, 1);
790 dma->buf_mapped = false;
791 dma->xfer_in_progress = false;
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100792 dev->use_dma = true;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100793
794 dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
795 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
796
797 return ret;
798
799error:
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100800 if (ret != -EPROBE_DEFER)
801 dev_info(dev->dev, "can't use DMA, error %d\n", ret);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100802 if (dma->chan_rx)
803 dma_release_channel(dma->chan_rx);
804 if (dma->chan_tx)
805 dma_release_channel(dma->chan_tx);
806 return ret;
807}
808
Bill Pemberton0b255e92012-11-27 15:59:38 -0500809static struct at91_twi_pdata *at91_twi_get_driver_data(
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200810 struct platform_device *pdev)
811{
812 if (pdev->dev.of_node) {
813 const struct of_device_id *match;
814 match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node);
815 if (!match)
816 return NULL;
Ludovic Desrochescd32e6c2012-11-23 17:03:16 +0100817 return (struct at91_twi_pdata *)match->data;
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200818 }
819 return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data;
820}
821
Bill Pemberton0b255e92012-11-27 15:59:38 -0500822static int at91_twi_probe(struct platform_device *pdev)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100823{
824 struct at91_twi_dev *dev;
825 struct resource *mem;
826 int rc;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100827 u32 phy_addr;
Marek Roszko75b6c4b2014-03-11 00:25:38 -0400828 u32 bus_clk_rate;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100829
830 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
831 if (!dev)
832 return -ENOMEM;
833 init_completion(&dev->cmd_complete);
834 dev->dev = &pdev->dev;
835
836 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
837 if (!mem)
838 return -ENODEV;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100839 phy_addr = mem->start;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100840
841 dev->pdata = at91_twi_get_driver_data(pdev);
842 if (!dev->pdata)
843 return -ENODEV;
844
Thierry Reding84dbf802013-01-21 11:09:03 +0100845 dev->base = devm_ioremap_resource(&pdev->dev, mem);
846 if (IS_ERR(dev->base))
847 return PTR_ERR(dev->base);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100848
849 dev->irq = platform_get_irq(pdev, 0);
850 if (dev->irq < 0)
851 return dev->irq;
852
853 rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
854 dev_name(dev->dev), dev);
855 if (rc) {
856 dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
857 return rc;
858 }
859
860 platform_set_drvdata(pdev, dev);
861
862 dev->clk = devm_clk_get(dev->dev, NULL);
863 if (IS_ERR(dev->clk)) {
864 dev_err(dev->dev, "no clock defined\n");
865 return -ENODEV;
866 }
867 clk_prepare_enable(dev->clk);
868
Arnd Bergmanndc6df6e2014-11-21 14:44:31 +0100869 if (dev->dev->of_node) {
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100870 rc = at91_twi_configure_dma(dev, phy_addr);
871 if (rc == -EPROBE_DEFER)
872 return rc;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100873 }
874
Marek Roszko75b6c4b2014-03-11 00:25:38 -0400875 rc = of_property_read_u32(dev->dev->of_node, "clock-frequency",
876 &bus_clk_rate);
877 if (rc)
878 bus_clk_rate = DEFAULT_TWI_CLK_HZ;
879
880 at91_calc_twi_clock(dev, bus_clk_rate);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100881 at91_init_twi_bus(dev);
882
883 snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
884 i2c_set_adapdata(&dev->adapter, dev);
885 dev->adapter.owner = THIS_MODULE;
Wolfram Sangb8505792014-07-10 13:46:22 +0200886 dev->adapter.class = I2C_CLASS_DEPRECATED;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100887 dev->adapter.algo = &at91_twi_algorithm;
Wolfram Sanga7405842015-01-07 12:24:10 +0100888 dev->adapter.quirks = &at91_twi_quirks;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100889 dev->adapter.dev.parent = dev->dev;
890 dev->adapter.nr = pdev->id;
891 dev->adapter.timeout = AT91_I2C_TIMEOUT;
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200892 dev->adapter.dev.of_node = pdev->dev.of_node;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100893
Wenyou Yangd64a8182014-10-24 14:50:15 +0800894 pm_runtime_set_autosuspend_delay(dev->dev, AUTOSUSPEND_TIMEOUT);
895 pm_runtime_use_autosuspend(dev->dev);
896 pm_runtime_set_active(dev->dev);
897 pm_runtime_enable(dev->dev);
898
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100899 rc = i2c_add_numbered_adapter(&dev->adapter);
900 if (rc) {
901 dev_err(dev->dev, "Adapter %s registration failed\n",
902 dev->adapter.name);
903 clk_disable_unprepare(dev->clk);
Wenyou Yangd64a8182014-10-24 14:50:15 +0800904
905 pm_runtime_disable(dev->dev);
906 pm_runtime_set_suspended(dev->dev);
907
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100908 return rc;
909 }
910
911 dev_info(dev->dev, "AT91 i2c bus driver.\n");
912 return 0;
913}
914
Bill Pemberton0b255e92012-11-27 15:59:38 -0500915static int at91_twi_remove(struct platform_device *pdev)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100916{
917 struct at91_twi_dev *dev = platform_get_drvdata(pdev);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100918
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000919 i2c_del_adapter(&dev->adapter);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100920 clk_disable_unprepare(dev->clk);
921
Wenyou Yangd64a8182014-10-24 14:50:15 +0800922 pm_runtime_disable(dev->dev);
923 pm_runtime_set_suspended(dev->dev);
924
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000925 return 0;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100926}
927
928#ifdef CONFIG_PM
929
930static int at91_twi_runtime_suspend(struct device *dev)
931{
932 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
933
Wenyou Yangd64a8182014-10-24 14:50:15 +0800934 clk_disable_unprepare(twi_dev->clk);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100935
Wenyou Yang62d10c42014-11-10 09:55:52 +0800936 pinctrl_pm_select_sleep_state(dev);
937
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100938 return 0;
939}
940
941static int at91_twi_runtime_resume(struct device *dev)
942{
943 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
944
Wenyou Yang62d10c42014-11-10 09:55:52 +0800945 pinctrl_pm_select_default_state(dev);
946
Wenyou Yangd64a8182014-10-24 14:50:15 +0800947 return clk_prepare_enable(twi_dev->clk);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100948}
949
Wenyou Yang36765292014-10-24 14:50:16 +0800950static int at91_twi_suspend_noirq(struct device *dev)
951{
952 if (!pm_runtime_status_suspended(dev))
953 at91_twi_runtime_suspend(dev);
954
955 return 0;
956}
957
958static int at91_twi_resume_noirq(struct device *dev)
959{
960 int ret;
961
962 if (!pm_runtime_status_suspended(dev)) {
963 ret = at91_twi_runtime_resume(dev);
964 if (ret)
965 return ret;
966 }
967
968 pm_runtime_mark_last_busy(dev);
969 pm_request_autosuspend(dev);
970
971 return 0;
972}
973
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100974static const struct dev_pm_ops at91_twi_pm = {
Wenyou Yang36765292014-10-24 14:50:16 +0800975 .suspend_noirq = at91_twi_suspend_noirq,
976 .resume_noirq = at91_twi_resume_noirq,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100977 .runtime_suspend = at91_twi_runtime_suspend,
978 .runtime_resume = at91_twi_runtime_resume,
979};
980
981#define at91_twi_pm_ops (&at91_twi_pm)
982#else
983#define at91_twi_pm_ops NULL
984#endif
985
986static struct platform_driver at91_twi_driver = {
987 .probe = at91_twi_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500988 .remove = at91_twi_remove,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100989 .id_table = at91_twi_devtypes,
990 .driver = {
991 .name = "at91_i2c",
Sachin Kamat600abea2013-03-14 00:13:03 +0000992 .of_match_table = of_match_ptr(atmel_twi_dt_ids),
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100993 .pm = at91_twi_pm_ops,
994 },
995};
996
997static int __init at91_twi_init(void)
998{
999 return platform_driver_register(&at91_twi_driver);
1000}
1001
1002static void __exit at91_twi_exit(void)
1003{
1004 platform_driver_unregister(&at91_twi_driver);
1005}
1006
1007subsys_initcall(at91_twi_init);
1008module_exit(at91_twi_exit);
1009
1010MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
1011MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
1012MODULE_LICENSE("GPL");
1013MODULE_ALIAS("platform:at91_i2c");