Lines Matching full:i2c

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);
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()
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()
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()
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()
268 * @i2c: ocores I2C device instance
279 static int ocores_wait(struct ocores_i2c *i2c, in ocores_wait() argument
287 u8 status = oc_getreg(i2c, reg); in ocores_wait()
300 * @i2c: ocores I2C device instance
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()
338 * @i2c: ocores I2C device instance
340 * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same
348 static int ocores_process_polling(struct ocores_i2c *i2c) in ocores_process_polling() argument
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
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()
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()
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()
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_CMD, OCI2C_CMD_IACK); in ocores_init()
446 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN); in ocores_init()
465 .name = "i2c-ocores",
472 .compatible = "opencores,i2c-ocores",
480 .compatible = "sifive,fu540-c000-i2c",
495 static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg) in oc_getreg_grlib() argument
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
517 curr = ioread32be(i2c->base + (rreg << i2c->reg_shift)); 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
537 if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) { in ocores_i2c_of_probe()
545 i2c->reg_shift = ilog2(val); 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()
571 i2c->ip_clock_khz = clock_frequency / 1000; 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()
582 &i2c->reg_io_width); 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()
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()
610 spin_lock_init(&i2c->process_lock); 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()
621 i2c->iobase = res->start; 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()
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()
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()
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()
672 i2c->reg_io_width); in ocores_i2c_probe()
677 init_waitqueue_head(&i2c->wait); in ocores_i2c_probe()
686 "sifive,fu540-c000-i2c")) { in ocores_i2c_probe()
687 i2c->flags |= OCORES_FLAG_BROKEN_IRQ; in ocores_i2c_probe()
701 pdev->name, i2c); 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()
727 i2c_new_client_device(&i2c->adap, pdata->devices + i); 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()
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");