190792Sgshapiro/*-
2120256Sgshapiro * Copyright (c) 2015 Michael Gmelin <freebsd@grem.de>
390792Sgshapiro * All rights reserved.
490792Sgshapiro *
590792Sgshapiro * Redistribution and use in source and binary forms, with or without
690792Sgshapiro * modification, are permitted provided that the following conditions
790792Sgshapiro * are met:
890792Sgshapiro * 1. Redistributions of source code must retain the above copyright
990792Sgshapiro *    notice, this list of conditions and the following disclaimer.
1090792Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
1190792Sgshapiro *    notice, this list of conditions and the following disclaimer in the
1290792Sgshapiro *    documentation and/or other materials provided with the distribution.
1390792Sgshapiro *
1490792Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1590792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16120256Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1790792Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1890792Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1990792Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2090792Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2190792Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2290792Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2390792Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2490792Sgshapiro * SUCH DAMAGE.
2590792Sgshapiro */
2690792Sgshapiro
2790792Sgshapiro#include <sys/cdefs.h>
2890792Sgshapiro__FBSDID("$FreeBSD: releng/11.0/sys/dev/isl/isl.c 286918 2015-08-19 09:49:29Z grembo $");
2990792Sgshapiro
3090792Sgshapiro/*
3190792Sgshapiro * Driver for intersil I2C ISL29018 Digital Ambient Light Sensor and Proximity
3290792Sgshapiro * Sensor with Interrupt Function, only tested connected over SMBus (ig4iic).
3390792Sgshapiro *
3490792Sgshapiro * Datasheet:
3590792Sgshapiro * http://www.intersil.com/en/products/optoelectronics/ambient-light-and-proximity-sensors/light-to-digital-sensors/ISL29018.html
3690792Sgshapiro * http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29018.pdf
3790792Sgshapiro */
38120256Sgshapiro
3990792Sgshapiro#include <sys/param.h>
4090792Sgshapiro#include <sys/bus.h>
4190792Sgshapiro#include <sys/conf.h>
4290792Sgshapiro#include <sys/event.h>
4390792Sgshapiro#include <sys/fcntl.h>
4490792Sgshapiro#include <sys/kernel.h>
4590792Sgshapiro#include <sys/lock.h>
4690792Sgshapiro#include <sys/lockmgr.h>
4790792Sgshapiro#include <sys/malloc.h>
4890792Sgshapiro#include <sys/mbuf.h>
4990792Sgshapiro#include <sys/module.h>
5090792Sgshapiro#include <sys/poll.h>
5190792Sgshapiro#include <sys/sx.h>
5290792Sgshapiro#include <sys/sysctl.h>
5390792Sgshapiro#include <sys/systm.h>
5490792Sgshapiro#include <sys/systm.h>
5590792Sgshapiro#include <sys/uio.h>
5690792Sgshapiro#include <sys/vnode.h>
5790792Sgshapiro
5890792Sgshapiro#include <dev/smbus/smbconf.h>
5990792Sgshapiro#include <dev/smbus/smbus.h>
60120256Sgshapiro#include <dev/isl/isl.h>
61120256Sgshapiro
6290792Sgshapiro#include "smbus_if.h"
6390792Sgshapiro#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
72struct isl_softc {
73	device_t	dev;
74	int		addr;
75
76	struct sx	isl_sx;
77};
78
79/* Returns < 0 on problem. */
80static int isl_read_sensor(device_t dev, int addr, uint8_t cmd_mask);
81
82/*
83 * Initialize the device
84 */
85static int
86init_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
112done:
113	if (error)
114		device_printf(dev, "Unable to initialize\n");
115	return (error);
116}
117
118static int isl_probe(device_t);
119static int isl_attach(device_t);
120static int isl_detach(device_t);
121
122static int isl_sysctl(SYSCTL_HANDLER_ARGS);
123
124static devclass_t isl_devclass;
125
126static 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
135static driver_t isl_driver = {
136	"isl",
137	isl_methods,
138	sizeof(struct isl_softc),
139};
140
141static int
142isl_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
165static int
166isl_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
237static int
238isl_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
248static int
249isl_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
302static int
303isl_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
351DRIVER_MODULE(isl, smbus, isl_driver, isl_devclass, NULL, NULL);
352MODULE_DEPEND(isl, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
353MODULE_VERSION(isl, 1);
354