1 /*-
2 * Copyright (c) 2010 Andreas Tobler
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/callout.h>
35 #include <sys/conf.h>
36 #include <sys/cpu.h>
37 #include <sys/ctype.h>
38 #include <sys/kernel.h>
39 #include <sys/reboot.h>
40 #include <sys/rman.h>
41 #include <sys/sysctl.h>
42 #include <sys/limits.h>
43
44 #include <machine/bus.h>
45 #include <machine/md_var.h>
46
47 #include <dev/iicbus/iicbus.h>
48 #include <dev/iicbus/iiconf.h>
49
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <powerpc/powermac/powermac_thermal.h>
53
54 /* Drivebay sensor: LM75/DS1775. */
55 #define DS1775_TEMP 0x0
56
57 /* Regular bus attachment functions */
58 static int ds1775_probe(device_t);
59 static int ds1775_attach(device_t);
60
61 struct ds1775_softc {
62 struct pmac_therm sc_sensor;
63 device_t sc_dev;
64 struct intr_config_hook enum_hook;
65 uint32_t sc_addr;
66 };
67
68 /* Utility functions */
69 static int ds1775_sensor_read(struct ds1775_softc *sc);
70 static int ds1775_sensor_sysctl(SYSCTL_HANDLER_ARGS);
71 static void ds1775_start(void *xdev);
72 static int ds1775_read_2(device_t dev, uint32_t addr, uint8_t reg,
73 uint16_t *data);
74
75 static device_method_t ds1775_methods[] = {
76 /* Device interface */
77 DEVMETHOD(device_probe, ds1775_probe),
78 DEVMETHOD(device_attach, ds1775_attach),
79 { 0, 0 },
80 };
81
82 static driver_t ds1775_driver = {
83 "ds1775",
84 ds1775_methods,
85 sizeof(struct ds1775_softc)
86 };
87
88 static devclass_t ds1775_devclass;
89
90 DRIVER_MODULE(ds1755, iicbus, ds1775_driver, ds1775_devclass, 0, 0);
91
92 static int
93 ds1775_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
94 {
95 uint8_t buf[4];
96 int err, try = 0;
97
98 struct iic_msg msg[2] = {
99 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
100 { addr, IIC_M_RD, 2, buf },
101 };
102
103 for (;;)
104 {
105 err = iicbus_transfer(dev, msg, 2);
106 if (err != 0)
107 goto retry;
108
109 *data = *((uint16_t*)buf);
110 return (0);
111 retry:
112 if (++try > 5) {
113 device_printf(dev, "iicbus read failed\n");
114 return (-1);
115 }
116 pause("ds1775_read_2", hz);
117 }
118 }
119
120 static int
121 ds1775_probe(device_t dev)
122 {
123 const char *name, *compatible;
124 struct ds1775_softc *sc;
125
126 name = ofw_bus_get_name(dev);
127 compatible = ofw_bus_get_compat(dev);
128
129 if (!name)
130 return (ENXIO);
131
132 if (strcmp(name, "temp-monitor") != 0 ||
133 (strcmp(compatible, "ds1775") != 0 &&
134 strcmp(compatible, "lm75") != 0))
135 return (ENXIO);
136
137 sc = device_get_softc(dev);
138 sc->sc_dev = dev;
139 sc->sc_addr = iicbus_get_addr(dev);
140
141 device_set_desc(dev, "Temp-Monitor DS1775");
142
143 return (0);
144 }
145
146 static int
147 ds1775_attach(device_t dev)
148 {
149 struct ds1775_softc *sc;
150
151 sc = device_get_softc(dev);
152
153 sc->enum_hook.ich_func = ds1775_start;
154 sc->enum_hook.ich_arg = dev;
155
156 /* We have to wait until interrupts are enabled. I2C read and write
157 * only works if the interrupts are available.
158 * The unin/i2c is controlled by the htpic on unin. But this is not
159 * the master. The openpic on mac-io is controlling the htpic.
160 * This one gets attached after the mac-io probing and then the
161 * interrupts will be available.
162 */
163
164 if (config_intrhook_establish(&sc->enum_hook) != 0)
165 return (ENOMEM);
166
167 return (0);
168 }
169
170 static void
171 ds1775_start(void *xdev)
172 {
173 phandle_t child;
174 struct ds1775_softc *sc;
175 struct sysctl_oid *sensroot_oid;
176 struct sysctl_ctx_list *ctx;
177 ssize_t plen;
178 int i;
179 char sysctl_name[40], sysctl_desc[40];
180 const char *units;
181
182 device_t dev = (device_t)xdev;
183
184 sc = device_get_softc(dev);
185
186 child = ofw_bus_get_node(dev);
187
188 ctx = device_get_sysctl_ctx(dev);
189 sensroot_oid = device_get_sysctl_tree(dev);
190
191 if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone,
192 sizeof(int)) < 0)
193 sc->sc_sensor.zone = 0;
194
195 plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
196 sizeof(sc->sc_sensor.name));
197 units = "C";
198
199 if (plen == -1) {
200 strcpy(sysctl_name, "sensor");
201 } else {
202 for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
203 sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
204 if (isspace(sysctl_name[i]))
205 sysctl_name[i] = '_';
206 }
207 sysctl_name[i] = 0;
208 }
209
210 /* Make up target temperatures. These are low, for the drive bay. */
211 if (sc->sc_sensor.zone == 0) {
212 sc->sc_sensor.target_temp = 500 + ZERO_C_TO_K;
213 sc->sc_sensor.max_temp = 600 + ZERO_C_TO_K;
214 }
215 else {
216 sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K;
217 sc->sc_sensor.max_temp = 600 + ZERO_C_TO_K;
218 }
219
220 sc->sc_sensor.read =
221 (int (*)(struct pmac_therm *sc))(ds1775_sensor_read);
222 pmac_thermal_sensor_register(&sc->sc_sensor);
223
224 sprintf(sysctl_desc,"%s (%s)", sc->sc_sensor.name, units);
225 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
226 sysctl_name,
227 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
228 0, ds1775_sensor_sysctl, "IK", sysctl_desc);
229
230 config_intrhook_disestablish(&sc->enum_hook);
231 }
232
233 static int
234 ds1775_sensor_read(struct ds1775_softc *sc)
235 {
236 uint16_t buf[2];
237 uint16_t read;
238 int err;
239
240 err = ds1775_read_2(sc->sc_dev, sc->sc_addr, DS1775_TEMP, buf);
241 if (err < 0)
242 return (-1);
243
244 read = *((int16_t *)buf);
245
246 /* The default mode of the ADC is 9 bit, the resolution is 0.5 C per
247 bit. The temperature is in tenth kelvin.
248 */
249 return (((int16_t)(read) >> 7) * 5 + ZERO_C_TO_K);
250 }
251
252 static int
253 ds1775_sensor_sysctl(SYSCTL_HANDLER_ARGS)
254 {
255 device_t dev;
256 struct ds1775_softc *sc;
257 int error;
258 unsigned int temp;
259
260 dev = arg1;
261 sc = device_get_softc(dev);
262
263 temp = ds1775_sensor_read(sc);
264 if (temp < 0)
265 return (EIO);
266
267 error = sysctl_handle_int(oidp, &temp, 0, req);
268
269 return (error);
270 }
Cache object: be3099f98bdc5c8df9acd12a40253882
|