xref: /openbmc/linux/drivers/i2c/busses/i2c-rk3x.c (revision 72cf8c565cf4e25312fb9fa65d306ae736104ade)
1c41aa3ceSMax Schwarz /*
2c41aa3ceSMax Schwarz  * Driver for I2C adapter in Rockchip RK3xxx SoC
3c41aa3ceSMax Schwarz  *
4c41aa3ceSMax Schwarz  * Max Schwarz <max.schwarz@online.de>
5c41aa3ceSMax Schwarz  * based on the patches by Rockchip Inc.
6c41aa3ceSMax Schwarz  *
7c41aa3ceSMax Schwarz  * This program is free software; you can redistribute it and/or modify
8c41aa3ceSMax Schwarz  * it under the terms of the GNU General Public License version 2 as
9c41aa3ceSMax Schwarz  * published by the Free Software Foundation.
10c41aa3ceSMax Schwarz  */
11c41aa3ceSMax Schwarz 
12c41aa3ceSMax Schwarz #include <linux/kernel.h>
13c41aa3ceSMax Schwarz #include <linux/module.h>
14c41aa3ceSMax Schwarz #include <linux/i2c.h>
15c41aa3ceSMax Schwarz #include <linux/interrupt.h>
16c41aa3ceSMax Schwarz #include <linux/errno.h>
17c41aa3ceSMax Schwarz #include <linux/err.h>
18c41aa3ceSMax Schwarz #include <linux/platform_device.h>
19c41aa3ceSMax Schwarz #include <linux/io.h>
20c41aa3ceSMax Schwarz #include <linux/of_address.h>
21c41aa3ceSMax Schwarz #include <linux/of_irq.h>
22c41aa3ceSMax Schwarz #include <linux/spinlock.h>
23c41aa3ceSMax Schwarz #include <linux/clk.h>
24c41aa3ceSMax Schwarz #include <linux/wait.h>
25c41aa3ceSMax Schwarz #include <linux/mfd/syscon.h>
26c41aa3ceSMax Schwarz #include <linux/regmap.h>
270285f8f5Saddy ke #include <linux/math64.h>
28c41aa3ceSMax Schwarz 
29c41aa3ceSMax Schwarz 
30c41aa3ceSMax Schwarz /* Register Map */
31c41aa3ceSMax Schwarz #define REG_CON        0x00 /* control register */
32c41aa3ceSMax Schwarz #define REG_CLKDIV     0x04 /* clock divisor register */
33c41aa3ceSMax Schwarz #define REG_MRXADDR    0x08 /* slave address for REGISTER_TX */
34c41aa3ceSMax Schwarz #define REG_MRXRADDR   0x0c /* slave register address for REGISTER_TX */
35c41aa3ceSMax Schwarz #define REG_MTXCNT     0x10 /* number of bytes to be transmitted */
36c41aa3ceSMax Schwarz #define REG_MRXCNT     0x14 /* number of bytes to be received */
37c41aa3ceSMax Schwarz #define REG_IEN        0x18 /* interrupt enable */
38c41aa3ceSMax Schwarz #define REG_IPD        0x1c /* interrupt pending */
39c41aa3ceSMax Schwarz #define REG_FCNT       0x20 /* finished count */
40c41aa3ceSMax Schwarz 
41c41aa3ceSMax Schwarz /* Data buffer offsets */
42c41aa3ceSMax Schwarz #define TXBUFFER_BASE 0x100
43c41aa3ceSMax Schwarz #define RXBUFFER_BASE 0x200
44c41aa3ceSMax Schwarz 
45c41aa3ceSMax Schwarz /* REG_CON bits */
46c41aa3ceSMax Schwarz #define REG_CON_EN        BIT(0)
47c41aa3ceSMax Schwarz enum {
48c41aa3ceSMax Schwarz 	REG_CON_MOD_TX = 0,      /* transmit data */
49c41aa3ceSMax Schwarz 	REG_CON_MOD_REGISTER_TX, /* select register and restart */
50c41aa3ceSMax Schwarz 	REG_CON_MOD_RX,          /* receive data */
51c41aa3ceSMax Schwarz 	REG_CON_MOD_REGISTER_RX, /* broken: transmits read addr AND writes
52c41aa3ceSMax Schwarz 				  * register addr */
53c41aa3ceSMax Schwarz };
54c41aa3ceSMax Schwarz #define REG_CON_MOD(mod)  ((mod) << 1)
55c41aa3ceSMax Schwarz #define REG_CON_MOD_MASK  (BIT(1) | BIT(2))
56c41aa3ceSMax Schwarz #define REG_CON_START     BIT(3)
57c41aa3ceSMax Schwarz #define REG_CON_STOP      BIT(4)
58c41aa3ceSMax Schwarz #define REG_CON_LASTACK   BIT(5) /* 1: send NACK after last received byte */
59c41aa3ceSMax Schwarz #define REG_CON_ACTACK    BIT(6) /* 1: stop if NACK is received */
60c41aa3ceSMax Schwarz 
61a8a7d09eSDavid Wu #define REG_CON_TUNING_MASK GENMASK_ULL(15, 8)
627e086c3fSDavid Wu 
637e086c3fSDavid Wu #define REG_CON_SDA_CFG(cfg) ((cfg) << 8)
647e086c3fSDavid Wu #define REG_CON_STA_CFG(cfg) ((cfg) << 12)
657e086c3fSDavid Wu #define REG_CON_STO_CFG(cfg) ((cfg) << 14)
667e086c3fSDavid Wu 
67c41aa3ceSMax Schwarz /* REG_MRXADDR bits */
68c41aa3ceSMax Schwarz #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
69c41aa3ceSMax Schwarz 
70c41aa3ceSMax Schwarz /* REG_IEN/REG_IPD bits */
71c41aa3ceSMax Schwarz #define REG_INT_BTF       BIT(0) /* a byte was transmitted */
72c41aa3ceSMax Schwarz #define REG_INT_BRF       BIT(1) /* a byte was received */
73c41aa3ceSMax Schwarz #define REG_INT_MBTF      BIT(2) /* master data transmit finished */
74c41aa3ceSMax Schwarz #define REG_INT_MBRF      BIT(3) /* master data receive finished */
75c41aa3ceSMax Schwarz #define REG_INT_START     BIT(4) /* START condition generated */
76c41aa3ceSMax Schwarz #define REG_INT_STOP      BIT(5) /* STOP condition generated */
77c41aa3ceSMax Schwarz #define REG_INT_NAKRCV    BIT(6) /* NACK received */
78c41aa3ceSMax Schwarz #define REG_INT_ALL       0x7f
79c41aa3ceSMax Schwarz 
80c41aa3ceSMax Schwarz /* Constants */
814489750fSDoug Anderson #define WAIT_TIMEOUT      1000 /* ms */
82c41aa3ceSMax Schwarz #define DEFAULT_SCL_RATE  (100 * 1000) /* Hz */
83c41aa3ceSMax Schwarz 
84e26747bfSDavid Wu /**
85b58fd3beSDavid Wu  * struct i2c_spec_values:
867e086c3fSDavid Wu  * @min_hold_start_ns: min hold time (repeated) START condition
87b58fd3beSDavid Wu  * @min_low_ns: min LOW period of the SCL clock
88b58fd3beSDavid Wu  * @min_high_ns: min HIGH period of the SCL cloc
89b58fd3beSDavid Wu  * @min_setup_start_ns: min set-up time for a repeated START conditio
90b58fd3beSDavid Wu  * @max_data_hold_ns: max data hold time
917e086c3fSDavid Wu  * @min_data_setup_ns: min data set-up time
927e086c3fSDavid Wu  * @min_setup_stop_ns: min set-up time for STOP condition
937e086c3fSDavid Wu  * @min_hold_buffer_ns: min bus free time between a STOP and
947e086c3fSDavid Wu  * START condition
95b58fd3beSDavid Wu  */
96b58fd3beSDavid Wu struct i2c_spec_values {
977e086c3fSDavid Wu 	unsigned long min_hold_start_ns;
98b58fd3beSDavid Wu 	unsigned long min_low_ns;
99b58fd3beSDavid Wu 	unsigned long min_high_ns;
100b58fd3beSDavid Wu 	unsigned long min_setup_start_ns;
101b58fd3beSDavid Wu 	unsigned long max_data_hold_ns;
1027e086c3fSDavid Wu 	unsigned long min_data_setup_ns;
1037e086c3fSDavid Wu 	unsigned long min_setup_stop_ns;
1047e086c3fSDavid Wu 	unsigned long min_hold_buffer_ns;
105b58fd3beSDavid Wu };
106b58fd3beSDavid Wu 
107b58fd3beSDavid Wu static const struct i2c_spec_values standard_mode_spec = {
1087e086c3fSDavid Wu 	.min_hold_start_ns = 4000,
109b58fd3beSDavid Wu 	.min_low_ns = 4700,
110b58fd3beSDavid Wu 	.min_high_ns = 4000,
111b58fd3beSDavid Wu 	.min_setup_start_ns = 4700,
112b58fd3beSDavid Wu 	.max_data_hold_ns = 3450,
1137e086c3fSDavid Wu 	.min_data_setup_ns = 250,
1147e086c3fSDavid Wu 	.min_setup_stop_ns = 4000,
1157e086c3fSDavid Wu 	.min_hold_buffer_ns = 4700,
116b58fd3beSDavid Wu };
117b58fd3beSDavid Wu 
118b58fd3beSDavid Wu static const struct i2c_spec_values fast_mode_spec = {
1197e086c3fSDavid Wu 	.min_hold_start_ns = 600,
120b58fd3beSDavid Wu 	.min_low_ns = 1300,
121b58fd3beSDavid Wu 	.min_high_ns = 600,
122b58fd3beSDavid Wu 	.min_setup_start_ns = 600,
123b58fd3beSDavid Wu 	.max_data_hold_ns = 900,
1247e086c3fSDavid Wu 	.min_data_setup_ns = 100,
1257e086c3fSDavid Wu 	.min_setup_stop_ns = 600,
1267e086c3fSDavid Wu 	.min_hold_buffer_ns = 1300,
127b58fd3beSDavid Wu };
128b58fd3beSDavid Wu 
129a02f3d08SDavid Wu static const struct i2c_spec_values fast_mode_plus_spec = {
130a02f3d08SDavid Wu 	.min_hold_start_ns = 260,
131a02f3d08SDavid Wu 	.min_low_ns = 500,
132a02f3d08SDavid Wu 	.min_high_ns = 260,
133a02f3d08SDavid Wu 	.min_setup_start_ns = 260,
134a02f3d08SDavid Wu 	.max_data_hold_ns = 400,
135a02f3d08SDavid Wu 	.min_data_setup_ns = 50,
136a02f3d08SDavid Wu 	.min_setup_stop_ns = 260,
137a02f3d08SDavid Wu 	.min_hold_buffer_ns = 500,
138a02f3d08SDavid Wu };
139a02f3d08SDavid Wu 
140b58fd3beSDavid Wu /**
141e26747bfSDavid Wu  * struct rk3x_i2c_calced_timings:
142e26747bfSDavid Wu  * @div_low: Divider output for low
143e26747bfSDavid Wu  * @div_high: Divider output for high
1447e086c3fSDavid Wu  * @tuning: Used to adjust setup/hold data time,
1457e086c3fSDavid Wu  * setup/hold start time and setup stop time for
1467e086c3fSDavid Wu  * v1's calc_timings, the tuning should all be 0
1477e086c3fSDavid Wu  * for old hardware anyone using v0's calc_timings.
148e26747bfSDavid Wu  */
149e26747bfSDavid Wu struct rk3x_i2c_calced_timings {
150e26747bfSDavid Wu 	unsigned long div_low;
151e26747bfSDavid Wu 	unsigned long div_high;
1527e086c3fSDavid Wu 	unsigned int tuning;
153e26747bfSDavid Wu };
154e26747bfSDavid Wu 
155c41aa3ceSMax Schwarz enum rk3x_i2c_state {
156c41aa3ceSMax Schwarz 	STATE_IDLE,
157c41aa3ceSMax Schwarz 	STATE_START,
158c41aa3ceSMax Schwarz 	STATE_READ,
159c41aa3ceSMax Schwarz 	STATE_WRITE,
160c41aa3ceSMax Schwarz 	STATE_STOP
161c41aa3ceSMax Schwarz };
162c41aa3ceSMax Schwarz 
163c41aa3ceSMax Schwarz /**
164c41aa3ceSMax Schwarz  * @grf_offset: offset inside the grf regmap for setting the i2c type
1657e086c3fSDavid Wu  * @calc_timings: Callback function for i2c timing information calculated
166c41aa3ceSMax Schwarz  */
167c41aa3ceSMax Schwarz struct rk3x_i2c_soc_data {
168c41aa3ceSMax Schwarz 	int grf_offset;
1697e086c3fSDavid Wu 	int (*calc_timings)(unsigned long, struct i2c_timings *,
1707e086c3fSDavid Wu 			    struct rk3x_i2c_calced_timings *);
171c41aa3ceSMax Schwarz };
172c41aa3ceSMax Schwarz 
1730a6ad2f9SDavid Wu /**
1740a6ad2f9SDavid Wu  * struct rk3x_i2c - private data of the controller
1750a6ad2f9SDavid Wu  * @adap: corresponding I2C adapter
1760a6ad2f9SDavid Wu  * @dev: device for this controller
1770a6ad2f9SDavid Wu  * @soc_data: related soc data struct
1780a6ad2f9SDavid Wu  * @regs: virtual memory area
1797e086c3fSDavid Wu  * @clk: function clk for rk3399 or function & Bus clks for others
1807e086c3fSDavid Wu  * @pclk: Bus clk for rk3399
1810a6ad2f9SDavid Wu  * @clk_rate_nb: i2c clk rate change notify
1820a6ad2f9SDavid Wu  * @t: I2C known timing information
1830a6ad2f9SDavid Wu  * @lock: spinlock for the i2c bus
1840a6ad2f9SDavid Wu  * @wait: the waitqueue to wait for i2c transfer
1850a6ad2f9SDavid Wu  * @busy: the condition for the event to wait for
1860a6ad2f9SDavid Wu  * @msg: current i2c message
1870a6ad2f9SDavid Wu  * @addr: addr of i2c slave device
1880a6ad2f9SDavid Wu  * @mode: mode of i2c transfer
1890a6ad2f9SDavid Wu  * @is_last_msg: flag determines whether it is the last msg in this transfer
1900a6ad2f9SDavid Wu  * @state: state of i2c transfer
1910a6ad2f9SDavid Wu  * @processed: byte length which has been send or received
1920a6ad2f9SDavid Wu  * @error: error code for i2c transfer
1930a6ad2f9SDavid Wu  */
194c41aa3ceSMax Schwarz struct rk3x_i2c {
195c41aa3ceSMax Schwarz 	struct i2c_adapter adap;
196c41aa3ceSMax Schwarz 	struct device *dev;
197c41aa3ceSMax Schwarz 	struct rk3x_i2c_soc_data *soc_data;
198c41aa3ceSMax Schwarz 
199c41aa3ceSMax Schwarz 	/* Hardware resources */
200c41aa3ceSMax Schwarz 	void __iomem *regs;
201c41aa3ceSMax Schwarz 	struct clk *clk;
2027e086c3fSDavid Wu 	struct clk *pclk;
203249051f4SMax Schwarz 	struct notifier_block clk_rate_nb;
204c41aa3ceSMax Schwarz 
205c41aa3ceSMax Schwarz 	/* Settings */
2061ab92956SDavid Wu 	struct i2c_timings t;
207c41aa3ceSMax Schwarz 
208c41aa3ceSMax Schwarz 	/* Synchronization & notification */
209c41aa3ceSMax Schwarz 	spinlock_t lock;
210c41aa3ceSMax Schwarz 	wait_queue_head_t wait;
211c41aa3ceSMax Schwarz 	bool busy;
212c41aa3ceSMax Schwarz 
213c41aa3ceSMax Schwarz 	/* Current message */
214c41aa3ceSMax Schwarz 	struct i2c_msg *msg;
215c41aa3ceSMax Schwarz 	u8 addr;
216c41aa3ceSMax Schwarz 	unsigned int mode;
217c41aa3ceSMax Schwarz 	bool is_last_msg;
218c41aa3ceSMax Schwarz 
219c41aa3ceSMax Schwarz 	/* I2C state machine */
220c41aa3ceSMax Schwarz 	enum rk3x_i2c_state state;
2210a6ad2f9SDavid Wu 	unsigned int processed;
222c41aa3ceSMax Schwarz 	int error;
223c41aa3ceSMax Schwarz };
224c41aa3ceSMax Schwarz 
225c41aa3ceSMax Schwarz static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
226c41aa3ceSMax Schwarz 			      unsigned int offset)
227c41aa3ceSMax Schwarz {
228c41aa3ceSMax Schwarz 	writel(value, i2c->regs + offset);
229c41aa3ceSMax Schwarz }
230c41aa3ceSMax Schwarz 
231c41aa3ceSMax Schwarz static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
232c41aa3ceSMax Schwarz {
233c41aa3ceSMax Schwarz 	return readl(i2c->regs + offset);
234c41aa3ceSMax Schwarz }
235c41aa3ceSMax Schwarz 
236c41aa3ceSMax Schwarz /* Reset all interrupt pending bits */
237c41aa3ceSMax Schwarz static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
238c41aa3ceSMax Schwarz {
239c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_ALL, REG_IPD);
240c41aa3ceSMax Schwarz }
241c41aa3ceSMax Schwarz 
242c41aa3ceSMax Schwarz /**
243c41aa3ceSMax Schwarz  * Generate a START condition, which triggers a REG_INT_START interrupt.
244c41aa3ceSMax Schwarz  */
245c41aa3ceSMax Schwarz static void rk3x_i2c_start(struct rk3x_i2c *i2c)
246c41aa3ceSMax Schwarz {
2477e086c3fSDavid Wu 	u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
248c41aa3ceSMax Schwarz 
249c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_START, REG_IEN);
250c41aa3ceSMax Schwarz 
251c41aa3ceSMax Schwarz 	/* enable adapter with correct mode, send START condition */
2527e086c3fSDavid Wu 	val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
253c41aa3ceSMax Schwarz 
254c41aa3ceSMax Schwarz 	/* if we want to react to NACK, set ACTACK bit */
255c41aa3ceSMax Schwarz 	if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
256c41aa3ceSMax Schwarz 		val |= REG_CON_ACTACK;
257c41aa3ceSMax Schwarz 
258c41aa3ceSMax Schwarz 	i2c_writel(i2c, val, REG_CON);
259c41aa3ceSMax Schwarz }
260c41aa3ceSMax Schwarz 
261c41aa3ceSMax Schwarz /**
262c41aa3ceSMax Schwarz  * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
263c41aa3ceSMax Schwarz  *
264c41aa3ceSMax Schwarz  * @error: Error code to return in rk3x_i2c_xfer
265c41aa3ceSMax Schwarz  */
266c41aa3ceSMax Schwarz static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
267c41aa3ceSMax Schwarz {
268c41aa3ceSMax Schwarz 	unsigned int ctrl;
269c41aa3ceSMax Schwarz 
270c41aa3ceSMax Schwarz 	i2c->processed = 0;
271c41aa3ceSMax Schwarz 	i2c->msg = NULL;
272c41aa3ceSMax Schwarz 	i2c->error = error;
273c41aa3ceSMax Schwarz 
274c41aa3ceSMax Schwarz 	if (i2c->is_last_msg) {
275c41aa3ceSMax Schwarz 		/* Enable stop interrupt */
276c41aa3ceSMax Schwarz 		i2c_writel(i2c, REG_INT_STOP, REG_IEN);
277c41aa3ceSMax Schwarz 
278c41aa3ceSMax Schwarz 		i2c->state = STATE_STOP;
279c41aa3ceSMax Schwarz 
280c41aa3ceSMax Schwarz 		ctrl = i2c_readl(i2c, REG_CON);
281c41aa3ceSMax Schwarz 		ctrl |= REG_CON_STOP;
282c41aa3ceSMax Schwarz 		i2c_writel(i2c, ctrl, REG_CON);
283c41aa3ceSMax Schwarz 	} else {
284c41aa3ceSMax Schwarz 		/* Signal rk3x_i2c_xfer to start the next message. */
285c41aa3ceSMax Schwarz 		i2c->busy = false;
286c41aa3ceSMax Schwarz 		i2c->state = STATE_IDLE;
287c41aa3ceSMax Schwarz 
288c41aa3ceSMax Schwarz 		/*
289c41aa3ceSMax Schwarz 		 * The HW is actually not capable of REPEATED START. But we can
290c41aa3ceSMax Schwarz 		 * get the intended effect by resetting its internal state
291c41aa3ceSMax Schwarz 		 * and issuing an ordinary START.
292c41aa3ceSMax Schwarz 		 */
2937e086c3fSDavid Wu 		ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
2947e086c3fSDavid Wu 		i2c_writel(i2c, ctrl, REG_CON);
295c41aa3ceSMax Schwarz 
296c41aa3ceSMax Schwarz 		/* signal that we are finished with the current msg */
297c41aa3ceSMax Schwarz 		wake_up(&i2c->wait);
298c41aa3ceSMax Schwarz 	}
299c41aa3ceSMax Schwarz }
300c41aa3ceSMax Schwarz 
301c41aa3ceSMax Schwarz /**
302c41aa3ceSMax Schwarz  * Setup a read according to i2c->msg
303c41aa3ceSMax Schwarz  */
304c41aa3ceSMax Schwarz static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
305c41aa3ceSMax Schwarz {
306c41aa3ceSMax Schwarz 	unsigned int len = i2c->msg->len - i2c->processed;
307c41aa3ceSMax Schwarz 	u32 con;
308c41aa3ceSMax Schwarz 
309c41aa3ceSMax Schwarz 	con = i2c_readl(i2c, REG_CON);
310c41aa3ceSMax Schwarz 
311c41aa3ceSMax Schwarz 	/*
312c41aa3ceSMax Schwarz 	 * The hw can read up to 32 bytes at a time. If we need more than one
313c41aa3ceSMax Schwarz 	 * chunk, send an ACK after the last byte of the current chunk.
314c41aa3ceSMax Schwarz 	 */
31529209338SDoug Anderson 	if (len > 32) {
316c41aa3ceSMax Schwarz 		len = 32;
317c41aa3ceSMax Schwarz 		con &= ~REG_CON_LASTACK;
318c41aa3ceSMax Schwarz 	} else {
319c41aa3ceSMax Schwarz 		con |= REG_CON_LASTACK;
320c41aa3ceSMax Schwarz 	}
321c41aa3ceSMax Schwarz 
322c41aa3ceSMax Schwarz 	/* make sure we are in plain RX mode if we read a second chunk */
323c41aa3ceSMax Schwarz 	if (i2c->processed != 0) {
324c41aa3ceSMax Schwarz 		con &= ~REG_CON_MOD_MASK;
325c41aa3ceSMax Schwarz 		con |= REG_CON_MOD(REG_CON_MOD_RX);
326c41aa3ceSMax Schwarz 	}
327c41aa3ceSMax Schwarz 
328c41aa3ceSMax Schwarz 	i2c_writel(i2c, con, REG_CON);
329c41aa3ceSMax Schwarz 	i2c_writel(i2c, len, REG_MRXCNT);
330c41aa3ceSMax Schwarz }
331c41aa3ceSMax Schwarz 
332c41aa3ceSMax Schwarz /**
333c41aa3ceSMax Schwarz  * Fill the transmit buffer with data from i2c->msg
334c41aa3ceSMax Schwarz  */
335c41aa3ceSMax Schwarz static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
336c41aa3ceSMax Schwarz {
337c41aa3ceSMax Schwarz 	unsigned int i, j;
338c41aa3ceSMax Schwarz 	u32 cnt = 0;
339c41aa3ceSMax Schwarz 	u32 val;
340c41aa3ceSMax Schwarz 	u8 byte;
341c41aa3ceSMax Schwarz 
342c41aa3ceSMax Schwarz 	for (i = 0; i < 8; ++i) {
343c41aa3ceSMax Schwarz 		val = 0;
344c41aa3ceSMax Schwarz 		for (j = 0; j < 4; ++j) {
345cf27020dSAlexandru M Stan 			if ((i2c->processed == i2c->msg->len) && (cnt != 0))
346c41aa3ceSMax Schwarz 				break;
347c41aa3ceSMax Schwarz 
348c41aa3ceSMax Schwarz 			if (i2c->processed == 0 && cnt == 0)
349c41aa3ceSMax Schwarz 				byte = (i2c->addr & 0x7f) << 1;
350c41aa3ceSMax Schwarz 			else
351c41aa3ceSMax Schwarz 				byte = i2c->msg->buf[i2c->processed++];
352c41aa3ceSMax Schwarz 
353c41aa3ceSMax Schwarz 			val |= byte << (j * 8);
354c41aa3ceSMax Schwarz 			cnt++;
355c41aa3ceSMax Schwarz 		}
356c41aa3ceSMax Schwarz 
357c41aa3ceSMax Schwarz 		i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
358c41aa3ceSMax Schwarz 
359c41aa3ceSMax Schwarz 		if (i2c->processed == i2c->msg->len)
360c41aa3ceSMax Schwarz 			break;
361c41aa3ceSMax Schwarz 	}
362c41aa3ceSMax Schwarz 
363c41aa3ceSMax Schwarz 	i2c_writel(i2c, cnt, REG_MTXCNT);
364c41aa3ceSMax Schwarz }
365c41aa3ceSMax Schwarz 
366c41aa3ceSMax Schwarz 
367c41aa3ceSMax Schwarz /* IRQ handlers for individual states */
368c41aa3ceSMax Schwarz 
369c41aa3ceSMax Schwarz static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
370c41aa3ceSMax Schwarz {
371c41aa3ceSMax Schwarz 	if (!(ipd & REG_INT_START)) {
372c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, -EIO);
373c41aa3ceSMax Schwarz 		dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
374c41aa3ceSMax Schwarz 		rk3x_i2c_clean_ipd(i2c);
375c41aa3ceSMax Schwarz 		return;
376c41aa3ceSMax Schwarz 	}
377c41aa3ceSMax Schwarz 
378c41aa3ceSMax Schwarz 	/* ack interrupt */
379c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_START, REG_IPD);
380c41aa3ceSMax Schwarz 
381c41aa3ceSMax Schwarz 	/* disable start bit */
382c41aa3ceSMax Schwarz 	i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
383c41aa3ceSMax Schwarz 
384c41aa3ceSMax Schwarz 	/* enable appropriate interrupts and transition */
385c41aa3ceSMax Schwarz 	if (i2c->mode == REG_CON_MOD_TX) {
386c41aa3ceSMax Schwarz 		i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
387c41aa3ceSMax Schwarz 		i2c->state = STATE_WRITE;
388c41aa3ceSMax Schwarz 		rk3x_i2c_fill_transmit_buf(i2c);
389c41aa3ceSMax Schwarz 	} else {
390c41aa3ceSMax Schwarz 		/* in any other case, we are going to be reading. */
391c41aa3ceSMax Schwarz 		i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
392c41aa3ceSMax Schwarz 		i2c->state = STATE_READ;
393c41aa3ceSMax Schwarz 		rk3x_i2c_prepare_read(i2c);
394c41aa3ceSMax Schwarz 	}
395c41aa3ceSMax Schwarz }
396c41aa3ceSMax Schwarz 
397c41aa3ceSMax Schwarz static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
398c41aa3ceSMax Schwarz {
399c41aa3ceSMax Schwarz 	if (!(ipd & REG_INT_MBTF)) {
400c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, -EIO);
401c41aa3ceSMax Schwarz 		dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
402c41aa3ceSMax Schwarz 		rk3x_i2c_clean_ipd(i2c);
403c41aa3ceSMax Schwarz 		return;
404c41aa3ceSMax Schwarz 	}
405c41aa3ceSMax Schwarz 
406c41aa3ceSMax Schwarz 	/* ack interrupt */
407c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
408c41aa3ceSMax Schwarz 
409c41aa3ceSMax Schwarz 	/* are we finished? */
410c41aa3ceSMax Schwarz 	if (i2c->processed == i2c->msg->len)
411c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, i2c->error);
412c41aa3ceSMax Schwarz 	else
413c41aa3ceSMax Schwarz 		rk3x_i2c_fill_transmit_buf(i2c);
414c41aa3ceSMax Schwarz }
415c41aa3ceSMax Schwarz 
416c41aa3ceSMax Schwarz static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
417c41aa3ceSMax Schwarz {
418c41aa3ceSMax Schwarz 	unsigned int i;
419c41aa3ceSMax Schwarz 	unsigned int len = i2c->msg->len - i2c->processed;
420c41aa3ceSMax Schwarz 	u32 uninitialized_var(val);
421c41aa3ceSMax Schwarz 	u8 byte;
422c41aa3ceSMax Schwarz 
423c41aa3ceSMax Schwarz 	/* we only care for MBRF here. */
424c41aa3ceSMax Schwarz 	if (!(ipd & REG_INT_MBRF))
425c41aa3ceSMax Schwarz 		return;
426c41aa3ceSMax Schwarz 
427c41aa3ceSMax Schwarz 	/* ack interrupt */
428c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
429c41aa3ceSMax Schwarz 
4305da4309fSaddy ke 	/* Can only handle a maximum of 32 bytes at a time */
4315da4309fSaddy ke 	if (len > 32)
4325da4309fSaddy ke 		len = 32;
4335da4309fSaddy ke 
434c41aa3ceSMax Schwarz 	/* read the data from receive buffer */
435c41aa3ceSMax Schwarz 	for (i = 0; i < len; ++i) {
436c41aa3ceSMax Schwarz 		if (i % 4 == 0)
437c41aa3ceSMax Schwarz 			val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
438c41aa3ceSMax Schwarz 
439c41aa3ceSMax Schwarz 		byte = (val >> ((i % 4) * 8)) & 0xff;
440c41aa3ceSMax Schwarz 		i2c->msg->buf[i2c->processed++] = byte;
441c41aa3ceSMax Schwarz 	}
442c41aa3ceSMax Schwarz 
443c41aa3ceSMax Schwarz 	/* are we finished? */
444c41aa3ceSMax Schwarz 	if (i2c->processed == i2c->msg->len)
445c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, i2c->error);
446c41aa3ceSMax Schwarz 	else
447c41aa3ceSMax Schwarz 		rk3x_i2c_prepare_read(i2c);
448c41aa3ceSMax Schwarz }
449c41aa3ceSMax Schwarz 
450c41aa3ceSMax Schwarz static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
451c41aa3ceSMax Schwarz {
452c41aa3ceSMax Schwarz 	unsigned int con;
453c41aa3ceSMax Schwarz 
454c41aa3ceSMax Schwarz 	if (!(ipd & REG_INT_STOP)) {
455c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, -EIO);
456c41aa3ceSMax Schwarz 		dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
457c41aa3ceSMax Schwarz 		rk3x_i2c_clean_ipd(i2c);
458c41aa3ceSMax Schwarz 		return;
459c41aa3ceSMax Schwarz 	}
460c41aa3ceSMax Schwarz 
461c41aa3ceSMax Schwarz 	/* ack interrupt */
462c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_STOP, REG_IPD);
463c41aa3ceSMax Schwarz 
464c41aa3ceSMax Schwarz 	/* disable STOP bit */
465c41aa3ceSMax Schwarz 	con = i2c_readl(i2c, REG_CON);
466c41aa3ceSMax Schwarz 	con &= ~REG_CON_STOP;
467c41aa3ceSMax Schwarz 	i2c_writel(i2c, con, REG_CON);
468c41aa3ceSMax Schwarz 
469c41aa3ceSMax Schwarz 	i2c->busy = false;
470c41aa3ceSMax Schwarz 	i2c->state = STATE_IDLE;
471c41aa3ceSMax Schwarz 
472c41aa3ceSMax Schwarz 	/* signal rk3x_i2c_xfer that we are finished */
473c41aa3ceSMax Schwarz 	wake_up(&i2c->wait);
474c41aa3ceSMax Schwarz }
475c41aa3ceSMax Schwarz 
476c41aa3ceSMax Schwarz static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
477c41aa3ceSMax Schwarz {
478c41aa3ceSMax Schwarz 	struct rk3x_i2c *i2c = dev_id;
479c41aa3ceSMax Schwarz 	unsigned int ipd;
480c41aa3ceSMax Schwarz 
481c41aa3ceSMax Schwarz 	spin_lock(&i2c->lock);
482c41aa3ceSMax Schwarz 
483c41aa3ceSMax Schwarz 	ipd = i2c_readl(i2c, REG_IPD);
484c41aa3ceSMax Schwarz 	if (i2c->state == STATE_IDLE) {
485c41aa3ceSMax Schwarz 		dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
486c41aa3ceSMax Schwarz 		rk3x_i2c_clean_ipd(i2c);
487c41aa3ceSMax Schwarz 		goto out;
488c41aa3ceSMax Schwarz 	}
489c41aa3ceSMax Schwarz 
490c41aa3ceSMax Schwarz 	dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
491c41aa3ceSMax Schwarz 
492c41aa3ceSMax Schwarz 	/* Clean interrupt bits we don't care about */
493c41aa3ceSMax Schwarz 	ipd &= ~(REG_INT_BRF | REG_INT_BTF);
494c41aa3ceSMax Schwarz 
495c41aa3ceSMax Schwarz 	if (ipd & REG_INT_NAKRCV) {
496c41aa3ceSMax Schwarz 		/*
497c41aa3ceSMax Schwarz 		 * We got a NACK in the last operation. Depending on whether
498c41aa3ceSMax Schwarz 		 * IGNORE_NAK is set, we have to stop the operation and report
499c41aa3ceSMax Schwarz 		 * an error.
500c41aa3ceSMax Schwarz 		 */
501c41aa3ceSMax Schwarz 		i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
502c41aa3ceSMax Schwarz 
503c41aa3ceSMax Schwarz 		ipd &= ~REG_INT_NAKRCV;
504c41aa3ceSMax Schwarz 
505c41aa3ceSMax Schwarz 		if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
506c41aa3ceSMax Schwarz 			rk3x_i2c_stop(i2c, -ENXIO);
507c41aa3ceSMax Schwarz 	}
508c41aa3ceSMax Schwarz 
509c41aa3ceSMax Schwarz 	/* is there anything left to handle? */
51029209338SDoug Anderson 	if ((ipd & REG_INT_ALL) == 0)
511c41aa3ceSMax Schwarz 		goto out;
512c41aa3ceSMax Schwarz 
513c41aa3ceSMax Schwarz 	switch (i2c->state) {
514c41aa3ceSMax Schwarz 	case STATE_START:
515c41aa3ceSMax Schwarz 		rk3x_i2c_handle_start(i2c, ipd);
516c41aa3ceSMax Schwarz 		break;
517c41aa3ceSMax Schwarz 	case STATE_WRITE:
518c41aa3ceSMax Schwarz 		rk3x_i2c_handle_write(i2c, ipd);
519c41aa3ceSMax Schwarz 		break;
520c41aa3ceSMax Schwarz 	case STATE_READ:
521c41aa3ceSMax Schwarz 		rk3x_i2c_handle_read(i2c, ipd);
522c41aa3ceSMax Schwarz 		break;
523c41aa3ceSMax Schwarz 	case STATE_STOP:
524c41aa3ceSMax Schwarz 		rk3x_i2c_handle_stop(i2c, ipd);
525c41aa3ceSMax Schwarz 		break;
526c41aa3ceSMax Schwarz 	case STATE_IDLE:
527c41aa3ceSMax Schwarz 		break;
528c41aa3ceSMax Schwarz 	}
529c41aa3ceSMax Schwarz 
530c41aa3ceSMax Schwarz out:
531c41aa3ceSMax Schwarz 	spin_unlock(&i2c->lock);
532c41aa3ceSMax Schwarz 	return IRQ_HANDLED;
533c41aa3ceSMax Schwarz }
534c41aa3ceSMax Schwarz 
535249051f4SMax Schwarz /**
536b58fd3beSDavid Wu  * Get timing values of I2C specification
537b58fd3beSDavid Wu  *
538b58fd3beSDavid Wu  * @speed: Desired SCL frequency
539b58fd3beSDavid Wu  *
540b58fd3beSDavid Wu  * Returns: Matched i2c spec values.
541b58fd3beSDavid Wu  */
542b58fd3beSDavid Wu static const struct i2c_spec_values *rk3x_i2c_get_spec(unsigned int speed)
543b58fd3beSDavid Wu {
544b58fd3beSDavid Wu 	if (speed <= 100000)
545b58fd3beSDavid Wu 		return &standard_mode_spec;
546a02f3d08SDavid Wu 	else if (speed <= 400000)
547b58fd3beSDavid Wu 		return &fast_mode_spec;
548a02f3d08SDavid Wu 	else
549a02f3d08SDavid Wu 		return &fast_mode_plus_spec;
550b58fd3beSDavid Wu }
551b58fd3beSDavid Wu 
552b58fd3beSDavid Wu /**
553249051f4SMax Schwarz  * Calculate divider values for desired SCL frequency
554249051f4SMax Schwarz  *
555249051f4SMax Schwarz  * @clk_rate: I2C input clock rate
556e26747bfSDavid Wu  * @t: Known I2C timing information
557e26747bfSDavid Wu  * @t_calc: Caculated rk3x private timings that would be written into regs
558249051f4SMax Schwarz  *
559249051f4SMax Schwarz  * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
560249051f4SMax Schwarz  * a best-effort divider value is returned in divs. If the target rate is
561249051f4SMax Schwarz  * too high, we silently use the highest possible rate.
562249051f4SMax Schwarz  */
5637e086c3fSDavid Wu static int rk3x_i2c_v0_calc_timings(unsigned long clk_rate,
5641ab92956SDavid Wu 				    struct i2c_timings *t,
565e26747bfSDavid Wu 				    struct rk3x_i2c_calced_timings *t_calc)
5660285f8f5Saddy ke {
5671330e291Saddy ke 	unsigned long min_low_ns, min_high_ns;
5680285f8f5Saddy ke 	unsigned long max_low_ns, min_total_ns;
5690285f8f5Saddy ke 
570249051f4SMax Schwarz 	unsigned long clk_rate_khz, scl_rate_khz;
5710285f8f5Saddy ke 
5720285f8f5Saddy ke 	unsigned long min_low_div, min_high_div;
5730285f8f5Saddy ke 	unsigned long max_low_div;
5740285f8f5Saddy ke 
5750285f8f5Saddy ke 	unsigned long min_div_for_hold, min_total_div;
5760285f8f5Saddy ke 	unsigned long extra_div, extra_low_div, ideal_low_div;
5770285f8f5Saddy ke 
578b58fd3beSDavid Wu 	unsigned long data_hold_buffer_ns = 50;
579b58fd3beSDavid Wu 	const struct i2c_spec_values *spec;
580249051f4SMax Schwarz 	int ret = 0;
581249051f4SMax Schwarz 
5820285f8f5Saddy ke 	/* Only support standard-mode and fast-mode */
5831ab92956SDavid Wu 	if (WARN_ON(t->bus_freq_hz > 400000))
5841ab92956SDavid Wu 		t->bus_freq_hz = 400000;
5850285f8f5Saddy ke 
5860285f8f5Saddy ke 	/* prevent scl_rate_khz from becoming 0 */
5871ab92956SDavid Wu 	if (WARN_ON(t->bus_freq_hz < 1000))
5881ab92956SDavid Wu 		t->bus_freq_hz = 1000;
5890285f8f5Saddy ke 
5900285f8f5Saddy ke 	/*
5911330e291Saddy ke 	 * min_low_ns:  The minimum number of ns we need to hold low to
5921330e291Saddy ke 	 *		meet I2C specification, should include fall time.
5931330e291Saddy ke 	 * min_high_ns: The minimum number of ns we need to hold high to
5941330e291Saddy ke 	 *		meet I2C specification, should include rise time.
5951330e291Saddy ke 	 * max_low_ns:  The maximum number of ns we can hold low to meet
5961330e291Saddy ke 	 *		I2C specification.
5970285f8f5Saddy ke 	 *
5981330e291Saddy ke 	 * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
5990285f8f5Saddy ke 	 *	 This is because the i2c host on Rockchip holds the data line
6000285f8f5Saddy ke 	 *	 for half the low time.
6010285f8f5Saddy ke 	 */
602b58fd3beSDavid Wu 	spec = rk3x_i2c_get_spec(t->bus_freq_hz);
603b58fd3beSDavid Wu 	min_high_ns = t->scl_rise_ns + spec->min_high_ns;
604387f0de6SDoug Anderson 
605387f0de6SDoug Anderson 	/*
606387f0de6SDoug Anderson 	 * Timings for repeated start:
607387f0de6SDoug Anderson 	 * - controller appears to drop SDA at .875x (7/8) programmed clk high.
608387f0de6SDoug Anderson 	 * - controller appears to keep SCL high for 2x programmed clk high.
609387f0de6SDoug Anderson 	 *
610387f0de6SDoug Anderson 	 * We need to account for those rules in picking our "high" time so
611387f0de6SDoug Anderson 	 * we meet tSU;STA and tHD;STA times.
612387f0de6SDoug Anderson 	 */
613b58fd3beSDavid Wu 	min_high_ns = max(min_high_ns, DIV_ROUND_UP(
614b58fd3beSDavid Wu 		(t->scl_rise_ns + spec->min_setup_start_ns) * 1000, 875));
615b58fd3beSDavid Wu 	min_high_ns = max(min_high_ns, DIV_ROUND_UP(
616b58fd3beSDavid Wu 		(t->scl_rise_ns + spec->min_setup_start_ns + t->sda_fall_ns +
617b58fd3beSDavid Wu 		spec->min_high_ns), 2));
618387f0de6SDoug Anderson 
619b58fd3beSDavid Wu 	min_low_ns = t->scl_fall_ns + spec->min_low_ns;
620b58fd3beSDavid Wu 	max_low_ns =  spec->max_data_hold_ns * 2 - data_hold_buffer_ns;
6210285f8f5Saddy ke 	min_total_ns = min_low_ns + min_high_ns;
6220285f8f5Saddy ke 
6230285f8f5Saddy ke 	/* Adjust to avoid overflow */
624249051f4SMax Schwarz 	clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
6251ab92956SDavid Wu 	scl_rate_khz = t->bus_freq_hz / 1000;
6260285f8f5Saddy ke 
6270285f8f5Saddy ke 	/*
6280285f8f5Saddy ke 	 * We need the total div to be >= this number
6290285f8f5Saddy ke 	 * so we don't clock too fast.
6300285f8f5Saddy ke 	 */
631249051f4SMax Schwarz 	min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
6320285f8f5Saddy ke 
6330285f8f5Saddy ke 	/* These are the min dividers needed for min hold times. */
634249051f4SMax Schwarz 	min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
635249051f4SMax Schwarz 	min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
6360285f8f5Saddy ke 	min_div_for_hold = (min_low_div + min_high_div);
6370285f8f5Saddy ke 
6380285f8f5Saddy ke 	/*
6391330e291Saddy ke 	 * This is the maximum divider so we don't go over the maximum.
6401330e291Saddy ke 	 * We don't round up here (we round down) since this is a maximum.
6410285f8f5Saddy ke 	 */
642249051f4SMax Schwarz 	max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
6430285f8f5Saddy ke 
6440285f8f5Saddy ke 	if (min_low_div > max_low_div) {
6450285f8f5Saddy ke 		WARN_ONCE(true,
6460285f8f5Saddy ke 			  "Conflicting, min_low_div %lu, max_low_div %lu\n",
6470285f8f5Saddy ke 			  min_low_div, max_low_div);
6480285f8f5Saddy ke 		max_low_div = min_low_div;
6490285f8f5Saddy ke 	}
6500285f8f5Saddy ke 
6510285f8f5Saddy ke 	if (min_div_for_hold > min_total_div) {
6520285f8f5Saddy ke 		/*
6530285f8f5Saddy ke 		 * Time needed to meet hold requirements is important.
6540285f8f5Saddy ke 		 * Just use that.
6550285f8f5Saddy ke 		 */
656e26747bfSDavid Wu 		t_calc->div_low = min_low_div;
657e26747bfSDavid Wu 		t_calc->div_high = min_high_div;
6580285f8f5Saddy ke 	} else {
6590285f8f5Saddy ke 		/*
6600285f8f5Saddy ke 		 * We've got to distribute some time among the low and high
6610285f8f5Saddy ke 		 * so we don't run too fast.
6620285f8f5Saddy ke 		 */
6630285f8f5Saddy ke 		extra_div = min_total_div - min_div_for_hold;
6640285f8f5Saddy ke 
6650285f8f5Saddy ke 		/*
6660285f8f5Saddy ke 		 * We'll try to split things up perfectly evenly,
6670285f8f5Saddy ke 		 * biasing slightly towards having a higher div
6680285f8f5Saddy ke 		 * for low (spend more time low).
6690285f8f5Saddy ke 		 */
670249051f4SMax Schwarz 		ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
6710285f8f5Saddy ke 					     scl_rate_khz * 8 * min_total_ns);
6720285f8f5Saddy ke 
6731330e291Saddy ke 		/* Don't allow it to go over the maximum */
6740285f8f5Saddy ke 		if (ideal_low_div > max_low_div)
6750285f8f5Saddy ke 			ideal_low_div = max_low_div;
6760285f8f5Saddy ke 
6770285f8f5Saddy ke 		/*
6780285f8f5Saddy ke 		 * Handle when the ideal low div is going to take up
6790285f8f5Saddy ke 		 * more than we have.
6800285f8f5Saddy ke 		 */
6810285f8f5Saddy ke 		if (ideal_low_div > min_low_div + extra_div)
6820285f8f5Saddy ke 			ideal_low_div = min_low_div + extra_div;
6830285f8f5Saddy ke 
6840285f8f5Saddy ke 		/* Give low the "ideal" and give high whatever extra is left */
6850285f8f5Saddy ke 		extra_low_div = ideal_low_div - min_low_div;
686e26747bfSDavid Wu 		t_calc->div_low = ideal_low_div;
687e26747bfSDavid Wu 		t_calc->div_high = min_high_div + (extra_div - extra_low_div);
6880285f8f5Saddy ke 	}
6890285f8f5Saddy ke 
6900285f8f5Saddy ke 	/*
6910285f8f5Saddy ke 	 * Adjust to the fact that the hardware has an implicit "+1".
6920285f8f5Saddy ke 	 * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
6930285f8f5Saddy ke 	 */
694e26747bfSDavid Wu 	t_calc->div_low--;
695e26747bfSDavid Wu 	t_calc->div_high--;
6960285f8f5Saddy ke 
697249051f4SMax Schwarz 	/* Maximum divider supported by hw is 0xffff */
698e26747bfSDavid Wu 	if (t_calc->div_low > 0xffff) {
699e26747bfSDavid Wu 		t_calc->div_low = 0xffff;
700249051f4SMax Schwarz 		ret = -EINVAL;
7010285f8f5Saddy ke 	}
7020285f8f5Saddy ke 
703e26747bfSDavid Wu 	if (t_calc->div_high > 0xffff) {
704e26747bfSDavid Wu 		t_calc->div_high = 0xffff;
705249051f4SMax Schwarz 		ret = -EINVAL;
706249051f4SMax Schwarz 	}
707249051f4SMax Schwarz 
708249051f4SMax Schwarz 	return ret;
709249051f4SMax Schwarz }
710249051f4SMax Schwarz 
7117e086c3fSDavid Wu /**
7127e086c3fSDavid Wu  * Calculate timing values for desired SCL frequency
7137e086c3fSDavid Wu  *
7147e086c3fSDavid Wu  * @clk_rate: I2C input clock rate
7157e086c3fSDavid Wu  * @t: Known I2C timing information
7167e086c3fSDavid Wu  * @t_calc: Caculated rk3x private timings that would be written into regs
7177e086c3fSDavid Wu  *
7187e086c3fSDavid Wu  * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
7197e086c3fSDavid Wu  * a best-effort divider value is returned in divs. If the target rate is
7207e086c3fSDavid Wu  * too high, we silently use the highest possible rate.
7217e086c3fSDavid Wu  * The following formulas are v1's method to calculate timings.
7227e086c3fSDavid Wu  *
7237e086c3fSDavid Wu  * l = divl + 1;
7247e086c3fSDavid Wu  * h = divh + 1;
7257e086c3fSDavid Wu  * s = sda_update_config + 1;
7267e086c3fSDavid Wu  * u = start_setup_config + 1;
7277e086c3fSDavid Wu  * p = stop_setup_config + 1;
7287e086c3fSDavid Wu  * T = Tclk_i2c;
7297e086c3fSDavid Wu  *
7307e086c3fSDavid Wu  * tHigh = 8 * h * T;
7317e086c3fSDavid Wu  * tLow = 8 * l * T;
7327e086c3fSDavid Wu  *
7337e086c3fSDavid Wu  * tHD;sda = (l * s + 1) * T;
7347e086c3fSDavid Wu  * tSU;sda = [(8 - s) * l + 1] * T;
7357e086c3fSDavid Wu  * tI2C = 8 * (l + h) * T;
7367e086c3fSDavid Wu  *
7377e086c3fSDavid Wu  * tSU;sta = (8h * u + 1) * T;
7387e086c3fSDavid Wu  * tHD;sta = [8h * (u + 1) - 1] * T;
7397e086c3fSDavid Wu  * tSU;sto = (8h * p + 1) * T;
7407e086c3fSDavid Wu  */
7417e086c3fSDavid Wu static int rk3x_i2c_v1_calc_timings(unsigned long clk_rate,
7427e086c3fSDavid Wu 				    struct i2c_timings *t,
7437e086c3fSDavid Wu 				    struct rk3x_i2c_calced_timings *t_calc)
7447e086c3fSDavid Wu {
745*72cf8c56SDavid Wu 	unsigned long min_low_ns, min_high_ns;
7467e086c3fSDavid Wu 	unsigned long min_setup_start_ns, min_setup_data_ns;
7477e086c3fSDavid Wu 	unsigned long min_setup_stop_ns, max_hold_data_ns;
7487e086c3fSDavid Wu 
7497e086c3fSDavid Wu 	unsigned long clk_rate_khz, scl_rate_khz;
7507e086c3fSDavid Wu 
7517e086c3fSDavid Wu 	unsigned long min_low_div, min_high_div;
7527e086c3fSDavid Wu 
7537e086c3fSDavid Wu 	unsigned long min_div_for_hold, min_total_div;
7547e086c3fSDavid Wu 	unsigned long extra_div, extra_low_div;
7557e086c3fSDavid Wu 	unsigned long sda_update_cfg, stp_sta_cfg, stp_sto_cfg;
7567e086c3fSDavid Wu 
7577e086c3fSDavid Wu 	const struct i2c_spec_values *spec;
7587e086c3fSDavid Wu 	int ret = 0;
7597e086c3fSDavid Wu 
760a02f3d08SDavid Wu 	/* Support standard-mode, fast-mode and fast-mode plus */
761a02f3d08SDavid Wu 	if (WARN_ON(t->bus_freq_hz > 1000000))
762a02f3d08SDavid Wu 		t->bus_freq_hz = 1000000;
7637e086c3fSDavid Wu 
7647e086c3fSDavid Wu 	/* prevent scl_rate_khz from becoming 0 */
7657e086c3fSDavid Wu 	if (WARN_ON(t->bus_freq_hz < 1000))
7667e086c3fSDavid Wu 		t->bus_freq_hz = 1000;
7677e086c3fSDavid Wu 
7687e086c3fSDavid Wu 	/*
7697e086c3fSDavid Wu 	 * min_low_ns: The minimum number of ns we need to hold low to
7707e086c3fSDavid Wu 	 *	       meet I2C specification, should include fall time.
7717e086c3fSDavid Wu 	 * min_high_ns: The minimum number of ns we need to hold high to
7727e086c3fSDavid Wu 	 *	        meet I2C specification, should include rise time.
7737e086c3fSDavid Wu 	 */
7747e086c3fSDavid Wu 	spec = rk3x_i2c_get_spec(t->bus_freq_hz);
7757e086c3fSDavid Wu 
7767e086c3fSDavid Wu 	/* calculate min-divh and min-divl */
7777e086c3fSDavid Wu 	clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
7787e086c3fSDavid Wu 	scl_rate_khz = t->bus_freq_hz / 1000;
7797e086c3fSDavid Wu 	min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
7807e086c3fSDavid Wu 
7817e086c3fSDavid Wu 	min_high_ns = t->scl_rise_ns + spec->min_high_ns;
7827e086c3fSDavid Wu 	min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
7837e086c3fSDavid Wu 
7847e086c3fSDavid Wu 	min_low_ns = t->scl_fall_ns + spec->min_low_ns;
7857e086c3fSDavid Wu 	min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
7867e086c3fSDavid Wu 
7877e086c3fSDavid Wu 	/*
7887e086c3fSDavid Wu 	 * Final divh and divl must be greater than 0, otherwise the
7897e086c3fSDavid Wu 	 * hardware would not output the i2c clk.
7907e086c3fSDavid Wu 	 */
7917e086c3fSDavid Wu 	min_high_div = (min_high_div < 1) ? 2 : min_high_div;
7927e086c3fSDavid Wu 	min_low_div = (min_low_div < 1) ? 2 : min_low_div;
7937e086c3fSDavid Wu 
7947e086c3fSDavid Wu 	/* These are the min dividers needed for min hold times. */
7957e086c3fSDavid Wu 	min_div_for_hold = (min_low_div + min_high_div);
7967e086c3fSDavid Wu 
7977e086c3fSDavid Wu 	/*
7987e086c3fSDavid Wu 	 * This is the maximum divider so we don't go over the maximum.
7997e086c3fSDavid Wu 	 * We don't round up here (we round down) since this is a maximum.
8007e086c3fSDavid Wu 	 */
8017e086c3fSDavid Wu 	if (min_div_for_hold >= min_total_div) {
8027e086c3fSDavid Wu 		/*
8037e086c3fSDavid Wu 		 * Time needed to meet hold requirements is important.
8047e086c3fSDavid Wu 		 * Just use that.
8057e086c3fSDavid Wu 		 */
8067e086c3fSDavid Wu 		t_calc->div_low = min_low_div;
8077e086c3fSDavid Wu 		t_calc->div_high = min_high_div;
8087e086c3fSDavid Wu 	} else {
8097e086c3fSDavid Wu 		/*
8107e086c3fSDavid Wu 		 * We've got to distribute some time among the low and high
8117e086c3fSDavid Wu 		 * so we don't run too fast.
8127e086c3fSDavid Wu 		 * We'll try to split things up by the scale of min_low_div and
8137e086c3fSDavid Wu 		 * min_high_div, biasing slightly towards having a higher div
8147e086c3fSDavid Wu 		 * for low (spend more time low).
8157e086c3fSDavid Wu 		 */
8167e086c3fSDavid Wu 		extra_div = min_total_div - min_div_for_hold;
8177e086c3fSDavid Wu 		extra_low_div = DIV_ROUND_UP(min_low_div * extra_div,
8187e086c3fSDavid Wu 					     min_div_for_hold);
8197e086c3fSDavid Wu 
8207e086c3fSDavid Wu 		t_calc->div_low = min_low_div + extra_low_div;
8217e086c3fSDavid Wu 		t_calc->div_high = min_high_div + (extra_div - extra_low_div);
8227e086c3fSDavid Wu 	}
8237e086c3fSDavid Wu 
8247e086c3fSDavid Wu 	/*
8257e086c3fSDavid Wu 	 * calculate sda data hold count by the rules, data_upd_st:3
8267e086c3fSDavid Wu 	 * is a appropriate value to reduce calculated times.
8277e086c3fSDavid Wu 	 */
8287e086c3fSDavid Wu 	for (sda_update_cfg = 3; sda_update_cfg > 0; sda_update_cfg--) {
8297e086c3fSDavid Wu 		max_hold_data_ns =  DIV_ROUND_UP((sda_update_cfg
8307e086c3fSDavid Wu 						 * (t_calc->div_low) + 1)
8317e086c3fSDavid Wu 						 * 1000000, clk_rate_khz);
8327e086c3fSDavid Wu 		min_setup_data_ns =  DIV_ROUND_UP(((8 - sda_update_cfg)
8337e086c3fSDavid Wu 						 * (t_calc->div_low) + 1)
8347e086c3fSDavid Wu 						 * 1000000, clk_rate_khz);
8357e086c3fSDavid Wu 		if ((max_hold_data_ns < spec->max_data_hold_ns) &&
8367e086c3fSDavid Wu 		    (min_setup_data_ns > spec->min_data_setup_ns))
8377e086c3fSDavid Wu 			break;
8387e086c3fSDavid Wu 	}
8397e086c3fSDavid Wu 
8407e086c3fSDavid Wu 	/* calculate setup start config */
8417e086c3fSDavid Wu 	min_setup_start_ns = t->scl_rise_ns + spec->min_setup_start_ns;
8427e086c3fSDavid Wu 	stp_sta_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_start_ns
8437e086c3fSDavid Wu 			   - 1000000, 8 * 1000000 * (t_calc->div_high));
8447e086c3fSDavid Wu 
8457e086c3fSDavid Wu 	/* calculate setup stop config */
8467e086c3fSDavid Wu 	min_setup_stop_ns = t->scl_rise_ns + spec->min_setup_stop_ns;
8477e086c3fSDavid Wu 	stp_sto_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_stop_ns
8487e086c3fSDavid Wu 			   - 1000000, 8 * 1000000 * (t_calc->div_high));
8497e086c3fSDavid Wu 
8507e086c3fSDavid Wu 	t_calc->tuning = REG_CON_SDA_CFG(--sda_update_cfg) |
8517e086c3fSDavid Wu 			 REG_CON_STA_CFG(--stp_sta_cfg) |
8527e086c3fSDavid Wu 			 REG_CON_STO_CFG(--stp_sto_cfg);
8537e086c3fSDavid Wu 
8547e086c3fSDavid Wu 	t_calc->div_low--;
8557e086c3fSDavid Wu 	t_calc->div_high--;
8567e086c3fSDavid Wu 
8577e086c3fSDavid Wu 	/* Maximum divider supported by hw is 0xffff */
8587e086c3fSDavid Wu 	if (t_calc->div_low > 0xffff) {
8597e086c3fSDavid Wu 		t_calc->div_low = 0xffff;
8607e086c3fSDavid Wu 		ret = -EINVAL;
8617e086c3fSDavid Wu 	}
8627e086c3fSDavid Wu 
8637e086c3fSDavid Wu 	if (t_calc->div_high > 0xffff) {
8647e086c3fSDavid Wu 		t_calc->div_high = 0xffff;
8657e086c3fSDavid Wu 		ret = -EINVAL;
8667e086c3fSDavid Wu 	}
8677e086c3fSDavid Wu 
8687e086c3fSDavid Wu 	return ret;
8697e086c3fSDavid Wu }
8707e086c3fSDavid Wu 
871249051f4SMax Schwarz static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
872c41aa3ceSMax Schwarz {
8731ab92956SDavid Wu 	struct i2c_timings *t = &i2c->t;
874e26747bfSDavid Wu 	struct rk3x_i2c_calced_timings calc;
8750285f8f5Saddy ke 	u64 t_low_ns, t_high_ns;
8767e086c3fSDavid Wu 	unsigned long flags;
8777e086c3fSDavid Wu 	u32 val;
878249051f4SMax Schwarz 	int ret;
879c41aa3ceSMax Schwarz 
8807e086c3fSDavid Wu 	ret = i2c->soc_data->calc_timings(clk_rate, t, &calc);
8811ab92956SDavid Wu 	WARN_ONCE(ret != 0, "Could not reach SCL freq %u", t->bus_freq_hz);
882249051f4SMax Schwarz 
8837e086c3fSDavid Wu 	clk_enable(i2c->pclk);
8847e086c3fSDavid Wu 
8857e086c3fSDavid Wu 	spin_lock_irqsave(&i2c->lock, flags);
8867e086c3fSDavid Wu 	val = i2c_readl(i2c, REG_CON);
8877e086c3fSDavid Wu 	val &= ~REG_CON_TUNING_MASK;
8887e086c3fSDavid Wu 	val |= calc.tuning;
8897e086c3fSDavid Wu 	i2c_writel(i2c, val, REG_CON);
890e26747bfSDavid Wu 	i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff),
891e26747bfSDavid Wu 		   REG_CLKDIV);
8927e086c3fSDavid Wu 	spin_unlock_irqrestore(&i2c->lock, flags);
8937e086c3fSDavid Wu 
8947e086c3fSDavid Wu 	clk_disable(i2c->pclk);
8950285f8f5Saddy ke 
896e26747bfSDavid Wu 	t_low_ns = div_u64(((u64)calc.div_low + 1) * 8 * 1000000000, clk_rate);
897e26747bfSDavid Wu 	t_high_ns = div_u64(((u64)calc.div_high + 1) * 8 * 1000000000,
898e26747bfSDavid Wu 			    clk_rate);
8990285f8f5Saddy ke 	dev_dbg(i2c->dev,
900249051f4SMax Schwarz 		"CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
901249051f4SMax Schwarz 		clk_rate / 1000,
9021ab92956SDavid Wu 		1000000000 / t->bus_freq_hz,
9030285f8f5Saddy ke 		t_low_ns, t_high_ns);
904249051f4SMax Schwarz }
9050285f8f5Saddy ke 
906249051f4SMax Schwarz /**
907249051f4SMax Schwarz  * rk3x_i2c_clk_notifier_cb - Clock rate change callback
908249051f4SMax Schwarz  * @nb:		Pointer to notifier block
909249051f4SMax Schwarz  * @event:	Notification reason
910249051f4SMax Schwarz  * @data:	Pointer to notification data object
911249051f4SMax Schwarz  *
912249051f4SMax Schwarz  * The callback checks whether a valid bus frequency can be generated after the
913249051f4SMax Schwarz  * change. If so, the change is acknowledged, otherwise the change is aborted.
914249051f4SMax Schwarz  * New dividers are written to the HW in the pre- or post change notification
915249051f4SMax Schwarz  * depending on the scaling direction.
916249051f4SMax Schwarz  *
917249051f4SMax Schwarz  * Code adapted from i2c-cadence.c.
918249051f4SMax Schwarz  *
919249051f4SMax Schwarz  * Return:	NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
920249051f4SMax Schwarz  *		to acknowedge the change, NOTIFY_DONE if the notification is
921249051f4SMax Schwarz  *		considered irrelevant.
922249051f4SMax Schwarz  */
923249051f4SMax Schwarz static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
924249051f4SMax Schwarz 				    event, void *data)
925249051f4SMax Schwarz {
926249051f4SMax Schwarz 	struct clk_notifier_data *ndata = data;
927249051f4SMax Schwarz 	struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
928e26747bfSDavid Wu 	struct rk3x_i2c_calced_timings calc;
929249051f4SMax Schwarz 
930249051f4SMax Schwarz 	switch (event) {
931249051f4SMax Schwarz 	case PRE_RATE_CHANGE:
9327e086c3fSDavid Wu 		/*
9337e086c3fSDavid Wu 		 * Try the calculation (but don't store the result) ahead of
9347e086c3fSDavid Wu 		 * time to see if we need to block the clock change.  Timings
9357e086c3fSDavid Wu 		 * shouldn't actually take effect until rk3x_i2c_adapt_div().
9367e086c3fSDavid Wu 		 */
9377e086c3fSDavid Wu 		if (i2c->soc_data->calc_timings(ndata->new_rate, &i2c->t,
9387e086c3fSDavid Wu 						&calc) != 0)
939249051f4SMax Schwarz 			return NOTIFY_STOP;
940249051f4SMax Schwarz 
941249051f4SMax Schwarz 		/* scale up */
942249051f4SMax Schwarz 		if (ndata->new_rate > ndata->old_rate)
943249051f4SMax Schwarz 			rk3x_i2c_adapt_div(i2c, ndata->new_rate);
944249051f4SMax Schwarz 
945249051f4SMax Schwarz 		return NOTIFY_OK;
946249051f4SMax Schwarz 	case POST_RATE_CHANGE:
947249051f4SMax Schwarz 		/* scale down */
948249051f4SMax Schwarz 		if (ndata->new_rate < ndata->old_rate)
949249051f4SMax Schwarz 			rk3x_i2c_adapt_div(i2c, ndata->new_rate);
950249051f4SMax Schwarz 		return NOTIFY_OK;
951249051f4SMax Schwarz 	case ABORT_RATE_CHANGE:
952249051f4SMax Schwarz 		/* scale up */
953249051f4SMax Schwarz 		if (ndata->new_rate > ndata->old_rate)
954249051f4SMax Schwarz 			rk3x_i2c_adapt_div(i2c, ndata->old_rate);
955249051f4SMax Schwarz 		return NOTIFY_OK;
956249051f4SMax Schwarz 	default:
957249051f4SMax Schwarz 		return NOTIFY_DONE;
958249051f4SMax Schwarz 	}
959c41aa3ceSMax Schwarz }
960c41aa3ceSMax Schwarz 
961c41aa3ceSMax Schwarz /**
962c41aa3ceSMax Schwarz  * Setup I2C registers for an I2C operation specified by msgs, num.
963c41aa3ceSMax Schwarz  *
964c41aa3ceSMax Schwarz  * Must be called with i2c->lock held.
965c41aa3ceSMax Schwarz  *
966c41aa3ceSMax Schwarz  * @msgs: I2C msgs to process
967c41aa3ceSMax Schwarz  * @num: Number of msgs
968c41aa3ceSMax Schwarz  *
969c41aa3ceSMax Schwarz  * returns: Number of I2C msgs processed or negative in case of error
970c41aa3ceSMax Schwarz  */
971c41aa3ceSMax Schwarz static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
972c41aa3ceSMax Schwarz {
973c41aa3ceSMax Schwarz 	u32 addr = (msgs[0].addr & 0x7f) << 1;
974c41aa3ceSMax Schwarz 	int ret = 0;
975c41aa3ceSMax Schwarz 
976c41aa3ceSMax Schwarz 	/*
977c41aa3ceSMax Schwarz 	 * The I2C adapter can issue a small (len < 4) write packet before
978c41aa3ceSMax Schwarz 	 * reading. This speeds up SMBus-style register reads.
979c41aa3ceSMax Schwarz 	 * The MRXADDR/MRXRADDR hold the slave address and the slave register
980c41aa3ceSMax Schwarz 	 * address in this case.
981c41aa3ceSMax Schwarz 	 */
982c41aa3ceSMax Schwarz 
983c41aa3ceSMax Schwarz 	if (num >= 2 && msgs[0].len < 4 &&
984c41aa3ceSMax Schwarz 	    !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
985c41aa3ceSMax Schwarz 		u32 reg_addr = 0;
986c41aa3ceSMax Schwarz 		int i;
987c41aa3ceSMax Schwarz 
988c41aa3ceSMax Schwarz 		dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
989c41aa3ceSMax Schwarz 			addr >> 1);
990c41aa3ceSMax Schwarz 
991c41aa3ceSMax Schwarz 		/* Fill MRXRADDR with the register address(es) */
992c41aa3ceSMax Schwarz 		for (i = 0; i < msgs[0].len; ++i) {
993c41aa3ceSMax Schwarz 			reg_addr |= msgs[0].buf[i] << (i * 8);
994c41aa3ceSMax Schwarz 			reg_addr |= REG_MRXADDR_VALID(i);
995c41aa3ceSMax Schwarz 		}
996c41aa3ceSMax Schwarz 
997c41aa3ceSMax Schwarz 		/* msgs[0] is handled by hw. */
998c41aa3ceSMax Schwarz 		i2c->msg = &msgs[1];
999c41aa3ceSMax Schwarz 
1000c41aa3ceSMax Schwarz 		i2c->mode = REG_CON_MOD_REGISTER_TX;
1001c41aa3ceSMax Schwarz 
1002c41aa3ceSMax Schwarz 		i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
1003c41aa3ceSMax Schwarz 		i2c_writel(i2c, reg_addr, REG_MRXRADDR);
1004c41aa3ceSMax Schwarz 
1005c41aa3ceSMax Schwarz 		ret = 2;
1006c41aa3ceSMax Schwarz 	} else {
1007c41aa3ceSMax Schwarz 		/*
1008c41aa3ceSMax Schwarz 		 * We'll have to do it the boring way and process the msgs
1009c41aa3ceSMax Schwarz 		 * one-by-one.
1010c41aa3ceSMax Schwarz 		 */
1011c41aa3ceSMax Schwarz 
1012c41aa3ceSMax Schwarz 		if (msgs[0].flags & I2C_M_RD) {
1013c41aa3ceSMax Schwarz 			addr |= 1; /* set read bit */
1014c41aa3ceSMax Schwarz 
1015c41aa3ceSMax Schwarz 			/*
1016c41aa3ceSMax Schwarz 			 * We have to transmit the slave addr first. Use
1017c41aa3ceSMax Schwarz 			 * MOD_REGISTER_TX for that purpose.
1018c41aa3ceSMax Schwarz 			 */
1019c41aa3ceSMax Schwarz 			i2c->mode = REG_CON_MOD_REGISTER_TX;
1020c41aa3ceSMax Schwarz 			i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
1021c41aa3ceSMax Schwarz 				   REG_MRXADDR);
1022c41aa3ceSMax Schwarz 			i2c_writel(i2c, 0, REG_MRXRADDR);
1023c41aa3ceSMax Schwarz 		} else {
1024c41aa3ceSMax Schwarz 			i2c->mode = REG_CON_MOD_TX;
1025c41aa3ceSMax Schwarz 		}
1026c41aa3ceSMax Schwarz 
1027c41aa3ceSMax Schwarz 		i2c->msg = &msgs[0];
1028c41aa3ceSMax Schwarz 
1029c41aa3ceSMax Schwarz 		ret = 1;
1030c41aa3ceSMax Schwarz 	}
1031c41aa3ceSMax Schwarz 
1032c41aa3ceSMax Schwarz 	i2c->addr = msgs[0].addr;
1033c41aa3ceSMax Schwarz 	i2c->busy = true;
1034c41aa3ceSMax Schwarz 	i2c->state = STATE_START;
1035c41aa3ceSMax Schwarz 	i2c->processed = 0;
1036c41aa3ceSMax Schwarz 	i2c->error = 0;
1037c41aa3ceSMax Schwarz 
1038c41aa3ceSMax Schwarz 	rk3x_i2c_clean_ipd(i2c);
1039c41aa3ceSMax Schwarz 
1040c41aa3ceSMax Schwarz 	return ret;
1041c41aa3ceSMax Schwarz }
1042c41aa3ceSMax Schwarz 
1043c41aa3ceSMax Schwarz static int rk3x_i2c_xfer(struct i2c_adapter *adap,
1044c41aa3ceSMax Schwarz 			 struct i2c_msg *msgs, int num)
1045c41aa3ceSMax Schwarz {
1046c41aa3ceSMax Schwarz 	struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
1047c41aa3ceSMax Schwarz 	unsigned long timeout, flags;
10487e086c3fSDavid Wu 	u32 val;
1049c41aa3ceSMax Schwarz 	int ret = 0;
1050c41aa3ceSMax Schwarz 	int i;
1051c41aa3ceSMax Schwarz 
1052c41aa3ceSMax Schwarz 	spin_lock_irqsave(&i2c->lock, flags);
1053c41aa3ceSMax Schwarz 
1054c41aa3ceSMax Schwarz 	clk_enable(i2c->clk);
10557e086c3fSDavid Wu 	clk_enable(i2c->pclk);
1056c41aa3ceSMax Schwarz 
1057c41aa3ceSMax Schwarz 	i2c->is_last_msg = false;
1058c41aa3ceSMax Schwarz 
1059c41aa3ceSMax Schwarz 	/*
1060c41aa3ceSMax Schwarz 	 * Process msgs. We can handle more than one message at once (see
1061c41aa3ceSMax Schwarz 	 * rk3x_i2c_setup()).
1062c41aa3ceSMax Schwarz 	 */
1063c41aa3ceSMax Schwarz 	for (i = 0; i < num; i += ret) {
1064c41aa3ceSMax Schwarz 		ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
1065c41aa3ceSMax Schwarz 
1066c41aa3ceSMax Schwarz 		if (ret < 0) {
1067c41aa3ceSMax Schwarz 			dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
1068c41aa3ceSMax Schwarz 			break;
1069c41aa3ceSMax Schwarz 		}
1070c41aa3ceSMax Schwarz 
1071c41aa3ceSMax Schwarz 		if (i + ret >= num)
1072c41aa3ceSMax Schwarz 			i2c->is_last_msg = true;
1073c41aa3ceSMax Schwarz 
1074c41aa3ceSMax Schwarz 		spin_unlock_irqrestore(&i2c->lock, flags);
1075c41aa3ceSMax Schwarz 
1076c41aa3ceSMax Schwarz 		rk3x_i2c_start(i2c);
1077c41aa3ceSMax Schwarz 
1078c41aa3ceSMax Schwarz 		timeout = wait_event_timeout(i2c->wait, !i2c->busy,
1079c41aa3ceSMax Schwarz 					     msecs_to_jiffies(WAIT_TIMEOUT));
1080c41aa3ceSMax Schwarz 
1081c41aa3ceSMax Schwarz 		spin_lock_irqsave(&i2c->lock, flags);
1082c41aa3ceSMax Schwarz 
1083c41aa3ceSMax Schwarz 		if (timeout == 0) {
1084c41aa3ceSMax Schwarz 			dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
1085c41aa3ceSMax Schwarz 				i2c_readl(i2c, REG_IPD), i2c->state);
1086c41aa3ceSMax Schwarz 
1087c41aa3ceSMax Schwarz 			/* Force a STOP condition without interrupt */
1088c41aa3ceSMax Schwarz 			i2c_writel(i2c, 0, REG_IEN);
10897e086c3fSDavid Wu 			val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
10907e086c3fSDavid Wu 			val |= REG_CON_EN | REG_CON_STOP;
10917e086c3fSDavid Wu 			i2c_writel(i2c, val, REG_CON);
1092c41aa3ceSMax Schwarz 
1093c41aa3ceSMax Schwarz 			i2c->state = STATE_IDLE;
1094c41aa3ceSMax Schwarz 
1095c41aa3ceSMax Schwarz 			ret = -ETIMEDOUT;
1096c41aa3ceSMax Schwarz 			break;
1097c41aa3ceSMax Schwarz 		}
1098c41aa3ceSMax Schwarz 
1099c41aa3ceSMax Schwarz 		if (i2c->error) {
1100c41aa3ceSMax Schwarz 			ret = i2c->error;
1101c41aa3ceSMax Schwarz 			break;
1102c41aa3ceSMax Schwarz 		}
1103c41aa3ceSMax Schwarz 	}
1104c41aa3ceSMax Schwarz 
11057e086c3fSDavid Wu 	clk_disable(i2c->pclk);
1106c41aa3ceSMax Schwarz 	clk_disable(i2c->clk);
11077e086c3fSDavid Wu 
1108c41aa3ceSMax Schwarz 	spin_unlock_irqrestore(&i2c->lock, flags);
1109c41aa3ceSMax Schwarz 
1110c6cbfb91SDmitry Torokhov 	return ret < 0 ? ret : num;
1111c41aa3ceSMax Schwarz }
1112c41aa3ceSMax Schwarz 
1113c41aa3ceSMax Schwarz static u32 rk3x_i2c_func(struct i2c_adapter *adap)
1114c41aa3ceSMax Schwarz {
1115c41aa3ceSMax Schwarz 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
1116c41aa3ceSMax Schwarz }
1117c41aa3ceSMax Schwarz 
1118c41aa3ceSMax Schwarz static const struct i2c_algorithm rk3x_i2c_algorithm = {
1119c41aa3ceSMax Schwarz 	.master_xfer		= rk3x_i2c_xfer,
1120c41aa3ceSMax Schwarz 	.functionality		= rk3x_i2c_func,
1121c41aa3ceSMax Schwarz };
1122c41aa3ceSMax Schwarz 
1123bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3066_soc_data = {
1124bef358c4SDavid Wu 	.grf_offset = 0x154,
11257e086c3fSDavid Wu 	.calc_timings = rk3x_i2c_v0_calc_timings,
1126bef358c4SDavid Wu };
1127bef358c4SDavid Wu 
1128bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3188_soc_data = {
1129bef358c4SDavid Wu 	.grf_offset = 0x0a4,
11307e086c3fSDavid Wu 	.calc_timings = rk3x_i2c_v0_calc_timings,
1131bef358c4SDavid Wu };
1132bef358c4SDavid Wu 
1133bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3228_soc_data = {
1134bef358c4SDavid Wu 	.grf_offset = -1,
11357e086c3fSDavid Wu 	.calc_timings = rk3x_i2c_v0_calc_timings,
1136bef358c4SDavid Wu };
1137bef358c4SDavid Wu 
1138bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3288_soc_data = {
1139bef358c4SDavid Wu 	.grf_offset = -1,
11407e086c3fSDavid Wu 	.calc_timings = rk3x_i2c_v0_calc_timings,
11417e086c3fSDavid Wu };
11427e086c3fSDavid Wu 
11437e086c3fSDavid Wu static const struct rk3x_i2c_soc_data rk3399_soc_data = {
11447e086c3fSDavid Wu 	.grf_offset = -1,
11457e086c3fSDavid Wu 	.calc_timings = rk3x_i2c_v1_calc_timings,
1146c41aa3ceSMax Schwarz };
1147c41aa3ceSMax Schwarz 
1148c41aa3ceSMax Schwarz static const struct of_device_id rk3x_i2c_match[] = {
1149bef358c4SDavid Wu 	{
1150bef358c4SDavid Wu 		.compatible = "rockchip,rk3066-i2c",
1151bef358c4SDavid Wu 		.data = (void *)&rk3066_soc_data
1152bef358c4SDavid Wu 	},
1153bef358c4SDavid Wu 	{
1154bef358c4SDavid Wu 		.compatible = "rockchip,rk3188-i2c",
1155bef358c4SDavid Wu 		.data = (void *)&rk3188_soc_data
1156bef358c4SDavid Wu 	},
1157bef358c4SDavid Wu 	{
1158bef358c4SDavid Wu 		.compatible = "rockchip,rk3228-i2c",
1159bef358c4SDavid Wu 		.data = (void *)&rk3228_soc_data
1160bef358c4SDavid Wu 	},
1161bef358c4SDavid Wu 	{
1162bef358c4SDavid Wu 		.compatible = "rockchip,rk3288-i2c",
1163bef358c4SDavid Wu 		.data = (void *)&rk3288_soc_data
1164bef358c4SDavid Wu 	},
11657e086c3fSDavid Wu 	{
11667e086c3fSDavid Wu 		.compatible = "rockchip,rk3399-i2c",
11677e086c3fSDavid Wu 		.data = (void *)&rk3399_soc_data
11687e086c3fSDavid Wu 	},
1169c51bd6acSDan Carpenter 	{},
1170c41aa3ceSMax Schwarz };
1171598cf161SLuis de Bethencourt MODULE_DEVICE_TABLE(of, rk3x_i2c_match);
1172c41aa3ceSMax Schwarz 
1173c41aa3ceSMax Schwarz static int rk3x_i2c_probe(struct platform_device *pdev)
1174c41aa3ceSMax Schwarz {
1175c41aa3ceSMax Schwarz 	struct device_node *np = pdev->dev.of_node;
1176c41aa3ceSMax Schwarz 	const struct of_device_id *match;
1177c41aa3ceSMax Schwarz 	struct rk3x_i2c *i2c;
1178c41aa3ceSMax Schwarz 	struct resource *mem;
1179c41aa3ceSMax Schwarz 	int ret = 0;
1180c41aa3ceSMax Schwarz 	int bus_nr;
1181c41aa3ceSMax Schwarz 	u32 value;
1182c41aa3ceSMax Schwarz 	int irq;
1183249051f4SMax Schwarz 	unsigned long clk_rate;
1184c41aa3ceSMax Schwarz 
1185c41aa3ceSMax Schwarz 	i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
1186c41aa3ceSMax Schwarz 	if (!i2c)
1187c41aa3ceSMax Schwarz 		return -ENOMEM;
1188c41aa3ceSMax Schwarz 
1189c41aa3ceSMax Schwarz 	match = of_match_node(rk3x_i2c_match, np);
1190c41aa3ceSMax Schwarz 	i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
1191c41aa3ceSMax Schwarz 
11921ab92956SDavid Wu 	/* use common interface to get I2C timing properties */
11931ab92956SDavid Wu 	i2c_parse_fw_timings(&pdev->dev, &i2c->t, true);
11941330e291Saddy ke 
1195c41aa3ceSMax Schwarz 	strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
1196c41aa3ceSMax Schwarz 	i2c->adap.owner = THIS_MODULE;
1197c41aa3ceSMax Schwarz 	i2c->adap.algo = &rk3x_i2c_algorithm;
1198c41aa3ceSMax Schwarz 	i2c->adap.retries = 3;
1199c41aa3ceSMax Schwarz 	i2c->adap.dev.of_node = np;
1200c41aa3ceSMax Schwarz 	i2c->adap.algo_data = i2c;
1201c41aa3ceSMax Schwarz 	i2c->adap.dev.parent = &pdev->dev;
1202c41aa3ceSMax Schwarz 
1203c41aa3ceSMax Schwarz 	i2c->dev = &pdev->dev;
1204c41aa3ceSMax Schwarz 
1205c41aa3ceSMax Schwarz 	spin_lock_init(&i2c->lock);
1206c41aa3ceSMax Schwarz 	init_waitqueue_head(&i2c->wait);
1207c41aa3ceSMax Schwarz 
1208c41aa3ceSMax Schwarz 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1209c41aa3ceSMax Schwarz 	i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
1210c41aa3ceSMax Schwarz 	if (IS_ERR(i2c->regs))
1211c41aa3ceSMax Schwarz 		return PTR_ERR(i2c->regs);
1212c41aa3ceSMax Schwarz 
1213c41aa3ceSMax Schwarz 	/* Try to set the I2C adapter number from dt */
1214c41aa3ceSMax Schwarz 	bus_nr = of_alias_get_id(np, "i2c");
1215c41aa3ceSMax Schwarz 
1216c41aa3ceSMax Schwarz 	/*
1217c41aa3ceSMax Schwarz 	 * Switch to new interface if the SoC also offers the old one.
1218c41aa3ceSMax Schwarz 	 * The control bit is located in the GRF register space.
1219c41aa3ceSMax Schwarz 	 */
1220c41aa3ceSMax Schwarz 	if (i2c->soc_data->grf_offset >= 0) {
1221c41aa3ceSMax Schwarz 		struct regmap *grf;
1222c41aa3ceSMax Schwarz 
1223c41aa3ceSMax Schwarz 		grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
1224c41aa3ceSMax Schwarz 		if (IS_ERR(grf)) {
1225c41aa3ceSMax Schwarz 			dev_err(&pdev->dev,
1226c41aa3ceSMax Schwarz 				"rk3x-i2c needs 'rockchip,grf' property\n");
1227c41aa3ceSMax Schwarz 			return PTR_ERR(grf);
1228c41aa3ceSMax Schwarz 		}
1229c41aa3ceSMax Schwarz 
1230c41aa3ceSMax Schwarz 		if (bus_nr < 0) {
1231c41aa3ceSMax Schwarz 			dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
1232c41aa3ceSMax Schwarz 			return -EINVAL;
1233c41aa3ceSMax Schwarz 		}
1234c41aa3ceSMax Schwarz 
1235c41aa3ceSMax Schwarz 		/* 27+i: write mask, 11+i: value */
1236c41aa3ceSMax Schwarz 		value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
1237c41aa3ceSMax Schwarz 
1238c41aa3ceSMax Schwarz 		ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
1239c41aa3ceSMax Schwarz 		if (ret != 0) {
1240c41aa3ceSMax Schwarz 			dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
1241c41aa3ceSMax Schwarz 			return ret;
1242c41aa3ceSMax Schwarz 		}
1243c41aa3ceSMax Schwarz 	}
1244c41aa3ceSMax Schwarz 
1245c41aa3ceSMax Schwarz 	/* IRQ setup */
1246c41aa3ceSMax Schwarz 	irq = platform_get_irq(pdev, 0);
1247c41aa3ceSMax Schwarz 	if (irq < 0) {
1248c41aa3ceSMax Schwarz 		dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
1249c41aa3ceSMax Schwarz 		return irq;
1250c41aa3ceSMax Schwarz 	}
1251c41aa3ceSMax Schwarz 
1252c41aa3ceSMax Schwarz 	ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
1253c41aa3ceSMax Schwarz 			       0, dev_name(&pdev->dev), i2c);
1254c41aa3ceSMax Schwarz 	if (ret < 0) {
1255c41aa3ceSMax Schwarz 		dev_err(&pdev->dev, "cannot request IRQ\n");
1256c41aa3ceSMax Schwarz 		return ret;
1257c41aa3ceSMax Schwarz 	}
1258c41aa3ceSMax Schwarz 
1259c41aa3ceSMax Schwarz 	platform_set_drvdata(pdev, i2c);
1260c41aa3ceSMax Schwarz 
12617e086c3fSDavid Wu 	if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) {
12627e086c3fSDavid Wu 		/* Only one clock to use for bus clock and peripheral clock */
12637e086c3fSDavid Wu 		i2c->clk = devm_clk_get(&pdev->dev, NULL);
12647e086c3fSDavid Wu 		i2c->pclk = i2c->clk;
12657e086c3fSDavid Wu 	} else {
12667e086c3fSDavid Wu 		i2c->clk = devm_clk_get(&pdev->dev, "i2c");
12677e086c3fSDavid Wu 		i2c->pclk = devm_clk_get(&pdev->dev, "pclk");
12687e086c3fSDavid Wu 	}
12697e086c3fSDavid Wu 
12707e086c3fSDavid Wu 	if (IS_ERR(i2c->clk)) {
12717e086c3fSDavid Wu 		ret = PTR_ERR(i2c->clk);
12727e086c3fSDavid Wu 		if (ret != -EPROBE_DEFER)
12737e086c3fSDavid Wu 			dev_err(&pdev->dev, "Can't get bus clk: %d\n", ret);
12747e086c3fSDavid Wu 		return ret;
12757e086c3fSDavid Wu 	}
12767e086c3fSDavid Wu 	if (IS_ERR(i2c->pclk)) {
12777e086c3fSDavid Wu 		ret = PTR_ERR(i2c->pclk);
12787e086c3fSDavid Wu 		if (ret != -EPROBE_DEFER)
12797e086c3fSDavid Wu 			dev_err(&pdev->dev, "Can't get periph clk: %d\n", ret);
12807e086c3fSDavid Wu 		return ret;
12817e086c3fSDavid Wu 	}
12827e086c3fSDavid Wu 
1283c41aa3ceSMax Schwarz 	ret = clk_prepare(i2c->clk);
1284c41aa3ceSMax Schwarz 	if (ret < 0) {
12857e086c3fSDavid Wu 		dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
1286c41aa3ceSMax Schwarz 		return ret;
1287c41aa3ceSMax Schwarz 	}
12887e086c3fSDavid Wu 	ret = clk_prepare(i2c->pclk);
12897e086c3fSDavid Wu 	if (ret < 0) {
12907e086c3fSDavid Wu 		dev_err(&pdev->dev, "Can't prepare periph clock: %d\n", ret);
12917e086c3fSDavid Wu 		goto err_clk;
12927e086c3fSDavid Wu 	}
1293c41aa3ceSMax Schwarz 
1294249051f4SMax Schwarz 	i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
1295249051f4SMax Schwarz 	ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
1296249051f4SMax Schwarz 	if (ret != 0) {
1297249051f4SMax Schwarz 		dev_err(&pdev->dev, "Unable to register clock notifier\n");
12987e086c3fSDavid Wu 		goto err_pclk;
1299249051f4SMax Schwarz 	}
1300249051f4SMax Schwarz 
1301249051f4SMax Schwarz 	clk_rate = clk_get_rate(i2c->clk);
1302249051f4SMax Schwarz 	rk3x_i2c_adapt_div(i2c, clk_rate);
1303249051f4SMax Schwarz 
1304c41aa3ceSMax Schwarz 	ret = i2c_add_adapter(&i2c->adap);
1305ea734404SWolfram Sang 	if (ret < 0)
1306249051f4SMax Schwarz 		goto err_clk_notifier;
1307c41aa3ceSMax Schwarz 
1308c41aa3ceSMax Schwarz 	dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
1309c41aa3ceSMax Schwarz 
1310c41aa3ceSMax Schwarz 	return 0;
1311c41aa3ceSMax Schwarz 
1312249051f4SMax Schwarz err_clk_notifier:
1313249051f4SMax Schwarz 	clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
13147e086c3fSDavid Wu err_pclk:
13157e086c3fSDavid Wu 	clk_unprepare(i2c->pclk);
1316c41aa3ceSMax Schwarz err_clk:
1317c41aa3ceSMax Schwarz 	clk_unprepare(i2c->clk);
1318c41aa3ceSMax Schwarz 	return ret;
1319c41aa3ceSMax Schwarz }
1320c41aa3ceSMax Schwarz 
1321c41aa3ceSMax Schwarz static int rk3x_i2c_remove(struct platform_device *pdev)
1322c41aa3ceSMax Schwarz {
1323c41aa3ceSMax Schwarz 	struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
1324c41aa3ceSMax Schwarz 
1325c41aa3ceSMax Schwarz 	i2c_del_adapter(&i2c->adap);
1326249051f4SMax Schwarz 
1327249051f4SMax Schwarz 	clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
13287e086c3fSDavid Wu 	clk_unprepare(i2c->pclk);
1329c41aa3ceSMax Schwarz 	clk_unprepare(i2c->clk);
1330c41aa3ceSMax Schwarz 
1331c41aa3ceSMax Schwarz 	return 0;
1332c41aa3ceSMax Schwarz }
1333c41aa3ceSMax Schwarz 
1334c41aa3ceSMax Schwarz static struct platform_driver rk3x_i2c_driver = {
1335c41aa3ceSMax Schwarz 	.probe   = rk3x_i2c_probe,
1336c41aa3ceSMax Schwarz 	.remove  = rk3x_i2c_remove,
1337c41aa3ceSMax Schwarz 	.driver  = {
1338c41aa3ceSMax Schwarz 		.name  = "rk3x-i2c",
1339c41aa3ceSMax Schwarz 		.of_match_table = rk3x_i2c_match,
1340c41aa3ceSMax Schwarz 	},
1341c41aa3ceSMax Schwarz };
1342c41aa3ceSMax Schwarz 
1343c41aa3ceSMax Schwarz module_platform_driver(rk3x_i2c_driver);
1344c41aa3ceSMax Schwarz 
1345c41aa3ceSMax Schwarz MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
1346c41aa3ceSMax Schwarz MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
1347c41aa3ceSMax Schwarz MODULE_LICENSE("GPL v2");
1348