Lines Matching +full:i2c +full:- +full:0
1 // SPDX-License-Identifier: GPL-2.0
3 * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
4 * (https://opencores.org/project/i2c/overview)
19 #include <linux/i2c.h>
22 #include <linux/platform_data/i2c-ocores.h>
49 void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
50 u8 (*getreg)(struct ocores_i2c *i2c, int reg);
54 #define OCI2C_PRELOW 0
61 #define OCI2C_CTRL_IEN 0x40
62 #define OCI2C_CTRL_EN 0x80
64 #define OCI2C_CMD_START 0x91
65 #define OCI2C_CMD_STOP 0x41
66 #define OCI2C_CMD_READ 0x21
67 #define OCI2C_CMD_WRITE 0x11
68 #define OCI2C_CMD_READ_ACK 0x21
69 #define OCI2C_CMD_READ_NACK 0x29
70 #define OCI2C_CMD_IACK 0x01
72 #define OCI2C_STAT_IF 0x01
73 #define OCI2C_STAT_TIP 0x02
74 #define OCI2C_STAT_ARBLOST 0x20
75 #define OCI2C_STAT_BUSY 0x40
76 #define OCI2C_STAT_NACK 0x80
78 #define STATE_DONE 0
84 #define TYPE_OCORES 0
87 #define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */
89 static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_8() argument
91 iowrite8(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_8()
94 static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_16() argument
96 iowrite16(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_16()
99 static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_32() argument
101 iowrite32(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_32()
104 static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_16be() argument
106 iowrite16be(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_16be()
109 static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_32be() argument
111 iowrite32be(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_32be()
114 static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg) in oc_getreg_8() argument
116 return ioread8(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_8()
119 static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg) in oc_getreg_16() argument
121 return ioread16(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_16()
124 static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg) in oc_getreg_32() argument
126 return ioread32(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_32()
129 static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg) in oc_getreg_16be() argument
131 return ioread16be(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_16be()
134 static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg) in oc_getreg_32be() argument
136 return ioread32be(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_32be()
139 static void oc_setreg_io_8(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_io_8() argument
141 outb(value, i2c->iobase + reg); in oc_setreg_io_8()
144 static inline u8 oc_getreg_io_8(struct ocores_i2c *i2c, int reg) in oc_getreg_io_8() argument
146 return inb(i2c->iobase + reg); in oc_getreg_io_8()
149 static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg() argument
151 i2c->setreg(i2c, reg, value); in oc_setreg()
154 static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg) in oc_getreg() argument
156 return i2c->getreg(i2c, reg); in oc_getreg()
159 static void ocores_process(struct ocores_i2c *i2c, u8 stat) in ocores_process() argument
161 struct i2c_msg *msg = i2c->msg; in ocores_process()
168 spin_lock_irqsave(&i2c->process_lock, flags); in ocores_process()
170 if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) { in ocores_process()
172 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); in ocores_process()
173 wake_up(&i2c->wait); in ocores_process()
179 i2c->state = STATE_ERROR; in ocores_process()
180 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
184 if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) { in ocores_process()
185 i2c->state = in ocores_process()
186 (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE; in ocores_process()
189 i2c->state = STATE_ERROR; in ocores_process()
190 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
194 msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA); in ocores_process()
198 if (i2c->pos == msg->len) { in ocores_process()
199 i2c->nmsgs--; in ocores_process()
200 i2c->msg++; in ocores_process()
201 i2c->pos = 0; in ocores_process()
202 msg = i2c->msg; in ocores_process()
204 if (i2c->nmsgs) { /* end? */ in ocores_process()
206 if (!(msg->flags & I2C_M_NOSTART)) { in ocores_process()
209 i2c->state = STATE_START; in ocores_process()
211 oc_setreg(i2c, OCI2C_DATA, addr); in ocores_process()
212 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); in ocores_process()
215 i2c->state = (msg->flags & I2C_M_RD) in ocores_process()
218 i2c->state = STATE_DONE; in ocores_process()
219 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
224 if (i2c->state == STATE_READ) { in ocores_process()
225 oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ? in ocores_process()
228 oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]); in ocores_process()
229 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE); in ocores_process()
233 spin_unlock_irqrestore(&i2c->process_lock, flags); in ocores_process()
238 struct ocores_i2c *i2c = dev_id; in ocores_isr() local
239 u8 stat = oc_getreg(i2c, OCI2C_STATUS); in ocores_isr()
241 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) { in ocores_isr()
247 ocores_process(i2c, stat); in ocores_isr()
253 * ocores_process_timeout() - Process timeout event
254 * @i2c: ocores I2C device instance
256 static void ocores_process_timeout(struct ocores_i2c *i2c) in ocores_process_timeout() argument
260 spin_lock_irqsave(&i2c->process_lock, flags); in ocores_process_timeout()
261 i2c->state = STATE_ERROR; in ocores_process_timeout()
262 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process_timeout()
263 spin_unlock_irqrestore(&i2c->process_lock, flags); in ocores_process_timeout()
267 * ocores_wait() - Wait until something change in a given register
268 * @i2c: ocores I2C device instance
277 * Return: 0 on success, -ETIMEDOUT on timeout
279 static int ocores_wait(struct ocores_i2c *i2c, in ocores_wait() argument
287 u8 status = oc_getreg(i2c, reg); in ocores_wait()
293 return -ETIMEDOUT; in ocores_wait()
295 return 0; in ocores_wait()
299 * ocores_poll_wait() - Wait until is possible to process some data
300 * @i2c: ocores I2C device instance
304 * Return: 0 on success, -ETIMEDOUT on timeout
306 static int ocores_poll_wait(struct ocores_i2c *i2c) in ocores_poll_wait() argument
311 if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) { in ocores_poll_wait()
321 udelay((8 * 1000) / i2c->bus_clock_khz); in ocores_poll_wait()
328 err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(1)); in ocores_poll_wait()
330 dev_warn(i2c->adap.dev.parent, in ocores_poll_wait()
331 "%s: STATUS timeout, bit 0x%x did not clear in 1ms\n", in ocores_poll_wait()
337 * ocores_process_polling() - It handles an IRQ-less transfer
338 * @i2c: ocores I2C device instance
340 * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same
341 * (only that IRQ are not produced). This means that we can re-use entirely
346 * Return: 0 on success, -ETIMEDOUT on timeout
348 static int ocores_process_polling(struct ocores_i2c *i2c) in ocores_process_polling() argument
351 int err = 0; in ocores_process_polling()
354 err = ocores_poll_wait(i2c); in ocores_process_polling()
358 ret = ocores_isr(-1, i2c); in ocores_process_polling()
362 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) in ocores_process_polling()
363 if (i2c->state == STATE_DONE) in ocores_process_polling()
371 static int ocores_xfer_core(struct ocores_i2c *i2c, in ocores_xfer_core() argument
375 int ret = 0; in ocores_xfer_core()
378 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_xfer_core()
380 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN); in ocores_xfer_core()
382 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN); in ocores_xfer_core()
384 i2c->msg = msgs; in ocores_xfer_core()
385 i2c->pos = 0; in ocores_xfer_core()
386 i2c->nmsgs = num; in ocores_xfer_core()
387 i2c->state = STATE_START; in ocores_xfer_core()
389 oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg)); in ocores_xfer_core()
390 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); in ocores_xfer_core()
393 ret = ocores_process_polling(i2c); in ocores_xfer_core()
395 if (wait_event_timeout(i2c->wait, in ocores_xfer_core()
396 (i2c->state == STATE_ERROR) || in ocores_xfer_core()
397 (i2c->state == STATE_DONE), HZ) == 0) in ocores_xfer_core()
398 ret = -ETIMEDOUT; in ocores_xfer_core()
401 ocores_process_timeout(i2c); in ocores_xfer_core()
405 return (i2c->state == STATE_DONE) ? num : -EIO; in ocores_xfer_core()
420 static int ocores_init(struct device *dev, struct ocores_i2c *i2c) in ocores_init() argument
424 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_init()
428 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_init()
430 prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1; in ocores_init()
431 prescale = clamp(prescale, 0, 0xffff); in ocores_init()
433 diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz; in ocores_init()
434 if (abs(diff) > i2c->bus_clock_khz / 10) { in ocores_init()
437 i2c->ip_clock_khz, i2c->bus_clock_khz); in ocores_init()
438 return -EINVAL; in ocores_init()
441 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff); in ocores_init()
442 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8); in ocores_init()
445 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN); in ocores_init()
446 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); in ocores_init()
448 return 0; in ocores_init()
465 .name = "i2c-ocores",
472 .compatible = "opencores,i2c-ocores",
480 .compatible = "sifive,fu540-c000-i2c",
492 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
495 static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg) in oc_getreg_grlib() argument
501 rreg--; in oc_getreg_grlib()
502 rd = ioread32be(i2c->base + (rreg << i2c->reg_shift)); in oc_getreg_grlib()
509 static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_grlib() argument
515 rreg--; in oc_setreg_grlib()
517 curr = ioread32be(i2c->base + (rreg << i2c->reg_shift)); in oc_setreg_grlib()
519 wr = (curr & 0xff00) | value; in oc_setreg_grlib()
521 wr = (((u32)value) << 8) | (curr & 0xff); in oc_setreg_grlib()
525 iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift)); in oc_setreg_grlib()
529 struct ocores_i2c *i2c) in ocores_i2c_of_probe() argument
531 struct device_node *np = pdev->dev.of_node; in ocores_i2c_of_probe()
537 if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) { in ocores_i2c_of_probe()
538 /* no 'reg-shift', check for deprecated 'regstep' */ in ocores_i2c_of_probe()
541 dev_err(&pdev->dev, "invalid regstep %d\n", in ocores_i2c_of_probe()
543 return -EINVAL; in ocores_i2c_of_probe()
545 i2c->reg_shift = ilog2(val); in ocores_i2c_of_probe()
546 dev_warn(&pdev->dev, in ocores_i2c_of_probe()
547 "regstep property deprecated, use reg-shift\n"); in ocores_i2c_of_probe()
551 clock_frequency_present = !of_property_read_u32(np, "clock-frequency", in ocores_i2c_of_probe()
553 i2c->bus_clock_khz = 100; in ocores_i2c_of_probe()
555 i2c->clk = devm_clk_get_optional_enabled(&pdev->dev, NULL); in ocores_i2c_of_probe()
556 if (IS_ERR(i2c->clk)) in ocores_i2c_of_probe()
557 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk), in ocores_i2c_of_probe()
560 i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000; in ocores_i2c_of_probe()
562 i2c->bus_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
563 if (i2c->ip_clock_khz == 0) { in ocores_i2c_of_probe()
564 if (of_property_read_u32(np, "opencores,ip-clock-frequency", in ocores_i2c_of_probe()
567 dev_err(&pdev->dev, in ocores_i2c_of_probe()
568 "Missing required parameter 'opencores,ip-clock-frequency'\n"); in ocores_i2c_of_probe()
569 return -ENODEV; in ocores_i2c_of_probe()
571 i2c->ip_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
572 dev_warn(&pdev->dev, in ocores_i2c_of_probe()
573 …"Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequenc… in ocores_i2c_of_probe()
575 i2c->ip_clock_khz = val / 1000; in ocores_i2c_of_probe()
577 i2c->bus_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
581 of_property_read_u32(pdev->dev.of_node, "reg-io-width", in ocores_i2c_of_probe()
582 &i2c->reg_io_width); in ocores_i2c_of_probe()
584 match = of_match_node(ocores_i2c_match, pdev->dev.of_node); in ocores_i2c_of_probe()
585 if (match && (long)match->data == TYPE_GRLIB) { in ocores_i2c_of_probe()
586 dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n"); in ocores_i2c_of_probe()
587 i2c->setreg = oc_setreg_grlib; in ocores_i2c_of_probe()
588 i2c->getreg = oc_getreg_grlib; in ocores_i2c_of_probe()
591 return 0; in ocores_i2c_of_probe()
594 #define ocores_i2c_of_probe(pdev, i2c) -ENODEV argument
599 struct ocores_i2c *i2c; in ocores_i2c_probe() local
606 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); in ocores_i2c_probe()
607 if (!i2c) in ocores_i2c_probe()
608 return -ENOMEM; in ocores_i2c_probe()
610 spin_lock_init(&i2c->process_lock); in ocores_i2c_probe()
612 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); in ocores_i2c_probe()
614 i2c->base = devm_ioremap_resource(&pdev->dev, res); in ocores_i2c_probe()
615 if (IS_ERR(i2c->base)) in ocores_i2c_probe()
616 return PTR_ERR(i2c->base); in ocores_i2c_probe()
618 res = platform_get_resource(pdev, IORESOURCE_IO, 0); in ocores_i2c_probe()
620 return -EINVAL; in ocores_i2c_probe()
621 i2c->iobase = res->start; in ocores_i2c_probe()
622 if (!devm_request_region(&pdev->dev, res->start, in ocores_i2c_probe()
624 pdev->name)) { in ocores_i2c_probe()
625 dev_err(&pdev->dev, "Can't get I/O resource.\n"); in ocores_i2c_probe()
626 return -EBUSY; in ocores_i2c_probe()
628 i2c->setreg = oc_setreg_io_8; in ocores_i2c_probe()
629 i2c->getreg = oc_getreg_io_8; in ocores_i2c_probe()
632 pdata = dev_get_platdata(&pdev->dev); in ocores_i2c_probe()
634 i2c->reg_shift = pdata->reg_shift; in ocores_i2c_probe()
635 i2c->reg_io_width = pdata->reg_io_width; in ocores_i2c_probe()
636 i2c->ip_clock_khz = pdata->clock_khz; in ocores_i2c_probe()
637 if (pdata->bus_khz) in ocores_i2c_probe()
638 i2c->bus_clock_khz = pdata->bus_khz; in ocores_i2c_probe()
640 i2c->bus_clock_khz = 100; in ocores_i2c_probe()
642 ret = ocores_i2c_of_probe(pdev, i2c); in ocores_i2c_probe()
647 if (i2c->reg_io_width == 0) in ocores_i2c_probe()
648 i2c->reg_io_width = 1; /* Set to default value */ in ocores_i2c_probe()
650 if (!i2c->setreg || !i2c->getreg) { in ocores_i2c_probe()
651 bool be = pdata ? pdata->big_endian : in ocores_i2c_probe()
652 of_device_is_big_endian(pdev->dev.of_node); in ocores_i2c_probe()
654 switch (i2c->reg_io_width) { in ocores_i2c_probe()
656 i2c->setreg = oc_setreg_8; in ocores_i2c_probe()
657 i2c->getreg = oc_getreg_8; in ocores_i2c_probe()
661 i2c->setreg = be ? oc_setreg_16be : oc_setreg_16; in ocores_i2c_probe()
662 i2c->getreg = be ? oc_getreg_16be : oc_getreg_16; in ocores_i2c_probe()
666 i2c->setreg = be ? oc_setreg_32be : oc_setreg_32; in ocores_i2c_probe()
667 i2c->getreg = be ? oc_getreg_32be : oc_getreg_32; in ocores_i2c_probe()
671 dev_err(&pdev->dev, "Unsupported I/O width (%d)\n", in ocores_i2c_probe()
672 i2c->reg_io_width); in ocores_i2c_probe()
673 return -EINVAL; in ocores_i2c_probe()
677 init_waitqueue_head(&i2c->wait); in ocores_i2c_probe()
679 irq = platform_get_irq_optional(pdev, 0); in ocores_i2c_probe()
682 * property - But this should be bypassed as the IRQ logic in this in ocores_i2c_probe()
685 if (of_device_is_compatible(pdev->dev.of_node, in ocores_i2c_probe()
686 "sifive,fu540-c000-i2c")) { in ocores_i2c_probe()
687 i2c->flags |= OCORES_FLAG_BROKEN_IRQ; in ocores_i2c_probe()
688 irq = -ENXIO; in ocores_i2c_probe()
691 if (irq == -ENXIO) { in ocores_i2c_probe()
694 if (irq < 0) in ocores_i2c_probe()
699 ret = devm_request_any_context_irq(&pdev->dev, irq, in ocores_i2c_probe()
700 ocores_isr, 0, in ocores_i2c_probe()
701 pdev->name, i2c); in ocores_i2c_probe()
703 dev_err(&pdev->dev, "Cannot claim IRQ\n"); in ocores_i2c_probe()
708 ret = ocores_init(&pdev->dev, i2c); in ocores_i2c_probe()
713 platform_set_drvdata(pdev, i2c); in ocores_i2c_probe()
714 i2c->adap = ocores_adapter; in ocores_i2c_probe()
715 i2c_set_adapdata(&i2c->adap, i2c); in ocores_i2c_probe()
716 i2c->adap.dev.parent = &pdev->dev; in ocores_i2c_probe()
717 i2c->adap.dev.of_node = pdev->dev.of_node; in ocores_i2c_probe()
719 /* add i2c adapter to i2c tree */ in ocores_i2c_probe()
720 ret = i2c_add_adapter(&i2c->adap); in ocores_i2c_probe()
726 for (i = 0; i < pdata->num_devices; i++) in ocores_i2c_probe()
727 i2c_new_client_device(&i2c->adap, pdata->devices + i); in ocores_i2c_probe()
730 return 0; in ocores_i2c_probe()
735 struct ocores_i2c *i2c = platform_get_drvdata(pdev); in ocores_i2c_remove() local
736 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_i2c_remove()
738 /* disable i2c logic */ in ocores_i2c_remove()
740 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_i2c_remove()
743 i2c_del_adapter(&i2c->adap); in ocores_i2c_remove()
748 struct ocores_i2c *i2c = dev_get_drvdata(dev); in ocores_i2c_suspend() local
749 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_i2c_suspend()
753 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_i2c_suspend()
755 clk_disable_unprepare(i2c->clk); in ocores_i2c_suspend()
756 return 0; in ocores_i2c_suspend()
761 struct ocores_i2c *i2c = dev_get_drvdata(dev); in ocores_i2c_resume() local
765 ret = clk_prepare_enable(i2c->clk); in ocores_i2c_resume()
768 rate = clk_get_rate(i2c->clk) / 1000; in ocores_i2c_resume()
770 i2c->ip_clock_khz = rate; in ocores_i2c_resume()
771 return ocores_init(dev, i2c); in ocores_i2c_resume()
781 .name = "ocores-i2c",
790 MODULE_DESCRIPTION("OpenCores I2C bus driver");
792 MODULE_ALIAS("platform:ocores-i2c");