tsl2772.c 44.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2
/*
3
 * Device driver for monitoring ambient light intensity in (lux) and proximity
4 5
 * detection (prox) within the TAOS TSL2571, TSL2671, TMD2671, TSL2771, TMD2771,
 * TSL2572, TSL2672, TMD2672, TSL2772, and TMD2772 devices.
6 7
 *
 * Copyright (c) 2012, TAOS Corporation.
8
 * Copyright (c) 2017-2018 Brian Masney <masneyb@onstation.org>
9 10 11
 */

#include <linux/delay.h>
12 13
#include <linux/errno.h>
#include <linux/i2c.h>
14
#include <linux/interrupt.h>
15
#include <linux/kernel.h>
16
#include <linux/module.h>
17 18
#include <linux/mutex.h>
#include <linux/slab.h>
19 20 21
#include <linux/iio/events.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
22
#include <linux/platform_data/tsl2772.h>
23

24
/* Cal defs */
25 26 27
#define PROX_STAT_CAL			0
#define PROX_STAT_SAMP			1
#define MAX_SAMPLES_CAL			200
28

29
/* TSL2772 Device ID */
30 31 32
#define TRITON_ID			0x00
#define SWORDFISH_ID			0x30
#define HALIBUT_ID			0x20
33 34

/* Lux calculation constants */
35
#define TSL2772_LUX_CALC_OVER_FLOW	65535
36

37 38 39
/*
 * TAOS Register definitions - Note: depending on device, some of these register
 * are not used and the register address is benign.
40
 */
41

42 43
/* Register offsets */
#define TSL2772_MAX_CONFIG_REG		16
44 45

/* Device Registers and Masks */
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#define TSL2772_CNTRL			0x00
#define TSL2772_ALS_TIME		0X01
#define TSL2772_PRX_TIME		0x02
#define TSL2772_WAIT_TIME		0x03
#define TSL2772_ALS_MINTHRESHLO		0X04
#define TSL2772_ALS_MINTHRESHHI		0X05
#define TSL2772_ALS_MAXTHRESHLO		0X06
#define TSL2772_ALS_MAXTHRESHHI		0X07
#define TSL2772_PRX_MINTHRESHLO		0X08
#define TSL2772_PRX_MINTHRESHHI		0X09
#define TSL2772_PRX_MAXTHRESHLO		0X0A
#define TSL2772_PRX_MAXTHRESHHI		0X0B
#define TSL2772_PERSISTENCE		0x0C
#define TSL2772_ALS_PRX_CONFIG		0x0D
#define TSL2772_PRX_COUNT		0x0E
#define TSL2772_GAIN			0x0F
#define TSL2772_NOTUSED			0x10
#define TSL2772_REVID			0x11
#define TSL2772_CHIPID			0x12
#define TSL2772_STATUS			0x13
#define TSL2772_ALS_CHAN0LO		0x14
#define TSL2772_ALS_CHAN0HI		0x15
#define TSL2772_ALS_CHAN1LO		0x16
#define TSL2772_ALS_CHAN1HI		0x17
#define TSL2772_PRX_LO			0x18
#define TSL2772_PRX_HI			0x19

/* tsl2772 cmd reg masks */
#define TSL2772_CMD_REG			0x80
#define TSL2772_CMD_SPL_FN		0x60
#define TSL2772_CMD_REPEAT_PROTO	0x00
#define TSL2772_CMD_AUTOINC_PROTO	0x20

#define TSL2772_CMD_PROX_INT_CLR	0X05
#define TSL2772_CMD_ALS_INT_CLR		0x06
#define TSL2772_CMD_PROXALS_INT_CLR	0X07

/* tsl2772 cntrl reg masks */
#define TSL2772_CNTL_ADC_ENBL		0x02
#define TSL2772_CNTL_PWR_ON		0x01

/* tsl2772 status reg masks */
#define TSL2772_STA_ADC_VALID		0x01
#define TSL2772_STA_PRX_VALID		0x02
#define TSL2772_STA_ADC_PRX_VALID	(TSL2772_STA_ADC_VALID | \
					 TSL2772_STA_PRX_VALID)
#define TSL2772_STA_ALS_INTR		0x10
#define TSL2772_STA_PRX_INTR		0x20

/* tsl2772 cntrl reg masks */
#define TSL2772_CNTL_REG_CLEAR		0x00
#define TSL2772_CNTL_PROX_INT_ENBL	0X20
#define TSL2772_CNTL_ALS_INT_ENBL	0X10
#define TSL2772_CNTL_WAIT_TMR_ENBL	0X08
#define TSL2772_CNTL_PROX_DET_ENBL	0X04
#define TSL2772_CNTL_PWRON		0x01
#define TSL2772_CNTL_ALSPON_ENBL	0x03
#define TSL2772_CNTL_INTALSPON_ENBL	0x13
#define TSL2772_CNTL_PROXPON_ENBL	0x0F
#define TSL2772_CNTL_INTPROXPON_ENBL	0x2F

#define TSL2772_ALS_GAIN_TRIM_MIN	250
#define TSL2772_ALS_GAIN_TRIM_MAX	4000

/* Device family members */
111 112 113 114 115 116 117 118 119 120 121 122 123 124
enum {
	tsl2571,
	tsl2671,
	tmd2671,
	tsl2771,
	tmd2771,
	tsl2572,
	tsl2672,
	tmd2672,
	tsl2772,
	tmd2772
};

enum {
125 126 127
	TSL2772_CHIP_UNKNOWN = 0,
	TSL2772_CHIP_WORKING = 1,
	TSL2772_CHIP_SUSPENDED = 2
128 129 130
};

/* Per-device data */
131
struct tsl2772_als_info {
132 133 134 135 136
	u16 als_ch0;
	u16 als_ch1;
	u16 lux;
};

137
struct tsl2772_chip_info {
138
	int chan_table_elements;
139 140 141
	struct iio_chan_spec channel_with_events[4];
	struct iio_chan_spec channel_without_events[4];
	const struct iio_info *info;
142 143
};

144
struct tsl2772_chip {
145 146 147 148 149
	kernel_ulong_t id;
	struct mutex prox_mutex;
	struct mutex als_mutex;
	struct i2c_client *client;
	u16 prox_data;
150 151 152
	struct tsl2772_als_info als_cur_info;
	struct tsl2772_settings settings;
	struct tsl2772_platform_data *pdata;
153
	int als_gain_time_scale;
154
	int als_saturation;
155 156 157
	int tsl2772_chip_status;
	u8 tsl2772_config[TSL2772_MAX_CONFIG_REG];
	const struct tsl2772_chip_info	*chip_info;
158 159
	const struct iio_info *info;
	s64 event_timestamp;
160 161 162 163 164
	/*
	 * This structure is intentionally large to accommodate
	 * updates via sysfs.
	 * Sized to 9 = max 8 segments + 1 termination segment
	 */
165
	struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
166 167
};

168 169 170 171 172 173 174 175
/*
 * Different devices require different coefficents, and these numbers were
 * derived from the 'Lux Equation' section of the various device datasheets.
 * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
 * The coefficients are multiplied by 1000 to avoid floating point operations.
 * The two rows in each table correspond to the Lux1 and Lux2 equations from
 * the datasheets.
 */
176
static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
177 178 179
	{ 53000, 106000 },
	{ 31800,  53000 },
	{ 0,          0 },
180 181
};

182
static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
183 184 185
	{ 24000,  48000 },
	{ 14400,  24000 },
	{ 0,          0 },
