blob: fc13511f4562c1f274f346361aa2a44d47817da6 [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
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200370static 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öderlund73e8b052016-05-14 14:17:08 +0200375 dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg),
Wolfram Sang91633502017-05-28 09:52:17 +0200376 sg_dma_len(&priv->sg), priv->dma_direction);
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200377
Wolfram Sang3b770012018-06-28 22:45:38 +0200378 /* 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öderlund73e8b052016-05-14 14:17:08 +0200383 priv->dma_direction = DMA_NONE;
Hiromitsu Yamasakia35ba2f2019-03-03 16:03:13 +0100384
385 /* Disable DMA Master Received/Transmitted, must be last! */
386 rcar_i2c_write(priv, ICDMAER, 0);
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200387}
388
389static 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
401static 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 Sang03f85e32019-03-05 18:54:33 +0100410static bool rcar_i2c_dma(struct rcar_i2c_priv *priv)
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200411{
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 Sang3b770012018-06-28 22:45:38 +0200423 /* Do various checks to see if DMA is feasible at all */
Ulrich Hecht82531df2021-02-12 17:45:41 +0100424 if (priv->atomic_xfer || IS_ERR(chan) || msg->len < RCAR_MIN_DMA_LEN ||
Wolfram Sanged5a8102019-03-05 18:54:32 +0100425 !(msg->flags & I2C_M_DMA_SAFE) || (read && priv->flags & ID_P_NO_RXDMA))
Wolfram Sang03f85e32019-03-05 18:54:33 +0100426 return false;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200427
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 Sangc13c2912016-08-24 11:19:29 +0200444 if (dma_mapping_error(chan->device->dev, dma_addr)) {
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200445 dev_dbg(dev, "dma map failed, using PIO\n");
Wolfram Sang03f85e32019-03-05 18:54:33 +0100446 return false;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200447 }
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 Sang03f85e32019-03-05 18:54:33 +0100460 return false;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200461 }
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 Sang03f85e32019-03-05 18:54:33 +0100470 return false;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200471 }
472
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200473 /* 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 Sang03f85e32019-03-05 18:54:33 +0100480 return true;
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200481}
482
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100483static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700484{
485 struct i2c_msg *msg = priv->msg;
486
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100487 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700488 if (!(msr & MDE))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100489 return;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700490
Wolfram Sang94e290b2019-03-05 18:54:34 +0100491 /* Check if DMA can be enabled and take over */
492 if (priv->pos == 1 && rcar_i2c_dma(priv))
493 return;
494
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700495 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 Morimoto6ccbe602012-09-27 23:44:25 -0700505 } 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 Sangcc21d0b2015-11-19 16:56:46 +0100514 if (priv->flags & ID_LAST_MSG) {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700515 /*
516 * If current msg is the _LAST_ msg,
517 * prepare stop condition here.
518 * ID_DONE will be set on STOP irq.
519 */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200520 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100521 } else {
522 rcar_i2c_next_msg(priv);
523 return;
524 }
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700525 }
526
Wolfram Sang3c95de62014-05-28 09:44:42 +0200527 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700528}
529
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100530static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700531{
532 struct i2c_msg *msg = priv->msg;
533
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100534 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700535 if (!(msr & MDR))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100536 return;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700537
538 if (msr & MAT) {
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200539 /*
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 Morimoto6ccbe602012-09-27 23:44:25 -0700544 } else if (priv->pos < msg->len) {
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100545 /* get received data */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700546 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
547 priv->pos++;
548 }
549
Wolfram Sang19358d42018-08-08 09:59:28 +0200550 /* 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 Morimoto6ccbe602012-09-27 23:44:25 -0700559
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100560 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
561 rcar_i2c_next_msg(priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700562 else
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100563 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700564}
565
Wolfram Sangde20d182014-11-18 17:04:55 +0100566static 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 Sang5b77d162015-03-23 09:26:36 +0100581 i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100582 rcar_i2c_write(priv, ICRXTX, value);
583 rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
584 } else {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100585 i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100586 rcar_i2c_read(priv, ICRXTX); /* dummy read */
587 rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
588 }
589
Wolfram Sang314139f2020-06-29 17:38:07 +0200590 /* Clear SSR, too, because of old STOPs to other clients than us */
591 rcar_i2c_write(priv, ICSSR, ~(SAR | SSR) & 0xff);
Wolfram Sangde20d182014-11-18 17:04:55 +0100592 }
593
594 /* master sent stop */
595 if (ssr_filtered & SSR) {
596 i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
Wolfram Sang914a7b32020-08-17 14:19:30 +0200597 rcar_i2c_write(priv, ICSCR, SIE | SDBS); /* clear our NACK */
Wolfram Sang314139f2020-06-29 17:38:07 +0200598 rcar_i2c_write(priv, ICSIER, SAR);
Wolfram Sangde20d182014-11-18 17:04:55 +0100599 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 Sang5b77d162015-03-23 09:26:36 +0100607 ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100608 /* Send NACK in case of error */
609 rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
Wolfram Sangde20d182014-11-18 17:04:55 +0100610 rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
611 }
612
613 /* master wants to read from us */
614 if (ssr_filtered & SDE) {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100615 i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100616 rcar_i2c_write(priv, ICRXTX, value);
617 rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
618 }
619
620 return true;
621}
622
Wolfram Sang7ce98a52019-03-03 16:03:14 +0100623/*
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 Sangc7b514e2020-12-23 18:21:51 +0100626 * There, we need to switch to RCAR_BUS_PHASE_DATA as soon as possible after
Wolfram Sang7ce98a52019-03-03 16:03:14 +0100627 * 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 Sang9c975c42020-12-23 18:21:54 +0100631 * 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 Sang7ce98a52019-03-03 16:03:14 +0100633 */
Wolfram Sang9c975c42020-12-23 18:21:54 +0100634static irqreturn_t rcar_i2c_irq(int irq, struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700635{
Sergei Shtylyovaa5beaf2014-09-15 00:20:19 +0400636 if (!msr) {
Wolfram Sangc3be0af12015-11-19 16:56:48 +0100637 if (rcar_i2c_slave_irq(priv))
638 return IRQ_HANDLED;
639
640 return IRQ_NONE;
Sergei Shtylyovaa5beaf2014-09-15 00:20:19 +0400641 }
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400642
Wolfram Sang51371cd2014-05-28 09:44:45 +0200643 /* Arbitration lost */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700644 if (msr & MAL) {
Wolfram Sang42c07832015-12-23 17:56:33 +0100645 priv->flags |= ID_DONE | ID_ARBLOST;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700646 goto out;
647 }
648
Wolfram Sang51371cd2014-05-28 09:44:45 +0200649 /* Nack */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700650 if (msr & MNR) {
Wolfram Sangd89667b2015-11-19 16:56:47 +0100651 /* HW automatically sends STOP after received NACK */
Ulrich Hecht82531df2021-02-12 17:45:41 +0100652 if (!priv->atomic_xfer)
653 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
Wolfram Sang42c07832015-12-23 17:56:33 +0100654 priv->flags |= ID_NACK;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700655 goto out;
656 }
657
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400658 /* Stop */
659 if (msr & MST) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100660 priv->msgs_left--; /* The last message also made it */
Wolfram Sang42c07832015-12-23 17:56:33 +0100661 priv->flags |= ID_DONE;
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400662 goto out;
663 }
664
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700665 if (rcar_i2c_is_recv(priv))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100666 rcar_i2c_irq_recv(priv, msr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700667 else
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100668 rcar_i2c_irq_send(priv, msr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700669
670out:
Wolfram Sang42c07832015-12-23 17:56:33 +0100671 if (priv->flags & ID_DONE) {
Wolfram Sangf2382242014-05-28 09:44:39 +0200672 rcar_i2c_write(priv, ICMIER, 0);
Wolfram Sang3c95de62014-05-28 09:44:42 +0200673 rcar_i2c_write(priv, ICMSR, 0);
Ulrich Hecht82531df2021-02-12 17:45:41 +0100674 if (!priv->atomic_xfer)
675 wake_up(&priv->wait);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700676 }
677
Wolfram Sangc3be0af12015-11-19 16:56:48 +0100678 return IRQ_HANDLED;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700679}
680
Wolfram Sang9c975c42020-12-23 18:21:54 +0100681static 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 Hecht82531df2021-02-12 17:45:41 +0100692 if (!priv->atomic_xfer)
693 msr &= rcar_i2c_read(priv, ICMIER);
Wolfram Sang9c975c42020-12-23 18:21:54 +0100694
695 return rcar_i2c_irq(irq, priv, msr);
696}
697
698static 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 Hecht82531df2021-02-12 17:45:41 +0100705 if (!priv->atomic_xfer)
706 msr &= rcar_i2c_read(priv, ICMIER);
Wolfram Sang9c975c42020-12-23 18:21:54 +0100707
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öderlund73e8b052016-05-14 14:17:08 +0200718static 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öderlund6aabf9d2016-05-19 10:29:17 +0200727 chan = dma_request_chan(dev, chan_name);
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200728 if (IS_ERR(chan)) {
Eugeniu Rosca8ae034c2017-08-15 22:36:23 +0200729 dev_dbg(dev, "request_channel failed for %s (%ld)\n",
730 chan_name, PTR_ERR(chan));
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200731 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
756static 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
779static 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 Sang3b770012018-06-28 22:45:38 +0200792/* I2C is a special case, we need to poll the status of a reset */
793static int rcar_i2c_do_reset(struct rcar_i2c_priv *priv)
794{
Wolfram Sang74779f62020-08-29 22:38:10 +0200795 int ret;
Wolfram Sang3b770012018-06-28 22:45:38 +0200796
797 ret = reset_control_reset(priv->rstc);
798 if (ret)
799 return ret;
800
Wolfram Sang74779f62020-08-29 22:38:10 +0200801 return read_poll_timeout_atomic(reset_control_status, ret, ret == 0, 1,
802 100, false, priv->rstc);
Wolfram Sang3b770012018-06-28 22:45:38 +0200803}
804
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700805static 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 Sangb6763d02015-06-20 21:03:20 +0200811 int i, ret;
Wolfram Sangff2316b2015-11-19 16:56:44 +0100812 long time_left;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700813
Ulrich Hecht82531df2021-02-12 17:45:41 +0100814 priv->atomic_xfer = false;
815
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700816 pm_runtime_get_sync(dev);
817
Wolfram Sang0b574362018-11-13 12:15:42 +0100818 /* 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 Sang3b770012018-06-28 22:45:38 +0200823 /* 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 Sangae481cc2017-04-18 20:38:35 +0200833 rcar_i2c_init(priv);
834
Wolfram Sang3ef3e5c2018-07-23 22:26:14 +0200835 for (i = 0; i < num; i++)
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200836 rcar_i2c_request_dma(priv, msgs + i);
Wolfram Sangd7653962014-05-05 18:36:21 +0200837
Wolfram Sange49865d2015-11-19 16:56:51 +0100838 /* init first message */
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100839 priv->msg = msgs;
840 priv->msgs_left = num;
Wolfram Sang7ee24eb2015-12-23 17:56:34 +0100841 priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100842 rcar_i2c_prepare_msg(priv);
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400843
Wolfram Sang42c07832015-12-23 17:56:33 +0100844 time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE,
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100845 num * adap->timeout);
Wolfram Sang31d86032018-10-19 21:15:26 +0200846
847 /* cleanup DMA if it couldn't complete properly due to an error */
848 if (priv->dma_direction != DMA_NONE)
Niklas Söderlund73e8b052016-05-14 14:17:08 +0200849 rcar_i2c_cleanup_dma(priv);
Wolfram Sang31d86032018-10-19 21:15:26 +0200850
851 if (!time_left) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100852 rcar_i2c_init(priv);
853 ret = -ETIMEDOUT;
Wolfram Sang42c07832015-12-23 17:56:33 +0100854 } else if (priv->flags & ID_NACK) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100855 ret = -ENXIO;
Wolfram Sang42c07832015-12-23 17:56:33 +0100856 } else if (priv->flags & ID_ARBLOST) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100857 ret = -EAGAIN;
858 } else {
859 ret = num - priv->msgs_left; /* The number of transfer */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700860 }
Wolfram Sang3f7de222014-05-28 09:44:40 +0200861out:
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700862 pm_runtime_put(dev);
863
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000864 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700865 dev_err(dev, "error %d : %x\n", ret, priv->flags);
866
867 return ret;
868}
869
Ulrich Hecht82531df2021-02-12 17:45:41 +0100870static 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 }
923out:
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 Sangde20d182014-11-18 17:04:55 +0100932static 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 Sang63a761e2017-04-20 12:04:33 +0200942 /* Keep device active for slave address detection logic */
Wolfram Sangb4cd08a2015-12-16 20:05:18 +0100943 pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
Wolfram Sangde20d182014-11-18 17:04:55 +0100944
945 priv->slave = slave;
946 rcar_i2c_write(priv, ICSAR, slave->addr);
947 rcar_i2c_write(priv, ICSSR, 0);
Wolfram Sang314139f2020-06-29 17:38:07 +0200948 rcar_i2c_write(priv, ICSIER, SAR);
Wolfram Sangde20d182014-11-18 17:04:55 +0100949 rcar_i2c_write(priv, ICSCR, SIE | SDBS);
950
951 return 0;
952}
953
954static 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 Sangc7c9e912020-07-26 18:16:06 +0200960 /* ensure no irq is running before clearing ptr */
961 disable_irq(priv->irq);
Wolfram Sangde20d182014-11-18 17:04:55 +0100962 rcar_i2c_write(priv, ICSIER, 0);
Wolfram Sangc7c9e912020-07-26 18:16:06 +0200963 rcar_i2c_write(priv, ICSSR, 0);
964 enable_irq(priv->irq);
965 rcar_i2c_write(priv, ICSCR, SDBS);
Wolfram Sangeb0159712020-07-04 15:38:29 +0200966 rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
Wolfram Sangde20d182014-11-18 17:04:55 +0100967
968 priv->slave = NULL;
969
Wolfram Sangb4cd08a2015-12-16 20:05:18 +0100970 pm_runtime_put(rcar_i2c_priv_to_dev(priv));
Wolfram Sangde20d182014-11-18 17:04:55 +0100971
972 return 0;
973}
974
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700975static u32 rcar_i2c_func(struct i2c_adapter *adap)
976{
Wolfram Sangc4651f12020-09-10 11:11:18 +0200977 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
978
Wolfram Sangb395ba22017-06-19 23:41:46 +0200979 /*
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 Sangc4651f12020-09-10 11:11:18 +0200985 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 Morimoto6ccbe602012-09-27 23:44:25 -0700992}
993
994static const struct i2c_algorithm rcar_i2c_algo = {
995 .master_xfer = rcar_i2c_master_xfer,
Ulrich Hecht82531df2021-02-12 17:45:41 +0100996 .master_xfer_atomic = rcar_i2c_master_xfer_atomic,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700997 .functionality = rcar_i2c_func,
Wolfram Sangde20d182014-11-18 17:04:55 +0100998 .reg_slave = rcar_reg_slave,
999 .unreg_slave = rcar_unreg_slave,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001000};
1001
Wolfram Sang3ef3e5c2018-07-23 22:26:14 +02001002static const struct i2c_adapter_quirks rcar_i2c_quirks = {
1003 .flags = I2C_AQ_NO_ZERO_LEN,
1004};
1005
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +02001006static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -07001007 { .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 Sange8936452014-02-20 09:03:20 +01001010 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +02001011 { .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 Sange7db0d32015-08-05 15:18:25 +02001014 { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
Ulrich Hechtc13f7432016-09-14 18:46:06 +02001015 { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 },
Simon Hormanad4a8dc2016-12-06 17:01:28 +01001016 { .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 Liakhovetski7679c0e2013-09-12 14:36:46 +02001019 {},
1020};
1021MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
1022
Bill Pemberton0b255e92012-11-27 15:59:38 -05001023static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001024{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001025 struct rcar_i2c_priv *priv;
1026 struct i2c_adapter *adap;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001027 struct device *dev = &pdev->dev;
Wolfram Sang24c6d4b2020-12-23 18:21:53 +01001028 unsigned long irqflags = 0;
Wolfram Sang9c975c42020-12-23 18:21:54 +01001029 irqreturn_t (*irqhandler)(int irq, void *ptr) = rcar_i2c_gen3_irq;
Wolfram Sang7b814d82019-08-08 21:39:10 +02001030 int ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001031
Wolfram Sanged5a8102019-03-05 18:54:32 +01001032 /* 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 Morimoto6ccbe602012-09-27 23:44:25 -07001035 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +09001036 if (!priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001037 return -ENOMEM;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001038
Ben Dooksbc8120f2014-01-26 16:05:35 +00001039 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 Zhengc02fb2b2020-04-14 21:48:27 +08001045 priv->io = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res);
Thierry Reding84dbf802013-01-21 11:09:03 +01001046 if (IS_ERR(priv->io))
1047 return PTR_ERR(priv->io);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001048
Wolfram Sang69e558f2016-03-01 17:36:43 +01001049 priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001050 init_waitqueue_head(&priv->wait);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001051
Wolfram Sang929e3aba2014-07-10 13:46:31 +02001052 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 Sang7d2c17f2018-01-09 14:58:59 +01001059 adap->bus_recovery_info = &rcar_i2c_bri;
Wolfram Sang3ef3e5c2018-07-23 22:26:14 +02001060 adap->quirks = &rcar_i2c_quirks;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001061 i2c_set_adapdata(adap, priv);
1062 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1063
Niklas Söderlund73e8b052016-05-14 14:17:08 +02001064 /* 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 Sang63a761e2017-04-20 12:04:33 +02001069 /* Activate device for clock calculation */
Wolfram Sang4f7effd2015-10-09 10:39:25 +01001070 pm_runtime_enable(dev);
Wolfram Sangf9c9d312015-12-08 10:37:47 +01001071 pm_runtime_get_sync(dev);
Andy Shevchenko38a592e2020-03-24 14:32:13 +02001072 ret = rcar_i2c_clock_calculate(priv);
Wolfram Sangf9c9d312015-12-08 10:37:47 +01001073 if (ret < 0)
1074 goto out_pm_put;
1075
Wolfram Sangeb0159712020-07-04 15:38:29 +02001076 rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
1077
Wolfram Sang9c975c42020-12-23 18:21:54 +01001078 if (priv->devtype < I2C_RCAR_GEN3) {
Wolfram Sang24c6d4b2020-12-23 18:21:53 +01001079 irqflags |= IRQF_NO_THREAD;
Wolfram Sang9c975c42020-12-23 18:21:54 +01001080 irqhandler = rcar_i2c_gen2_irq;
1081 }
Wolfram Sang24c6d4b2020-12-23 18:21:53 +01001082
Wolfram Sang3b770012018-06-28 22:45:38 +02001083 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 Sang63a761e2017-04-20 12:04:33 +02001092 /* Stay always active when multi-master to keep arbitration working */
Wolfram Sang7ee24eb2015-12-23 17:56:34 +01001093 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 Sangc4651f12020-09-10 11:11:18 +02001098 if (of_property_read_bool(dev->of_node, "smbus"))
1099 priv->flags |= ID_P_HOST_NOTIFY;
Wolfram Sangf9c9d312015-12-08 10:37:47 +01001100
Sergey Shtylyov147178c2021-04-10 23:23:33 +03001101 ret = platform_get_irq(pdev, 0);
1102 if (ret < 0)
1103 goto out_pm_disable;
1104 priv->irq = ret;
Wolfram Sang9c975c42020-12-23 18:21:54 +01001105 ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001106 if (ret < 0) {
Wolfram Sang7b814d82019-08-08 21:39:10 +02001107 dev_err(dev, "cannot get irq %d\n", priv->irq);
Wolfram Sange43e0df2015-11-19 16:56:41 +01001108 goto out_pm_disable;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001109 }
1110
Wolfram Sang4f7effd2015-10-09 10:39:25 +01001111 platform_set_drvdata(pdev, priv);
1112
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001113 ret = i2c_add_numbered_adapter(adap);
Wolfram Sangea734402016-08-09 13:36:17 +02001114 if (ret < 0)
Wolfram Sange43e0df2015-11-19 16:56:41 +01001115 goto out_pm_disable;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001116
Wolfram Sangc4651f12020-09-10 11:11:18 +02001117 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 Morimoto6ccbe602012-09-27 23:44:25 -07001125 dev_info(dev, "probed\n");
1126
1127 return 0;
Wolfram Sange43e0df2015-11-19 16:56:41 +01001128
Wolfram Sangc4651f12020-09-10 11:11:18 +02001129 out_del_device:
1130 i2c_del_adapter(&priv->adap);
Wolfram Sange43e0df2015-11-19 16:56:41 +01001131 out_pm_put:
1132 pm_runtime_put(dev);
1133 out_pm_disable:
1134 pm_runtime_disable(dev);
1135 return ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001136}
1137
Bill Pemberton0b255e92012-11-27 15:59:38 -05001138static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001139{
1140 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
1141 struct device *dev = &pdev->dev;
1142
Wolfram Sangc4651f12020-09-10 11:11:18 +02001143 if (priv->host_notify_client)
1144 i2c_free_slave_host_notify_device(priv->host_notify_client);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001145 i2c_del_adapter(&priv->adap);
Niklas Söderlund73e8b052016-05-14 14:17:08 +02001146 rcar_i2c_release_dma(priv);
Wolfram Sang7ee24eb2015-12-23 17:56:34 +01001147 if (priv->flags & ID_P_PM_BLOCKED)
1148 pm_runtime_put(dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001149 pm_runtime_disable(dev);
1150
1151 return 0;
1152}
1153
Wolfram Sang18569fa2018-12-19 17:48:26 +01001154#ifdef CONFIG_PM_SLEEP
1155static 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
1163static 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 Uytterhoeven81d696c2019-01-22 11:03:57 +01001171static const struct dev_pm_ops rcar_i2c_pm_ops = {
1172 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rcar_i2c_suspend, rcar_i2c_resume)
1173};
Wolfram Sang18569fa2018-12-19 17:48:26 +01001174
1175#define DEV_PM_OPS (&rcar_i2c_pm_ops)
1176#else
1177#define DEV_PM_OPS NULL
1178#endif /* CONFIG_PM_SLEEP */
1179
Wolfram Sang45fd5e42012-11-13 11:24:15 +01001180static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001181 .driver = {
1182 .name = "i2c-rcar",
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +02001183 .of_match_table = rcar_i2c_dt_ids,
Wolfram Sang18569fa2018-12-19 17:48:26 +01001184 .pm = DEV_PM_OPS,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001185 },
1186 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001187 .remove = rcar_i2c_remove,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001188};
1189
Wolfram Sang45fd5e42012-11-13 11:24:15 +01001190module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001191
Wolfram Sang3d99bea2014-05-28 09:44:46 +02001192MODULE_LICENSE("GPL v2");
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001193MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
1194MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");