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