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