2 * Copyright (c) 2014 Linaro Ltd.
3 * Copyright (c) 2014 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Now only support 7 bit address.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
24 #define HIX5I2C_CTRL 0x00
25 #define HIX5I2C_COM 0x04
26 #define HIX5I2C_ICR 0x08
27 #define HIX5I2C_SR 0x0c
28 #define HIX5I2C_SCL_H 0x10
29 #define HIX5I2C_SCL_L 0x14
30 #define HIX5I2C_TXR 0x18
31 #define HIX5I2C_RXR 0x1c
34 #define I2C_ENABLE BIT(8)
35 #define I2C_UNMASK_TOTAL BIT(7)
36 #define I2C_UNMASK_START BIT(6)
37 #define I2C_UNMASK_END BIT(5)
38 #define I2C_UNMASK_SEND BIT(4)
39 #define I2C_UNMASK_RECEIVE BIT(3)
40 #define I2C_UNMASK_ACK BIT(2)
41 #define I2C_UNMASK_ARBITRATE BIT(1)
42 #define I2C_UNMASK_OVER BIT(0)
43 #define I2C_UNMASK_ALL (I2C_UNMASK_ACK | I2C_UNMASK_OVER)
46 #define I2C_NO_ACK BIT(4)
47 #define I2C_START BIT(3)
48 #define I2C_READ BIT(2)
49 #define I2C_WRITE BIT(1)
50 #define I2C_STOP BIT(0)
53 #define I2C_CLEAR_START BIT(6)
54 #define I2C_CLEAR_END BIT(5)
55 #define I2C_CLEAR_SEND BIT(4)
56 #define I2C_CLEAR_RECEIVE BIT(3)
57 #define I2C_CLEAR_ACK BIT(2)
58 #define I2C_CLEAR_ARBITRATE BIT(1)
59 #define I2C_CLEAR_OVER BIT(0)
60 #define I2C_CLEAR_ALL (I2C_CLEAR_START | I2C_CLEAR_END | \
61 I2C_CLEAR_SEND | I2C_CLEAR_RECEIVE | \
62 I2C_CLEAR_ACK | I2C_CLEAR_ARBITRATE | \
66 #define I2C_BUSY BIT(7)
67 #define I2C_START_INTR BIT(6)
68 #define I2C_END_INTR BIT(5)
69 #define I2C_SEND_INTR BIT(4)
70 #define I2C_RECEIVE_INTR BIT(3)
71 #define I2C_ACK_INTR BIT(2)
72 #define I2C_ARBITRATE_INTR BIT(1)
73 #define I2C_OVER_INTR BIT(0)
75 #define HIX5I2C_MAX_FREQ 400000 /* 400k */
76 #define HIX5I2C_READ_OPERATION 0x01
78 enum hix5hd2_i2c_state
{
79 HIX5I2C_STAT_RW_ERR
= -1,
82 HIX5I2C_STAT_SND_STOP
,
83 HIX5I2C_STAT_RW_SUCCESS
,
86 struct hix5hd2_i2c_priv
{
87 struct i2c_adapter adap
;
89 struct completion msg_complete
;
96 spinlock_t lock
; /* IRQ synchronization */
99 enum hix5hd2_i2c_state state
;
102 static u32
hix5hd2_i2c_clr_pend_irq(struct hix5hd2_i2c_priv
*priv
)
104 u32 val
= readl_relaxed(priv
->regs
+ HIX5I2C_SR
);
106 writel_relaxed(val
, priv
->regs
+ HIX5I2C_ICR
);
111 static void hix5hd2_i2c_clr_all_irq(struct hix5hd2_i2c_priv
*priv
)
113 writel_relaxed(I2C_CLEAR_ALL
, priv
->regs
+ HIX5I2C_ICR
);
116 static void hix5hd2_i2c_disable_irq(struct hix5hd2_i2c_priv
*priv
)
118 writel_relaxed(0, priv
->regs
+ HIX5I2C_CTRL
);
121 static void hix5hd2_i2c_enable_irq(struct hix5hd2_i2c_priv
*priv
)
123 writel_relaxed(I2C_ENABLE
| I2C_UNMASK_TOTAL
| I2C_UNMASK_ALL
,
124 priv
->regs
+ HIX5I2C_CTRL
);
127 static void hix5hd2_i2c_drv_setrate(struct hix5hd2_i2c_priv
*priv
)
132 /* close all i2c interrupt */
133 val
= readl_relaxed(priv
->regs
+ HIX5I2C_CTRL
);
134 writel_relaxed(val
& (~I2C_UNMASK_TOTAL
), priv
->regs
+ HIX5I2C_CTRL
);
137 sysclock
= clk_get_rate(priv
->clk
);
138 scl
= (sysclock
/ (rate
* 2)) / 2 - 1;
139 writel_relaxed(scl
, priv
->regs
+ HIX5I2C_SCL_H
);
140 writel_relaxed(scl
, priv
->regs
+ HIX5I2C_SCL_L
);
142 /* restore original interrupt*/
143 writel_relaxed(val
, priv
->regs
+ HIX5I2C_CTRL
);
145 dev_dbg(priv
->dev
, "%s: sysclock=%d, rate=%d, scl=%d\n",
146 __func__
, sysclock
, rate
, scl
);
149 static void hix5hd2_i2c_init(struct hix5hd2_i2c_priv
*priv
)
151 hix5hd2_i2c_disable_irq(priv
);
152 hix5hd2_i2c_drv_setrate(priv
);
153 hix5hd2_i2c_clr_all_irq(priv
);
154 hix5hd2_i2c_enable_irq(priv
);
157 static void hix5hd2_i2c_reset(struct hix5hd2_i2c_priv
*priv
)
159 clk_disable_unprepare(priv
->clk
);
161 clk_prepare_enable(priv
->clk
);
162 hix5hd2_i2c_init(priv
);
165 static int hix5hd2_i2c_wait_bus_idle(struct hix5hd2_i2c_priv
*priv
)
167 unsigned long stop_time
;
170 /* wait for 100 milli seconds for the bus to be idle */
171 stop_time
= jiffies
+ msecs_to_jiffies(100);
173 int_status
= hix5hd2_i2c_clr_pend_irq(priv
);
174 if (!(int_status
& I2C_BUSY
))
177 usleep_range(50, 200);
178 } while (time_before(jiffies
, stop_time
));
183 static void hix5hd2_rw_over(struct hix5hd2_i2c_priv
*priv
)
185 if (priv
->state
== HIX5I2C_STAT_SND_STOP
)
186 dev_dbg(priv
->dev
, "%s: rw and send stop over\n", __func__
);
188 dev_dbg(priv
->dev
, "%s: have not data to send\n", __func__
);
190 priv
->state
= HIX5I2C_STAT_RW_SUCCESS
;
194 static void hix5hd2_rw_handle_stop(struct hix5hd2_i2c_priv
*priv
)
197 priv
->state
= HIX5I2C_STAT_SND_STOP
;
198 writel_relaxed(I2C_STOP
, priv
->regs
+ HIX5I2C_COM
);
200 hix5hd2_rw_over(priv
);
204 static void hix5hd2_read_handle(struct hix5hd2_i2c_priv
*priv
)
206 if (priv
->msg_len
== 1) {
207 /* the last byte don't need send ACK */
208 writel_relaxed(I2C_READ
| I2C_NO_ACK
, priv
->regs
+ HIX5I2C_COM
);
209 } else if (priv
->msg_len
> 1) {
210 /* if i2c master receive data will send ACK */
211 writel_relaxed(I2C_READ
, priv
->regs
+ HIX5I2C_COM
);
213 hix5hd2_rw_handle_stop(priv
);
217 static void hix5hd2_write_handle(struct hix5hd2_i2c_priv
*priv
)
221 if (priv
->msg_len
> 0) {
222 data
= priv
->msg
->buf
[priv
->msg_idx
++];
223 writel_relaxed(data
, priv
->regs
+ HIX5I2C_TXR
);
224 writel_relaxed(I2C_WRITE
, priv
->regs
+ HIX5I2C_COM
);
226 hix5hd2_rw_handle_stop(priv
);
230 static int hix5hd2_rw_preprocess(struct hix5hd2_i2c_priv
*priv
)
234 if (priv
->state
== HIX5I2C_STAT_INIT
) {
235 priv
->state
= HIX5I2C_STAT_RW
;
236 } else if (priv
->state
== HIX5I2C_STAT_RW
) {
237 if (priv
->msg
->flags
& I2C_M_RD
) {
238 data
= readl_relaxed(priv
->regs
+ HIX5I2C_RXR
);
239 priv
->msg
->buf
[priv
->msg_idx
++] = data
;
243 dev_dbg(priv
->dev
, "%s: error: priv->state = %d, msg_len = %d\n",
244 __func__
, priv
->state
, priv
->msg_len
);
250 static irqreturn_t
hix5hd2_i2c_irq(int irqno
, void *dev_id
)
252 struct hix5hd2_i2c_priv
*priv
= dev_id
;
256 spin_lock(&priv
->lock
);
258 int_status
= hix5hd2_i2c_clr_pend_irq(priv
);
261 if (int_status
& I2C_ARBITRATE_INTR
) {
263 dev_dbg(priv
->dev
, "ARB bus loss\n");
265 priv
->state
= HIX5I2C_STAT_RW_ERR
;
267 } else if (int_status
& I2C_ACK_INTR
) {
269 dev_dbg(priv
->dev
, "No ACK from device\n");
271 priv
->state
= HIX5I2C_STAT_RW_ERR
;
275 if (int_status
& I2C_OVER_INTR
) {
276 if (priv
->msg_len
> 0) {
277 ret
= hix5hd2_rw_preprocess(priv
);
280 priv
->state
= HIX5I2C_STAT_RW_ERR
;
283 if (priv
->msg
->flags
& I2C_M_RD
)
284 hix5hd2_read_handle(priv
);
286 hix5hd2_write_handle(priv
);
288 hix5hd2_rw_over(priv
);
293 if ((priv
->state
== HIX5I2C_STAT_RW_SUCCESS
&&
294 priv
->msg
->len
== priv
->msg_idx
) ||
295 (priv
->state
== HIX5I2C_STAT_RW_ERR
)) {
296 hix5hd2_i2c_disable_irq(priv
);
297 hix5hd2_i2c_clr_pend_irq(priv
);
298 complete(&priv
->msg_complete
);
301 spin_unlock(&priv
->lock
);
306 static void hix5hd2_i2c_message_start(struct hix5hd2_i2c_priv
*priv
, int stop
)
310 spin_lock_irqsave(&priv
->lock
, flags
);
311 hix5hd2_i2c_clr_all_irq(priv
);
312 hix5hd2_i2c_enable_irq(priv
);
314 if (priv
->msg
->flags
& I2C_M_RD
)
315 writel_relaxed((priv
->msg
->addr
<< 1) | HIX5I2C_READ_OPERATION
,
316 priv
->regs
+ HIX5I2C_TXR
);
318 writel_relaxed(priv
->msg
->addr
<< 1,
319 priv
->regs
+ HIX5I2C_TXR
);
321 writel_relaxed(I2C_WRITE
| I2C_START
, priv
->regs
+ HIX5I2C_COM
);
322 spin_unlock_irqrestore(&priv
->lock
, flags
);
325 static int hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv
*priv
,
326 struct i2c_msg
*msgs
, int stop
)
328 unsigned long timeout
;
333 priv
->msg_len
= priv
->msg
->len
;
336 priv
->state
= HIX5I2C_STAT_INIT
;
338 reinit_completion(&priv
->msg_complete
);
339 hix5hd2_i2c_message_start(priv
, stop
);
341 timeout
= wait_for_completion_timeout(&priv
->msg_complete
,
344 priv
->state
= HIX5I2C_STAT_RW_ERR
;
345 priv
->err
= -ETIMEDOUT
;
346 dev_warn(priv
->dev
, "%s timeout=%d\n",
347 msgs
->flags
& I2C_M_RD
? "rx" : "tx",
353 * If this is the last message to be transfered (stop == 1)
354 * Then check if the bus can be brought back to idle.
356 if (priv
->state
== HIX5I2C_STAT_RW_SUCCESS
&& stop
)
357 ret
= hix5hd2_i2c_wait_bus_idle(priv
);
360 hix5hd2_i2c_reset(priv
);
365 static int hix5hd2_i2c_xfer(struct i2c_adapter
*adap
,
366 struct i2c_msg
*msgs
, int num
)
368 struct hix5hd2_i2c_priv
*priv
= i2c_get_adapdata(adap
);
371 pm_runtime_get_sync(priv
->dev
);
373 for (i
= 0; i
< num
; i
++, msgs
++) {
374 stop
= (i
== num
- 1);
375 ret
= hix5hd2_i2c_xfer_msg(priv
, msgs
, stop
);
383 /* Only one message, cannot access the device */
389 dev_warn(priv
->dev
, "xfer message failed\n");
393 pm_runtime_mark_last_busy(priv
->dev
);
394 pm_runtime_put_autosuspend(priv
->dev
);
398 static u32
hix5hd2_i2c_func(struct i2c_adapter
*adap
)
400 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
403 static const struct i2c_algorithm hix5hd2_i2c_algorithm
= {
404 .master_xfer
= hix5hd2_i2c_xfer
,
405 .functionality
= hix5hd2_i2c_func
,
408 static int hix5hd2_i2c_probe(struct platform_device
*pdev
)
410 struct device_node
*np
= pdev
->dev
.of_node
;
411 struct hix5hd2_i2c_priv
*priv
;
412 struct resource
*mem
;
416 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
420 if (of_property_read_u32(np
, "clock-frequency", &freq
)) {
421 /* use 100k as default value */
424 if (freq
> HIX5I2C_MAX_FREQ
) {
425 priv
->freq
= HIX5I2C_MAX_FREQ
;
426 dev_warn(priv
->dev
, "use max freq %d instead\n",
433 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
434 priv
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
435 if (IS_ERR(priv
->regs
))
436 return PTR_ERR(priv
->regs
);
438 irq
= platform_get_irq(pdev
, 0);
440 dev_err(&pdev
->dev
, "cannot find HS-I2C IRQ\n");
444 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
445 if (IS_ERR(priv
->clk
)) {
446 dev_err(&pdev
->dev
, "cannot get clock\n");
447 return PTR_ERR(priv
->clk
);
449 clk_prepare_enable(priv
->clk
);
451 strlcpy(priv
->adap
.name
, "hix5hd2-i2c", sizeof(priv
->adap
.name
));
452 priv
->dev
= &pdev
->dev
;
453 priv
->adap
.owner
= THIS_MODULE
;
454 priv
->adap
.algo
= &hix5hd2_i2c_algorithm
;
455 priv
->adap
.retries
= 3;
456 priv
->adap
.dev
.of_node
= np
;
457 priv
->adap
.algo_data
= priv
;
458 priv
->adap
.dev
.parent
= &pdev
->dev
;
459 i2c_set_adapdata(&priv
->adap
, priv
);
460 platform_set_drvdata(pdev
, priv
);
461 spin_lock_init(&priv
->lock
);
462 init_completion(&priv
->msg_complete
);
464 hix5hd2_i2c_init(priv
);
466 ret
= devm_request_irq(&pdev
->dev
, irq
, hix5hd2_i2c_irq
,
467 IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
468 dev_name(&pdev
->dev
), priv
);
470 dev_err(&pdev
->dev
, "cannot request HS-I2C IRQ %d\n", irq
);
474 pm_suspend_ignore_children(&pdev
->dev
, true);
475 pm_runtime_set_autosuspend_delay(priv
->dev
, MSEC_PER_SEC
);
476 pm_runtime_use_autosuspend(priv
->dev
);
477 pm_runtime_set_active(priv
->dev
);
478 pm_runtime_enable(priv
->dev
);
480 ret
= i2c_add_adapter(&priv
->adap
);
482 dev_err(&pdev
->dev
, "failed to add bus to i2c core\n");
489 pm_runtime_disable(priv
->dev
);
490 pm_runtime_set_suspended(priv
->dev
);
492 clk_disable_unprepare(priv
->clk
);
496 static int hix5hd2_i2c_remove(struct platform_device
*pdev
)
498 struct hix5hd2_i2c_priv
*priv
= platform_get_drvdata(pdev
);
500 i2c_del_adapter(&priv
->adap
);
501 pm_runtime_disable(priv
->dev
);
502 pm_runtime_set_suspended(priv
->dev
);
508 static int hix5hd2_i2c_runtime_suspend(struct device
*dev
)
510 struct platform_device
*pdev
= to_platform_device(dev
);
511 struct hix5hd2_i2c_priv
*priv
= platform_get_drvdata(pdev
);
513 clk_disable_unprepare(priv
->clk
);
518 static int hix5hd2_i2c_runtime_resume(struct device
*dev
)
520 struct platform_device
*pdev
= to_platform_device(dev
);
521 struct hix5hd2_i2c_priv
*priv
= platform_get_drvdata(pdev
);
523 clk_prepare_enable(priv
->clk
);
524 hix5hd2_i2c_init(priv
);
530 static const struct dev_pm_ops hix5hd2_i2c_pm_ops
= {
531 SET_RUNTIME_PM_OPS(hix5hd2_i2c_runtime_suspend
,
532 hix5hd2_i2c_runtime_resume
,
536 static const struct of_device_id hix5hd2_i2c_match
[] = {
537 { .compatible
= "hisilicon,hix5hd2-i2c" },
540 MODULE_DEVICE_TABLE(of
, hix5hd2_i2c_match
);
542 static struct platform_driver hix5hd2_i2c_driver
= {
543 .probe
= hix5hd2_i2c_probe
,
544 .remove
= hix5hd2_i2c_remove
,
546 .name
= "hix5hd2-i2c",
547 .pm
= &hix5hd2_i2c_pm_ops
,
548 .of_match_table
= hix5hd2_i2c_match
,
552 module_platform_driver(hix5hd2_i2c_driver
);
554 MODULE_DESCRIPTION("Hix5hd2 I2C Bus driver");
555 MODULE_AUTHOR("Wei Yan <sledge.yanwei@huawei.com>");
556 MODULE_LICENSE("GPL");
557 MODULE_ALIAS("platform:hix5hd2-i2c");