Lines Matching refs:i2c_dev

238 static void st_i2c_flush_rx_fifo(struct st_i2c_dev *i2c_dev)  in st_i2c_flush_rx_fifo()  argument
247 if (readl_relaxed(i2c_dev->base + SSC_STA) & SSC_STA_RIR) in st_i2c_flush_rx_fifo()
250 count = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT) & in st_i2c_flush_rx_fifo()
254 readl_relaxed(i2c_dev->base + SSC_RBUF); in st_i2c_flush_rx_fifo()
257 static void st_i2c_soft_reset(struct st_i2c_dev *i2c_dev) in st_i2c_soft_reset() argument
263 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_soft_reset()
265 st_i2c_set_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR); in st_i2c_soft_reset()
266 st_i2c_clr_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR); in st_i2c_soft_reset()
273 static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev) in st_i2c_hw_config() argument
277 struct st_i2c_timings *t = &i2c_timings[i2c_dev->mode]; in st_i2c_hw_config()
279 st_i2c_soft_reset(i2c_dev); in st_i2c_hw_config()
283 writel_relaxed(val, i2c_dev->base + SSC_CLR); in st_i2c_hw_config()
287 writel_relaxed(val, i2c_dev->base + SSC_CTL); in st_i2c_hw_config()
289 rate = clk_get_rate(i2c_dev->clk); in st_i2c_hw_config()
294 writel_relaxed(val, i2c_dev->base + SSC_BRG); in st_i2c_hw_config()
297 writel_relaxed(1, i2c_dev->base + SSC_PRE_SCALER_BRG); in st_i2c_hw_config()
300 writel_relaxed(SSC_I2C_I2CM, i2c_dev->base + SSC_I2C); in st_i2c_hw_config()
304 writel_relaxed(val, i2c_dev->base + SSC_REP_START_HOLD); in st_i2c_hw_config()
308 writel_relaxed(val, i2c_dev->base + SSC_REP_START_SETUP); in st_i2c_hw_config()
312 writel_relaxed(val, i2c_dev->base + SSC_START_HOLD); in st_i2c_hw_config()
316 writel_relaxed(val, i2c_dev->base + SSC_DATA_SETUP); in st_i2c_hw_config()
320 writel_relaxed(val, i2c_dev->base + SSC_STOP_SETUP); in st_i2c_hw_config()
324 writel_relaxed(val, i2c_dev->base + SSC_BUS_FREE); in st_i2c_hw_config()
328 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER); in st_i2c_hw_config()
329 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER_DATAOUT); in st_i2c_hw_config()
332 val = i2c_dev->scl_min_width_us * rate / 100000000; in st_i2c_hw_config()
333 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH); in st_i2c_hw_config()
336 val = i2c_dev->sda_min_width_us * rate / 100000000; in st_i2c_hw_config()
337 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT); in st_i2c_hw_config()
342 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in st_i2c_recover_bus() local
345 dev_dbg(i2c_dev->dev, "Trying to recover bus\n"); in st_i2c_recover_bus()
355 writel_relaxed(0, i2c_dev->base + SSC_IEN); in st_i2c_recover_bus()
357 st_i2c_hw_config(i2c_dev); in st_i2c_recover_bus()
360 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); in st_i2c_recover_bus()
362 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM); in st_i2c_recover_bus()
365 writel_relaxed(0, i2c_dev->base + SSC_TBUF); in st_i2c_recover_bus()
367 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM); in st_i2c_recover_bus()
372 static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev) in st_i2c_wait_free_bus() argument
378 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_wait_free_bus()
385 dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta); in st_i2c_wait_free_bus()
387 ret = i2c_recover_bus(&i2c_dev->adap); in st_i2c_wait_free_bus()
389 dev_err(i2c_dev->dev, "Failed to recover the bus (%d)\n", ret); in st_i2c_wait_free_bus()
401 static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte) in st_i2c_write_tx_fifo() argument
405 writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF); in st_i2c_write_tx_fifo()
415 static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_wr_fill_tx_fifo() argument
417 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_wr_fill_tx_fifo()
421 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_wr_fill_tx_fifo()
425 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_wr_fill_tx_fifo()
434 st_i2c_write_tx_fifo(i2c_dev, *c->buf); in st_i2c_wr_fill_tx_fifo()
444 static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max) in st_i2c_rd_fill_tx_fifo() argument
446 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_rd_fill_tx_fifo()
450 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_rd_fill_tx_fifo()
454 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_rd_fill_tx_fifo()
463 st_i2c_write_tx_fifo(i2c_dev, 0xff); in st_i2c_rd_fill_tx_fifo()
466 static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_read_rx_fifo() argument
468 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_read_rx_fifo()
472 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_read_rx_fifo()
476 i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT); in st_i2c_read_rx_fifo()
481 rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1; in st_i2c_read_rx_fifo()
486 dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i); in st_i2c_read_rx_fifo()
487 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_read_rx_fifo()
495 static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev) in st_i2c_terminate_xfer() argument
497 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_terminate_xfer()
499 st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN); in st_i2c_terminate_xfer()
500 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_terminate_xfer()
503 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN); in st_i2c_terminate_xfer()
504 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_terminate_xfer()
506 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN); in st_i2c_terminate_xfer()
507 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG); in st_i2c_terminate_xfer()
515 static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev) in st_i2c_handle_write() argument
517 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_write()
519 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_handle_write()
523 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_write()
525 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_handle_write()
532 static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev) in st_i2c_handle_read() argument
534 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_read()
539 readl_relaxed(i2c_dev->base + SSC_RBUF); in st_i2c_handle_read()
540 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB); in st_i2c_handle_read()
542 st_i2c_read_rx_fifo(i2c_dev); in st_i2c_handle_read()
547 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_read()
550 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG); in st_i2c_handle_read()
554 writel_relaxed(ien, i2c_dev->base + SSC_IEN); in st_i2c_handle_read()
556 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count); in st_i2c_handle_read()
558 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1); in st_i2c_handle_read()
569 struct st_i2c_dev *i2c_dev = data; in st_i2c_isr_thread() local
570 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_isr_thread()
574 ien = readl_relaxed(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
575 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_isr_thread()
580 dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n", in st_i2c_isr_thread()
588 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
590 st_i2c_handle_write(i2c_dev); in st_i2c_isr_thread()
595 writel_relaxed(0, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
596 complete(&i2c_dev->complete); in st_i2c_isr_thread()
600 writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
604 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
609 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
611 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
616 writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
619 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
621 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
626 dev_err(i2c_dev->dev, in st_i2c_isr_thread()
635 readl(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
647 static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg, in st_i2c_xfer_msg() argument
650 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_xfer_msg()
662 reinit_completion(&i2c_dev->complete); in st_i2c_xfer_msg()
665 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); in st_i2c_xfer_msg()
670 st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
673 st_i2c_write_tx_fifo(i2c_dev, c->addr); in st_i2c_xfer_msg()
677 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_xfer_msg()
680 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_xfer_msg()
683 ret = st_i2c_wait_free_bus(i2c_dev); in st_i2c_xfer_msg()
687 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_xfer_msg()
690 timeout = wait_for_completion_timeout(&i2c_dev->complete, in st_i2c_xfer_msg()
691 i2c_dev->adap.timeout); in st_i2c_xfer_msg()
695 dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n", in st_i2c_xfer_msg()
701 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
704 i2c_dev->base + SSC_CLR); in st_i2c_xfer_msg()
718 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in st_i2c_xfer() local
721 i2c_dev->busy = true; in st_i2c_xfer()
723 ret = clk_prepare_enable(i2c_dev->clk); in st_i2c_xfer()
725 dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n"); in st_i2c_xfer()
729 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_xfer()
731 st_i2c_hw_config(i2c_dev); in st_i2c_xfer()
734 ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1); in st_i2c_xfer()
736 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_xfer()
738 clk_disable_unprepare(i2c_dev->clk); in st_i2c_xfer()
740 i2c_dev->busy = false; in st_i2c_xfer()
748 struct st_i2c_dev *i2c_dev = dev_get_drvdata(dev); in st_i2c_suspend() local
750 if (i2c_dev->busy) in st_i2c_suspend()
788 struct st_i2c_dev *i2c_dev) in st_i2c_of_get_deglitch() argument
793 &i2c_dev->scl_min_width_us); in st_i2c_of_get_deglitch()
795 dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
800 &i2c_dev->sda_min_width_us); in st_i2c_of_get_deglitch()
802 dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
812 struct st_i2c_dev *i2c_dev; in st_i2c_probe() local
818 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); in st_i2c_probe()
819 if (!i2c_dev) in st_i2c_probe()
823 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res); in st_i2c_probe()
824 if (IS_ERR(i2c_dev->base)) in st_i2c_probe()
825 return PTR_ERR(i2c_dev->base); in st_i2c_probe()
827 i2c_dev->irq = irq_of_parse_and_map(np, 0); in st_i2c_probe()
828 if (!i2c_dev->irq) { in st_i2c_probe()
833 i2c_dev->clk = of_clk_get_by_name(np, "ssc"); in st_i2c_probe()
834 if (IS_ERR(i2c_dev->clk)) { in st_i2c_probe()
836 return PTR_ERR(i2c_dev->clk); in st_i2c_probe()
839 i2c_dev->mode = I2C_MODE_STANDARD; in st_i2c_probe()
842 i2c_dev->mode = I2C_MODE_FAST; in st_i2c_probe()
844 i2c_dev->dev = &pdev->dev; in st_i2c_probe()
846 ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq, in st_i2c_probe()
848 IRQF_ONESHOT, pdev->name, i2c_dev); in st_i2c_probe()
850 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); in st_i2c_probe()
854 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_probe()
856 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_probe()
858 ret = st_i2c_of_get_deglitch(np, i2c_dev); in st_i2c_probe()
862 adap = &i2c_dev->adap; in st_i2c_probe()
863 i2c_set_adapdata(adap, i2c_dev); in st_i2c_probe()
873 init_completion(&i2c_dev->complete); in st_i2c_probe()
879 platform_set_drvdata(pdev, i2c_dev); in st_i2c_probe()
881 dev_info(i2c_dev->dev, "%s initialized\n", adap->name); in st_i2c_probe()
888 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev); in st_i2c_remove() local
890 i2c_del_adapter(&i2c_dev->adap); in st_i2c_remove()