Lines Matching full:i2c

3  * Driver for I2C adapter in Rockchip RK3xxx SoC
11 #include <linux/i2c.h>
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
181 * @t: I2C known timing information
182 * @lock: spinlock for the i2c bus
183 * @wait: the waitqueue to wait for i2c transfer
185 * @msg: current i2c message
186 * @addr: addr of i2c slave device
187 * @mode: mode of i2c transfer
189 * @state: state of i2c transfer
191 * @error: error code for i2c transfer
218 /* I2C state machine */
224 static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value, in i2c_writel() argument
227 writel(value, i2c->regs + offset); in i2c_writel()
230 static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset) in i2c_readl() argument
232 return readl(i2c->regs + offset); in i2c_readl()
236 static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c) in rk3x_i2c_clean_ipd() argument
238 i2c_writel(i2c, REG_INT_ALL, REG_IPD); in rk3x_i2c_clean_ipd()
244 static void rk3x_i2c_start(struct rk3x_i2c *i2c) in rk3x_i2c_start() argument
246 u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; in rk3x_i2c_start()
248 i2c_writel(i2c, REG_INT_START, REG_IEN); in rk3x_i2c_start()
251 val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START; in rk3x_i2c_start()
254 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) in rk3x_i2c_start()
257 i2c_writel(i2c, val, REG_CON); in rk3x_i2c_start()
265 static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error) in rk3x_i2c_stop() argument
269 i2c->processed = 0; in rk3x_i2c_stop()
270 i2c->msg = NULL; in rk3x_i2c_stop()
271 i2c->error = error; in rk3x_i2c_stop()
273 if (i2c->is_last_msg) { in rk3x_i2c_stop()
275 i2c_writel(i2c, REG_INT_STOP, REG_IEN); in rk3x_i2c_stop()
277 i2c->state = STATE_STOP; in rk3x_i2c_stop()
279 ctrl = i2c_readl(i2c, REG_CON); in rk3x_i2c_stop()
281 i2c_writel(i2c, ctrl, REG_CON); in rk3x_i2c_stop()
284 i2c->busy = false; in rk3x_i2c_stop()
285 i2c->state = STATE_IDLE; in rk3x_i2c_stop()
292 ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; in rk3x_i2c_stop()
293 i2c_writel(i2c, ctrl, REG_CON); in rk3x_i2c_stop()
296 wake_up(&i2c->wait); in rk3x_i2c_stop()
301 * Setup a read according to i2c->msg
303 static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c) in rk3x_i2c_prepare_read() argument
305 unsigned int len = i2c->msg->len - i2c->processed; in rk3x_i2c_prepare_read()
308 con = i2c_readl(i2c, REG_CON); in rk3x_i2c_prepare_read()
322 if (i2c->processed != 0) { in rk3x_i2c_prepare_read()
327 i2c_writel(i2c, con, REG_CON); in rk3x_i2c_prepare_read()
328 i2c_writel(i2c, len, REG_MRXCNT); in rk3x_i2c_prepare_read()
332 * Fill the transmit buffer with data from i2c->msg
334 static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c) in rk3x_i2c_fill_transmit_buf() argument
344 if ((i2c->processed == i2c->msg->len) && (cnt != 0)) in rk3x_i2c_fill_transmit_buf()
347 if (i2c->processed == 0 && cnt == 0) in rk3x_i2c_fill_transmit_buf()
348 byte = (i2c->addr & 0x7f) << 1; in rk3x_i2c_fill_transmit_buf()
350 byte = i2c->msg->buf[i2c->processed++]; in rk3x_i2c_fill_transmit_buf()
356 i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i); in rk3x_i2c_fill_transmit_buf()
358 if (i2c->processed == i2c->msg->len) in rk3x_i2c_fill_transmit_buf()
362 i2c_writel(i2c, cnt, REG_MTXCNT); in rk3x_i2c_fill_transmit_buf()
368 static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd) in rk3x_i2c_handle_start() argument
371 rk3x_i2c_stop(i2c, -EIO); in rk3x_i2c_handle_start()
372 dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd); in rk3x_i2c_handle_start()
373 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_handle_start()
378 i2c_writel(i2c, REG_INT_START, REG_IPD); in rk3x_i2c_handle_start()
381 i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON); in rk3x_i2c_handle_start()
384 if (i2c->mode == REG_CON_MOD_TX) { in rk3x_i2c_handle_start()
385 i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN); in rk3x_i2c_handle_start()
386 i2c->state = STATE_WRITE; in rk3x_i2c_handle_start()
387 rk3x_i2c_fill_transmit_buf(i2c); in rk3x_i2c_handle_start()
390 i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN); in rk3x_i2c_handle_start()
391 i2c->state = STATE_READ; in rk3x_i2c_handle_start()
392 rk3x_i2c_prepare_read(i2c); in rk3x_i2c_handle_start()
396 static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd) in rk3x_i2c_handle_write() argument
399 rk3x_i2c_stop(i2c, -EIO); in rk3x_i2c_handle_write()
400 dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd); in rk3x_i2c_handle_write()
401 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_handle_write()
406 i2c_writel(i2c, REG_INT_MBTF, REG_IPD); in rk3x_i2c_handle_write()
409 if (i2c->processed == i2c->msg->len) in rk3x_i2c_handle_write()
410 rk3x_i2c_stop(i2c, i2c->error); in rk3x_i2c_handle_write()
412 rk3x_i2c_fill_transmit_buf(i2c); in rk3x_i2c_handle_write()
415 static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd) in rk3x_i2c_handle_read() argument
418 unsigned int len = i2c->msg->len - i2c->processed; in rk3x_i2c_handle_read()
427 i2c_writel(i2c, REG_INT_MBRF | REG_INT_START, REG_IPD); in rk3x_i2c_handle_read()
436 val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4); in rk3x_i2c_handle_read()
439 i2c->msg->buf[i2c->processed++] = byte; in rk3x_i2c_handle_read()
443 if (i2c->processed == i2c->msg->len) in rk3x_i2c_handle_read()
444 rk3x_i2c_stop(i2c, i2c->error); in rk3x_i2c_handle_read()
446 rk3x_i2c_prepare_read(i2c); in rk3x_i2c_handle_read()
449 static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd) in rk3x_i2c_handle_stop() argument
454 rk3x_i2c_stop(i2c, -EIO); in rk3x_i2c_handle_stop()
455 dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd); in rk3x_i2c_handle_stop()
456 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_handle_stop()
461 i2c_writel(i2c, REG_INT_STOP, REG_IPD); in rk3x_i2c_handle_stop()
464 con = i2c_readl(i2c, REG_CON); in rk3x_i2c_handle_stop()
466 i2c_writel(i2c, con, REG_CON); in rk3x_i2c_handle_stop()
468 i2c->busy = false; in rk3x_i2c_handle_stop()
469 i2c->state = STATE_IDLE; in rk3x_i2c_handle_stop()
472 wake_up(&i2c->wait); in rk3x_i2c_handle_stop()
477 struct rk3x_i2c *i2c = dev_id; in rk3x_i2c_irq() local
480 spin_lock(&i2c->lock); in rk3x_i2c_irq()
482 ipd = i2c_readl(i2c, REG_IPD); in rk3x_i2c_irq()
483 if (i2c->state == STATE_IDLE) { in rk3x_i2c_irq()
484 dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd); in rk3x_i2c_irq()
485 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_irq()
489 dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd); in rk3x_i2c_irq()
500 i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD); in rk3x_i2c_irq()
504 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) in rk3x_i2c_irq()
505 rk3x_i2c_stop(i2c, -ENXIO); in rk3x_i2c_irq()
512 switch (i2c->state) { in rk3x_i2c_irq()
514 rk3x_i2c_handle_start(i2c, ipd); in rk3x_i2c_irq()
517 rk3x_i2c_handle_write(i2c, ipd); in rk3x_i2c_irq()
520 rk3x_i2c_handle_read(i2c, ipd); in rk3x_i2c_irq()
523 rk3x_i2c_handle_stop(i2c, ipd); in rk3x_i2c_irq()
530 spin_unlock(&i2c->lock); in rk3x_i2c_irq()
535 * Get timing values of I2C specification
539 * Returns: Matched i2c spec values.
554 * @clk_rate: I2C input clock rate
555 * @t: Known I2C timing information
591 * meet I2C specification, should include fall time. in rk3x_i2c_v0_calc_timings()
593 * meet I2C specification, should include rise time. in rk3x_i2c_v0_calc_timings()
595 * I2C specification. in rk3x_i2c_v0_calc_timings()
598 * This is because the i2c host on Rockchip holds the data line in rk3x_i2c_v0_calc_timings()
715 * @clk_rate: I2C input clock rate
716 * @t: Known I2C timing information
771 * meet I2C specification, should include fall time. in rk3x_i2c_v1_calc_timings()
773 * meet I2C specification, should include rise time. in rk3x_i2c_v1_calc_timings()
790 * hardware would not output the i2c clk. in rk3x_i2c_v1_calc_timings()
872 static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate) in rk3x_i2c_adapt_div() argument
874 struct i2c_timings *t = &i2c->t; in rk3x_i2c_adapt_div()
881 ret = i2c->soc_data->calc_timings(clk_rate, t, &calc); in rk3x_i2c_adapt_div()
884 clk_enable(i2c->pclk); in rk3x_i2c_adapt_div()
886 spin_lock_irqsave(&i2c->lock, flags); in rk3x_i2c_adapt_div()
887 val = i2c_readl(i2c, REG_CON); in rk3x_i2c_adapt_div()
890 i2c_writel(i2c, val, REG_CON); in rk3x_i2c_adapt_div()
891 i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff), in rk3x_i2c_adapt_div()
893 spin_unlock_irqrestore(&i2c->lock, flags); in rk3x_i2c_adapt_div()
895 clk_disable(i2c->pclk); in rk3x_i2c_adapt_div()
900 dev_dbg(i2c->dev, in rk3x_i2c_adapt_div()
918 * Code adapted from i2c-cadence.c.
928 struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb); in rk3x_i2c_clk_notifier_cb() local
938 if (i2c->soc_data->calc_timings(ndata->new_rate, &i2c->t, in rk3x_i2c_clk_notifier_cb()
944 rk3x_i2c_adapt_div(i2c, ndata->new_rate); in rk3x_i2c_clk_notifier_cb()
950 rk3x_i2c_adapt_div(i2c, ndata->new_rate); in rk3x_i2c_clk_notifier_cb()
955 rk3x_i2c_adapt_div(i2c, ndata->old_rate); in rk3x_i2c_clk_notifier_cb()
963 * Setup I2C registers for an I2C operation specified by msgs, num.
965 * Must be called with i2c->lock held.
967 * @msgs: I2C msgs to process
970 * returns: Number of I2C msgs processed or negative in case of error
972 static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num) in rk3x_i2c_setup() argument
978 * The I2C adapter can issue a small (len < 4) write packet before in rk3x_i2c_setup()
989 dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n", in rk3x_i2c_setup()
999 i2c->msg = &msgs[1]; in rk3x_i2c_setup()
1001 i2c->mode = REG_CON_MOD_REGISTER_TX; in rk3x_i2c_setup()
1003 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR); in rk3x_i2c_setup()
1004 i2c_writel(i2c, reg_addr, REG_MRXRADDR); in rk3x_i2c_setup()
1020 i2c->mode = REG_CON_MOD_REGISTER_TX; in rk3x_i2c_setup()
1021 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), in rk3x_i2c_setup()
1023 i2c_writel(i2c, 0, REG_MRXRADDR); in rk3x_i2c_setup()
1025 i2c->mode = REG_CON_MOD_TX; in rk3x_i2c_setup()
1028 i2c->msg = &msgs[0]; in rk3x_i2c_setup()
1033 i2c->addr = msgs[0].addr; in rk3x_i2c_setup()
1034 i2c->busy = true; in rk3x_i2c_setup()
1035 i2c->state = STATE_START; in rk3x_i2c_setup()
1036 i2c->processed = 0; in rk3x_i2c_setup()
1037 i2c->error = 0; in rk3x_i2c_setup()
1039 rk3x_i2c_clean_ipd(i2c); in rk3x_i2c_setup()
1044 static int rk3x_i2c_wait_xfer_poll(struct rk3x_i2c *i2c) in rk3x_i2c_wait_xfer_poll() argument
1048 while (READ_ONCE(i2c->busy) && in rk3x_i2c_wait_xfer_poll()
1051 rk3x_i2c_irq(0, i2c); in rk3x_i2c_wait_xfer_poll()
1054 return !i2c->busy; in rk3x_i2c_wait_xfer_poll()
1060 struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data; in rk3x_i2c_xfer_common() local
1066 spin_lock_irqsave(&i2c->lock, flags); in rk3x_i2c_xfer_common()
1068 clk_enable(i2c->clk); in rk3x_i2c_xfer_common()
1069 clk_enable(i2c->pclk); in rk3x_i2c_xfer_common()
1071 i2c->is_last_msg = false; in rk3x_i2c_xfer_common()
1078 ret = rk3x_i2c_setup(i2c, msgs + i, num - i); in rk3x_i2c_xfer_common()
1081 dev_err(i2c->dev, "rk3x_i2c_setup() failed\n"); in rk3x_i2c_xfer_common()
1086 i2c->is_last_msg = true; in rk3x_i2c_xfer_common()
1088 spin_unlock_irqrestore(&i2c->lock, flags); in rk3x_i2c_xfer_common()
1090 rk3x_i2c_start(i2c); in rk3x_i2c_xfer_common()
1093 timeout = wait_event_timeout(i2c->wait, !i2c->busy, in rk3x_i2c_xfer_common()
1096 timeout = rk3x_i2c_wait_xfer_poll(i2c); in rk3x_i2c_xfer_common()
1099 spin_lock_irqsave(&i2c->lock, flags); in rk3x_i2c_xfer_common()
1102 dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n", in rk3x_i2c_xfer_common()
1103 i2c_readl(i2c, REG_IPD), i2c->state); in rk3x_i2c_xfer_common()
1106 i2c_writel(i2c, 0, REG_IEN); in rk3x_i2c_xfer_common()
1107 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK; in rk3x_i2c_xfer_common()
1109 i2c_writel(i2c, val, REG_CON); in rk3x_i2c_xfer_common()
1111 i2c->state = STATE_IDLE; in rk3x_i2c_xfer_common()
1117 if (i2c->error) { in rk3x_i2c_xfer_common()
1118 ret = i2c->error; in rk3x_i2c_xfer_common()
1123 clk_disable(i2c->pclk); in rk3x_i2c_xfer_common()
1124 clk_disable(i2c->clk); in rk3x_i2c_xfer_common()
1126 spin_unlock_irqrestore(&i2c->lock, flags); in rk3x_i2c_xfer_common()
1145 struct rk3x_i2c *i2c = dev_get_drvdata(dev); in rk3x_i2c_resume() local
1147 rk3x_i2c_adapt_div(i2c, clk_get_rate(i2c->clk)); in rk3x_i2c_resume()
1200 .compatible = "rockchip,rv1108-i2c",
1204 .compatible = "rockchip,rv1126-i2c",
1208 .compatible = "rockchip,rk3066-i2c",
1212 .compatible = "rockchip,rk3188-i2c",
1216 .compatible = "rockchip,rk3228-i2c",
1220 .compatible = "rockchip,rk3288-i2c",
1224 .compatible = "rockchip,rk3399-i2c",
1235 struct rk3x_i2c *i2c; in rk3x_i2c_probe() local
1242 i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL); in rk3x_i2c_probe()
1243 if (!i2c) in rk3x_i2c_probe()
1247 i2c->soc_data = match->data; in rk3x_i2c_probe()
1249 /* use common interface to get I2C timing properties */ in rk3x_i2c_probe()
1250 i2c_parse_fw_timings(&pdev->dev, &i2c->t, true); in rk3x_i2c_probe()
1252 strscpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name)); in rk3x_i2c_probe()
1253 i2c->adap.owner = THIS_MODULE; in rk3x_i2c_probe()
1254 i2c->adap.algo = &rk3x_i2c_algorithm; in rk3x_i2c_probe()
1255 i2c->adap.retries = 3; in rk3x_i2c_probe()
1256 i2c->adap.dev.of_node = np; in rk3x_i2c_probe()
1257 i2c->adap.algo_data = i2c; in rk3x_i2c_probe()
1258 i2c->adap.dev.parent = &pdev->dev; in rk3x_i2c_probe()
1260 i2c->dev = &pdev->dev; in rk3x_i2c_probe()
1262 spin_lock_init(&i2c->lock); in rk3x_i2c_probe()
1263 init_waitqueue_head(&i2c->wait); in rk3x_i2c_probe()
1265 i2c->regs = devm_platform_ioremap_resource(pdev, 0); in rk3x_i2c_probe()
1266 if (IS_ERR(i2c->regs)) in rk3x_i2c_probe()
1267 return PTR_ERR(i2c->regs); in rk3x_i2c_probe()
1269 /* Try to set the I2C adapter number from dt */ in rk3x_i2c_probe()
1270 bus_nr = of_alias_get_id(np, "i2c"); in rk3x_i2c_probe()
1276 if (i2c->soc_data->grf_offset >= 0) { in rk3x_i2c_probe()
1282 "rk3x-i2c needs 'rockchip,grf' property\n"); in rk3x_i2c_probe()
1287 dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias"); in rk3x_i2c_probe()
1294 ret = regmap_write(grf, i2c->soc_data->grf_offset, value); in rk3x_i2c_probe()
1296 dev_err(i2c->dev, "Could not write to GRF: %d\n", ret); in rk3x_i2c_probe()
1307 0, dev_name(&pdev->dev), i2c); in rk3x_i2c_probe()
1313 platform_set_drvdata(pdev, i2c); in rk3x_i2c_probe()
1315 if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) { in rk3x_i2c_probe()
1317 i2c->clk = devm_clk_get(&pdev->dev, NULL); in rk3x_i2c_probe()
1318 i2c->pclk = i2c->clk; in rk3x_i2c_probe()
1320 i2c->clk = devm_clk_get(&pdev->dev, "i2c"); in rk3x_i2c_probe()
1321 i2c->pclk = devm_clk_get(&pdev->dev, "pclk"); in rk3x_i2c_probe()
1324 if (IS_ERR(i2c->clk)) in rk3x_i2c_probe()
1325 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk), in rk3x_i2c_probe()
1328 if (IS_ERR(i2c->pclk)) in rk3x_i2c_probe()
1329 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->pclk), in rk3x_i2c_probe()
1332 ret = clk_prepare(i2c->clk); in rk3x_i2c_probe()
1337 ret = clk_prepare(i2c->pclk); in rk3x_i2c_probe()
1343 i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb; in rk3x_i2c_probe()
1344 ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb); in rk3x_i2c_probe()
1350 ret = clk_enable(i2c->clk); in rk3x_i2c_probe()
1356 clk_rate = clk_get_rate(i2c->clk); in rk3x_i2c_probe()
1357 rk3x_i2c_adapt_div(i2c, clk_rate); in rk3x_i2c_probe()
1358 clk_disable(i2c->clk); in rk3x_i2c_probe()
1360 ret = i2c_add_adapter(&i2c->adap); in rk3x_i2c_probe()
1367 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb); in rk3x_i2c_probe()
1369 clk_unprepare(i2c->pclk); in rk3x_i2c_probe()
1371 clk_unprepare(i2c->clk); in rk3x_i2c_probe()
1377 struct rk3x_i2c *i2c = platform_get_drvdata(pdev); in rk3x_i2c_remove() local
1379 i2c_del_adapter(&i2c->adap); in rk3x_i2c_remove()
1381 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb); in rk3x_i2c_remove()
1382 clk_unprepare(i2c->pclk); in rk3x_i2c_remove()
1383 clk_unprepare(i2c->clk); in rk3x_i2c_remove()
1394 .name = "rk3x-i2c",
1402 MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");