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