Lines Matching refs:i2c_dev
236 static void st_i2c_flush_rx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_flush_rx_fifo() argument
245 if (readl_relaxed(i2c_dev->base + SSC_STA) & SSC_STA_RIR) in st_i2c_flush_rx_fifo()
248 count = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT) & in st_i2c_flush_rx_fifo()
252 readl_relaxed(i2c_dev->base + SSC_RBUF); in st_i2c_flush_rx_fifo()
255 static void st_i2c_soft_reset(struct st_i2c_dev *i2c_dev) in st_i2c_soft_reset() argument
261 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_soft_reset()
263 st_i2c_set_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR); in st_i2c_soft_reset()
264 st_i2c_clr_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR); in st_i2c_soft_reset()
271 static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev) in st_i2c_hw_config() argument
275 struct st_i2c_timings *t = &i2c_timings[i2c_dev->mode]; in st_i2c_hw_config()
277 st_i2c_soft_reset(i2c_dev); in st_i2c_hw_config()
281 writel_relaxed(val, i2c_dev->base + SSC_CLR); in st_i2c_hw_config()
285 writel_relaxed(val, i2c_dev->base + SSC_CTL); in st_i2c_hw_config()
287 rate = clk_get_rate(i2c_dev->clk); in st_i2c_hw_config()
292 writel_relaxed(val, i2c_dev->base + SSC_BRG); in st_i2c_hw_config()
295 writel_relaxed(1, i2c_dev->base + SSC_PRE_SCALER_BRG); in st_i2c_hw_config()
298 writel_relaxed(SSC_I2C_I2CM, i2c_dev->base + SSC_I2C); in st_i2c_hw_config()
302 writel_relaxed(val, i2c_dev->base + SSC_REP_START_HOLD); in st_i2c_hw_config()
306 writel_relaxed(val, i2c_dev->base + SSC_REP_START_SETUP); in st_i2c_hw_config()
310 writel_relaxed(val, i2c_dev->base + SSC_START_HOLD); in st_i2c_hw_config()
314 writel_relaxed(val, i2c_dev->base + SSC_DATA_SETUP); in st_i2c_hw_config()
318 writel_relaxed(val, i2c_dev->base + SSC_STOP_SETUP); in st_i2c_hw_config()
322 writel_relaxed(val, i2c_dev->base + SSC_BUS_FREE); in st_i2c_hw_config()
326 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER); in st_i2c_hw_config()
327 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER_DATAOUT); in st_i2c_hw_config()
330 val = i2c_dev->scl_min_width_us * rate / 100000000; in st_i2c_hw_config()
331 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH); in st_i2c_hw_config()
334 val = i2c_dev->sda_min_width_us * rate / 100000000; in st_i2c_hw_config()
335 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT); in st_i2c_hw_config()
340 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in st_i2c_recover_bus() local
343 dev_dbg(i2c_dev->dev, "Trying to recover bus\n"); in st_i2c_recover_bus()
353 writel_relaxed(0, i2c_dev->base + SSC_IEN); in st_i2c_recover_bus()
355 st_i2c_hw_config(i2c_dev); in st_i2c_recover_bus()
358 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); in st_i2c_recover_bus()
360 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM); in st_i2c_recover_bus()
363 writel_relaxed(0, i2c_dev->base + SSC_TBUF); in st_i2c_recover_bus()
365 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM); in st_i2c_recover_bus()
370 static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev) in st_i2c_wait_free_bus() argument
376 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_wait_free_bus()
383 dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta); in st_i2c_wait_free_bus()
385 ret = i2c_recover_bus(&i2c_dev->adap); in st_i2c_wait_free_bus()
387 dev_err(i2c_dev->dev, "Failed to recover the bus (%d)\n", ret); in st_i2c_wait_free_bus()
399 static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte) in st_i2c_write_tx_fifo() argument
403 writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF); in st_i2c_write_tx_fifo()
413 static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_wr_fill_tx_fifo() argument
415 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_wr_fill_tx_fifo()
419 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_wr_fill_tx_fifo()
423 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_wr_fill_tx_fifo()
428 st_i2c_write_tx_fifo(i2c_dev, *c->buf); in st_i2c_wr_fill_tx_fifo()
439 static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, u32 max) in st_i2c_rd_fill_tx_fifo() argument
441 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_rd_fill_tx_fifo()
445 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_rd_fill_tx_fifo()
449 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_rd_fill_tx_fifo()
454 st_i2c_write_tx_fifo(i2c_dev, 0xff); in st_i2c_rd_fill_tx_fifo()
457 static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_read_rx_fifo() argument
459 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_read_rx_fifo()
463 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_read_rx_fifo()
467 i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT); in st_i2c_read_rx_fifo()
472 rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1; in st_i2c_read_rx_fifo()
477 dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i); in st_i2c_read_rx_fifo()
478 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_read_rx_fifo()
486 static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev) in st_i2c_terminate_xfer() argument
488 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_terminate_xfer()
490 st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN); in st_i2c_terminate_xfer()
491 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_terminate_xfer()
494 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN); in st_i2c_terminate_xfer()
495 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_terminate_xfer()
497 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN); in st_i2c_terminate_xfer()
498 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG); in st_i2c_terminate_xfer()
506 static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev) in st_i2c_handle_write() argument
508 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_write()
510 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_handle_write()
514 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_write()
516 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_handle_write()
523 static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev) in st_i2c_handle_read() argument
525 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_read()
530 readl_relaxed(i2c_dev->base + SSC_RBUF); in st_i2c_handle_read()
531 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB); in st_i2c_handle_read()
533 st_i2c_read_rx_fifo(i2c_dev); in st_i2c_handle_read()
538 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_read()
541 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG); in st_i2c_handle_read()
545 writel_relaxed(ien, i2c_dev->base + SSC_IEN); in st_i2c_handle_read()
547 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count); in st_i2c_handle_read()
549 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1); in st_i2c_handle_read()
560 struct st_i2c_dev *i2c_dev = data; in st_i2c_isr_thread() local
561 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_isr_thread()
565 ien = readl_relaxed(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
566 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_isr_thread()
571 dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n", in st_i2c_isr_thread()
579 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
581 st_i2c_handle_write(i2c_dev); in st_i2c_isr_thread()
586 writel_relaxed(0, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
587 complete(&i2c_dev->complete); in st_i2c_isr_thread()
591 writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
595 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
600 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
602 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
607 writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
610 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
612 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
617 dev_err(i2c_dev->dev, in st_i2c_isr_thread()
626 readl(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
638 static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg, in st_i2c_xfer_msg() argument
641 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_xfer_msg()
653 reinit_completion(&i2c_dev->complete); in st_i2c_xfer_msg()
656 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); in st_i2c_xfer_msg()
661 st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
664 st_i2c_write_tx_fifo(i2c_dev, c->addr); in st_i2c_xfer_msg()
668 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_xfer_msg()
671 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_xfer_msg()
674 ret = st_i2c_wait_free_bus(i2c_dev); in st_i2c_xfer_msg()
678 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_xfer_msg()
681 time_left = wait_for_completion_timeout(&i2c_dev->complete, in st_i2c_xfer_msg()
682 i2c_dev->adap.timeout); in st_i2c_xfer_msg()
689 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
692 i2c_dev->base + SSC_CLR); in st_i2c_xfer_msg()
706 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in st_i2c_xfer() local
709 i2c_dev->busy = true; in st_i2c_xfer()
711 ret = clk_prepare_enable(i2c_dev->clk); in st_i2c_xfer()
713 dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n"); in st_i2c_xfer()
717 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_xfer()
719 st_i2c_hw_config(i2c_dev); in st_i2c_xfer()
722 ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1); in st_i2c_xfer()
724 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_xfer()
726 clk_disable_unprepare(i2c_dev->clk); in st_i2c_xfer()
728 i2c_dev->busy = false; in st_i2c_xfer()
735 struct st_i2c_dev *i2c_dev = dev_get_drvdata(dev); in st_i2c_suspend() local
737 if (i2c_dev->busy) in st_i2c_suspend()
771 struct st_i2c_dev *i2c_dev) in st_i2c_of_get_deglitch() argument
776 &i2c_dev->scl_min_width_us); in st_i2c_of_get_deglitch()
778 dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
783 &i2c_dev->sda_min_width_us); in st_i2c_of_get_deglitch()
785 dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
795 struct st_i2c_dev *i2c_dev; in st_i2c_probe() local
801 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); in st_i2c_probe()
802 if (!i2c_dev) in st_i2c_probe()
805 i2c_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); in st_i2c_probe()
806 if (IS_ERR(i2c_dev->base)) in st_i2c_probe()
807 return PTR_ERR(i2c_dev->base); in st_i2c_probe()
809 i2c_dev->irq = irq_of_parse_and_map(np, 0); in st_i2c_probe()
810 if (!i2c_dev->irq) { in st_i2c_probe()
815 i2c_dev->clk = of_clk_get_by_name(np, "ssc"); in st_i2c_probe()
816 if (IS_ERR(i2c_dev->clk)) { in st_i2c_probe()
818 return PTR_ERR(i2c_dev->clk); in st_i2c_probe()
821 i2c_dev->mode = I2C_MODE_STANDARD; in st_i2c_probe()
824 i2c_dev->mode = I2C_MODE_FAST; in st_i2c_probe()
826 i2c_dev->dev = &pdev->dev; in st_i2c_probe()
828 ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq, in st_i2c_probe()
830 IRQF_ONESHOT, pdev->name, i2c_dev); in st_i2c_probe()
832 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); in st_i2c_probe()
836 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_probe()
838 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_probe()
840 ret = st_i2c_of_get_deglitch(np, i2c_dev); in st_i2c_probe()
844 adap = &i2c_dev->adap; in st_i2c_probe()
845 i2c_set_adapdata(adap, i2c_dev); in st_i2c_probe()
855 init_completion(&i2c_dev->complete); in st_i2c_probe()
861 platform_set_drvdata(pdev, i2c_dev); in st_i2c_probe()
863 dev_info(i2c_dev->dev, "%s initialized\n", adap->name); in st_i2c_probe()
870 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev); in st_i2c_remove() local
872 i2c_del_adapter(&i2c_dev->adap); in st_i2c_remove()