186 187
};

188
static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
189 190 191
	{ 60000, 112200 },
	{ 37800,  60000 },
	{     0,      0 },
192 193
};

194
static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
195 196 197
	{ 20000,  35000 },
	{ 12600,  20000 },
	{     0,      0 },
198 199
};

200
static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = {
201 202 203 204 205 206 207 208 209 210
	[tsl2571] = tsl2x71_lux_table,
	[tsl2671] = tsl2x71_lux_table,
	[tmd2671] = tmd2x71_lux_table,
	[tsl2771] = tsl2x71_lux_table,
	[tmd2771] = tmd2x71_lux_table,
	[tsl2572] = tsl2x72_lux_table,
	[tsl2672] = tsl2x72_lux_table,
	[tmd2672] = tmd2x72_lux_table,
	[tsl2772] = tsl2x72_lux_table,
	[tmd2772] = tmd2x72_lux_table,
211 212
};

213
static const struct tsl2772_settings tsl2772_default_settings = {
214
	.als_time = 255, /* 2.72 / 2.73 ms */
215
	.als_gain = 0,
216
	.prox_time = 255, /* 2.72 / 2.73 ms */
217
	.prox_gain = 0,
218
	.wait_time = 255,
219
	.als_prox_config = 0,
220 221
	.als_gain_trim = 1000,
	.als_cal_target = 150,
222
	.als_persistence = 1,
223
	.als_interrupt_en = false,
224 225
	.als_thresh_low = 200,
	.als_thresh_high = 256,
226
	.prox_persistence = 1,
227
	.prox_interrupt_en = false,
228 229 230
	.prox_thres_low  = 0,
	.prox_thres_high = 512,
	.prox_max_samples_cal = 30,
231
	.prox_pulse_count = 8,
232 233
	.prox_diode = TSL2772_DIODE1,
	.prox_power = TSL2772_100_mA
234 235
};

236
static const s16 tsl2772_als_gain[] = {
237 238 239 240 241 242
	1,
	8,
	16,
	120
};

243
static const s16 tsl2772_prox_gain[] = {
244 245 246 247 248 249
	1,
	2,
	4,
	8
};

250
static const int tsl2772_int_time_avail[][6] = {
251 252 253 254 255 256 257 258 259 260 261 262
	[tsl2571] = { 0, 2720, 0, 2720, 0, 696000 },
	[tsl2671] = { 0, 2720, 0, 2720, 0, 696000 },
	[tmd2671] = { 0, 2720, 0, 2720, 0, 696000 },
	[tsl2771] = { 0, 2720, 0, 2720, 0, 696000 },
	[tmd2771] = { 0, 2720, 0, 2720, 0, 696000 },
	[tsl2572] = { 0, 2730, 0, 2730, 0, 699000 },
	[tsl2672] = { 0, 2730, 0, 2730, 0, 699000 },
	[tmd2672] = { 0, 2730, 0, 2730, 0, 699000 },
	[tsl2772] = { 0, 2730, 0, 2730, 0, 699000 },
	[tmd2772] = { 0, 2730, 0, 2730, 0, 699000 },
};

263
static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
264

265
static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
266

267 268 269 270 271 272 273 274 275
/* Channel variations */
enum {
	ALS,
	PRX,
	ALSPRX,
	PRX2,
	ALSPRX2,
};

J
Jon Brenner 已提交
276
static const u8 device_channel_config[] = {
277 278 279 280 281 282 283 284 285 286
	[tsl2571] = ALS,
	[tsl2671] = PRX,
	[tmd2671] = PRX,
	[tsl2771] = ALSPRX,
	[tmd2771] = ALSPRX,
	[tsl2572] = ALS,
	[tsl2672] = PRX2,
	[tmd2672] = PRX2,
	[tsl2772] = ALSPRX2,
	[tmd2772] = ALSPRX2
287 288
};

289
static int tsl2772_read_status(struct tsl2772_chip *chip)
290 291 292 293
{
	int ret;

	ret = i2c_smbus_read_byte_data(chip->client,
294
				       TSL2772_CMD_REG | TSL2772_STATUS);
295 296 297 298 299 300 301 302
	if (ret < 0)
		dev_err(&chip->client->dev,
			"%s: failed to read STATUS register: %d\n", __func__,
			ret);

	return ret;
}

303
static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data)
304 305 306 307
{
	int ret;

	ret = i2c_smbus_write_byte_data(chip->client,
308
					TSL2772_CMD_REG | TSL2772_CNTRL, data);
309 310 311 312 313 314 315 316 317
	if (ret < 0) {
		dev_err(&chip->client->dev,
			"%s: failed to write to control register %x: %d\n",
			__func__, data, ret);
	}

	return ret;
}

318
static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg,
319 320 321 322 323 324
				     int upper_reg)
{
	u8 buf[2];
	int ret;

	ret = i2c_smbus_write_byte(chip->client,
325
				   TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO |
326 327 328 329 330 331 332 333 334
				   lower_reg);
	if (ret < 0) {
		dev_err(&chip->client->dev,
			"%s: failed to enable auto increment protocol: %d\n",
			__func__, ret);
		return ret;
	}

	ret = i2c_smbus_read_byte_data(chip->client,
335
				       TSL2772_CMD_REG | lower_reg);
336 337 338 339 340 341 342 343 344
	if (ret < 0) {
		dev_err(&chip->client->dev,
			"%s: failed to read from register %x: %d\n", __func__,
			lower_reg, ret);
		return ret;
	}
	buf[0] = ret;

	ret = i2c_smbus_read_byte_data(chip->client,
345
				       TSL2772_CMD_REG | upper_reg);
346 347 348 349 350 351 352 353 354
	if (ret < 0) {
		dev_err(&chip->client->dev,
			"%s: failed to read from register %x: %d\n", __func__,
			upper_reg, ret);
		return ret;
	}
	buf[1] = ret;

	ret = i2c_smbus_write_byte(chip->client,
355
				   TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO |
356 357 358 359 360 361 362 363 364 365 366
				   lower_reg);
	if (ret < 0) {
		dev_err(&chip->client->dev,
			"%s: failed to enable repeated byte protocol: %d\n",
			__func__, ret);
		return ret;
	}

	return le16_to_cpup((const __le16 *)&buf[0]);
}

367
/**
368
 * tsl2772_get_lux() - Reads and calculates current lux value.
369 370 371
 * @indio_dev:	pointer to IIO device
 *
 * The raw ch0 and ch1 values of the ambient light sensed in the last
372 373 374 375 376
 * integration cycle are read from the device. The raw values are multiplied
 * by a device-specific scale factor, and divided by the integration time and
 * device gain. The code supports multiple lux equations through the lux table
 * coefficients. A lux gain trim is applied to each lux equation, and then the
 * maximum lux within the interval 0..65535 is selected.
377
 */
378
static int tsl2772_get_lux(struct iio_dev *indio_dev)
379
{
380 381
	struct tsl2772_chip *chip = iio_priv(indio_dev);
	struct tsl2772_lux *p;
382 383
	int max_lux, ret;
	bool overflow;
384

385
	mutex_lock(&chip->als_mutex);
386

387
	if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) {
388
		dev_err(&chip->client->dev, "%s: device is not enabled\n",
389
			__func__);
390
		ret = -EBUSY;
391 392 393
		goto out_unlock;
	}

394
	ret = tsl2772_read_status(chip);
395
	if (ret < 0)
396
		goto out_unlock;
