Lines Matching +full:rcar +full:- +full:gen2 +full:- +full:can

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>
24 #include <linux/i2c-smbus.h>
58 #define MDBS BIT(7) /* non-fifo mode switch */
171 #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
172 #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
176 writel(val, priv->io + reg); in rcar_i2c_write()
181 return readl(priv->io + reg); in rcar_i2c_read()
186 writel(~val & 0x7f, priv->io + ICMSR); in rcar_i2c_clear_irq()
201 priv->recovery_icmcr |= FSCL; in rcar_i2c_set_scl()
203 priv->recovery_icmcr &= ~FSCL; in rcar_i2c_set_scl()
205 rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr); in rcar_i2c_set_scl()
213 priv->recovery_icmcr |= FSDA; in rcar_i2c_set_sda()
215 priv->recovery_icmcr &= ~FSDA; in rcar_i2c_set_sda()
217 rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr); in rcar_i2c_set_sda()
242 if (priv->devtype < I2C_RCAR_GEN3) { in rcar_i2c_init()
243 rcar_i2c_write(priv, ICCCR, priv->icccr); in rcar_i2c_init()
247 if (priv->flags & ID_P_FMPLUS) in rcar_i2c_init()
251 rcar_i2c_write(priv, ICCCR, priv->icccr); in rcar_i2c_init()
252 rcar_i2c_write(priv, ICMPR, priv->smd); in rcar_i2c_init()
253 rcar_i2c_write(priv, ICHPR, priv->schd); in rcar_i2c_init()
254 rcar_i2c_write(priv, ICLPR, priv->scld); in rcar_i2c_init()
264 rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */ in rcar_i2c_reset_slave()
272 ret = readl_poll_timeout(priv->io + ICMCR, val, !(val & FSDA), 10, in rcar_i2c_bus_barrier()
273 priv->adap.timeout); in rcar_i2c_bus_barrier()
276 priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL; in rcar_i2c_bus_barrier()
277 ret = i2c_recover_bus(&priv->adap); in rcar_i2c_bus_barrier()
297 priv->smd = RCAR_DEFAULT_SMD; in rcar_i2c_clock_calculate()
315 * F[] : integer up-valuation in rcar_i2c_clock_calculate()
317 rate = clk_get_rate(priv->clk); in rcar_i2c_clock_calculate()
319 cdf_width = (priv->devtype == I2C_RCAR_GEN1) ? 2 : 3; in rcar_i2c_clock_calculate()
323 if (t.bus_freq_hz > I2C_MAX_FAST_MODE_FREQ && priv->devtype >= I2C_RCAR_GEN4) in rcar_i2c_clock_calculate()
324 priv->flags |= ID_P_FMPLUS; in rcar_i2c_clock_calculate()
326 priv->flags &= ~ID_P_FMPLUS; in rcar_i2c_clock_calculate()
329 ick = rate / (priv->devtype < I2C_RCAR_GEN3 ? (cdf + 1) : 1); in rcar_i2c_clock_calculate()
342 if (priv->devtype < I2C_RCAR_GEN3) { in rcar_i2c_clock_calculate()
347 * SCGD = ((ick / SCL) - 20 - F[...]) / 8 in rcar_i2c_clock_calculate()
351 scgd = DIV_ROUND_UP(scgd - 20 - round, 8); in rcar_i2c_clock_calculate()
360 priv->icccr = scgd << cdf_width | cdf; in rcar_i2c_clock_calculate()
365 * where they are defined. With these definitions, we can compute in rcar_i2c_clock_calculate()
376 * x = ((clkp / SCL) - 8 - 2 * smd - F[...]) / sum_ratio in rcar_i2c_clock_calculate()
379 x = DIV_ROUND_UP(x - 8 - 2 * priv->smd - round, sum_ratio); in rcar_i2c_clock_calculate()
380 scl = rate / (8 + 2 * priv->smd + sum_ratio * x + round); in rcar_i2c_clock_calculate()
385 priv->icccr = cdf; in rcar_i2c_clock_calculate()
386 priv->schd = RCAR_SCHD_RATIO * x; in rcar_i2c_clock_calculate()
387 priv->scld = RCAR_SCLD_RATIO * x; in rcar_i2c_clock_calculate()
388 if (priv->smd >= priv->schd) in rcar_i2c_clock_calculate()
389 priv->smd = priv->schd - 1; in rcar_i2c_clock_calculate()
392 scl, t.bus_freq_hz, rate, round, cdf, priv->schd, priv->scld, priv->smd); in rcar_i2c_clock_calculate()
399 return -EINVAL; in rcar_i2c_clock_calculate()
411 bool rep_start = !(priv->flags & ID_REP_AFTER_RD); in rcar_i2c_prepare_msg()
413 priv->pos = 0; in rcar_i2c_prepare_msg()
414 priv->flags &= ID_P_MASK; in rcar_i2c_prepare_msg()
416 if (priv->msgs_left == 1) in rcar_i2c_prepare_msg()
417 priv->flags |= ID_LAST_MSG; in rcar_i2c_prepare_msg()
419 rcar_i2c_write(priv, ICMAR, i2c_8bit_addr_from_msg(priv->msg)); in rcar_i2c_prepare_msg()
420 if (priv->flags & ID_P_NOT_ATOMIC) in rcar_i2c_prepare_msg()
430 priv->msg = msgs; in rcar_i2c_first_msg()
431 priv->msgs_left = num; in rcar_i2c_first_msg()
438 priv->msg++; in rcar_i2c_next_msg()
439 priv->msgs_left--; in rcar_i2c_next_msg()
446 struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE in rcar_i2c_cleanup_dma()
447 ? priv->dma_rx : priv->dma_tx; in rcar_i2c_cleanup_dma()
453 dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg), in rcar_i2c_cleanup_dma()
454 sg_dma_len(&priv->sg), priv->dma_direction); in rcar_i2c_cleanup_dma()
456 /* Gen3+ can only do one RXDMA per transfer and we just completed it */ in rcar_i2c_cleanup_dma()
457 if (priv->devtype >= I2C_RCAR_GEN3 && in rcar_i2c_cleanup_dma()
458 priv->dma_direction == DMA_FROM_DEVICE) in rcar_i2c_cleanup_dma()
459 priv->flags |= ID_P_NO_RXDMA; in rcar_i2c_cleanup_dma()
461 priv->dma_direction = DMA_NONE; in rcar_i2c_cleanup_dma()
471 priv->pos += sg_dma_len(&priv->sg); in rcar_i2c_dma_callback()
479 struct i2c_msg *msg = priv->msg; in rcar_i2c_dma()
480 bool read = msg->flags & I2C_M_RD; in rcar_i2c_dma()
482 struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx; in rcar_i2c_dma()
490 if (!(priv->flags & ID_P_NOT_ATOMIC) || IS_ERR(chan) || msg->len < RCAR_MIN_DMA_LEN || in rcar_i2c_dma()
491 !(msg->flags & I2C_M_DMA_SAFE) || (read && priv->flags & ID_P_NO_RXDMA)) in rcar_i2c_dma()
499 buf = priv->msg->buf; in rcar_i2c_dma()
500 len = priv->msg->len - 2; in rcar_i2c_dma()
505 buf = priv->msg->buf + 1; in rcar_i2c_dma()
506 len = priv->msg->len - 1; in rcar_i2c_dma()
509 dma_addr = dma_map_single(chan->device->dev, buf, len, dir); in rcar_i2c_dma()
510 if (dma_mapping_error(chan->device->dev, dma_addr)) { in rcar_i2c_dma()
515 sg_dma_len(&priv->sg) = len; in rcar_i2c_dma()
516 sg_dma_address(&priv->sg) = dma_addr; in rcar_i2c_dma()
518 priv->dma_direction = dir; in rcar_i2c_dma()
520 txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1, in rcar_i2c_dma()
529 txdesc->callback = rcar_i2c_dma_callback; in rcar_i2c_dma()
530 txdesc->callback_param = priv; in rcar_i2c_dma()
551 struct i2c_msg *msg = priv->msg; in rcar_i2c_irq_send()
561 /* Check if DMA can be enabled and take over */ in rcar_i2c_irq_send()
562 if (priv->pos == 1 && rcar_i2c_dma(priv)) in rcar_i2c_irq_send()
565 if (priv->pos < msg->len) { in rcar_i2c_irq_send()
571 * [ICRXTX] -> [SHIFT] -> [I2C bus] in rcar_i2c_irq_send()
573 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]); in rcar_i2c_irq_send()
574 priv->pos++; in rcar_i2c_irq_send()
581 * [ICRXTX] -> [SHIFT] -> [I2C bus] in rcar_i2c_irq_send()
584 if (priv->flags & ID_LAST_MSG) in rcar_i2c_irq_send()
600 struct i2c_msg *msg = priv->msg; in rcar_i2c_irq_recv()
601 bool recv_len_init = priv->pos == 0 && msg->flags & I2C_M_RECV_LEN; in rcar_i2c_irq_recv()
615 } else if (priv->pos < msg->len) { in rcar_i2c_irq_recv()
619 msg->buf[priv->pos] = data; in rcar_i2c_irq_recv()
622 priv->flags |= ID_DONE | ID_EPROTO; in rcar_i2c_irq_recv()
625 msg->len += msg->buf[0]; in rcar_i2c_irq_recv()
632 priv->pos++; in rcar_i2c_irq_recv()
639 if (priv->pos + 1 == msg->len && !recv_len_init) { in rcar_i2c_irq_recv()
640 if (priv->flags & ID_LAST_MSG) { in rcar_i2c_irq_recv()
644 priv->flags |= ID_REP_AFTER_RD; in rcar_i2c_irq_recv()
648 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG)) in rcar_i2c_irq_recv()
669 i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value); in rcar_i2c_slave_irq()
673 i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value); in rcar_i2c_slave_irq()
684 i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value); in rcar_i2c_slave_irq()
695 ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value); in rcar_i2c_slave_irq()
703 i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value); in rcar_i2c_slave_irq()
712 * This driver has a lock-free design because there are IP cores (at least
713 * R-Car Gen2) which have an inherent race condition in their hardware design.
719 * R-Car Gen3 seems to have this fixed but earlier versions than R-Car Gen2 are
733 priv->flags |= ID_DONE | ID_ARBLOST; in rcar_i2c_irq()
740 if (priv->flags & ID_P_NOT_ATOMIC) in rcar_i2c_irq()
742 priv->flags |= ID_NACK; in rcar_i2c_irq()
748 priv->msgs_left--; /* The last message also made it */ in rcar_i2c_irq()
749 priv->flags |= ID_DONE; in rcar_i2c_irq()
759 if (priv->flags & ID_DONE) { in rcar_i2c_irq()
762 if (priv->flags & ID_P_NOT_ATOMIC) in rcar_i2c_irq()
763 wake_up(&priv->wait); in rcar_i2c_irq()
775 if (likely(!(priv->flags & ID_REP_AFTER_RD))) in rcar_i2c_gen2_irq()
780 if (priv->flags & ID_P_NOT_ATOMIC) in rcar_i2c_gen2_irq()
793 if (priv->flags & ID_P_NOT_ATOMIC) in rcar_i2c_gen3_irq()
800 if (likely(!(priv->flags & ID_REP_AFTER_RD) && msr)) in rcar_i2c_gen3_irq()
852 read = msg->flags & I2C_M_RD; in rcar_i2c_request_dma()
854 chan = read ? priv->dma_rx : priv->dma_tx; in rcar_i2c_request_dma()
855 if (PTR_ERR(chan) != -EPROBE_DEFER) in rcar_i2c_request_dma()
859 chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX); in rcar_i2c_request_dma()
862 priv->dma_rx = chan; in rcar_i2c_request_dma()
864 priv->dma_tx = chan; in rcar_i2c_request_dma()
869 if (!IS_ERR(priv->dma_tx)) { in rcar_i2c_release_dma()
870 dma_release_channel(priv->dma_tx); in rcar_i2c_release_dma()
871 priv->dma_tx = ERR_PTR(-EPROBE_DEFER); in rcar_i2c_release_dma()
874 if (!IS_ERR(priv->dma_rx)) { in rcar_i2c_release_dma()
875 dma_release_channel(priv->dma_rx); in rcar_i2c_release_dma()
876 priv->dma_rx = ERR_PTR(-EPROBE_DEFER); in rcar_i2c_release_dma()
886 if (priv->slave) in rcar_i2c_do_reset()
887 return -EISCONN; in rcar_i2c_do_reset()
889 ret = reset_control_reset(priv->rstc); in rcar_i2c_do_reset()
894 100, false, priv->rstc); in rcar_i2c_do_reset()
906 priv->flags |= ID_P_NOT_ATOMIC; in rcar_i2c_master_xfer()
916 if (priv->devtype >= I2C_RCAR_GEN3) { in rcar_i2c_master_xfer()
920 priv->flags &= ~ID_P_NO_RXDMA; in rcar_i2c_master_xfer()
930 time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE, in rcar_i2c_master_xfer()
931 num * adap->timeout); in rcar_i2c_master_xfer()
934 if (priv->dma_direction != DMA_NONE) in rcar_i2c_master_xfer()
939 ret = -ETIMEDOUT; in rcar_i2c_master_xfer()
940 } else if (priv->flags & ID_NACK) { in rcar_i2c_master_xfer()
941 ret = -ENXIO; in rcar_i2c_master_xfer()
942 } else if (priv->flags & ID_ARBLOST) { in rcar_i2c_master_xfer()
943 ret = -EAGAIN; in rcar_i2c_master_xfer()
944 } else if (priv->flags & ID_EPROTO) { in rcar_i2c_master_xfer()
945 ret = -EPROTO; in rcar_i2c_master_xfer()
947 ret = num - priv->msgs_left; /* The number of transfer */ in rcar_i2c_master_xfer()
952 if (ret < 0 && ret != -ENXIO) in rcar_i2c_master_xfer()
953 dev_err(dev, "error %d : %x\n", ret, priv->flags); in rcar_i2c_master_xfer()
968 priv->flags &= ~ID_P_NOT_ATOMIC; in rcar_i2c_master_xfer_atomic()
980 j = jiffies + num * adap->timeout; in rcar_i2c_master_xfer_atomic()
987 if (priv->devtype < I2C_RCAR_GEN3) in rcar_i2c_master_xfer_atomic()
994 } while (!(priv->flags & ID_DONE) && time_left); in rcar_i2c_master_xfer_atomic()
998 ret = -ETIMEDOUT; in rcar_i2c_master_xfer_atomic()
999 } else if (priv->flags & ID_NACK) { in rcar_i2c_master_xfer_atomic()
1000 ret = -ENXIO; in rcar_i2c_master_xfer_atomic()
1001 } else if (priv->flags & ID_ARBLOST) { in rcar_i2c_master_xfer_atomic()
1002 ret = -EAGAIN; in rcar_i2c_master_xfer_atomic()
1003 } else if (priv->flags & ID_EPROTO) { in rcar_i2c_master_xfer_atomic()
1004 ret = -EPROTO; in rcar_i2c_master_xfer_atomic()
1006 ret = num - priv->msgs_left; /* The number of transfer */ in rcar_i2c_master_xfer_atomic()
1011 if (ret < 0 && ret != -ENXIO) in rcar_i2c_master_xfer_atomic()
1012 dev_err(dev, "error %d : %x\n", ret, priv->flags); in rcar_i2c_master_xfer_atomic()
1019 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); in rcar_reg_slave()
1021 if (priv->slave) in rcar_reg_slave()
1022 return -EBUSY; in rcar_reg_slave()
1024 if (slave->flags & I2C_CLIENT_TEN) in rcar_reg_slave()
1025 return -EAFNOSUPPORT; in rcar_reg_slave()
1030 priv->slave = slave; in rcar_reg_slave()
1031 rcar_i2c_write(priv, ICSAR, slave->addr); in rcar_reg_slave()
1041 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); in rcar_unreg_slave()
1043 WARN_ON(!priv->slave); in rcar_unreg_slave()
1046 disable_irq(priv->irq); in rcar_unreg_slave()
1048 enable_irq(priv->irq); in rcar_unreg_slave()
1050 priv->slave = NULL; in rcar_unreg_slave()
1062 * This HW can't do: in rcar_i2c_func()
1070 if (priv->flags & ID_P_HOST_NOTIFY) in rcar_i2c_func()
1089 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
1090 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
1091 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
1092 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
1093 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
1094 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
1095 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
1096 { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
1097 { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 },
1099 { .compatible = "renesas,i2c-r8a779f0", .data = (void *)I2C_RCAR_GEN3 },
1100 { .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 },
1101 { .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 },
1102 { .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 },
1103 { .compatible = "renesas,rcar-gen4-i2c", .data = (void *)I2C_RCAR_GEN4 },
1112 struct device *dev = &pdev->dev; in rcar_i2c_probe()
1122 return -ENOMEM; in rcar_i2c_probe()
1124 priv->clk = devm_clk_get(dev, NULL); in rcar_i2c_probe()
1125 if (IS_ERR(priv->clk)) { in rcar_i2c_probe()
1127 return PTR_ERR(priv->clk); in rcar_i2c_probe()
1130 priv->io = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res); in rcar_i2c_probe()
1131 if (IS_ERR(priv->io)) in rcar_i2c_probe()
1132 return PTR_ERR(priv->io); in rcar_i2c_probe()
1134 priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev); in rcar_i2c_probe()
1135 init_waitqueue_head(&priv->wait); in rcar_i2c_probe()
1137 adap = &priv->adap; in rcar_i2c_probe()
1138 adap->nr = pdev->id; in rcar_i2c_probe()
1139 adap->algo = &rcar_i2c_algo; in rcar_i2c_probe()
1140 adap->class = I2C_CLASS_DEPRECATED; in rcar_i2c_probe()
1141 adap->retries = 3; in rcar_i2c_probe()
1142 adap->dev.parent = dev; in rcar_i2c_probe()
1143 adap->dev.of_node = dev->of_node; in rcar_i2c_probe()
1144 adap->bus_recovery_info = &rcar_i2c_bri; in rcar_i2c_probe()
1145 adap->quirks = &rcar_i2c_quirks; in rcar_i2c_probe()
1147 strscpy(adap->name, pdev->name, sizeof(adap->name)); in rcar_i2c_probe()
1150 sg_init_table(&priv->sg, 1); in rcar_i2c_probe()
1151 priv->dma_direction = DMA_NONE; in rcar_i2c_probe()
1152 priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER); in rcar_i2c_probe()
1167 /* Stay always active when multi-master to keep arbitration working */ in rcar_i2c_probe()
1168 if (of_property_read_bool(dev->of_node, "multi-master")) in rcar_i2c_probe()
1169 priv->flags |= ID_P_PM_BLOCKED; in rcar_i2c_probe()
1173 if (of_property_read_bool(dev->of_node, "smbus")) in rcar_i2c_probe()
1174 priv->flags |= ID_P_HOST_NOTIFY; in rcar_i2c_probe()
1176 if (priv->devtype < I2C_RCAR_GEN3) { in rcar_i2c_probe()
1180 /* R-Car Gen3+ needs a reset before every transfer */ in rcar_i2c_probe()
1181 priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); in rcar_i2c_probe()
1182 if (IS_ERR(priv->rstc)) { in rcar_i2c_probe()
1183 ret = PTR_ERR(priv->rstc); in rcar_i2c_probe()
1187 ret = reset_control_status(priv->rstc); in rcar_i2c_probe()
1192 priv->flags &= ~ID_P_HOST_NOTIFY; in rcar_i2c_probe()
1198 priv->irq = ret; in rcar_i2c_probe()
1199 ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv); in rcar_i2c_probe()
1201 dev_err(dev, "cannot get irq %d\n", priv->irq); in rcar_i2c_probe()
1211 if (priv->flags & ID_P_HOST_NOTIFY) { in rcar_i2c_probe()
1212 priv->host_notify_client = i2c_new_slave_host_notify_device(adap); in rcar_i2c_probe()
1213 if (IS_ERR(priv->host_notify_client)) { in rcar_i2c_probe()
1214 ret = PTR_ERR(priv->host_notify_client); in rcar_i2c_probe()
1224 i2c_del_adapter(&priv->adap); in rcar_i2c_probe()
1226 if (priv->flags & ID_P_PM_BLOCKED) in rcar_i2c_probe()
1236 struct device *dev = &pdev->dev; in rcar_i2c_remove()
1238 if (priv->host_notify_client) in rcar_i2c_remove()
1239 i2c_free_slave_host_notify_device(priv->host_notify_client); in rcar_i2c_remove()
1240 i2c_del_adapter(&priv->adap); in rcar_i2c_remove()
1242 if (priv->flags & ID_P_PM_BLOCKED) in rcar_i2c_remove()
1251 i2c_mark_adapter_suspended(&priv->adap); in rcar_i2c_suspend()
1259 i2c_mark_adapter_resumed(&priv->adap); in rcar_i2c_resume()
1269 .name = "i2c-rcar",
1280 MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");