blob: f2cbb8a7d0ba1a2dc9ab2e52e660356998ff4221 [file] [log] [blame]
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -07001/*
2 * drivers/i2c/busses/i2c-rcar.c
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
8 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
9 *
10 * This file used out-of-tree driver i2c-rcar.c
11 * Copyright (C) 2011-2012 Renesas Electronics Corporation
12 *
13 * 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
15 * the Free Software Foundation; either version 2 of the License
16 *
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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26#include <linux/clk.h>
27#include <linux/delay.h>
28#include <linux/err.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070029#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/i2c.h>
32#include <linux/i2c/i2c-rcar.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +020035#include <linux/of_device.h>
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070036#include <linux/platform_device.h>
37#include <linux/pm_runtime.h>
38#include <linux/slab.h>
39#include <linux/spinlock.h>
40
41/* register offsets */
42#define ICSCR 0x00 /* slave ctrl */
43#define ICMCR 0x04 /* master ctrl */
44#define ICSSR 0x08 /* slave status */
45#define ICMSR 0x0C /* master status */
46#define ICSIER 0x10 /* slave irq enable */
47#define ICMIER 0x14 /* master irq enable */
48#define ICCCR 0x18 /* clock dividers */
49#define ICSAR 0x1C /* slave address */
50#define ICMAR 0x20 /* master address */
51#define ICRXTX 0x24 /* data port */
52
53/* 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
63/* ICMSR */
64#define MNR (1 << 6) /* nack received */
65#define MAL (1 << 5) /* arbitration lost */
66#define MST (1 << 4) /* sent a stop */
67#define MDE (1 << 3)
68#define MDT (1 << 2)
69#define MDR (1 << 1)
70#define MAT (1 << 0) /* slave addr xfer done */
71
72/* ICMIE */
73#define MNRE (1 << 6) /* nack irq en */
74#define MALE (1 << 5) /* arblos irq en */
75#define MSTE (1 << 4) /* stop irq en */
76#define MDEE (1 << 3)
77#define MDTE (1 << 2)
78#define MDRE (1 << 1)
79#define MATE (1 << 0) /* address sent irq en */
80
81
Wolfram Sang4f443a82014-05-28 09:44:38 +020082#define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
83#define RCAR_BUS_PHASE_DATA (MDBS | MIE)
84#define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070085
Wolfram Sangf2382242014-05-28 09:44:39 +020086#define RCAR_IRQ_SEND (MNRE | MALE | MSTE | MATE | MDEE)
87#define RCAR_IRQ_RECV (MNRE | MALE | MSTE | MATE | MDRE)
88#define RCAR_IRQ_STOP (MSTE)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -070089
90/*
91 * flags
92 */
93#define ID_LAST_MSG (1 << 0)
94#define ID_IOERROR (1 << 1)
95#define ID_DONE (1 << 2)
96#define ID_ARBLOST (1 << 3)
97#define ID_NACK (1 << 4)
98
Nguyen Viet Dungb7204232013-09-03 09:09:25 +090099enum rcar_i2c_type {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700100 I2C_RCAR_GEN1,
101 I2C_RCAR_GEN2,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900102};
103
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700104struct rcar_i2c_priv {
105 void __iomem *io;
106 struct i2c_adapter adap;
107 struct i2c_msg *msg;
Ben Dooksbc8120f2014-01-26 16:05:35 +0000108 struct clk *clk;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700109
110 spinlock_t lock;
111 wait_queue_head_t wait;
112
113 int pos;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700114 u32 icccr;
115 u32 flags;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900116 enum rcar_i2c_type devtype;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700117};
118
119#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
120#define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
121
122#define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
123#define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
124
125#define LOOP_TIMEOUT 1024
126
127/*
128 * basic functions
129 */
130static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
131{
132 writel(val, priv->io + reg);
133}
134
135static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
136{
137 return readl(priv->io + reg);
138}
139
140static void rcar_i2c_init(struct rcar_i2c_priv *priv)
141{
142 /*
143 * reset slave mode.
144 * slave mode is not used on this driver
145 */
146 rcar_i2c_write(priv, ICSIER, 0);
147 rcar_i2c_write(priv, ICSAR, 0);
148 rcar_i2c_write(priv, ICSCR, 0);
149 rcar_i2c_write(priv, ICSSR, 0);
150
151 /* reset master mode */
152 rcar_i2c_write(priv, ICMIER, 0);
153 rcar_i2c_write(priv, ICMCR, 0);
154 rcar_i2c_write(priv, ICMSR, 0);
155 rcar_i2c_write(priv, ICMAR, 0);
156}
157
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700158static void rcar_i2c_set_addr(struct rcar_i2c_priv *priv, u32 recv)
159{
160 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | recv);
161}
162
163/*
164 * bus control functions
165 */
166static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
167{
168 int i;
169
170 for (i = 0; i < LOOP_TIMEOUT; i++) {
171 /* make sure that bus is not busy */
172 if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
173 return 0;
174 udelay(1);
175 }
176
177 return -EBUSY;
178}
179
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700180/*
181 * clock function
182 */
183static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
184 u32 bus_speed,
185 struct device *dev)
186{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700187 u32 scgd, cdf;
188 u32 round, ick;
189 u32 scl;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900190 u32 cdf_width;
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200191 unsigned long rate;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700192
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900193 switch (priv->devtype) {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700194 case I2C_RCAR_GEN1:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900195 cdf_width = 2;
196 break;
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700197 case I2C_RCAR_GEN2:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900198 cdf_width = 3;
199 break;
200 default:
201 dev_err(dev, "device type error\n");
202 return -EIO;
203 }
204
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700205 /*
206 * calculate SCL clock
207 * see
208 * ICCCR
209 *
210 * ick = clkp / (1 + CDF)
211 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
212 *
213 * ick : I2C internal clock < 20 MHz
214 * ticf : I2C SCL falling time = 35 ns here
215 * tr : I2C SCL rising time = 200 ns here
216 * intd : LSI internal delay = 50 ns here
217 * clkp : peripheral_clk
218 * F[] : integer up-valuation
219 */
Ben Dooksbc8120f2014-01-26 16:05:35 +0000220 rate = clk_get_rate(priv->clk);
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200221 cdf = rate / 20000000;
222 if (cdf >= 1 << cdf_width) {
223 dev_err(dev, "Input clock %lu too high\n", rate);
224 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700225 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200226 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700227
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700228 /*
229 * it is impossible to calculate large scale
230 * number on u32. separate it
231 *
232 * F[(ticf + tr + intd) * ick]
233 * = F[(35 + 200 + 50)ns * ick]
234 * = F[285 * ick / 1000000000]
235 * = F[(ick / 1000000) * 285 / 1000]
236 */
237 round = (ick + 500000) / 1000000 * 285;
238 round = (round + 500) / 1000;
239
240 /*
241 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
242 *
243 * Calculation result (= SCL) should be less than
244 * bus_speed for hardware safety
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200245 *
246 * We could use something along the lines of
247 * div = ick / (bus_speed + 1) + 1;
248 * scgd = (div - 20 - round + 7) / 8;
249 * scl = ick / (20 + (scgd * 8) + round);
250 * (not fully verified) but that would get pretty involved
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700251 */
252 for (scgd = 0; scgd < 0x40; scgd++) {
253 scl = ick / (20 + (scgd * 8) + round);
254 if (scl <= bus_speed)
255 goto scgd_find;
256 }
257 dev_err(dev, "it is impossible to calculate best SCL\n");
258 return -EIO;
259
260scgd_find:
261 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
Ben Dooksbc8120f2014-01-26 16:05:35 +0000262 scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700263
264 /*
265 * keep icccr value
266 */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200267 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700268
269 return 0;
270}
271
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700272/*
273 * status functions
274 */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700275
276#define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
277static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
278{
279 rcar_i2c_write(priv, ICMSR, ~bit);
280}
281
282/*
283 * recv/send functions
284 */
285static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
286{
287 rcar_i2c_set_addr(priv, 1);
288 rcar_i2c_status_clear(priv);
Wolfram Sang4f443a82014-05-28 09:44:38 +0200289 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
Wolfram Sangf2382242014-05-28 09:44:39 +0200290 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_RECV);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700291
292 return 0;
293}
294
295static int rcar_i2c_send(struct rcar_i2c_priv *priv)
296{
297 int ret;
298
299 /*
300 * It should check bus status when send case
301 */
302 ret = rcar_i2c_bus_barrier(priv);
303 if (ret < 0)
304 return ret;
305
306 rcar_i2c_set_addr(priv, 0);
307 rcar_i2c_status_clear(priv);
Wolfram Sang4f443a82014-05-28 09:44:38 +0200308 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
Wolfram Sangf2382242014-05-28 09:44:39 +0200309 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_SEND);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700310
311 return 0;
312}
313
314#define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
315#define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
316
317/*
318 * interrupt functions
319 */
320static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
321{
322 struct i2c_msg *msg = priv->msg;
323
324 /*
325 * FIXME
326 * sometimes, unknown interrupt happened.
327 * Do nothing
328 */
329 if (!(msr & MDE))
330 return 0;
331
332 /*
333 * If address transfer phase finished,
334 * goto data phase.
335 */
336 if (msr & MAT)
Wolfram Sang4f443a82014-05-28 09:44:38 +0200337 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700338
339 if (priv->pos < msg->len) {
340 /*
341 * Prepare next data to ICRXTX register.
342 * This data will go to _SHIFT_ register.
343 *
344 * *
345 * [ICRXTX] -> [SHIFT] -> [I2C bus]
346 */
347 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
348 priv->pos++;
349
350 } else {
351 /*
352 * The last data was pushed to ICRXTX on _PREV_ empty irq.
353 * It is on _SHIFT_ register, and will sent to I2C bus.
354 *
355 * *
356 * [ICRXTX] -> [SHIFT] -> [I2C bus]
357 */
358
359 if (priv->flags & ID_LAST_MSG)
360 /*
361 * If current msg is the _LAST_ msg,
362 * prepare stop condition here.
363 * ID_DONE will be set on STOP irq.
364 */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200365 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700366 else
367 /*
368 * If current msg is _NOT_ last msg,
369 * it doesn't call stop phase.
370 * thus, there is no STOP irq.
371 * return ID_DONE here.
372 */
373 return ID_DONE;
374 }
375
376 rcar_i2c_send_restart(priv);
377
378 return 0;
379}
380
381static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
382{
383 struct i2c_msg *msg = priv->msg;
384
385 /*
386 * FIXME
387 * sometimes, unknown interrupt happened.
388 * Do nothing
389 */
390 if (!(msr & MDR))
391 return 0;
392
393 if (msr & MAT) {
394 /*
395 * Address transfer phase finished,
396 * but, there is no data at this point.
397 * Do nothing.
398 */
399 } else if (priv->pos < msg->len) {
400 /*
401 * get received data
402 */
403 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
404 priv->pos++;
405 }
406
407 /*
408 * If next received data is the _LAST_,
409 * go to STOP phase,
410 * otherwise, go to DATA phase.
411 */
412 if (priv->pos + 1 >= msg->len)
Wolfram Sang4f443a82014-05-28 09:44:38 +0200413 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700414 else
Wolfram Sang4f443a82014-05-28 09:44:38 +0200415 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700416
417 rcar_i2c_recv_restart(priv);
418
419 return 0;
420}
421
422static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
423{
424 struct rcar_i2c_priv *priv = ptr;
425 struct device *dev = rcar_i2c_priv_to_dev(priv);
426 u32 msr;
427
428 /*-------------- spin lock -----------------*/
429 spin_lock(&priv->lock);
430
Wolfram Sang1c176d52014-05-28 09:44:36 +0200431 msr = rcar_i2c_read(priv, ICMSR);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700432
433 /*
434 * Arbitration lost
435 */
436 if (msr & MAL) {
437 /*
438 * CAUTION
439 *
440 * When arbitration lost, device become _slave_ mode.
441 */
442 dev_dbg(dev, "Arbitration Lost\n");
443 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
444 goto out;
445 }
446
447 /*
448 * Stop
449 */
450 if (msr & MST) {
451 dev_dbg(dev, "Stop\n");
452 rcar_i2c_flags_set(priv, ID_DONE);
453 goto out;
454 }
455
456 /*
457 * Nack
458 */
459 if (msr & MNR) {
460 dev_dbg(dev, "Nack\n");
461
462 /* go to stop phase */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200463 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Wolfram Sangf2382242014-05-28 09:44:39 +0200464 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700465 rcar_i2c_flags_set(priv, ID_NACK);
466 goto out;
467 }
468
469 /*
470 * recv/send
471 */
472 if (rcar_i2c_is_recv(priv))
473 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
474 else
475 rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
476
477out:
478 if (rcar_i2c_flags_has(priv, ID_DONE)) {
Wolfram Sangf2382242014-05-28 09:44:39 +0200479 rcar_i2c_write(priv, ICMIER, 0);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700480 rcar_i2c_status_clear(priv);
481 wake_up(&priv->wait);
482 }
483
484 spin_unlock(&priv->lock);
485 /*-------------- spin unlock -----------------*/
486
487 return IRQ_HANDLED;
488}
489
490static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
491 struct i2c_msg *msgs,
492 int num)
493{
494 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
495 struct device *dev = rcar_i2c_priv_to_dev(priv);
496 unsigned long flags;
497 int i, ret, timeout;
498
499 pm_runtime_get_sync(dev);
500
501 /*-------------- spin lock -----------------*/
502 spin_lock_irqsave(&priv->lock, flags);
503
504 rcar_i2c_init(priv);
Wolfram Sang1c176d52014-05-28 09:44:36 +0200505 /* start clock */
506 rcar_i2c_write(priv, ICCCR, priv->icccr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700507
508 spin_unlock_irqrestore(&priv->lock, flags);
509 /*-------------- spin unlock -----------------*/
510
511 ret = -EINVAL;
512 for (i = 0; i < num; i++) {
Wolfram Sangd7653962014-05-05 18:36:21 +0200513 /* This HW can't send STOP after address phase */
514 if (msgs[i].len == 0) {
515 ret = -EOPNOTSUPP;
516 break;
517 }
518
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700519 /*-------------- spin lock -----------------*/
520 spin_lock_irqsave(&priv->lock, flags);
521
522 /* init each data */
523 priv->msg = &msgs[i];
524 priv->pos = 0;
525 priv->flags = 0;
526 if (priv->msg == &msgs[num - 1])
527 rcar_i2c_flags_set(priv, ID_LAST_MSG);
528
529 /* start send/recv */
530 if (rcar_i2c_is_recv(priv))
531 ret = rcar_i2c_recv(priv);
532 else
533 ret = rcar_i2c_send(priv);
534
535 spin_unlock_irqrestore(&priv->lock, flags);
536 /*-------------- spin unlock -----------------*/
537
538 if (ret < 0)
539 break;
540
541 /*
542 * wait result
543 */
544 timeout = wait_event_timeout(priv->wait,
545 rcar_i2c_flags_has(priv, ID_DONE),
546 5 * HZ);
547 if (!timeout) {
548 ret = -ETIMEDOUT;
549 break;
550 }
551
552 /*
553 * error handling
554 */
555 if (rcar_i2c_flags_has(priv, ID_NACK)) {
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000556 ret = -ENXIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700557 break;
558 }
559
560 if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
561 ret = -EAGAIN;
562 break;
563 }
564
565 if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
566 ret = -EIO;
567 break;
568 }
569
570 ret = i + 1; /* The number of transfer */
571 }
572
573 pm_runtime_put(dev);
574
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000575 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700576 dev_err(dev, "error %d : %x\n", ret, priv->flags);
577
578 return ret;
579}
580
581static u32 rcar_i2c_func(struct i2c_adapter *adap)
582{
Wolfram Sangd7653962014-05-05 18:36:21 +0200583 /* This HW can't do SMBUS_QUICK and NOSTART */
584 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700585}
586
587static const struct i2c_algorithm rcar_i2c_algo = {
588 .master_xfer = rcar_i2c_master_xfer,
589 .functionality = rcar_i2c_func,
590};
591
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200592static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700593 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
594 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
595 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
596 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange8936452014-02-20 09:03:20 +0100597 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +0200598 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
599 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
600 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200601 {},
602};
603MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
604
Bill Pemberton0b255e92012-11-27 15:59:38 -0500605static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700606{
Jingoo Han6d4028c2013-07-30 16:59:33 +0900607 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700608 struct rcar_i2c_priv *priv;
609 struct i2c_adapter *adap;
610 struct resource *res;
611 struct device *dev = &pdev->dev;
612 u32 bus_speed;
Wolfram Sang93e953d2014-05-28 09:44:37 +0200613 int irq, ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700614
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700615 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
616 if (!priv) {
617 dev_err(dev, "no mem for private data\n");
618 return -ENOMEM;
619 }
620
Ben Dooksbc8120f2014-01-26 16:05:35 +0000621 priv->clk = devm_clk_get(dev, NULL);
622 if (IS_ERR(priv->clk)) {
623 dev_err(dev, "cannot get clock\n");
624 return PTR_ERR(priv->clk);
625 }
626
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700627 bus_speed = 100000; /* default 100 kHz */
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200628 ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
629 if (ret < 0 && pdata && pdata->bus_speed)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700630 bus_speed = pdata->bus_speed;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900631
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200632 if (pdev->dev.of_node)
633 priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
634 dev)->data;
635 else
636 priv->devtype = platform_get_device_id(pdev)->driver_data;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900637
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700638 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
639 if (ret < 0)
640 return ret;
641
Wolfram Sang3cc2d002013-05-10 10:16:54 +0200642 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100643 priv->io = devm_ioremap_resource(dev, res);
644 if (IS_ERR(priv->io))
645 return PTR_ERR(priv->io);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700646
Wolfram Sang93e953d2014-05-28 09:44:37 +0200647 irq = platform_get_irq(pdev, 0);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700648 init_waitqueue_head(&priv->wait);
649 spin_lock_init(&priv->lock);
650
651 adap = &priv->adap;
652 adap->nr = pdev->id;
653 adap->algo = &rcar_i2c_algo;
Wolfram Sang96c4b6b2014-02-10 11:04:06 +0100654 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD | I2C_CLASS_DEPRECATED;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700655 adap->retries = 3;
656 adap->dev.parent = dev;
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200657 adap->dev.of_node = dev->of_node;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700658 i2c_set_adapdata(adap, priv);
659 strlcpy(adap->name, pdev->name, sizeof(adap->name));
660
Wolfram Sang93e953d2014-05-28 09:44:37 +0200661 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700662 dev_name(dev), priv);
663 if (ret < 0) {
Wolfram Sang93e953d2014-05-28 09:44:37 +0200664 dev_err(dev, "cannot get irq %d\n", irq);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700665 return ret;
666 }
667
668 ret = i2c_add_numbered_adapter(adap);
669 if (ret < 0) {
670 dev_err(dev, "reg adap failed: %d\n", ret);
671 return ret;
672 }
673
674 pm_runtime_enable(dev);
675 platform_set_drvdata(pdev, priv);
676
677 dev_info(dev, "probed\n");
678
679 return 0;
680}
681
Bill Pemberton0b255e92012-11-27 15:59:38 -0500682static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700683{
684 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
685 struct device *dev = &pdev->dev;
686
687 i2c_del_adapter(&priv->adap);
688 pm_runtime_disable(dev);
689
690 return 0;
691}
692
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900693static struct platform_device_id rcar_i2c_id_table[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700694 { "i2c-rcar", I2C_RCAR_GEN1 },
695 { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
696 { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900697 {},
698};
699MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
700
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100701static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700702 .driver = {
703 .name = "i2c-rcar",
704 .owner = THIS_MODULE,
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200705 .of_match_table = rcar_i2c_dt_ids,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700706 },
707 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500708 .remove = rcar_i2c_remove,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900709 .id_table = rcar_i2c_id_table,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700710};
711
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100712module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700713
714MODULE_LICENSE("GPL");
715MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
716MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");