xref: /freebsd/sys/dev/isl/isl.c (revision 95d32ad641bd9d430e9eaeb0ac284acd81c17341)
1 /*-
2  * Copyright (c) 2015 Michael Gmelin <freebsd@grem.de>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following 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 AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Driver for intersil I2C ISL29018 Digital Ambient Light Sensor and Proximity
32  * Sensor with Interrupt Function, only tested connected over SMBus (ig4iic).
33  *
34  * Datasheet:
35  * http://www.intersil.com/en/products/optoelectronics/ambient-light-and-proximity-sensors/light-to-digital-sensors/ISL29018.html
36  * http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29018.pdf
37  */
38 
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/event.h>
43 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/lockmgr.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/module.h>
50 #include <sys/poll.h>
51 #include <sys/sx.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 #include <sys/systm.h>
55 
56 #include <dev/iicbus/iiconf.h>
57 #include <dev/iicbus/iicbus.h>
58 #include <dev/isl/isl.h>
59 
60 #include "iicbus_if.h"
61 #include "bus_if.h"
62 #include "device_if.h"
63 
64 #define ISL_METHOD_ALS		0x10
65 #define ISL_METHOD_IR		0x11
66 #define ISL_METHOD_PROX		0x12
67 #define ISL_METHOD_RESOLUTION	0x13
68 #define ISL_METHOD_RANGE	0x14
69 
70 struct isl_softc {
71 	device_t	dev;
72 	struct sx	isl_sx;
73 };
74 
75 /* Returns < 0 on problem. */
76 static int isl_read_sensor(device_t dev, uint8_t cmd_mask);
77 
78 static int
79 isl_read_byte(device_t dev, uint8_t reg, uint8_t *val)
80 {
81 	uint16_t addr = iicbus_get_addr(dev);
82 	struct iic_msg msgs[] = {
83 	     { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
84 	     { addr, IIC_M_RD, 1, val },
85 	};
86 
87 	return (iicbus_transfer(dev, msgs, nitems(msgs)));
88 }
89 
90 static int
91 isl_write_byte(device_t dev, uint8_t reg, uint8_t val)
92 {
93 	uint16_t addr = iicbus_get_addr(dev);
94 	uint8_t bytes[] = { reg, val };
95 	struct iic_msg msgs[] = {
96 	     { addr, IIC_M_WR, nitems(bytes), bytes },
97 	};
98 
99 	return (iicbus_transfer(dev, msgs, nitems(msgs)));
100 }
101 
102 /*
103  * Initialize the device
104  */
105 static int
106 init_device(device_t dev, int probe)
107 {
108 	int error;
109 
110 	/*
111 	 * init procedure: send 0x00 to test ref and cmd reg 1
112 	 */
113 	error = isl_write_byte(dev, REG_TEST, 0);
114 	if (error)
115 		goto done;
116 	error = isl_write_byte(dev, REG_CMD1, 0);
117 	if (error)
118 		goto done;
119 
120 	pause("islinit", hz/100);
121 
122 done:
123 	if (error && !probe)
124 		device_printf(dev, "Unable to initialize\n");
125 	return (error);
126 }
127 
128 static int isl_probe(device_t);
129 static int isl_attach(device_t);
130 static int isl_detach(device_t);
131 
132 static int isl_sysctl(SYSCTL_HANDLER_ARGS);
133 
134 static devclass_t isl_devclass;
135 
136 static device_method_t isl_methods[] = {
137 	/* device interface */
138 	DEVMETHOD(device_probe,		isl_probe),
139 	DEVMETHOD(device_attach,	isl_attach),
140 	DEVMETHOD(device_detach,	isl_detach),
141 
142 	DEVMETHOD_END
143 };
144 
145 static driver_t isl_driver = {
146 	"isl",
147 	isl_methods,
148 	sizeof(struct isl_softc),
149 };
150 
151 #if 0
152 static void
153 isl_identify(driver_t *driver, device_t parent)
154 {
155 
156 	if (device_find_child(parent, "asl", -1)) {
157 		if (bootverbose)
158 			printf("asl: device(s) already created\n");
159 		return;
160 	}
161 
162 	/* Check if we can communicate to our slave. */
163 	if (init_device(dev, 0x88, 1) == 0)
164 		BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "isl", -1);
165 }
166 #endif
167 
168 static int
169 isl_probe(device_t dev)
170 {
171 	uint32_t addr = iicbus_get_addr(dev);
172 
173 	if (addr != 0x88)
174 		return (ENXIO);
175 	if (init_device(dev, 1) != 0)
176 		return (ENXIO);
177 	device_set_desc(dev, "ISL Digital Ambient Light Sensor");
178 	return (BUS_PROBE_VENDOR);
179 }
180 
181 static int
182 isl_attach(device_t dev)
183 {
184 	struct isl_softc *sc;
185 	struct sysctl_ctx_list *sysctl_ctx;
186 	struct sysctl_oid *sysctl_tree;
187 	int use_als;
188 	int use_ir;
189 	int use_prox;
190 
191 	sc = device_get_softc(dev);
192 	sc->dev = dev;
193 
194 	if (init_device(dev, 0) != 0)
195 		return (ENXIO);
196 
197 	sx_init(&sc->isl_sx, "ISL read lock");
198 
199 	sysctl_ctx = device_get_sysctl_ctx(dev);
200 	sysctl_tree = device_get_sysctl_tree(dev);
201 
202 	use_als = isl_read_sensor(dev, CMD1_MASK_ALS_ONCE) >= 0;
203 	use_ir = isl_read_sensor(dev, CMD1_MASK_IR_ONCE) >= 0;
204 	use_prox = isl_read_sensor(dev, CMD1_MASK_PROX_ONCE) >= 0;
205 
206 	if (use_als) {
207 		SYSCTL_ADD_PROC(sysctl_ctx,
208 		    SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, "als",
209 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc,
210 		    ISL_METHOD_ALS, isl_sysctl, "I",
211 		    "Current ALS sensor read-out");
212 	}
213 
214 	if (use_ir) {
215 		SYSCTL_ADD_PROC(sysctl_ctx,
216 		    SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, "ir",
217 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc,
218 		    ISL_METHOD_IR, isl_sysctl, "I",
219 		    "Current IR sensor read-out");
220 	}
221 
222 	if (use_prox) {
223 		SYSCTL_ADD_PROC(sysctl_ctx,
224 		    SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, "prox",
225 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc,
226 		    ISL_METHOD_PROX, isl_sysctl, "I",
227 		    "Current proximity sensor read-out");
228 	}
229 
230 	SYSCTL_ADD_PROC(sysctl_ctx,
231 	    SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, "resolution",
232 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc,
233 	    ISL_METHOD_RESOLUTION, isl_sysctl, "I",
234 	    "Current proximity sensor resolution");
235 
236 	SYSCTL_ADD_PROC(sysctl_ctx,
237 	    SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, "range",
238 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc,
239 	    ISL_METHOD_RANGE, isl_sysctl, "I",
240 	    "Current proximity sensor range");
241 
242 	return (0);
243 }
244 
245 static int
246 isl_detach(device_t dev)
247 {
248 	struct isl_softc *sc;
249 
250 	sc = device_get_softc(dev);
251 	sx_destroy(&sc->isl_sx);
252 
253 	return (0);
254 }
255 
256 static int
257 isl_sysctl(SYSCTL_HANDLER_ARGS)
258 {
259 	static int resolutions[] = { 16, 12, 8, 4};
260 	static int ranges[] = { 1000, 4000, 16000, 64000};
261 
262 	struct isl_softc *sc;
263 	uint8_t rbyte;
264 	int arg;
265 	int resolution;
266 	int range;
267 
268 	sc = (struct isl_softc *)oidp->oid_arg1;
269 	arg = -1;
270 
271 	sx_xlock(&sc->isl_sx);
272 	if (isl_read_byte(sc->dev, REG_CMD2, &rbyte) != 0) {
273 		sx_xunlock(&sc->isl_sx);
274 		return (-1);
275 	}
276 	resolution = resolutions[(rbyte & CMD2_MASK_RESOLUTION)
277 			    >> CMD2_SHIFT_RESOLUTION];
278 	range = ranges[(rbyte & CMD2_MASK_RANGE) >> CMD2_SHIFT_RANGE];
279 
280 	switch (oidp->oid_arg2) {
281 	case ISL_METHOD_ALS:
282 		arg = (isl_read_sensor(sc->dev,
283 		    CMD1_MASK_ALS_ONCE) * range) >> resolution;
284 		break;
285 	case ISL_METHOD_IR:
286 		arg = isl_read_sensor(sc->dev, CMD1_MASK_IR_ONCE);
287 		break;
288 	case ISL_METHOD_PROX:
289 		arg = isl_read_sensor(sc->dev, CMD1_MASK_PROX_ONCE);
290 		break;
291 	case ISL_METHOD_RESOLUTION:
292 		arg = (1 << resolution);
293 		break;
294 	case ISL_METHOD_RANGE:
295 		arg = range;
296 		break;
297 	}
298 	sx_xunlock(&sc->isl_sx);
299 
300 	SYSCTL_OUT(req, &arg, sizeof(arg));
301 	return (0);
302 }
303 
304 static int
305 isl_read_sensor(device_t dev, uint8_t cmd_mask)
306 {
307 	uint8_t rbyte;
308 	uint8_t cmd;
309 	int ret;
310 
311 	if (isl_read_byte(dev, REG_CMD1, &rbyte) != 0) {
312 		device_printf(dev,
313 		    "Couldn't read first byte before issuing command %d\n",
314 		    cmd_mask);
315 		return (-1);
316 	}
317 
318 	cmd = (rbyte & 0x1f) | cmd_mask;
319 	if (isl_write_byte(dev, REG_CMD1, cmd) != 0) {
320 		device_printf(dev, "Couldn't write command %d\n", cmd_mask);
321 		return (-1);
322 	}
323 
324 	pause("islconv", hz/10);
325 
326 	if (isl_read_byte(dev, REG_DATA1, &rbyte) != 0) {
327 		device_printf(dev,
328 		    "Couldn't read first byte after command %d\n", cmd_mask);
329 		return (-1);
330 	}
331 
332 	ret = rbyte;
333 	if (isl_read_byte(dev, REG_DATA2, &rbyte) != 0) {
334 		device_printf(dev, "Couldn't read second byte after command %d\n", cmd_mask);
335 		return (-1);
336 	}
337 	ret += rbyte << 8;
338 
339 	return (ret);
340 }
341 
342 DRIVER_MODULE(isl, iicbus, isl_driver, isl_devclass, NULL, NULL);
343 MODULE_DEPEND(isl, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
344 MODULE_VERSION(isl, 1);
345