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>
21 /* I2C registers */
87 /* I2C bus timeout */
122 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_reset()
125 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_reset()
129 writel(0, i2c_dev->base + OWL_I2C_REG_STAT); in owl_i2c_reset()
137 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, in owl_i2c_reset_fifo()
143 val = readl(i2c_dev->base + OWL_I2C_REG_FIFOCTL); in owl_i2c_reset_fifo()
150 dev_err(&i2c_dev->adap.dev, "FIFO reset timeout\n"); in owl_i2c_reset_fifo()
151 return -ETIMEDOUT; in owl_i2c_reset_fifo()
161 val = DIV_ROUND_UP(i2c_dev->clk_rate, i2c_dev->bus_freq * 16); in owl_i2c_set_freq()
164 writel(OWL_I2C_DIV_FACTOR(val), i2c_dev->base + OWL_I2C_REG_CLKDIV); in owl_i2c_set_freq()
169 struct i2c_msg *msg = i2c_dev->msg; in owl_i2c_xfer_data()
172 i2c_dev->err = 0; in owl_i2c_xfer_data()
175 fifostat = readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT); in owl_i2c_xfer_data()
177 i2c_dev->err = -ENXIO; in owl_i2c_xfer_data()
179 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOSTAT, in owl_i2c_xfer_data()
185 stat = readl(i2c_dev->base + OWL_I2C_REG_STAT); in owl_i2c_xfer_data()
187 i2c_dev->err = -EIO; in owl_i2c_xfer_data()
189 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT, in owl_i2c_xfer_data()
195 if (msg->flags & I2C_M_RD) { in owl_i2c_xfer_data()
196 while ((readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & in owl_i2c_xfer_data()
197 OWL_I2C_FIFOSTAT_RFE) && i2c_dev->msg_ptr < msg->len) { in owl_i2c_xfer_data()
198 msg->buf[i2c_dev->msg_ptr++] = readl(i2c_dev->base + in owl_i2c_xfer_data()
203 while (!(readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & in owl_i2c_xfer_data()
204 OWL_I2C_FIFOSTAT_TFF) && i2c_dev->msg_ptr < msg->len) { in owl_i2c_xfer_data()
205 writel(msg->buf[i2c_dev->msg_ptr++], in owl_i2c_xfer_data()
206 i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_data()
215 spin_lock(&i2c_dev->lock); in owl_i2c_interrupt()
220 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT, in owl_i2c_interrupt()
223 complete_all(&i2c_dev->msg_complete); in owl_i2c_interrupt()
224 spin_unlock(&i2c_dev->lock); in owl_i2c_interrupt()
241 while (readl(i2c_dev->base + OWL_I2C_REG_STAT) & OWL_I2C_STAT_BBB) { in owl_i2c_check_bus_busy()
243 dev_err(&adap->dev, "Bus busy timeout\n"); in owl_i2c_check_bus_busy()
244 return -ETIMEDOUT; in owl_i2c_check_bus_busy()
261 spin_lock_irqsave(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
263 /* Reset I2C controller */ in owl_i2c_xfer_common()
273 spin_unlock_irqrestore(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
285 spin_lock_irqsave(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
288 val = readl(i2c_dev->base + OWL_I2C_REG_STAT); in owl_i2c_xfer_common()
291 writel(val, i2c_dev->base + OWL_I2C_REG_STAT); in owl_i2c_xfer_common()
292 ret = -EAGAIN; in owl_i2c_xfer_common()
297 reinit_completion(&i2c_dev->msg_complete); in owl_i2c_xfer_common()
299 /* Enable/disable I2C controller interrupt */ in owl_i2c_xfer_common()
300 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_xfer_common()
318 writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_common()
323 i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_common()
332 i2c_dev->msg = msg; in owl_i2c_xfer_common()
333 i2c_dev->msg_ptr = 0; in owl_i2c_xfer_common()
336 writel(msg->len, i2c_dev->base + OWL_I2C_REG_DATCNT); in owl_i2c_xfer_common()
339 writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_common()
341 if (!(msg->flags & I2C_M_RD)) { in owl_i2c_xfer_common()
343 for (idx = 0; idx < msg->len; idx++) { in owl_i2c_xfer_common()
345 if (readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & in owl_i2c_xfer_common()
349 writel(msg->buf[idx], in owl_i2c_xfer_common()
350 i2c_dev->base + OWL_I2C_REG_TXDAT); in owl_i2c_xfer_common()
353 i2c_dev->msg_ptr = idx; in owl_i2c_xfer_common()
357 if (msg->flags & I2C_M_IGNORE_NAK) in owl_i2c_xfer_common()
358 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, in owl_i2c_xfer_common()
361 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, in owl_i2c_xfer_common()
365 writel(i2c_cmd, i2c_dev->base + OWL_I2C_REG_CMD); in owl_i2c_xfer_common()
367 spin_unlock_irqrestore(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
371 ret = readl_poll_timeout_atomic(i2c_dev->base + OWL_I2C_REG_FIFOSTAT, in owl_i2c_xfer_common()
376 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete, in owl_i2c_xfer_common()
377 adap->timeout); in owl_i2c_xfer_common()
379 ret = -ETIMEDOUT; in owl_i2c_xfer_common()
382 spin_lock_irqsave(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
385 dev_err(&adap->dev, "Transaction timed out\n"); in owl_i2c_xfer_common()
387 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_xfer_common()
396 ret = i2c_dev->err < 0 ? i2c_dev->err : num; in owl_i2c_xfer_common()
399 spin_unlock_irqrestore(&i2c_dev->lock, flags); in owl_i2c_xfer_common()
402 /* Disable I2C controller */ in owl_i2c_xfer_common()
403 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, in owl_i2c_xfer_common()
437 struct device *dev = &pdev->dev; in owl_i2c_probe()
443 return -ENOMEM; in owl_i2c_probe()
445 i2c_dev->base = devm_platform_ioremap_resource(pdev, 0); in owl_i2c_probe()
446 if (IS_ERR(i2c_dev->base)) in owl_i2c_probe()
447 return PTR_ERR(i2c_dev->base); in owl_i2c_probe()
453 if (of_property_read_u32(dev->of_node, "clock-frequency", in owl_i2c_probe()
454 &i2c_dev->bus_freq)) in owl_i2c_probe()
455 i2c_dev->bus_freq = I2C_MAX_STANDARD_MODE_FREQ; in owl_i2c_probe()
458 if (i2c_dev->bus_freq != I2C_MAX_STANDARD_MODE_FREQ && in owl_i2c_probe()
459 i2c_dev->bus_freq != I2C_MAX_FAST_MODE_FREQ) { in owl_i2c_probe()
460 dev_err(dev, "invalid clock-frequency %d\n", i2c_dev->bus_freq); in owl_i2c_probe()
461 return -EINVAL; in owl_i2c_probe()
464 i2c_dev->clk = devm_clk_get(dev, NULL); in owl_i2c_probe()
465 if (IS_ERR(i2c_dev->clk)) { in owl_i2c_probe()
467 return PTR_ERR(i2c_dev->clk); in owl_i2c_probe()
470 ret = clk_prepare_enable(i2c_dev->clk); in owl_i2c_probe()
474 i2c_dev->clk_rate = clk_get_rate(i2c_dev->clk); in owl_i2c_probe()
475 if (!i2c_dev->clk_rate) { in owl_i2c_probe()
477 ret = -EINVAL; in owl_i2c_probe()
481 init_completion(&i2c_dev->msg_complete); in owl_i2c_probe()
482 spin_lock_init(&i2c_dev->lock); in owl_i2c_probe()
483 i2c_dev->adap.owner = THIS_MODULE; in owl_i2c_probe()
484 i2c_dev->adap.algo = &owl_i2c_algorithm; in owl_i2c_probe()
485 i2c_dev->adap.timeout = OWL_I2C_TIMEOUT; in owl_i2c_probe()
486 i2c_dev->adap.quirks = &owl_i2c_quirks; in owl_i2c_probe()
487 i2c_dev->adap.dev.parent = dev; in owl_i2c_probe()
488 i2c_dev->adap.dev.of_node = dev->of_node; in owl_i2c_probe()
489 snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name), in owl_i2c_probe()
490 "%s", "OWL I2C adapter"); in owl_i2c_probe()
491 i2c_set_adapdata(&i2c_dev->adap, i2c_dev); in owl_i2c_probe()
495 ret = devm_request_irq(dev, irq, owl_i2c_interrupt, 0, pdev->name, in owl_i2c_probe()
502 return i2c_add_adapter(&i2c_dev->adap); in owl_i2c_probe()
505 clk_disable_unprepare(i2c_dev->clk); in owl_i2c_probe()
511 { .compatible = "actions,s500-i2c" },
512 { .compatible = "actions,s700-i2c" },
513 { .compatible = "actions,s900-i2c" },
521 .name = "owl-i2c",
528 MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
530 MODULE_DESCRIPTION("Actions Semiconductor Owl SoC's I2C driver");