1/*-
2 * Copyright (c) 2010 Andreas Tobler.
3 * Copyright (c) 2013-2014 Luiz Otavio O Souza <loos@freebsd.org>
4 * All rights reserved.
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__FBSDID("$FreeBSD$");
30
31#include "opt_platform.h"
32
33#include <sys/param.h>
34#include <sys/bus.h>
35#include <sys/endian.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/sysctl.h>
39#include <sys/systm.h>
40
41#include <machine/bus.h>
42
43#include <dev/iicbus/iicbus.h>
44#include <dev/iicbus/iiconf.h>
45
46#ifdef FDT
47#include <dev/ofw/openfirm.h>
48#include <dev/ofw/ofw_bus.h>
49#include <dev/ofw/ofw_bus_subr.h>
50#endif
51
52/* LM75 registers. */
53#define	LM75_TEMP	0x0
54#define	LM75_TEMP_MASK		0xff80
55#define	LM75A_TEMP_MASK		0xffe0
56#define	LM75_CONF	0x1
57#define	LM75_CONF_FSHIFT	3
58#define	LM75_CONF_FAULT		0x18
59#define	LM75_CONF_POL		0x04
60#define	LM75_CONF_MODE		0x02
61#define	LM75_CONF_SHUTD		0x01
62#define	LM75_CONF_MASK		0x1f
63#define	LM75_THYST	0x2
64#define	LM75_TOS	0x3
65
66/* LM75 constants. */
67#define	LM75_TEST_PATTERN	0xa
68#define	LM75_MIN_TEMP		-55
69#define	LM75_MAX_TEMP		125
70#define	LM75_0500C		0x80
71#define	LM75_0250C		0x40
72#define	LM75_0125C		0x20
73#define	LM75_MSB		0x8000
74#define	LM75_NEG_BIT		LM75_MSB
75#define	TZ_ZEROC		2731
76
77/* LM75 supported models. */
78#define	HWTYPE_LM75		1
79#define	HWTYPE_LM75A		2
80
81/* Regular bus attachment functions */
82static int  lm75_probe(device_t);
83static int  lm75_attach(device_t);
84
85struct lm75_softc {
86	device_t		sc_dev;
87	struct intr_config_hook enum_hook;
88	int32_t			sc_hwtype;
89	uint32_t		sc_addr;
90	uint32_t		sc_conf;
91};
92
93/* Utility functions */
94static int  lm75_conf_read(struct lm75_softc *);
95static int  lm75_conf_write(struct lm75_softc *);
96static int  lm75_temp_read(struct lm75_softc *, uint8_t, int *);
97static int  lm75_temp_write(struct lm75_softc *, uint8_t, int);
98static void lm75_start(void *);
99static int  lm75_read(device_t, uint32_t, uint8_t, uint8_t *, size_t);
100static int  lm75_write(device_t, uint32_t, uint8_t *, size_t);
101static int  lm75_str_mode(char *);
102static int  lm75_str_pol(char *);
103static int  lm75_temp_sysctl(SYSCTL_HANDLER_ARGS);
104static int  lm75_faults_sysctl(SYSCTL_HANDLER_ARGS);
105static int  lm75_mode_sysctl(SYSCTL_HANDLER_ARGS);
106static int  lm75_pol_sysctl(SYSCTL_HANDLER_ARGS);
107static int  lm75_shutdown_sysctl(SYSCTL_HANDLER_ARGS);
108
109static device_method_t  lm75_methods[] = {
110	/* Device interface */
111	DEVMETHOD(device_probe,		lm75_probe),
112	DEVMETHOD(device_attach,	lm75_attach),
113
114	DEVMETHOD_END
115};
116
117static driver_t lm75_driver = {
118	"lm75",
119	lm75_methods,
120	sizeof(struct lm75_softc)
121};
122
123static devclass_t lm75_devclass;
124
125DRIVER_MODULE(lm75, iicbus, lm75_driver, lm75_devclass, 0, 0);
126
127static int
128lm75_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data, size_t len)
129{
130	struct iic_msg msg[2] = {
131	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
132	    { addr, IIC_M_RD, len, data },
133	};
134
135	if (iicbus_transfer(dev, msg, nitems(msg)) != 0)
136		return (-1);
137
138	return (0);
139}
140
141static int
142lm75_write(device_t dev, uint32_t addr, uint8_t *data, size_t len)
143{
144	struct iic_msg msg[1] = {
145	    { addr, IIC_M_WR, len, data },
146	};
147
148	if (iicbus_transfer(dev, msg, nitems(msg)) != 0)
149		return (-1);
150
151	return (0);
152}
153
154static int
155lm75_probe(device_t dev)
156{
157	struct lm75_softc *sc;
158
159	sc = device_get_softc(dev);
160	sc->sc_hwtype = HWTYPE_LM75;
161#ifdef FDT
162	if (!ofw_bus_is_compatible(dev, "national,lm75"))
163		return (ENXIO);
164#endif
165	device_set_desc(dev, "LM75 temperature sensor");
166
167	return (BUS_PROBE_GENERIC);
168}
169
170static int
171lm75_attach(device_t dev)
172{
173	struct lm75_softc *sc;
174
175	sc = device_get_softc(dev);
176	sc->sc_dev = dev;
177	sc->sc_addr = iicbus_get_addr(dev);
178
179	sc->enum_hook.ich_func = lm75_start;
180	sc->enum_hook.ich_arg = dev;
181
182	/*
183	 * We have to wait until interrupts are enabled.  Usually I2C read
184	 * and write only works when the interrupts are available.
185	 */
186	if (config_intrhook_establish(&sc->enum_hook) != 0)
187		return (ENOMEM);
188
189	return (0);
190}
191
192static int
193lm75_type_detect(struct lm75_softc *sc)
194{
195	int i, lm75a;
196	uint8_t buf8;
197	uint32_t conf;
198
199	/* Save the contents of the configuration register. */
200	if (lm75_conf_read(sc) != 0)
201		return (-1);
202	conf = sc->sc_conf;
203
204	/*
205	 * Just write some pattern at configuration register so we can later
206	 * verify.  The test pattern should be pretty harmless.
207	 */
208	sc->sc_conf = LM75_TEST_PATTERN;
209	if (lm75_conf_write(sc) != 0)
210		return (-1);
211
212	/*
213	 * Read the configuration register again and check for our test
214	 * pattern.
215	 */
216	if (lm75_conf_read(sc) != 0)
217		return (-1);
218	if (sc->sc_conf != LM75_TEST_PATTERN)
219		return (-1);
220
221	/*
222	 * Read from nonexistent registers (0x4 ~ 0x6).
223	 * LM75A always return 0xff for nonexistent registers.
224	 * LM75 will return the last read value - our test pattern written to
225	 * configuration register.
226	 */
227	lm75a = 0;
228	for (i = 4; i <= 6; i++) {
229		if (lm75_read(sc->sc_dev, sc->sc_addr, i,
230		    &buf8, sizeof(buf8)) < 0)
231			return (-1);
232		if (buf8 != LM75_TEST_PATTERN && buf8 != 0xff)
233			return (-1);
234		if (buf8 == 0xff)
235			lm75a++;
236	}
237	if (lm75a == 3)
238		sc->sc_hwtype = HWTYPE_LM75A;
239
240	/* Restore the configuration register. */
241	sc->sc_conf = conf;
242	if (lm75_conf_write(sc) != 0)
243		return (-1);
244
245	return (0);
246}
247
248static void
249lm75_start(void *xdev)
250{
251	device_t dev;
252	struct lm75_softc *sc;
253	struct sysctl_ctx_list *ctx;
254	struct sysctl_oid *tree_node;
255	struct sysctl_oid_list *tree;
256
257	dev = (device_t)xdev;
258	sc = device_get_softc(dev);
259	ctx = device_get_sysctl_ctx(dev);
260	tree_node = device_get_sysctl_tree(dev);
261	tree = SYSCTL_CHILDREN(tree_node);
262
263	config_intrhook_disestablish(&sc->enum_hook);
264
265	/*
266	 * Detect the kind of chip we are attaching to.
267	 * This may not work for LM75 clones.
268	 */
269	if (lm75_type_detect(sc) != 0) {
270		device_printf(dev, "cannot read from sensor.\n");
271		return;
272	}
273	if (sc->sc_hwtype == HWTYPE_LM75A)
274		device_printf(dev,
275		    "LM75A type sensor detected (11bits resolution).\n");
276
277	/* Temperature. */
278	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "temperature",
279	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, LM75_TEMP,
280	    lm75_temp_sysctl, "IK", "Current temperature");
281	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "thyst",
282	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, LM75_THYST,
283	    lm75_temp_sysctl, "IK", "Hysteresis temperature");
284	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tos",
285	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, LM75_TOS,
286	    lm75_temp_sysctl, "IK", "Overtemperature");
287
288	/* Configuration parameters. */
289	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "faults",
290	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, dev, 0,
291	    lm75_faults_sysctl, "IU", "LM75 fault queue");
292	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "mode",
293	    CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, dev, 0,
294	    lm75_mode_sysctl, "A", "LM75 mode");
295	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "polarity",
296	    CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, dev, 0,
297	    lm75_pol_sysctl, "A", "LM75 OS polarity");
298	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "shutdown",
299	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, dev, 0,
300	    lm75_shutdown_sysctl, "IU", "LM75 shutdown");
301}
302
303static int
304lm75_conf_read(struct lm75_softc *sc)
305{
306	uint8_t buf8;
307
308	if (lm75_read(sc->sc_dev, sc->sc_addr, LM75_CONF,
309	    &buf8, sizeof(buf8)) < 0)
310		return (-1);
311	sc->sc_conf = (uint32_t)buf8;
312
313	return (0);
314}
315
316static int
317lm75_conf_write(struct lm75_softc *sc)
318{
319	uint8_t buf8[2];
320
321	buf8[0] = LM75_CONF;
322	buf8[1] = (uint8_t)sc->sc_conf & LM75_CONF_MASK;
323	if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, sizeof(buf8)) < 0)
324		return (-1);
325
326	return (0);
327}
328
329static int
330lm75_temp_read(struct lm75_softc *sc, uint8_t reg, int *temp)
331{
332	uint8_t buf8[2];
333	uint16_t buf;
334	int neg, t;
335
336	if (lm75_read(sc->sc_dev, sc->sc_addr, reg, buf8, sizeof(buf8)) < 0)
337		return (-1);
338	buf = (uint16_t)((buf8[0] << 8) | (buf8[1] & 0xff));
339	/*
340	 * LM75 has a 9 bit ADC with resolution of 0.5 C per bit.
341	 * LM75A has an 11 bit ADC with resolution of 0.125 C per bit.
342	 * Temperature is stored with two's complement.
343	 */
344	neg = 0;
345	if (buf & LM75_NEG_BIT) {
346		if (sc->sc_hwtype == HWTYPE_LM75A)
347			buf = ~(buf & LM75A_TEMP_MASK) + 1;
348		else
349			buf = ~(buf & LM75_TEMP_MASK) + 1;
350		neg = 1;
351	}
352	*temp = ((int16_t)buf >> 8) * 10;
353	t = 0;
354	if (sc->sc_hwtype == HWTYPE_LM75A) {
355		if (buf & LM75_0125C)
356			t += 125;
357		if (buf & LM75_0250C)
358			t += 250;
359	}
360	if (buf & LM75_0500C)
361		t += 500;
362	t /= 100;
363	*temp += t;
364	if (neg)
365		*temp = -(*temp);
366	*temp += TZ_ZEROC;
367
368	return (0);
369}
370
371static int
372lm75_temp_write(struct lm75_softc *sc, uint8_t reg, int temp)
373{
374	uint8_t buf8[3];
375	uint16_t buf;
376
377	temp = (temp - TZ_ZEROC) / 10;
378	if (temp > LM75_MAX_TEMP)
379		temp = LM75_MAX_TEMP;
380	if (temp < LM75_MIN_TEMP)
381		temp = LM75_MIN_TEMP;
382
383	buf = (uint16_t)temp;
384	buf <<= 8;
385
386	buf8[0] = reg;
387	buf8[1] = buf >> 8;
388	buf8[2] = buf & 0xff;
389	if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, sizeof(buf8)) < 0)
390		return (-1);
391
392	return (0);
393}
394
395static int
396lm75_str_mode(char *buf)
397{
398	int len, rtrn;
399
400	rtrn = -1;
401	len = strlen(buf);
402	if (len > 2 && strncasecmp("interrupt", buf, len) == 0)
403		rtrn = 1;
404	else if (len > 2 && strncasecmp("comparator", buf, len) == 0)
405		rtrn = 0;
406
407	return (rtrn);
408}
409
410static int
411lm75_str_pol(char *buf)
412{
413	int len, rtrn;
414
415	rtrn = -1;
416	len = strlen(buf);
417	if (len > 1 && strncasecmp("high", buf, len) == 0)
418		rtrn = 1;
419	else if (len > 1 && strncasecmp("low", buf, len) == 0)
420		rtrn = 0;
421	else if (len > 8 && strncasecmp("active-high", buf, len) == 0)
422		rtrn = 1;
423	else if (len > 8 && strncasecmp("active-low", buf, len) == 0)
424		rtrn = 0;
425
426	return (rtrn);
427}
428
429static int
430lm75_temp_sysctl(SYSCTL_HANDLER_ARGS)
431{
432	device_t dev;
433	int error, temp;
434	struct lm75_softc *sc;
435	uint8_t reg;
436
437	dev = (device_t)arg1;
438	reg = (uint8_t)arg2;
439	sc = device_get_softc(dev);
440
441	if (lm75_temp_read(sc, reg, &temp) != 0)
442		return (EIO);
443
444	error = sysctl_handle_int(oidp, &temp, 0, req);
445	if (error != 0 || req->newptr == NULL)
446		return (error);
447
448	if (lm75_temp_write(sc, reg, temp) != 0)
449		return (EIO);
450
451	return (error);
452}
453
454static int
455lm75_faults_sysctl(SYSCTL_HANDLER_ARGS)
456{
457	device_t dev;
458	int lm75_faults[] = { 1, 2, 4, 6 };
459	int error, faults, i, newf, tmp;
460	struct lm75_softc *sc;
461
462	dev = (device_t)arg1;
463	sc = device_get_softc(dev);
464	tmp = (sc->sc_conf & LM75_CONF_FAULT) >> LM75_CONF_FSHIFT;
465	if (tmp >= nitems(lm75_faults))
466		tmp = nitems(lm75_faults) - 1;
467	faults = lm75_faults[tmp];
468
469	error = sysctl_handle_int(oidp, &faults, 0, req);
470	if (error != 0 || req->newptr == NULL)
471		return (error);
472
473	if (faults != lm75_faults[tmp]) {
474		newf = 0;
475		for (i = 0; i < nitems(lm75_faults); i++)
476			if (faults >= lm75_faults[i])
477				newf = i;
478		sc->sc_conf &= ~LM75_CONF_FAULT;
479		sc->sc_conf |= newf << LM75_CONF_FSHIFT;
480		if (lm75_conf_write(sc) != 0)
481			return (EIO);
482	}
483
484	return (error);
485}
486
487static int
488lm75_mode_sysctl(SYSCTL_HANDLER_ARGS)
489{
490	char buf[16];
491	device_t dev;
492	int error, mode, newm;
493	struct lm75_softc *sc;
494
495	dev = (device_t)arg1;
496	sc = device_get_softc(dev);
497	if (sc->sc_conf & LM75_CONF_MODE) {
498		mode = 1;
499		strlcpy(buf, "interrupt", sizeof(buf));
500	} else {
501		mode = 0;
502		strlcpy(buf, "comparator", sizeof(buf));
503	}
504
505	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
506	if (error != 0 || req->newptr == NULL)
507		return (error);
508
509	newm = lm75_str_mode(buf);
510	if (newm != -1 && mode != newm) {
511		sc->sc_conf &= ~LM75_CONF_MODE;
512		if (newm == 1)
513			sc->sc_conf |= LM75_CONF_MODE;
514		if (lm75_conf_write(sc) != 0)
515			return (EIO);
516	}
517
518	return (error);
519}
520
521static int
522lm75_pol_sysctl(SYSCTL_HANDLER_ARGS)
523{
524	char buf[16];
525	device_t dev;
526	int error, newp, pol;
527	struct lm75_softc *sc;
528
529	dev = (device_t)arg1;
530	sc = device_get_softc(dev);
531	if (sc->sc_conf & LM75_CONF_POL) {
532		pol = 1;
533		strlcpy(buf, "active-high", sizeof(buf));
534	} else {
535		pol = 0;
536		strlcpy(buf, "active-low", sizeof(buf));
537	}
538
539	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
540	if (error != 0 || req->newptr == NULL)
541		return (error);
542
543	newp = lm75_str_pol(buf);
544	if (newp != -1 && pol != newp) {
545		sc->sc_conf &= ~LM75_CONF_POL;
546		if (newp == 1)
547			sc->sc_conf |= LM75_CONF_POL;
548		if (lm75_conf_write(sc) != 0)
549			return (EIO);
550	}
551
552	return (error);
553}
554
555static int
556lm75_shutdown_sysctl(SYSCTL_HANDLER_ARGS)
557{
558	device_t dev;
559	int error, shutdown, tmp;
560	struct lm75_softc *sc;
561
562	dev = (device_t)arg1;
563	sc = device_get_softc(dev);
564	tmp = shutdown = (sc->sc_conf & LM75_CONF_SHUTD) ? 1 : 0;
565
566	error = sysctl_handle_int(oidp, &shutdown, 0, req);
567	if (error != 0 || req->newptr == NULL)
568		return (error);
569
570	if (shutdown != tmp) {
571		sc->sc_conf &= ~LM75_CONF_SHUTD;
572		if (shutdown)
573			sc->sc_conf |= LM75_CONF_SHUTD;
574		if (lm75_conf_write(sc) != 0)
575			return (EIO);
576	}
577
578	return (error);
579}
580