Lines Matching +full:slave +full:- +full:mode

1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
136 uint8_t mode; member
143 {"rockchip,rk3288-i2c", 1},
144 {"rockchip,rk3328-i2c", 1},
145 {"rockchip,rk3399-i2c", 1},
152 { -1, 0 }
159 #define RK_I2C_LOCK(sc) mtx_lock(&(sc)->mtx)
160 #define RK_I2C_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
161 #define RK_I2C_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED)
162 #define RK_I2C_READ(sc, reg) bus_read_4((sc)->res[0], (reg))
163 #define RK_I2C_WRITE(sc, reg, val) bus_write_4((sc)->res[0], (reg), (val))
172 err = clk_get_freq(sc->sclk, &sclk_freq); in rk_i2c_get_clkdiv()
176 clkdiv = (sclk_freq / speed / RK_I2C_CLKDIV_MUL / 2) - 1; in rk_i2c_get_clkdiv()
193 busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed); in rk_i2c_reset()
217 len = sc->msg->len - sc->cnt; in rk_i2c_fill_tx()
218 if (sc->tx_slave_addr) { in rk_i2c_fill_tx()
219 KASSERT(sc->cnt == 0, ("tx_slave_addr in the middle of data")); in rk_i2c_fill_tx()
230 for (j = 0; j < MIN(len - i, 4); j++) { in rk_i2c_fill_tx()
232 if (sc->tx_slave_addr) { in rk_i2c_fill_tx()
233 buf = sc->msg->slave; in rk_i2c_fill_tx()
234 sc->tx_slave_addr = false; in rk_i2c_fill_tx()
236 KASSERT(sc->cnt < sc->msg->len, in rk_i2c_fill_tx()
238 buf = sc->msg->buf[sc->cnt]; in rk_i2c_fill_tx()
239 sc->cnt++; in rk_i2c_fill_tx()
261 if (sc->msg == NULL) { in rk_i2c_drain_rx()
262 device_printf(sc->dev, "No current iic msg\n"); in rk_i2c_drain_rx()
266 len = sc->msg->len - sc->cnt; in rk_i2c_drain_rx()
275 sc->msg->buf[sc->cnt++] = buf8; in rk_i2c_drain_rx()
284 if (!(sc->msg->flags & IIC_M_NOSTOP)) { in rk_i2c_send_stop()
287 sc->state = STATE_STOP; in rk_i2c_send_stop()
297 sc->state = STATE_IDLE; in rk_i2c_send_stop()
298 sc->transfer_done = 1; in rk_i2c_send_stop()
312 sc->ipd = RK_I2C_READ(sc, RK_I2C_IPD); in rk_i2c_intr_locked()
315 if ((sc->ipd & RK_I2C_IPD_ALL) == 0) in rk_i2c_intr_locked()
318 RK_I2C_WRITE(sc, RK_I2C_IPD, sc->ipd); in rk_i2c_intr_locked()
319 sc->ipd &= RK_I2C_IPD_ALL; in rk_i2c_intr_locked()
321 if (sc->ipd & RK_I2C_IPD_NAKRCVIPD) { in rk_i2c_intr_locked()
323 sc->ipd &= ~RK_I2C_IPD_NAKRCVIPD; in rk_i2c_intr_locked()
324 sc->nak_recv = true; in rk_i2c_intr_locked()
326 sc->transfer_done = true; in rk_i2c_intr_locked()
327 sc->state = STATE_IDLE; in rk_i2c_intr_locked()
331 switch (sc->state) { in rk_i2c_intr_locked()
338 if (sc->mode == RK_I2C_CON_MODE_RRX || in rk_i2c_intr_locked()
339 sc->mode == RK_I2C_CON_MODE_RX) { in rk_i2c_intr_locked()
340 sc->state = STATE_READ; in rk_i2c_intr_locked()
344 if ((sc->msg->len - sc->cnt) > 32) in rk_i2c_intr_locked()
347 transfer_len = sc->msg->len - sc->cnt; in rk_i2c_intr_locked()
355 sc->state = STATE_WRITE; in rk_i2c_intr_locked()
366 if (sc->cnt == sc->msg->len) { in rk_i2c_intr_locked()
369 sc->mode = RK_I2C_CON_MODE_RX; in rk_i2c_intr_locked()
372 reg |= sc->mode << RK_I2C_CON_MODE_SHIFT; in rk_i2c_intr_locked()
375 if ((sc->msg->len - sc->cnt) > 32) in rk_i2c_intr_locked()
378 transfer_len = sc->msg->len - sc->cnt; in rk_i2c_intr_locked()
387 if (sc->cnt < sc->msg->len) { in rk_i2c_intr_locked()
403 sc->transfer_done = 1; in rk_i2c_intr_locked()
404 sc->state = STATE_IDLE; in rk_i2c_intr_locked()
432 sc->transfer_done = false; in rk_i2c_start_xfer()
433 sc->nak_recv = false; in rk_i2c_start_xfer()
434 sc->tx_slave_addr = false; in rk_i2c_start_xfer()
435 sc->cnt = 0; in rk_i2c_start_xfer()
436 sc->state = STATE_IDLE; in rk_i2c_start_xfer()
437 sc->msg = msg; in rk_i2c_start_xfer()
440 if (!(sc->msg->flags & IIC_M_NOSTART)) { in rk_i2c_start_xfer()
442 if (sc->mode == RK_I2C_CON_MODE_TX) { in rk_i2c_start_xfer()
443 sc->tx_slave_addr = true; in rk_i2c_start_xfer()
445 sc->state = STATE_START; in rk_i2c_start_xfer()
451 if (sc->mode == RK_I2C_CON_MODE_RX) { in rk_i2c_start_xfer()
452 sc->state = STATE_READ; in rk_i2c_start_xfer()
456 RK_I2C_WRITE(sc, RK_I2C_MRXCNT, sc->msg->len); in rk_i2c_start_xfer()
460 sc->state = STATE_WRITE; in rk_i2c_start_xfer()
470 reg |= sc->mode << RK_I2C_CON_MODE_SHIFT; in rk_i2c_start_xfer()
487 while (sc->busy) in rk_i2c_transfer()
488 mtx_sleep(sc, &sc->mtx, 0, "i2cbuswait", 0); in rk_i2c_transfer()
489 sc->busy = 1; in rk_i2c_transfer()
510 if (i < nmsgs - 1) { in rk_i2c_transfer()
514 (msgs[i].slave != msgs[i + 1].slave))) { in rk_i2c_transfer()
523 * messages should have same slave address. in rk_i2c_transfer()
526 if (nmsgs - i >= 2 && msgs[i].len < 4 && in rk_i2c_transfer()
529 (msgs[i].slave & ~LSB) == (msgs[i + 1].slave & ~LSB)) { in rk_i2c_transfer()
530 sc->mode = RK_I2C_CON_MODE_RRX; in rk_i2c_transfer()
532 /* Write slave address */ in rk_i2c_transfer()
533 reg = msgs[i].slave & ~LSB; in rk_i2c_transfer()
537 /* Write slave register address */ in rk_i2c_transfer()
549 sc->mode = RK_I2C_CON_MODE_RX; in rk_i2c_transfer()
551 sc->mode = RK_I2C_CON_MODE_RRX; in rk_i2c_transfer()
552 reg = msgs[i].slave & ~LSB; in rk_i2c_transfer()
558 sc->mode = RK_I2C_CON_MODE_TX; in rk_i2c_transfer()
562 last_msg = (i >= nmsgs - 1) || in rk_i2c_transfer()
567 for(timeout = 10000; timeout > 0; timeout--) { in rk_i2c_transfer()
569 if (sc->transfer_done) in rk_i2c_transfer()
576 while (err == 0 && !sc->transfer_done) { in rk_i2c_transfer()
577 err = msleep(sc, &sc->mtx, PZERO, "rk_i2c", in rk_i2c_transfer()
587 sc->busy = 0; in rk_i2c_transfer()
589 if (sc->nak_recv) in rk_i2c_transfer()
602 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) in rk_i2c_probe()
616 sc->dev = dev; in rk_i2c_attach()
618 mtx_init(&sc->mtx, device_get_nameunit(dev), "rk_i2c", MTX_DEF); in rk_i2c_attach()
620 if (bus_alloc_resources(dev, rk_i2c_spec, sc->res) != 0) { in rk_i2c_attach()
626 if (bus_setup_intr(dev, sc->res[1], in rk_i2c_attach()
628 &sc->intrhand)) { in rk_i2c_attach()
629 bus_release_resources(dev, rk_i2c_spec, sc->res); in rk_i2c_attach()
637 error = clk_get_by_ofw_name(dev, 0, "i2c", &sc->sclk); in rk_i2c_attach()
642 error = clk_enable(sc->sclk); in rk_i2c_attach()
648 error = clk_get_by_ofw_name(dev, 0, "pclk", &sc->pclk); in rk_i2c_attach()
653 if (sc->pclk != NULL) { in rk_i2c_attach()
654 error = clk_enable(sc->pclk); in rk_i2c_attach()
661 sc->iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY); in rk_i2c_attach()
662 if (sc->iicbus == NULL) { in rk_i2c_attach()
689 if (sc->iicbus != NULL) in rk_i2c_detach()
690 if ((error = device_delete_child(dev, sc->iicbus)) != 0) in rk_i2c_detach()
693 if (sc->sclk != NULL) in rk_i2c_detach()
694 clk_release(sc->sclk); in rk_i2c_detach()
695 if (sc->pclk != NULL) in rk_i2c_detach()
696 clk_release(sc->pclk); in rk_i2c_detach()
698 if (sc->intrhand != NULL) in rk_i2c_detach()
699 bus_teardown_intr(sc->dev, sc->res[1], sc->intrhand); in rk_i2c_detach()
701 bus_release_resources(dev, rk_i2c_spec, sc->res); in rk_i2c_detach()
703 mtx_destroy(&sc->mtx); in rk_i2c_detach()