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