xref: /freebsd/sys/dev/ow/ow_temp.c (revision 2f513db7)
1 /*-
2  * Copyright (c) 2015 M. Warner Losh <imp@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice unmodified, this list of conditions, and the following
9  *    disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 
33 #include <sys/bus.h>
34 #include <sys/errno.h>
35 #include <sys/libkern.h>
36 #include <sys/kthread.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/sysctl.h>
42 
43 #include <dev/ow/ow.h>
44 #include <dev/ow/own.h>
45 
46 #define OWT_DS1820	0x10		/* Also 18S20 */
47 #define OWT_DS1822	0x22		/* Very close to 18B20 */
48 #define OWT_DS18B20	0x28		/* Also MAX31820 */
49 #define	OWT_DS1825	0x3B		/* Just like 18B20 with address bits */
50 
51 #define	CONVERT_T		0x44
52 #define COPY_SCRATCHPAD		0x48
53 #define WRITE_SCRATCHPAD	0x4e
54 #define READ_POWER_SUPPLY	0xb4
55 #define	RECALL_EE		0xb8
56 #define	READ_SCRATCHPAD		0xbe
57 
58 
59 #define	OW_TEMP_DONE		0x01
60 #define	OW_TEMP_RUNNING		0x02
61 
62 struct ow_temp_softc
63 {
64 	device_t	dev;
65 	int		type;
66 	int		temp;
67 	int		flags;
68 	int		bad_crc;
69 	int		bad_reads;
70 	int		reading_interval;
71 	int		parasite;
72 	struct mtx	temp_lock;
73 	struct proc	*event_thread;
74 };
75 
76 static int
77 ow_temp_probe(device_t dev)
78 {
79 
80 	switch (ow_get_family(dev)) {
81 	case OWT_DS1820:
82 		device_set_desc(dev, "One Wire Temperature");
83 		return BUS_PROBE_DEFAULT;
84 	case OWT_DS1822:
85 	case OWT_DS1825:
86 	case OWT_DS18B20:
87 		device_set_desc(dev, "Advanced One Wire Temperature");
88 		return BUS_PROBE_DEFAULT;
89 	default:
90 		return ENXIO;
91 	}
92 }
93 
94 static int
95 ow_temp_read_scratchpad(device_t dev, uint8_t *scratch, int len)
96 {
97 	struct ow_cmd cmd;
98 
99 	own_self_command(dev, &cmd, READ_SCRATCHPAD);
100 	cmd.xpt_read_len = len;
101 	own_command_wait(dev, &cmd);
102 	memcpy(scratch, cmd.xpt_read, len);
103 
104 	return 0;
105 }
106 
107 static int
108 ow_temp_convert_t(device_t dev)
109 {
110 	struct ow_cmd cmd;
111 
112 	own_self_command(dev, &cmd, CONVERT_T);
113 	own_command_wait(dev, &cmd);
114 
115 	return 0;
116 }
117 
118 
119 static int
120 ow_temp_read_power_supply(device_t dev, int *parasite)
121 {
122 	struct ow_cmd cmd;
123 
124 	own_self_command(dev, &cmd, READ_POWER_SUPPLY);
125 	cmd.flags |= OW_FLAG_READ_BIT;
126 	cmd.xpt_read_len = 1;
127 	own_command_wait(dev, &cmd);
128 	*parasite = !cmd.xpt_read[0];	/* parasites pull bus low */
129 
130 	return 0;
131 }
132 
133 static void
134 ow_temp_event_thread(void *arg)
135 {
136 	struct ow_temp_softc *sc;
137 	uint8_t scratch[8 + 1];
138 	uint8_t crc;
139 	int retries, rv, tmp;
140 
141 	sc = arg;
142 	pause("owtstart", device_get_unit(sc->dev) * hz / 100);	// 10ms stagger
143 	mtx_lock(&sc->temp_lock);
144 	sc->flags |= OW_TEMP_RUNNING;
145 	mtx_unlock(&sc->temp_lock);
146 	ow_temp_read_power_supply(sc->dev, &sc->parasite);
147 	if (sc->parasite)
148 		device_printf(sc->dev, "Running in parasitic mode unsupported\n");
149 	mtx_lock(&sc->temp_lock);
150 	while ((sc->flags & OW_TEMP_DONE) == 0) {
151 		mtx_unlock(&sc->temp_lock);
152 		ow_temp_convert_t(sc->dev);
153 		mtx_lock(&sc->temp_lock);
154 		msleep(sc, &sc->temp_lock, 0, "owtcvt", hz);
155 		if (sc->flags & OW_TEMP_DONE)
156 			break;
157 		mtx_unlock(&sc->temp_lock);
158 		for (retries = 5; retries > 0; retries--) {
159 			rv = ow_temp_read_scratchpad(sc->dev, scratch, sizeof(scratch));
160 			if (rv == 0) {
161 				crc = own_crc(sc->dev, scratch, sizeof(scratch) - 1);
162 				if (crc == scratch[8]) {
163 					if (sc->type == OWT_DS1820) {
164 						if (scratch[7]) {
165 							/*
166 							 * Formula from DS18S20 datasheet, page 6
167 							 * DS18S20 datasheet says count_per_c is 16, DS1820 does not
168 							 */
169 							tmp = (int16_t)((scratch[0] & 0xfe) |
170 							    (scratch[1] << 8)) << 3;
171 							tmp += 16 - scratch[6] - 4; /* count_per_c == 16 */
172 						} else
173 							tmp = (int16_t)(scratch[0] | (scratch[1] << 8)) << 3;
174 					} else
175 						tmp = (int16_t)(scratch[0] | (scratch[1] << 8));
176 					sc->temp = tmp * 1000 / 16 + 273150;
177 					break;
178 				}
179 				sc->bad_crc++;
180 			} else
181 				sc->bad_reads++;
182 		}
183 		mtx_lock(&sc->temp_lock);
184 		msleep(sc, &sc->temp_lock, 0, "owtcvt", sc->reading_interval);
185 	}
186 	sc->flags &= ~OW_TEMP_RUNNING;
187 	mtx_unlock(&sc->temp_lock);
188 	kproc_exit(0);
189 }
190 
191 static int
192 ow_temp_attach(device_t dev)
193 {
194 	struct ow_temp_softc *sc;
195 
196 	sc = device_get_softc(dev);
197 	sc->dev = dev;
198 	sc->type = ow_get_family(dev);
199 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
200 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
201 	    OID_AUTO, "temperature", CTLFLAG_RD | CTLTYPE_INT,
202 	    &sc->temp, 0, sysctl_handle_int,
203 	    "IK3", "Current Temperature");
204 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
205 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
206 	    OID_AUTO, "badcrc", CTLFLAG_RD,
207 	    &sc->bad_crc, 0,
208 	    "Number of Bad CRC on reading scratchpad");
209 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
210 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
211 	    OID_AUTO, "badread", CTLFLAG_RD,
212 	    &sc->bad_reads, 0,
213 	    "Number of errors on reading scratchpad");
214 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
215 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
216 	    OID_AUTO, "reading_interval", CTLFLAG_RW,
217 	    &sc->reading_interval, 0,
218 	    "ticks between reads");
219 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
220 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
221 	    OID_AUTO, "parasite", CTLFLAG_RW,
222 	    &sc->parasite, 0,
223 	    "In Parasite mode");
224 	/*
225 	 * Just do this for unit 0 to avoid locking
226 	 * the ow bus until that code can be put
227 	 * into place.
228 	 */
229 	sc->temp = -1;
230 	sc->reading_interval = 10 * hz;
231 	mtx_init(&sc->temp_lock, "lock for doing temperature", NULL, MTX_DEF);
232 	/* Start the thread */
233 	if (kproc_create(ow_temp_event_thread, sc, &sc->event_thread, 0, 0,
234 	    "%s event thread", device_get_nameunit(dev))) {
235 		device_printf(dev, "unable to create event thread.\n");
236 		panic("ow_temp_attach, can't create thread");
237 	}
238 
239 	return 0;
240 }
241 
242 static int
243 ow_temp_detach(device_t dev)
244 {
245 	struct ow_temp_softc *sc;
246 
247 	sc = device_get_softc(dev);
248 
249 	/*
250 	 * Wait for the thread to die.  kproc_exit will do a wakeup
251 	 * on the event thread's struct thread * so that we know it is
252 	 * safe to proceed.  IF the thread is running, set the please
253 	 * die flag and wait for it to comply.  Since the wakeup on
254 	 * the event thread happens only in kproc_exit, we don't
255 	 * need to loop here.
256 	 */
257 	mtx_lock(&sc->temp_lock);
258 	sc->flags |= OW_TEMP_DONE;
259 	while (sc->flags & OW_TEMP_RUNNING) {
260 		wakeup(sc);
261 		msleep(sc->event_thread, &sc->temp_lock, PWAIT, "owtun", 0);
262 	}
263 	mtx_destroy(&sc->temp_lock);
264 
265 	return 0;
266 }
267 
268 devclass_t ow_temp_devclass;
269 
270 static device_method_t ow_temp_methods[] = {
271 	/* Device interface */
272 	DEVMETHOD(device_probe,		ow_temp_probe),
273 	DEVMETHOD(device_attach,	ow_temp_attach),
274 	DEVMETHOD(device_detach,	ow_temp_detach),
275 
276 	{ 0, 0 }
277 };
278 
279 static driver_t ow_temp_driver = {
280 	"ow_temp",
281 	ow_temp_methods,
282 	sizeof(struct ow_temp_softc),
283 };
284 
285 DRIVER_MODULE(ow_temp, ow, ow_temp_driver, ow_temp_devclass, 0, 0);
286 MODULE_DEPEND(ow_temp, ow, 1, 1, 1);
287