Lines Matching +full:geni +full:- +full:i2c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
6 #include <linux/dma-mapping.h>
8 #include <linux/i2c.h>
15 #include <linux/qcom-geni-se.h>
26 /* M_CMD OP codes for I2C */
33 /* M_CMD params for I2C */
44 /* I2C SCL COUNTER fields */
97 [GP_IRQ0] = {-EIO, "Unknown I2C err GP_IRQ0"},
98 [NACK] = {-ENXIO, "NACK: slv unresponsive, check its power/reset-ln"},
99 [GP_IRQ2] = {-EIO, "Unknown I2C err GP IRQ2"},
100 [BUS_PROTO] = {-EPROTO, "Bus proto err, noisy/unepxected start/stop"},
101 [ARB_LOST] = {-EAGAIN, "Bus arbitration lost, clock line undriveable"},
102 [GP_IRQ5] = {-EIO, "Unknown I2C err GP IRQ5"},
103 [GENI_OVERRUN] = {-EIO, "Cmd overrun, check GENI cmd-state machine"},
104 [GENI_ILLEGAL_CMD] = {-EIO, "Illegal cmd, check GENI cmd-state machine"},
105 [GENI_ABORT_DONE] = {-ETIMEDOUT, "Abort after timeout successful"},
106 [GENI_TIMEOUT] = {-ETIMEDOUT, "I2C TXN timed out"},
141 if (itr->clk_freq_out == gi2c->clk_freq_out) { in geni_i2c_clk_map_idx()
142 gi2c->clk_fld = itr; in geni_i2c_clk_map_idx()
146 return -EINVAL; in geni_i2c_clk_map_idx()
151 const struct geni_i2c_clk_fld *itr = gi2c->clk_fld; in qcom_geni_i2c_conf()
154 writel_relaxed(0, gi2c->se.base + SE_GENI_CLK_SEL); in qcom_geni_i2c_conf()
156 val = (itr->clk_div << CLK_DIV_SHFT) | SER_CLK_EN; in qcom_geni_i2c_conf()
157 writel_relaxed(val, gi2c->se.base + GENI_SER_M_CLK_CFG); in qcom_geni_i2c_conf()
159 val = itr->t_high_cnt << HIGH_COUNTER_SHFT; in qcom_geni_i2c_conf()
160 val |= itr->t_low_cnt << LOW_COUNTER_SHFT; in qcom_geni_i2c_conf()
161 val |= itr->t_cycle_cnt; in qcom_geni_i2c_conf()
162 writel_relaxed(val, gi2c->se.base + SE_I2C_SCL_COUNTERS); in qcom_geni_i2c_conf()
167 u32 m_cmd = readl_relaxed(gi2c->se.base + SE_GENI_M_CMD0); in geni_i2c_err_misc()
168 u32 m_stat = readl_relaxed(gi2c->se.base + SE_GENI_M_IRQ_STATUS); in geni_i2c_err_misc()
169 u32 geni_s = readl_relaxed(gi2c->se.base + SE_GENI_STATUS); in geni_i2c_err_misc()
170 u32 geni_ios = readl_relaxed(gi2c->se.base + SE_GENI_IOS); in geni_i2c_err_misc()
171 u32 dma = readl_relaxed(gi2c->se.base + SE_GENI_DMA_MODE_EN); in geni_i2c_err_misc()
175 rx_st = readl_relaxed(gi2c->se.base + SE_DMA_RX_IRQ_STAT); in geni_i2c_err_misc()
176 tx_st = readl_relaxed(gi2c->se.base + SE_DMA_TX_IRQ_STAT); in geni_i2c_err_misc()
178 rx_st = readl_relaxed(gi2c->se.base + SE_GENI_RX_FIFO_STATUS); in geni_i2c_err_misc()
179 tx_st = readl_relaxed(gi2c->se.base + SE_GENI_TX_FIFO_STATUS); in geni_i2c_err_misc()
181 dev_dbg(gi2c->se.dev, "DMA:%d tx_stat:0x%x, rx_stat:0x%x, irq-stat:0x%x\n", in geni_i2c_err_misc()
183 dev_dbg(gi2c->se.dev, "m_cmd:0x%x, geni_status:0x%x, geni_ios:0x%x\n", in geni_i2c_err_misc()
189 if (!gi2c->err) in geni_i2c_err()
190 gi2c->err = gi2c_log[err].err; in geni_i2c_err()
191 if (gi2c->cur) in geni_i2c_err()
192 dev_dbg(gi2c->se.dev, "len:%d, slv-addr:0x%x, RD/WR:%d\n", in geni_i2c_err()
193 gi2c->cur->len, gi2c->cur->addr, gi2c->cur->flags); in geni_i2c_err()
196 dev_err(gi2c->se.dev, "%s\n", gi2c_log[err].msg); in geni_i2c_err()
204 void __iomem *base = gi2c->se.base; in geni_i2c_irq()
214 spin_lock(&gi2c->lock); in geni_i2c_irq()
220 cur = gi2c->cur; in geni_i2c_irq()
244 dev_dbg(gi2c->se.dev, "i2c dma tx:0x%x, dma rx:0x%x\n", in geni_i2c_irq()
246 } else if (cur->flags & I2C_M_RD && in geni_i2c_irq()
253 while (gi2c->cur_rd < cur->len && p < sizeof(val)) { in geni_i2c_irq()
254 cur->buf[gi2c->cur_rd++] = val & 0xff; in geni_i2c_irq()
258 if (gi2c->cur_rd == cur->len) in geni_i2c_irq()
261 } else if (!(cur->flags & I2C_M_RD) && in geni_i2c_irq()
263 for (j = 0; j < gi2c->tx_wm; j++) { in geni_i2c_irq()
268 while (gi2c->cur_wr < cur->len && p < sizeof(val)) { in geni_i2c_irq()
269 temp = cur->buf[gi2c->cur_wr++]; in geni_i2c_irq()
275 if (gi2c->cur_wr == cur->len) { in geni_i2c_irq()
290 /* if this is err with done-bit not set, handle that through timeout. */ in geni_i2c_irq()
294 complete(&gi2c->done); in geni_i2c_irq()
296 spin_unlock(&gi2c->lock); in geni_i2c_irq()
307 spin_lock_irqsave(&gi2c->lock, flags); in geni_i2c_abort_xfer()
309 gi2c->cur = NULL; in geni_i2c_abort_xfer()
310 geni_se_abort_m_cmd(&gi2c->se); in geni_i2c_abort_xfer()
311 spin_unlock_irqrestore(&gi2c->lock, flags); in geni_i2c_abort_xfer()
313 time_left = wait_for_completion_timeout(&gi2c->done, time_left); in geni_i2c_abort_xfer()
314 val = readl_relaxed(gi2c->se.base + SE_GENI_M_IRQ_STATUS); in geni_i2c_abort_xfer()
318 dev_err(gi2c->se.dev, "Timeout abort_m_cmd\n"); in geni_i2c_abort_xfer()
326 writel_relaxed(1, gi2c->se.base + SE_DMA_RX_FSM_RST); in geni_i2c_rx_fsm_rst()
328 time_left = wait_for_completion_timeout(&gi2c->done, time_left); in geni_i2c_rx_fsm_rst()
329 val = readl_relaxed(gi2c->se.base + SE_DMA_RX_IRQ_STAT); in geni_i2c_rx_fsm_rst()
333 dev_err(gi2c->se.dev, "Timeout resetting RX_FSM\n"); in geni_i2c_rx_fsm_rst()
341 writel_relaxed(1, gi2c->se.base + SE_DMA_TX_FSM_RST); in geni_i2c_tx_fsm_rst()
343 time_left = wait_for_completion_timeout(&gi2c->done, time_left); in geni_i2c_tx_fsm_rst()
344 val = readl_relaxed(gi2c->se.base + SE_DMA_TX_IRQ_STAT); in geni_i2c_tx_fsm_rst()
348 dev_err(gi2c->se.dev, "Timeout resetting TX_FSM\n"); in geni_i2c_tx_fsm_rst()
357 struct geni_se *se = &gi2c->se; in geni_i2c_rx_one_msg()
358 size_t len = msg->len; in geni_i2c_rx_one_msg()
360 if (!of_machine_is_compatible("lenovo,yoga-c630")) in geni_i2c_rx_one_msg()
368 writel_relaxed(len, se->base + SE_I2C_RX_TRANS_LEN); in geni_i2c_rx_one_msg()
378 time_left = wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT); in geni_i2c_rx_one_msg()
382 gi2c->cur_rd = 0; in geni_i2c_rx_one_msg()
384 if (gi2c->err) in geni_i2c_rx_one_msg()
387 i2c_put_dma_safe_msg_buf(dma_buf, msg, !gi2c->err); in geni_i2c_rx_one_msg()
390 return gi2c->err; in geni_i2c_rx_one_msg()
399 struct geni_se *se = &gi2c->se; in geni_i2c_tx_one_msg()
400 size_t len = msg->len; in geni_i2c_tx_one_msg()
402 if (!of_machine_is_compatible("lenovo,yoga-c630")) in geni_i2c_tx_one_msg()
410 writel_relaxed(len, se->base + SE_I2C_TX_TRANS_LEN); in geni_i2c_tx_one_msg()
421 writel_relaxed(1, se->base + SE_GENI_TX_WATERMARK_REG); in geni_i2c_tx_one_msg()
423 time_left = wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT); in geni_i2c_tx_one_msg()
427 gi2c->cur_wr = 0; in geni_i2c_tx_one_msg()
429 if (gi2c->err) in geni_i2c_tx_one_msg()
432 i2c_put_dma_safe_msg_buf(dma_buf, msg, !gi2c->err); in geni_i2c_tx_one_msg()
435 return gi2c->err; in geni_i2c_tx_one_msg()
445 gi2c->err = 0; in geni_i2c_xfer()
446 reinit_completion(&gi2c->done); in geni_i2c_xfer()
447 ret = pm_runtime_get_sync(gi2c->se.dev); in geni_i2c_xfer()
449 dev_err(gi2c->se.dev, "error turning SE resources:%d\n", ret); in geni_i2c_xfer()
450 pm_runtime_put_noidle(gi2c->se.dev); in geni_i2c_xfer()
452 pm_runtime_set_suspended(gi2c->se.dev); in geni_i2c_xfer()
458 u32 m_param = i < (num - 1) ? STOP_STRETCH : 0; in geni_i2c_xfer()
462 gi2c->cur = &msgs[i]; in geni_i2c_xfer()
474 pm_runtime_mark_last_busy(gi2c->se.dev); in geni_i2c_xfer()
475 pm_runtime_put_autosuspend(gi2c->se.dev); in geni_i2c_xfer()
476 gi2c->cur = NULL; in geni_i2c_xfer()
477 gi2c->err = 0; in geni_i2c_xfer()
505 struct device *dev = &pdev->dev; in geni_i2c_probe()
509 return -ENOMEM; in geni_i2c_probe()
511 gi2c->se.dev = dev; in geni_i2c_probe()
512 gi2c->se.wrapper = dev_get_drvdata(dev->parent); in geni_i2c_probe()
514 gi2c->se.base = devm_ioremap_resource(dev, res); in geni_i2c_probe()
515 if (IS_ERR(gi2c->se.base)) in geni_i2c_probe()
516 return PTR_ERR(gi2c->se.base); in geni_i2c_probe()
518 gi2c->se.clk = devm_clk_get(dev, "se"); in geni_i2c_probe()
519 if (IS_ERR(gi2c->se.clk) && !has_acpi_companion(dev)) in geni_i2c_probe()
520 return PTR_ERR(gi2c->se.clk); in geni_i2c_probe()
522 ret = device_property_read_u32(dev, "clock-frequency", in geni_i2c_probe()
523 &gi2c->clk_freq_out); in geni_i2c_probe()
526 gi2c->clk_freq_out = KHZ(100); in geni_i2c_probe()
530 ACPI_COMPANION_SET(&gi2c->adap.dev, ACPI_COMPANION(dev)); in geni_i2c_probe()
532 gi2c->irq = platform_get_irq(pdev, 0); in geni_i2c_probe()
533 if (gi2c->irq < 0) in geni_i2c_probe()
534 return gi2c->irq; in geni_i2c_probe()
539 gi2c->clk_freq_out, ret); in geni_i2c_probe()
543 gi2c->adap.algo = &geni_i2c_algo; in geni_i2c_probe()
544 init_completion(&gi2c->done); in geni_i2c_probe()
545 spin_lock_init(&gi2c->lock); in geni_i2c_probe()
547 ret = devm_request_irq(dev, gi2c->irq, geni_i2c_irq, 0, in geni_i2c_probe()
551 gi2c->irq, ret); in geni_i2c_probe()
554 /* Disable the interrupt so that the system can enter low-power mode */ in geni_i2c_probe()
555 disable_irq(gi2c->irq); in geni_i2c_probe()
556 i2c_set_adapdata(&gi2c->adap, gi2c); in geni_i2c_probe()
557 gi2c->adap.dev.parent = dev; in geni_i2c_probe()
558 gi2c->adap.dev.of_node = dev->of_node; in geni_i2c_probe()
559 strlcpy(gi2c->adap.name, "Geni-I2C", sizeof(gi2c->adap.name)); in geni_i2c_probe()
561 ret = geni_icc_get(&gi2c->se, "qup-memory"); in geni_i2c_probe()
569 gi2c->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW; in geni_i2c_probe()
570 gi2c->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW; in geni_i2c_probe()
571 gi2c->se.icc_paths[GENI_TO_DDR].avg_bw = Bps_to_icc(gi2c->clk_freq_out); in geni_i2c_probe()
573 ret = geni_icc_set_bw(&gi2c->se); in geni_i2c_probe()
577 ret = geni_se_resources_on(&gi2c->se); in geni_i2c_probe()
582 proto = geni_se_read_proto(&gi2c->se); in geni_i2c_probe()
583 tx_depth = geni_se_get_tx_fifo_depth(&gi2c->se); in geni_i2c_probe()
586 geni_se_resources_off(&gi2c->se); in geni_i2c_probe()
587 return -ENXIO; in geni_i2c_probe()
589 gi2c->tx_wm = tx_depth - 1; in geni_i2c_probe()
590 geni_se_init(&gi2c->se, gi2c->tx_wm, tx_depth); in geni_i2c_probe()
591 geni_se_config_packing(&gi2c->se, BITS_PER_BYTE, PACKING_BYTES_PW, in geni_i2c_probe()
593 ret = geni_se_resources_off(&gi2c->se); in geni_i2c_probe()
599 ret = geni_icc_disable(&gi2c->se); in geni_i2c_probe()
603 dev_dbg(dev, "i2c fifo/se-dma mode. fifo depth:%d\n", tx_depth); in geni_i2c_probe()
605 gi2c->suspended = 1; in geni_i2c_probe()
606 pm_runtime_set_suspended(gi2c->se.dev); in geni_i2c_probe()
607 pm_runtime_set_autosuspend_delay(gi2c->se.dev, I2C_AUTO_SUSPEND_DELAY); in geni_i2c_probe()
608 pm_runtime_use_autosuspend(gi2c->se.dev); in geni_i2c_probe()
609 pm_runtime_enable(gi2c->se.dev); in geni_i2c_probe()
611 ret = i2c_add_adapter(&gi2c->adap); in geni_i2c_probe()
613 dev_err(dev, "Error adding i2c adapter %d\n", ret); in geni_i2c_probe()
614 pm_runtime_disable(gi2c->se.dev); in geni_i2c_probe()
618 dev_dbg(dev, "Geni-I2C adaptor successfully added\n"); in geni_i2c_probe()
627 i2c_del_adapter(&gi2c->adap); in geni_i2c_remove()
628 pm_runtime_disable(gi2c->se.dev); in geni_i2c_remove()
637 disable_irq(gi2c->irq); in geni_i2c_runtime_suspend()
638 ret = geni_se_resources_off(&gi2c->se); in geni_i2c_runtime_suspend()
640 enable_irq(gi2c->irq); in geni_i2c_runtime_suspend()
644 gi2c->suspended = 1; in geni_i2c_runtime_suspend()
647 return geni_icc_disable(&gi2c->se); in geni_i2c_runtime_suspend()
655 ret = geni_icc_enable(&gi2c->se); in geni_i2c_runtime_resume()
659 ret = geni_se_resources_on(&gi2c->se); in geni_i2c_runtime_resume()
663 enable_irq(gi2c->irq); in geni_i2c_runtime_resume()
664 gi2c->suspended = 0; in geni_i2c_runtime_resume()
672 if (!gi2c->suspended) { in geni_i2c_suspend_noirq()
688 { .compatible = "qcom,geni-i2c" },
706 MODULE_DESCRIPTION("I2C Controller Driver for GENI based QUP cores");