Lines Matching +full:axi4 +full:- +full:lite

1 // SPDX-License-Identifier: GPL-2.0-only
3 * i2c-xiic.c
4 * Copyright (c) 2002-2007 Xilinx Inc.
5 * Copyright (c) 2009-2010 Intel Corporation
27 #include <linux/platform_data/i2c-xiic.h>
34 #define DRIVER_NAME "xiic-i2c"
56 * struct xiic_i2c - Internal representation of the XIIC I2C bus
67 * @endianness: big/little-endian byte order
68 * @clk: Pointer to AXI4-lite input clock
106 * struct timing_regs - AXI I2C timing registers that depend on I2C spec
172 #define XIIC_SR_MSTR_RDING_SLAVE_MASK 0x08 /* 1=Dir: mstr <-- slave */
235 #define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos)
236 #define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos)
242 * For the register read and write functions, a little-endian and big-endian
246 * big-endian systems.
251 if (i2c->endianness == LITTLE) in xiic_setreg8()
252 iowrite8(value, i2c->base + reg); in xiic_setreg8()
254 iowrite8(value, i2c->base + reg + 3); in xiic_setreg8()
261 if (i2c->endianness == LITTLE) in xiic_getreg8()
262 ret = ioread8(i2c->base + reg); in xiic_getreg8()
264 ret = ioread8(i2c->base + reg + 3); in xiic_getreg8()
270 if (i2c->endianness == LITTLE) in xiic_setreg16()
271 iowrite16(value, i2c->base + reg); in xiic_setreg16()
273 iowrite16be(value, i2c->base + reg + 2); in xiic_setreg16()
278 if (i2c->endianness == LITTLE) in xiic_setreg32()
279 iowrite32(value, i2c->base + reg); in xiic_setreg32()
281 iowrite32be(value, i2c->base + reg); in xiic_setreg32()
288 if (i2c->endianness == LITTLE) in xiic_getreg32()
289 ret = ioread32(i2c->base + reg); in xiic_getreg32()
291 ret = ioread32be(i2c->base + reg); in xiic_getreg32()
333 dev_err(i2c->dev, "Failed to clear rx fifo\n"); in xiic_clear_rx_fifo()
334 return -ETIMEDOUT; in xiic_clear_rx_fifo()
351 dev_err(i2c->dev, "Timeout waiting at Tx empty\n"); in xiic_wait_tx_empty()
352 return -ETIMEDOUT; in xiic_wait_tx_empty()
360 * xiic_setclk - Sets the configured clock rate
369 * -EINVAL on failure (scl frequency not supported or THIGH is 0)
377 dev_dbg(i2c->adap.dev.parent, in xiic_setclk()
378 "%s entry, i2c->input_clk: %ld, i2c->i2c_clk: %d\n", in xiic_setclk()
379 __func__, i2c->input_clk, i2c->i2c_clk); in xiic_setclk()
382 if (!i2c->i2c_clk || !i2c->input_clk) in xiic_setclk()
385 clk_in_mhz = DIV_ROUND_UP(i2c->input_clk, 1000000); in xiic_setclk()
387 switch (i2c->i2c_clk) { in xiic_setclk()
398 dev_warn(i2c->adap.dev.parent, "Unsupported scl frequency\n"); in xiic_setclk()
399 return -EINVAL; in xiic_setclk()
409 /* THIGH - Depends on SCL clock frequency(i2c_clk) as below */ in xiic_setclk()
410 reg_val = (DIV_ROUND_UP(i2c->input_clk, 2 * i2c->i2c_clk)) - 7; in xiic_setclk()
412 return -EINVAL; in xiic_setclk()
414 xiic_setreg32(i2c, XIIC_THIGH_REG_OFFSET, reg_val - 1); in xiic_setclk()
416 /* TLOW - Value same as THIGH */ in xiic_setclk()
417 xiic_setreg32(i2c, XIIC_TLOW_REG_OFFSET, reg_val - 1); in xiic_setclk()
421 xiic_setreg32(i2c, XIIC_TSUSTA_REG_OFFSET, reg_val - 1); in xiic_setclk()
425 xiic_setreg32(i2c, XIIC_TSUSTO_REG_OFFSET, reg_val - 1); in xiic_setclk()
429 xiic_setreg32(i2c, XIIC_THDSTA_REG_OFFSET, reg_val - 1); in xiic_setclk()
433 xiic_setreg32(i2c, XIIC_TSUDAT_REG_OFFSET, reg_val - 1); in xiic_setclk()
437 xiic_setreg32(i2c, XIIC_TBUF_REG_OFFSET, reg_val - 1); in xiic_setclk()
456 xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, IIC_RX_FIFO_DEPTH - 1); in xiic_reinit()
496 i2c->rx_msg->flags &= ~I2C_M_RECV_LEN; in xiic_smbus_block_read_setup()
499 i2c->smbus_block_read = true; in xiic_smbus_block_read_setup()
504 i2c->rx_msg->buf[i2c->rx_pos++] = rxmsg_len; in xiic_smbus_block_read_setup()
514 rfd_set = IIC_RX_FIFO_DEPTH - 1; in xiic_smbus_block_read_setup()
515 i2c->rx_msg->len = rxmsg_len + 1; in xiic_smbus_block_read_setup()
524 i2c->rx_msg->len = SMBUS_BLOCK_READ_MIN_LEN; in xiic_smbus_block_read_setup()
530 rfd_set = rxmsg_len - 2; in xiic_smbus_block_read_setup()
531 i2c->rx_msg->len = rxmsg_len + 1; in xiic_smbus_block_read_setup()
539 i2c->tx_msg->len = 3; in xiic_smbus_block_read_setup()
540 i2c->smbus_block_read = false; in xiic_smbus_block_read_setup()
541 dev_err(i2c->adap.dev.parent, "smbus_block_read Invalid msg length\n"); in xiic_smbus_block_read_setup()
552 dev_dbg(i2c->adap.dev.parent, in xiic_read_rx()
563 if (!i2c->dynamic) { in xiic_read_rx()
564 bytes_rem = xiic_rx_space(i2c) - bytes_in_fifo; in xiic_read_rx()
567 if (i2c->rx_msg->flags & I2C_M_RECV_LEN) { in xiic_read_rx()
575 bytes_to_read = bytes_rem - 1; in xiic_read_rx()
586 if (i2c->nmsgs == 1) { in xiic_read_rx()
601 i2c->rx_msg->buf[i2c->rx_pos++] = in xiic_read_rx()
605 if (i2c->dynamic) { in xiic_read_rx()
610 bytes--; in xiic_read_rx()
618 return IIC_TX_FIFO_DEPTH - xiic_getreg8(i2c, XIIC_TFO_REG_OFFSET) - 1; in xiic_tx_fifo_space()
628 dev_dbg(i2c->adap.dev.parent, "%s entry, len: %d, fifo space: %d\n", in xiic_fill_tx_fifo()
631 while (len--) { in xiic_fill_tx_fifo()
632 u16 data = i2c->tx_msg->buf[i2c->tx_pos++]; in xiic_fill_tx_fifo()
634 if (!xiic_tx_space(i2c) && i2c->nmsgs == 1) { in xiic_fill_tx_fifo()
635 /* last message in transfer -> STOP */ in xiic_fill_tx_fifo()
636 if (i2c->dynamic) { in xiic_fill_tx_fifo()
652 dev_dbg(i2c->adap.dev.parent, "%s TX STOP\n", __func__); in xiic_fill_tx_fifo()
660 i2c->tx_msg = NULL; in xiic_wakeup()
661 i2c->rx_msg = NULL; in xiic_wakeup()
662 i2c->nmsgs = 0; in xiic_wakeup()
663 i2c->state = code; in xiic_wakeup()
664 complete(&i2c->completion); in xiic_wakeup()
682 mutex_lock(&i2c->lock); in xiic_process()
687 dev_dbg(i2c->adap.dev.parent, "%s: IER: 0x%x, ISR: 0x%x, pend: 0x%x\n", in xiic_process()
689 dev_dbg(i2c->adap.dev.parent, "%s: SR: 0x%x, msg: %p, nmsgs: %d\n", in xiic_process()
691 i2c->tx_msg, i2c->nmsgs); in xiic_process()
692 dev_dbg(i2c->adap.dev.parent, "%s, ISR: 0x%x, CR: 0x%x\n", in xiic_process()
706 dev_dbg(i2c->adap.dev.parent, "%s error\n", __func__); in xiic_process()
714 dev_dbg(i2c->adap.dev.parent, "reinit failed\n"); in xiic_process()
716 if (i2c->rx_msg) { in xiic_process()
720 if (i2c->tx_msg) { in xiic_process()
731 if (!i2c->rx_msg) { in xiic_process()
732 dev_dbg(i2c->adap.dev.parent, in xiic_process()
741 i2c->rx_msg = NULL; in xiic_process()
746 dev_dbg(i2c->adap.dev.parent, in xiic_process()
748 __func__, i2c->nmsgs); in xiic_process()
754 if (i2c->nmsgs > 1) { in xiic_process()
755 i2c->nmsgs--; in xiic_process()
756 i2c->tx_msg++; in xiic_process()
757 dev_dbg(i2c->adap.dev.parent, in xiic_process()
769 if (!i2c->tx_msg) { in xiic_process()
770 dev_dbg(i2c->adap.dev.parent, in xiic_process()
779 dev_dbg(i2c->adap.dev.parent, in xiic_process()
781 __func__, i2c->nmsgs); in xiic_process()
785 if (i2c->nmsgs > 1 && (pend & XIIC_INTR_TX_EMPTY_MASK)) { in xiic_process()
786 i2c->nmsgs--; in xiic_process()
787 i2c->tx_msg++; in xiic_process()
792 dev_dbg(i2c->adap.dev.parent, in xiic_process()
806 if (i2c->tx_msg && i2c->smbus_block_read) { in xiic_process()
807 i2c->smbus_block_read = false; in xiic_process()
809 i2c->tx_msg->len = 1; in xiic_process()
812 if (!i2c->tx_msg) in xiic_process()
817 if (i2c->nmsgs == 1 && !i2c->rx_msg && in xiic_process()
825 dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr); in xiic_process()
835 mutex_unlock(&i2c->lock); in xiic_process()
843 return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0; in xiic_bus_busy()
856 while (err && tries--) { in xiic_wait_not_busy()
868 struct i2c_msg *msg = i2c->rx_msg = i2c->tx_msg; in xiic_start_recv()
870 dev_dbg(i2c->adap.dev.parent, "%s entry, ISR: 0x%x, CR: 0x%x\n", in xiic_start_recv()
877 if (i2c->dynamic) { in xiic_start_recv()
893 rx_watermark = msg->len; in xiic_start_recv()
897 bytes--; in xiic_start_recv()
906 val = (i2c->nmsgs == 1) ? XIIC_TX_DYN_STOP_MASK : 0; in xiic_start_recv()
907 val |= msg->len; in xiic_start_recv()
919 if (i2c->prev_msg_tx) { in xiic_start_recv()
930 rx_watermark = msg->len; in xiic_start_recv()
932 rfd_set = IIC_RX_FIFO_DEPTH - 1; in xiic_start_recv()
934 rfd_set = rx_watermark - 1; in xiic_start_recv()
937 if (!(i2c->rx_msg->flags & I2C_M_RECV_LEN)) { in xiic_start_recv()
944 rfd_set = rx_watermark - 2; in xiic_start_recv()
970 dev_dbg(i2c->adap.dev.parent, "%s end, ISR: 0x%x, CR: 0x%x\n", in xiic_start_recv()
975 if (i2c->nmsgs == 1) in xiic_start_recv()
980 i2c->tx_pos = msg->len; in xiic_start_recv()
985 i2c->prev_msg_tx = false; in xiic_start_recv()
992 struct i2c_msg *msg = i2c->tx_msg; in xiic_start_send()
994 dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, len: %d", in xiic_start_send()
995 __func__, msg, msg->len); in xiic_start_send()
996 dev_dbg(i2c->adap.dev.parent, "%s entry, ISR: 0x%x, CR: 0x%x\n", in xiic_start_send()
1000 if (i2c->dynamic) { in xiic_start_send()
1005 if (i2c->nmsgs == 1 && msg->len == 0) in xiic_start_send()
1006 /* no data and last message -> add STOP */ in xiic_start_send()
1015 ((i2c->nmsgs > 1 || xiic_tx_space(i2c)) ? in xiic_start_send()
1026 if (i2c->prev_msg_tx) { in xiic_start_send()
1063 i2c->prev_msg_tx = true; in xiic_start_send()
1070 dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, fifos space: %d\n", in __xiic_start_xfer()
1071 __func__, i2c->tx_msg, fifo_space); in __xiic_start_xfer()
1073 if (!i2c->tx_msg) in __xiic_start_xfer()
1076 i2c->rx_pos = 0; in __xiic_start_xfer()
1077 i2c->tx_pos = 0; in __xiic_start_xfer()
1078 i2c->state = STATE_START; in __xiic_start_xfer()
1079 if (i2c->tx_msg->flags & I2C_M_RD) { in __xiic_start_xfer()
1092 mutex_lock(&i2c->lock); in xiic_start_xfer()
1094 if (i2c->tx_msg || i2c->rx_msg) { in xiic_start_xfer()
1095 dev_err(i2c->adap.dev.parent, in xiic_start_xfer()
1097 ret = -EBUSY; in xiic_start_xfer()
1106 if (!i2c->singlemaster) { in xiic_start_xfer()
1111 * then try to recover by re-initializing the controller and check in xiic_start_xfer()
1114 dev_warn(i2c->adap.dev.parent, "I2C bus busy timeout, reinitializing\n"); in xiic_start_xfer()
1124 i2c->tx_msg = msgs; in xiic_start_xfer()
1125 i2c->rx_msg = NULL; in xiic_start_xfer()
1126 i2c->nmsgs = num; in xiic_start_xfer()
1127 init_completion(&i2c->completion); in xiic_start_xfer()
1130 i2c->dynamic = true; in xiic_start_xfer()
1133 i2c->prev_msg_tx = false; in xiic_start_xfer()
1140 * in xlnx,axi-iic-2.0 / xlnx,xps-iic-2.00.a IP versions. in xiic_start_xfer()
1144 for (count = 0; count < i2c->nmsgs; count++) { in xiic_start_xfer()
1145 broken_read = (i2c->quirks & DYNAMIC_MODE_READ_BROKEN_BIT) && in xiic_start_xfer()
1146 (i2c->tx_msg[count].flags & I2C_M_RD); in xiic_start_xfer()
1147 max_read_len = (i2c->tx_msg[count].flags & I2C_M_RD) && in xiic_start_xfer()
1148 (i2c->tx_msg[count].len > MAX_READ_LENGTH_DYNAMIC); in xiic_start_xfer()
1149 smbus_blk_read = (i2c->tx_msg[count].flags & I2C_M_RECV_LEN); in xiic_start_xfer()
1152 i2c->dynamic = false; in xiic_start_xfer()
1162 mutex_unlock(&i2c->lock); in xiic_start_xfer()
1172 dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__, in xiic_xfer()
1175 err = pm_runtime_resume_and_get(i2c->dev); in xiic_xfer()
1183 err = wait_for_completion_timeout(&i2c->completion, XIIC_XFER_TIMEOUT); in xiic_xfer()
1184 mutex_lock(&i2c->lock); in xiic_xfer()
1186 i2c->tx_msg = NULL; in xiic_xfer()
1187 i2c->rx_msg = NULL; in xiic_xfer()
1188 i2c->nmsgs = 0; in xiic_xfer()
1189 err = -ETIMEDOUT; in xiic_xfer()
1191 err = (i2c->state == STATE_DONE) ? num : -EIO; in xiic_xfer()
1193 mutex_unlock(&i2c->lock); in xiic_xfer()
1196 pm_runtime_mark_last_busy(i2c->dev); in xiic_xfer()
1197 pm_runtime_put_autosuspend(i2c->dev); in xiic_xfer()
1223 { .compatible = "xlnx,xps-iic-2.00.a", .data = &xiic_2_00 },
1224 { .compatible = "xlnx,axi-iic-2.1", },
1240 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); in xiic_i2c_probe()
1242 return -ENOMEM; in xiic_i2c_probe()
1244 match = of_match_node(xiic_of_match, pdev->dev.of_node); in xiic_i2c_probe()
1245 if (match && match->data) { in xiic_i2c_probe()
1246 const struct xiic_version_data *data = match->data; in xiic_i2c_probe()
1248 i2c->quirks = data->quirks; in xiic_i2c_probe()
1251 i2c->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); in xiic_i2c_probe()
1252 if (IS_ERR(i2c->base)) in xiic_i2c_probe()
1253 return PTR_ERR(i2c->base); in xiic_i2c_probe()
1259 pdata = dev_get_platdata(&pdev->dev); in xiic_i2c_probe()
1263 i2c->adap = xiic_adapter; in xiic_i2c_probe()
1264 i2c_set_adapdata(&i2c->adap, i2c); in xiic_i2c_probe()
1265 i2c->adap.dev.parent = &pdev->dev; in xiic_i2c_probe()
1266 i2c->adap.dev.of_node = pdev->dev.of_node; in xiic_i2c_probe()
1267 snprintf(i2c->adap.name, sizeof(i2c->adap.name), in xiic_i2c_probe()
1268 DRIVER_NAME " %s", pdev->name); in xiic_i2c_probe()
1270 mutex_init(&i2c->lock); in xiic_i2c_probe()
1272 i2c->clk = devm_clk_get_enabled(&pdev->dev, NULL); in xiic_i2c_probe()
1273 if (IS_ERR(i2c->clk)) in xiic_i2c_probe()
1274 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk), in xiic_i2c_probe()
1277 i2c->dev = &pdev->dev; in xiic_i2c_probe()
1278 pm_runtime_set_autosuspend_delay(i2c->dev, XIIC_PM_TIMEOUT); in xiic_i2c_probe()
1279 pm_runtime_use_autosuspend(i2c->dev); in xiic_i2c_probe()
1280 pm_runtime_set_active(i2c->dev); in xiic_i2c_probe()
1281 pm_runtime_enable(i2c->dev); in xiic_i2c_probe()
1284 i2c->input_clk = clk_get_rate(i2c->clk); in xiic_i2c_probe()
1285 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency", in xiic_i2c_probe()
1286 &i2c->i2c_clk); in xiic_i2c_probe()
1287 /* If clock-frequency not specified in DT, do not configure in SW */ in xiic_i2c_probe()
1288 if (ret || i2c->i2c_clk > I2C_MAX_FAST_MODE_PLUS_FREQ) in xiic_i2c_probe()
1289 i2c->i2c_clk = 0; in xiic_i2c_probe()
1291 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, in xiic_i2c_probe()
1293 pdev->name, i2c); in xiic_i2c_probe()
1296 dev_err(&pdev->dev, "Cannot claim IRQ\n"); in xiic_i2c_probe()
1300 i2c->singlemaster = in xiic_i2c_probe()
1301 of_property_read_bool(pdev->dev.of_node, "single-master"); in xiic_i2c_probe()
1308 i2c->endianness = LITTLE; in xiic_i2c_probe()
1313 i2c->endianness = BIG; in xiic_i2c_probe()
1317 dev_err(&pdev->dev, "Cannot xiic_reinit\n"); in xiic_i2c_probe()
1322 ret = i2c_add_adapter(&i2c->adap); in xiic_i2c_probe()
1330 for (i = 0; i < pdata->num_devices; i++) in xiic_i2c_probe()
1331 i2c_new_client_device(&i2c->adap, pdata->devices + i); in xiic_i2c_probe()
1334 dev_dbg(&pdev->dev, "mmio %08lx irq %d scl clock frequency %d\n", in xiic_i2c_probe()
1335 (unsigned long)res->start, irq, i2c->i2c_clk); in xiic_i2c_probe()
1340 pm_runtime_disable(&pdev->dev); in xiic_i2c_probe()
1341 pm_runtime_set_suspended(&pdev->dev); in xiic_i2c_probe()
1352 i2c_del_adapter(&i2c->adap); in xiic_i2c_remove()
1354 ret = pm_runtime_get_sync(i2c->dev); in xiic_i2c_remove()
1357 dev_warn(&pdev->dev, "Failed to activate device for removal (%pe)\n", in xiic_i2c_remove()
1362 pm_runtime_put_sync(i2c->dev); in xiic_i2c_remove()
1363 pm_runtime_disable(&pdev->dev); in xiic_i2c_remove()
1364 pm_runtime_set_suspended(&pdev->dev); in xiic_i2c_remove()
1365 pm_runtime_dont_use_autosuspend(&pdev->dev); in xiic_i2c_remove()
1372 clk_disable(i2c->clk); in xiic_i2c_runtime_suspend()
1382 ret = clk_enable(i2c->clk); in xiic_i2c_runtime_resume()
1409 MODULE_AUTHOR("info@mocean-labs.com");