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