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);
90 static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_8() argument
92 iowrite8(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_8()
95 static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_16() argument
97 iowrite16(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_16()
100 static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_32() argument
102 iowrite32(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_32()
105 static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_16be() argument
107 iowrite16be(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_16be()
110 static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_32be() argument
112 iowrite32be(value, i2c->base + (reg << i2c->reg_shift)); in oc_setreg_32be()
115 static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg) in oc_getreg_8() argument
117 return ioread8(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_8()
120 static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg) in oc_getreg_16() argument
122 return ioread16(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_16()
125 static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg) in oc_getreg_32() argument
127 return ioread32(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_32()
130 static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg) in oc_getreg_16be() argument
132 return ioread16be(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_16be()
135 static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg) in oc_getreg_32be() argument
137 return ioread32be(i2c->base + (reg << i2c->reg_shift)); in oc_getreg_32be()
140 static void oc_setreg_io_8(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg_io_8() argument
142 outb(value, i2c->iobase + reg); in oc_setreg_io_8()
145 static inline u8 oc_getreg_io_8(struct ocores_i2c *i2c, int reg) in oc_getreg_io_8() argument
147 return inb(i2c->iobase + reg); in oc_getreg_io_8()
150 static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value) in oc_setreg() argument
152 i2c->setreg(i2c, reg, value); in oc_setreg()
155 static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg) in oc_getreg() argument
157 return i2c->getreg(i2c, reg); in oc_getreg()
160 static void ocores_process(struct ocores_i2c *i2c, u8 stat) in ocores_process() argument
162 struct i2c_msg *msg = i2c->msg; in ocores_process()
169 spin_lock_irqsave(&i2c->process_lock, flags); in ocores_process()
171 if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) { in ocores_process()
173 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); in ocores_process()
174 wake_up(&i2c->wait); in ocores_process()
180 i2c->state = STATE_ERROR; in ocores_process()
181 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
185 if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) { in ocores_process()
186 i2c->state = in ocores_process()
190 i2c->state = STATE_ERROR; in ocores_process()
191 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
195 msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA); in ocores_process()
199 if (i2c->pos == msg->len) { in ocores_process()
200 i2c->nmsgs--; in ocores_process()
201 i2c->msg++; in ocores_process()
202 i2c->pos = 0; in ocores_process()
203 msg = i2c->msg; in ocores_process()
205 if (i2c->nmsgs) { /* end? */ in ocores_process()
210 i2c->state = STATE_START; in ocores_process()
212 oc_setreg(i2c, OCI2C_DATA, addr); in ocores_process()
213 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); in ocores_process()
216 i2c->state = (msg->flags & I2C_M_RD) in ocores_process()
219 i2c->state = STATE_DONE; in ocores_process()
220 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process()
225 if (i2c->state == STATE_READ) { in ocores_process()
226 oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ? in ocores_process()
229 oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]); in ocores_process()
230 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE); in ocores_process()
234 spin_unlock_irqrestore(&i2c->process_lock, flags); in ocores_process()
239 struct ocores_i2c *i2c = dev_id; in ocores_isr() local
240 u8 stat = oc_getreg(i2c, OCI2C_STATUS); in ocores_isr()
242 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) { in ocores_isr()
248 ocores_process(i2c, stat); in ocores_isr()
255 * @i2c: ocores I2C device instance
257 static void ocores_process_timeout(struct ocores_i2c *i2c) in ocores_process_timeout() argument
261 spin_lock_irqsave(&i2c->process_lock, flags); in ocores_process_timeout()
262 i2c->state = STATE_ERROR; in ocores_process_timeout()
263 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); in ocores_process_timeout()
264 spin_unlock_irqrestore(&i2c->process_lock, flags); in ocores_process_timeout()
269 * @i2c: ocores I2C device instance
280 static int ocores_wait(struct ocores_i2c *i2c, in ocores_wait() argument
288 u8 status = oc_getreg(i2c, reg); in ocores_wait()
301 * @i2c: ocores I2C device instance
307 static int ocores_poll_wait(struct ocores_i2c *i2c) in ocores_poll_wait() argument
312 if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) { in ocores_poll_wait()
322 udelay((8 * 1000) / i2c->bus_clock_khz); in ocores_poll_wait()
329 err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(1)); in ocores_poll_wait()
331 dev_warn(i2c->adap.dev.parent, in ocores_poll_wait()
339 * @i2c: ocores I2C device instance
341 * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same
347 static void ocores_process_polling(struct ocores_i2c *i2c) in ocores_process_polling() argument
353 err = ocores_poll_wait(i2c); in ocores_process_polling()
355 i2c->state = STATE_ERROR; in ocores_process_polling()
359 ret = ocores_isr(-1, i2c); in ocores_process_polling()
363 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) in ocores_process_polling()
364 if (i2c->state == STATE_DONE) in ocores_process_polling()
370 static int ocores_xfer_core(struct ocores_i2c *i2c, in ocores_xfer_core() argument
377 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_xfer_core()
379 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN); in ocores_xfer_core()
381 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN); in ocores_xfer_core()
383 i2c->msg = msgs; in ocores_xfer_core()
384 i2c->pos = 0; in ocores_xfer_core()
385 i2c->nmsgs = num; in ocores_xfer_core()
386 i2c->state = STATE_START; in ocores_xfer_core()
388 oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg)); in ocores_xfer_core()
389 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); in ocores_xfer_core()
392 ocores_process_polling(i2c); in ocores_xfer_core()
394 ret = wait_event_timeout(i2c->wait, in ocores_xfer_core()
395 (i2c->state == STATE_ERROR) || in ocores_xfer_core()
396 (i2c->state == STATE_DONE), HZ); in ocores_xfer_core()
398 ocores_process_timeout(i2c); in ocores_xfer_core()
403 return (i2c->state == STATE_DONE) ? num : -EIO; in ocores_xfer_core()
418 static int ocores_init(struct device *dev, struct ocores_i2c *i2c) in ocores_init() argument
422 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_init()
426 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_init()
428 prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1; in ocores_init()
431 diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz; in ocores_init()
432 if (abs(diff) > i2c->bus_clock_khz / 10) { in ocores_init()
435 i2c->ip_clock_khz, i2c->bus_clock_khz); in ocores_init()
439 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff); in ocores_init()
440 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8); in ocores_init()
443 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); in ocores_init()
444 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN); in ocores_init()
463 .name = "i2c-ocores",
470 .compatible = "opencores,i2c-ocores",
478 .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(&pdev->dev, NULL); in ocores_i2c_of_probe()
557 if (!IS_ERR(i2c->clk)) { in ocores_i2c_of_probe()
558 int ret = clk_prepare_enable(i2c->clk); in ocores_i2c_of_probe()
565 i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000; in ocores_i2c_of_probe()
567 i2c->bus_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
570 if (i2c->ip_clock_khz == 0) { in ocores_i2c_of_probe()
576 clk_disable_unprepare(i2c->clk); in ocores_i2c_of_probe()
579 i2c->ip_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
583 i2c->ip_clock_khz = val / 1000; in ocores_i2c_of_probe()
585 i2c->bus_clock_khz = clock_frequency / 1000; in ocores_i2c_of_probe()
590 &i2c->reg_io_width); in ocores_i2c_of_probe()
594 dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n"); in ocores_i2c_of_probe()
595 i2c->setreg = oc_setreg_grlib; in ocores_i2c_of_probe()
596 i2c->getreg = oc_getreg_grlib; in ocores_i2c_of_probe()
602 #define ocores_i2c_of_probe(pdev, i2c) -ENODEV argument
607 struct ocores_i2c *i2c; in ocores_i2c_probe() local
615 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); in ocores_i2c_probe()
616 if (!i2c) in ocores_i2c_probe()
619 spin_lock_init(&i2c->process_lock); in ocores_i2c_probe()
623 i2c->base = devm_ioremap_resource(&pdev->dev, res); in ocores_i2c_probe()
624 if (IS_ERR(i2c->base)) in ocores_i2c_probe()
625 return PTR_ERR(i2c->base); in ocores_i2c_probe()
630 i2c->iobase = res->start; in ocores_i2c_probe()
637 i2c->setreg = oc_setreg_io_8; in ocores_i2c_probe()
638 i2c->getreg = oc_getreg_io_8; in ocores_i2c_probe()
643 i2c->reg_shift = pdata->reg_shift; in ocores_i2c_probe()
644 i2c->reg_io_width = pdata->reg_io_width; in ocores_i2c_probe()
645 i2c->ip_clock_khz = pdata->clock_khz; in ocores_i2c_probe()
647 i2c->bus_clock_khz = pdata->bus_khz; in ocores_i2c_probe()
649 i2c->bus_clock_khz = 100; in ocores_i2c_probe()
651 ret = ocores_i2c_of_probe(pdev, i2c); in ocores_i2c_probe()
656 if (i2c->reg_io_width == 0) in ocores_i2c_probe()
657 i2c->reg_io_width = 1; /* Set to default value */ in ocores_i2c_probe()
659 if (!i2c->setreg || !i2c->getreg) { in ocores_i2c_probe()
663 switch (i2c->reg_io_width) { in ocores_i2c_probe()
665 i2c->setreg = oc_setreg_8; in ocores_i2c_probe()
666 i2c->getreg = oc_getreg_8; in ocores_i2c_probe()
670 i2c->setreg = be ? oc_setreg_16be : oc_setreg_16; in ocores_i2c_probe()
671 i2c->getreg = be ? oc_getreg_16be : oc_getreg_16; in ocores_i2c_probe()
675 i2c->setreg = be ? oc_setreg_32be : oc_setreg_32; in ocores_i2c_probe()
676 i2c->getreg = be ? oc_getreg_32be : oc_getreg_32; in ocores_i2c_probe()
681 i2c->reg_io_width); in ocores_i2c_probe()
687 init_waitqueue_head(&i2c->wait); in ocores_i2c_probe()
699 i2c->flags |= OCORES_FLAG_BROKEN_IRQ; in ocores_i2c_probe()
708 pdev->name, i2c); in ocores_i2c_probe()
715 ret = ocores_init(&pdev->dev, i2c); in ocores_i2c_probe()
720 platform_set_drvdata(pdev, i2c); in ocores_i2c_probe()
721 i2c->adap = ocores_adapter; in ocores_i2c_probe()
722 i2c_set_adapdata(&i2c->adap, i2c); in ocores_i2c_probe()
723 i2c->adap.dev.parent = &pdev->dev; in ocores_i2c_probe()
724 i2c->adap.dev.of_node = pdev->dev.of_node; in ocores_i2c_probe()
726 /* add i2c adapter to i2c tree */ in ocores_i2c_probe()
727 ret = i2c_add_adapter(&i2c->adap); in ocores_i2c_probe()
734 i2c_new_client_device(&i2c->adap, pdata->devices + i); in ocores_i2c_probe()
740 clk_disable_unprepare(i2c->clk); in ocores_i2c_probe()
746 struct ocores_i2c *i2c = platform_get_drvdata(pdev); in ocores_i2c_remove() local
747 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_i2c_remove()
749 /* disable i2c logic */ in ocores_i2c_remove()
751 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_i2c_remove()
754 i2c_del_adapter(&i2c->adap); in ocores_i2c_remove()
756 if (!IS_ERR(i2c->clk)) in ocores_i2c_remove()
757 clk_disable_unprepare(i2c->clk); in ocores_i2c_remove()
765 struct ocores_i2c *i2c = dev_get_drvdata(dev); in ocores_i2c_suspend() local
766 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); in ocores_i2c_suspend()
770 oc_setreg(i2c, OCI2C_CONTROL, ctrl); in ocores_i2c_suspend()
772 if (!IS_ERR(i2c->clk)) in ocores_i2c_suspend()
773 clk_disable_unprepare(i2c->clk); in ocores_i2c_suspend()
779 struct ocores_i2c *i2c = dev_get_drvdata(dev); in ocores_i2c_resume() local
781 if (!IS_ERR(i2c->clk)) { in ocores_i2c_resume()
783 int ret = clk_prepare_enable(i2c->clk); in ocores_i2c_resume()
790 rate = clk_get_rate(i2c->clk) / 1000; in ocores_i2c_resume()
792 i2c->ip_clock_khz = rate; in ocores_i2c_resume()
794 return ocores_init(dev, i2c); in ocores_i2c_resume()
807 .name = "ocores-i2c",
816 MODULE_DESCRIPTION("OpenCores I2C bus driver");
818 MODULE_ALIAS("platform:ocores-i2c");