blob: 9325db49b4dfd4a5a4cd05dc21faa78f0e0bf72b [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>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/io.h>
32#include <linux/i2c.h>
33#include <linux/i2c/i2c-rcar.h>
34#include <linux/kernel.h>
35#include <linux/module.h>
36#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 {
105 I2C_RCAR_H1,
106 I2C_RCAR_H2,
107};
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;
113
114 spinlock_t lock;
115 wait_queue_head_t wait;
116
117 int pos;
118 int irq;
119 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{
229 struct clk *clkp = clk_get(NULL, "peripheral_clk");
230 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
236 if (!clkp) {
237 dev_err(dev, "there is no peripheral_clk\n");
238 return -EIO;
239 }
240
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900241 switch (priv->devtype) {
242 case I2C_RCAR_H1:
243 cdf_width = 2;
244 break;
245 case I2C_RCAR_H2:
246 cdf_width = 3;
247 break;
248 default:
249 dev_err(dev, "device type error\n");
250 return -EIO;
251 }
252
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700253 /*
254 * calculate SCL clock
255 * see
256 * ICCCR
257 *
258 * ick = clkp / (1 + CDF)
259 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
260 *
261 * ick : I2C internal clock < 20 MHz
262 * ticf : I2C SCL falling time = 35 ns here
263 * tr : I2C SCL rising time = 200 ns here
264 * intd : LSI internal delay = 50 ns here
265 * clkp : peripheral_clk
266 * F[] : integer up-valuation
267 */
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200268 rate = clk_get_rate(clkp);
269 cdf = rate / 20000000;
270 if (cdf >= 1 << cdf_width) {
271 dev_err(dev, "Input clock %lu too high\n", rate);
272 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700273 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200274 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700275
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700276 /*
277 * it is impossible to calculate large scale
278 * number on u32. separate it
279 *
280 * F[(ticf + tr + intd) * ick]
281 * = F[(35 + 200 + 50)ns * ick]
282 * = F[285 * ick / 1000000000]
283 * = F[(ick / 1000000) * 285 / 1000]
284 */
285 round = (ick + 500000) / 1000000 * 285;
286 round = (round + 500) / 1000;
287
288 /*
289 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
290 *
291 * Calculation result (= SCL) should be less than
292 * bus_speed for hardware safety
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200293 *
294 * We could use something along the lines of
295 * div = ick / (bus_speed + 1) + 1;
296 * scgd = (div - 20 - round + 7) / 8;
297 * scl = ick / (20 + (scgd * 8) + round);
298 * (not fully verified) but that would get pretty involved
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700299 */
300 for (scgd = 0; scgd < 0x40; scgd++) {
301 scl = ick / (20 + (scgd * 8) + round);
302 if (scl <= bus_speed)
303 goto scgd_find;
304 }
305 dev_err(dev, "it is impossible to calculate best SCL\n");
306 return -EIO;
307
308scgd_find:
309 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
310 scl, bus_speed, clk_get_rate(clkp), round, cdf, scgd);
311
312 /*
313 * keep icccr value
314 */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200315 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700316
317 return 0;
318}
319
320static void rcar_i2c_clock_start(struct rcar_i2c_priv *priv)
321{
322 rcar_i2c_write(priv, ICCCR, priv->icccr);
323}
324
325/*
326 * status functions
327 */
328static u32 rcar_i2c_status_get(struct rcar_i2c_priv *priv)
329{
330 return rcar_i2c_read(priv, ICMSR);
331}
332
333#define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
334static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
335{
336 rcar_i2c_write(priv, ICMSR, ~bit);
337}
338
339/*
340 * recv/send functions
341 */
342static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
343{
344 rcar_i2c_set_addr(priv, 1);
345 rcar_i2c_status_clear(priv);
346 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
347 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_RECV);
348
349 return 0;
350}
351
352static int rcar_i2c_send(struct rcar_i2c_priv *priv)
353{
354 int ret;
355
356 /*
357 * It should check bus status when send case
358 */
359 ret = rcar_i2c_bus_barrier(priv);
360 if (ret < 0)
361 return ret;
362
363 rcar_i2c_set_addr(priv, 0);
364 rcar_i2c_status_clear(priv);
365 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
366 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_SEND);
367
368 return 0;
369}
370
371#define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
372#define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
373
374/*
375 * interrupt functions
376 */
377static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
378{
379 struct i2c_msg *msg = priv->msg;
380
381 /*
382 * FIXME
383 * sometimes, unknown interrupt happened.
384 * Do nothing
385 */
386 if (!(msr & MDE))
387 return 0;
388
389 /*
390 * If address transfer phase finished,
391 * goto data phase.
392 */
393 if (msr & MAT)
394 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
395
396 if (priv->pos < msg->len) {
397 /*
398 * Prepare next data to ICRXTX register.
399 * This data will go to _SHIFT_ register.
400 *
401 * *
402 * [ICRXTX] -> [SHIFT] -> [I2C bus]
403 */
404 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
405 priv->pos++;
406
407 } else {
408 /*
409 * The last data was pushed to ICRXTX on _PREV_ empty irq.
410 * It is on _SHIFT_ register, and will sent to I2C bus.
411 *
412 * *
413 * [ICRXTX] -> [SHIFT] -> [I2C bus]
414 */
415
416 if (priv->flags & ID_LAST_MSG)
417 /*
418 * If current msg is the _LAST_ msg,
419 * prepare stop condition here.
420 * ID_DONE will be set on STOP irq.
421 */
422 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
423 else
424 /*
425 * If current msg is _NOT_ last msg,
426 * it doesn't call stop phase.
427 * thus, there is no STOP irq.
428 * return ID_DONE here.
429 */
430 return ID_DONE;
431 }
432
433 rcar_i2c_send_restart(priv);
434
435 return 0;
436}
437
438static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
439{
440 struct i2c_msg *msg = priv->msg;
441
442 /*
443 * FIXME
444 * sometimes, unknown interrupt happened.
445 * Do nothing
446 */
447 if (!(msr & MDR))
448 return 0;
449
450 if (msr & MAT) {
451 /*
452 * Address transfer phase finished,
453 * but, there is no data at this point.
454 * Do nothing.
455 */
456 } else if (priv->pos < msg->len) {
457 /*
458 * get received data
459 */
460 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
461 priv->pos++;
462 }
463
464 /*
465 * If next received data is the _LAST_,
466 * go to STOP phase,
467 * otherwise, go to DATA phase.
468 */
469 if (priv->pos + 1 >= msg->len)
470 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
471 else
472 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
473
474 rcar_i2c_recv_restart(priv);
475
476 return 0;
477}
478
479static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
480{
481 struct rcar_i2c_priv *priv = ptr;
482 struct device *dev = rcar_i2c_priv_to_dev(priv);
483 u32 msr;
484
485 /*-------------- spin lock -----------------*/
486 spin_lock(&priv->lock);
487
488 msr = rcar_i2c_status_get(priv);
489
490 /*
491 * Arbitration lost
492 */
493 if (msr & MAL) {
494 /*
495 * CAUTION
496 *
497 * When arbitration lost, device become _slave_ mode.
498 */
499 dev_dbg(dev, "Arbitration Lost\n");
500 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
501 goto out;
502 }
503
504 /*
505 * Stop
506 */
507 if (msr & MST) {
508 dev_dbg(dev, "Stop\n");
509 rcar_i2c_flags_set(priv, ID_DONE);
510 goto out;
511 }
512
513 /*
514 * Nack
515 */
516 if (msr & MNR) {
517 dev_dbg(dev, "Nack\n");
518
519 /* go to stop phase */
520 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
521 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_STOP);
522 rcar_i2c_flags_set(priv, ID_NACK);
523 goto out;
524 }
525
526 /*
527 * recv/send
528 */
529 if (rcar_i2c_is_recv(priv))
530 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
531 else
532 rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
533
534out:
535 if (rcar_i2c_flags_has(priv, ID_DONE)) {
536 rcar_i2c_irq_mask(priv, RCAR_IRQ_CLOSE);
537 rcar_i2c_status_clear(priv);
538 wake_up(&priv->wait);
539 }
540
541 spin_unlock(&priv->lock);
542 /*-------------- spin unlock -----------------*/
543
544 return IRQ_HANDLED;
545}
546
547static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
548 struct i2c_msg *msgs,
549 int num)
550{
551 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
552 struct device *dev = rcar_i2c_priv_to_dev(priv);
553 unsigned long flags;
554 int i, ret, timeout;
555
556 pm_runtime_get_sync(dev);
557
558 /*-------------- spin lock -----------------*/
559 spin_lock_irqsave(&priv->lock, flags);
560
561 rcar_i2c_init(priv);
562 rcar_i2c_clock_start(priv);
563
564 spin_unlock_irqrestore(&priv->lock, flags);
565 /*-------------- spin unlock -----------------*/
566
567 ret = -EINVAL;
568 for (i = 0; i < num; i++) {
569 /*-------------- spin lock -----------------*/
570 spin_lock_irqsave(&priv->lock, flags);
571
572 /* init each data */
573 priv->msg = &msgs[i];
574 priv->pos = 0;
575 priv->flags = 0;
576 if (priv->msg == &msgs[num - 1])
577 rcar_i2c_flags_set(priv, ID_LAST_MSG);
578
579 /* start send/recv */
580 if (rcar_i2c_is_recv(priv))
581 ret = rcar_i2c_recv(priv);
582 else
583 ret = rcar_i2c_send(priv);
584
585 spin_unlock_irqrestore(&priv->lock, flags);
586 /*-------------- spin unlock -----------------*/
587
588 if (ret < 0)
589 break;
590
591 /*
592 * wait result
593 */
594 timeout = wait_event_timeout(priv->wait,
595 rcar_i2c_flags_has(priv, ID_DONE),
596 5 * HZ);
597 if (!timeout) {
598 ret = -ETIMEDOUT;
599 break;
600 }
601
602 /*
603 * error handling
604 */
605 if (rcar_i2c_flags_has(priv, ID_NACK)) {
606 ret = -EREMOTEIO;
607 break;
608 }
609
610 if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
611 ret = -EAGAIN;
612 break;
613 }
614
615 if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
616 ret = -EIO;
617 break;
618 }
619
620 ret = i + 1; /* The number of transfer */
621 }
622
623 pm_runtime_put(dev);
624
625 if (ret < 0)
626 dev_err(dev, "error %d : %x\n", ret, priv->flags);
627
628 return ret;
629}
630
631static u32 rcar_i2c_func(struct i2c_adapter *adap)
632{
633 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
634}
635
636static const struct i2c_algorithm rcar_i2c_algo = {
637 .master_xfer = rcar_i2c_master_xfer,
638 .functionality = rcar_i2c_func,
639};
640
Bill Pemberton0b255e92012-11-27 15:59:38 -0500641static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700642{
Jingoo Han6d4028c2013-07-30 16:59:33 +0900643 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700644 struct rcar_i2c_priv *priv;
645 struct i2c_adapter *adap;
646 struct resource *res;
647 struct device *dev = &pdev->dev;
648 u32 bus_speed;
649 int ret;
650
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700651 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
652 if (!priv) {
653 dev_err(dev, "no mem for private data\n");
654 return -ENOMEM;
655 }
656
657 bus_speed = 100000; /* default 100 kHz */
658 if (pdata && pdata->bus_speed)
659 bus_speed = pdata->bus_speed;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900660
661 priv->devtype = platform_get_device_id(pdev)->driver_data;
662
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700663 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
664 if (ret < 0)
665 return ret;
666
Wolfram Sang3cc2d002013-05-10 10:16:54 +0200667 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100668 priv->io = devm_ioremap_resource(dev, res);
669 if (IS_ERR(priv->io))
670 return PTR_ERR(priv->io);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700671
672 priv->irq = platform_get_irq(pdev, 0);
673 init_waitqueue_head(&priv->wait);
674 spin_lock_init(&priv->lock);
675
676 adap = &priv->adap;
677 adap->nr = pdev->id;
678 adap->algo = &rcar_i2c_algo;
679 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
680 adap->retries = 3;
681 adap->dev.parent = dev;
682 i2c_set_adapdata(adap, priv);
683 strlcpy(adap->name, pdev->name, sizeof(adap->name));
684
685 ret = devm_request_irq(dev, priv->irq, rcar_i2c_irq, 0,
686 dev_name(dev), priv);
687 if (ret < 0) {
688 dev_err(dev, "cannot get irq %d\n", priv->irq);
689 return ret;
690 }
691
692 ret = i2c_add_numbered_adapter(adap);
693 if (ret < 0) {
694 dev_err(dev, "reg adap failed: %d\n", ret);
695 return ret;
696 }
697
698 pm_runtime_enable(dev);
699 platform_set_drvdata(pdev, priv);
700
701 dev_info(dev, "probed\n");
702
703 return 0;
704}
705
Bill Pemberton0b255e92012-11-27 15:59:38 -0500706static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700707{
708 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
709 struct device *dev = &pdev->dev;
710
711 i2c_del_adapter(&priv->adap);
712 pm_runtime_disable(dev);
713
714 return 0;
715}
716
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900717static struct platform_device_id rcar_i2c_id_table[] = {
718 { "i2c-rcar", I2C_RCAR_H1 },
719 { "i2c-rcar_h1", I2C_RCAR_H1 },
720 { "i2c-rcar_h2", I2C_RCAR_H2 },
721 {},
722};
723MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
724
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100725static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700726 .driver = {
727 .name = "i2c-rcar",
728 .owner = THIS_MODULE,
729 },
730 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500731 .remove = rcar_i2c_remove,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900732 .id_table = rcar_i2c_id_table,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700733};
734
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100735module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700736
737MODULE_LICENSE("GPL");
738MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
739MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");