1*7c569caaSEmmanuel Vadot /*-
2*7c569caaSEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause
3*7c569caaSEmmanuel Vadot *
4*7c569caaSEmmanuel Vadot * Copyright (c) 2012 Andreas Tobler
5*7c569caaSEmmanuel Vadot *
6*7c569caaSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
7*7c569caaSEmmanuel Vadot * modification, are permitted provided that the following conditions
8*7c569caaSEmmanuel Vadot * are met:
9*7c569caaSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
10*7c569caaSEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
11*7c569caaSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
12*7c569caaSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
13*7c569caaSEmmanuel Vadot * documentation and/or other materials provided with the distribution.
14*7c569caaSEmmanuel Vadot *
15*7c569caaSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*7c569caaSEmmanuel Vadot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*7c569caaSEmmanuel Vadot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*7c569caaSEmmanuel Vadot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*7c569caaSEmmanuel Vadot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20*7c569caaSEmmanuel Vadot * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21*7c569caaSEmmanuel Vadot * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22*7c569caaSEmmanuel Vadot * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23*7c569caaSEmmanuel Vadot * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*7c569caaSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*7c569caaSEmmanuel Vadot * SUCH DAMAGE.
26*7c569caaSEmmanuel Vadot */
27*7c569caaSEmmanuel Vadot
28*7c569caaSEmmanuel Vadot #include <sys/param.h>
29*7c569caaSEmmanuel Vadot #include <sys/bus.h>
30*7c569caaSEmmanuel Vadot #include <sys/systm.h>
31*7c569caaSEmmanuel Vadot #include <sys/module.h>
32*7c569caaSEmmanuel Vadot #include <sys/callout.h>
33*7c569caaSEmmanuel Vadot #include <sys/conf.h>
34*7c569caaSEmmanuel Vadot #include <sys/cpu.h>
35*7c569caaSEmmanuel Vadot #include <sys/ctype.h>
36*7c569caaSEmmanuel Vadot #include <sys/kernel.h>
37*7c569caaSEmmanuel Vadot #include <sys/reboot.h>
38*7c569caaSEmmanuel Vadot #include <sys/rman.h>
39*7c569caaSEmmanuel Vadot #include <sys/sysctl.h>
40*7c569caaSEmmanuel Vadot #include <sys/limits.h>
41*7c569caaSEmmanuel Vadot
42*7c569caaSEmmanuel Vadot #include <machine/bus.h>
43*7c569caaSEmmanuel Vadot #include <machine/md_var.h>
44*7c569caaSEmmanuel Vadot
45*7c569caaSEmmanuel Vadot #include <dev/iicbus/iicbus.h>
46*7c569caaSEmmanuel Vadot #include <dev/iicbus/iiconf.h>
47*7c569caaSEmmanuel Vadot
48*7c569caaSEmmanuel Vadot #include <dev/ofw/openfirm.h>
49*7c569caaSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
50*7c569caaSEmmanuel Vadot #include <powerpc/powermac/powermac_thermal.h>
51*7c569caaSEmmanuel Vadot
52*7c569caaSEmmanuel Vadot /* Sensor: Maxim DS1631 */
53*7c569caaSEmmanuel Vadot
54*7c569caaSEmmanuel Vadot #define DS1631_STOP 0x22
55*7c569caaSEmmanuel Vadot #define DS1631_START 0x51
56*7c569caaSEmmanuel Vadot #define DS1631_RESET 0x54
57*7c569caaSEmmanuel Vadot #define DS1631_TEMP 0xAA
58*7c569caaSEmmanuel Vadot #define DS1631_CONTROL 0xAC
59*7c569caaSEmmanuel Vadot #define DS1631_CONTROL_1SHOT 0x01
60*7c569caaSEmmanuel Vadot #define DS1631_CONTROL_9BIT 0x00
61*7c569caaSEmmanuel Vadot #define DS1631_CONTROL_10BIT 0x04
62*7c569caaSEmmanuel Vadot #define DS1631_CONTROL_11BIT 0x08
63*7c569caaSEmmanuel Vadot #define DS1631_CONTROL_12BIT 0x0C
64*7c569caaSEmmanuel Vadot
65*7c569caaSEmmanuel Vadot
66*7c569caaSEmmanuel Vadot
67*7c569caaSEmmanuel Vadot /* Regular bus attachment functions */
68*7c569caaSEmmanuel Vadot static int ds1631_probe(device_t);
69*7c569caaSEmmanuel Vadot static int ds1631_attach(device_t);
70*7c569caaSEmmanuel Vadot
71*7c569caaSEmmanuel Vadot struct ds1631_softc {
72*7c569caaSEmmanuel Vadot struct pmac_therm sc_sensor;
73*7c569caaSEmmanuel Vadot device_t sc_dev;
74*7c569caaSEmmanuel Vadot struct intr_config_hook enum_hook;
75*7c569caaSEmmanuel Vadot uint32_t sc_addr;
76*7c569caaSEmmanuel Vadot uint32_t init_done;
77*7c569caaSEmmanuel Vadot };
78*7c569caaSEmmanuel Vadot
79*7c569caaSEmmanuel Vadot struct write_data {
80*7c569caaSEmmanuel Vadot uint8_t reg;
81*7c569caaSEmmanuel Vadot uint8_t val;
82*7c569caaSEmmanuel Vadot };
83*7c569caaSEmmanuel Vadot
84*7c569caaSEmmanuel Vadot struct read_data {
85*7c569caaSEmmanuel Vadot uint8_t reg;
86*7c569caaSEmmanuel Vadot uint16_t val;
87*7c569caaSEmmanuel Vadot };
88*7c569caaSEmmanuel Vadot
89*7c569caaSEmmanuel Vadot /* Utility functions */
90*7c569caaSEmmanuel Vadot static int ds1631_sensor_read(struct ds1631_softc *sc);
91*7c569caaSEmmanuel Vadot static int ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS);
92*7c569caaSEmmanuel Vadot static void ds1631_start(void *xdev);
93*7c569caaSEmmanuel Vadot static int ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg,
94*7c569caaSEmmanuel Vadot uint8_t *data);
95*7c569caaSEmmanuel Vadot static int ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg,
96*7c569caaSEmmanuel Vadot uint16_t *data);
97*7c569caaSEmmanuel Vadot static int ds1631_write(device_t dev, uint32_t addr, uint8_t reg,
98*7c569caaSEmmanuel Vadot uint8_t *buff, int len);
99*7c569caaSEmmanuel Vadot
100*7c569caaSEmmanuel Vadot static device_method_t ds1631_methods[] = {
101*7c569caaSEmmanuel Vadot /* Device interface */
102*7c569caaSEmmanuel Vadot DEVMETHOD(device_probe, ds1631_probe),
103*7c569caaSEmmanuel Vadot DEVMETHOD(device_attach, ds1631_attach),
104*7c569caaSEmmanuel Vadot { 0, 0 },
105*7c569caaSEmmanuel Vadot };
106*7c569caaSEmmanuel Vadot
107*7c569caaSEmmanuel Vadot static driver_t ds1631_driver = {
108*7c569caaSEmmanuel Vadot "ds1631",
109*7c569caaSEmmanuel Vadot ds1631_methods,
110*7c569caaSEmmanuel Vadot sizeof(struct ds1631_softc)
111*7c569caaSEmmanuel Vadot };
112*7c569caaSEmmanuel Vadot
113*7c569caaSEmmanuel Vadot DRIVER_MODULE(ds1631, iicbus, ds1631_driver, 0, 0);
114*7c569caaSEmmanuel Vadot
115*7c569caaSEmmanuel Vadot static int
ds1631_write(device_t dev,uint32_t addr,uint8_t reg,uint8_t * buff,int len)116*7c569caaSEmmanuel Vadot ds1631_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
117*7c569caaSEmmanuel Vadot {
118*7c569caaSEmmanuel Vadot uint8_t buf[4];
119*7c569caaSEmmanuel Vadot int try = 0;
120*7c569caaSEmmanuel Vadot
121*7c569caaSEmmanuel Vadot struct iic_msg msg[] = {
122*7c569caaSEmmanuel Vadot { addr, IIC_M_WR, 0, buf }
123*7c569caaSEmmanuel Vadot };
124*7c569caaSEmmanuel Vadot
125*7c569caaSEmmanuel Vadot /* Prepare the write msg. */
126*7c569caaSEmmanuel Vadot msg[0].len = len + 1;
127*7c569caaSEmmanuel Vadot buf[0] = reg;
128*7c569caaSEmmanuel Vadot memcpy(buf + 1, buff, len);
129*7c569caaSEmmanuel Vadot
130*7c569caaSEmmanuel Vadot for (;;)
131*7c569caaSEmmanuel Vadot {
132*7c569caaSEmmanuel Vadot if (iicbus_transfer(dev, msg, nitems(msg)) == 0)
133*7c569caaSEmmanuel Vadot return (0);
134*7c569caaSEmmanuel Vadot if (++try > 5) {
135*7c569caaSEmmanuel Vadot device_printf(dev, "iicbus write failed\n");
136*7c569caaSEmmanuel Vadot return (-1);
137*7c569caaSEmmanuel Vadot }
138*7c569caaSEmmanuel Vadot pause("ds1631_write", hz);
139*7c569caaSEmmanuel Vadot }
140*7c569caaSEmmanuel Vadot }
141*7c569caaSEmmanuel Vadot
142*7c569caaSEmmanuel Vadot static int
ds1631_read_1(device_t dev,uint32_t addr,uint8_t reg,uint8_t * data)143*7c569caaSEmmanuel Vadot ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
144*7c569caaSEmmanuel Vadot {
145*7c569caaSEmmanuel Vadot uint8_t buf[4];
146*7c569caaSEmmanuel Vadot int err, try = 0;
147*7c569caaSEmmanuel Vadot
148*7c569caaSEmmanuel Vadot struct iic_msg msg[2] = {
149*7c569caaSEmmanuel Vadot { addr, IIC_M_WR, 1, ® },
150*7c569caaSEmmanuel Vadot { addr, IIC_M_RD, 1, buf },
151*7c569caaSEmmanuel Vadot };
152*7c569caaSEmmanuel Vadot
153*7c569caaSEmmanuel Vadot for (;;)
154*7c569caaSEmmanuel Vadot {
155*7c569caaSEmmanuel Vadot err = iicbus_transfer(dev, msg, nitems(msg));
156*7c569caaSEmmanuel Vadot if (err != 0)
157*7c569caaSEmmanuel Vadot goto retry;
158*7c569caaSEmmanuel Vadot
159*7c569caaSEmmanuel Vadot *data = *((uint8_t*)buf);
160*7c569caaSEmmanuel Vadot return (0);
161*7c569caaSEmmanuel Vadot retry:
162*7c569caaSEmmanuel Vadot if (++try > 5) {
163*7c569caaSEmmanuel Vadot device_printf(dev, "iicbus read failed\n");
164*7c569caaSEmmanuel Vadot return (-1);
165*7c569caaSEmmanuel Vadot }
166*7c569caaSEmmanuel Vadot pause("ds1631_read_1", hz);
167*7c569caaSEmmanuel Vadot }
168*7c569caaSEmmanuel Vadot }
169*7c569caaSEmmanuel Vadot
170*7c569caaSEmmanuel Vadot static int
ds1631_read_2(device_t dev,uint32_t addr,uint8_t reg,uint16_t * data)171*7c569caaSEmmanuel Vadot ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
172*7c569caaSEmmanuel Vadot {
173*7c569caaSEmmanuel Vadot uint8_t buf[4];
174*7c569caaSEmmanuel Vadot int err, try = 0;
175*7c569caaSEmmanuel Vadot
176*7c569caaSEmmanuel Vadot struct iic_msg msg[2] = {
177*7c569caaSEmmanuel Vadot { addr, IIC_M_WR, 1, ® },
178*7c569caaSEmmanuel Vadot { addr, IIC_M_RD, 2, buf },
179*7c569caaSEmmanuel Vadot };
180*7c569caaSEmmanuel Vadot
181*7c569caaSEmmanuel Vadot for (;;)
182*7c569caaSEmmanuel Vadot {
183*7c569caaSEmmanuel Vadot err = iicbus_transfer(dev, msg, nitems(msg));
184*7c569caaSEmmanuel Vadot if (err != 0)
185*7c569caaSEmmanuel Vadot goto retry;
186*7c569caaSEmmanuel Vadot
187*7c569caaSEmmanuel Vadot *data = *((uint16_t*)buf);
188*7c569caaSEmmanuel Vadot return (0);
189*7c569caaSEmmanuel Vadot retry:
190*7c569caaSEmmanuel Vadot if (++try > 5) {
191*7c569caaSEmmanuel Vadot device_printf(dev, "iicbus read failed\n");
192*7c569caaSEmmanuel Vadot return (-1);
193*7c569caaSEmmanuel Vadot }
194*7c569caaSEmmanuel Vadot pause("ds1631_read_2", hz);
195*7c569caaSEmmanuel Vadot }
196*7c569caaSEmmanuel Vadot }
197*7c569caaSEmmanuel Vadot
198*7c569caaSEmmanuel Vadot static int
ds1631_probe(device_t dev)199*7c569caaSEmmanuel Vadot ds1631_probe(device_t dev)
200*7c569caaSEmmanuel Vadot {
201*7c569caaSEmmanuel Vadot const char *name, *compatible;
202*7c569caaSEmmanuel Vadot struct ds1631_softc *sc;
203*7c569caaSEmmanuel Vadot
204*7c569caaSEmmanuel Vadot name = ofw_bus_get_name(dev);
205*7c569caaSEmmanuel Vadot compatible = ofw_bus_get_compat(dev);
206*7c569caaSEmmanuel Vadot
207*7c569caaSEmmanuel Vadot if (!name)
208*7c569caaSEmmanuel Vadot return (ENXIO);
209*7c569caaSEmmanuel Vadot
210*7c569caaSEmmanuel Vadot if (strcmp(name, "temp-monitor") != 0 ||
211*7c569caaSEmmanuel Vadot strcmp(compatible, "ds1631") != 0 )
212*7c569caaSEmmanuel Vadot return (ENXIO);
213*7c569caaSEmmanuel Vadot
214*7c569caaSEmmanuel Vadot sc = device_get_softc(dev);
215*7c569caaSEmmanuel Vadot sc->sc_dev = dev;
216*7c569caaSEmmanuel Vadot sc->sc_addr = iicbus_get_addr(dev);
217*7c569caaSEmmanuel Vadot
218*7c569caaSEmmanuel Vadot device_set_desc(dev, "Temp-Monitor DS1631");
219*7c569caaSEmmanuel Vadot
220*7c569caaSEmmanuel Vadot return (0);
221*7c569caaSEmmanuel Vadot }
222*7c569caaSEmmanuel Vadot
223*7c569caaSEmmanuel Vadot static int
ds1631_attach(device_t dev)224*7c569caaSEmmanuel Vadot ds1631_attach(device_t dev)
225*7c569caaSEmmanuel Vadot {
226*7c569caaSEmmanuel Vadot struct ds1631_softc *sc;
227*7c569caaSEmmanuel Vadot
228*7c569caaSEmmanuel Vadot sc = device_get_softc(dev);
229*7c569caaSEmmanuel Vadot
230*7c569caaSEmmanuel Vadot sc->enum_hook.ich_func = ds1631_start;
231*7c569caaSEmmanuel Vadot sc->enum_hook.ich_arg = dev;
232*7c569caaSEmmanuel Vadot
233*7c569caaSEmmanuel Vadot /*
234*7c569caaSEmmanuel Vadot * We have to wait until interrupts are enabled. I2C read and write
235*7c569caaSEmmanuel Vadot * only works if the interrupts are available.
236*7c569caaSEmmanuel Vadot * The unin/i2c is controlled by the htpic on unin. But this is not
237*7c569caaSEmmanuel Vadot * the master. The openpic on mac-io is controlling the htpic.
238*7c569caaSEmmanuel Vadot * This one gets attached after the mac-io probing and then the
239*7c569caaSEmmanuel Vadot * interrupts will be available.
240*7c569caaSEmmanuel Vadot */
241*7c569caaSEmmanuel Vadot
242*7c569caaSEmmanuel Vadot if (config_intrhook_establish(&sc->enum_hook) != 0)
243*7c569caaSEmmanuel Vadot return (ENOMEM);
244*7c569caaSEmmanuel Vadot
245*7c569caaSEmmanuel Vadot return (0);
246*7c569caaSEmmanuel Vadot }
247*7c569caaSEmmanuel Vadot static int
ds1631_init(device_t dev,uint32_t addr)248*7c569caaSEmmanuel Vadot ds1631_init(device_t dev, uint32_t addr)
249*7c569caaSEmmanuel Vadot {
250*7c569caaSEmmanuel Vadot uint8_t conf;
251*7c569caaSEmmanuel Vadot int err;
252*7c569caaSEmmanuel Vadot struct ds1631_softc *sc;
253*7c569caaSEmmanuel Vadot
254*7c569caaSEmmanuel Vadot sc = device_get_softc(dev);
255*7c569caaSEmmanuel Vadot
256*7c569caaSEmmanuel Vadot err = ds1631_read_1(dev, addr, DS1631_CONTROL, &conf);
257*7c569caaSEmmanuel Vadot if (err < 0) {
258*7c569caaSEmmanuel Vadot device_printf(dev, "ds1631 read config failed: %x\n", err);
259*7c569caaSEmmanuel Vadot return (-1);
260*7c569caaSEmmanuel Vadot }
261*7c569caaSEmmanuel Vadot
262*7c569caaSEmmanuel Vadot /* Stop the conversion if not in 1SHOT mode. */
263*7c569caaSEmmanuel Vadot if (conf & ~DS1631_CONTROL_1SHOT)
264*7c569caaSEmmanuel Vadot err = ds1631_write(dev, addr, DS1631_STOP, &conf, 0);
265*7c569caaSEmmanuel Vadot
266*7c569caaSEmmanuel Vadot /*
267*7c569caaSEmmanuel Vadot * Setup the resolution, 10-bit is enough. Each bit increase in
268*7c569caaSEmmanuel Vadot * resolution doubles the conversion time.
269*7c569caaSEmmanuel Vadot */
270*7c569caaSEmmanuel Vadot conf = DS1631_CONTROL_10BIT;
271*7c569caaSEmmanuel Vadot
272*7c569caaSEmmanuel Vadot err = ds1631_write(dev, addr, DS1631_CONTROL, &conf, sizeof(conf));
273*7c569caaSEmmanuel Vadot if (err < 0) {
274*7c569caaSEmmanuel Vadot device_printf(dev, "ds1631 write config failed: %x\n", err);
275*7c569caaSEmmanuel Vadot return (-1);
276*7c569caaSEmmanuel Vadot }
277*7c569caaSEmmanuel Vadot
278*7c569caaSEmmanuel Vadot /* And now start....*/
279*7c569caaSEmmanuel Vadot err = ds1631_write(dev, addr, DS1631_START, &conf, 0);
280*7c569caaSEmmanuel Vadot
281*7c569caaSEmmanuel Vadot if (err < 0) {
282*7c569caaSEmmanuel Vadot device_printf(dev, "ds1631 write start failed: %x\n", err);
283*7c569caaSEmmanuel Vadot return (-1);
284*7c569caaSEmmanuel Vadot }
285*7c569caaSEmmanuel Vadot
286*7c569caaSEmmanuel Vadot sc->init_done = 1;
287*7c569caaSEmmanuel Vadot
288*7c569caaSEmmanuel Vadot return (0);
289*7c569caaSEmmanuel Vadot
290*7c569caaSEmmanuel Vadot }
291*7c569caaSEmmanuel Vadot static void
ds1631_start(void * xdev)292*7c569caaSEmmanuel Vadot ds1631_start(void *xdev)
293*7c569caaSEmmanuel Vadot {
294*7c569caaSEmmanuel Vadot phandle_t child, node;
295*7c569caaSEmmanuel Vadot struct ds1631_softc *sc;
296*7c569caaSEmmanuel Vadot struct sysctl_oid *oid, *sensroot_oid;
297*7c569caaSEmmanuel Vadot struct sysctl_ctx_list *ctx;
298*7c569caaSEmmanuel Vadot ssize_t plen;
299*7c569caaSEmmanuel Vadot int i;
300*7c569caaSEmmanuel Vadot char sysctl_desc[40], sysctl_name[40];
301*7c569caaSEmmanuel Vadot
302*7c569caaSEmmanuel Vadot device_t dev = (device_t)xdev;
303*7c569caaSEmmanuel Vadot
304*7c569caaSEmmanuel Vadot sc = device_get_softc(dev);
305*7c569caaSEmmanuel Vadot
306*7c569caaSEmmanuel Vadot child = ofw_bus_get_node(dev);
307*7c569caaSEmmanuel Vadot
308*7c569caaSEmmanuel Vadot ctx = device_get_sysctl_ctx(dev);
309*7c569caaSEmmanuel Vadot sensroot_oid = SYSCTL_ADD_NODE(ctx,
310*7c569caaSEmmanuel Vadot SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
311*7c569caaSEmmanuel Vadot CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "DS1631 Sensor Information");
312*7c569caaSEmmanuel Vadot
313*7c569caaSEmmanuel Vadot if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone,
314*7c569caaSEmmanuel Vadot sizeof(int)) < 0)
315*7c569caaSEmmanuel Vadot sc->sc_sensor.zone = 0;
316*7c569caaSEmmanuel Vadot
317*7c569caaSEmmanuel Vadot plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
318*7c569caaSEmmanuel Vadot sizeof(sc->sc_sensor.name));
319*7c569caaSEmmanuel Vadot if (plen == -1) {
320*7c569caaSEmmanuel Vadot /*
321*7c569caaSEmmanuel Vadot * Ok, no hwsensor-location property, so let's look for a
322*7c569caaSEmmanuel Vadot * location property on a sub node.
323*7c569caaSEmmanuel Vadot */
324*7c569caaSEmmanuel Vadot for (node = OF_child(child); node; node = OF_peer(node))
325*7c569caaSEmmanuel Vadot plen = OF_getprop(node, "location", sc->sc_sensor.name,
326*7c569caaSEmmanuel Vadot sizeof(sc->sc_sensor.name));
327*7c569caaSEmmanuel Vadot }
328*7c569caaSEmmanuel Vadot
329*7c569caaSEmmanuel Vadot if (plen == -1) {
330*7c569caaSEmmanuel Vadot strcpy(sysctl_name, "sensor");
331*7c569caaSEmmanuel Vadot } else {
332*7c569caaSEmmanuel Vadot for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
333*7c569caaSEmmanuel Vadot sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
334*7c569caaSEmmanuel Vadot if (isspace(sysctl_name[i]))
335*7c569caaSEmmanuel Vadot sysctl_name[i] = '_';
336*7c569caaSEmmanuel Vadot }
337*7c569caaSEmmanuel Vadot sysctl_name[i] = 0;
338*7c569caaSEmmanuel Vadot }
339*7c569caaSEmmanuel Vadot
340*7c569caaSEmmanuel Vadot /* Make up target temperatures. These are low, for the drive bay. */
341*7c569caaSEmmanuel Vadot if (sc->sc_sensor.zone == 0) {
342*7c569caaSEmmanuel Vadot sc->sc_sensor.target_temp = 400 + ZERO_C_TO_K;
343*7c569caaSEmmanuel Vadot sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
344*7c569caaSEmmanuel Vadot } else {
345*7c569caaSEmmanuel Vadot sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K;
346*7c569caaSEmmanuel Vadot sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
347*7c569caaSEmmanuel Vadot }
348*7c569caaSEmmanuel Vadot
349*7c569caaSEmmanuel Vadot sc->sc_sensor.read =
350*7c569caaSEmmanuel Vadot (int (*)(struct pmac_therm *sc))(ds1631_sensor_read);
351*7c569caaSEmmanuel Vadot pmac_thermal_sensor_register(&sc->sc_sensor);
352*7c569caaSEmmanuel Vadot
353*7c569caaSEmmanuel Vadot sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)");
354*7c569caaSEmmanuel Vadot oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
355*7c569caaSEmmanuel Vadot OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
356*7c569caaSEmmanuel Vadot "Sensor Information");
357*7c569caaSEmmanuel Vadot SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
358*7c569caaSEmmanuel Vadot CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
359*7c569caaSEmmanuel Vadot 0, ds1631_sensor_sysctl, "IK", sysctl_desc);
360*7c569caaSEmmanuel Vadot
361*7c569caaSEmmanuel Vadot config_intrhook_disestablish(&sc->enum_hook);
362*7c569caaSEmmanuel Vadot }
363*7c569caaSEmmanuel Vadot
364*7c569caaSEmmanuel Vadot static int
ds1631_sensor_read(struct ds1631_softc * sc)365*7c569caaSEmmanuel Vadot ds1631_sensor_read(struct ds1631_softc *sc)
366*7c569caaSEmmanuel Vadot {
367*7c569caaSEmmanuel Vadot uint16_t buf[2];
368*7c569caaSEmmanuel Vadot uint16_t read;
369*7c569caaSEmmanuel Vadot int err;
370*7c569caaSEmmanuel Vadot
371*7c569caaSEmmanuel Vadot if (!sc->init_done)
372*7c569caaSEmmanuel Vadot ds1631_init(sc->sc_dev, sc->sc_addr);
373*7c569caaSEmmanuel Vadot
374*7c569caaSEmmanuel Vadot err = ds1631_read_2(sc->sc_dev, sc->sc_addr, DS1631_TEMP, buf);
375*7c569caaSEmmanuel Vadot if (err < 0) {
376*7c569caaSEmmanuel Vadot device_printf(sc->sc_dev, "ds1631 read TEMP failed: %x\n", err);
377*7c569caaSEmmanuel Vadot return (-1);
378*7c569caaSEmmanuel Vadot }
379*7c569caaSEmmanuel Vadot
380*7c569caaSEmmanuel Vadot read = *((int16_t *)buf);
381*7c569caaSEmmanuel Vadot
382*7c569caaSEmmanuel Vadot /*
383*7c569caaSEmmanuel Vadot * The default mode of the ADC is 12-bit, the resolution is 0.0625 C
384*7c569caaSEmmanuel Vadot * per bit. The temperature is in tenth kelvin.
385*7c569caaSEmmanuel Vadot * We use 10-bit resolution which seems enough, resolution is 0.25 C.
386*7c569caaSEmmanuel Vadot */
387*7c569caaSEmmanuel Vadot
388*7c569caaSEmmanuel Vadot return (((int16_t)(read) >> 6) * 25 / 10 + ZERO_C_TO_K);
389*7c569caaSEmmanuel Vadot }
390*7c569caaSEmmanuel Vadot
391*7c569caaSEmmanuel Vadot static int
ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS)392*7c569caaSEmmanuel Vadot ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS)
393*7c569caaSEmmanuel Vadot {
394*7c569caaSEmmanuel Vadot device_t dev;
395*7c569caaSEmmanuel Vadot struct ds1631_softc *sc;
396*7c569caaSEmmanuel Vadot int error;
397*7c569caaSEmmanuel Vadot int temp;
398*7c569caaSEmmanuel Vadot
399*7c569caaSEmmanuel Vadot dev = arg1;
400*7c569caaSEmmanuel Vadot sc = device_get_softc(dev);
401*7c569caaSEmmanuel Vadot
402*7c569caaSEmmanuel Vadot temp = ds1631_sensor_read(sc);
403*7c569caaSEmmanuel Vadot if (temp < 0)
404*7c569caaSEmmanuel Vadot return (EIO);
405*7c569caaSEmmanuel Vadot
406*7c569caaSEmmanuel Vadot error = sysctl_handle_int(oidp, &temp, 0, req);
407*7c569caaSEmmanuel Vadot
408*7c569caaSEmmanuel Vadot return (error);
409*7c569caaSEmmanuel Vadot }
410