blob: 0ec1f1502def98ced972f8a98e21c0200412cf48 [file] [log] [blame]
Wolfram Sanga8da7fe2011-02-16 13:39:16 +01001/*
2 * Freescale MXS I2C bus driver
3 *
Wolfram Sang82fa63b2012-10-12 11:55:16 +01004 * Copyright (C) 2011-2012 Wolfram Sang, Pengutronix e.K.
Wolfram Sanga8da7fe2011-02-16 13:39:16 +01005 *
6 * based on a (non-working) driver which was:
7 *
8 * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
9 *
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 */
16
17#include <linux/slab.h>
18#include <linux/device.h>
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/err.h>
22#include <linux/interrupt.h>
23#include <linux/completion.h>
24#include <linux/platform_device.h>
25#include <linux/jiffies.h>
26#include <linux/io.h>
Shawn Guod98d0332012-05-06 22:59:45 +080027#include <linux/pinctrl/consumer.h>
Wolfram Sang6b866c12011-08-31 20:37:50 +020028#include <linux/stmp_device.h>
Shawn Guob2378662012-05-12 13:43:32 +080029#include <linux/of.h>
30#include <linux/of_device.h>
31#include <linux/of_i2c.h>
Marek Vasut62885f52012-08-24 05:44:31 +020032#include <linux/dma-mapping.h>
33#include <linux/dmaengine.h>
34#include <linux/fsl/mxs-dma.h>
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010035
36#define DRIVER_NAME "mxs-i2c"
37
38#define MXS_I2C_CTRL0 (0x00)
39#define MXS_I2C_CTRL0_SET (0x04)
40
41#define MXS_I2C_CTRL0_SFTRST 0x80000000
Marek Vasutfc91e402013-01-24 13:56:21 +010042#define MXS_I2C_CTRL0_RUN 0x20000000
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010043#define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
44#define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
45#define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
46#define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
47#define MXS_I2C_CTRL0_MASTER_MODE 0x00020000
48#define MXS_I2C_CTRL0_DIRECTION 0x00010000
49#define MXS_I2C_CTRL0_XFER_COUNT(v) ((v) & 0x0000FFFF)
50
Marek Vasutcd4f2d42012-07-09 18:22:53 +020051#define MXS_I2C_TIMING0 (0x10)
52#define MXS_I2C_TIMING1 (0x20)
53#define MXS_I2C_TIMING2 (0x30)
54
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010055#define MXS_I2C_CTRL1 (0x40)
56#define MXS_I2C_CTRL1_SET (0x44)
57#define MXS_I2C_CTRL1_CLR (0x48)
58
59#define MXS_I2C_CTRL1_BUS_FREE_IRQ 0x80
60#define MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ 0x40
61#define MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ 0x20
62#define MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ 0x10
63#define MXS_I2C_CTRL1_EARLY_TERM_IRQ 0x08
64#define MXS_I2C_CTRL1_MASTER_LOSS_IRQ 0x04
65#define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
66#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
67
Lucas Stach535ebd22013-04-15 00:16:54 +000068#define MXS_I2C_STAT (0x50)
69#define MXS_I2C_STAT_BUS_BUSY 0x00000800
70#define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400
71
Marek Vasutfc91e402013-01-24 13:56:21 +010072#define MXS_I2C_DATA (0xa0)
73
74#define MXS_I2C_DEBUG0 (0xb0)
75#define MXS_I2C_DEBUG0_CLR (0xb8)
76
77#define MXS_I2C_DEBUG0_DMAREQ 0x80000000
78
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010079#define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
80 MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
81 MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
82 MXS_I2C_CTRL1_MASTER_LOSS_IRQ | \
83 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | \
84 MXS_I2C_CTRL1_SLAVE_IRQ)
85
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010086
87#define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
88 MXS_I2C_CTRL0_PRE_SEND_START | \
89 MXS_I2C_CTRL0_MASTER_MODE | \
90 MXS_I2C_CTRL0_DIRECTION | \
91 MXS_I2C_CTRL0_XFER_COUNT(1))
92
93#define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
94 MXS_I2C_CTRL0_MASTER_MODE | \
95 MXS_I2C_CTRL0_DIRECTION)
96
97#define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
98 MXS_I2C_CTRL0_MASTER_MODE)
99
100/**
101 * struct mxs_i2c_dev - per device, private MXS-I2C data
102 *
103 * @dev: driver model device node
104 * @regs: IO registers pointer
105 * @cmd_complete: completion object for transaction wait
106 * @cmd_err: error code for last transaction
107 * @adapter: i2c subsystem adapter node
108 */
109struct mxs_i2c_dev {
110 struct device *dev;
111 void __iomem *regs;
112 struct completion cmd_complete;
Fabio Estevam0f40cbc2013-01-07 22:32:06 -0200113 int cmd_err;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100114 struct i2c_adapter adapter;
Marek Vasut626f0a22012-11-30 18:48:35 +0100115
116 uint32_t timing0;
117 uint32_t timing1;
Marek Vasut62885f52012-08-24 05:44:31 +0200118
119 /* DMA support components */
Marek Vasut62885f52012-08-24 05:44:31 +0200120 int dma_channel;
121 struct dma_chan *dmach;
122 struct mxs_dma_data dma_data;
123 uint32_t pio_data[2];
124 uint32_t addr_data;
125 struct scatterlist sg_io[2];
126 bool dma_read;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100127};
128
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100129static void mxs_i2c_reset(struct mxs_i2c_dev *i2c)
130{
Wolfram Sang6b866c12011-08-31 20:37:50 +0200131 stmp_reset_block(i2c->regs);
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200132
Marek Vasut626f0a22012-11-30 18:48:35 +0100133 /*
134 * Configure timing for the I2C block. The I2C TIMING2 register has to
135 * be programmed with this particular magic number. The rest is derived
136 * from the XTAL speed and requested I2C speed.
137 *
138 * For details, see i.MX233 [25.4.2 - 25.4.4] and i.MX28 [27.5.2 - 27.5.4].
139 */
140 writel(i2c->timing0, i2c->regs + MXS_I2C_TIMING0);
141 writel(i2c->timing1, i2c->regs + MXS_I2C_TIMING1);
142 writel(0x00300030, i2c->regs + MXS_I2C_TIMING2);
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200143
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100144 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100145}
146
Marek Vasut62885f52012-08-24 05:44:31 +0200147static void mxs_i2c_dma_finish(struct mxs_i2c_dev *i2c)
148{
149 if (i2c->dma_read) {
150 dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
151 dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
152 } else {
153 dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
154 }
155}
156
157static void mxs_i2c_dma_irq_callback(void *param)
158{
159 struct mxs_i2c_dev *i2c = param;
160
161 complete(&i2c->cmd_complete);
162 mxs_i2c_dma_finish(i2c);
163}
164
165static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap,
166 struct i2c_msg *msg, uint32_t flags)
167{
168 struct dma_async_tx_descriptor *desc;
169 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
170
171 if (msg->flags & I2C_M_RD) {
172 i2c->dma_read = 1;
173 i2c->addr_data = (msg->addr << 1) | I2C_SMBUS_READ;
174
175 /*
176 * SELECT command.
177 */
178
179 /* Queue the PIO register write transfer. */
180 i2c->pio_data[0] = MXS_CMD_I2C_SELECT;
181 desc = dmaengine_prep_slave_sg(i2c->dmach,
182 (struct scatterlist *)&i2c->pio_data[0],
183 1, DMA_TRANS_NONE, 0);
184 if (!desc) {
185 dev_err(i2c->dev,
186 "Failed to get PIO reg. write descriptor.\n");
187 goto select_init_pio_fail;
188 }
189
190 /* Queue the DMA data transfer. */
191 sg_init_one(&i2c->sg_io[0], &i2c->addr_data, 1);
192 dma_map_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
193 desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[0], 1,
194 DMA_MEM_TO_DEV,
195 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
196 if (!desc) {
197 dev_err(i2c->dev,
198 "Failed to get DMA data write descriptor.\n");
199 goto select_init_dma_fail;
200 }
201
202 /*
203 * READ command.
204 */
205
206 /* Queue the PIO register write transfer. */
207 i2c->pio_data[1] = flags | MXS_CMD_I2C_READ |
208 MXS_I2C_CTRL0_XFER_COUNT(msg->len);
209 desc = dmaengine_prep_slave_sg(i2c->dmach,
210 (struct scatterlist *)&i2c->pio_data[1],
211 1, DMA_TRANS_NONE, DMA_PREP_INTERRUPT);
212 if (!desc) {
213 dev_err(i2c->dev,
214 "Failed to get PIO reg. write descriptor.\n");
215 goto select_init_dma_fail;
216 }
217
218 /* Queue the DMA data transfer. */
219 sg_init_one(&i2c->sg_io[1], msg->buf, msg->len);
220 dma_map_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
221 desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[1], 1,
222 DMA_DEV_TO_MEM,
223 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
224 if (!desc) {
225 dev_err(i2c->dev,
226 "Failed to get DMA data write descriptor.\n");
227 goto read_init_dma_fail;
228 }
229 } else {
230 i2c->dma_read = 0;
231 i2c->addr_data = (msg->addr << 1) | I2C_SMBUS_WRITE;
232
233 /*
234 * WRITE command.
235 */
236
237 /* Queue the PIO register write transfer. */
238 i2c->pio_data[0] = flags | MXS_CMD_I2C_WRITE |
239 MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1);
240 desc = dmaengine_prep_slave_sg(i2c->dmach,
241 (struct scatterlist *)&i2c->pio_data[0],
242 1, DMA_TRANS_NONE, 0);
243 if (!desc) {
244 dev_err(i2c->dev,
245 "Failed to get PIO reg. write descriptor.\n");
246 goto write_init_pio_fail;
247 }
248
249 /* Queue the DMA data transfer. */
250 sg_init_table(i2c->sg_io, 2);
251 sg_set_buf(&i2c->sg_io[0], &i2c->addr_data, 1);
252 sg_set_buf(&i2c->sg_io[1], msg->buf, msg->len);
253 dma_map_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
254 desc = dmaengine_prep_slave_sg(i2c->dmach, i2c->sg_io, 2,
255 DMA_MEM_TO_DEV,
256 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
257 if (!desc) {
258 dev_err(i2c->dev,
259 "Failed to get DMA data write descriptor.\n");
260 goto write_init_dma_fail;
261 }
262 }
263
264 /*
265 * The last descriptor must have this callback,
266 * to finish the DMA transaction.
267 */
268 desc->callback = mxs_i2c_dma_irq_callback;
269 desc->callback_param = i2c;
270
271 /* Start the transfer. */
272 dmaengine_submit(desc);
273 dma_async_issue_pending(i2c->dmach);
274 return 0;
275
276/* Read failpath. */
277read_init_dma_fail:
278 dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
279select_init_dma_fail:
280 dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
281select_init_pio_fail:
Marek Vasutc35d3cf2012-11-18 06:25:07 +0100282 dmaengine_terminate_all(i2c->dmach);
Marek Vasut62885f52012-08-24 05:44:31 +0200283 return -EINVAL;
284
285/* Write failpath. */
286write_init_dma_fail:
287 dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
288write_init_pio_fail:
Marek Vasutc35d3cf2012-11-18 06:25:07 +0100289 dmaengine_terminate_all(i2c->dmach);
Marek Vasut62885f52012-08-24 05:44:31 +0200290 return -EINVAL;
291}
292
Marek Vasutfc91e402013-01-24 13:56:21 +0100293static int mxs_i2c_pio_wait_dmareq(struct mxs_i2c_dev *i2c)
294{
295 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
296
297 while (!(readl(i2c->regs + MXS_I2C_DEBUG0) &
298 MXS_I2C_DEBUG0_DMAREQ)) {
299 if (time_after(jiffies, timeout))
300 return -ETIMEDOUT;
301 cond_resched();
302 }
303
Marek Vasutfc91e402013-01-24 13:56:21 +0100304 return 0;
305}
306
Lucas Stach535ebd22013-04-15 00:16:54 +0000307static int mxs_i2c_pio_wait_cplt(struct mxs_i2c_dev *i2c, int last)
Marek Vasutfc91e402013-01-24 13:56:21 +0100308{
309 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
310
311 /*
312 * We do not use interrupts in the PIO mode. Due to the
313 * maximum transfer length being 8 bytes in PIO mode, the
314 * overhead of interrupt would be too large and this would
315 * neglect the gain from using the PIO mode.
316 */
317
318 while (!(readl(i2c->regs + MXS_I2C_CTRL1) &
319 MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ)) {
320 if (time_after(jiffies, timeout))
321 return -ETIMEDOUT;
322 cond_resched();
323 }
324
325 writel(MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ,
326 i2c->regs + MXS_I2C_CTRL1_CLR);
327
Lucas Stach535ebd22013-04-15 00:16:54 +0000328 /*
329 * When ending a transfer with a stop, we have to wait for the bus to
330 * go idle before we report the transfer as completed. Otherwise the
331 * start of the next transfer may race with the end of the current one.
332 */
333 while (last && (readl(i2c->regs + MXS_I2C_STAT) &
334 (MXS_I2C_STAT_BUS_BUSY | MXS_I2C_STAT_CLK_GEN_BUSY))) {
335 if (time_after(jiffies, timeout))
336 return -ETIMEDOUT;
337 cond_resched();
338 }
339
Marek Vasutfc91e402013-01-24 13:56:21 +0100340 return 0;
341}
342
Lucas Stach535ebd22013-04-15 00:16:54 +0000343static void mxs_i2c_pio_trigger_cmd(struct mxs_i2c_dev *i2c, u32 cmd)
344{
345 u32 reg;
346
347 writel(cmd, i2c->regs + MXS_I2C_CTRL0);
348
349 /* readback makes sure the write is latched into hardware */
350 reg = readl(i2c->regs + MXS_I2C_CTRL0);
351 reg |= MXS_I2C_CTRL0_RUN;
352 writel(reg, i2c->regs + MXS_I2C_CTRL0);
353}
354
Marek Vasutfc91e402013-01-24 13:56:21 +0100355static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap,
356 struct i2c_msg *msg, uint32_t flags)
357{
358 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
359 uint32_t addr_data = msg->addr << 1;
360 uint32_t data = 0;
361 int i, shifts_left, ret;
362
363 /* Mute IRQs coming from this block. */
364 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR);
365
366 if (msg->flags & I2C_M_RD) {
367 addr_data |= I2C_SMBUS_READ;
368
369 /* SELECT command. */
Lucas Stach535ebd22013-04-15 00:16:54 +0000370 mxs_i2c_pio_trigger_cmd(i2c, MXS_CMD_I2C_SELECT);
Marek Vasutfc91e402013-01-24 13:56:21 +0100371
372 ret = mxs_i2c_pio_wait_dmareq(i2c);
373 if (ret)
374 return ret;
375
376 writel(addr_data, i2c->regs + MXS_I2C_DATA);
Lucas Stach535ebd22013-04-15 00:16:54 +0000377 writel(MXS_I2C_DEBUG0_DMAREQ, i2c->regs + MXS_I2C_DEBUG0_CLR);
Marek Vasutfc91e402013-01-24 13:56:21 +0100378
Lucas Stach535ebd22013-04-15 00:16:54 +0000379 ret = mxs_i2c_pio_wait_cplt(i2c, 0);
Marek Vasutfc91e402013-01-24 13:56:21 +0100380 if (ret)
381 return ret;
382
383 /* READ command. */
Lucas Stach535ebd22013-04-15 00:16:54 +0000384 mxs_i2c_pio_trigger_cmd(i2c,
385 MXS_CMD_I2C_READ | flags |
386 MXS_I2C_CTRL0_XFER_COUNT(msg->len));
Marek Vasutfc91e402013-01-24 13:56:21 +0100387
388 for (i = 0; i < msg->len; i++) {
389 if ((i & 3) == 0) {
390 ret = mxs_i2c_pio_wait_dmareq(i2c);
391 if (ret)
392 return ret;
393 data = readl(i2c->regs + MXS_I2C_DATA);
Lucas Stach535ebd22013-04-15 00:16:54 +0000394 writel(MXS_I2C_DEBUG0_DMAREQ,
395 i2c->regs + MXS_I2C_DEBUG0_CLR);
Marek Vasutfc91e402013-01-24 13:56:21 +0100396 }
397 msg->buf[i] = data & 0xff;
398 data >>= 8;
399 }
400 } else {
401 addr_data |= I2C_SMBUS_WRITE;
402
403 /* WRITE command. */
Lucas Stach535ebd22013-04-15 00:16:54 +0000404 mxs_i2c_pio_trigger_cmd(i2c,
405 MXS_CMD_I2C_WRITE | flags |
406 MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1));
Marek Vasutfc91e402013-01-24 13:56:21 +0100407
408 /*
409 * The LSB of data buffer is the first byte blasted across
410 * the bus. Higher order bytes follow. Thus the following
411 * filling schematic.
412 */
413 data = addr_data << 24;
414 for (i = 0; i < msg->len; i++) {
415 data >>= 8;
416 data |= (msg->buf[i] << 24);
417 if ((i & 3) == 2) {
418 ret = mxs_i2c_pio_wait_dmareq(i2c);
419 if (ret)
420 return ret;
421 writel(data, i2c->regs + MXS_I2C_DATA);
Lucas Stach535ebd22013-04-15 00:16:54 +0000422 writel(MXS_I2C_DEBUG0_DMAREQ,
423 i2c->regs + MXS_I2C_DEBUG0_CLR);
Marek Vasutfc91e402013-01-24 13:56:21 +0100424 }
425 }
426
427 shifts_left = 24 - (i & 3) * 8;
428 if (shifts_left) {
429 data >>= shifts_left;
430 ret = mxs_i2c_pio_wait_dmareq(i2c);
431 if (ret)
432 return ret;
433 writel(data, i2c->regs + MXS_I2C_DATA);
Lucas Stach535ebd22013-04-15 00:16:54 +0000434 writel(MXS_I2C_DEBUG0_DMAREQ,
435 i2c->regs + MXS_I2C_DEBUG0_CLR);
Marek Vasutfc91e402013-01-24 13:56:21 +0100436 }
437 }
438
Lucas Stach535ebd22013-04-15 00:16:54 +0000439 ret = mxs_i2c_pio_wait_cplt(i2c, flags & MXS_I2C_CTRL0_POST_SEND_STOP);
Marek Vasutfc91e402013-01-24 13:56:21 +0100440 if (ret)
441 return ret;
442
443 /* Clear any dangling IRQs and re-enable interrupts. */
444 writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR);
445 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
446
447 return 0;
448}
449
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100450/*
451 * Low level master read/write transaction.
452 */
453static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
454 int stop)
455{
456 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
457 int ret;
458 int flags;
459
Marek Vasut62885f52012-08-24 05:44:31 +0200460 flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
461
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100462 dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
463 msg->addr, msg->len, msg->flags, stop);
464
465 if (msg->len == 0)
466 return -EINVAL;
467
Marek Vasutfc91e402013-01-24 13:56:21 +0100468 /*
469 * The current boundary to select between PIO/DMA transfer method
470 * is set to 8 bytes, transfers shorter than 8 bytes are transfered
471 * using PIO mode while longer transfers use DMA. The 8 byte border is
472 * based on this empirical measurement and a lot of previous frobbing.
473 */
474 if (msg->len < 8) {
475 ret = mxs_i2c_pio_setup_xfer(adap, msg, flags);
476 if (ret)
477 mxs_i2c_reset(i2c);
478 } else {
479 i2c->cmd_err = 0;
480 INIT_COMPLETION(i2c->cmd_complete);
481 ret = mxs_i2c_dma_setup_xfer(adap, msg, flags);
482 if (ret)
483 return ret;
Wolfram Sang844990d2012-01-13 12:14:26 +0100484
Marek Vasutfc91e402013-01-24 13:56:21 +0100485 ret = wait_for_completion_timeout(&i2c->cmd_complete,
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100486 msecs_to_jiffies(1000));
Marek Vasutfc91e402013-01-24 13:56:21 +0100487 if (ret == 0)
488 goto timeout;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100489
Marek Vasutfc91e402013-01-24 13:56:21 +0100490 if (i2c->cmd_err == -ENXIO)
491 mxs_i2c_reset(i2c);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100492
Marek Vasutfc91e402013-01-24 13:56:21 +0100493 ret = i2c->cmd_err;
494 }
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100495
Marek Vasutfc91e402013-01-24 13:56:21 +0100496 dev_dbg(i2c->dev, "Done with err=%d\n", ret);
497
498 return ret;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100499
500timeout:
501 dev_dbg(i2c->dev, "Timeout!\n");
Wolfram Sang82fa63b2012-10-12 11:55:16 +0100502 mxs_i2c_dma_finish(i2c);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100503 mxs_i2c_reset(i2c);
504 return -ETIMEDOUT;
505}
506
507static int mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
508 int num)
509{
510 int i;
511 int err;
512
513 for (i = 0; i < num; i++) {
514 err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
515 if (err)
516 return err;
517 }
518
519 return num;
520}
521
522static u32 mxs_i2c_func(struct i2c_adapter *adap)
523{
Marek Vasut8f414052012-11-18 06:25:08 +0100524 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100525}
526
527static irqreturn_t mxs_i2c_isr(int this_irq, void *dev_id)
528{
529 struct mxs_i2c_dev *i2c = dev_id;
530 u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK;
531
532 if (!stat)
533 return IRQ_NONE;
534
535 if (stat & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
536 i2c->cmd_err = -ENXIO;
537 else if (stat & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
538 MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
539 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | MXS_I2C_CTRL1_SLAVE_IRQ))
540 /* MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ is only for slaves */
541 i2c->cmd_err = -EIO;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100542
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100543 writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR);
Wolfram Sang844990d2012-01-13 12:14:26 +0100544
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100545 return IRQ_HANDLED;
546}
547
548static const struct i2c_algorithm mxs_i2c_algo = {
549 .master_xfer = mxs_i2c_xfer,
550 .functionality = mxs_i2c_func,
551};
552
Marek Vasut62885f52012-08-24 05:44:31 +0200553static bool mxs_i2c_dma_filter(struct dma_chan *chan, void *param)
554{
555 struct mxs_i2c_dev *i2c = param;
556
557 if (!mxs_dma_is_apbx(chan))
558 return false;
559
560 if (chan->chan_id != i2c->dma_channel)
561 return false;
562
563 chan->private = &i2c->dma_data;
564
565 return true;
566}
567
Marek Vasut626f0a22012-11-30 18:48:35 +0100568static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, int speed)
569{
570 /* The I2C block clock run at 24MHz */
571 const uint32_t clk = 24000000;
572 uint32_t base;
573 uint16_t high_count, low_count, rcv_count, xmit_count;
574 struct device *dev = i2c->dev;
575
576 if (speed > 540000) {
577 dev_warn(dev, "Speed too high (%d Hz), using 540 kHz\n", speed);
578 speed = 540000;
579 } else if (speed < 12000) {
580 dev_warn(dev, "Speed too low (%d Hz), using 12 kHz\n", speed);
581 speed = 12000;
582 }
583
584 /*
585 * The timing derivation algorithm. There is no documentation for this
586 * algorithm available, it was derived by using the scope and fiddling
587 * with constants until the result observed on the scope was good enough
588 * for 20kHz, 50kHz, 100kHz, 200kHz, 300kHz and 400kHz. It should be
589 * possible to assume the algorithm works for other frequencies as well.
590 *
591 * Note it was necessary to cap the frequency on both ends as it's not
592 * possible to configure completely arbitrary frequency for the I2C bus
593 * clock.
594 */
595 base = ((clk / speed) - 38) / 2;
596 high_count = base + 3;
597 low_count = base - 3;
598 rcv_count = (high_count * 3) / 4;
599 xmit_count = low_count / 4;
600
601 i2c->timing0 = (high_count << 16) | rcv_count;
602 i2c->timing1 = (low_count << 16) | xmit_count;
603}
604
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200605static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c)
606{
607 uint32_t speed;
608 struct device *dev = i2c->dev;
609 struct device_node *node = dev->of_node;
610 int ret;
611
Marek Vasut62885f52012-08-24 05:44:31 +0200612 /*
Marek Vasut62885f52012-08-24 05:44:31 +0200613 * TODO: This is a temporary solution and should be changed
614 * to use generic DMA binding later when the helpers get in.
615 */
616 ret = of_property_read_u32(node, "fsl,i2c-dma-channel",
617 &i2c->dma_channel);
618 if (ret) {
Wolfram Sang82fa63b2012-10-12 11:55:16 +0100619 dev_err(dev, "Failed to get DMA channel!\n");
620 return -ENODEV;
Marek Vasut62885f52012-08-24 05:44:31 +0200621 }
622
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200623 ret = of_property_read_u32(node, "clock-frequency", &speed);
Marek Vasut626f0a22012-11-30 18:48:35 +0100624 if (ret) {
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200625 dev_warn(dev, "No I2C speed selected, using 100kHz\n");
Marek Vasut626f0a22012-11-30 18:48:35 +0100626 speed = 100000;
627 }
628
629 mxs_i2c_derive_timing(i2c, speed);
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200630
631 return 0;
632}
633
Bill Pemberton0b255e92012-11-27 15:59:38 -0500634static int mxs_i2c_probe(struct platform_device *pdev)
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100635{
636 struct device *dev = &pdev->dev;
637 struct mxs_i2c_dev *i2c;
638 struct i2c_adapter *adap;
Shawn Guod98d0332012-05-06 22:59:45 +0800639 struct pinctrl *pinctrl;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100640 struct resource *res;
641 resource_size_t res_size;
Marek Vasut62885f52012-08-24 05:44:31 +0200642 int err, irq, dmairq;
643 dma_cap_mask_t mask;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100644
Shawn Guod98d0332012-05-06 22:59:45 +0800645 pinctrl = devm_pinctrl_get_select_default(dev);
646 if (IS_ERR(pinctrl))
647 return PTR_ERR(pinctrl);
648
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100649 i2c = devm_kzalloc(dev, sizeof(struct mxs_i2c_dev), GFP_KERNEL);
650 if (!i2c)
651 return -ENOMEM;
652
653 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Marek Vasut62885f52012-08-24 05:44:31 +0200654 irq = platform_get_irq(pdev, 0);
655 dmairq = platform_get_irq(pdev, 1);
656
657 if (!res || irq < 0 || dmairq < 0)
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100658 return -ENOENT;
659
660 res_size = resource_size(res);
661 if (!devm_request_mem_region(dev, res->start, res_size, res->name))
662 return -EBUSY;
663
664 i2c->regs = devm_ioremap_nocache(dev, res->start, res_size);
665 if (!i2c->regs)
666 return -EBUSY;
667
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100668 err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
669 if (err)
670 return err;
671
672 i2c->dev = dev;
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200673
Marek Vasut85de7fa2012-11-21 06:19:06 +0100674 init_completion(&i2c->cmd_complete);
675
Wolfram Sang72ee7342012-09-08 17:28:06 +0200676 if (dev->of_node) {
677 err = mxs_i2c_get_ofdata(i2c);
678 if (err)
679 return err;
680 }
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200681
Marek Vasut62885f52012-08-24 05:44:31 +0200682 /* Setup the DMA */
Wolfram Sang82fa63b2012-10-12 11:55:16 +0100683 dma_cap_zero(mask);
684 dma_cap_set(DMA_SLAVE, mask);
685 i2c->dma_data.chan_irq = dmairq;
686 i2c->dmach = dma_request_channel(mask, mxs_i2c_dma_filter, i2c);
687 if (!i2c->dmach) {
688 dev_err(dev, "Failed to request dma\n");
689 return -ENODEV;
Marek Vasut62885f52012-08-24 05:44:31 +0200690 }
691
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100692 platform_set_drvdata(pdev, i2c);
693
694 /* Do reset to enforce correct startup after pinmuxing */
695 mxs_i2c_reset(i2c);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100696
697 adap = &i2c->adapter;
698 strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
699 adap->owner = THIS_MODULE;
700 adap->algo = &mxs_i2c_algo;
701 adap->dev.parent = dev;
702 adap->nr = pdev->id;
Shawn Guob2378662012-05-12 13:43:32 +0800703 adap->dev.of_node = pdev->dev.of_node;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100704 i2c_set_adapdata(adap, i2c);
705 err = i2c_add_numbered_adapter(adap);
706 if (err) {
707 dev_err(dev, "Failed to add adapter (%d)\n", err);
708 writel(MXS_I2C_CTRL0_SFTRST,
709 i2c->regs + MXS_I2C_CTRL0_SET);
710 return err;
711 }
712
Shawn Guob2378662012-05-12 13:43:32 +0800713 of_i2c_register_devices(adap);
714
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100715 return 0;
716}
717
Bill Pemberton0b255e92012-11-27 15:59:38 -0500718static int mxs_i2c_remove(struct platform_device *pdev)
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100719{
720 struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100721
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000722 i2c_del_adapter(&i2c->adapter);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100723
Marek Vasut62885f52012-08-24 05:44:31 +0200724 if (i2c->dmach)
725 dma_release_channel(i2c->dmach);
726
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100727 writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
728
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100729 return 0;
730}
731
Shawn Guob2378662012-05-12 13:43:32 +0800732static const struct of_device_id mxs_i2c_dt_ids[] = {
733 { .compatible = "fsl,imx28-i2c", },
734 { /* sentinel */ }
735};
736MODULE_DEVICE_TABLE(of, mxs_i2c_dt_ids);
737
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100738static struct platform_driver mxs_i2c_driver = {
739 .driver = {
740 .name = DRIVER_NAME,
741 .owner = THIS_MODULE,
Shawn Guob2378662012-05-12 13:43:32 +0800742 .of_match_table = mxs_i2c_dt_ids,
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100743 },
Bill Pemberton0b255e92012-11-27 15:59:38 -0500744 .remove = mxs_i2c_remove,
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100745};
746
747static int __init mxs_i2c_init(void)
748{
749 return platform_driver_probe(&mxs_i2c_driver, mxs_i2c_probe);
750}
751subsys_initcall(mxs_i2c_init);
752
753static void __exit mxs_i2c_exit(void)
754{
755 platform_driver_unregister(&mxs_i2c_driver);
756}
757module_exit(mxs_i2c_exit);
758
759MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
760MODULE_DESCRIPTION("MXS I2C Bus Driver");
761MODULE_LICENSE("GPL");
762MODULE_ALIAS("platform:" DRIVER_NAME);