Lines Matching full:pd

144 	int (*setup)(struct sh_mobile_i2c_data *pd);
185 static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data) in iic_wr() argument
188 data |= pd->icic; in iic_wr()
190 iowrite8(data, pd->reg + offs); in iic_wr()
193 static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs) in iic_rd() argument
195 return ioread8(pd->reg + offs); in iic_rd()
198 static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs, in iic_set_clr() argument
201 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr); in iic_set_clr()
238 static int sh_mobile_i2c_check_timing(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_check_timing() argument
240 u16 max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff; in sh_mobile_i2c_check_timing()
242 if (pd->iccl > max_val || pd->icch > max_val) { in sh_mobile_i2c_check_timing()
243 dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n", in sh_mobile_i2c_check_timing()
244 pd->iccl, pd->icch); in sh_mobile_i2c_check_timing()
249 if (pd->iccl & 0x100) in sh_mobile_i2c_check_timing()
250 pd->icic |= ICIC_ICCLB8; in sh_mobile_i2c_check_timing()
252 pd->icic &= ~ICIC_ICCLB8; in sh_mobile_i2c_check_timing()
255 if (pd->icch & 0x100) in sh_mobile_i2c_check_timing()
256 pd->icic |= ICIC_ICCHB8; in sh_mobile_i2c_check_timing()
258 pd->icic &= ~ICIC_ICCHB8; in sh_mobile_i2c_check_timing()
260 dev_dbg(pd->dev, "timing values: L/H=0x%x/0x%x\n", pd->iccl, pd->icch); in sh_mobile_i2c_check_timing()
264 static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_init() argument
269 i2c_clk_khz = clk_get_rate(pd->clk) / 1000 / pd->clks_per_count; in sh_mobile_i2c_init()
271 if (pd->bus_speed == I2C_MAX_STANDARD_MODE_FREQ) { in sh_mobile_i2c_init()
275 } else if (pd->bus_speed == I2C_MAX_FAST_MODE_FREQ) { in sh_mobile_i2c_init()
280 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n", in sh_mobile_i2c_init()
281 pd->bus_speed); in sh_mobile_i2c_init()
285 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf); in sh_mobile_i2c_init()
286 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf); in sh_mobile_i2c_init()
288 return sh_mobile_i2c_check_timing(pd); in sh_mobile_i2c_init()
291 static int sh_mobile_i2c_v2_init(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_v2_init() argument
296 clks_per_cycle = clk_get_rate(pd->clk) / pd->bus_speed; in sh_mobile_i2c_v2_init()
297 pd->iccl = DIV_ROUND_UP(clks_per_cycle * 5 / 9 - 1, pd->clks_per_count); in sh_mobile_i2c_v2_init()
298 pd->icch = DIV_ROUND_UP(clks_per_cycle * 4 / 9 - 5, pd->clks_per_count); in sh_mobile_i2c_v2_init()
300 return sh_mobile_i2c_check_timing(pd); in sh_mobile_i2c_v2_init()
303 static unsigned char i2c_op(struct sh_mobile_i2c_data *pd, enum sh_mobile_i2c_op op) in i2c_op() argument
308 dev_dbg(pd->dev, "op %d\n", op); in i2c_op()
310 spin_lock_irqsave(&pd->lock, flags); in i2c_op()
314 iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY); in i2c_op()
317 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE); in i2c_op()
318 iic_wr(pd, ICDR, i2c_8bit_addr_from_msg(pd->msg)); in i2c_op()
321 iic_wr(pd, ICDR, pd->msg->buf[pd->pos]); in i2c_op()
324 iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS in i2c_op()
328 iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP); in i2c_op()
331 ret = iic_rd(pd, ICDR); in i2c_op()
334 if (!pd->atomic_xfer) in i2c_op()
335 iic_wr(pd, ICIC, in i2c_op()
337 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK); in i2c_op()
340 if (!pd->atomic_xfer) in i2c_op()
341 iic_wr(pd, ICIC, in i2c_op()
343 ret = iic_rd(pd, ICDR); in i2c_op()
344 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK); in i2c_op()
348 spin_unlock_irqrestore(&pd->lock, flags); in i2c_op()
350 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret); in i2c_op()
354 static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_isr_tx() argument
356 if (pd->pos == pd->msg->len) { in sh_mobile_i2c_isr_tx()
357 i2c_op(pd, OP_TX_STOP); in sh_mobile_i2c_isr_tx()
361 if (pd->pos == -1) in sh_mobile_i2c_isr_tx()
362 i2c_op(pd, OP_TX_FIRST); in sh_mobile_i2c_isr_tx()
364 i2c_op(pd, OP_TX); in sh_mobile_i2c_isr_tx()
366 pd->pos++; in sh_mobile_i2c_isr_tx()
370 static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_isr_rx() argument
375 real_pos = pd->pos - 2; in sh_mobile_i2c_isr_rx()
377 if (pd->pos == -1) { in sh_mobile_i2c_isr_rx()
378 i2c_op(pd, OP_TX_FIRST); in sh_mobile_i2c_isr_rx()
379 } else if (pd->pos == 0) { in sh_mobile_i2c_isr_rx()
380 i2c_op(pd, OP_TX_TO_RX); in sh_mobile_i2c_isr_rx()
381 } else if (pd->pos == pd->msg->len) { in sh_mobile_i2c_isr_rx()
382 if (pd->stop_after_dma) { in sh_mobile_i2c_isr_rx()
384 i2c_op(pd, OP_RX_STOP); in sh_mobile_i2c_isr_rx()
385 pd->pos++; in sh_mobile_i2c_isr_rx()
390 i2c_op(pd, OP_RX_STOP); in sh_mobile_i2c_isr_rx()
392 pd->msg->buf[real_pos] = i2c_op(pd, OP_RX_STOP_DATA); in sh_mobile_i2c_isr_rx()
394 pd->msg->buf[real_pos] = i2c_op(pd, OP_RX); in sh_mobile_i2c_isr_rx()
398 pd->pos++; in sh_mobile_i2c_isr_rx()
399 return pd->pos == (pd->msg->len + 2); in sh_mobile_i2c_isr_rx()
404 struct sh_mobile_i2c_data *pd = dev_id; in sh_mobile_i2c_isr() local
408 sr = iic_rd(pd, ICSR); in sh_mobile_i2c_isr()
409 pd->sr |= sr; /* remember state */ in sh_mobile_i2c_isr()
411 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, in sh_mobile_i2c_isr()
412 (pd->msg->flags & I2C_M_RD) ? "read" : "write", in sh_mobile_i2c_isr()
413 pd->pos, pd->msg->len); in sh_mobile_i2c_isr()
416 if (pd->dma_direction == DMA_TO_DEVICE && pd->pos == 0) in sh_mobile_i2c_isr()
417 iic_set_clr(pd, ICIC, ICIC_TDMAE, 0); in sh_mobile_i2c_isr()
420 iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK)); in sh_mobile_i2c_isr()
421 else if (pd->msg->flags & I2C_M_RD) in sh_mobile_i2c_isr()
422 wakeup = sh_mobile_i2c_isr_rx(pd); in sh_mobile_i2c_isr()
424 wakeup = sh_mobile_i2c_isr_tx(pd); in sh_mobile_i2c_isr()
427 if (pd->dma_direction == DMA_FROM_DEVICE && pd->pos == 1) in sh_mobile_i2c_isr()
428 iic_set_clr(pd, ICIC, ICIC_RDMAE, 0); in sh_mobile_i2c_isr()
431 iic_wr(pd, ICSR, sr & ~ICSR_WAIT); in sh_mobile_i2c_isr()
434 pd->sr |= SW_DONE; in sh_mobile_i2c_isr()
435 if (!pd->atomic_xfer) in sh_mobile_i2c_isr()
436 wake_up(&pd->wait); in sh_mobile_i2c_isr()
440 iic_rd(pd, ICSR); in sh_mobile_i2c_isr()
445 static void sh_mobile_i2c_cleanup_dma(struct sh_mobile_i2c_data *pd, bool terminate) in sh_mobile_i2c_cleanup_dma() argument
447 struct dma_chan *chan = pd->dma_direction == DMA_FROM_DEVICE in sh_mobile_i2c_cleanup_dma()
448 ? pd->dma_rx : pd->dma_tx; in sh_mobile_i2c_cleanup_dma()
454 dma_unmap_single(chan->device->dev, sg_dma_address(&pd->sg), in sh_mobile_i2c_cleanup_dma()
455 pd->msg->len, pd->dma_direction); in sh_mobile_i2c_cleanup_dma()
457 pd->dma_direction = DMA_NONE; in sh_mobile_i2c_cleanup_dma()
462 struct sh_mobile_i2c_data *pd = data; in sh_mobile_i2c_dma_callback() local
464 sh_mobile_i2c_cleanup_dma(pd, false); in sh_mobile_i2c_dma_callback()
465 pd->pos = pd->msg->len; in sh_mobile_i2c_dma_callback()
466 pd->stop_after_dma = true; in sh_mobile_i2c_dma_callback()
468 iic_set_clr(pd, ICIC, 0, ICIC_TDMAE | ICIC_RDMAE); in sh_mobile_i2c_dma_callback()
507 static void sh_mobile_i2c_xfer_dma(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_xfer_dma() argument
509 bool read = pd->msg->flags & I2C_M_RD; in sh_mobile_i2c_xfer_dma()
511 struct dma_chan *chan = read ? pd->dma_rx : pd->dma_tx; in sh_mobile_i2c_xfer_dma()
518 chan = pd->dma_rx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_DEV_TO_MEM, in sh_mobile_i2c_xfer_dma()
519 pd->res->start + ICDR); in sh_mobile_i2c_xfer_dma()
521 chan = pd->dma_tx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_MEM_TO_DEV, in sh_mobile_i2c_xfer_dma()
522 pd->res->start + ICDR); in sh_mobile_i2c_xfer_dma()
528 dma_addr = dma_map_single(chan->device->dev, pd->dma_buf, pd->msg->len, dir); in sh_mobile_i2c_xfer_dma()
530 dev_dbg(pd->dev, "dma map failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
534 sg_dma_len(&pd->sg) = pd->msg->len; in sh_mobile_i2c_xfer_dma()
535 sg_dma_address(&pd->sg) = dma_addr; in sh_mobile_i2c_xfer_dma()
537 pd->dma_direction = dir; in sh_mobile_i2c_xfer_dma()
539 txdesc = dmaengine_prep_slave_sg(chan, &pd->sg, 1, in sh_mobile_i2c_xfer_dma()
543 dev_dbg(pd->dev, "dma prep slave sg failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
544 sh_mobile_i2c_cleanup_dma(pd, false); in sh_mobile_i2c_xfer_dma()
549 txdesc->callback_param = pd; in sh_mobile_i2c_xfer_dma()
553 dev_dbg(pd->dev, "submitting dma failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
554 sh_mobile_i2c_cleanup_dma(pd, false); in sh_mobile_i2c_xfer_dma()
561 static void start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg, in start_ch() argument
566 iic_wr(pd, ICCR, ICCR_SCP); in start_ch()
569 iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP); in start_ch()
572 iic_wr(pd, ICCL, pd->iccl & 0xff); in start_ch()
573 iic_wr(pd, ICCH, pd->icch & 0xff); in start_ch()
576 pd->msg = usr_msg; in start_ch()
577 pd->pos = -1; in start_ch()
578 pd->sr = 0; in start_ch()
580 if (pd->atomic_xfer) in start_ch()
583 pd->dma_buf = i2c_get_dma_safe_msg_buf(pd->msg, 8); in start_ch()
584 if (pd->dma_buf) in start_ch()
585 sh_mobile_i2c_xfer_dma(pd); in start_ch()
588 iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); in start_ch()
591 static int poll_dte(struct sh_mobile_i2c_data *pd) in poll_dte() argument
596 u_int8_t val = iic_rd(pd, ICSR); in poll_dte()
610 static int poll_busy(struct sh_mobile_i2c_data *pd) in poll_busy() argument
615 u_int8_t val = iic_rd(pd, ICSR); in poll_busy()
617 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); in poll_busy()
625 val |= pd->sr; in poll_busy()
639 static int sh_mobile_xfer(struct sh_mobile_i2c_data *pd, in sh_mobile_xfer() argument
648 pm_runtime_get_sync(pd->dev); in sh_mobile_xfer()
652 bool do_start = pd->send_stop || !i; in sh_mobile_xfer()
654 pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP; in sh_mobile_xfer()
655 pd->stop_after_dma = false; in sh_mobile_xfer()
657 start_ch(pd, msg, do_start); in sh_mobile_xfer()
660 i2c_op(pd, OP_START); in sh_mobile_xfer()
662 if (pd->atomic_xfer) { in sh_mobile_xfer()
663 unsigned long j = jiffies + pd->adap.timeout; in sh_mobile_xfer()
667 !(pd->sr & (ICSR_TACK | SW_DONE))) { in sh_mobile_xfer()
668 unsigned char sr = iic_rd(pd, ICSR); in sh_mobile_xfer()
672 sh_mobile_i2c_isr(0, pd); in sh_mobile_xfer()
681 time_left = wait_event_timeout(pd->wait, in sh_mobile_xfer()
682 pd->sr & (ICSR_TACK | SW_DONE), in sh_mobile_xfer()
683 pd->adap.timeout); in sh_mobile_xfer()
686 i2c_put_dma_safe_msg_buf(pd->dma_buf, pd->msg, in sh_mobile_xfer()
687 pd->stop_after_dma); in sh_mobile_xfer()
691 if (pd->dma_direction != DMA_NONE) in sh_mobile_xfer()
692 sh_mobile_i2c_cleanup_dma(pd, true); in sh_mobile_xfer()
698 if (pd->send_stop) in sh_mobile_xfer()
699 err = poll_busy(pd); in sh_mobile_xfer()
701 err = poll_dte(pd); in sh_mobile_xfer()
707 iic_wr(pd, ICCR, ICCR_SCP); in sh_mobile_xfer()
710 pm_runtime_put_sync(pd->dev); in sh_mobile_xfer()
719 struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter); in sh_mobile_i2c_xfer() local
721 pd->atomic_xfer = false; in sh_mobile_i2c_xfer()
722 return sh_mobile_xfer(pd, msgs, num); in sh_mobile_i2c_xfer()
729 struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter); in sh_mobile_i2c_xfer_atomic() local
731 pd->atomic_xfer = true; in sh_mobile_i2c_xfer_atomic()
732 return sh_mobile_xfer(pd, msgs, num); in sh_mobile_i2c_xfer_atomic()
753 static int sh_mobile_i2c_r8a7740_workaround(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_r8a7740_workaround() argument
755 iic_set_clr(pd, ICCR, ICCR_ICE, 0); in sh_mobile_i2c_r8a7740_workaround()
756 iic_rd(pd, ICCR); /* dummy read */ in sh_mobile_i2c_r8a7740_workaround()
758 iic_set_clr(pd, ICSTART, ICSTART_ICSTART, 0); in sh_mobile_i2c_r8a7740_workaround()
759 iic_rd(pd, ICSTART); /* dummy read */ in sh_mobile_i2c_r8a7740_workaround()
763 iic_wr(pd, ICCR, ICCR_SCP); in sh_mobile_i2c_r8a7740_workaround()
764 iic_wr(pd, ICSTART, 0); in sh_mobile_i2c_r8a7740_workaround()
768 iic_wr(pd, ICCR, ICCR_TRS); in sh_mobile_i2c_r8a7740_workaround()
770 iic_wr(pd, ICCR, 0); in sh_mobile_i2c_r8a7740_workaround()
772 iic_wr(pd, ICCR, ICCR_TRS); in sh_mobile_i2c_r8a7740_workaround()
775 return sh_mobile_i2c_v2_init(pd); in sh_mobile_i2c_r8a7740_workaround()
812 static void sh_mobile_i2c_release_dma(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_release_dma() argument
814 if (!IS_ERR(pd->dma_tx)) { in sh_mobile_i2c_release_dma()
815 dma_release_channel(pd->dma_tx); in sh_mobile_i2c_release_dma()
816 pd->dma_tx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_release_dma()
819 if (!IS_ERR(pd->dma_rx)) { in sh_mobile_i2c_release_dma()
820 dma_release_channel(pd->dma_rx); in sh_mobile_i2c_release_dma()
821 pd->dma_rx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_release_dma()
825 static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_hook_irqs() argument
837 0, dev_name(&dev->dev), pd); in sh_mobile_i2c_hook_irqs()
851 0, dev_name(&dev->dev), pd); in sh_mobile_i2c_hook_irqs()
866 struct sh_mobile_i2c_data *pd; in sh_mobile_i2c_probe() local
872 pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL); in sh_mobile_i2c_probe()
873 if (!pd) in sh_mobile_i2c_probe()
876 pd->clk = devm_clk_get(&dev->dev, NULL); in sh_mobile_i2c_probe()
877 if (IS_ERR(pd->clk)) { in sh_mobile_i2c_probe()
879 return PTR_ERR(pd->clk); in sh_mobile_i2c_probe()
882 ret = sh_mobile_i2c_hook_irqs(dev, pd); in sh_mobile_i2c_probe()
886 pd->dev = &dev->dev; in sh_mobile_i2c_probe()
887 platform_set_drvdata(dev, pd); in sh_mobile_i2c_probe()
889 pd->reg = devm_platform_get_and_ioremap_resource(dev, 0, &pd->res); in sh_mobile_i2c_probe()
890 if (IS_ERR(pd->reg)) in sh_mobile_i2c_probe()
891 return PTR_ERR(pd->reg); in sh_mobile_i2c_probe()
894 pd->bus_speed = (ret || !bus_speed) ? I2C_MAX_STANDARD_MODE_FREQ : bus_speed; in sh_mobile_i2c_probe()
895 pd->clks_per_count = 1; in sh_mobile_i2c_probe()
898 if (resource_size(pd->res) > 0x17) in sh_mobile_i2c_probe()
899 pd->flags |= IIC_FLAG_HAS_ICIC67; in sh_mobile_i2c_probe()
906 pd->clks_per_count = config->clks_per_count; in sh_mobile_i2c_probe()
907 ret = config->setup(pd); in sh_mobile_i2c_probe()
909 ret = sh_mobile_i2c_init(pd); in sh_mobile_i2c_probe()
917 sg_init_table(&pd->sg, 1); in sh_mobile_i2c_probe()
918 pd->dma_direction = DMA_NONE; in sh_mobile_i2c_probe()
919 pd->dma_rx = pd->dma_tx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_probe()
922 adap = &pd->adap; in sh_mobile_i2c_probe()
923 i2c_set_adapdata(adap, pd); in sh_mobile_i2c_probe()
935 spin_lock_init(&pd->lock); in sh_mobile_i2c_probe()
936 init_waitqueue_head(&pd->wait); in sh_mobile_i2c_probe()
940 sh_mobile_i2c_release_dma(pd); in sh_mobile_i2c_probe()
944 dev_info(&dev->dev, "I2C adapter %d, bus speed %lu Hz\n", adap->nr, pd->bus_speed); in sh_mobile_i2c_probe()
951 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); in sh_mobile_i2c_remove() local
953 i2c_del_adapter(&pd->adap); in sh_mobile_i2c_remove()
954 sh_mobile_i2c_release_dma(pd); in sh_mobile_i2c_remove()
960 struct sh_mobile_i2c_data *pd = dev_get_drvdata(dev); in sh_mobile_i2c_suspend() local
962 i2c_mark_adapter_suspended(&pd->adap); in sh_mobile_i2c_suspend()
968 struct sh_mobile_i2c_data *pd = dev_get_drvdata(dev); in sh_mobile_i2c_resume() local
970 i2c_mark_adapter_resumed(&pd->adap); in sh_mobile_i2c_resume()