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