]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/light/tsl2583.c
staging: iio: tsl2583: remove redundant power_state sysfs attribute
[karo-tx-linux.git] / drivers / staging / iio / light / tsl2583.c
1 /*
2  * Device driver for monitoring ambient light intensity (lux)
3  * within the TAOS tsl258x family of devices (tsl2580, tsl2581).
4  *
5  * Copyright (c) 2011, TAOS Corporation.
6  *
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.
11  *
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
15  * more details.
16  *
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.
20  */
21
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>
32
33 #define TSL258X_MAX_DEVICE_REGS         32
34
35 /* Triton register offsets */
36 #define TSL258X_REG_MAX         8
37
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
51
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
56
57 /* tsl2583 cntrl reg masks */
58 #define TSL258X_CNTL_ADC_ENBL   0x02
59 #define TSL258X_CNTL_PWR_ON             0x01
60
61 /* tsl2583 status reg masks */
62 #define TSL258X_STA_ADC_VALID   0x01
63 #define TSL258X_STA_ADC_INTR    0x10
64
65 /* Lux calculation constants */
66 #define TSL258X_LUX_CALC_OVER_FLOW              65535
67
68 enum {
69         TSL258X_CHIP_UNKNOWN = 0,
70         TSL258X_CHIP_WORKING = 1,
71         TSL258X_CHIP_SUSPENDED = 2
72 };
73
74 /* Per-device data */
75 struct taos_als_info {
76         u16 als_ch0;
77         u16 als_ch1;
78         u16 lux;
79 };
80
81 struct taos_settings {
82         int als_time;
83         int als_gain;
84         int als_gain_trim;
85         int als_cal_target;
86 };
87
88 struct tsl2583_chip {
89         struct mutex als_mutex;
90         struct i2c_client *client;
91         struct taos_als_info als_cur_info;
92         struct taos_settings taos_settings;
93         int als_time_scale;
94         int als_saturation;
95         int taos_chip_status;
96         u8 taos_config[8];
97 };
98
99 /*
100  * Initial values for device - this values can/will be changed by driver.
101  * and applications as needed.
102  * These values are dynamic.
103  */
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 */
107
108 struct taos_lux {
109         unsigned int ratio;
110         unsigned int ch0;
111         unsigned int ch1;
112 };
113
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 },
122 };
123
124 struct gainadj {
125         s16 ch0;
126         s16 ch1;
127 };
128
129 /* Index = (0 - 3) Used to validate the gain selection index */
130 static const struct gainadj gainadj[] = {
131         { 1, 1 },
132         { 8, 8 },
133         { 16, 16 },
134         { 107, 115 }
135 };
136
137 /*
138  * Provides initial operational parameter defaults.
139  * These defaults may be changed through the device's sysfs files.
140  */
141 static void taos_defaults(struct tsl2583_chip *chip)
142 {
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 */
153 }
154
155 /*
156  * Read a number of bytes starting at register (reg) location.
157  * Return 0, or i2c_smbus_write_byte ERROR code.
158  */
159 static int
160 taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, unsigned int len)
161 {
162         int i, ret;
163
164         for (i = 0; i < len; i++) {
165                 /* select register to write */
166                 ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | reg));
167                 if (ret < 0) {
168                         dev_err(&client->dev,
169                                 "taos_i2c_read failed to write register %x\n",
170                                 reg);
171                         return ret;
172                 }
173                 /* read the data */
174                 ret = i2c_smbus_read_byte(client);
175                 if (ret < 0) {
176                         dev_err(&client->dev,
177                                 "%s failed to read byte after writing to register %x\n",
178                                 __func__, reg);
179                         return ret;
180                 }
181                 *val = ret;
182                 val++;
183                 reg++;
184         }
185         return 0;
186 }
187
188 /*
189  * Reads and calculates current lux value.
190  * The raw ch0 and ch1 values of the ambient light sensed in the last
191  * integration cycle are read from the device.
192  * Time scale factor array values are adjusted based on the integration time.
193  * The raw values are multiplied by a scale factor, and device gain is obtained
194  * using gain index. Limit checks are done next, then the ratio of a multiple
195  * of ch1 value, to the ch0 value, is calculated. The array taos_device_lux[]
196  * declared above is then scanned to find the first ratio value that is just
197  * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
198  * the array are then used along with the time scale factor array values, to
199  * calculate the lux.
200  */
201 static int taos_get_lux(struct iio_dev *indio_dev)
202 {
203         u16 ch0, ch1; /* separated ch0/ch1 data from device */
204         u32 lux; /* raw lux calculated from device data */
205         u64 lux64;
206         u32 ratio;
207         u8 buf[5];
208         struct taos_lux *p;
209         struct tsl2583_chip *chip = iio_priv(indio_dev);
210         int i, ret;
211         u32 ch0lux = 0;
212         u32 ch1lux = 0;
213
214         if (mutex_trylock(&chip->als_mutex) == 0) {
215                 dev_info(&chip->client->dev, "taos_get_lux device is busy\n");
216                 return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
217         }
218
219         if (chip->taos_chip_status != TSL258X_CHIP_WORKING) {
220                 /* device is not enabled */
221                 dev_err(&chip->client->dev, "taos_get_lux device is not enabled\n");
222                 ret = -EBUSY;
223                 goto out_unlock;
224         }
225
226         ret = taos_i2c_read(chip->client, (TSL258X_CMD_REG), &buf[0], 1);
227         if (ret < 0) {
228                 dev_err(&chip->client->dev, "taos_get_lux failed to read CMD_REG\n");
229                 goto out_unlock;
230         }
231         /* is data new & valid */
232         if (!(buf[0] & TSL258X_STA_ADC_INTR)) {
233                 dev_err(&chip->client->dev, "taos_get_lux data not valid\n");
234                 ret = chip->als_cur_info.lux; /* return LAST VALUE */
235                 goto out_unlock;
236         }
237
238         for (i = 0; i < 4; i++) {
239                 int reg = TSL258X_CMD_REG | (TSL258X_ALS_CHAN0LO + i);
240
241                 ret = taos_i2c_read(chip->client, reg, &buf[i], 1);
242                 if (ret < 0) {
243                         dev_err(&chip->client->dev,
244                                 "taos_get_lux failed to read register %x\n",
245                                 reg);
246                         goto out_unlock;
247                 }
248         }
249
250         /*
251          * clear status, really interrupt status (interrupts are off), but
252          * we use the bit anyway - don't forget 0x80 - this is a command
253          */
254         ret = i2c_smbus_write_byte(chip->client,
255                                    (TSL258X_CMD_REG | TSL258X_CMD_SPL_FN |
256                                     TSL258X_CMD_ALS_INT_CLR));
257
258         if (ret < 0) {
259                 dev_err(&chip->client->dev,
260                         "taos_i2c_write_command failed in taos_get_lux, err = %d\n",
261                         ret);
262                 goto out_unlock; /* have no data, so return failure */
263         }
264
265         /* extract ALS/lux data */
266         ch0 = le16_to_cpup((const __le16 *)&buf[0]);
267         ch1 = le16_to_cpup((const __le16 *)&buf[2]);
268
269         chip->als_cur_info.als_ch0 = ch0;
270         chip->als_cur_info.als_ch1 = ch1;
271
272         if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
273                 goto return_max;
274
275         if (!ch0) {
276                 /* have no data, so return LAST VALUE */
277                 ret = 0;
278                 chip->als_cur_info.lux = 0;
279                 goto out_unlock;
280         }
281         /* calculate ratio */
282         ratio = (ch1 << 15) / ch0;
283         /* convert to unscaled lux using the pointer to the table */
284         for (p = (struct taos_lux *)taos_device_lux;
285              p->ratio != 0 && p->ratio < ratio; p++)
286                 ;
287
288         if (p->ratio == 0) {
289                 lux = 0;
290         } else {
291                 ch0lux = ((ch0 * p->ch0) +
292                           (gainadj[chip->taos_settings.als_gain].ch0 >> 1))
293                          / gainadj[chip->taos_settings.als_gain].ch0;
294                 ch1lux = ((ch1 * p->ch1) +
295                           (gainadj[chip->taos_settings.als_gain].ch1 >> 1))
296                          / gainadj[chip->taos_settings.als_gain].ch1;
297                 lux = ch0lux - ch1lux;
298         }
299
300         /* note: lux is 31 bit max at this point */
301         if (ch1lux > ch0lux) {
302                 dev_dbg(&chip->client->dev, "No Data - Return last value\n");
303                 ret = 0;
304                 chip->als_cur_info.lux = 0;
305                 goto out_unlock;
306         }
307
308         /* adjust for active time scale */
309         if (chip->als_time_scale == 0)
310                 lux = 0;
311         else
312                 lux = (lux + (chip->als_time_scale >> 1)) /
313                         chip->als_time_scale;
314
315         /* Adjust for active gain scale.
316          * The taos_device_lux tables above have a factor of 8192 built in,
317          * so we need to shift right.
318          * User-specified gain provides a multiplier.
319          * Apply user-specified gain before shifting right to retain precision.
320          * Use 64 bits to avoid overflow on multiplication.
321          * Then go back to 32 bits before division to avoid using div_u64().
322          */
323         lux64 = lux;
324         lux64 = lux64 * chip->taos_settings.als_gain_trim;
325         lux64 >>= 13;
326         lux = lux64;
327         lux = (lux + 500) / 1000;
328         if (lux > TSL258X_LUX_CALC_OVER_FLOW) { /* check for overflow */
329 return_max:
330                 lux = TSL258X_LUX_CALC_OVER_FLOW;
331         }
332
333         /* Update the structure with the latest VALID lux. */
334         chip->als_cur_info.lux = lux;
335         ret = lux;
336
337 out_unlock:
338         mutex_unlock(&chip->als_mutex);
339         return ret;
340 }
341
342 /*
343  * Obtain single reading and calculate the als_gain_trim (later used
344  * to derive actual lux).
345  * Return updated gain_trim value.
346  */
347 static int taos_als_calibrate(struct iio_dev *indio_dev)
348 {
349         struct tsl2583_chip *chip = iio_priv(indio_dev);
350         u8 reg_val;
351         unsigned int gain_trim_val;
352         int ret;
353         int lux_val;
354
355         ret = i2c_smbus_write_byte(chip->client,
356                                    (TSL258X_CMD_REG | TSL258X_CNTRL));
357         if (ret < 0) {
358                 dev_err(&chip->client->dev,
359                         "taos_als_calibrate failed to reach the CNTRL register, ret=%d\n",
360                         ret);
361                 return ret;
362         }
363
364         reg_val = i2c_smbus_read_byte(chip->client);
365         if (reg_val < 0) {
366                 dev_err(&chip->client->dev,
367                         "%s failed to read after writing to the CNTRL register\n",
368                         __func__);
369                 return ret;
370         }
371
372         if ((reg_val & (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON))
373                         != (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) {
374                 dev_err(&chip->client->dev,
375                         "taos_als_calibrate failed: device not powered on with ADC enabled\n");
376                 return -EINVAL;
377         }
378
379         ret = i2c_smbus_write_byte(chip->client,
380                                    (TSL258X_CMD_REG | TSL258X_CNTRL));
381         if (ret < 0) {
382                 dev_err(&chip->client->dev,
383                         "taos_als_calibrate failed to reach the STATUS register, ret=%d\n",
384                         ret);
385                 return ret;
386         }
387         reg_val = i2c_smbus_read_byte(chip->client);
388         if (reg_val < 0) {
389                 dev_err(&chip->client->dev,
390                         "%s failed to read after writing to the STATUS register\n",
391                         __func__);
392                 return ret;
393         }
394
395         if ((reg_val & TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) {
396                 dev_err(&chip->client->dev,
397                         "taos_als_calibrate failed: STATUS - ADC not valid.\n");
398                 return -ENODATA;
399         }
400         lux_val = taos_get_lux(indio_dev);
401         if (lux_val < 0) {
402                 dev_err(&chip->client->dev, "taos_als_calibrate failed to get lux\n");
403                 return lux_val;
404         }
405         gain_trim_val = (unsigned int)(((chip->taos_settings.als_cal_target)
406                         * chip->taos_settings.als_gain_trim) / lux_val);
407
408         if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
409                 dev_err(&chip->client->dev,
410                         "taos_als_calibrate failed: trim_val of %d is out of range\n",
411                         gain_trim_val);
412                 return -ENODATA;
413         }
414         chip->taos_settings.als_gain_trim = (int)gain_trim_val;
415
416         return (int)gain_trim_val;
417 }
418
419 /*
420  * Turn the device on.
421  * Configuration must be set before calling this function.
422  */
423 static int taos_chip_on(struct iio_dev *indio_dev)
424 {
425         int i;
426         int ret;
427         u8 *uP;
428         u8 utmp;
429         int als_count;
430         int als_time;
431         struct tsl2583_chip *chip = iio_priv(indio_dev);
432
433         /* and make sure we're not already on */
434         if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
435                 /* if forcing a register update - turn off, then on */
436                 dev_info(&chip->client->dev, "device is already enabled\n");
437                 return -EINVAL;
438         }
439
440         /* determine als integration register */
441         als_count = (chip->taos_settings.als_time * 100 + 135) / 270;
442         if (!als_count)
443                 als_count = 1; /* ensure at least one cycle */
444
445         /* convert back to time (encompasses overrides) */
446         als_time = (als_count * 27 + 5) / 10;
447         chip->taos_config[TSL258X_ALS_TIME] = 256 - als_count;
448
449         /* Set the gain based on taos_settings struct */
450         chip->taos_config[TSL258X_GAIN] = chip->taos_settings.als_gain;
451
452         /* set chip struct re scaling and saturation */
453         chip->als_saturation = als_count * 922; /* 90% of full scale */
454         chip->als_time_scale = (als_time + 25) / 50;
455
456         /*
457          * TSL258x Specific power-on / adc enable sequence
458          * Power on the device 1st.
459          */
460         utmp = TSL258X_CNTL_PWR_ON;
461         ret = i2c_smbus_write_byte_data(chip->client,
462                                         TSL258X_CMD_REG | TSL258X_CNTRL, utmp);
463         if (ret < 0) {
464                 dev_err(&chip->client->dev, "taos_chip_on failed on CNTRL reg.\n");
465                 return ret;
466         }
467
468         /*
469          * Use the following shadow copy for our delay before enabling ADC.
470          * Write all the registers.
471          */
472         for (i = 0, uP = chip->taos_config; i < TSL258X_REG_MAX; i++) {
473                 ret = i2c_smbus_write_byte_data(chip->client,
474                                                 TSL258X_CMD_REG + i,
475                                                 *uP++);
476                 if (ret < 0) {
477                         dev_err(&chip->client->dev,
478                                 "taos_chip_on failed on reg %d.\n", i);
479                         return ret;
480                 }
481         }
482
483         usleep_range(3000, 3500);
484         /*
485          * NOW enable the ADC
486          * initialize the desired mode of operation
487          */
488         utmp = TSL258X_CNTL_PWR_ON | TSL258X_CNTL_ADC_ENBL;
489         ret = i2c_smbus_write_byte_data(chip->client,
490                                         TSL258X_CMD_REG | TSL258X_CNTRL,
491                                         utmp);
492         if (ret < 0) {
493                 dev_err(&chip->client->dev, "taos_chip_on failed on 2nd CTRL reg.\n");
494                 return ret;
495         }
496         chip->taos_chip_status = TSL258X_CHIP_WORKING;
497
498         return ret;
499 }
500
501 static int taos_chip_off(struct iio_dev *indio_dev)
502 {
503         struct tsl2583_chip *chip = iio_priv(indio_dev);
504
505         /* turn device off */
506         chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
507         return i2c_smbus_write_byte_data(chip->client,
508                                         TSL258X_CMD_REG | TSL258X_CNTRL,
509                                         0x00);
510 }
511
512 /* Sysfs Interface Functions */
513
514 static ssize_t taos_gain_show(struct device *dev,
515                               struct device_attribute *attr, char *buf)
516 {
517         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
518         struct tsl2583_chip *chip = iio_priv(indio_dev);
519         char gain[4] = {0};
520
521         switch (chip->taos_settings.als_gain) {
522         case 0:
523                 strcpy(gain, "001");
524                 break;
525         case 1:
526                 strcpy(gain, "008");
527                 break;
528         case 2:
529                 strcpy(gain, "016");
530                 break;
531         case 3:
532                 strcpy(gain, "111");
533                 break;
534         }
535
536         return sprintf(buf, "%s\n", gain);
537 }
538
539 static ssize_t taos_gain_store(struct device *dev,
540                                struct device_attribute *attr,
541                                const char *buf, size_t len)
542 {
543         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
544         struct tsl2583_chip *chip = iio_priv(indio_dev);
545         int value;
546
547         if (kstrtoint(buf, 0, &value))
548                 return -EINVAL;
549
550         switch (value) {
551         case 1:
552                 chip->taos_settings.als_gain = 0;
553                 break;
554         case 8:
555                 chip->taos_settings.als_gain = 1;
556                 break;
557         case 16:
558                 chip->taos_settings.als_gain = 2;
559                 break;
560         case 111:
561                 chip->taos_settings.als_gain = 3;
562                 break;
563         default:
564                 dev_err(dev, "Invalid Gain Index (must be 1,8,16,111)\n");
565                 return -1;
566         }
567
568         return len;
569 }
570
571 static ssize_t taos_gain_available_show(struct device *dev,
572                                         struct device_attribute *attr,
573                                         char *buf)
574 {
575         return sprintf(buf, "%s\n", "1 8 16 111");
576 }
577
578 static ssize_t taos_als_time_show(struct device *dev,
579                                   struct device_attribute *attr, char *buf)
580 {
581         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
582         struct tsl2583_chip *chip = iio_priv(indio_dev);
583
584         return sprintf(buf, "%d\n", chip->taos_settings.als_time);
585 }
586
587 static ssize_t taos_als_time_store(struct device *dev,
588                                    struct device_attribute *attr,
589                                    const char *buf, size_t len)
590 {
591         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
592         struct tsl2583_chip *chip = iio_priv(indio_dev);
593         int value;
594
595         if (kstrtoint(buf, 0, &value))
596                 return -EINVAL;
597
598         if ((value < 50) || (value > 650))
599                 return -EINVAL;
600
601         if (value % 50)
602                 return -EINVAL;
603
604         chip->taos_settings.als_time = value;
605
606         return len;
607 }
608
609 static ssize_t taos_als_time_available_show(struct device *dev,
610                                             struct device_attribute *attr,
611                                             char *buf)
612 {
613         return sprintf(buf, "%s\n",
614                 "50 100 150 200 250 300 350 400 450 500 550 600 650");
615 }
616
617 static ssize_t taos_als_trim_show(struct device *dev,
618                                   struct device_attribute *attr, char *buf)
619 {
620         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
621         struct tsl2583_chip *chip = iio_priv(indio_dev);
622
623         return sprintf(buf, "%d\n", chip->taos_settings.als_gain_trim);
624 }
625
626 static ssize_t taos_als_trim_store(struct device *dev,
627                                    struct device_attribute *attr,
628                                    const char *buf, size_t len)
629 {
630         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
631         struct tsl2583_chip *chip = iio_priv(indio_dev);
632         int value;
633
634         if (kstrtoint(buf, 0, &value))
635                 return -EINVAL;
636
637         if (value)
638                 chip->taos_settings.als_gain_trim = value;
639
640         return len;
641 }
642
643 static ssize_t taos_als_cal_target_show(struct device *dev,
644                                         struct device_attribute *attr,
645                                         char *buf)
646 {
647         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
648         struct tsl2583_chip *chip = iio_priv(indio_dev);
649
650         return sprintf(buf, "%d\n", chip->taos_settings.als_cal_target);
651 }
652
653 static ssize_t taos_als_cal_target_store(struct device *dev,
654                                          struct device_attribute *attr,
655                                          const char *buf, size_t len)
656 {
657         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
658         struct tsl2583_chip *chip = iio_priv(indio_dev);
659         int value;
660
661         if (kstrtoint(buf, 0, &value))
662                 return -EINVAL;
663
664         if (value)
665                 chip->taos_settings.als_cal_target = value;
666
667         return len;
668 }
669
670 static ssize_t taos_lux_show(struct device *dev, struct device_attribute *attr,
671                              char *buf)
672 {
673         int ret;
674
675         ret = taos_get_lux(dev_to_iio_dev(dev));
676         if (ret < 0)
677                 return ret;
678
679         return sprintf(buf, "%d\n", ret);
680 }
681
682 static ssize_t taos_do_calibrate(struct device *dev,
683                                  struct device_attribute *attr,
684                                  const char *buf, size_t len)
685 {
686         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
687         int value;
688
689         if (kstrtoint(buf, 0, &value))
690                 return -EINVAL;
691
692         if (value == 1)
693                 taos_als_calibrate(indio_dev);
694
695         return len;
696 }
697
698 static ssize_t taos_luxtable_show(struct device *dev,
699                                   struct device_attribute *attr, char *buf)
700 {
701         int i;
702         int offset = 0;
703
704         for (i = 0; i < ARRAY_SIZE(taos_device_lux); i++) {
705                 offset += sprintf(buf + offset, "%u,%u,%u,",
706                                   taos_device_lux[i].ratio,
707                                   taos_device_lux[i].ch0,
708                                   taos_device_lux[i].ch1);
709                 if (taos_device_lux[i].ratio == 0) {
710                         /*
711                          * We just printed the first "0" entry.
712                          * Now get rid of the extra "," and break.
713                          */
714                         offset--;
715                         break;
716                 }
717         }
718
719         offset += sprintf(buf + offset, "\n");
720         return offset;
721 }
722
723 static ssize_t taos_luxtable_store(struct device *dev,
724                                    struct device_attribute *attr,
725                                    const char *buf, size_t len)
726 {
727         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
728         struct tsl2583_chip *chip = iio_priv(indio_dev);
729         int value[ARRAY_SIZE(taos_device_lux) * 3 + 1];
730         int n;
731
732         get_options(buf, ARRAY_SIZE(value), value);
733
734         /* We now have an array of ints starting at value[1], and
735          * enumerated by value[0].
736          * We expect each group of three ints is one table entry,
737          * and the last table entry is all 0.
738          */
739         n = value[0];
740         if ((n % 3) || n < 6 || n > ((ARRAY_SIZE(taos_device_lux) - 1) * 3)) {
741                 dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
742                 return -EINVAL;
743         }
744         if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
745                 dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
746                 return -EINVAL;
747         }
748
749         if (chip->taos_chip_status == TSL258X_CHIP_WORKING)
750                 taos_chip_off(indio_dev);
751
752         /* Zero out the table */
753         memset(taos_device_lux, 0, sizeof(taos_device_lux));
754         memcpy(taos_device_lux, &value[1], (value[0] * 4));
755
756         taos_chip_on(indio_dev);
757
758         return len;
759 }
760
761 static DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR,
762                 taos_gain_show, taos_gain_store);
763 static DEVICE_ATTR(illuminance0_calibscale_available, S_IRUGO,
764                 taos_gain_available_show, NULL);
765
766 static DEVICE_ATTR(illuminance0_integration_time, S_IRUGO | S_IWUSR,
767                 taos_als_time_show, taos_als_time_store);
768 static DEVICE_ATTR(illuminance0_integration_time_available, S_IRUGO,
769                 taos_als_time_available_show, NULL);
770
771 static DEVICE_ATTR(illuminance0_calibbias, S_IRUGO | S_IWUSR,
772                 taos_als_trim_show, taos_als_trim_store);
773
774 static DEVICE_ATTR(illuminance0_input_target, S_IRUGO | S_IWUSR,
775                 taos_als_cal_target_show, taos_als_cal_target_store);
776
777 static DEVICE_ATTR(illuminance0_input, S_IRUGO, taos_lux_show, NULL);
778 static DEVICE_ATTR(illuminance0_calibrate, S_IWUSR, NULL, taos_do_calibrate);
779 static DEVICE_ATTR(illuminance0_lux_table, S_IRUGO | S_IWUSR,
780                 taos_luxtable_show, taos_luxtable_store);
781
782 static struct attribute *sysfs_attrs_ctrl[] = {
783         &dev_attr_illuminance0_calibscale.attr,                 /* Gain  */
784         &dev_attr_illuminance0_calibscale_available.attr,
785         &dev_attr_illuminance0_integration_time.attr,   /* I time*/
786         &dev_attr_illuminance0_integration_time_available.attr,
787         &dev_attr_illuminance0_calibbias.attr,                  /* trim  */
788         &dev_attr_illuminance0_input_target.attr,
789         &dev_attr_illuminance0_input.attr,
790         &dev_attr_illuminance0_calibrate.attr,
791         &dev_attr_illuminance0_lux_table.attr,
792         NULL
793 };
794
795 static const struct attribute_group tsl2583_attribute_group = {
796         .attrs = sysfs_attrs_ctrl,
797 };
798
799 /* Use the default register values to identify the Taos device */
800 static int taos_tsl258x_device(unsigned char *bufp)
801 {
802         return ((bufp[TSL258X_CHIPID] & 0xf0) == 0x90);
803 }
804
805 static const struct iio_info tsl2583_info = {
806         .attrs = &tsl2583_attribute_group,
807         .driver_module = THIS_MODULE,
808 };
809
810 /*
811  * Client probe function - When a valid device is found, the driver's device
812  * data structure is updated, and initialization completes successfully.
813  */
814 static int taos_probe(struct i2c_client *clientp,
815                       const struct i2c_device_id *idp)
816 {
817         int i, ret;
818         unsigned char buf[TSL258X_MAX_DEVICE_REGS];
819         struct tsl2583_chip *chip;
820         struct iio_dev *indio_dev;
821
822         if (!i2c_check_functionality(clientp->adapter,
823                                      I2C_FUNC_SMBUS_BYTE_DATA)) {
824                 dev_err(&clientp->dev, "taos_probe() - i2c smbus byte data func unsupported\n");
825                 return -EOPNOTSUPP;
826         }
827
828         indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
829         if (!indio_dev)
830                 return -ENOMEM;
831         chip = iio_priv(indio_dev);
832         chip->client = clientp;
833         i2c_set_clientdata(clientp, indio_dev);
834
835         mutex_init(&chip->als_mutex);
836         chip->taos_chip_status = TSL258X_CHIP_UNKNOWN;
837         memcpy(chip->taos_config, taos_config, sizeof(chip->taos_config));
838
839         for (i = 0; i < TSL258X_MAX_DEVICE_REGS; i++) {
840                 ret = i2c_smbus_write_byte(clientp,
841                                 (TSL258X_CMD_REG | (TSL258X_CNTRL + i)));
842                 if (ret < 0) {
843                         dev_err(&clientp->dev,
844                                 "i2c_smbus_write_byte to cmd reg failed in taos_probe(), err = %d\n",
845                                 ret);
846                         return ret;
847                 }
848                 ret = i2c_smbus_read_byte(clientp);
849                 if (ret < 0) {
850                         dev_err(&clientp->dev,
851                                 "i2c_smbus_read_byte from reg failed in taos_probe(), err = %d\n",
852                                 ret);
853                         return ret;
854                 }
855                 buf[i] = ret;
856         }
857
858         if (!taos_tsl258x_device(buf)) {
859                 dev_info(&clientp->dev,
860                          "i2c device found but does not match expected id in taos_probe()\n");
861                 return -EINVAL;
862         }
863
864         ret = i2c_smbus_write_byte(clientp, (TSL258X_CMD_REG | TSL258X_CNTRL));
865         if (ret < 0) {
866                 dev_err(&clientp->dev,
867                         "i2c_smbus_write_byte() to cmd reg failed in taos_probe(), err = %d\n",
868                         ret);
869                 return ret;
870         }
871
872         indio_dev->info = &tsl2583_info;
873         indio_dev->dev.parent = &clientp->dev;
874         indio_dev->modes = INDIO_DIRECT_MODE;
875         indio_dev->name = chip->client->name;
876         ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
877         if (ret) {
878                 dev_err(&clientp->dev, "iio registration failed\n");
879                 return ret;
880         }
881
882         /* Load up the V2 defaults (these are hard coded defaults for now) */
883         taos_defaults(chip);
884
885         /* Make sure the chip is on */
886         taos_chip_on(indio_dev);
887
888         dev_info(&clientp->dev, "Light sensor found.\n");
889         return 0;
890 }
891
892 #ifdef CONFIG_PM_SLEEP
893 static int taos_suspend(struct device *dev)
894 {
895         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
896         struct tsl2583_chip *chip = iio_priv(indio_dev);
897         int ret = 0;
898
899         mutex_lock(&chip->als_mutex);
900
901         if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
902                 ret = taos_chip_off(indio_dev);
903                 chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
904         }
905
906         mutex_unlock(&chip->als_mutex);
907         return ret;
908 }
909
910 static int taos_resume(struct device *dev)
911 {
912         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
913         struct tsl2583_chip *chip = iio_priv(indio_dev);
914         int ret = 0;
915
916         mutex_lock(&chip->als_mutex);
917
918         if (chip->taos_chip_status == TSL258X_CHIP_SUSPENDED)
919                 ret = taos_chip_on(indio_dev);
920
921         mutex_unlock(&chip->als_mutex);
922         return ret;
923 }
924
925 static SIMPLE_DEV_PM_OPS(taos_pm_ops, taos_suspend, taos_resume);
926 #define TAOS_PM_OPS (&taos_pm_ops)
927 #else
928 #define TAOS_PM_OPS NULL
929 #endif
930
931 static struct i2c_device_id taos_idtable[] = {
932         { "tsl2580", 0 },
933         { "tsl2581", 1 },
934         { "tsl2583", 2 },
935         {}
936 };
937 MODULE_DEVICE_TABLE(i2c, taos_idtable);
938
939 #ifdef CONFIG_OF
940 static const struct of_device_id taos2583_of_match[] = {
941         { .compatible = "amstaos,tsl2580", },
942         { .compatible = "amstaos,tsl2581", },
943         { .compatible = "amstaos,tsl2583", },
944         { },
945 };
946 MODULE_DEVICE_TABLE(of, taos2583_of_match);
947 #else
948 #define taos2583_of_match NULL
949 #endif
950
951 /* Driver definition */
952 static struct i2c_driver taos_driver = {
953         .driver = {
954                 .name = "tsl2583",
955                 .pm = TAOS_PM_OPS,
956                 .of_match_table = taos2583_of_match,
957         },
958         .id_table = taos_idtable,
959         .probe = taos_probe,
960 };
961 module_i2c_driver(taos_driver);
962
963 MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
964 MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
965 MODULE_LICENSE("GPL");