blob: 94c087b0b7220027d5cb8314fb3f18ff6d8fdc0b [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 */
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +020057#define AT91_TWI_FIFOEN BIT(28) /* FIFO Enable */
58#define AT91_TWI_FIFODIS BIT(29) /* FIFO Disable */
Nikolaus Vossfac368a2011-11-08 11:49:46 +010059
60#define AT91_TWI_MMR 0x0004 /* Master Mode Register */
61#define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */
Cyrille Pitchene84cf8f2015-06-09 18:22:15 +020062#define AT91_TWI_MREAD BIT(12) /* Master Read Direction */
Nikolaus Vossfac368a2011-11-08 11:49:46 +010063
64#define AT91_TWI_IADR 0x000c /* Internal Address Register */
65
66#define AT91_TWI_CWGR 0x0010 /* Clock Waveform Generator Reg */
67
68#define AT91_TWI_SR 0x0020 /* Status Register */
Cyrille Pitchene84cf8f2015-06-09 18:22:15 +020069#define AT91_TWI_TXCOMP BIT(0) /* Transmission Complete */
70#define AT91_TWI_RXRDY BIT(1) /* Receive Holding Register Ready */
71#define AT91_TWI_TXRDY BIT(2) /* Transmit Holding Register Ready */
72#define AT91_TWI_OVRE BIT(6) /* Overrun Error */
73#define AT91_TWI_UNRE BIT(7) /* Underrun Error */
74#define AT91_TWI_NACK BIT(8) /* Not Acknowledged */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +020075#define AT91_TWI_LOCK BIT(23) /* TWI Lock due to Frame Errors */
Nikolaus Vossfac368a2011-11-08 11:49:46 +010076
Cyrille Pitchen93563a62015-06-09 18:22:14 +020077#define AT91_TWI_INT_MASK \
78 (AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY | AT91_TWI_NACK)
79
Nikolaus Vossfac368a2011-11-08 11:49:46 +010080#define AT91_TWI_IER 0x0024 /* Interrupt Enable Register */
81#define AT91_TWI_IDR 0x0028 /* Interrupt Disable Register */
82#define AT91_TWI_IMR 0x002c /* Interrupt Mask Register */
83#define AT91_TWI_RHR 0x0030 /* Receive Holding Register */
84#define AT91_TWI_THR 0x0034 /* Transmit Holding Register */
85
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +020086#define AT91_TWI_ACR 0x0040 /* Alternative Command Register */
87#define AT91_TWI_ACR_DATAL(len) ((len) & 0xff)
88#define AT91_TWI_ACR_DIR BIT(8)
89
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +020090#define AT91_TWI_FMR 0x0050 /* FIFO Mode Register */
91#define AT91_TWI_FMR_TXRDYM(mode) (((mode) & 0x3) << 0)
92#define AT91_TWI_FMR_TXRDYM_MASK (0x3 << 0)
93#define AT91_TWI_FMR_RXRDYM(mode) (((mode) & 0x3) << 4)
94#define AT91_TWI_FMR_RXRDYM_MASK (0x3 << 4)
95#define AT91_TWI_ONE_DATA 0x0
96#define AT91_TWI_TWO_DATA 0x1
97#define AT91_TWI_FOUR_DATA 0x2
98
99#define AT91_TWI_FLR 0x0054 /* FIFO Level Register */
100
101#define AT91_TWI_FSR 0x0060 /* FIFO Status Register */
102#define AT91_TWI_FIER 0x0064 /* FIFO Interrupt Enable Register */
103#define AT91_TWI_FIDR 0x0068 /* FIFO Interrupt Disable Register */
104#define AT91_TWI_FIMR 0x006c /* FIFO Interrupt Mask Register */
105
Cyrille Pitchen6ce461e2015-06-09 18:22:18 +0200106#define AT91_TWI_VER 0x00fc /* Version Register */
107
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100108struct at91_twi_pdata {
Ludovic Desroches5f433812012-11-23 10:09:03 +0100109 unsigned clk_max_div;
110 unsigned clk_offset;
111 bool has_unre_flag;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200112 bool has_alt_cmd;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100113 struct at_dma_slave dma_slave;
114};
115
116struct at91_twi_dma {
117 struct dma_chan *chan_rx;
118 struct dma_chan *chan_tx;
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200119 struct scatterlist sg[2];
Ludovic Desroches60937b22012-11-23 10:09:04 +0100120 struct dma_async_tx_descriptor *data_desc;
121 enum dma_data_direction direction;
122 bool buf_mapped;
123 bool xfer_in_progress;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100124};
125
126struct at91_twi_dev {
Ludovic Desroches5f433812012-11-23 10:09:03 +0100127 struct device *dev;
128 void __iomem *base;
129 struct completion cmd_complete;
130 struct clk *clk;
131 u8 *buf;
132 size_t buf_len;
133 struct i2c_msg *msg;
134 int irq;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100135 unsigned imr;
Ludovic Desroches5f433812012-11-23 10:09:03 +0100136 unsigned transfer_status;
137 struct i2c_adapter adapter;
138 unsigned twi_cwgr_reg;
139 struct at91_twi_pdata *pdata;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100140 bool use_dma;
Marek Roszko75b81f32014-08-20 21:39:41 -0400141 bool recv_len_abort;
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200142 u32 fifo_size;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100143 struct at91_twi_dma dma;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100144};
145
146static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg)
147{
148 return readl_relaxed(dev->base + reg);
149}
150
151static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val)
152{
153 writel_relaxed(val, dev->base + reg);
154}
155
156static void at91_disable_twi_interrupts(struct at91_twi_dev *dev)
157{
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200158 at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_INT_MASK);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100159}
160
Ludovic Desroches60937b22012-11-23 10:09:04 +0100161static void at91_twi_irq_save(struct at91_twi_dev *dev)
162{
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200163 dev->imr = at91_twi_read(dev, AT91_TWI_IMR) & AT91_TWI_INT_MASK;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100164 at91_disable_twi_interrupts(dev);
165}
166
167static void at91_twi_irq_restore(struct at91_twi_dev *dev)
168{
169 at91_twi_write(dev, AT91_TWI_IER, dev->imr);
170}
171
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100172static void at91_init_twi_bus(struct at91_twi_dev *dev)
173{
174 at91_disable_twi_interrupts(dev);
175 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200176 /* FIFO should be enabled immediately after the software reset */
177 if (dev->fifo_size)
178 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100179 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
180 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
181 at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
182}
183
184/*
185 * Calculate symmetric clock as stated in datasheet:
186 * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
187 */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500188static void at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100189{
190 int ckdiv, cdiv, div;
191 struct at91_twi_pdata *pdata = dev->pdata;
192 int offset = pdata->clk_offset;
193 int max_ckdiv = pdata->clk_max_div;
194
195 div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
196 2 * twi_clk) - offset);
197 ckdiv = fls(div >> 8);
198 cdiv = div >> ckdiv;
199
200 if (ckdiv > max_ckdiv) {
201 dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
202 ckdiv, max_ckdiv);
203 ckdiv = max_ckdiv;
204 cdiv = 255;
205 }
206
207 dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv;
208 dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv);
209}
210
Ludovic Desroches60937b22012-11-23 10:09:04 +0100211static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
212{
213 struct at91_twi_dma *dma = &dev->dma;
214
215 at91_twi_irq_save(dev);
216
217 if (dma->xfer_in_progress) {
218 if (dma->direction == DMA_FROM_DEVICE)
219 dmaengine_terminate_all(dma->chan_rx);
220 else
221 dmaengine_terminate_all(dma->chan_tx);
222 dma->xfer_in_progress = false;
223 }
224 if (dma->buf_mapped) {
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200225 dma_unmap_single(dev->dev, sg_dma_address(&dma->sg[0]),
Ludovic Desroches60937b22012-11-23 10:09:04 +0100226 dev->buf_len, dma->direction);
227 dma->buf_mapped = false;
228 }
229
230 at91_twi_irq_restore(dev);
231}
232
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100233static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
234{
Cyrille Pitchenf30dc522015-06-11 11:16:32 +0200235 if (!dev->buf_len)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100236 return;
237
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200238 /* 8bit write works with and without FIFO */
239 writeb_relaxed(*dev->buf, dev->base + AT91_TWI_THR);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100240
241 /* send stop when last byte has been written */
242 if (--dev->buf_len == 0)
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);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100245
246 dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len);
247
248 ++dev->buf;
249}
250
Ludovic Desroches60937b22012-11-23 10:09:04 +0100251static void at91_twi_write_data_dma_callback(void *data)
252{
253 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
254
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200255 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
Wolfram Sang28772ac2014-07-21 11:42:03 +0200256 dev->buf_len, DMA_TO_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100257
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200258 /*
259 * When this callback is called, THR/TX FIFO is likely not to be empty
260 * yet. So we have to wait for TXCOMP or NACK bits to be set into the
261 * Status Register to be sure that the STOP bit has been sent and the
262 * transfer is completed. The NACK interrupt has already been enabled,
263 * we just have to enable TXCOMP one.
264 */
265 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200266 if (!dev->pdata->has_alt_cmd)
267 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100268}
269
270static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
271{
272 dma_addr_t dma_addr;
273 struct dma_async_tx_descriptor *txdesc;
274 struct at91_twi_dma *dma = &dev->dma;
275 struct dma_chan *chan_tx = dma->chan_tx;
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200276 unsigned int sg_len = 1;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100277
Cyrille Pitchenf30dc522015-06-11 11:16:32 +0200278 if (!dev->buf_len)
Ludovic Desroches60937b22012-11-23 10:09:04 +0100279 return;
280
281 dma->direction = DMA_TO_DEVICE;
282
283 at91_twi_irq_save(dev);
284 dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
285 DMA_TO_DEVICE);
286 if (dma_mapping_error(dev->dev, dma_addr)) {
287 dev_err(dev->dev, "dma map failed\n");
288 return;
289 }
290 dma->buf_mapped = true;
291 at91_twi_irq_restore(dev);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100292
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200293 if (dev->fifo_size) {
294 size_t part1_len, part2_len;
295 struct scatterlist *sg;
296 unsigned fifo_mr;
297
298 sg_len = 0;
299
300 part1_len = dev->buf_len & ~0x3;
301 if (part1_len) {
302 sg = &dma->sg[sg_len++];
303 sg_dma_len(sg) = part1_len;
304 sg_dma_address(sg) = dma_addr;
305 }
306
307 part2_len = dev->buf_len & 0x3;
308 if (part2_len) {
309 sg = &dma->sg[sg_len++];
310 sg_dma_len(sg) = part2_len;
311 sg_dma_address(sg) = dma_addr + part1_len;
312 }
313
314 /*
315 * DMA controller is triggered when at least 4 data can be
316 * written into the TX FIFO
317 */
318 fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
319 fifo_mr &= ~AT91_TWI_FMR_TXRDYM_MASK;
320 fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_FOUR_DATA);
321 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
322 } else {
323 sg_dma_len(&dma->sg[0]) = dev->buf_len;
324 sg_dma_address(&dma->sg[0]) = dma_addr;
325 }
326
327 txdesc = dmaengine_prep_slave_sg(chan_tx, dma->sg, sg_len,
328 DMA_MEM_TO_DEV,
Ludovic Desroches60937b22012-11-23 10:09:04 +0100329 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
330 if (!txdesc) {
331 dev_err(dev->dev, "dma prep slave sg failed\n");
332 goto error;
333 }
334
335 txdesc->callback = at91_twi_write_data_dma_callback;
336 txdesc->callback_param = dev;
337
338 dma->xfer_in_progress = true;
339 dmaengine_submit(txdesc);
340 dma_async_issue_pending(chan_tx);
341
342 return;
343
344error:
345 at91_twi_dma_cleanup(dev);
346}
347
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100348static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
349{
Cyrille Pitchenf30dc522015-06-11 11:16:32 +0200350 if (!dev->buf_len)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100351 return;
352
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200353 /* 8bit read works with and without FIFO */
354 *dev->buf = readb_relaxed(dev->base + AT91_TWI_RHR);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100355 --dev->buf_len;
356
Marek Roszko75b81f32014-08-20 21:39:41 -0400357 /* return if aborting, we only needed to read RHR to clear RXRDY*/
358 if (dev->recv_len_abort)
359 return;
360
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100361 /* handle I2C_SMBUS_BLOCK_DATA */
362 if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
Marek Roszko75b81f32014-08-20 21:39:41 -0400363 /* ensure length byte is a valid value */
364 if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
365 dev->msg->flags &= ~I2C_M_RECV_LEN;
366 dev->buf_len += *dev->buf;
367 dev->msg->len = dev->buf_len + 1;
368 dev_dbg(dev->dev, "received block length %d\n",
369 dev->buf_len);
370 } else {
371 /* abort and send the stop by reading one more byte */
372 dev->recv_len_abort = true;
373 dev->buf_len = 1;
374 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100375 }
376
377 /* send stop if second but last byte has been read */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200378 if (!dev->pdata->has_alt_cmd && dev->buf_len == 1)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100379 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
380
381 dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len);
382
383 ++dev->buf;
384}
385
Ludovic Desroches60937b22012-11-23 10:09:04 +0100386static void at91_twi_read_data_dma_callback(void *data)
387{
388 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200389 unsigned ier = AT91_TWI_TXCOMP;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100390
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200391 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
Wolfram Sang28772ac2014-07-21 11:42:03 +0200392 dev->buf_len, DMA_FROM_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100393
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200394 if (!dev->pdata->has_alt_cmd) {
395 /* The last two bytes have to be read without using dma */
396 dev->buf += dev->buf_len - 2;
397 dev->buf_len = 2;
398 ier |= AT91_TWI_RXRDY;
399 }
400 at91_twi_write(dev, AT91_TWI_IER, ier);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100401}
402
403static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
404{
405 dma_addr_t dma_addr;
406 struct dma_async_tx_descriptor *rxdesc;
407 struct at91_twi_dma *dma = &dev->dma;
408 struct dma_chan *chan_rx = dma->chan_rx;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200409 size_t buf_len;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100410
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200411 buf_len = (dev->pdata->has_alt_cmd) ? dev->buf_len : dev->buf_len - 2;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100412 dma->direction = DMA_FROM_DEVICE;
413
414 /* Keep in mind that we won't use dma to read the last two bytes */
415 at91_twi_irq_save(dev);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200416 dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100417 if (dma_mapping_error(dev->dev, dma_addr)) {
418 dev_err(dev->dev, "dma map failed\n");
419 return;
420 }
421 dma->buf_mapped = true;
422 at91_twi_irq_restore(dev);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100423
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200424 if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) {
425 unsigned fifo_mr;
426
427 /*
428 * DMA controller is triggered when at least 4 data can be
429 * read from the RX FIFO
430 */
431 fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
432 fifo_mr &= ~AT91_TWI_FMR_RXRDYM_MASK;
433 fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_FOUR_DATA);
434 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
435 }
436
437 sg_dma_len(&dma->sg[0]) = buf_len;
438 sg_dma_address(&dma->sg[0]) = dma_addr;
439
440 rxdesc = dmaengine_prep_slave_sg(chan_rx, dma->sg, 1, DMA_DEV_TO_MEM,
Ludovic Desroches60937b22012-11-23 10:09:04 +0100441 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
442 if (!rxdesc) {
443 dev_err(dev->dev, "dma prep slave sg failed\n");
444 goto error;
445 }
446
447 rxdesc->callback = at91_twi_read_data_dma_callback;
448 rxdesc->callback_param = dev;
449
450 dma->xfer_in_progress = true;
451 dmaengine_submit(rxdesc);
452 dma_async_issue_pending(dma->chan_rx);
453
454 return;
455
456error:
457 at91_twi_dma_cleanup(dev);
458}
459
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100460static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
461{
462 struct at91_twi_dev *dev = dev_id;
463 const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
464 const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
465
466 if (!irqstatus)
467 return IRQ_NONE;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100468
Cyrille Pitchen6f6ddbb2015-10-21 15:44:03 +0200469 /*
470 * When a NACK condition is detected, the I2C controller sets the NACK,
471 * TXCOMP and TXRDY bits all together in the Status Register (SR).
472 *
473 * 1 - Handling NACK errors with CPU write transfer.
474 *
475 * In such case, we should not write the next byte into the Transmit
476 * Holding Register (THR) otherwise the I2C controller would start a new
477 * transfer and the I2C slave is likely to reply by another NACK.
478 *
479 * 2 - Handling NACK errors with DMA write transfer.
480 *
481 * By setting the TXRDY bit in the SR, the I2C controller also triggers
482 * the DMA controller to write the next data into the THR. Then the
483 * result depends on the hardware version of the I2C controller.
484 *
485 * 2a - Without support of the Alternative Command mode.
486 *
487 * This is the worst case: the DMA controller is triggered to write the
488 * next data into the THR, hence starting a new transfer: the I2C slave
489 * is likely to reply by another NACK.
490 * Concurrently, this interrupt handler is likely to be called to manage
491 * the first NACK before the I2C controller detects the second NACK and
492 * sets once again the NACK bit into the SR.
493 * When handling the first NACK, this interrupt handler disables the I2C
494 * controller interruptions, especially the NACK interrupt.
495 * Hence, the NACK bit is pending into the SR. This is why we should
496 * read the SR to clear all pending interrupts at the beginning of
497 * at91_do_twi_transfer() before actually starting a new transfer.
498 *
499 * 2b - With support of the Alternative Command mode.
500 *
501 * When a NACK condition is detected, the I2C controller also locks the
502 * THR (and sets the LOCK bit in the SR): even though the DMA controller
503 * is triggered by the TXRDY bit to write the next data into the THR,
504 * this data actually won't go on the I2C bus hence a second NACK is not
505 * generated.
506 */
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200507 if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100508 at91_disable_twi_interrupts(dev);
509 complete(&dev->cmd_complete);
Cyrille Pitchen6f6ddbb2015-10-21 15:44:03 +0200510 } else if (irqstatus & AT91_TWI_RXRDY) {
511 at91_twi_read_next_byte(dev);
512 } else if (irqstatus & AT91_TWI_TXRDY) {
513 at91_twi_write_next_byte(dev);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100514 }
515
Cyrille Pitchen6f6ddbb2015-10-21 15:44:03 +0200516 /* catch error flags */
517 dev->transfer_status |= status;
518
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100519 return IRQ_HANDLED;
520}
521
522static int at91_do_twi_transfer(struct at91_twi_dev *dev)
523{
524 int ret;
Nicholas Mc Guire1c42aca2015-02-08 11:12:07 -0500525 unsigned long time_left;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100526 bool has_unre_flag = dev->pdata->has_unre_flag;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200527 bool has_alt_cmd = dev->pdata->has_alt_cmd;
Cyrille Pitchen6f6ddbb2015-10-21 15:44:03 +0200528 unsigned sr;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100529
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200530 /*
531 * WARNING: the TXCOMP bit in the Status Register is NOT a clear on
532 * read flag but shows the state of the transmission at the time the
533 * Status Register is read. According to the programmer datasheet,
534 * TXCOMP is set when both holding register and internal shifter are
535 * empty and STOP condition has been sent.
536 * Consequently, we should enable NACK interrupt rather than TXCOMP to
537 * detect transmission failure.
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200538 * Indeed let's take the case of an i2c write command using DMA.
539 * Whenever the slave doesn't acknowledge a byte, the LOCK, NACK and
540 * TXCOMP bits are set together into the Status Register.
541 * LOCK is a clear on write bit, which is set to prevent the DMA
542 * controller from sending new data on the i2c bus after a NACK
543 * condition has happened. Once locked, this i2c peripheral stops
544 * triggering the DMA controller for new data but it is more than
545 * likely that a new DMA transaction is already in progress, writing
546 * into the Transmit Holding Register. Since the peripheral is locked,
547 * these new data won't be sent to the i2c bus but they will remain
548 * into the Transmit Holding Register, so TXCOMP bit is cleared.
549 * Then when the interrupt handler is called, the Status Register is
550 * read: the TXCOMP bit is clear but NACK bit is still set. The driver
551 * manage the error properly, without waiting for timeout.
552 * This case can be reproduced easyly when writing into an at24 eeprom.
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200553 *
554 * Besides, the TXCOMP bit is already set before the i2c transaction
555 * has been started. For read transactions, this bit is cleared when
556 * writing the START bit into the Control Register. So the
557 * corresponding interrupt can safely be enabled just after.
558 * However for write transactions managed by the CPU, we first write
559 * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP
560 * interrupt. If TXCOMP interrupt were enabled before writing into THR,
561 * the interrupt handler would be called immediately and the i2c command
562 * would be reported as completed.
563 * Also when a write transaction is managed by the DMA controller,
564 * enabling the TXCOMP interrupt in this function may lead to a race
565 * condition since we don't know whether the TXCOMP interrupt is enabled
566 * before or after the DMA has started to write into THR. So the TXCOMP
567 * interrupt is enabled later by at91_twi_write_data_dma_callback().
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200568 * Immediately after in that DMA callback, if the alternative command
569 * mode is not used, we still need to send the STOP condition manually
570 * writing the corresponding bit into the Control Register.
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200571 */
572
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100573 dev_dbg(dev->dev, "transfer: %s %d bytes.\n",
574 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
575
Wolfram Sang16735d02013-11-14 14:32:02 -0800576 reinit_completion(&dev->cmd_complete);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100577 dev->transfer_status = 0;
Ludovic Desroches7c3fe642012-11-13 16:43:21 +0100578
Cyrille Pitchen6f6ddbb2015-10-21 15:44:03 +0200579 /* Clear pending interrupts, such as NACK. */
580 sr = at91_twi_read(dev, AT91_TWI_SR);
581
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200582 if (dev->fifo_size) {
583 unsigned fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
584
585 /* Reset FIFO mode register */
586 fifo_mr &= ~(AT91_TWI_FMR_TXRDYM_MASK |
587 AT91_TWI_FMR_RXRDYM_MASK);
588 fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_ONE_DATA);
589 fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_ONE_DATA);
590 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
591
592 /* Flush FIFOs */
593 at91_twi_write(dev, AT91_TWI_CR,
594 AT91_TWI_THRCLR | AT91_TWI_RHRCLR);
595 }
596
Ludovic Desroches7c3fe642012-11-13 16:43:21 +0100597 if (!dev->buf_len) {
598 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
599 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
600 } else if (dev->msg->flags & I2C_M_RD) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100601 unsigned start_flags = AT91_TWI_START;
602
Cyrille Pitchen6f6ddbb2015-10-21 15:44:03 +0200603 if (sr & AT91_TWI_RXRDY) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100604 dev_err(dev->dev, "RXRDY still set!");
605 at91_twi_read(dev, AT91_TWI_RHR);
606 }
607
608 /* if only one byte is to be read, immediately stop transfer */
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200609 if (!has_alt_cmd && dev->buf_len <= 1 &&
610 !(dev->msg->flags & I2C_M_RECV_LEN))
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100611 start_flags |= AT91_TWI_STOP;
612 at91_twi_write(dev, AT91_TWI_CR, start_flags);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100613 /*
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200614 * When using dma without alternative command mode, the last
615 * byte has to be read manually in order to not send the stop
616 * command too late and then to receive extra data.
617 * In practice, there are some issues if you use the dma to
618 * read n-1 bytes because of latency.
Ludovic Desroches60937b22012-11-23 10:09:04 +0100619 * Reading n-2 bytes with dma and the two last ones manually
620 * seems to be the best solution.
621 */
622 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200623 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100624 at91_twi_read_data_dma(dev);
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200625 } else {
Ludovic Desroches60937b22012-11-23 10:09:04 +0100626 at91_twi_write(dev, AT91_TWI_IER,
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200627 AT91_TWI_TXCOMP |
628 AT91_TWI_NACK |
629 AT91_TWI_RXRDY);
630 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100631 } else {
Ludovic Desroches60937b22012-11-23 10:09:04 +0100632 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200633 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100634 at91_twi_write_data_dma(dev);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100635 } else {
636 at91_twi_write_next_byte(dev);
637 at91_twi_write(dev, AT91_TWI_IER,
Cyrille Pitchen93563a62015-06-09 18:22:14 +0200638 AT91_TWI_TXCOMP |
639 AT91_TWI_NACK |
640 AT91_TWI_TXRDY);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100641 }
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100642 }
643
Nicholas Mc Guire1c42aca2015-02-08 11:12:07 -0500644 time_left = wait_for_completion_timeout(&dev->cmd_complete,
645 dev->adapter.timeout);
646 if (time_left == 0) {
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200647 dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100648 dev_err(dev->dev, "controller timed out\n");
649 at91_init_twi_bus(dev);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100650 ret = -ETIMEDOUT;
651 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100652 }
653 if (dev->transfer_status & AT91_TWI_NACK) {
654 dev_dbg(dev->dev, "received nack\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100655 ret = -EREMOTEIO;
656 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100657 }
658 if (dev->transfer_status & AT91_TWI_OVRE) {
659 dev_err(dev->dev, "overrun while reading\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100660 ret = -EIO;
661 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100662 }
663 if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
664 dev_err(dev->dev, "underrun while writing\n");
Ludovic Desroches60937b22012-11-23 10:09:04 +0100665 ret = -EIO;
666 goto error;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100667 }
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200668 if ((has_alt_cmd || dev->fifo_size) &&
669 (dev->transfer_status & AT91_TWI_LOCK)) {
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200670 dev_err(dev->dev, "tx locked\n");
671 ret = -EIO;
672 goto error;
673 }
Marek Roszko75b81f32014-08-20 21:39:41 -0400674 if (dev->recv_len_abort) {
675 dev_err(dev->dev, "invalid smbus block length recvd\n");
676 ret = -EPROTO;
677 goto error;
678 }
679
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100680 dev_dbg(dev->dev, "transfer complete\n");
681
682 return 0;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100683
684error:
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200685 /* first stop DMA transfer if still in progress */
Ludovic Desroches60937b22012-11-23 10:09:04 +0100686 at91_twi_dma_cleanup(dev);
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200687 /* then flush THR/FIFO and unlock TX if locked */
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200688 if ((has_alt_cmd || dev->fifo_size) &&
689 (dev->transfer_status & AT91_TWI_LOCK)) {
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200690 dev_dbg(dev->dev, "unlock tx\n");
691 at91_twi_write(dev, AT91_TWI_CR,
692 AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
693 }
Ludovic Desroches60937b22012-11-23 10:09:04 +0100694 return ret;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100695}
696
697static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
698{
699 struct at91_twi_dev *dev = i2c_get_adapdata(adap);
700 int ret;
701 unsigned int_addr_flag = 0;
702 struct i2c_msg *m_start = msg;
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200703 bool is_read, use_alt_cmd = false;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100704
705 dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
706
Wenyou Yangd64a8182014-10-24 14:50:15 +0800707 ret = pm_runtime_get_sync(dev->dev);
708 if (ret < 0)
709 goto out;
710
Wolfram Sanga7405842015-01-07 12:24:10 +0100711 if (num == 2) {
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100712 int internal_address = 0;
713 int i;
714
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100715 /* 1st msg is put into the internal address, start with 2nd */
716 m_start = &msg[1];
717 for (i = 0; i < msg->len; ++i) {
718 const unsigned addr = msg->buf[msg->len - 1 - i];
719
720 internal_address |= addr << (8 * i);
721 int_addr_flag += AT91_TWI_IADRSZ_1;
722 }
723 at91_twi_write(dev, AT91_TWI_IADR, internal_address);
724 }
725
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200726 is_read = (m_start->flags & I2C_M_RD);
727 if (dev->pdata->has_alt_cmd) {
728 if (m_start->len > 0) {
729 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN);
730 at91_twi_write(dev, AT91_TWI_ACR,
731 AT91_TWI_ACR_DATAL(m_start->len) |
732 ((is_read) ? AT91_TWI_ACR_DIR : 0));
733 use_alt_cmd = true;
734 } else {
735 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS);
736 }
737 }
738
739 at91_twi_write(dev, AT91_TWI_MMR,
740 (m_start->addr << 16) |
741 int_addr_flag |
742 ((!use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0));
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100743
744 dev->buf_len = m_start->len;
745 dev->buf = m_start->buf;
746 dev->msg = m_start;
Marek Roszko75b81f32014-08-20 21:39:41 -0400747 dev->recv_len_abort = false;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100748
749 ret = at91_do_twi_transfer(dev);
750
Wenyou Yangd64a8182014-10-24 14:50:15 +0800751 ret = (ret < 0) ? ret : num;
752out:
753 pm_runtime_mark_last_busy(dev->dev);
754 pm_runtime_put_autosuspend(dev->dev);
755
756 return ret;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100757}
758
Wolfram Sanga7405842015-01-07 12:24:10 +0100759/*
760 * The hardware can handle at most two messages concatenated by a
761 * repeated start via it's internal address feature.
762 */
763static struct i2c_adapter_quirks at91_twi_quirks = {
764 .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
765 .max_comb_1st_msg_len = 3,
766};
767
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100768static u32 at91_twi_func(struct i2c_adapter *adapter)
769{
770 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
771 | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
772}
773
774static struct i2c_algorithm at91_twi_algorithm = {
775 .master_xfer = at91_twi_xfer,
776 .functionality = at91_twi_func,
777};
778
779static struct at91_twi_pdata at91rm9200_config = {
780 .clk_max_div = 5,
781 .clk_offset = 3,
782 .has_unre_flag = true,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200783 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100784};
785
786static struct at91_twi_pdata at91sam9261_config = {
787 .clk_max_div = 5,
788 .clk_offset = 4,
789 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200790 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100791};
792
793static struct at91_twi_pdata at91sam9260_config = {
794 .clk_max_div = 7,
795 .clk_offset = 4,
796 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200797 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100798};
799
800static struct at91_twi_pdata at91sam9g20_config = {
801 .clk_max_div = 7,
802 .clk_offset = 4,
803 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200804 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100805};
806
807static struct at91_twi_pdata at91sam9g10_config = {
808 .clk_max_div = 7,
809 .clk_offset = 4,
810 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200811 .has_alt_cmd = false,
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100812};
813
814static const struct platform_device_id at91_twi_devtypes[] = {
815 {
816 .name = "i2c-at91rm9200",
817 .driver_data = (unsigned long) &at91rm9200_config,
818 }, {
819 .name = "i2c-at91sam9261",
820 .driver_data = (unsigned long) &at91sam9261_config,
821 }, {
822 .name = "i2c-at91sam9260",
823 .driver_data = (unsigned long) &at91sam9260_config,
824 }, {
825 .name = "i2c-at91sam9g20",
826 .driver_data = (unsigned long) &at91sam9g20_config,
827 }, {
828 .name = "i2c-at91sam9g10",
829 .driver_data = (unsigned long) &at91sam9g10_config,
830 }, {
831 /* sentinel */
832 }
833};
834
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200835#if defined(CONFIG_OF)
Joachim Eastwood4182b432013-02-09 19:14:00 +0100836static struct at91_twi_pdata at91sam9x5_config = {
837 .clk_max_div = 7,
838 .clk_offset = 4,
839 .has_unre_flag = false,
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200840 .has_alt_cmd = false,
841};
842
843static struct at91_twi_pdata sama5d2_config = {
844 .clk_max_div = 7,
845 .clk_offset = 4,
846 .has_unre_flag = true,
847 .has_alt_cmd = true,
Joachim Eastwood4182b432013-02-09 19:14:00 +0100848};
849
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200850static const struct of_device_id atmel_twi_dt_ids[] = {
851 {
Joachim Eastwood631056c2012-12-05 22:42:12 +0100852 .compatible = "atmel,at91rm9200-i2c",
853 .data = &at91rm9200_config,
854 } , {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200855 .compatible = "atmel,at91sam9260-i2c",
856 .data = &at91sam9260_config,
857 } , {
jean-jacques hiblotd9a3afc2014-01-15 14:17:13 +0100858 .compatible = "atmel,at91sam9261-i2c",
859 .data = &at91sam9261_config,
860 } , {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200861 .compatible = "atmel,at91sam9g20-i2c",
862 .data = &at91sam9g20_config,
863 } , {
864 .compatible = "atmel,at91sam9g10-i2c",
865 .data = &at91sam9g10_config,
866 }, {
867 .compatible = "atmel,at91sam9x5-i2c",
868 .data = &at91sam9x5_config,
869 }, {
Cyrille Pitchen0ef6f322015-06-09 18:22:17 +0200870 .compatible = "atmel,sama5d2-i2c",
871 .data = &sama5d2_config,
872 }, {
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200873 /* sentinel */
874 }
875};
876MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids);
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200877#endif
878
Bill Pemberton0b255e92012-11-27 15:59:38 -0500879static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
Ludovic Desroches60937b22012-11-23 10:09:04 +0100880{
881 int ret = 0;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100882 struct dma_slave_config slave_config;
883 struct at91_twi_dma *dma = &dev->dma;
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200884 enum dma_slave_buswidth addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
885
886 /*
887 * The actual width of the access will be chosen in
888 * dmaengine_prep_slave_sg():
889 * for each buffer in the scatter-gather list, if its size is aligned
890 * to addr_width then addr_width accesses will be performed to transfer
891 * the buffer. On the other hand, if the buffer size is not aligned to
892 * addr_width then the buffer is transferred using single byte accesses.
893 * Please refer to the Atmel eXtended DMA controller driver.
894 * When FIFOs are used, the TXRDYM threshold can always be set to
895 * trigger the XDMAC when at least 4 data can be written into the TX
896 * FIFO, even if single byte accesses are performed.
897 * However the RXRDYM threshold must be set to fit the access width,
898 * deduced from buffer length, so the XDMAC is triggered properly to
899 * read data from the RX FIFO.
900 */
901 if (dev->fifo_size)
902 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100903
904 memset(&slave_config, 0, sizeof(slave_config));
905 slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200906 slave_config.src_addr_width = addr_width;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100907 slave_config.src_maxburst = 1;
908 slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200909 slave_config.dst_addr_width = addr_width;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100910 slave_config.dst_maxburst = 1;
911 slave_config.device_fc = false;
912
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100913 dma->chan_tx = dma_request_slave_channel_reason(dev->dev, "tx");
914 if (IS_ERR(dma->chan_tx)) {
915 ret = PTR_ERR(dma->chan_tx);
916 dma->chan_tx = NULL;
Ludovic Desrochesd877a722013-04-15 02:16:56 +0000917 goto error;
918 }
919
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100920 dma->chan_rx = dma_request_slave_channel_reason(dev->dev, "rx");
921 if (IS_ERR(dma->chan_rx)) {
922 ret = PTR_ERR(dma->chan_rx);
923 dma->chan_rx = NULL;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100924 goto error;
925 }
926
927 slave_config.direction = DMA_MEM_TO_DEV;
928 if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
929 dev_err(dev->dev, "failed to configure tx channel\n");
930 ret = -EINVAL;
931 goto error;
932 }
933
934 slave_config.direction = DMA_DEV_TO_MEM;
935 if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
936 dev_err(dev->dev, "failed to configure rx channel\n");
937 ret = -EINVAL;
938 goto error;
939 }
940
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +0200941 sg_init_table(dma->sg, 2);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100942 dma->buf_mapped = false;
943 dma->xfer_in_progress = false;
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100944 dev->use_dma = true;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100945
946 dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
947 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
948
949 return ret;
950
951error:
Ludovic Desroches727f9c22014-11-21 14:44:32 +0100952 if (ret != -EPROBE_DEFER)
953 dev_info(dev->dev, "can't use DMA, error %d\n", ret);
Ludovic Desroches60937b22012-11-23 10:09:04 +0100954 if (dma->chan_rx)
955 dma_release_channel(dma->chan_rx);
956 if (dma->chan_tx)
957 dma_release_channel(dma->chan_tx);
958 return ret;
959}
960
Bill Pemberton0b255e92012-11-27 15:59:38 -0500961static struct at91_twi_pdata *at91_twi_get_driver_data(
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200962 struct platform_device *pdev)
963{
964 if (pdev->dev.of_node) {
965 const struct of_device_id *match;
966 match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node);
967 if (!match)
968 return NULL;
Ludovic Desrochescd32e6c2012-11-23 17:03:16 +0100969 return (struct at91_twi_pdata *)match->data;
Ludovic Desroches70d46a22012-09-12 08:42:14 +0200970 }
971 return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data;
972}
973
Bill Pemberton0b255e92012-11-27 15:59:38 -0500974static int at91_twi_probe(struct platform_device *pdev)
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100975{
976 struct at91_twi_dev *dev;
977 struct resource *mem;
978 int rc;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100979 u32 phy_addr;
Marek Roszko75b6c4b2014-03-11 00:25:38 -0400980 u32 bus_clk_rate;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100981
982 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
983 if (!dev)
984 return -ENOMEM;
985 init_completion(&dev->cmd_complete);
986 dev->dev = &pdev->dev;
987
988 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
989 if (!mem)
990 return -ENODEV;
Ludovic Desroches60937b22012-11-23 10:09:04 +0100991 phy_addr = mem->start;
Nikolaus Vossfac368a2011-11-08 11:49:46 +0100992
993 dev->pdata = at91_twi_get_driver_data(pdev);
994 if (!dev->pdata)
995 return -ENODEV;
996
Thierry Reding84dbf802013-01-21 11:09:03 +0100997 dev->base = devm_ioremap_resource(&pdev->dev, mem);
998 if (IS_ERR(dev->base))
999 return PTR_ERR(dev->base);
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001000
1001 dev->irq = platform_get_irq(pdev, 0);
1002 if (dev->irq < 0)
1003 return dev->irq;
1004
1005 rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
1006 dev_name(dev->dev), dev);
1007 if (rc) {
1008 dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
1009 return rc;
1010 }
1011
1012 platform_set_drvdata(pdev, dev);
1013
1014 dev->clk = devm_clk_get(dev->dev, NULL);
1015 if (IS_ERR(dev->clk)) {
1016 dev_err(dev->dev, "no clock defined\n");
1017 return -ENODEV;
1018 }
1019 clk_prepare_enable(dev->clk);
1020
Arnd Bergmanndc6df6e2014-11-21 14:44:31 +01001021 if (dev->dev->of_node) {
Ludovic Desroches727f9c22014-11-21 14:44:32 +01001022 rc = at91_twi_configure_dma(dev, phy_addr);
1023 if (rc == -EPROBE_DEFER)
1024 return rc;
Ludovic Desroches60937b22012-11-23 10:09:04 +01001025 }
1026
Cyrille Pitchen5e3cfc62015-06-09 18:22:19 +02001027 if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
1028 &dev->fifo_size)) {
1029 dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
1030 }
1031
Marek Roszko75b6c4b2014-03-11 00:25:38 -04001032 rc = of_property_read_u32(dev->dev->of_node, "clock-frequency",
1033 &bus_clk_rate);
1034 if (rc)
1035 bus_clk_rate = DEFAULT_TWI_CLK_HZ;
1036
1037 at91_calc_twi_clock(dev, bus_clk_rate);
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001038 at91_init_twi_bus(dev);
1039
1040 snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
1041 i2c_set_adapdata(&dev->adapter, dev);
1042 dev->adapter.owner = THIS_MODULE;
Wolfram Sangb8505792014-07-10 13:46:22 +02001043 dev->adapter.class = I2C_CLASS_DEPRECATED;
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001044 dev->adapter.algo = &at91_twi_algorithm;
Wolfram Sanga7405842015-01-07 12:24:10 +01001045 dev->adapter.quirks = &at91_twi_quirks;
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001046 dev->adapter.dev.parent = dev->dev;
1047 dev->adapter.nr = pdev->id;
1048 dev->adapter.timeout = AT91_I2C_TIMEOUT;
Ludovic Desroches70d46a22012-09-12 08:42:14 +02001049 dev->adapter.dev.of_node = pdev->dev.of_node;
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001050
Wenyou Yangd64a8182014-10-24 14:50:15 +08001051 pm_runtime_set_autosuspend_delay(dev->dev, AUTOSUSPEND_TIMEOUT);
1052 pm_runtime_use_autosuspend(dev->dev);
1053 pm_runtime_set_active(dev->dev);
1054 pm_runtime_enable(dev->dev);
1055
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001056 rc = i2c_add_numbered_adapter(&dev->adapter);
1057 if (rc) {
1058 dev_err(dev->dev, "Adapter %s registration failed\n",
1059 dev->adapter.name);
1060 clk_disable_unprepare(dev->clk);
Wenyou Yangd64a8182014-10-24 14:50:15 +08001061
1062 pm_runtime_disable(dev->dev);
1063 pm_runtime_set_suspended(dev->dev);
1064
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001065 return rc;
1066 }
1067
Cyrille Pitchen6ce461e2015-06-09 18:22:18 +02001068 dev_info(dev->dev, "AT91 i2c bus driver (hw version: %#x).\n",
1069 at91_twi_read(dev, AT91_TWI_VER));
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001070 return 0;
1071}
1072
Bill Pemberton0b255e92012-11-27 15:59:38 -05001073static int at91_twi_remove(struct platform_device *pdev)
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001074{
1075 struct at91_twi_dev *dev = platform_get_drvdata(pdev);
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001076
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +00001077 i2c_del_adapter(&dev->adapter);
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001078 clk_disable_unprepare(dev->clk);
1079
Wenyou Yangd64a8182014-10-24 14:50:15 +08001080 pm_runtime_disable(dev->dev);
1081 pm_runtime_set_suspended(dev->dev);
1082
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +00001083 return 0;
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001084}
1085
1086#ifdef CONFIG_PM
1087
1088static int at91_twi_runtime_suspend(struct device *dev)
1089{
1090 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
1091
Wenyou Yangd64a8182014-10-24 14:50:15 +08001092 clk_disable_unprepare(twi_dev->clk);
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001093
Wenyou Yang62d10c42014-11-10 09:55:52 +08001094 pinctrl_pm_select_sleep_state(dev);
1095
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001096 return 0;
1097}
1098
1099static int at91_twi_runtime_resume(struct device *dev)
1100{
1101 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
1102
Wenyou Yang62d10c42014-11-10 09:55:52 +08001103 pinctrl_pm_select_default_state(dev);
1104
Wenyou Yangd64a8182014-10-24 14:50:15 +08001105 return clk_prepare_enable(twi_dev->clk);
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001106}
1107
Wenyou Yang36765292014-10-24 14:50:16 +08001108static int at91_twi_suspend_noirq(struct device *dev)
1109{
1110 if (!pm_runtime_status_suspended(dev))
1111 at91_twi_runtime_suspend(dev);
1112
1113 return 0;
1114}
1115
1116static int at91_twi_resume_noirq(struct device *dev)
1117{
1118 int ret;
1119
1120 if (!pm_runtime_status_suspended(dev)) {
1121 ret = at91_twi_runtime_resume(dev);
1122 if (ret)
1123 return ret;
1124 }
1125
1126 pm_runtime_mark_last_busy(dev);
1127 pm_request_autosuspend(dev);
1128
1129 return 0;
1130}
1131
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001132static const struct dev_pm_ops at91_twi_pm = {
Wenyou Yang36765292014-10-24 14:50:16 +08001133 .suspend_noirq = at91_twi_suspend_noirq,
1134 .resume_noirq = at91_twi_resume_noirq,
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001135 .runtime_suspend = at91_twi_runtime_suspend,
1136 .runtime_resume = at91_twi_runtime_resume,
1137};
1138
1139#define at91_twi_pm_ops (&at91_twi_pm)
1140#else
1141#define at91_twi_pm_ops NULL
1142#endif
1143
1144static struct platform_driver at91_twi_driver = {
1145 .probe = at91_twi_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001146 .remove = at91_twi_remove,
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001147 .id_table = at91_twi_devtypes,
1148 .driver = {
1149 .name = "at91_i2c",
Sachin Kamat600abea2013-03-14 00:13:03 +00001150 .of_match_table = of_match_ptr(atmel_twi_dt_ids),
Nikolaus Vossfac368a2011-11-08 11:49:46 +01001151 .pm = at91_twi_pm_ops,
1152 },
1153};
1154
1155static int __init at91_twi_init(void)
1156{
1157 return platform_driver_register(&at91_twi_driver);
1158}
1159
1160static void __exit at91_twi_exit(void)
1161{
1162 platform_driver_unregister(&at91_twi_driver);
1163}
1164
1165subsys_initcall(at91_twi_init);
1166module_exit(at91_twi_exit);
1167
1168MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
1169MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
1170MODULE_LICENSE("GPL");
1171MODULE_ALIAS("platform:at91_i2c");