1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/errno.h>
30#include <linux/err.h>
31#include <linux/delay.h>
32#include <linux/platform_device.h>
33#include <linux/i2c.h>
34#include <linux/interrupt.h>
35#include <linux/wait.h>
36#include <linux/i2c-xiic.h>
37#include <linux/io.h>
38#include <linux/slab.h>
39#include <linux/of.h>
40
41#define DRIVER_NAME "xiic-i2c"
42
43enum xilinx_i2c_state {
44 STATE_DONE,
45 STATE_ERROR,
46 STATE_START
47};
48
49enum xiic_endian {
50 LITTLE,
51 BIG
52};
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68struct xiic_i2c {
69 void __iomem *base;
70 wait_queue_head_t wait;
71 struct i2c_adapter adap;
72 struct i2c_msg *tx_msg;
73 spinlock_t lock;
74 unsigned int tx_pos;
75 unsigned int nmsgs;
76 enum xilinx_i2c_state state;
77 struct i2c_msg *rx_msg;
78 int rx_pos;
79 enum xiic_endian endianness;
80};
81
82
83#define XIIC_MSB_OFFSET 0
84#define XIIC_REG_OFFSET (0x100+XIIC_MSB_OFFSET)
85
86
87
88
89
90#define XIIC_CR_REG_OFFSET (0x00+XIIC_REG_OFFSET)
91#define XIIC_SR_REG_OFFSET (0x04+XIIC_REG_OFFSET)
92#define XIIC_DTR_REG_OFFSET (0x08+XIIC_REG_OFFSET)
93#define XIIC_DRR_REG_OFFSET (0x0C+XIIC_REG_OFFSET)
94#define XIIC_ADR_REG_OFFSET (0x10+XIIC_REG_OFFSET)
95#define XIIC_TFO_REG_OFFSET (0x14+XIIC_REG_OFFSET)
96#define XIIC_RFO_REG_OFFSET (0x18+XIIC_REG_OFFSET)
97#define XIIC_TBA_REG_OFFSET (0x1C+XIIC_REG_OFFSET)
98#define XIIC_RFD_REG_OFFSET (0x20+XIIC_REG_OFFSET)
99#define XIIC_GPO_REG_OFFSET (0x24+XIIC_REG_OFFSET)
100
101
102#define XIIC_CR_ENABLE_DEVICE_MASK 0x01
103#define XIIC_CR_TX_FIFO_RESET_MASK 0x02
104#define XIIC_CR_MSMS_MASK 0x04
105#define XIIC_CR_DIR_IS_TX_MASK 0x08
106#define XIIC_CR_NO_ACK_MASK 0x10
107#define XIIC_CR_REPEATED_START_MASK 0x20
108#define XIIC_CR_GENERAL_CALL_MASK 0x40
109
110
111#define XIIC_SR_GEN_CALL_MASK 0x01
112#define XIIC_SR_ADDR_AS_SLAVE_MASK 0x02
113#define XIIC_SR_BUS_BUSY_MASK 0x04
114#define XIIC_SR_MSTR_RDING_SLAVE_MASK 0x08
115#define XIIC_SR_TX_FIFO_FULL_MASK 0x10
116#define XIIC_SR_RX_FIFO_FULL_MASK 0x20
117#define XIIC_SR_RX_FIFO_EMPTY_MASK 0x40
118#define XIIC_SR_TX_FIFO_EMPTY_MASK 0x80
119
120
121#define XIIC_INTR_ARB_LOST_MASK 0x01
122#define XIIC_INTR_TX_ERROR_MASK 0x02
123#define XIIC_INTR_TX_EMPTY_MASK 0x04
124#define XIIC_INTR_RX_FULL_MASK 0x08
125#define XIIC_INTR_BNB_MASK 0x10
126#define XIIC_INTR_AAS_MASK 0x20
127#define XIIC_INTR_NAAS_MASK 0x40
128#define XIIC_INTR_TX_HALF_MASK 0x80
129
130
131#define IIC_RX_FIFO_DEPTH 16
132#define IIC_TX_FIFO_DEPTH 16
133
134
135
136
137#define XIIC_TX_INTERRUPTS \
138(XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)
139
140#define XIIC_TX_RX_INTERRUPTS (XIIC_INTR_RX_FULL_MASK | XIIC_TX_INTERRUPTS)
141
142
143
144
145#define XIIC_READ_OPERATION 1
146#define XIIC_WRITE_OPERATION 0
147
148
149
150
151#define XIIC_TX_DYN_START_MASK 0x0100
152#define XIIC_TX_DYN_STOP_MASK 0x0200
153
154
155
156
157
158
159
160#define XIIC_DGIER_OFFSET 0x1C
161#define XIIC_IISR_OFFSET 0x20
162#define XIIC_IIER_OFFSET 0x28
163#define XIIC_RESETR_OFFSET 0x40
164
165#define XIIC_RESET_MASK 0xAUL
166
167
168
169
170
171
172#define XIIC_GINTR_ENABLE_MASK 0x80000000UL
173
174#define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos)
175#define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos)
176
177static void xiic_start_xfer(struct xiic_i2c *i2c);
178static void __xiic_start_xfer(struct xiic_i2c *i2c);
179
180
181
182
183
184
185
186
187
188static inline void xiic_setreg8(struct xiic_i2c *i2c, int reg, u8 value)
189{
190 if (i2c->endianness == LITTLE)
191 iowrite8(value, i2c->base + reg);
192 else
193 iowrite8(value, i2c->base + reg + 3);
194}
195
196static inline u8 xiic_getreg8(struct xiic_i2c *i2c, int reg)
197{
198 u8 ret;
199
200 if (i2c->endianness == LITTLE)
201 ret = ioread8(i2c->base + reg);
202 else
203 ret = ioread8(i2c->base + reg + 3);
204 return ret;
205}
206
207static inline void xiic_setreg16(struct xiic_i2c *i2c, int reg, u16 value)
208{
209 if (i2c->endianness == LITTLE)
210 iowrite16(value, i2c->base + reg);
211 else
212 iowrite16be(value, i2c->base + reg + 2);
213}
214
215static inline void xiic_setreg32(struct xiic_i2c *i2c, int reg, int value)
216{
217 if (i2c->endianness == LITTLE)
218 iowrite32(value, i2c->base + reg);
219 else
220 iowrite32be(value, i2c->base + reg);
221}
222
223static inline int xiic_getreg32(struct xiic_i2c *i2c, int reg)
224{
225 u32 ret;
226
227 if (i2c->endianness == LITTLE)
228 ret = ioread32(i2c->base + reg);
229 else
230 ret = ioread32be(i2c->base + reg);
231 return ret;
232}
233
234static inline void xiic_irq_dis(struct xiic_i2c *i2c, u32 mask)
235{
236 u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
237 xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier & ~mask);
238}
239
240static inline void xiic_irq_en(struct xiic_i2c *i2c, u32 mask)
241{
242 u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
243 xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier | mask);
244}
245
246static inline void xiic_irq_clr(struct xiic_i2c *i2c, u32 mask)
247{
248 u32 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
249 xiic_setreg32(i2c, XIIC_IISR_OFFSET, isr & mask);
250}
251
252static inline void xiic_irq_clr_en(struct xiic_i2c *i2c, u32 mask)
253{
254 xiic_irq_clr(i2c, mask);
255 xiic_irq_en(i2c, mask);
256}
257
258static void xiic_clear_rx_fifo(struct xiic_i2c *i2c)
259{
260 u8 sr;
261 for (sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
262 !(sr & XIIC_SR_RX_FIFO_EMPTY_MASK);
263 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET))
264 xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
265}
266
267static void xiic_reinit(struct xiic_i2c *i2c)
268{
269 xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
270
271
272 xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, IIC_RX_FIFO_DEPTH - 1);
273
274
275 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
276
277
278 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_ENABLE_DEVICE_MASK);
279
280
281 xiic_clear_rx_fifo(i2c);
282
283
284 xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
285
286 xiic_irq_clr_en(i2c, XIIC_INTR_ARB_LOST_MASK);
287}
288
289static void xiic_deinit(struct xiic_i2c *i2c)
290{
291 u8 cr;
292
293 xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
294
295
296 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
297 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr & ~XIIC_CR_ENABLE_DEVICE_MASK);
298}
299
300static void xiic_read_rx(struct xiic_i2c *i2c)
301{
302 u8 bytes_in_fifo;
303 int i;
304
305 bytes_in_fifo = xiic_getreg8(i2c, XIIC_RFO_REG_OFFSET) + 1;
306
307 dev_dbg(i2c->adap.dev.parent,
308 "%s entry, bytes in fifo: %d, msg: %d, SR: 0x%x, CR: 0x%x\n",
309 __func__, bytes_in_fifo, xiic_rx_space(i2c),
310 xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
311 xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
312
313 if (bytes_in_fifo > xiic_rx_space(i2c))
314 bytes_in_fifo = xiic_rx_space(i2c);
315
316 for (i = 0; i < bytes_in_fifo; i++)
317 i2c->rx_msg->buf[i2c->rx_pos++] =
318 xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
319
320 xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET,
321 (xiic_rx_space(i2c) > IIC_RX_FIFO_DEPTH) ?
322 IIC_RX_FIFO_DEPTH - 1 : xiic_rx_space(i2c) - 1);
323}
324
325static int xiic_tx_fifo_space(struct xiic_i2c *i2c)
326{
327
328 return IIC_TX_FIFO_DEPTH - xiic_getreg8(i2c, XIIC_TFO_REG_OFFSET) - 1;
329}
330
331static void xiic_fill_tx_fifo(struct xiic_i2c *i2c)
332{
333 u8 fifo_space = xiic_tx_fifo_space(i2c);
334 int len = xiic_tx_space(i2c);
335
336 len = (len > fifo_space) ? fifo_space : len;
337
338 dev_dbg(i2c->adap.dev.parent, "%s entry, len: %d, fifo space: %d\n",
339 __func__, len, fifo_space);
340
341 while (len--) {
342 u16 data = i2c->tx_msg->buf[i2c->tx_pos++];
343 if ((xiic_tx_space(i2c) == 0) && (i2c->nmsgs == 1)) {
344
345 data |= XIIC_TX_DYN_STOP_MASK;
346 dev_dbg(i2c->adap.dev.parent, "%s TX STOP\n", __func__);
347 }
348 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
349 }
350}
351
352static void xiic_wakeup(struct xiic_i2c *i2c, int code)
353{
354 i2c->tx_msg = NULL;
355 i2c->rx_msg = NULL;
356 i2c->nmsgs = 0;
357 i2c->state = code;
358 wake_up(&i2c->wait);
359}
360
361static irqreturn_t xiic_process(int irq, void *dev_id)
362{
363 struct xiic_i2c *i2c = dev_id;
364 u32 pend, isr, ier;
365 u32 clr = 0;
366
367
368
369
370
371
372 spin_lock(&i2c->lock);
373 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
374 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
375 pend = isr & ier;
376
377 dev_dbg(i2c->adap.dev.parent, "%s: IER: 0x%x, ISR: 0x%x, pend: 0x%x\n",
378 __func__, ier, isr, pend);
379 dev_dbg(i2c->adap.dev.parent, "%s: SR: 0x%x, msg: %p, nmsgs: %d\n",
380 __func__, xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
381 i2c->tx_msg, i2c->nmsgs);
382
383
384
385 if ((pend & XIIC_INTR_ARB_LOST_MASK) ||
386 ((pend & XIIC_INTR_TX_ERROR_MASK) &&
387 !(pend & XIIC_INTR_RX_FULL_MASK))) {
388
389
390
391
392
393
394 dev_dbg(i2c->adap.dev.parent, "%s error\n", __func__);
395
396
397
398
399
400 xiic_reinit(i2c);
401
402 if (i2c->rx_msg)
403 xiic_wakeup(i2c, STATE_ERROR);
404 if (i2c->tx_msg)
405 xiic_wakeup(i2c, STATE_ERROR);
406 }
407 if (pend & XIIC_INTR_RX_FULL_MASK) {
408
409
410 clr |= XIIC_INTR_RX_FULL_MASK;
411 if (!i2c->rx_msg) {
412 dev_dbg(i2c->adap.dev.parent,
413 "%s unexpexted RX IRQ\n", __func__);
414 xiic_clear_rx_fifo(i2c);
415 goto out;
416 }
417
418 xiic_read_rx(i2c);
419 if (xiic_rx_space(i2c) == 0) {
420
421 i2c->rx_msg = NULL;
422
423
424 clr |= (isr & XIIC_INTR_TX_ERROR_MASK);
425
426 dev_dbg(i2c->adap.dev.parent,
427 "%s end of message, nmsgs: %d\n",
428 __func__, i2c->nmsgs);
429
430
431
432
433
434 if (i2c->nmsgs > 1) {
435 i2c->nmsgs--;
436 i2c->tx_msg++;
437 dev_dbg(i2c->adap.dev.parent,
438 "%s will start next...\n", __func__);
439
440 __xiic_start_xfer(i2c);
441 }
442 }
443 }
444 if (pend & XIIC_INTR_BNB_MASK) {
445
446 clr |= XIIC_INTR_BNB_MASK;
447
448
449 xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);
450
451 if (!i2c->tx_msg)
452 goto out;
453
454 if ((i2c->nmsgs == 1) && !i2c->rx_msg &&
455 xiic_tx_space(i2c) == 0)
456 xiic_wakeup(i2c, STATE_DONE);
457 else
458 xiic_wakeup(i2c, STATE_ERROR);
459 }
460 if (pend & (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)) {
461
462
463 clr |= (pend &
464 (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK));
465
466 if (!i2c->tx_msg) {
467 dev_dbg(i2c->adap.dev.parent,
468 "%s unexpexted TX IRQ\n", __func__);
469 goto out;
470 }
471
472 xiic_fill_tx_fifo(i2c);
473
474
475 if (!xiic_tx_space(i2c) && xiic_tx_fifo_space(i2c) >= 2) {
476 dev_dbg(i2c->adap.dev.parent,
477 "%s end of message sent, nmsgs: %d\n",
478 __func__, i2c->nmsgs);
479 if (i2c->nmsgs > 1) {
480 i2c->nmsgs--;
481 i2c->tx_msg++;
482 __xiic_start_xfer(i2c);
483 } else {
484 xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
485
486 dev_dbg(i2c->adap.dev.parent,
487 "%s Got TX IRQ but no more to do...\n",
488 __func__);
489 }
490 } else if (!xiic_tx_space(i2c) && (i2c->nmsgs == 1))
491
492
493
494 xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
495 }
496out:
497 dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr);
498
499 xiic_setreg32(i2c, XIIC_IISR_OFFSET, clr);
500 spin_unlock(&i2c->lock);
501 return IRQ_HANDLED;
502}
503
504static int xiic_bus_busy(struct xiic_i2c *i2c)
505{
506 u8 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
507
508 return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0;
509}
510
511static int xiic_busy(struct xiic_i2c *i2c)
512{
513 int tries = 3;
514 int err;
515
516 if (i2c->tx_msg)
517 return -EBUSY;
518
519
520
521
522
523 err = xiic_bus_busy(i2c);
524 while (err && tries--) {
525 msleep(1);
526 err = xiic_bus_busy(i2c);
527 }
528
529 return err;
530}
531
532static void xiic_start_recv(struct xiic_i2c *i2c)
533{
534 u8 rx_watermark;
535 struct i2c_msg *msg = i2c->rx_msg = i2c->tx_msg;
536
537
538 xiic_irq_clr_en(i2c, XIIC_INTR_RX_FULL_MASK | XIIC_INTR_TX_ERROR_MASK);
539
540
541
542
543
544
545
546 rx_watermark = msg->len;
547 if (rx_watermark > IIC_RX_FIFO_DEPTH)
548 rx_watermark = IIC_RX_FIFO_DEPTH;
549 xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, rx_watermark - 1);
550
551 if (!(msg->flags & I2C_M_NOSTART))
552
553 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
554 (msg->addr << 1) | XIIC_READ_OPERATION |
555 XIIC_TX_DYN_START_MASK);
556
557 xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
558
559 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
560 msg->len | ((i2c->nmsgs == 1) ? XIIC_TX_DYN_STOP_MASK : 0));
561 if (i2c->nmsgs == 1)
562
563 xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
564
565
566 i2c->tx_pos = msg->len;
567}
568
569static void xiic_start_send(struct xiic_i2c *i2c)
570{
571 struct i2c_msg *msg = i2c->tx_msg;
572
573 xiic_irq_clr(i2c, XIIC_INTR_TX_ERROR_MASK);
574
575 dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, len: %d",
576 __func__, msg, msg->len);
577 dev_dbg(i2c->adap.dev.parent, "%s entry, ISR: 0x%x, CR: 0x%x\n",
578 __func__, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
579 xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
580
581 if (!(msg->flags & I2C_M_NOSTART)) {
582
583 u16 data = ((msg->addr << 1) & 0xfe) | XIIC_WRITE_OPERATION |
584 XIIC_TX_DYN_START_MASK;
585 if ((i2c->nmsgs == 1) && msg->len == 0)
586
587 data |= XIIC_TX_DYN_STOP_MASK;
588
589 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
590 }
591
592 xiic_fill_tx_fifo(i2c);
593
594
595 xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_ERROR_MASK |
596 XIIC_INTR_BNB_MASK);
597}
598
599static irqreturn_t xiic_isr(int irq, void *dev_id)
600{
601 struct xiic_i2c *i2c = dev_id;
602 u32 pend, isr, ier;
603 irqreturn_t ret = IRQ_NONE;
604
605
606
607
608 dev_dbg(i2c->adap.dev.parent, "%s entry\n", __func__);
609
610 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
611 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
612 pend = isr & ier;
613 if (pend)
614 ret = IRQ_WAKE_THREAD;
615
616 return ret;
617}
618
619static void __xiic_start_xfer(struct xiic_i2c *i2c)
620{
621 int first = 1;
622 int fifo_space = xiic_tx_fifo_space(i2c);
623 dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, fifos space: %d\n",
624 __func__, i2c->tx_msg, fifo_space);
625
626 if (!i2c->tx_msg)
627 return;
628
629 i2c->rx_pos = 0;
630 i2c->tx_pos = 0;
631 i2c->state = STATE_START;
632 while ((fifo_space >= 2) && (first || (i2c->nmsgs > 1))) {
633 if (!first) {
634 i2c->nmsgs--;
635 i2c->tx_msg++;
636 i2c->tx_pos = 0;
637 } else
638 first = 0;
639
640 if (i2c->tx_msg->flags & I2C_M_RD) {
641
642 xiic_start_recv(i2c);
643 return;
644 } else {
645 xiic_start_send(i2c);
646 if (xiic_tx_space(i2c) != 0) {
647
648 break;
649 }
650 }
651
652 fifo_space = xiic_tx_fifo_space(i2c);
653 }
654
655
656
657
658 if (i2c->nmsgs > 1 || xiic_tx_space(i2c))
659 xiic_irq_clr_en(i2c, XIIC_INTR_TX_HALF_MASK);
660
661}
662
663static void xiic_start_xfer(struct xiic_i2c *i2c)
664{
665
666 __xiic_start_xfer(i2c);
667}
668
669static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
670{
671 struct xiic_i2c *i2c = i2c_get_adapdata(adap);
672 int err;
673
674 dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,
675 xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));
676
677 err = xiic_busy(i2c);
678 if (err)
679 return err;
680
681 i2c->tx_msg = msgs;
682 i2c->nmsgs = num;
683
684 xiic_start_xfer(i2c);
685
686 if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
687 (i2c->state == STATE_DONE), HZ))
688 return (i2c->state == STATE_DONE) ? num : -EIO;
689 else {
690 i2c->tx_msg = NULL;
691 i2c->rx_msg = NULL;
692 i2c->nmsgs = 0;
693 return -ETIMEDOUT;
694 }
695}
696
697static u32 xiic_func(struct i2c_adapter *adap)
698{
699 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
700}
701
702static const struct i2c_algorithm xiic_algorithm = {
703 .master_xfer = xiic_xfer,
704 .functionality = xiic_func,
705};
706
707static struct i2c_adapter xiic_adapter = {
708 .owner = THIS_MODULE,
709 .name = DRIVER_NAME,
710 .class = I2C_CLASS_DEPRECATED,
711 .algo = &xiic_algorithm,
712};
713
714
715static int xiic_i2c_probe(struct platform_device *pdev)
716{
717 struct xiic_i2c *i2c;
718 struct xiic_i2c_platform_data *pdata;
719 struct resource *res;
720 int ret, irq;
721 u8 i;
722 u32 sr;
723
724 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
725 if (!i2c)
726 return -ENOMEM;
727
728 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
729 i2c->base = devm_ioremap_resource(&pdev->dev, res);
730 if (IS_ERR(i2c->base))
731 return PTR_ERR(i2c->base);
732
733 irq = platform_get_irq(pdev, 0);
734 if (irq < 0)
735 return irq;
736
737 pdata = dev_get_platdata(&pdev->dev);
738
739
740 platform_set_drvdata(pdev, i2c);
741 i2c->adap = xiic_adapter;
742 i2c_set_adapdata(&i2c->adap, i2c);
743 i2c->adap.dev.parent = &pdev->dev;
744 i2c->adap.dev.of_node = pdev->dev.of_node;
745
746 spin_lock_init(&i2c->lock);
747 init_waitqueue_head(&i2c->wait);
748
749 ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr,
750 xiic_process, IRQF_ONESHOT,
751 pdev->name, i2c);
752
753 if (ret < 0) {
754 dev_err(&pdev->dev, "Cannot claim IRQ\n");
755 return ret;
756 }
757
758
759
760
761
762
763 i2c->endianness = LITTLE;
764 xiic_setreg32(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
765
766 sr = xiic_getreg32(i2c, XIIC_SR_REG_OFFSET);
767 if (!(sr & XIIC_SR_TX_FIFO_EMPTY_MASK))
768 i2c->endianness = BIG;
769
770 xiic_reinit(i2c);
771
772
773 ret = i2c_add_adapter(&i2c->adap);
774 if (ret) {
775 dev_err(&pdev->dev, "Failed to add adapter\n");
776 xiic_deinit(i2c);
777 return ret;
778 }
779
780 if (pdata) {
781
782 for (i = 0; i < pdata->num_devices; i++)
783 i2c_new_device(&i2c->adap, pdata->devices + i);
784 }
785
786 return 0;
787}
788
789static int xiic_i2c_remove(struct platform_device *pdev)
790{
791 struct xiic_i2c *i2c = platform_get_drvdata(pdev);
792
793
794 i2c_del_adapter(&i2c->adap);
795
796 xiic_deinit(i2c);
797
798 return 0;
799}
800
801#if defined(CONFIG_OF)
802static const struct of_device_id xiic_of_match[] = {
803 { .compatible = "xlnx,xps-iic-2.00.a", },
804 {},
805};
806MODULE_DEVICE_TABLE(of, xiic_of_match);
807#endif
808
809static struct platform_driver xiic_i2c_driver = {
810 .probe = xiic_i2c_probe,
811 .remove = xiic_i2c_remove,
812 .driver = {
813 .name = DRIVER_NAME,
814 .of_match_table = of_match_ptr(xiic_of_match),
815 },
816};
817
818module_platform_driver(xiic_i2c_driver);
819
820MODULE_AUTHOR("info@mocean-labs.com");
821MODULE_DESCRIPTION("Xilinx I2C bus driver");
822MODULE_LICENSE("GPL v2");
823MODULE_ALIAS("platform:"DRIVER_NAME);
824