1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c41aa3ceSMax Schwarz /*
3c41aa3ceSMax Schwarz * Driver for I2C adapter in Rockchip RK3xxx SoC
4c41aa3ceSMax Schwarz *
5c41aa3ceSMax Schwarz * Max Schwarz <max.schwarz@online.de>
6c41aa3ceSMax Schwarz * based on the patches by Rockchip Inc.
7c41aa3ceSMax Schwarz */
8c41aa3ceSMax Schwarz
9c41aa3ceSMax Schwarz #include <linux/kernel.h>
10c41aa3ceSMax Schwarz #include <linux/module.h>
11c41aa3ceSMax Schwarz #include <linux/i2c.h>
12c41aa3ceSMax Schwarz #include <linux/interrupt.h>
13f3e2bd71SJohn Keeping #include <linux/iopoll.h>
14c41aa3ceSMax Schwarz #include <linux/errno.h>
15c41aa3ceSMax Schwarz #include <linux/err.h>
16c41aa3ceSMax Schwarz #include <linux/platform_device.h>
17c41aa3ceSMax Schwarz #include <linux/io.h>
18c41aa3ceSMax Schwarz #include <linux/of_address.h>
19c41aa3ceSMax Schwarz #include <linux/of_irq.h>
20c41aa3ceSMax Schwarz #include <linux/spinlock.h>
21c41aa3ceSMax Schwarz #include <linux/clk.h>
22c41aa3ceSMax Schwarz #include <linux/wait.h>
23c41aa3ceSMax Schwarz #include <linux/mfd/syscon.h>
24c41aa3ceSMax Schwarz #include <linux/regmap.h>
250285f8f5Saddy ke #include <linux/math64.h>
26c41aa3ceSMax Schwarz
27c41aa3ceSMax Schwarz
28c41aa3ceSMax Schwarz /* Register Map */
29c41aa3ceSMax Schwarz #define REG_CON 0x00 /* control register */
30c41aa3ceSMax Schwarz #define REG_CLKDIV 0x04 /* clock divisor register */
31c41aa3ceSMax Schwarz #define REG_MRXADDR 0x08 /* slave address for REGISTER_TX */
32c41aa3ceSMax Schwarz #define REG_MRXRADDR 0x0c /* slave register address for REGISTER_TX */
33c41aa3ceSMax Schwarz #define REG_MTXCNT 0x10 /* number of bytes to be transmitted */
34c41aa3ceSMax Schwarz #define REG_MRXCNT 0x14 /* number of bytes to be received */
35c41aa3ceSMax Schwarz #define REG_IEN 0x18 /* interrupt enable */
36c41aa3ceSMax Schwarz #define REG_IPD 0x1c /* interrupt pending */
37c41aa3ceSMax Schwarz #define REG_FCNT 0x20 /* finished count */
38c41aa3ceSMax Schwarz
39c41aa3ceSMax Schwarz /* Data buffer offsets */
40c41aa3ceSMax Schwarz #define TXBUFFER_BASE 0x100
41c41aa3ceSMax Schwarz #define RXBUFFER_BASE 0x200
42c41aa3ceSMax Schwarz
43c41aa3ceSMax Schwarz /* REG_CON bits */
44c41aa3ceSMax Schwarz #define REG_CON_EN BIT(0)
45c41aa3ceSMax Schwarz enum {
46c41aa3ceSMax Schwarz REG_CON_MOD_TX = 0, /* transmit data */
47c41aa3ceSMax Schwarz REG_CON_MOD_REGISTER_TX, /* select register and restart */
48c41aa3ceSMax Schwarz REG_CON_MOD_RX, /* receive data */
49c41aa3ceSMax Schwarz REG_CON_MOD_REGISTER_RX, /* broken: transmits read addr AND writes
50c41aa3ceSMax Schwarz * register addr */
51c41aa3ceSMax Schwarz };
52c41aa3ceSMax Schwarz #define REG_CON_MOD(mod) ((mod) << 1)
53c41aa3ceSMax Schwarz #define REG_CON_MOD_MASK (BIT(1) | BIT(2))
54c41aa3ceSMax Schwarz #define REG_CON_START BIT(3)
55c41aa3ceSMax Schwarz #define REG_CON_STOP BIT(4)
56c41aa3ceSMax Schwarz #define REG_CON_LASTACK BIT(5) /* 1: send NACK after last received byte */
57c41aa3ceSMax Schwarz #define REG_CON_ACTACK BIT(6) /* 1: stop if NACK is received */
58c41aa3ceSMax Schwarz
59a8a7d09eSDavid Wu #define REG_CON_TUNING_MASK GENMASK_ULL(15, 8)
607e086c3fSDavid Wu
617e086c3fSDavid Wu #define REG_CON_SDA_CFG(cfg) ((cfg) << 8)
627e086c3fSDavid Wu #define REG_CON_STA_CFG(cfg) ((cfg) << 12)
637e086c3fSDavid Wu #define REG_CON_STO_CFG(cfg) ((cfg) << 14)
647e086c3fSDavid Wu
65c41aa3ceSMax Schwarz /* REG_MRXADDR bits */
66c41aa3ceSMax Schwarz #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
67c41aa3ceSMax Schwarz
68c41aa3ceSMax Schwarz /* REG_IEN/REG_IPD bits */
69c41aa3ceSMax Schwarz #define REG_INT_BTF BIT(0) /* a byte was transmitted */
70c41aa3ceSMax Schwarz #define REG_INT_BRF BIT(1) /* a byte was received */
71c41aa3ceSMax Schwarz #define REG_INT_MBTF BIT(2) /* master data transmit finished */
72c41aa3ceSMax Schwarz #define REG_INT_MBRF BIT(3) /* master data receive finished */
73c41aa3ceSMax Schwarz #define REG_INT_START BIT(4) /* START condition generated */
74c41aa3ceSMax Schwarz #define REG_INT_STOP BIT(5) /* STOP condition generated */
75c41aa3ceSMax Schwarz #define REG_INT_NAKRCV BIT(6) /* NACK received */
76c41aa3ceSMax Schwarz #define REG_INT_ALL 0x7f
77c41aa3ceSMax Schwarz
78c41aa3ceSMax Schwarz /* Constants */
794489750fSDoug Anderson #define WAIT_TIMEOUT 1000 /* ms */
80c41aa3ceSMax Schwarz #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
81c41aa3ceSMax Schwarz
82e26747bfSDavid Wu /**
830582d984SRandy Dunlap * struct i2c_spec_values - I2C specification values for various modes
847e086c3fSDavid Wu * @min_hold_start_ns: min hold time (repeated) START condition
85b58fd3beSDavid Wu * @min_low_ns: min LOW period of the SCL clock
86b58fd3beSDavid Wu * @min_high_ns: min HIGH period of the SCL cloc
87b58fd3beSDavid Wu * @min_setup_start_ns: min set-up time for a repeated START conditio
88b58fd3beSDavid Wu * @max_data_hold_ns: max data hold time
897e086c3fSDavid Wu * @min_data_setup_ns: min data set-up time
907e086c3fSDavid Wu * @min_setup_stop_ns: min set-up time for STOP condition
917e086c3fSDavid Wu * @min_hold_buffer_ns: min bus free time between a STOP and
927e086c3fSDavid Wu * START condition
93b58fd3beSDavid Wu */
94b58fd3beSDavid Wu struct i2c_spec_values {
957e086c3fSDavid Wu unsigned long min_hold_start_ns;
96b58fd3beSDavid Wu unsigned long min_low_ns;
97b58fd3beSDavid Wu unsigned long min_high_ns;
98b58fd3beSDavid Wu unsigned long min_setup_start_ns;
99b58fd3beSDavid Wu unsigned long max_data_hold_ns;
1007e086c3fSDavid Wu unsigned long min_data_setup_ns;
1017e086c3fSDavid Wu unsigned long min_setup_stop_ns;
1027e086c3fSDavid Wu unsigned long min_hold_buffer_ns;
103b58fd3beSDavid Wu };
104b58fd3beSDavid Wu
105b58fd3beSDavid Wu static const struct i2c_spec_values standard_mode_spec = {
1067e086c3fSDavid Wu .min_hold_start_ns = 4000,
107b58fd3beSDavid Wu .min_low_ns = 4700,
108b58fd3beSDavid Wu .min_high_ns = 4000,
109b58fd3beSDavid Wu .min_setup_start_ns = 4700,
110b58fd3beSDavid Wu .max_data_hold_ns = 3450,
1117e086c3fSDavid Wu .min_data_setup_ns = 250,
1127e086c3fSDavid Wu .min_setup_stop_ns = 4000,
1137e086c3fSDavid Wu .min_hold_buffer_ns = 4700,
114b58fd3beSDavid Wu };
115b58fd3beSDavid Wu
116b58fd3beSDavid Wu static const struct i2c_spec_values fast_mode_spec = {
1177e086c3fSDavid Wu .min_hold_start_ns = 600,
118b58fd3beSDavid Wu .min_low_ns = 1300,
119b58fd3beSDavid Wu .min_high_ns = 600,
120b58fd3beSDavid Wu .min_setup_start_ns = 600,
121b58fd3beSDavid Wu .max_data_hold_ns = 900,
1227e086c3fSDavid Wu .min_data_setup_ns = 100,
1237e086c3fSDavid Wu .min_setup_stop_ns = 600,
1247e086c3fSDavid Wu .min_hold_buffer_ns = 1300,
125b58fd3beSDavid Wu };
126b58fd3beSDavid Wu
127a02f3d08SDavid Wu static const struct i2c_spec_values fast_mode_plus_spec = {
128a02f3d08SDavid Wu .min_hold_start_ns = 260,
129a02f3d08SDavid Wu .min_low_ns = 500,
130a02f3d08SDavid Wu .min_high_ns = 260,
131a02f3d08SDavid Wu .min_setup_start_ns = 260,
132a02f3d08SDavid Wu .max_data_hold_ns = 400,
133a02f3d08SDavid Wu .min_data_setup_ns = 50,
134a02f3d08SDavid Wu .min_setup_stop_ns = 260,
135a02f3d08SDavid Wu .min_hold_buffer_ns = 500,
136a02f3d08SDavid Wu };
137a02f3d08SDavid Wu
138b58fd3beSDavid Wu /**
1390582d984SRandy Dunlap * struct rk3x_i2c_calced_timings - calculated V1 timings
140e26747bfSDavid Wu * @div_low: Divider output for low
141e26747bfSDavid Wu * @div_high: Divider output for high
1427e086c3fSDavid Wu * @tuning: Used to adjust setup/hold data time,
1437e086c3fSDavid Wu * setup/hold start time and setup stop time for
1447e086c3fSDavid Wu * v1's calc_timings, the tuning should all be 0
1457e086c3fSDavid Wu * for old hardware anyone using v0's calc_timings.
146e26747bfSDavid Wu */
147e26747bfSDavid Wu struct rk3x_i2c_calced_timings {
148e26747bfSDavid Wu unsigned long div_low;
149e26747bfSDavid Wu unsigned long div_high;
1507e086c3fSDavid Wu unsigned int tuning;
151e26747bfSDavid Wu };
152e26747bfSDavid Wu
153c41aa3ceSMax Schwarz enum rk3x_i2c_state {
154c41aa3ceSMax Schwarz STATE_IDLE,
155c41aa3ceSMax Schwarz STATE_START,
156c41aa3ceSMax Schwarz STATE_READ,
157c41aa3ceSMax Schwarz STATE_WRITE,
158c41aa3ceSMax Schwarz STATE_STOP
159c41aa3ceSMax Schwarz };
160c41aa3ceSMax Schwarz
161c41aa3ceSMax Schwarz /**
1620582d984SRandy Dunlap * struct rk3x_i2c_soc_data - SOC-specific data
163c41aa3ceSMax Schwarz * @grf_offset: offset inside the grf regmap for setting the i2c type
1647e086c3fSDavid Wu * @calc_timings: Callback function for i2c timing information calculated
165c41aa3ceSMax Schwarz */
166c41aa3ceSMax Schwarz struct rk3x_i2c_soc_data {
167c41aa3ceSMax Schwarz int grf_offset;
1687e086c3fSDavid Wu int (*calc_timings)(unsigned long, struct i2c_timings *,
1697e086c3fSDavid Wu struct rk3x_i2c_calced_timings *);
170c41aa3ceSMax Schwarz };
171c41aa3ceSMax Schwarz
1720a6ad2f9SDavid Wu /**
1730a6ad2f9SDavid Wu * struct rk3x_i2c - private data of the controller
1740a6ad2f9SDavid Wu * @adap: corresponding I2C adapter
1750a6ad2f9SDavid Wu * @dev: device for this controller
1760a6ad2f9SDavid Wu * @soc_data: related soc data struct
1770a6ad2f9SDavid Wu * @regs: virtual memory area
1787e086c3fSDavid Wu * @clk: function clk for rk3399 or function & Bus clks for others
1797e086c3fSDavid Wu * @pclk: Bus clk for rk3399
1800a6ad2f9SDavid Wu * @clk_rate_nb: i2c clk rate change notify
1815a358b97SJensen Huang * @irq: irq number
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;
197d032a2ebSJulia Lawall const 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;
2045a358b97SJensen Huang int irq;
205c41aa3ceSMax Schwarz
206c41aa3ceSMax Schwarz /* Settings */
2071ab92956SDavid Wu struct i2c_timings t;
208c41aa3ceSMax Schwarz
209c41aa3ceSMax Schwarz /* Synchronization & notification */
210c41aa3ceSMax Schwarz spinlock_t lock;
211c41aa3ceSMax Schwarz wait_queue_head_t wait;
212c41aa3ceSMax Schwarz bool busy;
213c41aa3ceSMax Schwarz
214c41aa3ceSMax Schwarz /* Current message */
215c41aa3ceSMax Schwarz struct i2c_msg *msg;
216c41aa3ceSMax Schwarz u8 addr;
217c41aa3ceSMax Schwarz unsigned int mode;
218c41aa3ceSMax Schwarz bool is_last_msg;
219c41aa3ceSMax Schwarz
220c41aa3ceSMax Schwarz /* I2C state machine */
221c41aa3ceSMax Schwarz enum rk3x_i2c_state state;
2220a6ad2f9SDavid Wu unsigned int processed;
223c41aa3ceSMax Schwarz int error;
224c41aa3ceSMax Schwarz };
225c41aa3ceSMax Schwarz
i2c_writel(struct rk3x_i2c * i2c,u32 value,unsigned int offset)226c41aa3ceSMax Schwarz static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
227c41aa3ceSMax Schwarz unsigned int offset)
228c41aa3ceSMax Schwarz {
229c41aa3ceSMax Schwarz writel(value, i2c->regs + offset);
230c41aa3ceSMax Schwarz }
231c41aa3ceSMax Schwarz
i2c_readl(struct rk3x_i2c * i2c,unsigned int offset)232c41aa3ceSMax Schwarz static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
233c41aa3ceSMax Schwarz {
234c41aa3ceSMax Schwarz return readl(i2c->regs + offset);
235c41aa3ceSMax Schwarz }
236c41aa3ceSMax Schwarz
237c41aa3ceSMax Schwarz /* Reset all interrupt pending bits */
rk3x_i2c_clean_ipd(struct rk3x_i2c * i2c)238c41aa3ceSMax Schwarz static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
239c41aa3ceSMax Schwarz {
240c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_ALL, REG_IPD);
241c41aa3ceSMax Schwarz }
242c41aa3ceSMax Schwarz
243c41aa3ceSMax Schwarz /**
2440582d984SRandy Dunlap * rk3x_i2c_start - Generate a START condition, which triggers a REG_INT_START interrupt.
2450582d984SRandy Dunlap * @i2c: target controller data
246c41aa3ceSMax Schwarz */
rk3x_i2c_start(struct rk3x_i2c * i2c)247c41aa3ceSMax Schwarz static void rk3x_i2c_start(struct rk3x_i2c *i2c)
248c41aa3ceSMax Schwarz {
2497e086c3fSDavid Wu u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
250c41aa3ceSMax Schwarz
251c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_START, REG_IEN);
252c41aa3ceSMax Schwarz
253c41aa3ceSMax Schwarz /* enable adapter with correct mode, send START condition */
2547e086c3fSDavid Wu val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
255c41aa3ceSMax Schwarz
256c41aa3ceSMax Schwarz /* if we want to react to NACK, set ACTACK bit */
257c41aa3ceSMax Schwarz if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
258c41aa3ceSMax Schwarz val |= REG_CON_ACTACK;
259c41aa3ceSMax Schwarz
260c41aa3ceSMax Schwarz i2c_writel(i2c, val, REG_CON);
261c41aa3ceSMax Schwarz }
262c41aa3ceSMax Schwarz
263c41aa3ceSMax Schwarz /**
2640582d984SRandy Dunlap * rk3x_i2c_stop - Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
2650582d984SRandy Dunlap * @i2c: target controller data
266c41aa3ceSMax Schwarz * @error: Error code to return in rk3x_i2c_xfer
267c41aa3ceSMax Schwarz */
rk3x_i2c_stop(struct rk3x_i2c * i2c,int error)268c41aa3ceSMax Schwarz static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
269c41aa3ceSMax Schwarz {
270c41aa3ceSMax Schwarz unsigned int ctrl;
271c41aa3ceSMax Schwarz
272c41aa3ceSMax Schwarz i2c->processed = 0;
273c41aa3ceSMax Schwarz i2c->msg = NULL;
274c41aa3ceSMax Schwarz i2c->error = error;
275c41aa3ceSMax Schwarz
276c41aa3ceSMax Schwarz if (i2c->is_last_msg) {
277c41aa3ceSMax Schwarz /* Enable stop interrupt */
278c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_STOP, REG_IEN);
279c41aa3ceSMax Schwarz
280c41aa3ceSMax Schwarz i2c->state = STATE_STOP;
281c41aa3ceSMax Schwarz
282c41aa3ceSMax Schwarz ctrl = i2c_readl(i2c, REG_CON);
283c41aa3ceSMax Schwarz ctrl |= REG_CON_STOP;
284c41aa3ceSMax Schwarz i2c_writel(i2c, ctrl, REG_CON);
285c41aa3ceSMax Schwarz } else {
286c41aa3ceSMax Schwarz /* Signal rk3x_i2c_xfer to start the next message. */
287c41aa3ceSMax Schwarz i2c->busy = false;
288c41aa3ceSMax Schwarz i2c->state = STATE_IDLE;
289c41aa3ceSMax Schwarz
290c41aa3ceSMax Schwarz /*
291c41aa3ceSMax Schwarz * The HW is actually not capable of REPEATED START. But we can
292c41aa3ceSMax Schwarz * get the intended effect by resetting its internal state
293c41aa3ceSMax Schwarz * and issuing an ordinary START.
294c41aa3ceSMax Schwarz */
2957e086c3fSDavid Wu ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
2967e086c3fSDavid Wu i2c_writel(i2c, ctrl, REG_CON);
297c41aa3ceSMax Schwarz
298c41aa3ceSMax Schwarz /* signal that we are finished with the current msg */
299c41aa3ceSMax Schwarz wake_up(&i2c->wait);
300c41aa3ceSMax Schwarz }
301c41aa3ceSMax Schwarz }
302c41aa3ceSMax Schwarz
303c41aa3ceSMax Schwarz /**
3040582d984SRandy Dunlap * rk3x_i2c_prepare_read - Setup a read according to i2c->msg
3050582d984SRandy Dunlap * @i2c: target controller data
306c41aa3ceSMax Schwarz */
rk3x_i2c_prepare_read(struct rk3x_i2c * i2c)307c41aa3ceSMax Schwarz static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
308c41aa3ceSMax Schwarz {
309c41aa3ceSMax Schwarz unsigned int len = i2c->msg->len - i2c->processed;
310c41aa3ceSMax Schwarz u32 con;
311c41aa3ceSMax Schwarz
312c41aa3ceSMax Schwarz con = i2c_readl(i2c, REG_CON);
313c41aa3ceSMax Schwarz
314c41aa3ceSMax Schwarz /*
315c41aa3ceSMax Schwarz * The hw can read up to 32 bytes at a time. If we need more than one
316c41aa3ceSMax Schwarz * chunk, send an ACK after the last byte of the current chunk.
317c41aa3ceSMax Schwarz */
31829209338SDoug Anderson if (len > 32) {
319c41aa3ceSMax Schwarz len = 32;
320c41aa3ceSMax Schwarz con &= ~REG_CON_LASTACK;
321c41aa3ceSMax Schwarz } else {
322c41aa3ceSMax Schwarz con |= REG_CON_LASTACK;
323c41aa3ceSMax Schwarz }
324c41aa3ceSMax Schwarz
325c41aa3ceSMax Schwarz /* make sure we are in plain RX mode if we read a second chunk */
326c41aa3ceSMax Schwarz if (i2c->processed != 0) {
327c41aa3ceSMax Schwarz con &= ~REG_CON_MOD_MASK;
328c41aa3ceSMax Schwarz con |= REG_CON_MOD(REG_CON_MOD_RX);
329c41aa3ceSMax Schwarz }
330c41aa3ceSMax Schwarz
331c41aa3ceSMax Schwarz i2c_writel(i2c, con, REG_CON);
332c41aa3ceSMax Schwarz i2c_writel(i2c, len, REG_MRXCNT);
333c41aa3ceSMax Schwarz }
334c41aa3ceSMax Schwarz
335c41aa3ceSMax Schwarz /**
3360582d984SRandy Dunlap * rk3x_i2c_fill_transmit_buf - Fill the transmit buffer with data from i2c->msg
3370582d984SRandy Dunlap * @i2c: target controller data
338c41aa3ceSMax Schwarz */
rk3x_i2c_fill_transmit_buf(struct rk3x_i2c * i2c)339c41aa3ceSMax Schwarz static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
340c41aa3ceSMax Schwarz {
341c41aa3ceSMax Schwarz unsigned int i, j;
342c41aa3ceSMax Schwarz u32 cnt = 0;
343c41aa3ceSMax Schwarz u32 val;
344c41aa3ceSMax Schwarz u8 byte;
345c41aa3ceSMax Schwarz
346c41aa3ceSMax Schwarz for (i = 0; i < 8; ++i) {
347c41aa3ceSMax Schwarz val = 0;
348c41aa3ceSMax Schwarz for (j = 0; j < 4; ++j) {
349cf27020dSAlexandru M Stan if ((i2c->processed == i2c->msg->len) && (cnt != 0))
350c41aa3ceSMax Schwarz break;
351c41aa3ceSMax Schwarz
352c41aa3ceSMax Schwarz if (i2c->processed == 0 && cnt == 0)
353c41aa3ceSMax Schwarz byte = (i2c->addr & 0x7f) << 1;
354c41aa3ceSMax Schwarz else
355c41aa3ceSMax Schwarz byte = i2c->msg->buf[i2c->processed++];
356c41aa3ceSMax Schwarz
357c41aa3ceSMax Schwarz val |= byte << (j * 8);
358c41aa3ceSMax Schwarz cnt++;
359c41aa3ceSMax Schwarz }
360c41aa3ceSMax Schwarz
361c41aa3ceSMax Schwarz i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
362c41aa3ceSMax Schwarz
363c41aa3ceSMax Schwarz if (i2c->processed == i2c->msg->len)
364c41aa3ceSMax Schwarz break;
365c41aa3ceSMax Schwarz }
366c41aa3ceSMax Schwarz
367c41aa3ceSMax Schwarz i2c_writel(i2c, cnt, REG_MTXCNT);
368c41aa3ceSMax Schwarz }
369c41aa3ceSMax Schwarz
370c41aa3ceSMax Schwarz
371c41aa3ceSMax Schwarz /* IRQ handlers for individual states */
372c41aa3ceSMax Schwarz
rk3x_i2c_handle_start(struct rk3x_i2c * i2c,unsigned int ipd)373c41aa3ceSMax Schwarz static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
374c41aa3ceSMax Schwarz {
375c41aa3ceSMax Schwarz if (!(ipd & REG_INT_START)) {
376c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, -EIO);
377c41aa3ceSMax Schwarz dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
378c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c);
379c41aa3ceSMax Schwarz return;
380c41aa3ceSMax Schwarz }
381c41aa3ceSMax Schwarz
382c41aa3ceSMax Schwarz /* ack interrupt */
383c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_START, REG_IPD);
384c41aa3ceSMax Schwarz
385c41aa3ceSMax Schwarz /* disable start bit */
386c41aa3ceSMax Schwarz i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
387c41aa3ceSMax Schwarz
388c41aa3ceSMax Schwarz /* enable appropriate interrupts and transition */
389c41aa3ceSMax Schwarz if (i2c->mode == REG_CON_MOD_TX) {
390c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
391c41aa3ceSMax Schwarz i2c->state = STATE_WRITE;
392c41aa3ceSMax Schwarz rk3x_i2c_fill_transmit_buf(i2c);
393c41aa3ceSMax Schwarz } else {
394c41aa3ceSMax Schwarz /* in any other case, we are going to be reading. */
395c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
396c41aa3ceSMax Schwarz i2c->state = STATE_READ;
397c41aa3ceSMax Schwarz rk3x_i2c_prepare_read(i2c);
398c41aa3ceSMax Schwarz }
399c41aa3ceSMax Schwarz }
400c41aa3ceSMax Schwarz
rk3x_i2c_handle_write(struct rk3x_i2c * i2c,unsigned int ipd)401c41aa3ceSMax Schwarz static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
402c41aa3ceSMax Schwarz {
403c41aa3ceSMax Schwarz if (!(ipd & REG_INT_MBTF)) {
404c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, -EIO);
405c41aa3ceSMax Schwarz dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
406c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c);
407c41aa3ceSMax Schwarz return;
408c41aa3ceSMax Schwarz }
409c41aa3ceSMax Schwarz
410c41aa3ceSMax Schwarz /* ack interrupt */
411c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
412c41aa3ceSMax Schwarz
413c41aa3ceSMax Schwarz /* are we finished? */
414c41aa3ceSMax Schwarz if (i2c->processed == i2c->msg->len)
415c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, i2c->error);
416c41aa3ceSMax Schwarz else
417c41aa3ceSMax Schwarz rk3x_i2c_fill_transmit_buf(i2c);
418c41aa3ceSMax Schwarz }
419c41aa3ceSMax Schwarz
rk3x_i2c_handle_read(struct rk3x_i2c * i2c,unsigned int ipd)420c41aa3ceSMax Schwarz static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
421c41aa3ceSMax Schwarz {
422c41aa3ceSMax Schwarz unsigned int i;
423c41aa3ceSMax Schwarz unsigned int len = i2c->msg->len - i2c->processed;
4243f649ab7SKees Cook u32 val;
425c41aa3ceSMax Schwarz u8 byte;
426c41aa3ceSMax Schwarz
427c41aa3ceSMax Schwarz /* we only care for MBRF here. */
428c41aa3ceSMax Schwarz if (!(ipd & REG_INT_MBRF))
429c41aa3ceSMax Schwarz return;
430c41aa3ceSMax Schwarz
43102fe0fbdSOndrej Jirman /* ack interrupt (read also produces a spurious START flag, clear it too) */
43202fe0fbdSOndrej Jirman i2c_writel(i2c, REG_INT_MBRF | REG_INT_START, REG_IPD);
433c41aa3ceSMax Schwarz
4345da4309fSaddy ke /* Can only handle a maximum of 32 bytes at a time */
4355da4309fSaddy ke if (len > 32)
4365da4309fSaddy ke len = 32;
4375da4309fSaddy ke
438c41aa3ceSMax Schwarz /* read the data from receive buffer */
439c41aa3ceSMax Schwarz for (i = 0; i < len; ++i) {
440c41aa3ceSMax Schwarz if (i % 4 == 0)
441c41aa3ceSMax Schwarz val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
442c41aa3ceSMax Schwarz
443c41aa3ceSMax Schwarz byte = (val >> ((i % 4) * 8)) & 0xff;
444c41aa3ceSMax Schwarz i2c->msg->buf[i2c->processed++] = byte;
445c41aa3ceSMax Schwarz }
446c41aa3ceSMax Schwarz
447c41aa3ceSMax Schwarz /* are we finished? */
448c41aa3ceSMax Schwarz if (i2c->processed == i2c->msg->len)
449c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, i2c->error);
450c41aa3ceSMax Schwarz else
451c41aa3ceSMax Schwarz rk3x_i2c_prepare_read(i2c);
452c41aa3ceSMax Schwarz }
453c41aa3ceSMax Schwarz
rk3x_i2c_handle_stop(struct rk3x_i2c * i2c,unsigned int ipd)454c41aa3ceSMax Schwarz static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
455c41aa3ceSMax Schwarz {
456c41aa3ceSMax Schwarz unsigned int con;
457c41aa3ceSMax Schwarz
458c41aa3ceSMax Schwarz if (!(ipd & REG_INT_STOP)) {
459c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, -EIO);
460c41aa3ceSMax Schwarz dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
461c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c);
462c41aa3ceSMax Schwarz return;
463c41aa3ceSMax Schwarz }
464c41aa3ceSMax Schwarz
465c41aa3ceSMax Schwarz /* ack interrupt */
466c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_STOP, REG_IPD);
467c41aa3ceSMax Schwarz
468c41aa3ceSMax Schwarz /* disable STOP bit */
469c41aa3ceSMax Schwarz con = i2c_readl(i2c, REG_CON);
470c41aa3ceSMax Schwarz con &= ~REG_CON_STOP;
471c41aa3ceSMax Schwarz i2c_writel(i2c, con, REG_CON);
472c41aa3ceSMax Schwarz
473c41aa3ceSMax Schwarz i2c->busy = false;
474c41aa3ceSMax Schwarz i2c->state = STATE_IDLE;
475c41aa3ceSMax Schwarz
476c41aa3ceSMax Schwarz /* signal rk3x_i2c_xfer that we are finished */
477c41aa3ceSMax Schwarz wake_up(&i2c->wait);
478c41aa3ceSMax Schwarz }
479c41aa3ceSMax Schwarz
rk3x_i2c_irq(int irqno,void * dev_id)480c41aa3ceSMax Schwarz static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
481c41aa3ceSMax Schwarz {
482c41aa3ceSMax Schwarz struct rk3x_i2c *i2c = dev_id;
483c41aa3ceSMax Schwarz unsigned int ipd;
484c41aa3ceSMax Schwarz
485c41aa3ceSMax Schwarz spin_lock(&i2c->lock);
486c41aa3ceSMax Schwarz
487c41aa3ceSMax Schwarz ipd = i2c_readl(i2c, REG_IPD);
488c41aa3ceSMax Schwarz if (i2c->state == STATE_IDLE) {
489c41aa3ceSMax Schwarz dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
490c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c);
491c41aa3ceSMax Schwarz goto out;
492c41aa3ceSMax Schwarz }
493c41aa3ceSMax Schwarz
494c41aa3ceSMax Schwarz dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
495c41aa3ceSMax Schwarz
496c41aa3ceSMax Schwarz /* Clean interrupt bits we don't care about */
497c41aa3ceSMax Schwarz ipd &= ~(REG_INT_BRF | REG_INT_BTF);
498c41aa3ceSMax Schwarz
499c41aa3ceSMax Schwarz if (ipd & REG_INT_NAKRCV) {
500c41aa3ceSMax Schwarz /*
501c41aa3ceSMax Schwarz * We got a NACK in the last operation. Depending on whether
502c41aa3ceSMax Schwarz * IGNORE_NAK is set, we have to stop the operation and report
503c41aa3ceSMax Schwarz * an error.
504c41aa3ceSMax Schwarz */
505c41aa3ceSMax Schwarz i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
506c41aa3ceSMax Schwarz
507c41aa3ceSMax Schwarz ipd &= ~REG_INT_NAKRCV;
508c41aa3ceSMax Schwarz
509c41aa3ceSMax Schwarz if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
510c41aa3ceSMax Schwarz rk3x_i2c_stop(i2c, -ENXIO);
511c41aa3ceSMax Schwarz }
512c41aa3ceSMax Schwarz
513c41aa3ceSMax Schwarz /* is there anything left to handle? */
51429209338SDoug Anderson if ((ipd & REG_INT_ALL) == 0)
515c41aa3ceSMax Schwarz goto out;
516c41aa3ceSMax Schwarz
517c41aa3ceSMax Schwarz switch (i2c->state) {
518c41aa3ceSMax Schwarz case STATE_START:
519c41aa3ceSMax Schwarz rk3x_i2c_handle_start(i2c, ipd);
520c41aa3ceSMax Schwarz break;
521c41aa3ceSMax Schwarz case STATE_WRITE:
522c41aa3ceSMax Schwarz rk3x_i2c_handle_write(i2c, ipd);
523c41aa3ceSMax Schwarz break;
524c41aa3ceSMax Schwarz case STATE_READ:
525c41aa3ceSMax Schwarz rk3x_i2c_handle_read(i2c, ipd);
526c41aa3ceSMax Schwarz break;
527c41aa3ceSMax Schwarz case STATE_STOP:
528c41aa3ceSMax Schwarz rk3x_i2c_handle_stop(i2c, ipd);
529c41aa3ceSMax Schwarz break;
530c41aa3ceSMax Schwarz case STATE_IDLE:
531c41aa3ceSMax Schwarz break;
532c41aa3ceSMax Schwarz }
533c41aa3ceSMax Schwarz
534c41aa3ceSMax Schwarz out:
535c41aa3ceSMax Schwarz spin_unlock(&i2c->lock);
536c41aa3ceSMax Schwarz return IRQ_HANDLED;
537c41aa3ceSMax Schwarz }
538c41aa3ceSMax Schwarz
539249051f4SMax Schwarz /**
5400582d984SRandy Dunlap * rk3x_i2c_get_spec - Get timing values of I2C specification
541b58fd3beSDavid Wu * @speed: Desired SCL frequency
542b58fd3beSDavid Wu *
5430582d984SRandy Dunlap * Return: Matched i2c_spec_values.
544b58fd3beSDavid Wu */
rk3x_i2c_get_spec(unsigned int speed)545b58fd3beSDavid Wu static const struct i2c_spec_values *rk3x_i2c_get_spec(unsigned int speed)
546b58fd3beSDavid Wu {
54790224e64SAndy Shevchenko if (speed <= I2C_MAX_STANDARD_MODE_FREQ)
548b58fd3beSDavid Wu return &standard_mode_spec;
54990224e64SAndy Shevchenko else if (speed <= I2C_MAX_FAST_MODE_FREQ)
550b58fd3beSDavid Wu return &fast_mode_spec;
551a02f3d08SDavid Wu else
552a02f3d08SDavid Wu return &fast_mode_plus_spec;
553b58fd3beSDavid Wu }
554b58fd3beSDavid Wu
555b58fd3beSDavid Wu /**
5560582d984SRandy Dunlap * rk3x_i2c_v0_calc_timings - Calculate divider values for desired SCL frequency
557249051f4SMax Schwarz * @clk_rate: I2C input clock rate
558e26747bfSDavid Wu * @t: Known I2C timing information
559e26747bfSDavid Wu * @t_calc: Caculated rk3x private timings that would be written into regs
560249051f4SMax Schwarz *
5610582d984SRandy Dunlap * Return: %0 on success, -%EINVAL if the goal SCL rate is too slow. In that case
562249051f4SMax Schwarz * a best-effort divider value is returned in divs. If the target rate is
563249051f4SMax Schwarz * too high, we silently use the highest possible rate.
564249051f4SMax Schwarz */
rk3x_i2c_v0_calc_timings(unsigned long clk_rate,struct i2c_timings * t,struct rk3x_i2c_calced_timings * t_calc)5657e086c3fSDavid Wu static int rk3x_i2c_v0_calc_timings(unsigned long clk_rate,
5661ab92956SDavid Wu struct i2c_timings *t,
567e26747bfSDavid Wu struct rk3x_i2c_calced_timings *t_calc)
5680285f8f5Saddy ke {
5691330e291Saddy ke unsigned long min_low_ns, min_high_ns;
5700285f8f5Saddy ke unsigned long max_low_ns, min_total_ns;
5710285f8f5Saddy ke
572249051f4SMax Schwarz unsigned long clk_rate_khz, scl_rate_khz;
5730285f8f5Saddy ke
5740285f8f5Saddy ke unsigned long min_low_div, min_high_div;
5750285f8f5Saddy ke unsigned long max_low_div;
5760285f8f5Saddy ke
5770285f8f5Saddy ke unsigned long min_div_for_hold, min_total_div;
5780285f8f5Saddy ke unsigned long extra_div, extra_low_div, ideal_low_div;
5790285f8f5Saddy ke
580b58fd3beSDavid Wu unsigned long data_hold_buffer_ns = 50;
581b58fd3beSDavid Wu const struct i2c_spec_values *spec;
582249051f4SMax Schwarz int ret = 0;
583249051f4SMax Schwarz
5840285f8f5Saddy ke /* Only support standard-mode and fast-mode */
58590224e64SAndy Shevchenko if (WARN_ON(t->bus_freq_hz > I2C_MAX_FAST_MODE_FREQ))
58690224e64SAndy Shevchenko t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ;
5870285f8f5Saddy ke
5880285f8f5Saddy ke /* prevent scl_rate_khz from becoming 0 */
5891ab92956SDavid Wu if (WARN_ON(t->bus_freq_hz < 1000))
5901ab92956SDavid Wu t->bus_freq_hz = 1000;
5910285f8f5Saddy ke
5920285f8f5Saddy ke /*
5931330e291Saddy ke * min_low_ns: The minimum number of ns we need to hold low to
5941330e291Saddy ke * meet I2C specification, should include fall time.
5951330e291Saddy ke * min_high_ns: The minimum number of ns we need to hold high to
5961330e291Saddy ke * meet I2C specification, should include rise time.
5971330e291Saddy ke * max_low_ns: The maximum number of ns we can hold low to meet
5981330e291Saddy ke * I2C specification.
5990285f8f5Saddy ke *
6001330e291Saddy ke * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
6010285f8f5Saddy ke * This is because the i2c host on Rockchip holds the data line
6020285f8f5Saddy ke * for half the low time.
6030285f8f5Saddy ke */
604b58fd3beSDavid Wu spec = rk3x_i2c_get_spec(t->bus_freq_hz);
605b58fd3beSDavid Wu min_high_ns = t->scl_rise_ns + spec->min_high_ns;
606387f0de6SDoug Anderson
607387f0de6SDoug Anderson /*
608387f0de6SDoug Anderson * Timings for repeated start:
609387f0de6SDoug Anderson * - controller appears to drop SDA at .875x (7/8) programmed clk high.
610387f0de6SDoug Anderson * - controller appears to keep SCL high for 2x programmed clk high.
611387f0de6SDoug Anderson *
612387f0de6SDoug Anderson * We need to account for those rules in picking our "high" time so
613387f0de6SDoug Anderson * we meet tSU;STA and tHD;STA times.
614387f0de6SDoug Anderson */
615b58fd3beSDavid Wu min_high_ns = max(min_high_ns, DIV_ROUND_UP(
616b58fd3beSDavid Wu (t->scl_rise_ns + spec->min_setup_start_ns) * 1000, 875));
617b58fd3beSDavid Wu min_high_ns = max(min_high_ns, DIV_ROUND_UP(
618b58fd3beSDavid Wu (t->scl_rise_ns + spec->min_setup_start_ns + t->sda_fall_ns +
619b58fd3beSDavid Wu spec->min_high_ns), 2));
620387f0de6SDoug Anderson
621b58fd3beSDavid Wu min_low_ns = t->scl_fall_ns + spec->min_low_ns;
622b58fd3beSDavid Wu max_low_ns = spec->max_data_hold_ns * 2 - data_hold_buffer_ns;
6230285f8f5Saddy ke min_total_ns = min_low_ns + min_high_ns;
6240285f8f5Saddy ke
6250285f8f5Saddy ke /* Adjust to avoid overflow */
626249051f4SMax Schwarz clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
6271ab92956SDavid Wu scl_rate_khz = t->bus_freq_hz / 1000;
6280285f8f5Saddy ke
6290285f8f5Saddy ke /*
6300285f8f5Saddy ke * We need the total div to be >= this number
6310285f8f5Saddy ke * so we don't clock too fast.
6320285f8f5Saddy ke */
633249051f4SMax Schwarz min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
6340285f8f5Saddy ke
6350285f8f5Saddy ke /* These are the min dividers needed for min hold times. */
636249051f4SMax Schwarz min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
637249051f4SMax Schwarz min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
6380285f8f5Saddy ke min_div_for_hold = (min_low_div + min_high_div);
6390285f8f5Saddy ke
6400285f8f5Saddy ke /*
6411330e291Saddy ke * This is the maximum divider so we don't go over the maximum.
6421330e291Saddy ke * We don't round up here (we round down) since this is a maximum.
6430285f8f5Saddy ke */
644249051f4SMax Schwarz max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
6450285f8f5Saddy ke
6460285f8f5Saddy ke if (min_low_div > max_low_div) {
6470285f8f5Saddy ke WARN_ONCE(true,
6480285f8f5Saddy ke "Conflicting, min_low_div %lu, max_low_div %lu\n",
6490285f8f5Saddy ke min_low_div, max_low_div);
6500285f8f5Saddy ke max_low_div = min_low_div;
6510285f8f5Saddy ke }
6520285f8f5Saddy ke
6530285f8f5Saddy ke if (min_div_for_hold > min_total_div) {
6540285f8f5Saddy ke /*
6550285f8f5Saddy ke * Time needed to meet hold requirements is important.
6560285f8f5Saddy ke * Just use that.
6570285f8f5Saddy ke */
658e26747bfSDavid Wu t_calc->div_low = min_low_div;
659e26747bfSDavid Wu t_calc->div_high = min_high_div;
6600285f8f5Saddy ke } else {
6610285f8f5Saddy ke /*
6620285f8f5Saddy ke * We've got to distribute some time among the low and high
6630285f8f5Saddy ke * so we don't run too fast.
6640285f8f5Saddy ke */
6650285f8f5Saddy ke extra_div = min_total_div - min_div_for_hold;
6660285f8f5Saddy ke
6670285f8f5Saddy ke /*
6680285f8f5Saddy ke * We'll try to split things up perfectly evenly,
6690285f8f5Saddy ke * biasing slightly towards having a higher div
6700285f8f5Saddy ke * for low (spend more time low).
6710285f8f5Saddy ke */
672249051f4SMax Schwarz ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
6730285f8f5Saddy ke scl_rate_khz * 8 * min_total_ns);
6740285f8f5Saddy ke
6751330e291Saddy ke /* Don't allow it to go over the maximum */
6760285f8f5Saddy ke if (ideal_low_div > max_low_div)
6770285f8f5Saddy ke ideal_low_div = max_low_div;
6780285f8f5Saddy ke
6790285f8f5Saddy ke /*
6800285f8f5Saddy ke * Handle when the ideal low div is going to take up
6810285f8f5Saddy ke * more than we have.
6820285f8f5Saddy ke */
6830285f8f5Saddy ke if (ideal_low_div > min_low_div + extra_div)
6840285f8f5Saddy ke ideal_low_div = min_low_div + extra_div;
6850285f8f5Saddy ke
6860285f8f5Saddy ke /* Give low the "ideal" and give high whatever extra is left */
6870285f8f5Saddy ke extra_low_div = ideal_low_div - min_low_div;
688e26747bfSDavid Wu t_calc->div_low = ideal_low_div;
689e26747bfSDavid Wu t_calc->div_high = min_high_div + (extra_div - extra_low_div);
6900285f8f5Saddy ke }
6910285f8f5Saddy ke
6920285f8f5Saddy ke /*
6930285f8f5Saddy ke * Adjust to the fact that the hardware has an implicit "+1".
6940285f8f5Saddy ke * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
6950285f8f5Saddy ke */
696e26747bfSDavid Wu t_calc->div_low--;
697e26747bfSDavid Wu t_calc->div_high--;
6980285f8f5Saddy ke
699399c168aSDavid Wu /* Give the tuning value 0, that would not update con register */
700399c168aSDavid Wu t_calc->tuning = 0;
701249051f4SMax Schwarz /* Maximum divider supported by hw is 0xffff */
702e26747bfSDavid Wu if (t_calc->div_low > 0xffff) {
703e26747bfSDavid Wu t_calc->div_low = 0xffff;
704249051f4SMax Schwarz ret = -EINVAL;
7050285f8f5Saddy ke }
7060285f8f5Saddy ke
707e26747bfSDavid Wu if (t_calc->div_high > 0xffff) {
708e26747bfSDavid Wu t_calc->div_high = 0xffff;
709249051f4SMax Schwarz ret = -EINVAL;
710249051f4SMax Schwarz }
711249051f4SMax Schwarz
712249051f4SMax Schwarz return ret;
713249051f4SMax Schwarz }
714249051f4SMax Schwarz
7157e086c3fSDavid Wu /**
7160582d984SRandy Dunlap * rk3x_i2c_v1_calc_timings - Calculate timing values for desired SCL frequency
7177e086c3fSDavid Wu * @clk_rate: I2C input clock rate
7187e086c3fSDavid Wu * @t: Known I2C timing information
7197e086c3fSDavid Wu * @t_calc: Caculated rk3x private timings that would be written into regs
7207e086c3fSDavid Wu *
7210582d984SRandy Dunlap * Return: %0 on success, -%EINVAL if the goal SCL rate is too slow. In that case
7227e086c3fSDavid Wu * a best-effort divider value is returned in divs. If the target rate is
7237e086c3fSDavid Wu * too high, we silently use the highest possible rate.
7247e086c3fSDavid Wu * The following formulas are v1's method to calculate timings.
7257e086c3fSDavid Wu *
7267e086c3fSDavid Wu * l = divl + 1;
7277e086c3fSDavid Wu * h = divh + 1;
7287e086c3fSDavid Wu * s = sda_update_config + 1;
7297e086c3fSDavid Wu * u = start_setup_config + 1;
7307e086c3fSDavid Wu * p = stop_setup_config + 1;
7317e086c3fSDavid Wu * T = Tclk_i2c;
7327e086c3fSDavid Wu *
7337e086c3fSDavid Wu * tHigh = 8 * h * T;
7347e086c3fSDavid Wu * tLow = 8 * l * T;
7357e086c3fSDavid Wu *
7367e086c3fSDavid Wu * tHD;sda = (l * s + 1) * T;
7377e086c3fSDavid Wu * tSU;sda = [(8 - s) * l + 1] * T;
7387e086c3fSDavid Wu * tI2C = 8 * (l + h) * T;
7397e086c3fSDavid Wu *
7407e086c3fSDavid Wu * tSU;sta = (8h * u + 1) * T;
7417e086c3fSDavid Wu * tHD;sta = [8h * (u + 1) - 1] * T;
7427e086c3fSDavid Wu * tSU;sto = (8h * p + 1) * T;
7437e086c3fSDavid Wu */
rk3x_i2c_v1_calc_timings(unsigned long clk_rate,struct i2c_timings * t,struct rk3x_i2c_calced_timings * t_calc)7447e086c3fSDavid Wu static int rk3x_i2c_v1_calc_timings(unsigned long clk_rate,
7457e086c3fSDavid Wu struct i2c_timings *t,
7467e086c3fSDavid Wu struct rk3x_i2c_calced_timings *t_calc)
7477e086c3fSDavid Wu {
74872cf8c56SDavid Wu unsigned long min_low_ns, min_high_ns;
7497e086c3fSDavid Wu unsigned long min_setup_start_ns, min_setup_data_ns;
7507e086c3fSDavid Wu unsigned long min_setup_stop_ns, max_hold_data_ns;
7517e086c3fSDavid Wu
7527e086c3fSDavid Wu unsigned long clk_rate_khz, scl_rate_khz;
7537e086c3fSDavid Wu
7547e086c3fSDavid Wu unsigned long min_low_div, min_high_div;
7557e086c3fSDavid Wu
7567e086c3fSDavid Wu unsigned long min_div_for_hold, min_total_div;
7577e086c3fSDavid Wu unsigned long extra_div, extra_low_div;
7587e086c3fSDavid Wu unsigned long sda_update_cfg, stp_sta_cfg, stp_sto_cfg;
7597e086c3fSDavid Wu
7607e086c3fSDavid Wu const struct i2c_spec_values *spec;
7617e086c3fSDavid Wu int ret = 0;
7627e086c3fSDavid Wu
763a02f3d08SDavid Wu /* Support standard-mode, fast-mode and fast-mode plus */
76490224e64SAndy Shevchenko if (WARN_ON(t->bus_freq_hz > I2C_MAX_FAST_MODE_PLUS_FREQ))
76590224e64SAndy Shevchenko t->bus_freq_hz = I2C_MAX_FAST_MODE_PLUS_FREQ;
7667e086c3fSDavid Wu
7677e086c3fSDavid Wu /* prevent scl_rate_khz from becoming 0 */
7687e086c3fSDavid Wu if (WARN_ON(t->bus_freq_hz < 1000))
7697e086c3fSDavid Wu t->bus_freq_hz = 1000;
7707e086c3fSDavid Wu
7717e086c3fSDavid Wu /*
7727e086c3fSDavid Wu * min_low_ns: The minimum number of ns we need to hold low to
7737e086c3fSDavid Wu * meet I2C specification, should include fall time.
7747e086c3fSDavid Wu * min_high_ns: The minimum number of ns we need to hold high to
7757e086c3fSDavid Wu * meet I2C specification, should include rise time.
7767e086c3fSDavid Wu */
7777e086c3fSDavid Wu spec = rk3x_i2c_get_spec(t->bus_freq_hz);
7787e086c3fSDavid Wu
7797e086c3fSDavid Wu /* calculate min-divh and min-divl */
7807e086c3fSDavid Wu clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
7817e086c3fSDavid Wu scl_rate_khz = t->bus_freq_hz / 1000;
7827e086c3fSDavid Wu min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
7837e086c3fSDavid Wu
7847e086c3fSDavid Wu min_high_ns = t->scl_rise_ns + spec->min_high_ns;
7857e086c3fSDavid Wu min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
7867e086c3fSDavid Wu
7877e086c3fSDavid Wu min_low_ns = t->scl_fall_ns + spec->min_low_ns;
7887e086c3fSDavid Wu min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
7897e086c3fSDavid Wu
7907e086c3fSDavid Wu /*
7917e086c3fSDavid Wu * Final divh and divl must be greater than 0, otherwise the
7927e086c3fSDavid Wu * hardware would not output the i2c clk.
7937e086c3fSDavid Wu */
7947e086c3fSDavid Wu min_high_div = (min_high_div < 1) ? 2 : min_high_div;
7957e086c3fSDavid Wu min_low_div = (min_low_div < 1) ? 2 : min_low_div;
7967e086c3fSDavid Wu
7977e086c3fSDavid Wu /* These are the min dividers needed for min hold times. */
7987e086c3fSDavid Wu min_div_for_hold = (min_low_div + min_high_div);
7997e086c3fSDavid Wu
8007e086c3fSDavid Wu /*
8017e086c3fSDavid Wu * This is the maximum divider so we don't go over the maximum.
8027e086c3fSDavid Wu * We don't round up here (we round down) since this is a maximum.
8037e086c3fSDavid Wu */
8047e086c3fSDavid Wu if (min_div_for_hold >= min_total_div) {
8057e086c3fSDavid Wu /*
8067e086c3fSDavid Wu * Time needed to meet hold requirements is important.
8077e086c3fSDavid Wu * Just use that.
8087e086c3fSDavid Wu */
8097e086c3fSDavid Wu t_calc->div_low = min_low_div;
8107e086c3fSDavid Wu t_calc->div_high = min_high_div;
8117e086c3fSDavid Wu } else {
8127e086c3fSDavid Wu /*
8137e086c3fSDavid Wu * We've got to distribute some time among the low and high
8147e086c3fSDavid Wu * so we don't run too fast.
8157e086c3fSDavid Wu * We'll try to split things up by the scale of min_low_div and
8167e086c3fSDavid Wu * min_high_div, biasing slightly towards having a higher div
8177e086c3fSDavid Wu * for low (spend more time low).
8187e086c3fSDavid Wu */
8197e086c3fSDavid Wu extra_div = min_total_div - min_div_for_hold;
8207e086c3fSDavid Wu extra_low_div = DIV_ROUND_UP(min_low_div * extra_div,
8217e086c3fSDavid Wu min_div_for_hold);
8227e086c3fSDavid Wu
8237e086c3fSDavid Wu t_calc->div_low = min_low_div + extra_low_div;
8247e086c3fSDavid Wu t_calc->div_high = min_high_div + (extra_div - extra_low_div);
8257e086c3fSDavid Wu }
8267e086c3fSDavid Wu
8277e086c3fSDavid Wu /*
8287e086c3fSDavid Wu * calculate sda data hold count by the rules, data_upd_st:3
8297e086c3fSDavid Wu * is a appropriate value to reduce calculated times.
8307e086c3fSDavid Wu */
8317e086c3fSDavid Wu for (sda_update_cfg = 3; sda_update_cfg > 0; sda_update_cfg--) {
8327e086c3fSDavid Wu max_hold_data_ns = DIV_ROUND_UP((sda_update_cfg
8337e086c3fSDavid Wu * (t_calc->div_low) + 1)
8347e086c3fSDavid Wu * 1000000, clk_rate_khz);
8357e086c3fSDavid Wu min_setup_data_ns = DIV_ROUND_UP(((8 - sda_update_cfg)
8367e086c3fSDavid Wu * (t_calc->div_low) + 1)
8377e086c3fSDavid Wu * 1000000, clk_rate_khz);
8387e086c3fSDavid Wu if ((max_hold_data_ns < spec->max_data_hold_ns) &&
8397e086c3fSDavid Wu (min_setup_data_ns > spec->min_data_setup_ns))
8407e086c3fSDavid Wu break;
8417e086c3fSDavid Wu }
8427e086c3fSDavid Wu
8437e086c3fSDavid Wu /* calculate setup start config */
8447e086c3fSDavid Wu min_setup_start_ns = t->scl_rise_ns + spec->min_setup_start_ns;
8457e086c3fSDavid Wu stp_sta_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_start_ns
8467e086c3fSDavid Wu - 1000000, 8 * 1000000 * (t_calc->div_high));
8477e086c3fSDavid Wu
8487e086c3fSDavid Wu /* calculate setup stop config */
8497e086c3fSDavid Wu min_setup_stop_ns = t->scl_rise_ns + spec->min_setup_stop_ns;
8507e086c3fSDavid Wu stp_sto_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_stop_ns
8517e086c3fSDavid Wu - 1000000, 8 * 1000000 * (t_calc->div_high));
8527e086c3fSDavid Wu
8537e086c3fSDavid Wu t_calc->tuning = REG_CON_SDA_CFG(--sda_update_cfg) |
8547e086c3fSDavid Wu REG_CON_STA_CFG(--stp_sta_cfg) |
8557e086c3fSDavid Wu REG_CON_STO_CFG(--stp_sto_cfg);
8567e086c3fSDavid Wu
8577e086c3fSDavid Wu t_calc->div_low--;
8587e086c3fSDavid Wu t_calc->div_high--;
8597e086c3fSDavid Wu
8607e086c3fSDavid Wu /* Maximum divider supported by hw is 0xffff */
8617e086c3fSDavid Wu if (t_calc->div_low > 0xffff) {
8627e086c3fSDavid Wu t_calc->div_low = 0xffff;
8637e086c3fSDavid Wu ret = -EINVAL;
8647e086c3fSDavid Wu }
8657e086c3fSDavid Wu
8667e086c3fSDavid Wu if (t_calc->div_high > 0xffff) {
8677e086c3fSDavid Wu t_calc->div_high = 0xffff;
8687e086c3fSDavid Wu ret = -EINVAL;
8697e086c3fSDavid Wu }
8707e086c3fSDavid Wu
8717e086c3fSDavid Wu return ret;
8727e086c3fSDavid Wu }
8737e086c3fSDavid Wu
rk3x_i2c_adapt_div(struct rk3x_i2c * i2c,unsigned long clk_rate)874249051f4SMax Schwarz static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
875c41aa3ceSMax Schwarz {
8761ab92956SDavid Wu struct i2c_timings *t = &i2c->t;
877e26747bfSDavid Wu struct rk3x_i2c_calced_timings calc;
8780285f8f5Saddy ke u64 t_low_ns, t_high_ns;
8797e086c3fSDavid Wu unsigned long flags;
8807e086c3fSDavid Wu u32 val;
881249051f4SMax Schwarz int ret;
882c41aa3ceSMax Schwarz
8837e086c3fSDavid Wu ret = i2c->soc_data->calc_timings(clk_rate, t, &calc);
8841ab92956SDavid Wu WARN_ONCE(ret != 0, "Could not reach SCL freq %u", t->bus_freq_hz);
885249051f4SMax Schwarz
8867e086c3fSDavid Wu clk_enable(i2c->pclk);
8877e086c3fSDavid Wu
8887e086c3fSDavid Wu spin_lock_irqsave(&i2c->lock, flags);
8897e086c3fSDavid Wu val = i2c_readl(i2c, REG_CON);
8907e086c3fSDavid Wu val &= ~REG_CON_TUNING_MASK;
8917e086c3fSDavid Wu val |= calc.tuning;
8927e086c3fSDavid Wu i2c_writel(i2c, val, REG_CON);
893e26747bfSDavid Wu i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff),
894e26747bfSDavid Wu REG_CLKDIV);
8957e086c3fSDavid Wu spin_unlock_irqrestore(&i2c->lock, flags);
8967e086c3fSDavid Wu
8977e086c3fSDavid Wu clk_disable(i2c->pclk);
8980285f8f5Saddy ke
899e26747bfSDavid Wu t_low_ns = div_u64(((u64)calc.div_low + 1) * 8 * 1000000000, clk_rate);
900e26747bfSDavid Wu t_high_ns = div_u64(((u64)calc.div_high + 1) * 8 * 1000000000,
901e26747bfSDavid Wu clk_rate);
9020285f8f5Saddy ke dev_dbg(i2c->dev,
903249051f4SMax Schwarz "CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
904249051f4SMax Schwarz clk_rate / 1000,
9051ab92956SDavid Wu 1000000000 / t->bus_freq_hz,
9060285f8f5Saddy ke t_low_ns, t_high_ns);
907249051f4SMax Schwarz }
9080285f8f5Saddy ke
909249051f4SMax Schwarz /**
910249051f4SMax Schwarz * rk3x_i2c_clk_notifier_cb - Clock rate change callback
911249051f4SMax Schwarz * @nb: Pointer to notifier block
912249051f4SMax Schwarz * @event: Notification reason
913249051f4SMax Schwarz * @data: Pointer to notification data object
914249051f4SMax Schwarz *
915249051f4SMax Schwarz * The callback checks whether a valid bus frequency can be generated after the
916249051f4SMax Schwarz * change. If so, the change is acknowledged, otherwise the change is aborted.
917249051f4SMax Schwarz * New dividers are written to the HW in the pre- or post change notification
918249051f4SMax Schwarz * depending on the scaling direction.
919249051f4SMax Schwarz *
920249051f4SMax Schwarz * Code adapted from i2c-cadence.c.
921249051f4SMax Schwarz *
922249051f4SMax Schwarz * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
923e0603c8dSGeert Uytterhoeven * to acknowledge the change, NOTIFY_DONE if the notification is
924249051f4SMax Schwarz * considered irrelevant.
925249051f4SMax Schwarz */
rk3x_i2c_clk_notifier_cb(struct notifier_block * nb,unsigned long event,void * data)926249051f4SMax Schwarz static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
927249051f4SMax Schwarz event, void *data)
928249051f4SMax Schwarz {
929249051f4SMax Schwarz struct clk_notifier_data *ndata = data;
930249051f4SMax Schwarz struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
931e26747bfSDavid Wu struct rk3x_i2c_calced_timings calc;
932249051f4SMax Schwarz
933249051f4SMax Schwarz switch (event) {
934249051f4SMax Schwarz case PRE_RATE_CHANGE:
9357e086c3fSDavid Wu /*
9367e086c3fSDavid Wu * Try the calculation (but don't store the result) ahead of
9377e086c3fSDavid Wu * time to see if we need to block the clock change. Timings
9387e086c3fSDavid Wu * shouldn't actually take effect until rk3x_i2c_adapt_div().
9397e086c3fSDavid Wu */
9407e086c3fSDavid Wu if (i2c->soc_data->calc_timings(ndata->new_rate, &i2c->t,
9417e086c3fSDavid Wu &calc) != 0)
942249051f4SMax Schwarz return NOTIFY_STOP;
943249051f4SMax Schwarz
944249051f4SMax Schwarz /* scale up */
945249051f4SMax Schwarz if (ndata->new_rate > ndata->old_rate)
946249051f4SMax Schwarz rk3x_i2c_adapt_div(i2c, ndata->new_rate);
947249051f4SMax Schwarz
948249051f4SMax Schwarz return NOTIFY_OK;
949249051f4SMax Schwarz case POST_RATE_CHANGE:
950249051f4SMax Schwarz /* scale down */
951249051f4SMax Schwarz if (ndata->new_rate < ndata->old_rate)
952249051f4SMax Schwarz rk3x_i2c_adapt_div(i2c, ndata->new_rate);
953249051f4SMax Schwarz return NOTIFY_OK;
954249051f4SMax Schwarz case ABORT_RATE_CHANGE:
955249051f4SMax Schwarz /* scale up */
956249051f4SMax Schwarz if (ndata->new_rate > ndata->old_rate)
957249051f4SMax Schwarz rk3x_i2c_adapt_div(i2c, ndata->old_rate);
958249051f4SMax Schwarz return NOTIFY_OK;
959249051f4SMax Schwarz default:
960249051f4SMax Schwarz return NOTIFY_DONE;
961249051f4SMax Schwarz }
962c41aa3ceSMax Schwarz }
963c41aa3ceSMax Schwarz
964c41aa3ceSMax Schwarz /**
9650582d984SRandy Dunlap * rk3x_i2c_setup - Setup I2C registers for an I2C operation specified by msgs, num.
9660582d984SRandy Dunlap * @i2c: target controller data
967c41aa3ceSMax Schwarz * @msgs: I2C msgs to process
968c41aa3ceSMax Schwarz * @num: Number of msgs
969c41aa3ceSMax Schwarz *
9700582d984SRandy Dunlap * Must be called with i2c->lock held.
9710582d984SRandy Dunlap *
9720582d984SRandy Dunlap * Return: Number of I2C msgs processed or negative in case of error
973c41aa3ceSMax Schwarz */
rk3x_i2c_setup(struct rk3x_i2c * i2c,struct i2c_msg * msgs,int num)974c41aa3ceSMax Schwarz static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
975c41aa3ceSMax Schwarz {
976c41aa3ceSMax Schwarz u32 addr = (msgs[0].addr & 0x7f) << 1;
977c41aa3ceSMax Schwarz int ret = 0;
978c41aa3ceSMax Schwarz
979c41aa3ceSMax Schwarz /*
980c41aa3ceSMax Schwarz * The I2C adapter can issue a small (len < 4) write packet before
981c41aa3ceSMax Schwarz * reading. This speeds up SMBus-style register reads.
982c41aa3ceSMax Schwarz * The MRXADDR/MRXRADDR hold the slave address and the slave register
983c41aa3ceSMax Schwarz * address in this case.
984c41aa3ceSMax Schwarz */
985c41aa3ceSMax Schwarz
986c41aa3ceSMax Schwarz if (num >= 2 && msgs[0].len < 4 &&
987c41aa3ceSMax Schwarz !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
988c41aa3ceSMax Schwarz u32 reg_addr = 0;
989c41aa3ceSMax Schwarz int i;
990c41aa3ceSMax Schwarz
991c41aa3ceSMax Schwarz dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
992c41aa3ceSMax Schwarz addr >> 1);
993c41aa3ceSMax Schwarz
994c41aa3ceSMax Schwarz /* Fill MRXRADDR with the register address(es) */
995c41aa3ceSMax Schwarz for (i = 0; i < msgs[0].len; ++i) {
996c41aa3ceSMax Schwarz reg_addr |= msgs[0].buf[i] << (i * 8);
997c41aa3ceSMax Schwarz reg_addr |= REG_MRXADDR_VALID(i);
998c41aa3ceSMax Schwarz }
999c41aa3ceSMax Schwarz
1000c41aa3ceSMax Schwarz /* msgs[0] is handled by hw. */
1001c41aa3ceSMax Schwarz i2c->msg = &msgs[1];
1002c41aa3ceSMax Schwarz
1003c41aa3ceSMax Schwarz i2c->mode = REG_CON_MOD_REGISTER_TX;
1004c41aa3ceSMax Schwarz
1005c41aa3ceSMax Schwarz i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
1006c41aa3ceSMax Schwarz i2c_writel(i2c, reg_addr, REG_MRXRADDR);
1007c41aa3ceSMax Schwarz
1008c41aa3ceSMax Schwarz ret = 2;
1009c41aa3ceSMax Schwarz } else {
1010c41aa3ceSMax Schwarz /*
1011c41aa3ceSMax Schwarz * We'll have to do it the boring way and process the msgs
1012c41aa3ceSMax Schwarz * one-by-one.
1013c41aa3ceSMax Schwarz */
1014c41aa3ceSMax Schwarz
1015c41aa3ceSMax Schwarz if (msgs[0].flags & I2C_M_RD) {
1016c41aa3ceSMax Schwarz addr |= 1; /* set read bit */
1017c41aa3ceSMax Schwarz
1018c41aa3ceSMax Schwarz /*
1019c41aa3ceSMax Schwarz * We have to transmit the slave addr first. Use
1020c41aa3ceSMax Schwarz * MOD_REGISTER_TX for that purpose.
1021c41aa3ceSMax Schwarz */
1022c41aa3ceSMax Schwarz i2c->mode = REG_CON_MOD_REGISTER_TX;
1023c41aa3ceSMax Schwarz i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
1024c41aa3ceSMax Schwarz REG_MRXADDR);
1025c41aa3ceSMax Schwarz i2c_writel(i2c, 0, REG_MRXRADDR);
1026c41aa3ceSMax Schwarz } else {
1027c41aa3ceSMax Schwarz i2c->mode = REG_CON_MOD_TX;
1028c41aa3ceSMax Schwarz }
1029c41aa3ceSMax Schwarz
1030c41aa3ceSMax Schwarz i2c->msg = &msgs[0];
1031c41aa3ceSMax Schwarz
1032c41aa3ceSMax Schwarz ret = 1;
1033c41aa3ceSMax Schwarz }
1034c41aa3ceSMax Schwarz
1035c41aa3ceSMax Schwarz i2c->addr = msgs[0].addr;
1036c41aa3ceSMax Schwarz i2c->busy = true;
1037c41aa3ceSMax Schwarz i2c->state = STATE_START;
1038c41aa3ceSMax Schwarz i2c->processed = 0;
1039c41aa3ceSMax Schwarz i2c->error = 0;
1040c41aa3ceSMax Schwarz
1041c41aa3ceSMax Schwarz rk3x_i2c_clean_ipd(i2c);
1042c41aa3ceSMax Schwarz
1043c41aa3ceSMax Schwarz return ret;
1044c41aa3ceSMax Schwarz }
1045c41aa3ceSMax Schwarz
rk3x_i2c_wait_xfer_poll(struct rk3x_i2c * i2c)1046f3e2bd71SJohn Keeping static int rk3x_i2c_wait_xfer_poll(struct rk3x_i2c *i2c)
1047f3e2bd71SJohn Keeping {
1048f3e2bd71SJohn Keeping ktime_t timeout = ktime_add_ms(ktime_get(), WAIT_TIMEOUT);
1049f3e2bd71SJohn Keeping
1050f3e2bd71SJohn Keeping while (READ_ONCE(i2c->busy) &&
1051f3e2bd71SJohn Keeping ktime_compare(ktime_get(), timeout) < 0) {
1052f3e2bd71SJohn Keeping udelay(5);
1053f3e2bd71SJohn Keeping rk3x_i2c_irq(0, i2c);
1054f3e2bd71SJohn Keeping }
1055f3e2bd71SJohn Keeping
1056f3e2bd71SJohn Keeping return !i2c->busy;
1057f3e2bd71SJohn Keeping }
1058f3e2bd71SJohn Keeping
rk3x_i2c_xfer_common(struct i2c_adapter * adap,struct i2c_msg * msgs,int num,bool polling)1059f3e2bd71SJohn Keeping static int rk3x_i2c_xfer_common(struct i2c_adapter *adap,
1060f3e2bd71SJohn Keeping struct i2c_msg *msgs, int num, bool polling)
1061c41aa3ceSMax Schwarz {
1062c41aa3ceSMax Schwarz struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
1063c41aa3ceSMax Schwarz unsigned long timeout, flags;
10647e086c3fSDavid Wu u32 val;
1065c41aa3ceSMax Schwarz int ret = 0;
1066c41aa3ceSMax Schwarz int i;
1067c41aa3ceSMax Schwarz
1068c41aa3ceSMax Schwarz spin_lock_irqsave(&i2c->lock, flags);
1069c41aa3ceSMax Schwarz
1070c41aa3ceSMax Schwarz clk_enable(i2c->clk);
10717e086c3fSDavid Wu clk_enable(i2c->pclk);
1072c41aa3ceSMax Schwarz
1073c41aa3ceSMax Schwarz i2c->is_last_msg = false;
1074c41aa3ceSMax Schwarz
1075c41aa3ceSMax Schwarz /*
1076c41aa3ceSMax Schwarz * Process msgs. We can handle more than one message at once (see
1077c41aa3ceSMax Schwarz * rk3x_i2c_setup()).
1078c41aa3ceSMax Schwarz */
1079c41aa3ceSMax Schwarz for (i = 0; i < num; i += ret) {
1080c41aa3ceSMax Schwarz ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
1081c41aa3ceSMax Schwarz
1082c41aa3ceSMax Schwarz if (ret < 0) {
1083c41aa3ceSMax Schwarz dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
1084c41aa3ceSMax Schwarz break;
1085c41aa3ceSMax Schwarz }
1086c41aa3ceSMax Schwarz
1087c41aa3ceSMax Schwarz if (i + ret >= num)
1088c41aa3ceSMax Schwarz i2c->is_last_msg = true;
1089c41aa3ceSMax Schwarz
1090c41aa3ceSMax Schwarz spin_unlock_irqrestore(&i2c->lock, flags);
1091c41aa3ceSMax Schwarz
10925a358b97SJensen Huang if (!polling) {
1093c41aa3ceSMax Schwarz rk3x_i2c_start(i2c);
1094c41aa3ceSMax Schwarz
1095c41aa3ceSMax Schwarz timeout = wait_event_timeout(i2c->wait, !i2c->busy,
1096c41aa3ceSMax Schwarz msecs_to_jiffies(WAIT_TIMEOUT));
1097f3e2bd71SJohn Keeping } else {
10985a358b97SJensen Huang disable_irq(i2c->irq);
10995a358b97SJensen Huang rk3x_i2c_start(i2c);
11005a358b97SJensen Huang
1101f3e2bd71SJohn Keeping timeout = rk3x_i2c_wait_xfer_poll(i2c);
11025a358b97SJensen Huang
11035a358b97SJensen Huang enable_irq(i2c->irq);
1104f3e2bd71SJohn Keeping }
1105c41aa3ceSMax Schwarz
1106c41aa3ceSMax Schwarz spin_lock_irqsave(&i2c->lock, flags);
1107c41aa3ceSMax Schwarz
1108c41aa3ceSMax Schwarz if (timeout == 0) {
1109c41aa3ceSMax Schwarz dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
1110c41aa3ceSMax Schwarz i2c_readl(i2c, REG_IPD), i2c->state);
1111c41aa3ceSMax Schwarz
1112c41aa3ceSMax Schwarz /* Force a STOP condition without interrupt */
1113c41aa3ceSMax Schwarz i2c_writel(i2c, 0, REG_IEN);
11147e086c3fSDavid Wu val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
11157e086c3fSDavid Wu val |= REG_CON_EN | REG_CON_STOP;
11167e086c3fSDavid Wu i2c_writel(i2c, val, REG_CON);
1117c41aa3ceSMax Schwarz
1118c41aa3ceSMax Schwarz i2c->state = STATE_IDLE;
1119c41aa3ceSMax Schwarz
1120c41aa3ceSMax Schwarz ret = -ETIMEDOUT;
1121c41aa3ceSMax Schwarz break;
1122c41aa3ceSMax Schwarz }
1123c41aa3ceSMax Schwarz
1124c41aa3ceSMax Schwarz if (i2c->error) {
1125c41aa3ceSMax Schwarz ret = i2c->error;
1126c41aa3ceSMax Schwarz break;
1127c41aa3ceSMax Schwarz }
1128c41aa3ceSMax Schwarz }
1129c41aa3ceSMax Schwarz
11307e086c3fSDavid Wu clk_disable(i2c->pclk);
1131c41aa3ceSMax Schwarz clk_disable(i2c->clk);
11327e086c3fSDavid Wu
1133c41aa3ceSMax Schwarz spin_unlock_irqrestore(&i2c->lock, flags);
1134c41aa3ceSMax Schwarz
1135c6cbfb91SDmitry Torokhov return ret < 0 ? ret : num;
1136c41aa3ceSMax Schwarz }
1137c41aa3ceSMax Schwarz
rk3x_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)1138f3e2bd71SJohn Keeping static int rk3x_i2c_xfer(struct i2c_adapter *adap,
1139f3e2bd71SJohn Keeping struct i2c_msg *msgs, int num)
1140f3e2bd71SJohn Keeping {
1141f3e2bd71SJohn Keeping return rk3x_i2c_xfer_common(adap, msgs, num, false);
1142f3e2bd71SJohn Keeping }
1143f3e2bd71SJohn Keeping
rk3x_i2c_xfer_polling(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)1144f3e2bd71SJohn Keeping static int rk3x_i2c_xfer_polling(struct i2c_adapter *adap,
1145f3e2bd71SJohn Keeping struct i2c_msg *msgs, int num)
1146f3e2bd71SJohn Keeping {
1147f3e2bd71SJohn Keeping return rk3x_i2c_xfer_common(adap, msgs, num, true);
1148f3e2bd71SJohn Keeping }
1149f3e2bd71SJohn Keeping
rk3x_i2c_resume(struct device * dev)1150cbfff439SDoug Anderson static __maybe_unused int rk3x_i2c_resume(struct device *dev)
1151cbfff439SDoug Anderson {
1152cbfff439SDoug Anderson struct rk3x_i2c *i2c = dev_get_drvdata(dev);
1153cbfff439SDoug Anderson
1154cbfff439SDoug Anderson rk3x_i2c_adapt_div(i2c, clk_get_rate(i2c->clk));
1155cbfff439SDoug Anderson
1156cbfff439SDoug Anderson return 0;
1157cbfff439SDoug Anderson }
1158cbfff439SDoug Anderson
rk3x_i2c_func(struct i2c_adapter * adap)1159c41aa3ceSMax Schwarz static u32 rk3x_i2c_func(struct i2c_adapter *adap)
1160c41aa3ceSMax Schwarz {
1161c41aa3ceSMax Schwarz return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
1162c41aa3ceSMax Schwarz }
1163c41aa3ceSMax Schwarz
1164c41aa3ceSMax Schwarz static const struct i2c_algorithm rk3x_i2c_algorithm = {
1165c41aa3ceSMax Schwarz .master_xfer = rk3x_i2c_xfer,
1166f3e2bd71SJohn Keeping .master_xfer_atomic = rk3x_i2c_xfer_polling,
1167c41aa3ceSMax Schwarz .functionality = rk3x_i2c_func,
1168c41aa3ceSMax Schwarz };
1169c41aa3ceSMax Schwarz
11700dbb9634SAndy Yan static const struct rk3x_i2c_soc_data rv1108_soc_data = {
11710dbb9634SAndy Yan .grf_offset = -1,
11720dbb9634SAndy Yan .calc_timings = rk3x_i2c_v1_calc_timings,
11730dbb9634SAndy Yan };
11740dbb9634SAndy Yan
1175859d6468SJagan Teki static const struct rk3x_i2c_soc_data rv1126_soc_data = {
1176859d6468SJagan Teki .grf_offset = 0x118,
1177859d6468SJagan Teki .calc_timings = rk3x_i2c_v1_calc_timings,
1178859d6468SJagan Teki };
1179859d6468SJagan Teki
1180bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3066_soc_data = {
1181bef358c4SDavid Wu .grf_offset = 0x154,
11827e086c3fSDavid Wu .calc_timings = rk3x_i2c_v0_calc_timings,
1183bef358c4SDavid Wu };
1184bef358c4SDavid Wu
1185bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3188_soc_data = {
1186bef358c4SDavid Wu .grf_offset = 0x0a4,
11877e086c3fSDavid Wu .calc_timings = rk3x_i2c_v0_calc_timings,
1188bef358c4SDavid Wu };
1189bef358c4SDavid Wu
1190bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3228_soc_data = {
1191bef358c4SDavid Wu .grf_offset = -1,
11927e086c3fSDavid Wu .calc_timings = rk3x_i2c_v0_calc_timings,
1193bef358c4SDavid Wu };
1194bef358c4SDavid Wu
1195bef358c4SDavid Wu static const struct rk3x_i2c_soc_data rk3288_soc_data = {
1196bef358c4SDavid Wu .grf_offset = -1,
11977e086c3fSDavid Wu .calc_timings = rk3x_i2c_v0_calc_timings,
11987e086c3fSDavid Wu };
11997e086c3fSDavid Wu
12007e086c3fSDavid Wu static const struct rk3x_i2c_soc_data rk3399_soc_data = {
12017e086c3fSDavid Wu .grf_offset = -1,
12027e086c3fSDavid Wu .calc_timings = rk3x_i2c_v1_calc_timings,
1203c41aa3ceSMax Schwarz };
1204c41aa3ceSMax Schwarz
1205c41aa3ceSMax Schwarz static const struct of_device_id rk3x_i2c_match[] = {
1206bef358c4SDavid Wu {
12070dbb9634SAndy Yan .compatible = "rockchip,rv1108-i2c",
1208d032a2ebSJulia Lawall .data = &rv1108_soc_data
12090dbb9634SAndy Yan },
12100dbb9634SAndy Yan {
1211859d6468SJagan Teki .compatible = "rockchip,rv1126-i2c",
1212859d6468SJagan Teki .data = &rv1126_soc_data
1213859d6468SJagan Teki },
1214859d6468SJagan Teki {
1215bef358c4SDavid Wu .compatible = "rockchip,rk3066-i2c",
1216d032a2ebSJulia Lawall .data = &rk3066_soc_data
1217bef358c4SDavid Wu },
1218bef358c4SDavid Wu {
1219bef358c4SDavid Wu .compatible = "rockchip,rk3188-i2c",
1220d032a2ebSJulia Lawall .data = &rk3188_soc_data
1221bef358c4SDavid Wu },
1222bef358c4SDavid Wu {
1223bef358c4SDavid Wu .compatible = "rockchip,rk3228-i2c",
1224d032a2ebSJulia Lawall .data = &rk3228_soc_data
1225bef358c4SDavid Wu },
1226bef358c4SDavid Wu {
1227bef358c4SDavid Wu .compatible = "rockchip,rk3288-i2c",
1228d032a2ebSJulia Lawall .data = &rk3288_soc_data
1229bef358c4SDavid Wu },
12307e086c3fSDavid Wu {
12317e086c3fSDavid Wu .compatible = "rockchip,rk3399-i2c",
1232d032a2ebSJulia Lawall .data = &rk3399_soc_data
12337e086c3fSDavid Wu },
1234c51bd6acSDan Carpenter {},
1235c41aa3ceSMax Schwarz };
1236598cf161SLuis de Bethencourt MODULE_DEVICE_TABLE(of, rk3x_i2c_match);
1237c41aa3ceSMax Schwarz
rk3x_i2c_probe(struct platform_device * pdev)1238c41aa3ceSMax Schwarz static int rk3x_i2c_probe(struct platform_device *pdev)
1239c41aa3ceSMax Schwarz {
1240c41aa3ceSMax Schwarz struct device_node *np = pdev->dev.of_node;
1241c41aa3ceSMax Schwarz const struct of_device_id *match;
1242c41aa3ceSMax Schwarz struct rk3x_i2c *i2c;
1243c41aa3ceSMax Schwarz int ret = 0;
1244c41aa3ceSMax Schwarz int bus_nr;
1245c41aa3ceSMax Schwarz u32 value;
1246c41aa3ceSMax Schwarz int irq;
1247249051f4SMax Schwarz unsigned long clk_rate;
1248c41aa3ceSMax Schwarz
1249c41aa3ceSMax Schwarz i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
1250c41aa3ceSMax Schwarz if (!i2c)
1251c41aa3ceSMax Schwarz return -ENOMEM;
1252c41aa3ceSMax Schwarz
1253c41aa3ceSMax Schwarz match = of_match_node(rk3x_i2c_match, np);
1254d032a2ebSJulia Lawall i2c->soc_data = match->data;
1255c41aa3ceSMax Schwarz
12561ab92956SDavid Wu /* use common interface to get I2C timing properties */
12571ab92956SDavid Wu i2c_parse_fw_timings(&pdev->dev, &i2c->t, true);
12581330e291Saddy ke
1259ea1558ceSWolfram Sang strscpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
1260c41aa3ceSMax Schwarz i2c->adap.owner = THIS_MODULE;
1261c41aa3ceSMax Schwarz i2c->adap.algo = &rk3x_i2c_algorithm;
1262c41aa3ceSMax Schwarz i2c->adap.retries = 3;
1263c41aa3ceSMax Schwarz i2c->adap.dev.of_node = np;
1264c41aa3ceSMax Schwarz i2c->adap.algo_data = i2c;
1265c41aa3ceSMax Schwarz i2c->adap.dev.parent = &pdev->dev;
1266c41aa3ceSMax Schwarz
1267c41aa3ceSMax Schwarz i2c->dev = &pdev->dev;
1268c41aa3ceSMax Schwarz
1269c41aa3ceSMax Schwarz spin_lock_init(&i2c->lock);
1270c41aa3ceSMax Schwarz init_waitqueue_head(&i2c->wait);
1271c41aa3ceSMax Schwarz
1272e0442d76SDejin Zheng i2c->regs = devm_platform_ioremap_resource(pdev, 0);
1273c41aa3ceSMax Schwarz if (IS_ERR(i2c->regs))
1274c41aa3ceSMax Schwarz return PTR_ERR(i2c->regs);
1275c41aa3ceSMax Schwarz
1276c41aa3ceSMax Schwarz /* Try to set the I2C adapter number from dt */
1277c41aa3ceSMax Schwarz bus_nr = of_alias_get_id(np, "i2c");
1278c41aa3ceSMax Schwarz
1279c41aa3ceSMax Schwarz /*
1280c41aa3ceSMax Schwarz * Switch to new interface if the SoC also offers the old one.
1281c41aa3ceSMax Schwarz * The control bit is located in the GRF register space.
1282c41aa3ceSMax Schwarz */
1283c41aa3ceSMax Schwarz if (i2c->soc_data->grf_offset >= 0) {
1284c41aa3ceSMax Schwarz struct regmap *grf;
1285c41aa3ceSMax Schwarz
1286c41aa3ceSMax Schwarz grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
1287c41aa3ceSMax Schwarz if (IS_ERR(grf)) {
1288c41aa3ceSMax Schwarz dev_err(&pdev->dev,
1289c41aa3ceSMax Schwarz "rk3x-i2c needs 'rockchip,grf' property\n");
1290c41aa3ceSMax Schwarz return PTR_ERR(grf);
1291c41aa3ceSMax Schwarz }
1292c41aa3ceSMax Schwarz
1293c41aa3ceSMax Schwarz if (bus_nr < 0) {
1294c41aa3ceSMax Schwarz dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
1295c41aa3ceSMax Schwarz return -EINVAL;
1296c41aa3ceSMax Schwarz }
1297c41aa3ceSMax Schwarz
1298*250cfafbSTim Lunn /* rv1126 i2c2 uses non-sequential write mask 20, value 4 */
1299*250cfafbSTim Lunn if (i2c->soc_data == &rv1126_soc_data && bus_nr == 2)
1300*250cfafbSTim Lunn value = BIT(20) | BIT(4);
1301*250cfafbSTim Lunn else
1302c41aa3ceSMax Schwarz /* 27+i: write mask, 11+i: value */
1303c41aa3ceSMax Schwarz value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
1304c41aa3ceSMax Schwarz
1305c41aa3ceSMax Schwarz ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
1306c41aa3ceSMax Schwarz if (ret != 0) {
1307c41aa3ceSMax Schwarz dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
1308c41aa3ceSMax Schwarz return ret;
1309c41aa3ceSMax Schwarz }
1310c41aa3ceSMax Schwarz }
1311c41aa3ceSMax Schwarz
1312c41aa3ceSMax Schwarz /* IRQ setup */
1313c41aa3ceSMax Schwarz irq = platform_get_irq(pdev, 0);
1314e42688edSDejin Zheng if (irq < 0)
1315c41aa3ceSMax Schwarz return irq;
1316c41aa3ceSMax Schwarz
1317c41aa3ceSMax Schwarz ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
1318c41aa3ceSMax Schwarz 0, dev_name(&pdev->dev), i2c);
1319c41aa3ceSMax Schwarz if (ret < 0) {
1320c41aa3ceSMax Schwarz dev_err(&pdev->dev, "cannot request IRQ\n");
1321c41aa3ceSMax Schwarz return ret;
1322c41aa3ceSMax Schwarz }
1323c41aa3ceSMax Schwarz
13245a358b97SJensen Huang i2c->irq = irq;
13255a358b97SJensen Huang
1326c41aa3ceSMax Schwarz platform_set_drvdata(pdev, i2c);
1327c41aa3ceSMax Schwarz
13287e086c3fSDavid Wu if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) {
13297e086c3fSDavid Wu /* Only one clock to use for bus clock and peripheral clock */
13307e086c3fSDavid Wu i2c->clk = devm_clk_get(&pdev->dev, NULL);
13317e086c3fSDavid Wu i2c->pclk = i2c->clk;
13327e086c3fSDavid Wu } else {
13337e086c3fSDavid Wu i2c->clk = devm_clk_get(&pdev->dev, "i2c");
13347e086c3fSDavid Wu i2c->pclk = devm_clk_get(&pdev->dev, "pclk");
13357e086c3fSDavid Wu }
13367e086c3fSDavid Wu
133791a73027SKrzysztof Kozlowski if (IS_ERR(i2c->clk))
133891a73027SKrzysztof Kozlowski return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk),
133991a73027SKrzysztof Kozlowski "Can't get bus clk\n");
134091a73027SKrzysztof Kozlowski
134191a73027SKrzysztof Kozlowski if (IS_ERR(i2c->pclk))
134291a73027SKrzysztof Kozlowski return dev_err_probe(&pdev->dev, PTR_ERR(i2c->pclk),
134391a73027SKrzysztof Kozlowski "Can't get periph clk\n");
13447e086c3fSDavid Wu
1345c41aa3ceSMax Schwarz ret = clk_prepare(i2c->clk);
1346c41aa3ceSMax Schwarz if (ret < 0) {
13477e086c3fSDavid Wu dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
1348c41aa3ceSMax Schwarz return ret;
1349c41aa3ceSMax Schwarz }
13507e086c3fSDavid Wu ret = clk_prepare(i2c->pclk);
13517e086c3fSDavid Wu if (ret < 0) {
13527e086c3fSDavid Wu dev_err(&pdev->dev, "Can't prepare periph clock: %d\n", ret);
13537e086c3fSDavid Wu goto err_clk;
13547e086c3fSDavid Wu }
1355c41aa3ceSMax Schwarz
1356249051f4SMax Schwarz i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
1357249051f4SMax Schwarz ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
1358249051f4SMax Schwarz if (ret != 0) {
1359249051f4SMax Schwarz dev_err(&pdev->dev, "Unable to register clock notifier\n");
13607e086c3fSDavid Wu goto err_pclk;
1361249051f4SMax Schwarz }
1362249051f4SMax Schwarz
1363b57e9018SJohn Keeping ret = clk_enable(i2c->clk);
1364b57e9018SJohn Keeping if (ret < 0) {
1365b57e9018SJohn Keeping dev_err(&pdev->dev, "Can't enable bus clk: %d\n", ret);
1366b57e9018SJohn Keeping goto err_clk_notifier;
1367b57e9018SJohn Keeping }
1368b57e9018SJohn Keeping
1369249051f4SMax Schwarz clk_rate = clk_get_rate(i2c->clk);
1370249051f4SMax Schwarz rk3x_i2c_adapt_div(i2c, clk_rate);
1371b57e9018SJohn Keeping clk_disable(i2c->clk);
1372249051f4SMax Schwarz
1373c41aa3ceSMax Schwarz ret = i2c_add_adapter(&i2c->adap);
1374ea734404SWolfram Sang if (ret < 0)
1375249051f4SMax Schwarz goto err_clk_notifier;
1376c41aa3ceSMax Schwarz
1377c41aa3ceSMax Schwarz return 0;
1378c41aa3ceSMax Schwarz
1379249051f4SMax Schwarz err_clk_notifier:
1380249051f4SMax Schwarz clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
13817e086c3fSDavid Wu err_pclk:
13827e086c3fSDavid Wu clk_unprepare(i2c->pclk);
1383c41aa3ceSMax Schwarz err_clk:
1384c41aa3ceSMax Schwarz clk_unprepare(i2c->clk);
1385c41aa3ceSMax Schwarz return ret;
1386c41aa3ceSMax Schwarz }
1387c41aa3ceSMax Schwarz
rk3x_i2c_remove(struct platform_device * pdev)1388e190a0c3SUwe Kleine-König static void rk3x_i2c_remove(struct platform_device *pdev)
1389c41aa3ceSMax Schwarz {
1390c41aa3ceSMax Schwarz struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
1391c41aa3ceSMax Schwarz
1392c41aa3ceSMax Schwarz i2c_del_adapter(&i2c->adap);
1393249051f4SMax Schwarz
1394249051f4SMax Schwarz clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
13957e086c3fSDavid Wu clk_unprepare(i2c->pclk);
1396c41aa3ceSMax Schwarz clk_unprepare(i2c->clk);
1397c41aa3ceSMax Schwarz }
1398c41aa3ceSMax Schwarz
1399cbfff439SDoug Anderson static SIMPLE_DEV_PM_OPS(rk3x_i2c_pm_ops, NULL, rk3x_i2c_resume);
1400cbfff439SDoug Anderson
1401c41aa3ceSMax Schwarz static struct platform_driver rk3x_i2c_driver = {
1402c41aa3ceSMax Schwarz .probe = rk3x_i2c_probe,
1403e190a0c3SUwe Kleine-König .remove_new = rk3x_i2c_remove,
1404c41aa3ceSMax Schwarz .driver = {
1405c41aa3ceSMax Schwarz .name = "rk3x-i2c",
1406c41aa3ceSMax Schwarz .of_match_table = rk3x_i2c_match,
1407cbfff439SDoug Anderson .pm = &rk3x_i2c_pm_ops,
1408c41aa3ceSMax Schwarz },
1409c41aa3ceSMax Schwarz };
1410c41aa3ceSMax Schwarz
1411c41aa3ceSMax Schwarz module_platform_driver(rk3x_i2c_driver);
1412c41aa3ceSMax Schwarz
1413c41aa3ceSMax Schwarz MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
1414c41aa3ceSMax Schwarz MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
1415c41aa3ceSMax Schwarz MODULE_LICENSE("GPL v2");
1416