1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/of_irq.h>
3 #include "i2c-viai2c-common.h"
5 int viai2c_wait_bus_not_busy(struct viai2c *i2c)
9 timeout = jiffies + VIAI2C_TIMEOUT;
10 while (!(readw(i2c->base + VIAI2C_REG_CSR) & VIAI2C_CSR_READY_MASK)) {
11 if (time_after(jiffies, timeout)) {
12 dev_warn(i2c->dev, "timeout waiting for bus ready\n");
21 static int viai2c_write(struct viai2c *i2c, struct i2c_msg *pmsg, int last)
23 u16 val, tcr_val = i2c->tcr;
29 * We still need to run through the while (..) once, so
30 * start at -1 and break out early from the loop
33 writew(0, i2c->base + VIAI2C_REG_CDR);
35 writew(pmsg->buf[0] & 0xFF, i2c->base + VIAI2C_REG_CDR);
38 if (i2c->platform == VIAI2C_PLAT_WMT && !(pmsg->flags & I2C_M_NOSTART)) {
39 val = readw(i2c->base + VIAI2C_REG_CR);
40 val &= ~VIAI2C_CR_TX_END;
41 val |= VIAI2C_CR_CPU_RDY;
42 writew(val, i2c->base + VIAI2C_REG_CR);
45 reinit_completion(&i2c->complete);
47 tcr_val |= pmsg->addr & VIAI2C_TCR_ADDR_MASK;
49 writew(tcr_val, i2c->base + VIAI2C_REG_TCR);
51 if (i2c->platform == VIAI2C_PLAT_WMT && pmsg->flags & I2C_M_NOSTART) {
52 val = readw(i2c->base + VIAI2C_REG_CR);
53 val |= VIAI2C_CR_CPU_RDY;
54 writew(val, i2c->base + VIAI2C_REG_CR);
57 if (!wait_for_completion_timeout(&i2c->complete, VIAI2C_TIMEOUT))
63 static int viai2c_read(struct viai2c *i2c, struct i2c_msg *pmsg, bool first)
65 u16 val, tcr_val = i2c->tcr;
67 val = readw(i2c->base + VIAI2C_REG_CR);
68 val &= ~(VIAI2C_CR_TX_END | VIAI2C_CR_RX_END);
70 if (i2c->platform == VIAI2C_PLAT_WMT && !(pmsg->flags & I2C_M_NOSTART))
71 val |= VIAI2C_CR_CPU_RDY;
74 val |= VIAI2C_CR_RX_END;
76 writew(val, i2c->base + VIAI2C_REG_CR);
78 reinit_completion(&i2c->complete);
80 tcr_val |= VIAI2C_TCR_READ | (pmsg->addr & VIAI2C_TCR_ADDR_MASK);
82 writew(tcr_val, i2c->base + VIAI2C_REG_TCR);
84 if ((i2c->platform == VIAI2C_PLAT_WMT && (pmsg->flags & I2C_M_NOSTART)) ||
85 (i2c->platform == VIAI2C_PLAT_ZHAOXIN && !first)) {
86 val = readw(i2c->base + VIAI2C_REG_CR);
87 val |= VIAI2C_CR_CPU_RDY;
88 writew(val, i2c->base + VIAI2C_REG_CR);
91 if (!wait_for_completion_timeout(&i2c->complete, VIAI2C_TIMEOUT))
97 int viai2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
102 struct viai2c *i2c = i2c_get_adapdata(adap);
104 i2c->mode = VIAI2C_BYTE_MODE;
105 for (i = 0; ret >= 0 && i < num; i++) {
107 if (i2c->platform == VIAI2C_PLAT_WMT && !(pmsg->flags & I2C_M_NOSTART)) {
108 ret = viai2c_wait_bus_not_busy(i2c);
116 if (pmsg->flags & I2C_M_RD)
117 ret = viai2c_read(i2c, pmsg, i == 0);
119 ret = viai2c_write(i2c, pmsg, (i + 1) == num);
122 return (ret < 0) ? ret : i;
126 * Main process of the byte mode xfer
128 * Return value indicates whether the transfer is complete
129 * 1: all the data has been successfully transferred
130 * 0: there is still data that needs to be transferred
131 * -EIO: error occurred
133 static int viai2c_irq_xfer(struct viai2c *i2c)
136 struct i2c_msg *msg = i2c->msg;
137 u8 read = msg->flags & I2C_M_RD;
138 void __iomem *base = i2c->base;
141 msg->buf[i2c->xfered_len] = readw(base + VIAI2C_REG_CDR) >> 8;
143 val = readw(base + VIAI2C_REG_CR) | VIAI2C_CR_CPU_RDY;
144 if (i2c->xfered_len == msg->len - 2)
145 val |= VIAI2C_CR_RX_END;
146 writew(val, base + VIAI2C_REG_CR);
148 val = readw(base + VIAI2C_REG_CSR);
149 if (val & VIAI2C_CSR_RCV_NOT_ACK)
152 /* I2C_SMBUS_QUICK */
154 val = VIAI2C_CR_TX_END | VIAI2C_CR_CPU_RDY | VIAI2C_CR_ENABLE;
155 writew(val, base + VIAI2C_REG_CR);
159 if ((i2c->xfered_len + 1) == msg->len) {
160 if (i2c->platform == VIAI2C_PLAT_WMT && !i2c->last)
161 writew(VIAI2C_CR_ENABLE, base + VIAI2C_REG_CR);
162 else if (i2c->platform == VIAI2C_PLAT_ZHAOXIN && i2c->last)
163 writeb(VIAI2C_CR_TX_END, base + VIAI2C_REG_CR);
165 writew(msg->buf[i2c->xfered_len + 1] & 0xFF, base + VIAI2C_REG_CDR);
166 writew(VIAI2C_CR_CPU_RDY | VIAI2C_CR_ENABLE, base + VIAI2C_REG_CR);
172 return i2c->xfered_len == msg->len;
175 int __weak viai2c_fifo_irq_xfer(struct viai2c *i2c, bool irq)
180 static irqreturn_t viai2c_isr(int irq, void *data)
182 struct viai2c *i2c = data;
185 /* save the status and write-clear it */
186 status = readw(i2c->base + VIAI2C_REG_ISR);
187 if (!status && i2c->platform == VIAI2C_PLAT_ZHAOXIN)
190 writew(status, i2c->base + VIAI2C_REG_ISR);
193 if (status & VIAI2C_ISR_NACK_ADDR)
196 if (i2c->platform == VIAI2C_PLAT_WMT && (status & VIAI2C_ISR_SCL_TIMEOUT))
197 i2c->ret = -ETIMEDOUT;
200 if (i2c->mode == VIAI2C_BYTE_MODE)
201 i2c->ret = viai2c_irq_xfer(i2c);
203 i2c->ret = viai2c_fifo_irq_xfer(i2c, true);
206 /* All the data has been successfully transferred or error occurred */
208 complete(&i2c->complete);
213 int viai2c_init(struct platform_device *pdev, struct viai2c **pi2c, int plat)
218 struct device_node *np = pdev->dev.of_node;
220 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
224 i2c->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
225 if (IS_ERR(i2c->base))
226 return PTR_ERR(i2c->base);
228 if (plat == VIAI2C_PLAT_WMT) {
230 i2c->irq = irq_of_parse_and_map(np, 0);
233 } else if (plat == VIAI2C_PLAT_ZHAOXIN) {
234 irq_flags = IRQF_SHARED;
235 i2c->irq = platform_get_irq(pdev, 0);
239 return dev_err_probe(&pdev->dev, -EINVAL, "wrong platform type\n");
242 i2c->platform = plat;
244 err = devm_request_irq(&pdev->dev, i2c->irq, viai2c_isr,
245 irq_flags, pdev->name, i2c);
247 return dev_err_probe(&pdev->dev, err,
248 "failed to request irq %i\n", i2c->irq);
250 i2c->dev = &pdev->dev;
251 init_completion(&i2c->complete);
252 platform_set_drvdata(pdev, i2c);