/*-
* Copyright (c) 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Driver for NXP PCA954x / PCA984x I2C switches and multiplexers.
*
* There are two flavors of this device:
*
* - Multiplexers, which connect the upstream bus to one downstream bus
* at a time.
*
* - Switches, which can connect the upstream bus to one or more downstream
* busses at a time (which is useful when using an all-call address for
* a large array of PCA9685 LED controllers, for example).
*
* Alas, the device tree bindings don't have anything specifically for
* switches, so we treat the switch variants as basic multiplexers,
* only enabling one downstream bus at a time.
*
* Note that some versions of these chips also have interrupt mux
* capability. XXX We do not support this yet.
*/
if (bus->busidx >= sc->sc_type->nchannels) {
aprint_error_dev(iicmux->sc_dev,
"device tree error: bus index %d out of range\n",
bus->busidx);
return NULL;
}
switch (bus->handletype) {
case I2C_COOKIE_OF:
error = fdtbus_get_reg(bus->handle, 0, &addr, NULL);
if (error) {
aprint_error_dev(iicmux->sc_dev,
"unable to get reg property for bus %d\n",
bus->busidx);
return NULL;
}
break;
#if NACPICA > 0
case I2C_COOKIE_ACPI: {
ACPI_INTEGER val;
ACPI_STATUS rv;
rv = acpi_eval_integer((ACPI_HANDLE)bus->handle, "_ADR", &val);
if (ACPI_FAILURE(rv)) {
aprint_error_dev(iicmux->sc_dev,
"unable to evaluate _ADR for bus %d: %s\n",
bus->busidx, AcpiFormatException(rv));
return NULL;
}
addr = (bus_addr_t)val;
} break;
#endif
default:
aprint_error_dev(iicmux->sc_dev, "unsupported handle type\n");
return NULL;
}
if (addr >= sc->sc_type->nchannels) {
aprint_error_dev(iicmux->sc_dev,
"device tree error: reg property %llu out of range\n",
(unsigned long long)addr);
return NULL;
}
/*
* If it's a mux type, the enable value is the channel number
* (from the reg property) OR'd with the enable bit.
*
* If it's a switch type, the enable value is 1 << channel number
* (from the reg property).
*/
if (sc->sc_type->enable_bit) {
bus_info->enable_value =
(uint8_t)addr | sc->sc_type->enable_bit;
} else {
bus_info->enable_value = 1 << addr;
}
return bus_info;
}
static int
pcaiicmux_acquire_bus(struct iicmux_bus * const bus, int const flags)
{
struct pcaiicmux_softc * const sc = bus->mux->sc_mux_data;
struct pcaiicmux_bus_info * const bus_info = bus->bus_data;
int error;
if (ia->ia_cookietype == I2C_COOKIE_OF) {
const int phandle = (int)ia->ia_cookie;
if (of_hasprop(phandle, "i2c-mux-idle-disconnect")) {
sc->sc_idle_disconnect = true;
}
/* Reset the mux if a reset GPIO is specified. */
sc->sc_reset_gpio = fdtbus_gpio_acquire(phandle, "reset-gpios",
GPIO_PIN_OUTPUT);
if (sc->sc_reset_gpio) {
fdtbus_gpio_write(sc->sc_reset_gpio, 1);
delay(10);
fdtbus_gpio_write(sc->sc_reset_gpio, 0);
delay(10);
}
}
/* Force the mux into a disconnected state. */
sc->sc_cur_value = -1;
error = iic_acquire_bus(ia->ia_tag, 0);
if (error) {
aprint_error_dev(self,
"failed to acquire I2C bus (error = %d)\n", error);
return;
}
error = pcaiicmux_write(sc, 0, 0);
iic_release_bus(ia->ia_tag, 0);
if (error) {
aprint_error_dev(self,
"failed to set mux to disconnected state (error = %d)\n",
error);
return;
}