blob: eadaca0ef4be7bb4d31e3825e84e72adb61c0d9d [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
86enum {
87 RCAR_IRQ_CLOSE,
88 RCAR_IRQ_OPEN_FOR_SEND,
89 RCAR_IRQ_OPEN_FOR_RECV,
90 RCAR_IRQ_OPEN_FOR_STOP,
91};
92
93/*
94 * flags
95 */
96#define ID_LAST_MSG (1 << 0)
97#define ID_IOERROR (1 << 1)
98#define ID_DONE (1 << 2)
99#define ID_ARBLOST (1 << 3)
100#define ID_NACK (1 << 4)
101
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900102enum rcar_i2c_type {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700103 I2C_RCAR_GEN1,
104 I2C_RCAR_GEN2,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900105};
106
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700107struct rcar_i2c_priv {
108 void __iomem *io;
109 struct i2c_adapter adap;
110 struct i2c_msg *msg;
Ben Dooksbc8120f2014-01-26 16:05:35 +0000111 struct clk *clk;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700112
113 spinlock_t lock;
114 wait_queue_head_t wait;
115
116 int pos;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700117 u32 icccr;
118 u32 flags;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900119 enum rcar_i2c_type devtype;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700120};
121
122#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
123#define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
124
125#define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
126#define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
127
128#define LOOP_TIMEOUT 1024
129
130/*
131 * basic functions
132 */
133static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
134{
135 writel(val, priv->io + reg);
136}
137
138static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
139{
140 return readl(priv->io + reg);
141}
142
143static void rcar_i2c_init(struct rcar_i2c_priv *priv)
144{
145 /*
146 * reset slave mode.
147 * slave mode is not used on this driver
148 */
149 rcar_i2c_write(priv, ICSIER, 0);
150 rcar_i2c_write(priv, ICSAR, 0);
151 rcar_i2c_write(priv, ICSCR, 0);
152 rcar_i2c_write(priv, ICSSR, 0);
153
154 /* reset master mode */
155 rcar_i2c_write(priv, ICMIER, 0);
156 rcar_i2c_write(priv, ICMCR, 0);
157 rcar_i2c_write(priv, ICMSR, 0);
158 rcar_i2c_write(priv, ICMAR, 0);
159}
160
161static void rcar_i2c_irq_mask(struct rcar_i2c_priv *priv, int open)
162{
163 u32 val = MNRE | MALE | MSTE | MATE; /* default */
164
165 switch (open) {
166 case RCAR_IRQ_OPEN_FOR_SEND:
167 val |= MDEE; /* default + send */
168 break;
169 case RCAR_IRQ_OPEN_FOR_RECV:
170 val |= MDRE; /* default + read */
171 break;
172 case RCAR_IRQ_OPEN_FOR_STOP:
173 val = MSTE; /* stop irq only */
174 break;
175 case RCAR_IRQ_CLOSE:
176 default:
177 val = 0; /* all close */
178 break;
179 }
180 rcar_i2c_write(priv, ICMIER, val);
181}
182
183static void rcar_i2c_set_addr(struct rcar_i2c_priv *priv, u32 recv)
184{
185 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | recv);
186}
187
188/*
189 * bus control functions
190 */
191static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
192{
193 int i;
194
195 for (i = 0; i < LOOP_TIMEOUT; i++) {
196 /* make sure that bus is not busy */
197 if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
198 return 0;
199 udelay(1);
200 }
201
202 return -EBUSY;
203}
204
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700205/*
206 * clock function
207 */
208static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
209 u32 bus_speed,
210 struct device *dev)
211{
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700212 u32 scgd, cdf;
213 u32 round, ick;
214 u32 scl;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900215 u32 cdf_width;
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200216 unsigned long rate;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700217
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900218 switch (priv->devtype) {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700219 case I2C_RCAR_GEN1:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900220 cdf_width = 2;
221 break;
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700222 case I2C_RCAR_GEN2:
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900223 cdf_width = 3;
224 break;
225 default:
226 dev_err(dev, "device type error\n");
227 return -EIO;
228 }
229
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700230 /*
231 * calculate SCL clock
232 * see
233 * ICCCR
234 *
235 * ick = clkp / (1 + CDF)
236 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
237 *
238 * ick : I2C internal clock < 20 MHz
239 * ticf : I2C SCL falling time = 35 ns here
240 * tr : I2C SCL rising time = 200 ns here
241 * intd : LSI internal delay = 50 ns here
242 * clkp : peripheral_clk
243 * F[] : integer up-valuation
244 */
Ben Dooksbc8120f2014-01-26 16:05:35 +0000245 rate = clk_get_rate(priv->clk);
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200246 cdf = rate / 20000000;
247 if (cdf >= 1 << cdf_width) {
248 dev_err(dev, "Input clock %lu too high\n", rate);
249 return -EIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700250 }
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200251 ick = rate / (cdf + 1);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700252
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700253 /*
254 * it is impossible to calculate large scale
255 * number on u32. separate it
256 *
257 * F[(ticf + tr + intd) * ick]
258 * = F[(35 + 200 + 50)ns * ick]
259 * = F[285 * ick / 1000000000]
260 * = F[(ick / 1000000) * 285 / 1000]
261 */
262 round = (ick + 500000) / 1000000 * 285;
263 round = (round + 500) / 1000;
264
265 /*
266 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
267 *
268 * Calculation result (= SCL) should be less than
269 * bus_speed for hardware safety
Guennadi Liakhovetski8d049402013-09-12 14:36:45 +0200270 *
271 * We could use something along the lines of
272 * div = ick / (bus_speed + 1) + 1;
273 * scgd = (div - 20 - round + 7) / 8;
274 * scl = ick / (20 + (scgd * 8) + round);
275 * (not fully verified) but that would get pretty involved
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700276 */
277 for (scgd = 0; scgd < 0x40; scgd++) {
278 scl = ick / (20 + (scgd * 8) + round);
279 if (scl <= bus_speed)
280 goto scgd_find;
281 }
282 dev_err(dev, "it is impossible to calculate best SCL\n");
283 return -EIO;
284
285scgd_find:
286 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
Ben Dooksbc8120f2014-01-26 16:05:35 +0000287 scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700288
289 /*
290 * keep icccr value
291 */
Guennadi Liakhovetski14d32f12013-09-12 14:36:44 +0200292 priv->icccr = scgd << cdf_width | cdf;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700293
294 return 0;
295}
296
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700297/*
298 * status functions
299 */
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700300
301#define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
302static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
303{
304 rcar_i2c_write(priv, ICMSR, ~bit);
305}
306
307/*
308 * recv/send functions
309 */
310static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
311{
312 rcar_i2c_set_addr(priv, 1);
313 rcar_i2c_status_clear(priv);
Wolfram Sang4f443a82014-05-28 09:44:38 +0200314 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700315 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_RECV);
316
317 return 0;
318}
319
320static int rcar_i2c_send(struct rcar_i2c_priv *priv)
321{
322 int ret;
323
324 /*
325 * It should check bus status when send case
326 */
327 ret = rcar_i2c_bus_barrier(priv);
328 if (ret < 0)
329 return ret;
330
331 rcar_i2c_set_addr(priv, 0);
332 rcar_i2c_status_clear(priv);
Wolfram Sang4f443a82014-05-28 09:44:38 +0200333 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700334 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_SEND);
335
336 return 0;
337}
338
339#define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
340#define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
341
342/*
343 * interrupt functions
344 */
345static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
346{
347 struct i2c_msg *msg = priv->msg;
348
349 /*
350 * FIXME
351 * sometimes, unknown interrupt happened.
352 * Do nothing
353 */
354 if (!(msr & MDE))
355 return 0;
356
357 /*
358 * If address transfer phase finished,
359 * goto data phase.
360 */
361 if (msr & MAT)
Wolfram Sang4f443a82014-05-28 09:44:38 +0200362 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700363
364 if (priv->pos < msg->len) {
365 /*
366 * Prepare next data to ICRXTX register.
367 * This data will go to _SHIFT_ register.
368 *
369 * *
370 * [ICRXTX] -> [SHIFT] -> [I2C bus]
371 */
372 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
373 priv->pos++;
374
375 } else {
376 /*
377 * The last data was pushed to ICRXTX on _PREV_ empty irq.
378 * It is on _SHIFT_ register, and will sent to I2C bus.
379 *
380 * *
381 * [ICRXTX] -> [SHIFT] -> [I2C bus]
382 */
383
384 if (priv->flags & ID_LAST_MSG)
385 /*
386 * If current msg is the _LAST_ msg,
387 * prepare stop condition here.
388 * ID_DONE will be set on STOP irq.
389 */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200390 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700391 else
392 /*
393 * If current msg is _NOT_ last msg,
394 * it doesn't call stop phase.
395 * thus, there is no STOP irq.
396 * return ID_DONE here.
397 */
398 return ID_DONE;
399 }
400
401 rcar_i2c_send_restart(priv);
402
403 return 0;
404}
405
406static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
407{
408 struct i2c_msg *msg = priv->msg;
409
410 /*
411 * FIXME
412 * sometimes, unknown interrupt happened.
413 * Do nothing
414 */
415 if (!(msr & MDR))
416 return 0;
417
418 if (msr & MAT) {
419 /*
420 * Address transfer phase finished,
421 * but, there is no data at this point.
422 * Do nothing.
423 */
424 } else if (priv->pos < msg->len) {
425 /*
426 * get received data
427 */
428 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
429 priv->pos++;
430 }
431
432 /*
433 * If next received data is the _LAST_,
434 * go to STOP phase,
435 * otherwise, go to DATA phase.
436 */
437 if (priv->pos + 1 >= msg->len)
Wolfram Sang4f443a82014-05-28 09:44:38 +0200438 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700439 else
Wolfram Sang4f443a82014-05-28 09:44:38 +0200440 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700441
442 rcar_i2c_recv_restart(priv);
443
444 return 0;
445}
446
447static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
448{
449 struct rcar_i2c_priv *priv = ptr;
450 struct device *dev = rcar_i2c_priv_to_dev(priv);
451 u32 msr;
452
453 /*-------------- spin lock -----------------*/
454 spin_lock(&priv->lock);
455
Wolfram Sang1c176d52014-05-28 09:44:36 +0200456 msr = rcar_i2c_read(priv, ICMSR);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700457
458 /*
459 * Arbitration lost
460 */
461 if (msr & MAL) {
462 /*
463 * CAUTION
464 *
465 * When arbitration lost, device become _slave_ mode.
466 */
467 dev_dbg(dev, "Arbitration Lost\n");
468 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
469 goto out;
470 }
471
472 /*
473 * Stop
474 */
475 if (msr & MST) {
476 dev_dbg(dev, "Stop\n");
477 rcar_i2c_flags_set(priv, ID_DONE);
478 goto out;
479 }
480
481 /*
482 * Nack
483 */
484 if (msr & MNR) {
485 dev_dbg(dev, "Nack\n");
486
487 /* go to stop phase */
Wolfram Sang4f443a82014-05-28 09:44:38 +0200488 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700489 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_STOP);
490 rcar_i2c_flags_set(priv, ID_NACK);
491 goto out;
492 }
493
494 /*
495 * recv/send
496 */
497 if (rcar_i2c_is_recv(priv))
498 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
499 else
500 rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
501
502out:
503 if (rcar_i2c_flags_has(priv, ID_DONE)) {
504 rcar_i2c_irq_mask(priv, RCAR_IRQ_CLOSE);
505 rcar_i2c_status_clear(priv);
506 wake_up(&priv->wait);
507 }
508
509 spin_unlock(&priv->lock);
510 /*-------------- spin unlock -----------------*/
511
512 return IRQ_HANDLED;
513}
514
515static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
516 struct i2c_msg *msgs,
517 int num)
518{
519 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
520 struct device *dev = rcar_i2c_priv_to_dev(priv);
521 unsigned long flags;
522 int i, ret, timeout;
523
524 pm_runtime_get_sync(dev);
525
526 /*-------------- spin lock -----------------*/
527 spin_lock_irqsave(&priv->lock, flags);
528
529 rcar_i2c_init(priv);
Wolfram Sang1c176d52014-05-28 09:44:36 +0200530 /* start clock */
531 rcar_i2c_write(priv, ICCCR, priv->icccr);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700532
533 spin_unlock_irqrestore(&priv->lock, flags);
534 /*-------------- spin unlock -----------------*/
535
536 ret = -EINVAL;
537 for (i = 0; i < num; i++) {
Wolfram Sangd7653962014-05-05 18:36:21 +0200538 /* This HW can't send STOP after address phase */
539 if (msgs[i].len == 0) {
540 ret = -EOPNOTSUPP;
541 break;
542 }
543
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700544 /*-------------- spin lock -----------------*/
545 spin_lock_irqsave(&priv->lock, flags);
546
547 /* init each data */
548 priv->msg = &msgs[i];
549 priv->pos = 0;
550 priv->flags = 0;
551 if (priv->msg == &msgs[num - 1])
552 rcar_i2c_flags_set(priv, ID_LAST_MSG);
553
554 /* start send/recv */
555 if (rcar_i2c_is_recv(priv))
556 ret = rcar_i2c_recv(priv);
557 else
558 ret = rcar_i2c_send(priv);
559
560 spin_unlock_irqrestore(&priv->lock, flags);
561 /*-------------- spin unlock -----------------*/
562
563 if (ret < 0)
564 break;
565
566 /*
567 * wait result
568 */
569 timeout = wait_event_timeout(priv->wait,
570 rcar_i2c_flags_has(priv, ID_DONE),
571 5 * HZ);
572 if (!timeout) {
573 ret = -ETIMEDOUT;
574 break;
575 }
576
577 /*
578 * error handling
579 */
580 if (rcar_i2c_flags_has(priv, ID_NACK)) {
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000581 ret = -ENXIO;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700582 break;
583 }
584
585 if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
586 ret = -EAGAIN;
587 break;
588 }
589
590 if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
591 ret = -EIO;
592 break;
593 }
594
595 ret = i + 1; /* The number of transfer */
596 }
597
598 pm_runtime_put(dev);
599
Ben Dooks6ff4b1052014-01-26 16:05:37 +0000600 if (ret < 0 && ret != -ENXIO)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700601 dev_err(dev, "error %d : %x\n", ret, priv->flags);
602
603 return ret;
604}
605
606static u32 rcar_i2c_func(struct i2c_adapter *adap)
607{
Wolfram Sangd7653962014-05-05 18:36:21 +0200608 /* This HW can't do SMBUS_QUICK and NOSTART */
609 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700610}
611
612static const struct i2c_algorithm rcar_i2c_algo = {
613 .master_xfer = rcar_i2c_master_xfer,
614 .functionality = rcar_i2c_func,
615};
616
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200617static const struct of_device_id rcar_i2c_dt_ids[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700618 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
619 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
620 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
621 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sange8936452014-02-20 09:03:20 +0100622 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
Wolfram Sang819a3952014-05-27 14:06:28 +0200623 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
624 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
625 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200626 {},
627};
628MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
629
Bill Pemberton0b255e92012-11-27 15:59:38 -0500630static int rcar_i2c_probe(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700631{
Jingoo Han6d4028c2013-07-30 16:59:33 +0900632 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700633 struct rcar_i2c_priv *priv;
634 struct i2c_adapter *adap;
635 struct resource *res;
636 struct device *dev = &pdev->dev;
637 u32 bus_speed;
Wolfram Sang93e953d2014-05-28 09:44:37 +0200638 int irq, ret;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700639
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700640 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
641 if (!priv) {
642 dev_err(dev, "no mem for private data\n");
643 return -ENOMEM;
644 }
645
Ben Dooksbc8120f2014-01-26 16:05:35 +0000646 priv->clk = devm_clk_get(dev, NULL);
647 if (IS_ERR(priv->clk)) {
648 dev_err(dev, "cannot get clock\n");
649 return PTR_ERR(priv->clk);
650 }
651
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700652 bus_speed = 100000; /* default 100 kHz */
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200653 ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
654 if (ret < 0 && pdata && pdata->bus_speed)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700655 bus_speed = pdata->bus_speed;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900656
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200657 if (pdev->dev.of_node)
658 priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
659 dev)->data;
660 else
661 priv->devtype = platform_get_device_id(pdev)->driver_data;
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900662
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
Wolfram Sang93e953d2014-05-28 09:44:37 +0200672 irq = platform_get_irq(pdev, 0);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700673 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;
Wolfram Sang96c4b6b2014-02-10 11:04:06 +0100679 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD | I2C_CLASS_DEPRECATED;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700680 adap->retries = 3;
681 adap->dev.parent = dev;
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200682 adap->dev.of_node = dev->of_node;
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700683 i2c_set_adapdata(adap, priv);
684 strlcpy(adap->name, pdev->name, sizeof(adap->name));
685
Wolfram Sang93e953d2014-05-28 09:44:37 +0200686 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700687 dev_name(dev), priv);
688 if (ret < 0) {
Wolfram Sang93e953d2014-05-28 09:44:37 +0200689 dev_err(dev, "cannot get irq %d\n", irq);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700690 return ret;
691 }
692
693 ret = i2c_add_numbered_adapter(adap);
694 if (ret < 0) {
695 dev_err(dev, "reg adap failed: %d\n", ret);
696 return ret;
697 }
698
699 pm_runtime_enable(dev);
700 platform_set_drvdata(pdev, priv);
701
702 dev_info(dev, "probed\n");
703
704 return 0;
705}
706
Bill Pemberton0b255e92012-11-27 15:59:38 -0500707static int rcar_i2c_remove(struct platform_device *pdev)
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700708{
709 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
710 struct device *dev = &pdev->dev;
711
712 i2c_del_adapter(&priv->adap);
713 pm_runtime_disable(dev);
714
715 return 0;
716}
717
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900718static struct platform_device_id rcar_i2c_id_table[] = {
Kuninori Morimoto043a3f12013-10-21 01:04:32 -0700719 { "i2c-rcar", I2C_RCAR_GEN1 },
720 { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
721 { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900722 {},
723};
724MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
725
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100726static struct platform_driver rcar_i2c_driver = {
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700727 .driver = {
728 .name = "i2c-rcar",
729 .owner = THIS_MODULE,
Guennadi Liakhovetski7679c0e2013-09-12 14:36:46 +0200730 .of_match_table = rcar_i2c_dt_ids,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700731 },
732 .probe = rcar_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500733 .remove = rcar_i2c_remove,
Nguyen Viet Dungb7204232013-09-03 09:09:25 +0900734 .id_table = rcar_i2c_id_table,
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700735};
736
Wolfram Sang45fd5e42012-11-13 11:24:15 +0100737module_platform_driver(rcar_i2c_driver);
Kuninori Morimoto6ccbe602012-09-27 23:44:25 -0700738
739MODULE_LICENSE("GPL");
740MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
741MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");