2 * Driver for I2C adapter in Rockchip RK3xxx SoC
4 * Max Schwarz <max.schwarz@online.de>
5 * based on the patches by Rockchip Inc.
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.
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>
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>
27 #include <linux/math64.h>
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 */
41 /* Data buffer offsets */
42 #define TXBUFFER_BASE 0x100
43 #define RXBUFFER_BASE 0x200
46 #define REG_CON_EN BIT(0)
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
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 */
61 /* REG_MRXADDR bits */
62 #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
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
75 #define WAIT_TIMEOUT 200 /* ms */
76 #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
87 * @grf_offset: offset inside the grf regmap for setting the i2c type
89 struct rk3x_i2c_soc_data {
94 struct i2c_adapter adap;
96 struct rk3x_i2c_soc_data *soc_data;
98 /* Hardware resources */
101 struct notifier_block clk_rate_nb;
104 unsigned int scl_frequency;
105 unsigned int rise_ns;
106 unsigned int fall_ns;
108 /* Synchronization & notification */
110 wait_queue_head_t wait;
113 /* Current message */
119 /* I2C state machine */
120 enum rk3x_i2c_state state;
121 unsigned int processed; /* sent/received bytes */
125 static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
128 writel(value, i2c->regs + offset);
131 static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
133 return readl(i2c->regs + offset);
136 /* Reset all interrupt pending bits */
137 static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
139 i2c_writel(i2c, REG_INT_ALL, REG_IPD);
143 * Generate a START condition, which triggers a REG_INT_START interrupt.
145 static void rk3x_i2c_start(struct rk3x_i2c *i2c)
149 rk3x_i2c_clean_ipd(i2c);
150 i2c_writel(i2c, REG_INT_START, REG_IEN);
152 /* enable adapter with correct mode, send START condition */
153 val = REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
155 /* if we want to react to NACK, set ACTACK bit */
156 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
157 val |= REG_CON_ACTACK;
159 i2c_writel(i2c, val, REG_CON);
163 * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
165 * @error: Error code to return in rk3x_i2c_xfer
167 static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
175 if (i2c->is_last_msg) {
176 /* Enable stop interrupt */
177 i2c_writel(i2c, REG_INT_STOP, REG_IEN);
179 i2c->state = STATE_STOP;
181 ctrl = i2c_readl(i2c, REG_CON);
182 ctrl |= REG_CON_STOP;
183 i2c_writel(i2c, ctrl, REG_CON);
185 /* Signal rk3x_i2c_xfer to start the next message. */
187 i2c->state = STATE_IDLE;
190 * The HW is actually not capable of REPEATED START. But we can
191 * get the intended effect by resetting its internal state
192 * and issuing an ordinary START.
194 i2c_writel(i2c, 0, REG_CON);
196 /* signal that we are finished with the current msg */
202 * Setup a read according to i2c->msg
204 static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
206 unsigned int len = i2c->msg->len - i2c->processed;
209 con = i2c_readl(i2c, REG_CON);
212 * The hw can read up to 32 bytes at a time. If we need more than one
213 * chunk, send an ACK after the last byte of the current chunk.
217 con &= ~REG_CON_LASTACK;
219 con |= REG_CON_LASTACK;
222 /* make sure we are in plain RX mode if we read a second chunk */
223 if (i2c->processed != 0) {
224 con &= ~REG_CON_MOD_MASK;
225 con |= REG_CON_MOD(REG_CON_MOD_RX);
228 i2c_writel(i2c, con, REG_CON);
229 i2c_writel(i2c, len, REG_MRXCNT);
233 * Fill the transmit buffer with data from i2c->msg
235 static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
242 for (i = 0; i < 8; ++i) {
244 for (j = 0; j < 4; ++j) {
245 if ((i2c->processed == i2c->msg->len) && (cnt != 0))
248 if (i2c->processed == 0 && cnt == 0)
249 byte = (i2c->addr & 0x7f) << 1;
251 byte = i2c->msg->buf[i2c->processed++];
253 val |= byte << (j * 8);
257 i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
259 if (i2c->processed == i2c->msg->len)
263 i2c_writel(i2c, cnt, REG_MTXCNT);
267 /* IRQ handlers for individual states */
269 static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
271 if (!(ipd & REG_INT_START)) {
272 rk3x_i2c_stop(i2c, -EIO);
273 dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
274 rk3x_i2c_clean_ipd(i2c);
279 i2c_writel(i2c, REG_INT_START, REG_IPD);
281 /* disable start bit */
282 i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
284 /* enable appropriate interrupts and transition */
285 if (i2c->mode == REG_CON_MOD_TX) {
286 i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
287 i2c->state = STATE_WRITE;
288 rk3x_i2c_fill_transmit_buf(i2c);
290 /* in any other case, we are going to be reading. */
291 i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
292 i2c->state = STATE_READ;
293 rk3x_i2c_prepare_read(i2c);
297 static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
299 if (!(ipd & REG_INT_MBTF)) {
300 rk3x_i2c_stop(i2c, -EIO);
301 dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
302 rk3x_i2c_clean_ipd(i2c);
307 i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
309 /* are we finished? */
310 if (i2c->processed == i2c->msg->len)
311 rk3x_i2c_stop(i2c, i2c->error);
313 rk3x_i2c_fill_transmit_buf(i2c);
316 static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
319 unsigned int len = i2c->msg->len - i2c->processed;
320 u32 uninitialized_var(val);
323 /* we only care for MBRF here. */
324 if (!(ipd & REG_INT_MBRF))
328 i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
330 /* Can only handle a maximum of 32 bytes at a time */
334 /* read the data from receive buffer */
335 for (i = 0; i < len; ++i) {
337 val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
339 byte = (val >> ((i % 4) * 8)) & 0xff;
340 i2c->msg->buf[i2c->processed++] = byte;
343 /* are we finished? */
344 if (i2c->processed == i2c->msg->len)
345 rk3x_i2c_stop(i2c, i2c->error);
347 rk3x_i2c_prepare_read(i2c);
350 static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
354 if (!(ipd & REG_INT_STOP)) {
355 rk3x_i2c_stop(i2c, -EIO);
356 dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
357 rk3x_i2c_clean_ipd(i2c);
362 i2c_writel(i2c, REG_INT_STOP, REG_IPD);
364 /* disable STOP bit */
365 con = i2c_readl(i2c, REG_CON);
366 con &= ~REG_CON_STOP;
367 i2c_writel(i2c, con, REG_CON);
370 i2c->state = STATE_IDLE;
372 /* signal rk3x_i2c_xfer that we are finished */
376 static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
378 struct rk3x_i2c *i2c = dev_id;
381 spin_lock(&i2c->lock);
383 ipd = i2c_readl(i2c, REG_IPD);
384 if (i2c->state == STATE_IDLE) {
385 dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
386 rk3x_i2c_clean_ipd(i2c);
390 dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
392 /* Clean interrupt bits we don't care about */
393 ipd &= ~(REG_INT_BRF | REG_INT_BTF);
395 if (ipd & REG_INT_NAKRCV) {
397 * We got a NACK in the last operation. Depending on whether
398 * IGNORE_NAK is set, we have to stop the operation and report
401 i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
403 ipd &= ~REG_INT_NAKRCV;
405 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
406 rk3x_i2c_stop(i2c, -ENXIO);
409 /* is there anything left to handle? */
410 if ((ipd & REG_INT_ALL) == 0)
413 switch (i2c->state) {
415 rk3x_i2c_handle_start(i2c, ipd);
418 rk3x_i2c_handle_write(i2c, ipd);
421 rk3x_i2c_handle_read(i2c, ipd);
424 rk3x_i2c_handle_stop(i2c, ipd);
431 spin_unlock(&i2c->lock);
436 * Calculate divider values for desired SCL frequency
438 * @clk_rate: I2C input clock rate
439 * @scl_rate: Desired SCL rate
440 * @rise_ns: How many ns it takes for signals to rise.
441 * @fall_ns: How many ns it takes for signals to fall.
442 * @div_low: Divider output for low
443 * @div_high: Divider output for high
445 * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
446 * a best-effort divider value is returned in divs. If the target rate is
447 * too high, we silently use the highest possible rate.
449 static int rk3x_i2c_calc_divs(unsigned long clk_rate, unsigned long scl_rate,
450 unsigned long rise_ns, unsigned long fall_ns,
451 unsigned long *div_low, unsigned long *div_high)
453 unsigned long spec_min_low_ns, spec_min_high_ns;
454 unsigned long spec_max_data_hold_ns;
455 unsigned long data_hold_buffer_ns;
457 unsigned long min_low_ns, min_high_ns;
458 unsigned long max_low_ns, min_total_ns;
460 unsigned long clk_rate_khz, scl_rate_khz;
462 unsigned long min_low_div, min_high_div;
463 unsigned long max_low_div;
465 unsigned long min_div_for_hold, min_total_div;
466 unsigned long extra_div, extra_low_div, ideal_low_div;
470 /* Only support standard-mode and fast-mode */
471 if (WARN_ON(scl_rate > 400000))
474 /* prevent scl_rate_khz from becoming 0 */
475 if (WARN_ON(scl_rate < 1000))
479 * min_low_ns: The minimum number of ns we need to hold low to
480 * meet I2C specification, should include fall time.
481 * min_high_ns: The minimum number of ns we need to hold high to
482 * meet I2C specification, should include rise time.
483 * max_low_ns: The maximum number of ns we can hold low to meet
486 * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
487 * This is because the i2c host on Rockchip holds the data line
488 * for half the low time.
490 if (scl_rate <= 100000) {
492 spec_min_low_ns = 4700;
493 spec_min_high_ns = 4000;
494 spec_max_data_hold_ns = 3450;
495 data_hold_buffer_ns = 50;
498 spec_min_low_ns = 1300;
499 spec_min_high_ns = 600;
500 spec_max_data_hold_ns = 900;
501 data_hold_buffer_ns = 50;
503 min_low_ns = spec_min_low_ns + fall_ns;
504 min_high_ns = spec_min_high_ns + rise_ns;
505 max_low_ns = spec_max_data_hold_ns * 2 - data_hold_buffer_ns;
506 min_total_ns = min_low_ns + min_high_ns;
508 /* Adjust to avoid overflow */
509 clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
510 scl_rate_khz = scl_rate / 1000;
513 * We need the total div to be >= this number
514 * so we don't clock too fast.
516 min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
518 /* These are the min dividers needed for min hold times. */
519 min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
520 min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
521 min_div_for_hold = (min_low_div + min_high_div);
524 * This is the maximum divider so we don't go over the maximum.
525 * We don't round up here (we round down) since this is a maximum.
527 max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
529 if (min_low_div > max_low_div) {
531 "Conflicting, min_low_div %lu, max_low_div %lu\n",
532 min_low_div, max_low_div);
533 max_low_div = min_low_div;
536 if (min_div_for_hold > min_total_div) {
538 * Time needed to meet hold requirements is important.
541 *div_low = min_low_div;
542 *div_high = min_high_div;
545 * We've got to distribute some time among the low and high
546 * so we don't run too fast.
548 extra_div = min_total_div - min_div_for_hold;
551 * We'll try to split things up perfectly evenly,
552 * biasing slightly towards having a higher div
553 * for low (spend more time low).
555 ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
556 scl_rate_khz * 8 * min_total_ns);
558 /* Don't allow it to go over the maximum */
559 if (ideal_low_div > max_low_div)
560 ideal_low_div = max_low_div;
563 * Handle when the ideal low div is going to take up
566 if (ideal_low_div > min_low_div + extra_div)
567 ideal_low_div = min_low_div + extra_div;
569 /* Give low the "ideal" and give high whatever extra is left */
570 extra_low_div = ideal_low_div - min_low_div;
571 *div_low = ideal_low_div;
572 *div_high = min_high_div + (extra_div - extra_low_div);
576 * Adjust to the fact that the hardware has an implicit "+1".
577 * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
579 *div_low = *div_low - 1;
580 *div_high = *div_high - 1;
582 /* Maximum divider supported by hw is 0xffff */
583 if (*div_low > 0xffff) {
588 if (*div_high > 0xffff) {
596 static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
598 unsigned long div_low, div_high;
599 u64 t_low_ns, t_high_ns;
602 ret = rk3x_i2c_calc_divs(clk_rate, i2c->scl_frequency, i2c->rise_ns,
603 i2c->fall_ns, &div_low, &div_high);
605 WARN_ONCE(ret != 0, "Could not reach SCL freq %u", i2c->scl_frequency);
607 clk_enable(i2c->clk);
608 i2c_writel(i2c, (div_high << 16) | (div_low & 0xffff), REG_CLKDIV);
609 clk_disable(i2c->clk);
611 t_low_ns = div_u64(((u64)div_low + 1) * 8 * 1000000000, clk_rate);
612 t_high_ns = div_u64(((u64)div_high + 1) * 8 * 1000000000, clk_rate);
614 "CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
616 1000000000 / i2c->scl_frequency,
617 t_low_ns, t_high_ns);
621 * rk3x_i2c_clk_notifier_cb - Clock rate change callback
622 * @nb: Pointer to notifier block
623 * @event: Notification reason
624 * @data: Pointer to notification data object
626 * The callback checks whether a valid bus frequency can be generated after the
627 * change. If so, the change is acknowledged, otherwise the change is aborted.
628 * New dividers are written to the HW in the pre- or post change notification
629 * depending on the scaling direction.
631 * Code adapted from i2c-cadence.c.
633 * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
634 * to acknowedge the change, NOTIFY_DONE if the notification is
635 * considered irrelevant.
637 static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
640 struct clk_notifier_data *ndata = data;
641 struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
642 unsigned long div_low, div_high;
645 case PRE_RATE_CHANGE:
646 if (rk3x_i2c_calc_divs(ndata->new_rate, i2c->scl_frequency,
647 i2c->rise_ns, i2c->fall_ns, &div_low,
652 if (ndata->new_rate > ndata->old_rate)
653 rk3x_i2c_adapt_div(i2c, ndata->new_rate);
656 case POST_RATE_CHANGE:
658 if (ndata->new_rate < ndata->old_rate)
659 rk3x_i2c_adapt_div(i2c, ndata->new_rate);
661 case ABORT_RATE_CHANGE:
663 if (ndata->new_rate > ndata->old_rate)
664 rk3x_i2c_adapt_div(i2c, ndata->old_rate);
672 * Setup I2C registers for an I2C operation specified by msgs, num.
674 * Must be called with i2c->lock held.
676 * @msgs: I2C msgs to process
677 * @num: Number of msgs
679 * returns: Number of I2C msgs processed or negative in case of error
681 static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
683 u32 addr = (msgs[0].addr & 0x7f) << 1;
687 * The I2C adapter can issue a small (len < 4) write packet before
688 * reading. This speeds up SMBus-style register reads.
689 * The MRXADDR/MRXRADDR hold the slave address and the slave register
690 * address in this case.
693 if (num >= 2 && msgs[0].len < 4 &&
694 !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
698 dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
701 /* Fill MRXRADDR with the register address(es) */
702 for (i = 0; i < msgs[0].len; ++i) {
703 reg_addr |= msgs[0].buf[i] << (i * 8);
704 reg_addr |= REG_MRXADDR_VALID(i);
707 /* msgs[0] is handled by hw. */
710 i2c->mode = REG_CON_MOD_REGISTER_TX;
712 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
713 i2c_writel(i2c, reg_addr, REG_MRXRADDR);
718 * We'll have to do it the boring way and process the msgs
722 if (msgs[0].flags & I2C_M_RD) {
723 addr |= 1; /* set read bit */
726 * We have to transmit the slave addr first. Use
727 * MOD_REGISTER_TX for that purpose.
729 i2c->mode = REG_CON_MOD_REGISTER_TX;
730 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
732 i2c_writel(i2c, 0, REG_MRXRADDR);
734 i2c->mode = REG_CON_MOD_TX;
742 i2c->addr = msgs[0].addr;
744 i2c->state = STATE_START;
748 rk3x_i2c_clean_ipd(i2c);
753 static int rk3x_i2c_xfer(struct i2c_adapter *adap,
754 struct i2c_msg *msgs, int num)
756 struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
757 unsigned long timeout, flags;
761 spin_lock_irqsave(&i2c->lock, flags);
763 clk_enable(i2c->clk);
765 i2c->is_last_msg = false;
768 * Process msgs. We can handle more than one message at once (see
771 for (i = 0; i < num; i += ret) {
772 ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
775 dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
780 i2c->is_last_msg = true;
782 spin_unlock_irqrestore(&i2c->lock, flags);
786 timeout = wait_event_timeout(i2c->wait, !i2c->busy,
787 msecs_to_jiffies(WAIT_TIMEOUT));
789 spin_lock_irqsave(&i2c->lock, flags);
792 dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
793 i2c_readl(i2c, REG_IPD), i2c->state);
795 /* Force a STOP condition without interrupt */
796 i2c_writel(i2c, 0, REG_IEN);
797 i2c_writel(i2c, REG_CON_EN | REG_CON_STOP, REG_CON);
799 i2c->state = STATE_IDLE;
811 clk_disable(i2c->clk);
812 spin_unlock_irqrestore(&i2c->lock, flags);
817 static u32 rk3x_i2c_func(struct i2c_adapter *adap)
819 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
822 static const struct i2c_algorithm rk3x_i2c_algorithm = {
823 .master_xfer = rk3x_i2c_xfer,
824 .functionality = rk3x_i2c_func,
827 static struct rk3x_i2c_soc_data soc_data[3] = {
828 { .grf_offset = 0x154 }, /* rk3066 */
829 { .grf_offset = 0x0a4 }, /* rk3188 */
830 { .grf_offset = -1 }, /* no I2C switching needed */
833 static const struct of_device_id rk3x_i2c_match[] = {
834 { .compatible = "rockchip,rk3066-i2c", .data = (void *)&soc_data[0] },
835 { .compatible = "rockchip,rk3188-i2c", .data = (void *)&soc_data[1] },
836 { .compatible = "rockchip,rk3288-i2c", .data = (void *)&soc_data[2] },
840 static int rk3x_i2c_probe(struct platform_device *pdev)
842 struct device_node *np = pdev->dev.of_node;
843 const struct of_device_id *match;
844 struct rk3x_i2c *i2c;
845 struct resource *mem;
850 unsigned long clk_rate;
852 i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
856 match = of_match_node(rk3x_i2c_match, np);
857 i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
859 if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
860 &i2c->scl_frequency)) {
861 dev_info(&pdev->dev, "using default SCL frequency: %d\n",
863 i2c->scl_frequency = DEFAULT_SCL_RATE;
866 if (i2c->scl_frequency == 0 || i2c->scl_frequency > 400 * 1000) {
867 dev_warn(&pdev->dev, "invalid SCL frequency specified.\n");
868 dev_warn(&pdev->dev, "using default SCL frequency: %d\n",
870 i2c->scl_frequency = DEFAULT_SCL_RATE;
874 * Read rise and fall time from device tree. If not available use
875 * the default maximum timing from the specification.
877 if (of_property_read_u32(pdev->dev.of_node, "i2c-scl-rising-time-ns",
879 if (i2c->scl_frequency <= 100000)
884 if (of_property_read_u32(pdev->dev.of_node, "i2c-scl-falling-time-ns",
888 strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
889 i2c->adap.owner = THIS_MODULE;
890 i2c->adap.algo = &rk3x_i2c_algorithm;
891 i2c->adap.retries = 3;
892 i2c->adap.dev.of_node = np;
893 i2c->adap.algo_data = i2c;
894 i2c->adap.dev.parent = &pdev->dev;
896 i2c->dev = &pdev->dev;
898 spin_lock_init(&i2c->lock);
899 init_waitqueue_head(&i2c->wait);
901 i2c->clk = devm_clk_get(&pdev->dev, NULL);
902 if (IS_ERR(i2c->clk)) {
903 dev_err(&pdev->dev, "cannot get clock\n");
904 return PTR_ERR(i2c->clk);
907 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
908 i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
909 if (IS_ERR(i2c->regs))
910 return PTR_ERR(i2c->regs);
912 /* Try to set the I2C adapter number from dt */
913 bus_nr = of_alias_get_id(np, "i2c");
916 * Switch to new interface if the SoC also offers the old one.
917 * The control bit is located in the GRF register space.
919 if (i2c->soc_data->grf_offset >= 0) {
922 grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
925 "rk3x-i2c needs 'rockchip,grf' property\n");
930 dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
934 /* 27+i: write mask, 11+i: value */
935 value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
937 ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
939 dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
945 irq = platform_get_irq(pdev, 0);
947 dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
951 ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
952 0, dev_name(&pdev->dev), i2c);
954 dev_err(&pdev->dev, "cannot request IRQ\n");
958 platform_set_drvdata(pdev, i2c);
960 ret = clk_prepare(i2c->clk);
962 dev_err(&pdev->dev, "Could not prepare clock\n");
966 i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
967 ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
969 dev_err(&pdev->dev, "Unable to register clock notifier\n");
973 clk_rate = clk_get_rate(i2c->clk);
974 rk3x_i2c_adapt_div(i2c, clk_rate);
976 ret = i2c_add_adapter(&i2c->adap);
978 dev_err(&pdev->dev, "Could not register adapter\n");
979 goto err_clk_notifier;
982 dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
987 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
989 clk_unprepare(i2c->clk);
993 static int rk3x_i2c_remove(struct platform_device *pdev)
995 struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
997 i2c_del_adapter(&i2c->adap);
999 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
1000 clk_unprepare(i2c->clk);
1005 static struct platform_driver rk3x_i2c_driver = {
1006 .probe = rk3x_i2c_probe,
1007 .remove = rk3x_i2c_remove,
1010 .of_match_table = rk3x_i2c_match,
1014 module_platform_driver(rk3x_i2c_driver);
1016 MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
1017 MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
1018 MODULE_LICENSE("GPL v2");