Lines Matching refs:pd
153 void (*setup)(struct sh_mobile_i2c_data *pd);
197 static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data) in iic_wr() argument
200 data |= pd->icic; in iic_wr()
202 iowrite8(data, pd->reg + offs); in iic_wr()
205 static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs) in iic_rd() argument
207 return ioread8(pd->reg + offs); in iic_rd()
210 static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs, in iic_set_clr() argument
213 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr); in iic_set_clr()
250 static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_init() argument
257 clk_prepare_enable(pd->clk); in sh_mobile_i2c_init()
258 i2c_clk_khz = clk_get_rate(pd->clk) / 1000; in sh_mobile_i2c_init()
259 clk_disable_unprepare(pd->clk); in sh_mobile_i2c_init()
260 i2c_clk_khz /= pd->clks_per_count; in sh_mobile_i2c_init()
262 if (pd->bus_speed == STANDARD_MODE) { in sh_mobile_i2c_init()
266 } else if (pd->bus_speed == FAST_MODE) { in sh_mobile_i2c_init()
271 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n", in sh_mobile_i2c_init()
272 pd->bus_speed); in sh_mobile_i2c_init()
276 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf); in sh_mobile_i2c_init()
277 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf); in sh_mobile_i2c_init()
279 max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff; in sh_mobile_i2c_init()
280 if (pd->iccl > max_val || pd->icch > max_val) { in sh_mobile_i2c_init()
281 dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n", in sh_mobile_i2c_init()
282 pd->iccl, pd->icch); in sh_mobile_i2c_init()
287 if (pd->iccl & 0x100) in sh_mobile_i2c_init()
288 pd->icic |= ICIC_ICCLB8; in sh_mobile_i2c_init()
290 pd->icic &= ~ICIC_ICCLB8; in sh_mobile_i2c_init()
293 if (pd->icch & 0x100) in sh_mobile_i2c_init()
294 pd->icic |= ICIC_ICCHB8; in sh_mobile_i2c_init()
296 pd->icic &= ~ICIC_ICCHB8; in sh_mobile_i2c_init()
298 dev_dbg(pd->dev, "timing values: L/H=0x%x/0x%x\n", pd->iccl, pd->icch); in sh_mobile_i2c_init()
302 static void activate_ch(struct sh_mobile_i2c_data *pd) in activate_ch() argument
305 pm_runtime_get_sync(pd->dev); in activate_ch()
306 clk_prepare_enable(pd->clk); in activate_ch()
309 iic_set_clr(pd, ICCR, ICCR_ICE, 0); in activate_ch()
312 iic_wr(pd, ICIC, 0); in activate_ch()
315 iic_wr(pd, ICCL, pd->iccl & 0xff); in activate_ch()
316 iic_wr(pd, ICCH, pd->icch & 0xff); in activate_ch()
319 static void deactivate_ch(struct sh_mobile_i2c_data *pd) in deactivate_ch() argument
322 iic_wr(pd, ICSR, 0); in deactivate_ch()
323 iic_wr(pd, ICIC, 0); in deactivate_ch()
326 iic_set_clr(pd, ICCR, 0, ICCR_ICE); in deactivate_ch()
329 clk_disable_unprepare(pd->clk); in deactivate_ch()
330 pm_runtime_put_sync(pd->dev); in deactivate_ch()
333 static unsigned char i2c_op(struct sh_mobile_i2c_data *pd, in i2c_op() argument
339 dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data); in i2c_op()
341 spin_lock_irqsave(&pd->lock, flags); in i2c_op()
345 iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY); in i2c_op()
348 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE); in i2c_op()
349 iic_wr(pd, ICDR, data); in i2c_op()
352 iic_wr(pd, ICDR, data); in i2c_op()
355 iic_wr(pd, ICDR, data); in i2c_op()
358 iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS in i2c_op()
362 iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP); in i2c_op()
365 ret = iic_rd(pd, ICDR); in i2c_op()
368 iic_wr(pd, ICIC, in i2c_op()
370 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK); in i2c_op()
373 iic_wr(pd, ICIC, in i2c_op()
375 ret = iic_rd(pd, ICDR); in i2c_op()
376 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK); in i2c_op()
380 spin_unlock_irqrestore(&pd->lock, flags); in i2c_op()
382 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret); in i2c_op()
386 static bool sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_is_first_byte() argument
388 return pd->pos == -1; in sh_mobile_i2c_is_first_byte()
391 static bool sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_is_last_byte() argument
393 return pd->pos == pd->msg->len - 1; in sh_mobile_i2c_is_last_byte()
396 static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd, in sh_mobile_i2c_get_data() argument
399 switch (pd->pos) { in sh_mobile_i2c_get_data()
401 *buf = (pd->msg->addr & 0x7f) << 1; in sh_mobile_i2c_get_data()
402 *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0; in sh_mobile_i2c_get_data()
405 *buf = pd->msg->buf[pd->pos]; in sh_mobile_i2c_get_data()
409 static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_isr_tx() argument
413 if (pd->pos == pd->msg->len) { in sh_mobile_i2c_isr_tx()
415 if (pd->send_stop && pd->stop_after_dma) in sh_mobile_i2c_isr_tx()
416 i2c_op(pd, OP_TX_STOP, 0); in sh_mobile_i2c_isr_tx()
420 sh_mobile_i2c_get_data(pd, &data); in sh_mobile_i2c_isr_tx()
422 if (sh_mobile_i2c_is_last_byte(pd)) in sh_mobile_i2c_isr_tx()
423 i2c_op(pd, OP_TX_STOP_DATA, data); in sh_mobile_i2c_isr_tx()
424 else if (sh_mobile_i2c_is_first_byte(pd)) in sh_mobile_i2c_isr_tx()
425 i2c_op(pd, OP_TX_FIRST, data); in sh_mobile_i2c_isr_tx()
427 i2c_op(pd, OP_TX, data); in sh_mobile_i2c_isr_tx()
429 pd->pos++; in sh_mobile_i2c_isr_tx()
433 static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_isr_rx() argument
439 if (pd->pos <= -1) { in sh_mobile_i2c_isr_rx()
440 sh_mobile_i2c_get_data(pd, &data); in sh_mobile_i2c_isr_rx()
442 if (sh_mobile_i2c_is_first_byte(pd)) in sh_mobile_i2c_isr_rx()
443 i2c_op(pd, OP_TX_FIRST, data); in sh_mobile_i2c_isr_rx()
445 i2c_op(pd, OP_TX, data); in sh_mobile_i2c_isr_rx()
449 if (pd->pos == 0) { in sh_mobile_i2c_isr_rx()
450 i2c_op(pd, OP_TX_TO_RX, 0); in sh_mobile_i2c_isr_rx()
454 real_pos = pd->pos - 2; in sh_mobile_i2c_isr_rx()
456 if (pd->pos == pd->msg->len) { in sh_mobile_i2c_isr_rx()
457 if (pd->stop_after_dma) { in sh_mobile_i2c_isr_rx()
459 i2c_op(pd, OP_RX_STOP, 0); in sh_mobile_i2c_isr_rx()
460 pd->pos++; in sh_mobile_i2c_isr_rx()
465 i2c_op(pd, OP_RX_STOP, 0); in sh_mobile_i2c_isr_rx()
468 data = i2c_op(pd, OP_RX_STOP_DATA, 0); in sh_mobile_i2c_isr_rx()
470 data = i2c_op(pd, OP_RX, 0); in sh_mobile_i2c_isr_rx()
473 pd->msg->buf[real_pos] = data; in sh_mobile_i2c_isr_rx()
476 pd->pos++; in sh_mobile_i2c_isr_rx()
477 return pd->pos == (pd->msg->len + 2); in sh_mobile_i2c_isr_rx()
482 struct sh_mobile_i2c_data *pd = dev_id; in sh_mobile_i2c_isr() local
486 sr = iic_rd(pd, ICSR); in sh_mobile_i2c_isr()
487 pd->sr |= sr; /* remember state */ in sh_mobile_i2c_isr()
489 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, in sh_mobile_i2c_isr()
490 (pd->msg->flags & I2C_M_RD) ? "read" : "write", in sh_mobile_i2c_isr()
491 pd->pos, pd->msg->len); in sh_mobile_i2c_isr()
494 if (pd->dma_direction == DMA_TO_DEVICE && pd->pos == 0) in sh_mobile_i2c_isr()
495 iic_set_clr(pd, ICIC, ICIC_TDMAE, 0); in sh_mobile_i2c_isr()
498 iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK)); in sh_mobile_i2c_isr()
499 else if (pd->msg->flags & I2C_M_RD) in sh_mobile_i2c_isr()
500 wakeup = sh_mobile_i2c_isr_rx(pd); in sh_mobile_i2c_isr()
502 wakeup = sh_mobile_i2c_isr_tx(pd); in sh_mobile_i2c_isr()
505 if (pd->dma_direction == DMA_FROM_DEVICE && pd->pos == 1) in sh_mobile_i2c_isr()
506 iic_set_clr(pd, ICIC, ICIC_RDMAE, 0); in sh_mobile_i2c_isr()
509 iic_wr(pd, ICSR, sr & ~ICSR_WAIT); in sh_mobile_i2c_isr()
512 pd->sr |= SW_DONE; in sh_mobile_i2c_isr()
513 wake_up(&pd->wait); in sh_mobile_i2c_isr()
517 iic_rd(pd, ICSR); in sh_mobile_i2c_isr()
522 static void sh_mobile_i2c_dma_unmap(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_dma_unmap() argument
524 struct dma_chan *chan = pd->dma_direction == DMA_FROM_DEVICE in sh_mobile_i2c_dma_unmap()
525 ? pd->dma_rx : pd->dma_tx; in sh_mobile_i2c_dma_unmap()
527 dma_unmap_single(chan->device->dev, sg_dma_address(&pd->sg), in sh_mobile_i2c_dma_unmap()
528 pd->msg->len, pd->dma_direction); in sh_mobile_i2c_dma_unmap()
530 pd->dma_direction = DMA_NONE; in sh_mobile_i2c_dma_unmap()
533 static void sh_mobile_i2c_cleanup_dma(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_cleanup_dma() argument
535 if (pd->dma_direction == DMA_NONE) in sh_mobile_i2c_cleanup_dma()
537 else if (pd->dma_direction == DMA_FROM_DEVICE) in sh_mobile_i2c_cleanup_dma()
538 dmaengine_terminate_all(pd->dma_rx); in sh_mobile_i2c_cleanup_dma()
539 else if (pd->dma_direction == DMA_TO_DEVICE) in sh_mobile_i2c_cleanup_dma()
540 dmaengine_terminate_all(pd->dma_tx); in sh_mobile_i2c_cleanup_dma()
542 sh_mobile_i2c_dma_unmap(pd); in sh_mobile_i2c_cleanup_dma()
547 struct sh_mobile_i2c_data *pd = data; in sh_mobile_i2c_dma_callback() local
549 sh_mobile_i2c_dma_unmap(pd); in sh_mobile_i2c_dma_callback()
550 pd->pos = pd->msg->len; in sh_mobile_i2c_dma_callback()
551 pd->stop_after_dma = true; in sh_mobile_i2c_dma_callback()
553 iic_set_clr(pd, ICIC, 0, ICIC_TDMAE | ICIC_RDMAE); in sh_mobile_i2c_dma_callback()
592 static void sh_mobile_i2c_xfer_dma(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_xfer_dma() argument
594 bool read = pd->msg->flags & I2C_M_RD; in sh_mobile_i2c_xfer_dma()
596 struct dma_chan *chan = read ? pd->dma_rx : pd->dma_tx; in sh_mobile_i2c_xfer_dma()
603 chan = pd->dma_rx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_DEV_TO_MEM, in sh_mobile_i2c_xfer_dma()
604 pd->res->start + ICDR); in sh_mobile_i2c_xfer_dma()
606 chan = pd->dma_tx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_MEM_TO_DEV, in sh_mobile_i2c_xfer_dma()
607 pd->res->start + ICDR); in sh_mobile_i2c_xfer_dma()
613 dma_addr = dma_map_single(chan->device->dev, pd->msg->buf, pd->msg->len, dir); in sh_mobile_i2c_xfer_dma()
614 if (dma_mapping_error(pd->dev, dma_addr)) { in sh_mobile_i2c_xfer_dma()
615 dev_dbg(pd->dev, "dma map failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
619 sg_dma_len(&pd->sg) = pd->msg->len; in sh_mobile_i2c_xfer_dma()
620 sg_dma_address(&pd->sg) = dma_addr; in sh_mobile_i2c_xfer_dma()
622 pd->dma_direction = dir; in sh_mobile_i2c_xfer_dma()
624 txdesc = dmaengine_prep_slave_sg(chan, &pd->sg, 1, in sh_mobile_i2c_xfer_dma()
628 dev_dbg(pd->dev, "dma prep slave sg failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
629 sh_mobile_i2c_cleanup_dma(pd); in sh_mobile_i2c_xfer_dma()
634 txdesc->callback_param = pd; in sh_mobile_i2c_xfer_dma()
638 dev_dbg(pd->dev, "submitting dma failed, using PIO\n"); in sh_mobile_i2c_xfer_dma()
639 sh_mobile_i2c_cleanup_dma(pd); in sh_mobile_i2c_xfer_dma()
646 static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg, in start_ch() argument
650 dev_err(pd->dev, "Unsupported zero length i2c read\n"); in start_ch()
656 iic_set_clr(pd, ICCR, 0, ICCR_ICE); in start_ch()
659 iic_set_clr(pd, ICCR, ICCR_ICE, 0); in start_ch()
662 iic_wr(pd, ICCL, pd->iccl & 0xff); in start_ch()
663 iic_wr(pd, ICCH, pd->icch & 0xff); in start_ch()
666 pd->msg = usr_msg; in start_ch()
667 pd->pos = -1; in start_ch()
668 pd->sr = 0; in start_ch()
670 if (pd->msg->len > 8) in start_ch()
671 sh_mobile_i2c_xfer_dma(pd); in start_ch()
674 iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); in start_ch()
678 static int poll_dte(struct sh_mobile_i2c_data *pd) in poll_dte() argument
683 u_int8_t val = iic_rd(pd, ICSR); in poll_dte()
697 static int poll_busy(struct sh_mobile_i2c_data *pd) in poll_busy() argument
702 u_int8_t val = iic_rd(pd, ICSR); in poll_busy()
704 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); in poll_busy()
712 val |= pd->sr; in poll_busy()
730 struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter); in sh_mobile_i2c_xfer() local
736 activate_ch(pd); in sh_mobile_i2c_xfer()
740 bool do_start = pd->send_stop || !i; in sh_mobile_i2c_xfer()
742 pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP; in sh_mobile_i2c_xfer()
743 pd->stop_after_dma = false; in sh_mobile_i2c_xfer()
745 err = start_ch(pd, msg, do_start); in sh_mobile_i2c_xfer()
750 i2c_op(pd, OP_START, 0); in sh_mobile_i2c_xfer()
753 timeout = wait_event_timeout(pd->wait, in sh_mobile_i2c_xfer()
754 pd->sr & (ICSR_TACK | SW_DONE), in sh_mobile_i2c_xfer()
757 dev_err(pd->dev, "Transfer request timed out\n"); in sh_mobile_i2c_xfer()
758 if (pd->dma_direction != DMA_NONE) in sh_mobile_i2c_xfer()
759 sh_mobile_i2c_cleanup_dma(pd); in sh_mobile_i2c_xfer()
765 if (pd->send_stop) in sh_mobile_i2c_xfer()
766 err = poll_busy(pd); in sh_mobile_i2c_xfer()
768 err = poll_dte(pd); in sh_mobile_i2c_xfer()
773 deactivate_ch(pd); in sh_mobile_i2c_xfer()
794 static void sh_mobile_i2c_r8a7740_workaround(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_r8a7740_workaround() argument
796 iic_set_clr(pd, ICCR, ICCR_ICE, 0); in sh_mobile_i2c_r8a7740_workaround()
797 iic_rd(pd, ICCR); /* dummy read */ in sh_mobile_i2c_r8a7740_workaround()
799 iic_set_clr(pd, ICSTART, ICSTART_ICSTART, 0); in sh_mobile_i2c_r8a7740_workaround()
800 iic_rd(pd, ICSTART); /* dummy read */ in sh_mobile_i2c_r8a7740_workaround()
804 iic_wr(pd, ICCR, ICCR_SCP); in sh_mobile_i2c_r8a7740_workaround()
805 iic_wr(pd, ICSTART, 0); in sh_mobile_i2c_r8a7740_workaround()
809 iic_wr(pd, ICCR, ICCR_TRS); in sh_mobile_i2c_r8a7740_workaround()
811 iic_wr(pd, ICCR, 0); in sh_mobile_i2c_r8a7740_workaround()
813 iic_wr(pd, ICCR, ICCR_TRS); in sh_mobile_i2c_r8a7740_workaround()
845 static void sh_mobile_i2c_release_dma(struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_release_dma() argument
847 if (!IS_ERR(pd->dma_tx)) { in sh_mobile_i2c_release_dma()
848 dma_release_channel(pd->dma_tx); in sh_mobile_i2c_release_dma()
849 pd->dma_tx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_release_dma()
852 if (!IS_ERR(pd->dma_rx)) { in sh_mobile_i2c_release_dma()
853 dma_release_channel(pd->dma_rx); in sh_mobile_i2c_release_dma()
854 pd->dma_rx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_release_dma()
858 static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, struct sh_mobile_i2c_data *pd) in sh_mobile_i2c_hook_irqs() argument
867 0, dev_name(&dev->dev), pd); in sh_mobile_i2c_hook_irqs()
882 struct sh_mobile_i2c_data *pd; in sh_mobile_i2c_probe() local
888 pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL); in sh_mobile_i2c_probe()
889 if (!pd) in sh_mobile_i2c_probe()
892 pd->clk = devm_clk_get(&dev->dev, NULL); in sh_mobile_i2c_probe()
893 if (IS_ERR(pd->clk)) { in sh_mobile_i2c_probe()
895 return PTR_ERR(pd->clk); in sh_mobile_i2c_probe()
898 ret = sh_mobile_i2c_hook_irqs(dev, pd); in sh_mobile_i2c_probe()
902 pd->dev = &dev->dev; in sh_mobile_i2c_probe()
903 platform_set_drvdata(dev, pd); in sh_mobile_i2c_probe()
907 pd->res = res; in sh_mobile_i2c_probe()
908 pd->reg = devm_ioremap_resource(&dev->dev, res); in sh_mobile_i2c_probe()
909 if (IS_ERR(pd->reg)) in sh_mobile_i2c_probe()
910 return PTR_ERR(pd->reg); in sh_mobile_i2c_probe()
914 pd->bus_speed = ret ? STANDARD_MODE : bus_speed; in sh_mobile_i2c_probe()
916 pd->clks_per_count = 1; in sh_mobile_i2c_probe()
926 pd->clks_per_count = config->clks_per_count; in sh_mobile_i2c_probe()
929 config->setup(pd); in sh_mobile_i2c_probe()
933 pd->bus_speed = pdata->bus_speed; in sh_mobile_i2c_probe()
935 pd->clks_per_count = pdata->clks_per_count; in sh_mobile_i2c_probe()
942 pd->flags |= IIC_FLAG_HAS_ICIC67; in sh_mobile_i2c_probe()
944 ret = sh_mobile_i2c_init(pd); in sh_mobile_i2c_probe()
949 sg_init_table(&pd->sg, 1); in sh_mobile_i2c_probe()
950 pd->dma_direction = DMA_NONE; in sh_mobile_i2c_probe()
951 pd->dma_rx = pd->dma_tx = ERR_PTR(-EPROBE_DEFER); in sh_mobile_i2c_probe()
967 adap = &pd->adap; in sh_mobile_i2c_probe()
968 i2c_set_adapdata(adap, pd); in sh_mobile_i2c_probe()
979 spin_lock_init(&pd->lock); in sh_mobile_i2c_probe()
980 init_waitqueue_head(&pd->wait); in sh_mobile_i2c_probe()
984 sh_mobile_i2c_release_dma(pd); in sh_mobile_i2c_probe()
989 dev_info(&dev->dev, "I2C adapter %d, bus speed %lu Hz\n", adap->nr, pd->bus_speed); in sh_mobile_i2c_probe()
996 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); in sh_mobile_i2c_remove() local
998 i2c_del_adapter(&pd->adap); in sh_mobile_i2c_remove()
999 sh_mobile_i2c_release_dma(pd); in sh_mobile_i2c_remove()