Lines Matching refs:dev
32 void at91_init_twi_bus_master(struct at91_twi_dev *dev) in at91_init_twi_bus_master() argument
34 struct at91_twi_pdata *pdata = dev->pdata; in at91_init_twi_bus_master()
38 if (dev->fifo_size) in at91_init_twi_bus_master()
39 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN); in at91_init_twi_bus_master()
40 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN); in at91_init_twi_bus_master()
41 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS); in at91_init_twi_bus_master()
42 at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg); in at91_init_twi_bus_master()
45 if (pdata->has_dig_filtr && dev->enable_dig_filt) in at91_init_twi_bus_master()
49 if (pdata->has_adv_dig_filtr && dev->enable_dig_filt) in at91_init_twi_bus_master()
51 (AT91_TWI_FILTR_THRES(dev->filter_width) & in at91_init_twi_bus_master()
55 if (pdata->has_ana_filtr && dev->enable_ana_filt) in at91_init_twi_bus_master()
59 at91_twi_write(dev, AT91_TWI_FILTR, filtr); in at91_init_twi_bus_master()
66 static void at91_calc_twi_clock(struct at91_twi_dev *dev) in at91_calc_twi_clock() argument
69 struct at91_twi_pdata *pdata = dev->pdata; in at91_calc_twi_clock()
74 i2c_parse_fw_timings(dev->dev, t, true); in at91_calc_twi_clock()
76 div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk), in at91_calc_twi_clock()
82 dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n", in at91_calc_twi_clock()
95 * (clk_get_rate(dev->clk) / 1000), 1000000); in at91_calc_twi_clock()
100 dev_warn(dev->dev, in at91_calc_twi_clock()
113 * (clk_get_rate(dev->clk) / 1000), 1000000); in at91_calc_twi_clock()
115 dev_warn(dev->dev, in at91_calc_twi_clock()
122 dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv in at91_calc_twi_clock()
125 dev->filter_width = filter_width; in at91_calc_twi_clock()
127 dev_dbg(dev->dev, "cdiv %d ckdiv %d hold %d (%d ns), filter_width %d (%d ns)\n", in at91_calc_twi_clock()
132 static void at91_twi_dma_cleanup(struct at91_twi_dev *dev) in at91_twi_dma_cleanup() argument
134 struct at91_twi_dma *dma = &dev->dma; in at91_twi_dma_cleanup()
136 at91_twi_irq_save(dev); in at91_twi_dma_cleanup()
146 dma_unmap_single(dev->dev, sg_dma_address(&dma->sg[0]), in at91_twi_dma_cleanup()
147 dev->buf_len, dma->direction); in at91_twi_dma_cleanup()
151 at91_twi_irq_restore(dev); in at91_twi_dma_cleanup()
154 static void at91_twi_write_next_byte(struct at91_twi_dev *dev) in at91_twi_write_next_byte() argument
156 if (!dev->buf_len) in at91_twi_write_next_byte()
160 writeb_relaxed(*dev->buf, dev->base + AT91_TWI_THR); in at91_twi_write_next_byte()
163 if (--dev->buf_len == 0) { in at91_twi_write_next_byte()
164 if (!dev->use_alt_cmd) in at91_twi_write_next_byte()
165 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); in at91_twi_write_next_byte()
166 at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_TXRDY); in at91_twi_write_next_byte()
169 dev_dbg(dev->dev, "wrote 0x%x, to go %zu\n", *dev->buf, dev->buf_len); in at91_twi_write_next_byte()
171 ++dev->buf; in at91_twi_write_next_byte()
176 struct at91_twi_dev *dev = (struct at91_twi_dev *)data; in at91_twi_write_data_dma_callback() local
178 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]), in at91_twi_write_data_dma_callback()
179 dev->buf_len, DMA_TO_DEVICE); in at91_twi_write_data_dma_callback()
188 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP); in at91_twi_write_data_dma_callback()
189 if (!dev->use_alt_cmd) in at91_twi_write_data_dma_callback()
190 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); in at91_twi_write_data_dma_callback()
193 static void at91_twi_write_data_dma(struct at91_twi_dev *dev) in at91_twi_write_data_dma() argument
197 struct at91_twi_dma *dma = &dev->dma; in at91_twi_write_data_dma()
201 if (!dev->buf_len) in at91_twi_write_data_dma()
206 at91_twi_irq_save(dev); in at91_twi_write_data_dma()
207 dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len, in at91_twi_write_data_dma()
209 if (dma_mapping_error(dev->dev, dma_addr)) { in at91_twi_write_data_dma()
210 dev_err(dev->dev, "dma map failed\n"); in at91_twi_write_data_dma()
214 at91_twi_irq_restore(dev); in at91_twi_write_data_dma()
216 if (dev->fifo_size) { in at91_twi_write_data_dma()
223 part1_len = dev->buf_len & ~0x3; in at91_twi_write_data_dma()
230 part2_len = dev->buf_len & 0x3; in at91_twi_write_data_dma()
241 fifo_mr = at91_twi_read(dev, AT91_TWI_FMR); in at91_twi_write_data_dma()
244 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr); in at91_twi_write_data_dma()
246 sg_dma_len(&dma->sg[0]) = dev->buf_len; in at91_twi_write_data_dma()
254 dev_err(dev->dev, "dma prep slave sg failed\n"); in at91_twi_write_data_dma()
259 txdesc->callback_param = dev; in at91_twi_write_data_dma()
268 at91_twi_dma_cleanup(dev); in at91_twi_write_data_dma()
271 static void at91_twi_read_next_byte(struct at91_twi_dev *dev) in at91_twi_read_next_byte() argument
277 if (!dev->buf_len) { in at91_twi_read_next_byte()
278 at91_twi_read(dev, AT91_TWI_RHR); in at91_twi_read_next_byte()
283 *dev->buf = readb_relaxed(dev->base + AT91_TWI_RHR); in at91_twi_read_next_byte()
284 --dev->buf_len; in at91_twi_read_next_byte()
287 if (dev->recv_len_abort) in at91_twi_read_next_byte()
291 if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) { in at91_twi_read_next_byte()
293 if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) { in at91_twi_read_next_byte()
294 dev->msg->flags &= ~I2C_M_RECV_LEN; in at91_twi_read_next_byte()
295 dev->buf_len += *dev->buf; in at91_twi_read_next_byte()
296 dev->msg->len = dev->buf_len + 1; in at91_twi_read_next_byte()
297 dev_dbg(dev->dev, "received block length %zu\n", in at91_twi_read_next_byte()
298 dev->buf_len); in at91_twi_read_next_byte()
301 dev->recv_len_abort = true; in at91_twi_read_next_byte()
302 dev->buf_len = 1; in at91_twi_read_next_byte()
307 if (!dev->use_alt_cmd && dev->buf_len == 1) in at91_twi_read_next_byte()
308 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); in at91_twi_read_next_byte()
310 dev_dbg(dev->dev, "read 0x%x, to go %zu\n", *dev->buf, dev->buf_len); in at91_twi_read_next_byte()
312 ++dev->buf; in at91_twi_read_next_byte()
317 struct at91_twi_dev *dev = (struct at91_twi_dev *)data; in at91_twi_read_data_dma_callback() local
320 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]), in at91_twi_read_data_dma_callback()
321 dev->buf_len, DMA_FROM_DEVICE); in at91_twi_read_data_dma_callback()
323 if (!dev->use_alt_cmd) { in at91_twi_read_data_dma_callback()
325 dev->buf += dev->buf_len - 2; in at91_twi_read_data_dma_callback()
326 dev->buf_len = 2; in at91_twi_read_data_dma_callback()
329 at91_twi_write(dev, AT91_TWI_IER, ier); in at91_twi_read_data_dma_callback()
332 static void at91_twi_read_data_dma(struct at91_twi_dev *dev) in at91_twi_read_data_dma() argument
336 struct at91_twi_dma *dma = &dev->dma; in at91_twi_read_data_dma()
340 buf_len = (dev->use_alt_cmd) ? dev->buf_len : dev->buf_len - 2; in at91_twi_read_data_dma()
344 at91_twi_irq_save(dev); in at91_twi_read_data_dma()
345 dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE); in at91_twi_read_data_dma()
346 if (dma_mapping_error(dev->dev, dma_addr)) { in at91_twi_read_data_dma()
347 dev_err(dev->dev, "dma map failed\n"); in at91_twi_read_data_dma()
351 at91_twi_irq_restore(dev); in at91_twi_read_data_dma()
353 if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) { in at91_twi_read_data_dma()
360 fifo_mr = at91_twi_read(dev, AT91_TWI_FMR); in at91_twi_read_data_dma()
363 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr); in at91_twi_read_data_dma()
372 dev_err(dev->dev, "dma prep slave sg failed\n"); in at91_twi_read_data_dma()
377 rxdesc->callback_param = dev; in at91_twi_read_data_dma()
386 at91_twi_dma_cleanup(dev); in at91_twi_read_data_dma()
391 struct at91_twi_dev *dev = dev_id; in atmel_twi_interrupt() local
392 const unsigned status = at91_twi_read(dev, AT91_TWI_SR); in atmel_twi_interrupt()
393 const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR); in atmel_twi_interrupt()
420 at91_twi_read_next_byte(dev); in atmel_twi_interrupt()
421 } while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY); in atmel_twi_interrupt()
463 at91_disable_twi_interrupts(dev); in atmel_twi_interrupt()
464 complete(&dev->cmd_complete); in atmel_twi_interrupt()
466 at91_twi_write_next_byte(dev); in atmel_twi_interrupt()
470 dev->transfer_status |= status; in atmel_twi_interrupt()
475 static int at91_do_twi_transfer(struct at91_twi_dev *dev) in at91_do_twi_transfer() argument
479 bool has_unre_flag = dev->pdata->has_unre_flag; in at91_do_twi_transfer()
480 bool has_alt_cmd = dev->pdata->has_alt_cmd; in at91_do_twi_transfer()
525 dev_dbg(dev->dev, "transfer: %s %zu bytes.\n", in at91_do_twi_transfer()
526 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len); in at91_do_twi_transfer()
528 reinit_completion(&dev->cmd_complete); in at91_do_twi_transfer()
529 dev->transfer_status = 0; in at91_do_twi_transfer()
532 at91_twi_read(dev, AT91_TWI_SR); in at91_do_twi_transfer()
534 if (dev->fifo_size) { in at91_do_twi_transfer()
535 unsigned fifo_mr = at91_twi_read(dev, AT91_TWI_FMR); in at91_do_twi_transfer()
542 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr); in at91_do_twi_transfer()
545 at91_twi_write(dev, AT91_TWI_CR, in at91_do_twi_transfer()
549 if (!dev->buf_len) { in at91_do_twi_transfer()
550 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK); in at91_do_twi_transfer()
551 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP); in at91_do_twi_transfer()
552 } else if (dev->msg->flags & I2C_M_RD) { in at91_do_twi_transfer()
556 if (!dev->use_alt_cmd && dev->buf_len <= 1 && in at91_do_twi_transfer()
557 !(dev->msg->flags & I2C_M_RECV_LEN)) in at91_do_twi_transfer()
559 at91_twi_write(dev, AT91_TWI_CR, start_flags); in at91_do_twi_transfer()
569 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) { in at91_do_twi_transfer()
570 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK); in at91_do_twi_transfer()
571 at91_twi_read_data_dma(dev); in at91_do_twi_transfer()
573 at91_twi_write(dev, AT91_TWI_IER, in at91_do_twi_transfer()
579 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) { in at91_do_twi_transfer()
580 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK); in at91_do_twi_transfer()
581 at91_twi_write_data_dma(dev); in at91_do_twi_transfer()
583 at91_twi_write_next_byte(dev); in at91_do_twi_transfer()
584 at91_twi_write(dev, AT91_TWI_IER, in at91_do_twi_transfer()
586 (dev->buf_len ? AT91_TWI_TXRDY : 0)); in at91_do_twi_transfer()
590 time_left = wait_for_completion_timeout(&dev->cmd_complete, in at91_do_twi_transfer()
591 dev->adapter.timeout); in at91_do_twi_transfer()
593 dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR); in at91_do_twi_transfer()
594 dev_err(dev->dev, "controller timed out\n"); in at91_do_twi_transfer()
595 at91_init_twi_bus(dev); in at91_do_twi_transfer()
599 if (dev->transfer_status & AT91_TWI_NACK) { in at91_do_twi_transfer()
600 dev_dbg(dev->dev, "received nack\n"); in at91_do_twi_transfer()
604 if (dev->transfer_status & AT91_TWI_OVRE) { in at91_do_twi_transfer()
605 dev_err(dev->dev, "overrun while reading\n"); in at91_do_twi_transfer()
609 if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) { in at91_do_twi_transfer()
610 dev_err(dev->dev, "underrun while writing\n"); in at91_do_twi_transfer()
614 if ((has_alt_cmd || dev->fifo_size) && in at91_do_twi_transfer()
615 (dev->transfer_status & AT91_TWI_LOCK)) { in at91_do_twi_transfer()
616 dev_err(dev->dev, "tx locked\n"); in at91_do_twi_transfer()
620 if (dev->recv_len_abort) { in at91_do_twi_transfer()
621 dev_err(dev->dev, "invalid smbus block length recvd\n"); in at91_do_twi_transfer()
626 dev_dbg(dev->dev, "transfer complete\n"); in at91_do_twi_transfer()
632 at91_twi_dma_cleanup(dev); in at91_do_twi_transfer()
634 if ((has_alt_cmd || dev->fifo_size) && in at91_do_twi_transfer()
635 (dev->transfer_status & AT91_TWI_LOCK)) { in at91_do_twi_transfer()
636 dev_dbg(dev->dev, "unlock tx\n"); in at91_do_twi_transfer()
637 at91_twi_write(dev, AT91_TWI_CR, in at91_do_twi_transfer()
646 i2c_recover_bus(&dev->adapter); in at91_do_twi_transfer()
653 struct at91_twi_dev *dev = i2c_get_adapdata(adap); in at91_twi_xfer() local
660 dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num); in at91_twi_xfer()
662 ret = pm_runtime_get_sync(dev->dev); in at91_twi_xfer()
678 at91_twi_write(dev, AT91_TWI_IADR, internal_address); in at91_twi_xfer()
681 dev->use_alt_cmd = false; in at91_twi_xfer()
683 if (dev->pdata->has_alt_cmd) { in at91_twi_xfer()
686 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN); in at91_twi_xfer()
687 at91_twi_write(dev, AT91_TWI_ACR, in at91_twi_xfer()
690 dev->use_alt_cmd = true; in at91_twi_xfer()
692 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS); in at91_twi_xfer()
696 at91_twi_write(dev, AT91_TWI_MMR, in at91_twi_xfer()
699 ((!dev->use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0)); in at91_twi_xfer()
701 dev->buf_len = m_start->len; in at91_twi_xfer()
702 dev->buf = m_start->buf; in at91_twi_xfer()
703 dev->msg = m_start; in at91_twi_xfer()
704 dev->recv_len_abort = false; in at91_twi_xfer()
706 if (dev->use_dma) { in at91_twi_xfer()
712 dev->buf = dma_buf; in at91_twi_xfer()
715 ret = at91_do_twi_transfer(dev); in at91_twi_xfer()
720 pm_runtime_mark_last_busy(dev->dev); in at91_twi_xfer()
721 pm_runtime_put_autosuspend(dev->dev); in at91_twi_xfer()
746 static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr) in at91_twi_configure_dma() argument
750 struct at91_twi_dma *dma = &dev->dma; in at91_twi_configure_dma()
768 if (dev->fifo_size) in at91_twi_configure_dma()
780 dma->chan_tx = dma_request_chan(dev->dev, "tx"); in at91_twi_configure_dma()
787 dma->chan_rx = dma_request_chan(dev->dev, "rx"); in at91_twi_configure_dma()
796 dev_err(dev->dev, "failed to configure tx channel\n"); in at91_twi_configure_dma()
803 dev_err(dev->dev, "failed to configure rx channel\n"); in at91_twi_configure_dma()
811 dev->use_dma = true; in at91_twi_configure_dma()
813 dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n", in at91_twi_configure_dma()
820 dev_info(dev->dev, "can't get DMA channel, continue without DMA support\n"); in at91_twi_configure_dma()
829 struct at91_twi_dev *dev) in at91_init_twi_recovery_gpio() argument
831 struct i2c_bus_recovery_info *rinfo = &dev->rinfo; in at91_init_twi_recovery_gpio()
833 rinfo->pinctrl = devm_pinctrl_get(&pdev->dev); in at91_init_twi_recovery_gpio()
835 dev_info(dev->dev, "pinctrl unavailable, bus recovery not supported\n"); in at91_init_twi_recovery_gpio()
839 dev_info(dev->dev, "can't get pinctrl, bus recovery not supported\n"); in at91_init_twi_recovery_gpio()
842 dev->adapter.bus_recovery_info = rinfo; in at91_init_twi_recovery_gpio()
849 struct at91_twi_dev *dev = i2c_get_adapdata(adap); in at91_twi_recover_bus_cmd() local
851 dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR); in at91_twi_recover_bus_cmd()
852 if (!(dev->transfer_status & AT91_TWI_SDA)) { in at91_twi_recover_bus_cmd()
853 dev_dbg(dev->dev, "SDA is down; sending bus clear command\n"); in at91_twi_recover_bus_cmd()
854 if (dev->use_alt_cmd) { in at91_twi_recover_bus_cmd()
857 acr = at91_twi_read(dev, AT91_TWI_ACR); in at91_twi_recover_bus_cmd()
859 at91_twi_write(dev, AT91_TWI_ACR, acr); in at91_twi_recover_bus_cmd()
861 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_CLEAR); in at91_twi_recover_bus_cmd()
868 struct at91_twi_dev *dev) in at91_init_twi_recovery_info() argument
870 struct i2c_bus_recovery_info *rinfo = &dev->rinfo; in at91_init_twi_recovery_info()
871 bool has_clear_cmd = dev->pdata->has_clear_cmd; in at91_init_twi_recovery_info()
874 return at91_init_twi_recovery_gpio(pdev, dev); in at91_init_twi_recovery_info()
877 dev->adapter.bus_recovery_info = rinfo; in at91_init_twi_recovery_info()
883 u32 phy_addr, struct at91_twi_dev *dev) in at91_twi_probe_master() argument
887 init_completion(&dev->cmd_complete); in at91_twi_probe_master()
889 rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0, in at91_twi_probe_master()
890 dev_name(dev->dev), dev); in at91_twi_probe_master()
892 dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc); in at91_twi_probe_master()
896 if (dev->dev->of_node) { in at91_twi_probe_master()
897 rc = at91_twi_configure_dma(dev, phy_addr); in at91_twi_probe_master()
902 if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size", in at91_twi_probe_master()
903 &dev->fifo_size)) { in at91_twi_probe_master()
904 dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size); in at91_twi_probe_master()
907 dev->enable_dig_filt = of_property_read_bool(pdev->dev.of_node, in at91_twi_probe_master()
910 dev->enable_ana_filt = of_property_read_bool(pdev->dev.of_node, in at91_twi_probe_master()
912 at91_calc_twi_clock(dev); in at91_twi_probe_master()
914 rc = at91_init_twi_recovery_info(pdev, dev); in at91_twi_probe_master()
918 dev->adapter.algo = &at91_twi_algorithm; in at91_twi_probe_master()
919 dev->adapter.quirks = &at91_twi_quirks; in at91_twi_probe_master()