Lines Matching +full:dte +full:- +full:mode
1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2014 Wolfram Sang <wsa@sang-engineering.com>
9 * Portions of the code based on out-of-tree driver i2c-sh7343.c
16 #include <linux/dma-mapping.h>
33 /* IRQ: DTE WAIT */
40 /* IRQ: DTE WAIT WAIT */
41 /* ICIC: -DTE */
47 /* IRQ: DTE WAIT WAIT WAIT */
48 /* ICIC: -DTE */
52 /* 3 bytes or more, +---------+ gets repeated */
57 /* 0 byte receive - not supported since slave may hold SDA low */
61 /* IRQ: DTE WAIT | WAIT DTE */
62 /* ICIC: -DTE | +DTE */
68 /* IRQ: DTE WAIT | WAIT WAIT DTE */
69 /* ICIC: -DTE | +DTE */
75 /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
76 /* ICIC: -DTE | +DTE */
80 /* 4 bytes or more, this part is repeated +---------+ */
92 /* DTE IRQ __________________________________________/ \_ */
100 /* poll for the DTE bit. */
191 data |= pd->icic; in iic_wr()
193 iowrite8(data, pd->reg + offs); in iic_wr()
198 return ioread8(pd->reg + offs); in iic_rd()
213 * SH-Mobile IIC hardware starts counting the LOW period of in sh_mobile_i2c_iccl()
228 * SH-Mobile IIC hardware is aware of SCL transition period 'tr', in sh_mobile_i2c_icch()
229 * and can ignore it. SH-Mobile IIC controller starts counting in sh_mobile_i2c_icch()
243 u16 max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff; in sh_mobile_i2c_check_timing()
245 if (pd->iccl > max_val || pd->icch > max_val) { in sh_mobile_i2c_check_timing()
246 dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n", in sh_mobile_i2c_check_timing()
247 pd->iccl, pd->icch); in sh_mobile_i2c_check_timing()
248 return -EINVAL; in sh_mobile_i2c_check_timing()
252 if (pd->iccl & 0x100) in sh_mobile_i2c_check_timing()
253 pd->icic |= ICIC_ICCLB8; in sh_mobile_i2c_check_timing()
255 pd->icic &= ~ICIC_ICCLB8; in sh_mobile_i2c_check_timing()
258 if (pd->icch & 0x100) in sh_mobile_i2c_check_timing()
259 pd->icic |= ICIC_ICCHB8; in sh_mobile_i2c_check_timing()
261 pd->icic &= ~ICIC_ICCHB8; in sh_mobile_i2c_check_timing()
263 dev_dbg(pd->dev, "timing values: L/H=0x%x/0x%x\n", pd->iccl, pd->icch); in sh_mobile_i2c_check_timing()
272 i2c_clk_khz = clk_get_rate(pd->clk) / 1000 / pd->clks_per_count; in sh_mobile_i2c_init()
274 if (pd->bus_speed == STANDARD_MODE) { in sh_mobile_i2c_init()
278 } else if (pd->bus_speed == FAST_MODE) { in sh_mobile_i2c_init()
283 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n", in sh_mobile_i2c_init()
284 pd->bus_speed); in sh_mobile_i2c_init()
285 return -EINVAL; in sh_mobile_i2c_init()
288 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf); in sh_mobile_i2c_init()
289 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf); in sh_mobile_i2c_init()
299 clks_per_cycle = clk_get_rate(pd->clk) / pd->bus_speed; in sh_mobile_i2c_v2_init()
300 pd->iccl = DIV_ROUND_UP(clks_per_cycle * 5 / 9 - 1, pd->clks_per_count); in sh_mobile_i2c_v2_init()
301 pd->icch = DIV_ROUND_UP(clks_per_cycle * 4 / 9 - 5, pd->clks_per_count); in sh_mobile_i2c_v2_init()
312 dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data); in i2c_op()
314 spin_lock_irqsave(&pd->lock, flags); in i2c_op()
317 case OP_START: /* issue start and trigger DTE interrupt */ in i2c_op()
320 case OP_TX_FIRST: /* disable DTE interrupt and write data */ in i2c_op()
328 iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS in i2c_op()
331 case OP_TX_TO_RX: /* select read mode */ in i2c_op()
337 case OP_RX_STOP: /* enable DTE interrupt, issue stop */ in i2c_op()
342 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ in i2c_op()
350 spin_unlock_irqrestore(&pd->lock, flags); in i2c_op()
352 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret); in i2c_op()
358 return pd->pos == -1; in sh_mobile_i2c_is_first_byte()
364 switch (pd->pos) { in sh_mobile_i2c_get_data()
365 case -1: in sh_mobile_i2c_get_data()
366 *buf = i2c_8bit_addr_from_msg(pd->msg); in sh_mobile_i2c_get_data()
369 *buf = pd->msg->buf[pd->pos]; in sh_mobile_i2c_get_data()
377 if (pd->pos == pd->msg->len) { in sh_mobile_i2c_isr_tx()
385 pd->pos++; in sh_mobile_i2c_isr_tx()
395 if (pd->pos <= -1) { in sh_mobile_i2c_isr_rx()
405 if (pd->pos == 0) { in sh_mobile_i2c_isr_rx()
410 real_pos = pd->pos - 2; in sh_mobile_i2c_isr_rx()
412 if (pd->pos == pd->msg->len) { in sh_mobile_i2c_isr_rx()
413 if (pd->stop_after_dma) { in sh_mobile_i2c_isr_rx()
416 pd->pos++; in sh_mobile_i2c_isr_rx()
430 pd->msg->buf[real_pos] = data; in sh_mobile_i2c_isr_rx()
433 pd->pos++; in sh_mobile_i2c_isr_rx()
434 return pd->pos == (pd->msg->len + 2); in sh_mobile_i2c_isr_rx()
444 pd->sr |= sr; /* remember state */ in sh_mobile_i2c_isr()
446 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, in sh_mobile_i2c_isr()
447 (pd->msg->flags & I2C_M_RD) ? "read" : "write", in sh_mobile_i2c_isr()
448 pd->pos, pd->msg->len); in sh_mobile_i2c_isr()
451 if (pd->dma_direction == DMA_TO_DEVICE && pd->pos == 0) in sh_mobile_i2c_isr()
454 /* don't interrupt transaction - continue to issue stop */ in sh_mobile_i2c_isr()
456 else if (pd->msg->flags & I2C_M_RD) in sh_mobile_i2c_isr()
462 if (pd->dma_direction == DMA_FROM_DEVICE && pd->pos == 1) in sh_mobile_i2c_isr()
469 pd->sr |= SW_DONE; in sh_mobile_i2c_isr()
470 wake_up(&pd->wait); in sh_mobile_i2c_isr()
481 struct dma_chan *chan = pd->dma_direction == DMA_FROM_DEVICE in sh_mobile_i2c_dma_unmap()
482 ? pd->dma_rx : pd->dma_tx; in sh_mobile_i2c_dma_unmap()
484 dma_unmap_single(chan->device->dev, sg_dma_address(&pd->sg), in sh_mobile_i2c_dma_unmap()
485 pd->msg->len, pd->dma_direction); in sh_mobile_i2c_dma_unmap()
487 pd->dma_direction = DMA_NONE; in sh_mobile_i2c_dma_unmap()
492 if (pd->dma_direction == DMA_NONE) in sh_mobile_i2c_cleanup_dma()
494 else if (pd->dma_direction == DMA_FROM_DEVICE) in sh_mobile_i2c_cleanup_dma()
495 dmaengine_terminate_all(pd->dma_rx); in sh_mobile_i2c_cleanup_dma()
496 else if (pd->dma_direction == DMA_TO_DEVICE) in sh_mobile_i2c_cleanup_dma()
497 dmaengine_terminate_all(pd->dma_tx); in sh_mobile_i2c_cleanup_dma()
507 pd->pos = pd->msg->len; in sh_mobile_i2c_dma_callback()
508 pd->stop_after_dma = true; in sh_mobile_i2c_dma_callback()
551 bool read = pd->msg->flags & I2C_M_RD; in sh_mobile_i2c_xfer_dma()
553 struct dma_chan *chan = read ? pd->dma_rx : pd->dma_tx; in sh_mobile_i2c_xfer_dma()
558 if (PTR_ERR(chan) == -EPROBE_DEFER) { in sh_mobile_i2c_xfer_dma()
560 chan = pd->dma_rx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_DEV_TO_MEM, in sh_mobile_i2c_xfer_dma()
561 pd->res->start + ICDR); in sh_mobile_i2c_xfer_dma()
563 chan = pd->dma_tx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_MEM_TO_DEV, in sh_mobile_i2c_xfer_dma()
564 pd->res->start + ICDR); in sh_mobile_i2c_xfer_dma()
570 dma_addr = dma_map_single(chan->device->dev, pd->dma_buf, pd->msg->len, dir); in sh_mobile_i2c_xfer_dma()
571 if (dma_mapping_error(chan->device->dev, dma_addr)) { in sh_mobile_i2c_xfer_dma()
572 dev_dbg(pd->dev, "dma map failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
576 sg_dma_len(&pd->sg) = pd->msg->len; in sh_mobile_i2c_xfer_dma()
577 sg_dma_address(&pd->sg) = dma_addr; in sh_mobile_i2c_xfer_dma()
579 pd->dma_direction = dir; in sh_mobile_i2c_xfer_dma()
581 txdesc = dmaengine_prep_slave_sg(chan, &pd->sg, 1, in sh_mobile_i2c_xfer_dma()
585 dev_dbg(pd->dev, "dma prep slave sg failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
590 txdesc->callback = sh_mobile_i2c_dma_callback; in sh_mobile_i2c_xfer_dma()
591 txdesc->callback_param = pd; in sh_mobile_i2c_xfer_dma()
595 dev_dbg(pd->dev, "submitting dma failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
614 iic_wr(pd, ICCL, pd->iccl & 0xff); in start_ch()
615 iic_wr(pd, ICCH, pd->icch & 0xff); in start_ch()
618 pd->msg = usr_msg; in start_ch()
619 pd->pos = -1; in start_ch()
620 pd->sr = 0; in start_ch()
622 pd->dma_buf = i2c_get_dma_safe_msg_buf(pd->msg, 8); in start_ch()
623 if (pd->dma_buf) in start_ch()
634 for (i = 1000; i; i--) { in poll_dte()
641 return -ENXIO; in poll_dte()
646 return i ? 0 : -ETIMEDOUT; in poll_dte()
653 for (i = 1000; i; i--) { in poll_busy()
656 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); in poll_busy()
664 val |= pd->sr; in poll_busy()
666 return -ENXIO; in poll_busy()
668 return -EAGAIN; in poll_busy()
675 return i ? 0 : -ETIMEDOUT; in poll_busy()
689 pm_runtime_get_sync(pd->dev); in sh_mobile_i2c_xfer()
693 bool do_start = pd->send_stop || !i; in sh_mobile_i2c_xfer()
695 pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP; in sh_mobile_i2c_xfer()
696 pd->stop_after_dma = false; in sh_mobile_i2c_xfer()
704 timeout = wait_event_timeout(pd->wait, in sh_mobile_i2c_xfer()
705 pd->sr & (ICSR_TACK | SW_DONE), in sh_mobile_i2c_xfer()
706 adapter->timeout); in sh_mobile_i2c_xfer()
709 i2c_put_dma_safe_msg_buf(pd->dma_buf, pd->msg, pd->stop_after_dma); in sh_mobile_i2c_xfer()
712 dev_err(pd->dev, "Transfer request timed out\n"); in sh_mobile_i2c_xfer()
713 if (pd->dma_direction != DMA_NONE) in sh_mobile_i2c_xfer()
716 err = -ETIMEDOUT; in sh_mobile_i2c_xfer()
720 if (pd->send_stop) in sh_mobile_i2c_xfer()
732 pm_runtime_put_sync(pd->dev); in sh_mobile_i2c_xfer()
753 * this is work-around for it.
801 { .compatible = "renesas,iic-r8a73a4", .data = &fast_clock_dt_config },
802 { .compatible = "renesas,iic-r8a7740", .data = &r8a7740_dt_config },
803 { .compatible = "renesas,iic-r8a774c0", .data = &fast_clock_dt_config },
804 { .compatible = "renesas,iic-r8a7790", .data = &v2_freq_calc_dt_config },
805 { .compatible = "renesas,iic-r8a7791", .data = &fast_clock_dt_config },
806 { .compatible = "renesas,iic-r8a7792", .data = &fast_clock_dt_config },
807 { .compatible = "renesas,iic-r8a7793", .data = &fast_clock_dt_config },
808 { .compatible = "renesas,iic-r8a7794", .data = &fast_clock_dt_config },
809 { .compatible = "renesas,rcar-gen2-iic", .data = &fast_clock_dt_config },
810 { .compatible = "renesas,iic-r8a7795", .data = &fast_clock_dt_config },
811 { .compatible = "renesas,rcar-gen3-iic", .data = &fast_clock_dt_config },
812 { .compatible = "renesas,iic-r8a77990", .data = &fast_clock_dt_config },
813 { .compatible = "renesas,iic-sh73a0", .data = &fast_clock_dt_config },
814 { .compatible = "renesas,rmobile-iic", .data = &default_dt_config },
821 if (!IS_ERR(pd->dma_tx)) { in sh_mobile_i2c_release_dma()
822 dma_release_channel(pd->dma_tx); in sh_mobile_i2c_release_dma()
823 pd->dma_tx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_release_dma()
826 if (!IS_ERR(pd->dma_rx)) { in sh_mobile_i2c_release_dma()
827 dma_release_channel(pd->dma_rx); in sh_mobile_i2c_release_dma()
828 pd->dma_rx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_release_dma()
839 for (n = res->start; n <= res->end; n++) { in sh_mobile_i2c_hook_irqs()
840 ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr, in sh_mobile_i2c_hook_irqs()
841 0, dev_name(&dev->dev), pd); in sh_mobile_i2c_hook_irqs()
843 dev_err(&dev->dev, "cannot request IRQ %pa\n", &n); in sh_mobile_i2c_hook_irqs()
850 return k > 0 ? 0 : -ENOENT; in sh_mobile_i2c_hook_irqs()
862 pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL); in sh_mobile_i2c_probe()
864 return -ENOMEM; in sh_mobile_i2c_probe()
866 pd->clk = devm_clk_get(&dev->dev, NULL); in sh_mobile_i2c_probe()
867 if (IS_ERR(pd->clk)) { in sh_mobile_i2c_probe()
868 dev_err(&dev->dev, "cannot get clock\n"); in sh_mobile_i2c_probe()
869 return PTR_ERR(pd->clk); in sh_mobile_i2c_probe()
876 pd->dev = &dev->dev; in sh_mobile_i2c_probe()
881 pd->res = res; in sh_mobile_i2c_probe()
882 pd->reg = devm_ioremap_resource(&dev->dev, res); in sh_mobile_i2c_probe()
883 if (IS_ERR(pd->reg)) in sh_mobile_i2c_probe()
884 return PTR_ERR(pd->reg); in sh_mobile_i2c_probe()
886 ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed); in sh_mobile_i2c_probe()
887 pd->bus_speed = (ret || !bus_speed) ? STANDARD_MODE : bus_speed; in sh_mobile_i2c_probe()
888 pd->clks_per_count = 1; in sh_mobile_i2c_probe()
892 pd->flags |= IIC_FLAG_HAS_ICIC67; in sh_mobile_i2c_probe()
894 pm_runtime_enable(&dev->dev); in sh_mobile_i2c_probe()
895 pm_runtime_get_sync(&dev->dev); in sh_mobile_i2c_probe()
897 config = of_device_get_match_data(&dev->dev); in sh_mobile_i2c_probe()
899 pd->clks_per_count = config->clks_per_count; in sh_mobile_i2c_probe()
900 ret = config->setup(pd); in sh_mobile_i2c_probe()
905 pm_runtime_put_sync(&dev->dev); in sh_mobile_i2c_probe()
910 sg_init_table(&pd->sg, 1); in sh_mobile_i2c_probe()
911 pd->dma_direction = DMA_NONE; in sh_mobile_i2c_probe()
912 pd->dma_rx = pd->dma_tx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_probe()
915 adap = &pd->adap; in sh_mobile_i2c_probe()
918 adap->owner = THIS_MODULE; in sh_mobile_i2c_probe()
919 adap->algo = &sh_mobile_i2c_algorithm; in sh_mobile_i2c_probe()
920 adap->quirks = &sh_mobile_i2c_quirks; in sh_mobile_i2c_probe()
921 adap->dev.parent = &dev->dev; in sh_mobile_i2c_probe()
922 adap->retries = 5; in sh_mobile_i2c_probe()
923 adap->nr = dev->id; in sh_mobile_i2c_probe()
924 adap->dev.of_node = dev->dev.of_node; in sh_mobile_i2c_probe()
926 strlcpy(adap->name, dev->name, sizeof(adap->name)); in sh_mobile_i2c_probe()
928 spin_lock_init(&pd->lock); in sh_mobile_i2c_probe()
929 init_waitqueue_head(&pd->wait); in sh_mobile_i2c_probe()
937 dev_info(&dev->dev, "I2C adapter %d, bus speed %lu Hz\n", adap->nr, pd->bus_speed); in sh_mobile_i2c_probe()
946 i2c_del_adapter(&pd->adap); in sh_mobile_i2c_remove()
948 pm_runtime_disable(&dev->dev); in sh_mobile_i2c_remove()
954 /* Runtime PM callback shared between ->runtime_suspend() in sh_mobile_i2c_runtime_nop()
955 * and ->runtime_resume(). Simply returns success. in sh_mobile_i2c_runtime_nop()
957 * This driver re-initializes all registers after in sh_mobile_i2c_runtime_nop()
971 .name = "i2c-sh_mobile",
994 MODULE_ALIAS("platform:i2c-sh_mobile");