1
2
3
4
5
6
7
8
9
10#include <linux/clk.h>
11#include <linux/clkdev.h>
12#include <linux/delay.h>
13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/module.h>
18#include <linux/io.h>
19#include <linux/kernel.h>
20#include <linux/platform_device.h>
21
22#define SCL_WAIT_TIMEOUT_NS 25000000
23#define I2C_XFER_TIMEOUT (msecs_to_jiffies(250))
24#define I2C_STOP_TIMEOUT (msecs_to_jiffies(100))
25#define FIFO_SIZE 8
26#define SEQ_LEN 2
27
28#define GLOBAL_CONTROL 0x00
29#define GLOBAL_MST_EN BIT(0)
30#define GLOBAL_SLV_EN BIT(1)
31#define GLOBAL_IBML_EN BIT(2)
32#define INTERRUPT_STATUS 0x04
33#define INTERRUPT_ENABLE 0x08
34#define INT_SLV BIT(1)
35#define INT_MST BIT(0)
36#define WAIT_TIMER_CONTROL 0x0c
37#define WT_EN BIT(15)
38#define WT_VALUE(_x) ((_x) & 0x7fff)
39#define IBML_TIMEOUT 0x10
40#define IBML_LOW_MEXT 0x14
41#define IBML_LOW_SEXT 0x18
42#define TIMER_CLOCK_DIV 0x1c
43#define I2C_BUS_MONITOR 0x20
44#define BM_SDAC BIT(3)
45#define BM_SCLC BIT(2)
46#define BM_SDAS BIT(1)
47#define BM_SCLS BIT(0)
48#define SOFT_RESET 0x24
49#define MST_COMMAND 0x28
50#define CMD_BUSY (1<<3)
51#define CMD_MANUAL (0x00 | CMD_BUSY)
52#define CMD_AUTO (0x01 | CMD_BUSY)
53#define CMD_SEQUENCE (0x02 | CMD_BUSY)
54#define MST_RX_XFER 0x2c
55#define MST_TX_XFER 0x30
56#define MST_ADDR_1 0x34
57#define MST_ADDR_2 0x38
58#define MST_DATA 0x3c
59#define MST_TX_FIFO 0x40
60#define MST_RX_FIFO 0x44
61#define MST_INT_ENABLE 0x48
62#define MST_INT_STATUS 0x4c
63#define MST_STATUS_RFL (1 << 13)
64#define MST_STATUS_TFL (1 << 12)
65#define MST_STATUS_SNS (1 << 11)
66#define MST_STATUS_SS (1 << 10)
67#define MST_STATUS_SCC (1 << 9)
68#define MST_STATUS_IP (1 << 8)
69#define MST_STATUS_TSS (1 << 7)
70#define MST_STATUS_AL (1 << 6)
71#define MST_STATUS_ND (1 << 5)
72#define MST_STATUS_NA (1 << 4)
73#define MST_STATUS_NAK (MST_STATUS_NA | \
74 MST_STATUS_ND)
75#define MST_STATUS_ERR (MST_STATUS_NAK | \
76 MST_STATUS_AL | \
77 MST_STATUS_IP)
78#define MST_TX_BYTES_XFRD 0x50
79#define MST_RX_BYTES_XFRD 0x54
80#define SLV_ADDR_DEC_CTL 0x58
81#define SLV_ADDR_DEC_GCE BIT(0)
82#define SLV_ADDR_DEC_OGCE BIT(1)
83#define SLV_ADDR_DEC_SA1E BIT(2)
84#define SLV_ADDR_DEC_SA1M BIT(3)
85#define SLV_ADDR_DEC_SA2E BIT(4)
86#define SLV_ADDR_DEC_SA2M BIT(5)
87#define SLV_ADDR_1 0x5c
88#define SLV_ADDR_2 0x60
89#define SLV_RX_CTL 0x64
90#define SLV_RX_ACSA1 BIT(0)
91#define SLV_RX_ACSA2 BIT(1)
92#define SLV_RX_ACGCA BIT(2)
93#define SLV_DATA 0x68
94#define SLV_RX_FIFO 0x6c
95#define SLV_FIFO_DV1 BIT(0)
96#define SLV_FIFO_DV2 BIT(1)
97#define SLV_FIFO_AS BIT(2)
98#define SLV_FIFO_TNAK BIT(3)
99#define SLV_FIFO_STRC BIT(4)
100#define SLV_FIFO_RSC BIT(5)
101#define SLV_FIFO_STPC BIT(6)
102#define SLV_FIFO_DV (SLV_FIFO_DV1 | SLV_FIFO_DV2)
103#define SLV_INT_ENABLE 0x70
104#define SLV_INT_STATUS 0x74
105#define SLV_STATUS_RFH BIT(0)
106#define SLV_STATUS_WTC BIT(1)
107#define SLV_STATUS_SRS1 BIT(2)
108#define SLV_STATUS_SRRS1 BIT(3)
109#define SLV_STATUS_SRND1 BIT(4)
110#define SLV_STATUS_SRC1 BIT(5)
111#define SLV_STATUS_SRAT1 BIT(6)
112#define SLV_STATUS_SRDRE1 BIT(7)
113#define SLV_READ_DUMMY 0x78
114#define SCL_HIGH_PERIOD 0x80
115#define SCL_LOW_PERIOD 0x84
116#define SPIKE_FLTR_LEN 0x88
117#define SDA_SETUP_TIME 0x8c
118#define SDA_HOLD_TIME 0x90
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135struct axxia_i2c_dev {
136 void __iomem *base;
137 struct i2c_msg *msg;
138 struct i2c_msg *msg_r;
139 size_t msg_xfrd;
140 size_t msg_xfrd_r;
141 int msg_err;
142 struct completion msg_complete;
143 struct device *dev;
144 struct i2c_adapter adapter;
145 struct clk *i2c_clk;
146 u32 bus_clk_rate;
147 bool last;
148 struct i2c_client *slave;
149 int irq;
150};
151
152static void i2c_int_disable(struct axxia_i2c_dev *idev, u32 mask)
153{
154 u32 int_en;
155
156 int_en = readl(idev->base + MST_INT_ENABLE);
157 writel(int_en & ~mask, idev->base + MST_INT_ENABLE);
158}
159
160static void i2c_int_enable(struct axxia_i2c_dev *idev, u32 mask)
161{
162 u32 int_en;
163
164 int_en = readl(idev->base + MST_INT_ENABLE);
165 writel(int_en | mask, idev->base + MST_INT_ENABLE);
166}
167
168
169
170
171static u32 ns_to_clk(u64 ns, u32 clk_mhz)
172{
173 return div_u64(ns * clk_mhz, 1000);
174}
175
176static int axxia_i2c_init(struct axxia_i2c_dev *idev)
177{
178 u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate;
179 u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000;
180 u32 t_setup;
181 u32 t_high, t_low;
182 u32 tmo_clk;
183 u32 prescale;
184 unsigned long timeout;
185
186 dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n",
187 idev->bus_clk_rate, clk_mhz, divisor);
188
189
190 writel(0x01, idev->base + SOFT_RESET);
191 timeout = jiffies + msecs_to_jiffies(100);
192 while (readl(idev->base + SOFT_RESET) & 1) {
193 if (time_after(jiffies, timeout)) {
194 dev_warn(idev->dev, "Soft reset failed\n");
195 break;
196 }
197 }
198
199
200 writel(0x1, idev->base + GLOBAL_CONTROL);
201
202 if (idev->bus_clk_rate <= I2C_MAX_STANDARD_MODE_FREQ) {
203
204 t_high = divisor * 1 / 2;
205 t_low = divisor * 1 / 2;
206 t_setup = ns_to_clk(250, clk_mhz);
207 } else {
208
209 t_high = divisor * 1 / 3;
210 t_low = divisor * 2 / 3;
211 t_setup = ns_to_clk(100, clk_mhz);
212 }
213
214
215 writel(t_high, idev->base + SCL_HIGH_PERIOD);
216
217 writel(t_low, idev->base + SCL_LOW_PERIOD);
218
219 writel(t_setup, idev->base + SDA_SETUP_TIME);
220
221 writel(ns_to_clk(300, clk_mhz), idev->base + SDA_HOLD_TIME);
222
223 writel(ns_to_clk(50, clk_mhz), idev->base + SPIKE_FLTR_LEN);
224
225
226 tmo_clk = ns_to_clk(SCL_WAIT_TIMEOUT_NS, clk_mhz);
227
228
229 for (prescale = 0; prescale < 15; ++prescale) {
230 if (tmo_clk <= 0x7fff)
231 break;
232 tmo_clk >>= 1;
233 }
234 if (tmo_clk > 0x7fff)
235 tmo_clk = 0x7fff;
236
237
238 writel(prescale, idev->base + TIMER_CLOCK_DIV);
239
240 writel(WT_EN | WT_VALUE(tmo_clk), idev->base + WAIT_TIMER_CONTROL);
241
242
243 i2c_int_disable(idev, ~0);
244
245
246 writel(0x01, idev->base + INTERRUPT_ENABLE);
247
248 return 0;
249}
250
251static int i2c_m_rd(const struct i2c_msg *msg)
252{
253 return (msg->flags & I2C_M_RD) != 0;
254}
255
256static int i2c_m_ten(const struct i2c_msg *msg)
257{
258 return (msg->flags & I2C_M_TEN) != 0;
259}
260
261static int i2c_m_recv_len(const struct i2c_msg *msg)
262{
263 return (msg->flags & I2C_M_RECV_LEN) != 0;
264}
265
266
267
268
269
270static int axxia_i2c_empty_rx_fifo(struct axxia_i2c_dev *idev)
271{
272 struct i2c_msg *msg = idev->msg_r;
273 size_t rx_fifo_avail = readl(idev->base + MST_RX_FIFO);
274 int bytes_to_transfer = min(rx_fifo_avail, msg->len - idev->msg_xfrd_r);
275
276 while (bytes_to_transfer-- > 0) {
277 int c = readl(idev->base + MST_DATA);
278
279 if (idev->msg_xfrd_r == 0 && i2c_m_recv_len(msg)) {
280
281
282
283 if (c <= 0 || c > I2C_SMBUS_BLOCK_MAX) {
284 idev->msg_err = -EPROTO;
285 i2c_int_disable(idev, ~MST_STATUS_TSS);
286 complete(&idev->msg_complete);
287 break;
288 }
289 msg->len = 1 + c;
290 writel(msg->len, idev->base + MST_RX_XFER);
291 }
292 msg->buf[idev->msg_xfrd_r++] = c;
293 }
294
295 return 0;
296}
297
298
299
300
301
302static int axxia_i2c_fill_tx_fifo(struct axxia_i2c_dev *idev)
303{
304 struct i2c_msg *msg = idev->msg;
305 size_t tx_fifo_avail = FIFO_SIZE - readl(idev->base + MST_TX_FIFO);
306 int bytes_to_transfer = min(tx_fifo_avail, msg->len - idev->msg_xfrd);
307 int ret = msg->len - idev->msg_xfrd - bytes_to_transfer;
308
309 while (bytes_to_transfer-- > 0)
310 writel(msg->buf[idev->msg_xfrd++], idev->base + MST_DATA);
311
312 return ret;
313}
314
315static void axxia_i2c_slv_fifo_event(struct axxia_i2c_dev *idev)
316{
317 u32 fifo_status = readl(idev->base + SLV_RX_FIFO);
318 u8 val;
319
320 dev_dbg(idev->dev, "slave irq fifo_status=0x%x\n", fifo_status);
321
322 if (fifo_status & SLV_FIFO_DV1) {
323 if (fifo_status & SLV_FIFO_STRC)
324 i2c_slave_event(idev->slave,
325 I2C_SLAVE_WRITE_REQUESTED, &val);
326
327 val = readl(idev->base + SLV_DATA);
328 i2c_slave_event(idev->slave, I2C_SLAVE_WRITE_RECEIVED, &val);
329 }
330 if (fifo_status & SLV_FIFO_STPC) {
331 readl(idev->base + SLV_DATA);
332 i2c_slave_event(idev->slave, I2C_SLAVE_STOP, &val);
333 }
334 if (fifo_status & SLV_FIFO_RSC)
335 readl(idev->base + SLV_DATA);
336}
337
338static irqreturn_t axxia_i2c_slv_isr(struct axxia_i2c_dev *idev)
339{
340 u32 status = readl(idev->base + SLV_INT_STATUS);
341 u8 val;
342
343 dev_dbg(idev->dev, "slave irq status=0x%x\n", status);
344
345 if (status & SLV_STATUS_RFH)
346 axxia_i2c_slv_fifo_event(idev);
347 if (status & SLV_STATUS_SRS1) {
348 i2c_slave_event(idev->slave, I2C_SLAVE_READ_REQUESTED, &val);
349 writel(val, idev->base + SLV_DATA);
350 }
351 if (status & SLV_STATUS_SRND1) {
352 i2c_slave_event(idev->slave, I2C_SLAVE_READ_PROCESSED, &val);
353 writel(val, idev->base + SLV_DATA);
354 }
355 if (status & SLV_STATUS_SRC1)
356 i2c_slave_event(idev->slave, I2C_SLAVE_STOP, &val);
357
358 writel(INT_SLV, idev->base + INTERRUPT_STATUS);
359 return IRQ_HANDLED;
360}
361
362static irqreturn_t axxia_i2c_isr(int irq, void *_dev)
363{
364 struct axxia_i2c_dev *idev = _dev;
365 irqreturn_t ret = IRQ_NONE;
366 u32 status;
367
368 status = readl(idev->base + INTERRUPT_STATUS);
369
370 if (status & INT_SLV)
371 ret = axxia_i2c_slv_isr(idev);
372 if (!(status & INT_MST))
373 return ret;
374
375
376 status = readl(idev->base + MST_INT_STATUS);
377
378 if (!idev->msg) {
379 dev_warn(idev->dev, "unexpected interrupt\n");
380 goto out;
381 }
382
383
384 if (i2c_m_rd(idev->msg_r) && (status & MST_STATUS_RFL))
385 axxia_i2c_empty_rx_fifo(idev);
386
387
388 if (!i2c_m_rd(idev->msg) && (status & MST_STATUS_TFL)) {
389 if (axxia_i2c_fill_tx_fifo(idev) == 0)
390 i2c_int_disable(idev, MST_STATUS_TFL);
391 }
392
393 if (unlikely(status & MST_STATUS_ERR)) {
394
395 i2c_int_disable(idev, ~0);
396 if (status & MST_STATUS_AL)
397 idev->msg_err = -EAGAIN;
398 else if (status & MST_STATUS_NAK)
399 idev->msg_err = -ENXIO;
400 else
401 idev->msg_err = -EIO;
402 dev_dbg(idev->dev, "error %#x, addr=%#x rx=%u/%u tx=%u/%u\n",
403 status,
404 idev->msg->addr,
405 readl(idev->base + MST_RX_BYTES_XFRD),
406 readl(idev->base + MST_RX_XFER),
407 readl(idev->base + MST_TX_BYTES_XFRD),
408 readl(idev->base + MST_TX_XFER));
409 complete(&idev->msg_complete);
410 } else if (status & MST_STATUS_SCC) {
411
412 i2c_int_disable(idev, ~MST_STATUS_TSS);
413 complete(&idev->msg_complete);
414 } else if (status & (MST_STATUS_SNS | MST_STATUS_SS)) {
415
416 int mask = idev->last ? ~0 : ~MST_STATUS_TSS;
417
418 i2c_int_disable(idev, mask);
419 if (i2c_m_rd(idev->msg_r) && idev->msg_xfrd_r < idev->msg_r->len)
420 axxia_i2c_empty_rx_fifo(idev);
421 complete(&idev->msg_complete);
422 } else if (status & MST_STATUS_TSS) {
423
424 idev->msg_err = -ETIMEDOUT;
425 i2c_int_disable(idev, ~MST_STATUS_TSS);
426 complete(&idev->msg_complete);
427 }
428
429out:
430
431 writel(INT_MST, idev->base + INTERRUPT_STATUS);
432
433 return IRQ_HANDLED;
434}
435
436static void axxia_i2c_set_addr(struct axxia_i2c_dev *idev, struct i2c_msg *msg)
437{
438 u32 addr_1, addr_2;
439
440 if (i2c_m_ten(msg)) {
441
442
443
444
445 addr_1 = 0xF0 | ((msg->addr >> 7) & 0x06);
446 if (i2c_m_rd(msg))
447 addr_1 |= 1;
448 addr_2 = msg->addr & 0xFF;
449 } else {
450
451
452
453
454 addr_1 = i2c_8bit_addr_from_msg(msg);
455 addr_2 = 0;
456 }
457
458 writel(addr_1, idev->base + MST_ADDR_1);
459 writel(addr_2, idev->base + MST_ADDR_2);
460}
461
462
463
464
465
466static int axxia_i2c_handle_seq_nak(struct axxia_i2c_dev *idev)
467{
468 unsigned long timeout = jiffies + I2C_XFER_TIMEOUT;
469
470 do {
471 if ((readl(idev->base + MST_COMMAND) & CMD_BUSY) == 0)
472 return 0;
473 usleep_range(1, 100);
474 } while (time_before(jiffies, timeout));
475
476 return -ETIMEDOUT;
477}
478
479static int axxia_i2c_xfer_seq(struct axxia_i2c_dev *idev, struct i2c_msg msgs[])
480{
481 u32 int_mask = MST_STATUS_ERR | MST_STATUS_SS | MST_STATUS_RFL;
482 u32 rlen = i2c_m_recv_len(&msgs[1]) ? I2C_SMBUS_BLOCK_MAX : msgs[1].len;
483 unsigned long time_left;
484
485 axxia_i2c_set_addr(idev, &msgs[0]);
486
487 writel(msgs[0].len, idev->base + MST_TX_XFER);
488 writel(rlen, idev->base + MST_RX_XFER);
489
490 idev->msg = &msgs[0];
491 idev->msg_r = &msgs[1];
492 idev->msg_xfrd = 0;
493 idev->msg_xfrd_r = 0;
494 idev->last = true;
495 axxia_i2c_fill_tx_fifo(idev);
496
497 writel(CMD_SEQUENCE, idev->base + MST_COMMAND);
498
499 reinit_completion(&idev->msg_complete);
500 i2c_int_enable(idev, int_mask);
501
502 time_left = wait_for_completion_timeout(&idev->msg_complete,
503 I2C_XFER_TIMEOUT);
504
505 if (idev->msg_err == -ENXIO) {
506 if (axxia_i2c_handle_seq_nak(idev))
507 axxia_i2c_init(idev);
508 } else if (readl(idev->base + MST_COMMAND) & CMD_BUSY) {
509 dev_warn(idev->dev, "busy after xfer\n");
510 }
511
512 if (time_left == 0) {
513 idev->msg_err = -ETIMEDOUT;
514 i2c_recover_bus(&idev->adapter);
515 axxia_i2c_init(idev);
516 }
517
518 if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO)
519 axxia_i2c_init(idev);
520
521 return idev->msg_err;
522}
523
524static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg,
525 bool last)
526{
527 u32 int_mask = MST_STATUS_ERR;
528 u32 rx_xfer, tx_xfer;
529 unsigned long time_left;
530 unsigned int wt_value;
531
532 idev->msg = msg;
533 idev->msg_r = msg;
534 idev->msg_xfrd = 0;
535 idev->msg_xfrd_r = 0;
536 idev->last = last;
537 reinit_completion(&idev->msg_complete);
538
539 axxia_i2c_set_addr(idev, msg);
540
541 if (i2c_m_rd(msg)) {
542
543 rx_xfer = i2c_m_recv_len(msg) ? I2C_SMBUS_BLOCK_MAX : msg->len;
544 tx_xfer = 0;
545 } else {
546
547 rx_xfer = 0;
548 tx_xfer = msg->len;
549 }
550
551 writel(rx_xfer, idev->base + MST_RX_XFER);
552 writel(tx_xfer, idev->base + MST_TX_XFER);
553
554 if (i2c_m_rd(msg))
555 int_mask |= MST_STATUS_RFL;
556 else if (axxia_i2c_fill_tx_fifo(idev) != 0)
557 int_mask |= MST_STATUS_TFL;
558
559 wt_value = WT_VALUE(readl(idev->base + WAIT_TIMER_CONTROL));
560
561 writel(wt_value, idev->base + WAIT_TIMER_CONTROL);
562
563 if (idev->msg_err)
564 goto out;
565
566 if (!last) {
567 writel(CMD_MANUAL, idev->base + MST_COMMAND);
568 int_mask |= MST_STATUS_SNS;
569 } else {
570 writel(CMD_AUTO, idev->base + MST_COMMAND);
571 int_mask |= MST_STATUS_SS;
572 }
573
574 writel(WT_EN | wt_value, idev->base + WAIT_TIMER_CONTROL);
575
576 i2c_int_enable(idev, int_mask);
577
578 time_left = wait_for_completion_timeout(&idev->msg_complete,
579 I2C_XFER_TIMEOUT);
580
581 i2c_int_disable(idev, int_mask);
582
583 if (readl(idev->base + MST_COMMAND) & CMD_BUSY)
584 dev_warn(idev->dev, "busy after xfer\n");
585
586 if (time_left == 0) {
587 idev->msg_err = -ETIMEDOUT;
588 i2c_recover_bus(&idev->adapter);
589 axxia_i2c_init(idev);
590 }
591
592out:
593 if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO &&
594 idev->msg_err != -ETIMEDOUT)
595 axxia_i2c_init(idev);
596
597 return idev->msg_err;
598}
599
600
601
602
603
604
605static bool axxia_i2c_sequence_ok(struct i2c_msg msgs[], int num)
606{
607 return num == SEQ_LEN && !i2c_m_rd(&msgs[0]) && i2c_m_rd(&msgs[1]) &&
608 msgs[0].len > 0 && msgs[0].len <= FIFO_SIZE &&
609 msgs[1].len > 0 && msgs[0].addr == msgs[1].addr;
610}
611
612static int
613axxia_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
614{
615 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
616 int i;
617 int ret = 0;
618
619 idev->msg_err = 0;
620
621 if (axxia_i2c_sequence_ok(msgs, num)) {
622 ret = axxia_i2c_xfer_seq(idev, msgs);
623 return ret ? : SEQ_LEN;
624 }
625
626 i2c_int_enable(idev, MST_STATUS_TSS);
627
628 for (i = 0; ret == 0 && i < num; ++i)
629 ret = axxia_i2c_xfer_msg(idev, &msgs[i], i == (num - 1));
630
631 return ret ? : i;
632}
633
634static int axxia_i2c_get_scl(struct i2c_adapter *adap)
635{
636 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
637
638 return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SCLS);
639}
640
641static void axxia_i2c_set_scl(struct i2c_adapter *adap, int val)
642{
643 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
644 u32 tmp;
645
646
647 tmp = readl(idev->base + I2C_BUS_MONITOR) & BM_SDAC;
648 if (!val)
649 tmp |= BM_SCLC;
650 writel(tmp, idev->base + I2C_BUS_MONITOR);
651}
652
653static int axxia_i2c_get_sda(struct i2c_adapter *adap)
654{
655 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
656
657 return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SDAS);
658}
659
660static struct i2c_bus_recovery_info axxia_i2c_recovery_info = {
661 .recover_bus = i2c_generic_scl_recovery,
662 .get_scl = axxia_i2c_get_scl,
663 .set_scl = axxia_i2c_set_scl,
664 .get_sda = axxia_i2c_get_sda,
665};
666
667static u32 axxia_i2c_func(struct i2c_adapter *adap)
668{
669 u32 caps = (I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
670 I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA);
671 return caps;
672}
673
674static int axxia_i2c_reg_slave(struct i2c_client *slave)
675{
676 struct axxia_i2c_dev *idev = i2c_get_adapdata(slave->adapter);
677 u32 slv_int_mask = SLV_STATUS_RFH;
678 u32 dec_ctl;
679
680 if (idev->slave)
681 return -EBUSY;
682
683 idev->slave = slave;
684
685
686 writel(GLOBAL_MST_EN | GLOBAL_SLV_EN, idev->base + GLOBAL_CONTROL);
687 writel(INT_MST | INT_SLV, idev->base + INTERRUPT_ENABLE);
688
689
690 dec_ctl = SLV_ADDR_DEC_SA1E;
691 if (slave->flags & I2C_CLIENT_TEN)
692 dec_ctl |= SLV_ADDR_DEC_SA1M;
693
694 writel(SLV_RX_ACSA1, idev->base + SLV_RX_CTL);
695 writel(dec_ctl, idev->base + SLV_ADDR_DEC_CTL);
696 writel(slave->addr, idev->base + SLV_ADDR_1);
697
698
699 slv_int_mask |= SLV_STATUS_SRS1 | SLV_STATUS_SRRS1 | SLV_STATUS_SRND1;
700 slv_int_mask |= SLV_STATUS_SRC1;
701 writel(slv_int_mask, idev->base + SLV_INT_ENABLE);
702
703 return 0;
704}
705
706static int axxia_i2c_unreg_slave(struct i2c_client *slave)
707{
708 struct axxia_i2c_dev *idev = i2c_get_adapdata(slave->adapter);
709
710
711 writel(GLOBAL_MST_EN, idev->base + GLOBAL_CONTROL);
712 writel(INT_MST, idev->base + INTERRUPT_ENABLE);
713
714 synchronize_irq(idev->irq);
715
716 idev->slave = NULL;
717
718 return 0;
719}
720
721static const struct i2c_algorithm axxia_i2c_algo = {
722 .master_xfer = axxia_i2c_xfer,
723 .functionality = axxia_i2c_func,
724 .reg_slave = axxia_i2c_reg_slave,
725 .unreg_slave = axxia_i2c_unreg_slave,
726};
727
728static const struct i2c_adapter_quirks axxia_i2c_quirks = {
729 .max_read_len = 255,
730 .max_write_len = 255,
731};
732
733static int axxia_i2c_probe(struct platform_device *pdev)
734{
735 struct device_node *np = pdev->dev.of_node;
736 struct axxia_i2c_dev *idev = NULL;
737 void __iomem *base;
738 int ret = 0;
739
740 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
741 if (!idev)
742 return -ENOMEM;
743
744 base = devm_platform_ioremap_resource(pdev, 0);
745 if (IS_ERR(base))
746 return PTR_ERR(base);
747
748 idev->irq = platform_get_irq(pdev, 0);
749 if (idev->irq < 0)
750 return idev->irq;
751
752 idev->i2c_clk = devm_clk_get(&pdev->dev, "i2c");
753 if (IS_ERR(idev->i2c_clk)) {
754 dev_err(&pdev->dev, "missing clock\n");
755 return PTR_ERR(idev->i2c_clk);
756 }
757
758 idev->base = base;
759 idev->dev = &pdev->dev;
760 init_completion(&idev->msg_complete);
761
762 of_property_read_u32(np, "clock-frequency", &idev->bus_clk_rate);
763 if (idev->bus_clk_rate == 0)
764 idev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ;
765
766 ret = clk_prepare_enable(idev->i2c_clk);
767 if (ret) {
768 dev_err(&pdev->dev, "failed to enable clock\n");
769 return ret;
770 }
771
772 ret = axxia_i2c_init(idev);
773 if (ret) {
774 dev_err(&pdev->dev, "failed to initialize\n");
775 goto error_disable_clk;
776 }
777
778 ret = devm_request_irq(&pdev->dev, idev->irq, axxia_i2c_isr, 0,
779 pdev->name, idev);
780 if (ret) {
781 dev_err(&pdev->dev, "failed to claim IRQ%d\n", idev->irq);
782 goto error_disable_clk;
783 }
784
785 i2c_set_adapdata(&idev->adapter, idev);
786 strlcpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name));
787 idev->adapter.owner = THIS_MODULE;
788 idev->adapter.algo = &axxia_i2c_algo;
789 idev->adapter.bus_recovery_info = &axxia_i2c_recovery_info;
790 idev->adapter.quirks = &axxia_i2c_quirks;
791 idev->adapter.dev.parent = &pdev->dev;
792 idev->adapter.dev.of_node = pdev->dev.of_node;
793
794 platform_set_drvdata(pdev, idev);
795
796 ret = i2c_add_adapter(&idev->adapter);
797 if (ret)
798 goto error_disable_clk;
799
800 return 0;
801
802error_disable_clk:
803 clk_disable_unprepare(idev->i2c_clk);
804 return ret;
805}
806
807static int axxia_i2c_remove(struct platform_device *pdev)
808{
809 struct axxia_i2c_dev *idev = platform_get_drvdata(pdev);
810
811 clk_disable_unprepare(idev->i2c_clk);
812 i2c_del_adapter(&idev->adapter);
813
814 return 0;
815}
816
817
818static const struct of_device_id axxia_i2c_of_match[] = {
819 { .compatible = "lsi,api2c", },
820 {},
821};
822
823MODULE_DEVICE_TABLE(of, axxia_i2c_of_match);
824
825static struct platform_driver axxia_i2c_driver = {
826 .probe = axxia_i2c_probe,
827 .remove = axxia_i2c_remove,
828 .driver = {
829 .name = "axxia-i2c",
830 .of_match_table = axxia_i2c_of_match,
831 },
832};
833
834module_platform_driver(axxia_i2c_driver);
835
836MODULE_DESCRIPTION("Axxia I2C Bus driver");
837MODULE_AUTHOR("Anders Berg <anders.berg@lsi.com>");
838MODULE_LICENSE("GPL v2");
839