397

398
	if (!(ret & TSL2772_STA_ADC_VALID)) {
399 400 401 402 403 404
		dev_err(&chip->client->dev,
			"%s: data not valid yet\n", __func__);
		ret = chip->als_cur_info.lux; /* return LAST VALUE */
		goto out_unlock;
	}

405 406
	ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO,
					TSL2772_ALS_CHAN0HI);
407 408 409
	if (ret < 0)
		goto out_unlock;
	chip->als_cur_info.als_ch0 = ret;
410

411 412
	ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO,
					TSL2772_ALS_CHAN1HI);
413 414 415
	if (ret < 0)
		goto out_unlock;
	chip->als_cur_info.als_ch1 = ret;
416

417
	if (chip->als_cur_info.als_ch0 >= chip->als_saturation) {
418
		max_lux = TSL2772_LUX_CALC_OVER_FLOW;
419
		goto update_struct_with_max_lux;
420 421
	}

422
	if (!chip->als_cur_info.als_ch0) {
423 424 425 426
		/* have no data, so return LAST VALUE */
		ret = chip->als_cur_info.lux;
		goto out_unlock;
	}
427

428 429
	max_lux = 0;
	overflow = false;
430
	for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0;
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	     p++) {
		int lux;

		lux = ((chip->als_cur_info.als_ch0 * p->ch0) -
		       (chip->als_cur_info.als_ch1 * p->ch1)) /
			chip->als_gain_time_scale;

		/*
		 * The als_gain_trim can have a value within the range 250..4000
		 * and is a multiplier for the lux. A trim of 1000 makes no
		 * changes to the lux, less than 1000 scales it down, and
		 * greater than 1000 scales it up.
		 */
		lux = (lux * chip->settings.als_gain_trim) / 1000;

446
		if (lux > TSL2772_LUX_CALC_OVER_FLOW) {
447 448 449
			overflow = true;
			continue;
		}
450

451 452
		max_lux = max(max_lux, lux);
	}
453

454
	if (overflow && max_lux == 0)
455
		max_lux = TSL2772_LUX_CALC_OVER_FLOW;
456

457 458 459
update_struct_with_max_lux:
	chip->als_cur_info.lux = max_lux;
	ret = max_lux;
460 461 462 463 464 465 466 467

out_unlock:
	mutex_unlock(&chip->als_mutex);

	return ret;
}

/**
468
 * tsl2772_get_prox() - Reads proximity data registers and updates
469 470 471 472
 *                      chip->prox_data.
 *
 * @indio_dev:	pointer to IIO device
 */
473
static int tsl2772_get_prox(struct iio_dev *indio_dev)
474
{
475
	struct tsl2772_chip *chip = iio_priv(indio_dev);
476
	int ret;
477

478
	mutex_lock(&chip->prox_mutex);
479

480
	ret = tsl2772_read_status(chip);
481
	if (ret < 0)
482 483 484 485 486 487 488 489
		goto prox_poll_err;

	switch (chip->id) {
	case tsl2571:
	case tsl2671:
	case tmd2671:
	case tsl2771:
	case tmd2771:
490
		if (!(ret & TSL2772_STA_ADC_VALID)) {
491
			ret = -EINVAL;
492
			goto prox_poll_err;
493
		}
494
		break;
495 496 497 498 499
	case tsl2572:
	case tsl2672:
	case tmd2672:
	case tsl2772:
	case tmd2772:
500
		if (!(ret & TSL2772_STA_PRX_VALID)) {
501
			ret = -EINVAL;
502
			goto prox_poll_err;
503
		}
504
		break;
505 506
	}

507
	ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI);
508 509 510
	if (ret < 0)
		goto prox_poll_err;
	chip->prox_data = ret;
511 512 513 514

prox_poll_err:
	mutex_unlock(&chip->prox_mutex);

515
	return ret;
516 517 518
}

/**
519
 * tsl2772_defaults() - Populates the device nominal operating parameters
520 521 522 523 524
 *                      with those provided by a 'platform' data struct or
 *                      with prefined defaults.
 *
 * @chip:               pointer to device structure.
 */
525
static void tsl2772_defaults(struct tsl2772_chip *chip)
526 527
{
	/* If Operational settings defined elsewhere.. */
528
	if (chip->pdata && chip->pdata->platform_default_settings)
529
		memcpy(&chip->settings, chip->pdata->platform_default_settings,
530
		       sizeof(tsl2772_default_settings));
531
	else
532 533
		memcpy(&chip->settings, &tsl2772_default_settings,
		       sizeof(tsl2772_default_settings));
534 535

	/* Load up the proper lux table. */
536
	if (chip->pdata && chip->pdata->platform_lux_table[0].ch0 != 0)
537
		memcpy(chip->tsl2772_device_lux,
538 539
		       chip->pdata->platform_lux_table,
		       sizeof(chip->pdata->platform_lux_table));
540
	else
541 542 543
		memcpy(chip->tsl2772_device_lux,
		       tsl2772_default_lux_table_group[chip->id],
		       TSL2772_DEFAULT_TABLE_BYTES);
544 545 546
}

/**
547
 * tsl2772_als_calibrate() -	Obtain single reading and calculate
548 549 550 551
 *                              the als_gain_trim.
 *
 * @indio_dev:	pointer to IIO device
 */
