Lines Matching +full:autosuspend +full:- +full:delay +full:- +full:ms
1 // SPDX-License-Identifier: GPL-2.0-or-later
10 * ----------------------------------------------------------------------------
12 * ----------------------------------------------------------------------------
16 #include <linux/delay.h>
29 #include <linux/platform_data/i2c-davinci.h>
32 /* ----- global defines ----------------------------------------------- */
117 /* timeout for pm runtime autosuspend */
118 #define DAVINCI_I2C_PM_TIMEOUT 1000 /* ms */
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()
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()
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()
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()
224 if (pdata->bus_freq > 100) 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()
258 struct davinci_i2c_platform_data *pdata = dev->pdata; 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()
388 unsigned long timeout = jiffies + dev->adapter.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()
427 /* Introduce a delay, required for some boards (e.g Davinci EVM) */ 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()
447 if (msg->flags & I2C_M_TEN) in i2c_davinci_xfer_msg()
449 if (!(msg->flags & I2C_M_RD)) in i2c_davinci_xfer_msg()
451 if (msg->len == 0) in i2c_davinci_xfer_msg()
456 if (msg->flags & I2C_M_RD) in i2c_davinci_xfer_msg()
462 dev->terminate = 0; in i2c_davinci_xfer_msg()
466 * on OMAP-L138, but don't set STT yet to avoid a race with XRDY in i2c_davinci_xfer_msg()
473 * because transmit-data-ready interrupt can come before in i2c_davinci_xfer_msg()
474 * NACK-interrupt during sending of previous message and in i2c_davinci_xfer_msg()
478 if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) { in i2c_davinci_xfer_msg()
479 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++); in i2c_davinci_xfer_msg()
480 dev->buf_len--; in i2c_davinci_xfer_msg()
485 if (stop && msg->len != 0) in i2c_davinci_xfer_msg()
489 time_left = wait_for_completion_timeout(&dev->cmd_complete, in i2c_davinci_xfer_msg()
490 dev->adapter.timeout); in i2c_davinci_xfer_msg()
492 dev_err(dev->dev, "controller timed out\n"); in i2c_davinci_xfer_msg()
494 dev->buf_len = 0; in i2c_davinci_xfer_msg()
495 return -ETIMEDOUT; in i2c_davinci_xfer_msg()
497 if (dev->buf_len) { in i2c_davinci_xfer_msg()
499 * or dev->cmd_err denotes an error. in i2c_davinci_xfer_msg()
501 dev_err(dev->dev, "abnormal termination buf_len=%zu\n", in i2c_davinci_xfer_msg()
502 dev->buf_len); in i2c_davinci_xfer_msg()
503 dev->terminate = 1; in i2c_davinci_xfer_msg()
505 dev->buf_len = 0; in i2c_davinci_xfer_msg()
506 return -EREMOTEIO; in i2c_davinci_xfer_msg()
510 if (likely(!dev->cmd_err)) in i2c_davinci_xfer_msg()
511 return msg->len; in i2c_davinci_xfer_msg()
514 if (dev->cmd_err & DAVINCI_I2C_STR_AL) { in i2c_davinci_xfer_msg()
516 return -EIO; in i2c_davinci_xfer_msg()
519 if (dev->cmd_err & DAVINCI_I2C_STR_NACK) { in i2c_davinci_xfer_msg()
520 if (msg->flags & I2C_M_IGNORE_NAK) in i2c_davinci_xfer_msg()
521 return msg->len; in i2c_davinci_xfer_msg()
525 return -EREMOTEIO; in i2c_davinci_xfer_msg()
527 return -EIO; in i2c_davinci_xfer_msg()
540 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); in i2c_davinci_xfer()
542 ret = pm_runtime_resume_and_get(dev->dev); in i2c_davinci_xfer()
544 dev_err(dev->dev, "Failed to runtime_get device: %d\n", ret); in i2c_davinci_xfer()
550 dev_warn(dev->dev, "timeout waiting for bus ready\n"); in i2c_davinci_xfer()
555 ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1))); in i2c_davinci_xfer()
556 dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num, in i2c_davinci_xfer()
565 pm_runtime_mark_last_busy(dev->dev); in i2c_davinci_xfer()
566 pm_runtime_put_autosuspend(dev->dev); in i2c_davinci_xfer()
584 if (!dev->terminate) in terminate_read()
585 dev_err(dev->dev, "RDR IRQ while no data requested\n"); in terminate_read()
593 if (!dev->terminate) in terminate_write()
594 dev_dbg(dev->dev, "TDR IRQ while no data to send\n"); in terminate_write()
608 if (pm_runtime_suspended(dev->dev)) in i2c_davinci_isr()
612 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat); in i2c_davinci_isr()
614 dev_warn(dev->dev, "Too much work in one IRQ\n"); in i2c_davinci_isr()
621 dev->cmd_err |= DAVINCI_I2C_STR_AL; in i2c_davinci_isr()
622 dev->buf_len = 0; in i2c_davinci_isr()
623 complete(&dev->cmd_complete); in i2c_davinci_isr()
627 dev->cmd_err |= DAVINCI_I2C_STR_NACK; in i2c_davinci_isr()
628 dev->buf_len = 0; in i2c_davinci_isr()
629 complete(&dev->cmd_complete); in i2c_davinci_isr()
635 if (((dev->buf_len == 0) && (dev->stop != 0)) || in i2c_davinci_isr()
636 (dev->cmd_err & DAVINCI_I2C_STR_NACK)) { in i2c_davinci_isr()
643 complete(&dev->cmd_complete); in i2c_davinci_isr()
647 if (dev->buf_len) { in i2c_davinci_isr()
648 *dev->buf++ = in i2c_davinci_isr()
651 dev->buf_len--; in i2c_davinci_isr()
652 if (dev->buf_len) in i2c_davinci_isr()
665 if (dev->buf_len) { in i2c_davinci_isr()
667 *dev->buf++); in i2c_davinci_isr()
668 dev->buf_len--; in i2c_davinci_isr()
669 if (dev->buf_len) in i2c_davinci_isr()
687 complete(&dev->cmd_complete); in i2c_davinci_isr()
691 dev_dbg(dev->dev, "Address as slave interrupt\n"); in i2c_davinci_isr()
695 dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat); in i2c_davinci_isr()
711 i2c_lock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER); in i2c_davinci_cpufreq_transition()
718 i2c_unlock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER); in i2c_davinci_cpufreq_transition()
725 dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition; in i2c_davinci_cpufreq_register()
727 return cpufreq_register_notifier(&dev->freq_transition, in i2c_davinci_cpufreq_register()
733 cpufreq_unregister_notifier(&dev->freq_transition, in i2c_davinci_cpufreq_deregister()
753 {.compatible = "ti,davinci-i2c", },
754 {.compatible = "ti,keystone-i2c", },
769 irq = -ENXIO; in davinci_i2c_probe()
770 return dev_err_probe(&pdev->dev, irq, "can't get irq resource\n"); in davinci_i2c_probe()
773 dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev), in davinci_i2c_probe()
776 dev_err(&pdev->dev, "Memory allocation failed\n"); in davinci_i2c_probe()
777 return -ENOMEM; in davinci_i2c_probe()
780 init_completion(&dev->cmd_complete); in davinci_i2c_probe()
782 dev->dev = &pdev->dev; in davinci_i2c_probe()
783 dev->irq = irq; in davinci_i2c_probe()
784 dev->pdata = dev_get_platdata(&pdev->dev); in davinci_i2c_probe()
787 if (!dev->pdata && pdev->dev.of_node) { in davinci_i2c_probe()
790 dev->pdata = devm_kzalloc(&pdev->dev, in davinci_i2c_probe()
792 if (!dev->pdata) in davinci_i2c_probe()
793 return -ENOMEM; in davinci_i2c_probe()
795 memcpy(dev->pdata, &davinci_i2c_platform_data_default, in davinci_i2c_probe()
797 if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency", in davinci_i2c_probe()
799 dev->pdata->bus_freq = prop / 1000; in davinci_i2c_probe()
801 dev->pdata->has_pfunc = in davinci_i2c_probe()
802 of_property_read_bool(pdev->dev.of_node, in davinci_i2c_probe()
803 "ti,has-pfunc"); in davinci_i2c_probe()
804 } else if (!dev->pdata) { in davinci_i2c_probe()
805 dev->pdata = &davinci_i2c_platform_data_default; in davinci_i2c_probe()
808 dev->clk = devm_clk_get(&pdev->dev, NULL); in davinci_i2c_probe()
809 if (IS_ERR(dev->clk)) in davinci_i2c_probe()
810 return PTR_ERR(dev->clk); in davinci_i2c_probe()
812 dev->base = devm_platform_ioremap_resource(pdev, 0); in davinci_i2c_probe()
813 if (IS_ERR(dev->base)) { in davinci_i2c_probe()
814 return PTR_ERR(dev->base); in davinci_i2c_probe()
817 pm_runtime_set_autosuspend_delay(dev->dev, in davinci_i2c_probe()
819 pm_runtime_use_autosuspend(dev->dev); in davinci_i2c_probe()
821 pm_runtime_enable(dev->dev); in davinci_i2c_probe()
823 r = pm_runtime_resume_and_get(dev->dev); in davinci_i2c_probe()
825 dev_err(dev->dev, "failed to runtime_get device: %d\n", r); in davinci_i2c_probe()
831 r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0, in davinci_i2c_probe()
832 pdev->name, dev); in davinci_i2c_probe()
834 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); in davinci_i2c_probe()
840 dev_err(&pdev->dev, "failed to register cpufreq\n"); in davinci_i2c_probe()
844 adap = &dev->adapter; in davinci_i2c_probe()
846 adap->owner = THIS_MODULE; in davinci_i2c_probe()
847 adap->class = I2C_CLASS_DEPRECATED; in davinci_i2c_probe()
848 strscpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name)); in davinci_i2c_probe()
849 adap->algo = &i2c_davinci_algo; in davinci_i2c_probe()
850 adap->dev.parent = &pdev->dev; in davinci_i2c_probe()
851 adap->timeout = DAVINCI_I2C_TIMEOUT; in davinci_i2c_probe()
852 adap->dev.of_node = pdev->dev.of_node; in davinci_i2c_probe()
854 if (dev->pdata->has_pfunc) in davinci_i2c_probe()
855 adap->bus_recovery_info = &davinci_i2c_scl_recovery_info; in davinci_i2c_probe()
856 else if (dev->pdata->gpio_recovery) { in davinci_i2c_probe()
858 adap->bus_recovery_info = rinfo; in davinci_i2c_probe()
859 rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl", in davinci_i2c_probe()
861 if (IS_ERR(rinfo->scl_gpiod)) { in davinci_i2c_probe()
862 r = PTR_ERR(rinfo->scl_gpiod); in davinci_i2c_probe()
865 rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN); in davinci_i2c_probe()
866 if (IS_ERR(rinfo->sda_gpiod)) { in davinci_i2c_probe()
867 r = PTR_ERR(rinfo->sda_gpiod); in davinci_i2c_probe()
872 adap->nr = pdev->id; in davinci_i2c_probe()
877 pm_runtime_mark_last_busy(dev->dev); in davinci_i2c_probe()
878 pm_runtime_put_autosuspend(dev->dev); in davinci_i2c_probe()
883 pm_runtime_dont_use_autosuspend(dev->dev); in davinci_i2c_probe()
884 pm_runtime_put_sync(dev->dev); in davinci_i2c_probe()
886 pm_runtime_disable(dev->dev); in davinci_i2c_probe()
898 i2c_del_adapter(&dev->adapter); in davinci_i2c_remove()
900 ret = pm_runtime_resume_and_get(&pdev->dev); in davinci_i2c_remove()
906 pm_runtime_dont_use_autosuspend(dev->dev); in davinci_i2c_remove()
907 pm_runtime_put_sync(dev->dev); in davinci_i2c_remove()
908 pm_runtime_disable(dev->dev); in davinci_i2c_remove()