blob: d4322a9096786f32bb522d2bf4414815872c10e4 [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)
99
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900100enum rcar_i2c_type {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700101 I2C_RCAR_GEN1,
102 I2C_RCAR_GEN2,
Wolfram Sange7db0d32015-08-05 15:18:25 +0200103 I2C_RCAR_GEN3,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900104};
105
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700106struct rcar_i2c_priv {
107 void __iomem *io;
108 struct i2c_adapter adap;
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100109 struct i2c_msg *msg;
110 int msgs_left;
Ben Dooksbc8120f2014-01-26 16:05:35 +0000111 struct clk *clk;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700112
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700113 wait_queue_head_t wait;
114
115 int pos;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700116 u32 icccr;
117 u32 flags;
Wolfram Sang51371cd2014-05-28 09:44:45 +0200118 enum rcar_i2c_type devtype;
Wolfram Sangde20d182014-11-18 17:04:55 +0100119 struct i2c_client *slave;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700120};
121
122#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
123#define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
124
125#define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
126#define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
127
128#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 Sangf9c9d312015-12-08 10:37:47 +0100165static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, u32 bus_speed)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700166{
Wolfram Sangf9c9d312015-12-08 10:37:47 +0100167 u32 scgd, cdf, round, ick, 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
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900171 switch (priv->devtype) {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700172 case I2C_RCAR_GEN1:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900173 cdf_width = 2;
174 break;
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700175 case I2C_RCAR_GEN2:
Wolfram Sange7db0d32015-08-05 15:18:25 +0200176 case I2C_RCAR_GEN3:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900177 cdf_width = 3;
178 break;
179 default:
180 dev_err(dev, "device type error\n");
181 return -EIO;
182 }
183
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700184 /*
185 * calculate SCL clock
186 * see
187 * ICCCR
188 *
189 * ick = clkp / (1 + CDF)
190 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
191 *
192 * ick : I2C internal clock < 20 MHz
193 * ticf : I2C SCL falling time = 35 ns here
194 * tr : I2C SCL rising time = 200 ns here
195 * intd : LSI internal delay = 50 ns here
196 * clkp : peripheral_clk
197 * F[] : integer up-valuation
198 */
Ben Dooksbc8120f2014-01-26 16:05:35 +0000199 rate = clk_get_rate(priv->clk);
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200200 cdf = rate / 20000000;
Wolfram Sang22762cc2014-09-20 12:07:37 +0200201 if (cdf >= 1U << cdf_width) {
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200202 dev_err(dev, "Input clock %lu too high\n", rate);
203 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700204 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200205 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700206
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700207 /*
208 * it is impossible to calculate large scale
209 * number on u32. separate it
210 *
211 * F[(ticf + tr + intd) * ick]
212 * = F[(35 + 200 + 50)ns * ick]
213 * = F[285 * ick / 1000000000]
214 * = F[(ick / 1000000) * 285 / 1000]
215 */
216 round = (ick + 500000) / 1000000 * 285;
217 round = (round + 500) / 1000;
218
219 /*
220 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
221 *
222 * Calculation result (= SCL) should be less than
223 * bus_speed for hardware safety
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200224 *
225 * We could use something along the lines of
226 * div = ick / (bus_speed + 1) + 1;
227 * scgd = (div - 20 - round + 7) / 8;
228 * scl = ick / (20 + (scgd * 8) + round);
229 * (not fully verified) but that would get pretty involved
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700230 */
231 for (scgd = 0; scgd < 0x40; scgd++) {
232 scl = ick / (20 + (scgd * 8) + round);
233 if (scl <= bus_speed)
234 goto scgd_find;
235 }
236 dev_err(dev, "it is impossible to calculate best SCL\n");
237 return -EIO;
238
239scgd_find:
240 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
Ben Dooksbc8120f2014-01-26 16:05:35 +0000241 scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700242
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100243 /* keep icccr value */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200244 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700245
246 return 0;
247}
248
Sergei Shtylyov7c7117f2014-09-15 00:15:46 +0400249static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700250{
Wolfram Sang386babf2014-05-28 09:44:41 +0200251 int read = !!rcar_i2c_is_recv(priv);
252
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100253 priv->pos = 0;
Wolfram Sangb9d0684c2015-11-19 16:56:45 +0100254 if (priv->msgs_left == 1)
255 rcar_i2c_flags_set(priv, ID_LAST_MSG);
256
Wolfram Sang386babf2014-05-28 09:44:41 +0200257 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
Wolfram Sange49865d2015-11-19 16:56:51 +0100258 /*
259 * We don't have a testcase but the HW engineers say that the write order
260 * of ICMSR and ICMCR depends on whether we issue START or REP_START. Since
261 * it didn't cause a drawback for me, let's rather be safe than sorry.
262 */
263 if (rcar_i2c_flags_has(priv, ID_FIRST_MSG)) {
264 rcar_i2c_write(priv, ICMSR, 0);
265 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
266 } else {
267 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
268 rcar_i2c_write(priv, ICMSR, 0);
269 }
Wolfram Sang386babf2014-05-28 09:44:41 +0200270 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700271}
272
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100273static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
274{
275 priv->msg++;
276 priv->msgs_left--;
Wolfram Sange49865d2015-11-19 16:56:51 +0100277 priv->flags = 0;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100278 rcar_i2c_prepare_msg(priv);
279}
280
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700281/*
282 * interrupt functions
283 */
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100284static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700285{
286 struct i2c_msg *msg = priv->msg;
287
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100288 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700289 if (!(msr & MDE))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100290 return;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700291
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700292 if (priv->pos < msg->len) {
293 /*
294 * Prepare next data to ICRXTX register.
295 * This data will go to _SHIFT_ register.
296 *
297 * *
298 * [ICRXTX] -> [SHIFT] -> [I2C bus]
299 */
300 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
301 priv->pos++;
302
303 } else {
304 /*
305 * The last data was pushed to ICRXTX on _PREV_ empty irq.
306 * It is on _SHIFT_ register, and will sent to I2C bus.
307 *
308 * *
309 * [ICRXTX] -> [SHIFT] -> [I2C bus]
310 */
311
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100312 if (priv->flags & ID_LAST_MSG) {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700313 /*
314 * If current msg is the _LAST_ msg,
315 * prepare stop condition here.
316 * ID_DONE will be set on STOP irq.
317 */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200318 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100319 } else {
320 rcar_i2c_next_msg(priv);
321 return;
322 }
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700323 }
324
Wolfram Sang3c95de62014-05-28 09:44:42 +0200325 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700326}
327
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100328static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700329{
330 struct i2c_msg *msg = priv->msg;
331
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100332 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700333 if (!(msr & MDR))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100334 return;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700335
336 if (msr & MAT) {
Wolfram Sang52df4452015-11-19 16:56:49 +0100337 /* Address transfer phase finished, but no data at this point. */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700338 } else if (priv->pos < msg->len) {
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100339 /* get received data */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700340 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
341 priv->pos++;
342 }
343
344 /*
Wolfram Sang3c2b1ff2015-11-19 16:56:50 +0100345 * If next received data is the _LAST_, go to STOP phase. Might be
346 * overwritten by REP START when setting up a new msg. Not elegant
347 * but the only stable sequence for REP START I have found so far.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700348 */
349 if (priv->pos + 1 >= msg->len)
Wolfram Sang4f443a82014-05-28 09:44:38 +0200350 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700351
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100352 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
353 rcar_i2c_next_msg(priv);
354 else
355 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700356}
357
Wolfram Sangde20d182014-11-18 17:04:55 +0100358static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
359{
360 u32 ssr_raw, ssr_filtered;
361 u8 value;
362
363 ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
364 ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
365
366 if (!ssr_filtered)
367 return false;
368
369 /* address detected */
370 if (ssr_filtered & SAR) {
371 /* read or write request */
372 if (ssr_raw & STM) {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100373 i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100374 rcar_i2c_write(priv, ICRXTX, value);
375 rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
376 } else {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100377 i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100378 rcar_i2c_read(priv, ICRXTX); /* dummy read */
379 rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
380 }
381
382 rcar_i2c_write(priv, ICSSR, ~SAR & 0xff);
383 }
384
385 /* master sent stop */
386 if (ssr_filtered & SSR) {
387 i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
388 rcar_i2c_write(priv, ICSIER, SAR | SSR);
389 rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
390 }
391
392 /* master wants to write to us */
393 if (ssr_filtered & SDR) {
394 int ret;
395
396 value = rcar_i2c_read(priv, ICRXTX);
Wolfram Sang5b77d162015-03-23 09:26:36 +0100397 ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100398 /* Send NACK in case of error */
399 rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
Wolfram Sangde20d182014-11-18 17:04:55 +0100400 rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
401 }
402
403 /* master wants to read from us */
404 if (ssr_filtered & SDE) {
Wolfram Sang5b77d162015-03-23 09:26:36 +0100405 i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
Wolfram Sangde20d182014-11-18 17:04:55 +0100406 rcar_i2c_write(priv, ICRXTX, value);
407 rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
408 }
409
410 return true;
411}
412
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700413static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
414{
415 struct rcar_i2c_priv *priv = ptr;
Wolfram Sang52df4452015-11-19 16:56:49 +0100416 u32 msr, val;
417
418 /* Clear START or STOP as soon as we can */
419 val = rcar_i2c_read(priv, ICMCR);
420 rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700421
Wolfram Sang1c176d52014-05-28 09:44:36 +0200422 msr = rcar_i2c_read(priv, ICMSR);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700423
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400424 /* Only handle interrupts that are currently enabled */
425 msr &= rcar_i2c_read(priv, ICMIER);
Sergei Shtylyovaa5beaf2014-09-15 00:20:19 +0400426 if (!msr) {
Wolfram Sangc3be0af12015-11-19 16:56:48 +0100427 if (rcar_i2c_slave_irq(priv))
428 return IRQ_HANDLED;
429
430 return IRQ_NONE;
Sergei Shtylyovaa5beaf2014-09-15 00:20:19 +0400431 }
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400432
Wolfram Sang51371cd2014-05-28 09:44:45 +0200433 /* Arbitration lost */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700434 if (msr & MAL) {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700435 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
436 goto out;
437 }
438
Wolfram Sang51371cd2014-05-28 09:44:45 +0200439 /* Nack */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700440 if (msr & MNR) {
Wolfram Sangd89667b2015-11-19 16:56:47 +0100441 /* HW automatically sends STOP after received NACK */
Wolfram Sangf2382242014-05-28 09:44:39 +0200442 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700443 rcar_i2c_flags_set(priv, ID_NACK);
444 goto out;
445 }
446
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400447 /* Stop */
448 if (msr & MST) {
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100449 priv->msgs_left--; /* The last message also made it */
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400450 rcar_i2c_flags_set(priv, ID_DONE);
451 goto out;
452 }
453
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700454 if (rcar_i2c_is_recv(priv))
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100455 rcar_i2c_irq_recv(priv, msr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700456 else
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100457 rcar_i2c_irq_send(priv, msr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700458
459out:
460 if (rcar_i2c_flags_has(priv, ID_DONE)) {
Wolfram Sangf2382242014-05-28 09:44:39 +0200461 rcar_i2c_write(priv, ICMIER, 0);
Wolfram Sang3c95de62014-05-28 09:44:42 +0200462 rcar_i2c_write(priv, ICMSR, 0);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700463 wake_up(&priv->wait);
464 }
465
Wolfram Sangc3be0af12015-11-19 16:56:48 +0100466 return IRQ_HANDLED;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700467}
468
469static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
470 struct i2c_msg *msgs,
471 int num)
472{
473 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
474 struct device *dev = rcar_i2c_priv_to_dev(priv);
Wolfram Sangb6763d02015-06-20 21:03:20 +0200475 int i, ret;
Wolfram Sangff2316b2015-11-19 16:56:44 +0100476 long time_left;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700477
478 pm_runtime_get_sync(dev);
479
Wolfram Sang3f7de222014-05-28 09:44:40 +0200480 ret = rcar_i2c_bus_barrier(priv);
481 if (ret < 0)
482 goto out;
483
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700484 for (i = 0; i < num; i++) {
Wolfram Sangd7653962014-05-05 18:36:21 +0200485 /* This HW can't send STOP after address phase */
486 if (msgs[i].len == 0) {
487 ret = -EOPNOTSUPP;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100488 goto out;
Wolfram Sangd7653962014-05-05 18:36:21 +0200489 }
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100490 }
Wolfram Sangd7653962014-05-05 18:36:21 +0200491
Wolfram Sange49865d2015-11-19 16:56:51 +0100492 /* init first message */
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100493 priv->msg = msgs;
494 priv->msgs_left = num;
Wolfram Sange49865d2015-11-19 16:56:51 +0100495 priv->flags = ID_FIRST_MSG;
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100496 rcar_i2c_prepare_msg(priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700497
Wolfram Sangcc21d0b2015-11-19 16:56:46 +0100498 time_left = wait_event_timeout(priv->wait,
499 rcar_i2c_flags_has(priv, ID_DONE),
500 num * adap->timeout);
501 if (!time_left) {
502 rcar_i2c_init(priv);
503 ret = -ETIMEDOUT;
504 } else if (rcar_i2c_flags_has(priv, ID_NACK)) {
505 ret = -ENXIO;
506 } else if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
507 ret = -EAGAIN;
508 } else {
509 ret = num - priv->msgs_left; /* The number of transfer */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700510 }
Wolfram Sang3f7de222014-05-28 09:44:40 +0200511out:
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700512 pm_runtime_put(dev);
513
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000514 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700515 dev_err(dev, "error %d : %x\n", ret, priv->flags);
516
517 return ret;
518}
519
Wolfram Sangde20d182014-11-18 17:04:55 +0100520static int rcar_reg_slave(struct i2c_client *slave)
521{
522 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
523
524 if (priv->slave)
525 return -EBUSY;
526
527 if (slave->flags & I2C_CLIENT_TEN)
528 return -EAFNOSUPPORT;
529
530 pm_runtime_forbid(rcar_i2c_priv_to_dev(priv));
531
532 priv->slave = slave;
533 rcar_i2c_write(priv, ICSAR, slave->addr);
534 rcar_i2c_write(priv, ICSSR, 0);
535 rcar_i2c_write(priv, ICSIER, SAR | SSR);
536 rcar_i2c_write(priv, ICSCR, SIE | SDBS);
537
538 return 0;
539}
540
541static int rcar_unreg_slave(struct i2c_client *slave)
542{
543 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
544
545 WARN_ON(!priv->slave);
546
547 rcar_i2c_write(priv, ICSIER, 0);
548 rcar_i2c_write(priv, ICSCR, 0);
549
550 priv->slave = NULL;
551
552 pm_runtime_allow(rcar_i2c_priv_to_dev(priv));
553
554 return 0;
555}
556
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700557static u32 rcar_i2c_func(struct i2c_adapter *adap)
558{
Wolfram Sangd7653962014-05-05 18:36:21 +0200559 /* This HW can't do SMBUS_QUICK and NOSTART */
Wolfram Sang1fb2ad92015-05-14 14:40:03 +0200560 return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
561 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700562}
563
564static const struct i2c_algorithm rcar_i2c_algo = {
565 .master_xfer = rcar_i2c_master_xfer,
566 .functionality = rcar_i2c_func,
Wolfram Sangde20d182014-11-18 17:04:55 +0100567 .reg_slave = rcar_reg_slave,
568 .unreg_slave = rcar_unreg_slave,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700569};
570
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200571static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700572 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
573 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
574 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
575 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange8936452014-02-20 09:03:20 +0100576 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +0200577 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
578 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
579 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange7db0d32015-08-05 15:18:25 +0200580 { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200581 {},
582};
583MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
584
Bill Pemberton0b255e92012-11-27 15:59:38 -0500585static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700586{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700587 struct rcar_i2c_priv *priv;
588 struct i2c_adapter *adap;
589 struct resource *res;
590 struct device *dev = &pdev->dev;
591 u32 bus_speed;
Wolfram Sang93e953d2014-05-28 09:44:37 +0200592 int irq, ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700593
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700594 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900595 if (!priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700596 return -ENOMEM;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700597
Ben Dooksbc8120f2014-01-26 16:05:35 +0000598 priv->clk = devm_clk_get(dev, NULL);
599 if (IS_ERR(priv->clk)) {
600 dev_err(dev, "cannot get clock\n");
601 return PTR_ERR(priv->clk);
602 }
603
Wolfram Sange43e0df2015-11-19 16:56:41 +0100604 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
605 priv->io = devm_ioremap_resource(dev, res);
606 if (IS_ERR(priv->io))
607 return PTR_ERR(priv->io);
608
Geert Uytterhoevenc6f18912015-10-07 10:16:31 +0200609 priv->devtype = (enum rcar_i2c_type)of_match_device(rcar_i2c_dt_ids, dev)->data;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700610 init_waitqueue_head(&priv->wait);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700611
Wolfram Sang929e3aba2014-07-10 13:46:31 +0200612 adap = &priv->adap;
613 adap->nr = pdev->id;
614 adap->algo = &rcar_i2c_algo;
615 adap->class = I2C_CLASS_DEPRECATED;
616 adap->retries = 3;
617 adap->dev.parent = dev;
618 adap->dev.of_node = dev->of_node;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700619 i2c_set_adapdata(adap, priv);
620 strlcpy(adap->name, pdev->name, sizeof(adap->name));
621
Wolfram Sangf9c9d312015-12-08 10:37:47 +0100622 bus_speed = 100000; /* default 100 kHz */
623 of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
624
625 pm_runtime_enable(dev);
626 pm_runtime_get_sync(dev);
627 ret = rcar_i2c_clock_calculate(priv, bus_speed);
628 if (ret < 0)
629 goto out_pm_put;
630
631 rcar_i2c_init(priv);
632 pm_runtime_put(dev);
633
634 irq = platform_get_irq(pdev, 0);
635 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0, dev_name(dev), priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700636 if (ret < 0) {
Wolfram Sang93e953d2014-05-28 09:44:37 +0200637 dev_err(dev, "cannot get irq %d\n", irq);
Wolfram Sange43e0df2015-11-19 16:56:41 +0100638 goto out_pm_disable;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700639 }
640
Wolfram Sang4f7effd2015-10-09 10:39:25 +0100641 platform_set_drvdata(pdev, priv);
642
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700643 ret = i2c_add_numbered_adapter(adap);
644 if (ret < 0) {
645 dev_err(dev, "reg adap failed: %d\n", ret);
Wolfram Sange43e0df2015-11-19 16:56:41 +0100646 goto out_pm_disable;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700647 }
648
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700649 dev_info(dev, "probed\n");
650
651 return 0;
Wolfram Sange43e0df2015-11-19 16:56:41 +0100652
653 out_pm_put:
654 pm_runtime_put(dev);
655 out_pm_disable:
656 pm_runtime_disable(dev);
657 return ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700658}
659
Bill Pemberton0b255e92012-11-27 15:59:38 -0500660static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700661{
662 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
663 struct device *dev = &pdev->dev;
664
665 i2c_del_adapter(&priv->adap);
666 pm_runtime_disable(dev);
667
668 return 0;
669}
670
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100671static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700672 .driver = {
673 .name = "i2c-rcar",
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200674 .of_match_table = rcar_i2c_dt_ids,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700675 },
676 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500677 .remove = rcar_i2c_remove,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700678};
679
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100680module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700681
Wolfram Sang3d99bea2014-05-28 09:44:46 +0200682MODULE_LICENSE("GPL v2");
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700683MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
684MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");