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