Lines Matching +full:imx28 +full:- +full:i2c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale MXS I2C bus driver
5 * Copyright (C) 2012-2013 Marek Vasut <marex@denx.de>
6 * Copyright (C) 2011-2012 Wolfram Sang, Pengutronix e.K.
8 * based on a (non-working) driver which was:
10 * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
16 #include <linux/i2c.h>
26 #include <linux/dma-mapping.h>
28 #include <linux/dma/mxs-dma.h>
30 #define DRIVER_NAME "mxs-i2c"
70 #define MXS_I2C_DATA(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x60 : 0xa0) argument
72 #define MXS_I2C_DEBUG0_CLR(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x78 : 0xb8) argument
104 * struct mxs_i2c_dev - per device, private MXS-I2C data
111 * @adapter: i2c subsystem adapter node
133 static int mxs_i2c_reset(struct mxs_i2c_dev *i2c) in mxs_i2c_reset() argument
135 int ret = stmp_reset_block(i2c->regs); in mxs_i2c_reset()
140 * Configure timing for the I2C block. The I2C TIMING2 register has to in mxs_i2c_reset()
142 * from the XTAL speed and requested I2C speed. in mxs_i2c_reset()
144 * For details, see i.MX233 [25.4.2 - 25.4.4] and i.MX28 [27.5.2 - 27.5.4]. in mxs_i2c_reset()
146 writel(i2c->timing0, i2c->regs + MXS_I2C_TIMING0); in mxs_i2c_reset()
147 writel(i2c->timing1, i2c->regs + MXS_I2C_TIMING1); in mxs_i2c_reset()
148 writel(i2c->timing2, i2c->regs + MXS_I2C_TIMING2); in mxs_i2c_reset()
150 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET); in mxs_i2c_reset()
155 static void mxs_i2c_dma_finish(struct mxs_i2c_dev *i2c) in mxs_i2c_dma_finish() argument
157 if (i2c->dma_read) { in mxs_i2c_dma_finish()
158 dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE); in mxs_i2c_dma_finish()
159 dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE); in mxs_i2c_dma_finish()
161 dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE); in mxs_i2c_dma_finish()
167 struct mxs_i2c_dev *i2c = param; in mxs_i2c_dma_irq_callback() local
169 complete(&i2c->cmd_complete); in mxs_i2c_dma_irq_callback()
170 mxs_i2c_dma_finish(i2c); in mxs_i2c_dma_irq_callback()
177 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); in mxs_i2c_dma_setup_xfer() local
179 i2c->addr_data = i2c_8bit_addr_from_msg(msg); in mxs_i2c_dma_setup_xfer()
181 if (msg->flags & I2C_M_RD) { in mxs_i2c_dma_setup_xfer()
182 i2c->dma_read = true; in mxs_i2c_dma_setup_xfer()
189 i2c->pio_data[0] = MXS_CMD_I2C_SELECT; in mxs_i2c_dma_setup_xfer()
190 desc = dmaengine_prep_slave_sg(i2c->dmach, in mxs_i2c_dma_setup_xfer()
191 (struct scatterlist *)&i2c->pio_data[0], in mxs_i2c_dma_setup_xfer()
194 dev_err(i2c->dev, in mxs_i2c_dma_setup_xfer()
200 sg_init_one(&i2c->sg_io[0], &i2c->addr_data, 1); in mxs_i2c_dma_setup_xfer()
201 dma_map_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE); in mxs_i2c_dma_setup_xfer()
202 desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[0], 1, in mxs_i2c_dma_setup_xfer()
207 dev_err(i2c->dev, in mxs_i2c_dma_setup_xfer()
217 i2c->pio_data[1] = flags | MXS_CMD_I2C_READ | in mxs_i2c_dma_setup_xfer()
218 MXS_I2C_CTRL0_XFER_COUNT(msg->len); in mxs_i2c_dma_setup_xfer()
219 desc = dmaengine_prep_slave_sg(i2c->dmach, in mxs_i2c_dma_setup_xfer()
220 (struct scatterlist *)&i2c->pio_data[1], in mxs_i2c_dma_setup_xfer()
223 dev_err(i2c->dev, in mxs_i2c_dma_setup_xfer()
229 sg_init_one(&i2c->sg_io[1], msg->buf, msg->len); in mxs_i2c_dma_setup_xfer()
230 dma_map_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE); in mxs_i2c_dma_setup_xfer()
231 desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[1], 1, in mxs_i2c_dma_setup_xfer()
236 dev_err(i2c->dev, in mxs_i2c_dma_setup_xfer()
241 i2c->dma_read = false; in mxs_i2c_dma_setup_xfer()
248 i2c->pio_data[0] = flags | MXS_CMD_I2C_WRITE | in mxs_i2c_dma_setup_xfer()
249 MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1); in mxs_i2c_dma_setup_xfer()
250 desc = dmaengine_prep_slave_sg(i2c->dmach, in mxs_i2c_dma_setup_xfer()
251 (struct scatterlist *)&i2c->pio_data[0], in mxs_i2c_dma_setup_xfer()
254 dev_err(i2c->dev, in mxs_i2c_dma_setup_xfer()
260 sg_init_table(i2c->sg_io, 2); in mxs_i2c_dma_setup_xfer()
261 sg_set_buf(&i2c->sg_io[0], &i2c->addr_data, 1); in mxs_i2c_dma_setup_xfer()
262 sg_set_buf(&i2c->sg_io[1], msg->buf, msg->len); in mxs_i2c_dma_setup_xfer()
263 dma_map_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE); in mxs_i2c_dma_setup_xfer()
264 desc = dmaengine_prep_slave_sg(i2c->dmach, i2c->sg_io, 2, in mxs_i2c_dma_setup_xfer()
269 dev_err(i2c->dev, in mxs_i2c_dma_setup_xfer()
279 desc->callback = mxs_i2c_dma_irq_callback; in mxs_i2c_dma_setup_xfer()
280 desc->callback_param = i2c; in mxs_i2c_dma_setup_xfer()
284 dma_async_issue_pending(i2c->dmach); in mxs_i2c_dma_setup_xfer()
289 dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE); in mxs_i2c_dma_setup_xfer()
291 dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE); in mxs_i2c_dma_setup_xfer()
293 dmaengine_terminate_all(i2c->dmach); in mxs_i2c_dma_setup_xfer()
294 return -EINVAL; in mxs_i2c_dma_setup_xfer()
298 dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE); in mxs_i2c_dma_setup_xfer()
300 dmaengine_terminate_all(i2c->dmach); in mxs_i2c_dma_setup_xfer()
301 return -EINVAL; in mxs_i2c_dma_setup_xfer()
304 static int mxs_i2c_pio_wait_xfer_end(struct mxs_i2c_dev *i2c) in mxs_i2c_pio_wait_xfer_end() argument
308 while (readl(i2c->regs + MXS_I2C_CTRL0) & MXS_I2C_CTRL0_RUN) { in mxs_i2c_pio_wait_xfer_end()
309 if (readl(i2c->regs + MXS_I2C_CTRL1) & in mxs_i2c_pio_wait_xfer_end()
311 return -ENXIO; in mxs_i2c_pio_wait_xfer_end()
313 return -ETIMEDOUT; in mxs_i2c_pio_wait_xfer_end()
320 static int mxs_i2c_pio_check_error_state(struct mxs_i2c_dev *i2c) in mxs_i2c_pio_check_error_state() argument
324 state = readl(i2c->regs + MXS_I2C_CTRL1_CLR) & MXS_I2C_IRQ_MASK; in mxs_i2c_pio_check_error_state()
327 i2c->cmd_err = -ENXIO; in mxs_i2c_pio_check_error_state()
332 i2c->cmd_err = -EIO; in mxs_i2c_pio_check_error_state()
334 return i2c->cmd_err; in mxs_i2c_pio_check_error_state()
337 static void mxs_i2c_pio_trigger_cmd(struct mxs_i2c_dev *i2c, u32 cmd) in mxs_i2c_pio_trigger_cmd() argument
341 writel(cmd, i2c->regs + MXS_I2C_CTRL0); in mxs_i2c_pio_trigger_cmd()
344 reg = readl(i2c->regs + MXS_I2C_CTRL0); in mxs_i2c_pio_trigger_cmd()
346 writel(reg, i2c->regs + MXS_I2C_CTRL0); in mxs_i2c_pio_trigger_cmd()
350 * Start WRITE transaction on the I2C bus. By studying i.MX23 datasheet,
357 static void mxs_i2c_pio_trigger_write_cmd(struct mxs_i2c_dev *i2c, u32 cmd, in mxs_i2c_pio_trigger_write_cmd() argument
360 writel(cmd, i2c->regs + MXS_I2C_CTRL0); in mxs_i2c_pio_trigger_write_cmd()
362 if (i2c->dev_type == MXS_I2C_V1) in mxs_i2c_pio_trigger_write_cmd()
363 writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_SET); in mxs_i2c_pio_trigger_write_cmd()
365 writel(data, i2c->regs + MXS_I2C_DATA(i2c)); in mxs_i2c_pio_trigger_write_cmd()
366 writel(MXS_I2C_CTRL0_RUN, i2c->regs + MXS_I2C_CTRL0_SET); in mxs_i2c_pio_trigger_write_cmd()
372 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); in mxs_i2c_pio_setup_xfer() local
379 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR); in mxs_i2c_pio_setup_xfer()
383 * - Enable CTRL0::PIO_MODE (1 << 24) in mxs_i2c_pio_setup_xfer()
384 * - Enable CTRL1::ACK_MODE (1 << 27) in mxs_i2c_pio_setup_xfer()
398 if (msg->flags & I2C_M_RD) { in mxs_i2c_pio_setup_xfer()
411 BUG_ON(msg->len > 4); in mxs_i2c_pio_setup_xfer()
414 mxs_i2c_pio_trigger_write_cmd(i2c, MXS_CMD_I2C_SELECT, in mxs_i2c_pio_setup_xfer()
417 ret = mxs_i2c_pio_wait_xfer_end(i2c); in mxs_i2c_pio_setup_xfer()
419 dev_dbg(i2c->dev, in mxs_i2c_pio_setup_xfer()
425 mxs_i2c_pio_trigger_cmd(i2c, in mxs_i2c_pio_setup_xfer()
427 MXS_I2C_CTRL0_XFER_COUNT(msg->len)); in mxs_i2c_pio_setup_xfer()
429 ret = mxs_i2c_pio_wait_xfer_end(i2c); in mxs_i2c_pio_setup_xfer()
431 dev_dbg(i2c->dev, in mxs_i2c_pio_setup_xfer()
436 data = readl(i2c->regs + MXS_I2C_DATA(i2c)); in mxs_i2c_pio_setup_xfer()
437 for (i = 0; i < msg->len; i++) { in mxs_i2c_pio_setup_xfer()
438 msg->buf[i] = data & 0xff; in mxs_i2c_pio_setup_xfer()
463 if (msg->len > 3) in mxs_i2c_pio_setup_xfer()
466 for (i = 0; i < msg->len; i++) { in mxs_i2c_pio_setup_xfer()
468 data |= (msg->buf[i] << 24); in mxs_i2c_pio_setup_xfer()
473 if (i + 1 == msg->len) { in mxs_i2c_pio_setup_xfer()
504 data >>= (4 - xlen) * 8; in mxs_i2c_pio_setup_xfer()
506 dev_dbg(i2c->dev, in mxs_i2c_pio_setup_xfer()
508 xlen, i, msg->len, in mxs_i2c_pio_setup_xfer()
514 i2c->regs + MXS_I2C_DEBUG0_CLR(i2c)); in mxs_i2c_pio_setup_xfer()
516 mxs_i2c_pio_trigger_write_cmd(i2c, in mxs_i2c_pio_setup_xfer()
525 ret = mxs_i2c_pio_wait_xfer_end(i2c); in mxs_i2c_pio_setup_xfer()
527 dev_dbg(i2c->dev, in mxs_i2c_pio_setup_xfer()
533 ret = readl(i2c->regs + MXS_I2C_STAT) & in mxs_i2c_pio_setup_xfer()
536 ret = -ENXIO; in mxs_i2c_pio_setup_xfer()
543 ret = mxs_i2c_pio_check_error_state(i2c); in mxs_i2c_pio_setup_xfer()
546 /* Clear any dangling IRQs and re-enable interrupts. */ in mxs_i2c_pio_setup_xfer()
547 writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR); in mxs_i2c_pio_setup_xfer()
548 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET); in mxs_i2c_pio_setup_xfer()
551 if (i2c->dev_type == MXS_I2C_V1) in mxs_i2c_pio_setup_xfer()
552 writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_CLR); in mxs_i2c_pio_setup_xfer()
563 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); in mxs_i2c_xfer_msg() local
571 dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n", in mxs_i2c_xfer_msg()
572 msg->addr, msg->len, msg->flags, stop); in mxs_i2c_xfer_msg()
575 * The MX28 I2C IP block can only do PIO READ for transfer of to up in mxs_i2c_xfer_msg()
579 if ((msg->flags & I2C_M_RD) && (msg->len <= 4)) in mxs_i2c_xfer_msg()
581 if (!(msg->flags & I2C_M_RD) && (msg->len < 7)) in mxs_i2c_xfer_msg()
584 i2c->cmd_err = 0; in mxs_i2c_xfer_msg()
588 if (ret && (ret != -ENXIO)) in mxs_i2c_xfer_msg()
589 mxs_i2c_reset(i2c); in mxs_i2c_xfer_msg()
591 reinit_completion(&i2c->cmd_complete); in mxs_i2c_xfer_msg()
596 time_left = wait_for_completion_timeout(&i2c->cmd_complete, in mxs_i2c_xfer_msg()
601 ret = i2c->cmd_err; in mxs_i2c_xfer_msg()
604 if (ret == -ENXIO) { in mxs_i2c_xfer_msg()
610 i2c->regs + MXS_I2C_CTRL1_SET); in mxs_i2c_xfer_msg()
615 * The i.MX23 is strange. After each and every operation, it's I2C IP in mxs_i2c_xfer_msg()
624 if (i2c->dev_type == MXS_I2C_V1) in mxs_i2c_xfer_msg()
625 mxs_i2c_reset(i2c); in mxs_i2c_xfer_msg()
627 dev_dbg(i2c->dev, "Done with err=%d\n", ret); in mxs_i2c_xfer_msg()
632 dev_dbg(i2c->dev, "Timeout!\n"); in mxs_i2c_xfer_msg()
633 mxs_i2c_dma_finish(i2c); in mxs_i2c_xfer_msg()
634 ret = mxs_i2c_reset(i2c); in mxs_i2c_xfer_msg()
638 return -ETIMEDOUT; in mxs_i2c_xfer_msg()
648 err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1)); in mxs_i2c_xfer()
663 struct mxs_i2c_dev *i2c = dev_id; in mxs_i2c_isr() local
664 u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK; in mxs_i2c_isr()
670 i2c->cmd_err = -ENXIO; in mxs_i2c_isr()
675 i2c->cmd_err = -EIO; in mxs_i2c_isr()
677 writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR); in mxs_i2c_isr()
691 static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, uint32_t speed) in mxs_i2c_derive_timing() argument
693 /* The I2C block clock runs at 24MHz */ in mxs_i2c_derive_timing()
698 struct device *dev = i2c->dev; in mxs_i2c_derive_timing()
725 * The I2C spec specifies the following timing data: in mxs_i2c_derive_timing()
759 low_count -= 2; in mxs_i2c_derive_timing()
760 high_count -= 7; in mxs_i2c_derive_timing()
761 i2c->timing0 = (high_count << 16) | rcv_count; in mxs_i2c_derive_timing()
762 i2c->timing1 = (low_count << 16) | xmit_count; in mxs_i2c_derive_timing()
763 i2c->timing2 = (bus_free << 16 | leadin); in mxs_i2c_derive_timing()
766 static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c) in mxs_i2c_get_ofdata() argument
769 struct device *dev = i2c->dev; in mxs_i2c_get_ofdata()
770 struct device_node *node = dev->of_node; in mxs_i2c_get_ofdata()
773 ret = of_property_read_u32(node, "clock-frequency", &speed); in mxs_i2c_get_ofdata()
775 dev_warn(dev, "No I2C speed selected, using 100kHz\n"); in mxs_i2c_get_ofdata()
779 mxs_i2c_derive_timing(i2c, speed); in mxs_i2c_get_ofdata()
786 .name = "imx23-i2c",
789 .name = "imx28-i2c",
796 { .compatible = "fsl,imx23-i2c", .data = &mxs_i2c_devtype[0], },
797 { .compatible = "fsl,imx28-i2c", .data = &mxs_i2c_devtype[1], },
805 of_match_device(mxs_i2c_dt_ids, &pdev->dev); in mxs_i2c_probe()
806 struct device *dev = &pdev->dev; in mxs_i2c_probe()
807 struct mxs_i2c_dev *i2c; in mxs_i2c_probe() local
811 i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL); in mxs_i2c_probe()
812 if (!i2c) in mxs_i2c_probe()
813 return -ENOMEM; in mxs_i2c_probe()
816 const struct platform_device_id *device_id = of_id->data; in mxs_i2c_probe()
817 i2c->dev_type = device_id->driver_data; in mxs_i2c_probe()
820 i2c->regs = devm_platform_ioremap_resource(pdev, 0); in mxs_i2c_probe()
821 if (IS_ERR(i2c->regs)) in mxs_i2c_probe()
822 return PTR_ERR(i2c->regs); in mxs_i2c_probe()
828 err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c); in mxs_i2c_probe()
832 i2c->dev = dev; in mxs_i2c_probe()
834 init_completion(&i2c->cmd_complete); in mxs_i2c_probe()
836 if (dev->of_node) { in mxs_i2c_probe()
837 err = mxs_i2c_get_ofdata(i2c); in mxs_i2c_probe()
843 i2c->dmach = dma_request_chan(dev, "rx-tx"); in mxs_i2c_probe()
844 if (IS_ERR(i2c->dmach)) { in mxs_i2c_probe()
846 return PTR_ERR(i2c->dmach); in mxs_i2c_probe()
849 platform_set_drvdata(pdev, i2c); in mxs_i2c_probe()
852 err = mxs_i2c_reset(i2c); in mxs_i2c_probe()
856 adap = &i2c->adapter; in mxs_i2c_probe()
857 strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name)); in mxs_i2c_probe()
858 adap->owner = THIS_MODULE; in mxs_i2c_probe()
859 adap->algo = &mxs_i2c_algo; in mxs_i2c_probe()
860 adap->quirks = &mxs_i2c_quirks; in mxs_i2c_probe()
861 adap->dev.parent = dev; in mxs_i2c_probe()
862 adap->nr = pdev->id; in mxs_i2c_probe()
863 adap->dev.of_node = pdev->dev.of_node; in mxs_i2c_probe()
864 i2c_set_adapdata(adap, i2c); in mxs_i2c_probe()
868 i2c->regs + MXS_I2C_CTRL0_SET); in mxs_i2c_probe()
877 struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev); in mxs_i2c_remove() local
879 i2c_del_adapter(&i2c->adapter); in mxs_i2c_remove()
881 if (i2c->dmach) in mxs_i2c_remove()
882 dma_release_channel(i2c->dmach); in mxs_i2c_remove()
884 writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET); in mxs_i2c_remove()
912 MODULE_DESCRIPTION("MXS I2C Bus Driver");