tsl2583.c 22.8 KB
Newer Older
1 2
/*
 * Device driver for monitoring ambient light intensity (lux)
3
 * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
4 5
 *
 * Copyright (c) 2011, TAOS Corporation.
6
 * Copyright (c) 2016 Brian Masney <masneyb@onstation.org>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#include <linux/kernel.h>
#include <linux/i2c.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/mutex.h>
#include <linux/unistd.h>
#include <linux/slab.h>
27
#include <linux/module.h>
28
#include <linux/iio/iio.h>
29
#include <linux/iio/sysfs.h>
30 31

/* Device Registers and Masks */
32 33 34 35 36 37 38 39 40 41 42 43
#define TSL2583_CNTRL			0x00
#define TSL2583_ALS_TIME		0X01
#define TSL2583_INTERRUPT		0x02
#define TSL2583_GAIN			0x07
#define TSL2583_REVID			0x11
#define TSL2583_CHIPID			0x12
#define TSL2583_ALS_CHAN0LO		0x14
#define TSL2583_ALS_CHAN0HI		0x15
#define TSL2583_ALS_CHAN1LO		0x16
#define TSL2583_ALS_CHAN1HI		0x17
#define TSL2583_TMR_LO			0x18
#define TSL2583_TMR_HI			0x19
44 45

/* tsl2583 cmd reg masks */
46 47
#define TSL2583_CMD_REG			0x80
#define TSL2583_CMD_SPL_FN		0x60
48
#define TSL2583_CMD_ALS_INT_CLR		0x01
49 50

/* tsl2583 cntrl reg masks */
51
#define TSL2583_CNTL_ADC_ENBL		0x02
52 53
#define TSL2583_CNTL_PWR_OFF		0x00
#define TSL2583_CNTL_PWR_ON		0x01
54 55

/* tsl2583 status reg masks */
56 57
#define TSL2583_STA_ADC_VALID		0x01
#define TSL2583_STA_ADC_INTR		0x10
58 59

/* Lux calculation constants */
60
#define TSL2583_LUX_CALC_OVER_FLOW	65535
61

62 63
#define TSL2583_INTERRUPT_DISABLED	0x00

64 65 66
#define TSL2583_CHIP_ID			0x90
#define TSL2583_CHIP_ID_MASK		0xf0

67
/* Per-device data */
68
struct tsl2583_als_info {
69 70 71 72 73
	u16 als_ch0;
	u16 als_ch1;
	u16 lux;
};

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
struct tsl2583_lux {
	unsigned int ratio;
	unsigned int ch0;
	unsigned int ch1;
};

static const struct tsl2583_lux tsl2583_default_lux[] = {
	{  9830,  8520, 15729 },
	{ 12452, 10807, 23344 },
	{ 14746,  6383, 11705 },
	{ 17695,  4063,  6554 },
	{     0,     0,     0 }  /* Termination segment */
};

#define TSL2583_MAX_LUX_TABLE_ENTRIES 11

90
struct tsl2583_settings {
91 92 93 94
	int als_time;
	int als_gain;
	int als_gain_trim;
	int als_cal_target;
95 96 97 98 99 100 101

	/*
	 * This structure is intentionally large to accommodate updates via
	 * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
	 * Assumption is that one and only one type of glass used.
	 */
	struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES];
102 103 104 105 106
};

struct tsl2583_chip {
	struct mutex als_mutex;
	struct i2c_client *client;
107 108
	struct tsl2583_als_info als_cur_info;
	struct tsl2583_settings als_settings;
109 110
	int als_time_scale;
	int als_saturation;
111
	bool suspended;
112 113 114 115 116
};

struct gainadj {
	s16 ch0;
	s16 ch1;
117
	s16 mean;
118 119 120 121
};

/* Index = (0 - 3) Used to validate the gain selection index */
static const struct gainadj gainadj[] = {
122 123 124 125
	{ 1, 1, 1 },
	{ 8, 8, 8 },
	{ 16, 16, 16 },
	{ 107, 115, 111 }
126 127 128 129 130 131
};

/*
 * Provides initial operational parameter defaults.
 * These defaults may be changed through the device's sysfs files.
 */
