1 // SPDX-License-Identifier: GPL-2.0-only
3 * i2c-exynos5.c - Samsung Exynos5 I2C Controller Driver
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/time.h>
13 #include <linux/interrupt.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19 #include <linux/slab.h>
22 #include <linux/spinlock.h>
25 * HSI2C controller from Samsung supports 2 modes of operation
26 * 1. Auto mode: Where in master automatically controls the whole transaction
27 * 2. Manual mode: Software controls the transaction by issuing commands
28 * START, READ, WRITE, STOP, RESTART in I2C_MANUAL_CMD register.
30 * Operation mode can be selected by setting AUTO_MODE bit in I2C_CONF register
32 * Special bits are available for both modes of operation to set commands
33 * and for checking transfer status
37 #define HSI2C_CTL 0x00
38 #define HSI2C_FIFO_CTL 0x04
39 #define HSI2C_TRAILIG_CTL 0x08
40 #define HSI2C_CLK_CTL 0x0C
41 #define HSI2C_CLK_SLOT 0x10
42 #define HSI2C_INT_ENABLE 0x20
43 #define HSI2C_INT_STATUS 0x24
44 #define HSI2C_ERR_STATUS 0x2C
45 #define HSI2C_FIFO_STATUS 0x30
46 #define HSI2C_TX_DATA 0x34
47 #define HSI2C_RX_DATA 0x38
48 #define HSI2C_CONF 0x40
49 #define HSI2C_AUTO_CONF 0x44
50 #define HSI2C_TIMEOUT 0x48
51 #define HSI2C_MANUAL_CMD 0x4C
52 #define HSI2C_TRANS_STATUS 0x50
53 #define HSI2C_TIMING_HS1 0x54
54 #define HSI2C_TIMING_HS2 0x58
55 #define HSI2C_TIMING_HS3 0x5C
56 #define HSI2C_TIMING_FS1 0x60
57 #define HSI2C_TIMING_FS2 0x64
58 #define HSI2C_TIMING_FS3 0x68
59 #define HSI2C_TIMING_SLA 0x6C
60 #define HSI2C_ADDR 0x70
62 /* I2C_CTL Register bits */
63 #define HSI2C_FUNC_MODE_I2C (1u << 0)
64 #define HSI2C_MASTER (1u << 3)
65 #define HSI2C_RXCHON (1u << 6)
66 #define HSI2C_TXCHON (1u << 7)
67 #define HSI2C_SW_RST (1u << 31)
69 /* I2C_FIFO_CTL Register bits */
70 #define HSI2C_RXFIFO_EN (1u << 0)
71 #define HSI2C_TXFIFO_EN (1u << 1)
72 #define HSI2C_RXFIFO_TRIGGER_LEVEL(x) ((x) << 4)
73 #define HSI2C_TXFIFO_TRIGGER_LEVEL(x) ((x) << 16)
75 /* I2C_TRAILING_CTL Register bits */
76 #define HSI2C_TRAILING_COUNT (0xf)
78 /* I2C_INT_EN Register bits */
79 #define HSI2C_INT_TX_ALMOSTEMPTY_EN (1u << 0)
80 #define HSI2C_INT_RX_ALMOSTFULL_EN (1u << 1)
81 #define HSI2C_INT_TRAILING_EN (1u << 6)
83 /* I2C_INT_STAT Register bits */
84 #define HSI2C_INT_TX_ALMOSTEMPTY (1u << 0)
85 #define HSI2C_INT_RX_ALMOSTFULL (1u << 1)
86 #define HSI2C_INT_TX_UNDERRUN (1u << 2)
87 #define HSI2C_INT_TX_OVERRUN (1u << 3)
88 #define HSI2C_INT_RX_UNDERRUN (1u << 4)
89 #define HSI2C_INT_RX_OVERRUN (1u << 5)
90 #define HSI2C_INT_TRAILING (1u << 6)
91 #define HSI2C_INT_I2C (1u << 9)
93 #define HSI2C_INT_TRANS_DONE (1u << 7)
94 #define HSI2C_INT_TRANS_ABORT (1u << 8)
95 #define HSI2C_INT_NO_DEV_ACK (1u << 9)
96 #define HSI2C_INT_NO_DEV (1u << 10)
97 #define HSI2C_INT_TIMEOUT (1u << 11)
98 #define HSI2C_INT_I2C_TRANS (HSI2C_INT_TRANS_DONE | \
99 HSI2C_INT_TRANS_ABORT | \
100 HSI2C_INT_NO_DEV_ACK | \
104 /* I2C_FIFO_STAT Register bits */
105 #define HSI2C_RX_FIFO_EMPTY (1u << 24)
106 #define HSI2C_RX_FIFO_FULL (1u << 23)
107 #define HSI2C_RX_FIFO_LVL(x) ((x >> 16) & 0x7f)
108 #define HSI2C_TX_FIFO_EMPTY (1u << 8)
109 #define HSI2C_TX_FIFO_FULL (1u << 7)
110 #define HSI2C_TX_FIFO_LVL(x) ((x >> 0) & 0x7f)
112 /* I2C_CONF Register bits */
113 #define HSI2C_AUTO_MODE (1u << 31)
114 #define HSI2C_10BIT_ADDR_MODE (1u << 30)
115 #define HSI2C_HS_MODE (1u << 29)
117 /* I2C_AUTO_CONF Register bits */
118 #define HSI2C_READ_WRITE (1u << 16)
119 #define HSI2C_STOP_AFTER_TRANS (1u << 17)
120 #define HSI2C_MASTER_RUN (1u << 31)
122 /* I2C_TIMEOUT Register bits */
123 #define HSI2C_TIMEOUT_EN (1u << 31)
124 #define HSI2C_TIMEOUT_MASK 0xff
126 /* I2C_MANUAL_CMD register bits */
127 #define HSI2C_CMD_READ_DATA (1u << 4)
128 #define HSI2C_CMD_SEND_STOP (1u << 2)
130 /* I2C_TRANS_STATUS register bits */
131 #define HSI2C_MASTER_BUSY (1u << 17)
132 #define HSI2C_SLAVE_BUSY (1u << 16)
134 /* I2C_TRANS_STATUS register bits for Exynos5 variant */
135 #define HSI2C_TIMEOUT_AUTO (1u << 4)
136 #define HSI2C_NO_DEV (1u << 3)
137 #define HSI2C_NO_DEV_ACK (1u << 2)
138 #define HSI2C_TRANS_ABORT (1u << 1)
139 #define HSI2C_TRANS_DONE (1u << 0)
141 /* I2C_TRANS_STATUS register bits for Exynos7 variant */
142 #define HSI2C_MASTER_ST_MASK 0xf
143 #define HSI2C_MASTER_ST_IDLE 0x0
144 #define HSI2C_MASTER_ST_START 0x1
145 #define HSI2C_MASTER_ST_RESTART 0x2
146 #define HSI2C_MASTER_ST_STOP 0x3
147 #define HSI2C_MASTER_ST_MASTER_ID 0x4
148 #define HSI2C_MASTER_ST_ADDR0 0x5
149 #define HSI2C_MASTER_ST_ADDR1 0x6
150 #define HSI2C_MASTER_ST_ADDR2 0x7
151 #define HSI2C_MASTER_ST_ADDR_SR 0x8
152 #define HSI2C_MASTER_ST_READ 0x9
153 #define HSI2C_MASTER_ST_WRITE 0xa
154 #define HSI2C_MASTER_ST_NO_ACK 0xb
155 #define HSI2C_MASTER_ST_LOSE 0xc
156 #define HSI2C_MASTER_ST_WAIT 0xd
157 #define HSI2C_MASTER_ST_WAIT_CMD 0xe
159 /* I2C_ADDR register bits */
160 #define HSI2C_SLV_ADDR_SLV(x) ((x & 0x3ff) << 0)
161 #define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
162 #define HSI2C_MASTER_ID(x) ((x & 0xff) << 24)
163 #define MASTER_ID(x) ((x & 0x7) + 0x08)
165 #define EXYNOS5_I2C_TIMEOUT (msecs_to_jiffies(100))
167 enum i2c_type_exynos
{
170 I2C_TYPE_EXYNOSAUTOV9
,
174 struct i2c_adapter adap
;
177 struct completion msg_complete
;
178 unsigned int msg_ptr
;
183 struct clk
*clk
; /* operating clock */
184 struct clk
*pclk
; /* bus clock */
188 spinlock_t lock
; /* IRQ synchronization */
191 * Since the TRANS_DONE bit is cleared on read, and we may read it
192 * either during an IRQ or after a transaction, keep track of its
198 * Called from atomic context, don't use interrupts.
202 /* Controller operating frequency */
203 unsigned int op_clock
;
205 /* Version of HS-I2C Hardware */
206 const struct exynos_hsi2c_variant
*variant
;
210 * struct exynos_hsi2c_variant - platform specific HSI2C driver data
211 * @fifo_depth: the fifo depth supported by the HSI2C module
212 * @hw: the hardware variant of Exynos I2C controller
214 * Specifies platform specific configuration of HSI2C module.
215 * Note: A structure for driver specific platform data is used for future
216 * expansion of its usage.
218 struct exynos_hsi2c_variant
{
219 unsigned int fifo_depth
;
220 enum i2c_type_exynos hw
;
223 static const struct exynos_hsi2c_variant exynos5250_hsi2c_data
= {
225 .hw
= I2C_TYPE_EXYNOS5
,
228 static const struct exynos_hsi2c_variant exynos5260_hsi2c_data
= {
230 .hw
= I2C_TYPE_EXYNOS5
,
233 static const struct exynos_hsi2c_variant exynos7_hsi2c_data
= {
235 .hw
= I2C_TYPE_EXYNOS7
,
238 static const struct exynos_hsi2c_variant exynosautov9_hsi2c_data
= {
240 .hw
= I2C_TYPE_EXYNOSAUTOV9
,
243 static const struct of_device_id exynos5_i2c_match
[] = {
245 .compatible
= "samsung,exynos5-hsi2c",
246 .data
= &exynos5250_hsi2c_data
248 .compatible
= "samsung,exynos5250-hsi2c",
249 .data
= &exynos5250_hsi2c_data
251 .compatible
= "samsung,exynos5260-hsi2c",
252 .data
= &exynos5260_hsi2c_data
254 .compatible
= "samsung,exynos7-hsi2c",
255 .data
= &exynos7_hsi2c_data
257 .compatible
= "samsung,exynosautov9-hsi2c",
258 .data
= &exynosautov9_hsi2c_data
261 MODULE_DEVICE_TABLE(of
, exynos5_i2c_match
);
263 static void exynos5_i2c_clr_pend_irq(struct exynos5_i2c
*i2c
)
265 writel(readl(i2c
->regs
+ HSI2C_INT_STATUS
),
266 i2c
->regs
+ HSI2C_INT_STATUS
);
270 * exynos5_i2c_set_timing: updates the registers with appropriate
271 * timing values calculated
273 * Timing values for operation are calculated against 100kHz, 400kHz
274 * or 1MHz controller operating frequency.
276 * Returns 0 on success, -EINVAL if the cycle length cannot
279 static int exynos5_i2c_set_timing(struct exynos5_i2c
*i2c
, bool hs_timings
)
285 unsigned int t_start_su
, t_start_hd
;
286 unsigned int t_stop_su
;
287 unsigned int t_data_su
, t_data_hd
;
288 unsigned int t_scl_l
, t_scl_h
;
289 unsigned int t_sr_release
;
290 unsigned int t_ftl_cycle
;
291 unsigned int clkin
= clk_get_rate(i2c
->clk
);
292 unsigned int op_clk
= hs_timings
? i2c
->op_clock
:
293 (i2c
->op_clock
>= I2C_MAX_FAST_MODE_PLUS_FREQ
) ? I2C_MAX_STANDARD_MODE_FREQ
:
295 int div
, clk_cycle
, temp
;
298 * In case of HSI2C controllers in ExynosAutoV9:
300 * FSCL = IPCLK / ((CLK_DIV + 1) * 16)
301 * T_SCL_LOW = IPCLK * (CLK_DIV + 1) * (N + M)
302 * [N : number of 0's in the TSCL_H_HS]
303 * [M : number of 0's in the TSCL_L_HS]
304 * T_SCL_HIGH = IPCLK * (CLK_DIV + 1) * (N + M)
305 * [N : number of 1's in the TSCL_H_HS]
306 * [M : number of 1's in the TSCL_L_HS]
308 * Result of (N + M) is always 8.
309 * In general case, we don't need to control timing_s1 and timing_s2.
311 if (i2c
->variant
->hw
== I2C_TYPE_EXYNOSAUTOV9
) {
312 div
= ((clkin
/ (16 * i2c
->op_clock
)) - 1);
313 i2c_timing_s3
= div
<< 16;
315 writel(i2c_timing_s3
, i2c
->regs
+ HSI2C_TIMING_HS3
);
317 writel(i2c_timing_s3
, i2c
->regs
+ HSI2C_TIMING_FS3
);
323 * In case of HSI2C controller in Exynos5 series
325 * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
327 * In case of HSI2C controllers in Exynos7 series
329 * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + FLT_CYCLE
331 * clk_cycle := TSCLK_L + TSCLK_H
332 * temp := (CLK_DIV + 1) * (clk_cycle + 2)
334 * Constraints: 4 <= temp, 0 <= CLK_DIV < 256, 2 <= clk_cycle <= 510
336 * To split SCL clock into low, high periods appropriately, one
337 * proportion factor for each I2C mode is used, which is calculated
338 * using this formula.
340 * ((t_low_min + (scl_clock - t_low_min - t_high_min) / 2) / scl_clock)
343 * t_low_min is the minimal value of low period of the SCL clock in us;
344 * t_high_min is the minimal value of high period of the SCL clock in us;
345 * scl_clock is converted from SCL clock frequency into us.
347 * Below are the proportion factors for these I2C modes:
348 * t_low_min, t_high_min, scl_clock, proportion
349 * Standard Mode: 4.7us, 4.0us, 10us, 0.535
350 * Fast Mode: 1.3us, 0.6us, 2.5us, 0.64
351 * Fast-Plus Mode: 0.5us, 0.26us, 1us, 0.62
354 t_ftl_cycle
= (readl(i2c
->regs
+ HSI2C_CONF
) >> 16) & 0x7;
355 temp
= clkin
/ op_clk
- 8 - t_ftl_cycle
;
356 if (i2c
->variant
->hw
!= I2C_TYPE_EXYNOS7
)
359 clk_cycle
= temp
/ (div
+ 1) - 2;
360 if (temp
< 4 || div
>= 256 || clk_cycle
< 2) {
361 dev_err(i2c
->dev
, "%s clock set-up failed\n",
362 hs_timings
? "HS" : "FS");
367 * Scale clk_cycle to get t_scl_l using the proption factors for individual I2C modes.
369 if (op_clk
<= I2C_MAX_STANDARD_MODE_FREQ
)
370 t_scl_l
= clk_cycle
* 535 / 1000;
371 else if (op_clk
<= I2C_MAX_FAST_MODE_FREQ
)
372 t_scl_l
= clk_cycle
* 64 / 100;
374 t_scl_l
= clk_cycle
* 62 / 100;
378 t_scl_h
= clk_cycle
- t_scl_l
;
379 t_start_su
= t_scl_l
;
380 t_start_hd
= t_scl_l
;
382 t_data_su
= t_scl_l
/ 2;
383 t_data_hd
= t_scl_l
/ 2;
384 t_sr_release
= clk_cycle
;
386 i2c_timing_s1
= t_start_su
<< 24 | t_start_hd
<< 16 | t_stop_su
<< 8;
387 i2c_timing_s2
= t_data_su
<< 24 | t_scl_l
<< 8 | t_scl_h
<< 0;
388 i2c_timing_s3
= div
<< 16 | t_sr_release
<< 0;
389 i2c_timing_sla
= t_data_hd
<< 0;
391 dev_dbg(i2c
->dev
, "tSTART_SU: %X, tSTART_HD: %X, tSTOP_SU: %X\n",
392 t_start_su
, t_start_hd
, t_stop_su
);
393 dev_dbg(i2c
->dev
, "tDATA_SU: %X, tSCL_L: %X, tSCL_H: %X\n",
394 t_data_su
, t_scl_l
, t_scl_h
);
395 dev_dbg(i2c
->dev
, "nClkDiv: %X, tSR_RELEASE: %X\n",
397 dev_dbg(i2c
->dev
, "tDATA_HD: %X\n", t_data_hd
);
400 writel(i2c_timing_s1
, i2c
->regs
+ HSI2C_TIMING_HS1
);
401 writel(i2c_timing_s2
, i2c
->regs
+ HSI2C_TIMING_HS2
);
402 writel(i2c_timing_s3
, i2c
->regs
+ HSI2C_TIMING_HS3
);
404 writel(i2c_timing_s1
, i2c
->regs
+ HSI2C_TIMING_FS1
);
405 writel(i2c_timing_s2
, i2c
->regs
+ HSI2C_TIMING_FS2
);
406 writel(i2c_timing_s3
, i2c
->regs
+ HSI2C_TIMING_FS3
);
408 writel(i2c_timing_sla
, i2c
->regs
+ HSI2C_TIMING_SLA
);
413 static int exynos5_hsi2c_clock_setup(struct exynos5_i2c
*i2c
)
415 /* always set Fast Speed timings */
416 int ret
= exynos5_i2c_set_timing(i2c
, false);
418 if (ret
< 0 || i2c
->op_clock
< I2C_MAX_FAST_MODE_PLUS_FREQ
)
421 return exynos5_i2c_set_timing(i2c
, true);
425 * exynos5_i2c_init: configures the controller for I2C functionality
426 * Programs I2C controller for Master mode operation
428 static void exynos5_i2c_init(struct exynos5_i2c
*i2c
)
430 u32 i2c_conf
= readl(i2c
->regs
+ HSI2C_CONF
);
431 u32 i2c_timeout
= readl(i2c
->regs
+ HSI2C_TIMEOUT
);
433 /* Clear to disable Timeout */
434 i2c_timeout
&= ~HSI2C_TIMEOUT_EN
;
435 writel(i2c_timeout
, i2c
->regs
+ HSI2C_TIMEOUT
);
437 writel((HSI2C_FUNC_MODE_I2C
| HSI2C_MASTER
),
438 i2c
->regs
+ HSI2C_CTL
);
439 writel(HSI2C_TRAILING_COUNT
, i2c
->regs
+ HSI2C_TRAILIG_CTL
);
441 if (i2c
->op_clock
>= I2C_MAX_FAST_MODE_PLUS_FREQ
) {
442 writel(HSI2C_MASTER_ID(MASTER_ID(i2c
->adap
.nr
)),
443 i2c
->regs
+ HSI2C_ADDR
);
444 i2c_conf
|= HSI2C_HS_MODE
;
447 writel(i2c_conf
| HSI2C_AUTO_MODE
, i2c
->regs
+ HSI2C_CONF
);
450 static void exynos5_i2c_reset(struct exynos5_i2c
*i2c
)
454 /* Set and clear the bit for reset */
455 i2c_ctl
= readl(i2c
->regs
+ HSI2C_CTL
);
456 i2c_ctl
|= HSI2C_SW_RST
;
457 writel(i2c_ctl
, i2c
->regs
+ HSI2C_CTL
);
459 i2c_ctl
= readl(i2c
->regs
+ HSI2C_CTL
);
460 i2c_ctl
&= ~HSI2C_SW_RST
;
461 writel(i2c_ctl
, i2c
->regs
+ HSI2C_CTL
);
463 /* We don't expect calculations to fail during the run */
464 exynos5_hsi2c_clock_setup(i2c
);
465 /* Initialize the configure registers */
466 exynos5_i2c_init(i2c
);
470 * exynos5_i2c_irq: top level IRQ servicing routine
472 * INT_STATUS registers gives the interrupt details. Further,
473 * FIFO_STATUS or TRANS_STATUS registers are to be check for detailed
476 static irqreturn_t
exynos5_i2c_irq(int irqno
, void *dev_id
)
478 struct exynos5_i2c
*i2c
= dev_id
;
479 u32 fifo_level
, int_status
, fifo_status
, trans_status
;
483 i2c
->state
= -EINVAL
;
485 spin_lock(&i2c
->lock
);
487 int_status
= readl(i2c
->regs
+ HSI2C_INT_STATUS
);
488 writel(int_status
, i2c
->regs
+ HSI2C_INT_STATUS
);
490 /* handle interrupt related to the transfer status */
491 switch (i2c
->variant
->hw
) {
492 case I2C_TYPE_EXYNOSAUTOV9
:
494 case I2C_TYPE_EXYNOS7
:
495 if (int_status
& HSI2C_INT_TRANS_DONE
) {
498 } else if (int_status
& HSI2C_INT_TRANS_ABORT
) {
499 dev_dbg(i2c
->dev
, "Deal with arbitration lose\n");
500 i2c
->state
= -EAGAIN
;
502 } else if (int_status
& HSI2C_INT_NO_DEV_ACK
) {
503 dev_dbg(i2c
->dev
, "No ACK from device\n");
506 } else if (int_status
& HSI2C_INT_NO_DEV
) {
507 dev_dbg(i2c
->dev
, "No device\n");
510 } else if (int_status
& HSI2C_INT_TIMEOUT
) {
511 dev_dbg(i2c
->dev
, "Accessing device timed out\n");
512 i2c
->state
= -ETIMEDOUT
;
517 case I2C_TYPE_EXYNOS5
:
518 if (!(int_status
& HSI2C_INT_I2C
))
521 trans_status
= readl(i2c
->regs
+ HSI2C_TRANS_STATUS
);
522 if (trans_status
& HSI2C_NO_DEV_ACK
) {
523 dev_dbg(i2c
->dev
, "No ACK from device\n");
526 } else if (trans_status
& HSI2C_NO_DEV
) {
527 dev_dbg(i2c
->dev
, "No device\n");
530 } else if (trans_status
& HSI2C_TRANS_ABORT
) {
531 dev_dbg(i2c
->dev
, "Deal with arbitration lose\n");
532 i2c
->state
= -EAGAIN
;
534 } else if (trans_status
& HSI2C_TIMEOUT_AUTO
) {
535 dev_dbg(i2c
->dev
, "Accessing device timed out\n");
536 i2c
->state
= -ETIMEDOUT
;
538 } else if (trans_status
& HSI2C_TRANS_DONE
) {
546 if ((i2c
->msg
->flags
& I2C_M_RD
) && (int_status
&
547 (HSI2C_INT_TRAILING
| HSI2C_INT_RX_ALMOSTFULL
))) {
548 fifo_status
= readl(i2c
->regs
+ HSI2C_FIFO_STATUS
);
549 fifo_level
= HSI2C_RX_FIFO_LVL(fifo_status
);
550 len
= min(fifo_level
, i2c
->msg
->len
- i2c
->msg_ptr
);
553 byte
= (unsigned char)
554 readl(i2c
->regs
+ HSI2C_RX_DATA
);
555 i2c
->msg
->buf
[i2c
->msg_ptr
++] = byte
;
559 } else if (int_status
& HSI2C_INT_TX_ALMOSTEMPTY
) {
560 fifo_status
= readl(i2c
->regs
+ HSI2C_FIFO_STATUS
);
561 fifo_level
= HSI2C_TX_FIFO_LVL(fifo_status
);
563 len
= i2c
->variant
->fifo_depth
- fifo_level
;
564 if (len
> (i2c
->msg
->len
- i2c
->msg_ptr
)) {
565 u32 int_en
= readl(i2c
->regs
+ HSI2C_INT_ENABLE
);
567 int_en
&= ~HSI2C_INT_TX_ALMOSTEMPTY_EN
;
568 writel(int_en
, i2c
->regs
+ HSI2C_INT_ENABLE
);
569 len
= i2c
->msg
->len
- i2c
->msg_ptr
;
573 byte
= i2c
->msg
->buf
[i2c
->msg_ptr
++];
574 writel(byte
, i2c
->regs
+ HSI2C_TX_DATA
);
581 if ((i2c
->trans_done
&& (i2c
->msg
->len
== i2c
->msg_ptr
)) ||
583 writel(0, i2c
->regs
+ HSI2C_INT_ENABLE
);
584 exynos5_i2c_clr_pend_irq(i2c
);
585 complete(&i2c
->msg_complete
);
588 spin_unlock(&i2c
->lock
);
594 * exynos5_i2c_wait_bus_idle
596 * Wait for the bus to go idle, indicated by the MASTER_BUSY bit being
599 * Returns -EBUSY if the bus cannot be bought to idle
601 static int exynos5_i2c_wait_bus_idle(struct exynos5_i2c
*i2c
)
603 unsigned long stop_time
;
606 /* wait for 100 milli seconds for the bus to be idle */
607 stop_time
= jiffies
+ msecs_to_jiffies(100) + 1;
609 trans_status
= readl(i2c
->regs
+ HSI2C_TRANS_STATUS
);
610 if (!(trans_status
& HSI2C_MASTER_BUSY
))
613 usleep_range(50, 200);
614 } while (time_before(jiffies
, stop_time
));
619 static void exynos5_i2c_bus_recover(struct exynos5_i2c
*i2c
)
623 val
= readl(i2c
->regs
+ HSI2C_CTL
) | HSI2C_RXCHON
;
624 writel(val
, i2c
->regs
+ HSI2C_CTL
);
625 val
= readl(i2c
->regs
+ HSI2C_CONF
) & ~HSI2C_AUTO_MODE
;
626 writel(val
, i2c
->regs
+ HSI2C_CONF
);
629 * Specification says master should send nine clock pulses. It can be
630 * emulated by sending manual read command (nine pulses for read eight
631 * bits + one pulse for NACK).
633 writel(HSI2C_CMD_READ_DATA
, i2c
->regs
+ HSI2C_MANUAL_CMD
);
634 exynos5_i2c_wait_bus_idle(i2c
);
635 writel(HSI2C_CMD_SEND_STOP
, i2c
->regs
+ HSI2C_MANUAL_CMD
);
636 exynos5_i2c_wait_bus_idle(i2c
);
638 val
= readl(i2c
->regs
+ HSI2C_CTL
) & ~HSI2C_RXCHON
;
639 writel(val
, i2c
->regs
+ HSI2C_CTL
);
640 val
= readl(i2c
->regs
+ HSI2C_CONF
) | HSI2C_AUTO_MODE
;
641 writel(val
, i2c
->regs
+ HSI2C_CONF
);
644 static void exynos5_i2c_bus_check(struct exynos5_i2c
*i2c
)
646 unsigned long timeout
;
648 if (i2c
->variant
->hw
== I2C_TYPE_EXYNOS5
)
652 * HSI2C_MASTER_ST_LOSE state (in Exynos7 and ExynosAutoV9 variants)
653 * before transaction indicates that bus is stuck (SDA is low).
654 * In such case bus recovery can be performed.
656 timeout
= jiffies
+ msecs_to_jiffies(100);
658 u32 st
= readl(i2c
->regs
+ HSI2C_TRANS_STATUS
);
660 if ((st
& HSI2C_MASTER_ST_MASK
) != HSI2C_MASTER_ST_LOSE
)
663 if (time_is_before_jiffies(timeout
))
666 exynos5_i2c_bus_recover(i2c
);
671 * exynos5_i2c_message_start: Configures the bus and starts the xfer
672 * i2c: struct exynos5_i2c pointer for the current bus
673 * stop: Enables stop after transfer if set. Set for last transfer of
674 * in the list of messages.
676 * Configures the bus for read/write function
677 * Sets chip address to talk to, message length to be sent.
678 * Enables appropriate interrupts and sends start xfer command.
680 static void exynos5_i2c_message_start(struct exynos5_i2c
*i2c
, int stop
)
684 u32 i2c_auto_conf
= 0;
688 unsigned short trig_lvl
;
690 if (i2c
->variant
->hw
== I2C_TYPE_EXYNOS5
)
691 int_en
|= HSI2C_INT_I2C
;
693 int_en
|= HSI2C_INT_I2C_TRANS
;
695 i2c_ctl
= readl(i2c
->regs
+ HSI2C_CTL
);
696 i2c_ctl
&= ~(HSI2C_TXCHON
| HSI2C_RXCHON
);
697 fifo_ctl
= HSI2C_RXFIFO_EN
| HSI2C_TXFIFO_EN
;
699 if (i2c
->msg
->flags
& I2C_M_RD
) {
700 i2c_ctl
|= HSI2C_RXCHON
;
702 i2c_auto_conf
|= HSI2C_READ_WRITE
;
704 trig_lvl
= (i2c
->msg
->len
> i2c
->variant
->fifo_depth
) ?
705 (i2c
->variant
->fifo_depth
* 3 / 4) : i2c
->msg
->len
;
706 fifo_ctl
|= HSI2C_RXFIFO_TRIGGER_LEVEL(trig_lvl
);
708 int_en
|= (HSI2C_INT_RX_ALMOSTFULL_EN
|
709 HSI2C_INT_TRAILING_EN
);
711 i2c_ctl
|= HSI2C_TXCHON
;
713 trig_lvl
= (i2c
->msg
->len
> i2c
->variant
->fifo_depth
) ?
714 (i2c
->variant
->fifo_depth
* 1 / 4) : i2c
->msg
->len
;
715 fifo_ctl
|= HSI2C_TXFIFO_TRIGGER_LEVEL(trig_lvl
);
717 int_en
|= HSI2C_INT_TX_ALMOSTEMPTY_EN
;
720 i2c_addr
= HSI2C_SLV_ADDR_MAS(i2c
->msg
->addr
);
722 if (i2c
->op_clock
>= I2C_MAX_FAST_MODE_PLUS_FREQ
)
723 i2c_addr
|= HSI2C_MASTER_ID(MASTER_ID(i2c
->adap
.nr
));
725 writel(i2c_addr
, i2c
->regs
+ HSI2C_ADDR
);
727 writel(fifo_ctl
, i2c
->regs
+ HSI2C_FIFO_CTL
);
728 writel(i2c_ctl
, i2c
->regs
+ HSI2C_CTL
);
730 exynos5_i2c_bus_check(i2c
);
733 * Enable interrupts before starting the transfer so that we don't
734 * miss any INT_I2C interrupts.
736 spin_lock_irqsave(&i2c
->lock
, flags
);
737 writel(int_en
, i2c
->regs
+ HSI2C_INT_ENABLE
);
740 i2c_auto_conf
|= HSI2C_STOP_AFTER_TRANS
;
741 i2c_auto_conf
|= i2c
->msg
->len
;
742 i2c_auto_conf
|= HSI2C_MASTER_RUN
;
743 writel(i2c_auto_conf
, i2c
->regs
+ HSI2C_AUTO_CONF
);
744 spin_unlock_irqrestore(&i2c
->lock
, flags
);
747 static bool exynos5_i2c_poll_irqs_timeout(struct exynos5_i2c
*i2c
,
748 unsigned long timeout
)
750 unsigned long time_left
= jiffies
+ timeout
;
752 while (time_before(jiffies
, time_left
) &&
753 !((i2c
->trans_done
&& (i2c
->msg
->len
== i2c
->msg_ptr
)) ||
755 while (readl(i2c
->regs
+ HSI2C_INT_ENABLE
) &
756 readl(i2c
->regs
+ HSI2C_INT_STATUS
))
757 exynos5_i2c_irq(i2c
->irq
, i2c
);
758 usleep_range(100, 200);
760 return time_before(jiffies
, time_left
);
763 static int exynos5_i2c_xfer_msg(struct exynos5_i2c
*i2c
,
764 struct i2c_msg
*msgs
, int stop
)
766 unsigned long time_left
;
773 reinit_completion(&i2c
->msg_complete
);
775 exynos5_i2c_message_start(i2c
, stop
);
778 time_left
= wait_for_completion_timeout(&i2c
->msg_complete
,
779 EXYNOS5_I2C_TIMEOUT
);
781 time_left
= exynos5_i2c_poll_irqs_timeout(i2c
,
782 EXYNOS5_I2C_TIMEOUT
);
790 * If this is the last message to be transfered (stop == 1)
791 * Then check if the bus can be brought back to idle.
793 if (ret
== 0 && stop
)
794 ret
= exynos5_i2c_wait_bus_idle(i2c
);
797 exynos5_i2c_reset(i2c
);
798 if (ret
== -ETIMEDOUT
)
799 dev_warn(i2c
->dev
, "%s timeout\n",
800 (msgs
->flags
& I2C_M_RD
) ? "rx" : "tx");
803 /* Return the state as in interrupt routine */
807 static int exynos5_i2c_xfer(struct i2c_adapter
*adap
,
808 struct i2c_msg
*msgs
, int num
)
810 struct exynos5_i2c
*i2c
= adap
->algo_data
;
813 ret
= clk_enable(i2c
->pclk
);
817 ret
= clk_enable(i2c
->clk
);
821 for (i
= 0; i
< num
; ++i
) {
822 ret
= exynos5_i2c_xfer_msg(i2c
, msgs
+ i
, i
+ 1 == num
);
827 clk_disable(i2c
->clk
);
829 clk_disable(i2c
->pclk
);
834 static int exynos5_i2c_xfer_atomic(struct i2c_adapter
*adap
,
835 struct i2c_msg
*msgs
, int num
)
837 struct exynos5_i2c
*i2c
= adap
->algo_data
;
840 disable_irq(i2c
->irq
);
842 ret
= exynos5_i2c_xfer(adap
, msgs
, num
);
844 enable_irq(i2c
->irq
);
849 static u32
exynos5_i2c_func(struct i2c_adapter
*adap
)
851 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
854 static const struct i2c_algorithm exynos5_i2c_algorithm
= {
855 .master_xfer
= exynos5_i2c_xfer
,
856 .master_xfer_atomic
= exynos5_i2c_xfer_atomic
,
857 .functionality
= exynos5_i2c_func
,
860 static int exynos5_i2c_probe(struct platform_device
*pdev
)
862 struct device_node
*np
= pdev
->dev
.of_node
;
863 struct exynos5_i2c
*i2c
;
866 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(struct exynos5_i2c
), GFP_KERNEL
);
870 if (of_property_read_u32(np
, "clock-frequency", &i2c
->op_clock
))
871 i2c
->op_clock
= I2C_MAX_STANDARD_MODE_FREQ
;
873 strscpy(i2c
->adap
.name
, "exynos5-i2c", sizeof(i2c
->adap
.name
));
874 i2c
->adap
.owner
= THIS_MODULE
;
875 i2c
->adap
.algo
= &exynos5_i2c_algorithm
;
876 i2c
->adap
.retries
= 3;
878 i2c
->dev
= &pdev
->dev
;
879 i2c
->clk
= devm_clk_get(&pdev
->dev
, "hsi2c");
880 if (IS_ERR(i2c
->clk
)) {
881 dev_err(&pdev
->dev
, "cannot get clock\n");
885 i2c
->pclk
= devm_clk_get_optional(&pdev
->dev
, "hsi2c_pclk");
886 if (IS_ERR(i2c
->pclk
)) {
887 return dev_err_probe(&pdev
->dev
, PTR_ERR(i2c
->pclk
),
891 ret
= clk_prepare_enable(i2c
->pclk
);
895 ret
= clk_prepare_enable(i2c
->clk
);
899 i2c
->regs
= devm_platform_ioremap_resource(pdev
, 0);
900 if (IS_ERR(i2c
->regs
)) {
901 ret
= PTR_ERR(i2c
->regs
);
905 i2c
->adap
.dev
.of_node
= np
;
906 i2c
->adap
.algo_data
= i2c
;
907 i2c
->adap
.dev
.parent
= &pdev
->dev
;
909 /* Clear pending interrupts from u-boot or misc causes */
910 exynos5_i2c_clr_pend_irq(i2c
);
912 spin_lock_init(&i2c
->lock
);
913 init_completion(&i2c
->msg_complete
);
915 i2c
->irq
= ret
= platform_get_irq(pdev
, 0);
919 ret
= devm_request_irq(&pdev
->dev
, i2c
->irq
, exynos5_i2c_irq
,
920 IRQF_NO_SUSPEND
, dev_name(&pdev
->dev
), i2c
);
922 dev_err(&pdev
->dev
, "cannot request HS-I2C IRQ %d\n", i2c
->irq
);
926 i2c
->variant
= of_device_get_match_data(&pdev
->dev
);
928 ret
= exynos5_hsi2c_clock_setup(i2c
);
932 exynos5_i2c_reset(i2c
);
934 ret
= i2c_add_adapter(&i2c
->adap
);
938 platform_set_drvdata(pdev
, i2c
);
940 clk_disable(i2c
->clk
);
941 clk_disable(i2c
->pclk
);
946 clk_disable_unprepare(i2c
->clk
);
949 clk_disable_unprepare(i2c
->pclk
);
953 static void exynos5_i2c_remove(struct platform_device
*pdev
)
955 struct exynos5_i2c
*i2c
= platform_get_drvdata(pdev
);
957 i2c_del_adapter(&i2c
->adap
);
959 clk_unprepare(i2c
->clk
);
960 clk_unprepare(i2c
->pclk
);
963 static int exynos5_i2c_suspend_noirq(struct device
*dev
)
965 struct exynos5_i2c
*i2c
= dev_get_drvdata(dev
);
967 i2c_mark_adapter_suspended(&i2c
->adap
);
968 clk_unprepare(i2c
->clk
);
969 clk_unprepare(i2c
->pclk
);
974 static int exynos5_i2c_resume_noirq(struct device
*dev
)
976 struct exynos5_i2c
*i2c
= dev_get_drvdata(dev
);
979 ret
= clk_prepare_enable(i2c
->pclk
);
983 ret
= clk_prepare_enable(i2c
->clk
);
987 ret
= exynos5_hsi2c_clock_setup(i2c
);
991 exynos5_i2c_init(i2c
);
992 clk_disable(i2c
->clk
);
993 clk_disable(i2c
->pclk
);
994 i2c_mark_adapter_resumed(&i2c
->adap
);
999 clk_disable_unprepare(i2c
->clk
);
1001 clk_disable_unprepare(i2c
->pclk
);
1005 static const struct dev_pm_ops exynos5_i2c_dev_pm_ops
= {
1006 NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos5_i2c_suspend_noirq
,
1007 exynos5_i2c_resume_noirq
)
1010 static struct platform_driver exynos5_i2c_driver
= {
1011 .probe
= exynos5_i2c_probe
,
1012 .remove
= exynos5_i2c_remove
,
1014 .name
= "exynos5-hsi2c",
1015 .pm
= pm_sleep_ptr(&exynos5_i2c_dev_pm_ops
),
1016 .of_match_table
= exynos5_i2c_match
,
1020 module_platform_driver(exynos5_i2c_driver
);
1022 MODULE_DESCRIPTION("Exynos5 HS-I2C Bus driver");
1023 MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
1024 MODULE_AUTHOR("Taekgyun Ko <taeggyun.ko@samsung.com>");
1025 MODULE_LICENSE("GPL v2");