]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/ds1775.c
bhnd(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / sys / dev / iicbus / ds1775.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 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 /* Drivebay sensor: LM75/DS1775. */
56 #define DS1775_TEMP         0x0
57
58 /* Regular bus attachment functions */
59 static int  ds1775_probe(device_t);
60 static int  ds1775_attach(device_t);
61
62 struct ds1775_softc {
63         struct pmac_therm       sc_sensor;
64         device_t                sc_dev;
65         struct intr_config_hook enum_hook;
66         uint32_t                sc_addr;
67 };
68
69 /* Utility functions */
70 static int  ds1775_sensor_read(struct ds1775_softc *sc);
71 static int  ds1775_sensor_sysctl(SYSCTL_HANDLER_ARGS);
72 static void ds1775_start(void *xdev);
73 static int  ds1775_read_2(device_t dev, uint32_t addr, uint8_t reg,
74                           uint16_t *data);
75
76 static device_method_t  ds1775_methods[] = {
77         /* Device interface */
78         DEVMETHOD(device_probe,         ds1775_probe),
79         DEVMETHOD(device_attach,        ds1775_attach),
80         { 0, 0 },
81 };
82
83 static driver_t ds1775_driver = {
84         "ds1775",
85         ds1775_methods,
86         sizeof(struct ds1775_softc)
87 };
88
89 static devclass_t ds1775_devclass;
90
91 DRIVER_MODULE(ds1775, iicbus, ds1775_driver, ds1775_devclass, 0, 0);
92
93 static int
94 ds1775_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
95 {
96         uint8_t buf[4];
97         int err, try = 0;
98
99         struct iic_msg msg[2] = {
100             { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
101             { addr, IIC_M_RD, 2, buf },
102         };
103
104         for (;;)
105         {
106                 err = iicbus_transfer(dev, msg, nitems(msg));
107                 if (err != 0)
108                         goto retry;
109
110                 *data = *((uint16_t*)buf);
111                 return (0);
112         retry:
113                 if (++try > 5) {
114                         device_printf(dev, "iicbus read failed\n");
115                         return (-1);
116                 }
117                 pause("ds1775_read_2", hz);
118         }
119 }
120
121 static int
122 ds1775_probe(device_t dev)
123 {
124         const char  *name, *compatible;
125         struct ds1775_softc *sc;
126
127         name = ofw_bus_get_name(dev);
128         compatible = ofw_bus_get_compat(dev);
129
130         if (!name)
131                 return (ENXIO);
132
133         if (strcmp(name, "temp-monitor") != 0 ||
134             (strcmp(compatible, "ds1775") != 0 &&
135              strcmp(compatible, "lm75") != 0))
136                 return (ENXIO);
137
138         sc = device_get_softc(dev);
139         sc->sc_dev = dev;
140         sc->sc_addr = iicbus_get_addr(dev);
141
142         device_set_desc(dev, "Temp-Monitor DS1775");
143
144         return (0);
145 }
146
147 static int
148 ds1775_attach(device_t dev)
149 {
150         struct ds1775_softc *sc;
151
152         sc = device_get_softc(dev);
153
154         sc->enum_hook.ich_func = ds1775_start;
155         sc->enum_hook.ich_arg = dev;
156
157         /* We have to wait until interrupts are enabled. I2C read and write
158          * only works if the interrupts are available.
159          * The unin/i2c is controlled by the htpic on unin. But this is not
160          * the master. The openpic on mac-io is controlling the htpic.
161          * This one gets attached after the mac-io probing and then the
162          * interrupts will be available.
163          */
164
165         if (config_intrhook_establish(&sc->enum_hook) != 0)
166                 return (ENOMEM);
167
168         return (0);
169 }
170
171 static void
172 ds1775_start(void *xdev)
173 {
174         phandle_t child;
175         struct ds1775_softc *sc;
176         struct sysctl_oid *oid, *sensroot_oid;
177         struct sysctl_ctx_list *ctx;
178         ssize_t plen;
179         int i;
180         char sysctl_name[40], sysctl_desc[40];
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 = SYSCTL_ADD_NODE(ctx,
190             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
191             CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "DS1775 Sensor Information");
192
193         if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone,
194                        sizeof(int)) < 0)
195                 sc->sc_sensor.zone = 0;
196
197         plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
198                           sizeof(sc->sc_sensor.name));
199
200         if (plen == -1) {
201                 strcpy(sysctl_name, "sensor");
202         } else {
203                 for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
204                         sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
205                         if (isspace(sysctl_name[i]))
206                                 sysctl_name[i] = '_';
207                 }
208                 sysctl_name[i] = 0;
209         }
210
211         /* Make up target temperatures. These are low, for the drive bay. */
212         if (sc->sc_sensor.zone == 0) {
213                 sc->sc_sensor.target_temp = 500 + ZERO_C_TO_K;
214                 sc->sc_sensor.max_temp = 600 + ZERO_C_TO_K;
215         }
216         else {
217                 sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K;
218                 sc->sc_sensor.max_temp = 600 + ZERO_C_TO_K;
219         }
220
221         sc->sc_sensor.read =
222             (int (*)(struct pmac_therm *sc))(ds1775_sensor_read);
223         pmac_thermal_sensor_register(&sc->sc_sensor);
224
225         sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)");
226         oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
227             OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
228             "Sensor Information");
229         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
230                         CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
231                         0, ds1775_sensor_sysctl, "IK", sysctl_desc);
232
233         config_intrhook_disestablish(&sc->enum_hook);
234 }
235
236 static int
237 ds1775_sensor_read(struct ds1775_softc *sc)
238 {
239         uint16_t buf[2];
240         uint16_t read;
241         int err;
242
243         err = ds1775_read_2(sc->sc_dev, sc->sc_addr, DS1775_TEMP, buf);
244         if (err < 0)
245                 return (-1);
246
247         read = *((int16_t *)buf);
248
249         /* The default mode of the ADC is 9 bit, the resolution is 0.5 C per
250            bit. The temperature is in tenth kelvin.
251         */
252         return (((int16_t)(read) >> 7) * 5 + ZERO_C_TO_K);
253 }
254
255 static int
256 ds1775_sensor_sysctl(SYSCTL_HANDLER_ARGS)
257 {
258         device_t dev;
259         struct ds1775_softc *sc;
260         int error;
261         int temp;
262
263         dev = arg1;
264         sc = device_get_softc(dev);
265
266         temp = ds1775_sensor_read(sc);
267         if (temp < 0)
268                 return (EIO);
269
270         error = sysctl_handle_int(oidp, &temp, 0, req);
271
272         return (error);
273 }