Lines Matching refs:dev
169 static void bcm_kona_i2c_send_cmd_to_ctrl(struct bcm_kona_i2c_dev *dev, in bcm_kona_i2c_send_cmd_to_ctrl() argument
172 dev_dbg(dev->device, "%s, %d\n", __func__, cmd); in bcm_kona_i2c_send_cmd_to_ctrl()
178 dev->base + CS_OFFSET); in bcm_kona_i2c_send_cmd_to_ctrl()
185 dev->base + CS_OFFSET); in bcm_kona_i2c_send_cmd_to_ctrl()
192 dev->base + CS_OFFSET); in bcm_kona_i2c_send_cmd_to_ctrl()
198 dev->base + CS_OFFSET); in bcm_kona_i2c_send_cmd_to_ctrl()
202 dev_err(dev->device, "Unknown command %d\n", cmd); in bcm_kona_i2c_send_cmd_to_ctrl()
206 static void bcm_kona_i2c_enable_clock(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_enable_clock() argument
208 writel(readl(dev->base + CLKEN_OFFSET) | CLKEN_CLKEN_MASK, in bcm_kona_i2c_enable_clock()
209 dev->base + CLKEN_OFFSET); in bcm_kona_i2c_enable_clock()
212 static void bcm_kona_i2c_disable_clock(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_disable_clock() argument
214 writel(readl(dev->base + CLKEN_OFFSET) & ~CLKEN_CLKEN_MASK, in bcm_kona_i2c_disable_clock()
215 dev->base + CLKEN_OFFSET); in bcm_kona_i2c_disable_clock()
220 struct bcm_kona_i2c_dev *dev = devid; in bcm_kona_i2c_isr() local
221 uint32_t status = readl(dev->base + ISR_OFFSET); in bcm_kona_i2c_isr()
229 dev->base + TXFCR_OFFSET); in bcm_kona_i2c_isr()
231 writel(status & ~ISR_RESERVED_MASK, dev->base + ISR_OFFSET); in bcm_kona_i2c_isr()
232 complete(&dev->done); in bcm_kona_i2c_isr()
238 static int bcm_kona_i2c_wait_if_busy(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_wait_if_busy() argument
242 while (readl(dev->base + ISR_OFFSET) & ISR_CMDBUSY_MASK) in bcm_kona_i2c_wait_if_busy()
244 dev_err(dev->device, "CMDBUSY timeout\n"); in bcm_kona_i2c_wait_if_busy()
252 static int bcm_kona_send_i2c_cmd(struct bcm_kona_i2c_dev *dev, in bcm_kona_send_i2c_cmd() argument
259 rc = bcm_kona_i2c_wait_if_busy(dev); in bcm_kona_send_i2c_cmd()
264 writel(IER_I2C_INT_EN_MASK, dev->base + IER_OFFSET); in bcm_kona_send_i2c_cmd()
267 reinit_completion(&dev->done); in bcm_kona_send_i2c_cmd()
270 bcm_kona_i2c_send_cmd_to_ctrl(dev, cmd); in bcm_kona_send_i2c_cmd()
273 time_left = wait_for_completion_timeout(&dev->done, time_left); in bcm_kona_send_i2c_cmd()
276 writel(0, dev->base + IER_OFFSET); in bcm_kona_send_i2c_cmd()
279 dev_err(dev->device, "controller timed out\n"); in bcm_kona_send_i2c_cmd()
284 bcm_kona_i2c_send_cmd_to_ctrl(dev, BCM_CMD_NOACTION); in bcm_kona_send_i2c_cmd()
290 static int bcm_kona_i2c_read_fifo_single(struct bcm_kona_i2c_dev *dev, in bcm_kona_i2c_read_fifo_single() argument
297 reinit_completion(&dev->done); in bcm_kona_i2c_read_fifo_single()
300 writel(IER_READ_COMPLETE_INT_MASK, dev->base + IER_OFFSET); in bcm_kona_i2c_read_fifo_single()
305 dev->base + RXFCR_OFFSET); in bcm_kona_i2c_read_fifo_single()
308 time_left = wait_for_completion_timeout(&dev->done, time_left); in bcm_kona_i2c_read_fifo_single()
311 writel(0, dev->base + IER_OFFSET); in bcm_kona_i2c_read_fifo_single()
314 dev_err(dev->device, "RX FIFO time out\n"); in bcm_kona_i2c_read_fifo_single()
320 *buf = readl(dev->base + RXFIFORDOUT_OFFSET); in bcm_kona_i2c_read_fifo_single()
326 static int bcm_kona_i2c_read_fifo(struct bcm_kona_i2c_dev *dev, in bcm_kona_i2c_read_fifo() argument
342 rc = bcm_kona_i2c_read_fifo_single(dev, tmp_buf, bytes_to_read, in bcm_kona_i2c_read_fifo()
355 static int bcm_kona_i2c_write_byte(struct bcm_kona_i2c_dev *dev, uint8_t data, in bcm_kona_i2c_write_byte() argument
363 rc = bcm_kona_i2c_wait_if_busy(dev); in bcm_kona_i2c_write_byte()
368 writel(ISR_SES_DONE_MASK, dev->base + ISR_OFFSET); in bcm_kona_i2c_write_byte()
371 writel(IER_I2C_INT_EN_MASK, dev->base + IER_OFFSET); in bcm_kona_i2c_write_byte()
374 reinit_completion(&dev->done); in bcm_kona_i2c_write_byte()
377 writel(data, dev->base + DAT_OFFSET); in bcm_kona_i2c_write_byte()
380 time_left = wait_for_completion_timeout(&dev->done, time_left); in bcm_kona_i2c_write_byte()
383 writel(0, dev->base + IER_OFFSET); in bcm_kona_i2c_write_byte()
386 dev_dbg(dev->device, "controller timed out\n"); in bcm_kona_i2c_write_byte()
390 nak_received = readl(dev->base + CS_OFFSET) & CS_ACK_MASK ? 1 : 0; in bcm_kona_i2c_write_byte()
393 dev_dbg(dev->device, "unexpected NAK/ACK\n"); in bcm_kona_i2c_write_byte()
401 static int bcm_kona_i2c_write_fifo_single(struct bcm_kona_i2c_dev *dev, in bcm_kona_i2c_write_fifo_single() argument
409 reinit_completion(&dev->done); in bcm_kona_i2c_write_fifo_single()
413 dev->base + IER_OFFSET); in bcm_kona_i2c_write_fifo_single()
416 disable_irq(dev->irq); in bcm_kona_i2c_write_fifo_single()
420 writel(buf[k], (dev->base + DAT_OFFSET)); in bcm_kona_i2c_write_fifo_single()
423 enable_irq(dev->irq); in bcm_kona_i2c_write_fifo_single()
427 time_left = wait_for_completion_timeout(&dev->done, time_left); in bcm_kona_i2c_write_fifo_single()
428 fifo_status = readl(dev->base + FIFO_STATUS_OFFSET); in bcm_kona_i2c_write_fifo_single()
432 writel(0, dev->base + IER_OFFSET); in bcm_kona_i2c_write_fifo_single()
435 if (readl(dev->base + CS_OFFSET) & CS_ACK_MASK) { in bcm_kona_i2c_write_fifo_single()
436 dev_err(dev->device, "unexpected NAK\n"); in bcm_kona_i2c_write_fifo_single()
442 dev_err(dev->device, "completion timed out\n"); in bcm_kona_i2c_write_fifo_single()
451 static int bcm_kona_i2c_write_fifo(struct bcm_kona_i2c_dev *dev, in bcm_kona_i2c_write_fifo() argument
464 rc = bcm_kona_i2c_write_fifo_single(dev, tmp_buf, in bcm_kona_i2c_write_fifo()
477 static int bcm_kona_i2c_do_addr(struct bcm_kona_i2c_dev *dev, in bcm_kona_i2c_do_addr() argument
485 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0) in bcm_kona_i2c_do_addr()
490 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0) in bcm_kona_i2c_do_addr()
495 if (bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART) < 0) in bcm_kona_i2c_do_addr()
500 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0) in bcm_kona_i2c_do_addr()
506 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0) in bcm_kona_i2c_do_addr()
513 static void bcm_kona_i2c_enable_autosense(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_enable_autosense() argument
515 writel(readl(dev->base + CLKEN_OFFSET) & ~CLKEN_AUTOSENSE_OFF_MASK, in bcm_kona_i2c_enable_autosense()
516 dev->base + CLKEN_OFFSET); in bcm_kona_i2c_enable_autosense()
519 static void bcm_kona_i2c_config_timing(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_config_timing() argument
521 writel(readl(dev->base + HSTIM_OFFSET) & ~HSTIM_HS_MODE_MASK, in bcm_kona_i2c_config_timing()
522 dev->base + HSTIM_OFFSET); in bcm_kona_i2c_config_timing()
524 writel((dev->std_cfg->prescale << TIM_PRESCALE_SHIFT) | in bcm_kona_i2c_config_timing()
525 (dev->std_cfg->time_p << TIM_P_SHIFT) | in bcm_kona_i2c_config_timing()
526 (dev->std_cfg->no_div << TIM_NO_DIV_SHIFT) | in bcm_kona_i2c_config_timing()
527 (dev->std_cfg->time_div << TIM_DIV_SHIFT), in bcm_kona_i2c_config_timing()
528 dev->base + TIM_OFFSET); in bcm_kona_i2c_config_timing()
530 writel((dev->std_cfg->time_m << CLKEN_M_SHIFT) | in bcm_kona_i2c_config_timing()
531 (dev->std_cfg->time_n << CLKEN_N_SHIFT) | in bcm_kona_i2c_config_timing()
533 dev->base + CLKEN_OFFSET); in bcm_kona_i2c_config_timing()
536 static void bcm_kona_i2c_config_timing_hs(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_config_timing_hs() argument
538 writel((dev->hs_cfg->prescale << TIM_PRESCALE_SHIFT) | in bcm_kona_i2c_config_timing_hs()
539 (dev->hs_cfg->time_p << TIM_P_SHIFT) | in bcm_kona_i2c_config_timing_hs()
540 (dev->hs_cfg->no_div << TIM_NO_DIV_SHIFT) | in bcm_kona_i2c_config_timing_hs()
541 (dev->hs_cfg->time_div << TIM_DIV_SHIFT), in bcm_kona_i2c_config_timing_hs()
542 dev->base + TIM_OFFSET); in bcm_kona_i2c_config_timing_hs()
544 writel((dev->hs_cfg->hs_hold << HSTIM_HS_HOLD_SHIFT) | in bcm_kona_i2c_config_timing_hs()
545 (dev->hs_cfg->hs_high_phase << HSTIM_HS_HIGH_PHASE_SHIFT) | in bcm_kona_i2c_config_timing_hs()
546 (dev->hs_cfg->hs_setup << HSTIM_HS_SETUP_SHIFT), in bcm_kona_i2c_config_timing_hs()
547 dev->base + HSTIM_OFFSET); in bcm_kona_i2c_config_timing_hs()
549 writel(readl(dev->base + HSTIM_OFFSET) | HSTIM_HS_MODE_MASK, in bcm_kona_i2c_config_timing_hs()
550 dev->base + HSTIM_OFFSET); in bcm_kona_i2c_config_timing_hs()
553 static int bcm_kona_i2c_switch_to_hs(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_switch_to_hs() argument
558 rc = bcm_kona_i2c_write_byte(dev, MASTERCODE, 1); in bcm_kona_i2c_switch_to_hs()
565 rc = clk_set_rate(dev->external_clk, HS_EXT_CLK_FREQ); in bcm_kona_i2c_switch_to_hs()
567 dev_err(dev->device, "%s: clk_set_rate returned %d\n", in bcm_kona_i2c_switch_to_hs()
573 bcm_kona_i2c_config_timing_hs(dev); in bcm_kona_i2c_switch_to_hs()
576 rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART); in bcm_kona_i2c_switch_to_hs()
578 dev_err(dev->device, "High speed restart command failed\n"); in bcm_kona_i2c_switch_to_hs()
583 static int bcm_kona_i2c_switch_to_std(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_switch_to_std() argument
588 bcm_kona_i2c_config_timing(dev); in bcm_kona_i2c_switch_to_std()
591 rc = clk_set_rate(dev->external_clk, STD_EXT_CLK_FREQ); in bcm_kona_i2c_switch_to_std()
593 dev_err(dev->device, "%s: clk_set_rate returned %d\n", in bcm_kona_i2c_switch_to_std()
604 struct bcm_kona_i2c_dev *dev = i2c_get_adapdata(adapter); in bcm_kona_i2c_xfer() local
609 rc = clk_prepare_enable(dev->external_clk); in bcm_kona_i2c_xfer()
611 dev_err(dev->device, "%s: peri clock enable failed. err %d\n", in bcm_kona_i2c_xfer()
617 writel(0, dev->base + PADCTL_OFFSET); in bcm_kona_i2c_xfer()
620 bcm_kona_i2c_enable_clock(dev); in bcm_kona_i2c_xfer()
623 rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_START); in bcm_kona_i2c_xfer()
625 dev_err(dev->device, "Start command failed rc = %d\n", rc); in bcm_kona_i2c_xfer()
630 if (dev->hs_cfg) { in bcm_kona_i2c_xfer()
631 rc = bcm_kona_i2c_switch_to_hs(dev); in bcm_kona_i2c_xfer()
642 rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART); in bcm_kona_i2c_xfer()
644 dev_err(dev->device, in bcm_kona_i2c_xfer()
652 rc = bcm_kona_i2c_do_addr(dev, pmsg); in bcm_kona_i2c_xfer()
654 dev_err(dev->device, in bcm_kona_i2c_xfer()
663 rc = bcm_kona_i2c_read_fifo(dev, pmsg); in bcm_kona_i2c_xfer()
665 dev_err(dev->device, "read failure\n"); in bcm_kona_i2c_xfer()
669 rc = bcm_kona_i2c_write_fifo(dev, pmsg); in bcm_kona_i2c_xfer()
671 dev_err(dev->device, "write failure"); in bcm_kona_i2c_xfer()
681 bcm_kona_send_i2c_cmd(dev, BCM_CMD_STOP); in bcm_kona_i2c_xfer()
684 if (dev->hs_cfg) { in bcm_kona_i2c_xfer()
685 int hs_rc = bcm_kona_i2c_switch_to_std(dev); in bcm_kona_i2c_xfer()
693 writel(PADCTL_PAD_OUT_EN_MASK, dev->base + PADCTL_OFFSET); in bcm_kona_i2c_xfer()
696 bcm_kona_i2c_disable_clock(dev); in bcm_kona_i2c_xfer()
698 clk_disable_unprepare(dev->external_clk); in bcm_kona_i2c_xfer()
714 static int bcm_kona_i2c_assign_bus_speed(struct bcm_kona_i2c_dev *dev) in bcm_kona_i2c_assign_bus_speed() argument
717 int ret = of_property_read_u32(dev->device->of_node, "clock-frequency", in bcm_kona_i2c_assign_bus_speed()
720 dev_err(dev->device, "missing clock-frequency property\n"); in bcm_kona_i2c_assign_bus_speed()
726 dev->std_cfg = &std_cfg_table[BCM_SPD_100K]; in bcm_kona_i2c_assign_bus_speed()
729 dev->std_cfg = &std_cfg_table[BCM_SPD_400K]; in bcm_kona_i2c_assign_bus_speed()
732 dev->std_cfg = &std_cfg_table[BCM_SPD_1MHZ]; in bcm_kona_i2c_assign_bus_speed()
736 dev->std_cfg = &std_cfg_table[BCM_SPD_100K]; in bcm_kona_i2c_assign_bus_speed()
737 dev->hs_cfg = &hs_cfg_table[BCM_SPD_3P4MHZ]; in bcm_kona_i2c_assign_bus_speed()
751 struct bcm_kona_i2c_dev *dev; in bcm_kona_i2c_probe() local
756 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); in bcm_kona_i2c_probe()
757 if (!dev) in bcm_kona_i2c_probe()
760 platform_set_drvdata(pdev, dev); in bcm_kona_i2c_probe()
761 dev->device = &pdev->dev; in bcm_kona_i2c_probe()
762 init_completion(&dev->done); in bcm_kona_i2c_probe()
766 dev->base = devm_ioremap_resource(dev->device, iomem); in bcm_kona_i2c_probe()
767 if (IS_ERR(dev->base)) in bcm_kona_i2c_probe()
771 dev->external_clk = devm_clk_get(dev->device, NULL); in bcm_kona_i2c_probe()
772 if (IS_ERR(dev->external_clk)) { in bcm_kona_i2c_probe()
773 dev_err(dev->device, "couldn't get clock\n"); in bcm_kona_i2c_probe()
777 rc = clk_set_rate(dev->external_clk, STD_EXT_CLK_FREQ); in bcm_kona_i2c_probe()
779 dev_err(dev->device, "%s: clk_set_rate returned %d\n", in bcm_kona_i2c_probe()
784 rc = clk_prepare_enable(dev->external_clk); in bcm_kona_i2c_probe()
786 dev_err(dev->device, "couldn't enable clock\n"); in bcm_kona_i2c_probe()
791 rc = bcm_kona_i2c_assign_bus_speed(dev); in bcm_kona_i2c_probe()
796 bcm_kona_i2c_enable_clock(dev); in bcm_kona_i2c_probe()
799 bcm_kona_i2c_config_timing(dev); in bcm_kona_i2c_probe()
802 writel(0, dev->base + TOUT_OFFSET); in bcm_kona_i2c_probe()
805 bcm_kona_i2c_enable_autosense(dev); in bcm_kona_i2c_probe()
809 dev->base + TXFCR_OFFSET); in bcm_kona_i2c_probe()
812 writel(0, dev->base + IER_OFFSET); in bcm_kona_i2c_probe()
821 dev->base + ISR_OFFSET); in bcm_kona_i2c_probe()
824 dev->irq = platform_get_irq(pdev, 0); in bcm_kona_i2c_probe()
825 if (dev->irq < 0) { in bcm_kona_i2c_probe()
826 dev_err(dev->device, "no irq resource\n"); in bcm_kona_i2c_probe()
832 rc = devm_request_irq(&pdev->dev, dev->irq, bcm_kona_i2c_isr, in bcm_kona_i2c_probe()
833 IRQF_SHARED, pdev->name, dev); in bcm_kona_i2c_probe()
835 dev_err(dev->device, "failed to request irq %i\n", dev->irq); in bcm_kona_i2c_probe()
840 bcm_kona_i2c_send_cmd_to_ctrl(dev, BCM_CMD_NOACTION); in bcm_kona_i2c_probe()
843 writel(PADCTL_PAD_OUT_EN_MASK, dev->base + PADCTL_OFFSET); in bcm_kona_i2c_probe()
846 bcm_kona_i2c_disable_clock(dev); in bcm_kona_i2c_probe()
849 clk_disable_unprepare(dev->external_clk); in bcm_kona_i2c_probe()
852 adap = &dev->adapter; in bcm_kona_i2c_probe()
853 i2c_set_adapdata(adap, dev); in bcm_kona_i2c_probe()
857 adap->dev.parent = &pdev->dev; in bcm_kona_i2c_probe()
858 adap->dev.of_node = pdev->dev.of_node; in bcm_kona_i2c_probe()
864 dev_info(dev->device, "device registered successfully\n"); in bcm_kona_i2c_probe()
869 bcm_kona_i2c_disable_clock(dev); in bcm_kona_i2c_probe()
870 clk_disable_unprepare(dev->external_clk); in bcm_kona_i2c_probe()
877 struct bcm_kona_i2c_dev *dev = platform_get_drvdata(pdev); in bcm_kona_i2c_remove() local
879 i2c_del_adapter(&dev->adapter); in bcm_kona_i2c_remove()