Lines Matching +full:i2c +full:- +full:transfer +full:- +full:timeout +full:- +full:us
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
31 * Driver for the I2C module on the TI SoC.
38 * This driver currently doesn't use DMA for the transfer, although I hope to
72 * I2C device driver context, a pointer to this is stored in the device
109 * OMAP4 i2c bus clock is 96MHz / ((psc + 1) * (scll + 7 + sclh + 5)).
117 /* { 3200000, 1, 113, 115, 7, 10}, - HS mode */
124 * AM335x i2c bus clock is 48MHZ / ((psc + 1) * (scll + 7 + sclh + 5))
138 #define TI_I2C_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
139 #define TI_I2C_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
141 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
143 #define TI_I2C_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx)
144 #define TI_I2C_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED)
145 #define TI_I2C_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED)
149 device_printf((_sc)->sc_dev, fmt, ##args)
155 * ti_i2c_read_2 - reads a 16-bit value from one of the I2C registers
156 * @sc: I2C device context
164 * 16-bit value read from the register.
170 return (bus_read_2(sc->sc_mem_res, off)); in ti_i2c_read_2()
174 * ti_i2c_write_2 - writes a 16-bit value to one of the I2C registers
175 * @sc: I2C device context
183 * 16-bit value read from the register.
189 bus_write_2(sc->sc_mem_res, off, val); in ti_i2c_write_2()
204 sc->sc_error = ENXIO; in ti_i2c_transfer_intr()
209 sc->sc_error = ENXIO; in ti_i2c_transfer_intr()
214 /* Register access ready - transaction complete basically. */ in ti_i2c_transfer_intr()
216 if (sc->sc_error != 0 && sc->sc_buffer->flags & IIC_M_NOSTOP) { in ti_i2c_transfer_intr()
218 sc->sc_con_reg | I2C_CON_STP); in ti_i2c_transfer_intr()
226 if (sc->sc_buffer->flags & IIC_M_RD) { in ti_i2c_transfer_intr()
230 * Receive draining interrupt - last data received. in ti_i2c_transfer_intr()
239 amount = sc->sc_buffer->len - sc->sc_buffer_pos; in ti_i2c_transfer_intr()
242 * Receive data ready interrupt - FIFO has reached the in ti_i2c_transfer_intr()
247 amount = min(sc->sc_fifo_trsh, in ti_i2c_transfer_intr()
248 sc->sc_buffer->len - sc->sc_buffer_pos); in ti_i2c_transfer_intr()
253 sc->sc_buffer->buf[sc->sc_buffer_pos++] = in ti_i2c_transfer_intr()
265 * Transmit draining interrupt - FIFO level is below in ti_i2c_transfer_intr()
275 amount = sc->sc_buffer->len - sc->sc_buffer_pos; in ti_i2c_transfer_intr()
278 * Transmit data ready interrupt - the FIFO level in ti_i2c_transfer_intr()
283 amount = min(sc->sc_fifo_trsh, in ti_i2c_transfer_intr()
284 sc->sc_buffer->len - sc->sc_buffer_pos); in ti_i2c_transfer_intr()
290 sc->sc_buffer->buf[sc->sc_buffer_pos++]); in ti_i2c_transfer_intr()
302 * ti_i2c_intr - interrupt handler for the I2C module
303 * @dev: i2c device handle
338 if (sc->sc_buffer != NULL) in ti_i2c_intr()
341 ti_i2c_dbg(sc, "Transfer interrupt without buffer\n"); in ti_i2c_intr()
342 sc->sc_error = EINVAL; in ti_i2c_intr()
354 * ti_i2c_transfer - called to perform the transfer
355 * @dev: i2c device handle
370 int err, i, repstart, timeout; in ti_i2c_transfer() local
378 while (sc->sc_bus_inuse == 1) in ti_i2c_transfer()
379 mtx_sleep(sc, &sc->sc_mtx, 0, "i2cbuswait", 0); in ti_i2c_transfer()
381 /* Now we have control over the I2C controller. */ in ti_i2c_transfer()
382 sc->sc_bus_inuse = 1; in ti_i2c_transfer()
387 sc->sc_buffer = &msgs[i]; in ti_i2c_transfer()
388 sc->sc_buffer_pos = 0; in ti_i2c_transfer()
389 sc->sc_error = 0; in ti_i2c_transfer()
392 if (sc->sc_buffer == NULL || sc->sc_buffer->buf == NULL || in ti_i2c_transfer()
393 sc->sc_buffer->len == 0) { in ti_i2c_transfer()
398 /* Check if the i2c bus is free. */ in ti_i2c_transfer()
404 timeout = 0; in ti_i2c_transfer()
406 if (timeout++ > 100) { in ti_i2c_transfer()
412 timeout = 0; in ti_i2c_transfer()
416 if (sc->sc_buffer->flags & IIC_M_NOSTOP) in ti_i2c_transfer()
423 ti_i2c_write_2(sc, I2C_REG_CNT, sc->sc_buffer->len); in ti_i2c_transfer()
430 reg = sc->sc_con_reg | I2C_CON_STT; in ti_i2c_transfer()
433 if ((sc->sc_buffer->flags & IIC_M_RD) == 0) in ti_i2c_transfer()
438 err = mtx_sleep(sc, &sc->sc_mtx, 0, "i2ciowait", sc->sc_timeout); in ti_i2c_transfer()
440 err = sc->sc_error; in ti_i2c_transfer()
447 if (timeout == 0) { in ti_i2c_transfer()
449 if (timeout++ > 100) in ti_i2c_transfer()
456 ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg); in ti_i2c_transfer()
458 sc->sc_buffer = NULL; in ti_i2c_transfer()
459 sc->sc_bus_inuse = 0; in ti_i2c_transfer()
472 int timeout; in ti_i2c_reset() local
489 panic("Unknown TI SoC, unable to reset the i2c"); in ti_i2c_reset()
494 * speed. This lets us get the hardware initialized enough to attach in ti_i2c_reset()
500 if (sc->sc_iicbus == NULL) in ti_i2c_reset()
503 busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed); in ti_i2c_reset()
511 * 23.1.4.3 - HS I2C Software Reset in ti_i2c_reset()
516 sc->sc_con_reg = 0; in ti_i2c_reset()
517 ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg); in ti_i2c_reset()
520 bus_write_2(sc->sc_mem_res, I2C_REG_SYSC, I2C_REG_SYSC_SRST); in ti_i2c_reset()
530 timeout = 0; in ti_i2c_reset()
532 if (timeout++ > 100) in ti_i2c_reset()
538 * Disable the I2C controller once again, now that the reset has in ti_i2c_reset()
541 ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg); in ti_i2c_reset()
556 ti_i2c_write_2(sc, I2C_REG_PSC, clkcfg->psc); in ti_i2c_reset()
562 * (see Table 23-8). in ti_i2c_reset()
564 scll = clkcfg->scll & I2C_SCLL_MASK; in ti_i2c_reset()
565 sclh = clkcfg->sclh & I2C_SCLH_MASK; in ti_i2c_reset()
572 * Table 23-8). in ti_i2c_reset()
581 if ((clkcfg->hsscll + clkcfg->hssclh) > 0) { in ti_i2c_reset()
582 scll |= clkcfg->hsscll << I2C_HSSCLL_SHIFT; in ti_i2c_reset()
583 sclh |= clkcfg->hssclh << I2C_HSSCLH_SHIFT; in ti_i2c_reset()
584 sc->sc_con_reg |= I2C_CON_OPMODE_HS; in ti_i2c_reset()
595 * 6. Configure the Own Address of the I2C controller by storing it in in ti_i2c_reset()
598 * for each I2C controller. in ti_i2c_reset()
600 * Note: For a 10-bit address, set the corresponding expand Own Address in ti_i2c_reset()
609 * (TX threshold - 1) and the I2Ci.I2C_BUF[13:8]RTRSH field to (RX in ti_i2c_reset()
610 * threshold - 1), where the TX and RX thresholds are greater than in ti_i2c_reset()
615 fifo_trsh = (sc->sc_fifo_trsh - 1) & I2C_BUF_TRSH_MASK; in ti_i2c_reset()
620 * 8. Take the I2C controller out of reset by setting the in ti_i2c_reset()
623 * 23.1.5.1.1.1.2 - Initialize the I2C Controller in ti_i2c_reset()
625 * To initialize the I2C controller, perform the following steps: in ti_i2c_reset()
634 /* Enable the I2C controller in master mode. */ in ti_i2c_reset()
635 sc->sc_con_reg |= I2C_CON_I2C_EN | I2C_CON_MST; in ti_i2c_reset()
636 ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg); in ti_i2c_reset()
705 * ti_i2c_deactivate - deactivates the controller and releases resources
706 * @dev: i2c device handle
721 /* Disable the controller - cancel all transactions. */ in ti_i2c_deactivate()
727 if (sc->sc_irq_h != NULL) { in ti_i2c_deactivate()
728 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_h); in ti_i2c_deactivate()
729 sc->sc_irq_h = NULL; in ti_i2c_deactivate()
732 /* Unmap the I2C controller registers. */ in ti_i2c_deactivate()
733 if (sc->sc_mem_res != NULL) { in ti_i2c_deactivate()
734 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); in ti_i2c_deactivate()
735 sc->sc_mem_res = NULL; in ti_i2c_deactivate()
739 if (sc->sc_irq_res != NULL) { in ti_i2c_deactivate()
740 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res); in ti_i2c_deactivate()
741 sc->sc_irq_res = NULL; in ti_i2c_deactivate()
784 val = sc->sc_timeout; in ti_i2c_sysctl_timeout()
789 if ((err == 0) && (req->newptr != NULL)) { in ti_i2c_sysctl_timeout()
791 sc->sc_timeout = val; in ti_i2c_sysctl_timeout()
804 if (!ofw_bus_is_compatible(dev, "ti,omap4-i2c")) in ti_i2c_probe()
806 device_set_desc(dev, "TI I2C Controller"); in ti_i2c_probe()
821 sc->sc_dev = dev; in ti_i2c_attach()
825 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, in ti_i2c_attach()
827 if (sc->sc_mem_res == NULL) { in ti_i2c_attach()
834 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, in ti_i2c_attach()
836 if (sc->sc_irq_res == NULL) { in ti_i2c_attach()
837 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); in ti_i2c_attach()
851 /* Read the version number of the I2C module */ in ti_i2c_attach()
852 sc->sc_rev = ti_i2c_read_2(sc, I2C_REG_REVNB_HI) & 0xff; in ti_i2c_attach()
859 device_printf(dev, "I2C revision %d.%d FIFO size: %d bytes\n", in ti_i2c_attach()
860 sc->sc_rev >> 4, sc->sc_rev & 0xf, 8 << fifosz); in ti_i2c_attach()
863 sc->sc_fifo_trsh = 5; in ti_i2c_attach()
865 /* Set I2C bus timeout */ in ti_i2c_attach()
866 sc->sc_timeout = 5*hz; in ti_i2c_attach()
872 ti_i2c_sysctl_clk, "IU", "I2C bus clock"); in ti_i2c_attach()
876 ti_i2c_sysctl_timeout, "IU", "I2C bus timeout (in ticks)"); in ti_i2c_attach()
879 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE, in ti_i2c_attach()
880 NULL, ti_i2c_intr, sc, &sc->sc_irq_h); in ti_i2c_attach()
885 if ((sc->sc_iicbus = device_add_child(dev, "iicbus", -1)) == NULL) { in ti_i2c_attach()