blob: 2cb0317b14fb094e0a6645d304c7af4af7094fbd [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>
Wolfram Sang6b866c12011-08-31 20:37:50 +020027#include <linux/stmp_device.h>
Shawn Guob2378662012-05-12 13:43:32 +080028#include <linux/of.h>
29#include <linux/of_device.h>
Marek Vasut62885f52012-08-24 05:44:31 +020030#include <linux/dma-mapping.h>
31#include <linux/dmaengine.h>
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010032
33#define DRIVER_NAME "mxs-i2c"
34
35#define MXS_I2C_CTRL0 (0x00)
36#define MXS_I2C_CTRL0_SET (0x04)
37
38#define MXS_I2C_CTRL0_SFTRST 0x80000000
Marek Vasutfc91e402013-01-24 13:56:21 +010039#define MXS_I2C_CTRL0_RUN 0x20000000
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010040#define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
41#define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
42#define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
43#define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
44#define MXS_I2C_CTRL0_MASTER_MODE 0x00020000
45#define MXS_I2C_CTRL0_DIRECTION 0x00010000
46#define MXS_I2C_CTRL0_XFER_COUNT(v) ((v) & 0x0000FFFF)
47
Marek Vasutcd4f2d42012-07-09 18:22:53 +020048#define MXS_I2C_TIMING0 (0x10)
49#define MXS_I2C_TIMING1 (0x20)
50#define MXS_I2C_TIMING2 (0x30)
51
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010052#define MXS_I2C_CTRL1 (0x40)
53#define MXS_I2C_CTRL1_SET (0x44)
54#define MXS_I2C_CTRL1_CLR (0x48)
55
Lucas Stach92b775c2013-04-15 00:16:55 +000056#define MXS_I2C_CTRL1_CLR_GOT_A_NAK 0x10000000
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010057#define MXS_I2C_CTRL1_BUS_FREE_IRQ 0x80
58#define MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ 0x40
59#define MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ 0x20
60#define MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ 0x10
61#define MXS_I2C_CTRL1_EARLY_TERM_IRQ 0x08
62#define MXS_I2C_CTRL1_MASTER_LOSS_IRQ 0x04
63#define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
64#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
65
Lucas Stach535ebd22013-04-15 00:16:54 +000066#define MXS_I2C_STAT (0x50)
67#define MXS_I2C_STAT_BUS_BUSY 0x00000800
68#define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400
69
Marek Vasutfc91e402013-01-24 13:56:21 +010070#define MXS_I2C_DATA (0xa0)
71
72#define MXS_I2C_DEBUG0 (0xb0)
73#define MXS_I2C_DEBUG0_CLR (0xb8)
74
75#define MXS_I2C_DEBUG0_DMAREQ 0x80000000
76
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010077#define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
78 MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
79 MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
80 MXS_I2C_CTRL1_MASTER_LOSS_IRQ | \
81 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | \
82 MXS_I2C_CTRL1_SLAVE_IRQ)
83
Wolfram Sanga8da7fe2011-02-16 13:39:16 +010084
85#define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
86 MXS_I2C_CTRL0_PRE_SEND_START | \
87 MXS_I2C_CTRL0_MASTER_MODE | \
88 MXS_I2C_CTRL0_DIRECTION | \
89 MXS_I2C_CTRL0_XFER_COUNT(1))
90
91#define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
92 MXS_I2C_CTRL0_MASTER_MODE | \
93 MXS_I2C_CTRL0_DIRECTION)
94
95#define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
96 MXS_I2C_CTRL0_MASTER_MODE)
97
Juergen Beisert616228a2013-09-30 01:23:53 +020098enum mxs_i2c_devtype {
99 MXS_I2C_UNKNOWN = 0,
100 MXS_I2C_V1,
101 MXS_I2C_V2,
102};
103
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100104/**
105 * struct mxs_i2c_dev - per device, private MXS-I2C data
106 *
107 * @dev: driver model device node
Juergen Beisert616228a2013-09-30 01:23:53 +0200108 * @dev_type: distinguish i.MX23/i.MX28 features
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100109 * @regs: IO registers pointer
110 * @cmd_complete: completion object for transaction wait
111 * @cmd_err: error code for last transaction
112 * @adapter: i2c subsystem adapter node
113 */
114struct mxs_i2c_dev {
115 struct device *dev;
Juergen Beisert616228a2013-09-30 01:23:53 +0200116 enum mxs_i2c_devtype dev_type;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100117 void __iomem *regs;
118 struct completion cmd_complete;
Fabio Estevam0f40cbc2013-01-07 22:32:06 -0200119 int cmd_err;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100120 struct i2c_adapter adapter;
Marek Vasut626f0a22012-11-30 18:48:35 +0100121
122 uint32_t timing0;
123 uint32_t timing1;
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200124 uint32_t timing2;
Marek Vasut62885f52012-08-24 05:44:31 +0200125
126 /* DMA support components */
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200127 struct dma_chan *dmach;
Marek Vasut62885f52012-08-24 05:44:31 +0200128 uint32_t pio_data[2];
129 uint32_t addr_data;
130 struct scatterlist sg_io[2];
131 bool dma_read;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100132};
133
Fabio Estevam63151c52013-07-10 22:19:28 -0300134static int mxs_i2c_reset(struct mxs_i2c_dev *i2c)
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100135{
Fabio Estevam63151c52013-07-10 22:19:28 -0300136 int ret = stmp_reset_block(i2c->regs);
137 if (ret)
138 return ret;
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200139
Marek Vasut626f0a22012-11-30 18:48:35 +0100140 /*
141 * Configure timing for the I2C block. The I2C TIMING2 register has to
142 * be programmed with this particular magic number. The rest is derived
143 * from the XTAL speed and requested I2C speed.
144 *
145 * For details, see i.MX233 [25.4.2 - 25.4.4] and i.MX28 [27.5.2 - 27.5.4].
146 */
147 writel(i2c->timing0, i2c->regs + MXS_I2C_TIMING0);
148 writel(i2c->timing1, i2c->regs + MXS_I2C_TIMING1);
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200149 writel(i2c->timing2, i2c->regs + MXS_I2C_TIMING2);
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200150
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100151 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
Fabio Estevam63151c52013-07-10 22:19:28 -0300152
153 return 0;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100154}
155
Marek Vasut62885f52012-08-24 05:44:31 +0200156static void mxs_i2c_dma_finish(struct mxs_i2c_dev *i2c)
157{
158 if (i2c->dma_read) {
159 dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
160 dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
161 } else {
162 dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
163 }
164}
165
166static void mxs_i2c_dma_irq_callback(void *param)
167{
168 struct mxs_i2c_dev *i2c = param;
169
170 complete(&i2c->cmd_complete);
171 mxs_i2c_dma_finish(i2c);
172}
173
174static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap,
175 struct i2c_msg *msg, uint32_t flags)
176{
177 struct dma_async_tx_descriptor *desc;
178 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
179
180 if (msg->flags & I2C_M_RD) {
181 i2c->dma_read = 1;
182 i2c->addr_data = (msg->addr << 1) | I2C_SMBUS_READ;
183
184 /*
185 * SELECT command.
186 */
187
188 /* Queue the PIO register write transfer. */
189 i2c->pio_data[0] = MXS_CMD_I2C_SELECT;
190 desc = dmaengine_prep_slave_sg(i2c->dmach,
191 (struct scatterlist *)&i2c->pio_data[0],
192 1, DMA_TRANS_NONE, 0);
193 if (!desc) {
194 dev_err(i2c->dev,
195 "Failed to get PIO reg. write descriptor.\n");
196 goto select_init_pio_fail;
197 }
198
199 /* Queue the DMA data transfer. */
200 sg_init_one(&i2c->sg_io[0], &i2c->addr_data, 1);
201 dma_map_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
202 desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[0], 1,
203 DMA_MEM_TO_DEV,
204 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
205 if (!desc) {
206 dev_err(i2c->dev,
207 "Failed to get DMA data write descriptor.\n");
208 goto select_init_dma_fail;
209 }
210
211 /*
212 * READ command.
213 */
214
215 /* Queue the PIO register write transfer. */
216 i2c->pio_data[1] = flags | MXS_CMD_I2C_READ |
217 MXS_I2C_CTRL0_XFER_COUNT(msg->len);
218 desc = dmaengine_prep_slave_sg(i2c->dmach,
219 (struct scatterlist *)&i2c->pio_data[1],
220 1, DMA_TRANS_NONE, DMA_PREP_INTERRUPT);
221 if (!desc) {
222 dev_err(i2c->dev,
223 "Failed to get PIO reg. write descriptor.\n");
224 goto select_init_dma_fail;
225 }
226
227 /* Queue the DMA data transfer. */
228 sg_init_one(&i2c->sg_io[1], msg->buf, msg->len);
229 dma_map_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
230 desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[1], 1,
231 DMA_DEV_TO_MEM,
232 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
233 if (!desc) {
234 dev_err(i2c->dev,
235 "Failed to get DMA data write descriptor.\n");
236 goto read_init_dma_fail;
237 }
238 } else {
239 i2c->dma_read = 0;
240 i2c->addr_data = (msg->addr << 1) | I2C_SMBUS_WRITE;
241
242 /*
243 * WRITE command.
244 */
245
246 /* Queue the PIO register write transfer. */
247 i2c->pio_data[0] = flags | MXS_CMD_I2C_WRITE |
248 MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1);
249 desc = dmaengine_prep_slave_sg(i2c->dmach,
250 (struct scatterlist *)&i2c->pio_data[0],
251 1, DMA_TRANS_NONE, 0);
252 if (!desc) {
253 dev_err(i2c->dev,
254 "Failed to get PIO reg. write descriptor.\n");
255 goto write_init_pio_fail;
256 }
257
258 /* Queue the DMA data transfer. */
259 sg_init_table(i2c->sg_io, 2);
260 sg_set_buf(&i2c->sg_io[0], &i2c->addr_data, 1);
261 sg_set_buf(&i2c->sg_io[1], msg->buf, msg->len);
262 dma_map_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
263 desc = dmaengine_prep_slave_sg(i2c->dmach, i2c->sg_io, 2,
264 DMA_MEM_TO_DEV,
265 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
266 if (!desc) {
267 dev_err(i2c->dev,
268 "Failed to get DMA data write descriptor.\n");
269 goto write_init_dma_fail;
270 }
271 }
272
273 /*
274 * The last descriptor must have this callback,
275 * to finish the DMA transaction.
276 */
277 desc->callback = mxs_i2c_dma_irq_callback;
278 desc->callback_param = i2c;
279
280 /* Start the transfer. */
281 dmaengine_submit(desc);
282 dma_async_issue_pending(i2c->dmach);
283 return 0;
284
285/* Read failpath. */
286read_init_dma_fail:
287 dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
288select_init_dma_fail:
289 dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
290select_init_pio_fail:
Marek Vasutc35d3cf2012-11-18 06:25:07 +0100291 dmaengine_terminate_all(i2c->dmach);
Marek Vasut62885f52012-08-24 05:44:31 +0200292 return -EINVAL;
293
294/* Write failpath. */
295write_init_dma_fail:
296 dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
297write_init_pio_fail:
Marek Vasutc35d3cf2012-11-18 06:25:07 +0100298 dmaengine_terminate_all(i2c->dmach);
Marek Vasut62885f52012-08-24 05:44:31 +0200299 return -EINVAL;
300}
301
Marek Vasutfc91e402013-01-24 13:56:21 +0100302static int mxs_i2c_pio_wait_dmareq(struct mxs_i2c_dev *i2c)
303{
304 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
305
306 while (!(readl(i2c->regs + MXS_I2C_DEBUG0) &
307 MXS_I2C_DEBUG0_DMAREQ)) {
308 if (time_after(jiffies, timeout))
309 return -ETIMEDOUT;
310 cond_resched();
311 }
312
Marek Vasutfc91e402013-01-24 13:56:21 +0100313 return 0;
314}
315
Lucas Stach535ebd22013-04-15 00:16:54 +0000316static int mxs_i2c_pio_wait_cplt(struct mxs_i2c_dev *i2c, int last)
Marek Vasutfc91e402013-01-24 13:56:21 +0100317{
318 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
319
320 /*
321 * We do not use interrupts in the PIO mode. Due to the
322 * maximum transfer length being 8 bytes in PIO mode, the
323 * overhead of interrupt would be too large and this would
324 * neglect the gain from using the PIO mode.
325 */
326
327 while (!(readl(i2c->regs + MXS_I2C_CTRL1) &
328 MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ)) {
329 if (time_after(jiffies, timeout))
330 return -ETIMEDOUT;
331 cond_resched();
332 }
333
334 writel(MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ,
335 i2c->regs + MXS_I2C_CTRL1_CLR);
336
Lucas Stach535ebd22013-04-15 00:16:54 +0000337 /*
338 * When ending a transfer with a stop, we have to wait for the bus to
339 * go idle before we report the transfer as completed. Otherwise the
340 * start of the next transfer may race with the end of the current one.
341 */
342 while (last && (readl(i2c->regs + MXS_I2C_STAT) &
343 (MXS_I2C_STAT_BUS_BUSY | MXS_I2C_STAT_CLK_GEN_BUSY))) {
344 if (time_after(jiffies, timeout))
345 return -ETIMEDOUT;
346 cond_resched();
347 }
348
Marek Vasutfc91e402013-01-24 13:56:21 +0100349 return 0;
350}
351
Lucas Stach92b775c2013-04-15 00:16:55 +0000352static int mxs_i2c_pio_check_error_state(struct mxs_i2c_dev *i2c)
353{
354 u32 state;
355
356 state = readl(i2c->regs + MXS_I2C_CTRL1_CLR) & MXS_I2C_IRQ_MASK;
357
358 if (state & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
359 i2c->cmd_err = -ENXIO;
360 else if (state & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
361 MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
362 MXS_I2C_CTRL1_SLAVE_STOP_IRQ |
363 MXS_I2C_CTRL1_SLAVE_IRQ))
364 i2c->cmd_err = -EIO;
365
366 return i2c->cmd_err;
367}
368
Lucas Stach535ebd22013-04-15 00:16:54 +0000369static void mxs_i2c_pio_trigger_cmd(struct mxs_i2c_dev *i2c, u32 cmd)
370{
371 u32 reg;
372
373 writel(cmd, i2c->regs + MXS_I2C_CTRL0);
374
375 /* readback makes sure the write is latched into hardware */
376 reg = readl(i2c->regs + MXS_I2C_CTRL0);
377 reg |= MXS_I2C_CTRL0_RUN;
378 writel(reg, i2c->regs + MXS_I2C_CTRL0);
379}
380
Marek Vasutfc91e402013-01-24 13:56:21 +0100381static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap,
382 struct i2c_msg *msg, uint32_t flags)
383{
384 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
385 uint32_t addr_data = msg->addr << 1;
386 uint32_t data = 0;
387 int i, shifts_left, ret;
388
389 /* Mute IRQs coming from this block. */
390 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR);
391
392 if (msg->flags & I2C_M_RD) {
393 addr_data |= I2C_SMBUS_READ;
394
395 /* SELECT command. */
Lucas Stach535ebd22013-04-15 00:16:54 +0000396 mxs_i2c_pio_trigger_cmd(i2c, MXS_CMD_I2C_SELECT);
Marek Vasutfc91e402013-01-24 13:56:21 +0100397
398 ret = mxs_i2c_pio_wait_dmareq(i2c);
399 if (ret)
400 return ret;
401
402 writel(addr_data, i2c->regs + MXS_I2C_DATA);
Lucas Stach535ebd22013-04-15 00:16:54 +0000403 writel(MXS_I2C_DEBUG0_DMAREQ, i2c->regs + MXS_I2C_DEBUG0_CLR);
Marek Vasutfc91e402013-01-24 13:56:21 +0100404
Lucas Stach535ebd22013-04-15 00:16:54 +0000405 ret = mxs_i2c_pio_wait_cplt(i2c, 0);
Marek Vasutfc91e402013-01-24 13:56:21 +0100406 if (ret)
407 return ret;
408
Lucas Stach92b775c2013-04-15 00:16:55 +0000409 if (mxs_i2c_pio_check_error_state(i2c))
410 goto cleanup;
411
Marek Vasutfc91e402013-01-24 13:56:21 +0100412 /* READ command. */
Lucas Stach535ebd22013-04-15 00:16:54 +0000413 mxs_i2c_pio_trigger_cmd(i2c,
414 MXS_CMD_I2C_READ | flags |
415 MXS_I2C_CTRL0_XFER_COUNT(msg->len));
Marek Vasutfc91e402013-01-24 13:56:21 +0100416
417 for (i = 0; i < msg->len; i++) {
418 if ((i & 3) == 0) {
419 ret = mxs_i2c_pio_wait_dmareq(i2c);
420 if (ret)
421 return ret;
422 data = readl(i2c->regs + MXS_I2C_DATA);
Lucas Stach535ebd22013-04-15 00:16:54 +0000423 writel(MXS_I2C_DEBUG0_DMAREQ,
424 i2c->regs + MXS_I2C_DEBUG0_CLR);
Marek Vasutfc91e402013-01-24 13:56:21 +0100425 }
426 msg->buf[i] = data & 0xff;
427 data >>= 8;
428 }
429 } else {
430 addr_data |= I2C_SMBUS_WRITE;
431
432 /* WRITE command. */
Lucas Stach535ebd22013-04-15 00:16:54 +0000433 mxs_i2c_pio_trigger_cmd(i2c,
434 MXS_CMD_I2C_WRITE | flags |
435 MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1));
Marek Vasutfc91e402013-01-24 13:56:21 +0100436
437 /*
438 * The LSB of data buffer is the first byte blasted across
439 * the bus. Higher order bytes follow. Thus the following
440 * filling schematic.
441 */
442 data = addr_data << 24;
443 for (i = 0; i < msg->len; i++) {
444 data >>= 8;
445 data |= (msg->buf[i] << 24);
446 if ((i & 3) == 2) {
447 ret = mxs_i2c_pio_wait_dmareq(i2c);
448 if (ret)
449 return ret;
450 writel(data, i2c->regs + MXS_I2C_DATA);
Lucas Stach535ebd22013-04-15 00:16:54 +0000451 writel(MXS_I2C_DEBUG0_DMAREQ,
452 i2c->regs + MXS_I2C_DEBUG0_CLR);
Marek Vasutfc91e402013-01-24 13:56:21 +0100453 }
454 }
455
456 shifts_left = 24 - (i & 3) * 8;
457 if (shifts_left) {
458 data >>= shifts_left;
459 ret = mxs_i2c_pio_wait_dmareq(i2c);
460 if (ret)
461 return ret;
462 writel(data, i2c->regs + MXS_I2C_DATA);
Lucas Stach535ebd22013-04-15 00:16:54 +0000463 writel(MXS_I2C_DEBUG0_DMAREQ,
464 i2c->regs + MXS_I2C_DEBUG0_CLR);
Marek Vasutfc91e402013-01-24 13:56:21 +0100465 }
466 }
467
Lucas Stach535ebd22013-04-15 00:16:54 +0000468 ret = mxs_i2c_pio_wait_cplt(i2c, flags & MXS_I2C_CTRL0_POST_SEND_STOP);
Marek Vasutfc91e402013-01-24 13:56:21 +0100469 if (ret)
470 return ret;
471
Lucas Stach92b775c2013-04-15 00:16:55 +0000472 /* make sure we capture any occurred error into cmd_err */
473 mxs_i2c_pio_check_error_state(i2c);
474
475cleanup:
Marek Vasutfc91e402013-01-24 13:56:21 +0100476 /* Clear any dangling IRQs and re-enable interrupts. */
477 writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR);
478 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
479
480 return 0;
481}
482
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100483/*
484 * Low level master read/write transaction.
485 */
486static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
487 int stop)
488{
489 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
Fabio Estevam63151c52013-07-10 22:19:28 -0300490 int ret, err;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100491 int flags;
492
Marek Vasut62885f52012-08-24 05:44:31 +0200493 flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
494
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100495 dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
496 msg->addr, msg->len, msg->flags, stop);
497
498 if (msg->len == 0)
499 return -EINVAL;
500
Marek Vasutfc91e402013-01-24 13:56:21 +0100501 /*
502 * The current boundary to select between PIO/DMA transfer method
503 * is set to 8 bytes, transfers shorter than 8 bytes are transfered
504 * using PIO mode while longer transfers use DMA. The 8 byte border is
505 * based on this empirical measurement and a lot of previous frobbing.
Juergen Beisert616228a2013-09-30 01:23:53 +0200506 * Note: this special feature only works on i.MX28 SoC
Marek Vasutfc91e402013-01-24 13:56:21 +0100507 */
Lucas Stach92b775c2013-04-15 00:16:55 +0000508 i2c->cmd_err = 0;
Fabio Estevamd6e102f2013-07-01 18:14:21 -0300509 if (0) { /* disable PIO mode until a proper fix is made */
Marek Vasutfc91e402013-01-24 13:56:21 +0100510 ret = mxs_i2c_pio_setup_xfer(adap, msg, flags);
Fabio Estevam63151c52013-07-10 22:19:28 -0300511 if (ret) {
512 err = mxs_i2c_reset(i2c);
513 if (err)
514 return err;
515 }
Marek Vasutfc91e402013-01-24 13:56:21 +0100516 } else {
Marek Vasutfc91e402013-01-24 13:56:21 +0100517 INIT_COMPLETION(i2c->cmd_complete);
518 ret = mxs_i2c_dma_setup_xfer(adap, msg, flags);
519 if (ret)
520 return ret;
Wolfram Sang844990d2012-01-13 12:14:26 +0100521
Marek Vasutfc91e402013-01-24 13:56:21 +0100522 ret = wait_for_completion_timeout(&i2c->cmd_complete,
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100523 msecs_to_jiffies(1000));
Marek Vasutfc91e402013-01-24 13:56:21 +0100524 if (ret == 0)
525 goto timeout;
Marek Vasutfc91e402013-01-24 13:56:21 +0100526 }
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100527
Lucas Stach92b775c2013-04-15 00:16:55 +0000528 if (i2c->cmd_err == -ENXIO) {
529 /*
530 * If the transfer fails with a NAK from the slave the
531 * controller halts until it gets told to return to idle state.
532 */
533 writel(MXS_I2C_CTRL1_CLR_GOT_A_NAK,
534 i2c->regs + MXS_I2C_CTRL1_SET);
535 }
536
537 ret = i2c->cmd_err;
538
Marek Vasutfc91e402013-01-24 13:56:21 +0100539 dev_dbg(i2c->dev, "Done with err=%d\n", ret);
540
541 return ret;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100542
543timeout:
544 dev_dbg(i2c->dev, "Timeout!\n");
Wolfram Sang82fa63b2012-10-12 11:55:16 +0100545 mxs_i2c_dma_finish(i2c);
Fabio Estevam63151c52013-07-10 22:19:28 -0300546 ret = mxs_i2c_reset(i2c);
547 if (ret)
548 return ret;
549
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100550 return -ETIMEDOUT;
551}
552
553static int mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
554 int num)
555{
556 int i;
557 int err;
558
559 for (i = 0; i < num; i++) {
560 err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
561 if (err)
562 return err;
563 }
564
565 return num;
566}
567
568static u32 mxs_i2c_func(struct i2c_adapter *adap)
569{
Marek Vasut8f414052012-11-18 06:25:08 +0100570 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100571}
572
573static irqreturn_t mxs_i2c_isr(int this_irq, void *dev_id)
574{
575 struct mxs_i2c_dev *i2c = dev_id;
576 u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK;
577
578 if (!stat)
579 return IRQ_NONE;
580
581 if (stat & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
582 i2c->cmd_err = -ENXIO;
583 else if (stat & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
584 MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
585 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | MXS_I2C_CTRL1_SLAVE_IRQ))
586 /* MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ is only for slaves */
587 i2c->cmd_err = -EIO;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100588
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100589 writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR);
Wolfram Sang844990d2012-01-13 12:14:26 +0100590
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100591 return IRQ_HANDLED;
592}
593
594static const struct i2c_algorithm mxs_i2c_algo = {
595 .master_xfer = mxs_i2c_xfer,
596 .functionality = mxs_i2c_func,
597};
598
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200599static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, uint32_t speed)
Marek Vasut626f0a22012-11-30 18:48:35 +0100600{
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200601 /* The I2C block clock runs at 24MHz */
Marek Vasut626f0a22012-11-30 18:48:35 +0100602 const uint32_t clk = 24000000;
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200603 uint32_t divider;
Marek Vasut626f0a22012-11-30 18:48:35 +0100604 uint16_t high_count, low_count, rcv_count, xmit_count;
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200605 uint32_t bus_free, leadin;
Marek Vasut626f0a22012-11-30 18:48:35 +0100606 struct device *dev = i2c->dev;
607
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200608 divider = DIV_ROUND_UP(clk, speed);
609
610 if (divider < 25) {
611 /*
612 * limit the divider, so that min(low_count, high_count)
613 * is >= 1
614 */
615 divider = 25;
616 dev_warn(dev,
617 "Speed too high (%u.%03u kHz), using %u.%03u kHz\n",
618 speed / 1000, speed % 1000,
619 clk / divider / 1000, clk / divider % 1000);
620 } else if (divider > 1897) {
621 /*
622 * limit the divider, so that max(low_count, high_count)
623 * cannot exceed 1023
624 */
625 divider = 1897;
626 dev_warn(dev,
627 "Speed too low (%u.%03u kHz), using %u.%03u kHz\n",
628 speed / 1000, speed % 1000,
629 clk / divider / 1000, clk / divider % 1000);
Marek Vasut626f0a22012-11-30 18:48:35 +0100630 }
631
632 /*
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200633 * The I2C spec specifies the following timing data:
634 * standard mode fast mode Bitfield name
635 * tLOW (SCL LOW period) 4700 ns 1300 ns
636 * tHIGH (SCL HIGH period) 4000 ns 600 ns
637 * tSU;DAT (data setup time) 250 ns 100 ns
638 * tHD;STA (START hold time) 4000 ns 600 ns
639 * tBUF (bus free time) 4700 ns 1300 ns
Marek Vasut626f0a22012-11-30 18:48:35 +0100640 *
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200641 * The hardware (of the i.MX28 at least) seems to add 2 additional
642 * clock cycles to the low_count and 7 cycles to the high_count.
643 * This is compensated for by subtracting the respective constants
644 * from the values written to the timing registers.
Marek Vasut626f0a22012-11-30 18:48:35 +0100645 */
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200646 if (speed > 100000) {
647 /* fast mode */
648 low_count = DIV_ROUND_CLOSEST(divider * 13, (13 + 6));
649 high_count = DIV_ROUND_CLOSEST(divider * 6, (13 + 6));
650 leadin = DIV_ROUND_UP(600 * (clk / 1000000), 1000);
651 bus_free = DIV_ROUND_UP(1300 * (clk / 1000000), 1000);
652 } else {
653 /* normal mode */
654 low_count = DIV_ROUND_CLOSEST(divider * 47, (47 + 40));
655 high_count = DIV_ROUND_CLOSEST(divider * 40, (47 + 40));
656 leadin = DIV_ROUND_UP(4700 * (clk / 1000000), 1000);
657 bus_free = DIV_ROUND_UP(4700 * (clk / 1000000), 1000);
658 }
659 rcv_count = high_count * 3 / 8;
660 xmit_count = low_count * 3 / 8;
Marek Vasut626f0a22012-11-30 18:48:35 +0100661
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200662 dev_dbg(dev,
663 "speed=%u(actual %u) divider=%u low=%u high=%u xmit=%u rcv=%u leadin=%u bus_free=%u\n",
664 speed, clk / divider, divider, low_count, high_count,
665 xmit_count, rcv_count, leadin, bus_free);
666
667 low_count -= 2;
668 high_count -= 7;
Marek Vasut626f0a22012-11-30 18:48:35 +0100669 i2c->timing0 = (high_count << 16) | rcv_count;
670 i2c->timing1 = (low_count << 16) | xmit_count;
Lothar Waßmann869c6a32013-07-05 18:28:00 +0200671 i2c->timing2 = (bus_free << 16 | leadin);
Marek Vasut626f0a22012-11-30 18:48:35 +0100672}
673
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200674static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c)
675{
676 uint32_t speed;
677 struct device *dev = i2c->dev;
678 struct device_node *node = dev->of_node;
679 int ret;
680
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200681 ret = of_property_read_u32(node, "clock-frequency", &speed);
Marek Vasut626f0a22012-11-30 18:48:35 +0100682 if (ret) {
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200683 dev_warn(dev, "No I2C speed selected, using 100kHz\n");
Marek Vasut626f0a22012-11-30 18:48:35 +0100684 speed = 100000;
685 }
686
687 mxs_i2c_derive_timing(i2c, speed);
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200688
689 return 0;
690}
691
Juergen Beisert616228a2013-09-30 01:23:53 +0200692static struct platform_device_id mxs_i2c_devtype[] = {
693 {
694 .name = "imx23-i2c",
695 .driver_data = MXS_I2C_V1,
696 }, {
697 .name = "imx28-i2c",
698 .driver_data = MXS_I2C_V2,
699 }, { /* sentinel */ }
700};
701MODULE_DEVICE_TABLE(platform, mxs_i2c_devtype);
702
703static const struct of_device_id mxs_i2c_dt_ids[] = {
704 { .compatible = "fsl,imx23-i2c", .data = &mxs_i2c_devtype[0], },
705 { .compatible = "fsl,imx28-i2c", .data = &mxs_i2c_devtype[1], },
706 { /* sentinel */ }
707};
708MODULE_DEVICE_TABLE(of, mxs_i2c_dt_ids);
709
Bill Pemberton0b255e92012-11-27 15:59:38 -0500710static int mxs_i2c_probe(struct platform_device *pdev)
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100711{
Juergen Beisert616228a2013-09-30 01:23:53 +0200712 const struct of_device_id *of_id =
713 of_match_device(mxs_i2c_dt_ids, &pdev->dev);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100714 struct device *dev = &pdev->dev;
715 struct mxs_i2c_dev *i2c;
716 struct i2c_adapter *adap;
717 struct resource *res;
718 resource_size_t res_size;
Shawn Guoe5aba132013-02-26 11:20:22 +0800719 int err, irq;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100720
721 i2c = devm_kzalloc(dev, sizeof(struct mxs_i2c_dev), GFP_KERNEL);
722 if (!i2c)
723 return -ENOMEM;
724
Juergen Beisert616228a2013-09-30 01:23:53 +0200725 if (of_id) {
726 const struct platform_device_id *device_id = of_id->data;
727 i2c->dev_type = device_id->driver_data;
728 }
729
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100730 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Marek Vasut62885f52012-08-24 05:44:31 +0200731 irq = platform_get_irq(pdev, 0);
Marek Vasut62885f52012-08-24 05:44:31 +0200732
Shawn Guoe5aba132013-02-26 11:20:22 +0800733 if (!res || irq < 0)
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100734 return -ENOENT;
735
736 res_size = resource_size(res);
737 if (!devm_request_mem_region(dev, res->start, res_size, res->name))
738 return -EBUSY;
739
740 i2c->regs = devm_ioremap_nocache(dev, res->start, res_size);
741 if (!i2c->regs)
742 return -EBUSY;
743
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100744 err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
745 if (err)
746 return err;
747
748 i2c->dev = dev;
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200749
Marek Vasut85de7fa2012-11-21 06:19:06 +0100750 init_completion(&i2c->cmd_complete);
751
Wolfram Sang72ee7342012-09-08 17:28:06 +0200752 if (dev->of_node) {
753 err = mxs_i2c_get_ofdata(i2c);
754 if (err)
755 return err;
756 }
Marek Vasutcd4f2d42012-07-09 18:22:53 +0200757
Marek Vasut62885f52012-08-24 05:44:31 +0200758 /* Setup the DMA */
Shawn Guoe5aba132013-02-26 11:20:22 +0800759 i2c->dmach = dma_request_slave_channel(dev, "rx-tx");
Wolfram Sang82fa63b2012-10-12 11:55:16 +0100760 if (!i2c->dmach) {
761 dev_err(dev, "Failed to request dma\n");
762 return -ENODEV;
Marek Vasut62885f52012-08-24 05:44:31 +0200763 }
764
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100765 platform_set_drvdata(pdev, i2c);
766
767 /* Do reset to enforce correct startup after pinmuxing */
Fabio Estevam63151c52013-07-10 22:19:28 -0300768 err = mxs_i2c_reset(i2c);
769 if (err)
770 return err;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100771
772 adap = &i2c->adapter;
773 strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
774 adap->owner = THIS_MODULE;
775 adap->algo = &mxs_i2c_algo;
776 adap->dev.parent = dev;
777 adap->nr = pdev->id;
Shawn Guob2378662012-05-12 13:43:32 +0800778 adap->dev.of_node = pdev->dev.of_node;
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100779 i2c_set_adapdata(adap, i2c);
780 err = i2c_add_numbered_adapter(adap);
781 if (err) {
782 dev_err(dev, "Failed to add adapter (%d)\n", err);
783 writel(MXS_I2C_CTRL0_SFTRST,
784 i2c->regs + MXS_I2C_CTRL0_SET);
785 return err;
786 }
787
788 return 0;
789}
790
Bill Pemberton0b255e92012-11-27 15:59:38 -0500791static int mxs_i2c_remove(struct platform_device *pdev)
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100792{
793 struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100794
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000795 i2c_del_adapter(&i2c->adapter);
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100796
Marek Vasut62885f52012-08-24 05:44:31 +0200797 if (i2c->dmach)
798 dma_release_channel(i2c->dmach);
799
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100800 writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
801
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100802 return 0;
803}
804
805static struct platform_driver mxs_i2c_driver = {
806 .driver = {
807 .name = DRIVER_NAME,
808 .owner = THIS_MODULE,
Shawn Guob2378662012-05-12 13:43:32 +0800809 .of_match_table = mxs_i2c_dt_ids,
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100810 },
Bill Pemberton0b255e92012-11-27 15:59:38 -0500811 .remove = mxs_i2c_remove,
Wolfram Sanga8da7fe2011-02-16 13:39:16 +0100812};
813
814static int __init mxs_i2c_init(void)
815{
816 return platform_driver_probe(&mxs_i2c_driver, mxs_i2c_probe);
817}
818subsys_initcall(mxs_i2c_init);
819
820static void __exit mxs_i2c_exit(void)
821{
822 platform_driver_unregister(&mxs_i2c_driver);
823}
824module_exit(mxs_i2c_exit);
825
826MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
827MODULE_DESCRIPTION("MXS I2C Bus Driver");
828MODULE_LICENSE("GPL");
829MODULE_ALIAS("platform:" DRIVER_NAME);