1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/slab.h>
14#include <linux/device.h>
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/err.h>
18#include <linux/interrupt.h>
19#include <linux/completion.h>
20#include <linux/platform_device.h>
21#include <linux/jiffies.h>
22#include <linux/io.h>
23#include <linux/stmp_device.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/dma-mapping.h>
27#include <linux/dmaengine.h>
28
29#define DRIVER_NAME "mxs-i2c"
30
31#define MXS_I2C_CTRL0 (0x00)
32#define MXS_I2C_CTRL0_SET (0x04)
33#define MXS_I2C_CTRL0_CLR (0x08)
34
35#define MXS_I2C_CTRL0_SFTRST 0x80000000
36#define MXS_I2C_CTRL0_RUN 0x20000000
37#define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
38#define MXS_I2C_CTRL0_PIO_MODE 0x01000000
39#define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
40#define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
41#define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
42#define MXS_I2C_CTRL0_MASTER_MODE 0x00020000
43#define MXS_I2C_CTRL0_DIRECTION 0x00010000
44#define MXS_I2C_CTRL0_XFER_COUNT(v) ((v) & 0x0000FFFF)
45
46#define MXS_I2C_TIMING0 (0x10)
47#define MXS_I2C_TIMING1 (0x20)
48#define MXS_I2C_TIMING2 (0x30)
49
50#define MXS_I2C_CTRL1 (0x40)
51#define MXS_I2C_CTRL1_SET (0x44)
52#define MXS_I2C_CTRL1_CLR (0x48)
53
54#define MXS_I2C_CTRL1_CLR_GOT_A_NAK 0x10000000
55#define MXS_I2C_CTRL1_BUS_FREE_IRQ 0x80
56#define MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ 0x40
57#define MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ 0x20
58#define MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ 0x10
59#define MXS_I2C_CTRL1_EARLY_TERM_IRQ 0x08
60#define MXS_I2C_CTRL1_MASTER_LOSS_IRQ 0x04
61#define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
62#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
63
64#define MXS_I2C_STAT (0x50)
65#define MXS_I2C_STAT_GOT_A_NAK 0x10000000
66#define MXS_I2C_STAT_BUS_BUSY 0x00000800
67#define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400
68
69#define MXS_I2C_DATA(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x60 : 0xa0)
70
71#define MXS_I2C_DEBUG0_CLR(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x78 : 0xb8)
72
73#define MXS_I2C_DEBUG0_DMAREQ 0x80000000
74
75#define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
76 MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
77 MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
78 MXS_I2C_CTRL1_MASTER_LOSS_IRQ | \
79 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | \
80 MXS_I2C_CTRL1_SLAVE_IRQ)
81
82
83#define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
84 MXS_I2C_CTRL0_PRE_SEND_START | \
85 MXS_I2C_CTRL0_MASTER_MODE | \
86 MXS_I2C_CTRL0_DIRECTION | \
87 MXS_I2C_CTRL0_XFER_COUNT(1))
88
89#define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
90 MXS_I2C_CTRL0_MASTER_MODE | \
91 MXS_I2C_CTRL0_DIRECTION)
92
93#define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
94 MXS_I2C_CTRL0_MASTER_MODE)
95
96enum mxs_i2c_devtype {
97 MXS_I2C_UNKNOWN = 0,
98 MXS_I2C_V1,
99 MXS_I2C_V2,
100};
101
102
103
104
105
106
107
108
109
110
111
112struct mxs_i2c_dev {
113 struct device *dev;
114 enum mxs_i2c_devtype dev_type;
115 void __iomem *regs;
116 struct completion cmd_complete;
117 int cmd_err;
118 struct i2c_adapter adapter;
119
120 uint32_t timing0;
121 uint32_t timing1;
122 uint32_t timing2;
123
124
125 struct dma_chan *dmach;
126 uint32_t pio_data[2];
127 uint32_t addr_data;
128 struct scatterlist sg_io[2];
129 bool dma_read;
130};
131
132static int mxs_i2c_reset(struct mxs_i2c_dev *i2c)
133{
134 int ret = stmp_reset_block(i2c->regs);
135 if (ret)
136 return ret;
137
138
139
140
141
142
143
144
145 writel(i2c->timing0, i2c->regs + MXS_I2C_TIMING0);
146 writel(i2c->timing1, i2c->regs + MXS_I2C_TIMING1);
147 writel(i2c->timing2, i2c->regs + MXS_I2C_TIMING2);
148
149 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
150
151 return 0;
152}
153
154static void mxs_i2c_dma_finish(struct mxs_i2c_dev *i2c)
155{
156 if (i2c->dma_read) {
157 dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
158 dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
159 } else {
160 dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
161 }
162}
163
164static void mxs_i2c_dma_irq_callback(void *param)
165{
166 struct mxs_i2c_dev *i2c = param;
167
168 complete(&i2c->cmd_complete);
169 mxs_i2c_dma_finish(i2c);
170}
171
172static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap,
173 struct i2c_msg *msg, uint32_t flags)
174{
175 struct dma_async_tx_descriptor *desc;
176 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
177
178 i2c->addr_data = i2c_8bit_addr_from_msg(msg);
179
180 if (msg->flags & I2C_M_RD) {
181 i2c->dma_read = true;
182
183
184
185
186
187
188 i2c->pio_data[0] = MXS_CMD_I2C_SELECT;
189 desc = dmaengine_prep_slave_sg(i2c->dmach,
190 (struct scatterlist *)&i2c->pio_data[0],
191 1, DMA_TRANS_NONE, 0);
192 if (!desc) {
193 dev_err(i2c->dev,
194 "Failed to get PIO reg. write descriptor.\n");
195 goto select_init_pio_fail;
196 }
197
198
199 sg_init_one(&i2c->sg_io[0], &i2c->addr_data, 1);
200 dma_map_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
201 desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[0], 1,
202 DMA_MEM_TO_DEV,
203 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
204 if (!desc) {
205 dev_err(i2c->dev,
206 "Failed to get DMA data write descriptor.\n");
207 goto select_init_dma_fail;
208 }
209
210
211
212
213
214
215 i2c->pio_data[1] = flags | MXS_CMD_I2C_READ |
216 MXS_I2C_CTRL0_XFER_COUNT(msg->len);
217 desc = dmaengine_prep_slave_sg(i2c->dmach,
218 (struct scatterlist *)&i2c->pio_data[1],
219 1, DMA_TRANS_NONE, DMA_PREP_INTERRUPT);
220 if (!desc) {
221 dev_err(i2c->dev,
222 "Failed to get PIO reg. write descriptor.\n");
223 goto select_init_dma_fail;
224 }
225
226
227 sg_init_one(&i2c->sg_io[1], msg->buf, msg->len);
228 dma_map_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
229 desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[1], 1,
230 DMA_DEV_TO_MEM,
231 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
232 if (!desc) {
233 dev_err(i2c->dev,
234 "Failed to get DMA data write descriptor.\n");
235 goto read_init_dma_fail;
236 }
237 } else {
238 i2c->dma_read = false;
239
240
241
242
243
244
245 i2c->pio_data[0] = flags | MXS_CMD_I2C_WRITE |
246 MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1);
247 desc = dmaengine_prep_slave_sg(i2c->dmach,
248 (struct scatterlist *)&i2c->pio_data[0],
249 1, DMA_TRANS_NONE, 0);
250 if (!desc) {
251 dev_err(i2c->dev,
252 "Failed to get PIO reg. write descriptor.\n");
253 goto write_init_pio_fail;
254 }
255
256
257 sg_init_table(i2c->sg_io, 2);
258 sg_set_buf(&i2c->sg_io[0], &i2c->addr_data, 1);
259 sg_set_buf(&i2c->sg_io[1], msg->buf, msg->len);
260 dma_map_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
261 desc = dmaengine_prep_slave_sg(i2c->dmach, i2c->sg_io, 2,
262 DMA_MEM_TO_DEV,
263 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
264 if (!desc) {
265 dev_err(i2c->dev,
266 "Failed to get DMA data write descriptor.\n");
267 goto write_init_dma_fail;
268 }
269 }
270
271
272
273
274
275 desc->callback = mxs_i2c_dma_irq_callback;
276 desc->callback_param = i2c;
277
278
279 dmaengine_submit(desc);
280 dma_async_issue_pending(i2c->dmach);
281 return 0;
282
283
284read_init_dma_fail:
285 dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
286select_init_dma_fail:
287 dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
288select_init_pio_fail:
289 dmaengine_terminate_all(i2c->dmach);
290 return -EINVAL;
291
292
293write_init_dma_fail:
294 dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
295write_init_pio_fail:
296 dmaengine_terminate_all(i2c->dmach);
297 return -EINVAL;
298}
299
300static int mxs_i2c_pio_wait_xfer_end(struct mxs_i2c_dev *i2c)
301{
302 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
303
304 while (readl(i2c->regs + MXS_I2C_CTRL0) & MXS_I2C_CTRL0_RUN) {
305 if (readl(i2c->regs + MXS_I2C_CTRL1) &
306 MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
307 return -ENXIO;
308 if (time_after(jiffies, timeout))
309 return -ETIMEDOUT;
310 cond_resched();
311 }
312
313 return 0;
314}
315
316static int mxs_i2c_pio_check_error_state(struct mxs_i2c_dev *i2c)
317{
318 u32 state;
319
320 state = readl(i2c->regs + MXS_I2C_CTRL1_CLR) & MXS_I2C_IRQ_MASK;
321
322 if (state & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
323 i2c->cmd_err = -ENXIO;
324 else if (state & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
325 MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
326 MXS_I2C_CTRL1_SLAVE_STOP_IRQ |
327 MXS_I2C_CTRL1_SLAVE_IRQ))
328 i2c->cmd_err = -EIO;
329
330 return i2c->cmd_err;
331}
332
333static void mxs_i2c_pio_trigger_cmd(struct mxs_i2c_dev *i2c, u32 cmd)
334{
335 u32 reg;
336
337 writel(cmd, i2c->regs + MXS_I2C_CTRL0);
338
339
340 reg = readl(i2c->regs + MXS_I2C_CTRL0);
341 reg |= MXS_I2C_CTRL0_RUN;
342 writel(reg, i2c->regs + MXS_I2C_CTRL0);
343}
344
345
346
347
348
349
350
351
352
353static void mxs_i2c_pio_trigger_write_cmd(struct mxs_i2c_dev *i2c, u32 cmd,
354 u32 data)
355{
356 writel(cmd, i2c->regs + MXS_I2C_CTRL0);
357
358 if (i2c->dev_type == MXS_I2C_V1)
359 writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_SET);
360
361 writel(data, i2c->regs + MXS_I2C_DATA(i2c));
362 writel(MXS_I2C_CTRL0_RUN, i2c->regs + MXS_I2C_CTRL0_SET);
363}
364
365static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap,
366 struct i2c_msg *msg, uint32_t flags)
367{
368 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
369 uint32_t addr_data = i2c_8bit_addr_from_msg(msg);
370 uint32_t data = 0;
371 int i, ret, xlen = 0, xmit = 0;
372 uint32_t start;
373
374
375 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR);
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 if (msg->flags & I2C_M_RD) {
395
396
397
398
399
400
401
402
403
404
405
406
407 BUG_ON(msg->len > 4);
408
409
410 mxs_i2c_pio_trigger_write_cmd(i2c, MXS_CMD_I2C_SELECT,
411 addr_data);
412
413 ret = mxs_i2c_pio_wait_xfer_end(i2c);
414 if (ret) {
415 dev_dbg(i2c->dev,
416 "PIO: Failed to send SELECT command!\n");
417 goto cleanup;
418 }
419
420
421 mxs_i2c_pio_trigger_cmd(i2c,
422 MXS_CMD_I2C_READ | flags |
423 MXS_I2C_CTRL0_XFER_COUNT(msg->len));
424
425 ret = mxs_i2c_pio_wait_xfer_end(i2c);
426 if (ret) {
427 dev_dbg(i2c->dev,
428 "PIO: Failed to send READ command!\n");
429 goto cleanup;
430 }
431
432 data = readl(i2c->regs + MXS_I2C_DATA(i2c));
433 for (i = 0; i < msg->len; i++) {
434 msg->buf[i] = data & 0xff;
435 data >>= 8;
436 }
437 } else {
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453 data = addr_data << 24;
454
455
456 start = MXS_I2C_CTRL0_PRE_SEND_START;
457
458
459 if (msg->len > 3)
460 start |= MXS_I2C_CTRL0_RETAIN_CLOCK;
461
462 for (i = 0; i < msg->len; i++) {
463 data >>= 8;
464 data |= (msg->buf[i] << 24);
465
466 xmit = 0;
467
468
469 if (i + 1 == msg->len) {
470
471 start |= flags;
472
473 start &= ~MXS_I2C_CTRL0_RETAIN_CLOCK;
474 xmit = 1;
475 }
476
477
478 if ((i & 3) == 2)
479 xmit = 1;
480
481
482 if (!xmit)
483 continue;
484
485
486
487
488
489
490
491
492
493
494
495 if ((i % 4) == 3)
496 xlen = 1;
497 else
498 xlen = (i % 4) + 2;
499
500 data >>= (4 - xlen) * 8;
501
502 dev_dbg(i2c->dev,
503 "PIO: len=%i pos=%i total=%i [W%s%s%s]\n",
504 xlen, i, msg->len,
505 start & MXS_I2C_CTRL0_PRE_SEND_START ? "S" : "",
506 start & MXS_I2C_CTRL0_POST_SEND_STOP ? "E" : "",
507 start & MXS_I2C_CTRL0_RETAIN_CLOCK ? "C" : "");
508
509 writel(MXS_I2C_DEBUG0_DMAREQ,
510 i2c->regs + MXS_I2C_DEBUG0_CLR(i2c));
511
512 mxs_i2c_pio_trigger_write_cmd(i2c,
513 start | MXS_I2C_CTRL0_MASTER_MODE |
514 MXS_I2C_CTRL0_DIRECTION |
515 MXS_I2C_CTRL0_XFER_COUNT(xlen), data);
516
517
518 start &= ~MXS_I2C_CTRL0_PRE_SEND_START;
519
520
521 ret = mxs_i2c_pio_wait_xfer_end(i2c);
522 if (ret) {
523 dev_dbg(i2c->dev,
524 "PIO: Failed to finish WRITE cmd!\n");
525 break;
526 }
527
528
529 ret = readl(i2c->regs + MXS_I2C_STAT) &
530 MXS_I2C_STAT_GOT_A_NAK;
531 if (ret) {
532 ret = -ENXIO;
533 goto cleanup;
534 }
535 }
536 }
537
538
539 ret = mxs_i2c_pio_check_error_state(i2c);
540
541cleanup:
542
543 writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR);
544 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
545
546
547 if (i2c->dev_type == MXS_I2C_V1)
548 writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_CLR);
549
550 return ret;
551}
552
553
554
555
556static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
557 int stop)
558{
559 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
560 int ret;
561 int flags;
562 int use_pio = 0;
563 unsigned long time_left;
564
565 flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
566
567 dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
568 msg->addr, msg->len, msg->flags, stop);
569
570 if (msg->len == 0)
571 return -EINVAL;
572
573
574
575
576
577
578 if ((msg->flags & I2C_M_RD) && (msg->len <= 4))
579 use_pio = 1;
580 if (!(msg->flags & I2C_M_RD) && (msg->len < 7))
581 use_pio = 1;
582
583 i2c->cmd_err = 0;
584 if (use_pio) {
585 ret = mxs_i2c_pio_setup_xfer(adap, msg, flags);
586
587 if (ret && (ret != -ENXIO))
588 mxs_i2c_reset(i2c);
589 } else {
590 reinit_completion(&i2c->cmd_complete);
591 ret = mxs_i2c_dma_setup_xfer(adap, msg, flags);
592 if (ret)
593 return ret;
594
595 time_left = wait_for_completion_timeout(&i2c->cmd_complete,
596 msecs_to_jiffies(1000));
597 if (!time_left)
598 goto timeout;
599
600 ret = i2c->cmd_err;
601 }
602
603 if (ret == -ENXIO) {
604
605
606
607
608 writel(MXS_I2C_CTRL1_CLR_GOT_A_NAK,
609 i2c->regs + MXS_I2C_CTRL1_SET);
610 }
611
612
613
614
615
616
617
618
619
620
621
622
623 if (i2c->dev_type == MXS_I2C_V1)
624 mxs_i2c_reset(i2c);
625
626 dev_dbg(i2c->dev, "Done with err=%d\n", ret);
627
628 return ret;
629
630timeout:
631 dev_dbg(i2c->dev, "Timeout!\n");
632 mxs_i2c_dma_finish(i2c);
633 ret = mxs_i2c_reset(i2c);
634 if (ret)
635 return ret;
636
637 return -ETIMEDOUT;
638}
639
640static int mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
641 int num)
642{
643 int i;
644 int err;
645
646 for (i = 0; i < num; i++) {
647 err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
648 if (err)
649 return err;
650 }
651
652 return num;
653}
654
655static u32 mxs_i2c_func(struct i2c_adapter *adap)
656{
657 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
658}
659
660static irqreturn_t mxs_i2c_isr(int this_irq, void *dev_id)
661{
662 struct mxs_i2c_dev *i2c = dev_id;
663 u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK;
664
665 if (!stat)
666 return IRQ_NONE;
667
668 if (stat & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
669 i2c->cmd_err = -ENXIO;
670 else if (stat & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
671 MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
672 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | MXS_I2C_CTRL1_SLAVE_IRQ))
673
674 i2c->cmd_err = -EIO;
675
676 writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR);
677
678 return IRQ_HANDLED;
679}
680
681static const struct i2c_algorithm mxs_i2c_algo = {
682 .master_xfer = mxs_i2c_xfer,
683 .functionality = mxs_i2c_func,
684};
685
686static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, uint32_t speed)
687{
688
689 const uint32_t clk = 24000000;
690 uint32_t divider;
691 uint16_t high_count, low_count, rcv_count, xmit_count;
692 uint32_t bus_free, leadin;
693 struct device *dev = i2c->dev;
694
695 divider = DIV_ROUND_UP(clk, speed);
696
697 if (divider < 25) {
698
699
700
701
702 divider = 25;
703 dev_warn(dev,
704 "Speed too high (%u.%03u kHz), using %u.%03u kHz\n",
705 speed / 1000, speed % 1000,
706 clk / divider / 1000, clk / divider % 1000);
707 } else if (divider > 1897) {
708
709
710
711
712 divider = 1897;
713 dev_warn(dev,
714 "Speed too low (%u.%03u kHz), using %u.%03u kHz\n",
715 speed / 1000, speed % 1000,
716 clk / divider / 1000, clk / divider % 1000);
717 }
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733 if (speed > 100000) {
734
735 low_count = DIV_ROUND_CLOSEST(divider * 13, (13 + 6));
736 high_count = DIV_ROUND_CLOSEST(divider * 6, (13 + 6));
737 leadin = DIV_ROUND_UP(600 * (clk / 1000000), 1000);
738 bus_free = DIV_ROUND_UP(1300 * (clk / 1000000), 1000);
739 } else {
740
741 low_count = DIV_ROUND_CLOSEST(divider * 47, (47 + 40));
742 high_count = DIV_ROUND_CLOSEST(divider * 40, (47 + 40));
743 leadin = DIV_ROUND_UP(4700 * (clk / 1000000), 1000);
744 bus_free = DIV_ROUND_UP(4700 * (clk / 1000000), 1000);
745 }
746 rcv_count = high_count * 3 / 8;
747 xmit_count = low_count * 3 / 8;
748
749 dev_dbg(dev,
750 "speed=%u(actual %u) divider=%u low=%u high=%u xmit=%u rcv=%u leadin=%u bus_free=%u\n",
751 speed, clk / divider, divider, low_count, high_count,
752 xmit_count, rcv_count, leadin, bus_free);
753
754 low_count -= 2;
755 high_count -= 7;
756 i2c->timing0 = (high_count << 16) | rcv_count;
757 i2c->timing1 = (low_count << 16) | xmit_count;
758 i2c->timing2 = (bus_free << 16 | leadin);
759}
760
761static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c)
762{
763 uint32_t speed;
764 struct device *dev = i2c->dev;
765 struct device_node *node = dev->of_node;
766 int ret;
767
768 ret = of_property_read_u32(node, "clock-frequency", &speed);
769 if (ret) {
770 dev_warn(dev, "No I2C speed selected, using 100kHz\n");
771 speed = 100000;
772 }
773
774 mxs_i2c_derive_timing(i2c, speed);
775
776 return 0;
777}
778
779static const struct platform_device_id mxs_i2c_devtype[] = {
780 {
781 .name = "imx23-i2c",
782 .driver_data = MXS_I2C_V1,
783 }, {
784 .name = "imx28-i2c",
785 .driver_data = MXS_I2C_V2,
786 }, { }
787};
788MODULE_DEVICE_TABLE(platform, mxs_i2c_devtype);
789
790static const struct of_device_id mxs_i2c_dt_ids[] = {
791 { .compatible = "fsl,imx23-i2c", .data = &mxs_i2c_devtype[0], },
792 { .compatible = "fsl,imx28-i2c", .data = &mxs_i2c_devtype[1], },
793 { }
794};
795MODULE_DEVICE_TABLE(of, mxs_i2c_dt_ids);
796
797static int mxs_i2c_probe(struct platform_device *pdev)
798{
799 const struct of_device_id *of_id =
800 of_match_device(mxs_i2c_dt_ids, &pdev->dev);
801 struct device *dev = &pdev->dev;
802 struct mxs_i2c_dev *i2c;
803 struct i2c_adapter *adap;
804 struct resource *res;
805 int err, irq;
806
807 i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
808 if (!i2c)
809 return -ENOMEM;
810
811 if (of_id) {
812 const struct platform_device_id *device_id = of_id->data;
813 i2c->dev_type = device_id->driver_data;
814 }
815
816 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
817 i2c->regs = devm_ioremap_resource(&pdev->dev, res);
818 if (IS_ERR(i2c->regs))
819 return PTR_ERR(i2c->regs);
820
821 irq = platform_get_irq(pdev, 0);
822 if (irq < 0)
823 return irq;
824
825 err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
826 if (err)
827 return err;
828
829 i2c->dev = dev;
830
831 init_completion(&i2c->cmd_complete);
832
833 if (dev->of_node) {
834 err = mxs_i2c_get_ofdata(i2c);
835 if (err)
836 return err;
837 }
838
839
840 i2c->dmach = dma_request_slave_channel(dev, "rx-tx");
841 if (!i2c->dmach) {
842 dev_err(dev, "Failed to request dma\n");
843 return -ENODEV;
844 }
845
846 platform_set_drvdata(pdev, i2c);
847
848
849 err = mxs_i2c_reset(i2c);
850 if (err)
851 return err;
852
853 adap = &i2c->adapter;
854 strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
855 adap->owner = THIS_MODULE;
856 adap->algo = &mxs_i2c_algo;
857 adap->dev.parent = dev;
858 adap->nr = pdev->id;
859 adap->dev.of_node = pdev->dev.of_node;
860 i2c_set_adapdata(adap, i2c);
861 err = i2c_add_numbered_adapter(adap);
862 if (err) {
863 writel(MXS_I2C_CTRL0_SFTRST,
864 i2c->regs + MXS_I2C_CTRL0_SET);
865 return err;
866 }
867
868 return 0;
869}
870
871static int mxs_i2c_remove(struct platform_device *pdev)
872{
873 struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
874
875 i2c_del_adapter(&i2c->adapter);
876
877 if (i2c->dmach)
878 dma_release_channel(i2c->dmach);
879
880 writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
881
882 return 0;
883}
884
885static struct platform_driver mxs_i2c_driver = {
886 .driver = {
887 .name = DRIVER_NAME,
888 .of_match_table = mxs_i2c_dt_ids,
889 },
890 .probe = mxs_i2c_probe,
891 .remove = mxs_i2c_remove,
892};
893
894static int __init mxs_i2c_init(void)
895{
896 return platform_driver_register(&mxs_i2c_driver);
897}
898subsys_initcall(mxs_i2c_init);
899
900static void __exit mxs_i2c_exit(void)
901{
902 platform_driver_unregister(&mxs_i2c_driver);
903}
904module_exit(mxs_i2c_exit);
905
906MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
907MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
908MODULE_DESCRIPTION("MXS I2C Bus Driver");
909MODULE_LICENSE("GPL");
910MODULE_ALIAS("platform:" DRIVER_NAME);
911