jcs's openbsd hax
openbsd
at jcs 157 lines 4.3 kB view raw
1/* $OpenBSD: tmp451.c,v 1.2 2022/04/06 18:59:28 naddy Exp $ */ 2/* 3 * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18#include <sys/param.h> 19#include <sys/systm.h> 20#include <sys/device.h> 21#include <sys/sensors.h> 22 23#include <dev/i2c/i2cvar.h> 24 25/* TI TMP451 registers */ 26#define TMP451_LT_HI 0x00 27#define TMP451_RT_HI 0x01 28#define TMP451_RT_LO 0x10 29#define TMP451_RTOS_HI 0x11 30#define TMP451_RTOS_LO 0x12 31#define TMP451_LT_LO 0x15 32 33/* Sensors */ 34#define TITMP_LT 0 35#define TITMP_RT 1 36#define TITMP_NUM_SENSORS 2 37 38struct titmp_softc { 39 struct device sc_dev; 40 i2c_tag_t sc_tag; 41 i2c_addr_t sc_addr; 42 43 uint64_t sc_offset[TITMP_NUM_SENSORS]; 44 struct ksensor sc_sensor[TITMP_NUM_SENSORS]; 45 struct ksensordev sc_sensordev; 46}; 47 48int titmp_match(struct device *, void *, void *); 49void titmp_attach(struct device *, struct device *, void *); 50 51const struct cfattach titmp_ca = { 52 sizeof(struct titmp_softc), titmp_match, titmp_attach 53}; 54 55struct cfdriver titmp_cd = { 56 NULL, "titmp", DV_DULL 57}; 58 59void titmp_read_offsets(struct titmp_softc *); 60void titmp_refresh_sensors(void *); 61 62int 63titmp_match(struct device *parent, void *match, void *aux) 64{ 65 struct i2c_attach_args *ia = aux; 66 67 return (strcmp(ia->ia_name, "ti,tmp451") == 0); 68} 69 70void 71titmp_attach(struct device *parent, struct device *self, void *aux) 72{ 73 struct titmp_softc *sc = (struct titmp_softc *)self; 74 struct i2c_attach_args *ia = aux; 75 int i; 76 77 sc->sc_tag = ia->ia_tag; 78 sc->sc_addr = ia->ia_addr; 79 80 printf("\n"); 81 82 titmp_read_offsets(sc); 83 84 /* Register sensors. */ 85 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, 86 sizeof(sc->sc_sensordev.xname)); 87 sc->sc_sensor[TITMP_LT].type = SENSOR_TEMP; 88 strlcpy(sc->sc_sensor[TITMP_LT].desc, "Local", 89 sizeof(sc->sc_sensor[TITMP_LT].desc)); 90 sc->sc_sensor[TITMP_RT].type = SENSOR_TEMP; 91 strlcpy(sc->sc_sensor[TITMP_RT].desc, "Remote", 92 sizeof(sc->sc_sensor[TITMP_RT].desc)); 93 for (i = 0; i < TITMP_NUM_SENSORS; i++) 94 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]); 95 sensordev_install(&sc->sc_sensordev); 96 sensor_task_register(sc, titmp_refresh_sensors, 5); 97} 98 99void 100titmp_read_offsets(struct titmp_softc *sc) 101{ 102 uint8_t cmd, hi, lo; 103 int error; 104 105 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 106 107 cmd = TMP451_RTOS_HI; 108 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 109 &cmd, sizeof(cmd), &hi, sizeof(hi), I2C_F_POLL); 110 if (error) 111 goto fail; 112 cmd = TMP451_RTOS_LO; 113 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 114 &cmd, sizeof(cmd), &lo, sizeof(lo), I2C_F_POLL); 115 if (error) 116 goto fail; 117 118 sc->sc_offset[TITMP_RT] = 1000000 * hi + (lo >> 4) * 1000000 / 16; 119 120fail: 121 iic_release_bus(sc->sc_tag, I2C_F_POLL); 122} 123 124void 125titmp_read_sensor(struct titmp_softc *sc, uint8_t cmdhi, uint8_t cmdlo, 126 int index) 127{ 128 uint8_t hi, lo; 129 130 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, 131 sc->sc_addr, &cmdhi, sizeof(cmdhi), &hi, sizeof(hi), 0)) 132 goto invalid; 133 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, 134 sc->sc_addr, &cmdlo, sizeof(cmdlo), &lo, sizeof(lo), 0)) 135 goto invalid; 136 137 sc->sc_sensor[index].value = 273150000 + 138 1000000 * hi + (lo >> 4) * 1000000 / 16; 139 sc->sc_sensor[index].value += sc->sc_offset[index]; 140 return; 141 142invalid: 143 sc->sc_sensor[index].flags |= SENSOR_FINVALID; 144} 145 146void 147titmp_refresh_sensors(void *arg) 148{ 149 struct titmp_softc *sc = arg; 150 151 iic_acquire_bus(sc->sc_tag, 0); 152 153 titmp_read_sensor(sc, TMP451_LT_HI, TMP451_LT_LO, TITMP_LT); 154 titmp_read_sensor(sc, TMP451_RT_HI, TMP451_RT_LO, TITMP_RT); 155 156 iic_release_bus(sc->sc_tag, 0); 157}