552
static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
553
{
554
	struct tsl2772_chip *chip = iio_priv(indio_dev);
555
	int ret, lux_val;
556

557
	ret = i2c_smbus_read_byte_data(chip->client,
558
				       TSL2772_CMD_REG | TSL2772_CNTRL);
559 560
	if (ret < 0) {
		dev_err(&chip->client->dev,
561 562
			"%s: failed to read from the CNTRL register\n",
			__func__);
563 564 565
		return ret;
	}

566 567
	if ((ret & (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON))
			!= (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) {
568
		dev_err(&chip->client->dev,
569 570 571
			"%s: Device is not powered on and/or ADC is not enabled\n",
			__func__);
		return -EINVAL;
572
	} else if ((ret & TSL2772_STA_ADC_VALID) != TSL2772_STA_ADC_VALID) {
573
		dev_err(&chip->client->dev,
574 575
			"%s: The two ADC channels have not completed an integration cycle\n",
			__func__);
576 577 578
		return -ENODATA;
	}

579
	lux_val = tsl2772_get_lux(indio_dev);
580 581
	if (lux_val < 0) {
		dev_err(&chip->client->dev,
582
			"%s: failed to get lux\n", __func__);
583 584 585
		return lux_val;
	}

586 587
	ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
			lux_val;
588
	if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
589 590
		return -ERANGE;

591
	chip->settings.als_gain_trim = ret;
592

593
	return ret;
594 595
}

596
static int tsl2772_chip_on(struct iio_dev *indio_dev)
597
{
598
	struct tsl2772_chip *chip = iio_priv(indio_dev);
599
	int ret, i, als_count, als_time_us;
600
	u8 *dev_reg, reg_val;
601 602

	/* Non calculated parameters */
603 604 605 606
	chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
	chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
	chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
	chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
607
		chip->settings.als_prox_config;
608

609
	chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
610
		(chip->settings.als_thresh_low) & 0xFF;
611
	chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
612
		(chip->settings.als_thresh_low >> 8) & 0xFF;
613
	chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
614
		(chip->settings.als_thresh_high) & 0xFF;
615
	chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
616
		(chip->settings.als_thresh_high >> 8) & 0xFF;
617
	chip->tsl2772_config[TSL2772_PERSISTENCE] =
618 619
		(chip->settings.prox_persistence & 0xFF) << 4 |
		(chip->settings.als_persistence & 0xFF);
620

621
	chip->tsl2772_config[TSL2772_PRX_COUNT] =
622
			chip->settings.prox_pulse_count;
623
	chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
624
			(chip->settings.prox_thres_low) & 0xFF;
625
	chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
626
			(chip->settings.prox_thres_low >> 8) & 0xFF;
627
	chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
628
			(chip->settings.prox_thres_high) & 0xFF;
629
	chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
630
			(chip->settings.prox_thres_high >> 8) & 0xFF;
631 632

	/* and make sure we're not already on */
633
	if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
634 635 636 637 638
		/* if forcing a register update - turn off, then on */
		dev_info(&chip->client->dev, "device is already enabled\n");
		return -EINVAL;
	}

639 640
	/* Set the gain based on tsl2772_settings struct */
	chip->tsl2772_config[TSL2772_GAIN] =
641 642 643 644
		(chip->settings.als_gain & 0xFF) |
		((chip->settings.prox_gain & 0xFF) << 2) |
		(chip->settings.prox_diode << 4) |
		(chip->settings.prox_power << 6);
645

646 647
	/* set chip time scaling and saturation */
	als_count = 256 - chip->settings.als_time;
648
	als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
649 650
	chip->als_saturation = als_count * 768; /* 75% of full scale */
	chip->als_gain_time_scale = als_time_us *
651
		tsl2772_als_gain[chip->settings.als_gain];
652

653
	/*
654
	 * TSL2772 Specific power-on / adc enable sequence
655 656
	 * Power on the device 1st.
	 */
657
	ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
658
	if (ret < 0)
659 660
		return ret;

661 662 663 664
	/*
	 * Use the following shadow copy for our delay before enabling ADC.
	 * Write all the registers.
	 */
665 666 667
	for (i = 0, dev_reg = chip->tsl2772_config;
			i < TSL2772_MAX_CONFIG_REG; i++) {
		int reg = TSL2772_CMD_REG + i;
668 669

		ret = i2c_smbus_write_byte_data(chip->client, reg,
670
						*dev_reg++);
671 672
		if (ret < 0) {
			dev_err(&chip->client->dev,
673 674
				"%s: failed to write to register %x: %d\n",
				__func__, reg, ret);
675 676 677 678
			return ret;
		}
	}

679 680
	/* Power-on settling time */
	usleep_range(3000, 3500);
681

682 683
	reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
		  TSL2772_CNTL_PROX_DET_ENBL;
684
	if (chip->settings.als_interrupt_en)
685
		reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
686
	if (chip->settings.prox_interrupt_en)
687
		reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
688

689
	ret = tsl2772_write_control_reg(chip, reg_val);
690
	if (ret < 0)
691 692
		return ret;

693
	ret = i2c_smbus_write_byte(chip->client,
694 695
				   TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
				   TSL2772_CMD_PROXALS_INT_CLR);
696 697 698 699
	if (ret < 0) {
		dev_err(&chip->client->dev,
			"%s: failed to clear interrupt status: %d\n",
			__func__, ret);
700
		return ret;
701
	}
702

703
	chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
704 705 706 707

	return ret;
}

708
static int tsl2772_chip_off(struct iio_dev *indio_dev)
709
{
710
	struct tsl2772_chip *chip = iio_priv(indio_dev);
711 712

	/* turn device off */
713 714
	chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
	return tsl2772_write_control_reg(chip, 0x00);
715 716 717
}

/**
718
 * tsl2772_invoke_change - power cycle the device to implement the user
719
 *                         parameters
720 721
 * @indio_dev:	pointer to IIO device
 *
722 723 724
 * Obtain and lock both ALS and PROX resources, determine and save device state
 * (On/Off), cycle device to implement updated parameter, put device back into
 * proper state, and unlock resource.
725
 */
726
static int tsl2772_invoke_change(struct iio_dev *indio_dev)
727
{
728 729
	struct tsl2772_chip *chip = iio_priv(indio_dev);
	int device_status = chip->tsl2772_chip_status;
730
	int ret;
731 732 733 734

	mutex_lock(&chip->als_mutex);
	mutex_lock(&chip->prox_mutex);

735 736
	if (device_status == TSL2772_CHIP_WORKING) {
		ret = tsl2772_chip_off(indio_dev);
737 738 739
		if (ret < 0)
			goto unlock;
	}
740

741
	ret = tsl2772_chip_on(indio_dev);
742

743
unlock:
744 745 746
	mutex_unlock(&chip->prox_mutex);
	mutex_unlock(&chip->als_mutex);

747
	return ret;
748 749
}

750
static int tsl2772_prox_cal(struct iio_dev *indio_dev)
751
{
752
	struct tsl2772_chip *chip = iio_priv(indio_dev);
753 754
	int prox_history[MAX_SAMPLES_CAL + 1];
	int i, ret, mean, max, sample_sum;
755

756 757 758
	if (chip->settings.prox_max_samples_cal < 1 ||
	    chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
		return -EINVAL;
759

760
	for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
761
		usleep_range(15000, 17500);
762
		ret = tsl2772_get_prox(indio_dev);
763 764
		if (ret < 0)
			return ret;
765

766 767 768
		prox_history[i] = chip->prox_data;
	}

769 770 771 772 773
	sample_sum = 0;
	max = INT_MIN;
	for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
		sample_sum += prox_history[i];
		max = max(max, prox_history[i]);
774
	}
775 776 777
	mean = sample_sum / chip->settings.prox_max_samples_cal;

	chip->settings.prox_thres_high = (max << 1) - mean;
778

779
	return tsl2772_invoke_change(indio_dev);
780 781
}

782
static int tsl2772_read_avail(struct iio_dev *indio_dev,
783 784 785 786
			      struct iio_chan_spec const *chan,
			      const int **vals, int *type, int *length,
			      long mask)
{
787
	struct tsl2772_chip *chip = iio_priv(indio_dev);
788

789 790 791
	switch (mask) {
	case IIO_CHAN_INFO_CALIBSCALE:
		if (chan->type == IIO_INTENSITY) {
792 793
			*length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
			*vals = tsl2772_int_calibscale_avail;
794
		} else {
795 796
			*length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
			*vals = tsl2772_prox_calibscale_avail;
797 798 799 800
		}
		*type = IIO_VAL_INT;
		return IIO_AVAIL_LIST;
	case IIO_CHAN_INFO_INT_TIME:
801 802
		*length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
		*vals = tsl2772_int_time_avail[chip->id];
803 804 805
		*type = IIO_VAL_INT_PLUS_MICRO;
		return IIO_AVAIL_RANGE;
	}
806

807 808
	return -EINVAL;
}
809

810
static ssize_t in_illuminance0_target_input_show(struct device *dev,
811 812
						 struct device_attribute *attr,
						 char *buf)
813
{
814
	struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
815

816
	return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
817 818
}

819
static ssize_t in_illuminance0_target_input_store(struct device *dev,
820 821
						  struct device_attribute *attr,
						  const char *buf, size_t len)
822
{
823
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
824
	struct tsl2772_chip *chip = iio_priv(indio_dev);
825
	u16 value;
826
	int ret;
827

828
	if (kstrtou16(buf, 0, &value))
829 830
		return -EINVAL;

831
	chip->settings.als_cal_target = value;
832
	ret = tsl2772_invoke_change(indio_dev);
833 834
	if (ret < 0)
		return ret;
835 836 837 838

	return len;
}

