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