blob: 68ecb5630ad588c83baa6f12a1f089ea541a901e [file] [log] [blame]
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001/*
Wolfram Sang3d99bea2014-05-28 09:44:46 +02002 * Driver for the Renesas RCar I2C unit
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07003 *
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +01004 * Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com>
5 * Copyright (C) 2011-2015 Renesas Electronics Corporation
Wolfram Sang3d99bea2014-05-28 09:44:46 +02006 *
7 * Copyright (C) 2012-14 Renesas Solutions Corp.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07008 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 *
10 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
11 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
12 *
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
Wolfram Sang3d99bea2014-05-28 09:44:46 +020015 * the Free Software Foundation; version 2 of the License.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070016 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070021 */
22#include <linux/clk.h>
23#include <linux/delay.h>
24#include <linux/err.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070025#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/i2c.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070028#include <linux/kernel.h>
29#include <linux/module.h>
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +020030#include <linux/of_device.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070031#include <linux/platform_device.h>
32#include <linux/pm_runtime.h>
33#include <linux/slab.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070034
35/* register offsets */
36#define ICSCR 0x00 /* slave ctrl */
37#define ICMCR 0x04 /* master ctrl */
38#define ICSSR 0x08 /* slave status */
39#define ICMSR 0x0C /* master status */
40#define ICSIER 0x10 /* slave irq enable */
41#define ICMIER 0x14 /* master irq enable */
42#define ICCCR 0x18 /* clock dividers */
43#define ICSAR 0x1C /* slave address */
44#define ICMAR 0x20 /* master address */
45#define ICRXTX 0x24 /* data port */
46
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 */
61#define ESG (1 << 0) /* en startbit gen */
62
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
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070081
Wolfram Sang4f443a82014-05-28 09:44:38 +020082#define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
83#define RCAR_BUS_PHASE_DATA (MDBS | MIE)
Wolfram Sang52df4452015-11-19 16:56:49 +010084#define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF)
Wolfram Sang4f443a82014-05-28 09:44:38 +020085#define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070086
Wolfram Sang3e3aaba2014-05-28 09:44:44 +020087#define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
88#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
89#define RCAR_IRQ_STOP (MST)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070090
Sergei Shtylyov938916f2014-09-06 03:34:32 +040091#define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
92#define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
Wolfram Sang3c95de62014-05-28 09:44:42 +020093
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070094#define ID_LAST_MSG (1 << 0)
Wolfram Sange49865d2015-11-19 16:56:51 +010095#define ID_FIRST_MSG (1 << 1)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070096#define ID_DONE (1 << 2)
97#define ID_ARBLOST (1 << 3)
98#define ID_NACK (1 << 4)
Wolfram Sang7ee24eb2015-12-23 17:56:34 +010099/* persistent flags */
100#define ID_P_PM_BLOCKED (1 << 31)
101#define ID_P_MASK ID_P_PM_BLOCKED
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700102
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900103enum rcar_i2c_type {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700104 I2C_RCAR_GEN1,
105 I2C_RCAR_GEN2,
Wolfram Sange7db0d32015-08-05 15:18:25 +0200106 I2C_RCAR_GEN3,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900107};
108
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700109struct rcar_i2c_priv {
110 void __iomem *io;
111 struct i2c_adapter adap;
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100112 struct i2c_msg *msg;
113 int msgs_left;
Ben Dooksbc8120f2014-01-26 16:05:35 +0000114 struct clk *clk;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700115
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700116 wait_queue_head_t wait;
117
118 int pos;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700119 u32 icccr;
120 u32 flags;
Wolfram Sang51371cd2014-05-28 09:44:45 +0200121 enum rcar_i2c_type devtype;
Wolfram Sangde20d182014-11-18 17:04:55 +0100122 struct i2c_client *slave;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700123};
124
125#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
126#define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
127
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700128#define LOOP_TIMEOUT 1024
129
Wolfram Sang51371cd2014-05-28 09:44:45 +0200130
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700131static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
132{
133 writel(val, priv->io + reg);
134}
135
136static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
137{
138 return readl(priv->io + reg);
139}
140
141static void rcar_i2c_init(struct rcar_i2c_priv *priv)
142{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700143 /* reset master mode */
144 rcar_i2c_write(priv, ICMIER, 0);
Wolfram Sang2c78cdc2015-11-19 16:56:42 +0100145 rcar_i2c_write(priv, ICMCR, MDBS);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700146 rcar_i2c_write(priv, ICMSR, 0);
Wolfram Sang2c78cdc2015-11-19 16:56:42 +0100147 /* start clock */
148 rcar_i2c_write(priv, ICCCR, priv->icccr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700149}
150
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700151static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
152{
153 int i;
154
155 for (i = 0; i < LOOP_TIMEOUT; i++) {
156 /* make sure that bus is not busy */
157 if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
158 return 0;
159 udelay(1);
160 }
161
162 return -EBUSY;
163}
164
Wolfram Sangc7881872015-12-08 10:37:48 +0100165static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, struct i2c_timings *t)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700166{
Wolfram Sangca68ead2015-12-08 10:37:49 +0100167 u32 scgd, cdf, round, ick, sum, scl, cdf_width;
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200168 unsigned long rate;
Wolfram Sangf9c9d312015-12-08 10:37:47 +0100169 struct device *dev = rcar_i2c_priv_to_dev(priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700170
Wolfram Sangc7881872015-12-08 10:37:48 +0100171 /* Fall back to previously used values if not supplied */
172 t->bus_freq_hz = t->bus_freq_hz ?: 100000;
Wolfram Sangca68ead2015-12-08 10:37:49 +0100173 t->scl_fall_ns = t->scl_fall_ns ?: 35;
174 t->scl_rise_ns = t->scl_rise_ns ?: 200;
175 t->scl_int_delay_ns = t->scl_int_delay_ns ?: 50;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700176
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900177 switch (priv->devtype) {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700178 case I2C_RCAR_GEN1:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900179 cdf_width = 2;
180 break;
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700181 case I2C_RCAR_GEN2:
Wolfram Sange7db0d32015-08-05 15:18:25 +0200182 case I2C_RCAR_GEN3:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900183 cdf_width = 3;
184 break;
185 default:
186 dev_err(dev, "device type error\n");
187 return -EIO;
188 }
189
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700190 /*
191 * calculate SCL clock
192 * see
193 * ICCCR
194 *
195 * ick = clkp / (1 + CDF)
196 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
197 *
198 * ick : I2C internal clock < 20 MHz
Wolfram Sangca68ead2015-12-08 10:37:49 +0100199 * ticf : I2C SCL falling time
200 * tr : I2C SCL rising time
201 * intd : LSI internal delay
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700202 * clkp : peripheral_clk
203 * F[] : integer up-valuation
204 */
Ben Dooksbc8120f2014-01-26 16:05:35 +0000205 rate = clk_get_rate(priv->clk);
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200206 cdf = rate / 20000000;
Wolfram Sang22762cc2014-09-20 12:07:37 +0200207 if (cdf >= 1U << cdf_width) {
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200208 dev_err(dev, "Input clock %lu too high\n", rate);
209 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700210 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200211 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700212
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700213 /*
214 * it is impossible to calculate large scale
215 * number on u32. separate it
216 *
Wolfram Sangca68ead2015-12-08 10:37:49 +0100217 * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
218 * = F[sum * ick / 1000000000]
219 * = F[(ick / 1000000) * sum / 1000]
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700220 */
Wolfram Sangca68ead2015-12-08 10:37:49 +0100221 sum = t->scl_fall_ns + t->scl_rise_ns + t->scl_int_delay_ns;
222 round = (ick + 500000) / 1000000 * sum;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700223 round = (round + 500) / 1000;
224
225 /*
226 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
227 *
228 * Calculation result (= SCL) should be less than
229 * bus_speed for hardware safety
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200230 *
231 * We could use something along the lines of
232 * div = ick / (bus_speed + 1) + 1;
233 * scgd = (div - 20 - round + 7) / 8;
234 * scl = ick / (20 + (scgd * 8) + round);
235 * (not fully verified) but that would get pretty involved
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700236 */
237 for (scgd = 0; scgd < 0x40; scgd++) {
238 scl = ick / (20 + (scgd * 8) + round);
Wolfram Sangc7881872015-12-08 10:37:48 +0100239 if (scl <= t->bus_freq_hz)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700240 goto scgd_find;
241 }
242 dev_err(dev, "it is impossible to calculate best SCL\n");
243 return -EIO;
244
245scgd_find:
246 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
Wolfram Sangc7881872015-12-08 10:37:48 +0100247 scl, t->bus_freq_hz, clk_get_rate(priv->clk), round, cdf, scgd);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700248
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100249 /* keep icccr value */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200250 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700251
252 return 0;
253}
254
Sergei Shtylyov7c7117f2014-09-15 00:15:46 +0400255static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700256{
Wolfram Sang386babf2014-05-28 09:44:41 +0200257 int read = !!rcar_i2c_is_recv(priv);
258
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100259 priv->pos = 0;
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100260 if (priv->msgs_left == 1)
Wolfram Sang42c07832015-12-23 17:56:33 +0100261 priv->flags |= ID_LAST_MSG;
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100262
Wolfram Sang386babf2014-05-28 09:44:41 +0200263 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
Wolfram Sange49865d2015-11-19 16:56:51 +0100264 /*
265 * We don't have a testcase but the HW engineers say that the write order
266 * of ICMSR and ICMCR depends on whether we issue START or REP_START. Since
267 * it didn't cause a drawback for me, let's rather be safe than sorry.
268 */
Wolfram Sang42c07832015-12-23 17:56:33 +0100269 if (priv->flags & ID_FIRST_MSG) {
Wolfram Sange49865d2015-11-19 16:56:51 +0100270 rcar_i2c_write(priv, ICMSR, 0);
271 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
272 } else {
273 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
274 rcar_i2c_write(priv, ICMSR, 0);
275 }
Wolfram Sang386babf2014-05-28 09:44:41 +0200276 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700277}
278
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100279static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
280{
281 priv->msg++;
282 priv->msgs_left--;
Wolfram Sang7ee24eb2015-12-23 17:56:34 +0100283 priv->flags &= ID_P_MASK;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100284 rcar_i2c_prepare_msg(priv);
285}
286
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700287/*
288 * interrupt functions
289 */
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100290static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700291{
292 struct i2c_msg *msg = priv->msg;
293
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100294 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700295 if (!(msr & MDE))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100296 return;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700297
298 if (priv->pos < msg->len) {
299 /*
300 * Prepare next data to ICRXTX register.
301 * This data will go to _SHIFT_ register.
302 *
303 * *
304 * [ICRXTX] -> [SHIFT] -> [I2C bus]
305 */
306 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
307 priv->pos++;
308
309 } else {
310 /*
311 * The last data was pushed to ICRXTX on _PREV_ empty irq.
312 * It is on _SHIFT_ register, and will sent to I2C bus.
313 *
314 * *
315 * [ICRXTX] -> [SHIFT] -> [I2C bus]
316 */
317
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100318 if (priv->flags & ID_LAST_MSG) {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700319 /*
320 * If current msg is the _LAST_ msg,
321 * prepare stop condition here.
322 * ID_DONE will be set on STOP irq.
323 */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200324 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100325 } else {
326 rcar_i2c_next_msg(priv);
327 return;
328 }
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700329 }
330
Wolfram Sang3c95de62014-05-28 09:44:42 +0200331 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700332}
333
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100334static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700335{
336 struct i2c_msg *msg = priv->msg;
337
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100338 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700339 if (!(msr & MDR))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100340 return;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700341
342 if (msr & MAT) {
Wolfram Sang52df4452015-11-19 16:56:49 +0100343 /* Address transfer phase finished, but no data at this point. */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700344 } else if (priv->pos < msg->len) {
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100345 /* get received data */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700346 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
347 priv->pos++;
348 }
349
350 /*
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100351 * If next received data is the _LAST_, go to STOP phase. Might be
352 * overwritten by REP START when setting up a new msg. Not elegant
353 * but the only stable sequence for REP START I have found so far.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700354 */
355 if (priv->pos + 1 >= msg->len)
Wolfram Sang4f443a82014-05-28 09:44:38 +0200356 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700357
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100358 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
359 rcar_i2c_next_msg(priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700360 else
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100361 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700362}
363
Wolfram Sangde20d182014-11-18 17:04:55 +0100364static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
365{
366 u32 ssr_raw, ssr_filtered;
367 u8 value;
368
369 ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
370 ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
371
372 if (!ssr_filtered)
373 return false;
374
375 /* address detected */
376 if (ssr_filtered & SAR) {
377 /* read or write request */
378 if (ssr_raw & STM) {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100379 i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100380 rcar_i2c_write(priv, ICRXTX, value);
381 rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
382 } else {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100383 i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100384 rcar_i2c_read(priv, ICRXTX); /* dummy read */
385 rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
386 }
387
388 rcar_i2c_write(priv, ICSSR, ~SAR & 0xff);
389 }
390
391 /* master sent stop */
392 if (ssr_filtered & SSR) {
393 i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
394 rcar_i2c_write(priv, ICSIER, SAR | SSR);
395 rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
396 }
397
398 /* master wants to write to us */
399 if (ssr_filtered & SDR) {
400 int ret;
401
402 value = rcar_i2c_read(priv, ICRXTX);
Wolfram Sang5b77d162015-03-23 09:26:36 +0100403 ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100404 /* Send NACK in case of error */
405 rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
Wolfram Sangde20d182014-11-18 17:04:55 +0100406 rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
407 }
408
409 /* master wants to read from us */
410 if (ssr_filtered & SDE) {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100411 i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100412 rcar_i2c_write(priv, ICRXTX, value);
413 rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
414 }
415
416 return true;
417}
418
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700419static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
420{
421 struct rcar_i2c_priv *priv = ptr;
Wolfram Sang52df4452015-11-19 16:56:49 +0100422 u32 msr, val;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700423
Wolfram Sang52df4452015-11-19 16:56:49 +0100424 /* Clear START or STOP as soon as we can */
425 val = rcar_i2c_read(priv, ICMCR);
426 rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
Wolfram Sangde20d182014-11-18 17:04:55 +0100427
Wolfram Sang1c176d52014-05-28 09:44:36 +0200428 msr = rcar_i2c_read(priv, ICMSR);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700429
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400430 /* Only handle interrupts that are currently enabled */
431 msr &= rcar_i2c_read(priv, ICMIER);
Sergei Shtylyovaa5beaf2014-09-15 00:20:19 +0400432 if (!msr) {
Wolfram Sangc3be0af12015-11-19 16:56:48 +0100433 if (rcar_i2c_slave_irq(priv))
434 return IRQ_HANDLED;
435
436 return IRQ_NONE;
Sergei Shtylyovaa5beaf2014-09-15 00:20:19 +0400437 }
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400438
Wolfram Sang51371cd2014-05-28 09:44:45 +0200439 /* Arbitration lost */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700440 if (msr & MAL) {
Wolfram Sang42c07832015-12-23 17:56:33 +0100441 priv->flags |= ID_DONE | ID_ARBLOST;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700442 goto out;
443 }
444
Wolfram Sang51371cd2014-05-28 09:44:45 +0200445 /* Nack */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700446 if (msr & MNR) {
Wolfram Sangd89667b2015-11-19 16:56:47 +0100447 /* HW automatically sends STOP after received NACK */
Wolfram Sangf2382242014-05-28 09:44:39 +0200448 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
Wolfram Sang42c07832015-12-23 17:56:33 +0100449 priv->flags |= ID_NACK;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700450 goto out;
451 }
452
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400453 /* Stop */
454 if (msr & MST) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100455 priv->msgs_left--; /* The last message also made it */
Wolfram Sang42c07832015-12-23 17:56:33 +0100456 priv->flags |= ID_DONE;
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400457 goto out;
458 }
459
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700460 if (rcar_i2c_is_recv(priv))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100461 rcar_i2c_irq_recv(priv, msr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700462 else
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100463 rcar_i2c_irq_send(priv, msr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700464
465out:
Wolfram Sang42c07832015-12-23 17:56:33 +0100466 if (priv->flags & ID_DONE) {
Wolfram Sangf2382242014-05-28 09:44:39 +0200467 rcar_i2c_write(priv, ICMIER, 0);
Wolfram Sang3c95de62014-05-28 09:44:42 +0200468 rcar_i2c_write(priv, ICMSR, 0);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700469 wake_up(&priv->wait);
470 }
471
Wolfram Sangc3be0af12015-11-19 16:56:48 +0100472 return IRQ_HANDLED;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700473}
474
475static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
476 struct i2c_msg *msgs,
477 int num)
478{
479 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
480 struct device *dev = rcar_i2c_priv_to_dev(priv);
Wolfram Sangb6763d02015-06-20 21:03:20 +0200481 int i, ret;
Wolfram Sangff2316b2015-11-19 16:56:44 +0100482 long time_left;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700483
484 pm_runtime_get_sync(dev);
485
Wolfram Sang3f7de222014-05-28 09:44:40 +0200486 ret = rcar_i2c_bus_barrier(priv);
487 if (ret < 0)
488 goto out;
489
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700490 for (i = 0; i < num; i++) {
Wolfram Sangd7653962014-05-05 18:36:21 +0200491 /* This HW can't send STOP after address phase */
492 if (msgs[i].len == 0) {
493 ret = -EOPNOTSUPP;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100494 goto out;
Wolfram Sangd7653962014-05-05 18:36:21 +0200495 }
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100496 }
Wolfram Sangd7653962014-05-05 18:36:21 +0200497
Wolfram Sange49865d2015-11-19 16:56:51 +0100498 /* init first message */
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100499 priv->msg = msgs;
500 priv->msgs_left = num;
Wolfram Sang7ee24eb2015-12-23 17:56:34 +0100501 priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100502 rcar_i2c_prepare_msg(priv);
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400503
Wolfram Sang42c07832015-12-23 17:56:33 +0100504 time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE,
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100505 num * adap->timeout);
506 if (!time_left) {
507 rcar_i2c_init(priv);
508 ret = -ETIMEDOUT;
Wolfram Sang42c07832015-12-23 17:56:33 +0100509 } else if (priv->flags & ID_NACK) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100510 ret = -ENXIO;
Wolfram Sang42c07832015-12-23 17:56:33 +0100511 } else if (priv->flags & ID_ARBLOST) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100512 ret = -EAGAIN;
513 } else {
514 ret = num - priv->msgs_left; /* The number of transfer */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700515 }
Wolfram Sang3f7de222014-05-28 09:44:40 +0200516out:
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700517 pm_runtime_put(dev);
518
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000519 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700520 dev_err(dev, "error %d : %x\n", ret, priv->flags);
521
522 return ret;
523}
524
Wolfram Sangde20d182014-11-18 17:04:55 +0100525static int rcar_reg_slave(struct i2c_client *slave)
526{
527 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
528
529 if (priv->slave)
530 return -EBUSY;
531
532 if (slave->flags & I2C_CLIENT_TEN)
533 return -EAFNOSUPPORT;
534
Wolfram Sangb4cd08a2015-12-16 20:05:18 +0100535 pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
Wolfram Sangde20d182014-11-18 17:04:55 +0100536
537 priv->slave = slave;
538 rcar_i2c_write(priv, ICSAR, slave->addr);
539 rcar_i2c_write(priv, ICSSR, 0);
540 rcar_i2c_write(priv, ICSIER, SAR | SSR);
541 rcar_i2c_write(priv, ICSCR, SIE | SDBS);
542
543 return 0;
544}
545
546static int rcar_unreg_slave(struct i2c_client *slave)
547{
548 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
549
550 WARN_ON(!priv->slave);
551
552 rcar_i2c_write(priv, ICSIER, 0);
553 rcar_i2c_write(priv, ICSCR, 0);
554
555 priv->slave = NULL;
556
Wolfram Sangb4cd08a2015-12-16 20:05:18 +0100557 pm_runtime_put(rcar_i2c_priv_to_dev(priv));
Wolfram Sangde20d182014-11-18 17:04:55 +0100558
559 return 0;
560}
561
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700562static u32 rcar_i2c_func(struct i2c_adapter *adap)
563{
Wolfram Sangd7653962014-05-05 18:36:21 +0200564 /* This HW can't do SMBUS_QUICK and NOSTART */
Wolfram Sang1fb2ad92015-05-14 14:40:03 +0200565 return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
566 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700567}
568
569static const struct i2c_algorithm rcar_i2c_algo = {
570 .master_xfer = rcar_i2c_master_xfer,
571 .functionality = rcar_i2c_func,
Wolfram Sangde20d182014-11-18 17:04:55 +0100572 .reg_slave = rcar_reg_slave,
573 .unreg_slave = rcar_unreg_slave,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700574};
575
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200576static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700577 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
578 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
579 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
580 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange8936452014-02-20 09:03:20 +0100581 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +0200582 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
583 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
584 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange7db0d32015-08-05 15:18:25 +0200585 { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200586 {},
587};
588MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
589
Bill Pemberton0b255e92012-11-27 15:59:38 -0500590static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700591{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700592 struct rcar_i2c_priv *priv;
593 struct i2c_adapter *adap;
594 struct resource *res;
595 struct device *dev = &pdev->dev;
Wolfram Sangc7881872015-12-08 10:37:48 +0100596 struct i2c_timings i2c_t;
Wolfram Sang93e953d2014-05-28 09:44:37 +0200597 int irq, ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700598
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700599 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900600 if (!priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700601 return -ENOMEM;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700602
Ben Dooksbc8120f2014-01-26 16:05:35 +0000603 priv->clk = devm_clk_get(dev, NULL);
604 if (IS_ERR(priv->clk)) {
605 dev_err(dev, "cannot get clock\n");
606 return PTR_ERR(priv->clk);
607 }
608
Wolfram Sang3cc2d002013-05-10 10:16:54 +0200609 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100610 priv->io = devm_ioremap_resource(dev, res);
611 if (IS_ERR(priv->io))
612 return PTR_ERR(priv->io);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700613
Wolfram Sang69e558f2016-03-01 17:36:43 +0100614 priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700615 init_waitqueue_head(&priv->wait);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700616
Wolfram Sang929e3aba2014-07-10 13:46:31 +0200617 adap = &priv->adap;
618 adap->nr = pdev->id;
619 adap->algo = &rcar_i2c_algo;
620 adap->class = I2C_CLASS_DEPRECATED;
621 adap->retries = 3;
622 adap->dev.parent = dev;
623 adap->dev.of_node = dev->of_node;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700624 i2c_set_adapdata(adap, priv);
625 strlcpy(adap->name, pdev->name, sizeof(adap->name));
626
Wolfram Sangc7881872015-12-08 10:37:48 +0100627 i2c_parse_fw_timings(dev, &i2c_t, false);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700628
Wolfram Sang4f7effd2015-10-09 10:39:25 +0100629 pm_runtime_enable(dev);
Wolfram Sangf9c9d312015-12-08 10:37:47 +0100630 pm_runtime_get_sync(dev);
Wolfram Sangc7881872015-12-08 10:37:48 +0100631 ret = rcar_i2c_clock_calculate(priv, &i2c_t);
Wolfram Sangf9c9d312015-12-08 10:37:47 +0100632 if (ret < 0)
633 goto out_pm_put;
634
635 rcar_i2c_init(priv);
Wolfram Sang7ee24eb2015-12-23 17:56:34 +0100636
637 /* Don't suspend when multi-master to keep arbitration working */
638 if (of_property_read_bool(dev->of_node, "multi-master"))
639 priv->flags |= ID_P_PM_BLOCKED;
640 else
641 pm_runtime_put(dev);
642
Wolfram Sangf9c9d312015-12-08 10:37:47 +0100643
644 irq = platform_get_irq(pdev, 0);
645 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0, dev_name(dev), priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700646 if (ret < 0) {
647 dev_err(dev, "cannot get irq %d\n", irq);
Wolfram Sange43e0df2015-11-19 16:56:41 +0100648 goto out_pm_disable;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700649 }
650
Wolfram Sang4f7effd2015-10-09 10:39:25 +0100651 platform_set_drvdata(pdev, priv);
652
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700653 ret = i2c_add_numbered_adapter(adap);
654 if (ret < 0) {
655 dev_err(dev, "reg adap failed: %d\n", ret);
Wolfram Sange43e0df2015-11-19 16:56:41 +0100656 goto out_pm_disable;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700657 }
658
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700659 dev_info(dev, "probed\n");
660
661 return 0;
Wolfram Sange43e0df2015-11-19 16:56:41 +0100662
663 out_pm_put:
664 pm_runtime_put(dev);
665 out_pm_disable:
666 pm_runtime_disable(dev);
667 return ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700668}
669
Bill Pemberton0b255e92012-11-27 15:59:38 -0500670static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700671{
672 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
673 struct device *dev = &pdev->dev;
674
675 i2c_del_adapter(&priv->adap);
Wolfram Sang7ee24eb2015-12-23 17:56:34 +0100676 if (priv->flags & ID_P_PM_BLOCKED)
677 pm_runtime_put(dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700678 pm_runtime_disable(dev);
679
680 return 0;
681}
682
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100683static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700684 .driver = {
685 .name = "i2c-rcar",
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200686 .of_match_table = rcar_i2c_dt_ids,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700687 },
688 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500689 .remove = rcar_i2c_remove,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700690};
691
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100692module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700693
Wolfram Sang3d99bea2014-05-28 09:44:46 +0200694MODULE_LICENSE("GPL v2");
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700695MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
696MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");