839
static ssize_t in_illuminance0_calibrate_store(struct device *dev,
840 841
					       struct device_attribute *attr,
					       const char *buf, size_t len)
842
{
843
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
844
	bool value;
845
	int ret;
846

847
	if (kstrtobool(buf, &value) || !value)
848 849
		return -EINVAL;

850
	ret = tsl2772_als_calibrate(indio_dev);
851 852
	if (ret < 0)
		return ret;
853

854
	ret = tsl2772_invoke_change(indio_dev);
855 856
	if (ret < 0)
		return ret;
857 858 859 860

	return len;
}

861
static ssize_t in_illuminance0_lux_table_show(struct device *dev,
862 863
					      struct device_attribute *attr,
					      char *buf)
864
{
865
	struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
866 867 868
	int i = 0;
	int offset = 0;

869
	while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
870
		offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
871 872 873
			chip->tsl2772_device_lux[i].ch0,
			chip->tsl2772_device_lux[i].ch1);
		if (chip->tsl2772_device_lux[i].ch0 == 0) {
874 875 876 877
			/*
			 * We just printed the first "0" entry.
			 * Now get rid of the extra "," and break.
			 */
878 879 880 881 882 883 884 885 886 887
			offset--;
			break;
		}
		i++;
	}

	offset += snprintf(buf + offset, PAGE_SIZE, "\n");
	return offset;
}

888
static ssize_t in_illuminance0_lux_table_store(struct device *dev,
889 890
					       struct device_attribute *attr,
					       const char *buf, size_t len)
891
{
892
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
893 894
	struct tsl2772_chip *chip = iio_priv(indio_dev);
	int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
895
	int n, ret;
896 897 898

	get_options(buf, ARRAY_SIZE(value), value);

899 900
	/*
	 * We now have an array of ints starting at value[1], and
901
	 * enumerated by value[0].
902
	 * We expect each group of two ints to be one table entry,
903 904 905
	 * and the last table entry is all 0.
	 */
	n = value[0];
906
	if ((n % 2) || n < 4 ||
907
	    n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
908 909
		return -EINVAL;

910
	if ((value[(n - 1)] | value[n]) != 0)
911 912
		return -EINVAL;

913 914
	if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
		ret = tsl2772_chip_off(indio_dev);
915 916 917
		if (ret < 0)
			return ret;
	}
918 919

	/* Zero out the table */
920 921
	memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
	memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
922

923
	ret = tsl2772_invoke_change(indio_dev);
924 925
	if (ret < 0)
		return ret;
926 927 928 929

	return len;
}

930
static ssize_t in_proximity0_calibrate_store(struct device *dev,
931 932
					     struct device_attribute *attr,
					     const char *buf, size_t len)
933
{
934
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
935
	bool value;
936
	int ret;
937

938
	if (kstrtobool(buf, &value) || !value)
939 940
		return -EINVAL;

941
	ret = tsl2772_prox_cal(indio_dev);
942 943
	if (ret < 0)
		return ret;
944

945
	ret = tsl2772_invoke_change(indio_dev);
946 947
	if (ret < 0)
		return ret;
948 949 950 951

	return len;
}

952
static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
953 954 955
					 const struct iio_chan_spec *chan,
					 enum iio_event_type type,
					 enum iio_event_direction dir)
956
{
957
	struct tsl2772_chip *chip = iio_priv(indio_dev);
958

959
	if (chan->type == IIO_INTENSITY)
960
		return chip->settings.als_interrupt_en;
961
	else
962
		return chip->settings.prox_interrupt_en;
963 964
}

965
static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
966 967 968
					  const struct iio_chan_spec *chan,
					  enum iio_event_type type,
					  enum iio_event_direction dir,
969 970
					  int val)
{
971
	struct tsl2772_chip *chip = iio_priv(indio_dev);
972

973 974 975 976
	if (chan->type == IIO_INTENSITY)
		chip->settings.als_interrupt_en = val ? true : false;
	else
		chip->settings.prox_interrupt_en = val ? true : false;
977

978
	return tsl2772_invoke_change(indio_dev);
979 980
}

981
static int tsl2772_write_event_value(struct iio_dev *indio_dev,
982 983 984 985 986
				     const struct iio_chan_spec *chan,
				     enum iio_event_type type,
				     enum iio_event_direction dir,
				     enum iio_event_info info,
				     int val, int val2)
