Home | History | Annotate | Line # | Download | only in i2c
scmdi2c.c revision 1.1
      1  1.1  brad 
      2  1.1  brad /*	$NetBSD: scmdi2c.c,v 1.1 2021/12/07 17:39:54 brad Exp $	*/
      3  1.1  brad 
      4  1.1  brad /*
      5  1.1  brad  * Copyright (c) 2021 Brad Spencer <brad (at) anduin.eldar.org>
      6  1.1  brad  *
      7  1.1  brad  * Permission to use, copy, modify, and distribute this software for any
      8  1.1  brad  * purpose with or without fee is hereby granted, provided that the above
      9  1.1  brad  * copyright notice and this permission notice appear in all copies.
     10  1.1  brad  *
     11  1.1  brad  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  1.1  brad  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1  brad  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  1.1  brad  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1  brad  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1  brad  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  1.1  brad  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1  brad  */
     19  1.1  brad 
     20  1.1  brad #include <sys/cdefs.h>
     21  1.1  brad __KERNEL_RCSID(0, "$NetBSD: scmdi2c.c,v 1.1 2021/12/07 17:39:54 brad Exp $");
     22  1.1  brad 
     23  1.1  brad /*
     24  1.1  brad  * I2C driver for the Sparkfun Serial motor controller.
     25  1.1  brad  * Uses the common code to do the actual work.
     26  1.1  brad */
     27  1.1  brad 
     28  1.1  brad #include <sys/param.h>
     29  1.1  brad #include <sys/systm.h>
     30  1.1  brad #include <sys/kernel.h>
     31  1.1  brad #include <sys/device.h>
     32  1.1  brad #include <sys/module.h>
     33  1.1  brad #include <sys/conf.h>
     34  1.1  brad #include <sys/sysctl.h>
     35  1.1  brad #include <sys/mutex.h>
     36  1.1  brad #include <sys/condvar.h>
     37  1.1  brad #include <sys/pool.h>
     38  1.1  brad #include <sys/kmem.h>
     39  1.1  brad 
     40  1.1  brad #include <dev/i2c/i2cvar.h>
     41  1.1  brad #include <dev/spi/spivar.h>
     42  1.1  brad #include <dev/ic/scmdreg.h>
     43  1.1  brad #include <dev/ic/scmdvar.h>
     44  1.1  brad 
     45  1.1  brad extern struct cdevsw scmd_cdevsw;
     46  1.1  brad 
     47  1.1  brad extern void	scmd_attach(struct scmd_sc *);
     48  1.1  brad 
     49  1.1  brad static int 	scmdi2c_poke(i2c_tag_t, i2c_addr_t, bool);
     50  1.1  brad static int 	scmdi2c_match(device_t, cfdata_t, void *);
     51  1.1  brad static void 	scmdi2c_attach(device_t, device_t, void *);
     52  1.1  brad static int 	scmdi2c_detach(device_t, int);
     53  1.1  brad static int	scmdi2c_activate(device_t, enum devact);
     54  1.1  brad 
     55  1.1  brad #define SCMD_DEBUG
     56  1.1  brad #ifdef SCMD_DEBUG
     57  1.1  brad #define DPRINTF(s, l, x) \
     58  1.1  brad     do { \
     59  1.1  brad 	if (l <= s->sc_scmddebug) \
     60  1.1  brad 	    printf x; \
     61  1.1  brad     } while (/*CONSTCOND*/0)
     62  1.1  brad #else
     63  1.1  brad #define DPRINTF(s, l, x)
     64  1.1  brad #endif
     65  1.1  brad 
     66  1.1  brad CFATTACH_DECL_NEW(scmdi2c, sizeof(struct scmd_sc),
     67  1.1  brad     scmdi2c_match, scmdi2c_attach, scmdi2c_detach, scmdi2c_activate);
     68  1.1  brad 
     69  1.1  brad static int
     70  1.1  brad scmdi2c_read_reg_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
     71  1.1  brad     uint8_t *buf)
     72  1.1  brad {
     73  1.1  brad 	return iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &reg, 1, buf,
     74  1.1  brad 	    1, 0);
     75  1.1  brad }
     76  1.1  brad 
     77  1.1  brad static int
     78  1.1  brad scmdi2c_read_reg(struct scmd_sc *sc, uint8_t reg, uint8_t *buf)
     79  1.1  brad {
     80  1.1  brad 	return scmdi2c_read_reg_direct(sc->sc_tag, sc->sc_addr, reg, buf);
     81  1.1  brad }
     82  1.1  brad 
     83  1.1  brad static int
     84  1.1  brad scmdi2c_write_reg_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
     85  1.1  brad     uint8_t buf)
     86  1.1  brad {
     87  1.1  brad 	return iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &reg, 1, &buf,
     88  1.1  brad 	    1, 0);
     89  1.1  brad }
     90  1.1  brad 
     91  1.1  brad static int
     92  1.1  brad scmdi2c_write_reg(struct scmd_sc *sc, uint8_t reg, uint8_t buf)
     93  1.1  brad {
     94  1.1  brad 	return scmdi2c_write_reg_direct(sc->sc_tag, sc->sc_addr, reg, buf);
     95  1.1  brad }
     96  1.1  brad 
     97  1.1  brad static int
     98  1.1  brad scmdi2c_acquire_bus(struct scmd_sc *sc)
     99  1.1  brad {
    100  1.1  brad 	return(iic_acquire_bus(sc->sc_tag, 0));
    101  1.1  brad }
    102  1.1  brad 
    103  1.1  brad static void
    104  1.1  brad scmdi2c_release_bus(struct scmd_sc *sc)
    105  1.1  brad {
    106  1.1  brad 	iic_release_bus(sc->sc_tag, 0);
    107  1.1  brad }
    108  1.1  brad 
    109  1.1  brad static int
    110  1.1  brad scmdi2c_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
    111  1.1  brad {
    112  1.1  brad 	uint8_t reg = SCMD_REG_ID;
    113  1.1  brad 	uint8_t buf;
    114  1.1  brad 	int error;
    115  1.1  brad 
    116  1.1  brad 	error = scmdi2c_read_reg_direct(tag, addr, reg, &buf);
    117  1.1  brad 	if (matchdebug) {
    118  1.1  brad 		printf("poke X 1: %d %02x %d\n", addr, buf, error);
    119  1.1  brad 	}
    120  1.1  brad 	return error;
    121  1.1  brad }
    122  1.1  brad 
    123  1.1  brad static int
    124  1.1  brad scmdi2c_match(device_t parent, cfdata_t match, void *aux)
    125  1.1  brad {
    126  1.1  brad 	struct i2c_attach_args *ia = aux;
    127  1.1  brad 	int error, match_result;
    128  1.1  brad 	const bool matchdebug = false;
    129  1.1  brad 
    130  1.1  brad 	if (iic_use_direct_match(ia, match, NULL, &match_result))
    131  1.1  brad 		return match_result;
    132  1.1  brad 
    133  1.1  brad 	if (matchdebug) {
    134  1.1  brad 		printf("Looking at ia_addr: %x\n",ia->ia_addr);
    135  1.1  brad 	}
    136  1.1  brad 
    137  1.1  brad 	/* indirect config - check for configured address */
    138  1.1  brad 	if (!(ia->ia_addr >= SCMD_LOW_I2C_ADDR &&
    139  1.1  brad 	    ia->ia_addr <= SCMD_HIGH_I2C_ADDR))
    140  1.1  brad 		return 0;
    141  1.1  brad 
    142  1.1  brad 	/*
    143  1.1  brad 	 * Check to see if something is really at this i2c address.
    144  1.1  brad 	 * This will keep phantom devices from appearing
    145  1.1  brad 	 */
    146  1.1  brad 	if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
    147  1.1  brad 		if (matchdebug)
    148  1.1  brad 			printf("in match acquire bus failed\n");
    149  1.1  brad 		return 0;
    150  1.1  brad 	}
    151  1.1  brad 
    152  1.1  brad 	error = scmdi2c_poke(ia->ia_tag, ia->ia_addr, matchdebug);
    153  1.1  brad 	iic_release_bus(ia->ia_tag, 0);
    154  1.1  brad 
    155  1.1  brad 	return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
    156  1.1  brad }
    157  1.1  brad 
    158  1.1  brad static void
    159  1.1  brad scmdi2c_attach(device_t parent, device_t self, void *aux)
    160  1.1  brad {
    161  1.1  brad 	struct scmd_sc *sc;
    162  1.1  brad 	struct i2c_attach_args *ia;
    163  1.1  brad 
    164  1.1  brad 	ia = aux;
    165  1.1  brad 	sc = device_private(self);
    166  1.1  brad 
    167  1.1  brad 	sc->sc_dev = self;
    168  1.1  brad 	sc->sc_tag = ia->ia_tag;
    169  1.1  brad 	sc->sc_addr = ia->ia_addr;
    170  1.1  brad 	sc->sc_scmddebug = 0;
    171  1.1  brad 	sc->sc_topaddr = 0xff;
    172  1.1  brad 	sc->sc_opened = false;
    173  1.1  brad 	sc->sc_dying = false;
    174  1.1  brad 	sc->sc_func_acquire_bus = &scmdi2c_acquire_bus;
    175  1.1  brad 	sc->sc_func_release_bus = &scmdi2c_release_bus;
    176  1.1  brad 	sc->sc_func_read_register = &scmdi2c_read_reg;
    177  1.1  brad 	sc->sc_func_write_register = &scmdi2c_write_reg;
    178  1.1  brad 
    179  1.1  brad 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
    180  1.1  brad 	mutex_init(&sc->sc_condmutex, MUTEX_DEFAULT, IPL_NONE);
    181  1.1  brad 	mutex_init(&sc->sc_dying_mutex, MUTEX_DEFAULT, IPL_NONE);
    182  1.1  brad 	cv_init(&sc->sc_condvar, "scmdi2ccv");
    183  1.1  brad 	cv_init(&sc->sc_cond_dying, "scmdi2cdc");
    184  1.1  brad 
    185  1.1  brad 	scmd_attach(sc);
    186  1.1  brad 
    187  1.1  brad 	return;
    188  1.1  brad }
    189  1.1  brad 
    190  1.1  brad static int
    191  1.1  brad scmdi2c_detach(device_t self, int flags)
    192  1.1  brad {
    193  1.1  brad 	struct scmd_sc *sc;
    194  1.1  brad 
    195  1.1  brad 	sc = device_private(self);
    196  1.1  brad 
    197  1.1  brad 	mutex_enter(&sc->sc_mutex);
    198  1.1  brad 	sc->sc_dying = true;
    199  1.1  brad 	/* If this is true we are still open, destroy the condvar */
    200  1.1  brad 	if (sc->sc_opened) {
    201  1.1  brad 		mutex_enter(&sc->sc_dying_mutex);
    202  1.1  brad 		DPRINTF(sc, 2, ("%s: Will wait for anything to exit\n",
    203  1.1  brad 		    device_xname(sc->sc_dev)));
    204  1.1  brad 		/* In the worst case this will time out after 5 seconds.
    205  1.1  brad 		 * It really should not take that long for the drain / whatever
    206  1.1  brad 		 * to happen
    207  1.1  brad 		 */
    208  1.1  brad 		cv_timedwait_sig(&sc->sc_cond_dying,
    209  1.1  brad 		    &sc->sc_dying_mutex, mstohz(5000));
    210  1.1  brad 		mutex_exit(&sc->sc_dying_mutex);
    211  1.1  brad 		cv_destroy(&sc->sc_cond_dying);
    212  1.1  brad 	}
    213  1.1  brad 	cv_destroy(&sc->sc_condvar);
    214  1.1  brad 	mutex_exit(&sc->sc_mutex);
    215  1.1  brad 
    216  1.1  brad 	mutex_destroy(&sc->sc_mutex);
    217  1.1  brad 	mutex_destroy(&sc->sc_condmutex);
    218  1.1  brad 
    219  1.1  brad 	return 0;
    220  1.1  brad }
    221  1.1  brad 
    222  1.1  brad int
    223  1.1  brad scmdi2c_activate(device_t self, enum devact act)
    224  1.1  brad {
    225  1.1  brad 	struct scmd_sc *sc = device_private(self);
    226  1.1  brad 
    227  1.1  brad 	switch (act) {
    228  1.1  brad 	case DVACT_DEACTIVATE:
    229  1.1  brad 		sc->sc_dying = true;
    230  1.1  brad 		return 0;
    231  1.1  brad 	default:
    232  1.1  brad 		return EOPNOTSUPP;
    233  1.1  brad 	}
    234  1.1  brad }
    235  1.1  brad 
    236  1.1  brad MODULE(MODULE_CLASS_DRIVER, scmdi2c, "i2cexec,scmd");
    237  1.1  brad 
    238  1.1  brad #ifdef _MODULE
    239  1.1  brad /* Like other drivers, we do this because the scmd common
    240  1.1  brad  * driver has the definitions already.
    241  1.1  brad  */
    242  1.1  brad #undef  CFDRIVER_DECL
    243  1.1  brad #define CFDRIVER_DECL(name, class, attr)
    244  1.1  brad #include "ioconf.c"
    245  1.1  brad #endif
    246  1.1  brad 
    247  1.1  brad static int
    248  1.1  brad scmdi2c_modcmd(modcmd_t cmd, void *opaque)
    249  1.1  brad {
    250  1.1  brad #ifdef _MODULE
    251  1.1  brad 	int error = 0;
    252  1.1  brad 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    253  1.1  brad #endif
    254  1.1  brad 
    255  1.1  brad 	switch (cmd) {
    256  1.1  brad 	case MODULE_CMD_INIT:
    257  1.1  brad #ifdef _MODULE
    258  1.1  brad 		/* I really do not understand why I had to do this this way.
    259  1.1  brad 		 * If I did not then the module would either not install when
    260  1.1  brad 		 * the SPI driver and hence the scmd dependent driver was compiled
    261  1.1  brad 		 * into the kernel, or it would not install if it was not.
    262  1.1  brad 		 * I suspect something is still not set up correctly somewhere, but
    263  1.1  brad 		 * I am at a lose to see what.
    264  1.1  brad 		 *
    265  1.1  brad 		 * The first config_init_component will fail if the SPI driver and the
    266  1.1  brad 		 * scmd dependent is in the kernel already, but the second one will work.
    267  1.1  brad 		 * Otherwise the other way around.
    268  1.1  brad 		 *
    269  1.1  brad 		 * This all manages to get this module set up in any situation.
    270  1.1  brad 		 */
    271  1.1  brad 		error = config_init_component(cfdriver_ioconf_scmdi2c,
    272  1.1  brad 		    cfattach_ioconf_scmdi2c, cfdata_ioconf_scmdi2c);
    273  1.1  brad 		if (error) {
    274  1.1  brad 			aprint_error("%s: Trying no_cfdriver_vec method: %d\n",
    275  1.1  brad 			    scmd_cd.cd_name, error);
    276  1.1  brad 			error = config_init_component(no_cfdriver_vec,
    277  1.1  brad 			    cfattach_ioconf_scmdi2c, cfdata_ioconf_scmdi2c);
    278  1.1  brad 		}
    279  1.1  brad 
    280  1.1  brad 		return error;
    281  1.1  brad #else
    282  1.1  brad 		return 0;
    283  1.1  brad #endif
    284  1.1  brad 	case MODULE_CMD_FINI:
    285  1.1  brad #ifdef _MODULE
    286  1.1  brad 		/* See above.. same thing */
    287  1.1  brad 		error = config_fini_component(cfdriver_ioconf_scmdi2c,
    288  1.1  brad 		    cfattach_ioconf_scmdi2c, cfdata_ioconf_scmdi2c);
    289  1.1  brad 		if (error) {
    290  1.1  brad 			aprint_error("%s: Trying no_cfdriver_vec method: %d\n",
    291  1.1  brad 			    scmd_cd.cd_name, error);
    292  1.1  brad 			error = config_fini_component(no_cfdriver_vec,
    293  1.1  brad 			    cfattach_ioconf_scmdi2c, cfdata_ioconf_scmdi2c);
    294  1.1  brad 		}
    295  1.1  brad 
    296  1.1  brad 		return error;
    297  1.1  brad #else
    298  1.1  brad 		return 0;
    299  1.1  brad #endif
    300  1.1  brad 	default:
    301  1.1  brad 		return ENOTTY;
    302  1.1  brad 	}
    303  1.1  brad }
    304