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