Lines Matching refs:idev

152 static void i2c_int_disable(struct axxia_i2c_dev *idev, u32 mask)  in i2c_int_disable()  argument
156 int_en = readl(idev->base + MST_INT_ENABLE); in i2c_int_disable()
157 writel(int_en & ~mask, idev->base + MST_INT_ENABLE); in i2c_int_disable()
160 static void i2c_int_enable(struct axxia_i2c_dev *idev, u32 mask) in i2c_int_enable() argument
164 int_en = readl(idev->base + MST_INT_ENABLE); in i2c_int_enable()
165 writel(int_en | mask, idev->base + MST_INT_ENABLE); in i2c_int_enable()
176 static int axxia_i2c_init(struct axxia_i2c_dev *idev) in axxia_i2c_init() argument
178 u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate; in axxia_i2c_init()
179 u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000; in axxia_i2c_init()
186 dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n", in axxia_i2c_init()
187 idev->bus_clk_rate, clk_mhz, divisor); in axxia_i2c_init()
190 writel(0x01, idev->base + SOFT_RESET); in axxia_i2c_init()
192 while (readl(idev->base + SOFT_RESET) & 1) { in axxia_i2c_init()
194 dev_warn(idev->dev, "Soft reset failed\n"); in axxia_i2c_init()
200 writel(0x1, idev->base + GLOBAL_CONTROL); in axxia_i2c_init()
202 if (idev->bus_clk_rate <= I2C_MAX_STANDARD_MODE_FREQ) { in axxia_i2c_init()
215 writel(t_high, idev->base + SCL_HIGH_PERIOD); in axxia_i2c_init()
217 writel(t_low, idev->base + SCL_LOW_PERIOD); in axxia_i2c_init()
219 writel(t_setup, idev->base + SDA_SETUP_TIME); in axxia_i2c_init()
221 writel(ns_to_clk(300, clk_mhz), idev->base + SDA_HOLD_TIME); in axxia_i2c_init()
223 writel(ns_to_clk(50, clk_mhz), idev->base + SPIKE_FLTR_LEN); in axxia_i2c_init()
238 writel(prescale, idev->base + TIMER_CLOCK_DIV); in axxia_i2c_init()
240 writel(WT_EN | WT_VALUE(tmo_clk), idev->base + WAIT_TIMER_CONTROL); in axxia_i2c_init()
243 i2c_int_disable(idev, ~0); in axxia_i2c_init()
246 writel(0x01, idev->base + INTERRUPT_ENABLE); in axxia_i2c_init()
270 static int axxia_i2c_empty_rx_fifo(struct axxia_i2c_dev *idev) in axxia_i2c_empty_rx_fifo() argument
272 struct i2c_msg *msg = idev->msg_r; in axxia_i2c_empty_rx_fifo()
273 size_t rx_fifo_avail = readl(idev->base + MST_RX_FIFO); in axxia_i2c_empty_rx_fifo()
274 int bytes_to_transfer = min(rx_fifo_avail, msg->len - idev->msg_xfrd_r); in axxia_i2c_empty_rx_fifo()
277 int c = readl(idev->base + MST_DATA); in axxia_i2c_empty_rx_fifo()
279 if (idev->msg_xfrd_r == 0 && i2c_m_recv_len(msg)) { in axxia_i2c_empty_rx_fifo()
284 idev->msg_err = -EPROTO; in axxia_i2c_empty_rx_fifo()
285 i2c_int_disable(idev, ~MST_STATUS_TSS); in axxia_i2c_empty_rx_fifo()
286 complete(&idev->msg_complete); in axxia_i2c_empty_rx_fifo()
290 writel(msg->len, idev->base + MST_RX_XFER); in axxia_i2c_empty_rx_fifo()
292 msg->buf[idev->msg_xfrd_r++] = c; in axxia_i2c_empty_rx_fifo()
302 static int axxia_i2c_fill_tx_fifo(struct axxia_i2c_dev *idev) in axxia_i2c_fill_tx_fifo() argument
304 struct i2c_msg *msg = idev->msg; in axxia_i2c_fill_tx_fifo()
305 size_t tx_fifo_avail = FIFO_SIZE - readl(idev->base + MST_TX_FIFO); in axxia_i2c_fill_tx_fifo()
306 int bytes_to_transfer = min(tx_fifo_avail, msg->len - idev->msg_xfrd); in axxia_i2c_fill_tx_fifo()
307 int ret = msg->len - idev->msg_xfrd - bytes_to_transfer; in axxia_i2c_fill_tx_fifo()
310 writel(msg->buf[idev->msg_xfrd++], idev->base + MST_DATA); in axxia_i2c_fill_tx_fifo()
315 static void axxia_i2c_slv_fifo_event(struct axxia_i2c_dev *idev) in axxia_i2c_slv_fifo_event() argument
317 u32 fifo_status = readl(idev->base + SLV_RX_FIFO); in axxia_i2c_slv_fifo_event()
320 dev_dbg(idev->dev, "slave irq fifo_status=0x%x\n", fifo_status); in axxia_i2c_slv_fifo_event()
324 i2c_slave_event(idev->slave, in axxia_i2c_slv_fifo_event()
327 val = readl(idev->base + SLV_DATA); in axxia_i2c_slv_fifo_event()
328 i2c_slave_event(idev->slave, I2C_SLAVE_WRITE_RECEIVED, &val); in axxia_i2c_slv_fifo_event()
331 readl(idev->base + SLV_DATA); /* dummy read */ in axxia_i2c_slv_fifo_event()
332 i2c_slave_event(idev->slave, I2C_SLAVE_STOP, &val); in axxia_i2c_slv_fifo_event()
335 readl(idev->base + SLV_DATA); /* dummy read */ in axxia_i2c_slv_fifo_event()
338 static irqreturn_t axxia_i2c_slv_isr(struct axxia_i2c_dev *idev) in axxia_i2c_slv_isr() argument
340 u32 status = readl(idev->base + SLV_INT_STATUS); in axxia_i2c_slv_isr()
343 dev_dbg(idev->dev, "slave irq status=0x%x\n", status); in axxia_i2c_slv_isr()
346 axxia_i2c_slv_fifo_event(idev); in axxia_i2c_slv_isr()
348 i2c_slave_event(idev->slave, I2C_SLAVE_READ_REQUESTED, &val); in axxia_i2c_slv_isr()
349 writel(val, idev->base + SLV_DATA); in axxia_i2c_slv_isr()
352 i2c_slave_event(idev->slave, I2C_SLAVE_READ_PROCESSED, &val); in axxia_i2c_slv_isr()
353 writel(val, idev->base + SLV_DATA); in axxia_i2c_slv_isr()
356 i2c_slave_event(idev->slave, I2C_SLAVE_STOP, &val); in axxia_i2c_slv_isr()
358 writel(INT_SLV, idev->base + INTERRUPT_STATUS); in axxia_i2c_slv_isr()
364 struct axxia_i2c_dev *idev = _dev; in axxia_i2c_isr() local
368 status = readl(idev->base + INTERRUPT_STATUS); in axxia_i2c_isr()
371 ret = axxia_i2c_slv_isr(idev); in axxia_i2c_isr()
376 status = readl(idev->base + MST_INT_STATUS); in axxia_i2c_isr()
378 if (!idev->msg) { in axxia_i2c_isr()
379 dev_warn(idev->dev, "unexpected interrupt\n"); in axxia_i2c_isr()
384 if (i2c_m_rd(idev->msg_r) && (status & MST_STATUS_RFL)) in axxia_i2c_isr()
385 axxia_i2c_empty_rx_fifo(idev); in axxia_i2c_isr()
388 if (!i2c_m_rd(idev->msg) && (status & MST_STATUS_TFL)) { in axxia_i2c_isr()
389 if (axxia_i2c_fill_tx_fifo(idev) == 0) in axxia_i2c_isr()
390 i2c_int_disable(idev, MST_STATUS_TFL); in axxia_i2c_isr()
395 i2c_int_disable(idev, ~0); in axxia_i2c_isr()
397 idev->msg_err = -EAGAIN; in axxia_i2c_isr()
399 idev->msg_err = -ENXIO; in axxia_i2c_isr()
401 idev->msg_err = -EIO; in axxia_i2c_isr()
402 dev_dbg(idev->dev, "error %#x, addr=%#x rx=%u/%u tx=%u/%u\n", in axxia_i2c_isr()
404 idev->msg->addr, in axxia_i2c_isr()
405 readl(idev->base + MST_RX_BYTES_XFRD), in axxia_i2c_isr()
406 readl(idev->base + MST_RX_XFER), in axxia_i2c_isr()
407 readl(idev->base + MST_TX_BYTES_XFRD), in axxia_i2c_isr()
408 readl(idev->base + MST_TX_XFER)); in axxia_i2c_isr()
409 complete(&idev->msg_complete); in axxia_i2c_isr()
412 i2c_int_disable(idev, ~MST_STATUS_TSS); in axxia_i2c_isr()
413 complete(&idev->msg_complete); in axxia_i2c_isr()
416 int mask = idev->last ? ~0 : ~MST_STATUS_TSS; in axxia_i2c_isr()
418 i2c_int_disable(idev, mask); in axxia_i2c_isr()
419 if (i2c_m_rd(idev->msg_r) && idev->msg_xfrd_r < idev->msg_r->len) in axxia_i2c_isr()
420 axxia_i2c_empty_rx_fifo(idev); in axxia_i2c_isr()
421 complete(&idev->msg_complete); in axxia_i2c_isr()
424 idev->msg_err = -ETIMEDOUT; in axxia_i2c_isr()
425 i2c_int_disable(idev, ~MST_STATUS_TSS); in axxia_i2c_isr()
426 complete(&idev->msg_complete); in axxia_i2c_isr()
431 writel(INT_MST, idev->base + INTERRUPT_STATUS); in axxia_i2c_isr()
436 static void axxia_i2c_set_addr(struct axxia_i2c_dev *idev, struct i2c_msg *msg) in axxia_i2c_set_addr() argument
458 writel(addr_1, idev->base + MST_ADDR_1); in axxia_i2c_set_addr()
459 writel(addr_2, idev->base + MST_ADDR_2); in axxia_i2c_set_addr()
466 static int axxia_i2c_handle_seq_nak(struct axxia_i2c_dev *idev) in axxia_i2c_handle_seq_nak() argument
471 if ((readl(idev->base + MST_COMMAND) & CMD_BUSY) == 0) in axxia_i2c_handle_seq_nak()
479 static int axxia_i2c_xfer_seq(struct axxia_i2c_dev *idev, struct i2c_msg msgs[]) in axxia_i2c_xfer_seq() argument
485 axxia_i2c_set_addr(idev, &msgs[0]); in axxia_i2c_xfer_seq()
487 writel(msgs[0].len, idev->base + MST_TX_XFER); in axxia_i2c_xfer_seq()
488 writel(rlen, idev->base + MST_RX_XFER); in axxia_i2c_xfer_seq()
490 idev->msg = &msgs[0]; in axxia_i2c_xfer_seq()
491 idev->msg_r = &msgs[1]; in axxia_i2c_xfer_seq()
492 idev->msg_xfrd = 0; in axxia_i2c_xfer_seq()
493 idev->msg_xfrd_r = 0; in axxia_i2c_xfer_seq()
494 idev->last = true; in axxia_i2c_xfer_seq()
495 axxia_i2c_fill_tx_fifo(idev); in axxia_i2c_xfer_seq()
497 writel(CMD_SEQUENCE, idev->base + MST_COMMAND); in axxia_i2c_xfer_seq()
499 reinit_completion(&idev->msg_complete); in axxia_i2c_xfer_seq()
500 i2c_int_enable(idev, int_mask); in axxia_i2c_xfer_seq()
502 time_left = wait_for_completion_timeout(&idev->msg_complete, in axxia_i2c_xfer_seq()
505 if (idev->msg_err == -ENXIO) { in axxia_i2c_xfer_seq()
506 if (axxia_i2c_handle_seq_nak(idev)) in axxia_i2c_xfer_seq()
507 axxia_i2c_init(idev); in axxia_i2c_xfer_seq()
508 } else if (readl(idev->base + MST_COMMAND) & CMD_BUSY) { in axxia_i2c_xfer_seq()
509 dev_warn(idev->dev, "busy after xfer\n"); in axxia_i2c_xfer_seq()
513 idev->msg_err = -ETIMEDOUT; in axxia_i2c_xfer_seq()
514 i2c_recover_bus(&idev->adapter); in axxia_i2c_xfer_seq()
515 axxia_i2c_init(idev); in axxia_i2c_xfer_seq()
518 if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO) in axxia_i2c_xfer_seq()
519 axxia_i2c_init(idev); in axxia_i2c_xfer_seq()
521 return idev->msg_err; in axxia_i2c_xfer_seq()
524 static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg, in axxia_i2c_xfer_msg() argument
532 idev->msg = msg; in axxia_i2c_xfer_msg()
533 idev->msg_r = msg; in axxia_i2c_xfer_msg()
534 idev->msg_xfrd = 0; in axxia_i2c_xfer_msg()
535 idev->msg_xfrd_r = 0; in axxia_i2c_xfer_msg()
536 idev->last = last; in axxia_i2c_xfer_msg()
537 reinit_completion(&idev->msg_complete); in axxia_i2c_xfer_msg()
539 axxia_i2c_set_addr(idev, msg); in axxia_i2c_xfer_msg()
551 writel(rx_xfer, idev->base + MST_RX_XFER); in axxia_i2c_xfer_msg()
552 writel(tx_xfer, idev->base + MST_TX_XFER); in axxia_i2c_xfer_msg()
556 else if (axxia_i2c_fill_tx_fifo(idev) != 0) in axxia_i2c_xfer_msg()
559 wt_value = WT_VALUE(readl(idev->base + WAIT_TIMER_CONTROL)); in axxia_i2c_xfer_msg()
561 writel(wt_value, idev->base + WAIT_TIMER_CONTROL); in axxia_i2c_xfer_msg()
563 if (idev->msg_err) in axxia_i2c_xfer_msg()
567 writel(CMD_MANUAL, idev->base + MST_COMMAND); in axxia_i2c_xfer_msg()
570 writel(CMD_AUTO, idev->base + MST_COMMAND); in axxia_i2c_xfer_msg()
574 writel(WT_EN | wt_value, idev->base + WAIT_TIMER_CONTROL); in axxia_i2c_xfer_msg()
576 i2c_int_enable(idev, int_mask); in axxia_i2c_xfer_msg()
578 time_left = wait_for_completion_timeout(&idev->msg_complete, in axxia_i2c_xfer_msg()
581 i2c_int_disable(idev, int_mask); in axxia_i2c_xfer_msg()
583 if (readl(idev->base + MST_COMMAND) & CMD_BUSY) in axxia_i2c_xfer_msg()
584 dev_warn(idev->dev, "busy after xfer\n"); in axxia_i2c_xfer_msg()
587 idev->msg_err = -ETIMEDOUT; in axxia_i2c_xfer_msg()
588 i2c_recover_bus(&idev->adapter); in axxia_i2c_xfer_msg()
589 axxia_i2c_init(idev); in axxia_i2c_xfer_msg()
593 if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO && in axxia_i2c_xfer_msg()
594 idev->msg_err != -ETIMEDOUT) in axxia_i2c_xfer_msg()
595 axxia_i2c_init(idev); in axxia_i2c_xfer_msg()
597 return idev->msg_err; in axxia_i2c_xfer_msg()
615 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap); in axxia_i2c_xfer() local
619 idev->msg_err = 0; in axxia_i2c_xfer()
622 ret = axxia_i2c_xfer_seq(idev, msgs); in axxia_i2c_xfer()
626 i2c_int_enable(idev, MST_STATUS_TSS); in axxia_i2c_xfer()
629 ret = axxia_i2c_xfer_msg(idev, &msgs[i], i == (num - 1)); in axxia_i2c_xfer()
636 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap); in axxia_i2c_get_scl() local
638 return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SCLS); in axxia_i2c_get_scl()
643 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap); in axxia_i2c_set_scl() local
647 tmp = readl(idev->base + I2C_BUS_MONITOR) & BM_SDAC; in axxia_i2c_set_scl()
650 writel(tmp, idev->base + I2C_BUS_MONITOR); in axxia_i2c_set_scl()
655 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap); in axxia_i2c_get_sda() local
657 return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SDAS); in axxia_i2c_get_sda()
676 struct axxia_i2c_dev *idev = i2c_get_adapdata(slave->adapter); in axxia_i2c_reg_slave() local
680 if (idev->slave) in axxia_i2c_reg_slave()
683 idev->slave = slave; in axxia_i2c_reg_slave()
686 writel(GLOBAL_MST_EN | GLOBAL_SLV_EN, idev->base + GLOBAL_CONTROL); in axxia_i2c_reg_slave()
687 writel(INT_MST | INT_SLV, idev->base + INTERRUPT_ENABLE); in axxia_i2c_reg_slave()
694 writel(SLV_RX_ACSA1, idev->base + SLV_RX_CTL); in axxia_i2c_reg_slave()
695 writel(dec_ctl, idev->base + SLV_ADDR_DEC_CTL); in axxia_i2c_reg_slave()
696 writel(slave->addr, idev->base + SLV_ADDR_1); in axxia_i2c_reg_slave()
701 writel(slv_int_mask, idev->base + SLV_INT_ENABLE); in axxia_i2c_reg_slave()
708 struct axxia_i2c_dev *idev = i2c_get_adapdata(slave->adapter); in axxia_i2c_unreg_slave() local
711 writel(GLOBAL_MST_EN, idev->base + GLOBAL_CONTROL); in axxia_i2c_unreg_slave()
712 writel(INT_MST, idev->base + INTERRUPT_ENABLE); in axxia_i2c_unreg_slave()
714 synchronize_irq(idev->irq); in axxia_i2c_unreg_slave()
716 idev->slave = NULL; in axxia_i2c_unreg_slave()
736 struct axxia_i2c_dev *idev = NULL; in axxia_i2c_probe() local
740 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); in axxia_i2c_probe()
741 if (!idev) in axxia_i2c_probe()
748 idev->irq = platform_get_irq(pdev, 0); in axxia_i2c_probe()
749 if (idev->irq < 0) in axxia_i2c_probe()
750 return idev->irq; in axxia_i2c_probe()
752 idev->i2c_clk = devm_clk_get(&pdev->dev, "i2c"); in axxia_i2c_probe()
753 if (IS_ERR(idev->i2c_clk)) { in axxia_i2c_probe()
755 return PTR_ERR(idev->i2c_clk); in axxia_i2c_probe()
758 idev->base = base; in axxia_i2c_probe()
759 idev->dev = &pdev->dev; in axxia_i2c_probe()
760 init_completion(&idev->msg_complete); in axxia_i2c_probe()
762 of_property_read_u32(np, "clock-frequency", &idev->bus_clk_rate); in axxia_i2c_probe()
763 if (idev->bus_clk_rate == 0) in axxia_i2c_probe()
764 idev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ; /* default clock rate */ in axxia_i2c_probe()
766 ret = clk_prepare_enable(idev->i2c_clk); in axxia_i2c_probe()
772 ret = axxia_i2c_init(idev); in axxia_i2c_probe()
778 ret = devm_request_irq(&pdev->dev, idev->irq, axxia_i2c_isr, 0, in axxia_i2c_probe()
779 pdev->name, idev); in axxia_i2c_probe()
781 dev_err(&pdev->dev, "failed to claim IRQ%d\n", idev->irq); in axxia_i2c_probe()
785 i2c_set_adapdata(&idev->adapter, idev); in axxia_i2c_probe()
786 strlcpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name)); in axxia_i2c_probe()
787 idev->adapter.owner = THIS_MODULE; in axxia_i2c_probe()
788 idev->adapter.algo = &axxia_i2c_algo; in axxia_i2c_probe()
789 idev->adapter.bus_recovery_info = &axxia_i2c_recovery_info; in axxia_i2c_probe()
790 idev->adapter.quirks = &axxia_i2c_quirks; in axxia_i2c_probe()
791 idev->adapter.dev.parent = &pdev->dev; in axxia_i2c_probe()
792 idev->adapter.dev.of_node = pdev->dev.of_node; in axxia_i2c_probe()
794 platform_set_drvdata(pdev, idev); in axxia_i2c_probe()
796 ret = i2c_add_adapter(&idev->adapter); in axxia_i2c_probe()
803 clk_disable_unprepare(idev->i2c_clk); in axxia_i2c_probe()
809 struct axxia_i2c_dev *idev = platform_get_drvdata(pdev); in axxia_i2c_remove() local
811 clk_disable_unprepare(idev->i2c_clk); in axxia_i2c_remove()
812 i2c_del_adapter(&idev->adapter); in axxia_i2c_remove()