Lines Matching +full:i2c +full:- +full:polling

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>
48 void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
49 u8 (*getreg)(struct ocores_i2c *i2c, int reg);
86 #define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */
88 static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_8() argument
90 iowrite8(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_8()
93 static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_16() argument
95 iowrite16(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_16()
98 static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_32() argument
100 iowrite32(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_32()
103 static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_16be() argument
105 iowrite16be(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_16be()
108 static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_32be() argument
110 iowrite32be(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_32be()
113 static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg) in oc_getreg_8() argument
115 return ioread8(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_8()
118 static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg) in oc_getreg_16() argument
120 return ioread16(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_16()
123 static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg) in oc_getreg_32() argument
125 return ioread32(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_32()
128 static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg) in oc_getreg_16be() argument
130 return ioread16be(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_16be()
133 static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg) in oc_getreg_32be() argument
135 return ioread32be(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_32be()
138 static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg() argument
140 i2c->setreg(i2c, reg, value); in oc_setreg()
143 static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg) in oc_getreg() argument
145 return i2c->getreg(i2c, reg); in oc_getreg()
148 static void ocores_process(struct ocores_i2c *i2c, u8 stat) in ocores_process() argument
150 struct i2c_msg *msg = i2c->msg; in ocores_process()
157 spin_lock_irqsave(&i2c->process_lock, flags); in ocores_process()
159 if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) { in ocores_process()
161 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); in ocores_process()
162 wake_up(&i2c->wait); in ocores_process()
168 i2c->state = STATE_ERROR; in ocores_process()
169 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
173 if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) { in ocores_process()
174 i2c->state = in ocores_process()
175 (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE; in ocores_process()
178 i2c->state = STATE_ERROR; in ocores_process()
179 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
183 msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA); in ocores_process()
187 if (i2c->pos == msg->len) { in ocores_process()
188 i2c->nmsgs--; in ocores_process()
189 i2c->msg++; in ocores_process()
190 i2c->pos = 0; in ocores_process()
191 msg = i2c->msg; in ocores_process()
193 if (i2c->nmsgs) { /* end? */ in ocores_process()
195 if (!(msg->flags & I2C_M_NOSTART)) { in ocores_process()
198 i2c->state = STATE_START; in ocores_process()
200 oc_setreg(i2c, OCI2C_DATA, addr); in ocores_process()
201 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); in ocores_process()
204 i2c->state = (msg->flags & I2C_M_RD) in ocores_process()
207 i2c->state = STATE_DONE; in ocores_process()
208 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
213 if (i2c->state == STATE_READ) { in ocores_process()
214 oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ? in ocores_process()
217 oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]); in ocores_process()
218 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE); in ocores_process()
222 spin_unlock_irqrestore(&i2c->process_lock, flags); in ocores_process()
227 struct ocores_i2c *i2c = dev_id; in ocores_isr() local
228 u8 stat = oc_getreg(i2c, OCI2C_STATUS); in ocores_isr()
230 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) { in ocores_isr()
236 ocores_process(i2c, stat); in ocores_isr()
242 * ocores_process_timeout() - Process timeout event
243 * @i2c: ocores I2C device instance
245 static void ocores_process_timeout(struct ocores_i2c *i2c) in ocores_process_timeout() argument
249 spin_lock_irqsave(&i2c->process_lock, flags); in ocores_process_timeout()
250 i2c->state = STATE_ERROR; in ocores_process_timeout()
251 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process_timeout()
252 spin_unlock_irqrestore(&i2c->process_lock, flags); in ocores_process_timeout()
256 * ocores_wait() - Wait until something change in a given register
257 * @i2c: ocores I2C device instance
266 * Return: 0 on success, -ETIMEDOUT on timeout
268 static int ocores_wait(struct ocores_i2c *i2c, in ocores_wait() argument
276 u8 status = oc_getreg(i2c, reg); in ocores_wait()
282 return -ETIMEDOUT; in ocores_wait()
288 * ocores_poll_wait() - Wait until is possible to process some data
289 * @i2c: ocores I2C device instance
291 * Used when the device is in polling mode (interrupts disabled).
293 * Return: 0 on success, -ETIMEDOUT on timeout
295 static int ocores_poll_wait(struct ocores_i2c *i2c) in ocores_poll_wait() argument
300 if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) { in ocores_poll_wait()
308 * then we start polling on the ACK/NACK bit in ocores_poll_wait()
310 udelay((8 * 1000) / i2c->bus_clock_khz); in ocores_poll_wait()
317 err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(1)); in ocores_poll_wait()
319 dev_warn(i2c->adap.dev.parent, in ocores_poll_wait()
326 * ocores_process_polling() - It handles an IRQ-less transfer
327 * @i2c: ocores I2C device instance
329 * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same
330 * (only that IRQ are not produced). This means that we can re-use entirely
331 * ocores_isr(), we just add our polling code around it.
335 * Return: 0 on success, -ETIMEDOUT on timeout
337 static int ocores_process_polling(struct ocores_i2c *i2c) in ocores_process_polling() argument
343 err = ocores_poll_wait(i2c); in ocores_process_polling()
347 ret = ocores_isr(-1, i2c); in ocores_process_polling()
351 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) in ocores_process_polling()
352 if (i2c->state == STATE_DONE) in ocores_process_polling()
360 static int ocores_xfer_core(struct ocores_i2c *i2c, in ocores_xfer_core() argument
362 bool polling) in ocores_xfer_core() argument
367 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_xfer_core()
368 if (polling) in ocores_xfer_core()
369 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN); in ocores_xfer_core()
371 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN); in ocores_xfer_core()
373 i2c->msg = msgs; in ocores_xfer_core()
374 i2c->pos = 0; in ocores_xfer_core()
375 i2c->nmsgs = num; in ocores_xfer_core()
376 i2c->state = STATE_START; in ocores_xfer_core()
378 oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg)); in ocores_xfer_core()
379 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); in ocores_xfer_core()
381 if (polling) { in ocores_xfer_core()
382 ret = ocores_process_polling(i2c); in ocores_xfer_core()
384 if (wait_event_timeout(i2c->wait, in ocores_xfer_core()
385 (i2c->state == STATE_ERROR) || in ocores_xfer_core()
386 (i2c->state == STATE_DONE), HZ) == 0) in ocores_xfer_core()
387 ret = -ETIMEDOUT; in ocores_xfer_core()
390 ocores_process_timeout(i2c); in ocores_xfer_core()
394 return (i2c->state == STATE_DONE) ? num : -EIO; in ocores_xfer_core()
409 static int ocores_init(struct device *dev, struct ocores_i2c *i2c) in ocores_init() argument
413 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_init()
417 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_init()
419 prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1; in ocores_init()
422 diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz; in ocores_init()
423 if (abs(diff) > i2c->bus_clock_khz / 10) { in ocores_init()
426 i2c->ip_clock_khz, i2c->bus_clock_khz); in ocores_init()
427 return -EINVAL; in ocores_init()
430 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff); in ocores_init()
431 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8); in ocores_init()
434 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN); in ocores_init()
435 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); in ocores_init()
454 .name = "i2c-ocores",
461 .compatible = "opencores,i2c-ocores",
469 .compatible = "sifive,fu540-c000-i2c",
481 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
484 static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg) in oc_getreg_grlib() argument
490 rreg--; in oc_getreg_grlib()
491 rd = ioread32be(i2c->base + (rreg << i2c->reg_shift)); in oc_getreg_grlib()
498 static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_grlib() argument
504 rreg--; in oc_setreg_grlib()
506 curr = ioread32be(i2c->base + (rreg << i2c->reg_shift)); in oc_setreg_grlib()
514 iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift)); in oc_setreg_grlib()
518 struct ocores_i2c *i2c) in ocores_i2c_of_probe() argument
520 struct device_node *np = pdev->dev.of_node; in ocores_i2c_of_probe()
526 if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) { in ocores_i2c_of_probe()
527 /* no 'reg-shift', check for deprecated 'regstep' */ in ocores_i2c_of_probe()
530 dev_err(&pdev->dev, "invalid regstep %d\n", in ocores_i2c_of_probe()
532 return -EINVAL; in ocores_i2c_of_probe()
534 i2c->reg_shift = ilog2(val); in ocores_i2c_of_probe()
535 dev_warn(&pdev->dev, in ocores_i2c_of_probe()
536 "regstep property deprecated, use reg-shift\n"); in ocores_i2c_of_probe()
540 clock_frequency_present = !of_property_read_u32(np, "clock-frequency", in ocores_i2c_of_probe()
542 i2c->bus_clock_khz = 100; in ocores_i2c_of_probe()
544 i2c->clk = devm_clk_get_optional_enabled(&pdev->dev, NULL); in ocores_i2c_of_probe()
545 if (IS_ERR(i2c->clk)) in ocores_i2c_of_probe()
546 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk), in ocores_i2c_of_probe()
549 i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000; in ocores_i2c_of_probe()
551 i2c->bus_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
552 if (i2c->ip_clock_khz == 0) { in ocores_i2c_of_probe()
553 if (of_property_read_u32(np, "opencores,ip-clock-frequency", in ocores_i2c_of_probe()
556 dev_err(&pdev->dev, in ocores_i2c_of_probe()
557 "Missing required parameter 'opencores,ip-clock-frequency'\n"); in ocores_i2c_of_probe()
558 return -ENODEV; in ocores_i2c_of_probe()
560 i2c->ip_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
561 dev_warn(&pdev->dev, in ocores_i2c_of_probe()
562 …"Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequenc… in ocores_i2c_of_probe()
564 i2c->ip_clock_khz = val / 1000; in ocores_i2c_of_probe()
566 i2c->bus_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
570 of_property_read_u32(pdev->dev.of_node, "reg-io-width", in ocores_i2c_of_probe()
571 &i2c->reg_io_width); in ocores_i2c_of_probe()
573 match = of_match_node(ocores_i2c_match, pdev->dev.of_node); in ocores_i2c_of_probe()
574 if (match && (long)match->data == TYPE_GRLIB) { in ocores_i2c_of_probe()
575 dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n"); in ocores_i2c_of_probe()
576 i2c->setreg = oc_setreg_grlib; in ocores_i2c_of_probe()
577 i2c->getreg = oc_getreg_grlib; in ocores_i2c_of_probe()
583 #define ocores_i2c_of_probe(pdev, i2c) -ENODEV argument
588 struct ocores_i2c *i2c; in ocores_i2c_probe() local
595 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); in ocores_i2c_probe()
596 if (!i2c) in ocores_i2c_probe()
597 return -ENOMEM; in ocores_i2c_probe()
599 spin_lock_init(&i2c->process_lock); in ocores_i2c_probe()
603 i2c->base = devm_ioremap_resource(&pdev->dev, res); in ocores_i2c_probe()
604 if (IS_ERR(i2c->base)) in ocores_i2c_probe()
605 return PTR_ERR(i2c->base); in ocores_i2c_probe()
609 return -EINVAL; in ocores_i2c_probe()
610 if (!devm_request_region(&pdev->dev, res->start, in ocores_i2c_probe()
612 pdev->name)) { in ocores_i2c_probe()
613 dev_err(&pdev->dev, "Can't get I/O resource.\n"); in ocores_i2c_probe()
614 return -EBUSY; in ocores_i2c_probe()
616 i2c->base = devm_ioport_map(&pdev->dev, res->start, in ocores_i2c_probe()
618 if (!i2c->base) { in ocores_i2c_probe()
619 dev_err(&pdev->dev, "Can't map I/O resource.\n"); in ocores_i2c_probe()
620 return -EBUSY; in ocores_i2c_probe()
622 i2c->reg_io_width = 1; in ocores_i2c_probe()
625 pdata = dev_get_platdata(&pdev->dev); in ocores_i2c_probe()
627 i2c->reg_shift = pdata->reg_shift; in ocores_i2c_probe()
628 i2c->reg_io_width = pdata->reg_io_width; in ocores_i2c_probe()
629 i2c->ip_clock_khz = pdata->clock_khz; in ocores_i2c_probe()
630 if (pdata->bus_khz) in ocores_i2c_probe()
631 i2c->bus_clock_khz = pdata->bus_khz; in ocores_i2c_probe()
633 i2c->bus_clock_khz = 100; in ocores_i2c_probe()
635 ret = ocores_i2c_of_probe(pdev, i2c); in ocores_i2c_probe()
640 if (i2c->reg_io_width == 0) in ocores_i2c_probe()
641 i2c->reg_io_width = 1; /* Set to default value */ in ocores_i2c_probe()
643 if (!i2c->setreg || !i2c->getreg) { in ocores_i2c_probe()
644 bool be = pdata ? pdata->big_endian : in ocores_i2c_probe()
645 of_device_is_big_endian(pdev->dev.of_node); in ocores_i2c_probe()
647 switch (i2c->reg_io_width) { in ocores_i2c_probe()
649 i2c->setreg = oc_setreg_8; in ocores_i2c_probe()
650 i2c->getreg = oc_getreg_8; in ocores_i2c_probe()
654 i2c->setreg = be ? oc_setreg_16be : oc_setreg_16; in ocores_i2c_probe()
655 i2c->getreg = be ? oc_getreg_16be : oc_getreg_16; in ocores_i2c_probe()
659 i2c->setreg = be ? oc_setreg_32be : oc_setreg_32; in ocores_i2c_probe()
660 i2c->getreg = be ? oc_getreg_32be : oc_getreg_32; in ocores_i2c_probe()
664 dev_err(&pdev->dev, "Unsupported I/O width (%d)\n", in ocores_i2c_probe()
665 i2c->reg_io_width); in ocores_i2c_probe()
666 return -EINVAL; in ocores_i2c_probe()
670 init_waitqueue_head(&i2c->wait); in ocores_i2c_probe()
675 * property - But this should be bypassed as the IRQ logic in this in ocores_i2c_probe()
678 if (of_device_is_compatible(pdev->dev.of_node, in ocores_i2c_probe()
679 "sifive,fu540-c000-i2c")) { in ocores_i2c_probe()
680 i2c->flags |= OCORES_FLAG_BROKEN_IRQ; in ocores_i2c_probe()
681 irq = -ENXIO; in ocores_i2c_probe()
684 if (irq == -ENXIO) { in ocores_i2c_probe()
692 ret = devm_request_any_context_irq(&pdev->dev, irq, in ocores_i2c_probe()
694 pdev->name, i2c); in ocores_i2c_probe()
696 dev_err(&pdev->dev, "Cannot claim IRQ\n"); in ocores_i2c_probe()
701 ret = ocores_init(&pdev->dev, i2c); in ocores_i2c_probe()
706 platform_set_drvdata(pdev, i2c); in ocores_i2c_probe()
707 i2c->adap = ocores_adapter; in ocores_i2c_probe()
708 i2c_set_adapdata(&i2c->adap, i2c); in ocores_i2c_probe()
709 i2c->adap.dev.parent = &pdev->dev; in ocores_i2c_probe()
710 i2c->adap.dev.of_node = pdev->dev.of_node; in ocores_i2c_probe()
712 /* add i2c adapter to i2c tree */ in ocores_i2c_probe()
713 ret = i2c_add_adapter(&i2c->adap); in ocores_i2c_probe()
719 for (i = 0; i < pdata->num_devices; i++) in ocores_i2c_probe()
720 i2c_new_client_device(&i2c->adap, pdata->devices + i); in ocores_i2c_probe()
728 struct ocores_i2c *i2c = platform_get_drvdata(pdev); in ocores_i2c_remove() local
729 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_i2c_remove()
731 /* disable i2c logic */ in ocores_i2c_remove()
733 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_i2c_remove()
736 i2c_del_adapter(&i2c->adap); in ocores_i2c_remove()
741 struct ocores_i2c *i2c = dev_get_drvdata(dev); in ocores_i2c_suspend() local
742 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_i2c_suspend()
746 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_i2c_suspend()
748 clk_disable_unprepare(i2c->clk); in ocores_i2c_suspend()
754 struct ocores_i2c *i2c = dev_get_drvdata(dev); in ocores_i2c_resume() local
758 ret = clk_prepare_enable(i2c->clk); in ocores_i2c_resume()
761 rate = clk_get_rate(i2c->clk) / 1000; in ocores_i2c_resume()
763 i2c->ip_clock_khz = rate; in ocores_i2c_resume()
764 return ocores_init(dev, i2c); in ocores_i2c_resume()
774 .name = "ocores-i2c",
783 MODULE_DESCRIPTION("OpenCores I2C bus driver");
785 MODULE_ALIAS("platform:ocores-i2c");