bmx280thpspi.c revision 1.7
11.7Sthorpej/*	$NetBSD: bmx280thpspi.c,v 1.7 2025/09/13 16:16:40 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.7Sthorpej__KERNEL_RCSID(0, "$NetBSD: bmx280thpspi.c,v 1.7 2025/09/13 16:16:40 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.6Sthorpejstruct bmx280_spi_softc {
461.6Sthorpej	struct bmx280_sc	sc_bmx280;
471.6Sthorpej	spi_handle_t		sc_sh;
481.6Sthorpej};
491.6Sthorpej
501.6Sthorpej#define	BMX280_TO_SPI(sc)	\
511.6Sthorpej	container_of((sc), struct bmx280_spi_softc, sc_bmx280)
521.6Sthorpej
531.1Sbradstatic int 	bmx280thpspi_match(device_t, cfdata_t, void *);
541.1Sbradstatic void 	bmx280thpspi_attach(device_t, device_t, void *);
551.1Sbradstatic int 	bmx280thpspi_detach(device_t, int);
561.1Sbrad
571.6SthorpejCFATTACH_DECL_NEW(bmx280thpspi, sizeof(struct bmx280_spi_softc),
581.1Sbrad    bmx280thpspi_match, bmx280thpspi_attach, bmx280thpspi_detach, NULL);
591.1Sbrad
601.1Sbrad/* The SPI interface of the chip, assuming that it has managed to get into that
611.1Sbrad * mode to start with, is pretty simple.  Simply send the register MINUS the 7th
621.1Sbrad * bit which will be 1 and then do as many reads as you want.  The chip will
631.1Sbrad * auto increment for you.
641.1Sbrad *
651.1Sbrad * The delays are only hinted at in the data sheet.
661.1Sbrad */
671.1Sbrad
681.1Sbradstatic int
691.6Sthorpejbmx280thpspi_read_reg_direct(spi_handle_t sh, uint8_t reg, uint8_t *buf,
701.6Sthorpej    size_t rlen)
711.1Sbrad{
721.1Sbrad	int err = 0;
731.1Sbrad	uint8_t rreg = reg | 0x80;
741.1Sbrad
751.1Sbrad	if (buf != NULL) {
761.1Sbrad		err = spi_send_recv(sh, 1, &rreg,
771.1Sbrad		    rlen, buf);
781.1Sbrad	} else {
791.1Sbrad		err = spi_send(sh, 1, &rreg);
801.1Sbrad	}
811.1Sbrad
821.1Sbrad	return err;
831.1Sbrad}
841.1Sbrad
851.1Sbradstatic int
861.6Sthorpejbmx280thpspi_read_reg(struct bmx280_sc *sc, uint8_t reg, uint8_t *buf,
871.6Sthorpej    size_t rlen)
881.1Sbrad{
891.6Sthorpej	struct bmx280_spi_softc *ssc = BMX280_TO_SPI(sc);
901.6Sthorpej
911.6Sthorpej	return bmx280thpspi_read_reg_direct(ssc->sc_sh, reg, buf, rlen);
921.1Sbrad}
931.1Sbrad
941.1Sbrad/* SPI writes to this device are normal enough.  You send the register
951.1Sbrad * you want making sure that the high bit, 0x80, is clear and then the
961.1Sbrad * data.  These pairs can be repeated as many times as you like.
971.1Sbrad */
981.1Sbradstatic int
991.5Sthorpejbmx280thpspi_write_reg_direct(spi_handle_t sh, uint8_t *buf, size_t slen)
1001.1Sbrad{
1011.1Sbrad	int err = 0;
1021.1Sbrad	int i;
1031.1Sbrad
1041.1Sbrad	/* XXX -
1051.1Sbrad	   this is probably  BAD thing to do... but we must insure that the
1061.1Sbrad	   registers have a cleared bit.. otherwise it is a read ....
1071.1Sbrad	*/
1081.1Sbrad
1091.1Sbrad	for(i = 0; i < slen;i+=2) {
1101.1Sbrad		buf[i] = buf[i] & 0x7F;
1111.1Sbrad	}
1121.1Sbrad
1131.1Sbrad	err = spi_send(sh, slen, buf);
1141.1Sbrad
1151.1Sbrad	return err;
1161.1Sbrad}
1171.1Sbrad
1181.1Sbradstatic int
1191.1Sbradbmx280thpspi_write_reg(struct bmx280_sc *sc, uint8_t *buf, size_t slen)
1201.1Sbrad{
1211.6Sthorpej	struct bmx280_spi_softc *ssc = BMX280_TO_SPI(sc);
1221.6Sthorpej
1231.6Sthorpej	return bmx280thpspi_write_reg_direct(ssc->sc_sh, buf, slen);
1241.1Sbrad}
1251.1Sbrad
1261.1Sbrad/* These are to satisfy the common code */
1271.1Sbradstatic int
1281.6Sthorpejbmx280thpspi_acquire_bus(struct bmx280_sc *sc __unused)
1291.1Sbrad{
1301.1Sbrad	return 0;
1311.1Sbrad}
1321.1Sbrad
1331.1Sbradstatic void
1341.6Sthorpejbmx280thpspi_release_bus(struct bmx280_sc *sc __unused)
1351.1Sbrad{
1361.1Sbrad	return;
1371.1Sbrad}
1381.1Sbrad
1391.6Sthorpejstatic const struct bmx280_accessfuncs bmx280_spi_accessfuncs = {
1401.6Sthorpej	.acquire_bus	=	bmx280thpspi_acquire_bus,
1411.6Sthorpej	.release_bus	=	bmx280thpspi_release_bus,
1421.6Sthorpej	.read_reg	=	bmx280thpspi_read_reg,
1431.6Sthorpej	.write_reg	=	bmx280thpspi_write_reg,
1441.6Sthorpej};
1451.6Sthorpej
1461.1Sbrad/* Nothing more is done here.  Assumptions on whether or not
1471.1Sbrad * the SPI interface is set up may not be proper.... for better
1481.1Sbrad * or worse... and there is setting that are needed such as the
1491.1Sbrad * SPI mode and bus speed that really should not be done here, so
1501.1Sbrad * any active match might not work anyway.
1511.1Sbrad */
1521.1Sbradstatic int
1531.1Sbradbmx280thpspi_match(device_t parent, cfdata_t match, void *aux)
1541.1Sbrad{
1551.4Sthorpej	struct spi_attach_args *sa = aux;
1561.4Sthorpej	int match_result;
1571.1Sbrad
1581.7Sthorpej	if (spi_use_direct_match(sa, bmx280_compat_data, &match_result)) {
1591.4Sthorpej		return match_result;
1601.1Sbrad	}
1611.1Sbrad
1621.3Sthorpej	return SPI_MATCH_DEFAULT;
1631.1Sbrad}
1641.1Sbrad
1651.1Sbradstatic void
1661.1Sbradbmx280thpspi_attach(device_t parent, device_t self, void *aux)
1671.1Sbrad{
1681.6Sthorpej	struct bmx280_spi_softc *ssc = device_private(self);
1691.6Sthorpej	struct bmx280_sc *sc = &ssc->sc_bmx280;
1701.6Sthorpej	struct spi_attach_args *sa = aux;
1711.1Sbrad	int error;
1721.1Sbrad
1731.6Sthorpej	sc->sc_dev = self;
1741.6Sthorpej	sc->sc_funcs = &bmx280_spi_accessfuncs;
1751.6Sthorpej
1761.6Sthorpej	ssc->sc_sh = sa->sa_handle;
1771.1Sbrad
1781.1Sbrad	/* Configure for 1MHz and SPI mode 0 according to the data sheet.
1791.1Sbrad	 * The chip will actually handle a number of different modes and
1801.1Sbrad	 * can go a lot faster, just use this for now...
1811.1Sbrad	 */
1821.2Sthorpej	error = spi_configure(self, sa->sa_handle, SPI_MODE_0, SPI_FREQ_MHz(1));
1831.1Sbrad	if (error) {
1841.1Sbrad		return;
1851.1Sbrad	}
1861.1Sbrad
1871.1Sbrad	/* Please note that if the pins are not set up for SPI, the attachment
1881.1Sbrad	 * will probably not work out.
1891.1Sbrad	 */
1901.1Sbrad	bmx280_attach(sc);
1911.1Sbrad}
1921.1Sbrad
1931.1Sbradstatic int
1941.1Sbradbmx280thpspi_detach(device_t self, int flags)
1951.1Sbrad{
1961.7Sthorpej	struct bmx280_spi_softc *ssc = device_private(self);
1971.1Sbrad
1981.7Sthorpej	return bmx280_detach(&ssc->sc_bmx280, flags);
1991.1Sbrad}
200