2 * Device driver for monitoring ambient light intensity (lux)
3 * within the TAOS tsl258x family of devices (tsl2580, tsl2581).
5 * Copyright (c) 2011, TAOS Corporation.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <linux/kernel.h>
23 #include <linux/i2c.h>
24 #include <linux/errno.h>
25 #include <linux/delay.h>
26 #include <linux/string.h>
27 #include <linux/mutex.h>
28 #include <linux/unistd.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <linux/iio/iio.h>
33 #define TSL258X_MAX_DEVICE_REGS 32
35 /* Triton register offsets */
36 #define TSL258X_REG_MAX 8
38 /* Device Registers and Masks */
39 #define TSL258X_CNTRL 0x00
40 #define TSL258X_ALS_TIME 0X01
41 #define TSL258X_INTERRUPT 0x02
42 #define TSL258X_GAIN 0x07
43 #define TSL258X_REVID 0x11
44 #define TSL258X_CHIPID 0x12
45 #define TSL258X_ALS_CHAN0LO 0x14
46 #define TSL258X_ALS_CHAN0HI 0x15
47 #define TSL258X_ALS_CHAN1LO 0x16
48 #define TSL258X_ALS_CHAN1HI 0x17
49 #define TSL258X_TMR_LO 0x18
50 #define TSL258X_TMR_HI 0x19
52 /* tsl2583 cmd reg masks */
53 #define TSL258X_CMD_REG 0x80
54 #define TSL258X_CMD_SPL_FN 0x60
55 #define TSL258X_CMD_ALS_INT_CLR 0X01
57 /* tsl2583 cntrl reg masks */
58 #define TSL258X_CNTL_ADC_ENBL 0x02
59 #define TSL258X_CNTL_PWR_ON 0x01
61 /* tsl2583 status reg masks */
62 #define TSL258X_STA_ADC_VALID 0x01
63 #define TSL258X_STA_ADC_INTR 0x10
65 /* Lux calculation constants */
66 #define TSL258X_LUX_CALC_OVER_FLOW 65535
69 TSL258X_CHIP_UNKNOWN = 0,
70 TSL258X_CHIP_WORKING = 1,
71 TSL258X_CHIP_SUSPENDED = 2
75 struct taos_als_info {
81 struct taos_settings {
89 struct mutex als_mutex;
90 struct i2c_client *client;
91 struct taos_als_info als_cur_info;
92 struct taos_settings taos_settings;
100 * Initial values for device - this values can/will be changed by driver.
101 * and applications as needed.
102 * These values are dynamic.
104 static const u8 taos_config[8] = {
105 0x00, 0xee, 0x00, 0x03, 0x00, 0xFF, 0xFF, 0x00
106 }; /* cntrl atime intC Athl0 Athl1 Athh0 Athh1 gain */
114 /* This structure is intentionally large to accommodate updates via sysfs. */
115 /* Sized to 11 = max 10 segments + 1 termination segment */
116 /* Assumption is one and only one type of glass used */
117 static struct taos_lux taos_device_lux[11] = {
118 { 9830, 8520, 15729 },
119 { 12452, 10807, 23344 },
120 { 14746, 6383, 11705 },
121 { 17695, 4063, 6554 },
129 /* Index = (0 - 3) Used to validate the gain selection index */
130 static const struct gainadj gainadj[] = {
138 * Provides initial operational parameter defaults.
139 * These defaults may be changed through the device's sysfs files.
141 static void taos_defaults(struct tsl2583_chip *chip)
143 /* Operational parameters */
144 chip->taos_settings.als_time = 100;
145 /* must be a multiple of 50mS */
146 chip->taos_settings.als_gain = 0;
147 /* this is actually an index into the gain table */
148 /* assume clear glass as default */
149 chip->taos_settings.als_gain_trim = 1000;
150 /* default gain trim to account for aperture effects */
151 chip->taos_settings.als_cal_target = 130;
152 /* Known external ALS reading used for calibration */
156 * Read a number of bytes starting at register (reg) location.
157 * Return 0, or i2c_smbus_write_byte ERROR code.
160 taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, unsigned int len)
164 for (i = 0; i < len; i++) {
165 /* select register to write */
166 ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | reg));
168 dev_err(&client->dev,
169 "taos_i2c_read failed to write register %x\n",
174 *val = i2c_smbus_read_byte(client);
182 * Reads and calculates current lux value.
183 * The raw ch0 and ch1 values of the ambient light sensed in the last
184 * integration cycle are read from the device.
185 * Time scale factor array values are adjusted based on the integration time.
186 * The raw values are multiplied by a scale factor, and device gain is obtained
187 * using gain index. Limit checks are done next, then the ratio of a multiple
188 * of ch1 value, to the ch0 value, is calculated. The array taos_device_lux[]
189 * declared above is then scanned to find the first ratio value that is just
190 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
191 * the array are then used along with the time scale factor array values, to
194 static int taos_get_lux(struct iio_dev *indio_dev)
196 u16 ch0, ch1; /* separated ch0/ch1 data from device */
197 u32 lux; /* raw lux calculated from device data */
202 struct tsl2583_chip *chip = iio_priv(indio_dev);
207 if (mutex_trylock(&chip->als_mutex) == 0) {
208 dev_info(&chip->client->dev, "taos_get_lux device is busy\n");
209 return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
212 if (chip->taos_chip_status != TSL258X_CHIP_WORKING) {
213 /* device is not enabled */
214 dev_err(&chip->client->dev, "taos_get_lux device is not enabled\n");
219 ret = taos_i2c_read(chip->client, (TSL258X_CMD_REG), &buf[0], 1);
221 dev_err(&chip->client->dev, "taos_get_lux failed to read CMD_REG\n");
224 /* is data new & valid */
225 if (!(buf[0] & TSL258X_STA_ADC_INTR)) {
226 dev_err(&chip->client->dev, "taos_get_lux data not valid\n");
227 ret = chip->als_cur_info.lux; /* return LAST VALUE */
231 for (i = 0; i < 4; i++) {
232 int reg = TSL258X_CMD_REG | (TSL258X_ALS_CHAN0LO + i);
234 ret = taos_i2c_read(chip->client, reg, &buf[i], 1);
236 dev_err(&chip->client->dev,
237 "taos_get_lux failed to read register %x\n",
244 * clear status, really interrupt status (interrupts are off), but
245 * we use the bit anyway - don't forget 0x80 - this is a command
247 ret = i2c_smbus_write_byte(chip->client,
248 (TSL258X_CMD_REG | TSL258X_CMD_SPL_FN |
249 TSL258X_CMD_ALS_INT_CLR));
252 dev_err(&chip->client->dev,
253 "taos_i2c_write_command failed in taos_get_lux, err = %d\n",
255 goto out_unlock; /* have no data, so return failure */
258 /* extract ALS/lux data */
259 ch0 = le16_to_cpup((const __le16 *)&buf[0]);
260 ch1 = le16_to_cpup((const __le16 *)&buf[2]);
262 chip->als_cur_info.als_ch0 = ch0;
263 chip->als_cur_info.als_ch1 = ch1;
265 if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
269 /* have no data, so return LAST VALUE */
271 chip->als_cur_info.lux = 0;
274 /* calculate ratio */
275 ratio = (ch1 << 15) / ch0;
276 /* convert to unscaled lux using the pointer to the table */
277 for (p = (struct taos_lux *)taos_device_lux;
278 p->ratio != 0 && p->ratio < ratio; p++)
284 ch0lux = ((ch0 * p->ch0) +
285 (gainadj[chip->taos_settings.als_gain].ch0 >> 1))
286 / gainadj[chip->taos_settings.als_gain].ch0;
287 ch1lux = ((ch1 * p->ch1) +
288 (gainadj[chip->taos_settings.als_gain].ch1 >> 1))
289 / gainadj[chip->taos_settings.als_gain].ch1;
290 lux = ch0lux - ch1lux;
293 /* note: lux is 31 bit max at this point */
294 if (ch1lux > ch0lux) {
295 dev_dbg(&chip->client->dev, "No Data - Return last value\n");
297 chip->als_cur_info.lux = 0;
301 /* adjust for active time scale */
302 if (chip->als_time_scale == 0)
305 lux = (lux + (chip->als_time_scale >> 1)) /
306 chip->als_time_scale;
308 /* Adjust for active gain scale.
309 * The taos_device_lux tables above have a factor of 8192 built in,
310 * so we need to shift right.
311 * User-specified gain provides a multiplier.
312 * Apply user-specified gain before shifting right to retain precision.
313 * Use 64 bits to avoid overflow on multiplication.
314 * Then go back to 32 bits before division to avoid using div_u64().
317 lux64 = lux64 * chip->taos_settings.als_gain_trim;
320 lux = (lux + 500) / 1000;
321 if (lux > TSL258X_LUX_CALC_OVER_FLOW) { /* check for overflow */
323 lux = TSL258X_LUX_CALC_OVER_FLOW;
326 /* Update the structure with the latest VALID lux. */
327 chip->als_cur_info.lux = lux;
331 mutex_unlock(&chip->als_mutex);
336 * Obtain single reading and calculate the als_gain_trim (later used
337 * to derive actual lux).
338 * Return updated gain_trim value.
340 static int taos_als_calibrate(struct iio_dev *indio_dev)
342 struct tsl2583_chip *chip = iio_priv(indio_dev);
344 unsigned int gain_trim_val;
348 ret = i2c_smbus_write_byte(chip->client,
349 (TSL258X_CMD_REG | TSL258X_CNTRL));
351 dev_err(&chip->client->dev,
352 "taos_als_calibrate failed to reach the CNTRL register, ret=%d\n",
357 reg_val = i2c_smbus_read_byte(chip->client);
358 if ((reg_val & (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON))
359 != (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) {
360 dev_err(&chip->client->dev,
361 "taos_als_calibrate failed: device not powered on with ADC enabled\n");
365 ret = i2c_smbus_write_byte(chip->client,
366 (TSL258X_CMD_REG | TSL258X_CNTRL));
368 dev_err(&chip->client->dev,
369 "taos_als_calibrate failed to reach the STATUS register, ret=%d\n",
373 reg_val = i2c_smbus_read_byte(chip->client);
375 if ((reg_val & TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) {
376 dev_err(&chip->client->dev,
377 "taos_als_calibrate failed: STATUS - ADC not valid.\n");
380 lux_val = taos_get_lux(indio_dev);
382 dev_err(&chip->client->dev, "taos_als_calibrate failed to get lux\n");
386 /* Avoid division by zero of lux_value later on */
388 dev_err(&chip->client->dev,
389 "%s: lux_val of 0 will produce out of range trim_value\n",
394 gain_trim_val = (unsigned int)(((chip->taos_settings.als_cal_target)
395 * chip->taos_settings.als_gain_trim) / lux_val);
397 if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
398 dev_err(&chip->client->dev,
399 "taos_als_calibrate failed: trim_val of %d is out of range\n",
403 chip->taos_settings.als_gain_trim = (int)gain_trim_val;
405 return (int)gain_trim_val;
409 * Turn the device on.
410 * Configuration must be set before calling this function.
412 static int taos_chip_on(struct iio_dev *indio_dev)
420 struct tsl2583_chip *chip = iio_priv(indio_dev);
422 /* and make sure we're not already on */
423 if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
424 /* if forcing a register update - turn off, then on */
425 dev_info(&chip->client->dev, "device is already enabled\n");
429 /* determine als integration register */
430 als_count = (chip->taos_settings.als_time * 100 + 135) / 270;
432 als_count = 1; /* ensure at least one cycle */
434 /* convert back to time (encompasses overrides) */
435 als_time = (als_count * 27 + 5) / 10;
436 chip->taos_config[TSL258X_ALS_TIME] = 256 - als_count;
438 /* Set the gain based on taos_settings struct */
439 chip->taos_config[TSL258X_GAIN] = chip->taos_settings.als_gain;
441 /* set chip struct re scaling and saturation */
442 chip->als_saturation = als_count * 922; /* 90% of full scale */
443 chip->als_time_scale = (als_time + 25) / 50;
446 * TSL258x Specific power-on / adc enable sequence
447 * Power on the device 1st.
449 utmp = TSL258X_CNTL_PWR_ON;
450 ret = i2c_smbus_write_byte_data(chip->client,
451 TSL258X_CMD_REG | TSL258X_CNTRL, utmp);
453 dev_err(&chip->client->dev, "taos_chip_on failed on CNTRL reg.\n");
458 * Use the following shadow copy for our delay before enabling ADC.
459 * Write all the registers.
461 for (i = 0, uP = chip->taos_config; i < TSL258X_REG_MAX; i++) {
462 ret = i2c_smbus_write_byte_data(chip->client,
466 dev_err(&chip->client->dev,
467 "taos_chip_on failed on reg %d.\n", i);
472 usleep_range(3000, 3500);
475 * initialize the desired mode of operation
477 utmp = TSL258X_CNTL_PWR_ON | TSL258X_CNTL_ADC_ENBL;
478 ret = i2c_smbus_write_byte_data(chip->client,
479 TSL258X_CMD_REG | TSL258X_CNTRL,
482 dev_err(&chip->client->dev, "taos_chip_on failed on 2nd CTRL reg.\n");
485 chip->taos_chip_status = TSL258X_CHIP_WORKING;
490 static int taos_chip_off(struct iio_dev *indio_dev)
492 struct tsl2583_chip *chip = iio_priv(indio_dev);
494 /* turn device off */
495 chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
496 return i2c_smbus_write_byte_data(chip->client,
497 TSL258X_CMD_REG | TSL258X_CNTRL,
501 /* Sysfs Interface Functions */
503 static ssize_t taos_power_state_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
506 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
507 struct tsl2583_chip *chip = iio_priv(indio_dev);
509 return sprintf(buf, "%d\n", chip->taos_chip_status);
512 static ssize_t taos_power_state_store(struct device *dev,
513 struct device_attribute *attr,
514 const char *buf, size_t len)
516 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
519 if (kstrtoint(buf, 0, &value))
523 taos_chip_off(indio_dev);
525 taos_chip_on(indio_dev);
530 static ssize_t taos_gain_show(struct device *dev,
531 struct device_attribute *attr, char *buf)
533 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
534 struct tsl2583_chip *chip = iio_priv(indio_dev);
537 switch (chip->taos_settings.als_gain) {
552 return sprintf(buf, "%s\n", gain);
555 static ssize_t taos_gain_store(struct device *dev,
556 struct device_attribute *attr,
557 const char *buf, size_t len)
559 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
560 struct tsl2583_chip *chip = iio_priv(indio_dev);
563 if (kstrtoint(buf, 0, &value))
568 chip->taos_settings.als_gain = 0;
571 chip->taos_settings.als_gain = 1;
574 chip->taos_settings.als_gain = 2;
577 chip->taos_settings.als_gain = 3;
580 dev_err(dev, "Invalid Gain Index (must be 1,8,16,111)\n");
587 static ssize_t taos_gain_available_show(struct device *dev,
588 struct device_attribute *attr,
591 return sprintf(buf, "%s\n", "1 8 16 111");
594 static ssize_t taos_als_time_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
598 struct tsl2583_chip *chip = iio_priv(indio_dev);
600 return sprintf(buf, "%d\n", chip->taos_settings.als_time);
603 static ssize_t taos_als_time_store(struct device *dev,
604 struct device_attribute *attr,
605 const char *buf, size_t len)
607 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
608 struct tsl2583_chip *chip = iio_priv(indio_dev);
611 if (kstrtoint(buf, 0, &value))
614 if ((value < 50) || (value > 650))
620 chip->taos_settings.als_time = value;
625 static ssize_t taos_als_time_available_show(struct device *dev,
626 struct device_attribute *attr,
629 return sprintf(buf, "%s\n",
630 "50 100 150 200 250 300 350 400 450 500 550 600 650");
633 static ssize_t taos_als_trim_show(struct device *dev,
634 struct device_attribute *attr, char *buf)
636 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
637 struct tsl2583_chip *chip = iio_priv(indio_dev);
639 return sprintf(buf, "%d\n", chip->taos_settings.als_gain_trim);
642 static ssize_t taos_als_trim_store(struct device *dev,
643 struct device_attribute *attr,
644 const char *buf, size_t len)
646 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
647 struct tsl2583_chip *chip = iio_priv(indio_dev);
650 if (kstrtoint(buf, 0, &value))
654 chip->taos_settings.als_gain_trim = value;
659 static ssize_t taos_als_cal_target_show(struct device *dev,
660 struct device_attribute *attr,
663 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
664 struct tsl2583_chip *chip = iio_priv(indio_dev);
666 return sprintf(buf, "%d\n", chip->taos_settings.als_cal_target);
669 static ssize_t taos_als_cal_target_store(struct device *dev,
670 struct device_attribute *attr,
671 const char *buf, size_t len)
673 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
674 struct tsl2583_chip *chip = iio_priv(indio_dev);
677 if (kstrtoint(buf, 0, &value))
681 chip->taos_settings.als_cal_target = value;
686 static ssize_t taos_lux_show(struct device *dev, struct device_attribute *attr,
691 ret = taos_get_lux(dev_to_iio_dev(dev));
695 return sprintf(buf, "%d\n", ret);
698 static ssize_t taos_do_calibrate(struct device *dev,
699 struct device_attribute *attr,
700 const char *buf, size_t len)
702 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
705 if (kstrtoint(buf, 0, &value))
709 taos_als_calibrate(indio_dev);
714 static ssize_t taos_luxtable_show(struct device *dev,
715 struct device_attribute *attr, char *buf)
720 for (i = 0; i < ARRAY_SIZE(taos_device_lux); i++) {
721 offset += sprintf(buf + offset, "%u,%u,%u,",
722 taos_device_lux[i].ratio,
723 taos_device_lux[i].ch0,
724 taos_device_lux[i].ch1);
725 if (taos_device_lux[i].ratio == 0) {
727 * We just printed the first "0" entry.
728 * Now get rid of the extra "," and break.
735 offset += sprintf(buf + offset, "\n");
739 static ssize_t taos_luxtable_store(struct device *dev,
740 struct device_attribute *attr,
741 const char *buf, size_t len)
743 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
744 struct tsl2583_chip *chip = iio_priv(indio_dev);
745 int value[ARRAY_SIZE(taos_device_lux) * 3 + 1];
748 get_options(buf, ARRAY_SIZE(value), value);
750 /* We now have an array of ints starting at value[1], and
751 * enumerated by value[0].
752 * We expect each group of three ints is one table entry,
753 * and the last table entry is all 0.
756 if ((n % 3) || n < 6 || n > ((ARRAY_SIZE(taos_device_lux) - 1) * 3)) {
757 dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
760 if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
761 dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
765 if (chip->taos_chip_status == TSL258X_CHIP_WORKING)
766 taos_chip_off(indio_dev);
768 /* Zero out the table */
769 memset(taos_device_lux, 0, sizeof(taos_device_lux));
770 memcpy(taos_device_lux, &value[1], (value[0] * 4));
772 taos_chip_on(indio_dev);
777 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
778 taos_power_state_show, taos_power_state_store);
780 static DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR,
781 taos_gain_show, taos_gain_store);
782 static DEVICE_ATTR(illuminance0_calibscale_available, S_IRUGO,
783 taos_gain_available_show, NULL);
785 static DEVICE_ATTR(illuminance0_integration_time, S_IRUGO | S_IWUSR,
786 taos_als_time_show, taos_als_time_store);
787 static DEVICE_ATTR(illuminance0_integration_time_available, S_IRUGO,
788 taos_als_time_available_show, NULL);
790 static DEVICE_ATTR(illuminance0_calibbias, S_IRUGO | S_IWUSR,
791 taos_als_trim_show, taos_als_trim_store);
793 static DEVICE_ATTR(illuminance0_input_target, S_IRUGO | S_IWUSR,
794 taos_als_cal_target_show, taos_als_cal_target_store);
796 static DEVICE_ATTR(illuminance0_input, S_IRUGO, taos_lux_show, NULL);
797 static DEVICE_ATTR(illuminance0_calibrate, S_IWUSR, NULL, taos_do_calibrate);
798 static DEVICE_ATTR(illuminance0_lux_table, S_IRUGO | S_IWUSR,
799 taos_luxtable_show, taos_luxtable_store);
801 static struct attribute *sysfs_attrs_ctrl[] = {
802 &dev_attr_power_state.attr,
803 &dev_attr_illuminance0_calibscale.attr, /* Gain */
804 &dev_attr_illuminance0_calibscale_available.attr,
805 &dev_attr_illuminance0_integration_time.attr, /* I time*/
806 &dev_attr_illuminance0_integration_time_available.attr,
807 &dev_attr_illuminance0_calibbias.attr, /* trim */
808 &dev_attr_illuminance0_input_target.attr,
809 &dev_attr_illuminance0_input.attr,
810 &dev_attr_illuminance0_calibrate.attr,
811 &dev_attr_illuminance0_lux_table.attr,
815 static const struct attribute_group tsl2583_attribute_group = {
816 .attrs = sysfs_attrs_ctrl,
819 /* Use the default register values to identify the Taos device */
820 static int taos_tsl258x_device(unsigned char *bufp)
822 return ((bufp[TSL258X_CHIPID] & 0xf0) == 0x90);
825 static const struct iio_info tsl2583_info = {
826 .attrs = &tsl2583_attribute_group,
827 .driver_module = THIS_MODULE,
831 * Client probe function - When a valid device is found, the driver's device
832 * data structure is updated, and initialization completes successfully.
834 static int taos_probe(struct i2c_client *clientp,
835 const struct i2c_device_id *idp)
838 unsigned char buf[TSL258X_MAX_DEVICE_REGS];
839 struct tsl2583_chip *chip;
840 struct iio_dev *indio_dev;
842 if (!i2c_check_functionality(clientp->adapter,
843 I2C_FUNC_SMBUS_BYTE_DATA)) {
844 dev_err(&clientp->dev, "taos_probe() - i2c smbus byte data func unsupported\n");
848 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
851 chip = iio_priv(indio_dev);
852 chip->client = clientp;
853 i2c_set_clientdata(clientp, indio_dev);
855 mutex_init(&chip->als_mutex);
856 chip->taos_chip_status = TSL258X_CHIP_UNKNOWN;
857 memcpy(chip->taos_config, taos_config, sizeof(chip->taos_config));
859 for (i = 0; i < TSL258X_MAX_DEVICE_REGS; i++) {
860 ret = i2c_smbus_write_byte(clientp,
861 (TSL258X_CMD_REG | (TSL258X_CNTRL + i)));
863 dev_err(&clientp->dev,
864 "i2c_smbus_write_byte to cmd reg failed in taos_probe(), err = %d\n",
868 ret = i2c_smbus_read_byte(clientp);
870 dev_err(&clientp->dev,
871 "i2c_smbus_read_byte from reg failed in taos_probe(), err = %d\n",
878 if (!taos_tsl258x_device(buf)) {
879 dev_info(&clientp->dev,
880 "i2c device found but does not match expected id in taos_probe()\n");
884 ret = i2c_smbus_write_byte(clientp, (TSL258X_CMD_REG | TSL258X_CNTRL));
886 dev_err(&clientp->dev,
887 "i2c_smbus_write_byte() to cmd reg failed in taos_probe(), err = %d\n",
892 indio_dev->info = &tsl2583_info;
893 indio_dev->dev.parent = &clientp->dev;
894 indio_dev->modes = INDIO_DIRECT_MODE;
895 indio_dev->name = chip->client->name;
896 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
898 dev_err(&clientp->dev, "iio registration failed\n");
902 /* Load up the V2 defaults (these are hard coded defaults for now) */
905 /* Make sure the chip is on */
906 taos_chip_on(indio_dev);
908 dev_info(&clientp->dev, "Light sensor found.\n");
912 #ifdef CONFIG_PM_SLEEP
913 static int taos_suspend(struct device *dev)
915 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
916 struct tsl2583_chip *chip = iio_priv(indio_dev);
919 mutex_lock(&chip->als_mutex);
921 if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
922 ret = taos_chip_off(indio_dev);
923 chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
926 mutex_unlock(&chip->als_mutex);
930 static int taos_resume(struct device *dev)
932 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
933 struct tsl2583_chip *chip = iio_priv(indio_dev);
936 mutex_lock(&chip->als_mutex);
938 if (chip->taos_chip_status == TSL258X_CHIP_SUSPENDED)
939 ret = taos_chip_on(indio_dev);
941 mutex_unlock(&chip->als_mutex);
945 static SIMPLE_DEV_PM_OPS(taos_pm_ops, taos_suspend, taos_resume);
946 #define TAOS_PM_OPS (&taos_pm_ops)
948 #define TAOS_PM_OPS NULL
951 static struct i2c_device_id taos_idtable[] = {
957 MODULE_DEVICE_TABLE(i2c, taos_idtable);
959 /* Driver definition */
960 static struct i2c_driver taos_driver = {
965 .id_table = taos_idtable,
968 module_i2c_driver(taos_driver);
970 MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
971 MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
972 MODULE_LICENSE("GPL");