Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tsl2x7x_core.c
Go to the documentation of this file.
1 /*
2  * Device driver for monitoring ambient light intensity in (lux)
3  * and proximity detection (prox) within the TAOS TSL2X7X family of devices.
4  *
5  * Copyright (c) 2012, 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/mutex.h>
27 #include <linux/interrupt.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/iio/events.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
33 #include "tsl2x7x.h"
34 
35 /* Cal defs*/
36 #define PROX_STAT_CAL 0
37 #define PROX_STAT_SAMP 1
38 #define MAX_SAMPLES_CAL 200
39 
40 /* TSL2X7X Device ID */
41 #define TRITON_ID 0x00
42 #define SWORDFISH_ID 0x30
43 #define HALIBUT_ID 0x20
44 
45 /* Lux calculation constants */
46 #define TSL2X7X_LUX_CALC_OVER_FLOW 65535
47 
48 /* TAOS Register definitions - note:
49  * depending on device, some of these register are not used and the
50  * register address is benign.
51  */
52 /* 2X7X register offsets */
53 #define TSL2X7X_MAX_CONFIG_REG 16
54 
55 /* Device Registers and Masks */
56 #define TSL2X7X_CNTRL 0x00
57 #define TSL2X7X_ALS_TIME 0X01
58 #define TSL2X7X_PRX_TIME 0x02
59 #define TSL2X7X_WAIT_TIME 0x03
60 #define TSL2X7X_ALS_MINTHRESHLO 0X04
61 #define TSL2X7X_ALS_MINTHRESHHI 0X05
62 #define TSL2X7X_ALS_MAXTHRESHLO 0X06
63 #define TSL2X7X_ALS_MAXTHRESHHI 0X07
64 #define TSL2X7X_PRX_MINTHRESHLO 0X08
65 #define TSL2X7X_PRX_MINTHRESHHI 0X09
66 #define TSL2X7X_PRX_MAXTHRESHLO 0X0A
67 #define TSL2X7X_PRX_MAXTHRESHHI 0X0B
68 #define TSL2X7X_PERSISTENCE 0x0C
69 #define TSL2X7X_PRX_CONFIG 0x0D
70 #define TSL2X7X_PRX_COUNT 0x0E
71 #define TSL2X7X_GAIN 0x0F
72 #define TSL2X7X_NOTUSED 0x10
73 #define TSL2X7X_REVID 0x11
74 #define TSL2X7X_CHIPID 0x12
75 #define TSL2X7X_STATUS 0x13
76 #define TSL2X7X_ALS_CHAN0LO 0x14
77 #define TSL2X7X_ALS_CHAN0HI 0x15
78 #define TSL2X7X_ALS_CHAN1LO 0x16
79 #define TSL2X7X_ALS_CHAN1HI 0x17
80 #define TSL2X7X_PRX_LO 0x18
81 #define TSL2X7X_PRX_HI 0x19
82 
83 /* tsl2X7X cmd reg masks */
84 #define TSL2X7X_CMD_REG 0x80
85 #define TSL2X7X_CMD_SPL_FN 0x60
86 
87 #define TSL2X7X_CMD_PROX_INT_CLR 0X05
88 #define TSL2X7X_CMD_ALS_INT_CLR 0x06
89 #define TSL2X7X_CMD_PROXALS_INT_CLR 0X07
90 
91 /* tsl2X7X cntrl reg masks */
92 #define TSL2X7X_CNTL_ADC_ENBL 0x02
93 #define TSL2X7X_CNTL_PWR_ON 0x01
94 
95 /* tsl2X7X status reg masks */
96 #define TSL2X7X_STA_ADC_VALID 0x01
97 #define TSL2X7X_STA_PRX_VALID 0x02
98 #define TSL2X7X_STA_ADC_PRX_VALID (TSL2X7X_STA_ADC_VALID |\
99  TSL2X7X_STA_PRX_VALID)
100 #define TSL2X7X_STA_ALS_INTR 0x10
101 #define TSL2X7X_STA_PRX_INTR 0x20
102 
103 /* tsl2X7X cntrl reg masks */
104 #define TSL2X7X_CNTL_REG_CLEAR 0x00
105 #define TSL2X7X_CNTL_PROX_INT_ENBL 0X20
106 #define TSL2X7X_CNTL_ALS_INT_ENBL 0X10
107 #define TSL2X7X_CNTL_WAIT_TMR_ENBL 0X08
108 #define TSL2X7X_CNTL_PROX_DET_ENBL 0X04
109 #define TSL2X7X_CNTL_PWRON 0x01
110 #define TSL2X7X_CNTL_ALSPON_ENBL 0x03
111 #define TSL2X7X_CNTL_INTALSPON_ENBL 0x13
112 #define TSL2X7X_CNTL_PROXPON_ENBL 0x0F
113 #define TSL2X7X_CNTL_INTPROXPON_ENBL 0x2F
114 
115 /*Prox diode to use */
116 #define TSL2X7X_DIODE0 0x10
117 #define TSL2X7X_DIODE1 0x20
118 #define TSL2X7X_DIODE_BOTH 0x30
119 
120 /* LED Power */
121 #define TSL2X7X_mA100 0x00
122 #define TSL2X7X_mA50 0x40
123 #define TSL2X7X_mA25 0x80
124 #define TSL2X7X_mA13 0xD0
125 #define TSL2X7X_MAX_TIMER_CNT (0xFF)
126 
127 /*Common device IIO EventMask */
128 #define TSL2X7X_EVENT_MASK \
129  (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
130  IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)),
131 
132 #define TSL2X7X_MIN_ITIME 3
133 
134 /* TAOS txx2x7x Device family members */
135 enum {
146 };
147 
148 enum {
152 };
153 
155  int integer;
156  int fract;
157 };
158 
159 /* Per-device data */
164 };
165 
167  int min;
168  int max;
169  int mean;
170  unsigned long stddev;
171 };
172 
176  const struct iio_info *info;
177 };
178 
179 struct tsl2X7X_chip {
182  struct mutex als_mutex;
193  const struct iio_info *info;
195  /* This structure is intentionally large to accommodate
196  * updates via sysfs. */
197  /* Sized to 9 = max 8 segments + 1 termination segment */
199 };
200 
201 /* Different devices require different coefficents */
202 static const struct tsl2x7x_lux tsl2x71_lux_table[] = {
203  { 14461, 611, 1211 },
204  { 18540, 352, 623 },
205  { 0, 0, 0 },
206 };
207 
208 static const struct tsl2x7x_lux tmd2x71_lux_table[] = {
209  { 11635, 115, 256 },
210  { 15536, 87, 179 },
211  { 0, 0, 0 },
212 };
213 
214 static const struct tsl2x7x_lux tsl2x72_lux_table[] = {
215  { 14013, 466, 917 },
216  { 18222, 310, 552 },
217  { 0, 0, 0 },
218 };
219 
220 static const struct tsl2x7x_lux tmd2x72_lux_table[] = {
221  { 13218, 130, 262 },
222  { 17592, 92, 169 },
223  { 0, 0, 0 },
224 };
225 
226 static const struct tsl2x7x_lux *tsl2x7x_default_lux_table_group[] = {
227  [tsl2571] = tsl2x71_lux_table,
228  [tsl2671] = tsl2x71_lux_table,
229  [tmd2671] = tmd2x71_lux_table,
230  [tsl2771] = tsl2x71_lux_table,
231  [tmd2771] = tmd2x71_lux_table,
232  [tsl2572] = tsl2x72_lux_table,
233  [tsl2672] = tsl2x72_lux_table,
234  [tmd2672] = tmd2x72_lux_table,
235  [tsl2772] = tsl2x72_lux_table,
236  [tmd2772] = tmd2x72_lux_table,
237 };
238 
239 static const struct tsl2x7x_settings tsl2x7x_default_settings = {
240  .als_time = 219, /* 101 ms */
241  .als_gain = 0,
242  .prx_time = 254, /* 5.4 ms */
243  .prox_gain = 1,
244  .wait_time = 245,
245  .prox_config = 0,
246  .als_gain_trim = 1000,
247  .als_cal_target = 150,
248  .als_thresh_low = 200,
249  .als_thresh_high = 256,
250  .persistence = 255,
251  .interrupts_en = 0,
252  .prox_thres_low = 0,
253  .prox_thres_high = 512,
254  .prox_max_samples_cal = 30,
255  .prox_pulse_count = 8
256 };
257 
258 static const s16 tsl2X7X_als_gainadj[] = {
259  1,
260  8,
261  16,
262  120
263 };
264 
265 static const s16 tsl2X7X_prx_gainadj[] = {
266  1,
267  2,
268  4,
269  8
270 };
271 
272 /* Channel variations */
273 enum {
279 };
280 
281 static const u8 device_channel_config[] = {
282  ALS,
283  PRX,
284  PRX,
285  ALSPRX,
286  ALSPRX,
287  ALS,
288  PRX2,
289  PRX2,
290  ALSPRX2,
291  ALSPRX2
292 };
293 
301 static int
302 tsl2x7x_parse_buffer(const char *buf, struct tsl2x7x_parse_result *result)
303 {
304  int integer = 0, fract = 0, fract_mult = 100000;
305  bool integer_part = true, negative = false;
306 
307  if (buf[0] == '-') {
308  negative = true;
309  buf++;
310  }
311 
312  while (*buf) {
313  if ('0' <= *buf && *buf <= '9') {
314  if (integer_part)
315  integer = integer*10 + *buf - '0';
316  else {
317  fract += fract_mult*(*buf - '0');
318  if (fract_mult == 1)
319  break;
320  fract_mult /= 10;
321  }
322  } else if (*buf == '\n') {
323  if (*(buf + 1) == '\0')
324  break;
325  else
326  return -EINVAL;
327  } else if (*buf == '.') {
328  integer_part = false;
329  } else {
330  return -EINVAL;
331  }
332  buf++;
333  }
334  if (negative) {
335  if (integer)
336  integer = -integer;
337  else
338  fract = -fract;
339  }
340 
341  result->integer = integer;
342  result->fract = fract;
343 
344  return 0;
345 }
346 
354 static int
355 tsl2x7x_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
356 {
357  int ret = 0;
358 
359  /* select register to write */
360  ret = i2c_smbus_write_byte(client, (TSL2X7X_CMD_REG | reg));
361  if (ret < 0) {
362  dev_err(&client->dev, "%s: failed to write register %x\n"
363  , __func__, reg);
364  return ret;
365  }
366 
367  /* read the data */
368  ret = i2c_smbus_read_byte(client);
369  if (ret >= 0)
370  *val = (u8)ret;
371  else
372  dev_err(&client->dev, "%s: failed to read register %x\n"
373  , __func__, reg);
374 
375  return ret;
376 }
377 
393 static int tsl2x7x_get_lux(struct iio_dev *indio_dev)
394 {
395  u16 ch0, ch1; /* separated ch0/ch1 data from device */
396  u32 lux; /* raw lux calculated from device data */
397  u64 lux64;
398  u32 ratio;
399  u8 buf[4];
400  struct tsl2x7x_lux *p;
401  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
402  int i, ret;
403  u32 ch0lux = 0;
404  u32 ch1lux = 0;
405 
406  if (mutex_trylock(&chip->als_mutex) == 0)
407  return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
408 
410  /* device is not enabled */
411  dev_err(&chip->client->dev, "%s: device is not enabled\n",
412  __func__);
413  ret = -EBUSY ;
414  goto out_unlock;
415  }
416 
417  ret = tsl2x7x_i2c_read(chip->client,
418  (TSL2X7X_CMD_REG | TSL2X7X_STATUS), &buf[0]);
419  if (ret < 0) {
420  dev_err(&chip->client->dev,
421  "%s: Failed to read STATUS Reg\n", __func__);
422  goto out_unlock;
423  }
424  /* is data new & valid */
425  if (!(buf[0] & TSL2X7X_STA_ADC_VALID)) {
426  dev_err(&chip->client->dev,
427  "%s: data not valid yet\n", __func__);
428  ret = chip->als_cur_info.lux; /* return LAST VALUE */
429  goto out_unlock;
430  }
431 
432  for (i = 0; i < 4; i++) {
433  ret = tsl2x7x_i2c_read(chip->client,
435  &buf[i]);
436  if (ret < 0) {
437  dev_err(&chip->client->dev,
438  "%s: failed to read. err=%x\n", __func__, ret);
439  goto out_unlock;
440  }
441  }
442 
443  /* clear any existing interrupt status */
444  ret = i2c_smbus_write_byte(chip->client,
445  (TSL2X7X_CMD_REG |
448  if (ret < 0) {
449  dev_err(&chip->client->dev,
450  "%s: i2c_write_command failed - err = %d\n",
451  __func__, ret);
452  goto out_unlock; /* have no data, so return failure */
453  }
454 
455  /* extract ALS/lux data */
456  ch0 = le16_to_cpup((const __le16 *)&buf[0]);
457  ch1 = le16_to_cpup((const __le16 *)&buf[2]);
458 
459  chip->als_cur_info.als_ch0 = ch0;
460  chip->als_cur_info.als_ch1 = ch1;
461 
462  if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation)) {
464  goto return_max;
465  }
466 
467  if (ch0 == 0) {
468  /* have no data, so return LAST VALUE */
469  ret = chip->als_cur_info.lux;
470  goto out_unlock;
471  }
472  /* calculate ratio */
473  ratio = (ch1 << 15) / ch0;
474  /* convert to unscaled lux using the pointer to the table */
475  p = (struct tsl2x7x_lux *) chip->tsl2x7x_device_lux;
476  while (p->ratio != 0 && p->ratio < ratio)
477  p++;
478 
479  if (p->ratio == 0) {
480  lux = 0;
481  } else {
482  ch0lux = DIV_ROUND_UP((ch0 * p->ch0),
483  tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
484  ch1lux = DIV_ROUND_UP((ch1 * p->ch1),
485  tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
486  lux = ch0lux - ch1lux;
487  }
488 
489  /* note: lux is 31 bit max at this point */
490  if (ch1lux > ch0lux) {
491  dev_dbg(&chip->client->dev, "ch1lux > ch0lux-return last value\n");
492  ret = chip->als_cur_info.lux;
493  goto out_unlock;
494  }
495 
496  /* adjust for active time scale */
497  if (chip->als_time_scale == 0)
498  lux = 0;
499  else
500  lux = (lux + (chip->als_time_scale >> 1)) /
501  chip->als_time_scale;
502 
503  /* adjust for active gain scale
504  * The tsl2x7x_device_lux tables have a factor of 256 built-in.
505  * User-specified gain provides a multiplier.
506  * Apply user-specified gain before shifting right to retain precision.
507  * Use 64 bits to avoid overflow on multiplication.
508  * Then go back to 32 bits before division to avoid using div_u64().
509  */
510 
511  lux64 = lux;
512  lux64 = lux64 * chip->tsl2x7x_settings.als_gain_trim;
513  lux64 >>= 8;
514  lux = lux64;
515  lux = (lux + 500) / 1000;
516 
517  if (lux > TSL2X7X_LUX_CALC_OVER_FLOW) /* check for overflow */
519 
520  /* Update the structure with the latest lux. */
521 return_max:
522  chip->als_cur_info.lux = lux;
523  ret = lux;
524 
525 out_unlock:
526  mutex_unlock(&chip->als_mutex);
527 
528  return ret;
529 }
530 
537 static int tsl2x7x_get_prox(struct iio_dev *indio_dev)
538 {
539  int i;
540  int ret;
541  u8 status;
542  u8 chdata[2];
543  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
544 
545  if (mutex_trylock(&chip->prox_mutex) == 0) {
546  dev_err(&chip->client->dev,
547  "%s: Can't get prox mutex\n", __func__);
548  return -EBUSY;
549  }
550 
551  ret = tsl2x7x_i2c_read(chip->client,
552  (TSL2X7X_CMD_REG | TSL2X7X_STATUS), &status);
553  if (ret < 0) {
554  dev_err(&chip->client->dev,
555  "%s: i2c err=%d\n", __func__, ret);
556  goto prox_poll_err;
557  }
558 
559  switch (chip->id) {
560  case tsl2571:
561  case tsl2671:
562  case tmd2671:
563  case tsl2771:
564  case tmd2771:
565  if (!(status & TSL2X7X_STA_ADC_VALID))
566  goto prox_poll_err;
567  break;
568  case tsl2572:
569  case tsl2672:
570  case tmd2672:
571  case tsl2772:
572  case tmd2772:
573  if (!(status & TSL2X7X_STA_PRX_VALID))
574  goto prox_poll_err;
575  break;
576  }
577 
578  for (i = 0; i < 2; i++) {
579  ret = tsl2x7x_i2c_read(chip->client,
580  (TSL2X7X_CMD_REG |
581  (TSL2X7X_PRX_LO + i)), &chdata[i]);
582  if (ret < 0)
583  goto prox_poll_err;
584  }
585 
586  chip->prox_data =
587  le16_to_cpup((const __le16 *)&chdata[0]);
588 
589 prox_poll_err:
590 
591  mutex_unlock(&chip->prox_mutex);
592 
593  return chip->prox_data;
594 }
595 
603 static void tsl2x7x_defaults(struct tsl2X7X_chip *chip)
604 {
605  /* If Operational settings defined elsewhere.. */
606  if (chip->pdata && chip->pdata->platform_default_settings != 0)
607  memcpy(&(chip->tsl2x7x_settings),
608  chip->pdata->platform_default_settings,
609  sizeof(tsl2x7x_default_settings));
610  else
611  memcpy(&(chip->tsl2x7x_settings),
612  &tsl2x7x_default_settings,
613  sizeof(tsl2x7x_default_settings));
614 
615  /* Load up the proper lux table. */
616  if (chip->pdata && chip->pdata->platform_lux_table[0].ratio != 0)
618  chip->pdata->platform_lux_table,
619  sizeof(chip->pdata->platform_lux_table));
620  else
622  (struct tsl2x7x_lux *)tsl2x7x_default_lux_table_group[chip->id],
624 }
625 
632 static int tsl2x7x_als_calibrate(struct iio_dev *indio_dev)
633 {
634  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
635  u8 reg_val;
636  int gain_trim_val;
637  int ret;
638  int lux_val;
639 
640  ret = i2c_smbus_write_byte(chip->client,
642  if (ret < 0) {
643  dev_err(&chip->client->dev,
644  "%s: failed to write CNTRL register, ret=%d\n",
645  __func__, ret);
646  return ret;
647  }
648 
649  reg_val = i2c_smbus_read_byte(chip->client);
650  if ((reg_val & (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON))
652  dev_err(&chip->client->dev,
653  "%s: failed: ADC not enabled\n", __func__);
654  return -1;
655  }
656 
657  ret = i2c_smbus_write_byte(chip->client,
659  if (ret < 0) {
660  dev_err(&chip->client->dev,
661  "%s: failed to write ctrl reg: ret=%d\n",
662  __func__, ret);
663  return ret;
664  }
665 
666  reg_val = i2c_smbus_read_byte(chip->client);
667  if ((reg_val & TSL2X7X_STA_ADC_VALID) != TSL2X7X_STA_ADC_VALID) {
668  dev_err(&chip->client->dev,
669  "%s: failed: STATUS - ADC not valid.\n", __func__);
670  return -ENODATA;
671  }
672 
673  lux_val = tsl2x7x_get_lux(indio_dev);
674  if (lux_val < 0) {
675  dev_err(&chip->client->dev,
676  "%s: failed to get lux\n", __func__);
677  return lux_val;
678  }
679 
680  gain_trim_val = (((chip->tsl2x7x_settings.als_cal_target)
681  * chip->tsl2x7x_settings.als_gain_trim) / lux_val);
682  if ((gain_trim_val < 250) || (gain_trim_val > 4000))
683  return -ERANGE;
684 
685  chip->tsl2x7x_settings.als_gain_trim = gain_trim_val;
686  dev_info(&chip->client->dev,
687  "%s als_calibrate completed\n", chip->client->name);
688 
689  return (int) gain_trim_val;
690 }
691 
692 static int tsl2x7x_chip_on(struct iio_dev *indio_dev)
693 {
694  int i;
695  int ret = 0;
696  u8 *dev_reg;
697  u8 utmp;
698  int als_count;
699  int als_time;
700  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
701  u8 reg_val = 0;
702 
703  if (chip->pdata && chip->pdata->power_on)
704  chip->pdata->power_on(indio_dev);
705 
706  /* Non calculated parameters */
708  chip->tsl2x7x_settings.prx_time;
710  chip->tsl2x7x_settings.wait_time;
712  chip->tsl2x7x_settings.prox_config;
713 
715  (chip->tsl2x7x_settings.als_thresh_low) & 0xFF;
717  (chip->tsl2x7x_settings.als_thresh_low >> 8) & 0xFF;
719  (chip->tsl2x7x_settings.als_thresh_high) & 0xFF;
721  (chip->tsl2x7x_settings.als_thresh_high >> 8) & 0xFF;
723  chip->tsl2x7x_settings.persistence;
724 
726  chip->tsl2x7x_settings.prox_pulse_count;
728  chip->tsl2x7x_settings.prox_thres_low;
730  chip->tsl2x7x_settings.prox_thres_high;
731 
732  /* and make sure we're not already on */
734  /* if forcing a register update - turn off, then on */
735  dev_info(&chip->client->dev, "device is already enabled\n");
736  return -EINVAL;
737  }
738 
739  /* determine als integration register */
740  als_count = (chip->tsl2x7x_settings.als_time * 100 + 135) / 270;
741  if (als_count == 0)
742  als_count = 1; /* ensure at least one cycle */
743 
744  /* convert back to time (encompasses overrides) */
745  als_time = (als_count * 27 + 5) / 10;
746  chip->tsl2x7x_config[TSL2X7X_ALS_TIME] = 256 - als_count;
747 
748  /* Set the gain based on tsl2x7x_settings struct */
750  (chip->tsl2x7x_settings.als_gain |
752  | ((chip->tsl2x7x_settings.prox_gain) << 2));
753 
754  /* set chip struct re scaling and saturation */
755  chip->als_saturation = als_count * 922; /* 90% of full scale */
756  chip->als_time_scale = (als_time + 25) / 50;
757 
758  /* TSL2X7X Specific power-on / adc enable sequence
759  * Power on the device 1st. */
760  utmp = TSL2X7X_CNTL_PWR_ON;
761  ret = i2c_smbus_write_byte_data(chip->client,
763  if (ret < 0) {
764  dev_err(&chip->client->dev,
765  "%s: failed on CNTRL reg.\n", __func__);
766  return ret;
767  }
768 
769  /* Use the following shadow copy for our delay before enabling ADC.
770  * Write all the registers. */
771  for (i = 0, dev_reg = chip->tsl2x7x_config;
772  i < TSL2X7X_MAX_CONFIG_REG; i++) {
773  ret = i2c_smbus_write_byte_data(chip->client,
774  TSL2X7X_CMD_REG + i, *dev_reg++);
775  if (ret < 0) {
776  dev_err(&chip->client->dev,
777  "%s: failed on write to reg %d.\n", __func__, i);
778  return ret;
779  }
780  }
781 
782  mdelay(3); /* Power-on settling time */
783 
784  /* NOW enable the ADC
785  * initialize the desired mode of operation */
786  utmp = TSL2X7X_CNTL_PWR_ON |
789  ret = i2c_smbus_write_byte_data(chip->client,
791  if (ret < 0) {
792  dev_err(&chip->client->dev,
793  "%s: failed on 2nd CTRL reg.\n", __func__);
794  return ret;
795  }
796 
798 
799  if (chip->tsl2x7x_settings.interrupts_en != 0) {
800  dev_info(&chip->client->dev, "Setting Up Interrupt(s)\n");
801 
803  if ((chip->tsl2x7x_settings.interrupts_en == 0x20) ||
804  (chip->tsl2x7x_settings.interrupts_en == 0x30))
805  reg_val |= TSL2X7X_CNTL_PROX_DET_ENBL;
806 
807  reg_val |= chip->tsl2x7x_settings.interrupts_en;
808  ret = i2c_smbus_write_byte_data(chip->client,
809  (TSL2X7X_CMD_REG | TSL2X7X_CNTRL), reg_val);
810  if (ret < 0)
811  dev_err(&chip->client->dev,
812  "%s: failed in tsl2x7x_IOCTL_INT_SET.\n",
813  __func__);
814 
815  /* Clear out any initial interrupts */
816  ret = i2c_smbus_write_byte(chip->client,
819  if (ret < 0) {
820  dev_err(&chip->client->dev,
821  "%s: Failed to clear Int status\n",
822  __func__);
823  return ret;
824  }
825  }
826 
827  return ret;
828 }
829 
830 static int tsl2x7x_chip_off(struct iio_dev *indio_dev)
831 {
832  int ret;
833  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
834 
835  /* turn device off */
837 
838  ret = i2c_smbus_write_byte_data(chip->client,
840 
841  if (chip->pdata && chip->pdata->power_off)
842  chip->pdata->power_off(chip->client);
843 
844  return ret;
845 }
846 
857 static
858 int tsl2x7x_invoke_change(struct iio_dev *indio_dev)
859 {
860  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
862 
863  mutex_lock(&chip->als_mutex);
864  mutex_lock(&chip->prox_mutex);
865 
866  if (device_status == TSL2X7X_CHIP_WORKING)
867  tsl2x7x_chip_off(indio_dev);
868 
869  tsl2x7x_chip_on(indio_dev);
870 
871  if (device_status != TSL2X7X_CHIP_WORKING)
872  tsl2x7x_chip_off(indio_dev);
873 
874  mutex_unlock(&chip->prox_mutex);
875  mutex_unlock(&chip->als_mutex);
876 
877  return 0;
878 }
879 
880 static
881 void tsl2x7x_prox_calculate(int *data, int length,
882  struct tsl2x7x_prox_stat *statP)
883 {
884  int i;
885  int sample_sum;
886  int tmp;
887 
888  if (length == 0)
889  length = 1;
890 
891  sample_sum = 0;
892  statP->min = INT_MAX;
893  statP->max = INT_MIN;
894  for (i = 0; i < length; i++) {
895  sample_sum += data[i];
896  statP->min = min(statP->min, data[i]);
897  statP->max = max(statP->max, data[i]);
898  }
899 
900  statP->mean = sample_sum / length;
901  sample_sum = 0;
902  for (i = 0; i < length; i++) {
903  tmp = data[i] - statP->mean;
904  sample_sum += tmp * tmp;
905  }
906  statP->stddev = int_sqrt((long)sample_sum)/length;
907 }
908 
916 static void tsl2x7x_prox_cal(struct iio_dev *indio_dev)
917 {
918  int prox_history[MAX_SAMPLES_CAL + 1];
919  int i;
920  struct tsl2x7x_prox_stat prox_stat_data[2];
921  struct tsl2x7x_prox_stat *calP;
922  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
923  u8 tmp_irq_settings;
924  u8 current_state = chip->tsl2x7x_chip_status;
925 
926  if (chip->tsl2x7x_settings.prox_max_samples_cal > MAX_SAMPLES_CAL) {
927  dev_err(&chip->client->dev,
928  "%s: max prox samples cal is too big: %d\n",
929  __func__, chip->tsl2x7x_settings.prox_max_samples_cal);
930  chip->tsl2x7x_settings.prox_max_samples_cal = MAX_SAMPLES_CAL;
931  }
932 
933  /* have to stop to change settings */
934  tsl2x7x_chip_off(indio_dev);
935 
936  /* Enable proximity detection save just in case prox not wanted yet*/
937  tmp_irq_settings = chip->tsl2x7x_settings.interrupts_en;
938  chip->tsl2x7x_settings.interrupts_en |= TSL2X7X_CNTL_PROX_INT_ENBL;
939 
940  /*turn on device if not already on*/
941  tsl2x7x_chip_on(indio_dev);
942 
943  /*gather the samples*/
944  for (i = 0; i < chip->tsl2x7x_settings.prox_max_samples_cal; i++) {
945  mdelay(15);
946  tsl2x7x_get_prox(indio_dev);
947  prox_history[i] = chip->prox_data;
948  dev_info(&chip->client->dev, "2 i=%d prox data= %d\n",
949  i, chip->prox_data);
950  }
951 
952  tsl2x7x_chip_off(indio_dev);
953  calP = &prox_stat_data[PROX_STAT_CAL];
954  tsl2x7x_prox_calculate(prox_history,
955  chip->tsl2x7x_settings.prox_max_samples_cal, calP);
956  chip->tsl2x7x_settings.prox_thres_high = (calP->max << 1) - calP->mean;
957 
958  dev_info(&chip->client->dev, " cal min=%d mean=%d max=%d\n",
959  calP->min, calP->mean, calP->max);
960  dev_info(&chip->client->dev,
961  "%s proximity threshold set to %d\n",
962  chip->client->name, chip->tsl2x7x_settings.prox_thres_high);
963 
964  /* back to the way they were */
965  chip->tsl2x7x_settings.interrupts_en = tmp_irq_settings;
966  if (current_state == TSL2X7X_CHIP_WORKING)
967  tsl2x7x_chip_on(indio_dev);
968 }
969 
970 static ssize_t tsl2x7x_power_state_show(struct device *dev,
971  struct device_attribute *attr, char *buf)
972 {
973  struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
974 
975  return snprintf(buf, PAGE_SIZE, "%d\n", chip->tsl2x7x_chip_status);
976 }
977 
978 static ssize_t tsl2x7x_power_state_store(struct device *dev,
979  struct device_attribute *attr, const char *buf, size_t len)
980 {
981  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
982  bool value;
983 
984  if (strtobool(buf, &value))
985  return -EINVAL;
986 
987  if (value)
988  tsl2x7x_chip_on(indio_dev);
989  else
990  tsl2x7x_chip_off(indio_dev);
991 
992  return len;
993 }
994 
995 static ssize_t tsl2x7x_gain_available_show(struct device *dev,
996  struct device_attribute *attr, char *buf)
997 {
998  struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
999 
1000  switch (chip->id) {
1001  case tsl2571:
1002  case tsl2671:
1003  case tmd2671:
1004  case tsl2771:
1005  case tmd2771:
1006  return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 128");
1007  break;
1008  }
1009 
1010  return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 120");
1011 }
1012 
1013 static ssize_t tsl2x7x_prox_gain_available_show(struct device *dev,
1014  struct device_attribute *attr, char *buf)
1015 {
1016  return snprintf(buf, PAGE_SIZE, "%s\n", "1 2 4 8");
1017 }
1018 
1019 static ssize_t tsl2x7x_als_time_show(struct device *dev,
1020  struct device_attribute *attr, char *buf)
1021 {
1022  struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
1023  int y, z;
1024 
1025  y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1026  z = y * TSL2X7X_MIN_ITIME;
1027  y /= 1000;
1028  z %= 1000;
1029 
1030  return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1031 }
1032 
1033 static ssize_t tsl2x7x_als_time_store(struct device *dev,
1034  struct device_attribute *attr, const char *buf, size_t len)
1035 {
1036  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1037  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1038  struct tsl2x7x_parse_result result;
1039 
1040  result.integer = 0;
1041  result.fract = 0;
1042 
1043  tsl2x7x_parse_buffer(buf, &result);
1044 
1045  result.fract /= 1000;
1046  result.fract /= 3;
1047  chip->tsl2x7x_settings.als_time =
1048  (TSL2X7X_MAX_TIMER_CNT - (u8)result.fract);
1049 
1050  dev_info(&chip->client->dev, "%s: als time = %d",
1051  __func__, chip->tsl2x7x_settings.als_time);
1052 
1053  tsl2x7x_invoke_change(indio_dev);
1054 
1055  return IIO_VAL_INT_PLUS_MICRO;
1056 }
1057 
1058 static IIO_CONST_ATTR(in_illuminance0_integration_time_available,
1059  ".00272 - .696");
1060 
1061 static ssize_t tsl2x7x_als_cal_target_show(struct device *dev,
1062  struct device_attribute *attr, char *buf)
1063 {
1064  struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
1065 
1066  return snprintf(buf, PAGE_SIZE, "%d\n",
1067  chip->tsl2x7x_settings.als_cal_target);
1068 }
1069 
1070 static ssize_t tsl2x7x_als_cal_target_store(struct device *dev,
1071  struct device_attribute *attr, const char *buf, size_t len)
1072 {
1073  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1074  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1075  unsigned long value;
1076 
1077  if (kstrtoul(buf, 0, &value))
1078  return -EINVAL;
1079 
1080  if (value)
1081  chip->tsl2x7x_settings.als_cal_target = value;
1082 
1083  tsl2x7x_invoke_change(indio_dev);
1084 
1085  return len;
1086 }
1087 
1088 /* persistence settings */
1089 static ssize_t tsl2x7x_als_persistence_show(struct device *dev,
1090  struct device_attribute *attr, char *buf)
1091 {
1092  struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
1093  int y, z, filter_delay;
1094 
1095  /* Determine integration time */
1096  y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1097  z = y * TSL2X7X_MIN_ITIME;
1098  filter_delay = z * (chip->tsl2x7x_settings.persistence & 0x0F);
1099  y = (filter_delay / 1000);
1100  z = (filter_delay % 1000);
1101 
1102  return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1103 }
1104 
1105 static ssize_t tsl2x7x_als_persistence_store(struct device *dev,
1106  struct device_attribute *attr, const char *buf, size_t len)
1107 {
1108  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1109  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1110  struct tsl2x7x_parse_result result;
1111  int y, z, filter_delay;
1112 
1113  result.integer = 0;
1114  result.fract = 0;
1115  tsl2x7x_parse_buffer(buf, &result);
1116 
1117  result.fract /= 1000;
1118  y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1119  z = y * TSL2X7X_MIN_ITIME;
1120 
1121  filter_delay =
1122  DIV_ROUND_UP(((result.integer * 1000) + result.fract), z);
1123 
1124  chip->tsl2x7x_settings.persistence &= 0xF0;
1125  chip->tsl2x7x_settings.persistence |= (filter_delay & 0x0F);
1126 
1127  dev_info(&chip->client->dev, "%s: als persistence = %d",
1128  __func__, filter_delay);
1129 
1130  tsl2x7x_invoke_change(indio_dev);
1131 
1132  return IIO_VAL_INT_PLUS_MICRO;
1133 }
1134 
1135 static ssize_t tsl2x7x_prox_persistence_show(struct device *dev,
1136  struct device_attribute *attr, char *buf)
1137 {
1138  struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
1139  int y, z, filter_delay;
1140 
1141  /* Determine integration time */
1142  y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
1143  z = y * TSL2X7X_MIN_ITIME;
1144  filter_delay = z * ((chip->tsl2x7x_settings.persistence & 0xF0) >> 4);
1145  y = (filter_delay / 1000);
1146  z = (filter_delay % 1000);
1147 
1148  return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1149 }
1150 
1151 static ssize_t tsl2x7x_prox_persistence_store(struct device *dev,
1152  struct device_attribute *attr, const char *buf, size_t len)
1153 {
1154  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1155  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1156  struct tsl2x7x_parse_result result;
1157  int y, z, filter_delay;
1158 
1159  result.integer = 0;
1160  result.fract = 0;
1161  tsl2x7x_parse_buffer(buf, &result);
1162 
1163  result.fract /= 1000;
1164  y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
1165  z = y * TSL2X7X_MIN_ITIME;
1166 
1167  filter_delay =
1168  DIV_ROUND_UP(((result.integer * 1000) + result.fract), z);
1169 
1170  chip->tsl2x7x_settings.persistence &= 0x0F;
1171  chip->tsl2x7x_settings.persistence |= ((filter_delay << 4) & 0xF0);
1172 
1173  dev_info(&chip->client->dev, "%s: prox persistence = %d",
1174  __func__, filter_delay);
1175 
1176  tsl2x7x_invoke_change(indio_dev);
1177 
1178  return IIO_VAL_INT_PLUS_MICRO;
1179 }
1180 
1181 static ssize_t tsl2x7x_do_calibrate(struct device *dev,
1182  struct device_attribute *attr, const char *buf, size_t len)
1183 {
1184  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1185  bool value;
1186 
1187  if (strtobool(buf, &value))
1188  return -EINVAL;
1189 
1190  if (value)
1191  tsl2x7x_als_calibrate(indio_dev);
1192 
1193  tsl2x7x_invoke_change(indio_dev);
1194 
1195  return len;
1196 }
1197 
1198 static ssize_t tsl2x7x_luxtable_show(struct device *dev,
1199  struct device_attribute *attr, char *buf)
1200 {
1201  struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
1202  int i = 0;
1203  int offset = 0;
1204 
1205  while (i < (TSL2X7X_MAX_LUX_TABLE_SIZE * 3)) {
1206  offset += snprintf(buf + offset, PAGE_SIZE, "%d,%d,%d,",
1207  chip->tsl2x7x_device_lux[i].ratio,
1208  chip->tsl2x7x_device_lux[i].ch0,
1209  chip->tsl2x7x_device_lux[i].ch1);
1210  if (chip->tsl2x7x_device_lux[i].ratio == 0) {
1211  /* We just printed the first "0" entry.
1212  * Now get rid of the extra "," and break. */
1213  offset--;
1214  break;
1215  }
1216  i++;
1217  }
1218 
1219  offset += snprintf(buf + offset, PAGE_SIZE, "\n");
1220  return offset;
1221 }
1222 
1223 static ssize_t tsl2x7x_luxtable_store(struct device *dev,
1224  struct device_attribute *attr, const char *buf, size_t len)
1225 {
1226  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1227  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1228  int value[ARRAY_SIZE(chip->tsl2x7x_device_lux)*3 + 1];
1229  int n;
1230 
1231  get_options(buf, ARRAY_SIZE(value), value);
1232 
1233  /* We now have an array of ints starting at value[1], and
1234  * enumerated by value[0].
1235  * We expect each group of three ints is one table entry,
1236  * and the last table entry is all 0.
1237  */
1238  n = value[0];
1239  if ((n % 3) || n < 6 ||
1240  n > ((ARRAY_SIZE(chip->tsl2x7x_device_lux) - 1) * 3)) {
1241  dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
1242  return -EINVAL;
1243  }
1244 
1245  if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
1246  dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
1247  return -EINVAL;
1248  }
1249 
1251  tsl2x7x_chip_off(indio_dev);
1252 
1253  /* Zero out the table */
1254  memset(chip->tsl2x7x_device_lux, 0, sizeof(chip->tsl2x7x_device_lux));
1255  memcpy(chip->tsl2x7x_device_lux, &value[1], (value[0] * 4));
1256 
1257  tsl2x7x_invoke_change(indio_dev);
1258 
1259  return len;
1260 }
1261 
1262 static ssize_t tsl2x7x_do_prox_calibrate(struct device *dev,
1263  struct device_attribute *attr, const char *buf, size_t len)
1264 {
1265  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1266  bool value;
1267 
1268  if (strtobool(buf, &value))
1269  return -EINVAL;
1270 
1271  if (value)
1272  tsl2x7x_prox_cal(indio_dev);
1273 
1274  tsl2x7x_invoke_change(indio_dev);
1275 
1276  return len;
1277 }
1278 
1279 static int tsl2x7x_read_interrupt_config(struct iio_dev *indio_dev,
1280  u64 event_code)
1281 {
1282  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1283  int ret;
1284 
1286  ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x10);
1287  else
1288  ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x20);
1289 
1290  return ret;
1291 }
1292 
1293 static int tsl2x7x_write_interrupt_config(struct iio_dev *indio_dev,
1294  u64 event_code,
1295  int val)
1296 {
1297  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1298 
1299  if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1300  if (val)
1301  chip->tsl2x7x_settings.interrupts_en |= 0x10;
1302  else
1303  chip->tsl2x7x_settings.interrupts_en &= 0x20;
1304  } else {
1305  if (val)
1306  chip->tsl2x7x_settings.interrupts_en |= 0x20;
1307  else
1308  chip->tsl2x7x_settings.interrupts_en &= 0x10;
1309  }
1310 
1311  tsl2x7x_invoke_change(indio_dev);
1312 
1313  return 0;
1314 }
1315 
1316 static int tsl2x7x_write_thresh(struct iio_dev *indio_dev,
1317  u64 event_code,
1318  int val)
1319 {
1320  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1321 
1322  if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1323  switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1324  case IIO_EV_DIR_RISING:
1325  chip->tsl2x7x_settings.als_thresh_high = val;
1326  break;
1327  case IIO_EV_DIR_FALLING:
1328  chip->tsl2x7x_settings.als_thresh_low = val;
1329  break;
1330  default:
1331  return -EINVAL;
1332  }
1333  } else {
1334  switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1335  case IIO_EV_DIR_RISING:
1336  chip->tsl2x7x_settings.prox_thres_high = val;
1337  break;
1338  case IIO_EV_DIR_FALLING:
1339  chip->tsl2x7x_settings.prox_thres_low = val;
1340  break;
1341  default:
1342  return -EINVAL;
1343  }
1344  }
1345 
1346  tsl2x7x_invoke_change(indio_dev);
1347 
1348  return 0;
1349 }
1350 
1351 static int tsl2x7x_read_thresh(struct iio_dev *indio_dev,
1352  u64 event_code,
1353  int *val)
1354 {
1355  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1356 
1357  if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1358  switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1359  case IIO_EV_DIR_RISING:
1360  *val = chip->tsl2x7x_settings.als_thresh_high;
1361  break;
1362  case IIO_EV_DIR_FALLING:
1363  *val = chip->tsl2x7x_settings.als_thresh_low;
1364  break;
1365  default:
1366  return -EINVAL;
1367  }
1368  } else {
1369  switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1370  case IIO_EV_DIR_RISING:
1371  *val = chip->tsl2x7x_settings.prox_thres_high;
1372  break;
1373  case IIO_EV_DIR_FALLING:
1374  *val = chip->tsl2x7x_settings.prox_thres_low;
1375  break;
1376  default:
1377  return -EINVAL;
1378  }
1379  }
1380 
1381  return 0;
1382 }
1383 
1384 static int tsl2x7x_read_raw(struct iio_dev *indio_dev,
1385  struct iio_chan_spec const *chan,
1386  int *val,
1387  int *val2,
1388  long mask)
1389 {
1390  int ret = -EINVAL;
1391  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1392 
1393  switch (mask) {
1395  switch (chan->type) {
1396  case IIO_LIGHT:
1397  tsl2x7x_get_lux(indio_dev);
1398  *val = chip->als_cur_info.lux;
1399  ret = IIO_VAL_INT;
1400  break;
1401  default:
1402  return -EINVAL;
1403  break;
1404  }
1405  break;
1406  case IIO_CHAN_INFO_RAW:
1407  switch (chan->type) {
1408  case IIO_INTENSITY:
1409  tsl2x7x_get_lux(indio_dev);
1410  if (chan->channel == 0)
1411  *val = chip->als_cur_info.als_ch0;
1412  else
1413  *val = chip->als_cur_info.als_ch1;
1414  ret = IIO_VAL_INT;
1415  break;
1416  case IIO_PROXIMITY:
1417  tsl2x7x_get_prox(indio_dev);
1418  *val = chip->prox_data;
1419  ret = IIO_VAL_INT;
1420  break;
1421  default:
1422  return -EINVAL;
1423  break;
1424  }
1425  break;
1427  if (chan->type == IIO_LIGHT)
1428  *val =
1429  tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain];
1430  else
1431  *val =
1432  tsl2X7X_prx_gainadj[chip->tsl2x7x_settings.prox_gain];
1433  ret = IIO_VAL_INT;
1434  break;
1436  *val = chip->tsl2x7x_settings.als_gain_trim;
1437  ret = IIO_VAL_INT;
1438  break;
1439 
1440  default:
1441  ret = -EINVAL;
1442  }
1443 
1444  return ret;
1445 }
1446 
1447 static int tsl2x7x_write_raw(struct iio_dev *indio_dev,
1448  struct iio_chan_spec const *chan,
1449  int val,
1450  int val2,
1451  long mask)
1452 {
1453  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1454 
1455  switch (mask) {
1457  if (chan->type == IIO_INTENSITY) {
1458  switch (val) {
1459  case 1:
1460  chip->tsl2x7x_settings.als_gain = 0;
1461  break;
1462  case 8:
1463  chip->tsl2x7x_settings.als_gain = 1;
1464  break;
1465  case 16:
1466  chip->tsl2x7x_settings.als_gain = 2;
1467  break;
1468  case 120:
1469  switch (chip->id) {
1470  case tsl2572:
1471  case tsl2672:
1472  case tmd2672:
1473  case tsl2772:
1474  case tmd2772:
1475  return -EINVAL;
1476  break;
1477  }
1478  chip->tsl2x7x_settings.als_gain = 3;
1479  break;
1480  case 128:
1481  switch (chip->id) {
1482  case tsl2571:
1483  case tsl2671:
1484  case tmd2671:
1485  case tsl2771:
1486  case tmd2771:
1487  return -EINVAL;
1488  break;
1489  }
1490  chip->tsl2x7x_settings.als_gain = 3;
1491  break;
1492  default:
1493  return -EINVAL;
1494  }
1495  } else {
1496  switch (val) {
1497  case 1:
1498  chip->tsl2x7x_settings.prox_gain = 0;
1499  break;
1500  case 2:
1501  chip->tsl2x7x_settings.prox_gain = 1;
1502  break;
1503  case 4:
1504  chip->tsl2x7x_settings.prox_gain = 2;
1505  break;
1506  case 8:
1507  chip->tsl2x7x_settings.prox_gain = 3;
1508  break;
1509  default:
1510  return -EINVAL;
1511  }
1512  }
1513  break;
1515  chip->tsl2x7x_settings.als_gain_trim = val;
1516  break;
1517 
1518  default:
1519  return -EINVAL;
1520  }
1521 
1522  tsl2x7x_invoke_change(indio_dev);
1523 
1524  return 0;
1525 }
1526 
1528  tsl2x7x_power_state_show, tsl2x7x_power_state_store);
1529 
1530 static DEVICE_ATTR(in_proximity0_calibscale_available, S_IRUGO,
1531  tsl2x7x_prox_gain_available_show, NULL);
1532 
1533 static DEVICE_ATTR(in_illuminance0_calibscale_available, S_IRUGO,
1534  tsl2x7x_gain_available_show, NULL);
1535 
1536 static DEVICE_ATTR(in_illuminance0_integration_time, S_IRUGO | S_IWUSR,
1537  tsl2x7x_als_time_show, tsl2x7x_als_time_store);
1538 
1539 static DEVICE_ATTR(in_illuminance0_target_input, S_IRUGO | S_IWUSR,
1540  tsl2x7x_als_cal_target_show, tsl2x7x_als_cal_target_store);
1541 
1542 static DEVICE_ATTR(in_illuminance0_calibrate, S_IWUSR, NULL,
1543  tsl2x7x_do_calibrate);
1544 
1545 static DEVICE_ATTR(in_proximity0_calibrate, S_IWUSR, NULL,
1546  tsl2x7x_do_prox_calibrate);
1547 
1548 static DEVICE_ATTR(in_illuminance0_lux_table, S_IRUGO | S_IWUSR,
1549  tsl2x7x_luxtable_show, tsl2x7x_luxtable_store);
1550 
1551 static DEVICE_ATTR(in_intensity0_thresh_period, S_IRUGO | S_IWUSR,
1552  tsl2x7x_als_persistence_show, tsl2x7x_als_persistence_store);
1553 
1554 static DEVICE_ATTR(in_proximity0_thresh_period, S_IRUGO | S_IWUSR,
1555  tsl2x7x_prox_persistence_show, tsl2x7x_prox_persistence_store);
1556 
1557 /* Use the default register values to identify the Taos device */
1558 static int tsl2x7x_device_id(unsigned char *id, int target)
1559 {
1560  switch (target) {
1561  case tsl2571:
1562  case tsl2671:
1563  case tsl2771:
1564  return ((*id & 0xf0) == TRITON_ID);
1565  break;
1566  case tmd2671:
1567  case tmd2771:
1568  return ((*id & 0xf0) == HALIBUT_ID);
1569  break;
1570  case tsl2572:
1571  case tsl2672:
1572  case tmd2672:
1573  case tsl2772:
1574  case tmd2772:
1575  return ((*id & 0xf0) == SWORDFISH_ID);
1576  break;
1577  }
1578 
1579  return -EINVAL;
1580 }
1581 
1582 static irqreturn_t tsl2x7x_event_handler(int irq, void *private)
1583 {
1584  struct iio_dev *indio_dev = private;
1585  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1586  s64 timestamp = iio_get_time_ns();
1587  int ret;
1588  u8 value;
1589 
1590  value = i2c_smbus_read_byte_data(chip->client,
1592 
1593  /* What type of interrupt do we need to process */
1594  if (value & TSL2X7X_STA_PRX_INTR) {
1595  tsl2x7x_get_prox(indio_dev); /* freshen data for ABI */
1596  iio_push_event(indio_dev,
1598  0,
1601  timestamp);
1602  }
1603 
1604  if (value & TSL2X7X_STA_ALS_INTR) {
1605  tsl2x7x_get_lux(indio_dev); /* freshen data for ABI */
1606  iio_push_event(indio_dev,
1608  0,
1611  timestamp);
1612  }
1613  /* Clear interrupt now that we have handled it. */
1614  ret = i2c_smbus_write_byte(chip->client,
1617  if (ret < 0)
1618  dev_err(&chip->client->dev,
1619  "%s: Failed to clear irq from event handler. err = %d\n",
1620  __func__, ret);
1621 
1622  return IRQ_HANDLED;
1623 }
1624 
1625 static struct attribute *tsl2x7x_ALS_device_attrs[] = {
1626  &dev_attr_power_state.attr,
1627  &dev_attr_in_illuminance0_calibscale_available.attr,
1628  &dev_attr_in_illuminance0_integration_time.attr,
1629  &iio_const_attr_in_illuminance0_integration_time_available\
1630  .dev_attr.attr,
1631  &dev_attr_in_illuminance0_target_input.attr,
1632  &dev_attr_in_illuminance0_calibrate.attr,
1633  &dev_attr_in_illuminance0_lux_table.attr,
1634  NULL
1635 };
1636 
1637 static struct attribute *tsl2x7x_PRX_device_attrs[] = {
1638  &dev_attr_power_state.attr,
1639  &dev_attr_in_proximity0_calibrate.attr,
1640  NULL
1641 };
1642 
1643 static struct attribute *tsl2x7x_ALSPRX_device_attrs[] = {
1644  &dev_attr_power_state.attr,
1645  &dev_attr_in_illuminance0_calibscale_available.attr,
1646  &dev_attr_in_illuminance0_integration_time.attr,
1647  &iio_const_attr_in_illuminance0_integration_time_available\
1648  .dev_attr.attr,
1649  &dev_attr_in_illuminance0_target_input.attr,
1650  &dev_attr_in_illuminance0_calibrate.attr,
1651  &dev_attr_in_illuminance0_lux_table.attr,
1652  &dev_attr_in_proximity0_calibrate.attr,
1653  NULL
1654 };
1655 
1656 static struct attribute *tsl2x7x_PRX2_device_attrs[] = {
1657  &dev_attr_power_state.attr,
1658  &dev_attr_in_proximity0_calibrate.attr,
1659  &dev_attr_in_proximity0_calibscale_available.attr,
1660  NULL
1661 };
1662 
1663 static struct attribute *tsl2x7x_ALSPRX2_device_attrs[] = {
1664  &dev_attr_power_state.attr,
1665  &dev_attr_in_illuminance0_calibscale_available.attr,
1666  &dev_attr_in_illuminance0_integration_time.attr,
1667  &iio_const_attr_in_illuminance0_integration_time_available\
1668  .dev_attr.attr,
1669  &dev_attr_in_illuminance0_target_input.attr,
1670  &dev_attr_in_illuminance0_calibrate.attr,
1671  &dev_attr_in_illuminance0_lux_table.attr,
1672  &dev_attr_in_proximity0_calibrate.attr,
1673  &dev_attr_in_proximity0_calibscale_available.attr,
1674  NULL
1675 };
1676 
1677 static struct attribute *tsl2X7X_ALS_event_attrs[] = {
1678  &dev_attr_in_intensity0_thresh_period.attr,
1679  NULL,
1680 };
1681 static struct attribute *tsl2X7X_PRX_event_attrs[] = {
1682  &dev_attr_in_proximity0_thresh_period.attr,
1683  NULL,
1684 };
1685 
1686 static struct attribute *tsl2X7X_ALSPRX_event_attrs[] = {
1687  &dev_attr_in_intensity0_thresh_period.attr,
1688  &dev_attr_in_proximity0_thresh_period.attr,
1689  NULL,
1690 };
1691 
1692 static const struct attribute_group tsl2X7X_device_attr_group_tbl[] = {
1693  [ALS] = {
1694  .attrs = tsl2x7x_ALS_device_attrs,
1695  },
1696  [PRX] = {
1697  .attrs = tsl2x7x_PRX_device_attrs,
1698  },
1699  [ALSPRX] = {
1700  .attrs = tsl2x7x_ALSPRX_device_attrs,
1701  },
1702  [PRX2] = {
1703  .attrs = tsl2x7x_PRX2_device_attrs,
1704  },
1705  [ALSPRX2] = {
1706  .attrs = tsl2x7x_ALSPRX2_device_attrs,
1707  },
1708 };
1709 
1710 static struct attribute_group tsl2X7X_event_attr_group_tbl[] = {
1711  [ALS] = {
1712  .attrs = tsl2X7X_ALS_event_attrs,
1713  .name = "events",
1714  },
1715  [PRX] = {
1716  .attrs = tsl2X7X_PRX_event_attrs,
1717  .name = "events",
1718  },
1719  [ALSPRX] = {
1720  .attrs = tsl2X7X_ALSPRX_event_attrs,
1721  .name = "events",
1722  },
1723 };
1724 
1725 static const struct iio_info tsl2X7X_device_info[] = {
1726  [ALS] = {
1727  .attrs = &tsl2X7X_device_attr_group_tbl[ALS],
1728  .event_attrs = &tsl2X7X_event_attr_group_tbl[ALS],
1729  .driver_module = THIS_MODULE,
1730  .read_raw = &tsl2x7x_read_raw,
1731  .write_raw = &tsl2x7x_write_raw,
1732  .read_event_value = &tsl2x7x_read_thresh,
1733  .write_event_value = &tsl2x7x_write_thresh,
1734  .read_event_config = &tsl2x7x_read_interrupt_config,
1735  .write_event_config = &tsl2x7x_write_interrupt_config,
1736  },
1737  [PRX] = {
1738  .attrs = &tsl2X7X_device_attr_group_tbl[PRX],
1739  .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
1740  .driver_module = THIS_MODULE,
1741  .read_raw = &tsl2x7x_read_raw,
1742  .write_raw = &tsl2x7x_write_raw,
1743  .read_event_value = &tsl2x7x_read_thresh,
1744  .write_event_value = &tsl2x7x_write_thresh,
1745  .read_event_config = &tsl2x7x_read_interrupt_config,
1746  .write_event_config = &tsl2x7x_write_interrupt_config,
1747  },
1748  [ALSPRX] = {
1749  .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX],
1750  .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
1751  .driver_module = THIS_MODULE,
1752  .read_raw = &tsl2x7x_read_raw,
1753  .write_raw = &tsl2x7x_write_raw,
1754  .read_event_value = &tsl2x7x_read_thresh,
1755  .write_event_value = &tsl2x7x_write_thresh,
1756  .read_event_config = &tsl2x7x_read_interrupt_config,
1757  .write_event_config = &tsl2x7x_write_interrupt_config,
1758  },
1759  [PRX2] = {
1760  .attrs = &tsl2X7X_device_attr_group_tbl[PRX2],
1761  .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
1762  .driver_module = THIS_MODULE,
1763  .read_raw = &tsl2x7x_read_raw,
1764  .write_raw = &tsl2x7x_write_raw,
1765  .read_event_value = &tsl2x7x_read_thresh,
1766  .write_event_value = &tsl2x7x_write_thresh,
1767  .read_event_config = &tsl2x7x_read_interrupt_config,
1768  .write_event_config = &tsl2x7x_write_interrupt_config,
1769  },
1770  [ALSPRX2] = {
1771  .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX2],
1772  .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
1773  .driver_module = THIS_MODULE,
1774  .read_raw = &tsl2x7x_read_raw,
1775  .write_raw = &tsl2x7x_write_raw,
1776  .read_event_value = &tsl2x7x_read_thresh,
1777  .write_event_value = &tsl2x7x_write_thresh,
1778  .read_event_config = &tsl2x7x_read_interrupt_config,
1779  .write_event_config = &tsl2x7x_write_interrupt_config,
1780  },
1781 };
1782 
1783 static const struct tsl2x7x_chip_info tsl2x7x_chip_info_tbl[] = {
1784  [ALS] = {
1785  .channel = {
1786  {
1787  .type = IIO_LIGHT,
1788  .indexed = 1,
1789  .channel = 0,
1791  }, {
1792  .type = IIO_INTENSITY,
1793  .indexed = 1,
1794  .channel = 0,
1795  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1798  .event_mask = TSL2X7X_EVENT_MASK
1799  }, {
1800  .type = IIO_INTENSITY,
1801  .indexed = 1,
1802  .channel = 1,
1803  },
1804  },
1805  .chan_table_elements = 3,
1806  .info = &tsl2X7X_device_info[ALS],
1807  },
1808  [PRX] = {
1809  .channel = {
1810  {
1811  .type = IIO_PROXIMITY,
1812  .indexed = 1,
1813  .channel = 0,
1814  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1815  .event_mask = TSL2X7X_EVENT_MASK
1816  },
1817  },
1818  .chan_table_elements = 1,
1819  .info = &tsl2X7X_device_info[PRX],
1820  },
1821  [ALSPRX] = {
1822  .channel = {
1823  {
1824  .type = IIO_LIGHT,
1825  .indexed = 1,
1826  .channel = 0,
1828  }, {
1829  .type = IIO_INTENSITY,
1830  .indexed = 1,
1831  .channel = 0,
1832  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1835  .event_mask = TSL2X7X_EVENT_MASK
1836  }, {
1837  .type = IIO_INTENSITY,
1838  .indexed = 1,
1839  .channel = 1,
1840  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1841  }, {
1842  .type = IIO_PROXIMITY,
1843  .indexed = 1,
1844  .channel = 0,
1845  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1846  .event_mask = TSL2X7X_EVENT_MASK
1847  },
1848  },
1849  .chan_table_elements = 4,
1850  .info = &tsl2X7X_device_info[ALSPRX],
1851  },
1852  [PRX2] = {
1853  .channel = {
1854  {
1855  .type = IIO_PROXIMITY,
1856  .indexed = 1,
1857  .channel = 0,
1858  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1860  .event_mask = TSL2X7X_EVENT_MASK
1861  },
1862  },
1863  .chan_table_elements = 1,
1864  .info = &tsl2X7X_device_info[PRX2],
1865  },
1866  [ALSPRX2] = {
1867  .channel = {
1868  {
1869  .type = IIO_LIGHT,
1870  .indexed = 1,
1871  .channel = 0,
1873  }, {
1874  .type = IIO_INTENSITY,
1875  .indexed = 1,
1876  .channel = 0,
1877  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1880  .event_mask = TSL2X7X_EVENT_MASK
1881  }, {
1882  .type = IIO_INTENSITY,
1883  .indexed = 1,
1884  .channel = 1,
1885  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1886  }, {
1887  .type = IIO_PROXIMITY,
1888  .indexed = 1,
1889  .channel = 0,
1890  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1892  .event_mask = TSL2X7X_EVENT_MASK
1893  },
1894  },
1895  .chan_table_elements = 4,
1896  .info = &tsl2X7X_device_info[ALSPRX2],
1897  },
1898 };
1899 
1900 static int __devinit tsl2x7x_probe(struct i2c_client *clientp,
1901  const struct i2c_device_id *id)
1902 {
1903  int ret;
1904  unsigned char device_id;
1905  struct iio_dev *indio_dev;
1906  struct tsl2X7X_chip *chip;
1907 
1908  indio_dev = iio_device_alloc(sizeof(*chip));
1909  if (!indio_dev)
1910  return -ENOMEM;
1911 
1912  chip = iio_priv(indio_dev);
1913  chip->client = clientp;
1914  i2c_set_clientdata(clientp, indio_dev);
1915 
1916  ret = tsl2x7x_i2c_read(chip->client,
1917  TSL2X7X_CHIPID, &device_id);
1918  if (ret < 0)
1919  goto fail1;
1920 
1921  if ((!tsl2x7x_device_id(&device_id, id->driver_data)) ||
1922  (tsl2x7x_device_id(&device_id, id->driver_data) == -EINVAL)) {
1923  dev_info(&chip->client->dev,
1924  "%s: i2c device found does not match expected id\n",
1925  __func__);
1926  goto fail1;
1927  }
1928 
1930  if (ret < 0) {
1931  dev_err(&clientp->dev, "%s: write to cmd reg failed. err = %d\n",
1932  __func__, ret);
1933  goto fail1;
1934  }
1935 
1936  /* ALS and PROX functions can be invoked via user space poll
1937  * or H/W interrupt. If busy return last sample. */
1938  mutex_init(&chip->als_mutex);
1939  mutex_init(&chip->prox_mutex);
1940 
1942  chip->pdata = clientp->dev.platform_data;
1943  chip->id = id->driver_data;
1944  chip->chip_info =
1945  &tsl2x7x_chip_info_tbl[device_channel_config[id->driver_data]];
1946 
1947  indio_dev->info = chip->chip_info->info;
1948  indio_dev->dev.parent = &clientp->dev;
1949  indio_dev->modes = INDIO_DIRECT_MODE;
1950  indio_dev->name = chip->client->name;
1951  indio_dev->channels = chip->chip_info->channel;
1952  indio_dev->num_channels = chip->chip_info->chan_table_elements;
1953 
1954  if (clientp->irq) {
1955  ret = request_threaded_irq(clientp->irq,
1956  NULL,
1957  &tsl2x7x_event_handler,
1959  "TSL2X7X_event",
1960  indio_dev);
1961  if (ret) {
1962  dev_err(&clientp->dev,
1963  "%s: irq request failed", __func__);
1964  goto fail2;
1965  }
1966  }
1967 
1968  /* Load up the defaults */
1969  tsl2x7x_defaults(chip);
1970  /* Make sure the chip is on */
1971  tsl2x7x_chip_on(indio_dev);
1972 
1973  ret = iio_device_register(indio_dev);
1974  if (ret) {
1975  dev_err(&clientp->dev,
1976  "%s: iio registration failed\n", __func__);
1977  goto fail1;
1978  }
1979 
1980  dev_info(&clientp->dev, "%s Light sensor found.\n", id->name);
1981 
1982  return 0;
1983 
1984 fail1:
1985  if (clientp->irq)
1986  free_irq(clientp->irq, indio_dev);
1987 fail2:
1988  iio_device_free(indio_dev);
1989 
1990  return ret;
1991 }
1992 
1993 static int tsl2x7x_suspend(struct device *dev)
1994 {
1995  struct iio_dev *indio_dev = dev_get_drvdata(dev);
1996  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1997  int ret = 0;
1998 
2000  ret = tsl2x7x_chip_off(indio_dev);
2002  }
2003 
2004  if (chip->pdata && chip->pdata->platform_power) {
2006  chip->pdata->platform_power(dev, pmm);
2007  }
2008 
2009  return ret;
2010 }
2011 
2012 static int tsl2x7x_resume(struct device *dev)
2013 {
2014  struct iio_dev *indio_dev = dev_get_drvdata(dev);
2015  struct tsl2X7X_chip *chip = iio_priv(indio_dev);
2016  int ret = 0;
2017 
2018  if (chip->pdata && chip->pdata->platform_power) {
2019  pm_message_t pmm = {PM_EVENT_RESUME};
2020  chip->pdata->platform_power(dev, pmm);
2021  }
2022 
2024  ret = tsl2x7x_chip_on(indio_dev);
2025 
2026  return ret;
2027 }
2028 
2029 static int __devexit tsl2x7x_remove(struct i2c_client *client)
2030 {
2031  struct iio_dev *indio_dev = i2c_get_clientdata(client);
2032 
2033  tsl2x7x_chip_off(indio_dev);
2034 
2035  iio_device_unregister(indio_dev);
2036  if (client->irq)
2037  free_irq(client->irq, indio_dev);
2038 
2039  iio_device_free(indio_dev);
2040 
2041  return 0;
2042 }
2043 
2044 static struct i2c_device_id tsl2x7x_idtable[] = {
2045  { "tsl2571", tsl2571 },
2046  { "tsl2671", tsl2671 },
2047  { "tmd2671", tmd2671 },
2048  { "tsl2771", tsl2771 },
2049  { "tmd2771", tmd2771 },
2050  { "tsl2572", tsl2572 },
2051  { "tsl2672", tsl2672 },
2052  { "tmd2672", tmd2672 },
2053  { "tsl2772", tsl2772 },
2054  { "tmd2772", tmd2772 },
2055  {}
2056 };
2057 
2058 MODULE_DEVICE_TABLE(i2c, tsl2x7x_idtable);
2059 
2060 static const struct dev_pm_ops tsl2x7x_pm_ops = {
2061  .suspend = tsl2x7x_suspend,
2062  .resume = tsl2x7x_resume,
2063 };
2064 
2065 /* Driver definition */
2066 static struct i2c_driver tsl2x7x_driver = {
2067  .driver = {
2068  .name = "tsl2x7x",
2069  .pm = &tsl2x7x_pm_ops,
2070  },
2071  .id_table = tsl2x7x_idtable,
2072  .probe = tsl2x7x_probe,
2073  .remove = __devexit_p(tsl2x7x_remove),
2074 };
2075 
2076 module_i2c_driver(tsl2x7x_driver);
2077 
2078 MODULE_AUTHOR("J. August Brenner<[email protected]>");
2079 MODULE_DESCRIPTION("TAOS tsl2x7x ambient and proximity light sensor driver");
2080 MODULE_LICENSE("GPL");