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()
442 static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max) in st_i2c_rd_fill_tx_fifo() argument
444 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_rd_fill_tx_fifo()
448 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_rd_fill_tx_fifo()
452 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_rd_fill_tx_fifo()
461 st_i2c_write_tx_fifo(i2c_dev, 0xff); in st_i2c_rd_fill_tx_fifo()
464 static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_read_rx_fifo() argument
466 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_read_rx_fifo()
470 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_read_rx_fifo()
474 i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT); in st_i2c_read_rx_fifo()
479 rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1; in st_i2c_read_rx_fifo()
484 dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i); in st_i2c_read_rx_fifo()
485 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_read_rx_fifo()
493 static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev) in st_i2c_terminate_xfer() argument
495 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_terminate_xfer()
497 st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN); in st_i2c_terminate_xfer()
498 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_terminate_xfer()
501 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN); in st_i2c_terminate_xfer()
502 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_terminate_xfer()
504 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN); in st_i2c_terminate_xfer()
505 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG); in st_i2c_terminate_xfer()
513 static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev) in st_i2c_handle_write() argument
515 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_write()
517 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_handle_write()
521 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_write()
523 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_handle_write()
530 static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev) in st_i2c_handle_read() argument
532 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_read()
537 readl_relaxed(i2c_dev->base + SSC_RBUF); in st_i2c_handle_read()
538 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB); in st_i2c_handle_read()
540 st_i2c_read_rx_fifo(i2c_dev); in st_i2c_handle_read()
545 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_read()
548 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG); in st_i2c_handle_read()
552 writel_relaxed(ien, i2c_dev->base + SSC_IEN); in st_i2c_handle_read()
554 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count); in st_i2c_handle_read()
556 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1); in st_i2c_handle_read()
567 struct st_i2c_dev *i2c_dev = data; in st_i2c_isr_thread() local
568 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_isr_thread()
572 ien = readl_relaxed(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
573 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_isr_thread()
578 dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n", in st_i2c_isr_thread()
586 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
588 st_i2c_handle_write(i2c_dev); in st_i2c_isr_thread()
593 writel_relaxed(0, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
594 complete(&i2c_dev->complete); in st_i2c_isr_thread()
598 writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
602 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
607 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
609 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
614 writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
617 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
619 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
624 dev_err(i2c_dev->dev, in st_i2c_isr_thread()
633 readl(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
645 static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg, in st_i2c_xfer_msg() argument
648 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_xfer_msg()
660 reinit_completion(&i2c_dev->complete); in st_i2c_xfer_msg()
663 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); in st_i2c_xfer_msg()
668 st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
671 st_i2c_write_tx_fifo(i2c_dev, c->addr); in st_i2c_xfer_msg()
675 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_xfer_msg()
678 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_xfer_msg()
681 ret = st_i2c_wait_free_bus(i2c_dev); in st_i2c_xfer_msg()
685 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_xfer_msg()
688 timeout = wait_for_completion_timeout(&i2c_dev->complete, in st_i2c_xfer_msg()
689 i2c_dev->adap.timeout); in st_i2c_xfer_msg()
693 dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n", in st_i2c_xfer_msg()
699 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
702 i2c_dev->base + SSC_CLR); in st_i2c_xfer_msg()
716 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in st_i2c_xfer() local
719 i2c_dev->busy = true; in st_i2c_xfer()
721 ret = clk_prepare_enable(i2c_dev->clk); in st_i2c_xfer()
723 dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n"); in st_i2c_xfer()
727 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_xfer()
729 st_i2c_hw_config(i2c_dev); in st_i2c_xfer()
732 ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1); in st_i2c_xfer()
734 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_xfer()
736 clk_disable_unprepare(i2c_dev->clk); in st_i2c_xfer()
738 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()
781 struct st_i2c_dev *i2c_dev) in st_i2c_of_get_deglitch() argument
786 &i2c_dev->scl_min_width_us); in st_i2c_of_get_deglitch()
788 dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
793 &i2c_dev->sda_min_width_us); in st_i2c_of_get_deglitch()
795 dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
805 struct st_i2c_dev *i2c_dev; in st_i2c_probe() local
811 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); in st_i2c_probe()
812 if (!i2c_dev) in st_i2c_probe()
816 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res); in st_i2c_probe()
817 if (IS_ERR(i2c_dev->base)) in st_i2c_probe()
818 return PTR_ERR(i2c_dev->base); in st_i2c_probe()
820 i2c_dev->irq = irq_of_parse_and_map(np, 0); in st_i2c_probe()
821 if (!i2c_dev->irq) { in st_i2c_probe()
826 i2c_dev->clk = of_clk_get_by_name(np, "ssc"); in st_i2c_probe()
827 if (IS_ERR(i2c_dev->clk)) { in st_i2c_probe()
829 return PTR_ERR(i2c_dev->clk); in st_i2c_probe()
832 i2c_dev->mode = I2C_MODE_STANDARD; in st_i2c_probe()
835 i2c_dev->mode = I2C_MODE_FAST; in st_i2c_probe()
837 i2c_dev->dev = &pdev->dev; in st_i2c_probe()
839 ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq, in st_i2c_probe()
841 IRQF_ONESHOT, pdev->name, i2c_dev); in st_i2c_probe()
843 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); in st_i2c_probe()
847 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_probe()
849 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_probe()
851 ret = st_i2c_of_get_deglitch(np, i2c_dev); in st_i2c_probe()
855 adap = &i2c_dev->adap; in st_i2c_probe()
856 i2c_set_adapdata(adap, i2c_dev); in st_i2c_probe()
866 init_completion(&i2c_dev->complete); in st_i2c_probe()
872 platform_set_drvdata(pdev, i2c_dev); in st_i2c_probe()
874 dev_info(i2c_dev->dev, "%s initialized\n", adap->name); in st_i2c_probe()
881 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev); in st_i2c_remove() local
883 i2c_del_adapter(&i2c_dev->adap); in st_i2c_remove()