Lines Matching defs:i2c

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
180 * @clk_rate_nb: i2c clk rate change notify
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 slave device
188 * @mode: mode of i2c transfer
190 * @state: state of i2c transfer
192 * @error: error code for i2c transfer
226 static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
229 writel(value, i2c->regs + offset);
232 static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
234 return readl(i2c->regs + offset);
238 static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
240 i2c_writel(i2c, REG_INT_ALL, REG_IPD);
245 * @i2c: target controller data
247 static void rk3x_i2c_start(struct rk3x_i2c *i2c)
249 u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
251 i2c_writel(i2c, REG_INT_START, REG_IEN);
254 val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
257 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
260 i2c_writel(i2c, val, REG_CON);
265 * @i2c: target controller data
268 static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
272 i2c->processed = 0;
273 i2c->msg = NULL;
274 i2c->error = error;
276 if (i2c->is_last_msg) {
278 i2c_writel(i2c, REG_INT_STOP, REG_IEN);
280 i2c->state = STATE_STOP;
282 ctrl = i2c_readl(i2c, REG_CON);
284 i2c_writel(i2c, ctrl, REG_CON);
287 i2c->busy = false;
288 i2c->state = STATE_IDLE;
295 ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
296 i2c_writel(i2c, ctrl, REG_CON);
299 wake_up(&i2c->wait);
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)
309 unsigned int len = i2c->msg->len - i2c->processed;
312 con = i2c_readl(i2c, REG_CON);
326 if (i2c->processed != 0) {
331 i2c_writel(i2c, con, REG_CON);
332 i2c_writel(i2c, len, REG_MRXCNT);
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)
349 if ((i2c->processed == i2c->msg->len) && (cnt != 0))
352 if (i2c->processed == 0 && cnt == 0)
353 byte = (i2c->addr & 0x7f) << 1;
355 byte = i2c->msg->buf[i2c->processed++];
361 i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
363 if (i2c->processed == i2c->msg->len)
367 i2c_writel(i2c, cnt, REG_MTXCNT);
373 static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
376 rk3x_i2c_stop(i2c, -EIO);
377 dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
378 rk3x_i2c_clean_ipd(i2c);
383 i2c_writel(i2c, REG_INT_START, REG_IPD);
386 i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
389 if (i2c->mode == REG_CON_MOD_TX) {
390 i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
391 i2c->state = STATE_WRITE;
392 rk3x_i2c_fill_transmit_buf(i2c);
395 i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
396 i2c->state = STATE_READ;
397 rk3x_i2c_prepare_read(i2c);
401 static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
404 rk3x_i2c_stop(i2c, -EIO);
405 dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
406 rk3x_i2c_clean_ipd(i2c);
411 i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
414 if (i2c->processed == i2c->msg->len)
415 rk3x_i2c_stop(i2c, i2c->error);
417 rk3x_i2c_fill_transmit_buf(i2c);
420 static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
423 unsigned int len = i2c->msg->len - i2c->processed;
432 i2c_writel(i2c, REG_INT_MBRF | REG_INT_START, REG_IPD);
441 val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
444 i2c->msg->buf[i2c->processed++] = byte;
448 if (i2c->processed == i2c->msg->len)
449 rk3x_i2c_stop(i2c, i2c->error);
451 rk3x_i2c_prepare_read(i2c);
454 static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
459 rk3x_i2c_stop(i2c, -EIO);
460 dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
461 rk3x_i2c_clean_ipd(i2c);
466 i2c_writel(i2c, REG_INT_STOP, REG_IPD);
469 con = i2c_readl(i2c, REG_CON);
471 i2c_writel(i2c, con, REG_CON);
473 i2c->busy = false;
474 i2c->state = STATE_IDLE;
477 wake_up(&i2c->wait);
482 struct rk3x_i2c *i2c = dev_id;
485 spin_lock(&i2c->lock);
487 ipd = i2c_readl(i2c, REG_IPD);
488 if (i2c->state == STATE_IDLE) {
489 dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
490 rk3x_i2c_clean_ipd(i2c);
494 dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
505 i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
509 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
510 rk3x_i2c_stop(i2c, -ENXIO);
517 switch (i2c->state) {
519 rk3x_i2c_handle_start(i2c, ipd);
522 rk3x_i2c_handle_write(i2c, ipd);
525 rk3x_i2c_handle_read(i2c, ipd);
528 rk3x_i2c_handle_stop(i2c, ipd);
535 spin_unlock(&i2c->lock);
601 * This is because the i2c host on Rockchip holds the data line
792 * hardware would not output the i2c clk.
874 static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
876 struct i2c_timings *t = &i2c->t;
883 ret = i2c->soc_data->calc_timings(clk_rate, t, &calc);
886 clk_enable(i2c->pclk);
888 spin_lock_irqsave(&i2c->lock, flags);
889 val = i2c_readl(i2c, REG_CON);
892 i2c_writel(i2c, val, REG_CON);
893 i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff),
895 spin_unlock_irqrestore(&i2c->lock, flags);
897 clk_disable(i2c->pclk);
902 dev_dbg(i2c->dev,
920 * Code adapted from i2c-cadence.c.
930 struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
940 if (i2c->soc_data->calc_timings(ndata->new_rate, &i2c->t,
946 rk3x_i2c_adapt_div(i2c, ndata->new_rate);
952 rk3x_i2c_adapt_div(i2c, ndata->new_rate);
957 rk3x_i2c_adapt_div(i2c, ndata->old_rate);
966 * @i2c: target controller data
970 * Must be called with i2c->lock held.
974 static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
991 dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
1001 i2c->msg = &msgs[1];
1003 i2c->mode = REG_CON_MOD_REGISTER_TX;
1005 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
1006 i2c_writel(i2c, reg_addr, REG_MRXRADDR);
1022 i2c->mode = REG_CON_MOD_REGISTER_TX;
1023 i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
1025 i2c_writel(i2c, 0, REG_MRXRADDR);
1027 i2c->mode = REG_CON_MOD_TX;
1030 i2c->msg = &msgs[0];
1035 i2c->addr = msgs[0].addr;
1036 i2c->busy = true;
1037 i2c->state = STATE_START;
1038 i2c->processed = 0;
1039 i2c->error = 0;
1041 rk3x_i2c_clean_ipd(i2c);
1046 static int rk3x_i2c_wait_xfer_poll(struct rk3x_i2c *i2c)
1050 while (READ_ONCE(i2c->busy) &&
1053 rk3x_i2c_irq(0, i2c);
1056 return !i2c->busy;
1062 struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
1069 spin_lock_irqsave(&i2c->lock, flags);
1071 clk_enable(i2c->clk);
1072 clk_enable(i2c->pclk);
1074 i2c->is_last_msg = false;
1081 ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
1084 dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
1089 i2c->is_last_msg = true;
1091 spin_unlock_irqrestore(&i2c->lock, flags);
1094 rk3x_i2c_start(i2c);
1096 time_left = wait_event_timeout(i2c->wait, !i2c->busy,
1099 disable_irq(i2c->irq);
1100 rk3x_i2c_start(i2c);
1102 time_left = rk3x_i2c_wait_xfer_poll(i2c);
1104 enable_irq(i2c->irq);
1107 spin_lock_irqsave(&i2c->lock, flags);
1111 i2c_writel(i2c, 0, REG_IEN);
1112 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
1114 i2c_writel(i2c, val, REG_CON);
1116 i2c->state = STATE_IDLE;
1122 if (i2c->error) {
1123 ret = i2c->error;
1128 clk_disable(i2c->pclk);
1129 clk_disable(i2c->clk);
1131 spin_unlock_irqrestore(&i2c->lock, flags);
1150 struct rk3x_i2c *i2c = dev_get_drvdata(dev);
1152 rk3x_i2c_adapt_div(i2c, clk_get_rate(i2c->clk));
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;
1247 i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
1248 if (!i2c)
1252 i2c->soc_data = match->data;
1255 i2c_parse_fw_timings(&pdev->dev, &i2c->t, true);
1257 strscpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
1258 i2c->adap.owner = THIS_MODULE;
1259 i2c->adap.algo = &rk3x_i2c_algorithm;
1260 i2c->adap.retries = 3;
1261 i2c->adap.dev.of_node = np;
1262 i2c->adap.algo_data = i2c;
1263 i2c->adap.dev.parent = &pdev->dev;
1265 i2c->dev = &pdev->dev;
1267 spin_lock_init(&i2c->lock);
1268 init_waitqueue_head(&i2c->wait);
1270 i2c->regs = devm_platform_ioremap_resource(pdev, 0);
1271 if (IS_ERR(i2c->regs))
1272 return PTR_ERR(i2c->regs);
1275 bus_nr = of_alias_get_id(np, "i2c");
1281 if (i2c->soc_data->grf_offset >= 0) {
1287 "rk3x-i2c needs 'rockchip,grf' property\n");
1292 dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
1297 if (i2c->soc_data == &rv1126_soc_data && bus_nr == 2)
1303 ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
1305 dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
1316 0, dev_name(&pdev->dev), i2c);
1322 i2c->irq = irq;
1324 platform_set_drvdata(pdev, i2c);
1326 if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) {
1328 i2c->clk = devm_clk_get(&pdev->dev, NULL);
1329 i2c->pclk = i2c->clk;
1331 i2c->clk = devm_clk_get(&pdev->dev, "i2c");
1332 i2c->pclk = devm_clk_get(&pdev->dev, "pclk");
1335 if (IS_ERR(i2c->clk))
1336 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk),
1339 if (IS_ERR(i2c->pclk))
1340 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->pclk),
1343 ret = clk_prepare(i2c->clk);
1348 ret = clk_prepare(i2c->pclk);
1354 i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
1355 ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
1361 ret = clk_enable(i2c->clk);
1367 clk_rate = clk_get_rate(i2c->clk);
1368 rk3x_i2c_adapt_div(i2c, clk_rate);
1369 clk_disable(i2c->clk);
1371 ret = i2c_add_adapter(&i2c->adap);
1378 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
1380 clk_unprepare(i2c->pclk);
1382 clk_unprepare(i2c->clk);
1388 struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
1390 i2c_del_adapter(&i2c->adap);
1392 clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
1393 clk_unprepare(i2c->pclk);
1394 clk_unprepare(i2c->clk);
1403 .name = "rk3x-i2c",