987
{
988
	struct tsl2772_chip *chip = iio_priv(indio_dev);
989
	int ret = -EINVAL, count, persistence;
990
	u8 time;
991

992 993 994 995 996
	switch (info) {
	case IIO_EV_INFO_VALUE:
		if (chan->type == IIO_INTENSITY) {
			switch (dir) {
			case IIO_EV_DIR_RISING:
997
				chip->settings.als_thresh_high = val;
998 999 1000
				ret = 0;
				break;
			case IIO_EV_DIR_FALLING:
1001
				chip->settings.als_thresh_low = val;
1002 1003 1004 1005 1006 1007 1008 1009
				ret = 0;
				break;
			default:
				break;
			}
		} else {
			switch (dir) {
			case IIO_EV_DIR_RISING:
1010
				chip->settings.prox_thres_high = val;
1011 1012 1013
				ret = 0;
				break;
			case IIO_EV_DIR_FALLING:
1014
				chip->settings.prox_thres_low = val;
1015 1016 1017 1018 1019
				ret = 0;
				break;
			default:
				break;
			}
1020
		}
1021
		break;
1022 1023 1024 1025
	case IIO_EV_INFO_PERIOD:
		if (chan->type == IIO_INTENSITY)
			time = chip->settings.als_time;
		else
1026
			time = chip->settings.prox_time;
1027

1028 1029
		count = 256 - time;
		persistence = ((val * 1000000) + val2) /
1030
			(count * tsl2772_int_time_avail[chip->id][3]);
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040
		if (chan->type == IIO_INTENSITY) {
			/* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
			if (persistence > 3)
				persistence = (persistence / 5) + 3;

			chip->settings.als_persistence = persistence;
		} else {
			chip->settings.prox_persistence = persistence;
		}
1041 1042 1043

		ret = 0;
		break;
1044 1045
	default:
		break;
1046 1047
	}

1048 1049
	if (ret < 0)
		return ret;
1050

1051
	return tsl2772_invoke_change(indio_dev);
1052 1053
}

1054
static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1055 1056 1057 1058 1059
				    const struct iio_chan_spec *chan,
				    enum iio_event_type type,
				    enum iio_event_direction dir,
				    enum iio_event_info info,
				    int *val, int *val2)
1060
{
1061
	struct tsl2772_chip *chip = iio_priv(indio_dev);
1062
	int filter_delay, persistence;
1063
	u8 time;
1064

1065 1066 1067 1068 1069
	switch (info) {
	case IIO_EV_INFO_VALUE:
		if (chan->type == IIO_INTENSITY) {
			switch (dir) {
			case IIO_EV_DIR_RISING:
1070
				*val = chip->settings.als_thresh_high;
1071
				return IIO_VAL_INT;
1072
			case IIO_EV_DIR_FALLING:
1073
				*val = chip->settings.als_thresh_low;
1074
				return IIO_VAL_INT;
1075
			default:
1076
				return -EINVAL;
1077 1078 1079 1080
			}
		} else {
			switch (dir) {
			case IIO_EV_DIR_RISING:
1081
				*val = chip->settings.prox_thres_high;
1082
				return IIO_VAL_INT;
1083
			case IIO_EV_DIR_FALLING:
1084
				*val = chip->settings.prox_thres_low;
1085
				return IIO_VAL_INT;
1086
			default:
1087
				return -EINVAL;
1088
			}
1089
		}
1090
		break;
1091 1092 1093
	case IIO_EV_INFO_PERIOD:
		if (chan->type == IIO_INTENSITY) {
			time = chip->settings.als_time;
1094 1095 1096 1097 1098
			persistence = chip->settings.als_persistence;

			/* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
			if (persistence > 3)
				persistence = (persistence - 3) * 5;
1099
		} else {
1100
			time = chip->settings.prox_time;
1101
			persistence = chip->settings.prox_persistence;
1102 1103
		}

1104
		filter_delay = persistence * (256 - time) *
1105
			tsl2772_int_time_avail[chip->id][3];
1106 1107 1108

		*val = filter_delay / 1000000;
		*val2 = filter_delay % 1000000;
1109
		return IIO_VAL_INT_PLUS_MICRO;
1110
	default:
1111
		return -EINVAL;
1112 1113 1114
	}
}

1115
static int tsl2772_read_raw(struct iio_dev *indio_dev,
1116 1117 1118 1119 1120
			    struct iio_chan_spec const *chan,
			    int *val,
			    int *val2,
			    long mask)
{
1121
	struct tsl2772_chip *chip = iio_priv(indio_dev);
1122 1123

	switch (mask) {
J
Jon Brenner 已提交
1124
	case IIO_CHAN_INFO_PROCESSED:
1125 1126
		switch (chan->type) {
		case IIO_LIGHT:
1127
			tsl2772_get_lux(indio_dev);
1128
			*val = chip->als_cur_info.lux;
1129
			return IIO_VAL_INT;
J
Jon Brenner 已提交
1130 1131 1132 1133 1134
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_RAW:
		switch (chan->type) {
1135
		case IIO_INTENSITY:
1136
			tsl2772_get_lux(indio_dev);
1137 1138 1139 1140
			if (chan->channel == 0)
				*val = chip->als_cur_info.als_ch0;
			else
				*val = chip->als_cur_info.als_ch1;
1141
			return IIO_VAL_INT;
1142
		case IIO_PROXIMITY:
1143
			tsl2772_get_prox(indio_dev);
1144
			*val = chip->prox_data;
1145
			return IIO_VAL_INT;
1146 1147 1148 1149 1150 1151
		default:
			return -EINVAL;
		}
		break;
	case IIO_CHAN_INFO_CALIBSCALE:
		if (chan->type == IIO_LIGHT)
1152
			*val = tsl2772_als_gain[chip->settings.als_gain];
1153
		else
1154
			*val = tsl2772_prox_gain[chip->settings.prox_gain];
1155
		return IIO_VAL_INT;
1156
	case IIO_CHAN_INFO_CALIBBIAS:
1157
		*val = chip->settings.als_gain_trim;
1158
		return IIO_VAL_INT;
1159
	case IIO_CHAN_INFO_INT_TIME:
1160
		*val = 0;
1161
		*val2 = (256 - chip->settings.als_time) *
1162
			tsl2772_int_time_avail[chip->id][3];
1163
		return IIO_VAL_INT_PLUS_MICRO;
1164
	default:
1165
		return -EINVAL;
1166 1167 1168
	}
}

1169
static int tsl2772_write_raw(struct iio_dev *indio_dev,
1170 1171 1172 1173
			     struct iio_chan_spec const *chan,
			     int val,
			     int val2,
			     long mask)
1174
{
1175
	struct tsl2772_chip *chip = iio_priv(indio_dev);
1176 1177 1178 1179 1180 1181

	switch (mask) {
	case IIO_CHAN_INFO_CALIBSCALE:
		if (chan->type == IIO_INTENSITY) {
			switch (val) {
			case 1:
1182
				chip->settings.als_gain = 0;
1183 1184
				break;
			case 8:
1185
				chip->settings.als_gain = 1;
1186 1187
				break;
			case 16:
1188
				chip->settings.als_gain = 2;
1189 1190
				break;
			case 120:
1191
				chip->settings.als_gain = 3;
1192 1193 1194 1195 1196 1197 1198
				break;
			default:
				return -EINVAL;
			}
		} else {
			switch (val) {
			case 1:
1199
				chip->settings.prox_gain = 0;
1200 1201
				break;
			case 2:
1202
				chip->settings.prox_gain = 1;
1203 1204
				break;
			case 4:
1205
				chip->settings.prox_gain = 2;
1206 1207
				break;
			case 8:
1208
				chip->settings.prox_gain = 3;
1209 1210 1211 1212 1213 1214 1215
				break;
			default:
				return -EINVAL;
			}
		}
		break;
	case IIO_CHAN_INFO_CALIBBIAS:
1216 1217
		if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
		    val > TSL2772_ALS_GAIN_TRIM_MAX)
1218 1219
			return -EINVAL;

1220
		chip->settings.als_gain_trim = val;
1221
		break;
1222
	case IIO_CHAN_INFO_INT_TIME:
1223 1224
		if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
		    val2 > tsl2772_int_time_avail[chip->id][5])
1225 1226
			return -EINVAL;

1227
		chip->settings.als_time = 256 -
1228
			(val2 / tsl2772_int_time_avail[chip->id][3]);
1229
		break;
1230 1231 1232 1233
	default:
		return -EINVAL;
	}

1234
	return tsl2772_invoke_change(indio_dev);
1235 1236
}

1237
static DEVICE_ATTR_RW(in_illuminance0_target_input);
1238

1239
static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1240

1241
static DEVICE_ATTR_WO(in_proximity0_calibrate);
1242

1243
static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1244 1245

/* Use the default register values to identify the Taos device */
1246
static int tsl2772_device_id_verif(int id, int target)
1247 1248 1249 1250 1251
{
	switch (target) {
	case tsl2571:
	case tsl2671:
	case tsl2771:
1252
		return (id & 0xf0) == TRITON_ID;
1253 1254
	case tmd2671:
	case tmd2771:
1255
		return (id & 0xf0) == HALIBUT_ID;
1256 1257 1258 1259 1260
	case tsl2572:
	case tsl2672:
	case tmd2672:
	case tsl2772:
	case tmd2772:
1261
		return (id & 0xf0) == SWORDFISH_ID;
1262 1263 1264 1265 1266
	}

	return -EINVAL;
}

1267
static irqreturn_t tsl2772_event_handler(int irq, void *private)
1268 1269
{
	struct iio_dev *indio_dev = private;
1270
	struct tsl2772_chip *chip = iio_priv(indio_dev);
1271
	s64 timestamp = iio_get_time_ns(indio_dev);
1272 1273
	int ret;

1274
	ret = tsl2772_read_status(chip);
1275
	if (ret < 0)
1276
		return IRQ_HANDLED;
1277 1278

	/* What type of interrupt do we need to process */
1279
	if (ret & TSL2772_STA_PRX_INTR) {
1280 1281 1282 1283 1284
		iio_push_event(indio_dev,
			       IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
						    0,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_EITHER),
1285
			       timestamp);
1286 1287
	}

1288
	if (ret & TSL2772_STA_ALS_INTR) {
1289
		iio_push_event(indio_dev,
1290 1291 1292 1293 1294
			       IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
						    0,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_EITHER),
			       timestamp);
1295
	}
1296

1297
	ret = i2c_smbus_write_byte(chip->client,
1298 1299
				   TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
				   TSL2772_CMD_PROXALS_INT_CLR);
1300 1301 1302 1303
	if (ret < 0)
		dev_err(&chip->client->dev,
			"%s: failed to clear interrupt status: %d\n",
			__func__, ret);
1304 1305 1306 1307

	return IRQ_HANDLED;
}

1308
static struct attribute *tsl2772_ALS_device_attrs[] = {
1309 1310 1311 1312 1313 1314
	&dev_attr_in_illuminance0_target_input.attr,
	&dev_attr_in_illuminance0_calibrate.attr,
	&dev_attr_in_illuminance0_lux_table.attr,
	NULL
};

1315
static struct attribute *tsl2772_PRX_device_attrs[] = {
1316 1317 1318 1319
	&dev_attr_in_proximity0_calibrate.attr,
	NULL
};

1320
static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1321 1322 1323 1324 1325 1326
	&dev_attr_in_illuminance0_target_input.attr,
	&dev_attr_in_illuminance0_calibrate.attr,
	&dev_attr_in_illuminance0_lux_table.attr,
	NULL
};

