Wolfram Sang | 6055af5 | 2018-08-22 00:02:16 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 2 | /* |
Wolfram Sang | 5c8e3ab | 2017-05-28 11:30:45 +0200 | [diff] [blame] | 3 | * Driver for the Renesas R-Car I2C unit |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 4 | * |
Wolfram Sang | d0051ca | 2019-01-21 18:07:59 +0100 | [diff] [blame] | 5 | * Copyright (C) 2014-19 Wolfram Sang <wsa@sang-engineering.com> |
| 6 | * Copyright (C) 2011-2019 Renesas Electronics Corporation |
Wolfram Sang | 3d99bea | 2014-05-28 09:44:46 +0200 | [diff] [blame] | 7 | * |
| 8 | * Copyright (C) 2012-14 Renesas Solutions Corp. |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 9 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 10 | * |
| 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> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 13 | */ |
Wolfram Sang | b07531a | 2018-08-08 09:59:27 +0200 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 15 | #include <linux/clk.h> |
| 16 | #include <linux/delay.h> |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 17 | #include <linux/dmaengine.h> |
| 18 | #include <linux/dma-mapping.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
Wolfram Sang | 9374ed1 | 2020-08-29 22:38:09 +0200 | [diff] [blame] | 22 | #include <linux/iopoll.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 23 | #include <linux/i2c.h> |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 24 | #include <linux/i2c-smbus.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 27 | #include <linux/of_device.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/pm_runtime.h> |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 30 | #include <linux/reset.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 31 | #include <linux/slab.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 32 | |
| 33 | /* register offsets */ |
| 34 | #define ICSCR 0x00 /* slave ctrl */ |
| 35 | #define ICMCR 0x04 /* master ctrl */ |
| 36 | #define ICSSR 0x08 /* slave status */ |
| 37 | #define ICMSR 0x0C /* master status */ |
| 38 | #define ICSIER 0x10 /* slave irq enable */ |
| 39 | #define ICMIER 0x14 /* master irq enable */ |
| 40 | #define ICCCR 0x18 /* clock dividers */ |
| 41 | #define ICSAR 0x1C /* slave address */ |
| 42 | #define ICMAR 0x20 /* master address */ |
| 43 | #define ICRXTX 0x24 /* data port */ |
Wolfram Sang | 1876944 | 2019-02-05 14:37:25 +0100 | [diff] [blame] | 44 | #define ICFBSCR 0x38 /* first bit setup cycle (Gen3) */ |
| 45 | #define ICDMAER 0x3c /* DMA enable (Gen3) */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 46 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 47 | /* ICSCR */ |
| 48 | #define SDBS (1 << 3) /* slave data buffer select */ |
| 49 | #define SIE (1 << 2) /* slave interface enable */ |
| 50 | #define GCAE (1 << 1) /* general call address enable */ |
| 51 | #define FNA (1 << 0) /* forced non acknowledgment */ |
| 52 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 53 | /* ICMCR */ |
| 54 | #define MDBS (1 << 7) /* non-fifo mode switch */ |
| 55 | #define FSCL (1 << 6) /* override SCL pin */ |
| 56 | #define FSDA (1 << 5) /* override SDA pin */ |
| 57 | #define OBPC (1 << 4) /* override pins */ |
| 58 | #define MIE (1 << 3) /* master if enable */ |
| 59 | #define TSBE (1 << 2) |
| 60 | #define FSB (1 << 1) /* force stop bit */ |
Wolfram Sang | fe34fbf | 2018-01-21 15:45:11 +0100 | [diff] [blame] | 61 | #define ESG (1 << 0) /* enable start bit gen */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 62 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 63 | /* ICSSR (also for ICSIER) */ |
| 64 | #define GCAR (1 << 6) /* general call received */ |
| 65 | #define STM (1 << 5) /* slave transmit mode */ |
| 66 | #define SSR (1 << 4) /* stop received */ |
| 67 | #define SDE (1 << 3) /* slave data empty */ |
| 68 | #define SDT (1 << 2) /* slave data transmitted */ |
| 69 | #define SDR (1 << 1) /* slave data received */ |
| 70 | #define SAR (1 << 0) /* slave addr received */ |
| 71 | |
Wolfram Sang | 3e3aaba | 2014-05-28 09:44:44 +0200 | [diff] [blame] | 72 | /* ICMSR (also for ICMIE) */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 73 | #define MNR (1 << 6) /* nack received */ |
| 74 | #define MAL (1 << 5) /* arbitration lost */ |
| 75 | #define MST (1 << 4) /* sent a stop */ |
| 76 | #define MDE (1 << 3) |
| 77 | #define MDT (1 << 2) |
| 78 | #define MDR (1 << 1) |
| 79 | #define MAT (1 << 0) /* slave addr xfer done */ |
| 80 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 81 | /* ICDMAER */ |
| 82 | #define RSDMAE (1 << 3) /* DMA Slave Received Enable */ |
| 83 | #define TSDMAE (1 << 2) /* DMA Slave Transmitted Enable */ |
| 84 | #define RMDMAE (1 << 1) /* DMA Master Received Enable */ |
| 85 | #define TMDMAE (1 << 0) /* DMA Master Transmitted Enable */ |
| 86 | |
| 87 | /* ICFBSCR */ |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 88 | #define TCYC17 0x0f /* 17*Tcyc delay 1st bit between SDA and SCL */ |
| 89 | |
Wolfram Sang | ed5a810 | 2019-03-05 18:54:32 +0100 | [diff] [blame] | 90 | #define RCAR_MIN_DMA_LEN 8 |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 91 | |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 92 | #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG) |
| 93 | #define RCAR_BUS_PHASE_DATA (MDBS | MIE) |
| 94 | #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 95 | |
Wolfram Sang | 3e3aaba | 2014-05-28 09:44:44 +0200 | [diff] [blame] | 96 | #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE) |
| 97 | #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR) |
| 98 | #define RCAR_IRQ_STOP (MST) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 99 | |
Hiromitsu Yamasaki | a1de325 | 2018-03-20 22:04:14 +0100 | [diff] [blame] | 100 | #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0x7F) |
| 101 | #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0x7F) |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 102 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 103 | #define ID_LAST_MSG (1 << 0) |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 104 | #define ID_FIRST_MSG (1 << 1) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 105 | #define ID_DONE (1 << 2) |
| 106 | #define ID_ARBLOST (1 << 3) |
| 107 | #define ID_NACK (1 << 4) |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 108 | /* persistent flags */ |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 109 | #define ID_P_HOST_NOTIFY BIT(28) |
Wolfram Sang | 19358d4 | 2018-08-08 09:59:28 +0200 | [diff] [blame] | 110 | #define ID_P_REP_AFTER_RD BIT(29) |
Wolfram Sang | b07531a | 2018-08-08 09:59:27 +0200 | [diff] [blame] | 111 | #define ID_P_NO_RXDMA BIT(30) /* HW forbids RXDMA sometimes */ |
| 112 | #define ID_P_PM_BLOCKED BIT(31) |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 113 | #define ID_P_MASK GENMASK(31, 28) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 114 | |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 115 | enum rcar_i2c_type { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 116 | I2C_RCAR_GEN1, |
| 117 | I2C_RCAR_GEN2, |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 118 | I2C_RCAR_GEN3, |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 119 | }; |
| 120 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 121 | struct rcar_i2c_priv { |
Wolfram Sang | 25c2e0f | 2020-12-23 18:21:52 +0100 | [diff] [blame] | 122 | u32 flags; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 123 | void __iomem *io; |
| 124 | struct i2c_adapter adap; |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 125 | struct i2c_msg *msg; |
| 126 | int msgs_left; |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 127 | struct clk *clk; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 128 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 129 | wait_queue_head_t wait; |
| 130 | |
| 131 | int pos; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 132 | u32 icccr; |
Wolfram Sang | 7d2c17f | 2018-01-09 14:58:59 +0100 | [diff] [blame] | 133 | u8 recovery_icmcr; /* protected by adapter lock */ |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 134 | enum rcar_i2c_type devtype; |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 135 | struct i2c_client *slave; |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 136 | |
| 137 | struct resource *res; |
| 138 | struct dma_chan *dma_tx; |
| 139 | struct dma_chan *dma_rx; |
| 140 | struct scatterlist sg; |
| 141 | enum dma_data_direction dma_direction; |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 142 | |
| 143 | struct reset_control *rstc; |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 144 | bool atomic_xfer; |
Wolfram Sang | 7b814d8 | 2019-08-08 21:39:10 +0200 | [diff] [blame] | 145 | int irq; |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 146 | |
| 147 | struct i2c_client *host_notify_client; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent) |
| 151 | #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD) |
| 152 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 153 | static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val) |
| 154 | { |
| 155 | writel(val, priv->io + reg); |
| 156 | } |
| 157 | |
| 158 | static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg) |
| 159 | { |
| 160 | return readl(priv->io + reg); |
| 161 | } |
| 162 | |
Wolfram Sang | 7d2c17f | 2018-01-09 14:58:59 +0100 | [diff] [blame] | 163 | static int rcar_i2c_get_scl(struct i2c_adapter *adap) |
| 164 | { |
| 165 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 166 | |
| 167 | return !!(rcar_i2c_read(priv, ICMCR) & FSCL); |
| 168 | |
| 169 | }; |
| 170 | |
| 171 | static void rcar_i2c_set_scl(struct i2c_adapter *adap, int val) |
| 172 | { |
| 173 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 174 | |
| 175 | if (val) |
| 176 | priv->recovery_icmcr |= FSCL; |
| 177 | else |
| 178 | priv->recovery_icmcr &= ~FSCL; |
| 179 | |
| 180 | rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr); |
| 181 | }; |
| 182 | |
Wolfram Sang | 7d2c17f | 2018-01-09 14:58:59 +0100 | [diff] [blame] | 183 | static void rcar_i2c_set_sda(struct i2c_adapter *adap, int val) |
| 184 | { |
| 185 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 186 | |
| 187 | if (val) |
| 188 | priv->recovery_icmcr |= FSDA; |
| 189 | else |
| 190 | priv->recovery_icmcr &= ~FSDA; |
| 191 | |
| 192 | rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr); |
| 193 | }; |
| 194 | |
Wolfram Sang | 4fe10de | 2018-07-11 00:24:23 +0200 | [diff] [blame] | 195 | static int rcar_i2c_get_bus_free(struct i2c_adapter *adap) |
| 196 | { |
| 197 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 198 | |
| 199 | return !(rcar_i2c_read(priv, ICMCR) & FSDA); |
| 200 | |
| 201 | }; |
| 202 | |
Wolfram Sang | 7d2c17f | 2018-01-09 14:58:59 +0100 | [diff] [blame] | 203 | static struct i2c_bus_recovery_info rcar_i2c_bri = { |
| 204 | .get_scl = rcar_i2c_get_scl, |
| 205 | .set_scl = rcar_i2c_set_scl, |
| 206 | .set_sda = rcar_i2c_set_sda, |
Wolfram Sang | 4fe10de | 2018-07-11 00:24:23 +0200 | [diff] [blame] | 207 | .get_bus_free = rcar_i2c_get_bus_free, |
Wolfram Sang | 7d2c17f | 2018-01-09 14:58:59 +0100 | [diff] [blame] | 208 | .recover_bus = i2c_generic_scl_recovery, |
| 209 | }; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 210 | static void rcar_i2c_init(struct rcar_i2c_priv *priv) |
| 211 | { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 212 | /* reset master mode */ |
| 213 | rcar_i2c_write(priv, ICMIER, 0); |
Wolfram Sang | 2c78cdc | 2015-11-19 16:56:42 +0100 | [diff] [blame] | 214 | rcar_i2c_write(priv, ICMCR, MDBS); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 215 | rcar_i2c_write(priv, ICMSR, 0); |
Wolfram Sang | 2c78cdc | 2015-11-19 16:56:42 +0100 | [diff] [blame] | 216 | /* start clock */ |
| 217 | rcar_i2c_write(priv, ICCCR, priv->icccr); |
Wolfram Sang | 1876944 | 2019-02-05 14:37:25 +0100 | [diff] [blame] | 218 | |
| 219 | if (priv->devtype == I2C_RCAR_GEN3) |
| 220 | rcar_i2c_write(priv, ICFBSCR, TCYC17); |
| 221 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 224 | static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv) |
| 225 | { |
Wolfram Sang | 9374ed1 | 2020-08-29 22:38:09 +0200 | [diff] [blame] | 226 | int ret; |
| 227 | u32 val; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 228 | |
Wolfram Sang | 9374ed1 | 2020-08-29 22:38:09 +0200 | [diff] [blame] | 229 | ret = readl_poll_timeout(priv->io + ICMCR, val, !(val & FSDA), 10, |
| 230 | priv->adap.timeout); |
| 231 | if (ret) { |
| 232 | /* Waiting did not help, try to recover */ |
| 233 | priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL; |
| 234 | ret = i2c_recover_bus(&priv->adap); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Wolfram Sang | 9374ed1 | 2020-08-29 22:38:09 +0200 | [diff] [blame] | 237 | return ret; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Andy Shevchenko | 38a592e | 2020-03-24 14:32:13 +0200 | [diff] [blame] | 240 | static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 241 | { |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 242 | u32 scgd, cdf, round, ick, sum, scl, cdf_width; |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 243 | unsigned long rate; |
Wolfram Sang | f9c9d31 | 2015-12-08 10:37:47 +0100 | [diff] [blame] | 244 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
Wolfram Sang | df576be | 2020-03-26 11:07:21 +0100 | [diff] [blame] | 245 | struct i2c_timings t = { |
Andy Shevchenko | 38a592e | 2020-03-24 14:32:13 +0200 | [diff] [blame] | 246 | .bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ, |
| 247 | .scl_fall_ns = 35, |
| 248 | .scl_rise_ns = 200, |
| 249 | .scl_int_delay_ns = 50, |
Wolfram Sang | df576be | 2020-03-26 11:07:21 +0100 | [diff] [blame] | 250 | }; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 251 | |
Wolfram Sang | c788187 | 2015-12-08 10:37:48 +0100 | [diff] [blame] | 252 | /* Fall back to previously used values if not supplied */ |
Wolfram Sang | df576be | 2020-03-26 11:07:21 +0100 | [diff] [blame] | 253 | i2c_parse_fw_timings(dev, &t, false); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 254 | |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 255 | switch (priv->devtype) { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 256 | case I2C_RCAR_GEN1: |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 257 | cdf_width = 2; |
| 258 | break; |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 259 | case I2C_RCAR_GEN2: |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 260 | case I2C_RCAR_GEN3: |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 261 | cdf_width = 3; |
| 262 | break; |
| 263 | default: |
| 264 | dev_err(dev, "device type error\n"); |
| 265 | return -EIO; |
| 266 | } |
| 267 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 268 | /* |
| 269 | * calculate SCL clock |
| 270 | * see |
| 271 | * ICCCR |
| 272 | * |
| 273 | * ick = clkp / (1 + CDF) |
| 274 | * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick]) |
| 275 | * |
| 276 | * ick : I2C internal clock < 20 MHz |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 277 | * ticf : I2C SCL falling time |
| 278 | * tr : I2C SCL rising time |
| 279 | * intd : LSI internal delay |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 280 | * clkp : peripheral_clk |
| 281 | * F[] : integer up-valuation |
| 282 | */ |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 283 | rate = clk_get_rate(priv->clk); |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 284 | cdf = rate / 20000000; |
Wolfram Sang | 22762cc | 2014-09-20 12:07:37 +0200 | [diff] [blame] | 285 | if (cdf >= 1U << cdf_width) { |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 286 | dev_err(dev, "Input clock %lu too high\n", rate); |
| 287 | return -EIO; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 288 | } |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 289 | ick = rate / (cdf + 1); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 290 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 291 | /* |
| 292 | * it is impossible to calculate large scale |
| 293 | * number on u32. separate it |
| 294 | * |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 295 | * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd) |
| 296 | * = F[sum * ick / 1000000000] |
| 297 | * = F[(ick / 1000000) * sum / 1000] |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 298 | */ |
Wolfram Sang | df576be | 2020-03-26 11:07:21 +0100 | [diff] [blame] | 299 | sum = t.scl_fall_ns + t.scl_rise_ns + t.scl_int_delay_ns; |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 300 | round = (ick + 500000) / 1000000 * sum; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 301 | round = (round + 500) / 1000; |
| 302 | |
| 303 | /* |
| 304 | * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick]) |
| 305 | * |
| 306 | * Calculation result (= SCL) should be less than |
| 307 | * bus_speed for hardware safety |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 308 | * |
| 309 | * We could use something along the lines of |
| 310 | * div = ick / (bus_speed + 1) + 1; |
| 311 | * scgd = (div - 20 - round + 7) / 8; |
| 312 | * scl = ick / (20 + (scgd * 8) + round); |
| 313 | * (not fully verified) but that would get pretty involved |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 314 | */ |
| 315 | for (scgd = 0; scgd < 0x40; scgd++) { |
| 316 | scl = ick / (20 + (scgd * 8) + round); |
Wolfram Sang | df576be | 2020-03-26 11:07:21 +0100 | [diff] [blame] | 317 | if (scl <= t.bus_freq_hz) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 318 | goto scgd_find; |
| 319 | } |
| 320 | dev_err(dev, "it is impossible to calculate best SCL\n"); |
| 321 | return -EIO; |
| 322 | |
| 323 | scgd_find: |
| 324 | dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n", |
Wolfram Sang | df576be | 2020-03-26 11:07:21 +0100 | [diff] [blame] | 325 | scl, t.bus_freq_hz, rate, round, cdf, scgd); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 326 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 327 | /* keep icccr value */ |
Guennadi Liakhovetski | 14d32f1 | 2013-09-12 14:36:44 +0200 | [diff] [blame] | 328 | priv->icccr = scgd << cdf_width | cdf; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Sergei Shtylyov | 7c7117f | 2014-09-15 00:15:46 +0400 | [diff] [blame] | 333 | static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 334 | { |
Wolfram Sang | 386babf | 2014-05-28 09:44:41 +0200 | [diff] [blame] | 335 | int read = !!rcar_i2c_is_recv(priv); |
| 336 | |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 337 | priv->pos = 0; |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 338 | if (priv->msgs_left == 1) |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 339 | priv->flags |= ID_LAST_MSG; |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 340 | |
Peter Rosin | 30a6475 | 2018-05-16 09:16:47 +0200 | [diff] [blame] | 341 | rcar_i2c_write(priv, ICMAR, i2c_8bit_addr_from_msg(priv->msg)); |
Wolfram Sang | e7f4264 | 2021-09-15 15:48:27 +0200 | [diff] [blame] | 342 | if (!priv->atomic_xfer) |
| 343 | rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND); |
| 344 | |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 345 | /* |
Wolfram Sang | fe34fbf | 2018-01-21 15:45:11 +0100 | [diff] [blame] | 346 | * We don't have a test case but the HW engineers say that the write order |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 347 | * of ICMSR and ICMCR depends on whether we issue START or REP_START. Since |
| 348 | * it didn't cause a drawback for me, let's rather be safe than sorry. |
| 349 | */ |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 350 | if (priv->flags & ID_FIRST_MSG) { |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 351 | rcar_i2c_write(priv, ICMSR, 0); |
| 352 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START); |
| 353 | } else { |
Wolfram Sang | 19358d4 | 2018-08-08 09:59:28 +0200 | [diff] [blame] | 354 | if (priv->flags & ID_P_REP_AFTER_RD) |
| 355 | priv->flags &= ~ID_P_REP_AFTER_RD; |
| 356 | else |
| 357 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START); |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 358 | rcar_i2c_write(priv, ICMSR, 0); |
| 359 | } |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 362 | static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv) |
| 363 | { |
| 364 | priv->msg++; |
| 365 | priv->msgs_left--; |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 366 | priv->flags &= ID_P_MASK; |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 367 | rcar_i2c_prepare_msg(priv); |
| 368 | } |
| 369 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 370 | static void rcar_i2c_dma_unmap(struct rcar_i2c_priv *priv) |
| 371 | { |
| 372 | struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE |
| 373 | ? priv->dma_rx : priv->dma_tx; |
| 374 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 375 | dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg), |
Wolfram Sang | 9163350 | 2017-05-28 09:52:17 +0200 | [diff] [blame] | 376 | sg_dma_len(&priv->sg), priv->dma_direction); |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 377 | |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 378 | /* Gen3 can only do one RXDMA per transfer and we just completed it */ |
| 379 | if (priv->devtype == I2C_RCAR_GEN3 && |
| 380 | priv->dma_direction == DMA_FROM_DEVICE) |
| 381 | priv->flags |= ID_P_NO_RXDMA; |
| 382 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 383 | priv->dma_direction = DMA_NONE; |
Hiromitsu Yamasaki | a35ba2f | 2019-03-03 16:03:13 +0100 | [diff] [blame] | 384 | |
| 385 | /* Disable DMA Master Received/Transmitted, must be last! */ |
| 386 | rcar_i2c_write(priv, ICDMAER, 0); |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void rcar_i2c_cleanup_dma(struct rcar_i2c_priv *priv) |
| 390 | { |
| 391 | if (priv->dma_direction == DMA_NONE) |
| 392 | return; |
| 393 | else if (priv->dma_direction == DMA_FROM_DEVICE) |
| 394 | dmaengine_terminate_all(priv->dma_rx); |
| 395 | else if (priv->dma_direction == DMA_TO_DEVICE) |
| 396 | dmaengine_terminate_all(priv->dma_tx); |
| 397 | |
| 398 | rcar_i2c_dma_unmap(priv); |
| 399 | } |
| 400 | |
| 401 | static void rcar_i2c_dma_callback(void *data) |
| 402 | { |
| 403 | struct rcar_i2c_priv *priv = data; |
| 404 | |
| 405 | priv->pos += sg_dma_len(&priv->sg); |
| 406 | |
| 407 | rcar_i2c_dma_unmap(priv); |
| 408 | } |
| 409 | |
Wolfram Sang | 03f85e3 | 2019-03-05 18:54:33 +0100 | [diff] [blame] | 410 | static bool rcar_i2c_dma(struct rcar_i2c_priv *priv) |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 411 | { |
| 412 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
| 413 | struct i2c_msg *msg = priv->msg; |
| 414 | bool read = msg->flags & I2C_M_RD; |
| 415 | enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 416 | struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx; |
| 417 | struct dma_async_tx_descriptor *txdesc; |
| 418 | dma_addr_t dma_addr; |
| 419 | dma_cookie_t cookie; |
| 420 | unsigned char *buf; |
| 421 | int len; |
| 422 | |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 423 | /* Do various checks to see if DMA is feasible at all */ |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 424 | if (priv->atomic_xfer || IS_ERR(chan) || msg->len < RCAR_MIN_DMA_LEN || |
Wolfram Sang | ed5a810 | 2019-03-05 18:54:32 +0100 | [diff] [blame] | 425 | !(msg->flags & I2C_M_DMA_SAFE) || (read && priv->flags & ID_P_NO_RXDMA)) |
Wolfram Sang | 03f85e3 | 2019-03-05 18:54:33 +0100 | [diff] [blame] | 426 | return false; |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 427 | |
| 428 | if (read) { |
| 429 | /* |
| 430 | * The last two bytes needs to be fetched using PIO in |
| 431 | * order for the STOP phase to work. |
| 432 | */ |
| 433 | buf = priv->msg->buf; |
| 434 | len = priv->msg->len - 2; |
| 435 | } else { |
| 436 | /* |
| 437 | * First byte in message was sent using PIO. |
| 438 | */ |
| 439 | buf = priv->msg->buf + 1; |
| 440 | len = priv->msg->len - 1; |
| 441 | } |
| 442 | |
| 443 | dma_addr = dma_map_single(chan->device->dev, buf, len, dir); |
Wolfram Sang | c13c291 | 2016-08-24 11:19:29 +0200 | [diff] [blame] | 444 | if (dma_mapping_error(chan->device->dev, dma_addr)) { |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 445 | dev_dbg(dev, "dma map failed, using PIO\n"); |
Wolfram Sang | 03f85e3 | 2019-03-05 18:54:33 +0100 | [diff] [blame] | 446 | return false; |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | sg_dma_len(&priv->sg) = len; |
| 450 | sg_dma_address(&priv->sg) = dma_addr; |
| 451 | |
| 452 | priv->dma_direction = dir; |
| 453 | |
| 454 | txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1, |
| 455 | read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV, |
| 456 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 457 | if (!txdesc) { |
| 458 | dev_dbg(dev, "dma prep slave sg failed, using PIO\n"); |
| 459 | rcar_i2c_cleanup_dma(priv); |
Wolfram Sang | 03f85e3 | 2019-03-05 18:54:33 +0100 | [diff] [blame] | 460 | return false; |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | txdesc->callback = rcar_i2c_dma_callback; |
| 464 | txdesc->callback_param = priv; |
| 465 | |
| 466 | cookie = dmaengine_submit(txdesc); |
| 467 | if (dma_submit_error(cookie)) { |
| 468 | dev_dbg(dev, "submitting dma failed, using PIO\n"); |
| 469 | rcar_i2c_cleanup_dma(priv); |
Wolfram Sang | 03f85e3 | 2019-03-05 18:54:33 +0100 | [diff] [blame] | 470 | return false; |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 473 | /* Enable DMA Master Received/Transmitted */ |
| 474 | if (read) |
| 475 | rcar_i2c_write(priv, ICDMAER, RMDMAE); |
| 476 | else |
| 477 | rcar_i2c_write(priv, ICDMAER, TMDMAE); |
| 478 | |
| 479 | dma_async_issue_pending(chan); |
Wolfram Sang | 03f85e3 | 2019-03-05 18:54:33 +0100 | [diff] [blame] | 480 | return true; |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 483 | static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 484 | { |
| 485 | struct i2c_msg *msg = priv->msg; |
| 486 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 487 | /* FIXME: sometimes, unknown interrupt happened. Do nothing */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 488 | if (!(msr & MDE)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 489 | return; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 490 | |
Wolfram Sang | 94e290b | 2019-03-05 18:54:34 +0100 | [diff] [blame] | 491 | /* Check if DMA can be enabled and take over */ |
| 492 | if (priv->pos == 1 && rcar_i2c_dma(priv)) |
| 493 | return; |
| 494 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 495 | if (priv->pos < msg->len) { |
| 496 | /* |
| 497 | * Prepare next data to ICRXTX register. |
| 498 | * This data will go to _SHIFT_ register. |
| 499 | * |
| 500 | * * |
| 501 | * [ICRXTX] -> [SHIFT] -> [I2C bus] |
| 502 | */ |
| 503 | rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]); |
| 504 | priv->pos++; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 505 | } else { |
| 506 | /* |
| 507 | * The last data was pushed to ICRXTX on _PREV_ empty irq. |
| 508 | * It is on _SHIFT_ register, and will sent to I2C bus. |
| 509 | * |
| 510 | * * |
| 511 | * [ICRXTX] -> [SHIFT] -> [I2C bus] |
| 512 | */ |
| 513 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 514 | if (priv->flags & ID_LAST_MSG) { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 515 | /* |
| 516 | * If current msg is the _LAST_ msg, |
| 517 | * prepare stop condition here. |
| 518 | * ID_DONE will be set on STOP irq. |
| 519 | */ |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 520 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP); |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 521 | } else { |
| 522 | rcar_i2c_next_msg(priv); |
| 523 | return; |
| 524 | } |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 527 | rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 530 | static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 531 | { |
| 532 | struct i2c_msg *msg = priv->msg; |
| 533 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 534 | /* FIXME: sometimes, unknown interrupt happened. Do nothing */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 535 | if (!(msr & MDR)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 536 | return; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 537 | |
| 538 | if (msr & MAT) { |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 539 | /* |
| 540 | * Address transfer phase finished, but no data at this point. |
| 541 | * Try to use DMA to receive data. |
| 542 | */ |
| 543 | rcar_i2c_dma(priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 544 | } else if (priv->pos < msg->len) { |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 545 | /* get received data */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 546 | msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX); |
| 547 | priv->pos++; |
| 548 | } |
| 549 | |
Wolfram Sang | 19358d4 | 2018-08-08 09:59:28 +0200 | [diff] [blame] | 550 | /* If next received data is the _LAST_, go to new phase. */ |
| 551 | if (priv->pos + 1 == msg->len) { |
| 552 | if (priv->flags & ID_LAST_MSG) { |
| 553 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP); |
| 554 | } else { |
| 555 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START); |
| 556 | priv->flags |= ID_P_REP_AFTER_RD; |
| 557 | } |
| 558 | } |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 559 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 560 | if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG)) |
| 561 | rcar_i2c_next_msg(priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 562 | else |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 563 | rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 566 | static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv) |
| 567 | { |
| 568 | u32 ssr_raw, ssr_filtered; |
| 569 | u8 value; |
| 570 | |
| 571 | ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff; |
| 572 | ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER); |
| 573 | |
| 574 | if (!ssr_filtered) |
| 575 | return false; |
| 576 | |
| 577 | /* address detected */ |
| 578 | if (ssr_filtered & SAR) { |
| 579 | /* read or write request */ |
| 580 | if (ssr_raw & STM) { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 581 | i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 582 | rcar_i2c_write(priv, ICRXTX, value); |
| 583 | rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR); |
| 584 | } else { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 585 | i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 586 | rcar_i2c_read(priv, ICRXTX); /* dummy read */ |
| 587 | rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR); |
| 588 | } |
| 589 | |
Wolfram Sang | 314139f | 2020-06-29 17:38:07 +0200 | [diff] [blame] | 590 | /* Clear SSR, too, because of old STOPs to other clients than us */ |
| 591 | rcar_i2c_write(priv, ICSSR, ~(SAR | SSR) & 0xff); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* master sent stop */ |
| 595 | if (ssr_filtered & SSR) { |
| 596 | i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value); |
Wolfram Sang | 914a7b3 | 2020-08-17 14:19:30 +0200 | [diff] [blame] | 597 | rcar_i2c_write(priv, ICSCR, SIE | SDBS); /* clear our NACK */ |
Wolfram Sang | 314139f | 2020-06-29 17:38:07 +0200 | [diff] [blame] | 598 | rcar_i2c_write(priv, ICSIER, SAR); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 599 | rcar_i2c_write(priv, ICSSR, ~SSR & 0xff); |
| 600 | } |
| 601 | |
| 602 | /* master wants to write to us */ |
| 603 | if (ssr_filtered & SDR) { |
| 604 | int ret; |
| 605 | |
| 606 | value = rcar_i2c_read(priv, ICRXTX); |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 607 | ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 608 | /* Send NACK in case of error */ |
| 609 | rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0)); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 610 | rcar_i2c_write(priv, ICSSR, ~SDR & 0xff); |
| 611 | } |
| 612 | |
| 613 | /* master wants to read from us */ |
| 614 | if (ssr_filtered & SDE) { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 615 | i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 616 | rcar_i2c_write(priv, ICRXTX, value); |
| 617 | rcar_i2c_write(priv, ICSSR, ~SDE & 0xff); |
| 618 | } |
| 619 | |
| 620 | return true; |
| 621 | } |
| 622 | |
Wolfram Sang | 7ce98a5 | 2019-03-03 16:03:14 +0100 | [diff] [blame] | 623 | /* |
| 624 | * This driver has a lock-free design because there are IP cores (at least |
| 625 | * R-Car Gen2) which have an inherent race condition in their hardware design. |
Wolfram Sang | c7b514e | 2020-12-23 18:21:51 +0100 | [diff] [blame] | 626 | * There, we need to switch to RCAR_BUS_PHASE_DATA as soon as possible after |
Wolfram Sang | 7ce98a5 | 2019-03-03 16:03:14 +0100 | [diff] [blame] | 627 | * the interrupt was generated, otherwise an unwanted repeated message gets |
| 628 | * generated. It turned out that taking a spinlock at the beginning of the ISR |
| 629 | * was already causing repeated messages. Thus, this driver was converted to |
| 630 | * the now lockless behaviour. Please keep this in mind when hacking the driver. |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 631 | * R-Car Gen3 seems to have this fixed but earlier versions than R-Car Gen2 are |
| 632 | * likely affected. Therefore, we have different interrupt handler entries. |
Wolfram Sang | 7ce98a5 | 2019-03-03 16:03:14 +0100 | [diff] [blame] | 633 | */ |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 634 | static irqreturn_t rcar_i2c_irq(int irq, struct rcar_i2c_priv *priv, u32 msr) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 635 | { |
Sergei Shtylyov | aa5beaf | 2014-09-15 00:20:19 +0400 | [diff] [blame] | 636 | if (!msr) { |
Wolfram Sang | c3be0af1 | 2015-11-19 16:56:48 +0100 | [diff] [blame] | 637 | if (rcar_i2c_slave_irq(priv)) |
| 638 | return IRQ_HANDLED; |
| 639 | |
| 640 | return IRQ_NONE; |
Sergei Shtylyov | aa5beaf | 2014-09-15 00:20:19 +0400 | [diff] [blame] | 641 | } |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 642 | |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 643 | /* Arbitration lost */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 644 | if (msr & MAL) { |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 645 | priv->flags |= ID_DONE | ID_ARBLOST; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 646 | goto out; |
| 647 | } |
| 648 | |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 649 | /* Nack */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 650 | if (msr & MNR) { |
Wolfram Sang | d89667b | 2015-11-19 16:56:47 +0100 | [diff] [blame] | 651 | /* HW automatically sends STOP after received NACK */ |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 652 | if (!priv->atomic_xfer) |
| 653 | rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP); |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 654 | priv->flags |= ID_NACK; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 655 | goto out; |
| 656 | } |
| 657 | |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 658 | /* Stop */ |
| 659 | if (msr & MST) { |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 660 | priv->msgs_left--; /* The last message also made it */ |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 661 | priv->flags |= ID_DONE; |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 662 | goto out; |
| 663 | } |
| 664 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 665 | if (rcar_i2c_is_recv(priv)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 666 | rcar_i2c_irq_recv(priv, msr); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 667 | else |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 668 | rcar_i2c_irq_send(priv, msr); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 669 | |
| 670 | out: |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 671 | if (priv->flags & ID_DONE) { |
Wolfram Sang | f238224 | 2014-05-28 09:44:39 +0200 | [diff] [blame] | 672 | rcar_i2c_write(priv, ICMIER, 0); |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 673 | rcar_i2c_write(priv, ICMSR, 0); |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 674 | if (!priv->atomic_xfer) |
| 675 | wake_up(&priv->wait); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Wolfram Sang | c3be0af1 | 2015-11-19 16:56:48 +0100 | [diff] [blame] | 678 | return IRQ_HANDLED; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 681 | static irqreturn_t rcar_i2c_gen2_irq(int irq, void *ptr) |
| 682 | { |
| 683 | struct rcar_i2c_priv *priv = ptr; |
| 684 | u32 msr; |
| 685 | |
| 686 | /* Clear START or STOP immediately, except for REPSTART after read */ |
| 687 | if (likely(!(priv->flags & ID_P_REP_AFTER_RD))) |
| 688 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA); |
| 689 | |
| 690 | /* Only handle interrupts that are currently enabled */ |
| 691 | msr = rcar_i2c_read(priv, ICMSR); |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 692 | if (!priv->atomic_xfer) |
| 693 | msr &= rcar_i2c_read(priv, ICMIER); |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 694 | |
| 695 | return rcar_i2c_irq(irq, priv, msr); |
| 696 | } |
| 697 | |
| 698 | static irqreturn_t rcar_i2c_gen3_irq(int irq, void *ptr) |
| 699 | { |
| 700 | struct rcar_i2c_priv *priv = ptr; |
| 701 | u32 msr; |
| 702 | |
| 703 | /* Only handle interrupts that are currently enabled */ |
| 704 | msr = rcar_i2c_read(priv, ICMSR); |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 705 | if (!priv->atomic_xfer) |
| 706 | msr &= rcar_i2c_read(priv, ICMIER); |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 707 | |
| 708 | /* |
| 709 | * Clear START or STOP immediately, except for REPSTART after read or |
| 710 | * if a spurious interrupt was detected. |
| 711 | */ |
| 712 | if (likely(!(priv->flags & ID_P_REP_AFTER_RD) && msr)) |
| 713 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA); |
| 714 | |
| 715 | return rcar_i2c_irq(irq, priv, msr); |
| 716 | } |
| 717 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 718 | static struct dma_chan *rcar_i2c_request_dma_chan(struct device *dev, |
| 719 | enum dma_transfer_direction dir, |
| 720 | dma_addr_t port_addr) |
| 721 | { |
| 722 | struct dma_chan *chan; |
| 723 | struct dma_slave_config cfg; |
| 724 | char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx"; |
| 725 | int ret; |
| 726 | |
Niklas Söderlund | 6aabf9d | 2016-05-19 10:29:17 +0200 | [diff] [blame] | 727 | chan = dma_request_chan(dev, chan_name); |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 728 | if (IS_ERR(chan)) { |
Eugeniu Rosca | 8ae034c | 2017-08-15 22:36:23 +0200 | [diff] [blame] | 729 | dev_dbg(dev, "request_channel failed for %s (%ld)\n", |
| 730 | chan_name, PTR_ERR(chan)); |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 731 | return chan; |
| 732 | } |
| 733 | |
| 734 | memset(&cfg, 0, sizeof(cfg)); |
| 735 | cfg.direction = dir; |
| 736 | if (dir == DMA_MEM_TO_DEV) { |
| 737 | cfg.dst_addr = port_addr; |
| 738 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 739 | } else { |
| 740 | cfg.src_addr = port_addr; |
| 741 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 742 | } |
| 743 | |
| 744 | ret = dmaengine_slave_config(chan, &cfg); |
| 745 | if (ret) { |
| 746 | dev_dbg(dev, "slave_config failed for %s (%d)\n", |
| 747 | chan_name, ret); |
| 748 | dma_release_channel(chan); |
| 749 | return ERR_PTR(ret); |
| 750 | } |
| 751 | |
| 752 | dev_dbg(dev, "got DMA channel for %s\n", chan_name); |
| 753 | return chan; |
| 754 | } |
| 755 | |
| 756 | static void rcar_i2c_request_dma(struct rcar_i2c_priv *priv, |
| 757 | struct i2c_msg *msg) |
| 758 | { |
| 759 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
| 760 | bool read; |
| 761 | struct dma_chan *chan; |
| 762 | enum dma_transfer_direction dir; |
| 763 | |
| 764 | read = msg->flags & I2C_M_RD; |
| 765 | |
| 766 | chan = read ? priv->dma_rx : priv->dma_tx; |
| 767 | if (PTR_ERR(chan) != -EPROBE_DEFER) |
| 768 | return; |
| 769 | |
| 770 | dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; |
| 771 | chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX); |
| 772 | |
| 773 | if (read) |
| 774 | priv->dma_rx = chan; |
| 775 | else |
| 776 | priv->dma_tx = chan; |
| 777 | } |
| 778 | |
| 779 | static void rcar_i2c_release_dma(struct rcar_i2c_priv *priv) |
| 780 | { |
| 781 | if (!IS_ERR(priv->dma_tx)) { |
| 782 | dma_release_channel(priv->dma_tx); |
| 783 | priv->dma_tx = ERR_PTR(-EPROBE_DEFER); |
| 784 | } |
| 785 | |
| 786 | if (!IS_ERR(priv->dma_rx)) { |
| 787 | dma_release_channel(priv->dma_rx); |
| 788 | priv->dma_rx = ERR_PTR(-EPROBE_DEFER); |
| 789 | } |
| 790 | } |
| 791 | |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 792 | /* I2C is a special case, we need to poll the status of a reset */ |
| 793 | static int rcar_i2c_do_reset(struct rcar_i2c_priv *priv) |
| 794 | { |
Wolfram Sang | 74779f6 | 2020-08-29 22:38:10 +0200 | [diff] [blame] | 795 | int ret; |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 796 | |
| 797 | ret = reset_control_reset(priv->rstc); |
| 798 | if (ret) |
| 799 | return ret; |
| 800 | |
Wolfram Sang | 74779f6 | 2020-08-29 22:38:10 +0200 | [diff] [blame] | 801 | return read_poll_timeout_atomic(reset_control_status, ret, ret == 0, 1, |
| 802 | 100, false, priv->rstc); |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 805 | static int rcar_i2c_master_xfer(struct i2c_adapter *adap, |
| 806 | struct i2c_msg *msgs, |
| 807 | int num) |
| 808 | { |
| 809 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 810 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
Wolfram Sang | b6763d0 | 2015-06-20 21:03:20 +0200 | [diff] [blame] | 811 | int i, ret; |
Wolfram Sang | ff2316b | 2015-11-19 16:56:44 +0100 | [diff] [blame] | 812 | long time_left; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 813 | |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 814 | priv->atomic_xfer = false; |
| 815 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 816 | pm_runtime_get_sync(dev); |
| 817 | |
Wolfram Sang | 0b57436 | 2018-11-13 12:15:42 +0100 | [diff] [blame] | 818 | /* Check bus state before init otherwise bus busy info will be lost */ |
| 819 | ret = rcar_i2c_bus_barrier(priv); |
| 820 | if (ret < 0) |
| 821 | goto out; |
| 822 | |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 823 | /* Gen3 needs a reset before allowing RXDMA once */ |
| 824 | if (priv->devtype == I2C_RCAR_GEN3) { |
| 825 | priv->flags |= ID_P_NO_RXDMA; |
| 826 | if (!IS_ERR(priv->rstc)) { |
| 827 | ret = rcar_i2c_do_reset(priv); |
| 828 | if (ret == 0) |
| 829 | priv->flags &= ~ID_P_NO_RXDMA; |
| 830 | } |
| 831 | } |
| 832 | |
Wolfram Sang | ae481cc | 2017-04-18 20:38:35 +0200 | [diff] [blame] | 833 | rcar_i2c_init(priv); |
| 834 | |
Wolfram Sang | 3ef3e5c | 2018-07-23 22:26:14 +0200 | [diff] [blame] | 835 | for (i = 0; i < num; i++) |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 836 | rcar_i2c_request_dma(priv, msgs + i); |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 837 | |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 838 | /* init first message */ |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 839 | priv->msg = msgs; |
| 840 | priv->msgs_left = num; |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 841 | priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG; |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 842 | rcar_i2c_prepare_msg(priv); |
Sergei Shtylyov | 91bfe29 | 2014-08-24 00:44:09 +0400 | [diff] [blame] | 843 | |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 844 | time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE, |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 845 | num * adap->timeout); |
Wolfram Sang | 31d8603 | 2018-10-19 21:15:26 +0200 | [diff] [blame] | 846 | |
| 847 | /* cleanup DMA if it couldn't complete properly due to an error */ |
| 848 | if (priv->dma_direction != DMA_NONE) |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 849 | rcar_i2c_cleanup_dma(priv); |
Wolfram Sang | 31d8603 | 2018-10-19 21:15:26 +0200 | [diff] [blame] | 850 | |
| 851 | if (!time_left) { |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 852 | rcar_i2c_init(priv); |
| 853 | ret = -ETIMEDOUT; |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 854 | } else if (priv->flags & ID_NACK) { |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 855 | ret = -ENXIO; |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 856 | } else if (priv->flags & ID_ARBLOST) { |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 857 | ret = -EAGAIN; |
| 858 | } else { |
| 859 | ret = num - priv->msgs_left; /* The number of transfer */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 860 | } |
Wolfram Sang | 3f7de22 | 2014-05-28 09:44:40 +0200 | [diff] [blame] | 861 | out: |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 862 | pm_runtime_put(dev); |
| 863 | |
Ben Dooks | 6ff4b105 | 2014-01-26 16:05:37 +0000 | [diff] [blame] | 864 | if (ret < 0 && ret != -ENXIO) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 865 | dev_err(dev, "error %d : %x\n", ret, priv->flags); |
| 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 870 | static int rcar_i2c_master_xfer_atomic(struct i2c_adapter *adap, |
| 871 | struct i2c_msg *msgs, |
| 872 | int num) |
| 873 | { |
| 874 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 875 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
| 876 | unsigned long j; |
| 877 | bool time_left; |
| 878 | int ret; |
| 879 | |
| 880 | priv->atomic_xfer = true; |
| 881 | |
| 882 | pm_runtime_get_sync(dev); |
| 883 | |
| 884 | /* Check bus state before init otherwise bus busy info will be lost */ |
| 885 | ret = rcar_i2c_bus_barrier(priv); |
| 886 | if (ret < 0) |
| 887 | goto out; |
| 888 | |
| 889 | rcar_i2c_init(priv); |
| 890 | |
| 891 | /* init first message */ |
| 892 | priv->msg = msgs; |
| 893 | priv->msgs_left = num; |
| 894 | priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG; |
| 895 | rcar_i2c_prepare_msg(priv); |
| 896 | |
| 897 | j = jiffies + num * adap->timeout; |
| 898 | do { |
| 899 | u32 msr = rcar_i2c_read(priv, ICMSR); |
| 900 | |
| 901 | msr &= (rcar_i2c_is_recv(priv) ? RCAR_IRQ_RECV : RCAR_IRQ_SEND) | RCAR_IRQ_STOP; |
| 902 | |
| 903 | if (msr) { |
| 904 | if (priv->devtype < I2C_RCAR_GEN3) |
| 905 | rcar_i2c_gen2_irq(0, priv); |
| 906 | else |
| 907 | rcar_i2c_gen3_irq(0, priv); |
| 908 | } |
| 909 | |
| 910 | time_left = time_before_eq(jiffies, j); |
| 911 | } while (!(priv->flags & ID_DONE) && time_left); |
| 912 | |
| 913 | if (!time_left) { |
| 914 | rcar_i2c_init(priv); |
| 915 | ret = -ETIMEDOUT; |
| 916 | } else if (priv->flags & ID_NACK) { |
| 917 | ret = -ENXIO; |
| 918 | } else if (priv->flags & ID_ARBLOST) { |
| 919 | ret = -EAGAIN; |
| 920 | } else { |
| 921 | ret = num - priv->msgs_left; /* The number of transfer */ |
| 922 | } |
| 923 | out: |
| 924 | pm_runtime_put(dev); |
| 925 | |
| 926 | if (ret < 0 && ret != -ENXIO) |
| 927 | dev_err(dev, "error %d : %x\n", ret, priv->flags); |
| 928 | |
| 929 | return ret; |
| 930 | } |
| 931 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 932 | static int rcar_reg_slave(struct i2c_client *slave) |
| 933 | { |
| 934 | struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); |
| 935 | |
| 936 | if (priv->slave) |
| 937 | return -EBUSY; |
| 938 | |
| 939 | if (slave->flags & I2C_CLIENT_TEN) |
| 940 | return -EAFNOSUPPORT; |
| 941 | |
Wolfram Sang | 63a761e | 2017-04-20 12:04:33 +0200 | [diff] [blame] | 942 | /* Keep device active for slave address detection logic */ |
Wolfram Sang | b4cd08a | 2015-12-16 20:05:18 +0100 | [diff] [blame] | 943 | pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv)); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 944 | |
| 945 | priv->slave = slave; |
| 946 | rcar_i2c_write(priv, ICSAR, slave->addr); |
| 947 | rcar_i2c_write(priv, ICSSR, 0); |
Wolfram Sang | 314139f | 2020-06-29 17:38:07 +0200 | [diff] [blame] | 948 | rcar_i2c_write(priv, ICSIER, SAR); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 949 | rcar_i2c_write(priv, ICSCR, SIE | SDBS); |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | static int rcar_unreg_slave(struct i2c_client *slave) |
| 955 | { |
| 956 | struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); |
| 957 | |
| 958 | WARN_ON(!priv->slave); |
| 959 | |
Wolfram Sang | c7c9e91 | 2020-07-26 18:16:06 +0200 | [diff] [blame] | 960 | /* ensure no irq is running before clearing ptr */ |
| 961 | disable_irq(priv->irq); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 962 | rcar_i2c_write(priv, ICSIER, 0); |
Wolfram Sang | c7c9e91 | 2020-07-26 18:16:06 +0200 | [diff] [blame] | 963 | rcar_i2c_write(priv, ICSSR, 0); |
| 964 | enable_irq(priv->irq); |
| 965 | rcar_i2c_write(priv, ICSCR, SDBS); |
Wolfram Sang | eb015971 | 2020-07-04 15:38:29 +0200 | [diff] [blame] | 966 | rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */ |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 967 | |
| 968 | priv->slave = NULL; |
| 969 | |
Wolfram Sang | b4cd08a | 2015-12-16 20:05:18 +0100 | [diff] [blame] | 970 | pm_runtime_put(rcar_i2c_priv_to_dev(priv)); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 975 | static u32 rcar_i2c_func(struct i2c_adapter *adap) |
| 976 | { |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 977 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 978 | |
Wolfram Sang | b395ba2 | 2017-06-19 23:41:46 +0200 | [diff] [blame] | 979 | /* |
| 980 | * This HW can't do: |
| 981 | * I2C_SMBUS_QUICK (setting FSB during START didn't work) |
| 982 | * I2C_M_NOSTART (automatically sends address after START) |
| 983 | * I2C_M_IGNORE_NAK (automatically sends STOP after NAK) |
| 984 | */ |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 985 | u32 func = I2C_FUNC_I2C | I2C_FUNC_SLAVE | |
| 986 | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); |
| 987 | |
| 988 | if (priv->flags & ID_P_HOST_NOTIFY) |
| 989 | func |= I2C_FUNC_SMBUS_HOST_NOTIFY; |
| 990 | |
| 991 | return func; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static const struct i2c_algorithm rcar_i2c_algo = { |
| 995 | .master_xfer = rcar_i2c_master_xfer, |
Ulrich Hecht | 82531df | 2021-02-12 17:45:41 +0100 | [diff] [blame] | 996 | .master_xfer_atomic = rcar_i2c_master_xfer_atomic, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 997 | .functionality = rcar_i2c_func, |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 998 | .reg_slave = rcar_reg_slave, |
| 999 | .unreg_slave = rcar_unreg_slave, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1000 | }; |
| 1001 | |
Wolfram Sang | 3ef3e5c | 2018-07-23 22:26:14 +0200 | [diff] [blame] | 1002 | static const struct i2c_adapter_quirks rcar_i2c_quirks = { |
| 1003 | .flags = I2C_AQ_NO_ZERO_LEN, |
| 1004 | }; |
| 1005 | |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 1006 | static const struct of_device_id rcar_i2c_dt_ids[] = { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 1007 | { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 }, |
| 1008 | { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 }, |
| 1009 | { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | e893645 | 2014-02-20 09:03:20 +0100 | [diff] [blame] | 1010 | { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | 819a395 | 2014-05-27 14:06:28 +0200 | [diff] [blame] | 1011 | { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 }, |
| 1012 | { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 }, |
| 1013 | { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 1014 | { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 }, |
Ulrich Hecht | c13f743 | 2016-09-14 18:46:06 +0200 | [diff] [blame] | 1015 | { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 }, |
Simon Horman | ad4a8dc | 2016-12-06 17:01:28 +0100 | [diff] [blame] | 1016 | { .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 }, |
| 1017 | { .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 }, |
| 1018 | { .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 }, |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 1019 | {}, |
| 1020 | }; |
| 1021 | MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids); |
| 1022 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 1023 | static int rcar_i2c_probe(struct platform_device *pdev) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1024 | { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1025 | struct rcar_i2c_priv *priv; |
| 1026 | struct i2c_adapter *adap; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1027 | struct device *dev = &pdev->dev; |
Wolfram Sang | 24c6d4b | 2020-12-23 18:21:53 +0100 | [diff] [blame] | 1028 | unsigned long irqflags = 0; |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 1029 | irqreturn_t (*irqhandler)(int irq, void *ptr) = rcar_i2c_gen3_irq; |
Wolfram Sang | 7b814d8 | 2019-08-08 21:39:10 +0200 | [diff] [blame] | 1030 | int ret; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1031 | |
Wolfram Sang | ed5a810 | 2019-03-05 18:54:32 +0100 | [diff] [blame] | 1032 | /* Otherwise logic will break because some bytes must always use PIO */ |
| 1033 | BUILD_BUG_ON_MSG(RCAR_MIN_DMA_LEN < 3, "Invalid min DMA length"); |
| 1034 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1035 | priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL); |
Jingoo Han | 46797a2 | 2014-05-13 10:51:58 +0900 | [diff] [blame] | 1036 | if (!priv) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1037 | return -ENOMEM; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1038 | |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 1039 | priv->clk = devm_clk_get(dev, NULL); |
| 1040 | if (IS_ERR(priv->clk)) { |
| 1041 | dev_err(dev, "cannot get clock\n"); |
| 1042 | return PTR_ERR(priv->clk); |
| 1043 | } |
| 1044 | |
Dejin Zheng | c02fb2b | 2020-04-14 21:48:27 +0800 | [diff] [blame] | 1045 | priv->io = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res); |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 1046 | if (IS_ERR(priv->io)) |
| 1047 | return PTR_ERR(priv->io); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1048 | |
Wolfram Sang | 69e558f | 2016-03-01 17:36:43 +0100 | [diff] [blame] | 1049 | priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1050 | init_waitqueue_head(&priv->wait); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1051 | |
Wolfram Sang | 929e3aba | 2014-07-10 13:46:31 +0200 | [diff] [blame] | 1052 | adap = &priv->adap; |
| 1053 | adap->nr = pdev->id; |
| 1054 | adap->algo = &rcar_i2c_algo; |
| 1055 | adap->class = I2C_CLASS_DEPRECATED; |
| 1056 | adap->retries = 3; |
| 1057 | adap->dev.parent = dev; |
| 1058 | adap->dev.of_node = dev->of_node; |
Wolfram Sang | 7d2c17f | 2018-01-09 14:58:59 +0100 | [diff] [blame] | 1059 | adap->bus_recovery_info = &rcar_i2c_bri; |
Wolfram Sang | 3ef3e5c | 2018-07-23 22:26:14 +0200 | [diff] [blame] | 1060 | adap->quirks = &rcar_i2c_quirks; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1061 | i2c_set_adapdata(adap, priv); |
| 1062 | strlcpy(adap->name, pdev->name, sizeof(adap->name)); |
| 1063 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 1064 | /* Init DMA */ |
| 1065 | sg_init_table(&priv->sg, 1); |
| 1066 | priv->dma_direction = DMA_NONE; |
| 1067 | priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER); |
| 1068 | |
Wolfram Sang | 63a761e | 2017-04-20 12:04:33 +0200 | [diff] [blame] | 1069 | /* Activate device for clock calculation */ |
Wolfram Sang | 4f7effd | 2015-10-09 10:39:25 +0100 | [diff] [blame] | 1070 | pm_runtime_enable(dev); |
Wolfram Sang | f9c9d31 | 2015-12-08 10:37:47 +0100 | [diff] [blame] | 1071 | pm_runtime_get_sync(dev); |
Andy Shevchenko | 38a592e | 2020-03-24 14:32:13 +0200 | [diff] [blame] | 1072 | ret = rcar_i2c_clock_calculate(priv); |
Wolfram Sang | f9c9d31 | 2015-12-08 10:37:47 +0100 | [diff] [blame] | 1073 | if (ret < 0) |
| 1074 | goto out_pm_put; |
| 1075 | |
Wolfram Sang | eb015971 | 2020-07-04 15:38:29 +0200 | [diff] [blame] | 1076 | rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */ |
| 1077 | |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 1078 | if (priv->devtype < I2C_RCAR_GEN3) { |
Wolfram Sang | 24c6d4b | 2020-12-23 18:21:53 +0100 | [diff] [blame] | 1079 | irqflags |= IRQF_NO_THREAD; |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 1080 | irqhandler = rcar_i2c_gen2_irq; |
| 1081 | } |
Wolfram Sang | 24c6d4b | 2020-12-23 18:21:53 +0100 | [diff] [blame] | 1082 | |
Wolfram Sang | 3b77001 | 2018-06-28 22:45:38 +0200 | [diff] [blame] | 1083 | if (priv->devtype == I2C_RCAR_GEN3) { |
| 1084 | priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); |
| 1085 | if (!IS_ERR(priv->rstc)) { |
| 1086 | ret = reset_control_status(priv->rstc); |
| 1087 | if (ret < 0) |
| 1088 | priv->rstc = ERR_PTR(-ENOTSUPP); |
| 1089 | } |
| 1090 | } |
| 1091 | |
Wolfram Sang | 63a761e | 2017-04-20 12:04:33 +0200 | [diff] [blame] | 1092 | /* Stay always active when multi-master to keep arbitration working */ |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 1093 | if (of_property_read_bool(dev->of_node, "multi-master")) |
| 1094 | priv->flags |= ID_P_PM_BLOCKED; |
| 1095 | else |
| 1096 | pm_runtime_put(dev); |
| 1097 | |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 1098 | if (of_property_read_bool(dev->of_node, "smbus")) |
| 1099 | priv->flags |= ID_P_HOST_NOTIFY; |
Wolfram Sang | f9c9d31 | 2015-12-08 10:37:47 +0100 | [diff] [blame] | 1100 | |
Sergey Shtylyov | 147178c | 2021-04-10 23:23:33 +0300 | [diff] [blame] | 1101 | ret = platform_get_irq(pdev, 0); |
| 1102 | if (ret < 0) |
| 1103 | goto out_pm_disable; |
| 1104 | priv->irq = ret; |
Wolfram Sang | 9c975c4 | 2020-12-23 18:21:54 +0100 | [diff] [blame] | 1105 | ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1106 | if (ret < 0) { |
Wolfram Sang | 7b814d8 | 2019-08-08 21:39:10 +0200 | [diff] [blame] | 1107 | dev_err(dev, "cannot get irq %d\n", priv->irq); |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 1108 | goto out_pm_disable; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Wolfram Sang | 4f7effd | 2015-10-09 10:39:25 +0100 | [diff] [blame] | 1111 | platform_set_drvdata(pdev, priv); |
| 1112 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1113 | ret = i2c_add_numbered_adapter(adap); |
Wolfram Sang | ea73440 | 2016-08-09 13:36:17 +0200 | [diff] [blame] | 1114 | if (ret < 0) |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 1115 | goto out_pm_disable; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1116 | |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 1117 | if (priv->flags & ID_P_HOST_NOTIFY) { |
| 1118 | priv->host_notify_client = i2c_new_slave_host_notify_device(adap); |
| 1119 | if (IS_ERR(priv->host_notify_client)) { |
| 1120 | ret = PTR_ERR(priv->host_notify_client); |
| 1121 | goto out_del_device; |
| 1122 | } |
| 1123 | } |
| 1124 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1125 | dev_info(dev, "probed\n"); |
| 1126 | |
| 1127 | return 0; |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 1128 | |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 1129 | out_del_device: |
| 1130 | i2c_del_adapter(&priv->adap); |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 1131 | out_pm_put: |
| 1132 | pm_runtime_put(dev); |
| 1133 | out_pm_disable: |
| 1134 | pm_runtime_disable(dev); |
| 1135 | return ret; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 1138 | static int rcar_i2c_remove(struct platform_device *pdev) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1139 | { |
| 1140 | struct rcar_i2c_priv *priv = platform_get_drvdata(pdev); |
| 1141 | struct device *dev = &pdev->dev; |
| 1142 | |
Wolfram Sang | c4651f1 | 2020-09-10 11:11:18 +0200 | [diff] [blame] | 1143 | if (priv->host_notify_client) |
| 1144 | i2c_free_slave_host_notify_device(priv->host_notify_client); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1145 | i2c_del_adapter(&priv->adap); |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 1146 | rcar_i2c_release_dma(priv); |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 1147 | if (priv->flags & ID_P_PM_BLOCKED) |
| 1148 | pm_runtime_put(dev); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1149 | pm_runtime_disable(dev); |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
Wolfram Sang | 18569fa | 2018-12-19 17:48:26 +0100 | [diff] [blame] | 1154 | #ifdef CONFIG_PM_SLEEP |
| 1155 | static int rcar_i2c_suspend(struct device *dev) |
| 1156 | { |
| 1157 | struct rcar_i2c_priv *priv = dev_get_drvdata(dev); |
| 1158 | |
| 1159 | i2c_mark_adapter_suspended(&priv->adap); |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | static int rcar_i2c_resume(struct device *dev) |
| 1164 | { |
| 1165 | struct rcar_i2c_priv *priv = dev_get_drvdata(dev); |
| 1166 | |
| 1167 | i2c_mark_adapter_resumed(&priv->adap); |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
Geert Uytterhoeven | 81d696c | 2019-01-22 11:03:57 +0100 | [diff] [blame] | 1171 | static const struct dev_pm_ops rcar_i2c_pm_ops = { |
| 1172 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rcar_i2c_suspend, rcar_i2c_resume) |
| 1173 | }; |
Wolfram Sang | 18569fa | 2018-12-19 17:48:26 +0100 | [diff] [blame] | 1174 | |
| 1175 | #define DEV_PM_OPS (&rcar_i2c_pm_ops) |
| 1176 | #else |
| 1177 | #define DEV_PM_OPS NULL |
| 1178 | #endif /* CONFIG_PM_SLEEP */ |
| 1179 | |
Wolfram Sang | 45fd5e4 | 2012-11-13 11:24:15 +0100 | [diff] [blame] | 1180 | static struct platform_driver rcar_i2c_driver = { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1181 | .driver = { |
| 1182 | .name = "i2c-rcar", |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 1183 | .of_match_table = rcar_i2c_dt_ids, |
Wolfram Sang | 18569fa | 2018-12-19 17:48:26 +0100 | [diff] [blame] | 1184 | .pm = DEV_PM_OPS, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1185 | }, |
| 1186 | .probe = rcar_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 1187 | .remove = rcar_i2c_remove, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1188 | }; |
| 1189 | |
Wolfram Sang | 45fd5e4 | 2012-11-13 11:24:15 +0100 | [diff] [blame] | 1190 | module_platform_driver(rcar_i2c_driver); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1191 | |
Wolfram Sang | 3d99bea | 2014-05-28 09:44:46 +0200 | [diff] [blame] | 1192 | MODULE_LICENSE("GPL v2"); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1193 | MODULE_DESCRIPTION("Renesas R-Car I2C bus driver"); |
| 1194 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |