1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Actions Semiconductor Owl SoC's I2C driver
5 * Copyright (c) 2014 Actions Semi Inc.
6 * Author: David Liu <liuwei@actions-semi.com>
8 * Copyright (c) 2018 Linaro Ltd.
9 * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
17 #include <linux/iopoll.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
22 #define OWL_I2C_REG_CTL 0x0000
23 #define OWL_I2C_REG_CLKDIV 0x0004
24 #define OWL_I2C_REG_STAT 0x0008
25 #define OWL_I2C_REG_ADDR 0x000C
26 #define OWL_I2C_REG_TXDAT 0x0010
27 #define OWL_I2C_REG_RXDAT 0x0014
28 #define OWL_I2C_REG_CMD 0x0018
29 #define OWL_I2C_REG_FIFOCTL 0x001C
30 #define OWL_I2C_REG_FIFOSTAT 0x0020
31 #define OWL_I2C_REG_DATCNT 0x0024
32 #define OWL_I2C_REG_RCNT 0x0028
34 /* I2Cx_CTL Bit Mask */
35 #define OWL_I2C_CTL_RB BIT(1)
36 #define OWL_I2C_CTL_GBCC(x) (((x) & 0x3) << 2)
37 #define OWL_I2C_CTL_GBCC_NONE OWL_I2C_CTL_GBCC(0)
38 #define OWL_I2C_CTL_GBCC_START OWL_I2C_CTL_GBCC(1)
39 #define OWL_I2C_CTL_GBCC_STOP OWL_I2C_CTL_GBCC(2)
40 #define OWL_I2C_CTL_GBCC_RSTART OWL_I2C_CTL_GBCC(3)
41 #define OWL_I2C_CTL_IRQE BIT(5)
42 #define OWL_I2C_CTL_EN BIT(7)
43 #define OWL_I2C_CTL_AE BIT(8)
44 #define OWL_I2C_CTL_SHSM BIT(10)
46 #define OWL_I2C_DIV_FACTOR(x) ((x) & 0xff)
48 /* I2Cx_STAT Bit Mask */
49 #define OWL_I2C_STAT_RACK BIT(0)
50 #define OWL_I2C_STAT_BEB BIT(1)
51 #define OWL_I2C_STAT_IRQP BIT(2)
52 #define OWL_I2C_STAT_LAB BIT(3)
53 #define OWL_I2C_STAT_STPD BIT(4)
54 #define OWL_I2C_STAT_STAD BIT(5)
55 #define OWL_I2C_STAT_BBB BIT(6)
56 #define OWL_I2C_STAT_TCB BIT(7)
57 #define OWL_I2C_STAT_LBST BIT(8)
58 #define OWL_I2C_STAT_SAMB BIT(9)
59 #define OWL_I2C_STAT_SRGC BIT(10)
61 /* I2Cx_CMD Bit Mask */
62 #define OWL_I2C_CMD_SBE BIT(0)
63 #define OWL_I2C_CMD_RBE BIT(4)
64 #define OWL_I2C_CMD_DE BIT(8)
65 #define OWL_I2C_CMD_NS BIT(9)
66 #define OWL_I2C_CMD_SE BIT(10)
67 #define OWL_I2C_CMD_MSS BIT(11)
68 #define OWL_I2C_CMD_WRS BIT(12)
69 #define OWL_I2C_CMD_SECL BIT(15)
71 #define OWL_I2C_CMD_AS(x) (((x) & 0x7) << 1)
72 #define OWL_I2C_CMD_SAS(x) (((x) & 0x7) << 5)
74 /* I2Cx_FIFOCTL Bit Mask */
75 #define OWL_I2C_FIFOCTL_NIB BIT(0)
76 #define OWL_I2C_FIFOCTL_RFR BIT(1)
77 #define OWL_I2C_FIFOCTL_TFR BIT(2)
79 /* I2Cc_FIFOSTAT Bit Mask */
80 #define OWL_I2C_FIFOSTAT_CECB BIT(0)
81 #define OWL_I2C_FIFOSTAT_RNB BIT(1)
82 #define OWL_I2C_FIFOSTAT_RFE BIT(2)
83 #define OWL_I2C_FIFOSTAT_TFF BIT(5)
84 #define OWL_I2C_FIFOSTAT_TFD GENMASK(23, 16)
85 #define OWL_I2C_FIFOSTAT_RFD GENMASK(15, 8)
88 #define OWL_I2C_TIMEOUT_MS (4 * 1000)
89 #define OWL_I2C_TIMEOUT msecs_to_jiffies(OWL_I2C_TIMEOUT_MS)
91 #define OWL_I2C_MAX_RETRIES 50
94 struct i2c_adapter adap;
96 struct completion msg_complete;
100 unsigned long clk_rate;
106 static void owl_i2c_update_reg(void __iomem *reg, unsigned int val, bool state)
120 static void owl_i2c_reset(struct owl_i2c_dev *i2c_dev)
122 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
123 OWL_I2C_CTL_EN, false);
125 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
126 OWL_I2C_CTL_EN, true);
128 /* Clear status registers */
129 writel(0, i2c_dev->base + OWL_I2C_REG_STAT);
132 static int owl_i2c_reset_fifo(struct owl_i2c_dev *i2c_dev)
134 unsigned int val, timeout = 0;
137 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
138 OWL_I2C_FIFOCTL_RFR | OWL_I2C_FIFOCTL_TFR,
141 /* Wait 50ms for FIFO reset complete */
143 val = readl(i2c_dev->base + OWL_I2C_REG_FIFOCTL);
144 if (!(val & (OWL_I2C_FIFOCTL_RFR | OWL_I2C_FIFOCTL_TFR)))
146 usleep_range(500, 1000);
147 } while (timeout++ < OWL_I2C_MAX_RETRIES);
149 if (timeout > OWL_I2C_MAX_RETRIES) {
150 dev_err(&i2c_dev->adap.dev, "FIFO reset timeout\n");
157 static void owl_i2c_set_freq(struct owl_i2c_dev *i2c_dev)
161 val = DIV_ROUND_UP(i2c_dev->clk_rate, i2c_dev->bus_freq * 16);
163 /* Set clock divider factor */
164 writel(OWL_I2C_DIV_FACTOR(val), i2c_dev->base + OWL_I2C_REG_CLKDIV);
167 static void owl_i2c_xfer_data(struct owl_i2c_dev *i2c_dev)
169 struct i2c_msg *msg = i2c_dev->msg;
170 unsigned int stat, fifostat;
174 /* Handle NACK from slave */
175 fifostat = readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT);
176 if (fifostat & OWL_I2C_FIFOSTAT_RNB) {
177 i2c_dev->err = -ENXIO;
178 /* Clear NACK error bit by writing "1" */
179 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOSTAT,
180 OWL_I2C_FIFOSTAT_RNB, true);
184 /* Handle bus error */
185 stat = readl(i2c_dev->base + OWL_I2C_REG_STAT);
186 if (stat & OWL_I2C_STAT_BEB) {
188 /* Clear BUS error bit by writing "1" */
189 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT,
190 OWL_I2C_STAT_BEB, true);
194 /* Handle FIFO read */
195 if (msg->flags & I2C_M_RD) {
196 while ((readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
197 OWL_I2C_FIFOSTAT_RFE) && i2c_dev->msg_ptr < msg->len) {
198 msg->buf[i2c_dev->msg_ptr++] = readl(i2c_dev->base +
202 /* Handle the remaining bytes which were not sent */
203 while (!(readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
204 OWL_I2C_FIFOSTAT_TFF) && i2c_dev->msg_ptr < msg->len) {
205 writel(msg->buf[i2c_dev->msg_ptr++],
206 i2c_dev->base + OWL_I2C_REG_TXDAT);
211 static irqreturn_t owl_i2c_interrupt(int irq, void *_dev)
213 struct owl_i2c_dev *i2c_dev = _dev;
215 spin_lock(&i2c_dev->lock);
217 owl_i2c_xfer_data(i2c_dev);
219 /* Clear pending interrupts */
220 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT,
221 OWL_I2C_STAT_IRQP, true);
223 complete_all(&i2c_dev->msg_complete);
224 spin_unlock(&i2c_dev->lock);
229 static u32 owl_i2c_func(struct i2c_adapter *adap)
231 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
234 static int owl_i2c_check_bus_busy(struct i2c_adapter *adap)
236 struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
237 unsigned long timeout;
239 /* Check for Bus busy */
240 timeout = jiffies + OWL_I2C_TIMEOUT;
241 while (readl(i2c_dev->base + OWL_I2C_REG_STAT) & OWL_I2C_STAT_BBB) {
242 if (time_after(jiffies, timeout)) {
243 dev_err(&adap->dev, "Bus busy timeout\n");
251 static int owl_i2c_xfer_common(struct i2c_adapter *adap, struct i2c_msg *msgs,
252 int num, bool atomic)
254 struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
256 unsigned long time_left, flags;
257 unsigned int i2c_cmd, val;
261 spin_lock_irqsave(&i2c_dev->lock, flags);
263 /* Reset I2C controller */
264 owl_i2c_reset(i2c_dev);
266 /* Set bus frequency */
267 owl_i2c_set_freq(i2c_dev);
270 * Spinlock should be released before calling reset FIFO and
271 * bus busy check since those functions may sleep
273 spin_unlock_irqrestore(&i2c_dev->lock, flags);
276 ret = owl_i2c_reset_fifo(i2c_dev);
278 goto unlocked_err_exit;
280 /* Check for bus busy */
281 ret = owl_i2c_check_bus_busy(adap);
283 goto unlocked_err_exit;
285 spin_lock_irqsave(&i2c_dev->lock, flags);
287 /* Check for Arbitration lost */
288 val = readl(i2c_dev->base + OWL_I2C_REG_STAT);
289 if (val & OWL_I2C_STAT_LAB) {
290 val &= ~OWL_I2C_STAT_LAB;
291 writel(val, i2c_dev->base + OWL_I2C_REG_STAT);
297 reinit_completion(&i2c_dev->msg_complete);
299 /* Enable/disable I2C controller interrupt */
300 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
301 OWL_I2C_CTL_IRQE, !atomic);
304 * Select: FIFO enable, Master mode, Stop enable, Data count enable,
307 i2c_cmd = OWL_I2C_CMD_SECL | OWL_I2C_CMD_MSS | OWL_I2C_CMD_SE |
308 OWL_I2C_CMD_NS | OWL_I2C_CMD_DE | OWL_I2C_CMD_SBE;
310 /* Handle repeated start condition */
312 /* Set internal address length and enable repeated start */
313 i2c_cmd |= OWL_I2C_CMD_AS(msgs[0].len + 1) |
314 OWL_I2C_CMD_SAS(1) | OWL_I2C_CMD_RBE;
316 /* Write slave address */
317 addr = i2c_8bit_addr_from_msg(&msgs[0]);
318 writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT);
320 /* Write internal register address */
321 for (idx = 0; idx < msgs[0].len; idx++)
322 writel(msgs[0].buf[idx],
323 i2c_dev->base + OWL_I2C_REG_TXDAT);
327 /* Set address length */
328 i2c_cmd |= OWL_I2C_CMD_AS(1);
333 i2c_dev->msg_ptr = 0;
335 /* Set data count for the message */
336 writel(msg->len, i2c_dev->base + OWL_I2C_REG_DATCNT);
338 addr = i2c_8bit_addr_from_msg(msg);
339 writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT);
341 if (!(msg->flags & I2C_M_RD)) {
342 /* Write data to FIFO */
343 for (idx = 0; idx < msg->len; idx++) {
344 /* Check for FIFO full */
345 if (readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
346 OWL_I2C_FIFOSTAT_TFF)
349 writel(msg->buf[idx],
350 i2c_dev->base + OWL_I2C_REG_TXDAT);
353 i2c_dev->msg_ptr = idx;
356 /* Ignore the NACK if needed */
357 if (msg->flags & I2C_M_IGNORE_NAK)
358 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
359 OWL_I2C_FIFOCTL_NIB, true);
361 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
362 OWL_I2C_FIFOCTL_NIB, false);
364 /* Start the transfer */
365 writel(i2c_cmd, i2c_dev->base + OWL_I2C_REG_CMD);
367 spin_unlock_irqrestore(&i2c_dev->lock, flags);
370 /* Wait for Command Execute Completed or NACK Error bits */
371 ret = readl_poll_timeout_atomic(i2c_dev->base + OWL_I2C_REG_FIFOSTAT,
372 val, val & (OWL_I2C_FIFOSTAT_CECB |
373 OWL_I2C_FIFOSTAT_RNB),
374 10, OWL_I2C_TIMEOUT_MS * 1000);
376 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
382 spin_lock_irqsave(&i2c_dev->lock, flags);
385 dev_err(&adap->dev, "Transaction timed out\n");
386 /* Send stop condition and release the bus */
387 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
388 OWL_I2C_CTL_GBCC_STOP | OWL_I2C_CTL_RB,
394 owl_i2c_xfer_data(i2c_dev);
396 ret = i2c_dev->err < 0 ? i2c_dev->err : num;
399 spin_unlock_irqrestore(&i2c_dev->lock, flags);
402 /* Disable I2C controller */
403 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
404 OWL_I2C_CTL_EN, false);
409 static int owl_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
412 return owl_i2c_xfer_common(adap, msgs, num, false);
415 static int owl_i2c_xfer_atomic(struct i2c_adapter *adap,
416 struct i2c_msg *msgs, int num)
418 return owl_i2c_xfer_common(adap, msgs, num, true);
421 static const struct i2c_algorithm owl_i2c_algorithm = {
422 .master_xfer = owl_i2c_xfer,
423 .master_xfer_atomic = owl_i2c_xfer_atomic,
424 .functionality = owl_i2c_func,
427 static const struct i2c_adapter_quirks owl_i2c_quirks = {
428 .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST,
430 .max_write_len = 240,
431 .max_comb_1st_msg_len = 6,
432 .max_comb_2nd_msg_len = 240,
435 static int owl_i2c_probe(struct platform_device *pdev)
437 struct device *dev = &pdev->dev;
438 struct owl_i2c_dev *i2c_dev;
441 i2c_dev = devm_kzalloc(dev, sizeof(*i2c_dev), GFP_KERNEL);
445 i2c_dev->base = devm_platform_ioremap_resource(pdev, 0);
446 if (IS_ERR(i2c_dev->base))
447 return PTR_ERR(i2c_dev->base);
449 irq = platform_get_irq(pdev, 0);
453 if (of_property_read_u32(dev->of_node, "clock-frequency",
455 i2c_dev->bus_freq = I2C_MAX_STANDARD_MODE_FREQ;
457 /* We support only frequencies of 100k and 400k for now */
458 if (i2c_dev->bus_freq != I2C_MAX_STANDARD_MODE_FREQ &&
459 i2c_dev->bus_freq != I2C_MAX_FAST_MODE_FREQ) {
460 dev_err(dev, "invalid clock-frequency %d\n", i2c_dev->bus_freq);
464 i2c_dev->clk = devm_clk_get(dev, NULL);
465 if (IS_ERR(i2c_dev->clk)) {
466 dev_err(dev, "failed to get clock\n");
467 return PTR_ERR(i2c_dev->clk);
470 ret = clk_prepare_enable(i2c_dev->clk);
474 i2c_dev->clk_rate = clk_get_rate(i2c_dev->clk);
475 if (!i2c_dev->clk_rate) {
476 dev_err(dev, "input clock rate should not be zero\n");
481 init_completion(&i2c_dev->msg_complete);
482 spin_lock_init(&i2c_dev->lock);
483 i2c_dev->adap.owner = THIS_MODULE;
484 i2c_dev->adap.algo = &owl_i2c_algorithm;
485 i2c_dev->adap.timeout = OWL_I2C_TIMEOUT;
486 i2c_dev->adap.quirks = &owl_i2c_quirks;
487 i2c_dev->adap.dev.parent = dev;
488 i2c_dev->adap.dev.of_node = dev->of_node;
489 snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name),
490 "%s", "OWL I2C adapter");
491 i2c_set_adapdata(&i2c_dev->adap, i2c_dev);
493 platform_set_drvdata(pdev, i2c_dev);
495 ret = devm_request_irq(dev, irq, owl_i2c_interrupt, 0, pdev->name,
498 dev_err(dev, "failed to request irq %d\n", irq);
502 return i2c_add_adapter(&i2c_dev->adap);
505 clk_disable_unprepare(i2c_dev->clk);
510 static const struct of_device_id owl_i2c_of_match[] = {
511 { .compatible = "actions,s500-i2c" },
512 { .compatible = "actions,s700-i2c" },
513 { .compatible = "actions,s900-i2c" },
516 MODULE_DEVICE_TABLE(of, owl_i2c_of_match);
518 static struct platform_driver owl_i2c_driver = {
519 .probe = owl_i2c_probe,
522 .of_match_table = of_match_ptr(owl_i2c_of_match),
523 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
526 module_platform_driver(owl_i2c_driver);
528 MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
529 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
530 MODULE_DESCRIPTION("Actions Semiconductor Owl SoC's I2C driver");
531 MODULE_LICENSE("GPL");