Lines Matching full:i2c

3  * Driver for I2C adapter in Rockchip RK3xxx SoC
11 #include <linux/i2c.h>
83 * struct i2c_spec_values - I2C specification values for various modes
163 * @grf_offset: offset inside the grf regmap for setting the i2c type
164 * @calc_timings: Callback function for i2c timing information calculated
174 * @adap: corresponding I2C adapter
180 * @clk_rate_nb: i2c clk rate change notify
182 * @t: I2C known timing information
183 * @lock: spinlock for the i2c bus
184 * @wait: the waitqueue to wait for i2c transfer
186 * @msg: current i2c message
187 * @addr: addr of i2c target device
188 * @mode: mode of i2c transfer
190 * @state: state of i2c transfer
192 * @error: error code for i2c transfer
220 /* I2C state machine */
226 static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value, in i2c_writel() argument
229 writel(value, i2c->regs + offset); in i2c_writel()
232 static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset) in i2c_readl() argument
234 return readl(i2c->regs + offset); in i2c_readl()
238 static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c) in rk3x_i2c_clean_ipd() argument
240 i2c_writel(i2c, REG_INT_ALL, REG_IPD); in rk3x_i2c_clean_ipd()
245 * @i2c: target controller data
247 static void rk3x_i2c_start(struct rk3x_i2c *i2c) in rk3x_i2c_start() argument
249 u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; in rk3x_i2c_start()
251 i2c_writel(i2c, REG_INT_START, REG_IEN); in rk3x_i2c_start()
254 val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START; in rk3x_i2c_start()
257 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) in rk3x_i2c_start()
260 i2c_writel(i2c, val, REG_CON); in rk3x_i2c_start()
265 * @i2c: target controller data
268 static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error) in rk3x_i2c_stop() argument
272 i2c->processed = 0; in rk3x_i2c_stop()
273 i2c->msg = NULL; in rk3x_i2c_stop()
274 i2c->error = error; in rk3x_i2c_stop()
276 if (i2c->is_last_msg) { in rk3x_i2c_stop()
278 i2c_writel(i2c, REG_INT_STOP, REG_IEN); in rk3x_i2c_stop()
280 i2c->state = STATE_STOP; in rk3x_i2c_stop()
282 ctrl = i2c_readl(i2c, REG_CON); in rk3x_i2c_stop()
284 i2c_writel(i2c, ctrl, REG_CON); in rk3x_i2c_stop()
287 i2c->busy = false; in rk3x_i2c_stop()
288 i2c->state = STATE_IDLE; in rk3x_i2c_stop()
295 ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; in rk3x_i2c_stop()
296 i2c_writel(i2c, ctrl, REG_CON); in rk3x_i2c_stop()
299 wake_up(&i2c->wait); in rk3x_i2c_stop()
304 * rk3x_i2c_prepare_read - Setup a read according to i2c->msg
305 * @i2c: target controller data
307 static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c) in rk3x_i2c_prepare_read() argument
309 unsigned int len = i2c->msg->len - i2c->processed; in rk3x_i2c_prepare_read()
312 con = i2c_readl(i2c, REG_CON); in rk3x_i2c_prepare_read()
326 if (i2c->processed != 0) { in rk3x_i2c_prepare_read()
331 i2c_writel(i2c, con, REG_CON); in rk3x_i2c_prepare_read()
332 i2c_writel(i2c, len, REG_MRXCNT); in rk3x_i2c_prepare_read()
336 * rk3x_i2c_fill_transmit_buf - Fill the transmit buffer with data from i2c->msg
337 * @i2c: target controller data
339 static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c) in rk3x_i2c_fill_transmit_buf() argument
349 if ((i2c->processed == i2c->msg->len) && (cnt != 0)) in rk3x_i2c_fill_transmit_buf()
352 if (i2c->processed == 0 && cnt == 0) in rk3x_i2c_fill_transmit_buf()
353 byte = (i2c->addr & 0x7f) << 1; in rk3x_i2c_fill_transmit_buf()
355 byte = i2c->msg->buf[i2c->processed++]; in rk3x_i2c_fill_transmit_buf()
361 i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i); in rk3x_i2c_fill_transmit_buf()
363 if (i2c->processed == i2c->msg->len) in rk3x_i2c_fill_transmit_buf()
367 i2c_writel(i2c, cnt, REG_MTXCNT); in rk3x_i2c_fill_transmit_buf()
373 static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd) in rk3x_i2c_handle_start() argument
376 rk3x_i2c_stop(i2c, -EIO); in rk3x_i2c_handle_start()
377 dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd); in rk3x_i2c_handle_start()
378 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_handle_start()
383 i2c_writel(i2c, REG_INT_START, REG_IPD); in rk3x_i2c_handle_start()
386 i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON); in rk3x_i2c_handle_start()
389 if (i2c->mode == REG_CON_MOD_TX) { in rk3x_i2c_handle_start()
390 i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN); in rk3x_i2c_handle_start()
391 i2c->state = STATE_WRITE; in rk3x_i2c_handle_start()
392 rk3x_i2c_fill_transmit_buf(i2c); in rk3x_i2c_handle_start()
395 i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN); in rk3x_i2c_handle_start()
396 i2c->state = STATE_READ; in rk3x_i2c_handle_start()
397 rk3x_i2c_prepare_read(i2c); in rk3x_i2c_handle_start()
401 static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd) in rk3x_i2c_handle_write() argument
404 rk3x_i2c_stop(i2c, -EIO); in rk3x_i2c_handle_write()
405 dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd); in rk3x_i2c_handle_write()
406 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_handle_write()
411 i2c_writel(i2c, REG_INT_MBTF, REG_IPD); in rk3x_i2c_handle_write()
414 if (i2c->processed == i2c->msg->len) in rk3x_i2c_handle_write()
415 rk3x_i2c_stop(i2c, i2c->error); in rk3x_i2c_handle_write()
417 rk3x_i2c_fill_transmit_buf(i2c); in rk3x_i2c_handle_write()
420 static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd) in rk3x_i2c_handle_read() argument
423 unsigned int len = i2c->msg->len - i2c->processed; in rk3x_i2c_handle_read()
432 i2c_writel(i2c, REG_INT_MBRF | REG_INT_START, REG_IPD); in rk3x_i2c_handle_read()
441 val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4); in rk3x_i2c_handle_read()
444 i2c->msg->buf[i2c->processed++] = byte; in rk3x_i2c_handle_read()
448 if (i2c->processed == i2c->msg->len) in rk3x_i2c_handle_read()
449 rk3x_i2c_stop(i2c, i2c->error); in rk3x_i2c_handle_read()
451 rk3x_i2c_prepare_read(i2c); in rk3x_i2c_handle_read()
454 static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd) in rk3x_i2c_handle_stop() argument
459 rk3x_i2c_stop(i2c, -EIO); in rk3x_i2c_handle_stop()
460 dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd); in rk3x_i2c_handle_stop()
461 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_handle_stop()
466 i2c_writel(i2c, REG_INT_STOP, REG_IPD); in rk3x_i2c_handle_stop()
469 con = i2c_readl(i2c, REG_CON); in rk3x_i2c_handle_stop()
471 i2c_writel(i2c, con, REG_CON); in rk3x_i2c_handle_stop()
473 i2c->busy = false; in rk3x_i2c_handle_stop()
474 i2c->state = STATE_IDLE; in rk3x_i2c_handle_stop()
477 wake_up(&i2c->wait); in rk3x_i2c_handle_stop()
482 struct rk3x_i2c *i2c = dev_id; in rk3x_i2c_irq() local
485 spin_lock(&i2c->lock); in rk3x_i2c_irq()
487 ipd = i2c_readl(i2c, REG_IPD); in rk3x_i2c_irq()
488 if (i2c->state == STATE_IDLE) { in rk3x_i2c_irq()
489 dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd); in rk3x_i2c_irq()
490 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_irq()
494 dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd); in rk3x_i2c_irq()
505 i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD); in rk3x_i2c_irq()
509 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) in rk3x_i2c_irq()
510 rk3x_i2c_stop(i2c, -ENXIO); in rk3x_i2c_irq()
517 switch (i2c->state) { in rk3x_i2c_irq()
519 rk3x_i2c_handle_start(i2c, ipd); in rk3x_i2c_irq()
522 rk3x_i2c_handle_write(i2c, ipd); in rk3x_i2c_irq()
525 rk3x_i2c_handle_read(i2c, ipd); in rk3x_i2c_irq()
528 rk3x_i2c_handle_stop(i2c, ipd); in rk3x_i2c_irq()
535 spin_unlock(&i2c->lock); in rk3x_i2c_irq()
540 * rk3x_i2c_get_spec - Get timing values of I2C specification
557 * @clk_rate: I2C input clock rate
558 * @t: Known I2C timing information
594 * meet I2C specification, should include fall time. in rk3x_i2c_v0_calc_timings()
596 * meet I2C specification, should include rise time. in rk3x_i2c_v0_calc_timings()
598 * I2C specification. in rk3x_i2c_v0_calc_timings()
601 * This is because the i2c host on Rockchip holds the data line in rk3x_i2c_v0_calc_timings()
717 * @clk_rate: I2C input clock rate
718 * @t: Known I2C timing information
773 * meet I2C specification, should include fall time. in rk3x_i2c_v1_calc_timings()
775 * meet I2C specification, should include rise time. in rk3x_i2c_v1_calc_timings()
792 * hardware would not output the i2c clk. in rk3x_i2c_v1_calc_timings()
874 static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate) in rk3x_i2c_adapt_div() argument
876 struct i2c_timings *t = &i2c->t; in rk3x_i2c_adapt_div()
883 ret = i2c->soc_data->calc_timings(clk_rate, t, &calc); in rk3x_i2c_adapt_div()
886 clk_enable(i2c->pclk); in rk3x_i2c_adapt_div()
888 spin_lock_irqsave(&i2c->lock, flags); in rk3x_i2c_adapt_div()
889 val = i2c_readl(i2c, REG_CON); in rk3x_i2c_adapt_div()
892 i2c_writel(i2c, val, REG_CON); in rk3x_i2c_adapt_div()
893 i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff), in rk3x_i2c_adapt_div()
895 spin_unlock_irqrestore(&i2c->lock, flags); in rk3x_i2c_adapt_div()
897 clk_disable(i2c->pclk); in rk3x_i2c_adapt_div()
902 dev_dbg(i2c->dev, in rk3x_i2c_adapt_div()
920 * Code adapted from i2c-cadence.c.
930 struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb); in rk3x_i2c_clk_notifier_cb() local
940 if (i2c->soc_data->calc_timings(ndata->new_rate, &i2c->t, in rk3x_i2c_clk_notifier_cb()
946 rk3x_i2c_adapt_div(i2c, ndata->new_rate); in rk3x_i2c_clk_notifier_cb()
952 rk3x_i2c_adapt_div(i2c, ndata->new_rate); in rk3x_i2c_clk_notifier_cb()
957 rk3x_i2c_adapt_div(i2c, ndata->old_rate); in rk3x_i2c_clk_notifier_cb()
965 * rk3x_i2c_setup - Setup I2C registers for an I2C operation specified by msgs, num.
966 * @i2c: target controller data
967 * @msgs: I2C msgs to process
970 * Must be called with i2c->lock held.
972 * Return: Number of I2C msgs processed or negative in case of error
974 static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num) in rk3x_i2c_setup() argument
980 * The I2C adapter can issue a small (len < 4) write packet before in rk3x_i2c_setup()
991 dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n", in rk3x_i2c_setup()
1001 i2c->msg = &msgs[1]; in rk3x_i2c_setup()
1003 i2c->mode = REG_CON_MOD_REGISTER_TX; in rk3x_i2c_setup()
1005 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR); in rk3x_i2c_setup()
1006 i2c_writel(i2c, reg_addr, REG_MRXRADDR); in rk3x_i2c_setup()
1022 i2c->mode = REG_CON_MOD_REGISTER_TX; in rk3x_i2c_setup()
1023 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), in rk3x_i2c_setup()
1025 i2c_writel(i2c, 0, REG_MRXRADDR); in rk3x_i2c_setup()
1027 i2c->mode = REG_CON_MOD_TX; in rk3x_i2c_setup()
1030 i2c->msg = &msgs[0]; in rk3x_i2c_setup()
1035 i2c->addr = msgs[0].addr; in rk3x_i2c_setup()
1036 i2c->busy = true; in rk3x_i2c_setup()
1037 i2c->state = STATE_START; in rk3x_i2c_setup()
1038 i2c->processed = 0; in rk3x_i2c_setup()
1039 i2c->error = 0; in rk3x_i2c_setup()
1041 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_setup()
1046 static int rk3x_i2c_wait_xfer_poll(struct rk3x_i2c *i2c) in rk3x_i2c_wait_xfer_poll() argument
1050 while (READ_ONCE(i2c->busy) && in rk3x_i2c_wait_xfer_poll()
1053 rk3x_i2c_irq(0, i2c); in rk3x_i2c_wait_xfer_poll()
1056 return !i2c->busy; in rk3x_i2c_wait_xfer_poll()
1062 struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data; in rk3x_i2c_xfer_common() local
1069 spin_lock_irqsave(&i2c->lock, flags); in rk3x_i2c_xfer_common()
1071 clk_enable(i2c->clk); in rk3x_i2c_xfer_common()
1072 clk_enable(i2c->pclk); in rk3x_i2c_xfer_common()
1074 i2c->is_last_msg = false; in rk3x_i2c_xfer_common()
1081 ret = rk3x_i2c_setup(i2c, msgs + i, num - i); in rk3x_i2c_xfer_common()
1084 dev_err(i2c->dev, "rk3x_i2c_setup() failed\n"); in rk3x_i2c_xfer_common()
1089 i2c->is_last_msg = true; in rk3x_i2c_xfer_common()
1091 spin_unlock_irqrestore(&i2c->lock, flags); in rk3x_i2c_xfer_common()
1094 rk3x_i2c_start(i2c); in rk3x_i2c_xfer_common()
1096 time_left = wait_event_timeout(i2c->wait, !i2c->busy, in rk3x_i2c_xfer_common()
1099 disable_irq(i2c->irq); in rk3x_i2c_xfer_common()
1100 rk3x_i2c_start(i2c); in rk3x_i2c_xfer_common()
1102 time_left = rk3x_i2c_wait_xfer_poll(i2c); in rk3x_i2c_xfer_common()
1104 enable_irq(i2c->irq); in rk3x_i2c_xfer_common()
1107 spin_lock_irqsave(&i2c->lock, flags); in rk3x_i2c_xfer_common()
1111 i2c_writel(i2c, 0, REG_IEN); in rk3x_i2c_xfer_common()
1112 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; in rk3x_i2c_xfer_common()
1114 i2c_writel(i2c, val, REG_CON); in rk3x_i2c_xfer_common()
1116 i2c->state = STATE_IDLE; in rk3x_i2c_xfer_common()
1122 if (i2c->error) { in rk3x_i2c_xfer_common()
1123 ret = i2c->error; in rk3x_i2c_xfer_common()
1128 clk_disable(i2c->pclk); in rk3x_i2c_xfer_common()
1129 clk_disable(i2c->clk); in rk3x_i2c_xfer_common()
1131 spin_unlock_irqrestore(&i2c->lock, flags); in rk3x_i2c_xfer_common()
1150 struct rk3x_i2c *i2c = dev_get_drvdata(dev); in rk3x_i2c_resume() local
1152 rk3x_i2c_adapt_div(i2c, clk_get_rate(i2c->clk)); in rk3x_i2c_resume()
1205 .compatible = "rockchip,rv1108-i2c",
1209 .compatible = "rockchip,rv1126-i2c",
1213 .compatible = "rockchip,rk3066-i2c",
1217 .compatible = "rockchip,rk3188-i2c",
1221 .compatible = "rockchip,rk3228-i2c",
1225 .compatible = "rockchip,rk3288-i2c",
1229 .compatible = "rockchip,rk3399-i2c",
1240 struct rk3x_i2c *i2c; in rk3x_i2c_probe() local
1247 i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL); in rk3x_i2c_probe()
1248 if (!i2c) in rk3x_i2c_probe()
1252 i2c->soc_data = match->data; in rk3x_i2c_probe()
1254 /* use common interface to get I2C timing properties */ in rk3x_i2c_probe()
1255 i2c_parse_fw_timings(&pdev->dev, &i2c->t, true); in rk3x_i2c_probe()
1257 strscpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name)); in rk3x_i2c_probe()
1258 i2c->adap.owner = THIS_MODULE; in rk3x_i2c_probe()
1259 i2c->adap.algo = &rk3x_i2c_algorithm; in rk3x_i2c_probe()
1260 i2c->adap.retries = 3; in rk3x_i2c_probe()
1261 i2c->adap.dev.of_node = np; in rk3x_i2c_probe()
1262 i2c->adap.algo_data = i2c; in rk3x_i2c_probe()
1263 i2c->adap.dev.parent = &pdev->dev; in rk3x_i2c_probe()
1265 i2c->dev = &pdev->dev; in rk3x_i2c_probe()
1267 spin_lock_init(&i2c->lock); in rk3x_i2c_probe()
1268 init_waitqueue_head(&i2c->wait); in rk3x_i2c_probe()
1270 i2c->regs = devm_platform_ioremap_resource(pdev, 0); in rk3x_i2c_probe()
1271 if (IS_ERR(i2c->regs)) in rk3x_i2c_probe()
1272 return PTR_ERR(i2c->regs); in rk3x_i2c_probe()
1274 /* Try to set the I2C adapter number from dt */ in rk3x_i2c_probe()
1275 bus_nr = of_alias_get_id(np, "i2c"); in rk3x_i2c_probe()
1281 if (i2c->soc_data->grf_offset >= 0) { in rk3x_i2c_probe()
1287 "rk3x-i2c needs 'rockchip,grf' property\n"); in rk3x_i2c_probe()
1292 dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias"); in rk3x_i2c_probe()
1297 if (i2c->soc_data == &rv1126_soc_data && bus_nr == 2) in rk3x_i2c_probe()
1303 ret = regmap_write(grf, i2c->soc_data->grf_offset, value); in rk3x_i2c_probe()
1305 dev_err(i2c->dev, "Could not write to GRF: %d\n", ret); in rk3x_i2c_probe()
1316 0, dev_name(&pdev->dev), i2c); in rk3x_i2c_probe()
1322 i2c->irq = irq; in rk3x_i2c_probe()
1324 platform_set_drvdata(pdev, i2c); in rk3x_i2c_probe()
1326 if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) { in rk3x_i2c_probe()
1328 i2c->clk = devm_clk_get(&pdev->dev, NULL); in rk3x_i2c_probe()
1329 i2c->pclk = i2c->clk; in rk3x_i2c_probe()
1331 i2c->clk = devm_clk_get(&pdev->dev, "i2c"); in rk3x_i2c_probe()
1332 i2c->pclk = devm_clk_get(&pdev->dev, "pclk"); in rk3x_i2c_probe()
1335 if (IS_ERR(i2c->clk)) in rk3x_i2c_probe()
1336 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk), in rk3x_i2c_probe()
1339 if (IS_ERR(i2c->pclk)) in rk3x_i2c_probe()
1340 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->pclk), in rk3x_i2c_probe()
1343 ret = clk_prepare(i2c->clk); in rk3x_i2c_probe()
1348 ret = clk_prepare(i2c->pclk); in rk3x_i2c_probe()
1354 i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb; in rk3x_i2c_probe()
1355 ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb); in rk3x_i2c_probe()
1361 ret = clk_enable(i2c->clk); in rk3x_i2c_probe()
1367 clk_rate = clk_get_rate(i2c->clk); in rk3x_i2c_probe()
1368 rk3x_i2c_adapt_div(i2c, clk_rate); in rk3x_i2c_probe()
1369 clk_disable(i2c->clk); in rk3x_i2c_probe()
1371 ret = i2c_add_adapter(&i2c->adap); in rk3x_i2c_probe()
1378 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb); in rk3x_i2c_probe()
1380 clk_unprepare(i2c->pclk); in rk3x_i2c_probe()
1382 clk_unprepare(i2c->clk); in rk3x_i2c_probe()
1388 struct rk3x_i2c *i2c = platform_get_drvdata(pdev); in rk3x_i2c_remove() local
1390 i2c_del_adapter(&i2c->adap); in rk3x_i2c_remove()
1392 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb); in rk3x_i2c_remove()
1393 clk_unprepare(i2c->pclk); in rk3x_i2c_remove()
1394 clk_unprepare(i2c->clk); in rk3x_i2c_remove()
1403 .name = "rk3x-i2c",
1411 MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");