132
static void tsl2583_defaults(struct tsl2583_chip *chip)
133
{
134 135 136 137
	/*
	 * The integration time must be a multiple of 50ms and within the
	 * range [50, 600] ms.
	 */
138
	chip->als_settings.als_time = 100;
139 140 141 142 143

	/*
	 * This is an index into the gainadj table. Assume clear glass as the
	 * default.
	 */
144
	chip->als_settings.als_gain = 0;
145 146

	/* Default gain trim to account for aperture effects */
147
	chip->als_settings.als_gain_trim = 1000;
148

149
	/* Known external ALS reading used for calibration */
150
	chip->als_settings.als_cal_target = 130;
151 152 153 154

	/* Default lux table. */
	memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux,
	       sizeof(tsl2583_default_lux));
155 156 157 158 159 160 161 162 163
}

/*
 * Reads and calculates current lux value.
 * The raw ch0 and ch1 values of the ambient light sensed in the last
 * integration cycle are read from the device.
 * Time scale factor array values are adjusted based on the integration time.
 * The raw values are multiplied by a scale factor, and device gain is obtained
 * using gain index. Limit checks are done next, then the ratio of a multiple
164
 * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
165 166 167 168 169
 * declared above is then scanned to find the first ratio value that is just
 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
 * the array are then used along with the time scale factor array values, to
 * calculate the lux.
 */
170
static int tsl2583_get_lux(struct iio_dev *indio_dev)
171 172 173
{
	u16 ch0, ch1; /* separated ch0/ch1 data from device */
	u32 lux; /* raw lux calculated from device data */
174
	u64 lux64;
175 176
	u32 ratio;
	u8 buf[5];
177
	struct tsl2583_lux *p;
178
	struct tsl2583_chip *chip = iio_priv(indio_dev);
179 180
	int i, ret;

181
	ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG);
182
	if (ret < 0) {
183 184
		dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n",
			__func__);
185
		goto done;
186
	}
187

188
	/* is data new & valid */
189
	if (!(ret & TSL2583_STA_ADC_INTR)) {
190 191
		dev_err(&chip->client->dev, "%s: data not valid; returning last value\n",
			__func__);
192
		ret = chip->als_cur_info.lux; /* return LAST VALUE */
193
		goto done;
194 195 196
	}

	for (i = 0; i < 4; i++) {
197
		int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i);
198

199
		ret = i2c_smbus_read_byte_data(chip->client, reg);
200
		if (ret < 0) {
201 202
			dev_err(&chip->client->dev, "%s: failed to read register %x\n",
				__func__, reg);
203
			goto done;
204
		}
205
		buf[i] = ret;
206 207
	}

208
	/*
209 210 211
	 * Clear the pending interrupt status bit on the chip to allow the next
	 * integration cycle to start. This has to be done even though this
	 * driver currently does not support interrupts.
212
	 */
213
	ret = i2c_smbus_write_byte(chip->client,
214 215
				   (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN |
				    TSL2583_CMD_ALS_INT_CLR));
216
	if (ret < 0) {
217 218
		dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n",
			__func__);
219
		goto done; /* have no data, so return failure */
220 221 222 223 224 225 226 227 228 229 230 231
	}

	/* extract ALS/lux data */
	ch0 = le16_to_cpup((const __le16 *)&buf[0]);
	ch1 = le16_to_cpup((const __le16 *)&buf[2]);

	chip->als_cur_info.als_ch0 = ch0;
	chip->als_cur_info.als_ch1 = ch1;

	if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
		goto return_max;

232
	if (!ch0) {
233 234 235 236 237
		/*
		 * The sensor appears to be in total darkness so set the
		 * calculated lux to 0 and return early to avoid a division by
		 * zero below when calculating the ratio.
		 */
238 239
		ret = 0;
		chip->als_cur_info.lux = 0;
240
		goto done;
241
	}
242

243 244
	/* calculate ratio */
	ratio = (ch1 << 15) / ch0;
245

246
	/* convert to unscaled lux using the pointer to the table */
247
	for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux;
