1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/clk.h>
14#include <linux/delay.h>
15#include <linux/i2c.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/of.h>
21#include <linux/pm_runtime.h>
22
23
24#define CDNS_I2C_CR_OFFSET 0x00
25#define CDNS_I2C_SR_OFFSET 0x04
26#define CDNS_I2C_ADDR_OFFSET 0x08
27#define CDNS_I2C_DATA_OFFSET 0x0C
28#define CDNS_I2C_ISR_OFFSET 0x10
29#define CDNS_I2C_XFER_SIZE_OFFSET 0x14
30#define CDNS_I2C_SLV_PAUSE_OFFSET 0x18
31#define CDNS_I2C_TIME_OUT_OFFSET 0x1C
32#define CDNS_I2C_IER_OFFSET 0x24
33#define CDNS_I2C_IDR_OFFSET 0x28
34
35
36#define CDNS_I2C_CR_SLVMON BIT(5)
37#define CDNS_I2C_CR_HOLD BIT(4)
38#define CDNS_I2C_CR_ACK_EN BIT(3)
39#define CDNS_I2C_CR_NEA BIT(2)
40#define CDNS_I2C_CR_MS BIT(1)
41
42#define CDNS_I2C_CR_RW BIT(0)
43
44#define CDNS_I2C_CR_CLR_FIFO BIT(6)
45#define CDNS_I2C_CR_DIVA_SHIFT 14
46#define CDNS_I2C_CR_DIVA_MASK (3 << CDNS_I2C_CR_DIVA_SHIFT)
47#define CDNS_I2C_CR_DIVB_SHIFT 8
48#define CDNS_I2C_CR_DIVB_MASK (0x3f << CDNS_I2C_CR_DIVB_SHIFT)
49
50
51#define CDNS_I2C_SR_BA BIT(8)
52#define CDNS_I2C_SR_RXDV BIT(5)
53
54
55
56
57
58
59
60#define CDNS_I2C_ADDR_MASK 0x000003FF
61
62
63
64
65
66
67#define CDNS_I2C_IXR_ARB_LOST BIT(9)
68#define CDNS_I2C_IXR_RX_UNF BIT(7)
69#define CDNS_I2C_IXR_TX_OVF BIT(6)
70#define CDNS_I2C_IXR_RX_OVF BIT(5)
71#define CDNS_I2C_IXR_SLV_RDY BIT(4)
72#define CDNS_I2C_IXR_TO BIT(3)
73#define CDNS_I2C_IXR_NACK BIT(2)
74#define CDNS_I2C_IXR_DATA BIT(1)
75#define CDNS_I2C_IXR_COMP BIT(0)
76
77#define CDNS_I2C_IXR_ALL_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
78 CDNS_I2C_IXR_RX_UNF | \
79 CDNS_I2C_IXR_TX_OVF | \
80 CDNS_I2C_IXR_RX_OVF | \
81 CDNS_I2C_IXR_SLV_RDY | \
82 CDNS_I2C_IXR_TO | \
83 CDNS_I2C_IXR_NACK | \
84 CDNS_I2C_IXR_DATA | \
85 CDNS_I2C_IXR_COMP)
86
87#define CDNS_I2C_IXR_ERR_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
88 CDNS_I2C_IXR_RX_UNF | \
89 CDNS_I2C_IXR_TX_OVF | \
90 CDNS_I2C_IXR_RX_OVF | \
91 CDNS_I2C_IXR_NACK)
92
93#define CDNS_I2C_ENABLED_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
94 CDNS_I2C_IXR_RX_UNF | \
95 CDNS_I2C_IXR_TX_OVF | \
96 CDNS_I2C_IXR_RX_OVF | \
97 CDNS_I2C_IXR_NACK | \
98 CDNS_I2C_IXR_DATA | \
99 CDNS_I2C_IXR_COMP)
100
101#define CDNS_I2C_TIMEOUT msecs_to_jiffies(1000)
102
103#define CNDS_I2C_PM_TIMEOUT 1000
104
105#define CDNS_I2C_FIFO_DEPTH 16
106
107#define CDNS_I2C_DATA_INTR_DEPTH (CDNS_I2C_FIFO_DEPTH - 2)
108#define CDNS_I2C_MAX_TRANSFER_SIZE 255
109
110#define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
111
112#define DRIVER_NAME "cdns-i2c"
113
114#define CDNS_I2C_SPEED_MAX 400000
115#define CDNS_I2C_SPEED_DEFAULT 100000
116
117#define CDNS_I2C_DIVA_MAX 4
118#define CDNS_I2C_DIVB_MAX 64
119
120#define CDNS_I2C_TIMEOUT_MAX 0xFF
121
122#define CDNS_I2C_BROKEN_HOLD_BIT BIT(0)
123
124#define cdns_i2c_readreg(offset) readl_relaxed(id->membase + offset)
125#define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150struct cdns_i2c {
151 struct device *dev;
152 void __iomem *membase;
153 struct i2c_adapter adap;
154 struct i2c_msg *p_msg;
155 int err_status;
156 struct completion xfer_done;
157 unsigned char *p_send_buf;
158 unsigned char *p_recv_buf;
159 unsigned int send_count;
160 unsigned int recv_count;
161 unsigned int curr_recv_count;
162 int irq;
163 unsigned long input_clk;
164 unsigned int i2c_clk;
165 unsigned int bus_hold_flag;
166 struct clk *clk;
167 struct notifier_block clk_rate_change_nb;
168 u32 quirks;
169 u32 ctrl_reg;
170};
171
172struct cdns_platform_data {
173 u32 quirks;
174};
175
176#define to_cdns_i2c(_nb) container_of(_nb, struct cdns_i2c, \
177 clk_rate_change_nb)
178
179
180
181
182
183
184
185static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id)
186{
187 u32 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
188 if (reg & CDNS_I2C_CR_HOLD)
189 cdns_i2c_writereg(reg & ~CDNS_I2C_CR_HOLD, CDNS_I2C_CR_OFFSET);
190}
191
192static inline bool cdns_is_holdquirk(struct cdns_i2c *id, bool hold_wrkaround)
193{
194 return (hold_wrkaround &&
195 (id->curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1));
196}
197
198
199
200
201
202
203
204
205
206
207
208static irqreturn_t cdns_i2c_isr(int irq, void *ptr)
209{
210 unsigned int isr_status, avail_bytes, updatetx;
211 unsigned int bytes_to_send;
212 bool hold_quirk;
213 struct cdns_i2c *id = ptr;
214
215 int done_flag = 0;
216 irqreturn_t status = IRQ_NONE;
217
218 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
219 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
220
221
222 if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_ARB_LOST)) {
223 done_flag = 1;
224 status = IRQ_HANDLED;
225 }
226
227
228
229
230
231 updatetx = 0;
232 if (id->recv_count > id->curr_recv_count)
233 updatetx = 1;
234
235 hold_quirk = (id->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx;
236
237
238 if (id->p_recv_buf &&
239 ((isr_status & CDNS_I2C_IXR_COMP) ||
240 (isr_status & CDNS_I2C_IXR_DATA))) {
241
242 while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) &
243 CDNS_I2C_SR_RXDV) {
244
245
246
247
248
249 if ((id->recv_count < CDNS_I2C_FIFO_DEPTH) &&
250 !id->bus_hold_flag)
251 cdns_i2c_clear_bus_hold(id);
252
253 *(id->p_recv_buf)++ =
254 cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
255 id->recv_count--;
256 id->curr_recv_count--;
257
258 if (cdns_is_holdquirk(id, hold_quirk))
259 break;
260 }
261
262
263
264
265
266
267
268
269 if (cdns_is_holdquirk(id, hold_quirk)) {
270
271 while (cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET) !=
272 (id->curr_recv_count - CDNS_I2C_FIFO_DEPTH))
273 ;
274
275
276
277
278
279 if (((int)(id->recv_count) - CDNS_I2C_FIFO_DEPTH) >
280 CDNS_I2C_TRANSFER_SIZE) {
281 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
282 CDNS_I2C_XFER_SIZE_OFFSET);
283 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
284 CDNS_I2C_FIFO_DEPTH;
285 } else {
286 cdns_i2c_writereg(id->recv_count -
287 CDNS_I2C_FIFO_DEPTH,
288 CDNS_I2C_XFER_SIZE_OFFSET);
289 id->curr_recv_count = id->recv_count;
290 }
291 } else if (id->recv_count && !hold_quirk &&
292 !id->curr_recv_count) {
293
294
295 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
296 CDNS_I2C_ADDR_OFFSET);
297
298 if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
299 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
300 CDNS_I2C_XFER_SIZE_OFFSET);
301 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
302 } else {
303 cdns_i2c_writereg(id->recv_count,
304 CDNS_I2C_XFER_SIZE_OFFSET);
305 id->curr_recv_count = id->recv_count;
306 }
307 }
308
309
310 if ((isr_status & CDNS_I2C_IXR_COMP) && !id->recv_count) {
311 if (!id->bus_hold_flag)
312 cdns_i2c_clear_bus_hold(id);
313 done_flag = 1;
314 }
315
316 status = IRQ_HANDLED;
317 }
318
319
320 if ((isr_status & CDNS_I2C_IXR_COMP) && !id->p_recv_buf) {
321
322
323
324
325 if (id->send_count) {
326 avail_bytes = CDNS_I2C_FIFO_DEPTH -
327 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
328 if (id->send_count > avail_bytes)
329 bytes_to_send = avail_bytes;
330 else
331 bytes_to_send = id->send_count;
332
333 while (bytes_to_send--) {
334 cdns_i2c_writereg(
335 (*(id->p_send_buf)++),
336 CDNS_I2C_DATA_OFFSET);
337 id->send_count--;
338 }
339 } else {
340
341
342
343
344
345 done_flag = 1;
346 }
347 if (!id->send_count && !id->bus_hold_flag)
348 cdns_i2c_clear_bus_hold(id);
349
350 status = IRQ_HANDLED;
351 }
352
353
354 if (isr_status & CDNS_I2C_IXR_SLV_RDY) {
355 unsigned int ctrl_reg;
356
357 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
358
359
360 ctrl_reg &= ~CDNS_I2C_CR_SLVMON;
361 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
362
363
364 cdns_i2c_writereg(CDNS_I2C_IXR_SLV_RDY, CDNS_I2C_IDR_OFFSET);
365
366 done_flag = 1;
367 status = IRQ_HANDLED;
368 }
369
370
371 id->err_status = isr_status & CDNS_I2C_IXR_ERR_INTR_MASK;
372 if (id->err_status)
373 status = IRQ_HANDLED;
374
375 if (done_flag)
376 complete(&id->xfer_done);
377
378 return status;
379}
380
381
382
383
384
385static void cdns_i2c_mrecv(struct cdns_i2c *id)
386{
387 unsigned int ctrl_reg;
388 unsigned int isr_status;
389
390 id->p_recv_buf = id->p_msg->buf;
391 id->recv_count = id->p_msg->len;
392
393
394 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
395 ctrl_reg |= CDNS_I2C_CR_RW | CDNS_I2C_CR_CLR_FIFO;
396
397 if (id->p_msg->flags & I2C_M_RECV_LEN)
398 id->recv_count = I2C_SMBUS_BLOCK_MAX + 1;
399
400 id->curr_recv_count = id->recv_count;
401
402
403
404
405
406 if (id->recv_count > CDNS_I2C_FIFO_DEPTH)
407 ctrl_reg |= CDNS_I2C_CR_HOLD;
408
409 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
410
411
412 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
413 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
414
415
416
417
418
419
420
421 if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
422 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
423 CDNS_I2C_XFER_SIZE_OFFSET);
424 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
425 } else {
426 cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
427 }
428
429
430 if (!id->bus_hold_flag &&
431 ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
432 (id->recv_count <= CDNS_I2C_FIFO_DEPTH))
433 cdns_i2c_clear_bus_hold(id);
434
435 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
436 CDNS_I2C_ADDR_OFFSET);
437 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
438}
439
440
441
442
443
444static void cdns_i2c_msend(struct cdns_i2c *id)
445{
446 unsigned int avail_bytes;
447 unsigned int bytes_to_send;
448 unsigned int ctrl_reg;
449 unsigned int isr_status;
450
451 id->p_recv_buf = NULL;
452 id->p_send_buf = id->p_msg->buf;
453 id->send_count = id->p_msg->len;
454
455
456 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
457 ctrl_reg &= ~CDNS_I2C_CR_RW;
458 ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
459
460
461
462
463
464 if (id->send_count > CDNS_I2C_FIFO_DEPTH)
465 ctrl_reg |= CDNS_I2C_CR_HOLD;
466 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
467
468
469 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
470 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
471
472
473
474
475
476
477 avail_bytes = CDNS_I2C_FIFO_DEPTH -
478 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
479
480 if (id->send_count > avail_bytes)
481 bytes_to_send = avail_bytes;
482 else
483 bytes_to_send = id->send_count;
484
485 while (bytes_to_send--) {
486 cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
487 id->send_count--;
488 }
489
490
491
492
493
494 if (!id->bus_hold_flag && !id->send_count)
495 cdns_i2c_clear_bus_hold(id);
496
497 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
498 CDNS_I2C_ADDR_OFFSET);
499
500 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
501}
502
503
504
505
506
507static void cdns_i2c_slvmon(struct cdns_i2c *id)
508{
509 unsigned int ctrl_reg;
510 unsigned int isr_status;
511
512 id->p_recv_buf = NULL;
513 id->p_send_buf = id->p_msg->buf;
514 id->send_count = id->p_msg->len;
515
516
517 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
518 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
519
520
521 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
522 ctrl_reg |= CDNS_I2C_CR_MS | CDNS_I2C_CR_NEA | CDNS_I2C_CR_SLVMON
523 | CDNS_I2C_CR_CLR_FIFO;
524 ctrl_reg &= ~(CDNS_I2C_CR_RW);
525 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
526
527
528 cdns_i2c_writereg(0xF, CDNS_I2C_SLV_PAUSE_OFFSET);
529
530
531 cdns_i2c_writereg(id->p_msg->addr, CDNS_I2C_ADDR_OFFSET);
532
533
534 cdns_i2c_writereg(CDNS_I2C_IXR_SLV_RDY, CDNS_I2C_IER_OFFSET);
535}
536
537
538
539
540
541
542
543
544static void cdns_i2c_master_reset(struct i2c_adapter *adap)
545{
546 struct cdns_i2c *id = adap->algo_data;
547 u32 regval;
548
549
550 cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
551
552 regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
553 regval &= ~(CDNS_I2C_CR_HOLD | CDNS_I2C_CR_SLVMON);
554 regval |= CDNS_I2C_CR_CLR_FIFO;
555 cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
556
557 cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
558
559 regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
560 cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
561
562 regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
563 cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
564}
565
566static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
567 struct i2c_adapter *adap)
568{
569 unsigned long time_left;
570 u32 reg;
571
572 id->p_msg = msg;
573 id->err_status = 0;
574 reinit_completion(&id->xfer_done);
575
576
577 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
578 if (msg->flags & I2C_M_TEN) {
579 if (reg & CDNS_I2C_CR_NEA)
580 cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
581 CDNS_I2C_CR_OFFSET);
582 } else {
583 if (!(reg & CDNS_I2C_CR_NEA))
584 cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
585 CDNS_I2C_CR_OFFSET);
586 }
587
588 if (msg->len == 0)
589 cdns_i2c_slvmon(id);
590
591 else if (msg->flags & I2C_M_RD)
592 cdns_i2c_mrecv(id);
593 else
594 cdns_i2c_msend(id);
595
596
597 time_left = wait_for_completion_timeout(&id->xfer_done, adap->timeout);
598 if (time_left == 0) {
599 cdns_i2c_master_reset(adap);
600 dev_err(id->adap.dev.parent,
601 "timeout waiting on completion\n");
602 return -ETIMEDOUT;
603 }
604
605 cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
606 CDNS_I2C_IDR_OFFSET);
607
608
609 if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
610 return -EAGAIN;
611
612 return 0;
613}
614
615
616
617
618
619
620
621
622
623
624
625static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
626 int num)
627{
628 int ret, count;
629 u32 reg;
630 struct cdns_i2c *id = adap->algo_data;
631 bool hold_quirk;
632
633 ret = pm_runtime_get_sync(id->dev);
634 if (ret < 0)
635 return ret;
636
637 if (msgs->len)
638 if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA) {
639 ret = -EAGAIN;
640 goto out;
641 }
642
643 hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
644
645
646
647
648 if (num > 1) {
649
650
651
652
653
654
655
656 for (count = 0; (count < num - 1 && hold_quirk); count++) {
657 if (msgs[count].flags & I2C_M_RD) {
658 dev_warn(adap->dev.parent,
659 "Can't do repeated start after a receive message\n");
660 ret = -EOPNOTSUPP;
661 goto out;
662 }
663 }
664 id->bus_hold_flag = 1;
665 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
666 reg |= CDNS_I2C_CR_HOLD;
667 cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
668 } else {
669 id->bus_hold_flag = 0;
670 }
671
672
673 for (count = 0; count < num; count++, msgs++) {
674 if (count == (num - 1))
675 id->bus_hold_flag = 0;
676
677 ret = cdns_i2c_process_msg(id, msgs, adap);
678 if (ret)
679 goto out;
680
681
682 if (id->err_status) {
683 cdns_i2c_master_reset(adap);
684
685 if (id->err_status & CDNS_I2C_IXR_NACK) {
686 ret = -ENXIO;
687 goto out;
688 }
689 ret = -EIO;
690 goto out;
691 }
692 }
693
694 ret = num;
695out:
696 pm_runtime_mark_last_busy(id->dev);
697 pm_runtime_put_autosuspend(id->dev);
698 return ret;
699}
700
701
702
703
704
705
706
707static u32 cdns_i2c_func(struct i2c_adapter *adap)
708{
709 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
710 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
711 I2C_FUNC_SMBUS_BLOCK_DATA;
712}
713
714static const struct i2c_algorithm cdns_i2c_algo = {
715 .master_xfer = cdns_i2c_master_xfer,
716 .functionality = cdns_i2c_func,
717};
718
719
720
721
722
723
724
725
726
727
728
729
730
731static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
732 unsigned int *a, unsigned int *b)
733{
734 unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
735 unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
736 unsigned int last_error, current_error;
737
738
739 temp = input_clk / (22 * fscl);
740
741
742
743
744
745 if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
746 return -EINVAL;
747
748 last_error = -1;
749 for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
750 div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
751
752 if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
753 continue;
754 div_b--;
755
756 actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
757
758 if (actual_fscl > fscl)
759 continue;
760
761 current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
762 (fscl - actual_fscl));
763
764 if (last_error > current_error) {
765 calc_div_a = div_a;
766 calc_div_b = div_b;
767 best_fscl = actual_fscl;
768 last_error = current_error;
769 }
770 }
771
772 *a = calc_div_a;
773 *b = calc_div_b;
774 *f = best_fscl;
775
776 return 0;
777}
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id)
796{
797 unsigned int div_a, div_b;
798 unsigned int ctrl_reg;
799 int ret = 0;
800 unsigned long fscl = id->i2c_clk;
801
802 ret = cdns_i2c_calc_divs(&fscl, clk_in, &div_a, &div_b);
803 if (ret)
804 return ret;
805
806 ctrl_reg = id->ctrl_reg;
807 ctrl_reg &= ~(CDNS_I2C_CR_DIVA_MASK | CDNS_I2C_CR_DIVB_MASK);
808 ctrl_reg |= ((div_a << CDNS_I2C_CR_DIVA_SHIFT) |
809 (div_b << CDNS_I2C_CR_DIVB_SHIFT));
810 id->ctrl_reg = ctrl_reg;
811 return 0;
812}
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
831 event, void *data)
832{
833 struct clk_notifier_data *ndata = data;
834 struct cdns_i2c *id = to_cdns_i2c(nb);
835
836 if (pm_runtime_suspended(id->dev))
837 return NOTIFY_OK;
838
839 switch (event) {
840 case PRE_RATE_CHANGE:
841 {
842 unsigned long input_clk = ndata->new_rate;
843 unsigned long fscl = id->i2c_clk;
844 unsigned int div_a, div_b;
845 int ret;
846
847 ret = cdns_i2c_calc_divs(&fscl, input_clk, &div_a, &div_b);
848 if (ret) {
849 dev_warn(id->adap.dev.parent,
850 "clock rate change rejected\n");
851 return NOTIFY_STOP;
852 }
853
854
855 if (ndata->new_rate > ndata->old_rate)
856 cdns_i2c_setclk(ndata->new_rate, id);
857
858 return NOTIFY_OK;
859 }
860 case POST_RATE_CHANGE:
861 id->input_clk = ndata->new_rate;
862
863 if (ndata->new_rate < ndata->old_rate)
864 cdns_i2c_setclk(ndata->new_rate, id);
865 return NOTIFY_OK;
866 case ABORT_RATE_CHANGE:
867
868 if (ndata->new_rate > ndata->old_rate)
869 cdns_i2c_setclk(ndata->old_rate, id);
870 return NOTIFY_OK;
871 default:
872 return NOTIFY_DONE;
873 }
874}
875
876
877
878
879
880
881
882
883
884static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev)
885{
886 struct platform_device *pdev = to_platform_device(dev);
887 struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
888
889 clk_disable(xi2c->clk);
890
891 return 0;
892}
893
894
895
896
897
898
899
900
901static void cdns_i2c_init(struct cdns_i2c *id)
902{
903 cdns_i2c_writereg(id->ctrl_reg, CDNS_I2C_CR_OFFSET);
904
905
906
907
908
909
910
911 cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX, CDNS_I2C_TIME_OUT_OFFSET);
912}
913
914
915
916
917
918
919
920
921
922static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev)
923{
924 struct platform_device *pdev = to_platform_device(dev);
925 struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
926 int ret;
927
928 ret = clk_enable(xi2c->clk);
929 if (ret) {
930 dev_err(dev, "Cannot enable clock.\n");
931 return ret;
932 }
933 cdns_i2c_init(xi2c);
934
935 return 0;
936}
937
938static const struct dev_pm_ops cdns_i2c_dev_pm_ops = {
939 SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend,
940 cdns_i2c_runtime_resume, NULL)
941};
942
943static const struct cdns_platform_data r1p10_i2c_def = {
944 .quirks = CDNS_I2C_BROKEN_HOLD_BIT,
945};
946
947static const struct of_device_id cdns_i2c_of_match[] = {
948 { .compatible = "cdns,i2c-r1p10", .data = &r1p10_i2c_def },
949 { .compatible = "cdns,i2c-r1p14",},
950 { }
951};
952MODULE_DEVICE_TABLE(of, cdns_i2c_of_match);
953
954
955
956
957
958
959
960
961
962
963
964static int cdns_i2c_probe(struct platform_device *pdev)
965{
966 struct resource *r_mem;
967 struct cdns_i2c *id;
968 int ret;
969 const struct of_device_id *match;
970
971 id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL);
972 if (!id)
973 return -ENOMEM;
974
975 id->dev = &pdev->dev;
976 platform_set_drvdata(pdev, id);
977
978 match = of_match_node(cdns_i2c_of_match, pdev->dev.of_node);
979 if (match && match->data) {
980 const struct cdns_platform_data *data = match->data;
981 id->quirks = data->quirks;
982 }
983
984 r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
985 id->membase = devm_ioremap_resource(&pdev->dev, r_mem);
986 if (IS_ERR(id->membase))
987 return PTR_ERR(id->membase);
988
989 id->irq = platform_get_irq(pdev, 0);
990
991 id->adap.owner = THIS_MODULE;
992 id->adap.dev.of_node = pdev->dev.of_node;
993 id->adap.algo = &cdns_i2c_algo;
994 id->adap.timeout = CDNS_I2C_TIMEOUT;
995 id->adap.retries = 3;
996 id->adap.algo_data = id;
997 id->adap.dev.parent = &pdev->dev;
998 init_completion(&id->xfer_done);
999 snprintf(id->adap.name, sizeof(id->adap.name),
1000 "Cadence I2C at %08lx", (unsigned long)r_mem->start);
1001
1002 id->clk = devm_clk_get(&pdev->dev, NULL);
1003 if (IS_ERR(id->clk)) {
1004 dev_err(&pdev->dev, "input clock not found.\n");
1005 return PTR_ERR(id->clk);
1006 }
1007 ret = clk_prepare_enable(id->clk);
1008 if (ret)
1009 dev_err(&pdev->dev, "Unable to enable clock.\n");
1010
1011 pm_runtime_set_autosuspend_delay(id->dev, CNDS_I2C_PM_TIMEOUT);
1012 pm_runtime_use_autosuspend(id->dev);
1013 pm_runtime_set_active(id->dev);
1014 pm_runtime_enable(id->dev);
1015
1016 id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb;
1017 if (clk_notifier_register(id->clk, &id->clk_rate_change_nb))
1018 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1019 id->input_clk = clk_get_rate(id->clk);
1020
1021 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
1022 &id->i2c_clk);
1023 if (ret || (id->i2c_clk > CDNS_I2C_SPEED_MAX))
1024 id->i2c_clk = CDNS_I2C_SPEED_DEFAULT;
1025
1026 id->ctrl_reg = CDNS_I2C_CR_ACK_EN | CDNS_I2C_CR_NEA | CDNS_I2C_CR_MS;
1027
1028 ret = cdns_i2c_setclk(id->input_clk, id);
1029 if (ret) {
1030 dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk);
1031 ret = -EINVAL;
1032 goto err_clk_dis;
1033 }
1034
1035 ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0,
1036 DRIVER_NAME, id);
1037 if (ret) {
1038 dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
1039 goto err_clk_dis;
1040 }
1041
1042 ret = i2c_add_adapter(&id->adap);
1043 if (ret < 0) {
1044 dev_err(&pdev->dev, "reg adap failed: %d\n", ret);
1045 goto err_clk_dis;
1046 }
1047 cdns_i2c_init(id);
1048
1049 dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n",
1050 id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq);
1051
1052 return 0;
1053
1054err_clk_dis:
1055 clk_disable_unprepare(id->clk);
1056 pm_runtime_disable(&pdev->dev);
1057 pm_runtime_set_suspended(&pdev->dev);
1058 return ret;
1059}
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069static int cdns_i2c_remove(struct platform_device *pdev)
1070{
1071 struct cdns_i2c *id = platform_get_drvdata(pdev);
1072
1073 pm_runtime_disable(&pdev->dev);
1074 pm_runtime_set_suspended(&pdev->dev);
1075 pm_runtime_dont_use_autosuspend(&pdev->dev);
1076
1077 i2c_del_adapter(&id->adap);
1078 clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
1079 clk_disable_unprepare(id->clk);
1080 pm_runtime_disable(&pdev->dev);
1081
1082 return 0;
1083}
1084
1085static struct platform_driver cdns_i2c_drv = {
1086 .driver = {
1087 .name = DRIVER_NAME,
1088 .of_match_table = cdns_i2c_of_match,
1089 .pm = &cdns_i2c_dev_pm_ops,
1090 },
1091 .probe = cdns_i2c_probe,
1092 .remove = cdns_i2c_remove,
1093};
1094
1095module_platform_driver(cdns_i2c_drv);
1096
1097MODULE_AUTHOR("Xilinx Inc.");
1098MODULE_DESCRIPTION("Cadence I2C bus driver");
1099MODULE_LICENSE("GPL");
1100