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