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