blob: e276ffbfbd1a9e91080af391125eb547db22ab6b [file] [log] [blame]
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001/*
2 * Driver for I2C adapter in Rockchip RK3xxx SoC
3 *
4 * Max Schwarz <max.schwarz@online.de>
5 * based on the patches by Rockchip Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/i2c.h>
15#include <linux/interrupt.h>
16#include <linux/errno.h>
17#include <linux/err.h>
18#include <linux/platform_device.h>
19#include <linux/io.h>
20#include <linux/of_address.h>
21#include <linux/of_irq.h>
22#include <linux/spinlock.h>
23#include <linux/clk.h>
24#include <linux/wait.h>
25#include <linux/mfd/syscon.h>
26#include <linux/regmap.h>
addy ke0285f8f2014-10-14 14:09:21 +080027#include <linux/math64.h>
Max Schwarzc41aa3c2014-06-11 22:34:37 +020028
29
30/* Register Map */
31#define REG_CON 0x00 /* control register */
32#define REG_CLKDIV 0x04 /* clock divisor register */
33#define REG_MRXADDR 0x08 /* slave address for REGISTER_TX */
34#define REG_MRXRADDR 0x0c /* slave register address for REGISTER_TX */
35#define REG_MTXCNT 0x10 /* number of bytes to be transmitted */
36#define REG_MRXCNT 0x14 /* number of bytes to be received */
37#define REG_IEN 0x18 /* interrupt enable */
38#define REG_IPD 0x1c /* interrupt pending */
39#define REG_FCNT 0x20 /* finished count */
40
41/* Data buffer offsets */
42#define TXBUFFER_BASE 0x100
43#define RXBUFFER_BASE 0x200
44
45/* REG_CON bits */
46#define REG_CON_EN BIT(0)
47enum {
48 REG_CON_MOD_TX = 0, /* transmit data */
49 REG_CON_MOD_REGISTER_TX, /* select register and restart */
50 REG_CON_MOD_RX, /* receive data */
51 REG_CON_MOD_REGISTER_RX, /* broken: transmits read addr AND writes
52 * register addr */
53};
54#define REG_CON_MOD(mod) ((mod) << 1)
55#define REG_CON_MOD_MASK (BIT(1) | BIT(2))
56#define REG_CON_START BIT(3)
57#define REG_CON_STOP BIT(4)
58#define REG_CON_LASTACK BIT(5) /* 1: send NACK after last received byte */
59#define REG_CON_ACTACK BIT(6) /* 1: stop if NACK is received */
60
61/* REG_MRXADDR bits */
62#define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
63
64/* REG_IEN/REG_IPD bits */
65#define REG_INT_BTF BIT(0) /* a byte was transmitted */
66#define REG_INT_BRF BIT(1) /* a byte was received */
67#define REG_INT_MBTF BIT(2) /* master data transmit finished */
68#define REG_INT_MBRF BIT(3) /* master data receive finished */
69#define REG_INT_START BIT(4) /* START condition generated */
70#define REG_INT_STOP BIT(5) /* STOP condition generated */
71#define REG_INT_NAKRCV BIT(6) /* NACK received */
72#define REG_INT_ALL 0x7f
73
74/* Constants */
75#define WAIT_TIMEOUT 200 /* ms */
76#define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
77
78enum rk3x_i2c_state {
79 STATE_IDLE,
80 STATE_START,
81 STATE_READ,
82 STATE_WRITE,
83 STATE_STOP
84};
85
86/**
87 * @grf_offset: offset inside the grf regmap for setting the i2c type
88 */
89struct rk3x_i2c_soc_data {
90 int grf_offset;
91};
92
93struct rk3x_i2c {
94 struct i2c_adapter adap;
95 struct device *dev;
96 struct rk3x_i2c_soc_data *soc_data;
97
98 /* Hardware resources */
99 void __iomem *regs;
100 struct clk *clk;
101
102 /* Settings */
103 unsigned int scl_frequency;
104
105 /* Synchronization & notification */
106 spinlock_t lock;
107 wait_queue_head_t wait;
108 bool busy;
109
110 /* Current message */
111 struct i2c_msg *msg;
112 u8 addr;
113 unsigned int mode;
114 bool is_last_msg;
115
116 /* I2C state machine */
117 enum rk3x_i2c_state state;
118 unsigned int processed; /* sent/received bytes */
119 int error;
120};
121
122static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
123 unsigned int offset)
124{
125 writel(value, i2c->regs + offset);
126}
127
128static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
129{
130 return readl(i2c->regs + offset);
131}
132
133/* Reset all interrupt pending bits */
134static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
135{
136 i2c_writel(i2c, REG_INT_ALL, REG_IPD);
137}
138
139/**
140 * Generate a START condition, which triggers a REG_INT_START interrupt.
141 */
142static void rk3x_i2c_start(struct rk3x_i2c *i2c)
143{
144 u32 val;
145
146 rk3x_i2c_clean_ipd(i2c);
147 i2c_writel(i2c, REG_INT_START, REG_IEN);
148
149 /* enable adapter with correct mode, send START condition */
150 val = REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
151
152 /* if we want to react to NACK, set ACTACK bit */
153 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
154 val |= REG_CON_ACTACK;
155
156 i2c_writel(i2c, val, REG_CON);
157}
158
159/**
160 * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
161 *
162 * @error: Error code to return in rk3x_i2c_xfer
163 */
164static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
165{
166 unsigned int ctrl;
167
168 i2c->processed = 0;
169 i2c->msg = NULL;
170 i2c->error = error;
171
172 if (i2c->is_last_msg) {
173 /* Enable stop interrupt */
174 i2c_writel(i2c, REG_INT_STOP, REG_IEN);
175
176 i2c->state = STATE_STOP;
177
178 ctrl = i2c_readl(i2c, REG_CON);
179 ctrl |= REG_CON_STOP;
180 i2c_writel(i2c, ctrl, REG_CON);
181 } else {
182 /* Signal rk3x_i2c_xfer to start the next message. */
183 i2c->busy = false;
184 i2c->state = STATE_IDLE;
185
186 /*
187 * The HW is actually not capable of REPEATED START. But we can
188 * get the intended effect by resetting its internal state
189 * and issuing an ordinary START.
190 */
191 i2c_writel(i2c, 0, REG_CON);
192
193 /* signal that we are finished with the current msg */
194 wake_up(&i2c->wait);
195 }
196}
197
198/**
199 * Setup a read according to i2c->msg
200 */
201static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
202{
203 unsigned int len = i2c->msg->len - i2c->processed;
204 u32 con;
205
206 con = i2c_readl(i2c, REG_CON);
207
208 /*
209 * The hw can read up to 32 bytes at a time. If we need more than one
210 * chunk, send an ACK after the last byte of the current chunk.
211 */
Doug Anderson29209332014-08-22 10:43:44 -0700212 if (len > 32) {
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200213 len = 32;
214 con &= ~REG_CON_LASTACK;
215 } else {
216 con |= REG_CON_LASTACK;
217 }
218
219 /* make sure we are in plain RX mode if we read a second chunk */
220 if (i2c->processed != 0) {
221 con &= ~REG_CON_MOD_MASK;
222 con |= REG_CON_MOD(REG_CON_MOD_RX);
223 }
224
225 i2c_writel(i2c, con, REG_CON);
226 i2c_writel(i2c, len, REG_MRXCNT);
227}
228
229/**
230 * Fill the transmit buffer with data from i2c->msg
231 */
232static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
233{
234 unsigned int i, j;
235 u32 cnt = 0;
236 u32 val;
237 u8 byte;
238
239 for (i = 0; i < 8; ++i) {
240 val = 0;
241 for (j = 0; j < 4; ++j) {
Alexandru M Stancf270202014-10-01 10:40:41 -0700242 if ((i2c->processed == i2c->msg->len) && (cnt != 0))
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200243 break;
244
245 if (i2c->processed == 0 && cnt == 0)
246 byte = (i2c->addr & 0x7f) << 1;
247 else
248 byte = i2c->msg->buf[i2c->processed++];
249
250 val |= byte << (j * 8);
251 cnt++;
252 }
253
254 i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
255
256 if (i2c->processed == i2c->msg->len)
257 break;
258 }
259
260 i2c_writel(i2c, cnt, REG_MTXCNT);
261}
262
263
264/* IRQ handlers for individual states */
265
266static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
267{
268 if (!(ipd & REG_INT_START)) {
269 rk3x_i2c_stop(i2c, -EIO);
270 dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
271 rk3x_i2c_clean_ipd(i2c);
272 return;
273 }
274
275 /* ack interrupt */
276 i2c_writel(i2c, REG_INT_START, REG_IPD);
277
278 /* disable start bit */
279 i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
280
281 /* enable appropriate interrupts and transition */
282 if (i2c->mode == REG_CON_MOD_TX) {
283 i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
284 i2c->state = STATE_WRITE;
285 rk3x_i2c_fill_transmit_buf(i2c);
286 } else {
287 /* in any other case, we are going to be reading. */
288 i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
289 i2c->state = STATE_READ;
290 rk3x_i2c_prepare_read(i2c);
291 }
292}
293
294static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
295{
296 if (!(ipd & REG_INT_MBTF)) {
297 rk3x_i2c_stop(i2c, -EIO);
298 dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
299 rk3x_i2c_clean_ipd(i2c);
300 return;
301 }
302
303 /* ack interrupt */
304 i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
305
306 /* are we finished? */
307 if (i2c->processed == i2c->msg->len)
308 rk3x_i2c_stop(i2c, i2c->error);
309 else
310 rk3x_i2c_fill_transmit_buf(i2c);
311}
312
313static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
314{
315 unsigned int i;
316 unsigned int len = i2c->msg->len - i2c->processed;
317 u32 uninitialized_var(val);
318 u8 byte;
319
320 /* we only care for MBRF here. */
321 if (!(ipd & REG_INT_MBRF))
322 return;
323
324 /* ack interrupt */
325 i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
326
addy ke5da43092014-08-23 02:00:52 +0800327 /* Can only handle a maximum of 32 bytes at a time */
328 if (len > 32)
329 len = 32;
330
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200331 /* read the data from receive buffer */
332 for (i = 0; i < len; ++i) {
333 if (i % 4 == 0)
334 val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
335
336 byte = (val >> ((i % 4) * 8)) & 0xff;
337 i2c->msg->buf[i2c->processed++] = byte;
338 }
339
340 /* are we finished? */
341 if (i2c->processed == i2c->msg->len)
342 rk3x_i2c_stop(i2c, i2c->error);
343 else
344 rk3x_i2c_prepare_read(i2c);
345}
346
347static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
348{
349 unsigned int con;
350
351 if (!(ipd & REG_INT_STOP)) {
352 rk3x_i2c_stop(i2c, -EIO);
353 dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
354 rk3x_i2c_clean_ipd(i2c);
355 return;
356 }
357
358 /* ack interrupt */
359 i2c_writel(i2c, REG_INT_STOP, REG_IPD);
360
361 /* disable STOP bit */
362 con = i2c_readl(i2c, REG_CON);
363 con &= ~REG_CON_STOP;
364 i2c_writel(i2c, con, REG_CON);
365
366 i2c->busy = false;
367 i2c->state = STATE_IDLE;
368
369 /* signal rk3x_i2c_xfer that we are finished */
370 wake_up(&i2c->wait);
371}
372
373static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
374{
375 struct rk3x_i2c *i2c = dev_id;
376 unsigned int ipd;
377
378 spin_lock(&i2c->lock);
379
380 ipd = i2c_readl(i2c, REG_IPD);
381 if (i2c->state == STATE_IDLE) {
382 dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
383 rk3x_i2c_clean_ipd(i2c);
384 goto out;
385 }
386
387 dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
388
389 /* Clean interrupt bits we don't care about */
390 ipd &= ~(REG_INT_BRF | REG_INT_BTF);
391
392 if (ipd & REG_INT_NAKRCV) {
393 /*
394 * We got a NACK in the last operation. Depending on whether
395 * IGNORE_NAK is set, we have to stop the operation and report
396 * an error.
397 */
398 i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
399
400 ipd &= ~REG_INT_NAKRCV;
401
402 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
403 rk3x_i2c_stop(i2c, -ENXIO);
404 }
405
406 /* is there anything left to handle? */
Doug Anderson29209332014-08-22 10:43:44 -0700407 if ((ipd & REG_INT_ALL) == 0)
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200408 goto out;
409
410 switch (i2c->state) {
411 case STATE_START:
412 rk3x_i2c_handle_start(i2c, ipd);
413 break;
414 case STATE_WRITE:
415 rk3x_i2c_handle_write(i2c, ipd);
416 break;
417 case STATE_READ:
418 rk3x_i2c_handle_read(i2c, ipd);
419 break;
420 case STATE_STOP:
421 rk3x_i2c_handle_stop(i2c, ipd);
422 break;
423 case STATE_IDLE:
424 break;
425 }
426
427out:
428 spin_unlock(&i2c->lock);
429 return IRQ_HANDLED;
430}
431
addy ke0285f8f2014-10-14 14:09:21 +0800432static int rk3x_i2c_calc_divs(unsigned long i2c_rate, unsigned long scl_rate,
433 unsigned long *div_low, unsigned long *div_high)
434{
435 unsigned long min_low_ns, min_high_ns;
436 unsigned long max_data_hold_ns;
437 unsigned long data_hold_buffer_ns;
438 unsigned long max_low_ns, min_total_ns;
439
440 unsigned long i2c_rate_khz, scl_rate_khz;
441
442 unsigned long min_low_div, min_high_div;
443 unsigned long max_low_div;
444
445 unsigned long min_div_for_hold, min_total_div;
446 unsigned long extra_div, extra_low_div, ideal_low_div;
447
448 /* Only support standard-mode and fast-mode */
449 if (WARN_ON(scl_rate > 400000))
450 scl_rate = 400000;
451
452 /* prevent scl_rate_khz from becoming 0 */
453 if (WARN_ON(scl_rate < 1000))
454 scl_rate = 1000;
455
456 /*
457 * min_low_ns: The minimum number of ns we need to hold low
458 * to meet i2c spec
459 * min_high_ns: The minimum number of ns we need to hold high
460 * to meet i2c spec
461 * max_low_ns: The maximum number of ns we can hold low
462 * to meet i2c spec
463 *
464 * Note: max_low_ns should be (max data hold time * 2 - buffer)
465 * This is because the i2c host on Rockchip holds the data line
466 * for half the low time.
467 */
468 if (scl_rate <= 100000) {
469 min_low_ns = 4700;
470 min_high_ns = 4000;
471 max_data_hold_ns = 3450;
472 data_hold_buffer_ns = 50;
473 } else {
474 min_low_ns = 1300;
475 min_high_ns = 600;
476 max_data_hold_ns = 900;
477 data_hold_buffer_ns = 50;
478 }
479 max_low_ns = max_data_hold_ns * 2 - data_hold_buffer_ns;
480 min_total_ns = min_low_ns + min_high_ns;
481
482 /* Adjust to avoid overflow */
483 i2c_rate_khz = DIV_ROUND_UP(i2c_rate, 1000);
484 scl_rate_khz = scl_rate / 1000;
485
486 /*
487 * We need the total div to be >= this number
488 * so we don't clock too fast.
489 */
490 min_total_div = DIV_ROUND_UP(i2c_rate_khz, scl_rate_khz * 8);
491
492 /* These are the min dividers needed for min hold times. */
493 min_low_div = DIV_ROUND_UP(i2c_rate_khz * min_low_ns, 8 * 1000000);
494 min_high_div = DIV_ROUND_UP(i2c_rate_khz * min_high_ns, 8 * 1000000);
495 min_div_for_hold = (min_low_div + min_high_div);
496
497 /*
498 * This is the maximum divider so we don't go over the max.
499 * We don't round up here (we round down) since this is a max.
500 */
501 max_low_div = i2c_rate_khz * max_low_ns / (8 * 1000000);
502
503 if (min_low_div > max_low_div) {
504 WARN_ONCE(true,
505 "Conflicting, min_low_div %lu, max_low_div %lu\n",
506 min_low_div, max_low_div);
507 max_low_div = min_low_div;
508 }
509
510 if (min_div_for_hold > min_total_div) {
511 /*
512 * Time needed to meet hold requirements is important.
513 * Just use that.
514 */
515 *div_low = min_low_div;
516 *div_high = min_high_div;
517 } else {
518 /*
519 * We've got to distribute some time among the low and high
520 * so we don't run too fast.
521 */
522 extra_div = min_total_div - min_div_for_hold;
523
524 /*
525 * We'll try to split things up perfectly evenly,
526 * biasing slightly towards having a higher div
527 * for low (spend more time low).
528 */
529 ideal_low_div = DIV_ROUND_UP(i2c_rate_khz * min_low_ns,
530 scl_rate_khz * 8 * min_total_ns);
531
532 /* Don't allow it to go over the max */
533 if (ideal_low_div > max_low_div)
534 ideal_low_div = max_low_div;
535
536 /*
537 * Handle when the ideal low div is going to take up
538 * more than we have.
539 */
540 if (ideal_low_div > min_low_div + extra_div)
541 ideal_low_div = min_low_div + extra_div;
542
543 /* Give low the "ideal" and give high whatever extra is left */
544 extra_low_div = ideal_low_div - min_low_div;
545 *div_low = ideal_low_div;
546 *div_high = min_high_div + (extra_div - extra_low_div);
547 }
548
549 /*
550 * Adjust to the fact that the hardware has an implicit "+1".
551 * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
552 */
553 *div_low = *div_low - 1;
554 *div_high = *div_high - 1;
555
556 if (*div_low >= 0xffff || *div_high >= 0xffff)
557 return -EINVAL;
558 else
559 return 0;
560}
561
562static int rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200563{
564 unsigned long i2c_rate = clk_get_rate(i2c->clk);
addy ke0285f8f2014-10-14 14:09:21 +0800565 unsigned long div_low, div_high;
566 u64 t_low_ns, t_high_ns;
567 int ret = 0;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200568
addy ke0285f8f2014-10-14 14:09:21 +0800569 ret = rk3x_i2c_calc_divs(i2c_rate, scl_rate, &div_low, &div_high);
570 if (ret < 0)
571 return ret;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200572
addy ke0285f8f2014-10-14 14:09:21 +0800573 i2c_writel(i2c, (div_high << 16) | (div_low & 0xffff), REG_CLKDIV);
574
575 t_low_ns = div_u64(((u64)div_low + 1) * 8 * 1000000000, i2c_rate);
576 t_high_ns = div_u64(((u64)div_high + 1) * 8 * 1000000000, i2c_rate);
577 dev_dbg(i2c->dev,
578 "CLK %lukhz, Req %luns, Act low %lluns high %lluns\n",
579 i2c_rate / 1000,
580 1000000000 / scl_rate,
581 t_low_ns, t_high_ns);
582
583 return ret;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200584}
585
586/**
587 * Setup I2C registers for an I2C operation specified by msgs, num.
588 *
589 * Must be called with i2c->lock held.
590 *
591 * @msgs: I2C msgs to process
592 * @num: Number of msgs
593 *
594 * returns: Number of I2C msgs processed or negative in case of error
595 */
596static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
597{
598 u32 addr = (msgs[0].addr & 0x7f) << 1;
599 int ret = 0;
600
601 /*
602 * The I2C adapter can issue a small (len < 4) write packet before
603 * reading. This speeds up SMBus-style register reads.
604 * The MRXADDR/MRXRADDR hold the slave address and the slave register
605 * address in this case.
606 */
607
608 if (num >= 2 && msgs[0].len < 4 &&
609 !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
610 u32 reg_addr = 0;
611 int i;
612
613 dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
614 addr >> 1);
615
616 /* Fill MRXRADDR with the register address(es) */
617 for (i = 0; i < msgs[0].len; ++i) {
618 reg_addr |= msgs[0].buf[i] << (i * 8);
619 reg_addr |= REG_MRXADDR_VALID(i);
620 }
621
622 /* msgs[0] is handled by hw. */
623 i2c->msg = &msgs[1];
624
625 i2c->mode = REG_CON_MOD_REGISTER_TX;
626
627 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
628 i2c_writel(i2c, reg_addr, REG_MRXRADDR);
629
630 ret = 2;
631 } else {
632 /*
633 * We'll have to do it the boring way and process the msgs
634 * one-by-one.
635 */
636
637 if (msgs[0].flags & I2C_M_RD) {
638 addr |= 1; /* set read bit */
639
640 /*
641 * We have to transmit the slave addr first. Use
642 * MOD_REGISTER_TX for that purpose.
643 */
644 i2c->mode = REG_CON_MOD_REGISTER_TX;
645 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
646 REG_MRXADDR);
647 i2c_writel(i2c, 0, REG_MRXRADDR);
648 } else {
649 i2c->mode = REG_CON_MOD_TX;
650 }
651
652 i2c->msg = &msgs[0];
653
654 ret = 1;
655 }
656
657 i2c->addr = msgs[0].addr;
658 i2c->busy = true;
659 i2c->state = STATE_START;
660 i2c->processed = 0;
661 i2c->error = 0;
662
663 rk3x_i2c_clean_ipd(i2c);
664
665 return ret;
666}
667
668static int rk3x_i2c_xfer(struct i2c_adapter *adap,
669 struct i2c_msg *msgs, int num)
670{
671 struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
672 unsigned long timeout, flags;
673 int ret = 0;
674 int i;
675
676 spin_lock_irqsave(&i2c->lock, flags);
677
678 clk_enable(i2c->clk);
679
680 /* The clock rate might have changed, so setup the divider again */
addy ke0285f8f2014-10-14 14:09:21 +0800681 ret = rk3x_i2c_set_scl_rate(i2c, i2c->scl_frequency);
682 if (ret < 0)
683 goto exit;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200684
685 i2c->is_last_msg = false;
686
687 /*
688 * Process msgs. We can handle more than one message at once (see
689 * rk3x_i2c_setup()).
690 */
691 for (i = 0; i < num; i += ret) {
692 ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
693
694 if (ret < 0) {
695 dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
696 break;
697 }
698
699 if (i + ret >= num)
700 i2c->is_last_msg = true;
701
702 spin_unlock_irqrestore(&i2c->lock, flags);
703
704 rk3x_i2c_start(i2c);
705
706 timeout = wait_event_timeout(i2c->wait, !i2c->busy,
707 msecs_to_jiffies(WAIT_TIMEOUT));
708
709 spin_lock_irqsave(&i2c->lock, flags);
710
711 if (timeout == 0) {
712 dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
713 i2c_readl(i2c, REG_IPD), i2c->state);
714
715 /* Force a STOP condition without interrupt */
716 i2c_writel(i2c, 0, REG_IEN);
717 i2c_writel(i2c, REG_CON_EN | REG_CON_STOP, REG_CON);
718
719 i2c->state = STATE_IDLE;
720
721 ret = -ETIMEDOUT;
722 break;
723 }
724
725 if (i2c->error) {
726 ret = i2c->error;
727 break;
728 }
729 }
730
addy ke0285f8f2014-10-14 14:09:21 +0800731exit:
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200732 clk_disable(i2c->clk);
733 spin_unlock_irqrestore(&i2c->lock, flags);
734
735 return ret;
736}
737
738static u32 rk3x_i2c_func(struct i2c_adapter *adap)
739{
740 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
741}
742
743static const struct i2c_algorithm rk3x_i2c_algorithm = {
744 .master_xfer = rk3x_i2c_xfer,
745 .functionality = rk3x_i2c_func,
746};
747
748static struct rk3x_i2c_soc_data soc_data[3] = {
749 { .grf_offset = 0x154 }, /* rk3066 */
750 { .grf_offset = 0x0a4 }, /* rk3188 */
751 { .grf_offset = -1 }, /* no I2C switching needed */
752};
753
754static const struct of_device_id rk3x_i2c_match[] = {
755 { .compatible = "rockchip,rk3066-i2c", .data = (void *)&soc_data[0] },
756 { .compatible = "rockchip,rk3188-i2c", .data = (void *)&soc_data[1] },
757 { .compatible = "rockchip,rk3288-i2c", .data = (void *)&soc_data[2] },
Dan Carpenterc51bd6a2014-06-12 23:56:09 +0200758 {},
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200759};
760
761static int rk3x_i2c_probe(struct platform_device *pdev)
762{
763 struct device_node *np = pdev->dev.of_node;
764 const struct of_device_id *match;
765 struct rk3x_i2c *i2c;
766 struct resource *mem;
767 int ret = 0;
768 int bus_nr;
769 u32 value;
770 int irq;
771
772 i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
773 if (!i2c)
774 return -ENOMEM;
775
776 match = of_match_node(rk3x_i2c_match, np);
777 i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
778
779 if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
780 &i2c->scl_frequency)) {
781 dev_info(&pdev->dev, "using default SCL frequency: %d\n",
782 DEFAULT_SCL_RATE);
783 i2c->scl_frequency = DEFAULT_SCL_RATE;
784 }
785
786 if (i2c->scl_frequency == 0 || i2c->scl_frequency > 400 * 1000) {
787 dev_warn(&pdev->dev, "invalid SCL frequency specified.\n");
788 dev_warn(&pdev->dev, "using default SCL frequency: %d\n",
789 DEFAULT_SCL_RATE);
790 i2c->scl_frequency = DEFAULT_SCL_RATE;
791 }
792
793 strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
794 i2c->adap.owner = THIS_MODULE;
795 i2c->adap.algo = &rk3x_i2c_algorithm;
796 i2c->adap.retries = 3;
797 i2c->adap.dev.of_node = np;
798 i2c->adap.algo_data = i2c;
799 i2c->adap.dev.parent = &pdev->dev;
800
801 i2c->dev = &pdev->dev;
802
803 spin_lock_init(&i2c->lock);
804 init_waitqueue_head(&i2c->wait);
805
806 i2c->clk = devm_clk_get(&pdev->dev, NULL);
807 if (IS_ERR(i2c->clk)) {
808 dev_err(&pdev->dev, "cannot get clock\n");
809 return PTR_ERR(i2c->clk);
810 }
811
812 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
813 i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
814 if (IS_ERR(i2c->regs))
815 return PTR_ERR(i2c->regs);
816
817 /* Try to set the I2C adapter number from dt */
818 bus_nr = of_alias_get_id(np, "i2c");
819
820 /*
821 * Switch to new interface if the SoC also offers the old one.
822 * The control bit is located in the GRF register space.
823 */
824 if (i2c->soc_data->grf_offset >= 0) {
825 struct regmap *grf;
826
827 grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
828 if (IS_ERR(grf)) {
829 dev_err(&pdev->dev,
830 "rk3x-i2c needs 'rockchip,grf' property\n");
831 return PTR_ERR(grf);
832 }
833
834 if (bus_nr < 0) {
835 dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
836 return -EINVAL;
837 }
838
839 /* 27+i: write mask, 11+i: value */
840 value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
841
842 ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
843 if (ret != 0) {
844 dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
845 return ret;
846 }
847 }
848
849 /* IRQ setup */
850 irq = platform_get_irq(pdev, 0);
851 if (irq < 0) {
852 dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
853 return irq;
854 }
855
856 ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
857 0, dev_name(&pdev->dev), i2c);
858 if (ret < 0) {
859 dev_err(&pdev->dev, "cannot request IRQ\n");
860 return ret;
861 }
862
863 platform_set_drvdata(pdev, i2c);
864
865 ret = clk_prepare(i2c->clk);
866 if (ret < 0) {
867 dev_err(&pdev->dev, "Could not prepare clock\n");
868 return ret;
869 }
870
871 ret = i2c_add_adapter(&i2c->adap);
872 if (ret < 0) {
873 dev_err(&pdev->dev, "Could not register adapter\n");
874 goto err_clk;
875 }
876
877 dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
878
879 return 0;
880
881err_clk:
882 clk_unprepare(i2c->clk);
883 return ret;
884}
885
886static int rk3x_i2c_remove(struct platform_device *pdev)
887{
888 struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
889
890 i2c_del_adapter(&i2c->adap);
891 clk_unprepare(i2c->clk);
892
893 return 0;
894}
895
896static struct platform_driver rk3x_i2c_driver = {
897 .probe = rk3x_i2c_probe,
898 .remove = rk3x_i2c_remove,
899 .driver = {
900 .owner = THIS_MODULE,
901 .name = "rk3x-i2c",
902 .of_match_table = rk3x_i2c_match,
903 },
904};
905
906module_platform_driver(rk3x_i2c_driver);
907
908MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
909MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
910MODULE_LICENSE("GPL v2");