2 * Copyright (c) 2015 Michael Gmelin <freebsd@grem.de>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Driver for intersil I2C ISL29018 Digital Ambient Light Sensor and Proximity
32 * Sensor with Interrupt Function, only tested connected over SMBus (ig4iic).
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
39 #include <sys/param.h>
42 #include <sys/event.h>
43 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
46 #include <sys/lockmgr.h>
47 #include <sys/malloc.h>
49 #include <sys/module.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 #include <sys/systm.h>
56 #include <sys/vnode.h>
58 #include <dev/smbus/smbconf.h>
59 #include <dev/smbus/smbus.h>
60 #include <dev/isl/isl.h>
64 #include "device_if.h"
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
79 /* Returns < 0 on problem. */
80 static int isl_read_sensor(device_t dev, int addr, uint8_t cmd_mask);
83 * Initialize the device
86 init_device(device_t dev, int addr, int probe)
88 static char bl_init[] = { 0x00 };
93 bus = device_get_parent(dev); /* smbus */
96 * init procedure: send 0x00 to test ref and cmd reg 1
98 error = smbus_trans(bus, addr, REG_TEST,
99 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
100 bl_init, sizeof(bl_init), NULL, 0, NULL);
104 error = smbus_trans(bus, addr, REG_CMD1,
105 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
106 bl_init, sizeof(bl_init), NULL, 0, NULL);
110 pause("islinit", hz/100);
114 device_printf(dev, "Unable to initialize\n");
118 static int isl_probe(device_t);
119 static int isl_attach(device_t);
120 static int isl_detach(device_t);
122 static int isl_sysctl(SYSCTL_HANDLER_ARGS);
124 static devclass_t isl_devclass;
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),
135 static driver_t isl_driver = {
138 sizeof(struct isl_softc),
142 isl_probe(device_t dev)
147 addr = smbus_get_addr(dev);
150 * 0x44 - isl ambient light sensor on the acer c720.
151 * (other devices might use other ids).
156 error = init_device(dev, addr, 1);
160 device_set_desc(dev, "ISL Digital Ambient Light Sensor");
162 return (BUS_PROBE_VENDOR);
166 isl_attach(device_t dev)
168 struct isl_softc *sc;
169 struct sysctl_ctx_list *sysctl_ctx;
170 struct sysctl_oid *sysctl_tree;
176 sc = device_get_softc(dev);
181 addr = smbus_get_addr(dev);
183 if (init_device(dev, addr, 0))
186 sx_init(&sc->isl_sx, "ISL read lock");
191 sysctl_ctx = device_get_sysctl_ctx(dev);
192 sysctl_tree = device_get_sysctl_tree(dev);
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;
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");
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");
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");
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");
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");
238 isl_detach(device_t dev)
240 struct isl_softc *sc;
242 sc = device_get_softc(dev);
243 sx_destroy(&sc->isl_sx);
249 isl_sysctl(SYSCTL_HANDLER_ARGS)
251 static int resolutions[] = { 16, 12, 8, 4};
252 static int ranges[] = { 1000, 4000, 16000, 64000};
254 struct isl_softc *sc;
261 sc = (struct isl_softc *)oidp->oid_arg1;
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);
272 resolution = resolutions[(rbyte & CMD2_MASK_RESOLUTION)
273 >> CMD2_SHIFT_RESOLUTION];
274 range = ranges[(rbyte & CMD2_MASK_RANGE) >> CMD2_SHIFT_RANGE];
276 switch (oidp->oid_arg2) {
278 arg = (isl_read_sensor(sc->dev, sc->addr,
279 CMD1_MASK_ALS_ONCE) * range) >> resolution;
282 arg = isl_read_sensor(sc->dev, sc->addr,
285 case ISL_METHOD_PROX:
286 arg = isl_read_sensor(sc->dev, sc->addr,
287 CMD1_MASK_PROX_ONCE);
289 case ISL_METHOD_RESOLUTION:
290 arg = (1 << resolution);
292 case ISL_METHOD_RANGE:
296 sx_xunlock(&sc->isl_sx);
298 SYSCTL_OUT(req, &arg, sizeof(arg));
303 isl_read_sensor(device_t dev, int addr, uint8_t cmd_mask)
310 bus = device_get_parent(dev); /* smbus */
312 if (smbus_trans(bus, addr, REG_CMD1,
313 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
314 NULL, 0, &rbyte, sizeof(rbyte), NULL)) {
316 "Couldn't read first byte before issuing command %d\n",
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);
329 pause("islconv", hz/10);
331 if (smbus_trans(bus, addr, REG_DATA1,
332 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
333 NULL, 0, &rbyte, sizeof(rbyte), NULL)) {
335 "Couldn't read first byte after command %d\n", cmd_mask);
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);
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);