Home | History | Annotate | Line # | Download | only in i2c
      1 /*	$NetBSD: mpl115a.c,v 1.5 2026/10/16 13:42:24 rkujawa Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Radoslaw Kujawa.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Freescale MPL115A2 miniature digital barometer driver.
     34  *
     35  * This driver could be split into bus-independent driver and I2C-specific
     36  * attachment, as SPI variant of this chip also exist.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: mpl115a.c,v 1.5 2026/10/16 13:42:24 rkujawa Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 #include <sys/kernel.h>
     46 #include <sys/mutex.h>
     47 
     48 #include <sys/bus.h>
     49 #include <dev/i2c/i2cvar.h>
     50 
     51 #include <dev/sysmon/sysmonvar.h>
     52 
     53 #include <dev/i2c/mpl115areg.h>
     54 
     55 struct mpl115a_softc {
     56 	device_t		sc_dev;
     57 
     58 	i2c_tag_t		sc_tag;
     59 	i2c_addr_t		sc_addr;
     60 
     61 	/* raw coefficients */
     62 	int16_t			sc_a0;
     63 	int16_t			sc_b1;
     64 	int16_t			sc_b2;
     65 	int16_t			sc_c12;
     66 
     67 	/* envsys(4) stuff */
     68 	struct sysmon_envsys	*sc_sme;
     69 	envsys_data_t		sc_sensor;
     70 	kmutex_t		sc_lock;
     71 };
     72 
     73 
     74 static int	mpl115a_match(device_t, cfdata_t, void *);
     75 static void	mpl115a_attach(device_t, device_t, void *);
     76 
     77 static uint8_t	mpl115a_reg_read_1(struct mpl115a_softc *sc, uint8_t);
     78 static void	mpl115a_reg_write_1(struct mpl115a_softc *sc, uint8_t, uint8_t);
     79 
     80 static void	mpl115a_load_coeffs(struct mpl115a_softc *sc);
     81 static uint16_t	mpl115a_make_coeff(uint8_t msb, uint8_t lsb);
     82 static uint32_t	mpl115a_pressure(struct mpl115a_softc *sc);
     83 static uint32_t mpl115a_calc(struct mpl115a_softc *sc, uint16_t padc, uint16_t tadc) ;
     84 
     85 static void mpl115a_envsys_register(struct mpl115a_softc *);
     86 static void mpl115a_envsys_refresh(struct sysmon_envsys *, envsys_data_t *);
     87 
     88 CFATTACH_DECL_NEW(mpl115a, sizeof (struct mpl115a_softc),
     89     mpl115a_match, mpl115a_attach, NULL, NULL);
     90 
     91 static int
     92 mpl115a_match(device_t parent, cfdata_t cf, void *aux)
     93 {
     94 	struct i2c_attach_args *ia = aux;
     95 
     96 	if (ia->ia_addr == MPL115A_ADDR)
     97 		return I2C_MATCH_ADDRESS_ONLY;
     98 	return 0;
     99 }
    100 
    101 static void
    102 mpl115a_attach(device_t parent, device_t self, void *aux)
    103 {
    104 	struct mpl115a_softc *sc = device_private(self);
    105 	struct i2c_attach_args *ia = aux;
    106 
    107 	sc->sc_dev = self;
    108 	sc->sc_addr = ia->ia_addr;
    109 	sc->sc_tag = ia->ia_tag;
    110 
    111 	aprint_normal(": Freescale MPL115A2 Pressure Sensor\n");
    112 
    113 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    114 
    115 	/* Since coefficients do not change load them once. */
    116 	mpl115a_load_coeffs(sc);
    117 
    118 	mpl115a_envsys_register(sc);
    119 }
    120 
    121 /* Construct a coefficients from MSB and LSB bytes. */
    122 static uint16_t
    123 mpl115a_make_coeff(uint8_t msb, uint8_t lsb)
    124 {
    125 	uint16_t rv;
    126 
    127 	rv = (msb << 8) | lsb;
    128 
    129 #ifdef MPL115A_DEBUG
    130 	aprint_normal("msb %x, lsb %x, ret val %x\n", msb, lsb, rv);
    131 #endif /* MPL115A_DEBUG */
    132 
    133 	return rv;
    134 }
    135 
    136 static void
    137 mpl115a_load_coeffs(struct mpl115a_softc *sc)
    138 {
    139 	sc->sc_a0 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_A0_MSB),
    140 	    mpl115a_reg_read_1(sc, MPL115A_A0_LSB));
    141 	sc->sc_b1 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_B1_MSB),
    142 	    mpl115a_reg_read_1(sc, MPL115A_B1_LSB));
    143 	sc->sc_b2 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_B2_MSB),
    144 	    mpl115a_reg_read_1(sc, MPL115A_B2_LSB));
    145 	sc->sc_c12 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_C12_MSB),
    146 	    mpl115a_reg_read_1(sc, MPL115A_C12_LSB));
    147 }
    148 
    149 static void
    150 mpl115a_reg_write_1(struct mpl115a_softc *sc, uint8_t reg, uint8_t val)
    151 {
    152 	if (iic_acquire_bus(sc->sc_tag, 0) != 0) {
    153 		aprint_error_dev(sc->sc_dev, "cannot acquire bus for write\n");
    154 		return;
    155 	}
    156 
    157 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, &reg, 1,
    158 	    &val, 1, 0)) {
    159 		aprint_error_dev(sc->sc_dev, "cannot execute write\n");
    160 	}
    161 	iic_release_bus(sc->sc_tag, 0);
    162 }
    163 
    164 static uint8_t
    165 mpl115a_reg_read_1(struct mpl115a_softc *sc, uint8_t reg)
    166 {
    167 	uint8_t rv, wbuf[2];
    168 
    169 	if (iic_acquire_bus(sc->sc_tag, 0) != 0) {
    170 #ifdef MPL115A_DEBUG
    171 		aprint_error_dev(sc->sc_dev, "cannot acquire bus for read\n");
    172 #endif /* MPL115A_DEBUG */
    173 		return 0;
    174 	}
    175 
    176 	wbuf[0] = reg;
    177 
    178 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, wbuf,
    179 	    1, &rv, 1, 0)) {
    180 		iic_release_bus(sc->sc_tag, 0);
    181 		aprint_error_dev(sc->sc_dev, "cannot execute read\n");
    182 		return 0;
    183 	}
    184 	iic_release_bus(sc->sc_tag, 0);
    185 
    186 	return rv;
    187 }
    188 
    189 
    190 
    191 /* Get pressure in pascals. */
    192 static uint32_t
    193 mpl115a_pressure(struct mpl115a_softc *sc)
    194 {
    195 	uint32_t rv;
    196 
    197 	uint16_t padc, tadc;
    198 
    199 	rv = 0;
    200 
    201 	mpl115a_reg_write_1(sc, MPL115A_CONVERT, 0);
    202 	delay(MPL115A_CONVERT_US);
    203 
    204 	padc = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_PADC_MSB),
    205 	    mpl115a_reg_read_1(sc, MPL115A_PADC_LSB));
    206 
    207 	tadc = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_TADC_MSB),
    208 	    mpl115a_reg_read_1(sc, MPL115A_TADC_LSB));
    209 
    210 #ifdef MPL115A_DEBUG
    211 	aprint_normal_dev(sc->sc_dev, "padc %x, tadc %x\n", padc, tadc);
    212 #endif /* MPL115A_DEBUG */
    213 
    214 	rv = mpl115a_calc(sc, padc, tadc);
    215 
    216 #ifdef MPL115A_DEBUG
    217 	aprint_normal_dev(sc->sc_dev, "%d Pa\n", rv);
    218 #endif /* MPL115A_DEBUG */
    219 
    220 	return rv;
    221 }
    222 
    223 /* The following calculation routine was suggested by Matt Thomas. */
    224 static uint32_t
    225 mpl115a_calc(struct mpl115a_softc *sc, uint16_t padc, uint16_t tadc)
    226 {
    227 	int32_t fp_a0, fp_b1, fp_b2, fp_c12, fp_padc, fp_tadc;
    228 	int64_t pre_kpa;
    229 	int32_t c12x2, a1, a1x2, y1, a2x2, pcomp;
    230 
    231 	/* convert coefficients to common fixed point format */
    232 	fp_a0 = sc->sc_a0 << 13;	/* 12.3 -> 15.16 */
    233 	fp_b1 = sc->sc_b1 << 3;		/* 2.13 -> 15.16 */
    234 	fp_b2 = sc->sc_b2 << 2;		/* 1.14 -> 15.16 */
    235 	fp_c12 = sc->sc_c12 >> 2;	/* 0.22 */
    236 
    237 	fp_padc = padc >> 6;
    238 	fp_tadc = tadc >> 6;
    239 
    240 	c12x2 = (fp_c12 * fp_tadc + 0x3f) >> 6;
    241 	a1 = fp_b1 + c12x2;
    242 	a1x2 = a1 * fp_padc;
    243 	y1 = fp_a0 + a1x2;
    244 	a2x2 = fp_b2 * fp_tadc;
    245 	pcomp = y1 + a2x2;
    246 
    247 	/* pre_kpa has 16 fractional digits so it's accurate to 1/65536 kPa */
    248 	pre_kpa = (int64_t)pcomp * (115 - 50) / 1023 + (50 << 16);
    249 
    250 	return (pre_kpa * 1000 + 32768) >> 16;
    251 }
    252 
    253 static void
    254 mpl115a_envsys_register(struct mpl115a_softc *sc)
    255 {
    256 	sc->sc_sme = sysmon_envsys_create();
    257 
    258 	strlcpy(sc->sc_sensor.desc, "Absolute pressure",
    259 	    sizeof(sc->sc_sensor.desc));
    260 	sc->sc_sensor.units = ENVSYS_PRESSURE;
    261 	sc->sc_sensor.state = ENVSYS_SINVALID;
    262 
    263 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    264 		aprint_error_dev(sc->sc_dev,
    265 		    "error attaching sensor\n");
    266 		return;
    267 	}
    268 
    269 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    270 	sc->sc_sme->sme_cookie = sc;
    271 	sc->sc_sme->sme_refresh = mpl115a_envsys_refresh;
    272 
    273 	if (sysmon_envsys_register(sc->sc_sme)) {
    274 		aprint_error_dev(sc->sc_dev, "unable to register in sysmon\n");
    275 		sysmon_envsys_destroy(sc->sc_sme);
    276 	}
    277 }
    278 
    279 static void
    280 mpl115a_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    281 {
    282 	struct mpl115a_softc *sc = sme->sme_cookie;
    283 
    284 	mutex_enter(&sc->sc_lock);
    285 
    286 	edata->value_cur = mpl115a_pressure(sc) * 100;
    287 	edata->state = ENVSYS_SVALID;
    288 
    289 	mutex_exit(&sc->sc_lock);
    290 }
    291 
    292