1327
static struct attribute *tsl2772_PRX2_device_attrs[] = {
1328 1329 1330 1331
	&dev_attr_in_proximity0_calibrate.attr,
	NULL
};

1332
static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1333 1334 1335 1336 1337 1338 1339
	&dev_attr_in_illuminance0_target_input.attr,
	&dev_attr_in_illuminance0_calibrate.attr,
	&dev_attr_in_illuminance0_lux_table.attr,
	&dev_attr_in_proximity0_calibrate.attr,
	NULL
};

1340
static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1341
	[ALS] = {
1342
		.attrs = tsl2772_ALS_device_attrs,
1343 1344
	},
	[PRX] = {
1345
		.attrs = tsl2772_PRX_device_attrs,
1346 1347
	},
	[ALSPRX] = {
1348
		.attrs = tsl2772_ALSPRX_device_attrs,
1349 1350
	},
	[PRX2] = {
1351
		.attrs = tsl2772_PRX2_device_attrs,
1352 1353
	},
	[ALSPRX2] = {
1354
		.attrs = tsl2772_ALSPRX2_device_attrs,
1355 1356 1357
	},
};

1358
#define TSL2772_DEVICE_INFO(type)[type] = \
1359
	{ \
1360 1361 1362 1363 1364 1365 1366 1367
		.attrs = &tsl2772_device_attr_group_tbl[type], \
		.read_raw = &tsl2772_read_raw, \
		.read_avail = &tsl2772_read_avail, \
		.write_raw = &tsl2772_write_raw, \
		.read_event_value = &tsl2772_read_event_value, \
		.write_event_value = &tsl2772_write_event_value, \
		.read_event_config = &tsl2772_read_interrupt_config, \
		.write_event_config = &tsl2772_write_interrupt_config, \
1368 1369
	}

1370 1371 1372 1373 1374 1375
static const struct iio_info tsl2772_device_info[] = {
	TSL2772_DEVICE_INFO(ALS),
	TSL2772_DEVICE_INFO(PRX),
	TSL2772_DEVICE_INFO(ALSPRX),
	TSL2772_DEVICE_INFO(PRX2),
	TSL2772_DEVICE_INFO(ALSPRX2),
1376 1377
};

1378
static const struct iio_event_spec tsl2772_events[] = {
1379 1380 1381
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_RISING,
1382
		.mask_separate = BIT(IIO_EV_INFO_VALUE),
1383 1384 1385
	}, {
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_FALLING,
1386
		.mask_separate = BIT(IIO_EV_INFO_VALUE),
1387 1388 1389
	}, {
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_EITHER,
1390 1391
		.mask_separate = BIT(IIO_EV_INFO_PERIOD) |
			BIT(IIO_EV_INFO_ENABLE),
1392 1393 1394
	},
};

1395
static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1396
	[ALS] = {
1397
		.channel_with_events = {
1398 1399 1400 1401
			{
			.type = IIO_LIGHT,
			.indexed = 1,
			.channel = 0,
1402
			.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1403 1404 1405 1406
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 0,
1407
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1408
				BIT(IIO_CHAN_INFO_INT_TIME) |
1409 1410
				BIT(IIO_CHAN_INFO_CALIBSCALE) |
				BIT(IIO_CHAN_INFO_CALIBBIAS),
1411 1412 1413
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1414 1415
			.event_spec = tsl2772_events,
			.num_event_specs = ARRAY_SIZE(tsl2772_events),
1416 1417 1418 1419 1420 1421
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 1,
			},
		},
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
		.channel_without_events = {
			{
			.type = IIO_LIGHT,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE) |
				BIT(IIO_CHAN_INFO_CALIBBIAS),
1436 1437 1438
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1439 1440 1441 1442 1443 1444 1445
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 1,
			},
		},
		.chan_table_elements = 3,
1446
		.info = &tsl2772_device_info[ALS],
1447 1448
	},
	[PRX] = {
1449
		.channel_with_events = {
1450 1451 1452 1453
			{
			.type = IIO_PROXIMITY,
			.indexed = 1,
			.channel = 0,
1454
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1455 1456
			.event_spec = tsl2772_events,
			.num_event_specs = ARRAY_SIZE(tsl2772_events),
1457 1458
			},
		},
1459 1460 1461 1462 1463 1464 1465 1466 1467
		.channel_without_events = {
			{
			.type = IIO_PROXIMITY,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
			},
		},
		.chan_table_elements = 1,
1468
		.info = &tsl2772_device_info[PRX],
1469 1470
	},
	[ALSPRX] = {
1471
		.channel_with_events = {
1472 1473 1474 1475
			{
			.type = IIO_LIGHT,
			.indexed = 1,
			.channel = 0,
1476
			.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1477 1478 1479 1480
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 0,
1481
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1482
				BIT(IIO_CHAN_INFO_INT_TIME) |
1483 1484
				BIT(IIO_CHAN_INFO_CALIBSCALE) |
				BIT(IIO_CHAN_INFO_CALIBBIAS),
1485 1486 1487
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1488 1489
			.event_spec = tsl2772_events,
			.num_event_specs = ARRAY_SIZE(tsl2772_events),
1490 1491 1492 1493
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 1,
1494
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1495 1496 1497 1498
			}, {
			.type = IIO_PROXIMITY,
			.indexed = 1,
			.channel = 0,
1499
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1500 1501
			.event_spec = tsl2772_events,
			.num_event_specs = ARRAY_SIZE(tsl2772_events),
1502 1503
			},
		},
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
		.channel_without_events = {
			{
			.type = IIO_LIGHT,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE) |
				BIT(IIO_CHAN_INFO_CALIBBIAS),
1518 1519 1520
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 1,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
			}, {
			.type = IIO_PROXIMITY,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
			},
		},
		.chan_table_elements = 4,
1534
		.info = &tsl2772_device_info[ALSPRX],
