perf/x86/intel/pt: Allow the disabling of branch tracing
[linux/fpc-iii.git] / drivers / iio / light / tsl2583.c
bloba78b6025c465217d78e0bfdde930b35418ffbc0e
1 /*
2 * Device driver for monitoring ambient light intensity (lux)
3 * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
5 * Copyright (c) 2011, TAOS Corporation.
6 * Copyright (c) 2016 Brian Masney <masneyb@onstation.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/errno.h>
22 #include <linux/delay.h>
23 #include <linux/string.h>
24 #include <linux/mutex.h>
25 #include <linux/unistd.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
31 /* Device Registers and Masks */
32 #define TSL2583_CNTRL 0x00
33 #define TSL2583_ALS_TIME 0X01
34 #define TSL2583_INTERRUPT 0x02
35 #define TSL2583_GAIN 0x07
36 #define TSL2583_REVID 0x11
37 #define TSL2583_CHIPID 0x12
38 #define TSL2583_ALS_CHAN0LO 0x14
39 #define TSL2583_ALS_CHAN0HI 0x15
40 #define TSL2583_ALS_CHAN1LO 0x16
41 #define TSL2583_ALS_CHAN1HI 0x17
42 #define TSL2583_TMR_LO 0x18
43 #define TSL2583_TMR_HI 0x19
45 /* tsl2583 cmd reg masks */
46 #define TSL2583_CMD_REG 0x80
47 #define TSL2583_CMD_SPL_FN 0x60
48 #define TSL2583_CMD_ALS_INT_CLR 0x01
50 /* tsl2583 cntrl reg masks */
51 #define TSL2583_CNTL_ADC_ENBL 0x02
52 #define TSL2583_CNTL_PWR_OFF 0x00
53 #define TSL2583_CNTL_PWR_ON 0x01
55 /* tsl2583 status reg masks */
56 #define TSL2583_STA_ADC_VALID 0x01
57 #define TSL2583_STA_ADC_INTR 0x10
59 /* Lux calculation constants */
60 #define TSL2583_LUX_CALC_OVER_FLOW 65535
62 #define TSL2583_INTERRUPT_DISABLED 0x00
64 #define TSL2583_CHIP_ID 0x90
65 #define TSL2583_CHIP_ID_MASK 0xf0
67 /* Per-device data */
68 struct tsl2583_als_info {
69 u16 als_ch0;
70 u16 als_ch1;
71 u16 lux;
74 struct tsl2583_lux {
75 unsigned int ratio;
76 unsigned int ch0;
77 unsigned int ch1;
80 static const struct tsl2583_lux tsl2583_default_lux[] = {
81 { 9830, 8520, 15729 },
82 { 12452, 10807, 23344 },
83 { 14746, 6383, 11705 },
84 { 17695, 4063, 6554 },
85 { 0, 0, 0 } /* Termination segment */
88 #define TSL2583_MAX_LUX_TABLE_ENTRIES 11
90 struct tsl2583_settings {
91 int als_time;
92 int als_gain;
93 int als_gain_trim;
94 int als_cal_target;
97 * This structure is intentionally large to accommodate updates via
98 * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
99 * Assumption is that one and only one type of glass used.
101 struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES];
104 struct tsl2583_chip {
105 struct mutex als_mutex;
106 struct i2c_client *client;
107 struct tsl2583_als_info als_cur_info;
108 struct tsl2583_settings als_settings;
109 int als_time_scale;
110 int als_saturation;
111 bool suspended;
114 struct gainadj {
115 s16 ch0;
116 s16 ch1;
117 s16 mean;
120 /* Index = (0 - 3) Used to validate the gain selection index */
121 static const struct gainadj gainadj[] = {
122 { 1, 1, 1 },
123 { 8, 8, 8 },
124 { 16, 16, 16 },
125 { 107, 115, 111 }
129 * Provides initial operational parameter defaults.
130 * These defaults may be changed through the device's sysfs files.
132 static void tsl2583_defaults(struct tsl2583_chip *chip)
135 * The integration time must be a multiple of 50ms and within the
136 * range [50, 600] ms.
138 chip->als_settings.als_time = 100;
141 * This is an index into the gainadj table. Assume clear glass as the
142 * default.
144 chip->als_settings.als_gain = 0;
146 /* Default gain trim to account for aperture effects */
147 chip->als_settings.als_gain_trim = 1000;
149 /* Known external ALS reading used for calibration */
150 chip->als_settings.als_cal_target = 130;
152 /* Default lux table. */
153 memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux,
154 sizeof(tsl2583_default_lux));
158 * Reads and calculates current lux value.
159 * The raw ch0 and ch1 values of the ambient light sensed in the last
160 * integration cycle are read from the device.
161 * Time scale factor array values are adjusted based on the integration time.
162 * The raw values are multiplied by a scale factor, and device gain is obtained
163 * 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 * declared above is then scanned to find the first ratio value that is just
166 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
167 * the array are then used along with the time scale factor array values, to
168 * calculate the lux.
170 static int tsl2583_get_lux(struct iio_dev *indio_dev)
172 u16 ch0, ch1; /* separated ch0/ch1 data from device */
173 u32 lux; /* raw lux calculated from device data */
174 u64 lux64;
175 u32 ratio;
176 u8 buf[5];
177 struct tsl2583_lux *p;
178 struct tsl2583_chip *chip = iio_priv(indio_dev);
179 int i, ret;
181 ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG);
182 if (ret < 0) {
183 dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n",
184 __func__);
185 goto done;
188 /* is data new & valid */
189 if (!(ret & TSL2583_STA_ADC_INTR)) {
190 dev_err(&chip->client->dev, "%s: data not valid; returning last value\n",
191 __func__);
192 ret = chip->als_cur_info.lux; /* return LAST VALUE */
193 goto done;
196 for (i = 0; i < 4; i++) {
197 int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i);
199 ret = i2c_smbus_read_byte_data(chip->client, reg);
200 if (ret < 0) {
201 dev_err(&chip->client->dev, "%s: failed to read register %x\n",
202 __func__, reg);
203 goto done;
205 buf[i] = ret;
209 * Clear the pending interrupt status bit on the chip to allow the next
210 * integration cycle to start. This has to be done even though this
211 * driver currently does not support interrupts.
213 ret = i2c_smbus_write_byte(chip->client,
214 (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN |
215 TSL2583_CMD_ALS_INT_CLR));
216 if (ret < 0) {
217 dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n",
218 __func__);
219 goto done; /* have no data, so return failure */
222 /* extract ALS/lux data */
223 ch0 = le16_to_cpup((const __le16 *)&buf[0]);
224 ch1 = le16_to_cpup((const __le16 *)&buf[2]);
226 chip->als_cur_info.als_ch0 = ch0;
227 chip->als_cur_info.als_ch1 = ch1;
229 if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
230 goto return_max;
232 if (!ch0) {
234 * The sensor appears to be in total darkness so set the
235 * calculated lux to 0 and return early to avoid a division by
236 * zero below when calculating the ratio.
238 ret = 0;
239 chip->als_cur_info.lux = 0;
240 goto done;
243 /* calculate ratio */
244 ratio = (ch1 << 15) / ch0;
246 /* convert to unscaled lux using the pointer to the table */
247 for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux;
248 p->ratio != 0 && p->ratio < ratio; p++)
251 if (p->ratio == 0) {
252 lux = 0;
253 } else {
254 u32 ch0lux, ch1lux;
256 ch0lux = ((ch0 * p->ch0) +
257 (gainadj[chip->als_settings.als_gain].ch0 >> 1))
258 / gainadj[chip->als_settings.als_gain].ch0;
259 ch1lux = ((ch1 * p->ch1) +
260 (gainadj[chip->als_settings.als_gain].ch1 >> 1))
261 / gainadj[chip->als_settings.als_gain].ch1;
263 /* note: lux is 31 bit max at this point */
264 if (ch1lux > ch0lux) {
265 dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n",
266 __func__);
267 ret = 0;
268 chip->als_cur_info.lux = 0;
269 goto done;
272 lux = ch0lux - ch1lux;
275 /* adjust for active time scale */
276 if (chip->als_time_scale == 0)
277 lux = 0;
278 else
279 lux = (lux + (chip->als_time_scale >> 1)) /
280 chip->als_time_scale;
283 * Adjust for active gain scale.
284 * The tsl2583_default_lux tables above have a factor of 8192 built in,
285 * so we need to shift right.
286 * User-specified gain provides a multiplier.
287 * Apply user-specified gain before shifting right to retain precision.
288 * Use 64 bits to avoid overflow on multiplication.
289 * Then go back to 32 bits before division to avoid using div_u64().
291 lux64 = lux;
292 lux64 = lux64 * chip->als_settings.als_gain_trim;
293 lux64 >>= 13;
294 lux = lux64;
295 lux = (lux + 500) / 1000;
297 if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
298 return_max:
299 lux = TSL2583_LUX_CALC_OVER_FLOW;
302 /* Update the structure with the latest VALID lux. */
303 chip->als_cur_info.lux = lux;
304 ret = lux;
306 done:
307 return ret;
311 * Obtain single reading and calculate the als_gain_trim (later used
312 * to derive actual lux).
313 * Return updated gain_trim value.
315 static int tsl2583_als_calibrate(struct iio_dev *indio_dev)
317 struct tsl2583_chip *chip = iio_priv(indio_dev);
318 unsigned int gain_trim_val;
319 int ret;
320 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 __func__);
328 return ret;
331 if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON))
332 != (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) {
333 dev_err(&chip->client->dev,
334 "%s: Device is not powered on and/or ADC is not enabled\n",
335 __func__);
336 return -EINVAL;
337 } else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) {
338 dev_err(&chip->client->dev,
339 "%s: The two ADC channels have not completed an integration cycle\n",
340 __func__);
341 return -ENODATA;
344 lux_val = tsl2583_get_lux(indio_dev);
345 if (lux_val < 0) {
346 dev_err(&chip->client->dev, "%s: failed to get lux\n",
347 __func__);
348 return lux_val;
351 gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target)
352 * 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 "%s: trim_val of %d is not within the range [250, 4000]\n",
356 __func__, gain_trim_val);
357 return -ENODATA;
360 chip->als_settings.als_gain_trim = (int)gain_trim_val;
362 return 0;
365 static int tsl2583_set_als_time(struct tsl2583_chip *chip)
367 int als_count, als_time, ret;
368 u8 val;
370 /* determine als integration register */
371 als_count = (chip->als_settings.als_time * 100 + 135) / 270;
372 if (!als_count)
373 als_count = 1; /* ensure at least one cycle */
375 /* convert back to time (encompasses overrides) */
376 als_time = (als_count * 27 + 5) / 10;
378 val = 256 - als_count;
379 ret = i2c_smbus_write_byte_data(chip->client,
380 TSL2583_CMD_REG | TSL2583_ALS_TIME,
381 val);
382 if (ret < 0) {
383 dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n",
384 __func__, val);
385 return ret;
388 /* set chip struct re scaling and saturation */
389 chip->als_saturation = als_count * 922; /* 90% of full scale */
390 chip->als_time_scale = (als_time + 25) / 50;
392 return ret;
395 static int tsl2583_set_als_gain(struct tsl2583_chip *chip)
397 int ret;
399 /* Set the gain based on als_settings struct */
400 ret = i2c_smbus_write_byte_data(chip->client,
401 TSL2583_CMD_REG | TSL2583_GAIN,
402 chip->als_settings.als_gain);
403 if (ret < 0)
404 dev_err(&chip->client->dev,
405 "%s: failed to set the gain to %d\n", __func__,
406 chip->als_settings.als_gain);
408 return ret;
411 static int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state)
413 int ret;
415 ret = i2c_smbus_write_byte_data(chip->client,
416 TSL2583_CMD_REG | TSL2583_CNTRL, state);
417 if (ret < 0)
418 dev_err(&chip->client->dev,
419 "%s: failed to set the power state to %d\n", __func__,
420 state);
422 return ret;
426 * Turn the device on.
427 * Configuration must be set before calling this function.
429 static int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev)
431 struct tsl2583_chip *chip = iio_priv(indio_dev);
432 int ret;
434 /* Power on the device; ADC off. */
435 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON);
436 if (ret < 0)
437 return ret;
439 ret = i2c_smbus_write_byte_data(chip->client,
440 TSL2583_CMD_REG | TSL2583_INTERRUPT,
441 TSL2583_INTERRUPT_DISABLED);
442 if (ret < 0) {
443 dev_err(&chip->client->dev,
444 "%s: failed to disable interrupts\n", __func__);
445 return ret;
448 ret = tsl2583_set_als_time(chip);
449 if (ret < 0)
450 return ret;
452 ret = tsl2583_set_als_gain(chip);
453 if (ret < 0)
454 return ret;
456 usleep_range(3000, 3500);
458 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON |
459 TSL2583_CNTL_ADC_ENBL);
460 if (ret < 0)
461 return ret;
463 chip->suspended = false;
465 return ret;
468 /* Sysfs Interface Functions */
470 static ssize_t in_illuminance_input_target_show(struct device *dev,
471 struct device_attribute *attr,
472 char *buf)
474 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
475 struct tsl2583_chip *chip = iio_priv(indio_dev);
476 int ret;
478 mutex_lock(&chip->als_mutex);
479 ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target);
480 mutex_unlock(&chip->als_mutex);
482 return ret;
485 static ssize_t in_illuminance_input_target_store(struct device *dev,
486 struct device_attribute *attr,
487 const char *buf, size_t len)
489 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
490 struct tsl2583_chip *chip = iio_priv(indio_dev);
491 int value;
493 if (kstrtoint(buf, 0, &value) || !value)
494 return -EINVAL;
496 mutex_lock(&chip->als_mutex);
497 chip->als_settings.als_cal_target = value;
498 mutex_unlock(&chip->als_mutex);
500 return len;
503 static ssize_t in_illuminance_calibrate_store(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t len)
507 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
508 struct tsl2583_chip *chip = iio_priv(indio_dev);
509 int value, ret;
511 if (kstrtoint(buf, 0, &value) || value != 1)
512 return -EINVAL;
514 mutex_lock(&chip->als_mutex);
516 if (chip->suspended) {
517 ret = -EBUSY;
518 goto done;
521 ret = tsl2583_als_calibrate(indio_dev);
522 if (ret < 0)
523 goto done;
525 ret = len;
526 done:
527 mutex_unlock(&chip->als_mutex);
529 return ret;
532 static ssize_t in_illuminance_lux_table_show(struct device *dev,
533 struct device_attribute *attr,
534 char *buf)
536 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
537 struct tsl2583_chip *chip = iio_priv(indio_dev);
538 unsigned int i;
539 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 chip->als_settings.als_device_lux[i].ratio,
544 chip->als_settings.als_device_lux[i].ch0,
545 chip->als_settings.als_device_lux[i].ch1);
546 if (chip->als_settings.als_device_lux[i].ratio == 0) {
548 * We just printed the first "0" entry.
549 * Now get rid of the extra "," and break.
551 offset--;
552 break;
556 offset += sprintf(buf + offset, "\n");
558 return offset;
561 static ssize_t in_illuminance_lux_table_store(struct device *dev,
562 struct device_attribute *attr,
563 const char *buf, size_t len)
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 int ret = -EINVAL;
570 unsigned int n;
572 mutex_lock(&chip->als_mutex);
574 get_options(buf, ARRAY_SIZE(value), value);
577 * We now have an array of ints starting at value[1], and
578 * enumerated by value[0].
579 * We expect each group of three ints is one table entry,
580 * and the last table entry is all 0.
582 n = value[0];
583 if ((n % 3) || n < 6 || n > max_ints) {
584 dev_err(dev,
585 "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
586 __func__, max_ints);
587 goto done;
589 if ((value[n - 2] | value[n - 1] | value[n]) != 0) {
590 dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n",
591 __func__);
592 goto done;
595 memcpy(chip->als_settings.als_device_lux, &value[1],
596 value[0] * sizeof(value[1]));
598 ret = len;
600 done:
601 mutex_unlock(&chip->als_mutex);
603 return ret;
606 static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111");
607 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
608 "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");
609 static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0);
610 static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0);
611 static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0);
613 static struct attribute *sysfs_attrs_ctrl[] = {
614 &iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr,
615 &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
616 &iio_dev_attr_in_illuminance_input_target.dev_attr.attr,
617 &iio_dev_attr_in_illuminance_calibrate.dev_attr.attr,
618 &iio_dev_attr_in_illuminance_lux_table.dev_attr.attr,
619 NULL
622 static const struct attribute_group tsl2583_attribute_group = {
623 .attrs = sysfs_attrs_ctrl,
626 static const struct iio_chan_spec tsl2583_channels[] = {
628 .type = IIO_LIGHT,
629 .modified = 1,
630 .channel2 = IIO_MOD_LIGHT_IR,
631 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
634 .type = IIO_LIGHT,
635 .modified = 1,
636 .channel2 = IIO_MOD_LIGHT_BOTH,
637 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
640 .type = IIO_LIGHT,
641 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
642 BIT(IIO_CHAN_INFO_CALIBBIAS) |
643 BIT(IIO_CHAN_INFO_CALIBSCALE) |
644 BIT(IIO_CHAN_INFO_INT_TIME),
648 static int tsl2583_read_raw(struct iio_dev *indio_dev,
649 struct iio_chan_spec const *chan,
650 int *val, int *val2, long mask)
652 struct tsl2583_chip *chip = iio_priv(indio_dev);
653 int ret = -EINVAL;
655 mutex_lock(&chip->als_mutex);
657 if (chip->suspended) {
658 ret = -EBUSY;
659 goto read_done;
662 switch (mask) {
663 case IIO_CHAN_INFO_RAW:
664 if (chan->type == IIO_LIGHT) {
665 ret = tsl2583_get_lux(indio_dev);
666 if (ret < 0)
667 goto read_done;
670 * From page 20 of the TSL2581, TSL2583 data
671 * sheet (TAOS134 − MARCH 2011):
673 * One of the photodiodes (channel 0) is
674 * sensitive to both visible and infrared light,
675 * while the second photodiode (channel 1) is
676 * sensitive primarily to infrared light.
678 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
679 *val = chip->als_cur_info.als_ch0;
680 else
681 *val = chip->als_cur_info.als_ch1;
683 ret = IIO_VAL_INT;
685 break;
686 case IIO_CHAN_INFO_PROCESSED:
687 if (chan->type == IIO_LIGHT) {
688 ret = tsl2583_get_lux(indio_dev);
689 if (ret < 0)
690 goto read_done;
692 *val = ret;
693 ret = IIO_VAL_INT;
695 break;
696 case IIO_CHAN_INFO_CALIBBIAS:
697 if (chan->type == IIO_LIGHT) {
698 *val = chip->als_settings.als_gain_trim;
699 ret = IIO_VAL_INT;
701 break;
702 case IIO_CHAN_INFO_CALIBSCALE:
703 if (chan->type == IIO_LIGHT) {
704 *val = gainadj[chip->als_settings.als_gain].mean;
705 ret = IIO_VAL_INT;
707 break;
708 case IIO_CHAN_INFO_INT_TIME:
709 if (chan->type == IIO_LIGHT) {
710 *val = 0;
711 *val2 = chip->als_settings.als_time;
712 ret = IIO_VAL_INT_PLUS_MICRO;
714 break;
715 default:
716 break;
719 read_done:
720 mutex_unlock(&chip->als_mutex);
722 return ret;
725 static int tsl2583_write_raw(struct iio_dev *indio_dev,
726 struct iio_chan_spec const *chan,
727 int val, int val2, long mask)
729 struct tsl2583_chip *chip = iio_priv(indio_dev);
730 int ret = -EINVAL;
732 mutex_lock(&chip->als_mutex);
734 if (chip->suspended) {
735 ret = -EBUSY;
736 goto write_done;
739 switch (mask) {
740 case IIO_CHAN_INFO_CALIBBIAS:
741 if (chan->type == IIO_LIGHT) {
742 chip->als_settings.als_gain_trim = val;
743 ret = 0;
745 break;
746 case IIO_CHAN_INFO_CALIBSCALE:
747 if (chan->type == IIO_LIGHT) {
748 unsigned int i;
750 for (i = 0; i < ARRAY_SIZE(gainadj); i++) {
751 if (gainadj[i].mean == val) {
752 chip->als_settings.als_gain = i;
753 ret = tsl2583_set_als_gain(chip);
754 break;
758 break;
759 case IIO_CHAN_INFO_INT_TIME:
760 if (chan->type == IIO_LIGHT && !val && val2 >= 50 &&
761 val2 <= 650 && !(val2 % 50)) {
762 chip->als_settings.als_time = val2;
763 ret = tsl2583_set_als_time(chip);
765 break;
766 default:
767 break;
770 write_done:
771 mutex_unlock(&chip->als_mutex);
773 return ret;
776 static const struct iio_info tsl2583_info = {
777 .attrs = &tsl2583_attribute_group,
778 .driver_module = THIS_MODULE,
779 .read_raw = tsl2583_read_raw,
780 .write_raw = tsl2583_write_raw,
783 static int tsl2583_probe(struct i2c_client *clientp,
784 const struct i2c_device_id *idp)
786 int ret;
787 struct tsl2583_chip *chip;
788 struct iio_dev *indio_dev;
790 if (!i2c_check_functionality(clientp->adapter,
791 I2C_FUNC_SMBUS_BYTE_DATA)) {
792 dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n",
793 __func__);
794 return -EOPNOTSUPP;
797 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
798 if (!indio_dev)
799 return -ENOMEM;
801 chip = iio_priv(indio_dev);
802 chip->client = clientp;
803 i2c_set_clientdata(clientp, indio_dev);
805 mutex_init(&chip->als_mutex);
806 chip->suspended = true;
808 ret = i2c_smbus_read_byte_data(clientp,
809 TSL2583_CMD_REG | TSL2583_CHIPID);
810 if (ret < 0) {
811 dev_err(&clientp->dev,
812 "%s: failed to read the chip ID register\n", __func__);
813 return ret;
816 if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) {
817 dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n",
818 __func__, ret);
819 return -EINVAL;
822 indio_dev->info = &tsl2583_info;
823 indio_dev->channels = tsl2583_channels;
824 indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels);
825 indio_dev->dev.parent = &clientp->dev;
826 indio_dev->modes = INDIO_DIRECT_MODE;
827 indio_dev->name = chip->client->name;
829 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
830 if (ret) {
831 dev_err(&clientp->dev, "%s: iio registration failed\n",
832 __func__);
833 return ret;
836 /* Load up the V2 defaults (these are hard coded defaults for now) */
837 tsl2583_defaults(chip);
839 /* Make sure the chip is on */
840 ret = tsl2583_chip_init_and_power_on(indio_dev);
841 if (ret < 0)
842 return ret;
844 dev_info(&clientp->dev, "Light sensor found.\n");
846 return 0;
849 static int __maybe_unused tsl2583_suspend(struct device *dev)
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;
855 mutex_lock(&chip->als_mutex);
857 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
858 chip->suspended = true;
860 mutex_unlock(&chip->als_mutex);
862 return ret;
865 static int __maybe_unused tsl2583_resume(struct device *dev)
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;
871 mutex_lock(&chip->als_mutex);
873 ret = tsl2583_chip_init_and_power_on(indio_dev);
875 mutex_unlock(&chip->als_mutex);
877 return ret;
880 static SIMPLE_DEV_PM_OPS(tsl2583_pm_ops, tsl2583_suspend, tsl2583_resume);
882 static struct i2c_device_id tsl2583_idtable[] = {
883 { "tsl2580", 0 },
884 { "tsl2581", 1 },
885 { "tsl2583", 2 },
888 MODULE_DEVICE_TABLE(i2c, tsl2583_idtable);
890 static const struct of_device_id tsl2583_of_match[] = {
891 { .compatible = "amstaos,tsl2580", },
892 { .compatible = "amstaos,tsl2581", },
893 { .compatible = "amstaos,tsl2583", },
894 { },
896 MODULE_DEVICE_TABLE(of, tsl2583_of_match);
898 /* Driver definition */
899 static struct i2c_driver tsl2583_driver = {
900 .driver = {
901 .name = "tsl2583",
902 .pm = &tsl2583_pm_ops,
903 .of_match_table = tsl2583_of_match,
905 .id_table = tsl2583_idtable,
906 .probe = tsl2583_probe,
908 module_i2c_driver(tsl2583_driver);
910 MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
911 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
912 MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
913 MODULE_LICENSE("GPL");