1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2014 Linaro Ltd.
4 * Copyright (c) 2014 Hisilicon Limited.
6 * Now only support 7 bit address.
10 #include <linux/delay.h>
11 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
20 #define HIX5I2C_CTRL 0x00
21 #define HIX5I2C_COM 0x04
22 #define HIX5I2C_ICR 0x08
23 #define HIX5I2C_SR 0x0c
24 #define HIX5I2C_SCL_H 0x10
25 #define HIX5I2C_SCL_L 0x14
26 #define HIX5I2C_TXR 0x18
27 #define HIX5I2C_RXR 0x1c
30 #define I2C_ENABLE BIT(8)
31 #define I2C_UNMASK_TOTAL BIT(7)
32 #define I2C_UNMASK_START BIT(6)
33 #define I2C_UNMASK_END BIT(5)
34 #define I2C_UNMASK_SEND BIT(4)
35 #define I2C_UNMASK_RECEIVE BIT(3)
36 #define I2C_UNMASK_ACK BIT(2)
37 #define I2C_UNMASK_ARBITRATE BIT(1)
38 #define I2C_UNMASK_OVER BIT(0)
39 #define I2C_UNMASK_ALL (I2C_UNMASK_ACK | I2C_UNMASK_OVER)
42 #define I2C_NO_ACK BIT(4)
43 #define I2C_START BIT(3)
44 #define I2C_READ BIT(2)
45 #define I2C_WRITE BIT(1)
46 #define I2C_STOP BIT(0)
49 #define I2C_CLEAR_START BIT(6)
50 #define I2C_CLEAR_END BIT(5)
51 #define I2C_CLEAR_SEND BIT(4)
52 #define I2C_CLEAR_RECEIVE BIT(3)
53 #define I2C_CLEAR_ACK BIT(2)
54 #define I2C_CLEAR_ARBITRATE BIT(1)
55 #define I2C_CLEAR_OVER BIT(0)
56 #define I2C_CLEAR_ALL (I2C_CLEAR_START | I2C_CLEAR_END | \
57 I2C_CLEAR_SEND | I2C_CLEAR_RECEIVE | \
58 I2C_CLEAR_ACK | I2C_CLEAR_ARBITRATE | \
62 #define I2C_BUSY BIT(7)
63 #define I2C_START_INTR BIT(6)
64 #define I2C_END_INTR BIT(5)
65 #define I2C_SEND_INTR BIT(4)
66 #define I2C_RECEIVE_INTR BIT(3)
67 #define I2C_ACK_INTR BIT(2)
68 #define I2C_ARBITRATE_INTR BIT(1)
69 #define I2C_OVER_INTR BIT(0)
71 #define HIX5I2C_MAX_FREQ 400000 /* 400k */
73 enum hix5hd2_i2c_state
{
74 HIX5I2C_STAT_RW_ERR
= -1,
77 HIX5I2C_STAT_SND_STOP
,
78 HIX5I2C_STAT_RW_SUCCESS
,
81 struct hix5hd2_i2c_priv
{
82 struct i2c_adapter adap
;
84 struct completion msg_complete
;
91 spinlock_t lock
; /* IRQ synchronization */
94 enum hix5hd2_i2c_state state
;
97 static u32
hix5hd2_i2c_clr_pend_irq(struct hix5hd2_i2c_priv
*priv
)
99 u32 val
= readl_relaxed(priv
->regs
+ HIX5I2C_SR
);
101 writel_relaxed(val
, priv
->regs
+ HIX5I2C_ICR
);
106 static void hix5hd2_i2c_clr_all_irq(struct hix5hd2_i2c_priv
*priv
)
108 writel_relaxed(I2C_CLEAR_ALL
, priv
->regs
+ HIX5I2C_ICR
);
111 static void hix5hd2_i2c_disable_irq(struct hix5hd2_i2c_priv
*priv
)
113 writel_relaxed(0, priv
->regs
+ HIX5I2C_CTRL
);
116 static void hix5hd2_i2c_enable_irq(struct hix5hd2_i2c_priv
*priv
)
118 writel_relaxed(I2C_ENABLE
| I2C_UNMASK_TOTAL
| I2C_UNMASK_ALL
,
119 priv
->regs
+ HIX5I2C_CTRL
);
122 static void hix5hd2_i2c_drv_setrate(struct hix5hd2_i2c_priv
*priv
)
127 /* close all i2c interrupt */
128 val
= readl_relaxed(priv
->regs
+ HIX5I2C_CTRL
);
129 writel_relaxed(val
& (~I2C_UNMASK_TOTAL
), priv
->regs
+ HIX5I2C_CTRL
);
132 sysclock
= clk_get_rate(priv
->clk
);
133 scl
= (sysclock
/ (rate
* 2)) / 2 - 1;
134 writel_relaxed(scl
, priv
->regs
+ HIX5I2C_SCL_H
);
135 writel_relaxed(scl
, priv
->regs
+ HIX5I2C_SCL_L
);
137 /* restore original interrupt*/
138 writel_relaxed(val
, priv
->regs
+ HIX5I2C_CTRL
);
140 dev_dbg(priv
->dev
, "%s: sysclock=%d, rate=%d, scl=%d\n",
141 __func__
, sysclock
, rate
, scl
);
144 static void hix5hd2_i2c_init(struct hix5hd2_i2c_priv
*priv
)
146 hix5hd2_i2c_disable_irq(priv
);
147 hix5hd2_i2c_drv_setrate(priv
);
148 hix5hd2_i2c_clr_all_irq(priv
);
149 hix5hd2_i2c_enable_irq(priv
);
152 static void hix5hd2_i2c_reset(struct hix5hd2_i2c_priv
*priv
)
154 clk_disable_unprepare(priv
->clk
);
156 clk_prepare_enable(priv
->clk
);
157 hix5hd2_i2c_init(priv
);
160 static int hix5hd2_i2c_wait_bus_idle(struct hix5hd2_i2c_priv
*priv
)
162 unsigned long stop_time
;
165 /* wait for 100 milli seconds for the bus to be idle */
166 stop_time
= jiffies
+ msecs_to_jiffies(100);
168 int_status
= hix5hd2_i2c_clr_pend_irq(priv
);
169 if (!(int_status
& I2C_BUSY
))
172 usleep_range(50, 200);
173 } while (time_before(jiffies
, stop_time
));
178 static void hix5hd2_rw_over(struct hix5hd2_i2c_priv
*priv
)
180 if (priv
->state
== HIX5I2C_STAT_SND_STOP
)
181 dev_dbg(priv
->dev
, "%s: rw and send stop over\n", __func__
);
183 dev_dbg(priv
->dev
, "%s: have not data to send\n", __func__
);
185 priv
->state
= HIX5I2C_STAT_RW_SUCCESS
;
189 static void hix5hd2_rw_handle_stop(struct hix5hd2_i2c_priv
*priv
)
192 priv
->state
= HIX5I2C_STAT_SND_STOP
;
193 writel_relaxed(I2C_STOP
, priv
->regs
+ HIX5I2C_COM
);
195 hix5hd2_rw_over(priv
);
199 static void hix5hd2_read_handle(struct hix5hd2_i2c_priv
*priv
)
201 if (priv
->msg_len
== 1) {
202 /* the last byte don't need send ACK */
203 writel_relaxed(I2C_READ
| I2C_NO_ACK
, priv
->regs
+ HIX5I2C_COM
);
204 } else if (priv
->msg_len
> 1) {
205 /* if i2c master receive data will send ACK */
206 writel_relaxed(I2C_READ
, priv
->regs
+ HIX5I2C_COM
);
208 hix5hd2_rw_handle_stop(priv
);
212 static void hix5hd2_write_handle(struct hix5hd2_i2c_priv
*priv
)
216 if (priv
->msg_len
> 0) {
217 data
= priv
->msg
->buf
[priv
->msg_idx
++];
218 writel_relaxed(data
, priv
->regs
+ HIX5I2C_TXR
);
219 writel_relaxed(I2C_WRITE
, priv
->regs
+ HIX5I2C_COM
);
221 hix5hd2_rw_handle_stop(priv
);
225 static int hix5hd2_rw_preprocess(struct hix5hd2_i2c_priv
*priv
)
229 if (priv
->state
== HIX5I2C_STAT_INIT
) {
230 priv
->state
= HIX5I2C_STAT_RW
;
231 } else if (priv
->state
== HIX5I2C_STAT_RW
) {
232 if (priv
->msg
->flags
& I2C_M_RD
) {
233 data
= readl_relaxed(priv
->regs
+ HIX5I2C_RXR
);
234 priv
->msg
->buf
[priv
->msg_idx
++] = data
;
238 dev_dbg(priv
->dev
, "%s: error: priv->state = %d, msg_len = %d\n",
239 __func__
, priv
->state
, priv
->msg_len
);
245 static irqreturn_t
hix5hd2_i2c_irq(int irqno
, void *dev_id
)
247 struct hix5hd2_i2c_priv
*priv
= dev_id
;
251 spin_lock(&priv
->lock
);
253 int_status
= hix5hd2_i2c_clr_pend_irq(priv
);
256 if (int_status
& I2C_ARBITRATE_INTR
) {
258 dev_dbg(priv
->dev
, "ARB bus loss\n");
260 priv
->state
= HIX5I2C_STAT_RW_ERR
;
262 } else if (int_status
& I2C_ACK_INTR
) {
264 dev_dbg(priv
->dev
, "No ACK from device\n");
266 priv
->state
= HIX5I2C_STAT_RW_ERR
;
270 if (int_status
& I2C_OVER_INTR
) {
271 if (priv
->msg_len
> 0) {
272 ret
= hix5hd2_rw_preprocess(priv
);
275 priv
->state
= HIX5I2C_STAT_RW_ERR
;
278 if (priv
->msg
->flags
& I2C_M_RD
)
279 hix5hd2_read_handle(priv
);
281 hix5hd2_write_handle(priv
);
283 hix5hd2_rw_over(priv
);
288 if ((priv
->state
== HIX5I2C_STAT_RW_SUCCESS
&&
289 priv
->msg
->len
== priv
->msg_idx
) ||
290 (priv
->state
== HIX5I2C_STAT_RW_ERR
)) {
291 hix5hd2_i2c_disable_irq(priv
);
292 hix5hd2_i2c_clr_pend_irq(priv
);
293 complete(&priv
->msg_complete
);
296 spin_unlock(&priv
->lock
);
301 static void hix5hd2_i2c_message_start(struct hix5hd2_i2c_priv
*priv
, int stop
)
305 spin_lock_irqsave(&priv
->lock
, flags
);
306 hix5hd2_i2c_clr_all_irq(priv
);
307 hix5hd2_i2c_enable_irq(priv
);
309 writel_relaxed(i2c_8bit_addr_from_msg(priv
->msg
),
310 priv
->regs
+ HIX5I2C_TXR
);
312 writel_relaxed(I2C_WRITE
| I2C_START
, priv
->regs
+ HIX5I2C_COM
);
313 spin_unlock_irqrestore(&priv
->lock
, flags
);
316 static int hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv
*priv
,
317 struct i2c_msg
*msgs
, int stop
)
319 unsigned long timeout
;
324 priv
->msg_len
= priv
->msg
->len
;
327 priv
->state
= HIX5I2C_STAT_INIT
;
329 reinit_completion(&priv
->msg_complete
);
330 hix5hd2_i2c_message_start(priv
, stop
);
332 timeout
= wait_for_completion_timeout(&priv
->msg_complete
,
335 priv
->state
= HIX5I2C_STAT_RW_ERR
;
336 priv
->err
= -ETIMEDOUT
;
337 dev_warn(priv
->dev
, "%s timeout=%d\n",
338 msgs
->flags
& I2C_M_RD
? "rx" : "tx",
344 * If this is the last message to be transfered (stop == 1)
345 * Then check if the bus can be brought back to idle.
347 if (priv
->state
== HIX5I2C_STAT_RW_SUCCESS
&& stop
)
348 ret
= hix5hd2_i2c_wait_bus_idle(priv
);
351 hix5hd2_i2c_reset(priv
);
356 static int hix5hd2_i2c_xfer(struct i2c_adapter
*adap
,
357 struct i2c_msg
*msgs
, int num
)
359 struct hix5hd2_i2c_priv
*priv
= i2c_get_adapdata(adap
);
362 pm_runtime_get_sync(priv
->dev
);
364 for (i
= 0; i
< num
; i
++, msgs
++) {
365 stop
= (i
== num
- 1);
366 ret
= hix5hd2_i2c_xfer_msg(priv
, msgs
, stop
);
374 pm_runtime_mark_last_busy(priv
->dev
);
375 pm_runtime_put_autosuspend(priv
->dev
);
379 static u32
hix5hd2_i2c_func(struct i2c_adapter
*adap
)
381 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
384 static const struct i2c_algorithm hix5hd2_i2c_algorithm
= {
385 .master_xfer
= hix5hd2_i2c_xfer
,
386 .functionality
= hix5hd2_i2c_func
,
389 static int hix5hd2_i2c_probe(struct platform_device
*pdev
)
391 struct device_node
*np
= pdev
->dev
.of_node
;
392 struct hix5hd2_i2c_priv
*priv
;
393 struct resource
*mem
;
397 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
401 if (of_property_read_u32(np
, "clock-frequency", &freq
)) {
402 /* use 100k as default value */
405 if (freq
> HIX5I2C_MAX_FREQ
) {
406 priv
->freq
= HIX5I2C_MAX_FREQ
;
407 dev_warn(priv
->dev
, "use max freq %d instead\n",
414 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
415 priv
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
416 if (IS_ERR(priv
->regs
))
417 return PTR_ERR(priv
->regs
);
419 irq
= platform_get_irq(pdev
, 0);
421 dev_err(&pdev
->dev
, "cannot find HS-I2C IRQ\n");
425 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
426 if (IS_ERR(priv
->clk
)) {
427 dev_err(&pdev
->dev
, "cannot get clock\n");
428 return PTR_ERR(priv
->clk
);
430 clk_prepare_enable(priv
->clk
);
432 strlcpy(priv
->adap
.name
, "hix5hd2-i2c", sizeof(priv
->adap
.name
));
433 priv
->dev
= &pdev
->dev
;
434 priv
->adap
.owner
= THIS_MODULE
;
435 priv
->adap
.algo
= &hix5hd2_i2c_algorithm
;
436 priv
->adap
.retries
= 3;
437 priv
->adap
.dev
.of_node
= np
;
438 priv
->adap
.algo_data
= priv
;
439 priv
->adap
.dev
.parent
= &pdev
->dev
;
440 i2c_set_adapdata(&priv
->adap
, priv
);
441 platform_set_drvdata(pdev
, priv
);
442 spin_lock_init(&priv
->lock
);
443 init_completion(&priv
->msg_complete
);
445 hix5hd2_i2c_init(priv
);
447 ret
= devm_request_irq(&pdev
->dev
, irq
, hix5hd2_i2c_irq
,
448 IRQF_NO_SUSPEND
, dev_name(&pdev
->dev
), priv
);
450 dev_err(&pdev
->dev
, "cannot request HS-I2C IRQ %d\n", irq
);
454 pm_runtime_set_autosuspend_delay(priv
->dev
, MSEC_PER_SEC
);
455 pm_runtime_use_autosuspend(priv
->dev
);
456 pm_runtime_set_active(priv
->dev
);
457 pm_runtime_enable(priv
->dev
);
459 ret
= i2c_add_adapter(&priv
->adap
);
466 pm_runtime_disable(priv
->dev
);
467 pm_runtime_set_suspended(priv
->dev
);
469 clk_disable_unprepare(priv
->clk
);
473 static int hix5hd2_i2c_remove(struct platform_device
*pdev
)
475 struct hix5hd2_i2c_priv
*priv
= platform_get_drvdata(pdev
);
477 i2c_del_adapter(&priv
->adap
);
478 pm_runtime_disable(priv
->dev
);
479 pm_runtime_set_suspended(priv
->dev
);
485 static int hix5hd2_i2c_runtime_suspend(struct device
*dev
)
487 struct hix5hd2_i2c_priv
*priv
= dev_get_drvdata(dev
);
489 clk_disable_unprepare(priv
->clk
);
494 static int hix5hd2_i2c_runtime_resume(struct device
*dev
)
496 struct hix5hd2_i2c_priv
*priv
= dev_get_drvdata(dev
);
498 clk_prepare_enable(priv
->clk
);
499 hix5hd2_i2c_init(priv
);
505 static const struct dev_pm_ops hix5hd2_i2c_pm_ops
= {
506 SET_RUNTIME_PM_OPS(hix5hd2_i2c_runtime_suspend
,
507 hix5hd2_i2c_runtime_resume
,
511 static const struct of_device_id hix5hd2_i2c_match
[] = {
512 { .compatible
= "hisilicon,hix5hd2-i2c" },
515 MODULE_DEVICE_TABLE(of
, hix5hd2_i2c_match
);
517 static struct platform_driver hix5hd2_i2c_driver
= {
518 .probe
= hix5hd2_i2c_probe
,
519 .remove
= hix5hd2_i2c_remove
,
521 .name
= "hix5hd2-i2c",
522 .pm
= &hix5hd2_i2c_pm_ops
,
523 .of_match_table
= hix5hd2_i2c_match
,
527 module_platform_driver(hix5hd2_i2c_driver
);
529 MODULE_DESCRIPTION("Hix5hd2 I2C Bus driver");
530 MODULE_AUTHOR("Wei Yan <sledge.yanwei@huawei.com>");
531 MODULE_LICENSE("GPL");
532 MODULE_ALIAS("platform:hix5hd2-i2c");