i2c: ocores: support big-endian register layout
[deliverable/linux.git] / drivers / i2c / busses / i2c-rcar.c
CommitLineData
6ccbe607 1/*
3d99beab 2 * Driver for the Renesas RCar I2C unit
6ccbe607 3 *
708ca408
WS
4 * Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com>
5 * Copyright (C) 2011-2015 Renesas Electronics Corporation
3d99beab
WS
6 *
7 * Copyright (C) 2012-14 Renesas Solutions Corp.
6ccbe607
KM
8 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 *
10 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
11 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
12 *
6ccbe607
KM
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
3d99beab 15 * the Free Software Foundation; version 2 of the License.
6ccbe607
KM
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.
6ccbe607
KM
21 */
22#include <linux/clk.h>
23#include <linux/delay.h>
24#include <linux/err.h>
6ccbe607
KM
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/i2c.h>
28#include <linux/i2c/i2c-rcar.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
7679c0e1 31#include <linux/of_device.h>
6ccbe607
KM
32#include <linux/platform_device.h>
33#include <linux/pm_runtime.h>
34#include <linux/slab.h>
6ccbe607
KM
35
36/* register offsets */
37#define ICSCR 0x00 /* slave ctrl */
38#define ICMCR 0x04 /* master ctrl */
39#define ICSSR 0x08 /* slave status */
40#define ICMSR 0x0C /* master status */
41#define ICSIER 0x10 /* slave irq enable */
42#define ICMIER 0x14 /* master irq enable */
43#define ICCCR 0x18 /* clock dividers */
44#define ICSAR 0x1C /* slave address */
45#define ICMAR 0x20 /* master address */
46#define ICRXTX 0x24 /* data port */
47
de20d185
WS
48/* ICSCR */
49#define SDBS (1 << 3) /* slave data buffer select */
50#define SIE (1 << 2) /* slave interface enable */
51#define GCAE (1 << 1) /* general call address enable */
52#define FNA (1 << 0) /* forced non acknowledgment */
53
6ccbe607
KM
54/* ICMCR */
55#define MDBS (1 << 7) /* non-fifo mode switch */
56#define FSCL (1 << 6) /* override SCL pin */
57#define FSDA (1 << 5) /* override SDA pin */
58#define OBPC (1 << 4) /* override pins */
59#define MIE (1 << 3) /* master if enable */
60#define TSBE (1 << 2)
61#define FSB (1 << 1) /* force stop bit */
62#define ESG (1 << 0) /* en startbit gen */
63
de20d185
WS
64/* ICSSR (also for ICSIER) */
65#define GCAR (1 << 6) /* general call received */
66#define STM (1 << 5) /* slave transmit mode */
67#define SSR (1 << 4) /* stop received */
68#define SDE (1 << 3) /* slave data empty */
69#define SDT (1 << 2) /* slave data transmitted */
70#define SDR (1 << 1) /* slave data received */
71#define SAR (1 << 0) /* slave addr received */
72
3e3aabac 73/* ICMSR (also for ICMIE) */
6ccbe607
KM
74#define MNR (1 << 6) /* nack received */
75#define MAL (1 << 5) /* arbitration lost */
76#define MST (1 << 4) /* sent a stop */
77#define MDE (1 << 3)
78#define MDT (1 << 2)
79#define MDR (1 << 1)
80#define MAT (1 << 0) /* slave addr xfer done */
81
6ccbe607 82
4f443a8a
WS
83#define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
84#define RCAR_BUS_PHASE_DATA (MDBS | MIE)
e5a7effa 85#define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF)
4f443a8a 86#define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
6ccbe607 87
3e3aabac
WS
88#define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
89#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
90#define RCAR_IRQ_STOP (MST)
6ccbe607 91
938916fb
SS
92#define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
93#define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
3c95de67 94
6ccbe607 95#define ID_LAST_MSG (1 << 0)
6ccbe607
KM
96#define ID_DONE (1 << 2)
97#define ID_ARBLOST (1 << 3)
98#define ID_NACK (1 << 4)
99
b720423a 100enum rcar_i2c_type {
043a3f11
KM
101 I2C_RCAR_GEN1,
102 I2C_RCAR_GEN2,
b720423a
NVD
103};
104
6ccbe607
KM
105struct rcar_i2c_priv {
106 void __iomem *io;
107 struct i2c_adapter adap;
344beeb2
WS
108 struct i2c_msg *msg;
109 int msgs_left;
bc8120f1 110 struct clk *clk;
6ccbe607 111
6ccbe607
KM
112 wait_queue_head_t wait;
113
114 int pos;
6ccbe607
KM
115 u32 icccr;
116 u32 flags;
51371cdc 117 enum rcar_i2c_type devtype;
de20d185 118 struct i2c_client *slave;
6ccbe607
KM
119};
120
121#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
122#define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
123
124#define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
125#define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
126
127#define LOOP_TIMEOUT 1024
128
51371cdc 129
6ccbe607
KM
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{
6ccbe607
KM
142 /* reset master mode */
143 rcar_i2c_write(priv, ICMIER, 0);
93c659d8 144 rcar_i2c_write(priv, ICMCR, MDBS);
6ccbe607 145 rcar_i2c_write(priv, ICMSR, 0);
93c659d8
WS
146 /* start clock */
147 rcar_i2c_write(priv, ICCCR, priv->icccr);
6ccbe607
KM
148}
149
6ccbe607
KM
150static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
151{
152 int i;
153
154 for (i = 0; i < LOOP_TIMEOUT; i++) {
155 /* make sure that bus is not busy */
156 if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
157 return 0;
158 udelay(1);
159 }
160
161 return -EBUSY;
162}
163
6ccbe607
KM
164static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
165 u32 bus_speed,
166 struct device *dev)
167{
6ccbe607
KM
168 u32 scgd, cdf;
169 u32 round, ick;
170 u32 scl;
b720423a 171 u32 cdf_width;
8d049403 172 unsigned long rate;
6ccbe607 173
b720423a 174 switch (priv->devtype) {
043a3f11 175 case I2C_RCAR_GEN1:
b720423a
NVD
176 cdf_width = 2;
177 break;
043a3f11 178 case I2C_RCAR_GEN2:
b720423a
NVD
179 cdf_width = 3;
180 break;
181 default:
182 dev_err(dev, "device type error\n");
183 return -EIO;
184 }
185
6ccbe607
KM
186 /*
187 * calculate SCL clock
188 * see
189 * ICCCR
190 *
191 * ick = clkp / (1 + CDF)
192 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
193 *
194 * ick : I2C internal clock < 20 MHz
195 * ticf : I2C SCL falling time = 35 ns here
196 * tr : I2C SCL rising time = 200 ns here
197 * intd : LSI internal delay = 50 ns here
198 * clkp : peripheral_clk
199 * F[] : integer up-valuation
200 */
bc8120f1 201 rate = clk_get_rate(priv->clk);
8d049403 202 cdf = rate / 20000000;
22762ccb 203 if (cdf >= 1U << cdf_width) {
8d049403
GL
204 dev_err(dev, "Input clock %lu too high\n", rate);
205 return -EIO;
6ccbe607 206 }
8d049403 207 ick = rate / (cdf + 1);
6ccbe607 208
6ccbe607
KM
209 /*
210 * it is impossible to calculate large scale
211 * number on u32. separate it
212 *
213 * F[(ticf + tr + intd) * ick]
214 * = F[(35 + 200 + 50)ns * ick]
215 * = F[285 * ick / 1000000000]
216 * = F[(ick / 1000000) * 285 / 1000]
217 */
218 round = (ick + 500000) / 1000000 * 285;
219 round = (round + 500) / 1000;
220
221 /*
222 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
223 *
224 * Calculation result (= SCL) should be less than
225 * bus_speed for hardware safety
8d049403
GL
226 *
227 * We could use something along the lines of
228 * div = ick / (bus_speed + 1) + 1;
229 * scgd = (div - 20 - round + 7) / 8;
230 * scl = ick / (20 + (scgd * 8) + round);
231 * (not fully verified) but that would get pretty involved
6ccbe607
KM
232 */
233 for (scgd = 0; scgd < 0x40; scgd++) {
234 scl = ick / (20 + (scgd * 8) + round);
235 if (scl <= bus_speed)
236 goto scgd_find;
237 }
238 dev_err(dev, "it is impossible to calculate best SCL\n");
239 return -EIO;
240
241scgd_find:
242 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
bc8120f1 243 scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
6ccbe607 244
708ca408 245 /* keep icccr value */
14d32f17 246 priv->icccr = scgd << cdf_width | cdf;
6ccbe607
KM
247
248 return 0;
249}
250
7c7117ff 251static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
6ccbe607 252{
386babf8 253 int read = !!rcar_i2c_is_recv(priv);
6ccbe607 254
344beeb2
WS
255 priv->pos = 0;
256 priv->flags = 0;
257 if (priv->msgs_left == 1)
258 rcar_i2c_flags_set(priv, ID_LAST_MSG);
259
386babf8 260 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
3c95de67 261 rcar_i2c_write(priv, ICMSR, 0);
4f443a8a 262 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
386babf8 263 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
6ccbe607
KM
264}
265
2bc3c5a8
WS
266static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
267{
268 priv->msg++;
269 priv->msgs_left--;
270 rcar_i2c_prepare_msg(priv);
271}
272
6ccbe607
KM
273/*
274 * interrupt functions
275 */
276static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
277{
278 struct i2c_msg *msg = priv->msg;
279
708ca408 280 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
6ccbe607
KM
281 if (!(msr & MDE))
282 return 0;
283
6ccbe607
KM
284 if (priv->pos < msg->len) {
285 /*
286 * Prepare next data to ICRXTX register.
287 * This data will go to _SHIFT_ register.
288 *
289 * *
290 * [ICRXTX] -> [SHIFT] -> [I2C bus]
291 */
292 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
293 priv->pos++;
294
295 } else {
296 /*
297 * The last data was pushed to ICRXTX on _PREV_ empty irq.
298 * It is on _SHIFT_ register, and will sent to I2C bus.
299 *
300 * *
301 * [ICRXTX] -> [SHIFT] -> [I2C bus]
302 */
303
2bc3c5a8 304 if (priv->flags & ID_LAST_MSG) {
6ccbe607
KM
305 /*
306 * If current msg is the _LAST_ msg,
307 * prepare stop condition here.
308 * ID_DONE will be set on STOP irq.
309 */
4f443a8a 310 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
2bc3c5a8
WS
311 } else {
312 rcar_i2c_next_msg(priv);
313 return 0;
314 }
6ccbe607
KM
315 }
316
3c95de67 317 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
6ccbe607
KM
318
319 return 0;
320}
321
322static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
323{
324 struct i2c_msg *msg = priv->msg;
325
708ca408 326 /* FIXME: sometimes, unknown interrupt happened. Do nothing */
6ccbe607
KM
327 if (!(msr & MDR))
328 return 0;
329
330 if (msr & MAT) {
e5a7effa 331 /* Address transfer phase finished, but no data at this point. */
6ccbe607 332 } else if (priv->pos < msg->len) {
708ca408 333 /* get received data */
6ccbe607
KM
334 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
335 priv->pos++;
336 }
337
338 /*
708ca408
WS
339 * If next received data is the _LAST_, go to STOP phase. Might be
340 * overwritten by REP START when setting up a new msg. Not elegant
341 * but the only stable sequence for REP START I have found so far.
6ccbe607
KM
342 */
343 if (priv->pos + 1 >= msg->len)
4f443a8a 344 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
6ccbe607 345
2bc3c5a8
WS
346 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
347 rcar_i2c_next_msg(priv);
348 else
349 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
6ccbe607
KM
350
351 return 0;
352}
353
de20d185
WS
354static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
355{
356 u32 ssr_raw, ssr_filtered;
357 u8 value;
358
359 ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
360 ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
361
362 if (!ssr_filtered)
363 return false;
364
365 /* address detected */
366 if (ssr_filtered & SAR) {
367 /* read or write request */
368 if (ssr_raw & STM) {
5b77d162 369 i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
de20d185
WS
370 rcar_i2c_write(priv, ICRXTX, value);
371 rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
372 } else {
5b77d162 373 i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
de20d185
WS
374 rcar_i2c_read(priv, ICRXTX); /* dummy read */
375 rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
376 }
377
378 rcar_i2c_write(priv, ICSSR, ~SAR & 0xff);
379 }
380
381 /* master sent stop */
382 if (ssr_filtered & SSR) {
383 i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
384 rcar_i2c_write(priv, ICSIER, SAR | SSR);
385 rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
386 }
387
388 /* master wants to write to us */
389 if (ssr_filtered & SDR) {
390 int ret;
391
392 value = rcar_i2c_read(priv, ICRXTX);
5b77d162 393 ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
de20d185
WS
394 /* Send NACK in case of error */
395 rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
de20d185
WS
396 rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
397 }
398
399 /* master wants to read from us */
400 if (ssr_filtered & SDE) {
5b77d162 401 i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
de20d185
WS
402 rcar_i2c_write(priv, ICRXTX, value);
403 rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
404 }
405
406 return true;
407}
408
6ccbe607
KM
409static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
410{
411 struct rcar_i2c_priv *priv = ptr;
e5a7effa
WS
412 u32 msr, val;
413
414 /* Clear START or STOP as soon as we can */
415 val = rcar_i2c_read(priv, ICMCR);
416 rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
6ccbe607 417
1c176d53 418 msr = rcar_i2c_read(priv, ICMSR);
6ccbe607 419
dd318b0d
SS
420 /* Only handle interrupts that are currently enabled */
421 msr &= rcar_i2c_read(priv, ICMIER);
aa5beaf6 422 if (!msr) {
2151ba75
WS
423 if (rcar_i2c_slave_irq(priv))
424 return IRQ_HANDLED;
425
426 return IRQ_NONE;
aa5beaf6 427 }
dd318b0d 428
51371cdc 429 /* Arbitration lost */
6ccbe607 430 if (msr & MAL) {
6ccbe607
KM
431 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
432 goto out;
433 }
434
51371cdc 435 /* Nack */
6ccbe607 436 if (msr & MNR) {
315a1736 437 /* HW automatically sends STOP after received NACK */
f2382249 438 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
6ccbe607
KM
439 rcar_i2c_flags_set(priv, ID_NACK);
440 goto out;
441 }
442
dd318b0d
SS
443 /* Stop */
444 if (msr & MST) {
2bc3c5a8 445 priv->msgs_left--; /* The last message also made it */
dd318b0d
SS
446 rcar_i2c_flags_set(priv, ID_DONE);
447 goto out;
448 }
449
6ccbe607
KM
450 if (rcar_i2c_is_recv(priv))
451 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
452 else
453 rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
454
455out:
456 if (rcar_i2c_flags_has(priv, ID_DONE)) {
f2382249 457 rcar_i2c_write(priv, ICMIER, 0);
3c95de67 458 rcar_i2c_write(priv, ICMSR, 0);
6ccbe607
KM
459 wake_up(&priv->wait);
460 }
461
2151ba75 462 return IRQ_HANDLED;
6ccbe607
KM
463}
464
465static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
466 struct i2c_msg *msgs,
467 int num)
468{
469 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
470 struct device *dev = rcar_i2c_priv_to_dev(priv);
b6763d0d 471 int i, ret;
738206de 472 long time_left;
6ccbe607
KM
473
474 pm_runtime_get_sync(dev);
475
3f7de22e
WS
476 ret = rcar_i2c_bus_barrier(priv);
477 if (ret < 0)
478 goto out;
479
6ccbe607 480 for (i = 0; i < num; i++) {
d7653964
WS
481 /* This HW can't send STOP after address phase */
482 if (msgs[i].len == 0) {
483 ret = -EOPNOTSUPP;
2bc3c5a8 484 goto out;
6ccbe607 485 }
2bc3c5a8 486 }
6ccbe607 487
2bc3c5a8
WS
488 /* init data */
489 priv->msg = msgs;
490 priv->msgs_left = num;
491
492 rcar_i2c_prepare_msg(priv);
493
494 time_left = wait_event_timeout(priv->wait,
495 rcar_i2c_flags_has(priv, ID_DONE),
496 num * adap->timeout);
497 if (!time_left) {
498 rcar_i2c_init(priv);
499 ret = -ETIMEDOUT;
500 } else if (rcar_i2c_flags_has(priv, ID_NACK)) {
501 ret = -ENXIO;
502 } else if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
503 ret = -EAGAIN;
504 } else {
505 ret = num - priv->msgs_left; /* The number of transfer */
6ccbe607 506 }
3f7de22e 507out:
6ccbe607
KM
508 pm_runtime_put(dev);
509
6ff4b105 510 if (ret < 0 && ret != -ENXIO)
6ccbe607
KM
511 dev_err(dev, "error %d : %x\n", ret, priv->flags);
512
513 return ret;
514}
515
de20d185
WS
516static int rcar_reg_slave(struct i2c_client *slave)
517{
518 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
519
520 if (priv->slave)
521 return -EBUSY;
522
523 if (slave->flags & I2C_CLIENT_TEN)
524 return -EAFNOSUPPORT;
525
526 pm_runtime_forbid(rcar_i2c_priv_to_dev(priv));
527
528 priv->slave = slave;
529 rcar_i2c_write(priv, ICSAR, slave->addr);
530 rcar_i2c_write(priv, ICSSR, 0);
531 rcar_i2c_write(priv, ICSIER, SAR | SSR);
532 rcar_i2c_write(priv, ICSCR, SIE | SDBS);
533
534 return 0;
535}
536
537static int rcar_unreg_slave(struct i2c_client *slave)
538{
539 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
540
541 WARN_ON(!priv->slave);
542
543 rcar_i2c_write(priv, ICSIER, 0);
544 rcar_i2c_write(priv, ICSCR, 0);
545
546 priv->slave = NULL;
547
548 pm_runtime_allow(rcar_i2c_priv_to_dev(priv));
549
550 return 0;
551}
552
6ccbe607
KM
553static u32 rcar_i2c_func(struct i2c_adapter *adap)
554{
d7653964 555 /* This HW can't do SMBUS_QUICK and NOSTART */
1fb2ad95
WS
556 return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
557 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
6ccbe607
KM
558}
559
560static const struct i2c_algorithm rcar_i2c_algo = {
561 .master_xfer = rcar_i2c_master_xfer,
562 .functionality = rcar_i2c_func,
de20d185
WS
563 .reg_slave = rcar_reg_slave,
564 .unreg_slave = rcar_unreg_slave,
6ccbe607
KM
565};
566
7679c0e1 567static const struct of_device_id rcar_i2c_dt_ids[] = {
043a3f11
KM
568 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
569 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
570 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
571 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
e8936455 572 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
819a3951
WS
573 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
574 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
575 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
7679c0e1
GL
576 {},
577};
578MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
579
0b255e92 580static int rcar_i2c_probe(struct platform_device *pdev)
6ccbe607 581{
6d4028c6 582 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
6ccbe607
KM
583 struct rcar_i2c_priv *priv;
584 struct i2c_adapter *adap;
585 struct resource *res;
586 struct device *dev = &pdev->dev;
587 u32 bus_speed;
93e953d3 588 int irq, ret;
6ccbe607 589
6ccbe607 590 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
46797a2a 591 if (!priv)
6ccbe607 592 return -ENOMEM;
6ccbe607 593
bc8120f1
BD
594 priv->clk = devm_clk_get(dev, NULL);
595 if (IS_ERR(priv->clk)) {
596 dev_err(dev, "cannot get clock\n");
597 return PTR_ERR(priv->clk);
598 }
599
6ccbe607 600 bus_speed = 100000; /* default 100 kHz */
7679c0e1
GL
601 ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
602 if (ret < 0 && pdata && pdata->bus_speed)
6ccbe607 603 bus_speed = pdata->bus_speed;
b720423a 604
7679c0e1
GL
605 if (pdev->dev.of_node)
606 priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
607 dev)->data;
608 else
609 priv->devtype = platform_get_device_id(pdev)->driver_data;
b720423a 610
6ccbe607
KM
611 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
612 if (ret < 0)
613 return ret;
614
3cc2d009 615 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
84dbf809
TR
616 priv->io = devm_ioremap_resource(dev, res);
617 if (IS_ERR(priv->io))
618 return PTR_ERR(priv->io);
6ccbe607 619
93c659d8
WS
620 rcar_i2c_init(priv);
621
93e953d3 622 irq = platform_get_irq(pdev, 0);
6ccbe607 623 init_waitqueue_head(&priv->wait);
6ccbe607 624
929e3aba
WS
625 adap = &priv->adap;
626 adap->nr = pdev->id;
627 adap->algo = &rcar_i2c_algo;
628 adap->class = I2C_CLASS_DEPRECATED;
629 adap->retries = 3;
630 adap->dev.parent = dev;
631 adap->dev.of_node = dev->of_node;
6ccbe607
KM
632 i2c_set_adapdata(adap, priv);
633 strlcpy(adap->name, pdev->name, sizeof(adap->name));
634
93e953d3 635 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
6ccbe607
KM
636 dev_name(dev), priv);
637 if (ret < 0) {
93e953d3 638 dev_err(dev, "cannot get irq %d\n", irq);
6ccbe607
KM
639 return ret;
640 }
641
642 ret = i2c_add_numbered_adapter(adap);
643 if (ret < 0) {
644 dev_err(dev, "reg adap failed: %d\n", ret);
645 return ret;
646 }
647
648 pm_runtime_enable(dev);
649 platform_set_drvdata(pdev, priv);
650
651 dev_info(dev, "probed\n");
652
653 return 0;
654}
655
0b255e92 656static int rcar_i2c_remove(struct platform_device *pdev)
6ccbe607
KM
657{
658 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
659 struct device *dev = &pdev->dev;
660
661 i2c_del_adapter(&priv->adap);
662 pm_runtime_disable(dev);
663
664 return 0;
665}
666
e9a02a3d 667static const struct platform_device_id rcar_i2c_id_table[] = {
043a3f11
KM
668 { "i2c-rcar", I2C_RCAR_GEN1 },
669 { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
670 { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
b720423a
NVD
671 {},
672};
673MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
674
45fd5e4a 675static struct platform_driver rcar_i2c_driver = {
6ccbe607
KM
676 .driver = {
677 .name = "i2c-rcar",
7679c0e1 678 .of_match_table = rcar_i2c_dt_ids,
6ccbe607
KM
679 },
680 .probe = rcar_i2c_probe,
0b255e92 681 .remove = rcar_i2c_remove,
b720423a 682 .id_table = rcar_i2c_id_table,
6ccbe607
KM
683};
684
45fd5e4a 685module_platform_driver(rcar_i2c_driver);
6ccbe607 686
3d99beab 687MODULE_LICENSE("GPL v2");
6ccbe607
KM
688MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
689MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.170123 seconds and 5 git commands to generate.