19b7215a7c569c7fcd268880515c8bbc5392acc6
[cascardo/linux.git] / drivers / staging / iio / light / tsl2563.c
1 /*
2  * drivers/i2c/chips/tsl2563.c
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
7  * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
8  *
9  * Converted to IIO driver
10  * Amit Kucheria <amit.kucheria@verdurent.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * version 2 as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24  * 02110-1301 USA
25  */
26
27 #include <linux/module.h>
28 #include <linux/i2c.h>
29 #include <linux/interrupt.h>
30 #include <linux/irq.h>
31 #include <linux/sched.h>
32 #include <linux/mutex.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/pm.h>
36 #include <linux/hwmon.h>
37 #include <linux/err.h>
38 #include <linux/slab.h>
39
40 #include "../iio.h"
41 #include "tsl2563.h"
42
43 /* Use this many bits for fraction part. */
44 #define ADC_FRAC_BITS           (14)
45
46 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
47 #define FRAC10K(f)              (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
48
49 /* Bits used for fraction in calibration coefficients.*/
50 #define CALIB_FRAC_BITS         (10)
51 /* 0.5 in CALIB_FRAC_BITS precision */
52 #define CALIB_FRAC_HALF         (1 << (CALIB_FRAC_BITS - 1))
53 /* Make a fraction from a number n that was multiplied with b. */
54 #define CALIB_FRAC(n, b)        (((n) << CALIB_FRAC_BITS) / (b))
55 /* Decimal 10^(digits in sysfs presentation) */
56 #define CALIB_BASE_SYSFS        (1000)
57
58 #define TSL2563_CMD             (0x80)
59 #define TSL2563_CLEARINT        (0x40)
60
61 #define TSL2563_REG_CTRL        (0x00)
62 #define TSL2563_REG_TIMING      (0x01)
63 #define TSL2563_REG_LOWLOW      (0x02) /* data0 low threshold, 2 bytes */
64 #define TSL2563_REG_LOWHIGH     (0x03)
65 #define TSL2563_REG_HIGHLOW     (0x04) /* data0 high threshold, 2 bytes */
66 #define TSL2563_REG_HIGHHIGH    (0x05)
67 #define TSL2563_REG_INT         (0x06)
68 #define TSL2563_REG_ID          (0x0a)
69 #define TSL2563_REG_DATA0LOW    (0x0c) /* broadband sensor value, 2 bytes */
70 #define TSL2563_REG_DATA0HIGH   (0x0d)
71 #define TSL2563_REG_DATA1LOW    (0x0e) /* infrared sensor value, 2 bytes */
72 #define TSL2563_REG_DATA1HIGH   (0x0f)
73
74 #define TSL2563_CMD_POWER_ON    (0x03)
75 #define TSL2563_CMD_POWER_OFF   (0x00)
76 #define TSL2563_CTRL_POWER_MASK (0x03)
77
78 #define TSL2563_TIMING_13MS     (0x00)
79 #define TSL2563_TIMING_100MS    (0x01)
80 #define TSL2563_TIMING_400MS    (0x02)
81 #define TSL2563_TIMING_MASK     (0x03)
82 #define TSL2563_TIMING_GAIN16   (0x10)
83 #define TSL2563_TIMING_GAIN1    (0x00)
84
85 #define TSL2563_INT_DISBLED     (0x00)
86 #define TSL2563_INT_LEVEL       (0x10)
87 #define TSL2563_INT_PERSIST(n)  ((n) & 0x0F)
88
89 struct tsl2563_gainlevel_coeff {
90         u8 gaintime;
91         u16 min;
92         u16 max;
93 };
94
95 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
96         {
97                 .gaintime       = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
98                 .min            = 0,
99                 .max            = 65534,
100         }, {
101                 .gaintime       = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
102                 .min            = 2048,
103                 .max            = 65534,
104         }, {
105                 .gaintime       = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
106                 .min            = 4095,
107                 .max            = 37177,
108         }, {
109                 .gaintime       = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
110                 .min            = 3000,
111                 .max            = 65535,
112         },
113 };
114
115 struct tsl2563_chip {
116         struct mutex            lock;
117         struct i2c_client       *client;
118         struct delayed_work     poweroff_work;
119
120         struct work_struct      work_thresh;
121         s64                     event_timestamp;
122         /* Remember state for suspend and resume functions */
123         pm_message_t            state;
124
125         struct tsl2563_gainlevel_coeff const *gainlevel;
126
127         u16                     low_thres;
128         u16                     high_thres;
129         u8                      intr;
130         bool                    int_enabled;
131
132         /* Calibration coefficients */
133         u32                     calib0;
134         u32                     calib1;
135         int                     cover_comp_gain;
136
137         /* Cache current values, to be returned while suspended */
138         u32                     data0;
139         u32                     data1;
140 };
141
142 static int tsl2563_write(struct i2c_client *client, u8 reg, u8 value)
143 {
144         int ret;
145         u8 buf[2];
146
147         buf[0] = TSL2563_CMD | reg;
148         buf[1] = value;
149
150         ret = i2c_master_send(client, buf, sizeof(buf));
151         return (ret == sizeof(buf)) ? 0 : ret;
152 }
153
154 static int tsl2563_read(struct i2c_client *client, u8 reg, void *buf, int len)
155 {
156         int ret;
157         u8 cmd = TSL2563_CMD | reg;
158
159         ret = i2c_master_send(client, &cmd, sizeof(cmd));
160         if (ret != sizeof(cmd))
161                 return ret;
162
163         return i2c_master_recv(client, buf, len);
164 }
165
166 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
167 {
168         struct i2c_client *client = chip->client;
169         u8 cmd;
170
171         cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
172         return tsl2563_write(client, TSL2563_REG_CTRL, cmd);
173 }
174
175 /*
176  * Return value is 0 for off, 1 for on, or a negative error
177  * code if reading failed.
178  */
179 static int tsl2563_get_power(struct tsl2563_chip *chip)
180 {
181         struct i2c_client *client = chip->client;
182         int ret;
183         u8 val;
184
185         ret = tsl2563_read(client, TSL2563_REG_CTRL, &val, sizeof(val));
186         if (ret != sizeof(val))
187                 return ret;
188
189         return (val & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
190 }
191
192 static int tsl2563_configure(struct tsl2563_chip *chip)
193 {
194         int ret;
195
196         ret = tsl2563_write(chip->client, TSL2563_REG_TIMING,
197                         chip->gainlevel->gaintime);
198         if (ret)
199                 goto error_ret;
200         ret = tsl2563_write(chip->client, TSL2563_REG_HIGHLOW,
201                         chip->high_thres & 0xFF);
202         if (ret)
203                 goto error_ret;
204         ret = tsl2563_write(chip->client, TSL2563_REG_HIGHHIGH,
205                         (chip->high_thres >> 8) & 0xFF);
206         if (ret)
207                 goto error_ret;
208         ret = tsl2563_write(chip->client, TSL2563_REG_LOWLOW,
209                         chip->low_thres & 0xFF);
210         if (ret)
211                 goto error_ret;
212         ret = tsl2563_write(chip->client, TSL2563_REG_LOWHIGH,
213                         (chip->low_thres >> 8) & 0xFF);
214 /* Interrupt register is automatically written anyway if it is relevant
215    so is not here */
216 error_ret:
217         return ret;
218 }
219
220 static void tsl2563_poweroff_work(struct work_struct *work)
221 {
222         struct tsl2563_chip *chip =
223                 container_of(work, struct tsl2563_chip, poweroff_work.work);
224         tsl2563_set_power(chip, 0);
225 }
226
227 static int tsl2563_detect(struct tsl2563_chip *chip)
228 {
229         int ret;
230
231         ret = tsl2563_set_power(chip, 1);
232         if (ret)
233                 return ret;
234
235         ret = tsl2563_get_power(chip);
236         if (ret < 0)
237                 return ret;
238
239         return ret ? 0 : -ENODEV;
240 }
241
242 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
243 {
244         struct i2c_client *client = chip->client;
245         int ret;
246
247         ret = tsl2563_read(client, TSL2563_REG_ID, id, sizeof(*id));
248         if (ret != sizeof(*id))
249                 return ret;
250
251         return 0;
252 }
253
254 /*
255  * "Normalized" ADC value is one obtained with 400ms of integration time and
256  * 16x gain. This function returns the number of bits of shift needed to
257  * convert between normalized values and HW values obtained using given
258  * timing and gain settings.
259  */
260 static int adc_shiftbits(u8 timing)
261 {
262         int shift = 0;
263
264         switch (timing & TSL2563_TIMING_MASK) {
265         case TSL2563_TIMING_13MS:
266                 shift += 5;
267                 break;
268         case TSL2563_TIMING_100MS:
269                 shift += 2;
270                 break;
271         case TSL2563_TIMING_400MS:
272                 /* no-op */
273                 break;
274         }
275
276         if (!(timing & TSL2563_TIMING_GAIN16))
277                 shift += 4;
278
279         return shift;
280 }
281
282 /* Convert a HW ADC value to normalized scale. */
283 static u32 normalize_adc(u16 adc, u8 timing)
284 {
285         return adc << adc_shiftbits(timing);
286 }
287
288 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
289 {
290         unsigned int delay;
291
292         switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
293         case TSL2563_TIMING_13MS:
294                 delay = 14;
295                 break;
296         case TSL2563_TIMING_100MS:
297                 delay = 101;
298                 break;
299         default:
300                 delay = 402;
301         }
302         /*
303          * TODO: Make sure that we wait at least required delay but why we
304          * have to extend it one tick more?
305          */
306         schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
307 }
308
309 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
310 {
311         struct i2c_client *client = chip->client;
312
313         if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
314
315                 (adc > chip->gainlevel->max) ?
316                         chip->gainlevel++ : chip->gainlevel--;
317
318                 tsl2563_write(client, TSL2563_REG_TIMING,
319                               chip->gainlevel->gaintime);
320
321                 tsl2563_wait_adc(chip);
322                 tsl2563_wait_adc(chip);
323
324                 return 1;
325         } else
326                 return 0;
327 }
328
329 static int tsl2563_get_adc(struct tsl2563_chip *chip)
330 {
331         struct i2c_client *client = chip->client;
332         u8 buf0[2], buf1[2];
333         u16 adc0, adc1;
334         int retry = 1;
335         int ret = 0;
336
337         if (chip->state.event != PM_EVENT_ON)
338                 goto out;
339
340         if (!chip->int_enabled) {
341                 cancel_delayed_work(&chip->poweroff_work);
342
343                 if (!tsl2563_get_power(chip)) {
344                         ret = tsl2563_set_power(chip, 1);
345                         if (ret)
346                                 goto out;
347                         ret = tsl2563_configure(chip);
348                         if (ret)
349                                 goto out;
350                         tsl2563_wait_adc(chip);
351                 }
352         }
353
354         while (retry) {
355                 ret = tsl2563_read(client,
356                                    TSL2563_REG_DATA0LOW,
357                                    buf0, sizeof(buf0));
358                 if (ret != sizeof(buf0))
359                         goto out;
360
361                 ret = tsl2563_read(client, TSL2563_REG_DATA1LOW,
362                                    buf1, sizeof(buf1));
363                 if (ret != sizeof(buf1))
364                         goto out;
365
366                 adc0 = (buf0[1] << 8) + buf0[0];
367                 adc1 = (buf1[1] << 8) + buf1[0];
368
369                 retry = tsl2563_adjust_gainlevel(chip, adc0);
370         }
371
372         chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
373         chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
374
375         if (!chip->int_enabled)
376                 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
377
378         ret = 0;
379 out:
380         return ret;
381 }
382
383 static inline int calib_to_sysfs(u32 calib)
384 {
385         return (int) (((calib * CALIB_BASE_SYSFS) +
386                        CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
387 }
388
389 static inline u32 calib_from_sysfs(int value)
390 {
391         return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
392 }
393
394 /*
395  * Conversions between lux and ADC values.
396  *
397  * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
398  * appropriate constants. Different constants are needed for different
399  * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
400  * of the intensities in infrared and visible wavelengths). lux_table below
401  * lists the upper threshold of the adc1/adc0 ratio and the corresponding
402  * constants.
403  */
404
405 struct tsl2563_lux_coeff {
406         unsigned long ch_ratio;
407         unsigned long ch0_coeff;
408         unsigned long ch1_coeff;
409 };
410
411 static const struct tsl2563_lux_coeff lux_table[] = {
412         {
413                 .ch_ratio       = FRAC10K(1300),
414                 .ch0_coeff      = FRAC10K(315),
415                 .ch1_coeff      = FRAC10K(262),
416         }, {
417                 .ch_ratio       = FRAC10K(2600),
418                 .ch0_coeff      = FRAC10K(337),
419                 .ch1_coeff      = FRAC10K(430),
420         }, {
421                 .ch_ratio       = FRAC10K(3900),
422                 .ch0_coeff      = FRAC10K(363),
423                 .ch1_coeff      = FRAC10K(529),
424         }, {
425                 .ch_ratio       = FRAC10K(5200),
426                 .ch0_coeff      = FRAC10K(392),
427                 .ch1_coeff      = FRAC10K(605),
428         }, {
429                 .ch_ratio       = FRAC10K(6500),
430                 .ch0_coeff      = FRAC10K(229),
431                 .ch1_coeff      = FRAC10K(291),
432         }, {
433                 .ch_ratio       = FRAC10K(8000),
434                 .ch0_coeff      = FRAC10K(157),
435                 .ch1_coeff      = FRAC10K(180),
436         }, {
437                 .ch_ratio       = FRAC10K(13000),
438                 .ch0_coeff      = FRAC10K(34),
439                 .ch1_coeff      = FRAC10K(26),
440         }, {
441                 .ch_ratio       = ULONG_MAX,
442                 .ch0_coeff      = 0,
443                 .ch1_coeff      = 0,
444         },
445 };
446
447 /*
448  * Convert normalized, scaled ADC values to lux.
449  */
450 static unsigned int adc_to_lux(u32 adc0, u32 adc1)
451 {
452         const struct tsl2563_lux_coeff *lp = lux_table;
453         unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
454
455         ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
456
457         while (lp->ch_ratio < ratio)
458                 lp++;
459
460         lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
461
462         return (unsigned int) (lux >> ADC_FRAC_BITS);
463 }
464
465 /*--------------------------------------------------------------*/
466 /*                      Sysfs interface                         */
467 /*--------------------------------------------------------------*/
468
469 static ssize_t tsl2563_adc_show(struct device *dev,
470                                 struct device_attribute *attr, char *buf)
471 {
472         struct tsl2563_chip *chip = iio_priv(dev_get_drvdata(dev));
473         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
474         int ret;
475
476         mutex_lock(&chip->lock);
477
478         ret = tsl2563_get_adc(chip);
479         if (ret)
480                 goto out;
481
482         switch (this_attr->address) {
483         case 0:
484                 ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->data0);
485                 break;
486         case 1:
487                 ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->data1);
488                 break;
489         }
490 out:
491         mutex_unlock(&chip->lock);
492         return ret;
493 }
494
495 /* Apply calibration coefficient to ADC count. */
496 static u32 calib_adc(u32 adc, u32 calib)
497 {
498         unsigned long scaled = adc;
499
500         scaled *= calib;
501         scaled >>= CALIB_FRAC_BITS;
502
503         return (u32) scaled;
504 }
505
506 static ssize_t tsl2563_lux_show(struct device *dev,
507                                 struct device_attribute *attr, char *buf)
508 {
509         struct tsl2563_chip *chip = iio_priv(dev_get_drvdata(dev));
510         u32 calib0, calib1;
511         int ret;
512
513         mutex_lock(&chip->lock);
514
515         ret = tsl2563_get_adc(chip);
516         if (ret)
517                 goto out;
518
519         calib0 = calib_adc(chip->data0, chip->calib0) * chip->cover_comp_gain;
520         calib1 = calib_adc(chip->data1, chip->calib1) * chip->cover_comp_gain;
521
522         ret = snprintf(buf, PAGE_SIZE, "%d\n", adc_to_lux(calib0, calib1));
523
524 out:
525         mutex_unlock(&chip->lock);
526         return ret;
527 }
528
529 static ssize_t format_calib(char *buf, int len, u32 calib)
530 {
531         return snprintf(buf, PAGE_SIZE, "%d\n", calib_to_sysfs(calib));
532 }
533
534 static ssize_t tsl2563_calib_show(struct device *dev,
535                                 struct device_attribute *attr, char *buf)
536 {
537         struct tsl2563_chip *chip = iio_priv(dev_get_drvdata(dev));
538         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
539         int ret;
540
541         mutex_lock(&chip->lock);
542         switch (this_attr->address) {
543         case 0:
544                 ret = format_calib(buf, PAGE_SIZE, chip->calib0);
545                 break;
546         case 1:
547                 ret = format_calib(buf, PAGE_SIZE, chip->calib1);
548                 break;
549         default:
550                 ret = -ENODEV;
551         }
552         mutex_unlock(&chip->lock);
553         return ret;
554 }
555
556 static ssize_t tsl2563_calib_store(struct device *dev,
557                                 struct device_attribute *attr,
558                                 const char *buf, size_t len)
559 {
560         struct tsl2563_chip *chip = iio_priv(dev_get_drvdata(dev));
561         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
562         int value;
563         u32 calib;
564
565         if (1 != sscanf(buf, "%d", &value))
566                 return -EINVAL;
567
568         calib = calib_from_sysfs(value);
569
570         switch (this_attr->address) {
571         case 0:
572                 chip->calib0 = calib;
573                 break;
574         case 1:
575                 chip->calib1 = calib;
576                 break;
577         }
578
579         return len;
580 }
581
582 static IIO_DEVICE_ATTR(intensity0_both_raw, S_IRUGO,
583                 tsl2563_adc_show, NULL, 0);
584 static IIO_DEVICE_ATTR(intensity1_ir_raw, S_IRUGO,
585                 tsl2563_adc_show, NULL, 1);
586 static DEVICE_ATTR(illuminance0_input, S_IRUGO, tsl2563_lux_show, NULL);
587 static IIO_DEVICE_ATTR(intensity0_both_calibgain, S_IRUGO | S_IWUSR,
588                 tsl2563_calib_show, tsl2563_calib_store, 0);
589 static IIO_DEVICE_ATTR(intensity1_ir_calibgain, S_IRUGO | S_IWUSR,
590                 tsl2563_calib_show, tsl2563_calib_store, 1);
591
592 static ssize_t tsl2563_show_name(struct device *dev,
593                                 struct device_attribute *attr,
594                                 char *buf)
595 {
596         struct tsl2563_chip *chip = iio_priv(dev_get_drvdata(dev));
597         return sprintf(buf, "%s\n", chip->client->name);
598 }
599
600 static DEVICE_ATTR(name, S_IRUGO, tsl2563_show_name, NULL);
601
602 static struct attribute *tsl2563_attributes[] = {
603         &iio_dev_attr_intensity0_both_raw.dev_attr.attr,
604         &iio_dev_attr_intensity1_ir_raw.dev_attr.attr,
605         &dev_attr_illuminance0_input.attr,
606         &iio_dev_attr_intensity0_both_calibgain.dev_attr.attr,
607         &iio_dev_attr_intensity1_ir_calibgain.dev_attr.attr,
608         &dev_attr_name.attr,
609         NULL
610 };
611
612 static const struct attribute_group tsl2563_group = {
613         .attrs = tsl2563_attributes,
614 };
615
616 static ssize_t tsl2563_read_thresh(struct device *dev,
617                         struct device_attribute *attr,
618                         char *buf)
619 {
620         struct tsl2563_chip *chip = iio_priv(dev_get_drvdata(dev));
621         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
622         u16 val = 0;
623         switch (this_attr->address) {
624         case TSL2563_REG_HIGHLOW:
625                 val = chip->high_thres;
626                 break;
627         case TSL2563_REG_LOWLOW:
628                 val = chip->low_thres;
629                 break;
630         }
631         return snprintf(buf, PAGE_SIZE, "%d\n", val);
632 }
633
634 static ssize_t tsl2563_write_thresh(struct device *dev,
635                                 struct device_attribute *attr,
636                                 const char *buf,
637                                 size_t len)
638 {
639         struct tsl2563_chip *chip = iio_priv(dev_get_drvdata(dev));
640         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
641         unsigned long val;
642         int ret;
643
644         ret = strict_strtoul(buf, 10, &val);
645         if (ret)
646                 return ret;
647         mutex_lock(&chip->lock);
648         ret = tsl2563_write(chip->client, this_attr->address, val & 0xFF);
649         if (ret)
650                 goto error_ret;
651         ret = tsl2563_write(chip->client, this_attr->address + 1,
652                         (val >> 8) & 0xFF);
653         switch (this_attr->address) {
654         case TSL2563_REG_HIGHLOW:
655                 chip->high_thres = val;
656                 break;
657         case TSL2563_REG_LOWLOW:
658                 chip->low_thres = val;
659                 break;
660         }
661
662 error_ret:
663         mutex_unlock(&chip->lock);
664
665         return ret < 0 ? ret : len;
666 }
667
668 static IIO_DEVICE_ATTR(intensity0_both_raw_thresh_rising_value,
669                 S_IRUGO | S_IWUSR,
670                 tsl2563_read_thresh,
671                 tsl2563_write_thresh,
672                 TSL2563_REG_HIGHLOW);
673
674 static IIO_DEVICE_ATTR(intensity0_both_raw_thresh_falling_value,
675                 S_IRUGO | S_IWUSR,
676                 tsl2563_read_thresh,
677                 tsl2563_write_thresh,
678                 TSL2563_REG_LOWLOW);
679
680 static int tsl2563_int_th(struct iio_dev *dev_info,
681                         int index,
682                         s64 timestamp,
683                         int not_test)
684 {
685         struct tsl2563_chip *chip = iio_priv(dev_info);
686
687         chip->event_timestamp = timestamp;
688         schedule_work(&chip->work_thresh);
689
690         return 0;
691 }
692
693 static void tsl2563_int_bh(struct work_struct *work_s)
694 {
695         struct tsl2563_chip *chip
696                 = container_of(work_s,
697                         struct tsl2563_chip, work_thresh);
698         u8 cmd = TSL2563_CMD | TSL2563_CLEARINT;
699
700         iio_push_event(iio_priv_to_dev(chip), 0,
701                        IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_LIGHT,
702                                             0,
703                                             IIO_EV_TYPE_THRESH,
704                                             IIO_EV_DIR_EITHER),
705                        chip->event_timestamp);
706
707         /* reenable_irq */
708         enable_irq(chip->client->irq);
709         /* clear the interrupt and push the event */
710         i2c_master_send(chip->client, &cmd, sizeof(cmd));
711
712 }
713
714 static ssize_t tsl2563_write_interrupt_config(struct device *dev,
715                                         struct device_attribute *attr,
716                                         const char *buf,
717                                         size_t len)
718 {
719         struct iio_dev *indio_dev = dev_get_drvdata(dev);
720         struct tsl2563_chip *chip = iio_priv(indio_dev);
721         struct iio_event_attr *this_attr = to_iio_event_attr(attr);
722         int input, ret = 0;
723
724         ret = sscanf(buf, "%d", &input);
725         if (ret != 1)
726                 return -EINVAL;
727         mutex_lock(&chip->lock);
728         if (input && !(chip->intr & 0x30)) {
729                 iio_add_event_to_list(this_attr->listel,
730                                 &indio_dev->interrupts[0]->ev_list);
731                 chip->intr &= ~0x30;
732                 chip->intr |= 0x10;
733                 /* ensure the chip is actually on */
734                 cancel_delayed_work(&chip->poweroff_work);
735                 if (!tsl2563_get_power(chip)) {
736                         ret = tsl2563_set_power(chip, 1);
737                         if (ret)
738                                 goto out;
739                         ret = tsl2563_configure(chip);
740                         if (ret)
741                                 goto out;
742                 }
743                 ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
744                 chip->int_enabled = true;
745         }
746
747         if (!input && (chip->intr & 0x30)) {
748                 chip->intr |= ~0x30;
749                 ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
750                 iio_remove_event_from_list(this_attr->listel,
751                                         &indio_dev->interrupts[0]->ev_list);
752                 chip->int_enabled = false;
753                 /* now the interrupt is not enabled, we can go to sleep */
754                 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
755         }
756 out:
757         mutex_unlock(&chip->lock);
758
759         return (ret < 0) ? ret : len;
760 }
761
762 static ssize_t tsl2563_read_interrupt_config(struct device *dev,
763                                         struct device_attribute *attr,
764                                         char *buf)
765 {
766         struct tsl2563_chip *chip = iio_priv(dev_get_drvdata(dev));
767         int ret;
768         u8 rxbuf;
769         ssize_t len;
770
771         mutex_lock(&chip->lock);
772         ret = tsl2563_read(chip->client,
773                         TSL2563_REG_INT,
774                         &rxbuf,
775                         sizeof(rxbuf));
776         mutex_unlock(&chip->lock);
777         if (ret < 0)
778                 goto error_ret;
779         len = snprintf(buf, PAGE_SIZE, "%d\n", !!(rxbuf & 0x30));
780 error_ret:
781
782         return (ret < 0) ? ret : len;
783 }
784
785 IIO_EVENT_ATTR(intensity0_both_thresh_en,
786         tsl2563_read_interrupt_config,
787         tsl2563_write_interrupt_config,
788         0,
789         tsl2563_int_th);
790
791 static struct attribute *tsl2563_event_attributes[] = {
792         &iio_event_attr_intensity0_both_thresh_en.dev_attr.attr,
793         &iio_dev_attr_intensity0_both_raw_thresh_rising_value.dev_attr.attr,
794         &iio_dev_attr_intensity0_both_raw_thresh_falling_value.dev_attr.attr,
795         NULL,
796 };
797
798 static struct attribute_group tsl2563_event_attribute_group = {
799         .attrs = tsl2563_event_attributes,
800 };
801
802 /*--------------------------------------------------------------*/
803 /*                      Probe, Attach, Remove                   */
804 /*--------------------------------------------------------------*/
805 static struct i2c_driver tsl2563_i2c_driver;
806
807 static int __devinit tsl2563_probe(struct i2c_client *client,
808                                 const struct i2c_device_id *device_id)
809 {
810         struct iio_dev *indio_dev;
811         struct tsl2563_chip *chip;
812         struct tsl2563_platform_data *pdata = client->dev.platform_data;
813         int err = 0;
814         int ret;
815         u8 id;
816
817         indio_dev = iio_allocate_device(sizeof(*chip));
818         if (!indio_dev)
819                 return -ENOMEM;
820
821         chip = iio_priv(indio_dev);
822
823         INIT_WORK(&chip->work_thresh, tsl2563_int_bh);
824         i2c_set_clientdata(client, chip);
825         chip->client = client;
826
827         err = tsl2563_detect(chip);
828         if (err) {
829                 dev_err(&client->dev, "device not found, error %d\n", -err);
830                 goto fail1;
831         }
832
833         err = tsl2563_read_id(chip, &id);
834         if (err)
835                 goto fail1;
836
837         mutex_init(&chip->lock);
838
839         /* Default values used until userspace says otherwise */
840         chip->low_thres = 0x0;
841         chip->high_thres = 0xffff;
842         chip->gainlevel = tsl2563_gainlevel_table;
843         chip->intr = TSL2563_INT_PERSIST(4);
844         chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
845         chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
846
847         if (pdata)
848                 chip->cover_comp_gain = pdata->cover_comp_gain;
849         else
850                 chip->cover_comp_gain = 1;
851
852         dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
853
854         indio_dev->attrs = &tsl2563_group;
855         indio_dev->dev.parent = &client->dev;
856         indio_dev->driver_module = THIS_MODULE;
857         indio_dev->modes = INDIO_DIRECT_MODE;
858         if (client->irq) {
859                 indio_dev->num_interrupt_lines = 1;
860                 indio_dev->event_attrs
861                         = &tsl2563_event_attribute_group;
862         }
863         ret = iio_device_register(indio_dev);
864         if (ret)
865                 goto fail1;
866
867         if (client->irq) {
868                 ret = iio_register_interrupt_line(client->irq,
869                                                 indio_dev,
870                                                 0,
871                                                 IRQF_TRIGGER_RISING,
872                                                 client->name);
873                 if (ret)
874                         goto fail2;
875         }
876         err = tsl2563_configure(chip);
877         if (err)
878                 goto fail3;
879
880         INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
881         /* The interrupt cannot yet be enabled so this is fine without lock */
882         schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
883
884         return 0;
885 fail3:
886         if (client->irq)
887                 iio_unregister_interrupt_line(indio_dev, 0);
888 fail2:
889         iio_device_unregister(indio_dev);
890 fail1:
891         kfree(chip);
892         return err;
893 }
894
895 static int tsl2563_remove(struct i2c_client *client)
896 {
897         struct tsl2563_chip *chip = i2c_get_clientdata(client);
898         struct iio_dev *indio_dev = iio_priv_to_dev(chip);
899         if (!chip->int_enabled)
900                 cancel_delayed_work(&chip->poweroff_work);
901         /* Ensure that interrupts are disabled - then flush any bottom halves */
902         chip->intr |= ~0x30;
903         tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
904         flush_scheduled_work();
905         tsl2563_set_power(chip, 0);
906         if (client->irq)
907                 iio_unregister_interrupt_line(indio_dev, 0);
908         iio_device_unregister(indio_dev);
909
910         return 0;
911 }
912
913 static int tsl2563_suspend(struct i2c_client *client, pm_message_t state)
914 {
915         struct tsl2563_chip *chip = i2c_get_clientdata(client);
916         int ret;
917
918         mutex_lock(&chip->lock);
919
920         ret = tsl2563_set_power(chip, 0);
921         if (ret)
922                 goto out;
923
924         chip->state = state;
925
926 out:
927         mutex_unlock(&chip->lock);
928         return ret;
929 }
930
931 static int tsl2563_resume(struct i2c_client *client)
932 {
933         struct tsl2563_chip *chip = i2c_get_clientdata(client);
934         int ret;
935
936         mutex_lock(&chip->lock);
937
938         ret = tsl2563_set_power(chip, 1);
939         if (ret)
940                 goto out;
941
942         ret = tsl2563_configure(chip);
943         if (ret)
944                 goto out;
945
946         chip->state.event = PM_EVENT_ON;
947
948 out:
949         mutex_unlock(&chip->lock);
950         return ret;
951 }
952
953 static const struct i2c_device_id tsl2563_id[] = {
954         { "tsl2560", 0 },
955         { "tsl2561", 1 },
956         { "tsl2562", 2 },
957         { "tsl2563", 3 },
958         {}
959 };
960 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
961
962 static struct i2c_driver tsl2563_i2c_driver = {
963         .driver = {
964                 .name    = "tsl2563",
965         },
966         .suspend        = tsl2563_suspend,
967         .resume         = tsl2563_resume,
968         .probe          = tsl2563_probe,
969         .remove         = __devexit_p(tsl2563_remove),
970         .id_table       = tsl2563_id,
971 };
972
973 static int __init tsl2563_init(void)
974 {
975         return i2c_add_driver(&tsl2563_i2c_driver);
976 }
977
978 static void __exit tsl2563_exit(void)
979 {
980         i2c_del_driver(&tsl2563_i2c_driver);
981 }
982
983 MODULE_AUTHOR("Nokia Corporation");
984 MODULE_DESCRIPTION("tsl2563 light sensor driver");
985 MODULE_LICENSE("GPL");
986
987 module_init(tsl2563_init);
988 module_exit(tsl2563_exit);