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 <dev/iicbus/iiconf.h>
57 #include <dev/iicbus/iicbus.h>
58 #include <dev/isl/isl.h>
60 #include "iicbus_if.h"
62 #include "device_if.h"
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
75 /* Returns < 0 on problem. */
76 static int isl_read_sensor(device_t dev, uint8_t cmd_mask);
79 isl_read_byte(device_t dev, uint8_t reg, uint8_t *val)
81 uint16_t addr = iicbus_get_addr(dev);
82 struct iic_msg msgs[] = {
83 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
84 { addr, IIC_M_RD, 1, val },
87 return (iicbus_transfer(dev, msgs, nitems(msgs)));
91 isl_write_byte(device_t dev, uint8_t reg, uint8_t val)
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 },
99 return (iicbus_transfer(dev, msgs, nitems(msgs)));
103 * Initialize the device
106 init_device(device_t dev, int probe)
111 * init procedure: send 0x00 to test ref and cmd reg 1
113 error = isl_write_byte(dev, REG_TEST, 0);
116 error = isl_write_byte(dev, REG_CMD1, 0);
120 pause("islinit", hz/100);
124 device_printf(dev, "Unable to initialize\n");
128 static int isl_probe(device_t);
129 static int isl_attach(device_t);
130 static int isl_detach(device_t);
132 static int isl_sysctl(SYSCTL_HANDLER_ARGS);
134 static devclass_t isl_devclass;
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),
145 static driver_t isl_driver = {
148 sizeof(struct isl_softc),
153 isl_identify(driver_t *driver, device_t parent)
156 if (device_find_child(parent, "asl", -1)) {
158 printf("asl: device(s) already created\n");
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);
169 isl_probe(device_t dev)
171 uint32_t addr = iicbus_get_addr(dev);
175 if (init_device(dev, 1) != 0)
177 device_set_desc(dev, "ISL Digital Ambient Light Sensor");
178 return (BUS_PROBE_VENDOR);
182 isl_attach(device_t dev)
184 struct isl_softc *sc;
185 struct sysctl_ctx_list *sysctl_ctx;
186 struct sysctl_oid *sysctl_tree;
191 sc = device_get_softc(dev);
194 if (init_device(dev, 0) != 0)
197 sx_init(&sc->isl_sx, "ISL read lock");
199 sysctl_ctx = device_get_sysctl_ctx(dev);
200 sysctl_tree = device_get_sysctl_tree(dev);
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;
207 SYSCTL_ADD_PROC(sysctl_ctx,
208 SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
209 "als", CTLTYPE_INT | CTLFLAG_RD,
210 sc, ISL_METHOD_ALS, isl_sysctl, "I",
211 "Current ALS sensor read-out");
215 SYSCTL_ADD_PROC(sysctl_ctx,
216 SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
217 "ir", CTLTYPE_INT | CTLFLAG_RD,
218 sc, ISL_METHOD_IR, isl_sysctl, "I",
219 "Current IR sensor read-out");
223 SYSCTL_ADD_PROC(sysctl_ctx,
224 SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
225 "prox", CTLTYPE_INT | CTLFLAG_RD,
226 sc, ISL_METHOD_PROX, isl_sysctl, "I",
227 "Current proximity sensor read-out");
230 SYSCTL_ADD_PROC(sysctl_ctx,
231 SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
232 "resolution", CTLTYPE_INT | CTLFLAG_RD,
233 sc, ISL_METHOD_RESOLUTION, isl_sysctl, "I",
234 "Current proximity sensor resolution");
236 SYSCTL_ADD_PROC(sysctl_ctx,
237 SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
238 "range", CTLTYPE_INT | CTLFLAG_RD,
239 sc, ISL_METHOD_RANGE, isl_sysctl, "I",
240 "Current proximity sensor range");
246 isl_detach(device_t dev)
248 struct isl_softc *sc;
250 sc = device_get_softc(dev);
251 sx_destroy(&sc->isl_sx);
257 isl_sysctl(SYSCTL_HANDLER_ARGS)
259 static int resolutions[] = { 16, 12, 8, 4};
260 static int ranges[] = { 1000, 4000, 16000, 64000};
262 struct isl_softc *sc;
268 sc = (struct isl_softc *)oidp->oid_arg1;
271 sx_xlock(&sc->isl_sx);
272 if (isl_read_byte(sc->dev, REG_CMD2, &rbyte) != 0) {
273 sx_xunlock(&sc->isl_sx);
276 resolution = resolutions[(rbyte & CMD2_MASK_RESOLUTION)
277 >> CMD2_SHIFT_RESOLUTION];
278 range = ranges[(rbyte & CMD2_MASK_RANGE) >> CMD2_SHIFT_RANGE];
280 switch (oidp->oid_arg2) {
282 arg = (isl_read_sensor(sc->dev,
283 CMD1_MASK_ALS_ONCE) * range) >> resolution;
286 arg = isl_read_sensor(sc->dev, CMD1_MASK_IR_ONCE);
288 case ISL_METHOD_PROX:
289 arg = isl_read_sensor(sc->dev, CMD1_MASK_PROX_ONCE);
291 case ISL_METHOD_RESOLUTION:
292 arg = (1 << resolution);
294 case ISL_METHOD_RANGE:
298 sx_xunlock(&sc->isl_sx);
300 SYSCTL_OUT(req, &arg, sizeof(arg));
305 isl_read_sensor(device_t dev, uint8_t cmd_mask)
311 if (isl_read_byte(dev, REG_CMD1, &rbyte) != 0) {
313 "Couldn't read first byte before issuing command %d\n",
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);
324 pause("islconv", hz/10);
326 if (isl_read_byte(dev, REG_DATA1, &rbyte) != 0) {
328 "Couldn't read first byte after command %d\n", cmd_mask);
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);
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);