248 249 250 251 252 253
	     p->ratio != 0 && p->ratio < ratio; p++)
		;

	if (p->ratio == 0) {
		lux = 0;
	} else {
254 255
		u32 ch0lux, ch1lux;

256
		ch0lux = ((ch0 * p->ch0) +
257 258
			  (gainadj[chip->als_settings.als_gain].ch0 >> 1))
			 / gainadj[chip->als_settings.als_gain].ch0;
259
		ch1lux = ((ch1 * p->ch1) +
260 261
			  (gainadj[chip->als_settings.als_gain].ch1 >> 1))
			 / gainadj[chip->als_settings.als_gain].ch1;
262

263 264 265 266 267 268 269 270 271 272
		/* note: lux is 31 bit max at this point */
		if (ch1lux > ch0lux) {
			dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n",
				__func__);
			ret = 0;
			chip->als_cur_info.lux = 0;
			goto done;
		}

		lux = ch0lux - ch1lux;
273 274 275 276 277 278 279 280 281
	}

	/* adjust for active time scale */
	if (chip->als_time_scale == 0)
		lux = 0;
	else
		lux = (lux + (chip->als_time_scale >> 1)) /
			chip->als_time_scale;

282 283
	/*
	 * Adjust for active gain scale.
284
	 * The tsl2583_default_lux tables above have a factor of 8192 built in,
285 286 287 288 289 290 291
	 * so we need to shift right.
	 * User-specified gain provides a multiplier.
	 * Apply user-specified gain before shifting right to retain precision.
	 * Use 64 bits to avoid overflow on multiplication.
	 * Then go back to 32 bits before division to avoid using div_u64().
	 */
	lux64 = lux;
292
	lux64 = lux64 * chip->als_settings.als_gain_trim;
293 294 295
	lux64 >>= 13;
	lux = lux64;
	lux = (lux + 500) / 1000;
296

297
	if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
298
return_max:
299
		lux = TSL2583_LUX_CALC_OVER_FLOW;
300 301 302 303 304 305
	}

	/* Update the structure with the latest VALID lux. */
	chip->als_cur_info.lux = lux;
	ret = lux;

306
done:
307 308 309 310 311 312 313 314
	return ret;
}

/*
 * Obtain single reading and calculate the als_gain_trim (later used
 * to derive actual lux).
 * Return updated gain_trim value.
 */
315
static int tsl2583_als_calibrate(struct iio_dev *indio_dev)
316
{
317
	struct tsl2583_chip *chip = iio_priv(indio_dev);
318 319 320 321
	unsigned int gain_trim_val;
	int ret;
	int lux_val;

322
	ret = i2c_smbus_read_byte_data(chip->client,
323
				       TSL2583_CMD_REG | TSL2583_CNTRL);
324
	if (ret < 0) {
325
		dev_err(&chip->client->dev,
326
			"%s: failed to read from the CNTRL register\n",
327 328 329 330
			__func__);
		return ret;
	}

331 332
	if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON))
			!= (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) {
333
		dev_err(&chip->client->dev,
334 335
			"%s: Device is not powered on and/or ADC is not enabled\n",
			__func__);
336
		return -EINVAL;
337
	} else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) {
338
		dev_err(&chip->client->dev,
339 340
			"%s: The two ADC channels have not completed an integration cycle\n",
			__func__);
341 342
		return -ENODATA;
	}
343

344
	lux_val = tsl2583_get_lux(indio_dev);
345
	if (lux_val < 0) {
346 347
		dev_err(&chip->client->dev, "%s: failed to get lux\n",
			__func__);
348 349
		return lux_val;
	}
350

351 352
	gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target)
			* chip->als_settings.als_gain_trim) / lux_val);
353
	if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
354
		dev_err(&chip->client->dev,
355 356
			"%s: trim_val of %d is not within the range [250, 4000]\n",
			__func__, gain_trim_val);
357 358
		return -ENODATA;
	}
359

360
	chip->als_settings.als_gain_trim = (int)gain_trim_val;
361

362
	return 0;
363 364
}

