blob: e9ce6a33e8c430fcb12fe6dd7801d09f0aec749f [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
David Wu7e086c32016-05-16 22:05:03 +080061#define REG_CON_TUNING_MASK GENMASK(15, 8)
62
63#define REG_CON_SDA_CFG(cfg) ((cfg) << 8)
64#define REG_CON_STA_CFG(cfg) ((cfg) << 12)
65#define REG_CON_STO_CFG(cfg) ((cfg) << 14)
66
Max Schwarzc41aa3c2014-06-11 22:34:37 +020067/* REG_MRXADDR bits */
68#define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
69
70/* REG_IEN/REG_IPD bits */
71#define REG_INT_BTF BIT(0) /* a byte was transmitted */
72#define REG_INT_BRF BIT(1) /* a byte was received */
73#define REG_INT_MBTF BIT(2) /* master data transmit finished */
74#define REG_INT_MBRF BIT(3) /* master data receive finished */
75#define REG_INT_START BIT(4) /* START condition generated */
76#define REG_INT_STOP BIT(5) /* STOP condition generated */
77#define REG_INT_NAKRCV BIT(6) /* NACK received */
78#define REG_INT_ALL 0x7f
79
80/* Constants */
Doug Anderson44897502015-05-11 12:44:28 -070081#define WAIT_TIMEOUT 1000 /* ms */
Max Schwarzc41aa3c2014-06-11 22:34:37 +020082#define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
83
David Wue26747b2016-05-16 21:57:37 +080084/**
David Wub58fd3b2016-05-16 22:03:24 +080085 * struct i2c_spec_values:
David Wu7e086c32016-05-16 22:05:03 +080086 * @min_hold_start_ns: min hold time (repeated) START condition
David Wub58fd3b2016-05-16 22:03:24 +080087 * @min_low_ns: min LOW period of the SCL clock
88 * @min_high_ns: min HIGH period of the SCL cloc
89 * @min_setup_start_ns: min set-up time for a repeated START conditio
90 * @max_data_hold_ns: max data hold time
David Wu7e086c32016-05-16 22:05:03 +080091 * @min_data_setup_ns: min data set-up time
92 * @min_setup_stop_ns: min set-up time for STOP condition
93 * @min_hold_buffer_ns: min bus free time between a STOP and
94 * START condition
David Wub58fd3b2016-05-16 22:03:24 +080095 */
96struct i2c_spec_values {
David Wu7e086c32016-05-16 22:05:03 +080097 unsigned long min_hold_start_ns;
David Wub58fd3b2016-05-16 22:03:24 +080098 unsigned long min_low_ns;
99 unsigned long min_high_ns;
100 unsigned long min_setup_start_ns;
101 unsigned long max_data_hold_ns;
David Wu7e086c32016-05-16 22:05:03 +0800102 unsigned long min_data_setup_ns;
103 unsigned long min_setup_stop_ns;
104 unsigned long min_hold_buffer_ns;
David Wub58fd3b2016-05-16 22:03:24 +0800105};
106
107static const struct i2c_spec_values standard_mode_spec = {
David Wu7e086c32016-05-16 22:05:03 +0800108 .min_hold_start_ns = 4000,
David Wub58fd3b2016-05-16 22:03:24 +0800109 .min_low_ns = 4700,
110 .min_high_ns = 4000,
111 .min_setup_start_ns = 4700,
112 .max_data_hold_ns = 3450,
David Wu7e086c32016-05-16 22:05:03 +0800113 .min_data_setup_ns = 250,
114 .min_setup_stop_ns = 4000,
115 .min_hold_buffer_ns = 4700,
David Wub58fd3b2016-05-16 22:03:24 +0800116};
117
118static const struct i2c_spec_values fast_mode_spec = {
David Wu7e086c32016-05-16 22:05:03 +0800119 .min_hold_start_ns = 600,
David Wub58fd3b2016-05-16 22:03:24 +0800120 .min_low_ns = 1300,
121 .min_high_ns = 600,
122 .min_setup_start_ns = 600,
123 .max_data_hold_ns = 900,
David Wu7e086c32016-05-16 22:05:03 +0800124 .min_data_setup_ns = 100,
125 .min_setup_stop_ns = 600,
126 .min_hold_buffer_ns = 1300,
David Wub58fd3b2016-05-16 22:03:24 +0800127};
128
129/**
David Wue26747b2016-05-16 21:57:37 +0800130 * struct rk3x_i2c_calced_timings:
131 * @div_low: Divider output for low
132 * @div_high: Divider output for high
David Wu7e086c32016-05-16 22:05:03 +0800133 * @tuning: Used to adjust setup/hold data time,
134 * setup/hold start time and setup stop time for
135 * v1's calc_timings, the tuning should all be 0
136 * for old hardware anyone using v0's calc_timings.
David Wue26747b2016-05-16 21:57:37 +0800137 */
138struct rk3x_i2c_calced_timings {
139 unsigned long div_low;
140 unsigned long div_high;
David Wu7e086c32016-05-16 22:05:03 +0800141 unsigned int tuning;
David Wue26747b2016-05-16 21:57:37 +0800142};
143
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200144enum rk3x_i2c_state {
145 STATE_IDLE,
146 STATE_START,
147 STATE_READ,
148 STATE_WRITE,
149 STATE_STOP
150};
151
152/**
153 * @grf_offset: offset inside the grf regmap for setting the i2c type
David Wu7e086c32016-05-16 22:05:03 +0800154 * @calc_timings: Callback function for i2c timing information calculated
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200155 */
156struct rk3x_i2c_soc_data {
157 int grf_offset;
David Wu7e086c32016-05-16 22:05:03 +0800158 int (*calc_timings)(unsigned long, struct i2c_timings *,
159 struct rk3x_i2c_calced_timings *);
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200160};
161
David Wu0a6ad2f2016-05-16 21:57:36 +0800162/**
163 * struct rk3x_i2c - private data of the controller
164 * @adap: corresponding I2C adapter
165 * @dev: device for this controller
166 * @soc_data: related soc data struct
167 * @regs: virtual memory area
David Wu7e086c32016-05-16 22:05:03 +0800168 * @clk: function clk for rk3399 or function & Bus clks for others
169 * @pclk: Bus clk for rk3399
David Wu0a6ad2f2016-05-16 21:57:36 +0800170 * @clk_rate_nb: i2c clk rate change notify
171 * @t: I2C known timing information
172 * @lock: spinlock for the i2c bus
173 * @wait: the waitqueue to wait for i2c transfer
174 * @busy: the condition for the event to wait for
175 * @msg: current i2c message
176 * @addr: addr of i2c slave device
177 * @mode: mode of i2c transfer
178 * @is_last_msg: flag determines whether it is the last msg in this transfer
179 * @state: state of i2c transfer
180 * @processed: byte length which has been send or received
181 * @error: error code for i2c transfer
182 */
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200183struct rk3x_i2c {
184 struct i2c_adapter adap;
185 struct device *dev;
186 struct rk3x_i2c_soc_data *soc_data;
187
188 /* Hardware resources */
189 void __iomem *regs;
190 struct clk *clk;
David Wu7e086c32016-05-16 22:05:03 +0800191 struct clk *pclk;
Max Schwarz249051f2014-11-20 10:26:50 +0100192 struct notifier_block clk_rate_nb;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200193
194 /* Settings */
David Wu1ab92952016-03-17 00:57:17 +0800195 struct i2c_timings t;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200196
197 /* Synchronization & notification */
198 spinlock_t lock;
199 wait_queue_head_t wait;
200 bool busy;
201
202 /* Current message */
203 struct i2c_msg *msg;
204 u8 addr;
205 unsigned int mode;
206 bool is_last_msg;
207
208 /* I2C state machine */
209 enum rk3x_i2c_state state;
David Wu0a6ad2f2016-05-16 21:57:36 +0800210 unsigned int processed;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200211 int error;
212};
213
214static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
215 unsigned int offset)
216{
217 writel(value, i2c->regs + offset);
218}
219
220static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
221{
222 return readl(i2c->regs + offset);
223}
224
225/* Reset all interrupt pending bits */
226static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
227{
228 i2c_writel(i2c, REG_INT_ALL, REG_IPD);
229}
230
231/**
232 * Generate a START condition, which triggers a REG_INT_START interrupt.
233 */
234static void rk3x_i2c_start(struct rk3x_i2c *i2c)
235{
David Wu7e086c32016-05-16 22:05:03 +0800236 u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200237
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200238 i2c_writel(i2c, REG_INT_START, REG_IEN);
239
240 /* enable adapter with correct mode, send START condition */
David Wu7e086c32016-05-16 22:05:03 +0800241 val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200242
243 /* if we want to react to NACK, set ACTACK bit */
244 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
245 val |= REG_CON_ACTACK;
246
247 i2c_writel(i2c, val, REG_CON);
248}
249
250/**
251 * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
252 *
253 * @error: Error code to return in rk3x_i2c_xfer
254 */
255static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
256{
257 unsigned int ctrl;
258
259 i2c->processed = 0;
260 i2c->msg = NULL;
261 i2c->error = error;
262
263 if (i2c->is_last_msg) {
264 /* Enable stop interrupt */
265 i2c_writel(i2c, REG_INT_STOP, REG_IEN);
266
267 i2c->state = STATE_STOP;
268
269 ctrl = i2c_readl(i2c, REG_CON);
270 ctrl |= REG_CON_STOP;
271 i2c_writel(i2c, ctrl, REG_CON);
272 } else {
273 /* Signal rk3x_i2c_xfer to start the next message. */
274 i2c->busy = false;
275 i2c->state = STATE_IDLE;
276
277 /*
278 * The HW is actually not capable of REPEATED START. But we can
279 * get the intended effect by resetting its internal state
280 * and issuing an ordinary START.
281 */
David Wu7e086c32016-05-16 22:05:03 +0800282 ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
283 i2c_writel(i2c, ctrl, REG_CON);
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200284
285 /* signal that we are finished with the current msg */
286 wake_up(&i2c->wait);
287 }
288}
289
290/**
291 * Setup a read according to i2c->msg
292 */
293static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
294{
295 unsigned int len = i2c->msg->len - i2c->processed;
296 u32 con;
297
298 con = i2c_readl(i2c, REG_CON);
299
300 /*
301 * The hw can read up to 32 bytes at a time. If we need more than one
302 * chunk, send an ACK after the last byte of the current chunk.
303 */
Doug Anderson29209332014-08-22 10:43:44 -0700304 if (len > 32) {
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200305 len = 32;
306 con &= ~REG_CON_LASTACK;
307 } else {
308 con |= REG_CON_LASTACK;
309 }
310
311 /* make sure we are in plain RX mode if we read a second chunk */
312 if (i2c->processed != 0) {
313 con &= ~REG_CON_MOD_MASK;
314 con |= REG_CON_MOD(REG_CON_MOD_RX);
315 }
316
317 i2c_writel(i2c, con, REG_CON);
318 i2c_writel(i2c, len, REG_MRXCNT);
319}
320
321/**
322 * Fill the transmit buffer with data from i2c->msg
323 */
324static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
325{
326 unsigned int i, j;
327 u32 cnt = 0;
328 u32 val;
329 u8 byte;
330
331 for (i = 0; i < 8; ++i) {
332 val = 0;
333 for (j = 0; j < 4; ++j) {
Alexandru M Stancf270202014-10-01 10:40:41 -0700334 if ((i2c->processed == i2c->msg->len) && (cnt != 0))
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200335 break;
336
337 if (i2c->processed == 0 && cnt == 0)
338 byte = (i2c->addr & 0x7f) << 1;
339 else
340 byte = i2c->msg->buf[i2c->processed++];
341
342 val |= byte << (j * 8);
343 cnt++;
344 }
345
346 i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
347
348 if (i2c->processed == i2c->msg->len)
349 break;
350 }
351
352 i2c_writel(i2c, cnt, REG_MTXCNT);
353}
354
355
356/* IRQ handlers for individual states */
357
358static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
359{
360 if (!(ipd & REG_INT_START)) {
361 rk3x_i2c_stop(i2c, -EIO);
362 dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
363 rk3x_i2c_clean_ipd(i2c);
364 return;
365 }
366
367 /* ack interrupt */
368 i2c_writel(i2c, REG_INT_START, REG_IPD);
369
370 /* disable start bit */
371 i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
372
373 /* enable appropriate interrupts and transition */
374 if (i2c->mode == REG_CON_MOD_TX) {
375 i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
376 i2c->state = STATE_WRITE;
377 rk3x_i2c_fill_transmit_buf(i2c);
378 } else {
379 /* in any other case, we are going to be reading. */
380 i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
381 i2c->state = STATE_READ;
382 rk3x_i2c_prepare_read(i2c);
383 }
384}
385
386static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
387{
388 if (!(ipd & REG_INT_MBTF)) {
389 rk3x_i2c_stop(i2c, -EIO);
390 dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
391 rk3x_i2c_clean_ipd(i2c);
392 return;
393 }
394
395 /* ack interrupt */
396 i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
397
398 /* are we finished? */
399 if (i2c->processed == i2c->msg->len)
400 rk3x_i2c_stop(i2c, i2c->error);
401 else
402 rk3x_i2c_fill_transmit_buf(i2c);
403}
404
405static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
406{
407 unsigned int i;
408 unsigned int len = i2c->msg->len - i2c->processed;
409 u32 uninitialized_var(val);
410 u8 byte;
411
412 /* we only care for MBRF here. */
413 if (!(ipd & REG_INT_MBRF))
414 return;
415
416 /* ack interrupt */
417 i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
418
addy ke5da43092014-08-23 02:00:52 +0800419 /* Can only handle a maximum of 32 bytes at a time */
420 if (len > 32)
421 len = 32;
422
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200423 /* read the data from receive buffer */
424 for (i = 0; i < len; ++i) {
425 if (i % 4 == 0)
426 val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
427
428 byte = (val >> ((i % 4) * 8)) & 0xff;
429 i2c->msg->buf[i2c->processed++] = byte;
430 }
431
432 /* are we finished? */
433 if (i2c->processed == i2c->msg->len)
434 rk3x_i2c_stop(i2c, i2c->error);
435 else
436 rk3x_i2c_prepare_read(i2c);
437}
438
439static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
440{
441 unsigned int con;
442
443 if (!(ipd & REG_INT_STOP)) {
444 rk3x_i2c_stop(i2c, -EIO);
445 dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
446 rk3x_i2c_clean_ipd(i2c);
447 return;
448 }
449
450 /* ack interrupt */
451 i2c_writel(i2c, REG_INT_STOP, REG_IPD);
452
453 /* disable STOP bit */
454 con = i2c_readl(i2c, REG_CON);
455 con &= ~REG_CON_STOP;
456 i2c_writel(i2c, con, REG_CON);
457
458 i2c->busy = false;
459 i2c->state = STATE_IDLE;
460
461 /* signal rk3x_i2c_xfer that we are finished */
462 wake_up(&i2c->wait);
463}
464
465static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
466{
467 struct rk3x_i2c *i2c = dev_id;
468 unsigned int ipd;
469
470 spin_lock(&i2c->lock);
471
472 ipd = i2c_readl(i2c, REG_IPD);
473 if (i2c->state == STATE_IDLE) {
474 dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
475 rk3x_i2c_clean_ipd(i2c);
476 goto out;
477 }
478
479 dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
480
481 /* Clean interrupt bits we don't care about */
482 ipd &= ~(REG_INT_BRF | REG_INT_BTF);
483
484 if (ipd & REG_INT_NAKRCV) {
485 /*
486 * We got a NACK in the last operation. Depending on whether
487 * IGNORE_NAK is set, we have to stop the operation and report
488 * an error.
489 */
490 i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
491
492 ipd &= ~REG_INT_NAKRCV;
493
494 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
495 rk3x_i2c_stop(i2c, -ENXIO);
496 }
497
498 /* is there anything left to handle? */
Doug Anderson29209332014-08-22 10:43:44 -0700499 if ((ipd & REG_INT_ALL) == 0)
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200500 goto out;
501
502 switch (i2c->state) {
503 case STATE_START:
504 rk3x_i2c_handle_start(i2c, ipd);
505 break;
506 case STATE_WRITE:
507 rk3x_i2c_handle_write(i2c, ipd);
508 break;
509 case STATE_READ:
510 rk3x_i2c_handle_read(i2c, ipd);
511 break;
512 case STATE_STOP:
513 rk3x_i2c_handle_stop(i2c, ipd);
514 break;
515 case STATE_IDLE:
516 break;
517 }
518
519out:
520 spin_unlock(&i2c->lock);
521 return IRQ_HANDLED;
522}
523
Max Schwarz249051f2014-11-20 10:26:50 +0100524/**
David Wub58fd3b2016-05-16 22:03:24 +0800525 * Get timing values of I2C specification
526 *
527 * @speed: Desired SCL frequency
528 *
529 * Returns: Matched i2c spec values.
530 */
531static const struct i2c_spec_values *rk3x_i2c_get_spec(unsigned int speed)
532{
533 if (speed <= 100000)
534 return &standard_mode_spec;
535 else
536 return &fast_mode_spec;
537}
538
539/**
Max Schwarz249051f2014-11-20 10:26:50 +0100540 * Calculate divider values for desired SCL frequency
541 *
542 * @clk_rate: I2C input clock rate
David Wue26747b2016-05-16 21:57:37 +0800543 * @t: Known I2C timing information
544 * @t_calc: Caculated rk3x private timings that would be written into regs
Max Schwarz249051f2014-11-20 10:26:50 +0100545 *
546 * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
547 * a best-effort divider value is returned in divs. If the target rate is
548 * too high, we silently use the highest possible rate.
549 */
David Wu7e086c32016-05-16 22:05:03 +0800550static int rk3x_i2c_v0_calc_timings(unsigned long clk_rate,
551 struct i2c_timings *t,
552 struct rk3x_i2c_calced_timings *t_calc)
addy ke0285f8f2014-10-14 14:09:21 +0800553{
addy ke1330e292014-12-11 19:02:40 +0800554 unsigned long min_low_ns, min_high_ns;
addy ke0285f8f2014-10-14 14:09:21 +0800555 unsigned long max_low_ns, min_total_ns;
556
Max Schwarz249051f2014-11-20 10:26:50 +0100557 unsigned long clk_rate_khz, scl_rate_khz;
addy ke0285f8f2014-10-14 14:09:21 +0800558
559 unsigned long min_low_div, min_high_div;
560 unsigned long max_low_div;
561
562 unsigned long min_div_for_hold, min_total_div;
563 unsigned long extra_div, extra_low_div, ideal_low_div;
564
David Wub58fd3b2016-05-16 22:03:24 +0800565 unsigned long data_hold_buffer_ns = 50;
566 const struct i2c_spec_values *spec;
Max Schwarz249051f2014-11-20 10:26:50 +0100567 int ret = 0;
568
addy ke0285f8f2014-10-14 14:09:21 +0800569 /* Only support standard-mode and fast-mode */
David Wu1ab92952016-03-17 00:57:17 +0800570 if (WARN_ON(t->bus_freq_hz > 400000))
571 t->bus_freq_hz = 400000;
addy ke0285f8f2014-10-14 14:09:21 +0800572
573 /* prevent scl_rate_khz from becoming 0 */
David Wu1ab92952016-03-17 00:57:17 +0800574 if (WARN_ON(t->bus_freq_hz < 1000))
575 t->bus_freq_hz = 1000;
addy ke0285f8f2014-10-14 14:09:21 +0800576
577 /*
addy ke1330e292014-12-11 19:02:40 +0800578 * min_low_ns: The minimum number of ns we need to hold low to
579 * meet I2C specification, should include fall time.
580 * min_high_ns: The minimum number of ns we need to hold high to
581 * meet I2C specification, should include rise time.
582 * max_low_ns: The maximum number of ns we can hold low to meet
583 * I2C specification.
addy ke0285f8f2014-10-14 14:09:21 +0800584 *
addy ke1330e292014-12-11 19:02:40 +0800585 * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
addy ke0285f8f2014-10-14 14:09:21 +0800586 * This is because the i2c host on Rockchip holds the data line
587 * for half the low time.
588 */
David Wub58fd3b2016-05-16 22:03:24 +0800589 spec = rk3x_i2c_get_spec(t->bus_freq_hz);
590 min_high_ns = t->scl_rise_ns + spec->min_high_ns;
Doug Anderson387f0de2014-12-18 09:44:07 -0800591
592 /*
593 * Timings for repeated start:
594 * - controller appears to drop SDA at .875x (7/8) programmed clk high.
595 * - controller appears to keep SCL high for 2x programmed clk high.
596 *
597 * We need to account for those rules in picking our "high" time so
598 * we meet tSU;STA and tHD;STA times.
599 */
David Wub58fd3b2016-05-16 22:03:24 +0800600 min_high_ns = max(min_high_ns, DIV_ROUND_UP(
601 (t->scl_rise_ns + spec->min_setup_start_ns) * 1000, 875));
602 min_high_ns = max(min_high_ns, DIV_ROUND_UP(
603 (t->scl_rise_ns + spec->min_setup_start_ns + t->sda_fall_ns +
604 spec->min_high_ns), 2));
Doug Anderson387f0de2014-12-18 09:44:07 -0800605
David Wub58fd3b2016-05-16 22:03:24 +0800606 min_low_ns = t->scl_fall_ns + spec->min_low_ns;
607 max_low_ns = spec->max_data_hold_ns * 2 - data_hold_buffer_ns;
addy ke0285f8f2014-10-14 14:09:21 +0800608 min_total_ns = min_low_ns + min_high_ns;
609
610 /* Adjust to avoid overflow */
Max Schwarz249051f2014-11-20 10:26:50 +0100611 clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
David Wu1ab92952016-03-17 00:57:17 +0800612 scl_rate_khz = t->bus_freq_hz / 1000;
addy ke0285f8f2014-10-14 14:09:21 +0800613
614 /*
615 * We need the total div to be >= this number
616 * so we don't clock too fast.
617 */
Max Schwarz249051f2014-11-20 10:26:50 +0100618 min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
addy ke0285f8f2014-10-14 14:09:21 +0800619
620 /* These are the min dividers needed for min hold times. */
Max Schwarz249051f2014-11-20 10:26:50 +0100621 min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
622 min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
addy ke0285f8f2014-10-14 14:09:21 +0800623 min_div_for_hold = (min_low_div + min_high_div);
624
625 /*
addy ke1330e292014-12-11 19:02:40 +0800626 * This is the maximum divider so we don't go over the maximum.
627 * We don't round up here (we round down) since this is a maximum.
addy ke0285f8f2014-10-14 14:09:21 +0800628 */
Max Schwarz249051f2014-11-20 10:26:50 +0100629 max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
addy ke0285f8f2014-10-14 14:09:21 +0800630
631 if (min_low_div > max_low_div) {
632 WARN_ONCE(true,
633 "Conflicting, min_low_div %lu, max_low_div %lu\n",
634 min_low_div, max_low_div);
635 max_low_div = min_low_div;
636 }
637
638 if (min_div_for_hold > min_total_div) {
639 /*
640 * Time needed to meet hold requirements is important.
641 * Just use that.
642 */
David Wue26747b2016-05-16 21:57:37 +0800643 t_calc->div_low = min_low_div;
644 t_calc->div_high = min_high_div;
addy ke0285f8f2014-10-14 14:09:21 +0800645 } else {
646 /*
647 * We've got to distribute some time among the low and high
648 * so we don't run too fast.
649 */
650 extra_div = min_total_div - min_div_for_hold;
651
652 /*
653 * We'll try to split things up perfectly evenly,
654 * biasing slightly towards having a higher div
655 * for low (spend more time low).
656 */
Max Schwarz249051f2014-11-20 10:26:50 +0100657 ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
addy ke0285f8f2014-10-14 14:09:21 +0800658 scl_rate_khz * 8 * min_total_ns);
659
addy ke1330e292014-12-11 19:02:40 +0800660 /* Don't allow it to go over the maximum */
addy ke0285f8f2014-10-14 14:09:21 +0800661 if (ideal_low_div > max_low_div)
662 ideal_low_div = max_low_div;
663
664 /*
665 * Handle when the ideal low div is going to take up
666 * more than we have.
667 */
668 if (ideal_low_div > min_low_div + extra_div)
669 ideal_low_div = min_low_div + extra_div;
670
671 /* Give low the "ideal" and give high whatever extra is left */
672 extra_low_div = ideal_low_div - min_low_div;
David Wue26747b2016-05-16 21:57:37 +0800673 t_calc->div_low = ideal_low_div;
674 t_calc->div_high = min_high_div + (extra_div - extra_low_div);
addy ke0285f8f2014-10-14 14:09:21 +0800675 }
676
677 /*
Max Schwarz249051f2014-11-20 10:26:50 +0100678 * Adjust to the fact that the hardware has an implicit "+1".
679 * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
680 */
David Wue26747b2016-05-16 21:57:37 +0800681 t_calc->div_low--;
682 t_calc->div_high--;
addy ke0285f8f2014-10-14 14:09:21 +0800683
Max Schwarz249051f2014-11-20 10:26:50 +0100684 /* Maximum divider supported by hw is 0xffff */
David Wue26747b2016-05-16 21:57:37 +0800685 if (t_calc->div_low > 0xffff) {
686 t_calc->div_low = 0xffff;
Max Schwarz249051f2014-11-20 10:26:50 +0100687 ret = -EINVAL;
688 }
addy ke0285f8f2014-10-14 14:09:21 +0800689
David Wue26747b2016-05-16 21:57:37 +0800690 if (t_calc->div_high > 0xffff) {
691 t_calc->div_high = 0xffff;
Max Schwarz249051f2014-11-20 10:26:50 +0100692 ret = -EINVAL;
693 }
addy ke0285f8f2014-10-14 14:09:21 +0800694
695 return ret;
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200696}
697
David Wu7e086c32016-05-16 22:05:03 +0800698/**
699 * Calculate timing values for desired SCL frequency
700 *
701 * @clk_rate: I2C input clock rate
702 * @t: Known I2C timing information
703 * @t_calc: Caculated rk3x private timings that would be written into regs
704 *
705 * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
706 * a best-effort divider value is returned in divs. If the target rate is
707 * too high, we silently use the highest possible rate.
708 * The following formulas are v1's method to calculate timings.
709 *
710 * l = divl + 1;
711 * h = divh + 1;
712 * s = sda_update_config + 1;
713 * u = start_setup_config + 1;
714 * p = stop_setup_config + 1;
715 * T = Tclk_i2c;
716 *
717 * tHigh = 8 * h * T;
718 * tLow = 8 * l * T;
719 *
720 * tHD;sda = (l * s + 1) * T;
721 * tSU;sda = [(8 - s) * l + 1] * T;
722 * tI2C = 8 * (l + h) * T;
723 *
724 * tSU;sta = (8h * u + 1) * T;
725 * tHD;sta = [8h * (u + 1) - 1] * T;
726 * tSU;sto = (8h * p + 1) * T;
727 */
728static int rk3x_i2c_v1_calc_timings(unsigned long clk_rate,
729 struct i2c_timings *t,
730 struct rk3x_i2c_calced_timings *t_calc)
731{
732 unsigned long min_low_ns, min_high_ns, min_total_ns;
733 unsigned long min_setup_start_ns, min_setup_data_ns;
734 unsigned long min_setup_stop_ns, max_hold_data_ns;
735
736 unsigned long clk_rate_khz, scl_rate_khz;
737
738 unsigned long min_low_div, min_high_div;
739
740 unsigned long min_div_for_hold, min_total_div;
741 unsigned long extra_div, extra_low_div;
742 unsigned long sda_update_cfg, stp_sta_cfg, stp_sto_cfg;
743
744 const struct i2c_spec_values *spec;
745 int ret = 0;
746
747 /* Support standard-mode and fast-mode */
748 if (WARN_ON(t->bus_freq_hz > 400000))
749 t->bus_freq_hz = 400000;
750
751 /* prevent scl_rate_khz from becoming 0 */
752 if (WARN_ON(t->bus_freq_hz < 1000))
753 t->bus_freq_hz = 1000;
754
755 /*
756 * min_low_ns: The minimum number of ns we need to hold low to
757 * meet I2C specification, should include fall time.
758 * min_high_ns: The minimum number of ns we need to hold high to
759 * meet I2C specification, should include rise time.
760 */
761 spec = rk3x_i2c_get_spec(t->bus_freq_hz);
762
763 /* calculate min-divh and min-divl */
764 clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
765 scl_rate_khz = t->bus_freq_hz / 1000;
766 min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
767
768 min_high_ns = t->scl_rise_ns + spec->min_high_ns;
769 min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
770
771 min_low_ns = t->scl_fall_ns + spec->min_low_ns;
772 min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
773
774 /*
775 * Final divh and divl must be greater than 0, otherwise the
776 * hardware would not output the i2c clk.
777 */
778 min_high_div = (min_high_div < 1) ? 2 : min_high_div;
779 min_low_div = (min_low_div < 1) ? 2 : min_low_div;
780
781 /* These are the min dividers needed for min hold times. */
782 min_div_for_hold = (min_low_div + min_high_div);
783 min_total_ns = min_low_ns + min_high_ns;
784
785 /*
786 * This is the maximum divider so we don't go over the maximum.
787 * We don't round up here (we round down) since this is a maximum.
788 */
789 if (min_div_for_hold >= min_total_div) {
790 /*
791 * Time needed to meet hold requirements is important.
792 * Just use that.
793 */
794 t_calc->div_low = min_low_div;
795 t_calc->div_high = min_high_div;
796 } else {
797 /*
798 * We've got to distribute some time among the low and high
799 * so we don't run too fast.
800 * We'll try to split things up by the scale of min_low_div and
801 * min_high_div, biasing slightly towards having a higher div
802 * for low (spend more time low).
803 */
804 extra_div = min_total_div - min_div_for_hold;
805 extra_low_div = DIV_ROUND_UP(min_low_div * extra_div,
806 min_div_for_hold);
807
808 t_calc->div_low = min_low_div + extra_low_div;
809 t_calc->div_high = min_high_div + (extra_div - extra_low_div);
810 }
811
812 /*
813 * calculate sda data hold count by the rules, data_upd_st:3
814 * is a appropriate value to reduce calculated times.
815 */
816 for (sda_update_cfg = 3; sda_update_cfg > 0; sda_update_cfg--) {
817 max_hold_data_ns = DIV_ROUND_UP((sda_update_cfg
818 * (t_calc->div_low) + 1)
819 * 1000000, clk_rate_khz);
820 min_setup_data_ns = DIV_ROUND_UP(((8 - sda_update_cfg)
821 * (t_calc->div_low) + 1)
822 * 1000000, clk_rate_khz);
823 if ((max_hold_data_ns < spec->max_data_hold_ns) &&
824 (min_setup_data_ns > spec->min_data_setup_ns))
825 break;
826 }
827
828 /* calculate setup start config */
829 min_setup_start_ns = t->scl_rise_ns + spec->min_setup_start_ns;
830 stp_sta_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_start_ns
831 - 1000000, 8 * 1000000 * (t_calc->div_high));
832
833 /* calculate setup stop config */
834 min_setup_stop_ns = t->scl_rise_ns + spec->min_setup_stop_ns;
835 stp_sto_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_stop_ns
836 - 1000000, 8 * 1000000 * (t_calc->div_high));
837
838 t_calc->tuning = REG_CON_SDA_CFG(--sda_update_cfg) |
839 REG_CON_STA_CFG(--stp_sta_cfg) |
840 REG_CON_STO_CFG(--stp_sto_cfg);
841
842 t_calc->div_low--;
843 t_calc->div_high--;
844
845 /* Maximum divider supported by hw is 0xffff */
846 if (t_calc->div_low > 0xffff) {
847 t_calc->div_low = 0xffff;
848 ret = -EINVAL;
849 }
850
851 if (t_calc->div_high > 0xffff) {
852 t_calc->div_high = 0xffff;
853 ret = -EINVAL;
854 }
855
856 return ret;
857}
858
Max Schwarz249051f2014-11-20 10:26:50 +0100859static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
860{
David Wu1ab92952016-03-17 00:57:17 +0800861 struct i2c_timings *t = &i2c->t;
David Wue26747b2016-05-16 21:57:37 +0800862 struct rk3x_i2c_calced_timings calc;
Max Schwarz249051f2014-11-20 10:26:50 +0100863 u64 t_low_ns, t_high_ns;
David Wu7e086c32016-05-16 22:05:03 +0800864 unsigned long flags;
865 u32 val;
Max Schwarz249051f2014-11-20 10:26:50 +0100866 int ret;
867
David Wu7e086c32016-05-16 22:05:03 +0800868 ret = i2c->soc_data->calc_timings(clk_rate, t, &calc);
David Wu1ab92952016-03-17 00:57:17 +0800869 WARN_ONCE(ret != 0, "Could not reach SCL freq %u", t->bus_freq_hz);
Max Schwarz249051f2014-11-20 10:26:50 +0100870
David Wu7e086c32016-05-16 22:05:03 +0800871 clk_enable(i2c->pclk);
872
873 spin_lock_irqsave(&i2c->lock, flags);
874 val = i2c_readl(i2c, REG_CON);
875 val &= ~REG_CON_TUNING_MASK;
876 val |= calc.tuning;
877 i2c_writel(i2c, val, REG_CON);
David Wue26747b2016-05-16 21:57:37 +0800878 i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff),
879 REG_CLKDIV);
David Wu7e086c32016-05-16 22:05:03 +0800880 spin_unlock_irqrestore(&i2c->lock, flags);
881
882 clk_disable(i2c->pclk);
Max Schwarz249051f2014-11-20 10:26:50 +0100883
David Wue26747b2016-05-16 21:57:37 +0800884 t_low_ns = div_u64(((u64)calc.div_low + 1) * 8 * 1000000000, clk_rate);
885 t_high_ns = div_u64(((u64)calc.div_high + 1) * 8 * 1000000000,
886 clk_rate);
Max Schwarz249051f2014-11-20 10:26:50 +0100887 dev_dbg(i2c->dev,
888 "CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
889 clk_rate / 1000,
David Wu1ab92952016-03-17 00:57:17 +0800890 1000000000 / t->bus_freq_hz,
Max Schwarz249051f2014-11-20 10:26:50 +0100891 t_low_ns, t_high_ns);
892}
893
894/**
895 * rk3x_i2c_clk_notifier_cb - Clock rate change callback
896 * @nb: Pointer to notifier block
897 * @event: Notification reason
898 * @data: Pointer to notification data object
899 *
900 * The callback checks whether a valid bus frequency can be generated after the
901 * change. If so, the change is acknowledged, otherwise the change is aborted.
902 * New dividers are written to the HW in the pre- or post change notification
903 * depending on the scaling direction.
904 *
905 * Code adapted from i2c-cadence.c.
906 *
907 * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
908 * to acknowedge the change, NOTIFY_DONE if the notification is
909 * considered irrelevant.
910 */
911static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
912 event, void *data)
913{
914 struct clk_notifier_data *ndata = data;
915 struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
David Wue26747b2016-05-16 21:57:37 +0800916 struct rk3x_i2c_calced_timings calc;
Max Schwarz249051f2014-11-20 10:26:50 +0100917
918 switch (event) {
919 case PRE_RATE_CHANGE:
David Wu7e086c32016-05-16 22:05:03 +0800920 /*
921 * Try the calculation (but don't store the result) ahead of
922 * time to see if we need to block the clock change. Timings
923 * shouldn't actually take effect until rk3x_i2c_adapt_div().
924 */
925 if (i2c->soc_data->calc_timings(ndata->new_rate, &i2c->t,
926 &calc) != 0)
Max Schwarz249051f2014-11-20 10:26:50 +0100927 return NOTIFY_STOP;
Max Schwarz249051f2014-11-20 10:26:50 +0100928
929 /* scale up */
930 if (ndata->new_rate > ndata->old_rate)
931 rk3x_i2c_adapt_div(i2c, ndata->new_rate);
932
933 return NOTIFY_OK;
934 case POST_RATE_CHANGE:
935 /* scale down */
936 if (ndata->new_rate < ndata->old_rate)
937 rk3x_i2c_adapt_div(i2c, ndata->new_rate);
938 return NOTIFY_OK;
939 case ABORT_RATE_CHANGE:
940 /* scale up */
941 if (ndata->new_rate > ndata->old_rate)
942 rk3x_i2c_adapt_div(i2c, ndata->old_rate);
943 return NOTIFY_OK;
944 default:
945 return NOTIFY_DONE;
946 }
947}
948
Max Schwarzc41aa3c2014-06-11 22:34:37 +0200949/**
950 * Setup I2C registers for an I2C operation specified by msgs, num.
951 *
952 * Must be called with i2c->lock held.
953 *
954 * @msgs: I2C msgs to process
955 * @num: Number of msgs
956 *
957 * returns: Number of I2C msgs processed or negative in case of error
958 */
959static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
960{
961 u32 addr = (msgs[0].addr & 0x7f) << 1;
962 int ret = 0;
963
964 /*
965 * The I2C adapter can issue a small (len < 4) write packet before
966 * reading. This speeds up SMBus-style register reads.
967 * The MRXADDR/MRXRADDR hold the slave address and the slave register
968 * address in this case.
969 */
970
971 if (num >= 2 && msgs[0].len < 4 &&
972 !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
973 u32 reg_addr = 0;
974 int i;
975
976 dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
977 addr >> 1);
978
979 /* Fill MRXRADDR with the register address(es) */
980 for (i = 0; i < msgs[0].len; ++i) {
981 reg_addr |= msgs[0].buf[i] << (i * 8);
982 reg_addr |= REG_MRXADDR_VALID(i);
983 }
984
985 /* msgs[0] is handled by hw. */
986 i2c->msg = &msgs[1];
987
988 i2c->mode = REG_CON_MOD_REGISTER_TX;
989
990 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
991 i2c_writel(i2c, reg_addr, REG_MRXRADDR);
992
993 ret = 2;
994 } else {
995 /*
996 * We'll have to do it the boring way and process the msgs
997 * one-by-one.
998 */
999
1000 if (msgs[0].flags & I2C_M_RD) {
1001 addr |= 1; /* set read bit */
1002
1003 /*
1004 * We have to transmit the slave addr first. Use
1005 * MOD_REGISTER_TX for that purpose.
1006 */
1007 i2c->mode = REG_CON_MOD_REGISTER_TX;
1008 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
1009 REG_MRXADDR);
1010 i2c_writel(i2c, 0, REG_MRXRADDR);
1011 } else {
1012 i2c->mode = REG_CON_MOD_TX;
1013 }
1014
1015 i2c->msg = &msgs[0];
1016
1017 ret = 1;
1018 }
1019
1020 i2c->addr = msgs[0].addr;
1021 i2c->busy = true;
1022 i2c->state = STATE_START;
1023 i2c->processed = 0;
1024 i2c->error = 0;
1025
1026 rk3x_i2c_clean_ipd(i2c);
1027
1028 return ret;
1029}
1030
1031static int rk3x_i2c_xfer(struct i2c_adapter *adap,
1032 struct i2c_msg *msgs, int num)
1033{
1034 struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
1035 unsigned long timeout, flags;
David Wu7e086c32016-05-16 22:05:03 +08001036 u32 val;
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001037 int ret = 0;
1038 int i;
1039
1040 spin_lock_irqsave(&i2c->lock, flags);
1041
1042 clk_enable(i2c->clk);
David Wu7e086c32016-05-16 22:05:03 +08001043 clk_enable(i2c->pclk);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001044
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001045 i2c->is_last_msg = false;
1046
1047 /*
1048 * Process msgs. We can handle more than one message at once (see
1049 * rk3x_i2c_setup()).
1050 */
1051 for (i = 0; i < num; i += ret) {
1052 ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
1053
1054 if (ret < 0) {
1055 dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
1056 break;
1057 }
1058
1059 if (i + ret >= num)
1060 i2c->is_last_msg = true;
1061
1062 spin_unlock_irqrestore(&i2c->lock, flags);
1063
1064 rk3x_i2c_start(i2c);
1065
1066 timeout = wait_event_timeout(i2c->wait, !i2c->busy,
1067 msecs_to_jiffies(WAIT_TIMEOUT));
1068
1069 spin_lock_irqsave(&i2c->lock, flags);
1070
1071 if (timeout == 0) {
1072 dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
1073 i2c_readl(i2c, REG_IPD), i2c->state);
1074
1075 /* Force a STOP condition without interrupt */
1076 i2c_writel(i2c, 0, REG_IEN);
David Wu7e086c32016-05-16 22:05:03 +08001077 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
1078 val |= REG_CON_EN | REG_CON_STOP;
1079 i2c_writel(i2c, val, REG_CON);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001080
1081 i2c->state = STATE_IDLE;
1082
1083 ret = -ETIMEDOUT;
1084 break;
1085 }
1086
1087 if (i2c->error) {
1088 ret = i2c->error;
1089 break;
1090 }
1091 }
1092
David Wu7e086c32016-05-16 22:05:03 +08001093 clk_disable(i2c->pclk);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001094 clk_disable(i2c->clk);
David Wu7e086c32016-05-16 22:05:03 +08001095
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001096 spin_unlock_irqrestore(&i2c->lock, flags);
1097
Dmitry Torokhovc6cbfb92015-04-20 15:14:47 -07001098 return ret < 0 ? ret : num;
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001099}
1100
1101static u32 rk3x_i2c_func(struct i2c_adapter *adap)
1102{
1103 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
1104}
1105
1106static const struct i2c_algorithm rk3x_i2c_algorithm = {
1107 .master_xfer = rk3x_i2c_xfer,
1108 .functionality = rk3x_i2c_func,
1109};
1110
David Wubef358c2016-05-16 21:57:39 +08001111static const struct rk3x_i2c_soc_data rk3066_soc_data = {
1112 .grf_offset = 0x154,
David Wu7e086c32016-05-16 22:05:03 +08001113 .calc_timings = rk3x_i2c_v0_calc_timings,
David Wubef358c2016-05-16 21:57:39 +08001114};
1115
1116static const struct rk3x_i2c_soc_data rk3188_soc_data = {
1117 .grf_offset = 0x0a4,
David Wu7e086c32016-05-16 22:05:03 +08001118 .calc_timings = rk3x_i2c_v0_calc_timings,
David Wubef358c2016-05-16 21:57:39 +08001119};
1120
1121static const struct rk3x_i2c_soc_data rk3228_soc_data = {
1122 .grf_offset = -1,
David Wu7e086c32016-05-16 22:05:03 +08001123 .calc_timings = rk3x_i2c_v0_calc_timings,
David Wubef358c2016-05-16 21:57:39 +08001124};
1125
1126static const struct rk3x_i2c_soc_data rk3288_soc_data = {
1127 .grf_offset = -1,
David Wu7e086c32016-05-16 22:05:03 +08001128 .calc_timings = rk3x_i2c_v0_calc_timings,
1129};
1130
1131static const struct rk3x_i2c_soc_data rk3399_soc_data = {
1132 .grf_offset = -1,
1133 .calc_timings = rk3x_i2c_v1_calc_timings,
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001134};
1135
1136static const struct of_device_id rk3x_i2c_match[] = {
David Wubef358c2016-05-16 21:57:39 +08001137 {
1138 .compatible = "rockchip,rk3066-i2c",
1139 .data = (void *)&rk3066_soc_data
1140 },
1141 {
1142 .compatible = "rockchip,rk3188-i2c",
1143 .data = (void *)&rk3188_soc_data
1144 },
1145 {
1146 .compatible = "rockchip,rk3228-i2c",
1147 .data = (void *)&rk3228_soc_data
1148 },
1149 {
1150 .compatible = "rockchip,rk3288-i2c",
1151 .data = (void *)&rk3288_soc_data
1152 },
David Wu7e086c32016-05-16 22:05:03 +08001153 {
1154 .compatible = "rockchip,rk3399-i2c",
1155 .data = (void *)&rk3399_soc_data
1156 },
Dan Carpenterc51bd6a2014-06-12 23:56:09 +02001157 {},
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001158};
Luis de Bethencourt598cf162015-10-20 15:16:29 +01001159MODULE_DEVICE_TABLE(of, rk3x_i2c_match);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001160
1161static int rk3x_i2c_probe(struct platform_device *pdev)
1162{
1163 struct device_node *np = pdev->dev.of_node;
1164 const struct of_device_id *match;
1165 struct rk3x_i2c *i2c;
1166 struct resource *mem;
1167 int ret = 0;
1168 int bus_nr;
1169 u32 value;
1170 int irq;
Max Schwarz249051f2014-11-20 10:26:50 +01001171 unsigned long clk_rate;
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001172
1173 i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
1174 if (!i2c)
1175 return -ENOMEM;
1176
1177 match = of_match_node(rk3x_i2c_match, np);
1178 i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
1179
David Wu1ab92952016-03-17 00:57:17 +08001180 /* use common interface to get I2C timing properties */
1181 i2c_parse_fw_timings(&pdev->dev, &i2c->t, true);
addy ke1330e292014-12-11 19:02:40 +08001182
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001183 strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
1184 i2c->adap.owner = THIS_MODULE;
1185 i2c->adap.algo = &rk3x_i2c_algorithm;
1186 i2c->adap.retries = 3;
1187 i2c->adap.dev.of_node = np;
1188 i2c->adap.algo_data = i2c;
1189 i2c->adap.dev.parent = &pdev->dev;
1190
1191 i2c->dev = &pdev->dev;
1192
1193 spin_lock_init(&i2c->lock);
1194 init_waitqueue_head(&i2c->wait);
1195
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001196 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1197 i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
1198 if (IS_ERR(i2c->regs))
1199 return PTR_ERR(i2c->regs);
1200
1201 /* Try to set the I2C adapter number from dt */
1202 bus_nr = of_alias_get_id(np, "i2c");
1203
1204 /*
1205 * Switch to new interface if the SoC also offers the old one.
1206 * The control bit is located in the GRF register space.
1207 */
1208 if (i2c->soc_data->grf_offset >= 0) {
1209 struct regmap *grf;
1210
1211 grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
1212 if (IS_ERR(grf)) {
1213 dev_err(&pdev->dev,
1214 "rk3x-i2c needs 'rockchip,grf' property\n");
1215 return PTR_ERR(grf);
1216 }
1217
1218 if (bus_nr < 0) {
1219 dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
1220 return -EINVAL;
1221 }
1222
1223 /* 27+i: write mask, 11+i: value */
1224 value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
1225
1226 ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
1227 if (ret != 0) {
1228 dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
1229 return ret;
1230 }
1231 }
1232
1233 /* IRQ setup */
1234 irq = platform_get_irq(pdev, 0);
1235 if (irq < 0) {
1236 dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
1237 return irq;
1238 }
1239
1240 ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
1241 0, dev_name(&pdev->dev), i2c);
1242 if (ret < 0) {
1243 dev_err(&pdev->dev, "cannot request IRQ\n");
1244 return ret;
1245 }
1246
1247 platform_set_drvdata(pdev, i2c);
1248
David Wu7e086c32016-05-16 22:05:03 +08001249 if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) {
1250 /* Only one clock to use for bus clock and peripheral clock */
1251 i2c->clk = devm_clk_get(&pdev->dev, NULL);
1252 i2c->pclk = i2c->clk;
1253 } else {
1254 i2c->clk = devm_clk_get(&pdev->dev, "i2c");
1255 i2c->pclk = devm_clk_get(&pdev->dev, "pclk");
1256 }
1257
1258 if (IS_ERR(i2c->clk)) {
1259 ret = PTR_ERR(i2c->clk);
1260 if (ret != -EPROBE_DEFER)
1261 dev_err(&pdev->dev, "Can't get bus clk: %d\n", ret);
1262 return ret;
1263 }
1264 if (IS_ERR(i2c->pclk)) {
1265 ret = PTR_ERR(i2c->pclk);
1266 if (ret != -EPROBE_DEFER)
1267 dev_err(&pdev->dev, "Can't get periph clk: %d\n", ret);
1268 return ret;
1269 }
1270
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001271 ret = clk_prepare(i2c->clk);
1272 if (ret < 0) {
David Wu7e086c32016-05-16 22:05:03 +08001273 dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001274 return ret;
1275 }
David Wu7e086c32016-05-16 22:05:03 +08001276 ret = clk_prepare(i2c->pclk);
1277 if (ret < 0) {
1278 dev_err(&pdev->dev, "Can't prepare periph clock: %d\n", ret);
1279 goto err_clk;
1280 }
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001281
Max Schwarz249051f2014-11-20 10:26:50 +01001282 i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
1283 ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
1284 if (ret != 0) {
1285 dev_err(&pdev->dev, "Unable to register clock notifier\n");
David Wu7e086c32016-05-16 22:05:03 +08001286 goto err_pclk;
Max Schwarz249051f2014-11-20 10:26:50 +01001287 }
1288
1289 clk_rate = clk_get_rate(i2c->clk);
1290 rk3x_i2c_adapt_div(i2c, clk_rate);
1291
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001292 ret = i2c_add_adapter(&i2c->adap);
1293 if (ret < 0) {
1294 dev_err(&pdev->dev, "Could not register adapter\n");
Max Schwarz249051f2014-11-20 10:26:50 +01001295 goto err_clk_notifier;
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001296 }
1297
1298 dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
1299
1300 return 0;
1301
Max Schwarz249051f2014-11-20 10:26:50 +01001302err_clk_notifier:
1303 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
David Wu7e086c32016-05-16 22:05:03 +08001304err_pclk:
1305 clk_unprepare(i2c->pclk);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001306err_clk:
1307 clk_unprepare(i2c->clk);
1308 return ret;
1309}
1310
1311static int rk3x_i2c_remove(struct platform_device *pdev)
1312{
1313 struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
1314
1315 i2c_del_adapter(&i2c->adap);
Max Schwarz249051f2014-11-20 10:26:50 +01001316
1317 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
David Wu7e086c32016-05-16 22:05:03 +08001318 clk_unprepare(i2c->pclk);
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001319 clk_unprepare(i2c->clk);
1320
1321 return 0;
1322}
1323
1324static struct platform_driver rk3x_i2c_driver = {
1325 .probe = rk3x_i2c_probe,
1326 .remove = rk3x_i2c_remove,
1327 .driver = {
Max Schwarzc41aa3c2014-06-11 22:34:37 +02001328 .name = "rk3x-i2c",
1329 .of_match_table = rk3x_i2c_match,
1330 },
1331};
1332
1333module_platform_driver(rk3x_i2c_driver);
1334
1335MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
1336MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
1337MODULE_LICENSE("GPL v2");