Lines Matching refs:lpi2c_imx

113 static void lpi2c_imx_intctrl(struct lpi2c_imx_struct *lpi2c_imx,  in lpi2c_imx_intctrl()  argument
116 writel(enable, lpi2c_imx->base + LPI2C_MIER); in lpi2c_imx_intctrl()
119 static int lpi2c_imx_bus_busy(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_bus_busy() argument
125 temp = readl(lpi2c_imx->base + LPI2C_MSR); in lpi2c_imx_bus_busy()
129 writel(temp, lpi2c_imx->base + LPI2C_MSR); in lpi2c_imx_bus_busy()
137 dev_dbg(&lpi2c_imx->adapter.dev, "bus not work\n"); in lpi2c_imx_bus_busy()
138 if (lpi2c_imx->adapter.bus_recovery_info) in lpi2c_imx_bus_busy()
139 i2c_recover_bus(&lpi2c_imx->adapter); in lpi2c_imx_bus_busy()
148 static void lpi2c_imx_set_mode(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_set_mode() argument
150 unsigned int bitrate = lpi2c_imx->bitrate; in lpi2c_imx_set_mode()
164 lpi2c_imx->mode = mode; in lpi2c_imx_set_mode()
167 static int lpi2c_imx_start(struct lpi2c_imx_struct *lpi2c_imx, in lpi2c_imx_start() argument
172 temp = readl(lpi2c_imx->base + LPI2C_MCR); in lpi2c_imx_start()
174 writel(temp, lpi2c_imx->base + LPI2C_MCR); in lpi2c_imx_start()
175 writel(0x7f00, lpi2c_imx->base + LPI2C_MSR); in lpi2c_imx_start()
178 writel(temp, lpi2c_imx->base + LPI2C_MTDR); in lpi2c_imx_start()
180 return lpi2c_imx_bus_busy(lpi2c_imx); in lpi2c_imx_start()
183 static void lpi2c_imx_stop(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_stop() argument
188 writel(GEN_STOP << 8, lpi2c_imx->base + LPI2C_MTDR); in lpi2c_imx_stop()
191 temp = readl(lpi2c_imx->base + LPI2C_MSR); in lpi2c_imx_stop()
196 dev_dbg(&lpi2c_imx->adapter.dev, "stop timeout\n"); in lpi2c_imx_stop()
197 if (lpi2c_imx->adapter.bus_recovery_info) in lpi2c_imx_stop()
198 i2c_recover_bus(&lpi2c_imx->adapter); in lpi2c_imx_stop()
207 static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_config() argument
214 lpi2c_imx_set_mode(lpi2c_imx); in lpi2c_imx_config()
216 clk_rate = lpi2c_imx->rate_per; in lpi2c_imx_config()
218 if (lpi2c_imx->mode == HS || lpi2c_imx->mode == ULTRA_FAST) in lpi2c_imx_config()
224 clk_cycle = clk_rate / ((1 << prescale) * lpi2c_imx->bitrate) in lpi2c_imx_config()
236 if (lpi2c_imx->mode == ULTRA_FAST) in lpi2c_imx_config()
242 if (lpi2c_imx->mode == ULTRA_FAST) in lpi2c_imx_config()
245 writel(temp, lpi2c_imx->base + LPI2C_MCFGR1); in lpi2c_imx_config()
249 writel(temp, lpi2c_imx->base + LPI2C_MCFGR2); in lpi2c_imx_config()
256 if (lpi2c_imx->mode == HS) in lpi2c_imx_config()
257 writel(temp, lpi2c_imx->base + LPI2C_MCCR1); in lpi2c_imx_config()
259 writel(temp, lpi2c_imx->base + LPI2C_MCCR0); in lpi2c_imx_config()
264 static int lpi2c_imx_master_enable(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_master_enable() argument
269 ret = pm_runtime_resume_and_get(lpi2c_imx->adapter.dev.parent); in lpi2c_imx_master_enable()
274 writel(temp, lpi2c_imx->base + LPI2C_MCR); in lpi2c_imx_master_enable()
275 writel(0, lpi2c_imx->base + LPI2C_MCR); in lpi2c_imx_master_enable()
277 ret = lpi2c_imx_config(lpi2c_imx); in lpi2c_imx_master_enable()
281 temp = readl(lpi2c_imx->base + LPI2C_MCR); in lpi2c_imx_master_enable()
283 writel(temp, lpi2c_imx->base + LPI2C_MCR); in lpi2c_imx_master_enable()
288 pm_runtime_mark_last_busy(lpi2c_imx->adapter.dev.parent); in lpi2c_imx_master_enable()
289 pm_runtime_put_autosuspend(lpi2c_imx->adapter.dev.parent); in lpi2c_imx_master_enable()
294 static int lpi2c_imx_master_disable(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_master_disable() argument
298 temp = readl(lpi2c_imx->base + LPI2C_MCR); in lpi2c_imx_master_disable()
300 writel(temp, lpi2c_imx->base + LPI2C_MCR); in lpi2c_imx_master_disable()
302 pm_runtime_mark_last_busy(lpi2c_imx->adapter.dev.parent); in lpi2c_imx_master_disable()
303 pm_runtime_put_autosuspend(lpi2c_imx->adapter.dev.parent); in lpi2c_imx_master_disable()
308 static int lpi2c_imx_msg_complete(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_msg_complete() argument
312 time_left = wait_for_completion_timeout(&lpi2c_imx->complete, HZ); in lpi2c_imx_msg_complete()
317 static int lpi2c_imx_txfifo_empty(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_txfifo_empty() argument
323 txcnt = readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff; in lpi2c_imx_txfifo_empty()
325 if (readl(lpi2c_imx->base + LPI2C_MSR) & MSR_NDF) { in lpi2c_imx_txfifo_empty()
326 dev_dbg(&lpi2c_imx->adapter.dev, "NDF detected\n"); in lpi2c_imx_txfifo_empty()
331 dev_dbg(&lpi2c_imx->adapter.dev, "txfifo empty timeout\n"); in lpi2c_imx_txfifo_empty()
332 if (lpi2c_imx->adapter.bus_recovery_info) in lpi2c_imx_txfifo_empty()
333 i2c_recover_bus(&lpi2c_imx->adapter); in lpi2c_imx_txfifo_empty()
343 static void lpi2c_imx_set_tx_watermark(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_set_tx_watermark() argument
345 writel(lpi2c_imx->txfifosize >> 1, lpi2c_imx->base + LPI2C_MFCR); in lpi2c_imx_set_tx_watermark()
348 static void lpi2c_imx_set_rx_watermark(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_set_rx_watermark() argument
352 remaining = lpi2c_imx->msglen - lpi2c_imx->delivered; in lpi2c_imx_set_rx_watermark()
354 if (remaining > (lpi2c_imx->rxfifosize >> 1)) in lpi2c_imx_set_rx_watermark()
355 temp = lpi2c_imx->rxfifosize >> 1; in lpi2c_imx_set_rx_watermark()
359 writel(temp << 16, lpi2c_imx->base + LPI2C_MFCR); in lpi2c_imx_set_rx_watermark()
362 static void lpi2c_imx_write_txfifo(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_write_txfifo() argument
366 txcnt = readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff; in lpi2c_imx_write_txfifo()
368 while (txcnt < lpi2c_imx->txfifosize) { in lpi2c_imx_write_txfifo()
369 if (lpi2c_imx->delivered == lpi2c_imx->msglen) in lpi2c_imx_write_txfifo()
372 data = lpi2c_imx->tx_buf[lpi2c_imx->delivered++]; in lpi2c_imx_write_txfifo()
373 writel(data, lpi2c_imx->base + LPI2C_MTDR); in lpi2c_imx_write_txfifo()
377 if (lpi2c_imx->delivered < lpi2c_imx->msglen) in lpi2c_imx_write_txfifo()
378 lpi2c_imx_intctrl(lpi2c_imx, MIER_TDIE | MIER_NDIE); in lpi2c_imx_write_txfifo()
380 complete(&lpi2c_imx->complete); in lpi2c_imx_write_txfifo()
383 static void lpi2c_imx_read_rxfifo(struct lpi2c_imx_struct *lpi2c_imx) in lpi2c_imx_read_rxfifo() argument
389 data = readl(lpi2c_imx->base + LPI2C_MRDR); in lpi2c_imx_read_rxfifo()
393 lpi2c_imx->rx_buf[lpi2c_imx->delivered++] = data & 0xff; in lpi2c_imx_read_rxfifo()
400 if (lpi2c_imx->block_data) { in lpi2c_imx_read_rxfifo()
401 blocklen = lpi2c_imx->rx_buf[0]; in lpi2c_imx_read_rxfifo()
402 lpi2c_imx->msglen += blocklen; in lpi2c_imx_read_rxfifo()
405 remaining = lpi2c_imx->msglen - lpi2c_imx->delivered; in lpi2c_imx_read_rxfifo()
408 complete(&lpi2c_imx->complete); in lpi2c_imx_read_rxfifo()
413 lpi2c_imx_set_rx_watermark(lpi2c_imx); in lpi2c_imx_read_rxfifo()
416 if (lpi2c_imx->block_data) { in lpi2c_imx_read_rxfifo()
417 lpi2c_imx->block_data = 0; in lpi2c_imx_read_rxfifo()
420 writel(temp, lpi2c_imx->base + LPI2C_MTDR); in lpi2c_imx_read_rxfifo()
421 } else if (!(lpi2c_imx->delivered & 0xff)) { in lpi2c_imx_read_rxfifo()
424 writel(temp, lpi2c_imx->base + LPI2C_MTDR); in lpi2c_imx_read_rxfifo()
427 lpi2c_imx_intctrl(lpi2c_imx, MIER_RDIE); in lpi2c_imx_read_rxfifo()
430 static void lpi2c_imx_write(struct lpi2c_imx_struct *lpi2c_imx, in lpi2c_imx_write() argument
433 lpi2c_imx->tx_buf = msgs->buf; in lpi2c_imx_write()
434 lpi2c_imx_set_tx_watermark(lpi2c_imx); in lpi2c_imx_write()
435 lpi2c_imx_write_txfifo(lpi2c_imx); in lpi2c_imx_write()
438 static void lpi2c_imx_read(struct lpi2c_imx_struct *lpi2c_imx, in lpi2c_imx_read() argument
443 lpi2c_imx->rx_buf = msgs->buf; in lpi2c_imx_read()
444 lpi2c_imx->block_data = msgs->flags & I2C_M_RECV_LEN; in lpi2c_imx_read()
446 lpi2c_imx_set_rx_watermark(lpi2c_imx); in lpi2c_imx_read()
449 writel(temp, lpi2c_imx->base + LPI2C_MTDR); in lpi2c_imx_read()
451 lpi2c_imx_intctrl(lpi2c_imx, MIER_RDIE | MIER_NDIE); in lpi2c_imx_read()
457 struct lpi2c_imx_struct *lpi2c_imx = i2c_get_adapdata(adapter); in lpi2c_imx_xfer() local
461 result = lpi2c_imx_master_enable(lpi2c_imx); in lpi2c_imx_xfer()
466 result = lpi2c_imx_start(lpi2c_imx, &msgs[i]); in lpi2c_imx_xfer()
474 lpi2c_imx->rx_buf = NULL; in lpi2c_imx_xfer()
475 lpi2c_imx->tx_buf = NULL; in lpi2c_imx_xfer()
476 lpi2c_imx->delivered = 0; in lpi2c_imx_xfer()
477 lpi2c_imx->msglen = msgs[i].len; in lpi2c_imx_xfer()
478 init_completion(&lpi2c_imx->complete); in lpi2c_imx_xfer()
481 lpi2c_imx_read(lpi2c_imx, &msgs[i]); in lpi2c_imx_xfer()
483 lpi2c_imx_write(lpi2c_imx, &msgs[i]); in lpi2c_imx_xfer()
485 result = lpi2c_imx_msg_complete(lpi2c_imx); in lpi2c_imx_xfer()
490 result = lpi2c_imx_txfifo_empty(lpi2c_imx); in lpi2c_imx_xfer()
497 lpi2c_imx_stop(lpi2c_imx); in lpi2c_imx_xfer()
499 temp = readl(lpi2c_imx->base + LPI2C_MSR); in lpi2c_imx_xfer()
504 lpi2c_imx_master_disable(lpi2c_imx); in lpi2c_imx_xfer()
506 dev_dbg(&lpi2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__, in lpi2c_imx_xfer()
515 struct lpi2c_imx_struct *lpi2c_imx = dev_id; in lpi2c_imx_isr() local
519 enabled = readl(lpi2c_imx->base + LPI2C_MIER); in lpi2c_imx_isr()
521 lpi2c_imx_intctrl(lpi2c_imx, 0); in lpi2c_imx_isr()
522 temp = readl(lpi2c_imx->base + LPI2C_MSR); in lpi2c_imx_isr()
526 complete(&lpi2c_imx->complete); in lpi2c_imx_isr()
528 lpi2c_imx_read_rxfifo(lpi2c_imx); in lpi2c_imx_isr()
530 lpi2c_imx_write_txfifo(lpi2c_imx); in lpi2c_imx_isr()
535 static int lpi2c_imx_init_recovery_info(struct lpi2c_imx_struct *lpi2c_imx, in lpi2c_imx_init_recovery_info() argument
538 struct i2c_bus_recovery_info *bri = &lpi2c_imx->rinfo; in lpi2c_imx_init_recovery_info()
544 lpi2c_imx->adapter.bus_recovery_info = bri; in lpi2c_imx_init_recovery_info()
568 struct lpi2c_imx_struct *lpi2c_imx; in lpi2c_imx_probe() local
572 lpi2c_imx = devm_kzalloc(&pdev->dev, sizeof(*lpi2c_imx), GFP_KERNEL); in lpi2c_imx_probe()
573 if (!lpi2c_imx) in lpi2c_imx_probe()
576 lpi2c_imx->base = devm_platform_ioremap_resource(pdev, 0); in lpi2c_imx_probe()
577 if (IS_ERR(lpi2c_imx->base)) in lpi2c_imx_probe()
578 return PTR_ERR(lpi2c_imx->base); in lpi2c_imx_probe()
584 lpi2c_imx->adapter.owner = THIS_MODULE; in lpi2c_imx_probe()
585 lpi2c_imx->adapter.algo = &lpi2c_imx_algo; in lpi2c_imx_probe()
586 lpi2c_imx->adapter.dev.parent = &pdev->dev; in lpi2c_imx_probe()
587 lpi2c_imx->adapter.dev.of_node = pdev->dev.of_node; in lpi2c_imx_probe()
588 strscpy(lpi2c_imx->adapter.name, pdev->name, in lpi2c_imx_probe()
589 sizeof(lpi2c_imx->adapter.name)); in lpi2c_imx_probe()
591 ret = devm_clk_bulk_get_all(&pdev->dev, &lpi2c_imx->clks); in lpi2c_imx_probe()
594 lpi2c_imx->num_clks = ret; in lpi2c_imx_probe()
597 "clock-frequency", &lpi2c_imx->bitrate); in lpi2c_imx_probe()
599 lpi2c_imx->bitrate = I2C_MAX_STANDARD_MODE_FREQ; in lpi2c_imx_probe()
602 pdev->name, lpi2c_imx); in lpi2c_imx_probe()
606 i2c_set_adapdata(&lpi2c_imx->adapter, lpi2c_imx); in lpi2c_imx_probe()
607 platform_set_drvdata(pdev, lpi2c_imx); in lpi2c_imx_probe()
609 ret = clk_bulk_prepare_enable(lpi2c_imx->num_clks, lpi2c_imx->clks); in lpi2c_imx_probe()
617 ret = devm_clk_rate_exclusive_get(&pdev->dev, lpi2c_imx->clks[0].clk); in lpi2c_imx_probe()
622 lpi2c_imx->rate_per = clk_get_rate(lpi2c_imx->clks[0].clk); in lpi2c_imx_probe()
623 if (!lpi2c_imx->rate_per) in lpi2c_imx_probe()
633 temp = readl(lpi2c_imx->base + LPI2C_PARAM); in lpi2c_imx_probe()
634 lpi2c_imx->txfifosize = 1 << (temp & 0x0f); in lpi2c_imx_probe()
635 lpi2c_imx->rxfifosize = 1 << ((temp >> 8) & 0x0f); in lpi2c_imx_probe()
638 ret = lpi2c_imx_init_recovery_info(lpi2c_imx, pdev); in lpi2c_imx_probe()
643 ret = i2c_add_adapter(&lpi2c_imx->adapter); in lpi2c_imx_probe()
650 dev_info(&lpi2c_imx->adapter.dev, "LPI2C adapter registered\n"); in lpi2c_imx_probe()
664 struct lpi2c_imx_struct *lpi2c_imx = platform_get_drvdata(pdev); in lpi2c_imx_remove() local
666 i2c_del_adapter(&lpi2c_imx->adapter); in lpi2c_imx_remove()
674 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev); in lpi2c_runtime_suspend() local
676 clk_bulk_disable(lpi2c_imx->num_clks, lpi2c_imx->clks); in lpi2c_runtime_suspend()
684 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev); in lpi2c_runtime_resume() local
688 ret = clk_bulk_enable(lpi2c_imx->num_clks, lpi2c_imx->clks); in lpi2c_runtime_resume()