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()
340 static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev) in st_i2c_wait_free_bus() argument
346 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_wait_free_bus()
353 dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta); in st_i2c_wait_free_bus()
363 static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte) in st_i2c_write_tx_fifo() argument
367 writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF); in st_i2c_write_tx_fifo()
377 static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_wr_fill_tx_fifo() argument
379 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_wr_fill_tx_fifo()
383 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_wr_fill_tx_fifo()
387 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_wr_fill_tx_fifo()
396 st_i2c_write_tx_fifo(i2c_dev, *c->buf); in st_i2c_wr_fill_tx_fifo()
406 static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max) in st_i2c_rd_fill_tx_fifo() argument
408 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_rd_fill_tx_fifo()
412 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_rd_fill_tx_fifo()
416 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); in st_i2c_rd_fill_tx_fifo()
425 st_i2c_write_tx_fifo(i2c_dev, 0xff); in st_i2c_rd_fill_tx_fifo()
428 static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev) in st_i2c_read_rx_fifo() argument
430 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_read_rx_fifo()
434 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_read_rx_fifo()
438 i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT); in st_i2c_read_rx_fifo()
443 rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1; in st_i2c_read_rx_fifo()
448 dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i); in st_i2c_read_rx_fifo()
449 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_read_rx_fifo()
457 static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev) in st_i2c_terminate_xfer() argument
459 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_terminate_xfer()
461 st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN); in st_i2c_terminate_xfer()
462 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_terminate_xfer()
465 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN); in st_i2c_terminate_xfer()
466 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_terminate_xfer()
468 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN); in st_i2c_terminate_xfer()
469 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG); in st_i2c_terminate_xfer()
477 static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev) in st_i2c_handle_write() argument
479 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_write()
481 st_i2c_flush_rx_fifo(i2c_dev); in st_i2c_handle_write()
485 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_write()
487 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_handle_write()
494 static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev) in st_i2c_handle_read() argument
496 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_handle_read()
501 readl_relaxed(i2c_dev->base + SSC_RBUF); in st_i2c_handle_read()
502 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB); in st_i2c_handle_read()
504 st_i2c_read_rx_fifo(i2c_dev); in st_i2c_handle_read()
509 st_i2c_terminate_xfer(i2c_dev); in st_i2c_handle_read()
512 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG); in st_i2c_handle_read()
516 writel_relaxed(ien, i2c_dev->base + SSC_IEN); in st_i2c_handle_read()
518 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count); in st_i2c_handle_read()
520 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1); in st_i2c_handle_read()
531 struct st_i2c_dev *i2c_dev = data; in st_i2c_isr_thread() local
532 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_isr_thread()
536 ien = readl_relaxed(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
537 sta = readl_relaxed(i2c_dev->base + SSC_STA); in st_i2c_isr_thread()
542 dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n", in st_i2c_isr_thread()
550 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
552 st_i2c_handle_write(i2c_dev); in st_i2c_isr_thread()
557 writel_relaxed(0, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
558 complete(&i2c_dev->complete); in st_i2c_isr_thread()
562 writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
566 st_i2c_handle_read(i2c_dev); in st_i2c_isr_thread()
571 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
573 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
578 writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR); in st_i2c_isr_thread()
581 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
583 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); in st_i2c_isr_thread()
588 dev_err(i2c_dev->dev, in st_i2c_isr_thread()
597 readl(i2c_dev->base + SSC_IEN); in st_i2c_isr_thread()
609 static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg, in st_i2c_xfer_msg() argument
612 struct st_i2c_client *c = &i2c_dev->client; in st_i2c_xfer_msg()
625 reinit_completion(&i2c_dev->complete); in st_i2c_xfer_msg()
628 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); in st_i2c_xfer_msg()
633 st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
636 st_i2c_write_tx_fifo(i2c_dev, c->addr); in st_i2c_xfer_msg()
640 st_i2c_wr_fill_tx_fifo(i2c_dev); in st_i2c_xfer_msg()
643 writel_relaxed(it, i2c_dev->base + SSC_IEN); in st_i2c_xfer_msg()
646 ret = st_i2c_wait_free_bus(i2c_dev); in st_i2c_xfer_msg()
650 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); in st_i2c_xfer_msg()
653 timeout = wait_for_completion_timeout(&i2c_dev->complete, in st_i2c_xfer_msg()
654 i2c_dev->adap.timeout); in st_i2c_xfer_msg()
658 dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n", in st_i2c_xfer_msg()
664 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c); in st_i2c_xfer_msg()
667 i2c_dev->base + SSC_CLR); in st_i2c_xfer_msg()
681 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in st_i2c_xfer() local
684 i2c_dev->busy = true; in st_i2c_xfer()
686 ret = clk_prepare_enable(i2c_dev->clk); in st_i2c_xfer()
688 dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n"); in st_i2c_xfer()
692 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_xfer()
694 st_i2c_hw_config(i2c_dev); in st_i2c_xfer()
697 ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1); in st_i2c_xfer()
699 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_xfer()
701 clk_disable_unprepare(i2c_dev->clk); in st_i2c_xfer()
703 i2c_dev->busy = false; in st_i2c_xfer()
713 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev); in st_i2c_suspend() local
715 if (i2c_dev->busy) in st_i2c_suspend()
749 struct st_i2c_dev *i2c_dev) in st_i2c_of_get_deglitch() argument
754 &i2c_dev->scl_min_width_us); in st_i2c_of_get_deglitch()
756 dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
761 &i2c_dev->sda_min_width_us); in st_i2c_of_get_deglitch()
763 dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n"); in st_i2c_of_get_deglitch()
773 struct st_i2c_dev *i2c_dev; in st_i2c_probe() local
779 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); in st_i2c_probe()
780 if (!i2c_dev) in st_i2c_probe()
784 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res); in st_i2c_probe()
785 if (IS_ERR(i2c_dev->base)) in st_i2c_probe()
786 return PTR_ERR(i2c_dev->base); in st_i2c_probe()
788 i2c_dev->irq = irq_of_parse_and_map(np, 0); in st_i2c_probe()
789 if (!i2c_dev->irq) { in st_i2c_probe()
794 i2c_dev->clk = of_clk_get_by_name(np, "ssc"); in st_i2c_probe()
795 if (IS_ERR(i2c_dev->clk)) { in st_i2c_probe()
797 return PTR_ERR(i2c_dev->clk); in st_i2c_probe()
800 i2c_dev->mode = I2C_MODE_STANDARD; in st_i2c_probe()
803 i2c_dev->mode = I2C_MODE_FAST; in st_i2c_probe()
805 i2c_dev->dev = &pdev->dev; in st_i2c_probe()
807 ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq, in st_i2c_probe()
809 IRQF_ONESHOT, pdev->name, i2c_dev); in st_i2c_probe()
811 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); in st_i2c_probe()
815 pinctrl_pm_select_default_state(i2c_dev->dev); in st_i2c_probe()
817 pinctrl_pm_select_idle_state(i2c_dev->dev); in st_i2c_probe()
819 ret = st_i2c_of_get_deglitch(np, i2c_dev); in st_i2c_probe()
823 adap = &i2c_dev->adap; in st_i2c_probe()
824 i2c_set_adapdata(adap, i2c_dev); in st_i2c_probe()
833 init_completion(&i2c_dev->complete); in st_i2c_probe()
841 platform_set_drvdata(pdev, i2c_dev); in st_i2c_probe()
843 dev_info(i2c_dev->dev, "%s initialized\n", adap->name); in st_i2c_probe()
850 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev); in st_i2c_remove() local
852 i2c_del_adapter(&i2c_dev->adap); in st_i2c_remove()