blob: c49b33e99d6863078c7150c3e34b1d153c4f0da1 [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 Sang3d99bea2014-05-28 09:44:46 +02004 * Copyright (C) 2014 Wolfram Sang <wsa@sang-engineering.com>
5 *
6 * Copyright (C) 2012-14 Renesas Solutions Corp.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07007 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 *
9 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
10 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
11 *
12 * This file used out-of-tree driver i2c-rcar.c
13 * Copyright (C) 2011-2012 Renesas Electronics Corporation
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
Wolfram Sang3d99bea2014-05-28 09:44:46 +020017 * the Free Software Foundation; version 2 of the License.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070018 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070023 */
24#include <linux/clk.h>
25#include <linux/delay.h>
26#include <linux/err.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070027#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/i2c.h>
30#include <linux/i2c/i2c-rcar.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +020033#include <linux/of_device.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070034#include <linux/platform_device.h>
35#include <linux/pm_runtime.h>
36#include <linux/slab.h>
Sergei Shtylyov91bfe292014-08-24 00:44:09 +040037#include <linux/spinlock.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070038
39/* register offsets */
40#define ICSCR 0x00 /* slave ctrl */
41#define ICMCR 0x04 /* master ctrl */
42#define ICSSR 0x08 /* slave status */
43#define ICMSR 0x0C /* master status */
44#define ICSIER 0x10 /* slave irq enable */
45#define ICMIER 0x14 /* master irq enable */
46#define ICCCR 0x18 /* clock dividers */
47#define ICSAR 0x1C /* slave address */
48#define ICMAR 0x20 /* master address */
49#define ICRXTX 0x24 /* data port */
50
51/* ICMCR */
52#define MDBS (1 << 7) /* non-fifo mode switch */
53#define FSCL (1 << 6) /* override SCL pin */
54#define FSDA (1 << 5) /* override SDA pin */
55#define OBPC (1 << 4) /* override pins */
56#define MIE (1 << 3) /* master if enable */
57#define TSBE (1 << 2)
58#define FSB (1 << 1) /* force stop bit */
59#define ESG (1 << 0) /* en startbit gen */
60
Wolfram Sang3e3aaba2014-05-28 09:44:44 +020061/* ICMSR (also for ICMIE) */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070062#define MNR (1 << 6) /* nack received */
63#define MAL (1 << 5) /* arbitration lost */
64#define MST (1 << 4) /* sent a stop */
65#define MDE (1 << 3)
66#define MDT (1 << 2)
67#define MDR (1 << 1)
68#define MAT (1 << 0) /* slave addr xfer done */
69
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070070
Wolfram Sang4f443a82014-05-28 09:44:38 +020071#define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
72#define RCAR_BUS_PHASE_DATA (MDBS | MIE)
73#define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070074
Wolfram Sang3e3aaba2014-05-28 09:44:44 +020075#define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
76#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
77#define RCAR_IRQ_STOP (MST)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070078
Sergei Shtylyov938916f2014-09-06 03:34:32 +040079#define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
80#define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
Wolfram Sang3c95de62014-05-28 09:44:42 +020081
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070082#define ID_LAST_MSG (1 << 0)
83#define ID_IOERROR (1 << 1)
84#define ID_DONE (1 << 2)
85#define ID_ARBLOST (1 << 3)
86#define ID_NACK (1 << 4)
87
Nguyen Viet Dungb7204232013-09-03 09:09:25 +090088enum rcar_i2c_type {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -070089 I2C_RCAR_GEN1,
90 I2C_RCAR_GEN2,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +090091};
92
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070093struct rcar_i2c_priv {
94 void __iomem *io;
95 struct i2c_adapter adap;
96 struct i2c_msg *msg;
Ben Dooksbc8120f2014-01-26 16:05:35 +000097 struct clk *clk;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070098
Sergei Shtylyov91bfe292014-08-24 00:44:09 +040099 spinlock_t lock;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700100 wait_queue_head_t wait;
101
102 int pos;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700103 u32 icccr;
104 u32 flags;
Wolfram Sang51371cd2014-05-28 09:44:45 +0200105 enum rcar_i2c_type devtype;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700106};
107
108#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
109#define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
110
111#define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
112#define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
113
114#define LOOP_TIMEOUT 1024
115
Wolfram Sang51371cd2014-05-28 09:44:45 +0200116
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700117static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
118{
119 writel(val, priv->io + reg);
120}
121
122static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
123{
124 return readl(priv->io + reg);
125}
126
127static void rcar_i2c_init(struct rcar_i2c_priv *priv)
128{
129 /*
130 * reset slave mode.
131 * slave mode is not used on this driver
132 */
133 rcar_i2c_write(priv, ICSIER, 0);
134 rcar_i2c_write(priv, ICSAR, 0);
135 rcar_i2c_write(priv, ICSCR, 0);
136 rcar_i2c_write(priv, ICSSR, 0);
137
138 /* reset master mode */
139 rcar_i2c_write(priv, ICMIER, 0);
140 rcar_i2c_write(priv, ICMCR, 0);
141 rcar_i2c_write(priv, ICMSR, 0);
142 rcar_i2c_write(priv, ICMAR, 0);
143}
144
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700145static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
146{
147 int i;
148
149 for (i = 0; i < LOOP_TIMEOUT; i++) {
150 /* make sure that bus is not busy */
151 if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
152 return 0;
153 udelay(1);
154 }
155
156 return -EBUSY;
157}
158
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700159static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
160 u32 bus_speed,
161 struct device *dev)
162{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700163 u32 scgd, cdf;
164 u32 round, ick;
165 u32 scl;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900166 u32 cdf_width;
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200167 unsigned long rate;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700168
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900169 switch (priv->devtype) {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700170 case I2C_RCAR_GEN1:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900171 cdf_width = 2;
172 break;
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700173 case I2C_RCAR_GEN2:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900174 cdf_width = 3;
175 break;
176 default:
177 dev_err(dev, "device type error\n");
178 return -EIO;
179 }
180
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700181 /*
182 * calculate SCL clock
183 * see
184 * ICCCR
185 *
186 * ick = clkp / (1 + CDF)
187 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
188 *
189 * ick : I2C internal clock < 20 MHz
190 * ticf : I2C SCL falling time = 35 ns here
191 * tr : I2C SCL rising time = 200 ns here
192 * intd : LSI internal delay = 50 ns here
193 * clkp : peripheral_clk
194 * F[] : integer up-valuation
195 */
Ben Dooksbc8120f2014-01-26 16:05:35 +0000196 rate = clk_get_rate(priv->clk);
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200197 cdf = rate / 20000000;
198 if (cdf >= 1 << cdf_width) {
199 dev_err(dev, "Input clock %lu too high\n", rate);
200 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700201 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200202 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700203
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700204 /*
205 * it is impossible to calculate large scale
206 * number on u32. separate it
207 *
208 * F[(ticf + tr + intd) * ick]
209 * = F[(35 + 200 + 50)ns * ick]
210 * = F[285 * ick / 1000000000]
211 * = F[(ick / 1000000) * 285 / 1000]
212 */
213 round = (ick + 500000) / 1000000 * 285;
214 round = (round + 500) / 1000;
215
216 /*
217 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
218 *
219 * Calculation result (= SCL) should be less than
220 * bus_speed for hardware safety
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200221 *
222 * We could use something along the lines of
223 * div = ick / (bus_speed + 1) + 1;
224 * scgd = (div - 20 - round + 7) / 8;
225 * scl = ick / (20 + (scgd * 8) + round);
226 * (not fully verified) but that would get pretty involved
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700227 */
228 for (scgd = 0; scgd < 0x40; scgd++) {
229 scl = ick / (20 + (scgd * 8) + round);
230 if (scl <= bus_speed)
231 goto scgd_find;
232 }
233 dev_err(dev, "it is impossible to calculate best SCL\n");
234 return -EIO;
235
236scgd_find:
237 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
Ben Dooksbc8120f2014-01-26 16:05:35 +0000238 scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700239
240 /*
241 * keep icccr value
242 */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200243 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700244
245 return 0;
246}
247
Sergei Shtylyov7c7117f2014-09-15 00:15:46 +0400248static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700249{
Wolfram Sang386babf2014-05-28 09:44:41 +0200250 int read = !!rcar_i2c_is_recv(priv);
251
252 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
Wolfram Sang3c95de62014-05-28 09:44:42 +0200253 rcar_i2c_write(priv, ICMSR, 0);
Wolfram Sang4f443a82014-05-28 09:44:38 +0200254 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
Wolfram Sang386babf2014-05-28 09:44:41 +0200255 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700256}
257
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700258/*
259 * interrupt functions
260 */
261static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
262{
263 struct i2c_msg *msg = priv->msg;
264
265 /*
266 * FIXME
267 * sometimes, unknown interrupt happened.
268 * Do nothing
269 */
270 if (!(msr & MDE))
271 return 0;
272
273 /*
274 * If address transfer phase finished,
275 * goto data phase.
276 */
277 if (msr & MAT)
Wolfram Sang4f443a82014-05-28 09:44:38 +0200278 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700279
280 if (priv->pos < msg->len) {
281 /*
282 * Prepare next data to ICRXTX register.
283 * This data will go to _SHIFT_ register.
284 *
285 * *
286 * [ICRXTX] -> [SHIFT] -> [I2C bus]
287 */
288 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
289 priv->pos++;
290
291 } else {
292 /*
293 * The last data was pushed to ICRXTX on _PREV_ empty irq.
294 * It is on _SHIFT_ register, and will sent to I2C bus.
295 *
296 * *
297 * [ICRXTX] -> [SHIFT] -> [I2C bus]
298 */
299
300 if (priv->flags & ID_LAST_MSG)
301 /*
302 * If current msg is the _LAST_ msg,
303 * prepare stop condition here.
304 * ID_DONE will be set on STOP irq.
305 */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200306 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700307 else
308 /*
309 * If current msg is _NOT_ last msg,
310 * it doesn't call stop phase.
311 * thus, there is no STOP irq.
312 * return ID_DONE here.
313 */
314 return ID_DONE;
315 }
316
Wolfram Sang3c95de62014-05-28 09:44:42 +0200317 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700318
319 return 0;
320}
321
322static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
323{
324 struct i2c_msg *msg = priv->msg;
325
326 /*
327 * FIXME
328 * sometimes, unknown interrupt happened.
329 * Do nothing
330 */
331 if (!(msr & MDR))
332 return 0;
333
334 if (msr & MAT) {
335 /*
336 * Address transfer phase finished,
337 * but, there is no data at this point.
338 * Do nothing.
339 */
340 } else if (priv->pos < msg->len) {
341 /*
342 * get received data
343 */
344 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
345 priv->pos++;
346 }
347
348 /*
349 * If next received data is the _LAST_,
350 * go to STOP phase,
351 * otherwise, go to DATA phase.
352 */
353 if (priv->pos + 1 >= msg->len)
Wolfram Sang4f443a82014-05-28 09:44:38 +0200354 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700355 else
Wolfram Sang4f443a82014-05-28 09:44:38 +0200356 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700357
Wolfram Sang3c95de62014-05-28 09:44:42 +0200358 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700359
360 return 0;
361}
362
363static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
364{
365 struct rcar_i2c_priv *priv = ptr;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700366 u32 msr;
367
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400368 /*-------------- spin lock -----------------*/
369 spin_lock(&priv->lock);
370
Wolfram Sang1c176d52014-05-28 09:44:36 +0200371 msr = rcar_i2c_read(priv, ICMSR);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700372
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400373 /* Only handle interrupts that are currently enabled */
374 msr &= rcar_i2c_read(priv, ICMIER);
375
Wolfram Sang51371cd2014-05-28 09:44:45 +0200376 /* Arbitration lost */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700377 if (msr & MAL) {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700378 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
379 goto out;
380 }
381
Wolfram Sang51371cd2014-05-28 09:44:45 +0200382 /* Nack */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700383 if (msr & MNR) {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700384 /* go to stop phase */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200385 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Wolfram Sangf2382242014-05-28 09:44:39 +0200386 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700387 rcar_i2c_flags_set(priv, ID_NACK);
388 goto out;
389 }
390
Sergei Shtylyovdd318b02014-09-02 01:15:26 +0400391 /* Stop */
392 if (msr & MST) {
393 rcar_i2c_flags_set(priv, ID_DONE);
394 goto out;
395 }
396
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700397 if (rcar_i2c_is_recv(priv))
398 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
399 else
400 rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
401
402out:
403 if (rcar_i2c_flags_has(priv, ID_DONE)) {
Wolfram Sangf2382242014-05-28 09:44:39 +0200404 rcar_i2c_write(priv, ICMIER, 0);
Wolfram Sang3c95de62014-05-28 09:44:42 +0200405 rcar_i2c_write(priv, ICMSR, 0);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700406 wake_up(&priv->wait);
407 }
408
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400409 spin_unlock(&priv->lock);
410 /*-------------- spin unlock -----------------*/
411
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700412 return IRQ_HANDLED;
413}
414
415static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
416 struct i2c_msg *msgs,
417 int num)
418{
419 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
420 struct device *dev = rcar_i2c_priv_to_dev(priv);
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400421 unsigned long flags;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700422 int i, ret, timeout;
423
424 pm_runtime_get_sync(dev);
425
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400426 /*-------------- spin lock -----------------*/
427 spin_lock_irqsave(&priv->lock, flags);
428
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700429 rcar_i2c_init(priv);
Wolfram Sang1c176d52014-05-28 09:44:36 +0200430 /* start clock */
431 rcar_i2c_write(priv, ICCCR, priv->icccr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700432
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400433 spin_unlock_irqrestore(&priv->lock, flags);
434 /*-------------- spin unlock -----------------*/
435
Wolfram Sang3f7de222014-05-28 09:44:40 +0200436 ret = rcar_i2c_bus_barrier(priv);
437 if (ret < 0)
438 goto out;
439
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700440 for (i = 0; i < num; i++) {
Wolfram Sangd7653962014-05-05 18:36:21 +0200441 /* This HW can't send STOP after address phase */
442 if (msgs[i].len == 0) {
443 ret = -EOPNOTSUPP;
444 break;
445 }
446
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400447 /*-------------- spin lock -----------------*/
448 spin_lock_irqsave(&priv->lock, flags);
449
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700450 /* init each data */
451 priv->msg = &msgs[i];
452 priv->pos = 0;
453 priv->flags = 0;
Sergei Shtylyovc30d7662014-09-15 00:14:14 +0400454 if (i == num - 1)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700455 rcar_i2c_flags_set(priv, ID_LAST_MSG);
456
Sergei Shtylyov7c7117f2014-09-15 00:15:46 +0400457 rcar_i2c_prepare_msg(priv);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700458
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400459 spin_unlock_irqrestore(&priv->lock, flags);
460 /*-------------- spin unlock -----------------*/
461
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700462 timeout = wait_event_timeout(priv->wait,
463 rcar_i2c_flags_has(priv, ID_DONE),
464 5 * HZ);
465 if (!timeout) {
466 ret = -ETIMEDOUT;
467 break;
468 }
469
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700470 if (rcar_i2c_flags_has(priv, ID_NACK)) {
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000471 ret = -ENXIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700472 break;
473 }
474
475 if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
476 ret = -EAGAIN;
477 break;
478 }
479
480 if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
481 ret = -EIO;
482 break;
483 }
484
485 ret = i + 1; /* The number of transfer */
486 }
Wolfram Sang3f7de222014-05-28 09:44:40 +0200487out:
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700488 pm_runtime_put(dev);
489
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000490 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700491 dev_err(dev, "error %d : %x\n", ret, priv->flags);
492
493 return ret;
494}
495
496static u32 rcar_i2c_func(struct i2c_adapter *adap)
497{
Wolfram Sangd7653962014-05-05 18:36:21 +0200498 /* This HW can't do SMBUS_QUICK and NOSTART */
499 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700500}
501
502static const struct i2c_algorithm rcar_i2c_algo = {
503 .master_xfer = rcar_i2c_master_xfer,
504 .functionality = rcar_i2c_func,
505};
506
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200507static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700508 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
509 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
510 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
511 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange8936452014-02-20 09:03:20 +0100512 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +0200513 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
514 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
515 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200516 {},
517};
518MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
519
Bill Pemberton0b255e92012-11-27 15:59:38 -0500520static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700521{
Jingoo Han6d4028c2013-07-30 16:59:33 +0900522 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700523 struct rcar_i2c_priv *priv;
524 struct i2c_adapter *adap;
525 struct resource *res;
526 struct device *dev = &pdev->dev;
527 u32 bus_speed;
Wolfram Sang93e953d2014-05-28 09:44:37 +0200528 int irq, ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700529
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700530 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900531 if (!priv)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700532 return -ENOMEM;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700533
Ben Dooksbc8120f2014-01-26 16:05:35 +0000534 priv->clk = devm_clk_get(dev, NULL);
535 if (IS_ERR(priv->clk)) {
536 dev_err(dev, "cannot get clock\n");
537 return PTR_ERR(priv->clk);
538 }
539
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700540 bus_speed = 100000; /* default 100 kHz */
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200541 ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
542 if (ret < 0 && pdata && pdata->bus_speed)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700543 bus_speed = pdata->bus_speed;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900544
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200545 if (pdev->dev.of_node)
546 priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
547 dev)->data;
548 else
549 priv->devtype = platform_get_device_id(pdev)->driver_data;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900550
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700551 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
552 if (ret < 0)
553 return ret;
554
Wolfram Sang3cc2d002013-05-10 10:16:54 +0200555 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100556 priv->io = devm_ioremap_resource(dev, res);
557 if (IS_ERR(priv->io))
558 return PTR_ERR(priv->io);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700559
Wolfram Sang93e953d2014-05-28 09:44:37 +0200560 irq = platform_get_irq(pdev, 0);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700561 init_waitqueue_head(&priv->wait);
Sergei Shtylyov91bfe292014-08-24 00:44:09 +0400562 spin_lock_init(&priv->lock);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700563
Wolfram Sang929e3aba2014-07-10 13:46:31 +0200564 adap = &priv->adap;
565 adap->nr = pdev->id;
566 adap->algo = &rcar_i2c_algo;
567 adap->class = I2C_CLASS_DEPRECATED;
568 adap->retries = 3;
569 adap->dev.parent = dev;
570 adap->dev.of_node = dev->of_node;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700571 i2c_set_adapdata(adap, priv);
572 strlcpy(adap->name, pdev->name, sizeof(adap->name));
573
Wolfram Sang93e953d2014-05-28 09:44:37 +0200574 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700575 dev_name(dev), priv);
576 if (ret < 0) {
Wolfram Sang93e953d2014-05-28 09:44:37 +0200577 dev_err(dev, "cannot get irq %d\n", irq);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700578 return ret;
579 }
580
581 ret = i2c_add_numbered_adapter(adap);
582 if (ret < 0) {
583 dev_err(dev, "reg adap failed: %d\n", ret);
584 return ret;
585 }
586
587 pm_runtime_enable(dev);
588 platform_set_drvdata(pdev, priv);
589
590 dev_info(dev, "probed\n");
591
592 return 0;
593}
594
Bill Pemberton0b255e92012-11-27 15:59:38 -0500595static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700596{
597 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
598 struct device *dev = &pdev->dev;
599
600 i2c_del_adapter(&priv->adap);
601 pm_runtime_disable(dev);
602
603 return 0;
604}
605
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900606static struct platform_device_id rcar_i2c_id_table[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700607 { "i2c-rcar", I2C_RCAR_GEN1 },
608 { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
609 { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900610 {},
611};
612MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
613
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100614static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700615 .driver = {
616 .name = "i2c-rcar",
617 .owner = THIS_MODULE,
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200618 .of_match_table = rcar_i2c_dt_ids,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700619 },
620 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500621 .remove = rcar_i2c_remove,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900622 .id_table = rcar_i2c_id_table,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700623};
624
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100625module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700626
Wolfram Sang3d99bea2014-05-28 09:44:46 +0200627MODULE_LICENSE("GPL v2");
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700628MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
629MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");