Lines Matching +full:rcar +full:- +full:gen2 +full:- +full:i2c
1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the Renesas R-Car I2C unit
5 * Copyright (C) 2014-19 Wolfram Sang <wsa@sang-engineering.com>
6 * Copyright (C) 2011-2019 Renesas Electronics Corporation
8 * Copyright (C) 2012-14 Renesas Solutions Corp.
11 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
12 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
18 #include <linux/dma-mapping.h>
23 #include <linux/i2c.h>
24 #include <linux/i2c-smbus.h>
54 #define MDBS (1 << 7) /* non-fifo mode switch */
149 #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
150 #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
154 writel(val, priv->io + reg); in rcar_i2c_write()
159 return readl(priv->io + reg); in rcar_i2c_read()
175 priv->recovery_icmcr |= FSCL; in rcar_i2c_set_scl()
177 priv->recovery_icmcr &= ~FSCL; in rcar_i2c_set_scl()
179 rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr); in rcar_i2c_set_scl()
187 priv->recovery_icmcr |= FSDA; in rcar_i2c_set_sda()
189 priv->recovery_icmcr &= ~FSDA; in rcar_i2c_set_sda()
191 rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr); in rcar_i2c_set_sda()
216 rcar_i2c_write(priv, ICCCR, priv->icccr); in rcar_i2c_init()
218 if (priv->devtype == I2C_RCAR_GEN3) in rcar_i2c_init()
228 ret = readl_poll_timeout(priv->io + ICMCR, val, !(val & FSDA), 10, in rcar_i2c_bus_barrier()
229 priv->adap.timeout); in rcar_i2c_bus_barrier()
232 priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL; in rcar_i2c_bus_barrier()
233 ret = i2c_recover_bus(&priv->adap); in rcar_i2c_bus_barrier()
254 switch (priv->devtype) { in rcar_i2c_clock_calculate()
264 return -EIO; in rcar_i2c_clock_calculate()
275 * ick : I2C internal clock < 20 MHz in rcar_i2c_clock_calculate()
276 * ticf : I2C SCL falling time in rcar_i2c_clock_calculate()
277 * tr : I2C SCL rising time in rcar_i2c_clock_calculate()
280 * F[] : integer up-valuation in rcar_i2c_clock_calculate()
282 rate = clk_get_rate(priv->clk); in rcar_i2c_clock_calculate()
286 return -EIO; in rcar_i2c_clock_calculate()
310 * scgd = (div - 20 - round + 7) / 8; in rcar_i2c_clock_calculate()
320 return -EIO; in rcar_i2c_clock_calculate()
327 priv->icccr = scgd << cdf_width | cdf; in rcar_i2c_clock_calculate()
336 priv->pos = 0; in rcar_i2c_prepare_msg()
337 if (priv->msgs_left == 1) in rcar_i2c_prepare_msg()
338 priv->flags |= ID_LAST_MSG; in rcar_i2c_prepare_msg()
340 rcar_i2c_write(priv, ICMAR, i2c_8bit_addr_from_msg(priv->msg)); in rcar_i2c_prepare_msg()
346 if (priv->flags & ID_FIRST_MSG) { in rcar_i2c_prepare_msg()
350 if (priv->flags & ID_P_REP_AFTER_RD) in rcar_i2c_prepare_msg()
351 priv->flags &= ~ID_P_REP_AFTER_RD; in rcar_i2c_prepare_msg()
361 priv->msg++; in rcar_i2c_next_msg()
362 priv->msgs_left--; in rcar_i2c_next_msg()
363 priv->flags &= ID_P_MASK; in rcar_i2c_next_msg()
369 struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE in rcar_i2c_dma_unmap()
370 ? priv->dma_rx : priv->dma_tx; in rcar_i2c_dma_unmap()
372 dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg), in rcar_i2c_dma_unmap()
373 sg_dma_len(&priv->sg), priv->dma_direction); in rcar_i2c_dma_unmap()
376 if (priv->devtype == I2C_RCAR_GEN3 && in rcar_i2c_dma_unmap()
377 priv->dma_direction == DMA_FROM_DEVICE) in rcar_i2c_dma_unmap()
378 priv->flags |= ID_P_NO_RXDMA; in rcar_i2c_dma_unmap()
380 priv->dma_direction = DMA_NONE; in rcar_i2c_dma_unmap()
388 if (priv->dma_direction == DMA_NONE) in rcar_i2c_cleanup_dma()
390 else if (priv->dma_direction == DMA_FROM_DEVICE) in rcar_i2c_cleanup_dma()
391 dmaengine_terminate_all(priv->dma_rx); in rcar_i2c_cleanup_dma()
392 else if (priv->dma_direction == DMA_TO_DEVICE) in rcar_i2c_cleanup_dma()
393 dmaengine_terminate_all(priv->dma_tx); in rcar_i2c_cleanup_dma()
402 priv->pos += sg_dma_len(&priv->sg); in rcar_i2c_dma_callback()
410 struct i2c_msg *msg = priv->msg; in rcar_i2c_dma()
411 bool read = msg->flags & I2C_M_RD; in rcar_i2c_dma()
413 struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx; in rcar_i2c_dma()
421 if (IS_ERR(chan) || msg->len < RCAR_MIN_DMA_LEN || in rcar_i2c_dma()
422 !(msg->flags & I2C_M_DMA_SAFE) || (read && priv->flags & ID_P_NO_RXDMA)) in rcar_i2c_dma()
430 buf = priv->msg->buf; in rcar_i2c_dma()
431 len = priv->msg->len - 2; in rcar_i2c_dma()
436 buf = priv->msg->buf + 1; in rcar_i2c_dma()
437 len = priv->msg->len - 1; in rcar_i2c_dma()
440 dma_addr = dma_map_single(chan->device->dev, buf, len, dir); in rcar_i2c_dma()
441 if (dma_mapping_error(chan->device->dev, dma_addr)) { in rcar_i2c_dma()
446 sg_dma_len(&priv->sg) = len; in rcar_i2c_dma()
447 sg_dma_address(&priv->sg) = dma_addr; in rcar_i2c_dma()
449 priv->dma_direction = dir; in rcar_i2c_dma()
451 txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1, in rcar_i2c_dma()
460 txdesc->callback = rcar_i2c_dma_callback; in rcar_i2c_dma()
461 txdesc->callback_param = priv; in rcar_i2c_dma()
482 struct i2c_msg *msg = priv->msg; in rcar_i2c_irq_send()
489 if (priv->pos == 1 && rcar_i2c_dma(priv)) in rcar_i2c_irq_send()
492 if (priv->pos < msg->len) { in rcar_i2c_irq_send()
498 * [ICRXTX] -> [SHIFT] -> [I2C bus] in rcar_i2c_irq_send()
500 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]); in rcar_i2c_irq_send()
501 priv->pos++; in rcar_i2c_irq_send()
505 * It is on _SHIFT_ register, and will sent to I2C bus. in rcar_i2c_irq_send()
508 * [ICRXTX] -> [SHIFT] -> [I2C bus] in rcar_i2c_irq_send()
511 if (priv->flags & ID_LAST_MSG) { in rcar_i2c_irq_send()
529 struct i2c_msg *msg = priv->msg; in rcar_i2c_irq_recv()
541 } else if (priv->pos < msg->len) { in rcar_i2c_irq_recv()
543 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX); in rcar_i2c_irq_recv()
544 priv->pos++; in rcar_i2c_irq_recv()
548 if (priv->pos + 1 == msg->len) { in rcar_i2c_irq_recv()
549 if (priv->flags & ID_LAST_MSG) { in rcar_i2c_irq_recv()
553 priv->flags |= ID_P_REP_AFTER_RD; in rcar_i2c_irq_recv()
557 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG)) in rcar_i2c_irq_recv()
578 i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value); in rcar_i2c_slave_irq()
582 i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value); in rcar_i2c_slave_irq()
593 i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value); in rcar_i2c_slave_irq()
604 ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value); in rcar_i2c_slave_irq()
612 i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value); in rcar_i2c_slave_irq()
621 * This driver has a lock-free design because there are IP cores (at least
622 * R-Car Gen2) which have an inherent race condition in their hardware design.
628 * R-Car Gen3 seems to have this fixed but earlier versions than R-Car Gen2 are
642 priv->flags |= ID_DONE | ID_ARBLOST; in rcar_i2c_irq()
650 priv->flags |= ID_NACK; in rcar_i2c_irq()
656 priv->msgs_left--; /* The last message also made it */ in rcar_i2c_irq()
657 priv->flags |= ID_DONE; in rcar_i2c_irq()
667 if (priv->flags & ID_DONE) { in rcar_i2c_irq()
670 wake_up(&priv->wait); in rcar_i2c_irq()
682 if (likely(!(priv->flags & ID_P_REP_AFTER_RD))) in rcar_i2c_gen2_irq()
705 if (likely(!(priv->flags & ID_P_REP_AFTER_RD) && msr)) in rcar_i2c_gen3_irq()
757 read = msg->flags & I2C_M_RD; in rcar_i2c_request_dma()
759 chan = read ? priv->dma_rx : priv->dma_tx; in rcar_i2c_request_dma()
760 if (PTR_ERR(chan) != -EPROBE_DEFER) in rcar_i2c_request_dma()
764 chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX); in rcar_i2c_request_dma()
767 priv->dma_rx = chan; in rcar_i2c_request_dma()
769 priv->dma_tx = chan; in rcar_i2c_request_dma()
774 if (!IS_ERR(priv->dma_tx)) { in rcar_i2c_release_dma()
775 dma_release_channel(priv->dma_tx); in rcar_i2c_release_dma()
776 priv->dma_tx = ERR_PTR(-EPROBE_DEFER); in rcar_i2c_release_dma()
779 if (!IS_ERR(priv->dma_rx)) { in rcar_i2c_release_dma()
780 dma_release_channel(priv->dma_rx); in rcar_i2c_release_dma()
781 priv->dma_rx = ERR_PTR(-EPROBE_DEFER); in rcar_i2c_release_dma()
785 /* I2C is a special case, we need to poll the status of a reset */
790 ret = reset_control_reset(priv->rstc); in rcar_i2c_do_reset()
795 100, false, priv->rstc); in rcar_i2c_do_reset()
815 if (priv->devtype == I2C_RCAR_GEN3) { in rcar_i2c_master_xfer()
816 priv->flags |= ID_P_NO_RXDMA; in rcar_i2c_master_xfer()
817 if (!IS_ERR(priv->rstc)) { in rcar_i2c_master_xfer()
820 priv->flags &= ~ID_P_NO_RXDMA; in rcar_i2c_master_xfer()
830 priv->msg = msgs; in rcar_i2c_master_xfer()
831 priv->msgs_left = num; in rcar_i2c_master_xfer()
832 priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG; in rcar_i2c_master_xfer()
835 time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE, in rcar_i2c_master_xfer()
836 num * adap->timeout); in rcar_i2c_master_xfer()
839 if (priv->dma_direction != DMA_NONE) in rcar_i2c_master_xfer()
844 ret = -ETIMEDOUT; in rcar_i2c_master_xfer()
845 } else if (priv->flags & ID_NACK) { in rcar_i2c_master_xfer()
846 ret = -ENXIO; in rcar_i2c_master_xfer()
847 } else if (priv->flags & ID_ARBLOST) { in rcar_i2c_master_xfer()
848 ret = -EAGAIN; in rcar_i2c_master_xfer()
850 ret = num - priv->msgs_left; /* The number of transfer */ in rcar_i2c_master_xfer()
855 if (ret < 0 && ret != -ENXIO) in rcar_i2c_master_xfer()
856 dev_err(dev, "error %d : %x\n", ret, priv->flags); in rcar_i2c_master_xfer()
863 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); in rcar_reg_slave()
865 if (priv->slave) in rcar_reg_slave()
866 return -EBUSY; in rcar_reg_slave()
868 if (slave->flags & I2C_CLIENT_TEN) in rcar_reg_slave()
869 return -EAFNOSUPPORT; in rcar_reg_slave()
874 priv->slave = slave; in rcar_reg_slave()
875 rcar_i2c_write(priv, ICSAR, slave->addr); in rcar_reg_slave()
885 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); in rcar_unreg_slave()
887 WARN_ON(!priv->slave); in rcar_unreg_slave()
890 disable_irq(priv->irq); in rcar_unreg_slave()
893 enable_irq(priv->irq); in rcar_unreg_slave()
895 rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */ in rcar_unreg_slave()
897 priv->slave = NULL; in rcar_unreg_slave()
917 if (priv->flags & ID_P_HOST_NOTIFY) in rcar_i2c_func()
935 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
936 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
937 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
938 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
939 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
940 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
941 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
942 { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
943 { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 },
944 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 }, /* Deprecated */
945 { .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 },
946 { .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 },
947 { .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 },
956 struct device *dev = &pdev->dev; in rcar_i2c_probe()
966 return -ENOMEM; in rcar_i2c_probe()
968 priv->clk = devm_clk_get(dev, NULL); in rcar_i2c_probe()
969 if (IS_ERR(priv->clk)) { in rcar_i2c_probe()
971 return PTR_ERR(priv->clk); in rcar_i2c_probe()
974 priv->io = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res); in rcar_i2c_probe()
975 if (IS_ERR(priv->io)) in rcar_i2c_probe()
976 return PTR_ERR(priv->io); in rcar_i2c_probe()
978 priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev); in rcar_i2c_probe()
979 init_waitqueue_head(&priv->wait); in rcar_i2c_probe()
981 adap = &priv->adap; in rcar_i2c_probe()
982 adap->nr = pdev->id; in rcar_i2c_probe()
983 adap->algo = &rcar_i2c_algo; in rcar_i2c_probe()
984 adap->class = I2C_CLASS_DEPRECATED; in rcar_i2c_probe()
985 adap->retries = 3; in rcar_i2c_probe()
986 adap->dev.parent = dev; in rcar_i2c_probe()
987 adap->dev.of_node = dev->of_node; in rcar_i2c_probe()
988 adap->bus_recovery_info = &rcar_i2c_bri; in rcar_i2c_probe()
989 adap->quirks = &rcar_i2c_quirks; in rcar_i2c_probe()
991 strlcpy(adap->name, pdev->name, sizeof(adap->name)); in rcar_i2c_probe()
994 sg_init_table(&priv->sg, 1); in rcar_i2c_probe()
995 priv->dma_direction = DMA_NONE; in rcar_i2c_probe()
996 priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER); in rcar_i2c_probe()
1007 rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */ in rcar_i2c_probe()
1009 if (priv->devtype < I2C_RCAR_GEN3) { in rcar_i2c_probe()
1014 if (priv->devtype == I2C_RCAR_GEN3) { in rcar_i2c_probe()
1015 priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); in rcar_i2c_probe()
1016 if (!IS_ERR(priv->rstc)) { in rcar_i2c_probe()
1017 ret = reset_control_status(priv->rstc); in rcar_i2c_probe()
1019 priv->rstc = ERR_PTR(-ENOTSUPP); in rcar_i2c_probe()
1023 /* Stay always active when multi-master to keep arbitration working */ in rcar_i2c_probe()
1024 if (of_property_read_bool(dev->of_node, "multi-master")) in rcar_i2c_probe()
1025 priv->flags |= ID_P_PM_BLOCKED; in rcar_i2c_probe()
1029 if (of_property_read_bool(dev->of_node, "smbus")) in rcar_i2c_probe()
1030 priv->flags |= ID_P_HOST_NOTIFY; in rcar_i2c_probe()
1035 priv->irq = ret; in rcar_i2c_probe()
1036 ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv); in rcar_i2c_probe()
1038 dev_err(dev, "cannot get irq %d\n", priv->irq); in rcar_i2c_probe()
1048 if (priv->flags & ID_P_HOST_NOTIFY) { in rcar_i2c_probe()
1049 priv->host_notify_client = i2c_new_slave_host_notify_device(adap); in rcar_i2c_probe()
1050 if (IS_ERR(priv->host_notify_client)) { in rcar_i2c_probe()
1051 ret = PTR_ERR(priv->host_notify_client); in rcar_i2c_probe()
1061 i2c_del_adapter(&priv->adap); in rcar_i2c_probe()
1063 if (priv->flags & ID_P_PM_BLOCKED) in rcar_i2c_probe()
1073 struct device *dev = &pdev->dev; in rcar_i2c_remove()
1075 if (priv->host_notify_client) in rcar_i2c_remove()
1076 i2c_free_slave_host_notify_device(priv->host_notify_client); in rcar_i2c_remove()
1077 i2c_del_adapter(&priv->adap); in rcar_i2c_remove()
1079 if (priv->flags & ID_P_PM_BLOCKED) in rcar_i2c_remove()
1091 i2c_mark_adapter_suspended(&priv->adap); in rcar_i2c_suspend()
1099 i2c_mark_adapter_resumed(&priv->adap); in rcar_i2c_resume()
1114 .name = "i2c-rcar",
1125 MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");