xref: /openbmc/linux/drivers/i2c/busses/i2c-rk3x.c (revision 4489750fa56e5c2ed49b3bf3590c0bfdab1a8a08)
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 
61c41aa3ceSMax Schwarz /* REG_MRXADDR bits */
62c41aa3ceSMax Schwarz #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
63c41aa3ceSMax Schwarz 
64c41aa3ceSMax Schwarz /* REG_IEN/REG_IPD bits */
65c41aa3ceSMax Schwarz #define REG_INT_BTF       BIT(0) /* a byte was transmitted */
66c41aa3ceSMax Schwarz #define REG_INT_BRF       BIT(1) /* a byte was received */
67c41aa3ceSMax Schwarz #define REG_INT_MBTF      BIT(2) /* master data transmit finished */
68c41aa3ceSMax Schwarz #define REG_INT_MBRF      BIT(3) /* master data receive finished */
69c41aa3ceSMax Schwarz #define REG_INT_START     BIT(4) /* START condition generated */
70c41aa3ceSMax Schwarz #define REG_INT_STOP      BIT(5) /* STOP condition generated */
71c41aa3ceSMax Schwarz #define REG_INT_NAKRCV    BIT(6) /* NACK received */
72c41aa3ceSMax Schwarz #define REG_INT_ALL       0x7f
73c41aa3ceSMax Schwarz 
74c41aa3ceSMax Schwarz /* Constants */
75*4489750fSDoug Anderson #define WAIT_TIMEOUT      1000 /* ms */
76c41aa3ceSMax Schwarz #define DEFAULT_SCL_RATE  (100 * 1000) /* Hz */
77c41aa3ceSMax Schwarz 
78c41aa3ceSMax Schwarz enum rk3x_i2c_state {
79c41aa3ceSMax Schwarz 	STATE_IDLE,
80c41aa3ceSMax Schwarz 	STATE_START,
81c41aa3ceSMax Schwarz 	STATE_READ,
82c41aa3ceSMax Schwarz 	STATE_WRITE,
83c41aa3ceSMax Schwarz 	STATE_STOP
84c41aa3ceSMax Schwarz };
85c41aa3ceSMax Schwarz 
86c41aa3ceSMax Schwarz /**
87c41aa3ceSMax Schwarz  * @grf_offset: offset inside the grf regmap for setting the i2c type
88c41aa3ceSMax Schwarz  */
89c41aa3ceSMax Schwarz struct rk3x_i2c_soc_data {
90c41aa3ceSMax Schwarz 	int grf_offset;
91c41aa3ceSMax Schwarz };
92c41aa3ceSMax Schwarz 
93c41aa3ceSMax Schwarz struct rk3x_i2c {
94c41aa3ceSMax Schwarz 	struct i2c_adapter adap;
95c41aa3ceSMax Schwarz 	struct device *dev;
96c41aa3ceSMax Schwarz 	struct rk3x_i2c_soc_data *soc_data;
97c41aa3ceSMax Schwarz 
98c41aa3ceSMax Schwarz 	/* Hardware resources */
99c41aa3ceSMax Schwarz 	void __iomem *regs;
100c41aa3ceSMax Schwarz 	struct clk *clk;
101249051f4SMax Schwarz 	struct notifier_block clk_rate_nb;
102c41aa3ceSMax Schwarz 
103c41aa3ceSMax Schwarz 	/* Settings */
104c41aa3ceSMax Schwarz 	unsigned int scl_frequency;
105387f0de6SDoug Anderson 	unsigned int scl_rise_ns;
106387f0de6SDoug Anderson 	unsigned int scl_fall_ns;
107387f0de6SDoug Anderson 	unsigned int sda_fall_ns;
108c41aa3ceSMax Schwarz 
109c41aa3ceSMax Schwarz 	/* Synchronization & notification */
110c41aa3ceSMax Schwarz 	spinlock_t lock;
111c41aa3ceSMax Schwarz 	wait_queue_head_t wait;
112c41aa3ceSMax Schwarz 	bool busy;
113c41aa3ceSMax Schwarz 
114c41aa3ceSMax Schwarz 	/* Current message */
115c41aa3ceSMax Schwarz 	struct i2c_msg *msg;
116c41aa3ceSMax Schwarz 	u8 addr;
117c41aa3ceSMax Schwarz 	unsigned int mode;
118c41aa3ceSMax Schwarz 	bool is_last_msg;
119c41aa3ceSMax Schwarz 
120c41aa3ceSMax Schwarz 	/* I2C state machine */
121c41aa3ceSMax Schwarz 	enum rk3x_i2c_state state;
122c41aa3ceSMax Schwarz 	unsigned int processed; /* sent/received bytes */
123c41aa3ceSMax Schwarz 	int error;
124c41aa3ceSMax Schwarz };
125c41aa3ceSMax Schwarz 
126c41aa3ceSMax Schwarz static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
127c41aa3ceSMax Schwarz 			      unsigned int offset)
128c41aa3ceSMax Schwarz {
129c41aa3ceSMax Schwarz 	writel(value, i2c->regs + offset);
130c41aa3ceSMax Schwarz }
131c41aa3ceSMax Schwarz 
132c41aa3ceSMax Schwarz static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
133c41aa3ceSMax Schwarz {
134c41aa3ceSMax Schwarz 	return readl(i2c->regs + offset);
135c41aa3ceSMax Schwarz }
136c41aa3ceSMax Schwarz 
137c41aa3ceSMax Schwarz /* Reset all interrupt pending bits */
138c41aa3ceSMax Schwarz static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
139c41aa3ceSMax Schwarz {
140c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_ALL, REG_IPD);
141c41aa3ceSMax Schwarz }
142c41aa3ceSMax Schwarz 
143c41aa3ceSMax Schwarz /**
144c41aa3ceSMax Schwarz  * Generate a START condition, which triggers a REG_INT_START interrupt.
145c41aa3ceSMax Schwarz  */
146c41aa3ceSMax Schwarz static void rk3x_i2c_start(struct rk3x_i2c *i2c)
147c41aa3ceSMax Schwarz {
148c41aa3ceSMax Schwarz 	u32 val;
149c41aa3ceSMax Schwarz 
150c41aa3ceSMax Schwarz 	rk3x_i2c_clean_ipd(i2c);
151c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_START, REG_IEN);
152c41aa3ceSMax Schwarz 
153c41aa3ceSMax Schwarz 	/* enable adapter with correct mode, send START condition */
154c41aa3ceSMax Schwarz 	val = REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
155c41aa3ceSMax Schwarz 
156c41aa3ceSMax Schwarz 	/* if we want to react to NACK, set ACTACK bit */
157c41aa3ceSMax Schwarz 	if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
158c41aa3ceSMax Schwarz 		val |= REG_CON_ACTACK;
159c41aa3ceSMax Schwarz 
160c41aa3ceSMax Schwarz 	i2c_writel(i2c, val, REG_CON);
161c41aa3ceSMax Schwarz }
162c41aa3ceSMax Schwarz 
163c41aa3ceSMax Schwarz /**
164c41aa3ceSMax Schwarz  * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
165c41aa3ceSMax Schwarz  *
166c41aa3ceSMax Schwarz  * @error: Error code to return in rk3x_i2c_xfer
167c41aa3ceSMax Schwarz  */
168c41aa3ceSMax Schwarz static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
169c41aa3ceSMax Schwarz {
170c41aa3ceSMax Schwarz 	unsigned int ctrl;
171c41aa3ceSMax Schwarz 
172c41aa3ceSMax Schwarz 	i2c->processed = 0;
173c41aa3ceSMax Schwarz 	i2c->msg = NULL;
174c41aa3ceSMax Schwarz 	i2c->error = error;
175c41aa3ceSMax Schwarz 
176c41aa3ceSMax Schwarz 	if (i2c->is_last_msg) {
177c41aa3ceSMax Schwarz 		/* Enable stop interrupt */
178c41aa3ceSMax Schwarz 		i2c_writel(i2c, REG_INT_STOP, REG_IEN);
179c41aa3ceSMax Schwarz 
180c41aa3ceSMax Schwarz 		i2c->state = STATE_STOP;
181c41aa3ceSMax Schwarz 
182c41aa3ceSMax Schwarz 		ctrl = i2c_readl(i2c, REG_CON);
183c41aa3ceSMax Schwarz 		ctrl |= REG_CON_STOP;
184c41aa3ceSMax Schwarz 		i2c_writel(i2c, ctrl, REG_CON);
185c41aa3ceSMax Schwarz 	} else {
186c41aa3ceSMax Schwarz 		/* Signal rk3x_i2c_xfer to start the next message. */
187c41aa3ceSMax Schwarz 		i2c->busy = false;
188c41aa3ceSMax Schwarz 		i2c->state = STATE_IDLE;
189c41aa3ceSMax Schwarz 
190c41aa3ceSMax Schwarz 		/*
191c41aa3ceSMax Schwarz 		 * The HW is actually not capable of REPEATED START. But we can
192c41aa3ceSMax Schwarz 		 * get the intended effect by resetting its internal state
193c41aa3ceSMax Schwarz 		 * and issuing an ordinary START.
194c41aa3ceSMax Schwarz 		 */
195c41aa3ceSMax Schwarz 		i2c_writel(i2c, 0, REG_CON);
196c41aa3ceSMax Schwarz 
197c41aa3ceSMax Schwarz 		/* signal that we are finished with the current msg */
198c41aa3ceSMax Schwarz 		wake_up(&i2c->wait);
199c41aa3ceSMax Schwarz 	}
200c41aa3ceSMax Schwarz }
201c41aa3ceSMax Schwarz 
202c41aa3ceSMax Schwarz /**
203c41aa3ceSMax Schwarz  * Setup a read according to i2c->msg
204c41aa3ceSMax Schwarz  */
205c41aa3ceSMax Schwarz static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
206c41aa3ceSMax Schwarz {
207c41aa3ceSMax Schwarz 	unsigned int len = i2c->msg->len - i2c->processed;
208c41aa3ceSMax Schwarz 	u32 con;
209c41aa3ceSMax Schwarz 
210c41aa3ceSMax Schwarz 	con = i2c_readl(i2c, REG_CON);
211c41aa3ceSMax Schwarz 
212c41aa3ceSMax Schwarz 	/*
213c41aa3ceSMax Schwarz 	 * The hw can read up to 32 bytes at a time. If we need more than one
214c41aa3ceSMax Schwarz 	 * chunk, send an ACK after the last byte of the current chunk.
215c41aa3ceSMax Schwarz 	 */
21629209338SDoug Anderson 	if (len > 32) {
217c41aa3ceSMax Schwarz 		len = 32;
218c41aa3ceSMax Schwarz 		con &= ~REG_CON_LASTACK;
219c41aa3ceSMax Schwarz 	} else {
220c41aa3ceSMax Schwarz 		con |= REG_CON_LASTACK;
221c41aa3ceSMax Schwarz 	}
222c41aa3ceSMax Schwarz 
223c41aa3ceSMax Schwarz 	/* make sure we are in plain RX mode if we read a second chunk */
224c41aa3ceSMax Schwarz 	if (i2c->processed != 0) {
225c41aa3ceSMax Schwarz 		con &= ~REG_CON_MOD_MASK;
226c41aa3ceSMax Schwarz 		con |= REG_CON_MOD(REG_CON_MOD_RX);
227c41aa3ceSMax Schwarz 	}
228c41aa3ceSMax Schwarz 
229c41aa3ceSMax Schwarz 	i2c_writel(i2c, con, REG_CON);
230c41aa3ceSMax Schwarz 	i2c_writel(i2c, len, REG_MRXCNT);
231c41aa3ceSMax Schwarz }
232c41aa3ceSMax Schwarz 
233c41aa3ceSMax Schwarz /**
234c41aa3ceSMax Schwarz  * Fill the transmit buffer with data from i2c->msg
235c41aa3ceSMax Schwarz  */
236c41aa3ceSMax Schwarz static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
237c41aa3ceSMax Schwarz {
238c41aa3ceSMax Schwarz 	unsigned int i, j;
239c41aa3ceSMax Schwarz 	u32 cnt = 0;
240c41aa3ceSMax Schwarz 	u32 val;
241c41aa3ceSMax Schwarz 	u8 byte;
242c41aa3ceSMax Schwarz 
243c41aa3ceSMax Schwarz 	for (i = 0; i < 8; ++i) {
244c41aa3ceSMax Schwarz 		val = 0;
245c41aa3ceSMax Schwarz 		for (j = 0; j < 4; ++j) {
246cf27020dSAlexandru M Stan 			if ((i2c->processed == i2c->msg->len) && (cnt != 0))
247c41aa3ceSMax Schwarz 				break;
248c41aa3ceSMax Schwarz 
249c41aa3ceSMax Schwarz 			if (i2c->processed == 0 && cnt == 0)
250c41aa3ceSMax Schwarz 				byte = (i2c->addr & 0x7f) << 1;
251c41aa3ceSMax Schwarz 			else
252c41aa3ceSMax Schwarz 				byte = i2c->msg->buf[i2c->processed++];
253c41aa3ceSMax Schwarz 
254c41aa3ceSMax Schwarz 			val |= byte << (j * 8);
255c41aa3ceSMax Schwarz 			cnt++;
256c41aa3ceSMax Schwarz 		}
257c41aa3ceSMax Schwarz 
258c41aa3ceSMax Schwarz 		i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
259c41aa3ceSMax Schwarz 
260c41aa3ceSMax Schwarz 		if (i2c->processed == i2c->msg->len)
261c41aa3ceSMax Schwarz 			break;
262c41aa3ceSMax Schwarz 	}
263c41aa3ceSMax Schwarz 
264c41aa3ceSMax Schwarz 	i2c_writel(i2c, cnt, REG_MTXCNT);
265c41aa3ceSMax Schwarz }
266c41aa3ceSMax Schwarz 
267c41aa3ceSMax Schwarz 
268c41aa3ceSMax Schwarz /* IRQ handlers for individual states */
269c41aa3ceSMax Schwarz 
270c41aa3ceSMax Schwarz static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
271c41aa3ceSMax Schwarz {
272c41aa3ceSMax Schwarz 	if (!(ipd & REG_INT_START)) {
273c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, -EIO);
274c41aa3ceSMax Schwarz 		dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
275c41aa3ceSMax Schwarz 		rk3x_i2c_clean_ipd(i2c);
276c41aa3ceSMax Schwarz 		return;
277c41aa3ceSMax Schwarz 	}
278c41aa3ceSMax Schwarz 
279c41aa3ceSMax Schwarz 	/* ack interrupt */
280c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_START, REG_IPD);
281c41aa3ceSMax Schwarz 
282c41aa3ceSMax Schwarz 	/* disable start bit */
283c41aa3ceSMax Schwarz 	i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
284c41aa3ceSMax Schwarz 
285c41aa3ceSMax Schwarz 	/* enable appropriate interrupts and transition */
286c41aa3ceSMax Schwarz 	if (i2c->mode == REG_CON_MOD_TX) {
287c41aa3ceSMax Schwarz 		i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
288c41aa3ceSMax Schwarz 		i2c->state = STATE_WRITE;
289c41aa3ceSMax Schwarz 		rk3x_i2c_fill_transmit_buf(i2c);
290c41aa3ceSMax Schwarz 	} else {
291c41aa3ceSMax Schwarz 		/* in any other case, we are going to be reading. */
292c41aa3ceSMax Schwarz 		i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
293c41aa3ceSMax Schwarz 		i2c->state = STATE_READ;
294c41aa3ceSMax Schwarz 		rk3x_i2c_prepare_read(i2c);
295c41aa3ceSMax Schwarz 	}
296c41aa3ceSMax Schwarz }
297c41aa3ceSMax Schwarz 
298c41aa3ceSMax Schwarz static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
299c41aa3ceSMax Schwarz {
300c41aa3ceSMax Schwarz 	if (!(ipd & REG_INT_MBTF)) {
301c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, -EIO);
302c41aa3ceSMax Schwarz 		dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
303c41aa3ceSMax Schwarz 		rk3x_i2c_clean_ipd(i2c);
304c41aa3ceSMax Schwarz 		return;
305c41aa3ceSMax Schwarz 	}
306c41aa3ceSMax Schwarz 
307c41aa3ceSMax Schwarz 	/* ack interrupt */
308c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
309c41aa3ceSMax Schwarz 
310c41aa3ceSMax Schwarz 	/* are we finished? */
311c41aa3ceSMax Schwarz 	if (i2c->processed == i2c->msg->len)
312c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, i2c->error);
313c41aa3ceSMax Schwarz 	else
314c41aa3ceSMax Schwarz 		rk3x_i2c_fill_transmit_buf(i2c);
315c41aa3ceSMax Schwarz }
316c41aa3ceSMax Schwarz 
317c41aa3ceSMax Schwarz static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
318c41aa3ceSMax Schwarz {
319c41aa3ceSMax Schwarz 	unsigned int i;
320c41aa3ceSMax Schwarz 	unsigned int len = i2c->msg->len - i2c->processed;
321c41aa3ceSMax Schwarz 	u32 uninitialized_var(val);
322c41aa3ceSMax Schwarz 	u8 byte;
323c41aa3ceSMax Schwarz 
324c41aa3ceSMax Schwarz 	/* we only care for MBRF here. */
325c41aa3ceSMax Schwarz 	if (!(ipd & REG_INT_MBRF))
326c41aa3ceSMax Schwarz 		return;
327c41aa3ceSMax Schwarz 
328c41aa3ceSMax Schwarz 	/* ack interrupt */
329c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
330c41aa3ceSMax Schwarz 
3315da4309fSaddy ke 	/* Can only handle a maximum of 32 bytes at a time */
3325da4309fSaddy ke 	if (len > 32)
3335da4309fSaddy ke 		len = 32;
3345da4309fSaddy ke 
335c41aa3ceSMax Schwarz 	/* read the data from receive buffer */
336c41aa3ceSMax Schwarz 	for (i = 0; i < len; ++i) {
337c41aa3ceSMax Schwarz 		if (i % 4 == 0)
338c41aa3ceSMax Schwarz 			val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
339c41aa3ceSMax Schwarz 
340c41aa3ceSMax Schwarz 		byte = (val >> ((i % 4) * 8)) & 0xff;
341c41aa3ceSMax Schwarz 		i2c->msg->buf[i2c->processed++] = byte;
342c41aa3ceSMax Schwarz 	}
343c41aa3ceSMax Schwarz 
344c41aa3ceSMax Schwarz 	/* are we finished? */
345c41aa3ceSMax Schwarz 	if (i2c->processed == i2c->msg->len)
346c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, i2c->error);
347c41aa3ceSMax Schwarz 	else
348c41aa3ceSMax Schwarz 		rk3x_i2c_prepare_read(i2c);
349c41aa3ceSMax Schwarz }
350c41aa3ceSMax Schwarz 
351c41aa3ceSMax Schwarz static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
352c41aa3ceSMax Schwarz {
353c41aa3ceSMax Schwarz 	unsigned int con;
354c41aa3ceSMax Schwarz 
355c41aa3ceSMax Schwarz 	if (!(ipd & REG_INT_STOP)) {
356c41aa3ceSMax Schwarz 		rk3x_i2c_stop(i2c, -EIO);
357c41aa3ceSMax Schwarz 		dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
358c41aa3ceSMax Schwarz 		rk3x_i2c_clean_ipd(i2c);
359c41aa3ceSMax Schwarz 		return;
360c41aa3ceSMax Schwarz 	}
361c41aa3ceSMax Schwarz 
362c41aa3ceSMax Schwarz 	/* ack interrupt */
363c41aa3ceSMax Schwarz 	i2c_writel(i2c, REG_INT_STOP, REG_IPD);
364c41aa3ceSMax Schwarz 
365c41aa3ceSMax Schwarz 	/* disable STOP bit */
366c41aa3ceSMax Schwarz 	con = i2c_readl(i2c, REG_CON);
367c41aa3ceSMax Schwarz 	con &= ~REG_CON_STOP;
368c41aa3ceSMax Schwarz 	i2c_writel(i2c, con, REG_CON);
369c41aa3ceSMax Schwarz 
370c41aa3ceSMax Schwarz 	i2c->busy = false;
371c41aa3ceSMax Schwarz 	i2c->state = STATE_IDLE;
372c41aa3ceSMax Schwarz 
373c41aa3ceSMax Schwarz 	/* signal rk3x_i2c_xfer that we are finished */
374c41aa3ceSMax Schwarz 	wake_up(&i2c->wait);
375c41aa3ceSMax Schwarz }
376c41aa3ceSMax Schwarz 
377c41aa3ceSMax Schwarz static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
378c41aa3ceSMax Schwarz {
379c41aa3ceSMax Schwarz 	struct rk3x_i2c *i2c = dev_id;
380c41aa3ceSMax Schwarz 	unsigned int ipd;
381c41aa3ceSMax Schwarz 
382c41aa3ceSMax Schwarz 	spin_lock(&i2c->lock);
383c41aa3ceSMax Schwarz 
384c41aa3ceSMax Schwarz 	ipd = i2c_readl(i2c, REG_IPD);
385c41aa3ceSMax Schwarz 	if (i2c->state == STATE_IDLE) {
386c41aa3ceSMax Schwarz 		dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
387c41aa3ceSMax Schwarz 		rk3x_i2c_clean_ipd(i2c);
388c41aa3ceSMax Schwarz 		goto out;
389c41aa3ceSMax Schwarz 	}
390c41aa3ceSMax Schwarz 
391c41aa3ceSMax Schwarz 	dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
392c41aa3ceSMax Schwarz 
393c41aa3ceSMax Schwarz 	/* Clean interrupt bits we don't care about */
394c41aa3ceSMax Schwarz 	ipd &= ~(REG_INT_BRF | REG_INT_BTF);
395c41aa3ceSMax Schwarz 
396c41aa3ceSMax Schwarz 	if (ipd & REG_INT_NAKRCV) {
397c41aa3ceSMax Schwarz 		/*
398c41aa3ceSMax Schwarz 		 * We got a NACK in the last operation. Depending on whether
399c41aa3ceSMax Schwarz 		 * IGNORE_NAK is set, we have to stop the operation and report
400c41aa3ceSMax Schwarz 		 * an error.
401c41aa3ceSMax Schwarz 		 */
402c41aa3ceSMax Schwarz 		i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
403c41aa3ceSMax Schwarz 
404c41aa3ceSMax Schwarz 		ipd &= ~REG_INT_NAKRCV;
405c41aa3ceSMax Schwarz 
406c41aa3ceSMax Schwarz 		if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
407c41aa3ceSMax Schwarz 			rk3x_i2c_stop(i2c, -ENXIO);
408c41aa3ceSMax Schwarz 	}
409c41aa3ceSMax Schwarz 
410c41aa3ceSMax Schwarz 	/* is there anything left to handle? */
41129209338SDoug Anderson 	if ((ipd & REG_INT_ALL) == 0)
412c41aa3ceSMax Schwarz 		goto out;
413c41aa3ceSMax Schwarz 
414c41aa3ceSMax Schwarz 	switch (i2c->state) {
415c41aa3ceSMax Schwarz 	case STATE_START:
416c41aa3ceSMax Schwarz 		rk3x_i2c_handle_start(i2c, ipd);
417c41aa3ceSMax Schwarz 		break;
418c41aa3ceSMax Schwarz 	case STATE_WRITE:
419c41aa3ceSMax Schwarz 		rk3x_i2c_handle_write(i2c, ipd);
420c41aa3ceSMax Schwarz 		break;
421c41aa3ceSMax Schwarz 	case STATE_READ:
422c41aa3ceSMax Schwarz 		rk3x_i2c_handle_read(i2c, ipd);
423c41aa3ceSMax Schwarz 		break;
424c41aa3ceSMax Schwarz 	case STATE_STOP:
425c41aa3ceSMax Schwarz 		rk3x_i2c_handle_stop(i2c, ipd);
426c41aa3ceSMax Schwarz 		break;
427c41aa3ceSMax Schwarz 	case STATE_IDLE:
428c41aa3ceSMax Schwarz 		break;
429c41aa3ceSMax Schwarz 	}
430c41aa3ceSMax Schwarz 
431c41aa3ceSMax Schwarz out:
432c41aa3ceSMax Schwarz 	spin_unlock(&i2c->lock);
433c41aa3ceSMax Schwarz 	return IRQ_HANDLED;
434c41aa3ceSMax Schwarz }
435c41aa3ceSMax Schwarz 
436249051f4SMax Schwarz /**
437249051f4SMax Schwarz  * Calculate divider values for desired SCL frequency
438249051f4SMax Schwarz  *
439249051f4SMax Schwarz  * @clk_rate: I2C input clock rate
440249051f4SMax Schwarz  * @scl_rate: Desired SCL rate
441387f0de6SDoug Anderson  * @scl_rise_ns: How many ns it takes for SCL to rise.
442387f0de6SDoug Anderson  * @scl_fall_ns: How many ns it takes for SCL to fall.
443387f0de6SDoug Anderson  * @sda_fall_ns: How many ns it takes for SDA to fall.
444249051f4SMax Schwarz  * @div_low: Divider output for low
445249051f4SMax Schwarz  * @div_high: Divider output for high
446249051f4SMax Schwarz  *
447249051f4SMax Schwarz  * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
448249051f4SMax Schwarz  * a best-effort divider value is returned in divs. If the target rate is
449249051f4SMax Schwarz  * too high, we silently use the highest possible rate.
450249051f4SMax Schwarz  */
451249051f4SMax Schwarz static int rk3x_i2c_calc_divs(unsigned long clk_rate, unsigned long scl_rate,
452387f0de6SDoug Anderson 			      unsigned long scl_rise_ns,
453387f0de6SDoug Anderson 			      unsigned long scl_fall_ns,
454387f0de6SDoug Anderson 			      unsigned long sda_fall_ns,
4550285f8f5Saddy ke 			      unsigned long *div_low, unsigned long *div_high)
4560285f8f5Saddy ke {
4571330e291Saddy ke 	unsigned long spec_min_low_ns, spec_min_high_ns;
458387f0de6SDoug Anderson 	unsigned long spec_setup_start, spec_max_data_hold_ns;
4590285f8f5Saddy ke 	unsigned long data_hold_buffer_ns;
4601330e291Saddy ke 
4611330e291Saddy ke 	unsigned long min_low_ns, min_high_ns;
4620285f8f5Saddy ke 	unsigned long max_low_ns, min_total_ns;
4630285f8f5Saddy ke 
464249051f4SMax Schwarz 	unsigned long clk_rate_khz, scl_rate_khz;
4650285f8f5Saddy ke 
4660285f8f5Saddy ke 	unsigned long min_low_div, min_high_div;
4670285f8f5Saddy ke 	unsigned long max_low_div;
4680285f8f5Saddy ke 
4690285f8f5Saddy ke 	unsigned long min_div_for_hold, min_total_div;
4700285f8f5Saddy ke 	unsigned long extra_div, extra_low_div, ideal_low_div;
4710285f8f5Saddy ke 
472249051f4SMax Schwarz 	int ret = 0;
473249051f4SMax Schwarz 
4740285f8f5Saddy ke 	/* Only support standard-mode and fast-mode */
4750285f8f5Saddy ke 	if (WARN_ON(scl_rate > 400000))
4760285f8f5Saddy ke 		scl_rate = 400000;
4770285f8f5Saddy ke 
4780285f8f5Saddy ke 	/* prevent scl_rate_khz from becoming 0 */
4790285f8f5Saddy ke 	if (WARN_ON(scl_rate < 1000))
4800285f8f5Saddy ke 		scl_rate = 1000;
4810285f8f5Saddy ke 
4820285f8f5Saddy ke 	/*
4831330e291Saddy ke 	 * min_low_ns:  The minimum number of ns we need to hold low to
4841330e291Saddy ke 	 *		meet I2C specification, should include fall time.
4851330e291Saddy ke 	 * min_high_ns: The minimum number of ns we need to hold high to
4861330e291Saddy ke 	 *		meet I2C specification, should include rise time.
4871330e291Saddy ke 	 * max_low_ns:  The maximum number of ns we can hold low to meet
4881330e291Saddy ke 	 *		I2C specification.
4890285f8f5Saddy ke 	 *
4901330e291Saddy ke 	 * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
4910285f8f5Saddy ke 	 *	 This is because the i2c host on Rockchip holds the data line
4920285f8f5Saddy ke 	 *	 for half the low time.
4930285f8f5Saddy ke 	 */
4940285f8f5Saddy ke 	if (scl_rate <= 100000) {
4951330e291Saddy ke 		/* Standard-mode */
4961330e291Saddy ke 		spec_min_low_ns = 4700;
497387f0de6SDoug Anderson 		spec_setup_start = 4700;
4981330e291Saddy ke 		spec_min_high_ns = 4000;
4991330e291Saddy ke 		spec_max_data_hold_ns = 3450;
5000285f8f5Saddy ke 		data_hold_buffer_ns = 50;
5010285f8f5Saddy ke 	} else {
5021330e291Saddy ke 		/* Fast-mode */
5031330e291Saddy ke 		spec_min_low_ns = 1300;
504387f0de6SDoug Anderson 		spec_setup_start = 600;
5051330e291Saddy ke 		spec_min_high_ns = 600;
5061330e291Saddy ke 		spec_max_data_hold_ns = 900;
5070285f8f5Saddy ke 		data_hold_buffer_ns = 50;
5080285f8f5Saddy ke 	}
509387f0de6SDoug Anderson 	min_high_ns = scl_rise_ns + spec_min_high_ns;
510387f0de6SDoug Anderson 
511387f0de6SDoug Anderson 	/*
512387f0de6SDoug Anderson 	 * Timings for repeated start:
513387f0de6SDoug Anderson 	 * - controller appears to drop SDA at .875x (7/8) programmed clk high.
514387f0de6SDoug Anderson 	 * - controller appears to keep SCL high for 2x programmed clk high.
515387f0de6SDoug Anderson 	 *
516387f0de6SDoug Anderson 	 * We need to account for those rules in picking our "high" time so
517387f0de6SDoug Anderson 	 * we meet tSU;STA and tHD;STA times.
518387f0de6SDoug Anderson 	 */
519387f0de6SDoug Anderson 	min_high_ns = max(min_high_ns,
520387f0de6SDoug Anderson 		DIV_ROUND_UP((scl_rise_ns + spec_setup_start) * 1000, 875));
521387f0de6SDoug Anderson 	min_high_ns = max(min_high_ns,
522387f0de6SDoug Anderson 		DIV_ROUND_UP((scl_rise_ns + spec_setup_start +
523387f0de6SDoug Anderson 			      sda_fall_ns + spec_min_high_ns), 2));
524387f0de6SDoug Anderson 
525387f0de6SDoug Anderson 	min_low_ns = scl_fall_ns + spec_min_low_ns;
5261330e291Saddy ke 	max_low_ns = spec_max_data_hold_ns * 2 - data_hold_buffer_ns;
5270285f8f5Saddy ke 	min_total_ns = min_low_ns + min_high_ns;
5280285f8f5Saddy ke 
5290285f8f5Saddy ke 	/* Adjust to avoid overflow */
530249051f4SMax Schwarz 	clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
5310285f8f5Saddy ke 	scl_rate_khz = scl_rate / 1000;
5320285f8f5Saddy ke 
5330285f8f5Saddy ke 	/*
5340285f8f5Saddy ke 	 * We need the total div to be >= this number
5350285f8f5Saddy ke 	 * so we don't clock too fast.
5360285f8f5Saddy ke 	 */
537249051f4SMax Schwarz 	min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
5380285f8f5Saddy ke 
5390285f8f5Saddy ke 	/* These are the min dividers needed for min hold times. */
540249051f4SMax Schwarz 	min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
541249051f4SMax Schwarz 	min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
5420285f8f5Saddy ke 	min_div_for_hold = (min_low_div + min_high_div);
5430285f8f5Saddy ke 
5440285f8f5Saddy ke 	/*
5451330e291Saddy ke 	 * This is the maximum divider so we don't go over the maximum.
5461330e291Saddy ke 	 * We don't round up here (we round down) since this is a maximum.
5470285f8f5Saddy ke 	 */
548249051f4SMax Schwarz 	max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
5490285f8f5Saddy ke 
5500285f8f5Saddy ke 	if (min_low_div > max_low_div) {
5510285f8f5Saddy ke 		WARN_ONCE(true,
5520285f8f5Saddy ke 			  "Conflicting, min_low_div %lu, max_low_div %lu\n",
5530285f8f5Saddy ke 			  min_low_div, max_low_div);
5540285f8f5Saddy ke 		max_low_div = min_low_div;
5550285f8f5Saddy ke 	}
5560285f8f5Saddy ke 
5570285f8f5Saddy ke 	if (min_div_for_hold > min_total_div) {
5580285f8f5Saddy ke 		/*
5590285f8f5Saddy ke 		 * Time needed to meet hold requirements is important.
5600285f8f5Saddy ke 		 * Just use that.
5610285f8f5Saddy ke 		 */
5620285f8f5Saddy ke 		*div_low = min_low_div;
5630285f8f5Saddy ke 		*div_high = min_high_div;
5640285f8f5Saddy ke 	} else {
5650285f8f5Saddy ke 		/*
5660285f8f5Saddy ke 		 * We've got to distribute some time among the low and high
5670285f8f5Saddy ke 		 * so we don't run too fast.
5680285f8f5Saddy ke 		 */
5690285f8f5Saddy ke 		extra_div = min_total_div - min_div_for_hold;
5700285f8f5Saddy ke 
5710285f8f5Saddy ke 		/*
5720285f8f5Saddy ke 		 * We'll try to split things up perfectly evenly,
5730285f8f5Saddy ke 		 * biasing slightly towards having a higher div
5740285f8f5Saddy ke 		 * for low (spend more time low).
5750285f8f5Saddy ke 		 */
576249051f4SMax Schwarz 		ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
5770285f8f5Saddy ke 					     scl_rate_khz * 8 * min_total_ns);
5780285f8f5Saddy ke 
5791330e291Saddy ke 		/* Don't allow it to go over the maximum */
5800285f8f5Saddy ke 		if (ideal_low_div > max_low_div)
5810285f8f5Saddy ke 			ideal_low_div = max_low_div;
5820285f8f5Saddy ke 
5830285f8f5Saddy ke 		/*
5840285f8f5Saddy ke 		 * Handle when the ideal low div is going to take up
5850285f8f5Saddy ke 		 * more than we have.
5860285f8f5Saddy ke 		 */
5870285f8f5Saddy ke 		if (ideal_low_div > min_low_div + extra_div)
5880285f8f5Saddy ke 			ideal_low_div = min_low_div + extra_div;
5890285f8f5Saddy ke 
5900285f8f5Saddy ke 		/* Give low the "ideal" and give high whatever extra is left */
5910285f8f5Saddy ke 		extra_low_div = ideal_low_div - min_low_div;
5920285f8f5Saddy ke 		*div_low = ideal_low_div;
5930285f8f5Saddy ke 		*div_high = min_high_div + (extra_div - extra_low_div);
5940285f8f5Saddy ke 	}
5950285f8f5Saddy ke 
5960285f8f5Saddy ke 	/*
5970285f8f5Saddy ke 	 * Adjust to the fact that the hardware has an implicit "+1".
5980285f8f5Saddy ke 	 * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
5990285f8f5Saddy ke 	 */
6000285f8f5Saddy ke 	*div_low = *div_low - 1;
6010285f8f5Saddy ke 	*div_high = *div_high - 1;
6020285f8f5Saddy ke 
603249051f4SMax Schwarz 	/* Maximum divider supported by hw is 0xffff */
604249051f4SMax Schwarz 	if (*div_low > 0xffff) {
605249051f4SMax Schwarz 		*div_low = 0xffff;
606249051f4SMax Schwarz 		ret = -EINVAL;
6070285f8f5Saddy ke 	}
6080285f8f5Saddy ke 
609249051f4SMax Schwarz 	if (*div_high > 0xffff) {
610249051f4SMax Schwarz 		*div_high = 0xffff;
611249051f4SMax Schwarz 		ret = -EINVAL;
612249051f4SMax Schwarz 	}
613249051f4SMax Schwarz 
614249051f4SMax Schwarz 	return ret;
615249051f4SMax Schwarz }
616249051f4SMax Schwarz 
617249051f4SMax Schwarz static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
618c41aa3ceSMax Schwarz {
6190285f8f5Saddy ke 	unsigned long div_low, div_high;
6200285f8f5Saddy ke 	u64 t_low_ns, t_high_ns;
621249051f4SMax Schwarz 	int ret;
622c41aa3ceSMax Schwarz 
623387f0de6SDoug Anderson 	ret = rk3x_i2c_calc_divs(clk_rate, i2c->scl_frequency, i2c->scl_rise_ns,
624387f0de6SDoug Anderson 				 i2c->scl_fall_ns, i2c->sda_fall_ns,
625387f0de6SDoug Anderson 				 &div_low, &div_high);
626249051f4SMax Schwarz 	WARN_ONCE(ret != 0, "Could not reach SCL freq %u", i2c->scl_frequency);
627249051f4SMax Schwarz 
628249051f4SMax Schwarz 	clk_enable(i2c->clk);
6290285f8f5Saddy ke 	i2c_writel(i2c, (div_high << 16) | (div_low & 0xffff), REG_CLKDIV);
630249051f4SMax Schwarz 	clk_disable(i2c->clk);
6310285f8f5Saddy ke 
632249051f4SMax Schwarz 	t_low_ns = div_u64(((u64)div_low + 1) * 8 * 1000000000, clk_rate);
633249051f4SMax Schwarz 	t_high_ns = div_u64(((u64)div_high + 1) * 8 * 1000000000, clk_rate);
6340285f8f5Saddy ke 	dev_dbg(i2c->dev,
635249051f4SMax Schwarz 		"CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
636249051f4SMax Schwarz 		clk_rate / 1000,
637249051f4SMax Schwarz 		1000000000 / i2c->scl_frequency,
6380285f8f5Saddy ke 		t_low_ns, t_high_ns);
639249051f4SMax Schwarz }
6400285f8f5Saddy ke 
641249051f4SMax Schwarz /**
642249051f4SMax Schwarz  * rk3x_i2c_clk_notifier_cb - Clock rate change callback
643249051f4SMax Schwarz  * @nb:		Pointer to notifier block
644249051f4SMax Schwarz  * @event:	Notification reason
645249051f4SMax Schwarz  * @data:	Pointer to notification data object
646249051f4SMax Schwarz  *
647249051f4SMax Schwarz  * The callback checks whether a valid bus frequency can be generated after the
648249051f4SMax Schwarz  * change. If so, the change is acknowledged, otherwise the change is aborted.
649249051f4SMax Schwarz  * New dividers are written to the HW in the pre- or post change notification
650249051f4SMax Schwarz  * depending on the scaling direction.
651249051f4SMax Schwarz  *
652249051f4SMax Schwarz  * Code adapted from i2c-cadence.c.
653249051f4SMax Schwarz  *
654249051f4SMax Schwarz  * Return:	NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
655249051f4SMax Schwarz  *		to acknowedge the change, NOTIFY_DONE if the notification is
656249051f4SMax Schwarz  *		considered irrelevant.
657249051f4SMax Schwarz  */
658249051f4SMax Schwarz static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
659249051f4SMax Schwarz 				    event, void *data)
660249051f4SMax Schwarz {
661249051f4SMax Schwarz 	struct clk_notifier_data *ndata = data;
662249051f4SMax Schwarz 	struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
663249051f4SMax Schwarz 	unsigned long div_low, div_high;
664249051f4SMax Schwarz 
665249051f4SMax Schwarz 	switch (event) {
666249051f4SMax Schwarz 	case PRE_RATE_CHANGE:
667249051f4SMax Schwarz 		if (rk3x_i2c_calc_divs(ndata->new_rate, i2c->scl_frequency,
668387f0de6SDoug Anderson 				       i2c->scl_rise_ns, i2c->scl_fall_ns,
669387f0de6SDoug Anderson 				       i2c->sda_fall_ns,
670387f0de6SDoug Anderson 				       &div_low, &div_high) != 0)
671249051f4SMax Schwarz 			return NOTIFY_STOP;
672249051f4SMax Schwarz 
673249051f4SMax Schwarz 		/* scale up */
674249051f4SMax Schwarz 		if (ndata->new_rate > ndata->old_rate)
675249051f4SMax Schwarz 			rk3x_i2c_adapt_div(i2c, ndata->new_rate);
676249051f4SMax Schwarz 
677249051f4SMax Schwarz 		return NOTIFY_OK;
678249051f4SMax Schwarz 	case POST_RATE_CHANGE:
679249051f4SMax Schwarz 		/* scale down */
680249051f4SMax Schwarz 		if (ndata->new_rate < ndata->old_rate)
681249051f4SMax Schwarz 			rk3x_i2c_adapt_div(i2c, ndata->new_rate);
682249051f4SMax Schwarz 		return NOTIFY_OK;
683249051f4SMax Schwarz 	case ABORT_RATE_CHANGE:
684249051f4SMax Schwarz 		/* scale up */
685249051f4SMax Schwarz 		if (ndata->new_rate > ndata->old_rate)
686249051f4SMax Schwarz 			rk3x_i2c_adapt_div(i2c, ndata->old_rate);
687249051f4SMax Schwarz 		return NOTIFY_OK;
688249051f4SMax Schwarz 	default:
689249051f4SMax Schwarz 		return NOTIFY_DONE;
690249051f4SMax Schwarz 	}
691c41aa3ceSMax Schwarz }
692c41aa3ceSMax Schwarz 
693c41aa3ceSMax Schwarz /**
694c41aa3ceSMax Schwarz  * Setup I2C registers for an I2C operation specified by msgs, num.
695c41aa3ceSMax Schwarz  *
696c41aa3ceSMax Schwarz  * Must be called with i2c->lock held.
697c41aa3ceSMax Schwarz  *
698c41aa3ceSMax Schwarz  * @msgs: I2C msgs to process
699c41aa3ceSMax Schwarz  * @num: Number of msgs
700c41aa3ceSMax Schwarz  *
701c41aa3ceSMax Schwarz  * returns: Number of I2C msgs processed or negative in case of error
702c41aa3ceSMax Schwarz  */
703c41aa3ceSMax Schwarz static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
704c41aa3ceSMax Schwarz {
705c41aa3ceSMax Schwarz 	u32 addr = (msgs[0].addr & 0x7f) << 1;
706c41aa3ceSMax Schwarz 	int ret = 0;
707c41aa3ceSMax Schwarz 
708c41aa3ceSMax Schwarz 	/*
709c41aa3ceSMax Schwarz 	 * The I2C adapter can issue a small (len < 4) write packet before
710c41aa3ceSMax Schwarz 	 * reading. This speeds up SMBus-style register reads.
711c41aa3ceSMax Schwarz 	 * The MRXADDR/MRXRADDR hold the slave address and the slave register
712c41aa3ceSMax Schwarz 	 * address in this case.
713c41aa3ceSMax Schwarz 	 */
714c41aa3ceSMax Schwarz 
715c41aa3ceSMax Schwarz 	if (num >= 2 && msgs[0].len < 4 &&
716c41aa3ceSMax Schwarz 	    !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
717c41aa3ceSMax Schwarz 		u32 reg_addr = 0;
718c41aa3ceSMax Schwarz 		int i;
719c41aa3ceSMax Schwarz 
720c41aa3ceSMax Schwarz 		dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
721c41aa3ceSMax Schwarz 			addr >> 1);
722c41aa3ceSMax Schwarz 
723c41aa3ceSMax Schwarz 		/* Fill MRXRADDR with the register address(es) */
724c41aa3ceSMax Schwarz 		for (i = 0; i < msgs[0].len; ++i) {
725c41aa3ceSMax Schwarz 			reg_addr |= msgs[0].buf[i] << (i * 8);
726c41aa3ceSMax Schwarz 			reg_addr |= REG_MRXADDR_VALID(i);
727c41aa3ceSMax Schwarz 		}
728c41aa3ceSMax Schwarz 
729c41aa3ceSMax Schwarz 		/* msgs[0] is handled by hw. */
730c41aa3ceSMax Schwarz 		i2c->msg = &msgs[1];
731c41aa3ceSMax Schwarz 
732c41aa3ceSMax Schwarz 		i2c->mode = REG_CON_MOD_REGISTER_TX;
733c41aa3ceSMax Schwarz 
734c41aa3ceSMax Schwarz 		i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
735c41aa3ceSMax Schwarz 		i2c_writel(i2c, reg_addr, REG_MRXRADDR);
736c41aa3ceSMax Schwarz 
737c41aa3ceSMax Schwarz 		ret = 2;
738c41aa3ceSMax Schwarz 	} else {
739c41aa3ceSMax Schwarz 		/*
740c41aa3ceSMax Schwarz 		 * We'll have to do it the boring way and process the msgs
741c41aa3ceSMax Schwarz 		 * one-by-one.
742c41aa3ceSMax Schwarz 		 */
743c41aa3ceSMax Schwarz 
744c41aa3ceSMax Schwarz 		if (msgs[0].flags & I2C_M_RD) {
745c41aa3ceSMax Schwarz 			addr |= 1; /* set read bit */
746c41aa3ceSMax Schwarz 
747c41aa3ceSMax Schwarz 			/*
748c41aa3ceSMax Schwarz 			 * We have to transmit the slave addr first. Use
749c41aa3ceSMax Schwarz 			 * MOD_REGISTER_TX for that purpose.
750c41aa3ceSMax Schwarz 			 */
751c41aa3ceSMax Schwarz 			i2c->mode = REG_CON_MOD_REGISTER_TX;
752c41aa3ceSMax Schwarz 			i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
753c41aa3ceSMax Schwarz 				   REG_MRXADDR);
754c41aa3ceSMax Schwarz 			i2c_writel(i2c, 0, REG_MRXRADDR);
755c41aa3ceSMax Schwarz 		} else {
756c41aa3ceSMax Schwarz 			i2c->mode = REG_CON_MOD_TX;
757c41aa3ceSMax Schwarz 		}
758c41aa3ceSMax Schwarz 
759c41aa3ceSMax Schwarz 		i2c->msg = &msgs[0];
760c41aa3ceSMax Schwarz 
761c41aa3ceSMax Schwarz 		ret = 1;
762c41aa3ceSMax Schwarz 	}
763c41aa3ceSMax Schwarz 
764c41aa3ceSMax Schwarz 	i2c->addr = msgs[0].addr;
765c41aa3ceSMax Schwarz 	i2c->busy = true;
766c41aa3ceSMax Schwarz 	i2c->state = STATE_START;
767c41aa3ceSMax Schwarz 	i2c->processed = 0;
768c41aa3ceSMax Schwarz 	i2c->error = 0;
769c41aa3ceSMax Schwarz 
770c41aa3ceSMax Schwarz 	rk3x_i2c_clean_ipd(i2c);
771c41aa3ceSMax Schwarz 
772c41aa3ceSMax Schwarz 	return ret;
773c41aa3ceSMax Schwarz }
774c41aa3ceSMax Schwarz 
775c41aa3ceSMax Schwarz static int rk3x_i2c_xfer(struct i2c_adapter *adap,
776c41aa3ceSMax Schwarz 			 struct i2c_msg *msgs, int num)
777c41aa3ceSMax Schwarz {
778c41aa3ceSMax Schwarz 	struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
779c41aa3ceSMax Schwarz 	unsigned long timeout, flags;
780c41aa3ceSMax Schwarz 	int ret = 0;
781c41aa3ceSMax Schwarz 	int i;
782c41aa3ceSMax Schwarz 
783c41aa3ceSMax Schwarz 	spin_lock_irqsave(&i2c->lock, flags);
784c41aa3ceSMax Schwarz 
785c41aa3ceSMax Schwarz 	clk_enable(i2c->clk);
786c41aa3ceSMax Schwarz 
787c41aa3ceSMax Schwarz 	i2c->is_last_msg = false;
788c41aa3ceSMax Schwarz 
789c41aa3ceSMax Schwarz 	/*
790c41aa3ceSMax Schwarz 	 * Process msgs. We can handle more than one message at once (see
791c41aa3ceSMax Schwarz 	 * rk3x_i2c_setup()).
792c41aa3ceSMax Schwarz 	 */
793c41aa3ceSMax Schwarz 	for (i = 0; i < num; i += ret) {
794c41aa3ceSMax Schwarz 		ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
795c41aa3ceSMax Schwarz 
796c41aa3ceSMax Schwarz 		if (ret < 0) {
797c41aa3ceSMax Schwarz 			dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
798c41aa3ceSMax Schwarz 			break;
799c41aa3ceSMax Schwarz 		}
800c41aa3ceSMax Schwarz 
801c41aa3ceSMax Schwarz 		if (i + ret >= num)
802c41aa3ceSMax Schwarz 			i2c->is_last_msg = true;
803c41aa3ceSMax Schwarz 
804c41aa3ceSMax Schwarz 		spin_unlock_irqrestore(&i2c->lock, flags);
805c41aa3ceSMax Schwarz 
806c41aa3ceSMax Schwarz 		rk3x_i2c_start(i2c);
807c41aa3ceSMax Schwarz 
808c41aa3ceSMax Schwarz 		timeout = wait_event_timeout(i2c->wait, !i2c->busy,
809c41aa3ceSMax Schwarz 					     msecs_to_jiffies(WAIT_TIMEOUT));
810c41aa3ceSMax Schwarz 
811c41aa3ceSMax Schwarz 		spin_lock_irqsave(&i2c->lock, flags);
812c41aa3ceSMax Schwarz 
813c41aa3ceSMax Schwarz 		if (timeout == 0) {
814c41aa3ceSMax Schwarz 			dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
815c41aa3ceSMax Schwarz 				i2c_readl(i2c, REG_IPD), i2c->state);
816c41aa3ceSMax Schwarz 
817c41aa3ceSMax Schwarz 			/* Force a STOP condition without interrupt */
818c41aa3ceSMax Schwarz 			i2c_writel(i2c, 0, REG_IEN);
819c41aa3ceSMax Schwarz 			i2c_writel(i2c, REG_CON_EN | REG_CON_STOP, REG_CON);
820c41aa3ceSMax Schwarz 
821c41aa3ceSMax Schwarz 			i2c->state = STATE_IDLE;
822c41aa3ceSMax Schwarz 
823c41aa3ceSMax Schwarz 			ret = -ETIMEDOUT;
824c41aa3ceSMax Schwarz 			break;
825c41aa3ceSMax Schwarz 		}
826c41aa3ceSMax Schwarz 
827c41aa3ceSMax Schwarz 		if (i2c->error) {
828c41aa3ceSMax Schwarz 			ret = i2c->error;
829c41aa3ceSMax Schwarz 			break;
830c41aa3ceSMax Schwarz 		}
831c41aa3ceSMax Schwarz 	}
832c41aa3ceSMax Schwarz 
833c41aa3ceSMax Schwarz 	clk_disable(i2c->clk);
834c41aa3ceSMax Schwarz 	spin_unlock_irqrestore(&i2c->lock, flags);
835c41aa3ceSMax Schwarz 
836c6cbfb91SDmitry Torokhov 	return ret < 0 ? ret : num;
837c41aa3ceSMax Schwarz }
838c41aa3ceSMax Schwarz 
839c41aa3ceSMax Schwarz static u32 rk3x_i2c_func(struct i2c_adapter *adap)
840c41aa3ceSMax Schwarz {
841c41aa3ceSMax Schwarz 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
842c41aa3ceSMax Schwarz }
843c41aa3ceSMax Schwarz 
844c41aa3ceSMax Schwarz static const struct i2c_algorithm rk3x_i2c_algorithm = {
845c41aa3ceSMax Schwarz 	.master_xfer		= rk3x_i2c_xfer,
846c41aa3ceSMax Schwarz 	.functionality		= rk3x_i2c_func,
847c41aa3ceSMax Schwarz };
848c41aa3ceSMax Schwarz 
849c41aa3ceSMax Schwarz static struct rk3x_i2c_soc_data soc_data[3] = {
850c41aa3ceSMax Schwarz 	{ .grf_offset = 0x154 }, /* rk3066 */
851c41aa3ceSMax Schwarz 	{ .grf_offset = 0x0a4 }, /* rk3188 */
852c41aa3ceSMax Schwarz 	{ .grf_offset = -1 },    /* no I2C switching needed */
853c41aa3ceSMax Schwarz };
854c41aa3ceSMax Schwarz 
855c41aa3ceSMax Schwarz static const struct of_device_id rk3x_i2c_match[] = {
856c41aa3ceSMax Schwarz 	{ .compatible = "rockchip,rk3066-i2c", .data = (void *)&soc_data[0] },
857c41aa3ceSMax Schwarz 	{ .compatible = "rockchip,rk3188-i2c", .data = (void *)&soc_data[1] },
858c41aa3ceSMax Schwarz 	{ .compatible = "rockchip,rk3288-i2c", .data = (void *)&soc_data[2] },
859c51bd6acSDan Carpenter 	{},
860c41aa3ceSMax Schwarz };
861c41aa3ceSMax Schwarz 
862c41aa3ceSMax Schwarz static int rk3x_i2c_probe(struct platform_device *pdev)
863c41aa3ceSMax Schwarz {
864c41aa3ceSMax Schwarz 	struct device_node *np = pdev->dev.of_node;
865c41aa3ceSMax Schwarz 	const struct of_device_id *match;
866c41aa3ceSMax Schwarz 	struct rk3x_i2c *i2c;
867c41aa3ceSMax Schwarz 	struct resource *mem;
868c41aa3ceSMax Schwarz 	int ret = 0;
869c41aa3ceSMax Schwarz 	int bus_nr;
870c41aa3ceSMax Schwarz 	u32 value;
871c41aa3ceSMax Schwarz 	int irq;
872249051f4SMax Schwarz 	unsigned long clk_rate;
873c41aa3ceSMax Schwarz 
874c41aa3ceSMax Schwarz 	i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
875c41aa3ceSMax Schwarz 	if (!i2c)
876c41aa3ceSMax Schwarz 		return -ENOMEM;
877c41aa3ceSMax Schwarz 
878c41aa3ceSMax Schwarz 	match = of_match_node(rk3x_i2c_match, np);
879c41aa3ceSMax Schwarz 	i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
880c41aa3ceSMax Schwarz 
881c41aa3ceSMax Schwarz 	if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
882c41aa3ceSMax Schwarz 				 &i2c->scl_frequency)) {
883c41aa3ceSMax Schwarz 		dev_info(&pdev->dev, "using default SCL frequency: %d\n",
884c41aa3ceSMax Schwarz 			 DEFAULT_SCL_RATE);
885c41aa3ceSMax Schwarz 		i2c->scl_frequency = DEFAULT_SCL_RATE;
886c41aa3ceSMax Schwarz 	}
887c41aa3ceSMax Schwarz 
888c41aa3ceSMax Schwarz 	if (i2c->scl_frequency == 0 || i2c->scl_frequency > 400 * 1000) {
889c41aa3ceSMax Schwarz 		dev_warn(&pdev->dev, "invalid SCL frequency specified.\n");
890c41aa3ceSMax Schwarz 		dev_warn(&pdev->dev, "using default SCL frequency: %d\n",
891c41aa3ceSMax Schwarz 			 DEFAULT_SCL_RATE);
892c41aa3ceSMax Schwarz 		i2c->scl_frequency = DEFAULT_SCL_RATE;
893c41aa3ceSMax Schwarz 	}
894c41aa3ceSMax Schwarz 
8951330e291Saddy ke 	/*
8961330e291Saddy ke 	 * Read rise and fall time from device tree. If not available use
8971330e291Saddy ke 	 * the default maximum timing from the specification.
8981330e291Saddy ke 	 */
8991330e291Saddy ke 	if (of_property_read_u32(pdev->dev.of_node, "i2c-scl-rising-time-ns",
900387f0de6SDoug Anderson 				 &i2c->scl_rise_ns)) {
9011330e291Saddy ke 		if (i2c->scl_frequency <= 100000)
902387f0de6SDoug Anderson 			i2c->scl_rise_ns = 1000;
9031330e291Saddy ke 		else
904387f0de6SDoug Anderson 			i2c->scl_rise_ns = 300;
9051330e291Saddy ke 	}
9061330e291Saddy ke 	if (of_property_read_u32(pdev->dev.of_node, "i2c-scl-falling-time-ns",
907387f0de6SDoug Anderson 				 &i2c->scl_fall_ns))
908387f0de6SDoug Anderson 		i2c->scl_fall_ns = 300;
909387f0de6SDoug Anderson 	if (of_property_read_u32(pdev->dev.of_node, "i2c-sda-falling-time-ns",
910387f0de6SDoug Anderson 				 &i2c->scl_fall_ns))
911387f0de6SDoug Anderson 		i2c->sda_fall_ns = i2c->scl_fall_ns;
9121330e291Saddy ke 
913c41aa3ceSMax Schwarz 	strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
914c41aa3ceSMax Schwarz 	i2c->adap.owner = THIS_MODULE;
915c41aa3ceSMax Schwarz 	i2c->adap.algo = &rk3x_i2c_algorithm;
916c41aa3ceSMax Schwarz 	i2c->adap.retries = 3;
917c41aa3ceSMax Schwarz 	i2c->adap.dev.of_node = np;
918c41aa3ceSMax Schwarz 	i2c->adap.algo_data = i2c;
919c41aa3ceSMax Schwarz 	i2c->adap.dev.parent = &pdev->dev;
920c41aa3ceSMax Schwarz 
921c41aa3ceSMax Schwarz 	i2c->dev = &pdev->dev;
922c41aa3ceSMax Schwarz 
923c41aa3ceSMax Schwarz 	spin_lock_init(&i2c->lock);
924c41aa3ceSMax Schwarz 	init_waitqueue_head(&i2c->wait);
925c41aa3ceSMax Schwarz 
926c41aa3ceSMax Schwarz 	i2c->clk = devm_clk_get(&pdev->dev, NULL);
927c41aa3ceSMax Schwarz 	if (IS_ERR(i2c->clk)) {
928c41aa3ceSMax Schwarz 		dev_err(&pdev->dev, "cannot get clock\n");
929c41aa3ceSMax Schwarz 		return PTR_ERR(i2c->clk);
930c41aa3ceSMax Schwarz 	}
931c41aa3ceSMax Schwarz 
932c41aa3ceSMax Schwarz 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
933c41aa3ceSMax Schwarz 	i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
934c41aa3ceSMax Schwarz 	if (IS_ERR(i2c->regs))
935c41aa3ceSMax Schwarz 		return PTR_ERR(i2c->regs);
936c41aa3ceSMax Schwarz 
937c41aa3ceSMax Schwarz 	/* Try to set the I2C adapter number from dt */
938c41aa3ceSMax Schwarz 	bus_nr = of_alias_get_id(np, "i2c");
939c41aa3ceSMax Schwarz 
940c41aa3ceSMax Schwarz 	/*
941c41aa3ceSMax Schwarz 	 * Switch to new interface if the SoC also offers the old one.
942c41aa3ceSMax Schwarz 	 * The control bit is located in the GRF register space.
943c41aa3ceSMax Schwarz 	 */
944c41aa3ceSMax Schwarz 	if (i2c->soc_data->grf_offset >= 0) {
945c41aa3ceSMax Schwarz 		struct regmap *grf;
946c41aa3ceSMax Schwarz 
947c41aa3ceSMax Schwarz 		grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
948c41aa3ceSMax Schwarz 		if (IS_ERR(grf)) {
949c41aa3ceSMax Schwarz 			dev_err(&pdev->dev,
950c41aa3ceSMax Schwarz 				"rk3x-i2c needs 'rockchip,grf' property\n");
951c41aa3ceSMax Schwarz 			return PTR_ERR(grf);
952c41aa3ceSMax Schwarz 		}
953c41aa3ceSMax Schwarz 
954c41aa3ceSMax Schwarz 		if (bus_nr < 0) {
955c41aa3ceSMax Schwarz 			dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
956c41aa3ceSMax Schwarz 			return -EINVAL;
957c41aa3ceSMax Schwarz 		}
958c41aa3ceSMax Schwarz 
959c41aa3ceSMax Schwarz 		/* 27+i: write mask, 11+i: value */
960c41aa3ceSMax Schwarz 		value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
961c41aa3ceSMax Schwarz 
962c41aa3ceSMax Schwarz 		ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
963c41aa3ceSMax Schwarz 		if (ret != 0) {
964c41aa3ceSMax Schwarz 			dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
965c41aa3ceSMax Schwarz 			return ret;
966c41aa3ceSMax Schwarz 		}
967c41aa3ceSMax Schwarz 	}
968c41aa3ceSMax Schwarz 
969c41aa3ceSMax Schwarz 	/* IRQ setup */
970c41aa3ceSMax Schwarz 	irq = platform_get_irq(pdev, 0);
971c41aa3ceSMax Schwarz 	if (irq < 0) {
972c41aa3ceSMax Schwarz 		dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
973c41aa3ceSMax Schwarz 		return irq;
974c41aa3ceSMax Schwarz 	}
975c41aa3ceSMax Schwarz 
976c41aa3ceSMax Schwarz 	ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
977c41aa3ceSMax Schwarz 			       0, dev_name(&pdev->dev), i2c);
978c41aa3ceSMax Schwarz 	if (ret < 0) {
979c41aa3ceSMax Schwarz 		dev_err(&pdev->dev, "cannot request IRQ\n");
980c41aa3ceSMax Schwarz 		return ret;
981c41aa3ceSMax Schwarz 	}
982c41aa3ceSMax Schwarz 
983c41aa3ceSMax Schwarz 	platform_set_drvdata(pdev, i2c);
984c41aa3ceSMax Schwarz 
985c41aa3ceSMax Schwarz 	ret = clk_prepare(i2c->clk);
986c41aa3ceSMax Schwarz 	if (ret < 0) {
987c41aa3ceSMax Schwarz 		dev_err(&pdev->dev, "Could not prepare clock\n");
988c41aa3ceSMax Schwarz 		return ret;
989c41aa3ceSMax Schwarz 	}
990c41aa3ceSMax Schwarz 
991249051f4SMax Schwarz 	i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
992249051f4SMax Schwarz 	ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
993249051f4SMax Schwarz 	if (ret != 0) {
994249051f4SMax Schwarz 		dev_err(&pdev->dev, "Unable to register clock notifier\n");
995249051f4SMax Schwarz 		goto err_clk;
996249051f4SMax Schwarz 	}
997249051f4SMax Schwarz 
998249051f4SMax Schwarz 	clk_rate = clk_get_rate(i2c->clk);
999249051f4SMax Schwarz 	rk3x_i2c_adapt_div(i2c, clk_rate);
1000249051f4SMax Schwarz 
1001c41aa3ceSMax Schwarz 	ret = i2c_add_adapter(&i2c->adap);
1002c41aa3ceSMax Schwarz 	if (ret < 0) {
1003c41aa3ceSMax Schwarz 		dev_err(&pdev->dev, "Could not register adapter\n");
1004249051f4SMax Schwarz 		goto err_clk_notifier;
1005c41aa3ceSMax Schwarz 	}
1006c41aa3ceSMax Schwarz 
1007c41aa3ceSMax Schwarz 	dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
1008c41aa3ceSMax Schwarz 
1009c41aa3ceSMax Schwarz 	return 0;
1010c41aa3ceSMax Schwarz 
1011249051f4SMax Schwarz err_clk_notifier:
1012249051f4SMax Schwarz 	clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
1013c41aa3ceSMax Schwarz err_clk:
1014c41aa3ceSMax Schwarz 	clk_unprepare(i2c->clk);
1015c41aa3ceSMax Schwarz 	return ret;
1016c41aa3ceSMax Schwarz }
1017c41aa3ceSMax Schwarz 
1018c41aa3ceSMax Schwarz static int rk3x_i2c_remove(struct platform_device *pdev)
1019c41aa3ceSMax Schwarz {
1020c41aa3ceSMax Schwarz 	struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
1021c41aa3ceSMax Schwarz 
1022c41aa3ceSMax Schwarz 	i2c_del_adapter(&i2c->adap);
1023249051f4SMax Schwarz 
1024249051f4SMax Schwarz 	clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
1025c41aa3ceSMax Schwarz 	clk_unprepare(i2c->clk);
1026c41aa3ceSMax Schwarz 
1027c41aa3ceSMax Schwarz 	return 0;
1028c41aa3ceSMax Schwarz }
1029c41aa3ceSMax Schwarz 
1030c41aa3ceSMax Schwarz static struct platform_driver rk3x_i2c_driver = {
1031c41aa3ceSMax Schwarz 	.probe   = rk3x_i2c_probe,
1032c41aa3ceSMax Schwarz 	.remove  = rk3x_i2c_remove,
1033c41aa3ceSMax Schwarz 	.driver  = {
1034c41aa3ceSMax Schwarz 		.name  = "rk3x-i2c",
1035c41aa3ceSMax Schwarz 		.of_match_table = rk3x_i2c_match,
1036c41aa3ceSMax Schwarz 	},
1037c41aa3ceSMax Schwarz };
1038c41aa3ceSMax Schwarz 
1039c41aa3ceSMax Schwarz module_platform_driver(rk3x_i2c_driver);
1040c41aa3ceSMax Schwarz 
1041c41aa3ceSMax Schwarz MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
1042c41aa3ceSMax Schwarz MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
1043c41aa3ceSMax Schwarz MODULE_LICENSE("GPL v2");
1044