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