1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * I2C bus driver for CSR SiRFprimaII
5 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 #include <linux/i2c.h>
14 #include <linux/clk.h>
15 #include <linux/err.h>
18 #define SIRFSOC_I2C_CLK_CTRL 0x00
19 #define SIRFSOC_I2C_STATUS 0x0C
20 #define SIRFSOC_I2C_CTRL 0x10
21 #define SIRFSOC_I2C_IO_CTRL 0x14
22 #define SIRFSOC_I2C_SDA_DELAY 0x18
23 #define SIRFSOC_I2C_CMD_START 0x1C
24 #define SIRFSOC_I2C_CMD_BUF 0x30
25 #define SIRFSOC_I2C_DATA_BUF 0x80
27 #define SIRFSOC_I2C_CMD_BUF_MAX 16
28 #define SIRFSOC_I2C_DATA_BUF_MAX 16
30 #define SIRFSOC_I2C_CMD(x) (SIRFSOC_I2C_CMD_BUF + (x)*0x04)
31 #define SIRFSOC_I2C_DATA_MASK(x) (0xFF<<(((x)&3)*8))
32 #define SIRFSOC_I2C_DATA_SHIFT(x) (((x)&3)*8)
34 #define SIRFSOC_I2C_DIV_MASK (0xFFFF)
36 /* I2C status flags */
37 #define SIRFSOC_I2C_STAT_BUSY BIT(0)
38 #define SIRFSOC_I2C_STAT_TIP BIT(1)
39 #define SIRFSOC_I2C_STAT_NACK BIT(2)
40 #define SIRFSOC_I2C_STAT_TR_INT BIT(4)
41 #define SIRFSOC_I2C_STAT_STOP BIT(6)
42 #define SIRFSOC_I2C_STAT_CMD_DONE BIT(8)
43 #define SIRFSOC_I2C_STAT_ERR BIT(9)
44 #define SIRFSOC_I2C_CMD_INDEX (0x1F<<16)
46 /* I2C control flags */
47 #define SIRFSOC_I2C_RESET BIT(0)
48 #define SIRFSOC_I2C_CORE_EN BIT(1)
49 #define SIRFSOC_I2C_MASTER_MODE BIT(2)
50 #define SIRFSOC_I2C_CMD_DONE_EN BIT(11)
51 #define SIRFSOC_I2C_ERR_INT_EN BIT(12)
53 #define SIRFSOC_I2C_SDA_DELAY_MASK (0xFF)
54 #define SIRFSOC_I2C_SCLF_FILTER (3<<8)
56 #define SIRFSOC_I2C_START_CMD BIT(0)
58 #define SIRFSOC_I2C_CMD_RP(x) ((x)&0x7)
59 #define SIRFSOC_I2C_NACK BIT(3)
60 #define SIRFSOC_I2C_WRITE BIT(4)
61 #define SIRFSOC_I2C_READ BIT(5)
62 #define SIRFSOC_I2C_STOP BIT(6)
63 #define SIRFSOC_I2C_START BIT(7)
65 #define SIRFSOC_I2C_DEFAULT_SPEED 100000
66 #define SIRFSOC_I2C_ERR_NOACK 1
67 #define SIRFSOC_I2C_ERR_TIMEOUT 2
72 u32 cmd_ptr
; /* Current position in CMD buffer */
73 u8
*buf
; /* Buffer passed by user */
74 u32 msg_len
; /* Message length */
75 u32 finished_len
; /* number of bytes read/written */
76 u32 read_cmd_len
; /* number of read cmd sent */
77 int msg_read
; /* 1 indicates a read message */
78 int err_status
; /* 1 indicates an error on bus */
80 u32 sda_delay
; /* For suspend/resume */
82 int last
; /* Last message in transfer, STOP cmd can be sent */
84 struct completion done
; /* indicates completion of message transfer */
85 struct i2c_adapter adapter
;
88 static void i2c_sirfsoc_read_data(struct sirfsoc_i2c
*siic
)
93 for (i
= 0; i
< siic
->read_cmd_len
; i
++) {
95 data
= readl(siic
->base
+ SIRFSOC_I2C_DATA_BUF
+ i
);
96 siic
->buf
[siic
->finished_len
++] =
97 (u8
)((data
& SIRFSOC_I2C_DATA_MASK(i
)) >>
98 SIRFSOC_I2C_DATA_SHIFT(i
));
102 static void i2c_sirfsoc_queue_cmd(struct sirfsoc_i2c
*siic
)
107 if (siic
->msg_read
) {
108 while (((siic
->finished_len
+ i
) < siic
->msg_len
)
109 && (siic
->cmd_ptr
< SIRFSOC_I2C_CMD_BUF_MAX
)) {
110 regval
= SIRFSOC_I2C_READ
| SIRFSOC_I2C_CMD_RP(0);
111 if (((siic
->finished_len
+ i
) ==
112 (siic
->msg_len
- 1)) && siic
->last
)
113 regval
|= SIRFSOC_I2C_STOP
| SIRFSOC_I2C_NACK
;
115 siic
->base
+ SIRFSOC_I2C_CMD(siic
->cmd_ptr
++));
119 siic
->read_cmd_len
= i
;
121 while ((siic
->cmd_ptr
< SIRFSOC_I2C_CMD_BUF_MAX
- 1)
122 && (siic
->finished_len
< siic
->msg_len
)) {
123 regval
= SIRFSOC_I2C_WRITE
| SIRFSOC_I2C_CMD_RP(0);
124 if ((siic
->finished_len
== (siic
->msg_len
- 1))
126 regval
|= SIRFSOC_I2C_STOP
;
128 siic
->base
+ SIRFSOC_I2C_CMD(siic
->cmd_ptr
++));
129 writel(siic
->buf
[siic
->finished_len
++],
130 siic
->base
+ SIRFSOC_I2C_CMD(siic
->cmd_ptr
++));
135 /* Trigger the transfer */
136 writel(SIRFSOC_I2C_START_CMD
, siic
->base
+ SIRFSOC_I2C_CMD_START
);
139 static irqreturn_t
i2c_sirfsoc_irq(int irq
, void *dev_id
)
141 struct sirfsoc_i2c
*siic
= (struct sirfsoc_i2c
*)dev_id
;
142 u32 i2c_stat
= readl(siic
->base
+ SIRFSOC_I2C_STATUS
);
144 if (i2c_stat
& SIRFSOC_I2C_STAT_ERR
) {
145 /* Error conditions */
146 siic
->err_status
= SIRFSOC_I2C_ERR_NOACK
;
147 writel(SIRFSOC_I2C_STAT_ERR
, siic
->base
+ SIRFSOC_I2C_STATUS
);
149 if (i2c_stat
& SIRFSOC_I2C_STAT_NACK
)
150 dev_dbg(&siic
->adapter
.dev
, "ACK not received\n");
152 dev_err(&siic
->adapter
.dev
, "I2C error\n");
155 * Due to hardware ANOMALY, we need to reset I2C earlier after
156 * we get NOACK while accessing non-existing clients, otherwise
157 * we will get errors even we access existing clients later
159 writel(readl(siic
->base
+ SIRFSOC_I2C_CTRL
) | SIRFSOC_I2C_RESET
,
160 siic
->base
+ SIRFSOC_I2C_CTRL
);
161 while (readl(siic
->base
+ SIRFSOC_I2C_CTRL
) & SIRFSOC_I2C_RESET
)
164 complete(&siic
->done
);
165 } else if (i2c_stat
& SIRFSOC_I2C_STAT_CMD_DONE
) {
166 /* CMD buffer execution complete */
168 i2c_sirfsoc_read_data(siic
);
169 if (siic
->finished_len
== siic
->msg_len
)
170 complete(&siic
->done
);
171 else /* Fill a new CMD buffer for left data */
172 i2c_sirfsoc_queue_cmd(siic
);
174 writel(SIRFSOC_I2C_STAT_CMD_DONE
, siic
->base
+ SIRFSOC_I2C_STATUS
);
180 static void i2c_sirfsoc_set_address(struct sirfsoc_i2c
*siic
,
184 u32 regval
= SIRFSOC_I2C_START
| SIRFSOC_I2C_CMD_RP(0) | SIRFSOC_I2C_WRITE
;
186 /* no data and last message -> add STOP */
187 if (siic
->last
&& (msg
->len
== 0))
188 regval
|= SIRFSOC_I2C_STOP
;
190 writel(regval
, siic
->base
+ SIRFSOC_I2C_CMD(siic
->cmd_ptr
++));
192 addr
= i2c_8bit_addr_from_msg(msg
);
194 /* Reverse direction bit */
195 if (msg
->flags
& I2C_M_REV_DIR_ADDR
)
198 writel(addr
, siic
->base
+ SIRFSOC_I2C_CMD(siic
->cmd_ptr
++));
201 static int i2c_sirfsoc_xfer_msg(struct sirfsoc_i2c
*siic
, struct i2c_msg
*msg
)
203 u32 regval
= readl(siic
->base
+ SIRFSOC_I2C_CTRL
);
204 /* timeout waiting for the xfer to finish or fail */
205 int timeout
= msecs_to_jiffies((msg
->len
+ 1) * 50);
207 i2c_sirfsoc_set_address(siic
, msg
);
209 writel(regval
| SIRFSOC_I2C_CMD_DONE_EN
| SIRFSOC_I2C_ERR_INT_EN
,
210 siic
->base
+ SIRFSOC_I2C_CTRL
);
211 i2c_sirfsoc_queue_cmd(siic
);
213 if (wait_for_completion_timeout(&siic
->done
, timeout
) == 0) {
214 siic
->err_status
= SIRFSOC_I2C_ERR_TIMEOUT
;
215 dev_err(&siic
->adapter
.dev
, "Transfer timeout\n");
218 writel(regval
& ~(SIRFSOC_I2C_CMD_DONE_EN
| SIRFSOC_I2C_ERR_INT_EN
),
219 siic
->base
+ SIRFSOC_I2C_CTRL
);
220 writel(0, siic
->base
+ SIRFSOC_I2C_CMD_START
);
222 /* i2c control doesn't response, reset it */
223 if (siic
->err_status
== SIRFSOC_I2C_ERR_TIMEOUT
) {
224 writel(readl(siic
->base
+ SIRFSOC_I2C_CTRL
) | SIRFSOC_I2C_RESET
,
225 siic
->base
+ SIRFSOC_I2C_CTRL
);
226 while (readl(siic
->base
+ SIRFSOC_I2C_CTRL
) & SIRFSOC_I2C_RESET
)
229 return siic
->err_status
? -EAGAIN
: 0;
232 static u32
i2c_sirfsoc_func(struct i2c_adapter
*adap
)
234 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
237 static int i2c_sirfsoc_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
240 struct sirfsoc_i2c
*siic
= adap
->algo_data
;
243 clk_enable(siic
->clk
);
245 for (i
= 0; i
< num
; i
++) {
246 siic
->buf
= msgs
[i
].buf
;
247 siic
->msg_len
= msgs
[i
].len
;
248 siic
->msg_read
= !!(msgs
[i
].flags
& I2C_M_RD
);
249 siic
->err_status
= 0;
251 siic
->finished_len
= 0;
252 siic
->last
= (i
== (num
- 1));
254 ret
= i2c_sirfsoc_xfer_msg(siic
, &msgs
[i
]);
256 clk_disable(siic
->clk
);
261 clk_disable(siic
->clk
);
265 /* I2C algorithms associated with this master controller driver */
266 static const struct i2c_algorithm i2c_sirfsoc_algo
= {
267 .master_xfer
= i2c_sirfsoc_xfer
,
268 .functionality
= i2c_sirfsoc_func
,
271 static int i2c_sirfsoc_probe(struct platform_device
*pdev
)
273 struct sirfsoc_i2c
*siic
;
274 struct i2c_adapter
*adap
;
275 struct resource
*mem_res
;
284 clk
= clk_get(&pdev
->dev
, NULL
);
287 dev_err(&pdev
->dev
, "Clock get failed\n");
291 err
= clk_prepare(clk
);
293 dev_err(&pdev
->dev
, "Clock prepare failed\n");
297 err
= clk_enable(clk
);
299 dev_err(&pdev
->dev
, "Clock enable failed\n");
303 ctrl_speed
= clk_get_rate(clk
);
305 siic
= devm_kzalloc(&pdev
->dev
, sizeof(*siic
), GFP_KERNEL
);
310 adap
= &siic
->adapter
;
311 adap
->class = I2C_CLASS_DEPRECATED
;
313 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
314 siic
->base
= devm_ioremap_resource(&pdev
->dev
, mem_res
);
315 if (IS_ERR(siic
->base
)) {
316 err
= PTR_ERR(siic
->base
);
320 irq
= platform_get_irq(pdev
, 0);
325 err
= devm_request_irq(&pdev
->dev
, irq
, i2c_sirfsoc_irq
, 0,
326 dev_name(&pdev
->dev
), siic
);
330 adap
->algo
= &i2c_sirfsoc_algo
;
331 adap
->algo_data
= siic
;
334 adap
->dev
.of_node
= pdev
->dev
.of_node
;
335 adap
->dev
.parent
= &pdev
->dev
;
338 strlcpy(adap
->name
, "sirfsoc-i2c", sizeof(adap
->name
));
340 platform_set_drvdata(pdev
, adap
);
341 init_completion(&siic
->done
);
343 /* Controller initialisation */
345 writel(SIRFSOC_I2C_RESET
, siic
->base
+ SIRFSOC_I2C_CTRL
);
346 while (readl(siic
->base
+ SIRFSOC_I2C_CTRL
) & SIRFSOC_I2C_RESET
)
348 writel(SIRFSOC_I2C_CORE_EN
| SIRFSOC_I2C_MASTER_MODE
,
349 siic
->base
+ SIRFSOC_I2C_CTRL
);
353 err
= of_property_read_u32(pdev
->dev
.of_node
,
354 "clock-frequency", &bitrate
);
356 bitrate
= SIRFSOC_I2C_DEFAULT_SPEED
;
359 * Due to some hardware design issues, we need to tune the formula.
360 * Since i2c is open drain interface that allows the slave to
361 * stall the transaction by holding the SCL line at '0', the RTL
362 * implementation is waiting for SCL feedback from the pin after
363 * setting it to High-Z ('1'). This wait adds to the high-time
364 * interval counter few cycles of the input synchronization
365 * (depending on the SCL_FILTER_REG field), and also the time it
366 * takes for the board pull-up resistor to rise the SCL line.
367 * For slow SCL settings these additions are negligible,
368 * but they start to affect the speed when clock is set to faster
370 * Through the actual tests, use the different user_div value(which
371 * in the divider formula 'Fio / (Fi2c * user_div)') to adapt
372 * the different ranges of i2c bus clock frequency, to make the SCL
375 if (bitrate
<= 30000)
376 regval
= ctrl_speed
/ (bitrate
* 5);
377 else if (bitrate
> 30000 && bitrate
<= 280000)
378 regval
= (2 * ctrl_speed
) / (bitrate
* 11);
380 regval
= ctrl_speed
/ (bitrate
* 6);
382 writel(regval
, siic
->base
+ SIRFSOC_I2C_CLK_CTRL
);
384 writel(0xFF, siic
->base
+ SIRFSOC_I2C_SDA_DELAY
);
386 writel(regval
, siic
->base
+ SIRFSOC_I2C_SDA_DELAY
);
388 err
= i2c_add_numbered_adapter(adap
);
394 dev_info(&pdev
->dev
, " I2C adapter ready to operate\n");
408 static int i2c_sirfsoc_remove(struct platform_device
*pdev
)
410 struct i2c_adapter
*adapter
= platform_get_drvdata(pdev
);
411 struct sirfsoc_i2c
*siic
= adapter
->algo_data
;
413 writel(SIRFSOC_I2C_RESET
, siic
->base
+ SIRFSOC_I2C_CTRL
);
414 i2c_del_adapter(adapter
);
415 clk_unprepare(siic
->clk
);
421 static int i2c_sirfsoc_suspend(struct device
*dev
)
423 struct i2c_adapter
*adapter
= dev_get_drvdata(dev
);
424 struct sirfsoc_i2c
*siic
= adapter
->algo_data
;
426 clk_enable(siic
->clk
);
427 siic
->sda_delay
= readl(siic
->base
+ SIRFSOC_I2C_SDA_DELAY
);
428 siic
->clk_div
= readl(siic
->base
+ SIRFSOC_I2C_CLK_CTRL
);
429 clk_disable(siic
->clk
);
433 static int i2c_sirfsoc_resume(struct device
*dev
)
435 struct i2c_adapter
*adapter
= dev_get_drvdata(dev
);
436 struct sirfsoc_i2c
*siic
= adapter
->algo_data
;
438 clk_enable(siic
->clk
);
439 writel(SIRFSOC_I2C_RESET
, siic
->base
+ SIRFSOC_I2C_CTRL
);
440 while (readl(siic
->base
+ SIRFSOC_I2C_CTRL
) & SIRFSOC_I2C_RESET
)
442 writel(SIRFSOC_I2C_CORE_EN
| SIRFSOC_I2C_MASTER_MODE
,
443 siic
->base
+ SIRFSOC_I2C_CTRL
);
444 writel(siic
->clk_div
, siic
->base
+ SIRFSOC_I2C_CLK_CTRL
);
445 writel(siic
->sda_delay
, siic
->base
+ SIRFSOC_I2C_SDA_DELAY
);
446 clk_disable(siic
->clk
);
450 static const struct dev_pm_ops i2c_sirfsoc_pm_ops
= {
451 .suspend
= i2c_sirfsoc_suspend
,
452 .resume
= i2c_sirfsoc_resume
,
456 static const struct of_device_id sirfsoc_i2c_of_match
[] = {
457 { .compatible
= "sirf,prima2-i2c", },
460 MODULE_DEVICE_TABLE(of
, sirfsoc_i2c_of_match
);
462 static struct platform_driver i2c_sirfsoc_driver
= {
464 .name
= "sirfsoc_i2c",
466 .pm
= &i2c_sirfsoc_pm_ops
,
468 .of_match_table
= sirfsoc_i2c_of_match
,
470 .probe
= i2c_sirfsoc_probe
,
471 .remove
= i2c_sirfsoc_remove
,
473 module_platform_driver(i2c_sirfsoc_driver
);
475 MODULE_DESCRIPTION("SiRF SoC I2C master controller driver");
476 MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>, "
477 "Xiangzhen Ye <Xiangzhen.Ye@csr.com>");
478 MODULE_LICENSE("GPL v2");