Home | History | Annotate | Line # | Download | only in i2c
      1  1.1      brad 
      2  1.3   thorpej /*	$NetBSD: scmdi2c.c,v 1.3 2025/09/12 13:48:26 thorpej 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.3   thorpej __KERNEL_RCSID(0, "$NetBSD: scmdi2c.c,v 1.3 2025/09/12 13:48:26 thorpej 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.3   thorpej static const struct device_compatible_entry compat_data[] = {
     46  1.3   thorpej 	{ .compat = "sparkfun,scmd-motor-driver" },
     47  1.3   thorpej 
     48  1.3   thorpej 	DEVICE_COMPAT_EOL
     49  1.3   thorpej };
     50  1.3   thorpej 
     51  1.1      brad extern struct cdevsw scmd_cdevsw;
     52  1.1      brad 
     53  1.1      brad extern void	scmd_attach(struct scmd_sc *);
     54  1.1      brad 
     55  1.1      brad static int 	scmdi2c_poke(i2c_tag_t, i2c_addr_t, bool);
     56  1.1      brad static int 	scmdi2c_match(device_t, cfdata_t, void *);
     57  1.1      brad static void 	scmdi2c_attach(device_t, device_t, void *);
     58  1.1      brad static int 	scmdi2c_detach(device_t, int);
     59  1.1      brad static int	scmdi2c_activate(device_t, enum devact);
     60  1.1      brad 
     61  1.1      brad #define SCMD_DEBUG
     62  1.1      brad #ifdef SCMD_DEBUG
     63  1.1      brad #define DPRINTF(s, l, x) \
     64  1.1      brad     do { \
     65  1.1      brad 	if (l <= s->sc_scmddebug) \
     66  1.1      brad 	    printf x; \
     67  1.1      brad     } while (/*CONSTCOND*/0)
     68  1.1      brad #else
     69  1.1      brad #define DPRINTF(s, l, x)
     70  1.1      brad #endif
     71  1.1      brad 
     72  1.1      brad CFATTACH_DECL_NEW(scmdi2c, sizeof(struct scmd_sc),
     73  1.1      brad     scmdi2c_match, scmdi2c_attach, scmdi2c_detach, scmdi2c_activate);
     74  1.1      brad 
     75  1.1      brad static int
     76  1.1      brad scmdi2c_read_reg_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
     77  1.1      brad     uint8_t *buf)
     78  1.1      brad {
     79  1.1      brad 	return iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &reg, 1, buf,
     80  1.1      brad 	    1, 0);
     81  1.1      brad }
     82  1.1      brad 
     83  1.1      brad static int
     84  1.1      brad scmdi2c_read_reg(struct scmd_sc *sc, uint8_t reg, uint8_t *buf)
     85  1.1      brad {
     86  1.1      brad 	return scmdi2c_read_reg_direct(sc->sc_tag, sc->sc_addr, reg, buf);
     87  1.1      brad }
     88  1.1      brad 
     89  1.1      brad static int
     90  1.1      brad scmdi2c_write_reg_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
     91  1.1      brad     uint8_t buf)
     92  1.1      brad {
     93  1.1      brad 	return iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &reg, 1, &buf,
     94  1.1      brad 	    1, 0);
     95  1.1      brad }
     96  1.1      brad 
     97  1.1      brad static int
     98  1.1      brad scmdi2c_write_reg(struct scmd_sc *sc, uint8_t reg, uint8_t buf)
     99  1.1      brad {
    100  1.1      brad 	return scmdi2c_write_reg_direct(sc->sc_tag, sc->sc_addr, reg, buf);
    101  1.1      brad }
    102  1.1      brad 
    103  1.1      brad static int
    104  1.1      brad scmdi2c_acquire_bus(struct scmd_sc *sc)
    105  1.1      brad {
    106  1.1      brad 	return(iic_acquire_bus(sc->sc_tag, 0));
    107  1.1      brad }
    108  1.1      brad 
    109  1.1      brad static void
    110  1.1      brad scmdi2c_release_bus(struct scmd_sc *sc)
    111  1.1      brad {
    112  1.1      brad 	iic_release_bus(sc->sc_tag, 0);
    113  1.1      brad }
    114  1.1      brad 
    115  1.1      brad static int
    116  1.1      brad scmdi2c_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
    117  1.1      brad {
    118  1.1      brad 	uint8_t reg = SCMD_REG_ID;
    119  1.1      brad 	uint8_t buf;
    120  1.1      brad 	int error;
    121  1.1      brad 
    122  1.1      brad 	error = scmdi2c_read_reg_direct(tag, addr, reg, &buf);
    123  1.1      brad 	if (matchdebug) {
    124  1.1      brad 		printf("poke X 1: %d %02x %d\n", addr, buf, error);
    125  1.1      brad 	}
    126  1.1      brad 	return error;
    127  1.1      brad }
    128  1.1      brad 
    129  1.1      brad static int
    130  1.3   thorpej scmdi2c_match(device_t parent, cfdata_t cf, void *aux)
    131  1.1      brad {
    132  1.1      brad 	struct i2c_attach_args *ia = aux;
    133  1.1      brad 	int error, match_result;
    134  1.1      brad 	const bool matchdebug = false;
    135  1.1      brad 
    136  1.3   thorpej 	if (iic_use_direct_match(ia, cf, compat_data, &match_result))
    137  1.1      brad 		return match_result;
    138  1.1      brad 
    139  1.1      brad 	if (matchdebug) {
    140  1.1      brad 		printf("Looking at ia_addr: %x\n",ia->ia_addr);
    141  1.1      brad 	}
    142  1.1      brad 
    143  1.1      brad 	/* indirect config - check for configured address */
    144  1.1      brad 	if (!(ia->ia_addr >= SCMD_LOW_I2C_ADDR &&
    145  1.1      brad 	    ia->ia_addr <= SCMD_HIGH_I2C_ADDR))
    146  1.1      brad 		return 0;
    147  1.1      brad 
    148  1.1      brad 	/*
    149  1.1      brad 	 * Check to see if something is really at this i2c address.
    150  1.1      brad 	 * This will keep phantom devices from appearing
    151  1.1      brad 	 */
    152  1.1      brad 	if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
    153  1.1      brad 		if (matchdebug)
    154  1.1      brad 			printf("in match acquire bus failed\n");
    155  1.1      brad 		return 0;
    156  1.1      brad 	}
    157  1.1      brad 
    158  1.1      brad 	error = scmdi2c_poke(ia->ia_tag, ia->ia_addr, matchdebug);
    159  1.1      brad 	iic_release_bus(ia->ia_tag, 0);
    160  1.1      brad 
    161  1.1      brad 	return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
    162  1.1      brad }
    163  1.1      brad 
    164  1.1      brad static void
    165  1.1      brad scmdi2c_attach(device_t parent, device_t self, void *aux)
    166  1.1      brad {
    167  1.1      brad 	struct scmd_sc *sc;
    168  1.1      brad 	struct i2c_attach_args *ia;
    169  1.1      brad 
    170  1.1      brad 	ia = aux;
    171  1.1      brad 	sc = device_private(self);
    172  1.1      brad 
    173  1.1      brad 	sc->sc_dev = self;
    174  1.1      brad 	sc->sc_tag = ia->ia_tag;
    175  1.1      brad 	sc->sc_addr = ia->ia_addr;
    176  1.1      brad 	sc->sc_scmddebug = 0;
    177  1.1      brad 	sc->sc_topaddr = 0xff;
    178  1.1      brad 	sc->sc_opened = false;
    179  1.1      brad 	sc->sc_dying = false;
    180  1.1      brad 	sc->sc_func_acquire_bus = &scmdi2c_acquire_bus;
    181  1.1      brad 	sc->sc_func_release_bus = &scmdi2c_release_bus;
    182  1.1      brad 	sc->sc_func_read_register = &scmdi2c_read_reg;
    183  1.1      brad 	sc->sc_func_write_register = &scmdi2c_write_reg;
    184  1.1      brad 
    185  1.1      brad 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
    186  1.1      brad 	mutex_init(&sc->sc_condmutex, MUTEX_DEFAULT, IPL_NONE);
    187  1.1      brad 	mutex_init(&sc->sc_dying_mutex, MUTEX_DEFAULT, IPL_NONE);
    188  1.1      brad 	cv_init(&sc->sc_condvar, "scmdi2ccv");
    189  1.1      brad 	cv_init(&sc->sc_cond_dying, "scmdi2cdc");
    190  1.1      brad 
    191  1.1      brad 	scmd_attach(sc);
    192  1.1      brad 
    193  1.1      brad 	return;
    194  1.1      brad }
    195  1.1      brad 
    196  1.1      brad static int
    197  1.1      brad scmdi2c_detach(device_t self, int flags)
    198  1.1      brad {
    199  1.1      brad 	struct scmd_sc *sc;
    200  1.1      brad 
    201  1.1      brad 	sc = device_private(self);
    202  1.1      brad 
    203  1.1      brad 	mutex_enter(&sc->sc_mutex);
    204  1.1      brad 	sc->sc_dying = true;
    205  1.1      brad 	/* If this is true we are still open, destroy the condvar */
    206  1.1      brad 	if (sc->sc_opened) {
    207  1.1      brad 		mutex_enter(&sc->sc_dying_mutex);
    208  1.1      brad 		DPRINTF(sc, 2, ("%s: Will wait for anything to exit\n",
    209  1.1      brad 		    device_xname(sc->sc_dev)));
    210  1.1      brad 		/* In the worst case this will time out after 5 seconds.
    211  1.1      brad 		 * It really should not take that long for the drain / whatever
    212  1.1      brad 		 * to happen
    213  1.1      brad 		 */
    214  1.1      brad 		cv_timedwait_sig(&sc->sc_cond_dying,
    215  1.1      brad 		    &sc->sc_dying_mutex, mstohz(5000));
    216  1.1      brad 		mutex_exit(&sc->sc_dying_mutex);
    217  1.1      brad 		cv_destroy(&sc->sc_cond_dying);
    218  1.1      brad 	}
    219  1.1      brad 	cv_destroy(&sc->sc_condvar);
    220  1.1      brad 	mutex_exit(&sc->sc_mutex);
    221  1.1      brad 
    222  1.1      brad 	mutex_destroy(&sc->sc_mutex);
    223  1.1      brad 	mutex_destroy(&sc->sc_condmutex);
    224  1.1      brad 
    225  1.1      brad 	return 0;
    226  1.1      brad }
    227  1.1      brad 
    228  1.1      brad int
    229  1.1      brad scmdi2c_activate(device_t self, enum devact act)
    230  1.1      brad {
    231  1.1      brad 	struct scmd_sc *sc = device_private(self);
    232  1.1      brad 
    233  1.1      brad 	switch (act) {
    234  1.1      brad 	case DVACT_DEACTIVATE:
    235  1.1      brad 		sc->sc_dying = true;
    236  1.1      brad 		return 0;
    237  1.1      brad 	default:
    238  1.1      brad 		return EOPNOTSUPP;
    239  1.1      brad 	}
    240  1.1      brad }
    241  1.1      brad 
    242  1.2  pgoyette MODULE(MODULE_CLASS_DRIVER, scmdi2c, "iic,scmd");
    243  1.1      brad 
    244  1.1      brad #ifdef _MODULE
    245  1.1      brad /* Like other drivers, we do this because the scmd common
    246  1.1      brad  * driver has the definitions already.
    247  1.1      brad  */
    248  1.1      brad #undef  CFDRIVER_DECL
    249  1.1      brad #define CFDRIVER_DECL(name, class, attr)
    250  1.1      brad #include "ioconf.c"
    251  1.1      brad #endif
    252  1.1      brad 
    253  1.1      brad static int
    254  1.1      brad scmdi2c_modcmd(modcmd_t cmd, void *opaque)
    255  1.1      brad {
    256  1.1      brad #ifdef _MODULE
    257  1.1      brad 	int error = 0;
    258  1.1      brad 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    259  1.1      brad #endif
    260  1.1      brad 
    261  1.1      brad 	switch (cmd) {
    262  1.1      brad 	case MODULE_CMD_INIT:
    263  1.1      brad #ifdef _MODULE
    264  1.1      brad 		/* I really do not understand why I had to do this this way.
    265  1.1      brad 		 * If I did not then the module would either not install when
    266  1.1      brad 		 * the SPI driver and hence the scmd dependent driver was compiled
    267  1.1      brad 		 * into the kernel, or it would not install if it was not.
    268  1.1      brad 		 * I suspect something is still not set up correctly somewhere, but
    269  1.1      brad 		 * I am at a lose to see what.
    270  1.1      brad 		 *
    271  1.1      brad 		 * The first config_init_component will fail if the SPI driver and the
    272  1.1      brad 		 * scmd dependent is in the kernel already, but the second one will work.
    273  1.1      brad 		 * Otherwise the other way around.
    274  1.1      brad 		 *
    275  1.1      brad 		 * This all manages to get this module set up in any situation.
    276  1.1      brad 		 */
    277  1.1      brad 		error = config_init_component(cfdriver_ioconf_scmdi2c,
    278  1.1      brad 		    cfattach_ioconf_scmdi2c, cfdata_ioconf_scmdi2c);
    279  1.1      brad 		if (error) {
    280  1.1      brad 			aprint_error("%s: Trying no_cfdriver_vec method: %d\n",
    281  1.1      brad 			    scmd_cd.cd_name, error);
    282  1.1      brad 			error = config_init_component(no_cfdriver_vec,
    283  1.1      brad 			    cfattach_ioconf_scmdi2c, cfdata_ioconf_scmdi2c);
    284  1.1      brad 		}
    285  1.1      brad 
    286  1.1      brad 		return error;
    287  1.1      brad #else
    288  1.1      brad 		return 0;
    289  1.1      brad #endif
    290  1.1      brad 	case MODULE_CMD_FINI:
    291  1.1      brad #ifdef _MODULE
    292  1.1      brad 		/* See above.. same thing */
    293  1.1      brad 		error = config_fini_component(cfdriver_ioconf_scmdi2c,
    294  1.1      brad 		    cfattach_ioconf_scmdi2c, cfdata_ioconf_scmdi2c);
    295  1.1      brad 		if (error) {
    296  1.1      brad 			aprint_error("%s: Trying no_cfdriver_vec method: %d\n",
    297  1.1      brad 			    scmd_cd.cd_name, error);
    298  1.1      brad 			error = config_fini_component(no_cfdriver_vec,
    299  1.1      brad 			    cfattach_ioconf_scmdi2c, cfdata_ioconf_scmdi2c);
    300  1.1      brad 		}
    301  1.1      brad 
    302  1.1      brad 		return error;
    303  1.1      brad #else
    304  1.1      brad 		return 0;
    305  1.1      brad #endif
    306  1.1      brad 	default:
    307  1.1      brad 		return ENOTTY;
    308  1.1      brad 	}
    309  1.1      brad }
    310