xref: /freebsd/sys/dev/isl/isl.c (revision bd18fd57db1df29da1a3adf94d47924a977a29c2)
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 #include <sys/uio.h>
56 #include <sys/vnode.h>
57 
58 #include <dev/smbus/smbconf.h>
59 #include <dev/smbus/smbus.h>
60 #include <dev/isl/isl.h>
61 
62 #include "smbus_if.h"
63 #include "bus_if.h"
64 #include "device_if.h"
65 
66 #define ISL_METHOD_ALS		0x10
67 #define ISL_METHOD_IR		0x11
68 #define ISL_METHOD_PROX		0x12
69 #define ISL_METHOD_RESOLUTION	0x13
70 #define ISL_METHOD_RANGE	0x14
71 
72 struct isl_softc {
73 	device_t	dev;
74 	int		addr;
75 
76 	struct sx	isl_sx;
77 };
78 
79 /* Returns < 0 on problem. */
80 static int isl_read_sensor(device_t dev, int addr, uint8_t cmd_mask);
81 
82 /*
83  * Initialize the device
84  */
85 static int
86 init_device(device_t dev, int addr, int probe)
87 {
88 	static char bl_init[] = { 0x00 };
89 
90 	device_t bus;
91 	int error;
92 
93 	bus = device_get_parent(dev);	/* smbus */
94 
95 	/*
96 	 * init procedure: send 0x00 to test ref and cmd reg 1
97 	 */
98 	error = smbus_trans(bus, addr, REG_TEST,
99 			    SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
100 			    bl_init, sizeof(bl_init), NULL, 0, NULL);
101 	if (error)
102 		goto done;
103 
104 	error = smbus_trans(bus, addr, REG_CMD1,
105 			    SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
106 			    bl_init, sizeof(bl_init), NULL, 0, NULL);
107 	if (error)
108 		goto done;
109 
110 	pause("islinit", hz/100);
111 
112 done:
113 	if (error)
114 		device_printf(dev, "Unable to initialize\n");
115 	return (error);
116 }
117 
118 static int isl_probe(device_t);
119 static int isl_attach(device_t);
120 static int isl_detach(device_t);
121 
122 static int isl_sysctl(SYSCTL_HANDLER_ARGS);
123 
124 static devclass_t isl_devclass;
125 
126 static device_method_t isl_methods[] = {
127 	/* device interface */
128 	DEVMETHOD(device_probe,		isl_probe),
129 	DEVMETHOD(device_attach,	isl_attach),
130 	DEVMETHOD(device_detach,	isl_detach),
131 
132 	DEVMETHOD_END
133 };
134 
135 static driver_t isl_driver = {
136 	"isl",
137 	isl_methods,
138 	sizeof(struct isl_softc),
139 };
140 
141 static int
142 isl_probe(device_t dev)
143 {
144 	int addr;
145 	int error;
146 
147 	addr = smbus_get_addr(dev);
148 
149 	/*
150 	 * 0x44 - isl ambient light sensor on the acer c720.
151 	 * (other devices might use other ids).
152 	 */
153 	if (addr != 0x44)
154 		return (ENXIO);
155 
156 	error = init_device(dev, addr, 1);
157 	if (error)
158 		return (ENXIO);
159 
160 	device_set_desc(dev, "ISL Digital Ambient Light Sensor");
161 
162 	return (BUS_PROBE_VENDOR);
163 }
164 
165 static int
166 isl_attach(device_t dev)
167 {
168 	struct isl_softc *sc;
169 	struct sysctl_ctx_list *sysctl_ctx;
170 	struct sysctl_oid *sysctl_tree;
171 	int addr;
172 	int use_als;
173 	int use_ir;
174 	int use_prox;
175 
176 	sc = device_get_softc(dev);
177 
178 	if (!sc)
179 		return (ENOMEM);
180 
181 	addr = smbus_get_addr(dev);
182 
183 	if (init_device(dev, addr, 0))
184 		return (ENXIO);
185 
186 	sx_init(&sc->isl_sx, "ISL read lock");
187 
188 	sc->dev = dev;
189 	sc->addr = addr;
190 
191 	sysctl_ctx = device_get_sysctl_ctx(dev);
192 	sysctl_tree = device_get_sysctl_tree(dev);
193 
194 	use_als = isl_read_sensor(dev, addr, CMD1_MASK_ALS_ONCE) >= 0;
195 	use_ir = isl_read_sensor(dev, addr, CMD1_MASK_IR_ONCE) >= 0;
196 	use_prox = isl_read_sensor(dev, addr, CMD1_MASK_PROX_ONCE) >= 0;
197 
198 	if (use_als) {
199 		SYSCTL_ADD_PROC(sysctl_ctx,
200 	 		SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
201 			    "als", CTLTYPE_INT | CTLFLAG_RD,
202 			    sc, ISL_METHOD_ALS, isl_sysctl, "I",
203 			    "Current ALS sensor read-out");
204 	}
205 
206 	if (use_ir) {
207 		SYSCTL_ADD_PROC(sysctl_ctx,
208 			SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
209 			    "ir", CTLTYPE_INT | CTLFLAG_RD,
210 			    sc, ISL_METHOD_IR, isl_sysctl, "I",
211 			    "Current IR sensor read-out");
212 	}
213 
214 	if (use_prox) {
215 		SYSCTL_ADD_PROC(sysctl_ctx,
216 			SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
217 			    "prox", CTLTYPE_INT | CTLFLAG_RD,
218 			    sc, ISL_METHOD_PROX, isl_sysctl, "I",
219 			    "Current proximity sensor read-out");
220 	}
221 
222 	SYSCTL_ADD_PROC(sysctl_ctx,
223 		SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
224 		    "resolution", CTLTYPE_INT | CTLFLAG_RD,
225 		    sc, ISL_METHOD_RESOLUTION, isl_sysctl, "I",
226 		    "Current proximity sensor resolution");
227 
228 	SYSCTL_ADD_PROC(sysctl_ctx,
229 	SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
230 	    "range", CTLTYPE_INT | CTLFLAG_RD,
231 	    sc, ISL_METHOD_RANGE, isl_sysctl, "I",
232 	    "Current proximity sensor range");
233 
234 	return (0);
235 }
236 
237 static int
238 isl_detach(device_t dev)
239 {
240 	struct isl_softc *sc;
241 
242 	sc = device_get_softc(dev);
243 	sx_destroy(&sc->isl_sx);
244 
245 	return (0);
246 }
247 
248 static int
249 isl_sysctl(SYSCTL_HANDLER_ARGS)
250 {
251 	static int resolutions[] = { 16, 12, 8, 4};
252 	static int ranges[] = { 1000, 4000, 16000, 64000};
253 
254 	struct isl_softc *sc;
255 	device_t bus;
256 	uint8_t rbyte;
257 	int arg;
258 	int resolution;
259 	int range;
260 
261 	sc = (struct isl_softc *)oidp->oid_arg1;
262 	arg = -1;
263 
264 	sx_xlock(&sc->isl_sx);
265 	bus = device_get_parent(sc->dev);	/* smbus */
266 	if (smbus_trans(bus, sc->addr, REG_CMD2,
267 	    SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
268 	    NULL, 0, &rbyte, sizeof(rbyte), NULL)) {
269 		sx_xunlock(&sc->isl_sx);
270 		return (-1);
271 	}
272 	resolution = resolutions[(rbyte & CMD2_MASK_RESOLUTION)
273 			    >> CMD2_SHIFT_RESOLUTION];
274 	range = ranges[(rbyte & CMD2_MASK_RANGE) >> CMD2_SHIFT_RANGE];
275 
276 	switch (oidp->oid_arg2) {
277 	case ISL_METHOD_ALS:
278 		arg = (isl_read_sensor(sc->dev, sc->addr,
279 		    CMD1_MASK_ALS_ONCE) * range) >> resolution;
280 		break;
281 	case ISL_METHOD_IR:
282 		arg = isl_read_sensor(sc->dev, sc->addr,
283 		    CMD1_MASK_IR_ONCE);
284 		break;
285 	case ISL_METHOD_PROX:
286 		arg = isl_read_sensor(sc->dev, sc->addr,
287 		    CMD1_MASK_PROX_ONCE);
288 		break;
289 	case ISL_METHOD_RESOLUTION:
290 		arg = (1 << resolution);
291 		break;
292 	case ISL_METHOD_RANGE:
293 		arg = range;
294 		break;
295 	}
296 	sx_xunlock(&sc->isl_sx);
297 
298 	SYSCTL_OUT(req, &arg, sizeof(arg));
299 	return (0);
300 }
301 
302 static int
303 isl_read_sensor(device_t dev, int addr, uint8_t cmd_mask)
304 {
305 	device_t bus;
306 	uint8_t rbyte;
307 	uint8_t cmd;
308 	int ret;
309 
310 	bus = device_get_parent(dev);	/* smbus */
311 
312 	if (smbus_trans(bus, addr, REG_CMD1,
313 	    SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
314 	    NULL, 0, &rbyte, sizeof(rbyte), NULL)) {
315 		device_printf(dev,
316 		    "Couldn't read first byte before issuing command %d\n",
317 		    cmd_mask);
318 		return (-1);
319 	}
320 
321 	cmd = (rbyte & 0x1f) | cmd_mask;
322 	if (smbus_trans(bus, addr, REG_CMD1,
323 	    SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
324 	    &cmd, sizeof(cmd), NULL, 0, NULL)) {
325 		device_printf(dev, "Couldn't write command %d\n", cmd_mask);
326 		return (-1);
327 	}
328 
329 	pause("islconv", hz/10);
330 
331 	if (smbus_trans(bus, addr, REG_DATA1,
332 	    SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
333 	    NULL, 0, &rbyte, sizeof(rbyte), NULL)) {
334 		device_printf(dev,
335 		    "Couldn't read first byte after command %d\n", cmd_mask);
336 		return (-1);
337 	}
338 
339 	ret = rbyte;
340 	if (smbus_trans(bus, addr, REG_DATA2,
341 	    SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
342 	    NULL, 0, &rbyte, sizeof(rbyte), NULL)) {
343 		device_printf(dev, "Couldn't read second byte after command %d\n", cmd_mask);
344 		return (-1);
345 	}
346 	ret += rbyte << 8;
347 
348 	return (ret);
349 }
350 
351 DRIVER_MODULE(isl, smbus, isl_driver, isl_devclass, NULL, NULL);
352 MODULE_DEPEND(isl, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
353 MODULE_VERSION(isl, 1);
354