365 366 367 368 369 370
static int tsl2583_set_als_time(struct tsl2583_chip *chip)
{
	int als_count, als_time, ret;
	u8 val;

	/* determine als integration register */
371
	als_count = (chip->als_settings.als_time * 100 + 135) / 270;
372 373 374 375 376 377 378 379
	if (!als_count)
		als_count = 1; /* ensure at least one cycle */

	/* convert back to time (encompasses overrides) */
	als_time = (als_count * 27 + 5) / 10;

	val = 256 - als_count;
	ret = i2c_smbus_write_byte_data(chip->client,
380
					TSL2583_CMD_REG | TSL2583_ALS_TIME,
381 382
					val);
	if (ret < 0) {
383
		dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n",
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
			__func__, val);
		return ret;
	}

	/* set chip struct re scaling and saturation */
	chip->als_saturation = als_count * 922; /* 90% of full scale */
	chip->als_time_scale = (als_time + 25) / 50;

	return ret;
}

static int tsl2583_set_als_gain(struct tsl2583_chip *chip)
{
	int ret;

399
	/* Set the gain based on als_settings struct */
400
	ret = i2c_smbus_write_byte_data(chip->client,
401 402
					TSL2583_CMD_REG | TSL2583_GAIN,
					chip->als_settings.als_gain);
403
	if (ret < 0)
404 405
		dev_err(&chip->client->dev,
			"%s: failed to set the gain to %d\n", __func__,
406
			chip->als_settings.als_gain);
407 408 409 410

	return ret;
}

411 412 413 414 415
static int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state)
{
	int ret;

	ret = i2c_smbus_write_byte_data(chip->client,
416
					TSL2583_CMD_REG | TSL2583_CNTRL, state);
417
	if (ret < 0)
418 419 420
		dev_err(&chip->client->dev,
			"%s: failed to set the power state to %d\n", __func__,
			state);
421 422 423 424

	return ret;
}

425 426 427 428
/*
 * Turn the device on.
 * Configuration must be set before calling this function.
 */
429
static int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev)
430
{
431
	struct tsl2583_chip *chip = iio_priv(indio_dev);
432
	int ret;
433

434
	/* Power on the device; ADC off. */
435
	ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON);
436 437 438 439
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(chip->client,
440
					TSL2583_CMD_REG | TSL2583_INTERRUPT,
441 442
					TSL2583_INTERRUPT_DISABLED);
	if (ret < 0) {
443 444
		dev_err(&chip->client->dev,
			"%s: failed to disable interrupts\n", __func__);
445 446 447
		return ret;
	}

448 449
	ret = tsl2583_set_als_time(chip);
	if (ret < 0)
450
		return ret;
451

452 453
	ret = tsl2583_set_als_gain(chip);
	if (ret < 0)
454
		return ret;
455

456
	usleep_range(3000, 3500);
457

458 459
	ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON |
					    TSL2583_CNTL_ADC_ENBL);
460
	if (ret < 0)
461
		return ret;
462

463
	chip->suspended = false;
464 465 466 467 468 469

	return ret;
}

/* Sysfs Interface Functions */

470 471 472
static ssize_t in_illuminance_input_target_show(struct device *dev,
						struct device_attribute *attr,
						char *buf)
473
{
474
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
475
	struct tsl2583_chip *chip = iio_priv(indio_dev);
476 477 478
	int ret;

	mutex_lock(&chip->als_mutex);
479
	ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target);
480
	mutex_unlock(&chip->als_mutex);
481

482
	return ret;
483 484
}

485 486 487
static ssize_t in_illuminance_input_target_store(struct device *dev,
						 struct device_attribute *attr,
						 const char *buf, size_t len)
488
{
489
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
490
	struct tsl2583_chip *chip = iio_priv(indio_dev);
491
	int value;
492

493
	if (kstrtoint(buf, 0, &value) || !value)
494 495
		return -EINVAL;

496
	mutex_lock(&chip->als_mutex);
497
	chip->als_settings.als_cal_target = value;
498
	mutex_unlock(&chip->als_mutex);
499 500 501 502

	return len;
}

503 504 505
static ssize_t in_illuminance_calibrate_store(struct device *dev,
					      struct device_attribute *attr,
					      const char *buf, size_t len)
