2 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
3 * Copyright 2017 Linaro Ltd.
5 * Author: Baoyou Xie <baoyou.xie@linaro.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/clk.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
20 #define REG_DEVADDR_H 0x0C
21 #define REG_DEVADDR_L 0x10
22 #define REG_CLK_DIV_FS 0x14
23 #define REG_CLK_DIV_HS 0x18
24 #define REG_WRCONF 0x1C
25 #define REG_RDCONF 0x20
30 #define I2C_MASTER BIT(0)
31 #define I2C_ADDR_MODE_TEN BIT(1)
32 #define I2C_IRQ_MSK_ENABLE BIT(3)
33 #define I2C_RW_READ BIT(4)
34 #define I2C_CMB_RW_EN BIT(5)
35 #define I2C_START BIT(6)
37 #define I2C_ADDR_LOW_MASK GENMASK(6, 0)
38 #define I2C_ADDR_LOW_SHIFT 0
39 #define I2C_ADDR_HI_MASK GENMASK(2, 0)
40 #define I2C_ADDR_HI_SHIFT 7
42 #define I2C_WFIFO_RESET BIT(7)
43 #define I2C_RFIFO_RESET BIT(7)
45 #define I2C_IRQ_ACK_CLEAR BIT(7)
46 #define I2C_INT_MASK GENMASK(6, 0)
48 #define I2C_TRANS_DONE BIT(0)
49 #define I2C_SR_EDEVICE BIT(1)
50 #define I2C_SR_EDATA BIT(2)
52 #define I2C_FIFO_MAX 16
54 #define I2C_TIMEOUT msecs_to_jiffies(1000)
56 #define DEV(i2c) ((i2c)->adap.dev.parent)
59 struct i2c_adapter adap
;
61 struct completion complete
;
63 void __iomem
*reg_base
;
73 static void zx2967_i2c_writel(struct zx2967_i2c
*i2c
,
74 u32 val
, unsigned long reg
)
76 writel_relaxed(val
, i2c
->reg_base
+ reg
);
79 static u32
zx2967_i2c_readl(struct zx2967_i2c
*i2c
, unsigned long reg
)
81 return readl_relaxed(i2c
->reg_base
+ reg
);
84 static void zx2967_i2c_writesb(struct zx2967_i2c
*i2c
,
85 void *data
, unsigned long reg
, int len
)
87 writesb(i2c
->reg_base
+ reg
, data
, len
);
90 static void zx2967_i2c_readsb(struct zx2967_i2c
*i2c
,
91 void *data
, unsigned long reg
, int len
)
93 readsb(i2c
->reg_base
+ reg
, data
, len
);
96 static void zx2967_i2c_start_ctrl(struct zx2967_i2c
*i2c
)
101 status
= zx2967_i2c_readl(i2c
, REG_STAT
);
102 status
|= I2C_IRQ_ACK_CLEAR
;
103 zx2967_i2c_writel(i2c
, status
, REG_STAT
);
105 ctl
= zx2967_i2c_readl(i2c
, REG_CMD
);
110 ctl
&= ~I2C_CMB_RW_EN
;
112 zx2967_i2c_writel(i2c
, ctl
, REG_CMD
);
115 static void zx2967_i2c_flush_fifos(struct zx2967_i2c
*i2c
)
122 val
= I2C_RFIFO_RESET
;
125 val
= I2C_WFIFO_RESET
;
128 val
|= zx2967_i2c_readl(i2c
, offset
);
129 zx2967_i2c_writel(i2c
, val
, offset
);
132 static int zx2967_i2c_empty_rx_fifo(struct zx2967_i2c
*i2c
, u32 size
)
134 u8 val
[I2C_FIFO_MAX
] = {0};
137 if (size
> I2C_FIFO_MAX
) {
138 dev_err(DEV(i2c
), "fifo size %d over the max value %d\n",
143 zx2967_i2c_readsb(i2c
, val
, REG_DATA
, size
);
144 for (i
= 0; i
< size
; i
++) {
145 *i2c
->cur_trans
++ = val
[i
];
154 static int zx2967_i2c_fill_tx_fifo(struct zx2967_i2c
*i2c
)
156 size_t residue
= i2c
->residue
;
157 u8
*buf
= i2c
->cur_trans
;
160 dev_err(DEV(i2c
), "residue is %d\n", (int)residue
);
164 if (residue
<= I2C_FIFO_MAX
) {
165 zx2967_i2c_writesb(i2c
, buf
, REG_DATA
, residue
);
167 /* Again update before writing to FIFO to make sure isr sees. */
169 i2c
->cur_trans
= NULL
;
171 zx2967_i2c_writesb(i2c
, buf
, REG_DATA
, I2C_FIFO_MAX
);
172 i2c
->residue
-= I2C_FIFO_MAX
;
173 i2c
->cur_trans
+= I2C_FIFO_MAX
;
181 static int zx2967_i2c_reset_hardware(struct zx2967_i2c
*i2c
)
186 val
= I2C_MASTER
| I2C_IRQ_MSK_ENABLE
;
187 zx2967_i2c_writel(i2c
, val
, REG_CMD
);
189 clk_div
= clk_get_rate(i2c
->clk
) / i2c
->clk_freq
- 1;
190 zx2967_i2c_writel(i2c
, clk_div
, REG_CLK_DIV_FS
);
191 zx2967_i2c_writel(i2c
, clk_div
, REG_CLK_DIV_HS
);
193 zx2967_i2c_writel(i2c
, I2C_FIFO_MAX
- 1, REG_WRCONF
);
194 zx2967_i2c_writel(i2c
, I2C_FIFO_MAX
- 1, REG_RDCONF
);
195 zx2967_i2c_writel(i2c
, 1, REG_RDCONF
);
197 zx2967_i2c_flush_fifos(i2c
);
202 static void zx2967_i2c_isr_clr(struct zx2967_i2c
*i2c
)
206 status
= zx2967_i2c_readl(i2c
, REG_STAT
);
207 status
|= I2C_IRQ_ACK_CLEAR
;
208 zx2967_i2c_writel(i2c
, status
, REG_STAT
);
211 static irqreturn_t
zx2967_i2c_isr(int irq
, void *dev_id
)
214 struct zx2967_i2c
*i2c
= (struct zx2967_i2c
*)dev_id
;
216 status
= zx2967_i2c_readl(i2c
, REG_STAT
) & I2C_INT_MASK
;
217 zx2967_i2c_isr_clr(i2c
);
219 if (status
& I2C_SR_EDEVICE
)
221 else if (status
& I2C_SR_EDATA
)
223 else if (status
& I2C_TRANS_DONE
)
228 complete(&i2c
->complete
);
233 static void zx2967_set_addr(struct zx2967_i2c
*i2c
, u16 addr
)
237 val
= (addr
>> I2C_ADDR_LOW_SHIFT
) & I2C_ADDR_LOW_MASK
;
238 zx2967_i2c_writel(i2c
, val
, REG_DEVADDR_L
);
240 val
= (addr
>> I2C_ADDR_HI_SHIFT
) & I2C_ADDR_HI_MASK
;
241 zx2967_i2c_writel(i2c
, val
, REG_DEVADDR_H
);
243 val
= zx2967_i2c_readl(i2c
, REG_CMD
) | I2C_ADDR_MODE_TEN
;
245 val
= zx2967_i2c_readl(i2c
, REG_CMD
) & ~I2C_ADDR_MODE_TEN
;
246 zx2967_i2c_writel(i2c
, val
, REG_CMD
);
249 static int zx2967_i2c_xfer_bytes(struct zx2967_i2c
*i2c
, u32 bytes
)
251 unsigned long time_left
;
252 int rd
= i2c
->msg_rd
;
255 reinit_completion(&i2c
->complete
);
258 zx2967_i2c_writel(i2c
, bytes
- 1, REG_RDCONF
);
260 ret
= zx2967_i2c_fill_tx_fifo(i2c
);
265 zx2967_i2c_start_ctrl(i2c
);
267 time_left
= wait_for_completion_timeout(&i2c
->complete
,
275 return rd
? zx2967_i2c_empty_rx_fifo(i2c
, bytes
) : 0;
278 static int zx2967_i2c_xfer_msg(struct zx2967_i2c
*i2c
,
284 zx2967_i2c_flush_fifos(i2c
);
286 i2c
->cur_trans
= msg
->buf
;
287 i2c
->residue
= msg
->len
;
288 i2c
->access_cnt
= msg
->len
/ I2C_FIFO_MAX
;
289 i2c
->msg_rd
= msg
->flags
& I2C_M_RD
;
291 for (i
= 0; i
< i2c
->access_cnt
; i
++) {
292 ret
= zx2967_i2c_xfer_bytes(i2c
, I2C_FIFO_MAX
);
297 if (i2c
->residue
> 0) {
298 ret
= zx2967_i2c_xfer_bytes(i2c
, i2c
->residue
);
309 static int zx2967_i2c_xfer(struct i2c_adapter
*adap
,
310 struct i2c_msg
*msgs
, int num
)
312 struct zx2967_i2c
*i2c
= i2c_get_adapdata(adap
);
316 if (i2c
->is_suspended
)
319 zx2967_set_addr(i2c
, msgs
->addr
);
321 for (i
= 0; i
< num
; i
++) {
322 ret
= zx2967_i2c_xfer_msg(i2c
, &msgs
[i
]);
331 zx2967_smbus_xfer_prepare(struct zx2967_i2c
*i2c
, u16 addr
,
332 char read_write
, u8 command
, int size
,
333 union i2c_smbus_data
*data
)
337 val
= zx2967_i2c_readl(i2c
, REG_RDCONF
);
338 val
|= I2C_RFIFO_RESET
;
339 zx2967_i2c_writel(i2c
, val
, REG_RDCONF
);
340 zx2967_set_addr(i2c
, addr
);
341 val
= zx2967_i2c_readl(i2c
, REG_CMD
);
343 zx2967_i2c_writel(i2c
, val
, REG_CMD
);
347 zx2967_i2c_writel(i2c
, command
, REG_DATA
);
349 case I2C_SMBUS_BYTE_DATA
:
350 zx2967_i2c_writel(i2c
, command
, REG_DATA
);
351 if (read_write
== I2C_SMBUS_WRITE
)
352 zx2967_i2c_writel(i2c
, data
->byte
, REG_DATA
);
354 case I2C_SMBUS_WORD_DATA
:
355 zx2967_i2c_writel(i2c
, command
, REG_DATA
);
356 if (read_write
== I2C_SMBUS_WRITE
) {
357 zx2967_i2c_writel(i2c
, (data
->word
>> 8), REG_DATA
);
358 zx2967_i2c_writel(i2c
, (data
->word
& 0xff),
365 static int zx2967_smbus_xfer_read(struct zx2967_i2c
*i2c
, int size
,
366 union i2c_smbus_data
*data
)
368 unsigned long time_left
;
372 reinit_completion(&i2c
->complete
);
374 val
= zx2967_i2c_readl(i2c
, REG_CMD
);
375 val
|= I2C_CMB_RW_EN
;
376 zx2967_i2c_writel(i2c
, val
, REG_CMD
);
378 val
= zx2967_i2c_readl(i2c
, REG_CMD
);
380 zx2967_i2c_writel(i2c
, val
, REG_CMD
);
382 time_left
= wait_for_completion_timeout(&i2c
->complete
,
392 case I2C_SMBUS_BYTE_DATA
:
393 val
= zx2967_i2c_readl(i2c
, REG_DATA
);
396 case I2C_SMBUS_WORD_DATA
:
397 case I2C_SMBUS_PROC_CALL
:
398 buf
[0] = zx2967_i2c_readl(i2c
, REG_DATA
);
399 buf
[1] = zx2967_i2c_readl(i2c
, REG_DATA
);
400 data
->word
= (buf
[0] << 8) | buf
[1];
409 static int zx2967_smbus_xfer_write(struct zx2967_i2c
*i2c
)
411 unsigned long time_left
;
414 reinit_completion(&i2c
->complete
);
415 val
= zx2967_i2c_readl(i2c
, REG_CMD
);
417 zx2967_i2c_writel(i2c
, val
, REG_CMD
);
419 time_left
= wait_for_completion_timeout(&i2c
->complete
,
430 static int zx2967_smbus_xfer(struct i2c_adapter
*adap
, u16 addr
,
431 unsigned short flags
, char read_write
,
432 u8 command
, int size
, union i2c_smbus_data
*data
)
434 struct zx2967_i2c
*i2c
= i2c_get_adapdata(adap
);
436 if (size
== I2C_SMBUS_QUICK
)
437 read_write
= I2C_SMBUS_WRITE
;
440 case I2C_SMBUS_QUICK
:
442 case I2C_SMBUS_BYTE_DATA
:
443 case I2C_SMBUS_WORD_DATA
:
444 zx2967_smbus_xfer_prepare(i2c
, addr
, read_write
,
445 command
, size
, data
);
451 if (read_write
== I2C_SMBUS_READ
)
452 return zx2967_smbus_xfer_read(i2c
, size
, data
);
454 return zx2967_smbus_xfer_write(i2c
);
457 static u32
zx2967_i2c_func(struct i2c_adapter
*adap
)
459 return I2C_FUNC_I2C
|
460 I2C_FUNC_SMBUS_QUICK
|
461 I2C_FUNC_SMBUS_BYTE
|
462 I2C_FUNC_SMBUS_BYTE_DATA
|
463 I2C_FUNC_SMBUS_WORD_DATA
|
464 I2C_FUNC_SMBUS_BLOCK_DATA
|
465 I2C_FUNC_SMBUS_PROC_CALL
|
466 I2C_FUNC_SMBUS_I2C_BLOCK
;
469 static int __maybe_unused
zx2967_i2c_suspend(struct device
*dev
)
471 struct zx2967_i2c
*i2c
= dev_get_drvdata(dev
);
473 i2c
->is_suspended
= true;
474 clk_disable_unprepare(i2c
->clk
);
479 static int __maybe_unused
zx2967_i2c_resume(struct device
*dev
)
481 struct zx2967_i2c
*i2c
= dev_get_drvdata(dev
);
483 i2c
->is_suspended
= false;
484 clk_prepare_enable(i2c
->clk
);
489 static SIMPLE_DEV_PM_OPS(zx2967_i2c_dev_pm_ops
,
490 zx2967_i2c_suspend
, zx2967_i2c_resume
);
492 static const struct i2c_algorithm zx2967_i2c_algo
= {
493 .master_xfer
= zx2967_i2c_xfer
,
494 .smbus_xfer
= zx2967_smbus_xfer
,
495 .functionality
= zx2967_i2c_func
,
498 static const struct i2c_adapter_quirks zx2967_i2c_quirks
= {
499 .flags
= I2C_AQ_NO_ZERO_LEN
,
502 static const struct of_device_id zx2967_i2c_of_match
[] = {
503 { .compatible
= "zte,zx296718-i2c", },
506 MODULE_DEVICE_TABLE(of
, zx2967_i2c_of_match
);
508 static int zx2967_i2c_probe(struct platform_device
*pdev
)
510 struct zx2967_i2c
*i2c
;
511 void __iomem
*reg_base
;
512 struct resource
*res
;
516 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
520 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
521 reg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
522 if (IS_ERR(reg_base
))
523 return PTR_ERR(reg_base
);
525 clk
= devm_clk_get(&pdev
->dev
, NULL
);
527 dev_err(&pdev
->dev
, "missing controller clock");
531 ret
= clk_prepare_enable(clk
);
533 dev_err(&pdev
->dev
, "failed to enable i2c_clk\n");
537 ret
= device_property_read_u32(&pdev
->dev
, "clock-frequency",
540 dev_err(&pdev
->dev
, "missing clock-frequency");
544 ret
= platform_get_irq(pdev
, 0);
549 i2c
->reg_base
= reg_base
;
552 init_completion(&i2c
->complete
);
553 platform_set_drvdata(pdev
, i2c
);
555 ret
= zx2967_i2c_reset_hardware(i2c
);
557 dev_err(&pdev
->dev
, "failed to initialize i2c controller\n");
558 goto err_clk_unprepare
;
561 ret
= devm_request_irq(&pdev
->dev
, i2c
->irq
,
562 zx2967_i2c_isr
, 0, dev_name(&pdev
->dev
), i2c
);
564 dev_err(&pdev
->dev
, "failed to request irq %i\n", i2c
->irq
);
565 goto err_clk_unprepare
;
568 i2c_set_adapdata(&i2c
->adap
, i2c
);
569 strlcpy(i2c
->adap
.name
, "zx2967 i2c adapter",
570 sizeof(i2c
->adap
.name
));
571 i2c
->adap
.algo
= &zx2967_i2c_algo
;
572 i2c
->adap
.quirks
= &zx2967_i2c_quirks
;
573 i2c
->adap
.nr
= pdev
->id
;
574 i2c
->adap
.dev
.parent
= &pdev
->dev
;
575 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
577 ret
= i2c_add_numbered_adapter(&i2c
->adap
);
579 goto err_clk_unprepare
;
584 clk_disable_unprepare(i2c
->clk
);
588 static int zx2967_i2c_remove(struct platform_device
*pdev
)
590 struct zx2967_i2c
*i2c
= platform_get_drvdata(pdev
);
592 i2c_del_adapter(&i2c
->adap
);
593 clk_disable_unprepare(i2c
->clk
);
598 static struct platform_driver zx2967_i2c_driver
= {
599 .probe
= zx2967_i2c_probe
,
600 .remove
= zx2967_i2c_remove
,
602 .name
= "zx2967_i2c",
603 .of_match_table
= zx2967_i2c_of_match
,
604 .pm
= &zx2967_i2c_dev_pm_ops
,
607 module_platform_driver(zx2967_i2c_driver
);
609 MODULE_AUTHOR("Baoyou Xie <baoyou.xie@linaro.org>");
610 MODULE_DESCRIPTION("ZTE ZX2967 I2C Bus Controller driver");
611 MODULE_LICENSE("GPL v2");