blob: f71c730f9838d89ca1150ea25d6db7df754c4b2d [file] [log] [blame]
Wolfram Sang6055af52018-08-22 00:02:16 +02001// SPDX-License-Identifier: GPL-2.0
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07002/*
Wolfram Sang5c8e3ab2017-05-28 11:30:45 +02003 * Driver for the Renesas R-Car I2C unit
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07004 *
Wolfram Sangd0051ca2019-01-21 18:07:59 +01005 * Copyright (C) 2014-19 Wolfram Sang <wsa@sang-engineering.com>
6 * Copyright (C) 2011-2019 Renesas Electronics Corporation
Wolfram Sang3d99bea2014-05-28 09:44:46 +02007 *
8 * Copyright (C) 2012-14 Renesas Solutions Corp.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07009 * 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 Morimoto6ccbe602012-09-27 23:44:25 -070013 */
Wolfram Sangb07531a2018-08-08 09:59:27 +020014#include <linux/bitops.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070015#include <linux/clk.h>
16#include <linux/delay.h>
Niklas Söderlund73e8b052016-05-14 14:17:08 +020017#include <linux/dmaengine.h>
18#include <linux/dma-mapping.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070019#include <linux/err.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070020#include <linux/interrupt.h>
21#include <linux/io.h>
Wolfram Sang9374ed12020-08-29 22:38:09 +020022#include <linux/iopoll.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070023#include <linux/i2c.h>
Wolfram Sangc4651f12020-09-10 11:11:18 +020024#include <linux/i2c-smbus.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070025#include <linux/kernel.h>
26#include <linux/module.h>
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +020027#include <linux/of_device.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070028#include <linux/platform_device.h>
29#include <linux/pm_runtime.h>
Wolfram Sang3b770012018-06-28 22:45:38 +020030#include <linux/reset.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070031#include <linux/slab.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070032
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 Sang18769442019-02-05 14:37:25 +010044#define ICFBSCR 0x38 /* first bit setup cycle (Gen3) */
45#define ICDMAER 0x3c /* DMA enable (Gen3) */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070046
Wolfram Sangde20d182014-11-18 17:04:55 +010047/* 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 Morimoto6ccbe602012-09-27 23:44:25 -070053/* 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 Sangfe34fbf2018-01-21 15:45:11 +010061#define ESG (1 << 0) /* enable start bit gen */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070062
Wolfram Sangde20d182014-11-18 17:04:55 +010063/* 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 Sang3e3aaba2014-05-28 09:44:44 +020072/* ICMSR (also for ICMIE) */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070073#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öderlund73e8b052016-05-14 14:17:08 +020081/* 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öderlund73e8b052016-05-14 14:17:08 +020088#define TCYC17 0x0f /* 17*Tcyc delay 1st bit between SDA and SCL */
89
Wolfram Sanged5a8102019-03-05 18:54:32 +010090#define RCAR_MIN_DMA_LEN 8
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070091
Wolfram Sang4f443a82014-05-28 09:44:38 +020092#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 Morimoto6ccbe602012-09-27 23:44:25 -070095
Wolfram Sang3e3aaba2014-05-28 09:44:44 +020096#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 Morimoto6ccbe602012-09-27 23:44:25 -070099
Hiromitsu Yamasakia1de3252018-03-20 22:04:14 +0100100#define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0x7F)
101#define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0x7F)
Wolfram Sang3c95de62014-05-28 09:44:42 +0200102
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700103#define ID_LAST_MSG (1 << 0)
Wolfram Sange49865d2015-11-19 16:56:51 +0100104#define ID_FIRST_MSG (1 << 1)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700105#define ID_DONE (1 << 2)
106#define ID_ARBLOST (1 << 3)
107#define ID_NACK (1 << 4)
Wolfram Sang7ee24eb2015-12-23 17:56:34 +0100108/* persistent flags */
Wolfram Sangc4651f12020-09-10 11:11:18 +0200109#define ID_P_HOST_NOTIFY BIT(28)
Wolfram Sang19358d42018-08-08 09:59:28 +0200110#define ID_P_REP_AFTER_RD BIT(29)
Wolfram Sangb07531a2018-08-08 09:59:27 +0200111#define ID_P_NO_RXDMA BIT(30) /* HW forbids RXDMA sometimes */
112#define ID_P_PM_BLOCKED BIT(31)
Wolfram Sangc4651f12020-09-10 11:11:18 +0200113#define ID_P_MASK GENMASK(31, 28)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700114
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900115enum rcar_i2c_type {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700116 I2C_RCAR_GEN1,
117 I2C_RCAR_GEN2,
Wolfram Sange7db0d32015-08-05 15:18:25 +0200118 I2C_RCAR_GEN3,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900119};
120
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700121struct rcar_i2c_priv {
Wolfram Sang25c2e0f2020-12-23 18:21:52 +0100122 u32 flags;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700123 void __iomem *io;
124 struct i2c_adapter adap;
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100125 struct i2c_msg *msg;
126 int msgs_left;
Ben Dooksbc8120f2014-01-26 16:05:35 +0000127 struct clk *clk;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700128
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700129 wait_queue_head_t wait;
130
131 int pos;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700132 u32 icccr;
Wolfram Sang7d2c17f2018-01-09 14:58:59 +0100133 u8 recovery_icmcr; /* protected by adapter lock */
Wolfram Sang51371cd2014-05-28 09:44:45 +0200134 enum rcar_i2c_type devtype;
Wolfram Sangde20d182014-11-18 17:04:55 +0100135 struct i2c_client *slave;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200136
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 Sang3b770012018-06-28 22:45:38 +0200142
143 struct reset_control *rstc;
Ulrich Hecht82531df2021-02-12 17:45:41 +0100144 bool atomic_xfer;
Wolfram Sang7b814d82019-08-08 21:39:10 +0200145 int irq;
Wolfram Sangc4651f12020-09-10 11:11:18 +0200146
147 struct i2c_client *host_notify_client;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700148};
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 Morimoto6ccbe602012-09-27 23:44:25 -0700153static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
154{
155 writel(val, priv->io + reg);
156}
157
158static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
159{
160 return readl(priv->io + reg);
161}
162
Wolfram Sang7d2c17f2018-01-09 14:58:59 +0100163static 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
171static 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 Sang7d2c17f2018-01-09 14:58:59 +0100183static 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 Sang4fe10de2018-07-11 00:24:23 +0200195static 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 Sang7d2c17f2018-01-09 14:58:59 +0100203static 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 Sang4fe10de2018-07-11 00:24:23 +0200207 .get_bus_free = rcar_i2c_get_bus_free,
Wolfram Sang7d2c17f2018-01-09 14:58:59 +0100208 .recover_bus = i2c_generic_scl_recovery,
209};
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700210static void rcar_i2c_init(struct rcar_i2c_priv *priv)
211{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700212 /* reset master mode */
213 rcar_i2c_write(priv, ICMIER, 0);
Wolfram Sang2c78cdc2015-11-19 16:56:42 +0100214 rcar_i2c_write(priv, ICMCR, MDBS);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700215 rcar_i2c_write(priv, ICMSR, 0);
Wolfram Sang2c78cdc2015-11-19 16:56:42 +0100216 /* start clock */
217 rcar_i2c_write(priv, ICCCR, priv->icccr);
Wolfram Sang18769442019-02-05 14:37:25 +0100218
219 if (priv->devtype == I2C_RCAR_GEN3)
220 rcar_i2c_write(priv, ICFBSCR, TCYC17);
221
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700222}
223
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700224static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
225{
Wolfram Sang9374ed12020-08-29 22:38:09 +0200226 int ret;
227 u32 val;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700228
Wolfram Sang9374ed12020-08-29 22:38:09 +0200229 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 Morimoto6ccbe602012-09-27 23:44:25 -0700235 }
236
Wolfram Sang9374ed12020-08-29 22:38:09 +0200237 return ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700238}
239
Andy Shevchenko38a592e2020-03-24 14:32:13 +0200240static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700241{
Wolfram Sangca68ead2015-12-08 10:37:49 +0100242 u32 scgd, cdf, round, ick, sum, scl, cdf_width;
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200243 unsigned long rate;
Wolfram Sangf9c9d312015-12-08 10:37:47 +0100244 struct device *dev = rcar_i2c_priv_to_dev(priv);
Wolfram Sangdf576be2020-03-26 11:07:21 +0100245 struct i2c_timings t = {
Andy Shevchenko38a592e2020-03-24 14:32:13 +0200246 .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 Sangdf576be2020-03-26 11:07:21 +0100250 };
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700251
Wolfram Sangc7881872015-12-08 10:37:48 +0100252 /* Fall back to previously used values if not supplied */
Wolfram Sangdf576be2020-03-26 11:07:21 +0100253 i2c_parse_fw_timings(dev, &t, false);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700254
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900255 switch (priv->devtype) {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700256 case I2C_RCAR_GEN1:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900257 cdf_width = 2;
258 break;
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700259 case I2C_RCAR_GEN2:
Wolfram Sange7db0d32015-08-05 15:18:25 +0200260 case I2C_RCAR_GEN3:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900261 cdf_width = 3;
262 break;
263 default:
264 dev_err(dev, "device type error\n");
265 return -EIO;
266 }
267
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700268 /*
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 Sangca68ead2015-12-08 10:37:49 +0100277 * ticf : I2C SCL falling time
278 * tr : I2C SCL rising time
279 * intd : LSI internal delay
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700280 * clkp : peripheral_clk
281 * F[] : integer up-valuation
282 */
Ben Dooksbc8120f2014-01-26 16:05:35 +0000283 rate = clk_get_rate(priv->clk);
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200284 cdf = rate / 20000000;
Wolfram Sang22762cc2014-09-20 12:07:37 +0200285 if (cdf >= 1U << cdf_width) {
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200286 dev_err(dev, "Input clock %lu too high\n", rate);
287 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700288 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200289 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700290
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700291 /*
292 * it is impossible to calculate large scale
293 * number on u32. separate it
294 *
Wolfram Sangca68ead2015-12-08 10:37:49 +0100295 * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
296 * = F[sum * ick / 1000000000]
297 * = F[(ick / 1000000) * sum / 1000]
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700298 */
Wolfram Sangdf576be2020-03-26 11:07:21 +0100299 sum = t.scl_fall_ns + t.scl_rise_ns + t.scl_int_delay_ns;
Wolfram Sangca68ead2015-12-08 10:37:49 +0100300 round = (ick + 500000) / 1000000 * sum;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700301 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 Liakhovetski8d049402013-09-12 14:36:45 +0200308 *
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 Morimoto6ccbe602012-09-27 23:44:25 -0700314 */
315 for (scgd = 0; scgd < 0x40; scgd++) {
316 scl = ick / (20 + (scgd * 8) + round);
Wolfram Sangdf576be2020-03-26 11:07:21 +0100317 if (scl <= t.bus_freq_hz)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700318 goto scgd_find;
319 }
320 dev_err(dev, "it is impossible to calculate best SCL\n");
321 return -EIO;
322
323scgd_find:
324 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
Wolfram Sangdf576be2020-03-26 11:07:21 +0100325 scl, t.bus_freq_hz, rate, round, cdf, scgd);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700326
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100327 /* keep icccr value */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200328 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700329
330 return 0;
331}
332
Sergei Shtylyov7c7117f2014-09-15 00:15:46 +0400333static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700334{
Wolfram Sang386babf2014-05-28 09:44:41 +0200335 int read = !!rcar_i2c_is_recv(priv);
336
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100337 priv->pos = 0;
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100338 if (priv->msgs_left == 1)
Wolfram Sang42c07832015-12-23 17:56:33 +0100339 priv->flags |= ID_LAST_MSG;
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100340
Peter Rosin30a64752018-05-16 09:16:47 +0200341 rcar_i2c_write(priv, ICMAR, i2c_8bit_addr_from_msg(priv->msg));
Wolfram Sange7f42642021-09-15 15:48:27 +0200342 if (!priv->atomic_xfer)
343 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
344
Wolfram Sange49865d2015-11-19 16:56:51 +0100345 /*
Wolfram Sangfe34fbf2018-01-21 15:45:11 +0100346 * We don't have a test case but the HW engineers say that the write order
Wolfram Sange49865d2015-11-19 16:56:51 +0100347 * 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 Sang42c07832015-12-23 17:56:33 +0100350 if (priv->flags & ID_FIRST_MSG) {
Wolfram Sange49865d2015-11-19 16:56:51 +0100351 rcar_i2c_write(priv, ICMSR, 0);
352 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
353 } else {
Wolfram Sang19358d42018-08-08 09:59:28 +0200354 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 Sange49865d2015-11-19 16:56:51 +0100358 rcar_i2c_write(priv, ICMSR, 0);
359 }
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700360}
361
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100362static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
363{
364 priv->msg++;
365 priv->msgs_left--;
Wolfram Sang7ee24eb2015-12-23 17:56:34 +0100366 priv->flags &= ID_P_MASK;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100367 rcar_i2c_prepare_msg(priv);
368}
369
Wolfram Sanga5f7cf92021-12-08 09:45:42 +0100370static void rcar_i2c_cleanup_dma(struct rcar_i2c_priv *priv, bool terminate)
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200371{
372 struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE
373 ? priv->dma_rx : priv->dma_tx;
374
Wolfram Sanga5f7cf92021-12-08 09:45:42 +0100375 /* only allowed from thread context! */
376 if (terminate)
377 dmaengine_terminate_sync(chan);
378
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200379 dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg),
Wolfram Sang91633502017-05-28 09:52:17 +0200380 sg_dma_len(&priv->sg), priv->dma_direction);
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200381
Wolfram Sang3b770012018-06-28 22:45:38 +0200382 /* Gen3 can only do one RXDMA per transfer and we just completed it */
383 if (priv->devtype == I2C_RCAR_GEN3 &&
384 priv->dma_direction == DMA_FROM_DEVICE)
385 priv->flags |= ID_P_NO_RXDMA;
386
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200387 priv->dma_direction = DMA_NONE;
Hiromitsu Yamasakia35ba2f2019-03-03 16:03:13 +0100388
389 /* Disable DMA Master Received/Transmitted, must be last! */
390 rcar_i2c_write(priv, ICDMAER, 0);
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200391}
392
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200393static void rcar_i2c_dma_callback(void *data)
394{
395 struct rcar_i2c_priv *priv = data;
396
397 priv->pos += sg_dma_len(&priv->sg);
398
Wolfram Sanga5f7cf92021-12-08 09:45:42 +0100399 rcar_i2c_cleanup_dma(priv, false);
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200400}
401
Wolfram Sang03f85e32019-03-05 18:54:33 +0100402static bool rcar_i2c_dma(struct rcar_i2c_priv *priv)
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200403{
404 struct device *dev = rcar_i2c_priv_to_dev(priv);
405 struct i2c_msg *msg = priv->msg;
406 bool read = msg->flags & I2C_M_RD;
407 enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
408 struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx;
409 struct dma_async_tx_descriptor *txdesc;
410 dma_addr_t dma_addr;
411 dma_cookie_t cookie;
412 unsigned char *buf;
413 int len;
414
Wolfram Sang3b770012018-06-28 22:45:38 +0200415 /* Do various checks to see if DMA is feasible at all */
Ulrich Hecht82531df2021-02-12 17:45:41 +0100416 if (priv->atomic_xfer || IS_ERR(chan) || msg->len < RCAR_MIN_DMA_LEN ||
Wolfram Sanged5a8102019-03-05 18:54:32 +0100417 !(msg->flags & I2C_M_DMA_SAFE) || (read && priv->flags & ID_P_NO_RXDMA))
Wolfram Sang03f85e32019-03-05 18:54:33 +0100418 return false;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200419
420 if (read) {
421 /*
422 * The last two bytes needs to be fetched using PIO in
423 * order for the STOP phase to work.
424 */
425 buf = priv->msg->buf;
426 len = priv->msg->len - 2;
427 } else {
428 /*
429 * First byte in message was sent using PIO.
430 */
431 buf = priv->msg->buf + 1;
432 len = priv->msg->len - 1;
433 }
434
435 dma_addr = dma_map_single(chan->device->dev, buf, len, dir);
Wolfram Sangc13c2912016-08-24 11:19:29 +0200436 if (dma_mapping_error(chan->device->dev, dma_addr)) {
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200437 dev_dbg(dev, "dma map failed, using PIO\n");
Wolfram Sang03f85e32019-03-05 18:54:33 +0100438 return false;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200439 }
440
441 sg_dma_len(&priv->sg) = len;
442 sg_dma_address(&priv->sg) = dma_addr;
443
444 priv->dma_direction = dir;
445
446 txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1,
447 read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
448 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
449 if (!txdesc) {
450 dev_dbg(dev, "dma prep slave sg failed, using PIO\n");
Wolfram Sanga5f7cf92021-12-08 09:45:42 +0100451 rcar_i2c_cleanup_dma(priv, false);
Wolfram Sang03f85e32019-03-05 18:54:33 +0100452 return false;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200453 }
454
455 txdesc->callback = rcar_i2c_dma_callback;
456 txdesc->callback_param = priv;
457
458 cookie = dmaengine_submit(txdesc);
459 if (dma_submit_error(cookie)) {
460 dev_dbg(dev, "submitting dma failed, using PIO\n");
Wolfram Sanga5f7cf92021-12-08 09:45:42 +0100461 rcar_i2c_cleanup_dma(priv, false);
Wolfram Sang03f85e32019-03-05 18:54:33 +0100462 return false;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200463 }
464
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200465 /* Enable DMA Master Received/Transmitted */
466 if (read)
467 rcar_i2c_write(priv, ICDMAER, RMDMAE);
468 else
469 rcar_i2c_write(priv, ICDMAER, TMDMAE);
470
471 dma_async_issue_pending(chan);
Wolfram Sang03f85e32019-03-05 18:54:33 +0100472 return true;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200473}
474
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100475static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700476{
477 struct i2c_msg *msg = priv->msg;
478
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100479 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700480 if (!(msr & MDE))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100481 return;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700482
Wolfram Sang94e290b2019-03-05 18:54:34 +0100483 /* Check if DMA can be enabled and take over */
484 if (priv->pos == 1 && rcar_i2c_dma(priv))
485 return;
486
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700487 if (priv->pos < msg->len) {
488 /*
489 * Prepare next data to ICRXTX register.
490 * This data will go to _SHIFT_ register.
491 *
492 * *
493 * [ICRXTX] -> [SHIFT] -> [I2C bus]
494 */
495 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
496 priv->pos++;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700497 } else {
498 /*
499 * The last data was pushed to ICRXTX on _PREV_ empty irq.
500 * It is on _SHIFT_ register, and will sent to I2C bus.
501 *
502 * *
503 * [ICRXTX] -> [SHIFT] -> [I2C bus]
504 */
505
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100506 if (priv->flags & ID_LAST_MSG) {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700507 /*
508 * If current msg is the _LAST_ msg,
509 * prepare stop condition here.
510 * ID_DONE will be set on STOP irq.
511 */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200512 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100513 } else {
514 rcar_i2c_next_msg(priv);
515 return;
516 }
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700517 }
518
Wolfram Sang3c95de62014-05-28 09:44:42 +0200519 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700520}
521
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100522static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700523{
524 struct i2c_msg *msg = priv->msg;
525
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100526 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700527 if (!(msr & MDR))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100528 return;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700529
530 if (msr & MAT) {
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200531 /*
532 * Address transfer phase finished, but no data at this point.
533 * Try to use DMA to receive data.
534 */
535 rcar_i2c_dma(priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700536 } else if (priv->pos < msg->len) {
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100537 /* get received data */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700538 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
539 priv->pos++;
540 }
541
Wolfram Sang19358d42018-08-08 09:59:28 +0200542 /* If next received data is the _LAST_, go to new phase. */
543 if (priv->pos + 1 == msg->len) {
544 if (priv->flags & ID_LAST_MSG) {
545 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
546 } else {
547 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
548 priv->flags |= ID_P_REP_AFTER_RD;
549 }
550 }
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700551
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100552 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
553 rcar_i2c_next_msg(priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700554 else
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100555 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700556}
557
Wolfram Sangde20d182014-11-18 17:04:55 +0100558static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
559{
560 u32 ssr_raw, ssr_filtered;
561 u8 value;
562
563 ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
564 ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
565
566 if (!ssr_filtered)
567 return false;
568
569 /* address detected */
570 if (ssr_filtered & SAR) {
571 /* read or write request */
572 if (ssr_raw & STM) {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100573 i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100574 rcar_i2c_write(priv, ICRXTX, value);
575 rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
576 } else {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100577 i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100578 rcar_i2c_read(priv, ICRXTX); /* dummy read */
579 rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
580 }
581
Wolfram Sang314139f2020-06-29 17:38:07 +0200582 /* Clear SSR, too, because of old STOPs to other clients than us */
583 rcar_i2c_write(priv, ICSSR, ~(SAR | SSR) & 0xff);
Wolfram Sangde20d182014-11-18 17:04:55 +0100584 }
585
586 /* master sent stop */
587 if (ssr_filtered & SSR) {
588 i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
Wolfram Sang914a7b32020-08-17 14:19:30 +0200589 rcar_i2c_write(priv, ICSCR, SIE | SDBS); /* clear our NACK */
Wolfram Sang314139f2020-06-29 17:38:07 +0200590 rcar_i2c_write(priv, ICSIER, SAR);
Wolfram Sangde20d182014-11-18 17:04:55 +0100591 rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
592 }
593
594 /* master wants to write to us */
595 if (ssr_filtered & SDR) {
596 int ret;
597
598 value = rcar_i2c_read(priv, ICRXTX);
Wolfram Sang5b77d162015-03-23 09:26:36 +0100599 ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100600 /* Send NACK in case of error */
601 rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
Wolfram Sangde20d182014-11-18 17:04:55 +0100602 rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
603 }
604
605 /* master wants to read from us */
606 if (ssr_filtered & SDE) {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100607 i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100608 rcar_i2c_write(priv, ICRXTX, value);
609 rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
610 }
611
612 return true;
613}
614
Wolfram Sang7ce98a52019-03-03 16:03:14 +0100615/*
616 * This driver has a lock-free design because there are IP cores (at least
617 * R-Car Gen2) which have an inherent race condition in their hardware design.
Wolfram Sangc7b514e2020-12-23 18:21:51 +0100618 * There, we need to switch to RCAR_BUS_PHASE_DATA as soon as possible after
Wolfram Sang7ce98a52019-03-03 16:03:14 +0100619 * the interrupt was generated, otherwise an unwanted repeated message gets
620 * generated. It turned out that taking a spinlock at the beginning of the ISR
621 * was already causing repeated messages. Thus, this driver was converted to
622 * the now lockless behaviour. Please keep this in mind when hacking the driver.
Wolfram Sang9c975c42020-12-23 18:21:54 +0100623 * R-Car Gen3 seems to have this fixed but earlier versions than R-Car Gen2 are
624 * likely affected. Therefore, we have different interrupt handler entries.
Wolfram Sang7ce98a52019-03-03 16:03:14 +0100625 */
Wolfram Sang9c975c42020-12-23 18:21:54 +0100626static irqreturn_t rcar_i2c_irq(int irq, struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700627{
Sergei Shtylyovaa5beaf2014-09-15 00:20:19 +0400628 if (!msr) {
Wolfram Sangc3be0af12015-11-19 16:56:48 +0100629 if (rcar_i2c_slave_irq(priv))
630 return IRQ_HANDLED;
631
632 return IRQ_NONE;
Sergei Shtylyovaa5beaf2014-09-15 00:20:19 +0400633 }
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400634
Wolfram Sang51371cd2014-05-28 09:44:45 +0200635 /* Arbitration lost */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700636 if (msr & MAL) {
Wolfram Sang42c07832015-12-23 17:56:33 +0100637 priv->flags |= ID_DONE | ID_ARBLOST;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700638 goto out;
639 }
640
Wolfram Sang51371cd2014-05-28 09:44:45 +0200641 /* Nack */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700642 if (msr & MNR) {
Wolfram Sangd89667b2015-11-19 16:56:47 +0100643 /* HW automatically sends STOP after received NACK */
Ulrich Hecht82531df2021-02-12 17:45:41 +0100644 if (!priv->atomic_xfer)
645 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
Wolfram Sang42c07832015-12-23 17:56:33 +0100646 priv->flags |= ID_NACK;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700647 goto out;
648 }
649
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400650 /* Stop */
651 if (msr & MST) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100652 priv->msgs_left--; /* The last message also made it */
Wolfram Sang42c07832015-12-23 17:56:33 +0100653 priv->flags |= ID_DONE;
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400654 goto out;
655 }
656
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700657 if (rcar_i2c_is_recv(priv))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100658 rcar_i2c_irq_recv(priv, msr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700659 else
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100660 rcar_i2c_irq_send(priv, msr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700661
662out:
Wolfram Sang42c07832015-12-23 17:56:33 +0100663 if (priv->flags & ID_DONE) {
Wolfram Sangf2382242014-05-28 09:44:39 +0200664 rcar_i2c_write(priv, ICMIER, 0);
Wolfram Sang3c95de62014-05-28 09:44:42 +0200665 rcar_i2c_write(priv, ICMSR, 0);
Ulrich Hecht82531df2021-02-12 17:45:41 +0100666 if (!priv->atomic_xfer)
667 wake_up(&priv->wait);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700668 }
669
Wolfram Sangc3be0af12015-11-19 16:56:48 +0100670 return IRQ_HANDLED;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700671}
672
Wolfram Sang9c975c42020-12-23 18:21:54 +0100673static irqreturn_t rcar_i2c_gen2_irq(int irq, void *ptr)
674{
675 struct rcar_i2c_priv *priv = ptr;
676 u32 msr;
677
678 /* Clear START or STOP immediately, except for REPSTART after read */
679 if (likely(!(priv->flags & ID_P_REP_AFTER_RD)))
680 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
681
682 /* Only handle interrupts that are currently enabled */
683 msr = rcar_i2c_read(priv, ICMSR);
Ulrich Hecht82531df2021-02-12 17:45:41 +0100684 if (!priv->atomic_xfer)
685 msr &= rcar_i2c_read(priv, ICMIER);
Wolfram Sang9c975c42020-12-23 18:21:54 +0100686
687 return rcar_i2c_irq(irq, priv, msr);
688}
689
690static irqreturn_t rcar_i2c_gen3_irq(int irq, void *ptr)
691{
692 struct rcar_i2c_priv *priv = ptr;
693 u32 msr;
694
695 /* Only handle interrupts that are currently enabled */
696 msr = rcar_i2c_read(priv, ICMSR);
Ulrich Hecht82531df2021-02-12 17:45:41 +0100697 if (!priv->atomic_xfer)
698 msr &= rcar_i2c_read(priv, ICMIER);
Wolfram Sang9c975c42020-12-23 18:21:54 +0100699
700 /*
701 * Clear START or STOP immediately, except for REPSTART after read or
702 * if a spurious interrupt was detected.
703 */
704 if (likely(!(priv->flags & ID_P_REP_AFTER_RD) && msr))
705 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
706
707 return rcar_i2c_irq(irq, priv, msr);
708}
709
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200710static struct dma_chan *rcar_i2c_request_dma_chan(struct device *dev,
711 enum dma_transfer_direction dir,
712 dma_addr_t port_addr)
713{
714 struct dma_chan *chan;
715 struct dma_slave_config cfg;
716 char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx";
717 int ret;
718
Niklas Söderlund6aabf9d2016-05-19 10:29:17 +0200719 chan = dma_request_chan(dev, chan_name);
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200720 if (IS_ERR(chan)) {
Eugeniu Rosca8ae034c2017-08-15 22:36:23 +0200721 dev_dbg(dev, "request_channel failed for %s (%ld)\n",
722 chan_name, PTR_ERR(chan));
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200723 return chan;
724 }
725
726 memset(&cfg, 0, sizeof(cfg));
727 cfg.direction = dir;
728 if (dir == DMA_MEM_TO_DEV) {
729 cfg.dst_addr = port_addr;
730 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
731 } else {
732 cfg.src_addr = port_addr;
733 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
734 }
735
736 ret = dmaengine_slave_config(chan, &cfg);
737 if (ret) {
738 dev_dbg(dev, "slave_config failed for %s (%d)\n",
739 chan_name, ret);
740 dma_release_channel(chan);
741 return ERR_PTR(ret);
742 }
743
744 dev_dbg(dev, "got DMA channel for %s\n", chan_name);
745 return chan;
746}
747
748static void rcar_i2c_request_dma(struct rcar_i2c_priv *priv,
749 struct i2c_msg *msg)
750{
751 struct device *dev = rcar_i2c_priv_to_dev(priv);
752 bool read;
753 struct dma_chan *chan;
754 enum dma_transfer_direction dir;
755
756 read = msg->flags & I2C_M_RD;
757
758 chan = read ? priv->dma_rx : priv->dma_tx;
759 if (PTR_ERR(chan) != -EPROBE_DEFER)
760 return;
761
762 dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
763 chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX);
764
765 if (read)
766 priv->dma_rx = chan;
767 else
768 priv->dma_tx = chan;
769}
770
771static void rcar_i2c_release_dma(struct rcar_i2c_priv *priv)
772{
773 if (!IS_ERR(priv->dma_tx)) {
774 dma_release_channel(priv->dma_tx);
775 priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
776 }
777
778 if (!IS_ERR(priv->dma_rx)) {
779 dma_release_channel(priv->dma_rx);
780 priv->dma_rx = ERR_PTR(-EPROBE_DEFER);
781 }
782}
783
Wolfram Sang3b770012018-06-28 22:45:38 +0200784/* I2C is a special case, we need to poll the status of a reset */
785static int rcar_i2c_do_reset(struct rcar_i2c_priv *priv)
786{
Wolfram Sang74779f62020-08-29 22:38:10 +0200787 int ret;
Wolfram Sang3b770012018-06-28 22:45:38 +0200788
789 ret = reset_control_reset(priv->rstc);
790 if (ret)
791 return ret;
792
Wolfram Sang74779f62020-08-29 22:38:10 +0200793 return read_poll_timeout_atomic(reset_control_status, ret, ret == 0, 1,
794 100, false, priv->rstc);
Wolfram Sang3b770012018-06-28 22:45:38 +0200795}
796
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700797static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
798 struct i2c_msg *msgs,
799 int num)
800{
801 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
802 struct device *dev = rcar_i2c_priv_to_dev(priv);
Wolfram Sangb6763d02015-06-20 21:03:20 +0200803 int i, ret;
Wolfram Sangff2316b2015-11-19 16:56:44 +0100804 long time_left;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700805
Ulrich Hecht82531df2021-02-12 17:45:41 +0100806 priv->atomic_xfer = false;
807
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700808 pm_runtime_get_sync(dev);
809
Wolfram Sang0b574362018-11-13 12:15:42 +0100810 /* Check bus state before init otherwise bus busy info will be lost */
811 ret = rcar_i2c_bus_barrier(priv);
812 if (ret < 0)
813 goto out;
814
Wolfram Sang3b770012018-06-28 22:45:38 +0200815 /* Gen3 needs a reset before allowing RXDMA once */
816 if (priv->devtype == I2C_RCAR_GEN3) {
817 priv->flags |= ID_P_NO_RXDMA;
818 if (!IS_ERR(priv->rstc)) {
819 ret = rcar_i2c_do_reset(priv);
820 if (ret == 0)
821 priv->flags &= ~ID_P_NO_RXDMA;
822 }
823 }
824
Wolfram Sangae481cc2017-04-18 20:38:35 +0200825 rcar_i2c_init(priv);
826
Wolfram Sang3ef3e5c2018-07-23 22:26:14 +0200827 for (i = 0; i < num; i++)
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200828 rcar_i2c_request_dma(priv, msgs + i);
Wolfram Sangd7653962014-05-05 18:36:21 +0200829
Wolfram Sange49865d2015-11-19 16:56:51 +0100830 /* init first message */
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100831 priv->msg = msgs;
832 priv->msgs_left = num;
Wolfram Sang7ee24eb2015-12-23 17:56:34 +0100833 priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100834 rcar_i2c_prepare_msg(priv);
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400835
Wolfram Sang42c07832015-12-23 17:56:33 +0100836 time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE,
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100837 num * adap->timeout);
Wolfram Sang31d86032018-10-19 21:15:26 +0200838
839 /* cleanup DMA if it couldn't complete properly due to an error */
840 if (priv->dma_direction != DMA_NONE)
Wolfram Sanga5f7cf92021-12-08 09:45:42 +0100841 rcar_i2c_cleanup_dma(priv, true);
Wolfram Sang31d86032018-10-19 21:15:26 +0200842
843 if (!time_left) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100844 rcar_i2c_init(priv);
845 ret = -ETIMEDOUT;
Wolfram Sang42c07832015-12-23 17:56:33 +0100846 } else if (priv->flags & ID_NACK) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100847 ret = -ENXIO;
Wolfram Sang42c07832015-12-23 17:56:33 +0100848 } else if (priv->flags & ID_ARBLOST) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100849 ret = -EAGAIN;
850 } else {
851 ret = num - priv->msgs_left; /* The number of transfer */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700852 }
Wolfram Sang3f7de222014-05-28 09:44:40 +0200853out:
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700854 pm_runtime_put(dev);
855
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000856 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700857 dev_err(dev, "error %d : %x\n", ret, priv->flags);
858
859 return ret;
860}
861
Ulrich Hecht82531df2021-02-12 17:45:41 +0100862static int rcar_i2c_master_xfer_atomic(struct i2c_adapter *adap,
863 struct i2c_msg *msgs,
864 int num)
865{
866 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
867 struct device *dev = rcar_i2c_priv_to_dev(priv);
868 unsigned long j;
869 bool time_left;
870 int ret;
871
872 priv->atomic_xfer = true;
873
874 pm_runtime_get_sync(dev);
875
876 /* Check bus state before init otherwise bus busy info will be lost */
877 ret = rcar_i2c_bus_barrier(priv);
878 if (ret < 0)
879 goto out;
880
881 rcar_i2c_init(priv);
882
883 /* init first message */
884 priv->msg = msgs;
885 priv->msgs_left = num;
886 priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG;
887 rcar_i2c_prepare_msg(priv);
888
889 j = jiffies + num * adap->timeout;
890 do {
891 u32 msr = rcar_i2c_read(priv, ICMSR);
892
893 msr &= (rcar_i2c_is_recv(priv) ? RCAR_IRQ_RECV : RCAR_IRQ_SEND) | RCAR_IRQ_STOP;
894
895 if (msr) {
896 if (priv->devtype < I2C_RCAR_GEN3)
897 rcar_i2c_gen2_irq(0, priv);
898 else
899 rcar_i2c_gen3_irq(0, priv);
900 }
901
902 time_left = time_before_eq(jiffies, j);
903 } while (!(priv->flags & ID_DONE) && time_left);
904
905 if (!time_left) {
906 rcar_i2c_init(priv);
907 ret = -ETIMEDOUT;
908 } else if (priv->flags & ID_NACK) {
909 ret = -ENXIO;
910 } else if (priv->flags & ID_ARBLOST) {
911 ret = -EAGAIN;
912 } else {
913 ret = num - priv->msgs_left; /* The number of transfer */
914 }
915out:
916 pm_runtime_put(dev);
917
918 if (ret < 0 && ret != -ENXIO)
919 dev_err(dev, "error %d : %x\n", ret, priv->flags);
920
921 return ret;
922}
923
Wolfram Sangde20d182014-11-18 17:04:55 +0100924static int rcar_reg_slave(struct i2c_client *slave)
925{
926 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
927
928 if (priv->slave)
929 return -EBUSY;
930
931 if (slave->flags & I2C_CLIENT_TEN)
932 return -EAFNOSUPPORT;
933
Wolfram Sang63a761e2017-04-20 12:04:33 +0200934 /* Keep device active for slave address detection logic */
Wolfram Sangb4cd08a2015-12-16 20:05:18 +0100935 pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
Wolfram Sangde20d182014-11-18 17:04:55 +0100936
937 priv->slave = slave;
938 rcar_i2c_write(priv, ICSAR, slave->addr);
939 rcar_i2c_write(priv, ICSSR, 0);
Wolfram Sang314139f2020-06-29 17:38:07 +0200940 rcar_i2c_write(priv, ICSIER, SAR);
Wolfram Sangde20d182014-11-18 17:04:55 +0100941 rcar_i2c_write(priv, ICSCR, SIE | SDBS);
942
943 return 0;
944}
945
946static int rcar_unreg_slave(struct i2c_client *slave)
947{
948 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
949
950 WARN_ON(!priv->slave);
951
Wolfram Sangc7c9e912020-07-26 18:16:06 +0200952 /* ensure no irq is running before clearing ptr */
953 disable_irq(priv->irq);
Wolfram Sangde20d182014-11-18 17:04:55 +0100954 rcar_i2c_write(priv, ICSIER, 0);
Wolfram Sangc7c9e912020-07-26 18:16:06 +0200955 rcar_i2c_write(priv, ICSSR, 0);
956 enable_irq(priv->irq);
957 rcar_i2c_write(priv, ICSCR, SDBS);
Wolfram Sangeb0159712020-07-04 15:38:29 +0200958 rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
Wolfram Sangde20d182014-11-18 17:04:55 +0100959
960 priv->slave = NULL;
961
Wolfram Sangb4cd08a2015-12-16 20:05:18 +0100962 pm_runtime_put(rcar_i2c_priv_to_dev(priv));
Wolfram Sangde20d182014-11-18 17:04:55 +0100963
964 return 0;
965}
966
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700967static u32 rcar_i2c_func(struct i2c_adapter *adap)
968{
Wolfram Sangc4651f12020-09-10 11:11:18 +0200969 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
970
Wolfram Sangb395ba22017-06-19 23:41:46 +0200971 /*
972 * This HW can't do:
973 * I2C_SMBUS_QUICK (setting FSB during START didn't work)
974 * I2C_M_NOSTART (automatically sends address after START)
975 * I2C_M_IGNORE_NAK (automatically sends STOP after NAK)
976 */
Wolfram Sangc4651f12020-09-10 11:11:18 +0200977 u32 func = I2C_FUNC_I2C | I2C_FUNC_SLAVE |
978 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
979
980 if (priv->flags & ID_P_HOST_NOTIFY)
981 func |= I2C_FUNC_SMBUS_HOST_NOTIFY;
982
983 return func;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700984}
985
986static const struct i2c_algorithm rcar_i2c_algo = {
987 .master_xfer = rcar_i2c_master_xfer,
Ulrich Hecht82531df2021-02-12 17:45:41 +0100988 .master_xfer_atomic = rcar_i2c_master_xfer_atomic,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700989 .functionality = rcar_i2c_func,
Wolfram Sangde20d182014-11-18 17:04:55 +0100990 .reg_slave = rcar_reg_slave,
991 .unreg_slave = rcar_unreg_slave,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700992};
993
Wolfram Sang3ef3e5c2018-07-23 22:26:14 +0200994static const struct i2c_adapter_quirks rcar_i2c_quirks = {
995 .flags = I2C_AQ_NO_ZERO_LEN,
996};
997
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200998static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700999 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
1000 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
1001 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange8936452014-02-20 09:03:20 +01001002 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +02001003 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
1004 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
1005 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange7db0d32015-08-05 15:18:25 +02001006 { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
Ulrich Hechtc13f7432016-09-14 18:46:06 +02001007 { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 },
Simon Hormanad4a8dc2016-12-06 17:01:28 +01001008 { .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 },
1009 { .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 },
1010 { .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 },
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +02001011 {},
1012};
1013MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
1014
Bill Pemberton0b255e92012-11-27 15:59:38 -05001015static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001016{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001017 struct rcar_i2c_priv *priv;
1018 struct i2c_adapter *adap;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001019 struct device *dev = &pdev->dev;
Wolfram Sang24c6d4b2020-12-23 18:21:53 +01001020 unsigned long irqflags = 0;
Wolfram Sang9c975c42020-12-23 18:21:54 +01001021 irqreturn_t (*irqhandler)(int irq, void *ptr) = rcar_i2c_gen3_irq;
Wolfram Sang7b814d82019-08-08 21:39:10 +02001022 int ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001023
Wolfram Sanged5a8102019-03-05 18:54:32 +01001024 /* Otherwise logic will break because some bytes must always use PIO */
1025 BUILD_BUG_ON_MSG(RCAR_MIN_DMA_LEN < 3, "Invalid min DMA length");
1026
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001027 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +09001028 if (!priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001029 return -ENOMEM;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001030
Ben Dooksbc8120f2014-01-26 16:05:35 +00001031 priv->clk = devm_clk_get(dev, NULL);
1032 if (IS_ERR(priv->clk)) {
1033 dev_err(dev, "cannot get clock\n");
1034 return PTR_ERR(priv->clk);
1035 }
1036
Dejin Zhengc02fb2b2020-04-14 21:48:27 +08001037 priv->io = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res);
Thierry Reding84dbf802013-01-21 11:09:03 +01001038 if (IS_ERR(priv->io))
1039 return PTR_ERR(priv->io);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001040
Wolfram Sang69e558f2016-03-01 17:36:43 +01001041 priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001042 init_waitqueue_head(&priv->wait);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001043
Wolfram Sang929e3aba2014-07-10 13:46:31 +02001044 adap = &priv->adap;
1045 adap->nr = pdev->id;
1046 adap->algo = &rcar_i2c_algo;
1047 adap->class = I2C_CLASS_DEPRECATED;
1048 adap->retries = 3;
1049 adap->dev.parent = dev;
1050 adap->dev.of_node = dev->of_node;
Wolfram Sang7d2c17f2018-01-09 14:58:59 +01001051 adap->bus_recovery_info = &rcar_i2c_bri;
Wolfram Sang3ef3e5c2018-07-23 22:26:14 +02001052 adap->quirks = &rcar_i2c_quirks;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001053 i2c_set_adapdata(adap, priv);
1054 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1055
Niklas Söderlund73e8b052016-05-14 14:17:08 +02001056 /* Init DMA */
1057 sg_init_table(&priv->sg, 1);
1058 priv->dma_direction = DMA_NONE;
1059 priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
1060
Wolfram Sang63a761e2017-04-20 12:04:33 +02001061 /* Activate device for clock calculation */
Wolfram Sang4f7effd2015-10-09 10:39:25 +01001062 pm_runtime_enable(dev);
Wolfram Sangf9c9d312015-12-08 10:37:47 +01001063 pm_runtime_get_sync(dev);
Andy Shevchenko38a592e2020-03-24 14:32:13 +02001064 ret = rcar_i2c_clock_calculate(priv);
Wolfram Sangf9c9d312015-12-08 10:37:47 +01001065 if (ret < 0)
1066 goto out_pm_put;
1067
Wolfram Sangeb0159712020-07-04 15:38:29 +02001068 rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
1069
Wolfram Sang9c975c42020-12-23 18:21:54 +01001070 if (priv->devtype < I2C_RCAR_GEN3) {
Wolfram Sang24c6d4b2020-12-23 18:21:53 +01001071 irqflags |= IRQF_NO_THREAD;
Wolfram Sang9c975c42020-12-23 18:21:54 +01001072 irqhandler = rcar_i2c_gen2_irq;
1073 }
Wolfram Sang24c6d4b2020-12-23 18:21:53 +01001074
Wolfram Sang3b770012018-06-28 22:45:38 +02001075 if (priv->devtype == I2C_RCAR_GEN3) {
1076 priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
1077 if (!IS_ERR(priv->rstc)) {
1078 ret = reset_control_status(priv->rstc);
1079 if (ret < 0)
1080 priv->rstc = ERR_PTR(-ENOTSUPP);
1081 }
1082 }
1083
Wolfram Sang63a761e2017-04-20 12:04:33 +02001084 /* Stay always active when multi-master to keep arbitration working */
Wolfram Sang7ee24eb2015-12-23 17:56:34 +01001085 if (of_property_read_bool(dev->of_node, "multi-master"))
1086 priv->flags |= ID_P_PM_BLOCKED;
1087 else
1088 pm_runtime_put(dev);
1089
Wolfram Sangc4651f12020-09-10 11:11:18 +02001090 if (of_property_read_bool(dev->of_node, "smbus"))
1091 priv->flags |= ID_P_HOST_NOTIFY;
Wolfram Sangf9c9d312015-12-08 10:37:47 +01001092
Sergey Shtylyov147178c2021-04-10 23:23:33 +03001093 ret = platform_get_irq(pdev, 0);
1094 if (ret < 0)
1095 goto out_pm_disable;
1096 priv->irq = ret;
Wolfram Sang9c975c42020-12-23 18:21:54 +01001097 ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001098 if (ret < 0) {
Wolfram Sang7b814d82019-08-08 21:39:10 +02001099 dev_err(dev, "cannot get irq %d\n", priv->irq);
Wolfram Sange43e0df2015-11-19 16:56:41 +01001100 goto out_pm_disable;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001101 }
1102
Wolfram Sang4f7effd2015-10-09 10:39:25 +01001103 platform_set_drvdata(pdev, priv);
1104
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001105 ret = i2c_add_numbered_adapter(adap);
Wolfram Sangea734402016-08-09 13:36:17 +02001106 if (ret < 0)
Wolfram Sange43e0df2015-11-19 16:56:41 +01001107 goto out_pm_disable;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001108
Wolfram Sangc4651f12020-09-10 11:11:18 +02001109 if (priv->flags & ID_P_HOST_NOTIFY) {
1110 priv->host_notify_client = i2c_new_slave_host_notify_device(adap);
1111 if (IS_ERR(priv->host_notify_client)) {
1112 ret = PTR_ERR(priv->host_notify_client);
1113 goto out_del_device;
1114 }
1115 }
1116
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001117 dev_info(dev, "probed\n");
1118
1119 return 0;
Wolfram Sange43e0df2015-11-19 16:56:41 +01001120
Wolfram Sangc4651f12020-09-10 11:11:18 +02001121 out_del_device:
1122 i2c_del_adapter(&priv->adap);
Wolfram Sange43e0df2015-11-19 16:56:41 +01001123 out_pm_put:
1124 pm_runtime_put(dev);
1125 out_pm_disable:
1126 pm_runtime_disable(dev);
1127 return ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001128}
1129
Bill Pemberton0b255e92012-11-27 15:59:38 -05001130static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001131{
1132 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
1133 struct device *dev = &pdev->dev;
1134
Wolfram Sangc4651f12020-09-10 11:11:18 +02001135 if (priv->host_notify_client)
1136 i2c_free_slave_host_notify_device(priv->host_notify_client);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001137 i2c_del_adapter(&priv->adap);
Niklas Söderlund73e8b052016-05-14 14:17:08 +02001138 rcar_i2c_release_dma(priv);
Wolfram Sang7ee24eb2015-12-23 17:56:34 +01001139 if (priv->flags & ID_P_PM_BLOCKED)
1140 pm_runtime_put(dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001141 pm_runtime_disable(dev);
1142
1143 return 0;
1144}
1145
Wolfram Sang18569fa2018-12-19 17:48:26 +01001146#ifdef CONFIG_PM_SLEEP
1147static int rcar_i2c_suspend(struct device *dev)
1148{
1149 struct rcar_i2c_priv *priv = dev_get_drvdata(dev);
1150
1151 i2c_mark_adapter_suspended(&priv->adap);
1152 return 0;
1153}
1154
1155static int rcar_i2c_resume(struct device *dev)
1156{
1157 struct rcar_i2c_priv *priv = dev_get_drvdata(dev);
1158
1159 i2c_mark_adapter_resumed(&priv->adap);
1160 return 0;
1161}
1162
Geert Uytterhoeven81d696c2019-01-22 11:03:57 +01001163static const struct dev_pm_ops rcar_i2c_pm_ops = {
1164 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rcar_i2c_suspend, rcar_i2c_resume)
1165};
Wolfram Sang18569fa2018-12-19 17:48:26 +01001166
1167#define DEV_PM_OPS (&rcar_i2c_pm_ops)
1168#else
1169#define DEV_PM_OPS NULL
1170#endif /* CONFIG_PM_SLEEP */
1171
Wolfram Sang45fd5e42012-11-13 11:24:15 +01001172static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001173 .driver = {
1174 .name = "i2c-rcar",
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +02001175 .of_match_table = rcar_i2c_dt_ids,
Wolfram Sang18569fa2018-12-19 17:48:26 +01001176 .pm = DEV_PM_OPS,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001177 },
1178 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001179 .remove = rcar_i2c_remove,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001180};
1181
Wolfram Sang45fd5e42012-11-13 11:24:15 +01001182module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001183
Wolfram Sang3d99bea2014-05-28 09:44:46 +02001184MODULE_LICENSE("GPL v2");
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001185MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
1186MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");