506
{
507
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
508
	struct tsl2583_chip *chip = iio_priv(indio_dev);
509
	int value, ret;
510

511
	if (kstrtoint(buf, 0, &value) || value != 1)
512 513
		return -EINVAL;

514
	mutex_lock(&chip->als_mutex);
515

516
	if (chip->suspended) {
517 518 519 520
		ret = -EBUSY;
		goto done;
	}

521
	ret = tsl2583_als_calibrate(indio_dev);
522 523 524 525 526
	if (ret < 0)
		goto done;

	ret = len;
done:
527
	mutex_unlock(&chip->als_mutex);
528

529
	return ret;
530 531
}

532 533 534
static ssize_t in_illuminance_lux_table_show(struct device *dev,
					     struct device_attribute *attr,
					     char *buf)
535
{
536 537
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct tsl2583_chip *chip = iio_priv(indio_dev);
538
	unsigned int i;
539 540
	int offset = 0;

541
	for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) {
542
		offset += sprintf(buf + offset, "%u,%u,%u,",
543 544 545 546
				  chip->als_settings.als_device_lux[i].ratio,
				  chip->als_settings.als_device_lux[i].ch0,
				  chip->als_settings.als_device_lux[i].ch1);
		if (chip->als_settings.als_device_lux[i].ratio == 0) {
547 548 549 550
			/*
			 * We just printed the first "0" entry.
			 * Now get rid of the extra "," and break.
			 */
551 552 553 554 555 556
			offset--;
			break;
		}
	}

	offset += sprintf(buf + offset, "\n");
557

558 559 560
	return offset;
}

561 562 563
static ssize_t in_illuminance_lux_table_store(struct device *dev,
					      struct device_attribute *attr,
					      const char *buf, size_t len)
564
{
565
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
566
	struct tsl2583_chip *chip = iio_priv(indio_dev);
567
	const unsigned int max_ints = TSL2583_MAX_LUX_TABLE_ENTRIES * 3;
568
	int value[TSL2583_MAX_LUX_TABLE_ENTRIES * 3 + 1];
569 570
	int ret = -EINVAL;
	unsigned int n;
571

572 573
	mutex_lock(&chip->als_mutex);

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

576 577
	/*
	 * We now have an array of ints starting at value[1], and
578 579 580 581 582
	 * enumerated by value[0].
	 * We expect each group of three ints is one table entry,
	 * and the last table entry is all 0.
	 */
	n = value[0];
583
	if ((n % 3) || n < 6 || n > max_ints) {
584
		dev_err(dev,
585 586
			"%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
			__func__, max_ints);
587
		goto done;
588
	}
589
	if ((value[n - 2] | value[n - 1] | value[n]) != 0) {
590 591
		dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n",
			__func__);
592
		goto done;
593 594
	}

595 596
	memcpy(chip->als_settings.als_device_lux, &value[1],
	       value[0] * sizeof(value[1]));
597

598 599 600
	ret = len;

done:
601 602
	mutex_unlock(&chip->als_mutex);

603
	return ret;
604 605
}

606 607 608 609 610 611
static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111");
static IIO_CONST_ATTR(in_illuminance_integration_time_available,
		      "0.000050 0.000100 0.000150 0.000200 0.000250 0.000300 0.000350 0.000400 0.000450 0.000500 0.000550 0.000600 0.000650");
static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0);
static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0);
static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0);
612 613

static struct attribute *sysfs_attrs_ctrl[] = {
614 615 616 617 618
	&iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr,
	&iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
	&iio_dev_attr_in_illuminance_input_target.dev_attr.attr,
	&iio_dev_attr_in_illuminance_calibrate.dev_attr.attr,
	&iio_dev_attr_in_illuminance_lux_table.dev_attr.attr,
619 620 621
	NULL
};

622
static const struct attribute_group tsl2583_attribute_group = {
623 624 625
	.attrs = sysfs_attrs_ctrl,
};

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
static const struct iio_chan_spec tsl2583_channels[] = {
	{
		.type = IIO_LIGHT,
		.modified = 1,
		.channel2 = IIO_MOD_LIGHT_IR,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
	},
	{
		.type = IIO_LIGHT,
		.modified = 1,
		.channel2 = IIO_MOD_LIGHT_BOTH,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
	},
	{
		.type = IIO_LIGHT,
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
				      BIT(IIO_CHAN_INFO_CALIBBIAS) |
643
				      BIT(IIO_CHAN_INFO_CALIBSCALE) |
644 645 646 647 648 649 650 651 652 653 654 655 656
				      BIT(IIO_CHAN_INFO_INT_TIME),
	},
};

