blob: de4e6b81fa9b3100e6a1a3842785d2450b388d1e [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;
119 int irq;
120 u32 icccr;
121 u32 flags;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900122 enum rcar_i2c_type devtype;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700123};
124
125#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
126#define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
127
128#define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
129#define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
130
131#define LOOP_TIMEOUT 1024
132
133/*
134 * basic functions
135 */
136static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
137{
138 writel(val, priv->io + reg);
139}
140
141static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
142{
143 return readl(priv->io + reg);
144}
145
146static void rcar_i2c_init(struct rcar_i2c_priv *priv)
147{
148 /*
149 * reset slave mode.
150 * slave mode is not used on this driver
151 */
152 rcar_i2c_write(priv, ICSIER, 0);
153 rcar_i2c_write(priv, ICSAR, 0);
154 rcar_i2c_write(priv, ICSCR, 0);
155 rcar_i2c_write(priv, ICSSR, 0);
156
157 /* reset master mode */
158 rcar_i2c_write(priv, ICMIER, 0);
159 rcar_i2c_write(priv, ICMCR, 0);
160 rcar_i2c_write(priv, ICMSR, 0);
161 rcar_i2c_write(priv, ICMAR, 0);
162}
163
164static void rcar_i2c_irq_mask(struct rcar_i2c_priv *priv, int open)
165{
166 u32 val = MNRE | MALE | MSTE | MATE; /* default */
167
168 switch (open) {
169 case RCAR_IRQ_OPEN_FOR_SEND:
170 val |= MDEE; /* default + send */
171 break;
172 case RCAR_IRQ_OPEN_FOR_RECV:
173 val |= MDRE; /* default + read */
174 break;
175 case RCAR_IRQ_OPEN_FOR_STOP:
176 val = MSTE; /* stop irq only */
177 break;
178 case RCAR_IRQ_CLOSE:
179 default:
180 val = 0; /* all close */
181 break;
182 }
183 rcar_i2c_write(priv, ICMIER, val);
184}
185
186static void rcar_i2c_set_addr(struct rcar_i2c_priv *priv, u32 recv)
187{
188 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | recv);
189}
190
191/*
192 * bus control functions
193 */
194static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
195{
196 int i;
197
198 for (i = 0; i < LOOP_TIMEOUT; i++) {
199 /* make sure that bus is not busy */
200 if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
201 return 0;
202 udelay(1);
203 }
204
205 return -EBUSY;
206}
207
208static void rcar_i2c_bus_phase(struct rcar_i2c_priv *priv, int phase)
209{
210 switch (phase) {
211 case RCAR_BUS_PHASE_ADDR:
212 rcar_i2c_write(priv, ICMCR, MDBS | MIE | ESG);
213 break;
214 case RCAR_BUS_PHASE_DATA:
215 rcar_i2c_write(priv, ICMCR, MDBS | MIE);
216 break;
217 case RCAR_BUS_PHASE_STOP:
218 rcar_i2c_write(priv, ICMCR, MDBS | MIE | FSB);
219 break;
220 }
221}
222
223/*
224 * clock function
225 */
226static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
227 u32 bus_speed,
228 struct device *dev)
229{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700230 u32 scgd, cdf;
231 u32 round, ick;
232 u32 scl;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900233 u32 cdf_width;
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200234 unsigned long rate;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700235
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900236 switch (priv->devtype) {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700237 case I2C_RCAR_GEN1:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900238 cdf_width = 2;
239 break;
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700240 case I2C_RCAR_GEN2:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900241 cdf_width = 3;
242 break;
243 default:
244 dev_err(dev, "device type error\n");
245 return -EIO;
246 }
247
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700248 /*
249 * calculate SCL clock
250 * see
251 * ICCCR
252 *
253 * ick = clkp / (1 + CDF)
254 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
255 *
256 * ick : I2C internal clock < 20 MHz
257 * ticf : I2C SCL falling time = 35 ns here
258 * tr : I2C SCL rising time = 200 ns here
259 * intd : LSI internal delay = 50 ns here
260 * clkp : peripheral_clk
261 * F[] : integer up-valuation
262 */
Ben Dooksbc8120f2014-01-26 16:05:35 +0000263 rate = clk_get_rate(priv->clk);
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200264 cdf = rate / 20000000;
265 if (cdf >= 1 << cdf_width) {
266 dev_err(dev, "Input clock %lu too high\n", rate);
267 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700268 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200269 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700270
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700271 /*
272 * it is impossible to calculate large scale
273 * number on u32. separate it
274 *
275 * F[(ticf + tr + intd) * ick]
276 * = F[(35 + 200 + 50)ns * ick]
277 * = F[285 * ick / 1000000000]
278 * = F[(ick / 1000000) * 285 / 1000]
279 */
280 round = (ick + 500000) / 1000000 * 285;
281 round = (round + 500) / 1000;
282
283 /*
284 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
285 *
286 * Calculation result (= SCL) should be less than
287 * bus_speed for hardware safety
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200288 *
289 * We could use something along the lines of
290 * div = ick / (bus_speed + 1) + 1;
291 * scgd = (div - 20 - round + 7) / 8;
292 * scl = ick / (20 + (scgd * 8) + round);
293 * (not fully verified) but that would get pretty involved
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700294 */
295 for (scgd = 0; scgd < 0x40; scgd++) {
296 scl = ick / (20 + (scgd * 8) + round);
297 if (scl <= bus_speed)
298 goto scgd_find;
299 }
300 dev_err(dev, "it is impossible to calculate best SCL\n");
301 return -EIO;
302
303scgd_find:
304 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
Ben Dooksbc8120f2014-01-26 16:05:35 +0000305 scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700306
307 /*
308 * keep icccr value
309 */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200310 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700311
312 return 0;
313}
314
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700315/*
316 * status functions
317 */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700318
319#define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
320static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
321{
322 rcar_i2c_write(priv, ICMSR, ~bit);
323}
324
325/*
326 * recv/send functions
327 */
328static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
329{
330 rcar_i2c_set_addr(priv, 1);
331 rcar_i2c_status_clear(priv);
332 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
333 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_RECV);
334
335 return 0;
336}
337
338static int rcar_i2c_send(struct rcar_i2c_priv *priv)
339{
340 int ret;
341
342 /*
343 * It should check bus status when send case
344 */
345 ret = rcar_i2c_bus_barrier(priv);
346 if (ret < 0)
347 return ret;
348
349 rcar_i2c_set_addr(priv, 0);
350 rcar_i2c_status_clear(priv);
351 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
352 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_SEND);
353
354 return 0;
355}
356
357#define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
358#define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
359
360/*
361 * interrupt functions
362 */
363static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
364{
365 struct i2c_msg *msg = priv->msg;
366
367 /*
368 * FIXME
369 * sometimes, unknown interrupt happened.
370 * Do nothing
371 */
372 if (!(msr & MDE))
373 return 0;
374
375 /*
376 * If address transfer phase finished,
377 * goto data phase.
378 */
379 if (msr & MAT)
380 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
381
382 if (priv->pos < msg->len) {
383 /*
384 * Prepare next data to ICRXTX register.
385 * This data will go to _SHIFT_ register.
386 *
387 * *
388 * [ICRXTX] -> [SHIFT] -> [I2C bus]
389 */
390 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
391 priv->pos++;
392
393 } else {
394 /*
395 * The last data was pushed to ICRXTX on _PREV_ empty irq.
396 * It is on _SHIFT_ register, and will sent to I2C bus.
397 *
398 * *
399 * [ICRXTX] -> [SHIFT] -> [I2C bus]
400 */
401
402 if (priv->flags & ID_LAST_MSG)
403 /*
404 * If current msg is the _LAST_ msg,
405 * prepare stop condition here.
406 * ID_DONE will be set on STOP irq.
407 */
408 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
409 else
410 /*
411 * If current msg is _NOT_ last msg,
412 * it doesn't call stop phase.
413 * thus, there is no STOP irq.
414 * return ID_DONE here.
415 */
416 return ID_DONE;
417 }
418
419 rcar_i2c_send_restart(priv);
420
421 return 0;
422}
423
424static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
425{
426 struct i2c_msg *msg = priv->msg;
427
428 /*
429 * FIXME
430 * sometimes, unknown interrupt happened.
431 * Do nothing
432 */
433 if (!(msr & MDR))
434 return 0;
435
436 if (msr & MAT) {
437 /*
438 * Address transfer phase finished,
439 * but, there is no data at this point.
440 * Do nothing.
441 */
442 } else if (priv->pos < msg->len) {
443 /*
444 * get received data
445 */
446 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
447 priv->pos++;
448 }
449
450 /*
451 * If next received data is the _LAST_,
452 * go to STOP phase,
453 * otherwise, go to DATA phase.
454 */
455 if (priv->pos + 1 >= msg->len)
456 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
457 else
458 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
459
460 rcar_i2c_recv_restart(priv);
461
462 return 0;
463}
464
465static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
466{
467 struct rcar_i2c_priv *priv = ptr;
468 struct device *dev = rcar_i2c_priv_to_dev(priv);
469 u32 msr;
470
471 /*-------------- spin lock -----------------*/
472 spin_lock(&priv->lock);
473
Wolfram Sang1c176d52014-05-28 09:44:36 +0200474 msr = rcar_i2c_read(priv, ICMSR);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700475
476 /*
477 * Arbitration lost
478 */
479 if (msr & MAL) {
480 /*
481 * CAUTION
482 *
483 * When arbitration lost, device become _slave_ mode.
484 */
485 dev_dbg(dev, "Arbitration Lost\n");
486 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
487 goto out;
488 }
489
490 /*
491 * Stop
492 */
493 if (msr & MST) {
494 dev_dbg(dev, "Stop\n");
495 rcar_i2c_flags_set(priv, ID_DONE);
496 goto out;
497 }
498
499 /*
500 * Nack
501 */
502 if (msr & MNR) {
503 dev_dbg(dev, "Nack\n");
504
505 /* go to stop phase */
506 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
507 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_STOP);
508 rcar_i2c_flags_set(priv, ID_NACK);
509 goto out;
510 }
511
512 /*
513 * recv/send
514 */
515 if (rcar_i2c_is_recv(priv))
516 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
517 else
518 rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
519
520out:
521 if (rcar_i2c_flags_has(priv, ID_DONE)) {
522 rcar_i2c_irq_mask(priv, RCAR_IRQ_CLOSE);
523 rcar_i2c_status_clear(priv);
524 wake_up(&priv->wait);
525 }
526
527 spin_unlock(&priv->lock);
528 /*-------------- spin unlock -----------------*/
529
530 return IRQ_HANDLED;
531}
532
533static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
534 struct i2c_msg *msgs,
535 int num)
536{
537 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
538 struct device *dev = rcar_i2c_priv_to_dev(priv);
539 unsigned long flags;
540 int i, ret, timeout;
541
542 pm_runtime_get_sync(dev);
543
544 /*-------------- spin lock -----------------*/
545 spin_lock_irqsave(&priv->lock, flags);
546
547 rcar_i2c_init(priv);
Wolfram Sang1c176d52014-05-28 09:44:36 +0200548 /* start clock */
549 rcar_i2c_write(priv, ICCCR, priv->icccr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700550
551 spin_unlock_irqrestore(&priv->lock, flags);
552 /*-------------- spin unlock -----------------*/
553
554 ret = -EINVAL;
555 for (i = 0; i < num; i++) {
Wolfram Sangd7653962014-05-05 18:36:21 +0200556 /* This HW can't send STOP after address phase */
557 if (msgs[i].len == 0) {
558 ret = -EOPNOTSUPP;
559 break;
560 }
561
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700562 /*-------------- spin lock -----------------*/
563 spin_lock_irqsave(&priv->lock, flags);
564
565 /* init each data */
566 priv->msg = &msgs[i];
567 priv->pos = 0;
568 priv->flags = 0;
569 if (priv->msg == &msgs[num - 1])
570 rcar_i2c_flags_set(priv, ID_LAST_MSG);
571
572 /* start send/recv */
573 if (rcar_i2c_is_recv(priv))
574 ret = rcar_i2c_recv(priv);
575 else
576 ret = rcar_i2c_send(priv);
577
578 spin_unlock_irqrestore(&priv->lock, flags);
579 /*-------------- spin unlock -----------------*/
580
581 if (ret < 0)
582 break;
583
584 /*
585 * wait result
586 */
587 timeout = wait_event_timeout(priv->wait,
588 rcar_i2c_flags_has(priv, ID_DONE),
589 5 * HZ);
590 if (!timeout) {
591 ret = -ETIMEDOUT;
592 break;
593 }
594
595 /*
596 * error handling
597 */
598 if (rcar_i2c_flags_has(priv, ID_NACK)) {
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000599 ret = -ENXIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700600 break;
601 }
602
603 if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
604 ret = -EAGAIN;
605 break;
606 }
607
608 if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
609 ret = -EIO;
610 break;
611 }
612
613 ret = i + 1; /* The number of transfer */
614 }
615
616 pm_runtime_put(dev);
617
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000618 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700619 dev_err(dev, "error %d : %x\n", ret, priv->flags);
620
621 return ret;
622}
623
624static u32 rcar_i2c_func(struct i2c_adapter *adap)
625{
Wolfram Sangd7653962014-05-05 18:36:21 +0200626 /* This HW can't do SMBUS_QUICK and NOSTART */
627 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700628}
629
630static const struct i2c_algorithm rcar_i2c_algo = {
631 .master_xfer = rcar_i2c_master_xfer,
632 .functionality = rcar_i2c_func,
633};
634
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200635static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700636 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
637 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
638 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
639 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange8936452014-02-20 09:03:20 +0100640 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +0200641 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
642 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
643 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200644 {},
645};
646MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
647
Bill Pemberton0b255e92012-11-27 15:59:38 -0500648static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700649{
Jingoo Han6d4028c2013-07-30 16:59:33 +0900650 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700651 struct rcar_i2c_priv *priv;
652 struct i2c_adapter *adap;
653 struct resource *res;
654 struct device *dev = &pdev->dev;
655 u32 bus_speed;
656 int ret;
657
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700658 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
659 if (!priv) {
660 dev_err(dev, "no mem for private data\n");
661 return -ENOMEM;
662 }
663
Ben Dooksbc8120f2014-01-26 16:05:35 +0000664 priv->clk = devm_clk_get(dev, NULL);
665 if (IS_ERR(priv->clk)) {
666 dev_err(dev, "cannot get clock\n");
667 return PTR_ERR(priv->clk);
668 }
669
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700670 bus_speed = 100000; /* default 100 kHz */
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200671 ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
672 if (ret < 0 && pdata && pdata->bus_speed)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700673 bus_speed = pdata->bus_speed;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900674
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200675 if (pdev->dev.of_node)
676 priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
677 dev)->data;
678 else
679 priv->devtype = platform_get_device_id(pdev)->driver_data;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900680
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700681 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
682 if (ret < 0)
683 return ret;
684
Wolfram Sang3cc2d002013-05-10 10:16:54 +0200685 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100686 priv->io = devm_ioremap_resource(dev, res);
687 if (IS_ERR(priv->io))
688 return PTR_ERR(priv->io);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700689
690 priv->irq = platform_get_irq(pdev, 0);
691 init_waitqueue_head(&priv->wait);
692 spin_lock_init(&priv->lock);
693
694 adap = &priv->adap;
695 adap->nr = pdev->id;
696 adap->algo = &rcar_i2c_algo;
Wolfram Sang96c4b6b2014-02-10 11:04:06 +0100697 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD | I2C_CLASS_DEPRECATED;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700698 adap->retries = 3;
699 adap->dev.parent = dev;
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200700 adap->dev.of_node = dev->of_node;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700701 i2c_set_adapdata(adap, priv);
702 strlcpy(adap->name, pdev->name, sizeof(adap->name));
703
704 ret = devm_request_irq(dev, priv->irq, rcar_i2c_irq, 0,
705 dev_name(dev), priv);
706 if (ret < 0) {
707 dev_err(dev, "cannot get irq %d\n", priv->irq);
708 return ret;
709 }
710
711 ret = i2c_add_numbered_adapter(adap);
712 if (ret < 0) {
713 dev_err(dev, "reg adap failed: %d\n", ret);
714 return ret;
715 }
716
717 pm_runtime_enable(dev);
718 platform_set_drvdata(pdev, priv);
719
720 dev_info(dev, "probed\n");
721
722 return 0;
723}
724
Bill Pemberton0b255e92012-11-27 15:59:38 -0500725static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700726{
727 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
728 struct device *dev = &pdev->dev;
729
730 i2c_del_adapter(&priv->adap);
731 pm_runtime_disable(dev);
732
733 return 0;
734}
735
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900736static struct platform_device_id rcar_i2c_id_table[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700737 { "i2c-rcar", I2C_RCAR_GEN1 },
738 { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
739 { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900740 {},
741};
742MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
743
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100744static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700745 .driver = {
746 .name = "i2c-rcar",
747 .owner = THIS_MODULE,
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200748 .of_match_table = rcar_i2c_dt_ids,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700749 },
750 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500751 .remove = rcar_i2c_remove,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900752 .id_table = rcar_i2c_id_table,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700753};
754
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100755module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700756
757MODULE_LICENSE("GPL");
758MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
759MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");