bmx280thpi2c.c revision 1.4 1 /* $NetBSD: bmx280thpi2c.c,v 1.4 2025/09/13 16:16:40 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2022 Brad Spencer <brad (at) anduin.eldar.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/cdefs.h>
20 __KERNEL_RCSID(0, "$NetBSD: bmx280thpi2c.c,v 1.4 2025/09/13 16:16:40 thorpej Exp $");
21
22 /*
23 * I2C driver for the Bosch BMP280 / BME280 sensor.
24 * Uses the common bmx280thp driver to do the real work.
25 */
26
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/device.h>
31 #include <sys/module.h>
32 #include <sys/conf.h>
33 #include <sys/sysctl.h>
34 #include <sys/mutex.h>
35 #include <sys/condvar.h>
36 #include <sys/pool.h>
37 #include <sys/kmem.h>
38
39 #include <dev/sysmon/sysmonvar.h>
40 #include <dev/i2c/i2cvar.h>
41 #include <dev/spi/spivar.h>
42 #include <dev/ic/bmx280reg.h>
43 #include <dev/ic/bmx280var.h>
44
45 struct bmx280_i2c_softc {
46 struct bmx280_sc sc_bmx280;
47 i2c_tag_t sc_tag;
48 i2c_addr_t sc_addr;
49 };
50
51 #define BMX280_TO_I2C(sc) \
52 container_of((sc), struct bmx280_i2c_softc, sc_bmx280)
53
54 static int bmx280thpi2c_poke(i2c_tag_t, i2c_addr_t, bool);
55 static int bmx280thpi2c_match(device_t, cfdata_t, void *);
56 static void bmx280thpi2c_attach(device_t, device_t, void *);
57 static int bmx280thpi2c_detach(device_t, int);
58
59 CFATTACH_DECL_NEW(bmx280thpi2c, sizeof(struct bmx280_sc),
60 bmx280thpi2c_match, bmx280thpi2c_attach, bmx280thpi2c_detach, NULL);
61
62 /* For the BMX280, a read consists of writing on the I2C bus
63 * a I2C START, I2C SLAVE address, then the starting register.
64 * If that works, then following will be another I2C START,
65 * I2C SLAVE address, followed by as many I2C reads that is
66 * desired and then a I2C STOP
67 */
68
69 static int
70 bmx280thpi2c_read_register_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
71 uint8_t *buf, size_t blen)
72 {
73 int error;
74
75 error = iic_exec(tag,I2C_OP_WRITE,addr,®,1,NULL,0,0);
76
77 if (error == 0) {
78 error = iic_exec(tag,I2C_OP_READ_WITH_STOP,addr,NULL,0,
79 buf,blen,0);
80 }
81
82 return error;
83 }
84
85 static int
86 bmx280thpi2c_read_register(struct bmx280_sc *sc, uint8_t reg,
87 uint8_t *buf, size_t blen)
88 {
89 struct bmx280_i2c_softc *isc = BMX280_TO_I2C(sc);
90 int error;
91
92 KASSERT(blen > 0);
93
94 error = bmx280thpi2c_read_register_direct(isc->sc_tag, isc->sc_addr,
95 reg, buf, blen);
96
97 return error;
98 }
99
100 /* For the BMX280, a write consists of sending a I2C START, I2C SLAVE
101 * address and then pairs of registers and data until a I2C STOP is
102 * sent.
103 */
104
105 static int
106 bmx280thpi2c_write_register(struct bmx280_sc *sc, uint8_t *buf, size_t blen)
107 {
108 struct bmx280_i2c_softc *isc = BMX280_TO_I2C(sc);
109 int error;
110
111 KASSERT(blen > 0);
112 /* XXX - there should be a KASSERT for blen at least
113 being an even number */
114
115 error = iic_exec(isc->sc_tag,I2C_OP_WRITE_WITH_STOP,isc->sc_addr,NULL,0,
116 buf,blen,0);
117
118 return error;
119 }
120
121 static int
122 bmx280thpi2c_acquire_bus(struct bmx280_sc *sc)
123 {
124 struct bmx280_i2c_softc *isc = BMX280_TO_I2C(sc);
125
126 return(iic_acquire_bus(isc->sc_tag, 0));
127 }
128
129 static void
130 bmx280thpi2c_release_bus(struct bmx280_sc *sc)
131 {
132 struct bmx280_i2c_softc *isc = BMX280_TO_I2C(sc);
133
134 iic_release_bus(isc->sc_tag, 0);
135 }
136
137 static const struct bmx280_accessfuncs bmx280_i2c_accessfuncs = {
138 .acquire_bus = bmx280thpi2c_acquire_bus,
139 .release_bus = bmx280thpi2c_release_bus,
140 .read_reg = bmx280thpi2c_read_register,
141 .write_reg = bmx280thpi2c_write_register,
142 };
143
144 static int
145 bmx280thpi2c_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
146 {
147 uint8_t reg = BMX280_REGISTER_ID;
148 uint8_t buf[1];
149 int error;
150
151 error = bmx280thpi2c_read_register_direct(tag, addr, reg, buf, 1);
152 if (matchdebug) {
153 printf("poke X 1: %d\n", error);
154 }
155 return error;
156 }
157
158 static int
159 bmx280thpi2c_match(device_t parent, cfdata_t cf, void *aux)
160 {
161 struct i2c_attach_args *ia = aux;
162 int error, match_result;
163 const bool matchdebug = false;
164
165 if (iic_use_direct_match(ia, cf, bmx280_compat_data, &match_result)) {
166 return match_result;
167 }
168
169 /* indirect config - check for configured address */
170 if (ia->ia_addr != BMX280_TYPICAL_ADDR_1 &&
171 ia->ia_addr != BMX280_TYPICAL_ADDR_2)
172 return 0;
173
174 /*
175 * Check to see if something is really at this i2c address. This will
176 * keep phantom devices from appearing
177 */
178 if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
179 if (matchdebug)
180 printf("in match acquire bus failed\n");
181 return 0;
182 }
183
184 error = bmx280thpi2c_poke(ia->ia_tag, ia->ia_addr, matchdebug);
185 iic_release_bus(ia->ia_tag, 0);
186
187 return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
188 }
189
190 static void
191 bmx280thpi2c_attach(device_t parent, device_t self, void *aux)
192 {
193 struct bmx280_i2c_softc *isc = device_private(self);
194 struct bmx280_sc *sc = &isc->sc_bmx280;
195 struct i2c_attach_args *ia = aux;
196
197 sc->sc_dev = self;
198 sc->sc_funcs = &bmx280_i2c_accessfuncs;
199
200 isc->sc_tag = ia->ia_tag;
201 isc->sc_addr = ia->ia_addr;
202
203 bmx280_attach(sc);
204 }
205
206 static int
207 bmx280thpi2c_detach(device_t self, int flags)
208 {
209 struct bmx280_i2c_softc *isc = device_private(self);
210
211 return bmx280_detach(&isc->sc_bmx280, flags);
212 }
213
214 MODULE(MODULE_CLASS_DRIVER, bmx280thpi2c, "iic,bmx280thp");
215
216 #ifdef _MODULE
217 /* Like other drivers, we do this because the bmx280 common
218 * driver has the definitions already.
219 */
220 #undef CFDRIVER_DECL
221 #define CFDRIVER_DECL(name, class, attr)
222 #include "ioconf.c"
223 #endif
224
225 static int
226 bmx280thpi2c_modcmd(modcmd_t cmd, void *opaque)
227 {
228
229 switch (cmd) {
230 case MODULE_CMD_INIT:
231 #ifdef _MODULE
232 return config_init_component(cfdriver_ioconf_bmx280thpi2c,
233 cfattach_ioconf_bmx280thpi2c, cfdata_ioconf_bmx280thpi2c);
234 #else
235 return 0;
236 #endif
237 case MODULE_CMD_FINI:
238 #ifdef _MODULE
239 return config_fini_component(cfdriver_ioconf_bmx280thpi2c,
240 cfattach_ioconf_bmx280thpi2c, cfdata_ioconf_bmx280thpi2c);
241 #else
242 return 0;
243 #endif
244 default:
245 return ENOTTY;
246 }
247 }
248