Path: blob/main/sys/compat/linuxkpi/common/src/linux_i2c.c
39586 views
/*-1* Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*24*/2526#include <sys/param.h>27#include <sys/systm.h>28#include <sys/bus.h>29#include <sys/malloc.h>3031#include <dev/iicbus/iicbus.h>32#include <dev/iicbus/iiconf.h>3334#include <linux/device.h>35#include <linux/i2c.h>36#include <linux/i2c-algo-bit.h>37#include <linux/list.h>38#include <linux/pci.h>3940#include "iicbus_if.h"41#include "iicbb_if.h"42#include "lkpi_iic_if.h"4344static int lkpi_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);45static int lkpi_i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr);4647struct lkpi_iic_softc {48device_t iicbus;49struct i2c_adapter *adapter;50};5152static struct sx lkpi_sx_i2c;5354static void55lkpi_sysinit_i2c(void *arg __unused)56{5758sx_init(&lkpi_sx_i2c, "lkpi-i2c");59}6061static void62lkpi_sysuninit_i2c(void *arg __unused)63{6465sx_destroy(&lkpi_sx_i2c);66}6768SYSINIT(lkpi_i2c, SI_SUB_DRIVERS, SI_ORDER_ANY,69lkpi_sysinit_i2c, NULL);70SYSUNINIT(lkpi_i2c, SI_SUB_DRIVERS, SI_ORDER_ANY,71lkpi_sysuninit_i2c, NULL);7273static int74lkpi_iic_probe(device_t dev)75{7677device_set_desc(dev, "LinuxKPI I2C");78return (BUS_PROBE_NOWILDCARD);79}8081static int82lkpi_iic_attach(device_t dev)83{84struct lkpi_iic_softc *sc;8586sc = device_get_softc(dev);87sc->iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY);88if (sc->iicbus == NULL) {89device_printf(dev, "Couldn't add iicbus child, aborting\n");90return (ENXIO);91}92bus_attach_children(dev);93return (0);94}9596static int97lkpi_iic_detach(device_t dev)98{99struct lkpi_iic_softc *sc;100101sc = device_get_softc(dev);102if (sc->iicbus)103device_delete_child(dev, sc->iicbus);104return (0);105}106107static int108lkpi_iic_add_adapter(device_t dev, struct i2c_adapter *adapter)109{110struct lkpi_iic_softc *sc;111112sc = device_get_softc(dev);113sc->adapter = adapter;114115return (0);116}117118static struct i2c_adapter *119lkpi_iic_get_adapter(device_t dev)120{121struct lkpi_iic_softc *sc;122123sc = device_get_softc(dev);124return (sc->adapter);125}126127static device_method_t lkpi_iic_methods[] = {128/* device interface */129DEVMETHOD(device_probe, lkpi_iic_probe),130DEVMETHOD(device_attach, lkpi_iic_attach),131DEVMETHOD(device_detach, lkpi_iic_detach),132DEVMETHOD(device_suspend, bus_generic_suspend),133DEVMETHOD(device_resume, bus_generic_resume),134135/* iicbus interface */136DEVMETHOD(iicbus_transfer, lkpi_i2c_transfer),137DEVMETHOD(iicbus_reset, lkpi_i2c_reset),138DEVMETHOD(iicbus_callback, iicbus_null_callback),139140/* lkpi_iic interface */141DEVMETHOD(lkpi_iic_add_adapter, lkpi_iic_add_adapter),142DEVMETHOD(lkpi_iic_get_adapter, lkpi_iic_get_adapter),143144DEVMETHOD_END145};146147driver_t lkpi_iic_driver = {148"lkpi_iic",149lkpi_iic_methods,150sizeof(struct lkpi_iic_softc),151};152153DRIVER_MODULE(lkpi_iic, drmn, lkpi_iic_driver, 0, 0);154DRIVER_MODULE(lkpi_iic, drm, lkpi_iic_driver, 0, 0);155DRIVER_MODULE(iicbus, lkpi_iic, iicbus_driver, 0, 0);156MODULE_DEPEND(linuxkpi, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);157158static int159lkpi_i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)160{161162/* That doesn't seems to be supported in linux */163return (0);164}165166static int i2c_check_for_quirks(struct i2c_adapter *adapter,167struct iic_msg *msgs, uint32_t nmsgs)168{169const struct i2c_adapter_quirks *quirks;170device_t dev;171int i, max_nmsgs;172bool check_len;173174dev = adapter->dev.parent->bsddev;175quirks = adapter->quirks;176if (quirks == NULL)177return (0);178179check_len = true;180max_nmsgs = quirks->max_num_msgs;181182if (quirks->flags & I2C_AQ_COMB) {183max_nmsgs = 2;184185if (nmsgs == 2) {186if (quirks->flags & I2C_AQ_COMB_WRITE_FIRST &&187msgs[0].flags & IIC_M_RD) {188device_printf(dev,189"Error: "190"first combined message must be write\n");191return (EOPNOTSUPP);192}193if (quirks->flags & I2C_AQ_COMB_READ_SECOND &&194!(msgs[1].flags & IIC_M_RD)) {195device_printf(dev,196"Error: "197"second combined message must be read\n");198return (EOPNOTSUPP);199}200201if (quirks->flags & I2C_AQ_COMB_SAME_ADDR &&202msgs[0].slave != msgs[1].slave) {203device_printf(dev,204"Error: "205"combined message must be use the same "206"address\n");207return (EOPNOTSUPP);208}209210if (quirks->max_comb_1st_msg_len &&211msgs[0].len > quirks->max_comb_1st_msg_len) {212device_printf(dev,213"Error: "214"message too long: %hu > %hu max\n",215msgs[0].len,216quirks->max_comb_1st_msg_len);217return (EOPNOTSUPP);218}219if (quirks->max_comb_2nd_msg_len &&220msgs[1].len > quirks->max_comb_2nd_msg_len) {221device_printf(dev,222"Error: "223"message too long: %hu > %hu max\n",224msgs[1].len,225quirks->max_comb_2nd_msg_len);226return (EOPNOTSUPP);227}228229check_len = false;230}231}232233if (max_nmsgs && nmsgs > max_nmsgs) {234device_printf(dev,235"Error: too many messages: %d > %d max\n",236nmsgs, max_nmsgs);237return (EOPNOTSUPP);238}239240for (i = 0; i < nmsgs; i++) {241if (msgs[i].flags & IIC_M_RD) {242if (check_len && quirks->max_read_len &&243msgs[i].len > quirks->max_read_len) {244device_printf(dev,245"Error: "246"message %d too long: %hu > %hu max\n",247i, msgs[i].len, quirks->max_read_len);248return (EOPNOTSUPP);249}250if (quirks->flags & I2C_AQ_NO_ZERO_LEN_READ &&251msgs[i].len == 0) {252device_printf(dev,253"Error: message %d of length 0\n", i);254return (EOPNOTSUPP);255}256} else {257if (check_len && quirks->max_write_len &&258msgs[i].len > quirks->max_write_len) {259device_printf(dev,260"Message %d too long: %hu > %hu max\n",261i, msgs[i].len, quirks->max_write_len);262return (EOPNOTSUPP);263}264if (quirks->flags & I2C_AQ_NO_ZERO_LEN_WRITE &&265msgs[i].len == 0) {266device_printf(dev,267"Error: message %d of length 0\n", i);268return (EOPNOTSUPP);269}270}271}272273return (0);274}275276static int277lkpi_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)278{279struct lkpi_iic_softc *sc;280struct i2c_msg *linux_msgs;281int i, ret = 0;282283sc = device_get_softc(dev);284if (sc->adapter == NULL)285return (ENXIO);286ret = i2c_check_for_quirks(sc->adapter, msgs, nmsgs);287if (ret != 0)288return (ret);289linux_set_current(curthread);290291linux_msgs = malloc(sizeof(struct i2c_msg) * nmsgs,292M_DEVBUF, M_WAITOK | M_ZERO);293294for (i = 0; i < nmsgs; i++) {295linux_msgs[i].addr = msgs[i].slave >> 1;296linux_msgs[i].len = msgs[i].len;297linux_msgs[i].buf = msgs[i].buf;298if (msgs[i].flags & IIC_M_RD) {299linux_msgs[i].flags |= I2C_M_RD;300for (int j = 0; j < msgs[i].len; j++)301msgs[i].buf[j] = 0;302}303if (msgs[i].flags & IIC_M_NOSTART)304linux_msgs[i].flags |= I2C_M_NOSTART;305}306ret = i2c_transfer(sc->adapter, linux_msgs, nmsgs);307free(linux_msgs, M_DEVBUF);308309if (ret < 0)310return (-ret);311return (0);312}313314int315lkpi_i2c_add_adapter(struct i2c_adapter *adapter)316{317device_t lkpi_iic;318319if (adapter->name[0] == '\0')320return (-EINVAL);321if (bootverbose)322device_printf(adapter->dev.parent->bsddev,323"Adding i2c adapter %s\n", adapter->name);324sx_xlock(&lkpi_sx_i2c);325lkpi_iic = device_add_child(adapter->dev.parent->bsddev, "lkpi_iic",326DEVICE_UNIT_ANY);327if (lkpi_iic == NULL) {328device_printf(adapter->dev.parent->bsddev, "Couldn't add lkpi_iic\n");329sx_xunlock(&lkpi_sx_i2c);330return (ENXIO);331}332333bus_topo_lock();334bus_attach_children(adapter->dev.parent->bsddev);335bus_topo_unlock();336LKPI_IIC_ADD_ADAPTER(lkpi_iic, adapter);337sx_xunlock(&lkpi_sx_i2c);338return (0);339}340341int342lkpi_i2c_del_adapter(struct i2c_adapter *adapter)343{344device_t child;345int unit, rv;346347if (adapter == NULL)348return (-EINVAL);349if (bootverbose)350device_printf(adapter->dev.parent->bsddev,351"Removing i2c adapter %s\n", adapter->name);352sx_xlock(&lkpi_sx_i2c);353unit = 0;354while ((child = device_find_child(adapter->dev.parent->bsddev, "lkpi_iic", unit++)) != NULL) {355356if (adapter == LKPI_IIC_GET_ADAPTER(child)) {357bus_topo_lock();358device_delete_child(adapter->dev.parent->bsddev, child);359bus_topo_unlock();360rv = 0;361goto out;362}363}364365unit = 0;366while ((child = device_find_child(adapter->dev.parent->bsddev, "lkpi_iicbb", unit++)) != NULL) {367368if (adapter == LKPI_IIC_GET_ADAPTER(child)) {369bus_topo_lock();370device_delete_child(adapter->dev.parent->bsddev, child);371bus_topo_unlock();372rv = 0;373goto out;374}375}376rv = -EINVAL;377out:378sx_xunlock(&lkpi_sx_i2c);379return (rv);380}381382383