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
346 static void ocores_process_polling(struct ocores_i2c *i2c) in ocores_process_polling() argument
352 err = ocores_poll_wait(i2c); in ocores_process_polling()
354 i2c->state = STATE_ERROR; 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()
369 static int ocores_xfer_core(struct ocores_i2c *i2c, in ocores_xfer_core() argument
376 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_xfer_core()
378 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN); in ocores_xfer_core()
380 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN); in ocores_xfer_core()
382 i2c->msg = msgs; in ocores_xfer_core()
383 i2c->pos = 0; in ocores_xfer_core()
384 i2c->nmsgs = num; in ocores_xfer_core()
385 i2c->state = STATE_START; in ocores_xfer_core()
387 oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg)); in ocores_xfer_core()
388 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); in ocores_xfer_core()
391 ocores_process_polling(i2c); in ocores_xfer_core()
393 ret = wait_event_timeout(i2c->wait, in ocores_xfer_core()
394 (i2c->state == STATE_ERROR) || in ocores_xfer_core()
395 (i2c->state == STATE_DONE), HZ); in ocores_xfer_core()
397 ocores_process_timeout(i2c); in ocores_xfer_core()
402 return (i2c->state == STATE_DONE) ? num : -EIO; in ocores_xfer_core()
417 static int ocores_init(struct device *dev, struct ocores_i2c *i2c) in ocores_init() argument
421 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_init()
425 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_init()
427 prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1; in ocores_init()
430 diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz; in ocores_init()
431 if (abs(diff) > i2c->bus_clock_khz / 10) { in ocores_init()
434 i2c->ip_clock_khz, i2c->bus_clock_khz); in ocores_init()
438 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff); in ocores_init()
439 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8); in ocores_init()
442 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); in ocores_init()
443 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN); in ocores_init()
462 .name = "i2c-ocores",
469 .compatible = "opencores,i2c-ocores",
477 .compatible = "sifive,fu540-c000-i2c",
492 static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg) in oc_getreg_grlib() argument
499 rd = ioread32be(i2c->base + (rreg << i2c->reg_shift)); in oc_getreg_grlib()
506 static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_grlib() argument
514 curr = ioread32be(i2c->base + (rreg << i2c->reg_shift)); in oc_setreg_grlib()
522 iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift)); in oc_setreg_grlib()
526 struct ocores_i2c *i2c) in ocores_i2c_of_probe() argument
534 if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) { in ocores_i2c_of_probe()
542 i2c->reg_shift = ilog2(val); in ocores_i2c_of_probe()
550 i2c->bus_clock_khz = 100; in ocores_i2c_of_probe()
552 i2c->clk = devm_clk_get(&pdev->dev, NULL); in ocores_i2c_of_probe()
554 if (!IS_ERR(i2c->clk)) { in ocores_i2c_of_probe()
555 int ret = clk_prepare_enable(i2c->clk); in ocores_i2c_of_probe()
562 i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000; in ocores_i2c_of_probe()
564 i2c->bus_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
567 if (i2c->ip_clock_khz == 0) { in ocores_i2c_of_probe()
573 clk_disable_unprepare(i2c->clk); in ocores_i2c_of_probe()
576 i2c->ip_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
580 i2c->ip_clock_khz = val / 1000; in ocores_i2c_of_probe()
582 i2c->bus_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
587 &i2c->reg_io_width); in ocores_i2c_of_probe()
591 dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n"); in ocores_i2c_of_probe()
592 i2c->setreg = oc_setreg_grlib; in ocores_i2c_of_probe()
593 i2c->getreg = oc_getreg_grlib; in ocores_i2c_of_probe()
599 #define ocores_i2c_of_probe(pdev, i2c) -ENODEV argument
604 struct ocores_i2c *i2c; in ocores_i2c_probe() local
611 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); in ocores_i2c_probe()
612 if (!i2c) in ocores_i2c_probe()
615 spin_lock_init(&i2c->process_lock); in ocores_i2c_probe()
619 i2c->base = devm_ioremap_resource(&pdev->dev, res); in ocores_i2c_probe()
620 if (IS_ERR(i2c->base)) in ocores_i2c_probe()
621 return PTR_ERR(i2c->base); in ocores_i2c_probe()
626 i2c->iobase = res->start; in ocores_i2c_probe()
633 i2c->setreg = oc_setreg_io_8; in ocores_i2c_probe()
634 i2c->getreg = oc_getreg_io_8; in ocores_i2c_probe()
639 i2c->reg_shift = pdata->reg_shift; in ocores_i2c_probe()
640 i2c->reg_io_width = pdata->reg_io_width; in ocores_i2c_probe()
641 i2c->ip_clock_khz = pdata->clock_khz; in ocores_i2c_probe()
643 i2c->bus_clock_khz = pdata->bus_khz; in ocores_i2c_probe()
645 i2c->bus_clock_khz = 100; in ocores_i2c_probe()
647 ret = ocores_i2c_of_probe(pdev, i2c); in ocores_i2c_probe()
652 if (i2c->reg_io_width == 0) in ocores_i2c_probe()
653 i2c->reg_io_width = 1; /* Set to default value */ in ocores_i2c_probe()
655 if (!i2c->setreg || !i2c->getreg) { in ocores_i2c_probe()
659 switch (i2c->reg_io_width) { in ocores_i2c_probe()
661 i2c->setreg = oc_setreg_8; in ocores_i2c_probe()
662 i2c->getreg = oc_getreg_8; in ocores_i2c_probe()
666 i2c->setreg = be ? oc_setreg_16be : oc_setreg_16; in ocores_i2c_probe()
667 i2c->getreg = be ? oc_getreg_16be : oc_getreg_16; in ocores_i2c_probe()
671 i2c->setreg = be ? oc_setreg_32be : oc_setreg_32; in ocores_i2c_probe()
672 i2c->getreg = be ? oc_getreg_32be : oc_getreg_32; in ocores_i2c_probe()
677 i2c->reg_io_width); in ocores_i2c_probe()
683 init_waitqueue_head(&i2c->wait); in ocores_i2c_probe()
692 "sifive,fu540-c000-i2c")) { in ocores_i2c_probe()
693 i2c->flags |= OCORES_FLAG_BROKEN_IRQ; in ocores_i2c_probe()
707 pdev->name, i2c); in ocores_i2c_probe()
714 ret = ocores_init(&pdev->dev, i2c); in ocores_i2c_probe()
719 platform_set_drvdata(pdev, i2c); in ocores_i2c_probe()
720 i2c->adap = ocores_adapter; in ocores_i2c_probe()
721 i2c_set_adapdata(&i2c->adap, i2c); in ocores_i2c_probe()
722 i2c->adap.dev.parent = &pdev->dev; in ocores_i2c_probe()
723 i2c->adap.dev.of_node = pdev->dev.of_node; in ocores_i2c_probe()
725 /* add i2c adapter to i2c tree */ in ocores_i2c_probe()
726 ret = i2c_add_adapter(&i2c->adap); in ocores_i2c_probe()
733 i2c_new_client_device(&i2c->adap, pdata->devices + i); in ocores_i2c_probe()
739 clk_disable_unprepare(i2c->clk); in ocores_i2c_probe()
745 struct ocores_i2c *i2c = platform_get_drvdata(pdev); in ocores_i2c_remove() local
746 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_i2c_remove()
748 /* disable i2c logic */ in ocores_i2c_remove()
750 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_i2c_remove()
753 i2c_del_adapter(&i2c->adap); in ocores_i2c_remove()
755 if (!IS_ERR(i2c->clk)) in ocores_i2c_remove()
756 clk_disable_unprepare(i2c->clk); in ocores_i2c_remove()
764 struct ocores_i2c *i2c = dev_get_drvdata(dev); in ocores_i2c_suspend() local
765 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_i2c_suspend()
769 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_i2c_suspend()
771 if (!IS_ERR(i2c->clk)) in ocores_i2c_suspend()
772 clk_disable_unprepare(i2c->clk); in ocores_i2c_suspend()
778 struct ocores_i2c *i2c = dev_get_drvdata(dev); in ocores_i2c_resume() local
780 if (!IS_ERR(i2c->clk)) { in ocores_i2c_resume()
782 int ret = clk_prepare_enable(i2c->clk); in ocores_i2c_resume()
789 rate = clk_get_rate(i2c->clk) / 1000; in ocores_i2c_resume()
791 i2c->ip_clock_khz = rate; in ocores_i2c_resume()
793 return ocores_init(dev, i2c); in ocores_i2c_resume()
806 .name = "ocores-i2c",
815 MODULE_DESCRIPTION("OpenCores I2C bus driver");
817 MODULE_ALIAS("platform:ocores-i2c");