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