Lines Matching +full:s900 +full:- +full:i2c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Actions Semiconductor Owl SoC's I2C driver
6 * Author: David Liu <liuwei@actions-semi.com>
14 #include <linux/i2c.h>
22 /* I2C registers */
88 /* I2C bus timeout */
123 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_reset()
126 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_reset()
130 writel(0, i2c_dev->base + OWL_I2C_REG_STAT); in owl_i2c_reset()
138 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, in owl_i2c_reset_fifo()
144 val = readl(i2c_dev->base + OWL_I2C_REG_FIFOCTL); in owl_i2c_reset_fifo()
151 dev_err(&i2c_dev->adap.dev, "FIFO reset timeout\n"); in owl_i2c_reset_fifo()
152 return -ETIMEDOUT; in owl_i2c_reset_fifo()
162 val = DIV_ROUND_UP(i2c_dev->clk_rate, i2c_dev->bus_freq * 16); in owl_i2c_set_freq()
165 writel(OWL_I2C_DIV_FACTOR(val), i2c_dev->base + OWL_I2C_REG_CLKDIV); in owl_i2c_set_freq()
170 struct i2c_msg *msg = i2c_dev->msg; in owl_i2c_xfer_data()
173 i2c_dev->err = 0; in owl_i2c_xfer_data()
176 fifostat = readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT); in owl_i2c_xfer_data()
178 i2c_dev->err = -ENXIO; in owl_i2c_xfer_data()
180 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOSTAT, in owl_i2c_xfer_data()
186 stat = readl(i2c_dev->base + OWL_I2C_REG_STAT); in owl_i2c_xfer_data()
188 i2c_dev->err = -EIO; in owl_i2c_xfer_data()
190 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT, in owl_i2c_xfer_data()
196 if (msg->flags & I2C_M_RD) { in owl_i2c_xfer_data()
197 while ((readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & in owl_i2c_xfer_data()
198 OWL_I2C_FIFOSTAT_RFE) && i2c_dev->msg_ptr < msg->len) { in owl_i2c_xfer_data()
199 msg->buf[i2c_dev->msg_ptr++] = readl(i2c_dev->base + in owl_i2c_xfer_data()
204 while (!(readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & in owl_i2c_xfer_data()
205 OWL_I2C_FIFOSTAT_TFF) && i2c_dev->msg_ptr < msg->len) { in owl_i2c_xfer_data()
206 writel(msg->buf[i2c_dev->msg_ptr++], in owl_i2c_xfer_data()
207 i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_data()
216 spin_lock(&i2c_dev->lock); in owl_i2c_interrupt()
221 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT, in owl_i2c_interrupt()
224 complete_all(&i2c_dev->msg_complete); in owl_i2c_interrupt()
225 spin_unlock(&i2c_dev->lock); in owl_i2c_interrupt()
242 while (readl(i2c_dev->base + OWL_I2C_REG_STAT) & OWL_I2C_STAT_BBB) { in owl_i2c_check_bus_busy()
244 dev_err(&adap->dev, "Bus busy timeout\n"); in owl_i2c_check_bus_busy()
245 return -ETIMEDOUT; in owl_i2c_check_bus_busy()
262 spin_lock_irqsave(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
264 /* Reset I2C controller */ in owl_i2c_xfer_common()
274 spin_unlock_irqrestore(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
286 spin_lock_irqsave(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
289 val = readl(i2c_dev->base + OWL_I2C_REG_STAT); in owl_i2c_xfer_common()
292 writel(val, i2c_dev->base + OWL_I2C_REG_STAT); in owl_i2c_xfer_common()
293 ret = -EAGAIN; in owl_i2c_xfer_common()
298 reinit_completion(&i2c_dev->msg_complete); in owl_i2c_xfer_common()
300 /* Enable/disable I2C controller interrupt */ in owl_i2c_xfer_common()
301 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_xfer_common()
319 writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_common()
324 i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_common()
333 i2c_dev->msg = msg; in owl_i2c_xfer_common()
334 i2c_dev->msg_ptr = 0; in owl_i2c_xfer_common()
337 writel(msg->len, i2c_dev->base + OWL_I2C_REG_DATCNT); in owl_i2c_xfer_common()
340 writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_common()
342 if (!(msg->flags & I2C_M_RD)) { in owl_i2c_xfer_common()
344 for (idx = 0; idx < msg->len; idx++) { in owl_i2c_xfer_common()
346 if (readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & in owl_i2c_xfer_common()
350 writel(msg->buf[idx], in owl_i2c_xfer_common()
351 i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_common()
354 i2c_dev->msg_ptr = idx; in owl_i2c_xfer_common()
358 if (msg->flags & I2C_M_IGNORE_NAK) in owl_i2c_xfer_common()
359 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, in owl_i2c_xfer_common()
362 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, in owl_i2c_xfer_common()
366 writel(i2c_cmd, i2c_dev->base + OWL_I2C_REG_CMD); in owl_i2c_xfer_common()
368 spin_unlock_irqrestore(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
372 ret = readl_poll_timeout_atomic(i2c_dev->base + OWL_I2C_REG_FIFOSTAT, in owl_i2c_xfer_common()
377 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete, in owl_i2c_xfer_common()
378 adap->timeout); in owl_i2c_xfer_common()
380 ret = -ETIMEDOUT; in owl_i2c_xfer_common()
383 spin_lock_irqsave(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
386 dev_err(&adap->dev, "Transaction timed out\n"); in owl_i2c_xfer_common()
388 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_xfer_common()
397 ret = i2c_dev->err < 0 ? i2c_dev->err : num; in owl_i2c_xfer_common()
400 spin_unlock_irqrestore(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
403 /* Disable I2C controller */ in owl_i2c_xfer_common()
404 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_xfer_common()
438 struct device *dev = &pdev->dev; in owl_i2c_probe()
444 return -ENOMEM; in owl_i2c_probe()
446 i2c_dev->base = devm_platform_ioremap_resource(pdev, 0); in owl_i2c_probe()
447 if (IS_ERR(i2c_dev->base)) in owl_i2c_probe()
448 return PTR_ERR(i2c_dev->base); in owl_i2c_probe()
454 if (of_property_read_u32(dev->of_node, "clock-frequency", in owl_i2c_probe()
455 &i2c_dev->bus_freq)) in owl_i2c_probe()
456 i2c_dev->bus_freq = I2C_MAX_STANDARD_MODE_FREQ; in owl_i2c_probe()
459 if (i2c_dev->bus_freq != I2C_MAX_STANDARD_MODE_FREQ && in owl_i2c_probe()
460 i2c_dev->bus_freq != I2C_MAX_FAST_MODE_FREQ) { in owl_i2c_probe()
461 dev_err(dev, "invalid clock-frequency %d\n", i2c_dev->bus_freq); in owl_i2c_probe()
462 return -EINVAL; in owl_i2c_probe()
465 i2c_dev->clk = devm_clk_get_enabled(dev, NULL); in owl_i2c_probe()
466 if (IS_ERR(i2c_dev->clk)) { in owl_i2c_probe()
468 return PTR_ERR(i2c_dev->clk); in owl_i2c_probe()
471 i2c_dev->clk_rate = clk_get_rate(i2c_dev->clk); in owl_i2c_probe()
472 if (!i2c_dev->clk_rate) { in owl_i2c_probe()
474 return -EINVAL; in owl_i2c_probe()
477 init_completion(&i2c_dev->msg_complete); in owl_i2c_probe()
478 spin_lock_init(&i2c_dev->lock); in owl_i2c_probe()
479 i2c_dev->adap.owner = THIS_MODULE; in owl_i2c_probe()
480 i2c_dev->adap.algo = &owl_i2c_algorithm; in owl_i2c_probe()
481 i2c_dev->adap.timeout = OWL_I2C_TIMEOUT; in owl_i2c_probe()
482 i2c_dev->adap.quirks = &owl_i2c_quirks; in owl_i2c_probe()
483 i2c_dev->adap.dev.parent = dev; in owl_i2c_probe()
484 i2c_dev->adap.dev.of_node = dev->of_node; in owl_i2c_probe()
485 snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name), in owl_i2c_probe()
486 "%s", "OWL I2C adapter"); in owl_i2c_probe()
487 i2c_set_adapdata(&i2c_dev->adap, i2c_dev); in owl_i2c_probe()
491 ret = devm_request_irq(dev, irq, owl_i2c_interrupt, 0, pdev->name, in owl_i2c_probe()
498 return i2c_add_adapter(&i2c_dev->adap); in owl_i2c_probe()
502 { .compatible = "actions,s500-i2c" },
503 { .compatible = "actions,s700-i2c" },
504 { .compatible = "actions,s900-i2c" },
512 .name = "owl-i2c",
519 MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
521 MODULE_DESCRIPTION("Actions Semiconductor Owl SoC's I2C driver");