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