static int tsl2583_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long mask)
{
	struct tsl2583_chip *chip = iio_priv(indio_dev);
	int ret = -EINVAL;

	mutex_lock(&chip->als_mutex);

657
	if (chip->suspended) {
658 659 660 661 662 663 664
		ret = -EBUSY;
		goto read_done;
	}

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		if (chan->type == IIO_LIGHT) {
665
			ret = tsl2583_get_lux(indio_dev);
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
			if (ret < 0)
				goto read_done;

			/*
			 * From page 20 of the TSL2581, TSL2583 data
			 * sheet (TAOS134 − MARCH 2011):
			 *
			 * One of the photodiodes (channel 0) is
			 * sensitive to both visible and infrared light,
			 * while the second photodiode (channel 1) is
			 * sensitive primarily to infrared light.
			 */
			if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
				*val = chip->als_cur_info.als_ch0;
			else
				*val = chip->als_cur_info.als_ch1;

			ret = IIO_VAL_INT;
		}
		break;
	case IIO_CHAN_INFO_PROCESSED:
		if (chan->type == IIO_LIGHT) {
688
			ret = tsl2583_get_lux(indio_dev);
689 690 691 692 693 694 695 696 697
			if (ret < 0)
				goto read_done;

			*val = ret;
			ret = IIO_VAL_INT;
		}
		break;
	case IIO_CHAN_INFO_CALIBBIAS:
		if (chan->type == IIO_LIGHT) {
698
			*val = chip->als_settings.als_gain_trim;
699 700 701
			ret = IIO_VAL_INT;
		}
		break;
702 703
	case IIO_CHAN_INFO_CALIBSCALE:
		if (chan->type == IIO_LIGHT) {
704
			*val = gainadj[chip->als_settings.als_gain].mean;
705 706 707
			ret = IIO_VAL_INT;
		}
		break;
708 709 710
	case IIO_CHAN_INFO_INT_TIME:
		if (chan->type == IIO_LIGHT) {
			*val = 0;
711
			*val2 = chip->als_settings.als_time;
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
			ret = IIO_VAL_INT_PLUS_MICRO;
		}
		break;
	default:
		break;
	}

read_done:
	mutex_unlock(&chip->als_mutex);

	return ret;
}

static int tsl2583_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{
	struct tsl2583_chip *chip = iio_priv(indio_dev);
	int ret = -EINVAL;

	mutex_lock(&chip->als_mutex);

734
	if (chip->suspended) {
735 736 737 738 739 740 741
		ret = -EBUSY;
		goto write_done;
	}

	switch (mask) {
	case IIO_CHAN_INFO_CALIBBIAS:
		if (chan->type == IIO_LIGHT) {
742
			chip->als_settings.als_gain_trim = val;
743 744 745
			ret = 0;
		}
		break;
746 747
	case IIO_CHAN_INFO_CALIBSCALE:
		if (chan->type == IIO_LIGHT) {
748
			unsigned int i;
749 750 751

			for (i = 0; i < ARRAY_SIZE(gainadj); i++) {
				if (gainadj[i].mean == val) {
752
					chip->als_settings.als_gain = i;
753
					ret = tsl2583_set_als_gain(chip);
754 755 756 757 758
					break;
				}
			}
		}
		break;
759 760 761
	case IIO_CHAN_INFO_INT_TIME:
		if (chan->type == IIO_LIGHT && !val && val2 >= 50 &&
		    val2 <= 650 && !(val2 % 50)) {
762
			chip->als_settings.als_time = val2;
763
			ret = tsl2583_set_als_time(chip);
764 765 766 767 768 769 770 771 772 773 774 775
		}
		break;
	default:
		break;
	}

write_done:
	mutex_unlock(&chip->als_mutex);

	return ret;
}

776 777 778
static const struct iio_info tsl2583_info = {
	.attrs = &tsl2583_attribute_group,
	.driver_module = THIS_MODULE,
779 780
	.read_raw = tsl2583_read_raw,
	.write_raw = tsl2583_write_raw,
781 782
};

783 784
static int tsl2583_probe(struct i2c_client *clientp,
			 const struct i2c_device_id *idp)
