titemp.c revision 1.3.14.2 1 1.3.14.2 jdolecek /* $NetBSD: titemp.c,v 1.3.14.2 2017/12/03 11:37:02 jdolecek Exp $ */
2 1.3.14.2 jdolecek
3 1.3.14.2 jdolecek /*-
4 1.3.14.2 jdolecek * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.3.14.2 jdolecek * All rights reserved.
6 1.3.14.2 jdolecek *
7 1.3.14.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.3.14.2 jdolecek * modification, are permitted provided that the following conditions
9 1.3.14.2 jdolecek * are met:
10 1.3.14.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.3.14.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.3.14.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.14.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.3.14.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.3.14.2 jdolecek *
16 1.3.14.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.3.14.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.3.14.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.3.14.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.3.14.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.3.14.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.3.14.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.3.14.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.3.14.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.3.14.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.3.14.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
27 1.3.14.2 jdolecek */
28 1.3.14.2 jdolecek
29 1.3.14.2 jdolecek #include <sys/cdefs.h>
30 1.3.14.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: titemp.c,v 1.3.14.2 2017/12/03 11:37:02 jdolecek Exp $");
31 1.3.14.2 jdolecek
32 1.3.14.2 jdolecek #include <sys/param.h>
33 1.3.14.2 jdolecek #include <sys/systm.h>
34 1.3.14.2 jdolecek #include <sys/device.h>
35 1.3.14.2 jdolecek #include <sys/conf.h>
36 1.3.14.2 jdolecek #include <sys/bus.h>
37 1.3.14.2 jdolecek #include <sys/kmem.h>
38 1.3.14.2 jdolecek
39 1.3.14.2 jdolecek #include <dev/i2c/i2cvar.h>
40 1.3.14.2 jdolecek
41 1.3.14.2 jdolecek #include <dev/sysmon/sysmonvar.h>
42 1.3.14.2 jdolecek
43 1.3.14.2 jdolecek #define TITEMP_LTEMP_HI_REG 0x00
44 1.3.14.2 jdolecek #define TITEMP_RTEMP_HI_REG 0x01
45 1.3.14.2 jdolecek #define TITEMP_STATUS_REG 0x02
46 1.3.14.2 jdolecek #define TITEMP_CONFIG_REG 0x03
47 1.3.14.2 jdolecek #define TITEMP_CONVRATE_REG 0x04
48 1.3.14.2 jdolecek #define TITEMP_LTEMP_HLIMIT_HI_REG 0x05
49 1.3.14.2 jdolecek #define TITEMP_LTEMP_LLIMIT_HI_REG 0x06
50 1.3.14.2 jdolecek #define TITEMP_RTEMP_HLIMIT_HI_REG 0x07
51 1.3.14.2 jdolecek #define TITEMP_RTEMP_LLIMIT_HI_REG 0x08
52 1.3.14.2 jdolecek #define TITEMP_RTEMP_LO_REG 0x10
53 1.3.14.2 jdolecek #define TITEMP_RTEMP_OFF_HI_REG 0x11
54 1.3.14.2 jdolecek #define TITEMP_RTEMP_OFF_LO_REG 0x12
55 1.3.14.2 jdolecek #define TITEMP_RTEMP_HLIMIT_LO_REG 0x13
56 1.3.14.2 jdolecek #define TITEMP_RTEMP_LLIMIT_LO_REG 0x14
57 1.3.14.2 jdolecek #define TITEMP_LTEMP_LO_REG 0x15
58 1.3.14.2 jdolecek #define TITEMP_RTEMP_THERM_LIMIT_REG 0x19
59 1.3.14.2 jdolecek #define TITEMP_LTEMP_THERM_LIMIT_REG 0x20
60 1.3.14.2 jdolecek #define TITEMP_THERM_HYST_REG 0x21
61 1.3.14.2 jdolecek #define TITEMP_CONAL_REG 0x22
62 1.3.14.2 jdolecek #define TITEMP_NC_REG 0x23
63 1.3.14.2 jdolecek #define TITEMP_DF_REG 0x24
64 1.3.14.2 jdolecek #define TITEMP_MFID_REG 0xfe
65 1.3.14.2 jdolecek
66 1.3.14.2 jdolecek #define TITEMP_MFID_TMP451 0x55
67 1.3.14.2 jdolecek
68 1.3.14.2 jdolecek struct titemp_softc {
69 1.3.14.2 jdolecek device_t sc_dev;
70 1.3.14.2 jdolecek i2c_tag_t sc_i2c;
71 1.3.14.2 jdolecek i2c_addr_t sc_addr;
72 1.3.14.2 jdolecek
73 1.3.14.2 jdolecek struct sysmon_envsys *sc_sme;
74 1.3.14.2 jdolecek envsys_data_t sc_sensor_ltemp;
75 1.3.14.2 jdolecek envsys_data_t sc_sensor_rtemp;
76 1.3.14.2 jdolecek };
77 1.3.14.2 jdolecek
78 1.3.14.2 jdolecek static int titemp_match(device_t, cfdata_t, void *);
79 1.3.14.2 jdolecek static void titemp_attach(device_t, device_t, void *);
80 1.3.14.2 jdolecek
81 1.3.14.2 jdolecek static void titemp_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
82 1.3.14.2 jdolecek static int titemp_read(struct titemp_softc *, uint8_t, uint8_t *);
83 1.3.14.2 jdolecek
84 1.3.14.2 jdolecek CFATTACH_DECL_NEW(titemp, sizeof(struct titemp_softc),
85 1.3.14.2 jdolecek titemp_match, titemp_attach, NULL, NULL);
86 1.3.14.2 jdolecek
87 1.3.14.2 jdolecek static const char * titemp_compats[] = {
88 1.3.14.2 jdolecek "ti,tmp451",
89 1.3.14.2 jdolecek NULL
90 1.3.14.2 jdolecek };
91 1.3.14.2 jdolecek
92 1.3.14.2 jdolecek static int
93 1.3.14.2 jdolecek titemp_match(device_t parent, cfdata_t match, void *aux)
94 1.3.14.2 jdolecek {
95 1.3.14.2 jdolecek struct i2c_attach_args *ia = aux;
96 1.3.14.2 jdolecek uint8_t mfid;
97 1.3.14.2 jdolecek int error;
98 1.3.14.2 jdolecek
99 1.3.14.2 jdolecek if (ia->ia_name == NULL) {
100 1.3.14.2 jdolecek if (iic_acquire_bus(ia->ia_tag, I2C_F_POLL) != 0)
101 1.3.14.2 jdolecek return 0;
102 1.3.14.2 jdolecek error = iic_smbus_read_byte(ia->ia_tag, ia->ia_addr,
103 1.3.14.2 jdolecek TITEMP_MFID_REG, &mfid, I2C_F_POLL);
104 1.3.14.2 jdolecek iic_release_bus(ia->ia_tag, I2C_F_POLL);
105 1.3.14.2 jdolecek
106 1.3.14.2 jdolecek if (error || mfid != TITEMP_MFID_TMP451)
107 1.3.14.2 jdolecek return 0;
108 1.3.14.2 jdolecek
109 1.3.14.2 jdolecek return 1;
110 1.3.14.2 jdolecek } else {
111 1.3.14.2 jdolecek return iic_compat_match(ia, titemp_compats);
112 1.3.14.2 jdolecek }
113 1.3.14.2 jdolecek }
114 1.3.14.2 jdolecek
115 1.3.14.2 jdolecek static void
116 1.3.14.2 jdolecek titemp_attach(device_t parent, device_t self, void *aux)
117 1.3.14.2 jdolecek {
118 1.3.14.2 jdolecek struct titemp_softc *sc = device_private(self);
119 1.3.14.2 jdolecek struct i2c_attach_args *ia = aux;
120 1.3.14.2 jdolecek
121 1.3.14.2 jdolecek sc->sc_dev = self;
122 1.3.14.2 jdolecek sc->sc_i2c = ia->ia_tag;
123 1.3.14.2 jdolecek sc->sc_addr = ia->ia_addr;
124 1.3.14.2 jdolecek
125 1.3.14.2 jdolecek aprint_naive("\n");
126 1.3.14.2 jdolecek aprint_normal(": TMP451\n");
127 1.3.14.2 jdolecek
128 1.3.14.2 jdolecek sc->sc_sme = sysmon_envsys_create();
129 1.3.14.2 jdolecek sc->sc_sme->sme_name = device_xname(self);
130 1.3.14.2 jdolecek sc->sc_sme->sme_cookie = sc;
131 1.3.14.2 jdolecek sc->sc_sme->sme_refresh = titemp_sensors_refresh;
132 1.3.14.2 jdolecek
133 1.3.14.2 jdolecek sc->sc_sensor_ltemp.units = ENVSYS_STEMP;
134 1.3.14.2 jdolecek sc->sc_sensor_ltemp.state = ENVSYS_SINVALID;
135 1.3.14.2 jdolecek snprintf(sc->sc_sensor_ltemp.desc, sizeof(sc->sc_sensor_ltemp.desc),
136 1.3.14.2 jdolecek "local temperature");
137 1.3.14.2 jdolecek sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_ltemp);
138 1.3.14.2 jdolecek
139 1.3.14.2 jdolecek sc->sc_sensor_rtemp.units = ENVSYS_STEMP;
140 1.3.14.2 jdolecek sc->sc_sensor_rtemp.state = ENVSYS_SINVALID;
141 1.3.14.2 jdolecek snprintf(sc->sc_sensor_rtemp.desc, sizeof(sc->sc_sensor_rtemp.desc),
142 1.3.14.2 jdolecek "remote temperature");
143 1.3.14.2 jdolecek sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_rtemp);
144 1.3.14.2 jdolecek
145 1.3.14.2 jdolecek sysmon_envsys_register(sc->sc_sme);
146 1.3.14.2 jdolecek }
147 1.3.14.2 jdolecek
148 1.3.14.2 jdolecek static void
149 1.3.14.2 jdolecek titemp_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
150 1.3.14.2 jdolecek {
151 1.3.14.2 jdolecek struct titemp_softc *sc = sme->sme_cookie;
152 1.3.14.2 jdolecek uint8_t reg_hi, reg_lo, temp[2];
153 1.3.14.2 jdolecek int error;
154 1.3.14.2 jdolecek
155 1.3.14.2 jdolecek if (edata == &sc->sc_sensor_ltemp) {
156 1.3.14.2 jdolecek reg_hi = TITEMP_LTEMP_HI_REG;
157 1.3.14.2 jdolecek reg_lo = TITEMP_LTEMP_LO_REG;
158 1.3.14.2 jdolecek } else if (edata == &sc->sc_sensor_rtemp) {
159 1.3.14.2 jdolecek reg_hi = TITEMP_RTEMP_HI_REG;
160 1.3.14.2 jdolecek reg_lo = TITEMP_RTEMP_LO_REG;
161 1.3.14.2 jdolecek } else {
162 1.3.14.2 jdolecek edata->state = ENVSYS_SINVALID;
163 1.3.14.2 jdolecek return;
164 1.3.14.2 jdolecek }
165 1.3.14.2 jdolecek
166 1.3.14.2 jdolecek iic_acquire_bus(sc->sc_i2c, 0);
167 1.3.14.2 jdolecek if ((error = titemp_read(sc, reg_hi, &temp[0])) != 0)
168 1.3.14.2 jdolecek goto done;
169 1.3.14.2 jdolecek if ((error = titemp_read(sc, reg_lo, &temp[1])) != 0)
170 1.3.14.2 jdolecek goto done;
171 1.3.14.2 jdolecek done:
172 1.3.14.2 jdolecek iic_release_bus(sc->sc_i2c, 0);
173 1.3.14.2 jdolecek
174 1.3.14.2 jdolecek if (error) {
175 1.3.14.2 jdolecek edata->state = ENVSYS_SINVALID;
176 1.3.14.2 jdolecek } else {
177 1.3.14.2 jdolecek edata->value_cur =
178 1.3.14.2 jdolecek ((uint64_t)temp[0] * 1000000) +
179 1.3.14.2 jdolecek ((uint64_t)(temp[1]>>4) * 62500) +
180 1.3.14.2 jdolecek + 273150000;
181 1.3.14.2 jdolecek edata->state = ENVSYS_SVALID;
182 1.3.14.2 jdolecek }
183 1.3.14.2 jdolecek }
184 1.3.14.2 jdolecek
185 1.3.14.2 jdolecek static int
186 1.3.14.2 jdolecek titemp_read(struct titemp_softc *sc, uint8_t reg, uint8_t *val)
187 1.3.14.2 jdolecek {
188 1.3.14.2 jdolecek return iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
189 1.3.14.2 jdolecek }
190