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 */
77 enum hix5hd2_i2c_state
{
78 HIX5I2C_STAT_RW_ERR
= -1,
81 HIX5I2C_STAT_SND_STOP
,
82 HIX5I2C_STAT_RW_SUCCESS
,
85 struct hix5hd2_i2c_priv
{
86 struct i2c_adapter adap
;
88 struct completion msg_complete
;
95 spinlock_t lock
; /* IRQ synchronization */
98 enum hix5hd2_i2c_state state
;
101 static u32
hix5hd2_i2c_clr_pend_irq(struct hix5hd2_i2c_priv
*priv
)
103 u32 val
= readl_relaxed(priv
->regs
+ HIX5I2C_SR
);
105 writel_relaxed(val
, priv
->regs
+ HIX5I2C_ICR
);
110 static void hix5hd2_i2c_clr_all_irq(struct hix5hd2_i2c_priv
*priv
)
112 writel_relaxed(I2C_CLEAR_ALL
, priv
->regs
+ HIX5I2C_ICR
);
115 static void hix5hd2_i2c_disable_irq(struct hix5hd2_i2c_priv
*priv
)
117 writel_relaxed(0, priv
->regs
+ HIX5I2C_CTRL
);
120 static void hix5hd2_i2c_enable_irq(struct hix5hd2_i2c_priv
*priv
)
122 writel_relaxed(I2C_ENABLE
| I2C_UNMASK_TOTAL
| I2C_UNMASK_ALL
,
123 priv
->regs
+ HIX5I2C_CTRL
);
126 static void hix5hd2_i2c_drv_setrate(struct hix5hd2_i2c_priv
*priv
)
131 /* close all i2c interrupt */
132 val
= readl_relaxed(priv
->regs
+ HIX5I2C_CTRL
);
133 writel_relaxed(val
& (~I2C_UNMASK_TOTAL
), priv
->regs
+ HIX5I2C_CTRL
);
136 sysclock
= clk_get_rate(priv
->clk
);
137 scl
= (sysclock
/ (rate
* 2)) / 2 - 1;
138 writel_relaxed(scl
, priv
->regs
+ HIX5I2C_SCL_H
);
139 writel_relaxed(scl
, priv
->regs
+ HIX5I2C_SCL_L
);
141 /* restore original interrupt*/
142 writel_relaxed(val
, priv
->regs
+ HIX5I2C_CTRL
);
144 dev_dbg(priv
->dev
, "%s: sysclock=%d, rate=%d, scl=%d\n",
145 __func__
, sysclock
, rate
, scl
);
148 static void hix5hd2_i2c_init(struct hix5hd2_i2c_priv
*priv
)
150 hix5hd2_i2c_disable_irq(priv
);
151 hix5hd2_i2c_drv_setrate(priv
);
152 hix5hd2_i2c_clr_all_irq(priv
);
153 hix5hd2_i2c_enable_irq(priv
);
156 static void hix5hd2_i2c_reset(struct hix5hd2_i2c_priv
*priv
)
158 clk_disable_unprepare(priv
->clk
);
160 clk_prepare_enable(priv
->clk
);
161 hix5hd2_i2c_init(priv
);
164 static int hix5hd2_i2c_wait_bus_idle(struct hix5hd2_i2c_priv
*priv
)
166 unsigned long stop_time
;
169 /* wait for 100 milli seconds for the bus to be idle */
170 stop_time
= jiffies
+ msecs_to_jiffies(100);
172 int_status
= hix5hd2_i2c_clr_pend_irq(priv
);
173 if (!(int_status
& I2C_BUSY
))
176 usleep_range(50, 200);
177 } while (time_before(jiffies
, stop_time
));
182 static void hix5hd2_rw_over(struct hix5hd2_i2c_priv
*priv
)
184 if (priv
->state
== HIX5I2C_STAT_SND_STOP
)
185 dev_dbg(priv
->dev
, "%s: rw and send stop over\n", __func__
);
187 dev_dbg(priv
->dev
, "%s: have not data to send\n", __func__
);
189 priv
->state
= HIX5I2C_STAT_RW_SUCCESS
;
193 static void hix5hd2_rw_handle_stop(struct hix5hd2_i2c_priv
*priv
)
196 priv
->state
= HIX5I2C_STAT_SND_STOP
;
197 writel_relaxed(I2C_STOP
, priv
->regs
+ HIX5I2C_COM
);
199 hix5hd2_rw_over(priv
);
203 static void hix5hd2_read_handle(struct hix5hd2_i2c_priv
*priv
)
205 if (priv
->msg_len
== 1) {
206 /* the last byte don't need send ACK */
207 writel_relaxed(I2C_READ
| I2C_NO_ACK
, priv
->regs
+ HIX5I2C_COM
);
208 } else if (priv
->msg_len
> 1) {
209 /* if i2c master receive data will send ACK */
210 writel_relaxed(I2C_READ
, priv
->regs
+ HIX5I2C_COM
);
212 hix5hd2_rw_handle_stop(priv
);
216 static void hix5hd2_write_handle(struct hix5hd2_i2c_priv
*priv
)
220 if (priv
->msg_len
> 0) {
221 data
= priv
->msg
->buf
[priv
->msg_idx
++];
222 writel_relaxed(data
, priv
->regs
+ HIX5I2C_TXR
);
223 writel_relaxed(I2C_WRITE
, priv
->regs
+ HIX5I2C_COM
);
225 hix5hd2_rw_handle_stop(priv
);
229 static int hix5hd2_rw_preprocess(struct hix5hd2_i2c_priv
*priv
)
233 if (priv
->state
== HIX5I2C_STAT_INIT
) {
234 priv
->state
= HIX5I2C_STAT_RW
;
235 } else if (priv
->state
== HIX5I2C_STAT_RW
) {
236 if (priv
->msg
->flags
& I2C_M_RD
) {
237 data
= readl_relaxed(priv
->regs
+ HIX5I2C_RXR
);
238 priv
->msg
->buf
[priv
->msg_idx
++] = data
;
242 dev_dbg(priv
->dev
, "%s: error: priv->state = %d, msg_len = %d\n",
243 __func__
, priv
->state
, priv
->msg_len
);
249 static irqreturn_t
hix5hd2_i2c_irq(int irqno
, void *dev_id
)
251 struct hix5hd2_i2c_priv
*priv
= dev_id
;
255 spin_lock(&priv
->lock
);
257 int_status
= hix5hd2_i2c_clr_pend_irq(priv
);
260 if (int_status
& I2C_ARBITRATE_INTR
) {
262 dev_dbg(priv
->dev
, "ARB bus loss\n");
264 priv
->state
= HIX5I2C_STAT_RW_ERR
;
266 } else if (int_status
& I2C_ACK_INTR
) {
268 dev_dbg(priv
->dev
, "No ACK from device\n");
270 priv
->state
= HIX5I2C_STAT_RW_ERR
;
274 if (int_status
& I2C_OVER_INTR
) {
275 if (priv
->msg_len
> 0) {
276 ret
= hix5hd2_rw_preprocess(priv
);
279 priv
->state
= HIX5I2C_STAT_RW_ERR
;
282 if (priv
->msg
->flags
& I2C_M_RD
)
283 hix5hd2_read_handle(priv
);
285 hix5hd2_write_handle(priv
);
287 hix5hd2_rw_over(priv
);
292 if ((priv
->state
== HIX5I2C_STAT_RW_SUCCESS
&&
293 priv
->msg
->len
== priv
->msg_idx
) ||
294 (priv
->state
== HIX5I2C_STAT_RW_ERR
)) {
295 hix5hd2_i2c_disable_irq(priv
);
296 hix5hd2_i2c_clr_pend_irq(priv
);
297 complete(&priv
->msg_complete
);
300 spin_unlock(&priv
->lock
);
305 static void hix5hd2_i2c_message_start(struct hix5hd2_i2c_priv
*priv
, int stop
)
309 spin_lock_irqsave(&priv
->lock
, flags
);
310 hix5hd2_i2c_clr_all_irq(priv
);
311 hix5hd2_i2c_enable_irq(priv
);
313 writel_relaxed(i2c_8bit_addr_from_msg(priv
->msg
),
314 priv
->regs
+ HIX5I2C_TXR
);
316 writel_relaxed(I2C_WRITE
| I2C_START
, priv
->regs
+ HIX5I2C_COM
);
317 spin_unlock_irqrestore(&priv
->lock
, flags
);
320 static int hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv
*priv
,
321 struct i2c_msg
*msgs
, int stop
)
323 unsigned long timeout
;
328 priv
->msg_len
= priv
->msg
->len
;
331 priv
->state
= HIX5I2C_STAT_INIT
;
333 reinit_completion(&priv
->msg_complete
);
334 hix5hd2_i2c_message_start(priv
, stop
);
336 timeout
= wait_for_completion_timeout(&priv
->msg_complete
,
339 priv
->state
= HIX5I2C_STAT_RW_ERR
;
340 priv
->err
= -ETIMEDOUT
;
341 dev_warn(priv
->dev
, "%s timeout=%d\n",
342 msgs
->flags
& I2C_M_RD
? "rx" : "tx",
348 * If this is the last message to be transfered (stop == 1)
349 * Then check if the bus can be brought back to idle.
351 if (priv
->state
== HIX5I2C_STAT_RW_SUCCESS
&& stop
)
352 ret
= hix5hd2_i2c_wait_bus_idle(priv
);
355 hix5hd2_i2c_reset(priv
);
360 static int hix5hd2_i2c_xfer(struct i2c_adapter
*adap
,
361 struct i2c_msg
*msgs
, int num
)
363 struct hix5hd2_i2c_priv
*priv
= i2c_get_adapdata(adap
);
366 pm_runtime_get_sync(priv
->dev
);
368 for (i
= 0; i
< num
; i
++, msgs
++) {
369 stop
= (i
== num
- 1);
370 ret
= hix5hd2_i2c_xfer_msg(priv
, msgs
, stop
);
378 pm_runtime_mark_last_busy(priv
->dev
);
379 pm_runtime_put_autosuspend(priv
->dev
);
383 static u32
hix5hd2_i2c_func(struct i2c_adapter
*adap
)
385 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
388 static const struct i2c_algorithm hix5hd2_i2c_algorithm
= {
389 .master_xfer
= hix5hd2_i2c_xfer
,
390 .functionality
= hix5hd2_i2c_func
,
393 static int hix5hd2_i2c_probe(struct platform_device
*pdev
)
395 struct device_node
*np
= pdev
->dev
.of_node
;
396 struct hix5hd2_i2c_priv
*priv
;
397 struct resource
*mem
;
401 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
405 if (of_property_read_u32(np
, "clock-frequency", &freq
)) {
406 /* use 100k as default value */
409 if (freq
> HIX5I2C_MAX_FREQ
) {
410 priv
->freq
= HIX5I2C_MAX_FREQ
;
411 dev_warn(priv
->dev
, "use max freq %d instead\n",
418 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
419 priv
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
420 if (IS_ERR(priv
->regs
))
421 return PTR_ERR(priv
->regs
);
423 irq
= platform_get_irq(pdev
, 0);
425 dev_err(&pdev
->dev
, "cannot find HS-I2C IRQ\n");
429 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
430 if (IS_ERR(priv
->clk
)) {
431 dev_err(&pdev
->dev
, "cannot get clock\n");
432 return PTR_ERR(priv
->clk
);
434 clk_prepare_enable(priv
->clk
);
436 strlcpy(priv
->adap
.name
, "hix5hd2-i2c", sizeof(priv
->adap
.name
));
437 priv
->dev
= &pdev
->dev
;
438 priv
->adap
.owner
= THIS_MODULE
;
439 priv
->adap
.algo
= &hix5hd2_i2c_algorithm
;
440 priv
->adap
.retries
= 3;
441 priv
->adap
.dev
.of_node
= np
;
442 priv
->adap
.algo_data
= priv
;
443 priv
->adap
.dev
.parent
= &pdev
->dev
;
444 i2c_set_adapdata(&priv
->adap
, priv
);
445 platform_set_drvdata(pdev
, priv
);
446 spin_lock_init(&priv
->lock
);
447 init_completion(&priv
->msg_complete
);
449 hix5hd2_i2c_init(priv
);
451 ret
= devm_request_irq(&pdev
->dev
, irq
, hix5hd2_i2c_irq
,
452 IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
453 dev_name(&pdev
->dev
), priv
);
455 dev_err(&pdev
->dev
, "cannot request HS-I2C IRQ %d\n", irq
);
459 pm_runtime_set_autosuspend_delay(priv
->dev
, MSEC_PER_SEC
);
460 pm_runtime_use_autosuspend(priv
->dev
);
461 pm_runtime_set_active(priv
->dev
);
462 pm_runtime_enable(priv
->dev
);
464 ret
= i2c_add_adapter(&priv
->adap
);
471 pm_runtime_disable(priv
->dev
);
472 pm_runtime_set_suspended(priv
->dev
);
474 clk_disable_unprepare(priv
->clk
);
478 static int hix5hd2_i2c_remove(struct platform_device
*pdev
)
480 struct hix5hd2_i2c_priv
*priv
= platform_get_drvdata(pdev
);
482 i2c_del_adapter(&priv
->adap
);
483 pm_runtime_disable(priv
->dev
);
484 pm_runtime_set_suspended(priv
->dev
);
485 clk_disable_unprepare(priv
->clk
);
491 static int hix5hd2_i2c_runtime_suspend(struct device
*dev
)
493 struct hix5hd2_i2c_priv
*priv
= dev_get_drvdata(dev
);
495 clk_disable_unprepare(priv
->clk
);
500 static int hix5hd2_i2c_runtime_resume(struct device
*dev
)
502 struct hix5hd2_i2c_priv
*priv
= dev_get_drvdata(dev
);
504 clk_prepare_enable(priv
->clk
);
505 hix5hd2_i2c_init(priv
);
511 static const struct dev_pm_ops hix5hd2_i2c_pm_ops
= {
512 SET_RUNTIME_PM_OPS(hix5hd2_i2c_runtime_suspend
,
513 hix5hd2_i2c_runtime_resume
,
517 static const struct of_device_id hix5hd2_i2c_match
[] = {
518 { .compatible
= "hisilicon,hix5hd2-i2c" },
521 MODULE_DEVICE_TABLE(of
, hix5hd2_i2c_match
);
523 static struct platform_driver hix5hd2_i2c_driver
= {
524 .probe
= hix5hd2_i2c_probe
,
525 .remove
= hix5hd2_i2c_remove
,
527 .name
= "hix5hd2-i2c",
528 .pm
= &hix5hd2_i2c_pm_ops
,
529 .of_match_table
= hix5hd2_i2c_match
,
533 module_platform_driver(hix5hd2_i2c_driver
);
535 MODULE_DESCRIPTION("Hix5hd2 I2C Bus driver");
536 MODULE_AUTHOR("Wei Yan <sledge.yanwei@huawei.com>");
537 MODULE_LICENSE("GPL");
538 MODULE_ALIAS("platform:hix5hd2-i2c");