Lines Matching refs:i2c_dev

235 static void st_i2c_flush_rx_fifo(struct st_i2c_dev *i2c_dev)  in st_i2c_flush_rx_fifo()  argument
244 if (readl_relaxed(i2c_dev->base + SSC_STA) & SSC_STA_RIR) in st_i2c_flush_rx_fifo()
247 count = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT) & in st_i2c_flush_rx_fifo()
251 readl_relaxed(i2c_dev->base + SSC_RBUF); in st_i2c_flush_rx_fifo()
254 static void st_i2c_soft_reset(struct st_i2c_dev *i2c_dev) in st_i2c_soft_reset() argument
260 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_soft_reset()
262 st_i2c_set_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR); in st_i2c_soft_reset()
263 st_i2c_clr_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR); in st_i2c_soft_reset()
270 static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev) in st_i2c_hw_config() argument
274 struct st_i2c_timings *t = &i2c_timings[i2c_dev->mode]; in st_i2c_hw_config()
276 st_i2c_soft_reset(i2c_dev); in st_i2c_hw_config()
280 writel_relaxed(val, i2c_dev->base + SSC_CLR); in st_i2c_hw_config()
284 writel_relaxed(val, i2c_dev->base + SSC_CTL); in st_i2c_hw_config()
286 rate = clk_get_rate(i2c_dev->clk); in st_i2c_hw_config()
291 writel_relaxed(val, i2c_dev->base + SSC_BRG); in st_i2c_hw_config()
294 writel_relaxed(1, i2c_dev->base + SSC_PRE_SCALER_BRG); in st_i2c_hw_config()
297 writel_relaxed(SSC_I2C_I2CM, i2c_dev->base + SSC_I2C); in st_i2c_hw_config()
301 writel_relaxed(val, i2c_dev->base + SSC_REP_START_HOLD); in st_i2c_hw_config()
305 writel_relaxed(val, i2c_dev->base + SSC_REP_START_SETUP); in st_i2c_hw_config()
309 writel_relaxed(val, i2c_dev->base + SSC_START_HOLD); in st_i2c_hw_config()
313 writel_relaxed(val, i2c_dev->base + SSC_DATA_SETUP); in st_i2c_hw_config()
317 writel_relaxed(val, i2c_dev->base + SSC_STOP_SETUP); in st_i2c_hw_config()
321 writel_relaxed(val, i2c_dev->base + SSC_BUS_FREE); in st_i2c_hw_config()
325 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER); in st_i2c_hw_config()
326 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER_DATAOUT); in st_i2c_hw_config()
329 val = i2c_dev->scl_min_width_us * rate / 100000000; in st_i2c_hw_config()
330 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH); in st_i2c_hw_config()
333 val = i2c_dev->sda_min_width_us * rate / 100000000; in st_i2c_hw_config()
334 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT); in st_i2c_hw_config()
339 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in st_i2c_recover_bus() local
342 dev_dbg(i2c_dev->dev, "Trying to recover bus\n"); in st_i2c_recover_bus()
352 writel_relaxed(0, i2c_dev->base + SSC_IEN); in st_i2c_recover_bus()
354 st_i2c_hw_config(i2c_dev); in st_i2c_recover_bus()
357 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); in st_i2c_recover_bus()
359 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM); in st_i2c_recover_bus()
362 writel_relaxed(0, i2c_dev->base + SSC_TBUF); in st_i2c_recover_bus()
364 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM); in st_i2c_recover_bus()
369 static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev) in st_i2c_wait_free_bus() argument
375 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_wait_free_bus()
382 dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta); in st_i2c_wait_free_bus()
384 ret = i2c_recover_bus(&i2c_dev->adap); in st_i2c_wait_free_bus()
386 dev_err(i2c_dev->dev, "Failed to recover the bus (%d)\n", ret); in st_i2c_wait_free_bus()
398 static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte) in st_i2c_write_tx_fifo() argument
402 writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF); in st_i2c_write_tx_fifo()
412 static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_wr_fill_tx_fifo() argument
414 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_wr_fill_tx_fifo()
418 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_wr_fill_tx_fifo()
422 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_wr_fill_tx_fifo()
431 st_i2c_write_tx_fifo(i2c_dev, *c->buf); in st_i2c_wr_fill_tx_fifo()
441 static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max) in st_i2c_rd_fill_tx_fifo() argument
443 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_rd_fill_tx_fifo()
447 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_rd_fill_tx_fifo()
451 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_rd_fill_tx_fifo()
460 st_i2c_write_tx_fifo(i2c_dev, 0xff); in st_i2c_rd_fill_tx_fifo()
463 static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_read_rx_fifo() argument
465 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_read_rx_fifo()
469 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_read_rx_fifo()
473 i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT); in st_i2c_read_rx_fifo()
478 rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1; in st_i2c_read_rx_fifo()
483 dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i); in st_i2c_read_rx_fifo()
484 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_read_rx_fifo()
492 static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev) in st_i2c_terminate_xfer() argument
494 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_terminate_xfer()
496 st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN); in st_i2c_terminate_xfer()
497 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_terminate_xfer()
500 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN); in st_i2c_terminate_xfer()
501 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_terminate_xfer()
503 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN); in st_i2c_terminate_xfer()
504 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG); in st_i2c_terminate_xfer()
512 static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev) in st_i2c_handle_write() argument
514 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_write()
516 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_handle_write()
520 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_write()
522 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_handle_write()
529 static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev) in st_i2c_handle_read() argument
531 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_read()
536 readl_relaxed(i2c_dev->base + SSC_RBUF); in st_i2c_handle_read()
537 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB); in st_i2c_handle_read()
539 st_i2c_read_rx_fifo(i2c_dev); in st_i2c_handle_read()
544 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_read()
547 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG); in st_i2c_handle_read()
551 writel_relaxed(ien, i2c_dev->base + SSC_IEN); in st_i2c_handle_read()
553 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count); in st_i2c_handle_read()
555 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1); in st_i2c_handle_read()
566 struct st_i2c_dev *i2c_dev = data; in st_i2c_isr_thread() local
567 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_isr_thread()
571 ien = readl_relaxed(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
572 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_isr_thread()
577 dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n", in st_i2c_isr_thread()
585 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
587 st_i2c_handle_write(i2c_dev); in st_i2c_isr_thread()
592 writel_relaxed(0, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
593 complete(&i2c_dev->complete); in st_i2c_isr_thread()
597 writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
601 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
606 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
608 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
613 writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
616 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
618 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
623 dev_err(i2c_dev->dev, in st_i2c_isr_thread()
632 readl(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
644 static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg, in st_i2c_xfer_msg() argument
647 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_xfer_msg()
659 reinit_completion(&i2c_dev->complete); in st_i2c_xfer_msg()
662 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); in st_i2c_xfer_msg()
667 st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
670 st_i2c_write_tx_fifo(i2c_dev, c->addr); in st_i2c_xfer_msg()
674 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_xfer_msg()
677 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_xfer_msg()
680 ret = st_i2c_wait_free_bus(i2c_dev); in st_i2c_xfer_msg()
684 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_xfer_msg()
687 timeout = wait_for_completion_timeout(&i2c_dev->complete, in st_i2c_xfer_msg()
688 i2c_dev->adap.timeout); in st_i2c_xfer_msg()
692 dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n", in st_i2c_xfer_msg()
698 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
701 i2c_dev->base + SSC_CLR); in st_i2c_xfer_msg()
715 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in st_i2c_xfer() local
718 i2c_dev->busy = true; in st_i2c_xfer()
720 ret = clk_prepare_enable(i2c_dev->clk); in st_i2c_xfer()
722 dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n"); in st_i2c_xfer()
726 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_xfer()
728 st_i2c_hw_config(i2c_dev); in st_i2c_xfer()
731 ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1); in st_i2c_xfer()
733 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_xfer()
735 clk_disable_unprepare(i2c_dev->clk); in st_i2c_xfer()
737 i2c_dev->busy = false; in st_i2c_xfer()
745 struct st_i2c_dev *i2c_dev = dev_get_drvdata(dev); in st_i2c_suspend() local
747 if (i2c_dev->busy) in st_i2c_suspend()
785 struct st_i2c_dev *i2c_dev) in st_i2c_of_get_deglitch() argument
790 &i2c_dev->scl_min_width_us); in st_i2c_of_get_deglitch()
792 dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
797 &i2c_dev->sda_min_width_us); in st_i2c_of_get_deglitch()
799 dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
809 struct st_i2c_dev *i2c_dev; in st_i2c_probe() local
815 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); in st_i2c_probe()
816 if (!i2c_dev) in st_i2c_probe()
820 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res); in st_i2c_probe()
821 if (IS_ERR(i2c_dev->base)) in st_i2c_probe()
822 return PTR_ERR(i2c_dev->base); in st_i2c_probe()
824 i2c_dev->irq = irq_of_parse_and_map(np, 0); in st_i2c_probe()
825 if (!i2c_dev->irq) { in st_i2c_probe()
830 i2c_dev->clk = of_clk_get_by_name(np, "ssc"); in st_i2c_probe()
831 if (IS_ERR(i2c_dev->clk)) { in st_i2c_probe()
833 return PTR_ERR(i2c_dev->clk); in st_i2c_probe()
836 i2c_dev->mode = I2C_MODE_STANDARD; in st_i2c_probe()
839 i2c_dev->mode = I2C_MODE_FAST; in st_i2c_probe()
841 i2c_dev->dev = &pdev->dev; in st_i2c_probe()
843 ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq, in st_i2c_probe()
845 IRQF_ONESHOT, pdev->name, i2c_dev); in st_i2c_probe()
847 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); in st_i2c_probe()
851 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_probe()
853 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_probe()
855 ret = st_i2c_of_get_deglitch(np, i2c_dev); in st_i2c_probe()
859 adap = &i2c_dev->adap; in st_i2c_probe()
860 i2c_set_adapdata(adap, i2c_dev); in st_i2c_probe()
870 init_completion(&i2c_dev->complete); in st_i2c_probe()
876 platform_set_drvdata(pdev, i2c_dev); in st_i2c_probe()
878 dev_info(i2c_dev->dev, "%s initialized\n", adap->name); in st_i2c_probe()
885 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev); in st_i2c_remove() local
887 i2c_del_adapter(&i2c_dev->adap); in st_i2c_remove()