Lines Matching +full:stop +full:- +full:ack
1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2014-19 Wolfram Sang <wsa@sang-engineering.com>
8 * Portions of the code based on out-of-tree driver i2c-sh7343.c
15 #include <linux/dma-mapping.h>
32 /* BUS: S A8 ACK P(*) */
39 /* BUS: S A8 ACK D8(1) ACK P(*) */
41 /* ICIC: -DTE */
46 /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P(*) */
48 /* ICIC: -DTE */
52 /* 3 bytes or more, +---------+ gets repeated */
57 /* 0 byte receive - not supported since slave may hold SDA low */
60 /* BUS: S A8 ACK | D8(1) ACK P(*) */
62 /* ICIC: -DTE | +DTE */
67 /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P(*) */
69 /* ICIC: -DTE | +DTE */
74 /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
76 /* ICIC: -DTE | +DTE */
80 /* 4 bytes or more, this part is repeated +---------+ */
97 /* (*) The STOP condition is only sent by the master at the end of the last */
99 /* only cleared after the STOP condition, so, between messages we have to */
189 data |= pd->icic; in iic_wr()
191 iowrite8(data, pd->reg + offs); in iic_wr()
196 return ioread8(pd->reg + offs); in iic_rd()
211 * SH-Mobile IIC hardware starts counting the LOW period of in sh_mobile_i2c_iccl()
226 * SH-Mobile IIC hardware is aware of SCL transition period 'tr', in sh_mobile_i2c_icch()
227 * and can ignore it. SH-Mobile IIC controller starts counting in sh_mobile_i2c_icch()
241 u16 max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff; in sh_mobile_i2c_check_timing()
243 if (pd->iccl > max_val || pd->icch > max_val) { in sh_mobile_i2c_check_timing()
244 dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n", in sh_mobile_i2c_check_timing()
245 pd->iccl, pd->icch); in sh_mobile_i2c_check_timing()
246 return -EINVAL; in sh_mobile_i2c_check_timing()
250 if (pd->iccl & 0x100) in sh_mobile_i2c_check_timing()
251 pd->icic |= ICIC_ICCLB8; in sh_mobile_i2c_check_timing()
253 pd->icic &= ~ICIC_ICCLB8; in sh_mobile_i2c_check_timing()
256 if (pd->icch & 0x100) in sh_mobile_i2c_check_timing()
257 pd->icic |= ICIC_ICCHB8; in sh_mobile_i2c_check_timing()
259 pd->icic &= ~ICIC_ICCHB8; in sh_mobile_i2c_check_timing()
261 dev_dbg(pd->dev, "timing values: L/H=0x%x/0x%x\n", pd->iccl, pd->icch); in sh_mobile_i2c_check_timing()
270 i2c_clk_khz = clk_get_rate(pd->clk) / 1000 / pd->clks_per_count; in sh_mobile_i2c_init()
272 if (pd->bus_speed == I2C_MAX_STANDARD_MODE_FREQ) { in sh_mobile_i2c_init()
276 } else if (pd->bus_speed == I2C_MAX_FAST_MODE_FREQ) { in sh_mobile_i2c_init()
281 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n", in sh_mobile_i2c_init()
282 pd->bus_speed); in sh_mobile_i2c_init()
283 return -EINVAL; in sh_mobile_i2c_init()
286 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf); in sh_mobile_i2c_init()
287 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf); in sh_mobile_i2c_init()
297 clks_per_cycle = clk_get_rate(pd->clk) / pd->bus_speed; in sh_mobile_i2c_v2_init()
298 pd->iccl = DIV_ROUND_UP(clks_per_cycle * 5 / 9 - 1, pd->clks_per_count); in sh_mobile_i2c_v2_init()
299 pd->icch = DIV_ROUND_UP(clks_per_cycle * 4 / 9 - 5, pd->clks_per_count); in sh_mobile_i2c_v2_init()
309 dev_dbg(pd->dev, "op %d\n", op); in i2c_op()
311 spin_lock_irqsave(&pd->lock, flags); in i2c_op()
319 iic_wr(pd, ICDR, i2c_8bit_addr_from_msg(pd->msg)); in i2c_op()
322 iic_wr(pd, ICDR, pd->msg->buf[pd->pos]); in i2c_op()
324 case OP_TX_STOP: /* issue a stop (or rep_start) */ in i2c_op()
325 iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS in i2c_op()
334 case OP_RX_STOP: /* enable DTE interrupt, issue stop */ in i2c_op()
335 if (!pd->atomic_xfer) in i2c_op()
340 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ in i2c_op()
341 if (!pd->atomic_xfer) in i2c_op()
349 spin_unlock_irqrestore(&pd->lock, flags); in i2c_op()
351 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret); in i2c_op()
357 if (pd->pos == pd->msg->len) { in sh_mobile_i2c_isr_tx()
362 if (pd->pos == -1) in sh_mobile_i2c_isr_tx()
367 pd->pos++; in sh_mobile_i2c_isr_tx()
376 real_pos = pd->pos - 2; in sh_mobile_i2c_isr_rx()
378 if (pd->pos == -1) { in sh_mobile_i2c_isr_rx()
380 } else if (pd->pos == 0) { in sh_mobile_i2c_isr_rx()
382 } else if (pd->pos == pd->msg->len) { in sh_mobile_i2c_isr_rx()
383 if (pd->stop_after_dma) { in sh_mobile_i2c_isr_rx()
386 pd->pos++; in sh_mobile_i2c_isr_rx()
393 pd->msg->buf[real_pos] = i2c_op(pd, OP_RX_STOP_DATA); in sh_mobile_i2c_isr_rx()
395 pd->msg->buf[real_pos] = i2c_op(pd, OP_RX); in sh_mobile_i2c_isr_rx()
399 pd->pos++; in sh_mobile_i2c_isr_rx()
400 return pd->pos == (pd->msg->len + 2); in sh_mobile_i2c_isr_rx()
410 pd->sr |= sr; /* remember state */ in sh_mobile_i2c_isr()
412 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, in sh_mobile_i2c_isr()
413 str_read_write(pd->msg->flags & I2C_M_RD), in sh_mobile_i2c_isr()
414 pd->pos, pd->msg->len); in sh_mobile_i2c_isr()
417 if (pd->dma_direction == DMA_TO_DEVICE && pd->pos == 0) in sh_mobile_i2c_isr()
420 /* don't interrupt transaction - continue to issue stop */ in sh_mobile_i2c_isr()
422 else if (pd->msg->flags & I2C_M_RD) in sh_mobile_i2c_isr()
428 if (pd->dma_direction == DMA_FROM_DEVICE && pd->pos == 1) in sh_mobile_i2c_isr()
435 pd->sr |= SW_DONE; in sh_mobile_i2c_isr()
436 if (!pd->atomic_xfer) in sh_mobile_i2c_isr()
437 wake_up(&pd->wait); in sh_mobile_i2c_isr()
448 struct dma_chan *chan = pd->dma_direction == DMA_FROM_DEVICE in sh_mobile_i2c_cleanup_dma()
449 ? pd->dma_rx : pd->dma_tx; in sh_mobile_i2c_cleanup_dma()
455 dma_unmap_single(chan->device->dev, sg_dma_address(&pd->sg), in sh_mobile_i2c_cleanup_dma()
456 pd->msg->len, pd->dma_direction); in sh_mobile_i2c_cleanup_dma()
458 pd->dma_direction = DMA_NONE; in sh_mobile_i2c_cleanup_dma()
466 pd->pos = pd->msg->len; in sh_mobile_i2c_dma_callback()
467 pd->stop_after_dma = true; in sh_mobile_i2c_dma_callback()
510 bool read = pd->msg->flags & I2C_M_RD; in sh_mobile_i2c_xfer_dma()
512 struct dma_chan *chan = read ? pd->dma_rx : pd->dma_tx; in sh_mobile_i2c_xfer_dma()
517 if (PTR_ERR(chan) == -EPROBE_DEFER) { in sh_mobile_i2c_xfer_dma()
519 chan = pd->dma_rx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_DEV_TO_MEM, in sh_mobile_i2c_xfer_dma()
520 pd->res->start + ICDR); in sh_mobile_i2c_xfer_dma()
522 chan = pd->dma_tx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_MEM_TO_DEV, in sh_mobile_i2c_xfer_dma()
523 pd->res->start + ICDR); in sh_mobile_i2c_xfer_dma()
529 dma_addr = dma_map_single(chan->device->dev, pd->dma_buf, pd->msg->len, dir); in sh_mobile_i2c_xfer_dma()
530 if (dma_mapping_error(chan->device->dev, dma_addr)) { in sh_mobile_i2c_xfer_dma()
531 dev_dbg(pd->dev, "dma map failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
535 sg_dma_len(&pd->sg) = pd->msg->len; in sh_mobile_i2c_xfer_dma()
536 sg_dma_address(&pd->sg) = dma_addr; in sh_mobile_i2c_xfer_dma()
538 pd->dma_direction = dir; in sh_mobile_i2c_xfer_dma()
540 txdesc = dmaengine_prep_slave_sg(chan, &pd->sg, 1, in sh_mobile_i2c_xfer_dma()
544 dev_dbg(pd->dev, "dma prep slave sg failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
549 txdesc->callback = sh_mobile_i2c_dma_callback; in sh_mobile_i2c_xfer_dma()
550 txdesc->callback_param = pd; in sh_mobile_i2c_xfer_dma()
554 dev_dbg(pd->dev, "submitting dma failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
569 /* Enable channel and configure rx ack */ in start_ch()
573 iic_wr(pd, ICCL, pd->iccl & 0xff); in start_ch()
574 iic_wr(pd, ICCH, pd->icch & 0xff); in start_ch()
577 pd->msg = usr_msg; in start_ch()
578 pd->pos = -1; in start_ch()
579 pd->sr = 0; in start_ch()
581 if (pd->atomic_xfer) in start_ch()
584 pd->dma_buf = i2c_get_dma_safe_msg_buf(pd->msg, 8); in start_ch()
585 if (pd->dma_buf) in start_ch()
596 for (i = 1000; i; i--) { in poll_dte()
603 return -ENXIO; in poll_dte()
608 return i ? 0 : -ETIMEDOUT; in poll_dte()
615 for (i = 1000; i; i--) { in poll_busy()
618 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); in poll_busy()
626 val |= pd->sr; in poll_busy()
628 return -ENXIO; in poll_busy()
630 return -EAGAIN; in poll_busy()
637 return i ? 0 : -ETIMEDOUT; in poll_busy()
649 pm_runtime_get_sync(pd->dev); in sh_mobile_xfer()
653 bool do_start = pd->send_stop || !i; in sh_mobile_xfer()
655 pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP; in sh_mobile_xfer()
656 pd->stop_after_dma = false; in sh_mobile_xfer()
663 if (pd->atomic_xfer) { in sh_mobile_xfer()
664 unsigned long j = jiffies + pd->adap.timeout; in sh_mobile_xfer()
668 !(pd->sr & (ICSR_TACK | SW_DONE))) { in sh_mobile_xfer()
682 time_left = wait_event_timeout(pd->wait, in sh_mobile_xfer()
683 pd->sr & (ICSR_TACK | SW_DONE), in sh_mobile_xfer()
684 pd->adap.timeout); in sh_mobile_xfer()
687 i2c_put_dma_safe_msg_buf(pd->dma_buf, pd->msg, in sh_mobile_xfer()
688 pd->stop_after_dma); in sh_mobile_xfer()
692 if (pd->dma_direction != DMA_NONE) in sh_mobile_xfer()
695 err = -ETIMEDOUT; in sh_mobile_xfer()
699 if (pd->send_stop) in sh_mobile_xfer()
711 pm_runtime_put_sync(pd->dev); in sh_mobile_xfer()
722 pd->atomic_xfer = false; in sh_mobile_i2c_xfer()
732 pd->atomic_xfer = true; in sh_mobile_i2c_xfer_atomic()
795 { .compatible = "renesas,iic-r8a73a4", .data = &fast_clock_dt_config },
796 { .compatible = "renesas,iic-r8a7740", .data = &r8a7740_dt_config },
797 { .compatible = "renesas,iic-r8a774c0", .data = &fast_clock_dt_config },
798 { .compatible = "renesas,iic-r8a7790", .data = &fast_clock_dt_config },
799 { .compatible = "renesas,iic-r8a7791", .data = &fast_clock_dt_config },
800 { .compatible = "renesas,iic-r8a7792", .data = &fast_clock_dt_config },
801 { .compatible = "renesas,iic-r8a7793", .data = &fast_clock_dt_config },
802 { .compatible = "renesas,iic-r8a7794", .data = &fast_clock_dt_config },
803 { .compatible = "renesas,iic-r8a7795", .data = &fast_clock_dt_config },
804 { .compatible = "renesas,iic-r8a77990", .data = &fast_clock_dt_config },
805 { .compatible = "renesas,iic-sh73a0", .data = &fast_clock_dt_config },
806 { .compatible = "renesas,rcar-gen2-iic", .data = &fast_clock_dt_config },
807 { .compatible = "renesas,rcar-gen3-iic", .data = &fast_clock_dt_config },
808 { .compatible = "renesas,rmobile-iic", .data = &default_dt_config },
815 if (!IS_ERR(pd->dma_tx)) { in sh_mobile_i2c_release_dma()
816 dma_release_channel(pd->dma_tx); in sh_mobile_i2c_release_dma()
817 pd->dma_tx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_release_dma()
820 if (!IS_ERR(pd->dma_rx)) { in sh_mobile_i2c_release_dma()
821 dma_release_channel(pd->dma_rx); in sh_mobile_i2c_release_dma()
822 pd->dma_rx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_release_dma()
828 struct device_node *np = dev_of_node(&dev->dev); in sh_mobile_i2c_hook_irqs()
834 while ((irq = platform_get_irq_optional(dev, k)) != -ENXIO) { in sh_mobile_i2c_hook_irqs()
837 ret = devm_request_irq(&dev->dev, irq, sh_mobile_i2c_isr, in sh_mobile_i2c_hook_irqs()
838 0, dev_name(&dev->dev), pd); in sh_mobile_i2c_hook_irqs()
840 dev_err(&dev->dev, "cannot request IRQ %d\n", irq); in sh_mobile_i2c_hook_irqs()
850 for (n = res->start; n <= res->end; n++) { in sh_mobile_i2c_hook_irqs()
851 ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr, in sh_mobile_i2c_hook_irqs()
852 0, dev_name(&dev->dev), pd); in sh_mobile_i2c_hook_irqs()
854 dev_err(&dev->dev, "cannot request IRQ %pa\n", &n); in sh_mobile_i2c_hook_irqs()
862 return k > 0 ? 0 : -ENOENT; in sh_mobile_i2c_hook_irqs()
873 pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL); in sh_mobile_i2c_probe()
875 return -ENOMEM; in sh_mobile_i2c_probe()
877 pd->clk = devm_clk_get(&dev->dev, NULL); in sh_mobile_i2c_probe()
878 if (IS_ERR(pd->clk)) { in sh_mobile_i2c_probe()
879 dev_err(&dev->dev, "cannot get clock\n"); in sh_mobile_i2c_probe()
880 return PTR_ERR(pd->clk); in sh_mobile_i2c_probe()
887 pd->dev = &dev->dev; in sh_mobile_i2c_probe()
890 pd->reg = devm_platform_get_and_ioremap_resource(dev, 0, &pd->res); in sh_mobile_i2c_probe()
891 if (IS_ERR(pd->reg)) in sh_mobile_i2c_probe()
892 return PTR_ERR(pd->reg); in sh_mobile_i2c_probe()
894 ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed); in sh_mobile_i2c_probe()
895 pd->bus_speed = (ret || !bus_speed) ? I2C_MAX_STANDARD_MODE_FREQ : bus_speed; in sh_mobile_i2c_probe()
896 pd->clks_per_count = 1; in sh_mobile_i2c_probe()
899 if (resource_size(pd->res) > 0x17) in sh_mobile_i2c_probe()
900 pd->flags |= IIC_FLAG_HAS_ICIC67; in sh_mobile_i2c_probe()
902 pm_runtime_enable(&dev->dev); in sh_mobile_i2c_probe()
903 pm_runtime_get_sync(&dev->dev); in sh_mobile_i2c_probe()
905 config = of_device_get_match_data(&dev->dev); in sh_mobile_i2c_probe()
907 pd->clks_per_count = config->clks_per_count; in sh_mobile_i2c_probe()
908 ret = config->setup(pd); in sh_mobile_i2c_probe()
913 pm_runtime_put_sync(&dev->dev); in sh_mobile_i2c_probe()
918 sg_init_table(&pd->sg, 1); in sh_mobile_i2c_probe()
919 pd->dma_direction = DMA_NONE; in sh_mobile_i2c_probe()
920 pd->dma_rx = pd->dma_tx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_probe()
923 adap = &pd->adap; in sh_mobile_i2c_probe()
926 adap->owner = THIS_MODULE; in sh_mobile_i2c_probe()
927 adap->algo = &sh_mobile_i2c_algorithm; in sh_mobile_i2c_probe()
928 adap->quirks = &sh_mobile_i2c_quirks; in sh_mobile_i2c_probe()
929 adap->dev.parent = &dev->dev; in sh_mobile_i2c_probe()
930 adap->retries = 5; in sh_mobile_i2c_probe()
931 adap->nr = dev->id; in sh_mobile_i2c_probe()
932 adap->dev.of_node = dev->dev.of_node; in sh_mobile_i2c_probe()
934 strscpy(adap->name, dev->name, sizeof(adap->name)); in sh_mobile_i2c_probe()
936 spin_lock_init(&pd->lock); in sh_mobile_i2c_probe()
937 init_waitqueue_head(&pd->wait); in sh_mobile_i2c_probe()
945 dev_info(&dev->dev, "I2C adapter %d, bus speed %lu Hz\n", adap->nr, pd->bus_speed); in sh_mobile_i2c_probe()
954 i2c_del_adapter(&pd->adap); in sh_mobile_i2c_remove()
956 pm_runtime_disable(&dev->dev); in sh_mobile_i2c_remove()
963 i2c_mark_adapter_suspended(&pd->adap); in sh_mobile_i2c_suspend()
971 i2c_mark_adapter_resumed(&pd->adap); in sh_mobile_i2c_resume()
982 .name = "i2c-sh_mobile",
1006 MODULE_ALIAS("platform:i2c-sh_mobile");