blob: 5a3e8a12e8d586a2d974ed71260f5a83476798d7 [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
82enum {
83 RCAR_BUS_PHASE_ADDR,
84 RCAR_BUS_PHASE_DATA,
85 RCAR_BUS_PHASE_STOP,
86};
87
88enum {
89 RCAR_IRQ_CLOSE,
90 RCAR_IRQ_OPEN_FOR_SEND,
91 RCAR_IRQ_OPEN_FOR_RECV,
92 RCAR_IRQ_OPEN_FOR_STOP,
93};
94
95/*
96 * flags
97 */
98#define ID_LAST_MSG (1 << 0)
99#define ID_IOERROR (1 << 1)
100#define ID_DONE (1 << 2)
101#define ID_ARBLOST (1 << 3)
102#define ID_NACK (1 << 4)
103
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900104enum rcar_i2c_type {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700105 I2C_RCAR_GEN1,
106 I2C_RCAR_GEN2,
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;
112 struct i2c_msg *msg;
Ben Dooksbc8120f2014-01-26 16:05:35 +0000113 struct clk *clk;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700114
115 spinlock_t lock;
116 wait_queue_head_t wait;
117
118 int pos;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700119 u32 icccr;
120 u32 flags;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900121 enum rcar_i2c_type devtype;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700122};
123
124#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
125#define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
126
127#define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
128#define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
129
130#define LOOP_TIMEOUT 1024
131
132/*
133 * basic functions
134 */
135static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
136{
137 writel(val, priv->io + reg);
138}
139
140static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
141{
142 return readl(priv->io + reg);
143}
144
145static void rcar_i2c_init(struct rcar_i2c_priv *priv)
146{
147 /*
148 * reset slave mode.
149 * slave mode is not used on this driver
150 */
151 rcar_i2c_write(priv, ICSIER, 0);
152 rcar_i2c_write(priv, ICSAR, 0);
153 rcar_i2c_write(priv, ICSCR, 0);
154 rcar_i2c_write(priv, ICSSR, 0);
155
156 /* reset master mode */
157 rcar_i2c_write(priv, ICMIER, 0);
158 rcar_i2c_write(priv, ICMCR, 0);
159 rcar_i2c_write(priv, ICMSR, 0);
160 rcar_i2c_write(priv, ICMAR, 0);
161}
162
163static void rcar_i2c_irq_mask(struct rcar_i2c_priv *priv, int open)
164{
165 u32 val = MNRE | MALE | MSTE | MATE; /* default */
166
167 switch (open) {
168 case RCAR_IRQ_OPEN_FOR_SEND:
169 val |= MDEE; /* default + send */
170 break;
171 case RCAR_IRQ_OPEN_FOR_RECV:
172 val |= MDRE; /* default + read */
173 break;
174 case RCAR_IRQ_OPEN_FOR_STOP:
175 val = MSTE; /* stop irq only */
176 break;
177 case RCAR_IRQ_CLOSE:
178 default:
179 val = 0; /* all close */
180 break;
181 }
182 rcar_i2c_write(priv, ICMIER, val);
183}
184
185static void rcar_i2c_set_addr(struct rcar_i2c_priv *priv, u32 recv)
186{
187 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | recv);
188}
189
190/*
191 * bus control functions
192 */
193static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
194{
195 int i;
196
197 for (i = 0; i < LOOP_TIMEOUT; i++) {
198 /* make sure that bus is not busy */
199 if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
200 return 0;
201 udelay(1);
202 }
203
204 return -EBUSY;
205}
206
207static void rcar_i2c_bus_phase(struct rcar_i2c_priv *priv, int phase)
208{
209 switch (phase) {
210 case RCAR_BUS_PHASE_ADDR:
211 rcar_i2c_write(priv, ICMCR, MDBS | MIE | ESG);
212 break;
213 case RCAR_BUS_PHASE_DATA:
214 rcar_i2c_write(priv, ICMCR, MDBS | MIE);
215 break;
216 case RCAR_BUS_PHASE_STOP:
217 rcar_i2c_write(priv, ICMCR, MDBS | MIE | FSB);
218 break;
219 }
220}
221
222/*
223 * clock function
224 */
225static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
226 u32 bus_speed,
227 struct device *dev)
228{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700229 u32 scgd, cdf;
230 u32 round, ick;
231 u32 scl;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900232 u32 cdf_width;
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200233 unsigned long rate;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700234
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900235 switch (priv->devtype) {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700236 case I2C_RCAR_GEN1:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900237 cdf_width = 2;
238 break;
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700239 case I2C_RCAR_GEN2:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900240 cdf_width = 3;
241 break;
242 default:
243 dev_err(dev, "device type error\n");
244 return -EIO;
245 }
246
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700247 /*
248 * calculate SCL clock
249 * see
250 * ICCCR
251 *
252 * ick = clkp / (1 + CDF)
253 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
254 *
255 * ick : I2C internal clock < 20 MHz
256 * ticf : I2C SCL falling time = 35 ns here
257 * tr : I2C SCL rising time = 200 ns here
258 * intd : LSI internal delay = 50 ns here
259 * clkp : peripheral_clk
260 * F[] : integer up-valuation
261 */
Ben Dooksbc8120f2014-01-26 16:05:35 +0000262 rate = clk_get_rate(priv->clk);
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200263 cdf = rate / 20000000;
264 if (cdf >= 1 << cdf_width) {
265 dev_err(dev, "Input clock %lu too high\n", rate);
266 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700267 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200268 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700269
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700270 /*
271 * it is impossible to calculate large scale
272 * number on u32. separate it
273 *
274 * F[(ticf + tr + intd) * ick]
275 * = F[(35 + 200 + 50)ns * ick]
276 * = F[285 * ick / 1000000000]
277 * = F[(ick / 1000000) * 285 / 1000]
278 */
279 round = (ick + 500000) / 1000000 * 285;
280 round = (round + 500) / 1000;
281
282 /*
283 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
284 *
285 * Calculation result (= SCL) should be less than
286 * bus_speed for hardware safety
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200287 *
288 * We could use something along the lines of
289 * div = ick / (bus_speed + 1) + 1;
290 * scgd = (div - 20 - round + 7) / 8;
291 * scl = ick / (20 + (scgd * 8) + round);
292 * (not fully verified) but that would get pretty involved
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700293 */
294 for (scgd = 0; scgd < 0x40; scgd++) {
295 scl = ick / (20 + (scgd * 8) + round);
296 if (scl <= bus_speed)
297 goto scgd_find;
298 }
299 dev_err(dev, "it is impossible to calculate best SCL\n");
300 return -EIO;
301
302scgd_find:
303 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
Ben Dooksbc8120f2014-01-26 16:05:35 +0000304 scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700305
306 /*
307 * keep icccr value
308 */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200309 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700310
311 return 0;
312}
313
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700314/*
315 * status functions
316 */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700317
318#define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
319static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
320{
321 rcar_i2c_write(priv, ICMSR, ~bit);
322}
323
324/*
325 * recv/send functions
326 */
327static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
328{
329 rcar_i2c_set_addr(priv, 1);
330 rcar_i2c_status_clear(priv);
331 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
332 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_RECV);
333
334 return 0;
335}
336
337static int rcar_i2c_send(struct rcar_i2c_priv *priv)
338{
339 int ret;
340
341 /*
342 * It should check bus status when send case
343 */
344 ret = rcar_i2c_bus_barrier(priv);
345 if (ret < 0)
346 return ret;
347
348 rcar_i2c_set_addr(priv, 0);
349 rcar_i2c_status_clear(priv);
350 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
351 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_SEND);
352
353 return 0;
354}
355
356#define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
357#define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
358
359/*
360 * interrupt functions
361 */
362static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
363{
364 struct i2c_msg *msg = priv->msg;
365
366 /*
367 * FIXME
368 * sometimes, unknown interrupt happened.
369 * Do nothing
370 */
371 if (!(msr & MDE))
372 return 0;
373
374 /*
375 * If address transfer phase finished,
376 * goto data phase.
377 */
378 if (msr & MAT)
379 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
380
381 if (priv->pos < msg->len) {
382 /*
383 * Prepare next data to ICRXTX register.
384 * This data will go to _SHIFT_ register.
385 *
386 * *
387 * [ICRXTX] -> [SHIFT] -> [I2C bus]
388 */
389 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
390 priv->pos++;
391
392 } else {
393 /*
394 * The last data was pushed to ICRXTX on _PREV_ empty irq.
395 * It is on _SHIFT_ register, and will sent to I2C bus.
396 *
397 * *
398 * [ICRXTX] -> [SHIFT] -> [I2C bus]
399 */
400
401 if (priv->flags & ID_LAST_MSG)
402 /*
403 * If current msg is the _LAST_ msg,
404 * prepare stop condition here.
405 * ID_DONE will be set on STOP irq.
406 */
407 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
408 else
409 /*
410 * If current msg is _NOT_ last msg,
411 * it doesn't call stop phase.
412 * thus, there is no STOP irq.
413 * return ID_DONE here.
414 */
415 return ID_DONE;
416 }
417
418 rcar_i2c_send_restart(priv);
419
420 return 0;
421}
422
423static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
424{
425 struct i2c_msg *msg = priv->msg;
426
427 /*
428 * FIXME
429 * sometimes, unknown interrupt happened.
430 * Do nothing
431 */
432 if (!(msr & MDR))
433 return 0;
434
435 if (msr & MAT) {
436 /*
437 * Address transfer phase finished,
438 * but, there is no data at this point.
439 * Do nothing.
440 */
441 } else if (priv->pos < msg->len) {
442 /*
443 * get received data
444 */
445 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
446 priv->pos++;
447 }
448
449 /*
450 * If next received data is the _LAST_,
451 * go to STOP phase,
452 * otherwise, go to DATA phase.
453 */
454 if (priv->pos + 1 >= msg->len)
455 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
456 else
457 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
458
459 rcar_i2c_recv_restart(priv);
460
461 return 0;
462}
463
464static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
465{
466 struct rcar_i2c_priv *priv = ptr;
467 struct device *dev = rcar_i2c_priv_to_dev(priv);
468 u32 msr;
469
470 /*-------------- spin lock -----------------*/
471 spin_lock(&priv->lock);
472
Wolfram Sang1c176d52014-05-28 09:44:36 +0200473 msr = rcar_i2c_read(priv, ICMSR);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700474
475 /*
476 * Arbitration lost
477 */
478 if (msr & MAL) {
479 /*
480 * CAUTION
481 *
482 * When arbitration lost, device become _slave_ mode.
483 */
484 dev_dbg(dev, "Arbitration Lost\n");
485 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
486 goto out;
487 }
488
489 /*
490 * Stop
491 */
492 if (msr & MST) {
493 dev_dbg(dev, "Stop\n");
494 rcar_i2c_flags_set(priv, ID_DONE);
495 goto out;
496 }
497
498 /*
499 * Nack
500 */
501 if (msr & MNR) {
502 dev_dbg(dev, "Nack\n");
503
504 /* go to stop phase */
505 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
506 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_STOP);
507 rcar_i2c_flags_set(priv, ID_NACK);
508 goto out;
509 }
510
511 /*
512 * recv/send
513 */
514 if (rcar_i2c_is_recv(priv))
515 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
516 else
517 rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
518
519out:
520 if (rcar_i2c_flags_has(priv, ID_DONE)) {
521 rcar_i2c_irq_mask(priv, RCAR_IRQ_CLOSE);
522 rcar_i2c_status_clear(priv);
523 wake_up(&priv->wait);
524 }
525
526 spin_unlock(&priv->lock);
527 /*-------------- spin unlock -----------------*/
528
529 return IRQ_HANDLED;
530}
531
532static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
533 struct i2c_msg *msgs,
534 int num)
535{
536 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
537 struct device *dev = rcar_i2c_priv_to_dev(priv);
538 unsigned long flags;
539 int i, ret, timeout;
540
541 pm_runtime_get_sync(dev);
542
543 /*-------------- spin lock -----------------*/
544 spin_lock_irqsave(&priv->lock, flags);
545
546 rcar_i2c_init(priv);
Wolfram Sang1c176d52014-05-28 09:44:36 +0200547 /* start clock */
548 rcar_i2c_write(priv, ICCCR, priv->icccr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700549
550 spin_unlock_irqrestore(&priv->lock, flags);
551 /*-------------- spin unlock -----------------*/
552
553 ret = -EINVAL;
554 for (i = 0; i < num; i++) {
Wolfram Sangd7653962014-05-05 18:36:21 +0200555 /* This HW can't send STOP after address phase */
556 if (msgs[i].len == 0) {
557 ret = -EOPNOTSUPP;
558 break;
559 }
560
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700561 /*-------------- spin lock -----------------*/
562 spin_lock_irqsave(&priv->lock, flags);
563
564 /* init each data */
565 priv->msg = &msgs[i];
566 priv->pos = 0;
567 priv->flags = 0;
568 if (priv->msg == &msgs[num - 1])
569 rcar_i2c_flags_set(priv, ID_LAST_MSG);
570
571 /* start send/recv */
572 if (rcar_i2c_is_recv(priv))
573 ret = rcar_i2c_recv(priv);
574 else
575 ret = rcar_i2c_send(priv);
576
577 spin_unlock_irqrestore(&priv->lock, flags);
578 /*-------------- spin unlock -----------------*/
579
580 if (ret < 0)
581 break;
582
583 /*
584 * wait result
585 */
586 timeout = wait_event_timeout(priv->wait,
587 rcar_i2c_flags_has(priv, ID_DONE),
588 5 * HZ);
589 if (!timeout) {
590 ret = -ETIMEDOUT;
591 break;
592 }
593
594 /*
595 * error handling
596 */
597 if (rcar_i2c_flags_has(priv, ID_NACK)) {
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000598 ret = -ENXIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700599 break;
600 }
601
602 if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
603 ret = -EAGAIN;
604 break;
605 }
606
607 if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
608 ret = -EIO;
609 break;
610 }
611
612 ret = i + 1; /* The number of transfer */
613 }
614
615 pm_runtime_put(dev);
616
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000617 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700618 dev_err(dev, "error %d : %x\n", ret, priv->flags);
619
620 return ret;
621}
622
623static u32 rcar_i2c_func(struct i2c_adapter *adap)
624{
Wolfram Sangd7653962014-05-05 18:36:21 +0200625 /* This HW can't do SMBUS_QUICK and NOSTART */
626 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700627}
628
629static const struct i2c_algorithm rcar_i2c_algo = {
630 .master_xfer = rcar_i2c_master_xfer,
631 .functionality = rcar_i2c_func,
632};
633
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200634static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700635 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
636 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
637 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
638 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange8936452014-02-20 09:03:20 +0100639 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +0200640 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
641 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
642 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200643 {},
644};
645MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
646
Bill Pemberton0b255e92012-11-27 15:59:38 -0500647static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700648{
Jingoo Han6d4028c2013-07-30 16:59:33 +0900649 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700650 struct rcar_i2c_priv *priv;
651 struct i2c_adapter *adap;
652 struct resource *res;
653 struct device *dev = &pdev->dev;
654 u32 bus_speed;
Wolfram Sang93e953d2014-05-28 09:44:37 +0200655 int irq, ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700656
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700657 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
658 if (!priv) {
659 dev_err(dev, "no mem for private data\n");
660 return -ENOMEM;
661 }
662
Ben Dooksbc8120f2014-01-26 16:05:35 +0000663 priv->clk = devm_clk_get(dev, NULL);
664 if (IS_ERR(priv->clk)) {
665 dev_err(dev, "cannot get clock\n");
666 return PTR_ERR(priv->clk);
667 }
668
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700669 bus_speed = 100000; /* default 100 kHz */
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200670 ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
671 if (ret < 0 && pdata && pdata->bus_speed)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700672 bus_speed = pdata->bus_speed;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900673
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200674 if (pdev->dev.of_node)
675 priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
676 dev)->data;
677 else
678 priv->devtype = platform_get_device_id(pdev)->driver_data;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900679
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700680 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
681 if (ret < 0)
682 return ret;
683
Wolfram Sang3cc2d002013-05-10 10:16:54 +0200684 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100685 priv->io = devm_ioremap_resource(dev, res);
686 if (IS_ERR(priv->io))
687 return PTR_ERR(priv->io);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700688
Wolfram Sang93e953d2014-05-28 09:44:37 +0200689 irq = platform_get_irq(pdev, 0);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700690 init_waitqueue_head(&priv->wait);
691 spin_lock_init(&priv->lock);
692
693 adap = &priv->adap;
694 adap->nr = pdev->id;
695 adap->algo = &rcar_i2c_algo;
Wolfram Sang96c4b6b2014-02-10 11:04:06 +0100696 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD | I2C_CLASS_DEPRECATED;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700697 adap->retries = 3;
698 adap->dev.parent = dev;
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200699 adap->dev.of_node = dev->of_node;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700700 i2c_set_adapdata(adap, priv);
701 strlcpy(adap->name, pdev->name, sizeof(adap->name));
702
Wolfram Sang93e953d2014-05-28 09:44:37 +0200703 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700704 dev_name(dev), priv);
705 if (ret < 0) {
Wolfram Sang93e953d2014-05-28 09:44:37 +0200706 dev_err(dev, "cannot get irq %d\n", irq);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700707 return ret;
708 }
709
710 ret = i2c_add_numbered_adapter(adap);
711 if (ret < 0) {
712 dev_err(dev, "reg adap failed: %d\n", ret);
713 return ret;
714 }
715
716 pm_runtime_enable(dev);
717 platform_set_drvdata(pdev, priv);
718
719 dev_info(dev, "probed\n");
720
721 return 0;
722}
723
Bill Pemberton0b255e92012-11-27 15:59:38 -0500724static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700725{
726 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
727 struct device *dev = &pdev->dev;
728
729 i2c_del_adapter(&priv->adap);
730 pm_runtime_disable(dev);
731
732 return 0;
733}
734
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900735static struct platform_device_id rcar_i2c_id_table[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700736 { "i2c-rcar", I2C_RCAR_GEN1 },
737 { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
738 { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900739 {},
740};
741MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
742
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100743static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700744 .driver = {
745 .name = "i2c-rcar",
746 .owner = THIS_MODULE,
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200747 .of_match_table = rcar_i2c_dt_ids,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700748 },
749 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500750 .remove = rcar_i2c_remove,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900751 .id_table = rcar_i2c_id_table,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700752};
753
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100754module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700755
756MODULE_LICENSE("GPL");
757MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
758MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");