bmx280thpspi.c revision 1.5
11.5Sthorpej/* $NetBSD: bmx280thpspi.c,v 1.5 2025/09/13 14:10:44 thorpej Exp $ */ 21.1Sbrad 31.1Sbrad/* 41.1Sbrad * Copyright (c) 2022 Brad Spencer <brad@anduin.eldar.org> 51.1Sbrad * 61.1Sbrad * Permission to use, copy, modify, and distribute this software for any 71.1Sbrad * purpose with or without fee is hereby granted, provided that the above 81.1Sbrad * copyright notice and this permission notice appear in all copies. 91.1Sbrad * 101.1Sbrad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 111.1Sbrad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 121.1Sbrad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 131.1Sbrad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 141.1Sbrad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 151.1Sbrad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 161.1Sbrad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 171.1Sbrad */ 181.1Sbrad 191.1Sbrad#include <sys/cdefs.h> 201.5Sthorpej__KERNEL_RCSID(0, "$NetBSD: bmx280thpspi.c,v 1.5 2025/09/13 14:10:44 thorpej Exp $"); 211.1Sbrad 221.1Sbrad/* 231.1Sbrad * SPI driver for the Bosch BMP280 / BME280 sensor. 241.1Sbrad * Uses the common bmx280thp driver to do the real work. 251.1Sbrad*/ 261.1Sbrad 271.1Sbrad#include <sys/param.h> 281.1Sbrad#include <sys/systm.h> 291.1Sbrad#include <sys/kernel.h> 301.1Sbrad#include <sys/device.h> 311.1Sbrad#include <sys/module.h> 321.1Sbrad#include <sys/conf.h> 331.1Sbrad#include <sys/sysctl.h> 341.1Sbrad#include <sys/mutex.h> 351.1Sbrad#include <sys/condvar.h> 361.1Sbrad#include <sys/pool.h> 371.1Sbrad#include <sys/kmem.h> 381.1Sbrad 391.1Sbrad#include <dev/sysmon/sysmonvar.h> 401.1Sbrad#include <dev/i2c/i2cvar.h> 411.1Sbrad#include <dev/spi/spivar.h> 421.1Sbrad#include <dev/ic/bmx280reg.h> 431.1Sbrad#include <dev/ic/bmx280var.h> 441.1Sbrad 451.4Sthorpej/* 461.4Sthorpej * XXX Should add configuration information for variants ... 471.4Sthorpej * XXX E devices have humidity sensors, P devices do not. 481.4Sthorpej */ 491.4Sthorpejstatic const struct device_compatible_entry compat_data[] = { 501.4Sthorpej { .compat = "bosch,bmp280" }, 511.4Sthorpej { .compat = "bosch,bme280" }, 521.4Sthorpej#if 0 531.4Sthorpej /* 541.4Sthorpej * XXX Should also add support for: 551.4Sthorpej * bosch,bmp085 561.4Sthorpej * bosch,bmp180 571.4Sthorpej * bosch,bmp380 581.4Sthorpej * bosch,bmp580 591.4Sthorpej */ 601.4Sthorpej#endif 611.4Sthorpej DEVICE_COMPAT_EOL 621.4Sthorpej}; 631.4Sthorpej 641.1Sbradextern void bmx280_attach(struct bmx280_sc *); 651.1Sbrad 661.1Sbradstatic int bmx280thpspi_match(device_t, cfdata_t, void *); 671.1Sbradstatic void bmx280thpspi_attach(device_t, device_t, void *); 681.1Sbradstatic int bmx280thpspi_detach(device_t, int); 691.1Sbrad 701.1Sbrad#define BMX280_DEBUG 711.1Sbrad#ifdef BMX280_DEBUG 721.1Sbrad#define DPRINTF(s, l, x) \ 731.1Sbrad do { \ 741.1Sbrad if (l <= s->sc_bmx280debug) \ 751.1Sbrad printf x; \ 761.1Sbrad } while (/*CONSTCOND*/0) 771.1Sbrad#else 781.1Sbrad#define DPRINTF(s, l, x) 791.1Sbrad#endif 801.1Sbrad 811.1SbradCFATTACH_DECL_NEW(bmx280thpspi, sizeof(struct bmx280_sc), 821.1Sbrad bmx280thpspi_match, bmx280thpspi_attach, bmx280thpspi_detach, NULL); 831.1Sbrad 841.1Sbrad/* The SPI interface of the chip, assuming that it has managed to get into that 851.1Sbrad * mode to start with, is pretty simple. Simply send the register MINUS the 7th 861.1Sbrad * bit which will be 1 and then do as many reads as you want. The chip will 871.1Sbrad * auto increment for you. 881.1Sbrad * 891.1Sbrad * The delays are only hinted at in the data sheet. 901.1Sbrad */ 911.1Sbrad 921.1Sbradstatic int 931.5Sthorpejbmx280thpspi_read_reg_direct(spi_handle_t sh, uint8_t reg, 941.1Sbrad uint8_t *buf, size_t rlen) 951.1Sbrad{ 961.1Sbrad int err = 0; 971.1Sbrad uint8_t rreg = reg | 0x80; 981.1Sbrad 991.1Sbrad if (buf != NULL) { 1001.1Sbrad err = spi_send_recv(sh, 1, &rreg, 1011.1Sbrad rlen, buf); 1021.1Sbrad } else { 1031.1Sbrad err = spi_send(sh, 1, &rreg); 1041.1Sbrad } 1051.1Sbrad 1061.1Sbrad return err; 1071.1Sbrad} 1081.1Sbrad 1091.1Sbradstatic int 1101.1Sbradbmx280thpspi_read_reg(struct bmx280_sc *sc, uint8_t reg, uint8_t *buf, size_t rlen) 1111.1Sbrad{ 1121.1Sbrad return bmx280thpspi_read_reg_direct(sc->sc_sh, reg, buf, rlen); 1131.1Sbrad} 1141.1Sbrad 1151.1Sbrad/* SPI writes to this device are normal enough. You send the register 1161.1Sbrad * you want making sure that the high bit, 0x80, is clear and then the 1171.1Sbrad * data. These pairs can be repeated as many times as you like. 1181.1Sbrad */ 1191.1Sbradstatic int 1201.5Sthorpejbmx280thpspi_write_reg_direct(spi_handle_t sh, uint8_t *buf, size_t slen) 1211.1Sbrad{ 1221.1Sbrad int err = 0; 1231.1Sbrad int i; 1241.1Sbrad 1251.1Sbrad /* XXX - 1261.1Sbrad this is probably BAD thing to do... but we must insure that the 1271.1Sbrad registers have a cleared bit.. otherwise it is a read .... 1281.1Sbrad */ 1291.1Sbrad 1301.1Sbrad for(i = 0; i < slen;i+=2) { 1311.1Sbrad buf[i] = buf[i] & 0x7F; 1321.1Sbrad } 1331.1Sbrad 1341.1Sbrad err = spi_send(sh, slen, buf); 1351.1Sbrad 1361.1Sbrad return err; 1371.1Sbrad} 1381.1Sbrad 1391.1Sbradstatic int 1401.1Sbradbmx280thpspi_write_reg(struct bmx280_sc *sc, uint8_t *buf, size_t slen) 1411.1Sbrad{ 1421.1Sbrad return bmx280thpspi_write_reg_direct(sc->sc_sh, buf, slen); 1431.1Sbrad} 1441.1Sbrad 1451.1Sbrad/* These are to satisfy the common code */ 1461.1Sbradstatic int 1471.1Sbradbmx280thpspi_acquire_bus(struct bmx280_sc *sc) 1481.1Sbrad{ 1491.1Sbrad return 0; 1501.1Sbrad} 1511.1Sbrad 1521.1Sbradstatic void 1531.1Sbradbmx280thpspi_release_bus(struct bmx280_sc *sc) 1541.1Sbrad{ 1551.1Sbrad return; 1561.1Sbrad} 1571.1Sbrad 1581.1Sbrad/* Nothing more is done here. Assumptions on whether or not 1591.1Sbrad * the SPI interface is set up may not be proper.... for better 1601.1Sbrad * or worse... and there is setting that are needed such as the 1611.1Sbrad * SPI mode and bus speed that really should not be done here, so 1621.1Sbrad * any active match might not work anyway. 1631.1Sbrad */ 1641.1Sbradstatic int 1651.1Sbradbmx280thpspi_match(device_t parent, cfdata_t match, void *aux) 1661.1Sbrad{ 1671.4Sthorpej struct spi_attach_args *sa = aux; 1681.4Sthorpej int match_result; 1691.1Sbrad 1701.4Sthorpej if (spi_use_direct_match(sa, compat_data, &match_result)) { 1711.4Sthorpej return match_result; 1721.1Sbrad } 1731.1Sbrad 1741.3Sthorpej return SPI_MATCH_DEFAULT; 1751.1Sbrad} 1761.1Sbrad 1771.1Sbradstatic void 1781.1Sbradbmx280thpspi_attach(device_t parent, device_t self, void *aux) 1791.1Sbrad{ 1801.1Sbrad struct bmx280_sc *sc; 1811.1Sbrad struct spi_attach_args *sa; 1821.1Sbrad int error; 1831.1Sbrad 1841.1Sbrad sa = aux; 1851.1Sbrad sc = device_private(self); 1861.1Sbrad 1871.1Sbrad sc->sc_dev = self; 1881.1Sbrad sc->sc_sh = sa->sa_handle; 1891.1Sbrad sc->sc_bmx280debug = 0; 1901.1Sbrad sc->sc_func_acquire_bus = &bmx280thpspi_acquire_bus; 1911.1Sbrad sc->sc_func_release_bus = &bmx280thpspi_release_bus; 1921.1Sbrad sc->sc_func_read_register = &bmx280thpspi_read_reg; 1931.1Sbrad sc->sc_func_write_register = &bmx280thpspi_write_reg; 1941.1Sbrad 1951.1Sbrad /* Configure for 1MHz and SPI mode 0 according to the data sheet. 1961.1Sbrad * The chip will actually handle a number of different modes and 1971.1Sbrad * can go a lot faster, just use this for now... 1981.1Sbrad */ 1991.2Sthorpej error = spi_configure(self, sa->sa_handle, SPI_MODE_0, SPI_FREQ_MHz(1)); 2001.1Sbrad if (error) { 2011.1Sbrad return; 2021.1Sbrad } 2031.1Sbrad 2041.4Sthorpej /* 2051.4Sthorpej * XXX Need to get this data from the device tree: 2061.4Sthorpej * 2071.4Sthorpej * vddd-supply (a regulator) 2081.4Sthorpej * vdda-supply (a regulator) 2091.4Sthorpej * reset-gpios (a gpio, active-low reset) 2101.4Sthorpej */ 2111.4Sthorpej 2121.1Sbrad /* Please note that if the pins are not set up for SPI, the attachment 2131.1Sbrad * will probably not work out. 2141.1Sbrad */ 2151.1Sbrad bmx280_attach(sc); 2161.1Sbrad 2171.1Sbrad return; 2181.1Sbrad} 2191.1Sbrad 2201.1Sbrad/* These really do not do a whole lot, as SPI devices do not seem to work 2211.1Sbrad * as modules. 2221.1Sbrad */ 2231.1Sbradstatic int 2241.1Sbradbmx280thpspi_detach(device_t self, int flags) 2251.1Sbrad{ 2261.1Sbrad struct bmx280_sc *sc; 2271.1Sbrad 2281.1Sbrad sc = device_private(self); 2291.1Sbrad 2301.1Sbrad mutex_destroy(&sc->sc_mutex); 2311.1Sbrad 2321.1Sbrad return 0; 2331.1Sbrad} 234