tmp121.c revision 1.1.4.2 1 1.1.4.2 ad /* $NetBSD: tmp121.c,v 1.1.4.2 2006/11/18 21:34:50 ad Exp $ */
2 1.1.4.2 ad
3 1.1.4.2 ad /*-
4 1.1.4.2 ad * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 1.1.4.2 ad * Copyright (c) 2006 Garrett D'Amore.
6 1.1.4.2 ad * All rights reserved.
7 1.1.4.2 ad *
8 1.1.4.2 ad * Portions of this code were written by Garrett D'Amore for the
9 1.1.4.2 ad * Champaign-Urbana Community Wireless Network Project.
10 1.1.4.2 ad *
11 1.1.4.2 ad * Redistribution and use in source and binary forms, with or
12 1.1.4.2 ad * without modification, are permitted provided that the following
13 1.1.4.2 ad * conditions are met:
14 1.1.4.2 ad * 1. Redistributions of source code must retain the above copyright
15 1.1.4.2 ad * notice, this list of conditions and the following disclaimer.
16 1.1.4.2 ad * 2. Redistributions in binary form must reproduce the above
17 1.1.4.2 ad * copyright notice, this list of conditions and the following
18 1.1.4.2 ad * disclaimer in the documentation and/or other materials provided
19 1.1.4.2 ad * with the distribution.
20 1.1.4.2 ad * 3. All advertising materials mentioning features or use of this
21 1.1.4.2 ad * software must display the following acknowledgements:
22 1.1.4.2 ad * This product includes software developed by the Urbana-Champaign
23 1.1.4.2 ad * Independent Media Center.
24 1.1.4.2 ad * This product includes software developed by Garrett D'Amore.
25 1.1.4.2 ad * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 1.1.4.2 ad * D'Amore's name may not be used to endorse or promote products
27 1.1.4.2 ad * derived from this software without specific prior written permission.
28 1.1.4.2 ad *
29 1.1.4.2 ad * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 1.1.4.2 ad * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 1.1.4.2 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 1.1.4.2 ad * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1.4.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 1.1.4.2 ad * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.1.4.2 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.1.4.2 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 1.1.4.2 ad * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 1.1.4.2 ad * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1.4.2 ad * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.1.4.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 1.1.4.2 ad * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 1.1.4.2 ad */
43 1.1.4.2 ad
44 1.1.4.2 ad #include <sys/cdefs.h>
45 1.1.4.2 ad __KERNEL_RCSID(0, "$NetBSD: tmp121.c,v 1.1.4.2 2006/11/18 21:34:50 ad Exp $");
46 1.1.4.2 ad
47 1.1.4.2 ad #include <sys/param.h>
48 1.1.4.2 ad #include <sys/systm.h>
49 1.1.4.2 ad #include <sys/device.h>
50 1.1.4.2 ad #include <sys/kernel.h>
51 1.1.4.2 ad
52 1.1.4.2 ad #include <dev/sysmon/sysmonvar.h>
53 1.1.4.2 ad
54 1.1.4.2 ad #include <dev/spi/spivar.h>
55 1.1.4.2 ad
56 1.1.4.2 ad struct tmp121temp_softc {
57 1.1.4.2 ad struct device sc_dev;
58 1.1.4.2 ad
59 1.1.4.2 ad struct spi_handle *sc_sh;
60 1.1.4.2 ad
61 1.1.4.2 ad struct envsys_tre_data sc_sensor[1];
62 1.1.4.2 ad struct envsys_basic_info sc_info[1];
63 1.1.4.2 ad
64 1.1.4.2 ad struct sysmon_envsys sc_sysmon;
65 1.1.4.2 ad };
66 1.1.4.2 ad
67 1.1.4.2 ad static int tmp121temp_match(struct device *, struct cfdata *, void *);
68 1.1.4.2 ad static void tmp121temp_attach(struct device *, struct device *, void *);
69 1.1.4.2 ad
70 1.1.4.2 ad static int tmp121temp_gtredata(struct sysmon_envsys *,
71 1.1.4.2 ad struct envsys_tre_data *);
72 1.1.4.2 ad static int tmp121temp_streinfo(struct sysmon_envsys *,
73 1.1.4.2 ad struct envsys_basic_info *);
74 1.1.4.2 ad
75 1.1.4.2 ad CFATTACH_DECL(tmp121temp, sizeof(struct tmp121temp_softc),
76 1.1.4.2 ad tmp121temp_match, tmp121temp_attach, NULL, NULL);
77 1.1.4.2 ad
78 1.1.4.2 ad
79 1.1.4.2 ad static const struct envsys_range tmp121temp_ranges[] = {
80 1.1.4.2 ad /* this looks suspect to me, manual says ranges are inclusive */
81 1.1.4.2 ad { 0, 1, ENVSYS_STEMP },
82 1.1.4.2 ad { 1, 0, -1 },
83 1.1.4.2 ad };
84 1.1.4.2 ad
85 1.1.4.2 ad static int
86 1.1.4.2 ad tmp121temp_match(struct device *parent, struct cfdata *cf, void *aux)
87 1.1.4.2 ad {
88 1.1.4.2 ad struct spi_attach_args *sa = aux;
89 1.1.4.2 ad
90 1.1.4.2 ad /* configure for 10MHz */
91 1.1.4.2 ad if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
92 1.1.4.2 ad return 0;
93 1.1.4.2 ad
94 1.1.4.2 ad return 1;
95 1.1.4.2 ad }
96 1.1.4.2 ad
97 1.1.4.2 ad static void
98 1.1.4.2 ad tmp121temp_attach(struct device *parent, struct device *self, void *aux)
99 1.1.4.2 ad {
100 1.1.4.2 ad struct tmp121temp_softc *sc = device_private(self);
101 1.1.4.2 ad struct spi_attach_args *sa = aux;
102 1.1.4.2 ad prop_string_t desc;
103 1.1.4.2 ad
104 1.1.4.2 ad aprint_naive(": Temperature Sensor\n");
105 1.1.4.2 ad aprint_normal(": TI TMP121 Temperature Sensor\n");
106 1.1.4.2 ad
107 1.1.4.2 ad sc->sc_sh = sa->sa_handle;
108 1.1.4.2 ad
109 1.1.4.2 ad sc->sc_info[0].sensor = 0;
110 1.1.4.2 ad sc->sc_info[0].validflags = ENVSYS_FVALID;
111 1.1.4.2 ad
112 1.1.4.2 ad sc->sc_sensor[0].sensor = 0;
113 1.1.4.2 ad sc->sc_sensor[0].validflags = ENVSYS_FVALID;
114 1.1.4.2 ad sc->sc_sensor[0].warnflags = ENVSYS_WARN_OK;
115 1.1.4.2 ad sc->sc_sensor[0].units = sc->sc_info[0].units = ENVSYS_STEMP;
116 1.1.4.2 ad
117 1.1.4.2 ad /*
118 1.1.4.2 ad * set up the description, which we allow MD code to override in
119 1.1.4.2 ad * a property.
120 1.1.4.2 ad */
121 1.1.4.2 ad desc = prop_dictionary_get(device_properties(self),
122 1.1.4.2 ad "envsys-description");
123 1.1.4.2 ad if (desc != NULL &&
124 1.1.4.2 ad prop_object_type(desc) == PROP_TYPE_STRING &&
125 1.1.4.2 ad prop_string_size(desc) > 0)
126 1.1.4.2 ad strcpy(sc->sc_info[0].desc, prop_string_cstring_nocopy(desc));
127 1.1.4.2 ad else
128 1.1.4.2 ad strcpy(sc->sc_info[0].desc, device_xname(self));
129 1.1.4.2 ad
130 1.1.4.2 ad
131 1.1.4.2 ad sc->sc_sysmon.sme_ranges = tmp121temp_ranges;
132 1.1.4.2 ad sc->sc_sysmon.sme_sensor_info = sc->sc_info;
133 1.1.4.2 ad sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
134 1.1.4.2 ad sc->sc_sysmon.sme_gtredata = tmp121temp_gtredata;
135 1.1.4.2 ad sc->sc_sysmon.sme_streinfo = tmp121temp_streinfo;
136 1.1.4.2 ad sc->sc_sysmon.sme_cookie = sc;
137 1.1.4.2 ad sc->sc_sysmon.sme_nsensors = 1;
138 1.1.4.2 ad sc->sc_sysmon.sme_envsys_version = 1000;
139 1.1.4.2 ad
140 1.1.4.2 ad if (sysmon_envsys_register(&sc->sc_sysmon))
141 1.1.4.2 ad aprint_error("%s: unable to register with sysmon\n",
142 1.1.4.2 ad device_xname(self));
143 1.1.4.2 ad
144 1.1.4.2 ad }
145 1.1.4.2 ad
146 1.1.4.2 ad static void
147 1.1.4.2 ad tmp121temp_refresh(struct tmp121temp_softc *sc)
148 1.1.4.2 ad {
149 1.1.4.2 ad uint16_t reg;
150 1.1.4.2 ad int16_t sreg;
151 1.1.4.2 ad int val;
152 1.1.4.2 ad
153 1.1.4.2 ad if (spi_recv(sc->sc_sh, 2, (uint8_t *)®) != 0) {
154 1.1.4.2 ad sc->sc_sensor[0].validflags &= ~ENVSYS_FCURVALID;
155 1.1.4.2 ad return;
156 1.1.4.2 ad }
157 1.1.4.2 ad
158 1.1.4.2 ad sreg = (int16_t)be16toh(reg);
159 1.1.4.2 ad
160 1.1.4.2 ad /*
161 1.1.4.2 ad * convert to uK:
162 1.1.4.2 ad *
163 1.1.4.2 ad * TMP121 bits:
164 1.1.4.2 ad * D15 : sign bit
165 1.1.4.2 ad * D14-D3 : data (D14 is MSB)
166 1.1.4.2 ad * D2-D0 : zero
167 1.1.4.2 ad *
168 1.1.4.2 ad * The data is represented in units of 0.0625 deg C.
169 1.1.4.2 ad */
170 1.1.4.2 ad sreg >>= 3;
171 1.1.4.2 ad val = sreg * 62500 + 273150000;
172 1.1.4.2 ad
173 1.1.4.2 ad sc->sc_sensor[0].cur.data_us = val;
174 1.1.4.2 ad sc->sc_sensor[0].validflags |= ENVSYS_FCURVALID;
175 1.1.4.2 ad }
176 1.1.4.2 ad
177 1.1.4.2 ad static int
178 1.1.4.2 ad tmp121temp_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
179 1.1.4.2 ad {
180 1.1.4.2 ad struct tmp121temp_softc *sc = sme->sme_cookie;
181 1.1.4.2 ad
182 1.1.4.2 ad /*
183 1.1.4.2 ad * XXX: locking? possibly we could copy out bad sensor data.
184 1.1.4.2 ad * I'm not going to worry about it right now -- sysmon is probably
185 1.1.4.2 ad * single threaded anyway.
186 1.1.4.2 ad */
187 1.1.4.2 ad
188 1.1.4.2 ad tmp121temp_refresh(sc);
189 1.1.4.2 ad *tred = sc->sc_sensor[tred->sensor];
190 1.1.4.2 ad
191 1.1.4.2 ad return (0);
192 1.1.4.2 ad }
193 1.1.4.2 ad
194 1.1.4.2 ad static int
195 1.1.4.2 ad tmp121temp_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
196 1.1.4.2 ad {
197 1.1.4.2 ad struct tmp121temp_softc *sc = sme->sme_cookie;
198 1.1.4.2 ad
199 1.1.4.2 ad memcpy(sc->sc_info[binfo->sensor].desc, binfo->desc,
200 1.1.4.2 ad sizeof(sc->sc_info[binfo->sensor].desc));
201 1.1.4.2 ad sc->sc_info[binfo->sensor].desc[
202 1.1.4.2 ad sizeof(sc->sc_info[binfo->sensor].desc) - 1] = '\0';
203 1.1.4.2 ad
204 1.1.4.2 ad binfo->validflags = ENVSYS_FVALID;
205 1.1.4.2 ad
206 1.1.4.2 ad return (0);
207 1.1.4.2 ad }
208