xref: /freebsd/sys/dev/iicbus/sensor/ds1631.c (revision e5b786625f7f82a1fa91e41823332459ea5550f9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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 /* Sensor: Maxim DS1631 */
54 
55 #define DS1631_STOP            0x22
56 #define DS1631_START           0x51
57 #define DS1631_RESET           0x54
58 #define DS1631_TEMP            0xAA
59 #define DS1631_CONTROL         0xAC
60 #define DS1631_CONTROL_1SHOT   0x01
61 #define DS1631_CONTROL_9BIT    0x00
62 #define DS1631_CONTROL_10BIT   0x04
63 #define DS1631_CONTROL_11BIT   0x08
64 #define DS1631_CONTROL_12BIT   0x0C
65 
66 
67 
68 /* Regular bus attachment functions */
69 static int  ds1631_probe(device_t);
70 static int  ds1631_attach(device_t);
71 
72 struct ds1631_softc {
73 	struct pmac_therm	sc_sensor;
74 	device_t		sc_dev;
75 	struct intr_config_hook enum_hook;
76 	uint32_t                sc_addr;
77 	uint32_t		init_done;
78 };
79 
80 struct write_data {
81 	uint8_t reg;
82 	uint8_t val;
83 };
84 
85 struct read_data {
86 	uint8_t reg;
87 	uint16_t val;
88 };
89 
90 /* Utility functions */
91 static int  ds1631_sensor_read(struct ds1631_softc *sc);
92 static int  ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS);
93 static void ds1631_start(void *xdev);
94 static int  ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg,
95 			  uint8_t *data);
96 static int  ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg,
97 			  uint16_t *data);
98 static int  ds1631_write(device_t dev, uint32_t addr, uint8_t reg,
99 			 uint8_t *buff, int len);
100 
101 static device_method_t  ds1631_methods[] = {
102 	/* Device interface */
103 	DEVMETHOD(device_probe,		ds1631_probe),
104 	DEVMETHOD(device_attach,	ds1631_attach),
105 	{ 0, 0 },
106 };
107 
108 static driver_t ds1631_driver = {
109 	"ds1631",
110 	ds1631_methods,
111 	sizeof(struct ds1631_softc)
112 };
113 
114 DRIVER_MODULE(ds1631, iicbus, ds1631_driver, 0, 0);
115 
116 static int
117 ds1631_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
118 {
119 	uint8_t buf[4];
120 	int try = 0;
121 
122 	struct iic_msg msg[] = {
123 		{ addr, IIC_M_WR, 0, buf }
124 	};
125 
126 	/* Prepare the write msg. */
127 	msg[0].len = len + 1;
128 	buf[0] = reg;
129 	memcpy(buf + 1, buff, len);
130 
131 	for (;;)
132 	{
133 		if (iicbus_transfer(dev, msg, nitems(msg)) == 0)
134 			return (0);
135 		if (++try > 5) {
136 			device_printf(dev, "iicbus write failed\n");
137 			return (-1);
138 		}
139 		pause("ds1631_write", hz);
140 	}
141 }
142 
143 static int
144 ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
145 {
146 	uint8_t buf[4];
147 	int err, try = 0;
148 
149 	struct iic_msg msg[2] = {
150 		{ addr, IIC_M_WR, 1, &reg },
151 		{ addr, IIC_M_RD, 1, buf },
152 	};
153 
154 	for (;;)
155 	{
156 		err = iicbus_transfer(dev, msg, nitems(msg));
157 		if (err != 0)
158 			goto retry;
159 
160 		*data = *((uint8_t*)buf);
161 		return (0);
162 	retry:
163 		if (++try > 5) {
164 			device_printf(dev, "iicbus read failed\n");
165 			return (-1);
166 		}
167 		pause("ds1631_read_1", hz);
168 	}
169 }
170 
171 static int
172 ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
173 {
174 	uint8_t buf[4];
175 	int err, try = 0;
176 
177 	struct iic_msg msg[2] = {
178 		{ addr, IIC_M_WR, 1, &reg },
179 		{ addr, IIC_M_RD, 2, buf },
180 	};
181 
182 	for (;;)
183 	{
184 		err = iicbus_transfer(dev, msg, nitems(msg));
185 		if (err != 0)
186 			goto retry;
187 
188 		*data = *((uint16_t*)buf);
189 		return (0);
190 	retry:
191 		if (++try > 5) {
192 			device_printf(dev, "iicbus read failed\n");
193 			return (-1);
194 		}
195 		pause("ds1631_read_2", hz);
196 	}
197 }
198 
199 static int
200 ds1631_probe(device_t dev)
201 {
202 	const char  *name, *compatible;
203 	struct ds1631_softc *sc;
204 
205 	name = ofw_bus_get_name(dev);
206 	compatible = ofw_bus_get_compat(dev);
207 
208 	if (!name)
209 		return (ENXIO);
210 
211 	if (strcmp(name, "temp-monitor") != 0 ||
212 	    strcmp(compatible, "ds1631") != 0 )
213 		return (ENXIO);
214 
215 	sc = device_get_softc(dev);
216 	sc->sc_dev = dev;
217 	sc->sc_addr = iicbus_get_addr(dev);
218 
219 	device_set_desc(dev, "Temp-Monitor DS1631");
220 
221 	return (0);
222 }
223 
224 static int
225 ds1631_attach(device_t dev)
226 {
227 	struct ds1631_softc *sc;
228 
229 	sc = device_get_softc(dev);
230 
231 	sc->enum_hook.ich_func = ds1631_start;
232 	sc->enum_hook.ich_arg = dev;
233 
234 	/*
235 	 * We have to wait until interrupts are enabled. I2C read and write
236 	 * only works if the interrupts are available.
237 	 * The unin/i2c is controlled by the htpic on unin. But this is not
238 	 * the master. The openpic on mac-io is controlling the htpic.
239 	 * This one gets attached after the mac-io probing and then the
240 	 * interrupts will be available.
241 	 */
242 
243 	if (config_intrhook_establish(&sc->enum_hook) != 0)
244 		return (ENOMEM);
245 
246 	return (0);
247 }
248 static int
249 ds1631_init(device_t dev, uint32_t addr)
250 {
251 	uint8_t conf;
252 	int err;
253 	struct ds1631_softc *sc;
254 
255 	sc = device_get_softc(dev);
256 
257 	err = ds1631_read_1(dev, addr, DS1631_CONTROL, &conf);
258 	if (err < 0) {
259 		device_printf(dev, "ds1631 read config failed: %x\n", err);
260 		return (-1);
261 	}
262 
263 	/* Stop the conversion if not in 1SHOT mode. */
264 	if (conf & ~DS1631_CONTROL_1SHOT)
265 		err = ds1631_write(dev, addr, DS1631_STOP, &conf, 0);
266 
267 	/*
268 	 * Setup the resolution, 10-bit is enough. Each bit increase in
269 	 * resolution doubles the conversion time.
270 	 */
271 	conf = DS1631_CONTROL_10BIT;
272 
273 	err = ds1631_write(dev, addr, DS1631_CONTROL, &conf, sizeof(conf));
274 	if (err < 0) {
275 		device_printf(dev, "ds1631 write config failed: %x\n", err);
276 		return (-1);
277 	}
278 
279 	/* And now start....*/
280 	err = ds1631_write(dev, addr, DS1631_START, &conf, 0);
281 
282 	if (err < 0) {
283 		device_printf(dev, "ds1631 write start failed: %x\n", err);
284 		return (-1);
285 	}
286 
287 	sc->init_done = 1;
288 
289 	return (0);
290 
291 }
292 static void
293 ds1631_start(void *xdev)
294 {
295 	phandle_t child, node;
296 	struct ds1631_softc *sc;
297 	struct sysctl_oid *oid, *sensroot_oid;
298 	struct sysctl_ctx_list *ctx;
299 	ssize_t plen;
300 	int i;
301 	char  sysctl_desc[40], sysctl_name[40];
302 
303 	device_t dev = (device_t)xdev;
304 
305 	sc = device_get_softc(dev);
306 
307 	child = ofw_bus_get_node(dev);
308 
309 	ctx = device_get_sysctl_ctx(dev);
310 	sensroot_oid = SYSCTL_ADD_NODE(ctx,
311 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
312 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "DS1631 Sensor Information");
313 
314 	if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone,
315 		       sizeof(int)) < 0)
316 		sc->sc_sensor.zone = 0;
317 
318 	plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
319 			  sizeof(sc->sc_sensor.name));
320 	if (plen == -1) {
321 		/*
322 		 * Ok, no hwsensor-location property, so let's look for a
323 		 * location property on a sub node.
324 		 */
325 		for (node = OF_child(child); node; node = OF_peer(node))
326 			plen = OF_getprop(node, "location", sc->sc_sensor.name,
327 					  sizeof(sc->sc_sensor.name));
328 	}
329 
330 	if (plen == -1) {
331 		strcpy(sysctl_name, "sensor");
332 	} else {
333 		for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
334 			sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
335 			if (isspace(sysctl_name[i]))
336 				sysctl_name[i] = '_';
337 		}
338 		sysctl_name[i] = 0;
339 	}
340 
341 	/* Make up target temperatures. These are low, for the drive bay. */
342 	if (sc->sc_sensor.zone == 0) {
343 		sc->sc_sensor.target_temp = 400 + ZERO_C_TO_K;
344 		sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
345 	} else {
346 		sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K;
347 		sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
348 	}
349 
350 	sc->sc_sensor.read =
351 	    (int (*)(struct pmac_therm *sc))(ds1631_sensor_read);
352 	pmac_thermal_sensor_register(&sc->sc_sensor);
353 
354 	sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)");
355 	oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
356 	    OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
357 	    "Sensor Information");
358 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
359 			CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
360 			0, ds1631_sensor_sysctl, "IK", sysctl_desc);
361 
362 	config_intrhook_disestablish(&sc->enum_hook);
363 }
364 
365 static int
366 ds1631_sensor_read(struct ds1631_softc *sc)
367 {
368 	uint16_t buf[2];
369 	uint16_t read;
370 	int err;
371 
372 	if (!sc->init_done)
373 		ds1631_init(sc->sc_dev, sc->sc_addr);
374 
375 	err = ds1631_read_2(sc->sc_dev, sc->sc_addr, DS1631_TEMP, buf);
376 	if (err < 0) {
377 		device_printf(sc->sc_dev, "ds1631 read TEMP failed: %x\n", err);
378 		return (-1);
379 	}
380 
381 	read = *((int16_t *)buf);
382 
383 	/*
384 	 * The default mode of the ADC is 12-bit, the resolution is 0.0625 C
385 	 * per bit. The temperature is in tenth kelvin.
386 	 * We use 10-bit resolution which seems enough, resolution is 0.25 C.
387 	 */
388 
389 	return (((int16_t)(read) >> 6) * 25 / 10 + ZERO_C_TO_K);
390 }
391 
392 static int
393 ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS)
394 {
395 	device_t dev;
396 	struct ds1631_softc *sc;
397 	int error;
398 	int temp;
399 
400 	dev = arg1;
401 	sc = device_get_softc(dev);
402 
403 	temp = ds1631_sensor_read(sc);
404 	if (temp < 0)
405 		return (EIO);
406 
407 	error = sysctl_handle_int(oidp, &temp, 0, req);
408 
409 	return (error);
410 }
411