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