Lines Matching +full:i2c +full:- +full:bus +full:- +full:name

1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
14 * 3. The name of the author may not be used to endorse or promote products
32 * Support routines for the Keywest I2C controller.
39 #include <sys/bus.h>
43 #include <machine/bus.h>
51 /* Keywest I2C Register offsets */
93 /* I2C flags */
125 static phandle_t kiic_get_node(device_t bus, device_t dev);
153 const char *name;
155 name = ofw_bus_get_name(self);
156 if (name && strcmp(name, "i2c") == 0) {
157 device_set_desc(self, "Keywest I2C controller");
170 char name[64];
173 sc->sc_dev = self;
176 if (node == 0 || node == -1) {
181 sc->sc_reg = bus_alloc_resource_any(self, SYS_RES_MEMORY,
183 if (sc->sc_reg == NULL) {
187 if (OF_getencprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
188 device_printf(self, "cannot get i2c-rate\n");
191 if (OF_getencprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
192 device_printf(self, "unable to find i2c address step\n");
197 * Some Keywest I2C devices have their children attached directly
200 * OFW I2C layer has the correct node.
202 * Note: the I2C children of the Uninorth bridges have two ports.
203 * In general, the port is designated in the 9th bit of the I2C
205 * an i2c-bus node, the port is indicated in the 'reg' property
206 * of the i2c-bus node.
209 sc->sc_node = node;
212 if (OF_getprop(node, "name", name, sizeof(name)) > 0) {
213 if (strcmp(name,"i2c-bus") == 0) {
216 sc->sc_i2c_base = reg << 8;
218 sc->sc_node = node;
222 mtx_init(&sc->sc_mutex, "kiic", NULL, MTX_DEF);
224 sc->sc_irq = bus_alloc_resource_any(self, SYS_RES_IRQ, &sc->sc_irqrid,
226 bus_setup_intr(self, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE, NULL,
227 kiic_intr, sc, &sc->sc_ih);
241 /* Add the IIC bus layer */
242 sc->sc_iicbus = device_add_child(self, "iicbus", DEVICE_UNIT_ANY);
251 bus_write_4(sc->sc_reg, sc->sc_regstep * reg, val);
258 return bus_read_4(sc->sc_reg, sc->sc_regstep * reg) & 0xff;
304 mtx_lock(&sc->sc_mutex);
308 sc->sc_flags |= I2C_SELECTED;
310 if (sc->sc_flags & I2C_READING) {
311 if (sc->sc_resid > 1) {
317 kiic_writereg(sc, DATA, *sc->sc_data++);
318 sc->sc_resid--;
323 if (sc->sc_flags & I2C_READING) {
324 if (sc->sc_resid > 0) {
325 *sc->sc_data++ = kiic_readreg(sc, DATA);
326 sc->sc_resid--;
328 if (sc->sc_resid == 0) /* done */
331 if (sc->sc_resid == 0) {
336 kiic_writereg(sc, DATA, *sc->sc_data++);
337 sc->sc_resid--;
344 sc->sc_flags &= ~I2C_SELECTED;
345 wakeup(sc->sc_dev);
349 mtx_unlock(&sc->sc_mutex);
364 mtx_lock(&sc->sc_mutex);
366 if (sc->sc_flags & I2C_BUSY)
367 mtx_sleep(dev, &sc->sc_mutex, 0, "kiic", timo);
369 if (sc->sc_flags & I2C_BUSY) {
370 mtx_unlock(&sc->sc_mutex);
374 sc->sc_flags = I2C_BUSY;
386 KASSERT(msgs[i].len == 1, ("oversize I2C message"));
393 sc->sc_data = msgs[i].buf;
394 sc->sc_resid = msgs[i].len;
395 sc->sc_flags = I2C_BUSY;
397 timo = 1000 + sc->sc_resid * 200;
401 sc->sc_flags |= I2C_READING;
405 addr |= sc->sc_i2c_base;
414 err = mtx_sleep(dev, &sc->sc_mutex, 0, "kiic", timo);
416 msgs[i].len -= sc->sc_resid;
418 if ((sc->sc_flags & I2C_ERROR) || err == EWOULDBLOCK) {
419 device_printf(sc->sc_dev, "I2C error\n");
420 sc->sc_flags = 0;
421 mtx_unlock(&sc->sc_mutex);
426 sc->sc_flags = 0;
428 mtx_unlock(&sc->sc_mutex);
434 kiic_get_node(device_t bus, device_t dev)
438 sc = device_get_softc(bus);
439 /* We only have one child, the I2C bus, which needs our own node. */
441 return sc->sc_node;