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;
72 static void zx2967_i2c_writel(struct zx2967_i2c *i2c,
73 u32 val, unsigned long reg)
75 writel_relaxed(val, i2c->reg_base + reg);
78 static u32 zx2967_i2c_readl(struct zx2967_i2c *i2c, unsigned long reg)
80 return readl_relaxed(i2c->reg_base + reg);
83 static void zx2967_i2c_writesb(struct zx2967_i2c *i2c,
84 void *data, unsigned long reg, int len)
86 writesb(i2c->reg_base + reg, data, len);
89 static void zx2967_i2c_readsb(struct zx2967_i2c *i2c,
90 void *data, unsigned long reg, int len)
92 readsb(i2c->reg_base + reg, data, len);
95 static void zx2967_i2c_start_ctrl(struct zx2967_i2c *i2c)
100 status = zx2967_i2c_readl(i2c, REG_STAT);
101 status |= I2C_IRQ_ACK_CLEAR;
102 zx2967_i2c_writel(i2c, status, REG_STAT);
104 ctl = zx2967_i2c_readl(i2c, REG_CMD);
109 ctl &= ~I2C_CMB_RW_EN;
111 zx2967_i2c_writel(i2c, ctl, REG_CMD);
114 static void zx2967_i2c_flush_fifos(struct zx2967_i2c *i2c)
121 val = I2C_RFIFO_RESET;
124 val = I2C_WFIFO_RESET;
127 val |= zx2967_i2c_readl(i2c, offset);
128 zx2967_i2c_writel(i2c, val, offset);
131 static int zx2967_i2c_empty_rx_fifo(struct zx2967_i2c *i2c, u32 size)
133 u8 val[I2C_FIFO_MAX] = {0};
136 if (size > I2C_FIFO_MAX) {
137 dev_err(DEV(i2c), "fifo size %d over the max value %d\n",
142 zx2967_i2c_readsb(i2c, val, REG_DATA, size);
143 for (i = 0; i < size; i++) {
144 *i2c->cur_trans++ = val[i];
153 static int zx2967_i2c_fill_tx_fifo(struct zx2967_i2c *i2c)
155 size_t residue = i2c->residue;
156 u8 *buf = i2c->cur_trans;
159 dev_err(DEV(i2c), "residue is %d\n", (int)residue);
163 if (residue <= I2C_FIFO_MAX) {
164 zx2967_i2c_writesb(i2c, buf, REG_DATA, residue);
166 /* Again update before writing to FIFO to make sure isr sees. */
168 i2c->cur_trans = NULL;
170 zx2967_i2c_writesb(i2c, buf, REG_DATA, I2C_FIFO_MAX);
171 i2c->residue -= I2C_FIFO_MAX;
172 i2c->cur_trans += I2C_FIFO_MAX;
180 static int zx2967_i2c_reset_hardware(struct zx2967_i2c *i2c)
185 val = I2C_MASTER | I2C_IRQ_MSK_ENABLE;
186 zx2967_i2c_writel(i2c, val, REG_CMD);
188 clk_div = clk_get_rate(i2c->clk) / i2c->clk_freq - 1;
189 zx2967_i2c_writel(i2c, clk_div, REG_CLK_DIV_FS);
190 zx2967_i2c_writel(i2c, clk_div, REG_CLK_DIV_HS);
192 zx2967_i2c_writel(i2c, I2C_FIFO_MAX - 1, REG_WRCONF);
193 zx2967_i2c_writel(i2c, I2C_FIFO_MAX - 1, REG_RDCONF);
194 zx2967_i2c_writel(i2c, 1, REG_RDCONF);
196 zx2967_i2c_flush_fifos(i2c);
201 static void zx2967_i2c_isr_clr(struct zx2967_i2c *i2c)
205 status = zx2967_i2c_readl(i2c, REG_STAT);
206 status |= I2C_IRQ_ACK_CLEAR;
207 zx2967_i2c_writel(i2c, status, REG_STAT);
210 static irqreturn_t zx2967_i2c_isr(int irq, void *dev_id)
213 struct zx2967_i2c *i2c = (struct zx2967_i2c *)dev_id;
215 status = zx2967_i2c_readl(i2c, REG_STAT) & I2C_INT_MASK;
216 zx2967_i2c_isr_clr(i2c);
218 if (status & I2C_SR_EDEVICE)
220 else if (status & I2C_SR_EDATA)
222 else if (status & I2C_TRANS_DONE)
227 complete(&i2c->complete);
232 static void zx2967_set_addr(struct zx2967_i2c *i2c, u16 addr)
236 val = (addr >> I2C_ADDR_LOW_SHIFT) & I2C_ADDR_LOW_MASK;
237 zx2967_i2c_writel(i2c, val, REG_DEVADDR_L);
239 val = (addr >> I2C_ADDR_HI_SHIFT) & I2C_ADDR_HI_MASK;
240 zx2967_i2c_writel(i2c, val, REG_DEVADDR_H);
242 val = zx2967_i2c_readl(i2c, REG_CMD) | I2C_ADDR_MODE_TEN;
244 val = zx2967_i2c_readl(i2c, REG_CMD) & ~I2C_ADDR_MODE_TEN;
245 zx2967_i2c_writel(i2c, val, REG_CMD);
248 static int zx2967_i2c_xfer_bytes(struct zx2967_i2c *i2c, u32 bytes)
250 unsigned long time_left;
251 int rd = i2c->msg_rd;
254 reinit_completion(&i2c->complete);
257 zx2967_i2c_writel(i2c, bytes - 1, REG_RDCONF);
259 ret = zx2967_i2c_fill_tx_fifo(i2c);
264 zx2967_i2c_start_ctrl(i2c);
266 time_left = wait_for_completion_timeout(&i2c->complete,
274 return rd ? zx2967_i2c_empty_rx_fifo(i2c, bytes) : 0;
277 static int zx2967_i2c_xfer_msg(struct zx2967_i2c *i2c,
283 zx2967_i2c_flush_fifos(i2c);
285 i2c->cur_trans = msg->buf;
286 i2c->residue = msg->len;
287 i2c->access_cnt = msg->len / I2C_FIFO_MAX;
288 i2c->msg_rd = msg->flags & I2C_M_RD;
290 for (i = 0; i < i2c->access_cnt; i++) {
291 ret = zx2967_i2c_xfer_bytes(i2c, I2C_FIFO_MAX);
296 if (i2c->residue > 0) {
297 ret = zx2967_i2c_xfer_bytes(i2c, i2c->residue);
308 static int zx2967_i2c_xfer(struct i2c_adapter *adap,
309 struct i2c_msg *msgs, int num)
311 struct zx2967_i2c *i2c = i2c_get_adapdata(adap);
315 zx2967_set_addr(i2c, msgs->addr);
317 for (i = 0; i < num; i++) {
318 ret = zx2967_i2c_xfer_msg(i2c, &msgs[i]);
327 zx2967_smbus_xfer_prepare(struct zx2967_i2c *i2c, u16 addr,
328 char read_write, u8 command, int size,
329 union i2c_smbus_data *data)
333 val = zx2967_i2c_readl(i2c, REG_RDCONF);
334 val |= I2C_RFIFO_RESET;
335 zx2967_i2c_writel(i2c, val, REG_RDCONF);
336 zx2967_set_addr(i2c, addr);
337 val = zx2967_i2c_readl(i2c, REG_CMD);
339 zx2967_i2c_writel(i2c, val, REG_CMD);
343 zx2967_i2c_writel(i2c, command, REG_DATA);
345 case I2C_SMBUS_BYTE_DATA:
346 zx2967_i2c_writel(i2c, command, REG_DATA);
347 if (read_write == I2C_SMBUS_WRITE)
348 zx2967_i2c_writel(i2c, data->byte, REG_DATA);
350 case I2C_SMBUS_WORD_DATA:
351 zx2967_i2c_writel(i2c, command, REG_DATA);
352 if (read_write == I2C_SMBUS_WRITE) {
353 zx2967_i2c_writel(i2c, (data->word >> 8), REG_DATA);
354 zx2967_i2c_writel(i2c, (data->word & 0xff),
361 static int zx2967_smbus_xfer_read(struct zx2967_i2c *i2c, int size,
362 union i2c_smbus_data *data)
364 unsigned long time_left;
368 reinit_completion(&i2c->complete);
370 val = zx2967_i2c_readl(i2c, REG_CMD);
371 val |= I2C_CMB_RW_EN;
372 zx2967_i2c_writel(i2c, val, REG_CMD);
374 val = zx2967_i2c_readl(i2c, REG_CMD);
376 zx2967_i2c_writel(i2c, val, REG_CMD);
378 time_left = wait_for_completion_timeout(&i2c->complete,
388 case I2C_SMBUS_BYTE_DATA:
389 val = zx2967_i2c_readl(i2c, REG_DATA);
392 case I2C_SMBUS_WORD_DATA:
393 case I2C_SMBUS_PROC_CALL:
394 buf[0] = zx2967_i2c_readl(i2c, REG_DATA);
395 buf[1] = zx2967_i2c_readl(i2c, REG_DATA);
396 data->word = (buf[0] << 8) | buf[1];
405 static int zx2967_smbus_xfer_write(struct zx2967_i2c *i2c)
407 unsigned long time_left;
410 reinit_completion(&i2c->complete);
411 val = zx2967_i2c_readl(i2c, REG_CMD);
413 zx2967_i2c_writel(i2c, val, REG_CMD);
415 time_left = wait_for_completion_timeout(&i2c->complete,
426 static int zx2967_smbus_xfer(struct i2c_adapter *adap, u16 addr,
427 unsigned short flags, char read_write,
428 u8 command, int size, union i2c_smbus_data *data)
430 struct zx2967_i2c *i2c = i2c_get_adapdata(adap);
432 if (size == I2C_SMBUS_QUICK)
433 read_write = I2C_SMBUS_WRITE;
436 case I2C_SMBUS_QUICK:
438 case I2C_SMBUS_BYTE_DATA:
439 case I2C_SMBUS_WORD_DATA:
440 zx2967_smbus_xfer_prepare(i2c, addr, read_write,
441 command, size, data);
447 if (read_write == I2C_SMBUS_READ)
448 return zx2967_smbus_xfer_read(i2c, size, data);
450 return zx2967_smbus_xfer_write(i2c);
453 static u32 zx2967_i2c_func(struct i2c_adapter *adap)
455 return I2C_FUNC_I2C |
456 I2C_FUNC_SMBUS_QUICK |
457 I2C_FUNC_SMBUS_BYTE |
458 I2C_FUNC_SMBUS_BYTE_DATA |
459 I2C_FUNC_SMBUS_WORD_DATA |
460 I2C_FUNC_SMBUS_BLOCK_DATA |
461 I2C_FUNC_SMBUS_PROC_CALL |
462 I2C_FUNC_SMBUS_I2C_BLOCK;
465 static int __maybe_unused zx2967_i2c_suspend(struct device *dev)
467 struct zx2967_i2c *i2c = dev_get_drvdata(dev);
469 i2c_mark_adapter_suspended(&i2c->adap);
470 clk_disable_unprepare(i2c->clk);
475 static int __maybe_unused zx2967_i2c_resume(struct device *dev)
477 struct zx2967_i2c *i2c = dev_get_drvdata(dev);
479 clk_prepare_enable(i2c->clk);
480 i2c_mark_adapter_resumed(&i2c->adap);
485 static SIMPLE_DEV_PM_OPS(zx2967_i2c_dev_pm_ops,
486 zx2967_i2c_suspend, zx2967_i2c_resume);
488 static const struct i2c_algorithm zx2967_i2c_algo = {
489 .master_xfer = zx2967_i2c_xfer,
490 .smbus_xfer = zx2967_smbus_xfer,
491 .functionality = zx2967_i2c_func,
494 static const struct i2c_adapter_quirks zx2967_i2c_quirks = {
495 .flags = I2C_AQ_NO_ZERO_LEN,
498 static const struct of_device_id zx2967_i2c_of_match[] = {
499 { .compatible = "zte,zx296718-i2c", },
502 MODULE_DEVICE_TABLE(of, zx2967_i2c_of_match);
504 static int zx2967_i2c_probe(struct platform_device *pdev)
506 struct zx2967_i2c *i2c;
507 void __iomem *reg_base;
508 struct resource *res;
512 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
516 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
517 reg_base = devm_ioremap_resource(&pdev->dev, res);
518 if (IS_ERR(reg_base))
519 return PTR_ERR(reg_base);
521 clk = devm_clk_get(&pdev->dev, NULL);
523 dev_err(&pdev->dev, "missing controller clock");
527 ret = clk_prepare_enable(clk);
529 dev_err(&pdev->dev, "failed to enable i2c_clk\n");
533 ret = device_property_read_u32(&pdev->dev, "clock-frequency",
536 dev_err(&pdev->dev, "missing clock-frequency");
540 ret = platform_get_irq(pdev, 0);
545 i2c->reg_base = reg_base;
548 init_completion(&i2c->complete);
549 platform_set_drvdata(pdev, i2c);
551 ret = zx2967_i2c_reset_hardware(i2c);
553 dev_err(&pdev->dev, "failed to initialize i2c controller\n");
554 goto err_clk_unprepare;
557 ret = devm_request_irq(&pdev->dev, i2c->irq,
558 zx2967_i2c_isr, 0, dev_name(&pdev->dev), i2c);
560 dev_err(&pdev->dev, "failed to request irq %i\n", i2c->irq);
561 goto err_clk_unprepare;
564 i2c_set_adapdata(&i2c->adap, i2c);
565 strlcpy(i2c->adap.name, "zx2967 i2c adapter",
566 sizeof(i2c->adap.name));
567 i2c->adap.algo = &zx2967_i2c_algo;
568 i2c->adap.quirks = &zx2967_i2c_quirks;
569 i2c->adap.nr = pdev->id;
570 i2c->adap.dev.parent = &pdev->dev;
571 i2c->adap.dev.of_node = pdev->dev.of_node;
573 ret = i2c_add_numbered_adapter(&i2c->adap);
575 goto err_clk_unprepare;
580 clk_disable_unprepare(i2c->clk);
584 static int zx2967_i2c_remove(struct platform_device *pdev)
586 struct zx2967_i2c *i2c = platform_get_drvdata(pdev);
588 i2c_del_adapter(&i2c->adap);
589 clk_disable_unprepare(i2c->clk);
594 static struct platform_driver zx2967_i2c_driver = {
595 .probe = zx2967_i2c_probe,
596 .remove = zx2967_i2c_remove,
598 .name = "zx2967_i2c",
599 .of_match_table = zx2967_i2c_of_match,
600 .pm = &zx2967_i2c_dev_pm_ops,
603 module_platform_driver(zx2967_i2c_driver);
605 MODULE_AUTHOR("Baoyou Xie <baoyou.xie@linaro.org>");
606 MODULE_DESCRIPTION("ZTE ZX2967 I2C Bus Controller driver");
607 MODULE_LICENSE("GPL v2");