1535 1536
	},
	[PRX2] = {
1537
		.channel_with_events = {
1538 1539 1540 1541
			{
			.type = IIO_PROXIMITY,
			.indexed = 1,
			.channel = 0,
1542 1543
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1544 1545
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1546 1547
			.event_spec = tsl2772_events,
			.num_event_specs = ARRAY_SIZE(tsl2772_events),
1548 1549
			},
		},
1550 1551 1552 1553 1554 1555 1556
		.channel_without_events = {
			{
			.type = IIO_PROXIMITY,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1557 1558
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1559 1560 1561
			},
		},
		.chan_table_elements = 1,
1562
		.info = &tsl2772_device_info[PRX2],
1563 1564
	},
	[ALSPRX2] = {
1565
		.channel_with_events = {
1566 1567 1568 1569
			{
			.type = IIO_LIGHT,
			.indexed = 1,
			.channel = 0,
1570
			.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1571 1572 1573 1574
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 0,
1575
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1576
				BIT(IIO_CHAN_INFO_INT_TIME) |
1577 1578
				BIT(IIO_CHAN_INFO_CALIBSCALE) |
				BIT(IIO_CHAN_INFO_CALIBBIAS),
1579 1580 1581
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1582 1583
			.event_spec = tsl2772_events,
			.num_event_specs = ARRAY_SIZE(tsl2772_events),
1584 1585 1586 1587
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 1,
1588
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1589 1590 1591 1592
			}, {
			.type = IIO_PROXIMITY,
			.indexed = 1,
			.channel = 0,
1593 1594
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1595 1596
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1597 1598
			.event_spec = tsl2772_events,
			.num_event_specs = ARRAY_SIZE(tsl2772_events),
1599 1600
			},
		},
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
		.channel_without_events = {
			{
			.type = IIO_LIGHT,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE) |
				BIT(IIO_CHAN_INFO_CALIBBIAS),
1615 1616 1617
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_INT_TIME) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
			}, {
			.type = IIO_INTENSITY,
			.indexed = 1,
			.channel = 1,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
			}, {
			.type = IIO_PROXIMITY,
			.indexed = 1,
			.channel = 0,
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1629 1630
			.info_mask_separate_available =
				BIT(IIO_CHAN_INFO_CALIBSCALE),
1631 1632 1633
			},
		},
		.chan_table_elements = 4,
1634
		.info = &tsl2772_device_info[ALSPRX2],
1635 1636 1637
	},
};

1638
static int tsl2772_probe(struct i2c_client *clientp,
1639
			 const struct i2c_device_id *id)
1640 1641
{
	struct iio_dev *indio_dev;
1642
	struct tsl2772_chip *chip;
1643
	int ret;
1644

1645
	indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1646 1647 1648 1649 1650 1651 1652
	if (!indio_dev)
		return -ENOMEM;

	chip = iio_priv(indio_dev);
	chip->client = clientp;
	i2c_set_clientdata(clientp, indio_dev);

1653
	ret = i2c_smbus_read_byte_data(chip->client,
1654
				       TSL2772_CMD_REG | TSL2772_CHIPID);
1655
	if (ret < 0)
1656
		return ret;
1657

1658
	if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1659
		dev_info(&chip->client->dev,
1660
			 "%s: i2c device found does not match expected id\n",
1661
				__func__);
1662
		return -EINVAL;
1663 1664
	}

1665
	ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1666
	if (ret < 0) {
1667 1668 1669
		dev_err(&clientp->dev,
			"%s: Failed to write to CMD register: %d\n",
			__func__, ret);
1670
		return ret;
1671 1672 1673 1674 1675
	}

	mutex_init(&chip->als_mutex);
	mutex_init(&chip->prox_mutex);

1676
	chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1677
	chip->pdata = dev_get_platdata(&clientp->dev);
1678 1679
	chip->id = id->driver_data;
	chip->chip_info =
1680
		&tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1681 1682 1683 1684 1685 1686 1687 1688

	indio_dev->info = chip->chip_info->info;
	indio_dev->dev.parent = &clientp->dev;
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->name = chip->client->name;
	indio_dev->num_channels = chip->chip_info->chan_table_elements;

	if (clientp->irq) {
1689 1690
		indio_dev->channels = chip->chip_info->channel_with_events;

1691 1692
		ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
						NULL,
1693
						&tsl2772_event_handler,
1694
						IRQF_TRIGGER_FALLING |
1695
						IRQF_ONESHOT,
1696
						"TSL2772_event",
1697
						indio_dev);
1698 1699
		if (ret) {
			dev_err(&clientp->dev,
1700
				"%s: irq request failed\n", __func__);
1701
			return ret;
1702
		}
1703 1704
	} else {
		indio_dev->channels = chip->chip_info->channel_without_events;
1705 1706
	}

1707 1708
	tsl2772_defaults(chip);
	ret = tsl2772_chip_on(indio_dev);
1709 1710
	if (ret < 0)
		return ret;
1711 1712 1713

	ret = iio_device_register(indio_dev);
	if (ret) {
1714
		tsl2772_chip_off(indio_dev);
1715 1716
		dev_err(&clientp->dev,
			"%s: iio registration failed\n", __func__);
1717
		return ret;
1718 1719 1720 1721 1722
	}

	return 0;
}

1723
static int tsl2772_suspend(struct device *dev)
1724 1725 1726
{
	struct iio_dev *indio_dev = dev_get_drvdata(dev);

1727
	return tsl2772_chip_off(indio_dev);
1728 1729
}

1730
static int tsl2772_resume(struct device *dev)
1731 1732 1733
{
	struct iio_dev *indio_dev = dev_get_drvdata(dev);

1734
	return tsl2772_chip_on(indio_dev);
1735 1736
}

1737
static int tsl2772_remove(struct i2c_client *client)
1738
{
1739
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1740

1741
	tsl2772_chip_off(indio_dev);
1742 1743 1744 1745 1746 1747

	iio_device_unregister(indio_dev);

	return 0;
}

1748
static const struct i2c_device_id tsl2772_idtable[] = {
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
	{ "tsl2571", tsl2571 },
	{ "tsl2671", tsl2671 },
	{ "tmd2671", tmd2671 },
	{ "tsl2771", tsl2771 },
	{ "tmd2771", tmd2771 },
	{ "tsl2572", tsl2572 },
	{ "tsl2672", tsl2672 },
	{ "tmd2672", tmd2672 },
	{ "tsl2772", tsl2772 },
	{ "tmd2772", tmd2772 },
	{}
};

1762
MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1763

1764
static const struct of_device_id tsl2772_of_match[] = {
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
	{ .compatible = "amstaos,tsl2571" },
	{ .compatible = "amstaos,tsl2671" },
	{ .compatible = "amstaos,tmd2671" },
	{ .compatible = "amstaos,tsl2771" },
	{ .compatible = "amstaos,tmd2771" },
	{ .compatible = "amstaos,tsl2572" },
	{ .compatible = "amstaos,tsl2672" },
	{ .compatible = "amstaos,tmd2672" },
	{ .compatible = "amstaos,tsl2772" },
	{ .compatible = "amstaos,tmd2772" },
	{}
};
1777
MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1778

1779 1780 1781
static const struct dev_pm_ops tsl2772_pm_ops = {
	.suspend = tsl2772_suspend,
	.resume  = tsl2772_resume,
1782 1783
};

1784
static struct i2c_driver tsl2772_driver = {
1785
	.driver = {
1786 1787 1788
		.name = "tsl2772",
		.of_match_table = tsl2772_of_match,
		.pm = &tsl2772_pm_ops,
1789
	},
1790 1791 1792
	.id_table = tsl2772_idtable,
	.probe = tsl2772_probe,
	.remove = tsl2772_remove,
1793 1794
};

1795
module_i2c_driver(tsl2772_driver);
1796

1797 1798
MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1799
MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1800
MODULE_LICENSE("GPL");