1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
4 #include <linux/acpi.h>
6 #include <linux/dmaengine.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/dma/qcom-gpi-dma.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/soc/qcom/geni-se.h>
18 #include <linux/spinlock.h>
19 #include <linux/units.h>
21 #define SE_I2C_TX_TRANS_LEN 0x26c
22 #define SE_I2C_RX_TRANS_LEN 0x270
23 #define SE_I2C_SCL_COUNTERS 0x278
25 #define SE_I2C_ERR (M_CMD_OVERRUN_EN | M_ILLEGAL_CMD_EN | M_CMD_FAILURE_EN |\
26 M_GP_IRQ_1_EN | M_GP_IRQ_3_EN | M_GP_IRQ_4_EN)
27 #define SE_I2C_ABORT BIT(1)
29 /* M_CMD OP codes for I2C */
32 #define I2C_WRITE_READ 0x3
33 #define I2C_ADDR_ONLY 0x4
34 #define I2C_BUS_CLEAR 0x6
35 #define I2C_STOP_ON_BUS 0x7
36 /* M_CMD params for I2C */
37 #define PRE_CMD_DELAY BIT(0)
38 #define TIMESTAMP_BEFORE BIT(1)
39 #define STOP_STRETCH BIT(2)
40 #define TIMESTAMP_AFTER BIT(3)
41 #define POST_COMMAND_DELAY BIT(4)
42 #define IGNORE_ADD_NACK BIT(6)
43 #define READ_FINISHED_WITH_ACK BIT(7)
44 #define BYPASS_ADDR_PHASE BIT(8)
45 #define SLV_ADDR_MSK GENMASK(15, 9)
46 #define SLV_ADDR_SHFT 9
47 /* I2C SCL COUNTER fields */
48 #define HIGH_COUNTER_MSK GENMASK(29, 20)
49 #define HIGH_COUNTER_SHFT 20
50 #define LOW_COUNTER_MSK GENMASK(19, 10)
51 #define LOW_COUNTER_SHFT 10
52 #define CYCLE_COUNTER_MSK GENMASK(9, 0)
54 #define I2C_PACK_TX BIT(0)
55 #define I2C_PACK_RX BIT(1)
57 enum geni_i2c_err_code
{
70 #define DM_I2C_CB_ERR ((BIT(NACK) | BIT(BUS_PROTO) | BIT(ARB_LOST)) \
73 #define I2C_AUTO_SUSPEND_DELAY 250
74 #define KHZ(freq) (1000 * freq)
75 #define PACKING_BYTES_PW 4
77 #define ABORT_TIMEOUT HZ
78 #define XFER_TIMEOUT HZ
79 #define RST_TIMEOUT HZ
86 struct i2c_adapter adap
;
87 struct completion done
;
94 const struct geni_i2c_clk_fld
*clk_fld
;
99 struct dma_chan
*tx_c
;
100 struct dma_chan
*rx_c
;
105 struct geni_i2c_desc
{
109 unsigned int tx_fifo_depth
;
112 struct geni_i2c_err_log
{
117 static const struct geni_i2c_err_log gi2c_log
[] = {
118 [GP_IRQ0
] = {-EIO
, "Unknown I2C err GP_IRQ0"},
119 [NACK
] = {-ENXIO
, "NACK: slv unresponsive, check its power/reset-ln"},
120 [GP_IRQ2
] = {-EIO
, "Unknown I2C err GP IRQ2"},
121 [BUS_PROTO
] = {-EPROTO
, "Bus proto err, noisy/unexpected start/stop"},
122 [ARB_LOST
] = {-EAGAIN
, "Bus arbitration lost, clock line undriveable"},
123 [GP_IRQ5
] = {-EIO
, "Unknown I2C err GP IRQ5"},
124 [GENI_OVERRUN
] = {-EIO
, "Cmd overrun, check GENI cmd-state machine"},
125 [GENI_ILLEGAL_CMD
] = {-EIO
, "Illegal cmd, check GENI cmd-state machine"},
126 [GENI_ABORT_DONE
] = {-ETIMEDOUT
, "Abort after timeout successful"},
127 [GENI_TIMEOUT
] = {-ETIMEDOUT
, "I2C TXN timed out"},
130 struct geni_i2c_clk_fld
{
139 * Hardware uses the underlying formula to calculate time periods of
140 * SCL clock cycle. Firmware uses some additional cycles excluded from the
141 * below formula and it is confirmed that the time periods are within
142 * specification limits.
144 * time of high period of SCL: t_high = (t_high_cnt * clk_div) / source_clock
145 * time of low period of SCL: t_low = (t_low_cnt * clk_div) / source_clock
146 * time of full period of SCL: t_cycle = (t_cycle_cnt * clk_div) / source_clock
147 * clk_freq_out = t / t_cycle
148 * source_clock = 19.2 MHz
150 static const struct geni_i2c_clk_fld geni_i2c_clk_map_19p2mhz
[] = {
151 {KHZ(100), 7, 10, 11, 26},
152 {KHZ(400), 2, 5, 12, 24},
153 {KHZ(1000), 1, 3, 9, 18},
157 /* source_clock = 32 MHz */
158 static const struct geni_i2c_clk_fld geni_i2c_clk_map_32mhz
[] = {
159 {KHZ(100), 8, 14, 18, 40},
160 {KHZ(400), 4, 3, 11, 20},
161 {KHZ(1000), 2, 3, 6, 15},
165 static int geni_i2c_clk_map_idx(struct geni_i2c_dev
*gi2c
)
167 const struct geni_i2c_clk_fld
*itr
;
169 if (clk_get_rate(gi2c
->se
.clk
) == 32 * HZ_PER_MHZ
)
170 itr
= geni_i2c_clk_map_32mhz
;
172 itr
= geni_i2c_clk_map_19p2mhz
;
174 while (itr
->clk_freq_out
!= 0) {
175 if (itr
->clk_freq_out
== gi2c
->clk_freq_out
) {
184 static void qcom_geni_i2c_conf(struct geni_i2c_dev
*gi2c
)
186 const struct geni_i2c_clk_fld
*itr
= gi2c
->clk_fld
;
189 writel_relaxed(0, gi2c
->se
.base
+ SE_GENI_CLK_SEL
);
191 val
= (itr
->clk_div
<< CLK_DIV_SHFT
) | SER_CLK_EN
;
192 writel_relaxed(val
, gi2c
->se
.base
+ GENI_SER_M_CLK_CFG
);
194 val
= itr
->t_high_cnt
<< HIGH_COUNTER_SHFT
;
195 val
|= itr
->t_low_cnt
<< LOW_COUNTER_SHFT
;
196 val
|= itr
->t_cycle_cnt
;
197 writel_relaxed(val
, gi2c
->se
.base
+ SE_I2C_SCL_COUNTERS
);
200 static void geni_i2c_err_misc(struct geni_i2c_dev
*gi2c
)
202 u32 m_cmd
= readl_relaxed(gi2c
->se
.base
+ SE_GENI_M_CMD0
);
203 u32 m_stat
= readl_relaxed(gi2c
->se
.base
+ SE_GENI_M_IRQ_STATUS
);
204 u32 geni_s
= readl_relaxed(gi2c
->se
.base
+ SE_GENI_STATUS
);
205 u32 geni_ios
= readl_relaxed(gi2c
->se
.base
+ SE_GENI_IOS
);
206 u32 dma
= readl_relaxed(gi2c
->se
.base
+ SE_GENI_DMA_MODE_EN
);
210 rx_st
= readl_relaxed(gi2c
->se
.base
+ SE_DMA_RX_IRQ_STAT
);
211 tx_st
= readl_relaxed(gi2c
->se
.base
+ SE_DMA_TX_IRQ_STAT
);
213 rx_st
= readl_relaxed(gi2c
->se
.base
+ SE_GENI_RX_FIFO_STATUS
);
214 tx_st
= readl_relaxed(gi2c
->se
.base
+ SE_GENI_TX_FIFO_STATUS
);
216 dev_dbg(gi2c
->se
.dev
, "DMA:%d tx_stat:0x%x, rx_stat:0x%x, irq-stat:0x%x\n",
217 dma
, tx_st
, rx_st
, m_stat
);
218 dev_dbg(gi2c
->se
.dev
, "m_cmd:0x%x, geni_status:0x%x, geni_ios:0x%x\n",
219 m_cmd
, geni_s
, geni_ios
);
222 static void geni_i2c_err(struct geni_i2c_dev
*gi2c
, int err
)
225 gi2c
->err
= gi2c_log
[err
].err
;
227 dev_dbg(gi2c
->se
.dev
, "len:%d, slv-addr:0x%x, RD/WR:%d\n",
228 gi2c
->cur
->len
, gi2c
->cur
->addr
, gi2c
->cur
->flags
);
231 case GENI_ABORT_DONE
:
232 gi2c
->abort_done
= true;
236 dev_dbg(gi2c
->se
.dev
, "%s\n", gi2c_log
[err
].msg
);
239 dev_err(gi2c
->se
.dev
, "%s\n", gi2c_log
[err
].msg
);
240 geni_i2c_err_misc(gi2c
);
245 static irqreturn_t
geni_i2c_irq(int irq
, void *dev
)
247 struct geni_i2c_dev
*gi2c
= dev
;
248 void __iomem
*base
= gi2c
->se
.base
;
258 spin_lock(&gi2c
->lock
);
259 m_stat
= readl_relaxed(base
+ SE_GENI_M_IRQ_STATUS
);
260 rx_st
= readl_relaxed(base
+ SE_GENI_RX_FIFO_STATUS
);
261 dm_tx_st
= readl_relaxed(base
+ SE_DMA_TX_IRQ_STAT
);
262 dm_rx_st
= readl_relaxed(base
+ SE_DMA_RX_IRQ_STAT
);
263 dma
= readl_relaxed(base
+ SE_GENI_DMA_MODE_EN
);
267 m_stat
& (M_CMD_FAILURE_EN
| M_CMD_ABORT_EN
) ||
268 dm_rx_st
& (DM_I2C_CB_ERR
)) {
269 if (m_stat
& M_GP_IRQ_1_EN
)
270 geni_i2c_err(gi2c
, NACK
);
271 if (m_stat
& M_GP_IRQ_3_EN
)
272 geni_i2c_err(gi2c
, BUS_PROTO
);
273 if (m_stat
& M_GP_IRQ_4_EN
)
274 geni_i2c_err(gi2c
, ARB_LOST
);
275 if (m_stat
& M_CMD_OVERRUN_EN
)
276 geni_i2c_err(gi2c
, GENI_OVERRUN
);
277 if (m_stat
& M_ILLEGAL_CMD_EN
)
278 geni_i2c_err(gi2c
, GENI_ILLEGAL_CMD
);
279 if (m_stat
& M_CMD_ABORT_EN
)
280 geni_i2c_err(gi2c
, GENI_ABORT_DONE
);
281 if (m_stat
& M_GP_IRQ_0_EN
)
282 geni_i2c_err(gi2c
, GP_IRQ0
);
284 /* Disable the TX Watermark interrupt to stop TX */
286 writel_relaxed(0, base
+ SE_GENI_TX_WATERMARK_REG
);
288 dev_dbg(gi2c
->se
.dev
, "i2c dma tx:0x%x, dma rx:0x%x\n",
290 } else if (cur
->flags
& I2C_M_RD
&&
291 m_stat
& (M_RX_FIFO_WATERMARK_EN
| M_RX_FIFO_LAST_EN
)) {
292 u32 rxcnt
= rx_st
& RX_FIFO_WC_MSK
;
294 for (j
= 0; j
< rxcnt
; j
++) {
296 val
= readl_relaxed(base
+ SE_GENI_RX_FIFOn
);
297 while (gi2c
->cur_rd
< cur
->len
&& p
< sizeof(val
)) {
298 cur
->buf
[gi2c
->cur_rd
++] = val
& 0xff;
302 if (gi2c
->cur_rd
== cur
->len
)
305 } else if (!(cur
->flags
& I2C_M_RD
) &&
306 m_stat
& M_TX_FIFO_WATERMARK_EN
) {
307 for (j
= 0; j
< gi2c
->tx_wm
; j
++) {
312 while (gi2c
->cur_wr
< cur
->len
&& p
< sizeof(val
)) {
313 temp
= cur
->buf
[gi2c
->cur_wr
++];
314 val
|= temp
<< (p
* 8);
317 writel_relaxed(val
, base
+ SE_GENI_TX_FIFOn
);
318 /* TX Complete, Disable the TX Watermark interrupt */
319 if (gi2c
->cur_wr
== cur
->len
) {
320 writel_relaxed(0, base
+ SE_GENI_TX_WATERMARK_REG
);
327 writel_relaxed(m_stat
, base
+ SE_GENI_M_IRQ_CLEAR
);
330 writel_relaxed(dm_tx_st
, base
+ SE_DMA_TX_IRQ_CLR
);
332 writel_relaxed(dm_rx_st
, base
+ SE_DMA_RX_IRQ_CLR
);
334 /* if this is err with done-bit not set, handle that through timeout. */
335 if (m_stat
& M_CMD_DONE_EN
|| m_stat
& M_CMD_ABORT_EN
||
336 dm_tx_st
& TX_DMA_DONE
|| dm_tx_st
& TX_RESET_DONE
||
337 dm_rx_st
& RX_DMA_DONE
|| dm_rx_st
& RX_RESET_DONE
)
338 complete(&gi2c
->done
);
340 spin_unlock(&gi2c
->lock
);
345 static void geni_i2c_abort_xfer(struct geni_i2c_dev
*gi2c
)
347 unsigned long time_left
= ABORT_TIMEOUT
;
350 spin_lock_irqsave(&gi2c
->lock
, flags
);
351 geni_i2c_err(gi2c
, GENI_TIMEOUT
);
353 gi2c
->abort_done
= false;
354 geni_se_abort_m_cmd(&gi2c
->se
);
355 spin_unlock_irqrestore(&gi2c
->lock
, flags
);
358 time_left
= wait_for_completion_timeout(&gi2c
->done
, time_left
);
359 } while (!gi2c
->abort_done
&& time_left
);
362 dev_err(gi2c
->se
.dev
, "Timeout abort_m_cmd\n");
365 static void geni_i2c_rx_fsm_rst(struct geni_i2c_dev
*gi2c
)
368 unsigned long time_left
= RST_TIMEOUT
;
370 writel_relaxed(1, gi2c
->se
.base
+ SE_DMA_RX_FSM_RST
);
372 time_left
= wait_for_completion_timeout(&gi2c
->done
, time_left
);
373 val
= readl_relaxed(gi2c
->se
.base
+ SE_DMA_RX_IRQ_STAT
);
374 } while (!(val
& RX_RESET_DONE
) && time_left
);
376 if (!(val
& RX_RESET_DONE
))
377 dev_err(gi2c
->se
.dev
, "Timeout resetting RX_FSM\n");
380 static void geni_i2c_tx_fsm_rst(struct geni_i2c_dev
*gi2c
)
383 unsigned long time_left
= RST_TIMEOUT
;
385 writel_relaxed(1, gi2c
->se
.base
+ SE_DMA_TX_FSM_RST
);
387 time_left
= wait_for_completion_timeout(&gi2c
->done
, time_left
);
388 val
= readl_relaxed(gi2c
->se
.base
+ SE_DMA_TX_IRQ_STAT
);
389 } while (!(val
& TX_RESET_DONE
) && time_left
);
391 if (!(val
& TX_RESET_DONE
))
392 dev_err(gi2c
->se
.dev
, "Timeout resetting TX_FSM\n");
395 static void geni_i2c_rx_msg_cleanup(struct geni_i2c_dev
*gi2c
,
401 geni_i2c_rx_fsm_rst(gi2c
);
402 geni_se_rx_dma_unprep(&gi2c
->se
, gi2c
->dma_addr
, gi2c
->xfer_len
);
403 i2c_put_dma_safe_msg_buf(gi2c
->dma_buf
, cur
, !gi2c
->err
);
407 static void geni_i2c_tx_msg_cleanup(struct geni_i2c_dev
*gi2c
,
413 geni_i2c_tx_fsm_rst(gi2c
);
414 geni_se_tx_dma_unprep(&gi2c
->se
, gi2c
->dma_addr
, gi2c
->xfer_len
);
415 i2c_put_dma_safe_msg_buf(gi2c
->dma_buf
, cur
, !gi2c
->err
);
419 static int geni_i2c_rx_one_msg(struct geni_i2c_dev
*gi2c
, struct i2c_msg
*msg
,
422 dma_addr_t rx_dma
= 0;
423 unsigned long time_left
;
425 struct geni_se
*se
= &gi2c
->se
;
426 size_t len
= msg
->len
;
429 dma_buf
= i2c_get_dma_safe_msg_buf(msg
, 32);
431 geni_se_select_mode(se
, GENI_SE_DMA
);
433 geni_se_select_mode(se
, GENI_SE_FIFO
);
435 writel_relaxed(len
, se
->base
+ SE_I2C_RX_TRANS_LEN
);
436 geni_se_setup_m_cmd(se
, I2C_READ
, m_param
);
438 if (dma_buf
&& geni_se_rx_dma_prep(se
, dma_buf
, len
, &rx_dma
)) {
439 geni_se_select_mode(se
, GENI_SE_FIFO
);
440 i2c_put_dma_safe_msg_buf(dma_buf
, msg
, false);
443 gi2c
->xfer_len
= len
;
444 gi2c
->dma_addr
= rx_dma
;
445 gi2c
->dma_buf
= dma_buf
;
449 time_left
= wait_for_completion_timeout(&gi2c
->done
, XFER_TIMEOUT
);
451 geni_i2c_abort_xfer(gi2c
);
453 geni_i2c_rx_msg_cleanup(gi2c
, cur
);
458 static int geni_i2c_tx_one_msg(struct geni_i2c_dev
*gi2c
, struct i2c_msg
*msg
,
461 dma_addr_t tx_dma
= 0;
462 unsigned long time_left
;
464 struct geni_se
*se
= &gi2c
->se
;
465 size_t len
= msg
->len
;
468 dma_buf
= i2c_get_dma_safe_msg_buf(msg
, 32);
470 geni_se_select_mode(se
, GENI_SE_DMA
);
472 geni_se_select_mode(se
, GENI_SE_FIFO
);
474 writel_relaxed(len
, se
->base
+ SE_I2C_TX_TRANS_LEN
);
475 geni_se_setup_m_cmd(se
, I2C_WRITE
, m_param
);
477 if (dma_buf
&& geni_se_tx_dma_prep(se
, dma_buf
, len
, &tx_dma
)) {
478 geni_se_select_mode(se
, GENI_SE_FIFO
);
479 i2c_put_dma_safe_msg_buf(dma_buf
, msg
, false);
482 gi2c
->xfer_len
= len
;
483 gi2c
->dma_addr
= tx_dma
;
484 gi2c
->dma_buf
= dma_buf
;
487 if (!dma_buf
) /* Get FIFO IRQ */
488 writel_relaxed(1, se
->base
+ SE_GENI_TX_WATERMARK_REG
);
491 time_left
= wait_for_completion_timeout(&gi2c
->done
, XFER_TIMEOUT
);
493 geni_i2c_abort_xfer(gi2c
);
495 geni_i2c_tx_msg_cleanup(gi2c
, cur
);
500 static void i2c_gpi_cb_result(void *cb
, const struct dmaengine_result
*result
)
502 struct geni_i2c_dev
*gi2c
= cb
;
504 if (result
->result
!= DMA_TRANS_NOERROR
) {
505 dev_err(gi2c
->se
.dev
, "DMA txn failed:%d\n", result
->result
);
507 } else if (result
->residue
) {
508 dev_dbg(gi2c
->se
.dev
, "DMA xfer has pending: %d\n", result
->residue
);
511 complete(&gi2c
->done
);
514 static void geni_i2c_gpi_unmap(struct geni_i2c_dev
*gi2c
, struct i2c_msg
*msg
,
515 void *tx_buf
, dma_addr_t tx_addr
,
516 void *rx_buf
, dma_addr_t rx_addr
)
519 dma_unmap_single(gi2c
->se
.dev
->parent
, tx_addr
, msg
->len
, DMA_TO_DEVICE
);
520 i2c_put_dma_safe_msg_buf(tx_buf
, msg
, !gi2c
->err
);
524 dma_unmap_single(gi2c
->se
.dev
->parent
, rx_addr
, msg
->len
, DMA_FROM_DEVICE
);
525 i2c_put_dma_safe_msg_buf(rx_buf
, msg
, !gi2c
->err
);
529 static int geni_i2c_gpi(struct geni_i2c_dev
*gi2c
, struct i2c_msg
*msg
,
530 struct dma_slave_config
*config
, dma_addr_t
*dma_addr_p
,
531 void **buf
, unsigned int op
, struct dma_chan
*dma_chan
)
533 struct gpi_i2c_config
*peripheral
;
537 enum dma_data_direction map_dirn
;
538 enum dma_transfer_direction dma_dirn
;
539 struct dma_async_tx_descriptor
*desc
;
542 peripheral
= config
->peripheral_config
;
544 dma_buf
= i2c_get_dma_safe_msg_buf(msg
, 1);
549 map_dirn
= DMA_TO_DEVICE
;
551 map_dirn
= DMA_FROM_DEVICE
;
553 addr
= dma_map_single(gi2c
->se
.dev
->parent
, dma_buf
, msg
->len
, map_dirn
);
554 if (dma_mapping_error(gi2c
->se
.dev
->parent
, addr
)) {
555 i2c_put_dma_safe_msg_buf(dma_buf
, msg
, false);
559 /* set the length as message for rx txn */
560 peripheral
->rx_len
= msg
->len
;
563 ret
= dmaengine_slave_config(dma_chan
, config
);
565 dev_err(gi2c
->se
.dev
, "dma config error: %d for op:%d\n", ret
, op
);
569 peripheral
->set_config
= 0;
570 peripheral
->multi_msg
= true;
571 flags
= DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
;
574 dma_dirn
= DMA_MEM_TO_DEV
;
576 dma_dirn
= DMA_DEV_TO_MEM
;
578 desc
= dmaengine_prep_slave_single(dma_chan
, addr
, msg
->len
, dma_dirn
, flags
);
580 dev_err(gi2c
->se
.dev
, "prep_slave_sg failed\n");
585 desc
->callback_result
= i2c_gpi_cb_result
;
586 desc
->callback_param
= gi2c
;
588 dmaengine_submit(desc
);
595 dma_unmap_single(gi2c
->se
.dev
->parent
, addr
, msg
->len
, map_dirn
);
596 i2c_put_dma_safe_msg_buf(dma_buf
, msg
, false);
600 static int geni_i2c_gpi_xfer(struct geni_i2c_dev
*gi2c
, struct i2c_msg msgs
[], int num
)
602 struct dma_slave_config config
= {};
603 struct gpi_i2c_config peripheral
= {};
605 unsigned long time_left
;
606 dma_addr_t tx_addr
, rx_addr
;
607 void *tx_buf
= NULL
, *rx_buf
= NULL
;
608 const struct geni_i2c_clk_fld
*itr
= gi2c
->clk_fld
;
610 config
.peripheral_config
= &peripheral
;
611 config
.peripheral_size
= sizeof(peripheral
);
613 peripheral
.pack_enable
= I2C_PACK_TX
| I2C_PACK_RX
;
614 peripheral
.cycle_count
= itr
->t_cycle_cnt
;
615 peripheral
.high_count
= itr
->t_high_cnt
;
616 peripheral
.low_count
= itr
->t_low_cnt
;
617 peripheral
.clk_div
= itr
->clk_div
;
618 peripheral
.set_config
= 1;
619 peripheral
.multi_msg
= false;
621 for (i
= 0; i
< num
; i
++) {
622 gi2c
->cur
= &msgs
[i
];
624 dev_dbg(gi2c
->se
.dev
, "msg[%d].len:%d\n", i
, gi2c
->cur
->len
);
626 peripheral
.stretch
= 0;
628 peripheral
.stretch
= 1;
630 peripheral
.addr
= msgs
[i
].addr
;
632 ret
= geni_i2c_gpi(gi2c
, &msgs
[i
], &config
,
633 &tx_addr
, &tx_buf
, I2C_WRITE
, gi2c
->tx_c
);
637 if (msgs
[i
].flags
& I2C_M_RD
) {
638 ret
= geni_i2c_gpi(gi2c
, &msgs
[i
], &config
,
639 &rx_addr
, &rx_buf
, I2C_READ
, gi2c
->rx_c
);
643 dma_async_issue_pending(gi2c
->rx_c
);
646 dma_async_issue_pending(gi2c
->tx_c
);
648 time_left
= wait_for_completion_timeout(&gi2c
->done
, XFER_TIMEOUT
);
650 gi2c
->err
= -ETIMEDOUT
;
657 geni_i2c_gpi_unmap(gi2c
, &msgs
[i
], tx_buf
, tx_addr
, rx_buf
, rx_addr
);
663 dev_err(gi2c
->se
.dev
, "GPI transfer failed: %d\n", ret
);
664 dmaengine_terminate_sync(gi2c
->rx_c
);
665 dmaengine_terminate_sync(gi2c
->tx_c
);
666 geni_i2c_gpi_unmap(gi2c
, &msgs
[i
], tx_buf
, tx_addr
, rx_buf
, rx_addr
);
670 static int geni_i2c_fifo_xfer(struct geni_i2c_dev
*gi2c
,
671 struct i2c_msg msgs
[], int num
)
675 for (i
= 0; i
< num
; i
++) {
676 u32 m_param
= i
< (num
- 1) ? STOP_STRETCH
: 0;
678 m_param
|= ((msgs
[i
].addr
<< SLV_ADDR_SHFT
) & SLV_ADDR_MSK
);
680 gi2c
->cur
= &msgs
[i
];
681 if (msgs
[i
].flags
& I2C_M_RD
)
682 ret
= geni_i2c_rx_one_msg(gi2c
, &msgs
[i
], m_param
);
684 ret
= geni_i2c_tx_one_msg(gi2c
, &msgs
[i
], m_param
);
693 static int geni_i2c_xfer(struct i2c_adapter
*adap
,
694 struct i2c_msg msgs
[],
697 struct geni_i2c_dev
*gi2c
= i2c_get_adapdata(adap
);
701 reinit_completion(&gi2c
->done
);
702 ret
= pm_runtime_get_sync(gi2c
->se
.dev
);
704 dev_err(gi2c
->se
.dev
, "error turning SE resources:%d\n", ret
);
705 pm_runtime_put_noidle(gi2c
->se
.dev
);
706 /* Set device in suspended since resume failed */
707 pm_runtime_set_suspended(gi2c
->se
.dev
);
711 qcom_geni_i2c_conf(gi2c
);
714 ret
= geni_i2c_gpi_xfer(gi2c
, msgs
, num
);
716 ret
= geni_i2c_fifo_xfer(gi2c
, msgs
, num
);
718 pm_runtime_mark_last_busy(gi2c
->se
.dev
);
719 pm_runtime_put_autosuspend(gi2c
->se
.dev
);
725 static u32
geni_i2c_func(struct i2c_adapter
*adap
)
727 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
730 static const struct i2c_algorithm geni_i2c_algo
= {
731 .master_xfer
= geni_i2c_xfer
,
732 .functionality
= geni_i2c_func
,
736 static const struct acpi_device_id geni_i2c_acpi_match
[] = {
741 MODULE_DEVICE_TABLE(acpi
, geni_i2c_acpi_match
);
744 static void release_gpi_dma(struct geni_i2c_dev
*gi2c
)
747 dma_release_channel(gi2c
->rx_c
);
750 dma_release_channel(gi2c
->tx_c
);
753 static int setup_gpi_dma(struct geni_i2c_dev
*gi2c
)
757 geni_se_select_mode(&gi2c
->se
, GENI_GPI_DMA
);
758 gi2c
->tx_c
= dma_request_chan(gi2c
->se
.dev
, "tx");
759 if (IS_ERR(gi2c
->tx_c
)) {
760 ret
= dev_err_probe(gi2c
->se
.dev
, PTR_ERR(gi2c
->tx_c
),
761 "Failed to get tx DMA ch\n");
765 gi2c
->rx_c
= dma_request_chan(gi2c
->se
.dev
, "rx");
766 if (IS_ERR(gi2c
->rx_c
)) {
767 ret
= dev_err_probe(gi2c
->se
.dev
, PTR_ERR(gi2c
->rx_c
),
768 "Failed to get rx DMA ch\n");
772 dev_dbg(gi2c
->se
.dev
, "Grabbed GPI dma channels\n");
776 dma_release_channel(gi2c
->tx_c
);
781 static int geni_i2c_probe(struct platform_device
*pdev
)
783 struct geni_i2c_dev
*gi2c
;
784 u32 proto
, tx_depth
, fifo_disable
;
786 struct device
*dev
= &pdev
->dev
;
787 const struct geni_i2c_desc
*desc
= NULL
;
789 gi2c
= devm_kzalloc(dev
, sizeof(*gi2c
), GFP_KERNEL
);
794 gi2c
->se
.wrapper
= dev_get_drvdata(dev
->parent
);
795 gi2c
->se
.base
= devm_platform_ioremap_resource(pdev
, 0);
796 if (IS_ERR(gi2c
->se
.base
))
797 return PTR_ERR(gi2c
->se
.base
);
799 desc
= device_get_match_data(&pdev
->dev
);
801 if (desc
&& desc
->has_core_clk
) {
802 gi2c
->core_clk
= devm_clk_get(dev
, "core");
803 if (IS_ERR(gi2c
->core_clk
))
804 return PTR_ERR(gi2c
->core_clk
);
807 gi2c
->se
.clk
= devm_clk_get(dev
, "se");
808 if (IS_ERR(gi2c
->se
.clk
) && !has_acpi_companion(dev
))
809 return PTR_ERR(gi2c
->se
.clk
);
811 ret
= device_property_read_u32(dev
, "clock-frequency",
812 &gi2c
->clk_freq_out
);
814 dev_info(dev
, "Bus frequency not specified, default to 100kHz.\n");
815 gi2c
->clk_freq_out
= KHZ(100);
818 if (has_acpi_companion(dev
))
819 ACPI_COMPANION_SET(&gi2c
->adap
.dev
, ACPI_COMPANION(dev
));
821 gi2c
->irq
= platform_get_irq(pdev
, 0);
825 ret
= geni_i2c_clk_map_idx(gi2c
);
827 dev_err(dev
, "Invalid clk frequency %d Hz: %d\n",
828 gi2c
->clk_freq_out
, ret
);
832 gi2c
->adap
.algo
= &geni_i2c_algo
;
833 init_completion(&gi2c
->done
);
834 spin_lock_init(&gi2c
->lock
);
835 platform_set_drvdata(pdev
, gi2c
);
837 /* Keep interrupts disabled initially to allow for low-power modes */
838 ret
= devm_request_irq(dev
, gi2c
->irq
, geni_i2c_irq
, IRQF_NO_AUTOEN
,
839 dev_name(dev
), gi2c
);
841 dev_err(dev
, "Request_irq failed:%d: err:%d\n",
845 i2c_set_adapdata(&gi2c
->adap
, gi2c
);
846 gi2c
->adap
.dev
.parent
= dev
;
847 gi2c
->adap
.dev
.of_node
= dev
->of_node
;
848 strscpy(gi2c
->adap
.name
, "Geni-I2C", sizeof(gi2c
->adap
.name
));
850 ret
= geni_icc_get(&gi2c
->se
, desc
? desc
->icc_ddr
: "qup-memory");
854 * Set the bus quota for core and cpu to a reasonable value for
856 * Set quota for DDR based on bus speed.
858 gi2c
->se
.icc_paths
[GENI_TO_CORE
].avg_bw
= GENI_DEFAULT_BW
;
859 gi2c
->se
.icc_paths
[CPU_TO_GENI
].avg_bw
= GENI_DEFAULT_BW
;
860 if (!desc
|| desc
->icc_ddr
)
861 gi2c
->se
.icc_paths
[GENI_TO_DDR
].avg_bw
= Bps_to_icc(gi2c
->clk_freq_out
);
863 ret
= geni_icc_set_bw(&gi2c
->se
);
867 ret
= clk_prepare_enable(gi2c
->core_clk
);
871 ret
= geni_se_resources_on(&gi2c
->se
);
873 dev_err(dev
, "Error turning on resources %d\n", ret
);
874 clk_disable_unprepare(gi2c
->core_clk
);
877 proto
= geni_se_read_proto(&gi2c
->se
);
878 if (proto
!= GENI_SE_I2C
) {
879 dev_err(dev
, "Invalid proto %d\n", proto
);
880 geni_se_resources_off(&gi2c
->se
);
881 clk_disable_unprepare(gi2c
->core_clk
);
885 if (desc
&& desc
->no_dma_support
)
886 fifo_disable
= false;
888 fifo_disable
= readl_relaxed(gi2c
->se
.base
+ GENI_IF_DISABLE_RO
) & FIFO_IF_DISABLE
;
891 /* FIFO is disabled, so we can only use GPI DMA */
892 gi2c
->gpi_mode
= true;
893 ret
= setup_gpi_dma(gi2c
);
895 geni_se_resources_off(&gi2c
->se
);
896 clk_disable_unprepare(gi2c
->core_clk
);
897 return dev_err_probe(dev
, ret
, "Failed to setup GPI DMA mode\n");
900 dev_dbg(dev
, "Using GPI DMA mode for I2C\n");
902 gi2c
->gpi_mode
= false;
903 tx_depth
= geni_se_get_tx_fifo_depth(&gi2c
->se
);
905 /* I2C Master Hub Serial Elements doesn't have the HW_PARAM_0 register */
906 if (!tx_depth
&& desc
)
907 tx_depth
= desc
->tx_fifo_depth
;
910 dev_err(dev
, "Invalid TX FIFO depth\n");
911 geni_se_resources_off(&gi2c
->se
);
912 clk_disable_unprepare(gi2c
->core_clk
);
916 gi2c
->tx_wm
= tx_depth
- 1;
917 geni_se_init(&gi2c
->se
, gi2c
->tx_wm
, tx_depth
);
918 geni_se_config_packing(&gi2c
->se
, BITS_PER_BYTE
,
919 PACKING_BYTES_PW
, true, true, true);
921 dev_dbg(dev
, "i2c fifo/se-dma mode. fifo depth:%d\n", tx_depth
);
924 clk_disable_unprepare(gi2c
->core_clk
);
925 ret
= geni_se_resources_off(&gi2c
->se
);
927 dev_err(dev
, "Error turning off resources %d\n", ret
);
931 ret
= geni_icc_disable(&gi2c
->se
);
936 pm_runtime_set_suspended(gi2c
->se
.dev
);
937 pm_runtime_set_autosuspend_delay(gi2c
->se
.dev
, I2C_AUTO_SUSPEND_DELAY
);
938 pm_runtime_use_autosuspend(gi2c
->se
.dev
);
939 pm_runtime_enable(gi2c
->se
.dev
);
941 ret
= i2c_add_adapter(&gi2c
->adap
);
943 dev_err(dev
, "Error adding i2c adapter %d\n", ret
);
944 pm_runtime_disable(gi2c
->se
.dev
);
948 dev_dbg(dev
, "Geni-I2C adaptor successfully added\n");
953 release_gpi_dma(gi2c
);
957 static void geni_i2c_remove(struct platform_device
*pdev
)
959 struct geni_i2c_dev
*gi2c
= platform_get_drvdata(pdev
);
961 i2c_del_adapter(&gi2c
->adap
);
962 release_gpi_dma(gi2c
);
963 pm_runtime_disable(gi2c
->se
.dev
);
966 static void geni_i2c_shutdown(struct platform_device
*pdev
)
968 struct geni_i2c_dev
*gi2c
= platform_get_drvdata(pdev
);
970 /* Make client i2c transfers start failing */
971 i2c_mark_adapter_suspended(&gi2c
->adap
);
974 static int __maybe_unused
geni_i2c_runtime_suspend(struct device
*dev
)
977 struct geni_i2c_dev
*gi2c
= dev_get_drvdata(dev
);
979 disable_irq(gi2c
->irq
);
980 ret
= geni_se_resources_off(&gi2c
->se
);
982 enable_irq(gi2c
->irq
);
989 clk_disable_unprepare(gi2c
->core_clk
);
991 return geni_icc_disable(&gi2c
->se
);
994 static int __maybe_unused
geni_i2c_runtime_resume(struct device
*dev
)
997 struct geni_i2c_dev
*gi2c
= dev_get_drvdata(dev
);
999 ret
= geni_icc_enable(&gi2c
->se
);
1003 ret
= clk_prepare_enable(gi2c
->core_clk
);
1005 goto out_icc_disable
;
1007 ret
= geni_se_resources_on(&gi2c
->se
);
1009 goto out_clk_disable
;
1011 enable_irq(gi2c
->irq
);
1012 gi2c
->suspended
= 0;
1017 clk_disable_unprepare(gi2c
->core_clk
);
1019 geni_icc_disable(&gi2c
->se
);
1024 static int __maybe_unused
geni_i2c_suspend_noirq(struct device
*dev
)
1026 struct geni_i2c_dev
*gi2c
= dev_get_drvdata(dev
);
1028 i2c_mark_adapter_suspended(&gi2c
->adap
);
1030 if (!gi2c
->suspended
) {
1031 geni_i2c_runtime_suspend(dev
);
1032 pm_runtime_disable(dev
);
1033 pm_runtime_set_suspended(dev
);
1034 pm_runtime_enable(dev
);
1039 static int __maybe_unused
geni_i2c_resume_noirq(struct device
*dev
)
1041 struct geni_i2c_dev
*gi2c
= dev_get_drvdata(dev
);
1043 i2c_mark_adapter_resumed(&gi2c
->adap
);
1047 static const struct dev_pm_ops geni_i2c_pm_ops
= {
1048 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(geni_i2c_suspend_noirq
, geni_i2c_resume_noirq
)
1049 SET_RUNTIME_PM_OPS(geni_i2c_runtime_suspend
, geni_i2c_runtime_resume
,
1053 static const struct geni_i2c_desc i2c_master_hub
= {
1054 .has_core_clk
= true,
1056 .no_dma_support
= true,
1057 .tx_fifo_depth
= 16,
1060 static const struct of_device_id geni_i2c_dt_match
[] = {
1061 { .compatible
= "qcom,geni-i2c" },
1062 { .compatible
= "qcom,geni-i2c-master-hub", .data
= &i2c_master_hub
},
1065 MODULE_DEVICE_TABLE(of
, geni_i2c_dt_match
);
1067 static struct platform_driver geni_i2c_driver
= {
1068 .probe
= geni_i2c_probe
,
1069 .remove
= geni_i2c_remove
,
1070 .shutdown
= geni_i2c_shutdown
,
1073 .pm
= &geni_i2c_pm_ops
,
1074 .of_match_table
= geni_i2c_dt_match
,
1075 .acpi_match_table
= ACPI_PTR(geni_i2c_acpi_match
),
1079 module_platform_driver(geni_i2c_driver
);
1081 MODULE_DESCRIPTION("I2C Controller Driver for GENI based QUP cores");
1082 MODULE_LICENSE("GPL v2");