blob: 97917978ddd95981a341382a469b3836b40cd9c9 [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 */
Doug Anderson44897502015-05-11 12:44:28 -070075#define WAIT_TIMEOUT 1000 /* ms */
Max Schwarzc41aa3c2014-06-11 22:34:37 +020076#define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
77
David Wue26747b2016-05-16 21:57:37 +080078/**
David Wub58fd3b2016-05-16 22:03:24 +080079 * struct i2c_spec_values:
80 * @min_low_ns: min LOW period of the SCL clock
81 * @min_high_ns: min HIGH period of the SCL cloc
82 * @min_setup_start_ns: min set-up time for a repeated START conditio
83 * @max_data_hold_ns: max data hold time
84 */
85struct i2c_spec_values {
86 unsigned long min_low_ns;
87 unsigned long min_high_ns;
88 unsigned long min_setup_start_ns;
89 unsigned long max_data_hold_ns;
90};
91
92static const struct i2c_spec_values standard_mode_spec = {
93 .min_low_ns = 4700,
94 .min_high_ns = 4000,
95 .min_setup_start_ns = 4700,
96 .max_data_hold_ns = 3450,
97};
98
99static const struct i2c_spec_values fast_mode_spec = {
100 .min_low_ns = 1300,
101 .min_high_ns = 600,
102 .min_setup_start_ns = 600,
103 .max_data_hold_ns = 900,
104};
105
106/**
David Wue26747b2016-05-16 21:57:37 +0800107 * struct rk3x_i2c_calced_timings:
108 * @div_low: Divider output for low
109 * @div_high: Divider output for high
110 */
111struct rk3x_i2c_calced_timings {
112 unsigned long div_low;
113 unsigned long div_high;
114};
115
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200116enum rk3x_i2c_state {
117 STATE_IDLE,
118 STATE_START,
119 STATE_READ,
120 STATE_WRITE,
121 STATE_STOP
122};
123
124/**
125 * @grf_offset: offset inside the grf regmap for setting the i2c type
126 */
127struct rk3x_i2c_soc_data {
128 int grf_offset;
129};
130
David Wu0a6ad2f2016-05-16 21:57:36 +0800131/**
132 * struct rk3x_i2c - private data of the controller
133 * @adap: corresponding I2C adapter
134 * @dev: device for this controller
135 * @soc_data: related soc data struct
136 * @regs: virtual memory area
137 * @clk: clock of i2c bus
138 * @clk_rate_nb: i2c clk rate change notify
139 * @t: I2C known timing information
140 * @lock: spinlock for the i2c bus
141 * @wait: the waitqueue to wait for i2c transfer
142 * @busy: the condition for the event to wait for
143 * @msg: current i2c message
144 * @addr: addr of i2c slave device
145 * @mode: mode of i2c transfer
146 * @is_last_msg: flag determines whether it is the last msg in this transfer
147 * @state: state of i2c transfer
148 * @processed: byte length which has been send or received
149 * @error: error code for i2c transfer
150 */
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200151struct rk3x_i2c {
152 struct i2c_adapter adap;
153 struct device *dev;
154 struct rk3x_i2c_soc_data *soc_data;
155
156 /* Hardware resources */
157 void __iomem *regs;
158 struct clk *clk;
Max Schwarz249051f2014-11-20 10:26:50 +0100159 struct notifier_block clk_rate_nb;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200160
161 /* Settings */
David Wu1ab92952016-03-17 00:57:17 +0800162 struct i2c_timings t;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200163
164 /* Synchronization & notification */
165 spinlock_t lock;
166 wait_queue_head_t wait;
167 bool busy;
168
169 /* Current message */
170 struct i2c_msg *msg;
171 u8 addr;
172 unsigned int mode;
173 bool is_last_msg;
174
175 /* I2C state machine */
176 enum rk3x_i2c_state state;
David Wu0a6ad2f2016-05-16 21:57:36 +0800177 unsigned int processed;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200178 int error;
179};
180
181static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
182 unsigned int offset)
183{
184 writel(value, i2c->regs + offset);
185}
186
187static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
188{
189 return readl(i2c->regs + offset);
190}
191
192/* Reset all interrupt pending bits */
193static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
194{
195 i2c_writel(i2c, REG_INT_ALL, REG_IPD);
196}
197
198/**
199 * Generate a START condition, which triggers a REG_INT_START interrupt.
200 */
201static void rk3x_i2c_start(struct rk3x_i2c *i2c)
202{
203 u32 val;
204
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200205 i2c_writel(i2c, REG_INT_START, REG_IEN);
206
207 /* enable adapter with correct mode, send START condition */
208 val = REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
209
210 /* if we want to react to NACK, set ACTACK bit */
211 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
212 val |= REG_CON_ACTACK;
213
214 i2c_writel(i2c, val, REG_CON);
215}
216
217/**
218 * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
219 *
220 * @error: Error code to return in rk3x_i2c_xfer
221 */
222static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
223{
224 unsigned int ctrl;
225
226 i2c->processed = 0;
227 i2c->msg = NULL;
228 i2c->error = error;
229
230 if (i2c->is_last_msg) {
231 /* Enable stop interrupt */
232 i2c_writel(i2c, REG_INT_STOP, REG_IEN);
233
234 i2c->state = STATE_STOP;
235
236 ctrl = i2c_readl(i2c, REG_CON);
237 ctrl |= REG_CON_STOP;
238 i2c_writel(i2c, ctrl, REG_CON);
239 } else {
240 /* Signal rk3x_i2c_xfer to start the next message. */
241 i2c->busy = false;
242 i2c->state = STATE_IDLE;
243
244 /*
245 * The HW is actually not capable of REPEATED START. But we can
246 * get the intended effect by resetting its internal state
247 * and issuing an ordinary START.
248 */
249 i2c_writel(i2c, 0, REG_CON);
250
251 /* signal that we are finished with the current msg */
252 wake_up(&i2c->wait);
253 }
254}
255
256/**
257 * Setup a read according to i2c->msg
258 */
259static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
260{
261 unsigned int len = i2c->msg->len - i2c->processed;
262 u32 con;
263
264 con = i2c_readl(i2c, REG_CON);
265
266 /*
267 * The hw can read up to 32 bytes at a time. If we need more than one
268 * chunk, send an ACK after the last byte of the current chunk.
269 */
Doug Anderson29209332014-08-22 10:43:44 -0700270 if (len > 32) {
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200271 len = 32;
272 con &= ~REG_CON_LASTACK;
273 } else {
274 con |= REG_CON_LASTACK;
275 }
276
277 /* make sure we are in plain RX mode if we read a second chunk */
278 if (i2c->processed != 0) {
279 con &= ~REG_CON_MOD_MASK;
280 con |= REG_CON_MOD(REG_CON_MOD_RX);
281 }
282
283 i2c_writel(i2c, con, REG_CON);
284 i2c_writel(i2c, len, REG_MRXCNT);
285}
286
287/**
288 * Fill the transmit buffer with data from i2c->msg
289 */
290static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
291{
292 unsigned int i, j;
293 u32 cnt = 0;
294 u32 val;
295 u8 byte;
296
297 for (i = 0; i < 8; ++i) {
298 val = 0;
299 for (j = 0; j < 4; ++j) {
Alexandru M Stancf270202014-10-01 10:40:41 -0700300 if ((i2c->processed == i2c->msg->len) && (cnt != 0))
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200301 break;
302
303 if (i2c->processed == 0 && cnt == 0)
304 byte = (i2c->addr & 0x7f) << 1;
305 else
306 byte = i2c->msg->buf[i2c->processed++];
307
308 val |= byte << (j * 8);
309 cnt++;
310 }
311
312 i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
313
314 if (i2c->processed == i2c->msg->len)
315 break;
316 }
317
318 i2c_writel(i2c, cnt, REG_MTXCNT);
319}
320
321
322/* IRQ handlers for individual states */
323
324static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
325{
326 if (!(ipd & REG_INT_START)) {
327 rk3x_i2c_stop(i2c, -EIO);
328 dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
329 rk3x_i2c_clean_ipd(i2c);
330 return;
331 }
332
333 /* ack interrupt */
334 i2c_writel(i2c, REG_INT_START, REG_IPD);
335
336 /* disable start bit */
337 i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
338
339 /* enable appropriate interrupts and transition */
340 if (i2c->mode == REG_CON_MOD_TX) {
341 i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
342 i2c->state = STATE_WRITE;
343 rk3x_i2c_fill_transmit_buf(i2c);
344 } else {
345 /* in any other case, we are going to be reading. */
346 i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
347 i2c->state = STATE_READ;
348 rk3x_i2c_prepare_read(i2c);
349 }
350}
351
352static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
353{
354 if (!(ipd & REG_INT_MBTF)) {
355 rk3x_i2c_stop(i2c, -EIO);
356 dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
357 rk3x_i2c_clean_ipd(i2c);
358 return;
359 }
360
361 /* ack interrupt */
362 i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
363
364 /* are we finished? */
365 if (i2c->processed == i2c->msg->len)
366 rk3x_i2c_stop(i2c, i2c->error);
367 else
368 rk3x_i2c_fill_transmit_buf(i2c);
369}
370
371static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
372{
373 unsigned int i;
374 unsigned int len = i2c->msg->len - i2c->processed;
375 u32 uninitialized_var(val);
376 u8 byte;
377
378 /* we only care for MBRF here. */
379 if (!(ipd & REG_INT_MBRF))
380 return;
381
382 /* ack interrupt */
383 i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
384
addy ke5da43092014-08-23 02:00:52 +0800385 /* Can only handle a maximum of 32 bytes at a time */
386 if (len > 32)
387 len = 32;
388
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200389 /* read the data from receive buffer */
390 for (i = 0; i < len; ++i) {
391 if (i % 4 == 0)
392 val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
393
394 byte = (val >> ((i % 4) * 8)) & 0xff;
395 i2c->msg->buf[i2c->processed++] = byte;
396 }
397
398 /* are we finished? */
399 if (i2c->processed == i2c->msg->len)
400 rk3x_i2c_stop(i2c, i2c->error);
401 else
402 rk3x_i2c_prepare_read(i2c);
403}
404
405static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
406{
407 unsigned int con;
408
409 if (!(ipd & REG_INT_STOP)) {
410 rk3x_i2c_stop(i2c, -EIO);
411 dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
412 rk3x_i2c_clean_ipd(i2c);
413 return;
414 }
415
416 /* ack interrupt */
417 i2c_writel(i2c, REG_INT_STOP, REG_IPD);
418
419 /* disable STOP bit */
420 con = i2c_readl(i2c, REG_CON);
421 con &= ~REG_CON_STOP;
422 i2c_writel(i2c, con, REG_CON);
423
424 i2c->busy = false;
425 i2c->state = STATE_IDLE;
426
427 /* signal rk3x_i2c_xfer that we are finished */
428 wake_up(&i2c->wait);
429}
430
431static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
432{
433 struct rk3x_i2c *i2c = dev_id;
434 unsigned int ipd;
435
436 spin_lock(&i2c->lock);
437
438 ipd = i2c_readl(i2c, REG_IPD);
439 if (i2c->state == STATE_IDLE) {
440 dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
441 rk3x_i2c_clean_ipd(i2c);
442 goto out;
443 }
444
445 dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
446
447 /* Clean interrupt bits we don't care about */
448 ipd &= ~(REG_INT_BRF | REG_INT_BTF);
449
450 if (ipd & REG_INT_NAKRCV) {
451 /*
452 * We got a NACK in the last operation. Depending on whether
453 * IGNORE_NAK is set, we have to stop the operation and report
454 * an error.
455 */
456 i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
457
458 ipd &= ~REG_INT_NAKRCV;
459
460 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
461 rk3x_i2c_stop(i2c, -ENXIO);
462 }
463
464 /* is there anything left to handle? */
Doug Anderson29209332014-08-22 10:43:44 -0700465 if ((ipd & REG_INT_ALL) == 0)
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200466 goto out;
467
468 switch (i2c->state) {
469 case STATE_START:
470 rk3x_i2c_handle_start(i2c, ipd);
471 break;
472 case STATE_WRITE:
473 rk3x_i2c_handle_write(i2c, ipd);
474 break;
475 case STATE_READ:
476 rk3x_i2c_handle_read(i2c, ipd);
477 break;
478 case STATE_STOP:
479 rk3x_i2c_handle_stop(i2c, ipd);
480 break;
481 case STATE_IDLE:
482 break;
483 }
484
485out:
486 spin_unlock(&i2c->lock);
487 return IRQ_HANDLED;
488}
489
Max Schwarz249051f2014-11-20 10:26:50 +0100490/**
David Wub58fd3b2016-05-16 22:03:24 +0800491 * Get timing values of I2C specification
492 *
493 * @speed: Desired SCL frequency
494 *
495 * Returns: Matched i2c spec values.
496 */
497static const struct i2c_spec_values *rk3x_i2c_get_spec(unsigned int speed)
498{
499 if (speed <= 100000)
500 return &standard_mode_spec;
501 else
502 return &fast_mode_spec;
503}
504
505/**
Max Schwarz249051f2014-11-20 10:26:50 +0100506 * Calculate divider values for desired SCL frequency
507 *
508 * @clk_rate: I2C input clock rate
David Wue26747b2016-05-16 21:57:37 +0800509 * @t: Known I2C timing information
510 * @t_calc: Caculated rk3x private timings that would be written into regs
Max Schwarz249051f2014-11-20 10:26:50 +0100511 *
512 * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
513 * a best-effort divider value is returned in divs. If the target rate is
514 * too high, we silently use the highest possible rate.
515 */
David Wu1ab92952016-03-17 00:57:17 +0800516static int rk3x_i2c_calc_divs(unsigned long clk_rate,
517 struct i2c_timings *t,
David Wue26747b2016-05-16 21:57:37 +0800518 struct rk3x_i2c_calced_timings *t_calc)
addy ke0285f8f2014-10-14 14:09:21 +0800519{
addy ke1330e292014-12-11 19:02:40 +0800520 unsigned long min_low_ns, min_high_ns;
addy ke0285f8f2014-10-14 14:09:21 +0800521 unsigned long max_low_ns, min_total_ns;
522
Max Schwarz249051f2014-11-20 10:26:50 +0100523 unsigned long clk_rate_khz, scl_rate_khz;
addy ke0285f8f2014-10-14 14:09:21 +0800524
525 unsigned long min_low_div, min_high_div;
526 unsigned long max_low_div;
527
528 unsigned long min_div_for_hold, min_total_div;
529 unsigned long extra_div, extra_low_div, ideal_low_div;
530
David Wub58fd3b2016-05-16 22:03:24 +0800531 unsigned long data_hold_buffer_ns = 50;
532 const struct i2c_spec_values *spec;
Max Schwarz249051f2014-11-20 10:26:50 +0100533 int ret = 0;
534
addy ke0285f8f2014-10-14 14:09:21 +0800535 /* Only support standard-mode and fast-mode */
David Wu1ab92952016-03-17 00:57:17 +0800536 if (WARN_ON(t->bus_freq_hz > 400000))
537 t->bus_freq_hz = 400000;
addy ke0285f8f2014-10-14 14:09:21 +0800538
539 /* prevent scl_rate_khz from becoming 0 */
David Wu1ab92952016-03-17 00:57:17 +0800540 if (WARN_ON(t->bus_freq_hz < 1000))
541 t->bus_freq_hz = 1000;
addy ke0285f8f2014-10-14 14:09:21 +0800542
543 /*
addy ke1330e292014-12-11 19:02:40 +0800544 * min_low_ns: The minimum number of ns we need to hold low to
545 * meet I2C specification, should include fall time.
546 * min_high_ns: The minimum number of ns we need to hold high to
547 * meet I2C specification, should include rise time.
548 * max_low_ns: The maximum number of ns we can hold low to meet
549 * I2C specification.
addy ke0285f8f2014-10-14 14:09:21 +0800550 *
addy ke1330e292014-12-11 19:02:40 +0800551 * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
addy ke0285f8f2014-10-14 14:09:21 +0800552 * This is because the i2c host on Rockchip holds the data line
553 * for half the low time.
554 */
David Wub58fd3b2016-05-16 22:03:24 +0800555 spec = rk3x_i2c_get_spec(t->bus_freq_hz);
556 min_high_ns = t->scl_rise_ns + spec->min_high_ns;
Doug Anderson387f0de2014-12-18 09:44:07 -0800557
558 /*
559 * Timings for repeated start:
560 * - controller appears to drop SDA at .875x (7/8) programmed clk high.
561 * - controller appears to keep SCL high for 2x programmed clk high.
562 *
563 * We need to account for those rules in picking our "high" time so
564 * we meet tSU;STA and tHD;STA times.
565 */
David Wub58fd3b2016-05-16 22:03:24 +0800566 min_high_ns = max(min_high_ns, DIV_ROUND_UP(
567 (t->scl_rise_ns + spec->min_setup_start_ns) * 1000, 875));
568 min_high_ns = max(min_high_ns, DIV_ROUND_UP(
569 (t->scl_rise_ns + spec->min_setup_start_ns + t->sda_fall_ns +
570 spec->min_high_ns), 2));
Doug Anderson387f0de2014-12-18 09:44:07 -0800571
David Wub58fd3b2016-05-16 22:03:24 +0800572 min_low_ns = t->scl_fall_ns + spec->min_low_ns;
573 max_low_ns = spec->max_data_hold_ns * 2 - data_hold_buffer_ns;
addy ke0285f8f2014-10-14 14:09:21 +0800574 min_total_ns = min_low_ns + min_high_ns;
575
576 /* Adjust to avoid overflow */
Max Schwarz249051f2014-11-20 10:26:50 +0100577 clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
David Wu1ab92952016-03-17 00:57:17 +0800578 scl_rate_khz = t->bus_freq_hz / 1000;
addy ke0285f8f2014-10-14 14:09:21 +0800579
580 /*
581 * We need the total div to be >= this number
582 * so we don't clock too fast.
583 */
Max Schwarz249051f2014-11-20 10:26:50 +0100584 min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
addy ke0285f8f2014-10-14 14:09:21 +0800585
586 /* These are the min dividers needed for min hold times. */
Max Schwarz249051f2014-11-20 10:26:50 +0100587 min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
588 min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
addy ke0285f8f2014-10-14 14:09:21 +0800589 min_div_for_hold = (min_low_div + min_high_div);
590
591 /*
addy ke1330e292014-12-11 19:02:40 +0800592 * This is the maximum divider so we don't go over the maximum.
593 * We don't round up here (we round down) since this is a maximum.
addy ke0285f8f2014-10-14 14:09:21 +0800594 */
Max Schwarz249051f2014-11-20 10:26:50 +0100595 max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
addy ke0285f8f2014-10-14 14:09:21 +0800596
597 if (min_low_div > max_low_div) {
598 WARN_ONCE(true,
599 "Conflicting, min_low_div %lu, max_low_div %lu\n",
600 min_low_div, max_low_div);
601 max_low_div = min_low_div;
602 }
603
604 if (min_div_for_hold > min_total_div) {
605 /*
606 * Time needed to meet hold requirements is important.
607 * Just use that.
608 */
David Wue26747b2016-05-16 21:57:37 +0800609 t_calc->div_low = min_low_div;
610 t_calc->div_high = min_high_div;
addy ke0285f8f2014-10-14 14:09:21 +0800611 } else {
612 /*
613 * We've got to distribute some time among the low and high
614 * so we don't run too fast.
615 */
616 extra_div = min_total_div - min_div_for_hold;
617
618 /*
619 * We'll try to split things up perfectly evenly,
620 * biasing slightly towards having a higher div
621 * for low (spend more time low).
622 */
Max Schwarz249051f2014-11-20 10:26:50 +0100623 ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
addy ke0285f8f2014-10-14 14:09:21 +0800624 scl_rate_khz * 8 * min_total_ns);
625
addy ke1330e292014-12-11 19:02:40 +0800626 /* Don't allow it to go over the maximum */
addy ke0285f8f2014-10-14 14:09:21 +0800627 if (ideal_low_div > max_low_div)
628 ideal_low_div = max_low_div;
629
630 /*
631 * Handle when the ideal low div is going to take up
632 * more than we have.
633 */
634 if (ideal_low_div > min_low_div + extra_div)
635 ideal_low_div = min_low_div + extra_div;
636
637 /* Give low the "ideal" and give high whatever extra is left */
638 extra_low_div = ideal_low_div - min_low_div;
David Wue26747b2016-05-16 21:57:37 +0800639 t_calc->div_low = ideal_low_div;
640 t_calc->div_high = min_high_div + (extra_div - extra_low_div);
addy ke0285f8f2014-10-14 14:09:21 +0800641 }
642
643 /*
Max Schwarz249051f2014-11-20 10:26:50 +0100644 * Adjust to the fact that the hardware has an implicit "+1".
645 * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
646 */
David Wue26747b2016-05-16 21:57:37 +0800647 t_calc->div_low--;
648 t_calc->div_high--;
addy ke0285f8f2014-10-14 14:09:21 +0800649
Max Schwarz249051f2014-11-20 10:26:50 +0100650 /* Maximum divider supported by hw is 0xffff */
David Wue26747b2016-05-16 21:57:37 +0800651 if (t_calc->div_low > 0xffff) {
652 t_calc->div_low = 0xffff;
Max Schwarz249051f2014-11-20 10:26:50 +0100653 ret = -EINVAL;
654 }
addy ke0285f8f2014-10-14 14:09:21 +0800655
David Wue26747b2016-05-16 21:57:37 +0800656 if (t_calc->div_high > 0xffff) {
657 t_calc->div_high = 0xffff;
Max Schwarz249051f2014-11-20 10:26:50 +0100658 ret = -EINVAL;
659 }
addy ke0285f8f2014-10-14 14:09:21 +0800660
661 return ret;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200662}
663
Max Schwarz249051f2014-11-20 10:26:50 +0100664static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
665{
David Wu1ab92952016-03-17 00:57:17 +0800666 struct i2c_timings *t = &i2c->t;
David Wue26747b2016-05-16 21:57:37 +0800667 struct rk3x_i2c_calced_timings calc;
Max Schwarz249051f2014-11-20 10:26:50 +0100668 u64 t_low_ns, t_high_ns;
669 int ret;
670
David Wue26747b2016-05-16 21:57:37 +0800671 ret = rk3x_i2c_calc_divs(clk_rate, t, &calc);
David Wu1ab92952016-03-17 00:57:17 +0800672 WARN_ONCE(ret != 0, "Could not reach SCL freq %u", t->bus_freq_hz);
Max Schwarz249051f2014-11-20 10:26:50 +0100673
674 clk_enable(i2c->clk);
David Wue26747b2016-05-16 21:57:37 +0800675 i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff),
676 REG_CLKDIV);
Max Schwarz249051f2014-11-20 10:26:50 +0100677 clk_disable(i2c->clk);
678
David Wue26747b2016-05-16 21:57:37 +0800679 t_low_ns = div_u64(((u64)calc.div_low + 1) * 8 * 1000000000, clk_rate);
680 t_high_ns = div_u64(((u64)calc.div_high + 1) * 8 * 1000000000,
681 clk_rate);
Max Schwarz249051f2014-11-20 10:26:50 +0100682 dev_dbg(i2c->dev,
683 "CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
684 clk_rate / 1000,
David Wu1ab92952016-03-17 00:57:17 +0800685 1000000000 / t->bus_freq_hz,
Max Schwarz249051f2014-11-20 10:26:50 +0100686 t_low_ns, t_high_ns);
687}
688
689/**
690 * rk3x_i2c_clk_notifier_cb - Clock rate change callback
691 * @nb: Pointer to notifier block
692 * @event: Notification reason
693 * @data: Pointer to notification data object
694 *
695 * The callback checks whether a valid bus frequency can be generated after the
696 * change. If so, the change is acknowledged, otherwise the change is aborted.
697 * New dividers are written to the HW in the pre- or post change notification
698 * depending on the scaling direction.
699 *
700 * Code adapted from i2c-cadence.c.
701 *
702 * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
703 * to acknowedge the change, NOTIFY_DONE if the notification is
704 * considered irrelevant.
705 */
706static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
707 event, void *data)
708{
709 struct clk_notifier_data *ndata = data;
710 struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
David Wue26747b2016-05-16 21:57:37 +0800711 struct rk3x_i2c_calced_timings calc;
Max Schwarz249051f2014-11-20 10:26:50 +0100712
713 switch (event) {
714 case PRE_RATE_CHANGE:
David Wue26747b2016-05-16 21:57:37 +0800715 if (rk3x_i2c_calc_divs(ndata->new_rate, &i2c->t, &calc) != 0)
Max Schwarz249051f2014-11-20 10:26:50 +0100716 return NOTIFY_STOP;
Max Schwarz249051f2014-11-20 10:26:50 +0100717
718 /* scale up */
719 if (ndata->new_rate > ndata->old_rate)
720 rk3x_i2c_adapt_div(i2c, ndata->new_rate);
721
722 return NOTIFY_OK;
723 case POST_RATE_CHANGE:
724 /* scale down */
725 if (ndata->new_rate < ndata->old_rate)
726 rk3x_i2c_adapt_div(i2c, ndata->new_rate);
727 return NOTIFY_OK;
728 case ABORT_RATE_CHANGE:
729 /* scale up */
730 if (ndata->new_rate > ndata->old_rate)
731 rk3x_i2c_adapt_div(i2c, ndata->old_rate);
732 return NOTIFY_OK;
733 default:
734 return NOTIFY_DONE;
735 }
736}
737
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200738/**
739 * Setup I2C registers for an I2C operation specified by msgs, num.
740 *
741 * Must be called with i2c->lock held.
742 *
743 * @msgs: I2C msgs to process
744 * @num: Number of msgs
745 *
746 * returns: Number of I2C msgs processed or negative in case of error
747 */
748static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
749{
750 u32 addr = (msgs[0].addr & 0x7f) << 1;
751 int ret = 0;
752
753 /*
754 * The I2C adapter can issue a small (len < 4) write packet before
755 * reading. This speeds up SMBus-style register reads.
756 * The MRXADDR/MRXRADDR hold the slave address and the slave register
757 * address in this case.
758 */
759
760 if (num >= 2 && msgs[0].len < 4 &&
761 !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
762 u32 reg_addr = 0;
763 int i;
764
765 dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
766 addr >> 1);
767
768 /* Fill MRXRADDR with the register address(es) */
769 for (i = 0; i < msgs[0].len; ++i) {
770 reg_addr |= msgs[0].buf[i] << (i * 8);
771 reg_addr |= REG_MRXADDR_VALID(i);
772 }
773
774 /* msgs[0] is handled by hw. */
775 i2c->msg = &msgs[1];
776
777 i2c->mode = REG_CON_MOD_REGISTER_TX;
778
779 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
780 i2c_writel(i2c, reg_addr, REG_MRXRADDR);
781
782 ret = 2;
783 } else {
784 /*
785 * We'll have to do it the boring way and process the msgs
786 * one-by-one.
787 */
788
789 if (msgs[0].flags & I2C_M_RD) {
790 addr |= 1; /* set read bit */
791
792 /*
793 * We have to transmit the slave addr first. Use
794 * MOD_REGISTER_TX for that purpose.
795 */
796 i2c->mode = REG_CON_MOD_REGISTER_TX;
797 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
798 REG_MRXADDR);
799 i2c_writel(i2c, 0, REG_MRXRADDR);
800 } else {
801 i2c->mode = REG_CON_MOD_TX;
802 }
803
804 i2c->msg = &msgs[0];
805
806 ret = 1;
807 }
808
809 i2c->addr = msgs[0].addr;
810 i2c->busy = true;
811 i2c->state = STATE_START;
812 i2c->processed = 0;
813 i2c->error = 0;
814
815 rk3x_i2c_clean_ipd(i2c);
816
817 return ret;
818}
819
820static int rk3x_i2c_xfer(struct i2c_adapter *adap,
821 struct i2c_msg *msgs, int num)
822{
823 struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
824 unsigned long timeout, flags;
825 int ret = 0;
826 int i;
827
828 spin_lock_irqsave(&i2c->lock, flags);
829
830 clk_enable(i2c->clk);
831
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200832 i2c->is_last_msg = false;
833
834 /*
835 * Process msgs. We can handle more than one message at once (see
836 * rk3x_i2c_setup()).
837 */
838 for (i = 0; i < num; i += ret) {
839 ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
840
841 if (ret < 0) {
842 dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
843 break;
844 }
845
846 if (i + ret >= num)
847 i2c->is_last_msg = true;
848
849 spin_unlock_irqrestore(&i2c->lock, flags);
850
851 rk3x_i2c_start(i2c);
852
853 timeout = wait_event_timeout(i2c->wait, !i2c->busy,
854 msecs_to_jiffies(WAIT_TIMEOUT));
855
856 spin_lock_irqsave(&i2c->lock, flags);
857
858 if (timeout == 0) {
859 dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
860 i2c_readl(i2c, REG_IPD), i2c->state);
861
862 /* Force a STOP condition without interrupt */
863 i2c_writel(i2c, 0, REG_IEN);
864 i2c_writel(i2c, REG_CON_EN | REG_CON_STOP, REG_CON);
865
866 i2c->state = STATE_IDLE;
867
868 ret = -ETIMEDOUT;
869 break;
870 }
871
872 if (i2c->error) {
873 ret = i2c->error;
874 break;
875 }
876 }
877
878 clk_disable(i2c->clk);
879 spin_unlock_irqrestore(&i2c->lock, flags);
880
Dmitry Torokhovc6cbfb92015-04-20 15:14:47 -0700881 return ret < 0 ? ret : num;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200882}
883
884static u32 rk3x_i2c_func(struct i2c_adapter *adap)
885{
886 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
887}
888
889static const struct i2c_algorithm rk3x_i2c_algorithm = {
890 .master_xfer = rk3x_i2c_xfer,
891 .functionality = rk3x_i2c_func,
892};
893
David Wubef358c2016-05-16 21:57:39 +0800894static const struct rk3x_i2c_soc_data rk3066_soc_data = {
895 .grf_offset = 0x154,
896};
897
898static const struct rk3x_i2c_soc_data rk3188_soc_data = {
899 .grf_offset = 0x0a4,
900};
901
902static const struct rk3x_i2c_soc_data rk3228_soc_data = {
903 .grf_offset = -1,
904};
905
906static const struct rk3x_i2c_soc_data rk3288_soc_data = {
907 .grf_offset = -1,
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200908};
909
910static const struct of_device_id rk3x_i2c_match[] = {
David Wubef358c2016-05-16 21:57:39 +0800911 {
912 .compatible = "rockchip,rk3066-i2c",
913 .data = (void *)&rk3066_soc_data
914 },
915 {
916 .compatible = "rockchip,rk3188-i2c",
917 .data = (void *)&rk3188_soc_data
918 },
919 {
920 .compatible = "rockchip,rk3228-i2c",
921 .data = (void *)&rk3228_soc_data
922 },
923 {
924 .compatible = "rockchip,rk3288-i2c",
925 .data = (void *)&rk3288_soc_data
926 },
Dan Carpenterc51bd6a2014-06-12 23:56:09 +0200927 {},
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200928};
Luis de Bethencourt598cf162015-10-20 15:16:29 +0100929MODULE_DEVICE_TABLE(of, rk3x_i2c_match);
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200930
931static int rk3x_i2c_probe(struct platform_device *pdev)
932{
933 struct device_node *np = pdev->dev.of_node;
934 const struct of_device_id *match;
935 struct rk3x_i2c *i2c;
936 struct resource *mem;
937 int ret = 0;
938 int bus_nr;
939 u32 value;
940 int irq;
Max Schwarz249051f2014-11-20 10:26:50 +0100941 unsigned long clk_rate;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200942
943 i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
944 if (!i2c)
945 return -ENOMEM;
946
947 match = of_match_node(rk3x_i2c_match, np);
948 i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
949
David Wu1ab92952016-03-17 00:57:17 +0800950 /* use common interface to get I2C timing properties */
951 i2c_parse_fw_timings(&pdev->dev, &i2c->t, true);
addy ke1330e292014-12-11 19:02:40 +0800952
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200953 strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
954 i2c->adap.owner = THIS_MODULE;
955 i2c->adap.algo = &rk3x_i2c_algorithm;
956 i2c->adap.retries = 3;
957 i2c->adap.dev.of_node = np;
958 i2c->adap.algo_data = i2c;
959 i2c->adap.dev.parent = &pdev->dev;
960
961 i2c->dev = &pdev->dev;
962
963 spin_lock_init(&i2c->lock);
964 init_waitqueue_head(&i2c->wait);
965
966 i2c->clk = devm_clk_get(&pdev->dev, NULL);
967 if (IS_ERR(i2c->clk)) {
968 dev_err(&pdev->dev, "cannot get clock\n");
969 return PTR_ERR(i2c->clk);
970 }
971
972 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
973 i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
974 if (IS_ERR(i2c->regs))
975 return PTR_ERR(i2c->regs);
976
977 /* Try to set the I2C adapter number from dt */
978 bus_nr = of_alias_get_id(np, "i2c");
979
980 /*
981 * Switch to new interface if the SoC also offers the old one.
982 * The control bit is located in the GRF register space.
983 */
984 if (i2c->soc_data->grf_offset >= 0) {
985 struct regmap *grf;
986
987 grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
988 if (IS_ERR(grf)) {
989 dev_err(&pdev->dev,
990 "rk3x-i2c needs 'rockchip,grf' property\n");
991 return PTR_ERR(grf);
992 }
993
994 if (bus_nr < 0) {
995 dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
996 return -EINVAL;
997 }
998
999 /* 27+i: write mask, 11+i: value */
1000 value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
1001
1002 ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
1003 if (ret != 0) {
1004 dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
1005 return ret;
1006 }
1007 }
1008
1009 /* IRQ setup */
1010 irq = platform_get_irq(pdev, 0);
1011 if (irq < 0) {
1012 dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
1013 return irq;
1014 }
1015
1016 ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
1017 0, dev_name(&pdev->dev), i2c);
1018 if (ret < 0) {
1019 dev_err(&pdev->dev, "cannot request IRQ\n");
1020 return ret;
1021 }
1022
1023 platform_set_drvdata(pdev, i2c);
1024
1025 ret = clk_prepare(i2c->clk);
1026 if (ret < 0) {
1027 dev_err(&pdev->dev, "Could not prepare clock\n");
1028 return ret;
1029 }
1030
Max Schwarz249051f2014-11-20 10:26:50 +01001031 i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
1032 ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
1033 if (ret != 0) {
1034 dev_err(&pdev->dev, "Unable to register clock notifier\n");
1035 goto err_clk;
1036 }
1037
1038 clk_rate = clk_get_rate(i2c->clk);
1039 rk3x_i2c_adapt_div(i2c, clk_rate);
1040
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001041 ret = i2c_add_adapter(&i2c->adap);
1042 if (ret < 0) {
1043 dev_err(&pdev->dev, "Could not register adapter\n");
Max Schwarz249051f2014-11-20 10:26:50 +01001044 goto err_clk_notifier;
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001045 }
1046
1047 dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
1048
1049 return 0;
1050
Max Schwarz249051f2014-11-20 10:26:50 +01001051err_clk_notifier:
1052 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001053err_clk:
1054 clk_unprepare(i2c->clk);
1055 return ret;
1056}
1057
1058static int rk3x_i2c_remove(struct platform_device *pdev)
1059{
1060 struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
1061
1062 i2c_del_adapter(&i2c->adap);
Max Schwarz249051f2014-11-20 10:26:50 +01001063
1064 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001065 clk_unprepare(i2c->clk);
1066
1067 return 0;
1068}
1069
1070static struct platform_driver rk3x_i2c_driver = {
1071 .probe = rk3x_i2c_probe,
1072 .remove = rk3x_i2c_remove,
1073 .driver = {
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001074 .name = "rk3x-i2c",
1075 .of_match_table = rk3x_i2c_match,
1076 },
1077};
1078
1079module_platform_driver(rk3x_i2c_driver);
1080
1081MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
1082MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
1083MODULE_LICENSE("GPL v2");