Lines Matching +full:i2c +full:- +full:transfer +full:- +full:timeout +full:- +full:us

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TI DAVINCI I2C adapter driver.
10 * ----------------------------------------------------------------------------
12 * ----------------------------------------------------------------------------
17 #include <linux/i2c.h>
29 #include <linux/platform_data/i2c-davinci.h>
32 /* ----- global defines ----------------------------------------------- */
117 /* timeout for pm runtime autosuspend */
147 writew_relaxed(val, i2c_dev->base + reg); in davinci_i2c_write_reg()
152 return readw_relaxed(i2c_dev->base + reg); in davinci_i2c_read_reg()
161 if (!val) /* put I2C into reset */ in davinci_i2c_reset_ctrl()
163 else /* take I2C out of reset */ in davinci_i2c_reset_ctrl()
171 struct davinci_i2c_platform_data *pdata = dev->pdata; in i2c_davinci_calc_clk_dividers()
177 u32 input_clock = clk_get_rate(dev->clk); in i2c_davinci_calc_clk_dividers()
178 struct device_node *of_node = dev->dev->of_node; in i2c_davinci_calc_clk_dividers()
180 /* NOTE: I2C Clock divider programming info in i2c_davinci_calc_clk_dividers()
181 * As per I2C specs the following formulas provide prescaler in i2c_davinci_calc_clk_dividers()
183 * input clk --> PSC Div -----------> ICCL/H Div --> output clock in i2c_davinci_calc_clk_dividers()
189 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d; in i2c_davinci_calc_clk_dividers()
196 * d is always 6 on Keystone I2C controller in i2c_davinci_calc_clk_dividers()
207 psc = (input_clock / 12000000) - 1; in i2c_davinci_calc_clk_dividers()
210 d = (psc >= 2) ? 5 : 7 - psc; in i2c_davinci_calc_clk_dividers()
212 if (of_node && of_device_is_compatible(of_node, "ti,keystone-i2c")) in i2c_davinci_calc_clk_dividers()
215 clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)); in i2c_davinci_calc_clk_dividers()
217 if (input_clock / (psc + 1) / clk > pdata->bus_freq * 1000) in i2c_davinci_calc_clk_dividers()
220 * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at in i2c_davinci_calc_clk_dividers()
221 * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH in i2c_davinci_calc_clk_dividers()
224 if (pdata->bus_freq > 100) in i2c_davinci_calc_clk_dividers()
233 * CLKH is not allowed to be 0, in this case I2C clock is not generated in i2c_davinci_calc_clk_dividers()
237 clkh = clk - clkl - d; in i2c_davinci_calc_clk_dividers()
238 clkl -= d; in i2c_davinci_calc_clk_dividers()
241 clkl = clk - (d << 1); in i2c_davinci_calc_clk_dividers()
248 dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk); in i2c_davinci_calc_clk_dividers()
252 * This function configures I2C and brings I2C out of reset.
253 * This function is called during I2C init function. This function
254 * also gets called if I2C encounters any errors.
258 struct davinci_i2c_platform_data *pdata = dev->pdata; in i2c_davinci_init()
260 /* put I2C into reset */ in i2c_davinci_init()
271 dev_dbg(dev->dev, "PSC = %d\n", in i2c_davinci_init()
273 dev_dbg(dev->dev, "CLKL = %d\n", in i2c_davinci_init()
275 dev_dbg(dev->dev, "CLKH = %d\n", in i2c_davinci_init()
277 dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n", in i2c_davinci_init()
278 pdata->bus_freq, pdata->bus_delay); in i2c_davinci_init()
281 /* Take the I2C module out of reset: */ in i2c_davinci_init()
291 * This routine does i2c bus recovery by using i2c_generic_scl_recovery
292 * which is provided by I2C Bus recovery infrastructure.
301 /* put I2C into reset */ in davinci_i2c_prepare_recovery()
368 /* change back to I2C mode */ in davinci_i2c_scl_unprepare_recovery()
388 unsigned long timeout = jiffies + dev->adapter.timeout; in i2c_davinci_wait_bus_not_busy() local
394 } while (time_before_eq(jiffies, timeout)); in i2c_davinci_wait_bus_not_busy()
396 dev_warn(dev->dev, "timeout waiting for bus ready\n"); in i2c_davinci_wait_bus_not_busy()
397 i2c_recover_bus(&dev->adapter); in i2c_davinci_wait_bus_not_busy()
401 * short-circuit in i2c_davinci_wait_bus_not_busy()
404 return -EIO; in i2c_davinci_wait_bus_not_busy()
417 struct davinci_i2c_platform_data *pdata = dev->pdata; in i2c_davinci_xfer_msg()
422 if (msg->addr == DAVINCI_I2C_OWN_ADDRESS) { in i2c_davinci_xfer_msg()
423 dev_warn(dev->dev, "transfer to own address aborted\n"); in i2c_davinci_xfer_msg()
424 return -EADDRNOTAVAIL; in i2c_davinci_xfer_msg()
428 if (pdata->bus_delay) in i2c_davinci_xfer_msg()
429 udelay(pdata->bus_delay); in i2c_davinci_xfer_msg()
432 davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr); in i2c_davinci_xfer_msg()
434 dev->buf = msg->buf; in i2c_davinci_xfer_msg()
435 dev->buf_len = msg->len; in i2c_davinci_xfer_msg()
436 dev->stop = stop; in i2c_davinci_xfer_msg()
438 davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len); in i2c_davinci_xfer_msg()
440 reinit_completion(&dev->cmd_complete); in i2c_davinci_xfer_msg()
441 dev->cmd_err = 0; in i2c_davinci_xfer_msg()
443 /* Take I2C out of reset and configure it as controller */ in i2c_davinci_xfer_msg()
446 if (msg->flags & I2C_M_TEN) in i2c_davinci_xfer_msg()
448 if (!(msg->flags & I2C_M_RD)) in i2c_davinci_xfer_msg()
450 if (msg->len == 0) in i2c_davinci_xfer_msg()
455 if (msg->flags & I2C_M_RD) in i2c_davinci_xfer_msg()
461 dev->terminate = 0; in i2c_davinci_xfer_msg()
465 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY in i2c_davinci_xfer_msg()
472 * because transmit-data-ready interrupt can come before in i2c_davinci_xfer_msg()
473 * NACK-interrupt during sending of previous message and in i2c_davinci_xfer_msg()
475 * It also saves us one interrupt, slightly faster in i2c_davinci_xfer_msg()
477 if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) { in i2c_davinci_xfer_msg()
478 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++); in i2c_davinci_xfer_msg()
479 dev->buf_len--; in i2c_davinci_xfer_msg()
484 if (stop && msg->len != 0) in i2c_davinci_xfer_msg()
488 time_left = wait_for_completion_timeout(&dev->cmd_complete, in i2c_davinci_xfer_msg()
489 dev->adapter.timeout); in i2c_davinci_xfer_msg()
492 dev->buf_len = 0; in i2c_davinci_xfer_msg()
493 return -ETIMEDOUT; in i2c_davinci_xfer_msg()
495 if (dev->buf_len) { in i2c_davinci_xfer_msg()
497 * or dev->cmd_err denotes an error. in i2c_davinci_xfer_msg()
499 dev_err(dev->dev, "abnormal termination buf_len=%zu\n", in i2c_davinci_xfer_msg()
500 dev->buf_len); in i2c_davinci_xfer_msg()
501 dev->terminate = 1; in i2c_davinci_xfer_msg()
503 dev->buf_len = 0; in i2c_davinci_xfer_msg()
504 return -EREMOTEIO; in i2c_davinci_xfer_msg()
508 if (likely(!dev->cmd_err)) in i2c_davinci_xfer_msg()
509 return msg->len; in i2c_davinci_xfer_msg()
512 if (dev->cmd_err & DAVINCI_I2C_STR_AL) { in i2c_davinci_xfer_msg()
514 return -EIO; in i2c_davinci_xfer_msg()
517 if (dev->cmd_err & DAVINCI_I2C_STR_NACK) { in i2c_davinci_xfer_msg()
518 if (msg->flags & I2C_M_IGNORE_NAK) in i2c_davinci_xfer_msg()
519 return msg->len; in i2c_davinci_xfer_msg()
523 return -EREMOTEIO; in i2c_davinci_xfer_msg()
525 return -EIO; in i2c_davinci_xfer_msg()
538 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); in i2c_davinci_xfer()
540 ret = pm_runtime_resume_and_get(dev->dev); in i2c_davinci_xfer()
542 dev_err(dev->dev, "Failed to runtime_get device: %d\n", ret); in i2c_davinci_xfer()
548 dev_warn(dev->dev, "timeout waiting for bus ready\n"); in i2c_davinci_xfer()
553 ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1))); in i2c_davinci_xfer()
554 dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num, in i2c_davinci_xfer()
563 pm_runtime_mark_last_busy(dev->dev); in i2c_davinci_xfer()
564 pm_runtime_put_autosuspend(dev->dev); in i2c_davinci_xfer()
582 if (!dev->terminate) in terminate_read()
583 dev_err(dev->dev, "RDR IRQ while no data requested\n"); in terminate_read()
591 if (!dev->terminate) in terminate_write()
592 dev_dbg(dev->dev, "TDR IRQ while no data to send\n"); in terminate_write()
596 * Interrupt service routine. This gets called whenever an I2C interrupt
606 if (pm_runtime_suspended(dev->dev)) in i2c_davinci_isr()
610 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat); in i2c_davinci_isr()
612 dev_warn(dev->dev, "Too much work in one IRQ\n"); in i2c_davinci_isr()
619 dev->cmd_err |= DAVINCI_I2C_STR_AL; in i2c_davinci_isr()
620 dev->buf_len = 0; in i2c_davinci_isr()
621 complete(&dev->cmd_complete); in i2c_davinci_isr()
625 dev->cmd_err |= DAVINCI_I2C_STR_NACK; in i2c_davinci_isr()
626 dev->buf_len = 0; in i2c_davinci_isr()
627 complete(&dev->cmd_complete); in i2c_davinci_isr()
633 if (((dev->buf_len == 0) && (dev->stop != 0)) || in i2c_davinci_isr()
634 (dev->cmd_err & DAVINCI_I2C_STR_NACK)) { in i2c_davinci_isr()
641 complete(&dev->cmd_complete); in i2c_davinci_isr()
645 if (dev->buf_len) { in i2c_davinci_isr()
646 *dev->buf++ = in i2c_davinci_isr()
649 dev->buf_len--; in i2c_davinci_isr()
650 if (dev->buf_len) in i2c_davinci_isr()
657 /* signal can terminate transfer */ in i2c_davinci_isr()
663 if (dev->buf_len) { in i2c_davinci_isr()
665 *dev->buf++); in i2c_davinci_isr()
666 dev->buf_len--; in i2c_davinci_isr()
667 if (dev->buf_len) in i2c_davinci_isr()
677 /* signal can terminate transfer */ in i2c_davinci_isr()
685 complete(&dev->cmd_complete); in i2c_davinci_isr()
689 dev_dbg(dev->dev, "Address as target interrupt\n"); in i2c_davinci_isr()
693 dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat); in i2c_davinci_isr()
709 i2c_lock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER); in i2c_davinci_cpufreq_transition()
716 i2c_unlock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER); in i2c_davinci_cpufreq_transition()
723 dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition; in i2c_davinci_cpufreq_register()
725 return cpufreq_register_notifier(&dev->freq_transition, in i2c_davinci_cpufreq_register()
731 cpufreq_unregister_notifier(&dev->freq_transition, in i2c_davinci_cpufreq_deregister()
751 {.compatible = "ti,davinci-i2c", },
752 {.compatible = "ti,keystone-i2c", },
768 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); in davinci_i2c_probe()
770 return -ENOMEM; in davinci_i2c_probe()
772 init_completion(&dev->cmd_complete); in davinci_i2c_probe()
774 dev->dev = &pdev->dev; in davinci_i2c_probe()
775 dev->irq = irq; in davinci_i2c_probe()
776 dev->pdata = dev_get_platdata(&pdev->dev); in davinci_i2c_probe()
779 if (!dev->pdata && pdev->dev.of_node) { in davinci_i2c_probe()
782 dev->pdata = devm_kzalloc(&pdev->dev, in davinci_i2c_probe()
784 if (!dev->pdata) in davinci_i2c_probe()
785 return -ENOMEM; in davinci_i2c_probe()
787 memcpy(dev->pdata, &davinci_i2c_platform_data_default, in davinci_i2c_probe()
789 if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency", in davinci_i2c_probe()
791 dev->pdata->bus_freq = prop / 1000; in davinci_i2c_probe()
793 dev->pdata->has_pfunc = in davinci_i2c_probe()
794 of_property_read_bool(pdev->dev.of_node, in davinci_i2c_probe()
795 "ti,has-pfunc"); in davinci_i2c_probe()
796 } else if (!dev->pdata) { in davinci_i2c_probe()
797 dev->pdata = &davinci_i2c_platform_data_default; in davinci_i2c_probe()
800 dev->clk = devm_clk_get(&pdev->dev, NULL); in davinci_i2c_probe()
801 if (IS_ERR(dev->clk)) in davinci_i2c_probe()
802 return PTR_ERR(dev->clk); in davinci_i2c_probe()
804 dev->base = devm_platform_ioremap_resource(pdev, 0); in davinci_i2c_probe()
805 if (IS_ERR(dev->base)) { in davinci_i2c_probe()
806 return PTR_ERR(dev->base); in davinci_i2c_probe()
809 pm_runtime_set_autosuspend_delay(dev->dev, in davinci_i2c_probe()
811 pm_runtime_use_autosuspend(dev->dev); in davinci_i2c_probe()
813 pm_runtime_enable(dev->dev); in davinci_i2c_probe()
815 r = pm_runtime_resume_and_get(dev->dev); in davinci_i2c_probe()
817 dev_err(dev->dev, "failed to runtime_get device: %d\n", r); in davinci_i2c_probe()
823 r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0, in davinci_i2c_probe()
824 pdev->name, dev); in davinci_i2c_probe()
826 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); in davinci_i2c_probe()
832 dev_err(&pdev->dev, "failed to register cpufreq\n"); in davinci_i2c_probe()
836 adap = &dev->adapter; in davinci_i2c_probe()
838 adap->owner = THIS_MODULE; in davinci_i2c_probe()
839 adap->class = I2C_CLASS_DEPRECATED; in davinci_i2c_probe()
840 strscpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name)); in davinci_i2c_probe()
841 adap->algo = &i2c_davinci_algo; in davinci_i2c_probe()
842 adap->dev.parent = &pdev->dev; in davinci_i2c_probe()
843 adap->timeout = DAVINCI_I2C_TIMEOUT; in davinci_i2c_probe()
844 adap->dev.of_node = pdev->dev.of_node; in davinci_i2c_probe()
846 if (dev->pdata->has_pfunc) in davinci_i2c_probe()
847 adap->bus_recovery_info = &davinci_i2c_scl_recovery_info; in davinci_i2c_probe()
848 else if (dev->pdata->gpio_recovery) { in davinci_i2c_probe()
850 adap->bus_recovery_info = rinfo; in davinci_i2c_probe()
851 rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl", in davinci_i2c_probe()
853 if (IS_ERR(rinfo->scl_gpiod)) { in davinci_i2c_probe()
854 r = PTR_ERR(rinfo->scl_gpiod); in davinci_i2c_probe()
857 rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN); in davinci_i2c_probe()
858 if (IS_ERR(rinfo->sda_gpiod)) { in davinci_i2c_probe()
859 r = PTR_ERR(rinfo->sda_gpiod); in davinci_i2c_probe()
864 adap->nr = pdev->id; in davinci_i2c_probe()
869 pm_runtime_mark_last_busy(dev->dev); in davinci_i2c_probe()
870 pm_runtime_put_autosuspend(dev->dev); in davinci_i2c_probe()
875 pm_runtime_dont_use_autosuspend(dev->dev); in davinci_i2c_probe()
876 pm_runtime_put_sync(dev->dev); in davinci_i2c_probe()
878 pm_runtime_disable(dev->dev); in davinci_i2c_probe()
890 i2c_del_adapter(&dev->adapter); in davinci_i2c_remove()
892 ret = pm_runtime_get_sync(&pdev->dev); in davinci_i2c_remove()
894 dev_err(&pdev->dev, "Failed to resume device\n"); in davinci_i2c_remove()
898 pm_runtime_dont_use_autosuspend(dev->dev); in davinci_i2c_remove()
899 pm_runtime_put_sync(dev->dev); in davinci_i2c_remove()
900 pm_runtime_disable(dev->dev); in davinci_i2c_remove()
907 /* put I2C into reset */ in davinci_i2c_suspend()
917 /* take I2C out of reset */ in davinci_i2c_resume()
947 /* I2C may be needed to bring up other drivers */
961 MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");