Lines Matching +full:autosuspend +full:- +full:delay +full:- +full:ms

4  * SPDX-License-Identifier: (GPL-2.0+ OR MIT)
8 #include <linux/delay.h>
73 /* timeout (ms) for pm runtime autosuspend */
75 /* timeout (ms) for transfer message */
96 writel(count, i2c_dev->base + I2C_COUNT); in sprd_i2c_set_count()
101 u32 tmp = readl(i2c_dev->base + I2C_CTL); in sprd_i2c_send_stop()
104 writel(tmp & ~STP_EN, i2c_dev->base + I2C_CTL); in sprd_i2c_send_stop()
106 writel(tmp | STP_EN, i2c_dev->base + I2C_CTL); in sprd_i2c_send_stop()
111 u32 tmp = readl(i2c_dev->base + I2C_CTL); in sprd_i2c_clear_start()
113 writel(tmp & ~I2C_START, i2c_dev->base + I2C_CTL); in sprd_i2c_clear_start()
118 u32 tmp = readl(i2c_dev->base + I2C_STATUS); in sprd_i2c_clear_ack()
120 writel(tmp & ~I2C_RX_ACK, i2c_dev->base + I2C_STATUS); in sprd_i2c_clear_ack()
125 u32 tmp = readl(i2c_dev->base + I2C_STATUS); in sprd_i2c_clear_irq()
127 writel(tmp & ~I2C_INT, i2c_dev->base + I2C_STATUS); in sprd_i2c_clear_irq()
132 writel(I2C_RST, i2c_dev->base + ADDR_RST); in sprd_i2c_reset_fifo()
137 writel(m->addr << 1, i2c_dev->base + I2C_ADDR_CFG); in sprd_i2c_set_devaddr()
145 writeb(buf[i], i2c_dev->base + I2C_TX); in sprd_i2c_write_bytes()
153 buf[i] = readb(i2c_dev->base + I2C_RX); in sprd_i2c_read_bytes()
158 u32 tmp = readl(i2c_dev->base + I2C_CTL); in sprd_i2c_set_full_thld()
162 writel(tmp, i2c_dev->base + I2C_CTL); in sprd_i2c_set_full_thld()
167 u32 tmp = readl(i2c_dev->base + I2C_CTL); in sprd_i2c_set_empty_thld()
171 writel(tmp, i2c_dev->base + I2C_CTL); in sprd_i2c_set_empty_thld()
176 u32 tmp = readl(i2c_dev->base + I2C_CTL); in sprd_i2c_set_fifo_full_int()
183 writel(tmp, i2c_dev->base + I2C_CTL); in sprd_i2c_set_fifo_full_int()
188 u32 tmp = readl(i2c_dev->base + I2C_CTL); in sprd_i2c_set_fifo_empty_int()
195 writel(tmp, i2c_dev->base + I2C_CTL); in sprd_i2c_set_fifo_empty_int()
200 u32 tmp = readl(i2c_dev->base + I2C_CTL); in sprd_i2c_opt_start()
202 writel(tmp | I2C_START, i2c_dev->base + I2C_CTL); in sprd_i2c_opt_start()
207 u32 cmd = readl(i2c_dev->base + I2C_CTL) & ~I2C_MODE; in sprd_i2c_opt_mode()
209 writel(cmd | rw << 3, i2c_dev->base + I2C_CTL); in sprd_i2c_opt_mode()
214 u32 i2c_count = i2c_dev->count; in sprd_i2c_data_transfer()
216 struct i2c_msg *msg = i2c_dev->msg; in sprd_i2c_data_transfer()
218 if (msg->flags & I2C_M_RD) { in sprd_i2c_data_transfer()
219 sprd_i2c_read_bytes(i2c_dev, i2c_dev->buf, I2C_FIFO_FULL_THLD); in sprd_i2c_data_transfer()
220 i2c_dev->count -= I2C_FIFO_FULL_THLD; in sprd_i2c_data_transfer()
221 i2c_dev->buf += I2C_FIFO_FULL_THLD; in sprd_i2c_data_transfer()
228 if (i2c_dev->count >= I2C_FIFO_FULL_THLD) in sprd_i2c_data_transfer()
231 sprd_i2c_write_bytes(i2c_dev, i2c_dev->buf, need_tran); in sprd_i2c_data_transfer()
232 i2c_dev->buf += need_tran; in sprd_i2c_data_transfer()
233 i2c_dev->count -= need_tran; in sprd_i2c_data_transfer()
248 struct sprd_i2c *i2c_dev = i2c_adap->algo_data; in sprd_i2c_handle_msg()
251 i2c_dev->msg = msg; in sprd_i2c_handle_msg()
252 i2c_dev->buf = msg->buf; in sprd_i2c_handle_msg()
253 i2c_dev->count = msg->len; in sprd_i2c_handle_msg()
255 reinit_completion(&i2c_dev->complete); in sprd_i2c_handle_msg()
258 sprd_i2c_set_count(i2c_dev, msg->len); in sprd_i2c_handle_msg()
260 if (msg->flags & I2C_M_RD) { in sprd_i2c_handle_msg()
272 if (msg->flags & I2C_M_RD) in sprd_i2c_handle_msg()
279 time_left = wait_for_completion_timeout(&i2c_dev->complete, in sprd_i2c_handle_msg()
282 return -ETIMEDOUT; in sprd_i2c_handle_msg()
284 return i2c_dev->err; in sprd_i2c_handle_msg()
290 struct sprd_i2c *i2c_dev = i2c_adap->algo_data; in sprd_i2c_master_xfer()
293 ret = pm_runtime_resume_and_get(i2c_dev->dev); in sprd_i2c_master_xfer()
297 for (im = 0; im < num - 1; im++) { in sprd_i2c_master_xfer()
306 pm_runtime_mark_last_busy(i2c_dev->dev); in sprd_i2c_master_xfer()
307 pm_runtime_put_autosuspend(i2c_dev->dev); in sprd_i2c_master_xfer()
324 u32 apb_clk = i2c_dev->src_clk; in sprd_i2c_set_clk()
327 * prescale = freq_i2c / (4 * freq_scl) - 1; in sprd_i2c_set_clk()
329 u32 i2c_dvd = apb_clk / (4 * freq) - 1; in sprd_i2c_set_clk()
342 writel(div0, i2c_dev->base + ADDR_DVD0); in sprd_i2c_set_clk()
343 writel(div1, i2c_dev->base + ADDR_DVD1); in sprd_i2c_set_clk()
347 writel((6 * apb_clk) / 10000000, i2c_dev->base + ADDR_STA0_DVD); in sprd_i2c_set_clk()
349 writel((4 * apb_clk) / 1000000, i2c_dev->base + ADDR_STA0_DVD); in sprd_i2c_set_clk()
356 writel(tmp, i2c_dev->base + I2C_CTL); in sprd_i2c_enable()
361 sprd_i2c_set_clk(i2c_dev, i2c_dev->bus_freq); in sprd_i2c_enable()
365 tmp = readl(i2c_dev->base + I2C_CTL); in sprd_i2c_enable()
366 writel(tmp | I2C_EN | I2C_INT_EN, i2c_dev->base + I2C_CTL); in sprd_i2c_enable()
372 struct i2c_msg *msg = i2c_dev->msg; in sprd_i2c_isr_thread()
373 bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK); in sprd_i2c_isr_thread()
376 if (msg->flags & I2C_M_RD) in sprd_i2c_isr_thread()
377 i2c_tran = i2c_dev->count >= I2C_FIFO_FULL_THLD; in sprd_i2c_isr_thread()
379 i2c_tran = i2c_dev->count; in sprd_i2c_isr_thread()
394 i2c_dev->err = 0; in sprd_i2c_isr_thread()
398 * return -EIO to notify users. in sprd_i2c_isr_thread()
401 i2c_dev->err = -EIO; in sprd_i2c_isr_thread()
402 else if (msg->flags & I2C_M_RD && i2c_dev->count) in sprd_i2c_isr_thread()
403 sprd_i2c_read_bytes(i2c_dev, i2c_dev->buf, i2c_dev->count); in sprd_i2c_isr_thread()
408 complete(&i2c_dev->complete); in sprd_i2c_isr_thread()
416 struct i2c_msg *msg = i2c_dev->msg; in sprd_i2c_isr()
417 bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK); in sprd_i2c_isr()
420 if (msg->flags & I2C_M_RD) in sprd_i2c_isr()
421 i2c_tran = i2c_dev->count >= I2C_FIFO_FULL_THLD; in sprd_i2c_isr()
423 i2c_tran = i2c_dev->count; in sprd_i2c_isr()
451 clk_i2c = devm_clk_get(i2c_dev->dev, "i2c"); in sprd_i2c_clk_init()
453 dev_warn(i2c_dev->dev, "i2c%d can't get the i2c clock\n", in sprd_i2c_clk_init()
454 i2c_dev->adap.nr); in sprd_i2c_clk_init()
458 clk_parent = devm_clk_get(i2c_dev->dev, "source"); in sprd_i2c_clk_init()
460 dev_warn(i2c_dev->dev, "i2c%d can't get the source clock\n", in sprd_i2c_clk_init()
461 i2c_dev->adap.nr); in sprd_i2c_clk_init()
466 i2c_dev->src_clk = clk_get_rate(clk_i2c); in sprd_i2c_clk_init()
468 i2c_dev->src_clk = 26000000; in sprd_i2c_clk_init()
470 dev_dbg(i2c_dev->dev, "i2c%d set source clock is %d\n", in sprd_i2c_clk_init()
471 i2c_dev->adap.nr, i2c_dev->src_clk); in sprd_i2c_clk_init()
473 i2c_dev->clk = devm_clk_get(i2c_dev->dev, "enable"); in sprd_i2c_clk_init()
474 if (IS_ERR(i2c_dev->clk)) { in sprd_i2c_clk_init()
475 dev_err(i2c_dev->dev, "i2c%d can't get the enable clock\n", in sprd_i2c_clk_init()
476 i2c_dev->adap.nr); in sprd_i2c_clk_init()
477 return PTR_ERR(i2c_dev->clk); in sprd_i2c_clk_init()
485 struct device *dev = &pdev->dev; in sprd_i2c_probe()
490 pdev->id = of_alias_get_id(dev->of_node, "i2c"); in sprd_i2c_probe()
494 return -ENOMEM; in sprd_i2c_probe()
496 i2c_dev->base = devm_platform_ioremap_resource(pdev, 0); in sprd_i2c_probe()
497 if (IS_ERR(i2c_dev->base)) in sprd_i2c_probe()
498 return PTR_ERR(i2c_dev->base); in sprd_i2c_probe()
500 i2c_dev->irq = platform_get_irq(pdev, 0); in sprd_i2c_probe()
501 if (i2c_dev->irq < 0) in sprd_i2c_probe()
502 return i2c_dev->irq; in sprd_i2c_probe()
504 i2c_set_adapdata(&i2c_dev->adap, i2c_dev); in sprd_i2c_probe()
505 init_completion(&i2c_dev->complete); in sprd_i2c_probe()
506 snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name), in sprd_i2c_probe()
507 "%s", "sprd-i2c"); in sprd_i2c_probe()
509 i2c_dev->bus_freq = I2C_MAX_STANDARD_MODE_FREQ; in sprd_i2c_probe()
510 i2c_dev->adap.owner = THIS_MODULE; in sprd_i2c_probe()
511 i2c_dev->dev = dev; in sprd_i2c_probe()
512 i2c_dev->adap.retries = 3; in sprd_i2c_probe()
513 i2c_dev->adap.algo = &sprd_i2c_algo; in sprd_i2c_probe()
514 i2c_dev->adap.algo_data = i2c_dev; in sprd_i2c_probe()
515 i2c_dev->adap.dev.parent = dev; in sprd_i2c_probe()
516 i2c_dev->adap.nr = pdev->id; in sprd_i2c_probe()
517 i2c_dev->adap.dev.of_node = dev->of_node; in sprd_i2c_probe()
519 if (!of_property_read_u32(dev->of_node, "clock-frequency", &prop)) in sprd_i2c_probe()
520 i2c_dev->bus_freq = prop; in sprd_i2c_probe()
523 if (i2c_dev->bus_freq != I2C_MAX_STANDARD_MODE_FREQ && in sprd_i2c_probe()
524 i2c_dev->bus_freq != I2C_MAX_FAST_MODE_FREQ) in sprd_i2c_probe()
525 return -EINVAL; in sprd_i2c_probe()
533 ret = clk_prepare_enable(i2c_dev->clk); in sprd_i2c_probe()
539 pm_runtime_set_autosuspend_delay(i2c_dev->dev, SPRD_I2C_PM_TIMEOUT); in sprd_i2c_probe()
540 pm_runtime_use_autosuspend(i2c_dev->dev); in sprd_i2c_probe()
541 pm_runtime_set_active(i2c_dev->dev); in sprd_i2c_probe()
542 pm_runtime_enable(i2c_dev->dev); in sprd_i2c_probe()
544 ret = pm_runtime_get_sync(i2c_dev->dev); in sprd_i2c_probe()
548 ret = devm_request_threaded_irq(dev, i2c_dev->irq, in sprd_i2c_probe()
551 pdev->name, i2c_dev); in sprd_i2c_probe()
553 dev_err(&pdev->dev, "failed to request irq %d\n", i2c_dev->irq); in sprd_i2c_probe()
557 ret = i2c_add_numbered_adapter(&i2c_dev->adap); in sprd_i2c_probe()
559 dev_err(&pdev->dev, "add adapter failed\n"); in sprd_i2c_probe()
563 pm_runtime_mark_last_busy(i2c_dev->dev); in sprd_i2c_probe()
564 pm_runtime_put_autosuspend(i2c_dev->dev); in sprd_i2c_probe()
568 pm_runtime_put_noidle(i2c_dev->dev); in sprd_i2c_probe()
569 pm_runtime_disable(i2c_dev->dev); in sprd_i2c_probe()
570 clk_disable_unprepare(i2c_dev->clk); in sprd_i2c_probe()
579 ret = pm_runtime_resume_and_get(i2c_dev->dev); in sprd_i2c_remove()
583 i2c_del_adapter(&i2c_dev->adap); in sprd_i2c_remove()
584 clk_disable_unprepare(i2c_dev->clk); in sprd_i2c_remove()
586 pm_runtime_put_noidle(i2c_dev->dev); in sprd_i2c_remove()
587 pm_runtime_disable(i2c_dev->dev); in sprd_i2c_remove()
596 i2c_mark_adapter_suspended(&i2c_dev->adap); in sprd_i2c_suspend_noirq()
604 i2c_mark_adapter_resumed(&i2c_dev->adap); in sprd_i2c_resume_noirq()
612 clk_disable_unprepare(i2c_dev->clk); in sprd_i2c_runtime_suspend()
622 ret = clk_prepare_enable(i2c_dev->clk); in sprd_i2c_runtime_resume()
640 { .compatible = "sprd,sc9860-i2c", },
649 .name = "sprd-i2c",