785
{
786
	int ret;
787 788
	struct tsl2583_chip *chip;
	struct iio_dev *indio_dev;
789 790

	if (!i2c_check_functionality(clientp->adapter,
791
				     I2C_FUNC_SMBUS_BYTE_DATA)) {
792 793
		dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n",
			__func__);
794 795 796
		return -EOPNOTSUPP;
	}

797 798 799
	indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
	if (!indio_dev)
		return -ENOMEM;
800

801
	chip = iio_priv(indio_dev);
802
	chip->client = clientp;
803
	i2c_set_clientdata(clientp, indio_dev);
804 805

	mutex_init(&chip->als_mutex);
806
	chip->suspended = true;
807

808
	ret = i2c_smbus_read_byte_data(clientp,
809
				       TSL2583_CMD_REG | TSL2583_CHIPID);
810 811
	if (ret < 0) {
		dev_err(&clientp->dev,
812
			"%s: failed to read the chip ID register\n", __func__);
813
		return ret;
814 815
	}

816
	if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) {
817 818
		dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n",
			__func__, ret);
819
		return -EINVAL;
820 821
	}

822
	indio_dev->info = &tsl2583_info;
823 824
	indio_dev->channels = tsl2583_channels;
	indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels);
825 826 827
	indio_dev->dev.parent = &clientp->dev;
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->name = chip->client->name;
828

829
	ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
830
	if (ret) {
831 832
		dev_err(&clientp->dev, "%s: iio registration failed\n",
			__func__);
833
		return ret;
834 835 836
	}

	/* Load up the V2 defaults (these are hard coded defaults for now) */
837
	tsl2583_defaults(chip);
838 839

	/* Make sure the chip is on */
840
	ret = tsl2583_chip_init_and_power_on(indio_dev);
841 842
	if (ret < 0)
		return ret;
843 844

	dev_info(&clientp->dev, "Light sensor found.\n");
845

846 847 848
	return 0;
}

849
static int __maybe_unused tsl2583_suspend(struct device *dev)
850
{
L
Lars-Peter Clausen 已提交
851
	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
852
	struct tsl2583_chip *chip = iio_priv(indio_dev);
853
	int ret;
854 855 856

	mutex_lock(&chip->als_mutex);

857
	ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
858
	chip->suspended = true;
859 860

	mutex_unlock(&chip->als_mutex);
861

862 863 864
	return ret;
}

865
static int __maybe_unused tsl2583_resume(struct device *dev)
866
{
L
Lars-Peter Clausen 已提交
867
	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
868
	struct tsl2583_chip *chip = iio_priv(indio_dev);
869
	int ret;
870 871 872

	mutex_lock(&chip->als_mutex);

873
	ret = tsl2583_chip_init_and_power_on(indio_dev);
874 875

	mutex_unlock(&chip->als_mutex);
876

877 878 879
	return ret;
}

880
static SIMPLE_DEV_PM_OPS(tsl2583_pm_ops, tsl2583_suspend, tsl2583_resume);
881

882
static struct i2c_device_id tsl2583_idtable[] = {
883 884 885 886 887
	{ "tsl2580", 0 },
	{ "tsl2581", 1 },
	{ "tsl2583", 2 },
	{}
};
888
MODULE_DEVICE_TABLE(i2c, tsl2583_idtable);
889

890
static const struct of_device_id tsl2583_of_match[] = {
891 892 893 894 895
	{ .compatible = "amstaos,tsl2580", },
	{ .compatible = "amstaos,tsl2581", },
	{ .compatible = "amstaos,tsl2583", },
	{ },
};
896
MODULE_DEVICE_TABLE(of, tsl2583_of_match);
897

898
/* Driver definition */
899
static struct i2c_driver tsl2583_driver = {
900 901
	.driver = {
		.name = "tsl2583",
902 903
		.pm = &tsl2583_pm_ops,
		.of_match_table = tsl2583_of_match,
904
	},
905 906
	.id_table = tsl2583_idtable,
	.probe = tsl2583_probe,
907
};
908
module_i2c_driver(tsl2583_driver);
909

910 911
MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
912 913
MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
MODULE_LICENSE("GPL");