0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/delay.h>
0012 #include <linux/errno.h>
0013 #include <linux/i2c.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/property.h>
0019 #include <linux/slab.h>
0020
0021 #include <linux/iio/events.h>
0022 #include <linux/iio/iio.h>
0023 #include <linux/iio/sysfs.h>
0024 #include <linux/platform_data/tsl2772.h>
0025 #include <linux/regulator/consumer.h>
0026
0027
0028 #define PROX_STAT_CAL 0
0029 #define PROX_STAT_SAMP 1
0030 #define MAX_SAMPLES_CAL 200
0031
0032
0033 #define TRITON_ID 0x00
0034 #define SWORDFISH_ID 0x30
0035 #define HALIBUT_ID 0x20
0036
0037
0038 #define TSL2772_LUX_CALC_OVER_FLOW 65535
0039
0040
0041
0042
0043
0044
0045
0046 #define TSL2772_MAX_CONFIG_REG 16
0047
0048
0049 #define TSL2772_CNTRL 0x00
0050 #define TSL2772_ALS_TIME 0X01
0051 #define TSL2772_PRX_TIME 0x02
0052 #define TSL2772_WAIT_TIME 0x03
0053 #define TSL2772_ALS_MINTHRESHLO 0X04
0054 #define TSL2772_ALS_MINTHRESHHI 0X05
0055 #define TSL2772_ALS_MAXTHRESHLO 0X06
0056 #define TSL2772_ALS_MAXTHRESHHI 0X07
0057 #define TSL2772_PRX_MINTHRESHLO 0X08
0058 #define TSL2772_PRX_MINTHRESHHI 0X09
0059 #define TSL2772_PRX_MAXTHRESHLO 0X0A
0060 #define TSL2772_PRX_MAXTHRESHHI 0X0B
0061 #define TSL2772_PERSISTENCE 0x0C
0062 #define TSL2772_ALS_PRX_CONFIG 0x0D
0063 #define TSL2772_PRX_COUNT 0x0E
0064 #define TSL2772_GAIN 0x0F
0065 #define TSL2772_NOTUSED 0x10
0066 #define TSL2772_REVID 0x11
0067 #define TSL2772_CHIPID 0x12
0068 #define TSL2772_STATUS 0x13
0069 #define TSL2772_ALS_CHAN0LO 0x14
0070 #define TSL2772_ALS_CHAN0HI 0x15
0071 #define TSL2772_ALS_CHAN1LO 0x16
0072 #define TSL2772_ALS_CHAN1HI 0x17
0073 #define TSL2772_PRX_LO 0x18
0074 #define TSL2772_PRX_HI 0x19
0075
0076
0077 #define TSL2772_CMD_REG 0x80
0078 #define TSL2772_CMD_SPL_FN 0x60
0079 #define TSL2772_CMD_REPEAT_PROTO 0x00
0080 #define TSL2772_CMD_AUTOINC_PROTO 0x20
0081
0082 #define TSL2772_CMD_PROX_INT_CLR 0X05
0083 #define TSL2772_CMD_ALS_INT_CLR 0x06
0084 #define TSL2772_CMD_PROXALS_INT_CLR 0X07
0085
0086
0087 #define TSL2772_CNTL_ADC_ENBL 0x02
0088 #define TSL2772_CNTL_PWR_ON 0x01
0089
0090
0091 #define TSL2772_STA_ADC_VALID 0x01
0092 #define TSL2772_STA_PRX_VALID 0x02
0093 #define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \
0094 TSL2772_STA_PRX_VALID)
0095 #define TSL2772_STA_ALS_INTR 0x10
0096 #define TSL2772_STA_PRX_INTR 0x20
0097
0098
0099 #define TSL2772_CNTL_REG_CLEAR 0x00
0100 #define TSL2772_CNTL_PROX_INT_ENBL 0X20
0101 #define TSL2772_CNTL_ALS_INT_ENBL 0X10
0102 #define TSL2772_CNTL_WAIT_TMR_ENBL 0X08
0103 #define TSL2772_CNTL_PROX_DET_ENBL 0X04
0104 #define TSL2772_CNTL_PWRON 0x01
0105 #define TSL2772_CNTL_ALSPON_ENBL 0x03
0106 #define TSL2772_CNTL_INTALSPON_ENBL 0x13
0107 #define TSL2772_CNTL_PROXPON_ENBL 0x0F
0108 #define TSL2772_CNTL_INTPROXPON_ENBL 0x2F
0109
0110 #define TSL2772_ALS_GAIN_TRIM_MIN 250
0111 #define TSL2772_ALS_GAIN_TRIM_MAX 4000
0112
0113 #define TSL2772_MAX_PROX_LEDS 2
0114
0115 #define TSL2772_BOOT_MIN_SLEEP_TIME 10000
0116 #define TSL2772_BOOT_MAX_SLEEP_TIME 28000
0117
0118
0119 enum {
0120 tsl2571,
0121 tsl2671,
0122 tmd2671,
0123 tsl2771,
0124 tmd2771,
0125 tsl2572,
0126 tsl2672,
0127 tmd2672,
0128 tsl2772,
0129 tmd2772,
0130 apds9930,
0131 };
0132
0133 enum {
0134 TSL2772_CHIP_UNKNOWN = 0,
0135 TSL2772_CHIP_WORKING = 1,
0136 TSL2772_CHIP_SUSPENDED = 2
0137 };
0138
0139 enum {
0140 TSL2772_SUPPLY_VDD = 0,
0141 TSL2772_SUPPLY_VDDIO = 1,
0142 TSL2772_NUM_SUPPLIES = 2
0143 };
0144
0145
0146 struct tsl2772_als_info {
0147 u16 als_ch0;
0148 u16 als_ch1;
0149 u16 lux;
0150 };
0151
0152 struct tsl2772_chip_info {
0153 int chan_table_elements;
0154 struct iio_chan_spec channel_with_events[4];
0155 struct iio_chan_spec channel_without_events[4];
0156 const struct iio_info *info;
0157 };
0158
0159 static const int tsl2772_led_currents[][2] = {
0160 { 100000, TSL2772_100_mA },
0161 { 50000, TSL2772_50_mA },
0162 { 25000, TSL2772_25_mA },
0163 { 13000, TSL2772_13_mA },
0164 { 0, 0 }
0165 };
0166
0167 struct tsl2772_chip {
0168 kernel_ulong_t id;
0169 struct mutex prox_mutex;
0170 struct mutex als_mutex;
0171 struct i2c_client *client;
0172 struct regulator_bulk_data supplies[TSL2772_NUM_SUPPLIES];
0173 u16 prox_data;
0174 struct tsl2772_als_info als_cur_info;
0175 struct tsl2772_settings settings;
0176 struct tsl2772_platform_data *pdata;
0177 int als_gain_time_scale;
0178 int als_saturation;
0179 int tsl2772_chip_status;
0180 u8 tsl2772_config[TSL2772_MAX_CONFIG_REG];
0181 const struct tsl2772_chip_info *chip_info;
0182 const struct iio_info *info;
0183 s64 event_timestamp;
0184
0185
0186
0187
0188
0189 struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
0190 };
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200 static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
0201 { 53000, 106000 },
0202 { 31800, 53000 },
0203 { 0, 0 },
0204 };
0205
0206 static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
0207 { 24000, 48000 },
0208 { 14400, 24000 },
0209 { 0, 0 },
0210 };
0211
0212 static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
0213 { 60000, 112200 },
0214 { 37800, 60000 },
0215 { 0, 0 },
0216 };
0217
0218 static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
0219 { 20000, 35000 },
0220 { 12600, 20000 },
0221 { 0, 0 },
0222 };
0223
0224 static const struct tsl2772_lux apds9930_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
0225 { 52000, 96824 },
0226 { 38792, 67132 },
0227 { 0, 0 },
0228 };
0229
0230 static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = {
0231 [tsl2571] = tsl2x71_lux_table,
0232 [tsl2671] = tsl2x71_lux_table,
0233 [tmd2671] = tmd2x71_lux_table,
0234 [tsl2771] = tsl2x71_lux_table,
0235 [tmd2771] = tmd2x71_lux_table,
0236 [tsl2572] = tsl2x72_lux_table,
0237 [tsl2672] = tsl2x72_lux_table,
0238 [tmd2672] = tmd2x72_lux_table,
0239 [tsl2772] = tsl2x72_lux_table,
0240 [tmd2772] = tmd2x72_lux_table,
0241 [apds9930] = apds9930_lux_table,
0242 };
0243
0244 static const struct tsl2772_settings tsl2772_default_settings = {
0245 .als_time = 255,
0246 .als_gain = 0,
0247 .prox_time = 255,
0248 .prox_gain = 0,
0249 .wait_time = 255,
0250 .als_prox_config = 0,
0251 .als_gain_trim = 1000,
0252 .als_cal_target = 150,
0253 .als_persistence = 1,
0254 .als_interrupt_en = false,
0255 .als_thresh_low = 200,
0256 .als_thresh_high = 256,
0257 .prox_persistence = 1,
0258 .prox_interrupt_en = false,
0259 .prox_thres_low = 0,
0260 .prox_thres_high = 512,
0261 .prox_max_samples_cal = 30,
0262 .prox_pulse_count = 8,
0263 .prox_diode = TSL2772_DIODE1,
0264 .prox_power = TSL2772_100_mA
0265 };
0266
0267 static const s16 tsl2772_als_gain[] = {
0268 1,
0269 8,
0270 16,
0271 120
0272 };
0273
0274 static const s16 tsl2772_prox_gain[] = {
0275 1,
0276 2,
0277 4,
0278 8
0279 };
0280
0281 static const int tsl2772_int_time_avail[][6] = {
0282 [tsl2571] = { 0, 2720, 0, 2720, 0, 696000 },
0283 [tsl2671] = { 0, 2720, 0, 2720, 0, 696000 },
0284 [tmd2671] = { 0, 2720, 0, 2720, 0, 696000 },
0285 [tsl2771] = { 0, 2720, 0, 2720, 0, 696000 },
0286 [tmd2771] = { 0, 2720, 0, 2720, 0, 696000 },
0287 [tsl2572] = { 0, 2730, 0, 2730, 0, 699000 },
0288 [tsl2672] = { 0, 2730, 0, 2730, 0, 699000 },
0289 [tmd2672] = { 0, 2730, 0, 2730, 0, 699000 },
0290 [tsl2772] = { 0, 2730, 0, 2730, 0, 699000 },
0291 [tmd2772] = { 0, 2730, 0, 2730, 0, 699000 },
0292 [apds9930] = { 0, 2730, 0, 2730, 0, 699000 },
0293 };
0294
0295 static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
0296
0297 static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
0298
0299
0300 enum {
0301 ALS,
0302 PRX,
0303 ALSPRX,
0304 PRX2,
0305 ALSPRX2,
0306 };
0307
0308 static const u8 device_channel_config[] = {
0309 [tsl2571] = ALS,
0310 [tsl2671] = PRX,
0311 [tmd2671] = PRX,
0312 [tsl2771] = ALSPRX,
0313 [tmd2771] = ALSPRX,
0314 [tsl2572] = ALS,
0315 [tsl2672] = PRX2,
0316 [tmd2672] = PRX2,
0317 [tsl2772] = ALSPRX2,
0318 [tmd2772] = ALSPRX2,
0319 [apds9930] = ALSPRX2,
0320 };
0321
0322 static int tsl2772_read_status(struct tsl2772_chip *chip)
0323 {
0324 int ret;
0325
0326 ret = i2c_smbus_read_byte_data(chip->client,
0327 TSL2772_CMD_REG | TSL2772_STATUS);
0328 if (ret < 0)
0329 dev_err(&chip->client->dev,
0330 "%s: failed to read STATUS register: %d\n", __func__,
0331 ret);
0332
0333 return ret;
0334 }
0335
0336 static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data)
0337 {
0338 int ret;
0339
0340 ret = i2c_smbus_write_byte_data(chip->client,
0341 TSL2772_CMD_REG | TSL2772_CNTRL, data);
0342 if (ret < 0) {
0343 dev_err(&chip->client->dev,
0344 "%s: failed to write to control register %x: %d\n",
0345 __func__, data, ret);
0346 }
0347
0348 return ret;
0349 }
0350
0351 static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg,
0352 int upper_reg)
0353 {
0354 u8 buf[2];
0355 int ret;
0356
0357 ret = i2c_smbus_write_byte(chip->client,
0358 TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO |
0359 lower_reg);
0360 if (ret < 0) {
0361 dev_err(&chip->client->dev,
0362 "%s: failed to enable auto increment protocol: %d\n",
0363 __func__, ret);
0364 return ret;
0365 }
0366
0367 ret = i2c_smbus_read_byte_data(chip->client,
0368 TSL2772_CMD_REG | lower_reg);
0369 if (ret < 0) {
0370 dev_err(&chip->client->dev,
0371 "%s: failed to read from register %x: %d\n", __func__,
0372 lower_reg, ret);
0373 return ret;
0374 }
0375 buf[0] = ret;
0376
0377 ret = i2c_smbus_read_byte_data(chip->client,
0378 TSL2772_CMD_REG | upper_reg);
0379 if (ret < 0) {
0380 dev_err(&chip->client->dev,
0381 "%s: failed to read from register %x: %d\n", __func__,
0382 upper_reg, ret);
0383 return ret;
0384 }
0385 buf[1] = ret;
0386
0387 ret = i2c_smbus_write_byte(chip->client,
0388 TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO |
0389 lower_reg);
0390 if (ret < 0) {
0391 dev_err(&chip->client->dev,
0392 "%s: failed to enable repeated byte protocol: %d\n",
0393 __func__, ret);
0394 return ret;
0395 }
0396
0397 return le16_to_cpup((const __le16 *)&buf[0]);
0398 }
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411 static int tsl2772_get_lux(struct iio_dev *indio_dev)
0412 {
0413 struct tsl2772_chip *chip = iio_priv(indio_dev);
0414 struct tsl2772_lux *p;
0415 int max_lux, ret;
0416 bool overflow;
0417
0418 mutex_lock(&chip->als_mutex);
0419
0420 if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) {
0421 dev_err(&chip->client->dev, "%s: device is not enabled\n",
0422 __func__);
0423 ret = -EBUSY;
0424 goto out_unlock;
0425 }
0426
0427 ret = tsl2772_read_status(chip);
0428 if (ret < 0)
0429 goto out_unlock;
0430
0431 if (!(ret & TSL2772_STA_ADC_VALID)) {
0432 dev_err(&chip->client->dev,
0433 "%s: data not valid yet\n", __func__);
0434 ret = chip->als_cur_info.lux;
0435 goto out_unlock;
0436 }
0437
0438 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO,
0439 TSL2772_ALS_CHAN0HI);
0440 if (ret < 0)
0441 goto out_unlock;
0442 chip->als_cur_info.als_ch0 = ret;
0443
0444 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO,
0445 TSL2772_ALS_CHAN1HI);
0446 if (ret < 0)
0447 goto out_unlock;
0448 chip->als_cur_info.als_ch1 = ret;
0449
0450 if (chip->als_cur_info.als_ch0 >= chip->als_saturation) {
0451 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
0452 goto update_struct_with_max_lux;
0453 }
0454
0455 if (!chip->als_cur_info.als_ch0) {
0456
0457 ret = chip->als_cur_info.lux;
0458 goto out_unlock;
0459 }
0460
0461 max_lux = 0;
0462 overflow = false;
0463 for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0;
0464 p++) {
0465 int lux;
0466
0467 lux = ((chip->als_cur_info.als_ch0 * p->ch0) -
0468 (chip->als_cur_info.als_ch1 * p->ch1)) /
0469 chip->als_gain_time_scale;
0470
0471
0472
0473
0474
0475
0476
0477 lux = (lux * chip->settings.als_gain_trim) / 1000;
0478
0479 if (lux > TSL2772_LUX_CALC_OVER_FLOW) {
0480 overflow = true;
0481 continue;
0482 }
0483
0484 max_lux = max(max_lux, lux);
0485 }
0486
0487 if (overflow && max_lux == 0)
0488 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
0489
0490 update_struct_with_max_lux:
0491 chip->als_cur_info.lux = max_lux;
0492 ret = max_lux;
0493
0494 out_unlock:
0495 mutex_unlock(&chip->als_mutex);
0496
0497 return ret;
0498 }
0499
0500
0501
0502
0503
0504
0505
0506 static int tsl2772_get_prox(struct iio_dev *indio_dev)
0507 {
0508 struct tsl2772_chip *chip = iio_priv(indio_dev);
0509 int ret;
0510
0511 mutex_lock(&chip->prox_mutex);
0512
0513 ret = tsl2772_read_status(chip);
0514 if (ret < 0)
0515 goto prox_poll_err;
0516
0517 switch (chip->id) {
0518 case tsl2571:
0519 case tsl2671:
0520 case tmd2671:
0521 case tsl2771:
0522 case tmd2771:
0523 if (!(ret & TSL2772_STA_ADC_VALID)) {
0524 ret = -EINVAL;
0525 goto prox_poll_err;
0526 }
0527 break;
0528 case tsl2572:
0529 case tsl2672:
0530 case tmd2672:
0531 case tsl2772:
0532 case tmd2772:
0533 case apds9930:
0534 if (!(ret & TSL2772_STA_PRX_VALID)) {
0535 ret = -EINVAL;
0536 goto prox_poll_err;
0537 }
0538 break;
0539 }
0540
0541 ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI);
0542 if (ret < 0)
0543 goto prox_poll_err;
0544 chip->prox_data = ret;
0545
0546 prox_poll_err:
0547 mutex_unlock(&chip->prox_mutex);
0548
0549 return ret;
0550 }
0551
0552 static int tsl2772_read_prox_led_current(struct tsl2772_chip *chip)
0553 {
0554 struct device *dev = &chip->client->dev;
0555 int ret, tmp, i;
0556
0557 ret = device_property_read_u32(dev, "led-max-microamp", &tmp);
0558 if (ret < 0)
0559 return ret;
0560
0561 for (i = 0; tsl2772_led_currents[i][0] != 0; i++) {
0562 if (tmp == tsl2772_led_currents[i][0]) {
0563 chip->settings.prox_power = tsl2772_led_currents[i][1];
0564 return 0;
0565 }
0566 }
0567
0568 dev_err(dev, "Invalid value %d for led-max-microamp\n", tmp);
0569
0570 return -EINVAL;
0571 }
0572
0573 static int tsl2772_read_prox_diodes(struct tsl2772_chip *chip)
0574 {
0575 struct device *dev = &chip->client->dev;
0576 int i, ret, num_leds, prox_diode_mask;
0577 u32 leds[TSL2772_MAX_PROX_LEDS];
0578
0579 ret = device_property_count_u32(dev, "amstaos,proximity-diodes");
0580 if (ret < 0)
0581 return ret;
0582
0583 num_leds = ret;
0584 if (num_leds > TSL2772_MAX_PROX_LEDS)
0585 num_leds = TSL2772_MAX_PROX_LEDS;
0586
0587 ret = device_property_read_u32_array(dev, "amstaos,proximity-diodes", leds, num_leds);
0588 if (ret < 0) {
0589 dev_err(dev, "Invalid value for amstaos,proximity-diodes: %d.\n", ret);
0590 return ret;
0591 }
0592
0593 prox_diode_mask = 0;
0594 for (i = 0; i < num_leds; i++) {
0595 if (leds[i] == 0)
0596 prox_diode_mask |= TSL2772_DIODE0;
0597 else if (leds[i] == 1)
0598 prox_diode_mask |= TSL2772_DIODE1;
0599 else {
0600 dev_err(dev, "Invalid value %d in amstaos,proximity-diodes.\n", leds[i]);
0601 return -EINVAL;
0602 }
0603 }
0604
0605 return 0;
0606 }
0607
0608 static void tsl2772_parse_dt(struct tsl2772_chip *chip)
0609 {
0610 tsl2772_read_prox_led_current(chip);
0611 tsl2772_read_prox_diodes(chip);
0612 }
0613
0614
0615
0616
0617
0618
0619
0620
0621 static void tsl2772_defaults(struct tsl2772_chip *chip)
0622 {
0623
0624 if (chip->pdata && chip->pdata->platform_default_settings)
0625 memcpy(&chip->settings, chip->pdata->platform_default_settings,
0626 sizeof(tsl2772_default_settings));
0627 else
0628 memcpy(&chip->settings, &tsl2772_default_settings,
0629 sizeof(tsl2772_default_settings));
0630
0631
0632 if (chip->pdata && chip->pdata->platform_lux_table[0].ch0 != 0)
0633 memcpy(chip->tsl2772_device_lux,
0634 chip->pdata->platform_lux_table,
0635 sizeof(chip->pdata->platform_lux_table));
0636 else
0637 memcpy(chip->tsl2772_device_lux,
0638 tsl2772_default_lux_table_group[chip->id],
0639 TSL2772_DEFAULT_TABLE_BYTES);
0640
0641 tsl2772_parse_dt(chip);
0642 }
0643
0644
0645
0646
0647
0648
0649
0650 static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
0651 {
0652 struct tsl2772_chip *chip = iio_priv(indio_dev);
0653 int ret, lux_val;
0654
0655 ret = i2c_smbus_read_byte_data(chip->client,
0656 TSL2772_CMD_REG | TSL2772_CNTRL);
0657 if (ret < 0) {
0658 dev_err(&chip->client->dev,
0659 "%s: failed to read from the CNTRL register\n",
0660 __func__);
0661 return ret;
0662 }
0663
0664 if ((ret & (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON))
0665 != (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) {
0666 dev_err(&chip->client->dev,
0667 "%s: Device is not powered on and/or ADC is not enabled\n",
0668 __func__);
0669 return -EINVAL;
0670 } else if ((ret & TSL2772_STA_ADC_VALID) != TSL2772_STA_ADC_VALID) {
0671 dev_err(&chip->client->dev,
0672 "%s: The two ADC channels have not completed an integration cycle\n",
0673 __func__);
0674 return -ENODATA;
0675 }
0676
0677 lux_val = tsl2772_get_lux(indio_dev);
0678 if (lux_val < 0) {
0679 dev_err(&chip->client->dev,
0680 "%s: failed to get lux\n", __func__);
0681 return lux_val;
0682 }
0683 if (lux_val == 0)
0684 return -ERANGE;
0685
0686 ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
0687 lux_val;
0688 if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
0689 return -ERANGE;
0690
0691 chip->settings.als_gain_trim = ret;
0692
0693 return ret;
0694 }
0695
0696 static void tsl2772_disable_regulators_action(void *_data)
0697 {
0698 struct tsl2772_chip *chip = _data;
0699
0700 regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies);
0701 }
0702
0703 static int tsl2772_chip_on(struct iio_dev *indio_dev)
0704 {
0705 struct tsl2772_chip *chip = iio_priv(indio_dev);
0706 int ret, i, als_count, als_time_us;
0707 u8 *dev_reg, reg_val;
0708
0709
0710 chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
0711 chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
0712 chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
0713 chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
0714 chip->settings.als_prox_config;
0715
0716 chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
0717 (chip->settings.als_thresh_low) & 0xFF;
0718 chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
0719 (chip->settings.als_thresh_low >> 8) & 0xFF;
0720 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
0721 (chip->settings.als_thresh_high) & 0xFF;
0722 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
0723 (chip->settings.als_thresh_high >> 8) & 0xFF;
0724 chip->tsl2772_config[TSL2772_PERSISTENCE] =
0725 (chip->settings.prox_persistence & 0xFF) << 4 |
0726 (chip->settings.als_persistence & 0xFF);
0727
0728 chip->tsl2772_config[TSL2772_PRX_COUNT] =
0729 chip->settings.prox_pulse_count;
0730 chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
0731 (chip->settings.prox_thres_low) & 0xFF;
0732 chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
0733 (chip->settings.prox_thres_low >> 8) & 0xFF;
0734 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
0735 (chip->settings.prox_thres_high) & 0xFF;
0736 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
0737 (chip->settings.prox_thres_high >> 8) & 0xFF;
0738
0739
0740 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
0741
0742 dev_info(&chip->client->dev, "device is already enabled\n");
0743 return -EINVAL;
0744 }
0745
0746
0747 chip->tsl2772_config[TSL2772_GAIN] =
0748 (chip->settings.als_gain & 0xFF) |
0749 ((chip->settings.prox_gain & 0xFF) << 2) |
0750 (chip->settings.prox_diode << 4) |
0751 (chip->settings.prox_power << 6);
0752
0753
0754 als_count = 256 - chip->settings.als_time;
0755 als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
0756 chip->als_saturation = als_count * 768;
0757 chip->als_gain_time_scale = als_time_us *
0758 tsl2772_als_gain[chip->settings.als_gain];
0759
0760
0761
0762
0763
0764 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
0765 if (ret < 0)
0766 return ret;
0767
0768
0769
0770
0771
0772 for (i = 0, dev_reg = chip->tsl2772_config;
0773 i < TSL2772_MAX_CONFIG_REG; i++) {
0774 int reg = TSL2772_CMD_REG + i;
0775
0776 ret = i2c_smbus_write_byte_data(chip->client, reg,
0777 *dev_reg++);
0778 if (ret < 0) {
0779 dev_err(&chip->client->dev,
0780 "%s: failed to write to register %x: %d\n",
0781 __func__, reg, ret);
0782 return ret;
0783 }
0784 }
0785
0786
0787 usleep_range(3000, 3500);
0788
0789 reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
0790 TSL2772_CNTL_PROX_DET_ENBL;
0791 if (chip->settings.als_interrupt_en)
0792 reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
0793 if (chip->settings.prox_interrupt_en)
0794 reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
0795
0796 ret = tsl2772_write_control_reg(chip, reg_val);
0797 if (ret < 0)
0798 return ret;
0799
0800 ret = i2c_smbus_write_byte(chip->client,
0801 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
0802 TSL2772_CMD_PROXALS_INT_CLR);
0803 if (ret < 0) {
0804 dev_err(&chip->client->dev,
0805 "%s: failed to clear interrupt status: %d\n",
0806 __func__, ret);
0807 return ret;
0808 }
0809
0810 chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
0811
0812 return ret;
0813 }
0814
0815 static int tsl2772_chip_off(struct iio_dev *indio_dev)
0816 {
0817 struct tsl2772_chip *chip = iio_priv(indio_dev);
0818
0819
0820 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
0821 return tsl2772_write_control_reg(chip, 0x00);
0822 }
0823
0824 static void tsl2772_chip_off_action(void *data)
0825 {
0826 struct iio_dev *indio_dev = data;
0827
0828 tsl2772_chip_off(indio_dev);
0829 }
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
0841 {
0842 struct tsl2772_chip *chip = iio_priv(indio_dev);
0843 int device_status = chip->tsl2772_chip_status;
0844 int ret;
0845
0846 mutex_lock(&chip->als_mutex);
0847 mutex_lock(&chip->prox_mutex);
0848
0849 if (device_status == TSL2772_CHIP_WORKING) {
0850 ret = tsl2772_chip_off(indio_dev);
0851 if (ret < 0)
0852 goto unlock;
0853 }
0854
0855 ret = tsl2772_chip_on(indio_dev);
0856
0857 unlock:
0858 mutex_unlock(&chip->prox_mutex);
0859 mutex_unlock(&chip->als_mutex);
0860
0861 return ret;
0862 }
0863
0864 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
0865 {
0866 struct tsl2772_chip *chip = iio_priv(indio_dev);
0867 int prox_history[MAX_SAMPLES_CAL + 1];
0868 int i, ret, mean, max, sample_sum;
0869
0870 if (chip->settings.prox_max_samples_cal < 1 ||
0871 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
0872 return -EINVAL;
0873
0874 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
0875 usleep_range(15000, 17500);
0876 ret = tsl2772_get_prox(indio_dev);
0877 if (ret < 0)
0878 return ret;
0879
0880 prox_history[i] = chip->prox_data;
0881 }
0882
0883 sample_sum = 0;
0884 max = INT_MIN;
0885 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
0886 sample_sum += prox_history[i];
0887 max = max(max, prox_history[i]);
0888 }
0889 mean = sample_sum / chip->settings.prox_max_samples_cal;
0890
0891 chip->settings.prox_thres_high = (max << 1) - mean;
0892
0893 return tsl2772_invoke_change(indio_dev);
0894 }
0895
0896 static int tsl2772_read_avail(struct iio_dev *indio_dev,
0897 struct iio_chan_spec const *chan,
0898 const int **vals, int *type, int *length,
0899 long mask)
0900 {
0901 struct tsl2772_chip *chip = iio_priv(indio_dev);
0902
0903 switch (mask) {
0904 case IIO_CHAN_INFO_CALIBSCALE:
0905 if (chan->type == IIO_INTENSITY) {
0906 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
0907 *vals = tsl2772_int_calibscale_avail;
0908 } else {
0909 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
0910 *vals = tsl2772_prox_calibscale_avail;
0911 }
0912 *type = IIO_VAL_INT;
0913 return IIO_AVAIL_LIST;
0914 case IIO_CHAN_INFO_INT_TIME:
0915 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
0916 *vals = tsl2772_int_time_avail[chip->id];
0917 *type = IIO_VAL_INT_PLUS_MICRO;
0918 return IIO_AVAIL_RANGE;
0919 }
0920
0921 return -EINVAL;
0922 }
0923
0924 static ssize_t in_illuminance0_target_input_show(struct device *dev,
0925 struct device_attribute *attr,
0926 char *buf)
0927 {
0928 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
0929
0930 return scnprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
0931 }
0932
0933 static ssize_t in_illuminance0_target_input_store(struct device *dev,
0934 struct device_attribute *attr,
0935 const char *buf, size_t len)
0936 {
0937 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0938 struct tsl2772_chip *chip = iio_priv(indio_dev);
0939 u16 value;
0940 int ret;
0941
0942 if (kstrtou16(buf, 0, &value))
0943 return -EINVAL;
0944
0945 chip->settings.als_cal_target = value;
0946 ret = tsl2772_invoke_change(indio_dev);
0947 if (ret < 0)
0948 return ret;
0949
0950 return len;
0951 }
0952
0953 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
0954 struct device_attribute *attr,
0955 const char *buf, size_t len)
0956 {
0957 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0958 bool value;
0959 int ret;
0960
0961 if (kstrtobool(buf, &value) || !value)
0962 return -EINVAL;
0963
0964 ret = tsl2772_als_calibrate(indio_dev);
0965 if (ret < 0)
0966 return ret;
0967
0968 ret = tsl2772_invoke_change(indio_dev);
0969 if (ret < 0)
0970 return ret;
0971
0972 return len;
0973 }
0974
0975 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
0976 struct device_attribute *attr,
0977 char *buf)
0978 {
0979 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
0980 int i = 0;
0981 int offset = 0;
0982
0983 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
0984 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%u,%u,",
0985 chip->tsl2772_device_lux[i].ch0,
0986 chip->tsl2772_device_lux[i].ch1);
0987 if (chip->tsl2772_device_lux[i].ch0 == 0) {
0988
0989
0990
0991
0992 offset--;
0993 break;
0994 }
0995 i++;
0996 }
0997
0998 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "\n");
0999 return offset;
1000 }
1001
1002 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
1003 struct device_attribute *attr,
1004 const char *buf, size_t len)
1005 {
1006 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1007 struct tsl2772_chip *chip = iio_priv(indio_dev);
1008 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
1009 int n, ret;
1010
1011 get_options(buf, ARRAY_SIZE(value), value);
1012
1013
1014
1015
1016
1017
1018
1019 n = value[0];
1020 if ((n % 2) || n < 4 ||
1021 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
1022 return -EINVAL;
1023
1024 if ((value[(n - 1)] | value[n]) != 0)
1025 return -EINVAL;
1026
1027 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
1028 ret = tsl2772_chip_off(indio_dev);
1029 if (ret < 0)
1030 return ret;
1031 }
1032
1033
1034 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
1035 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
1036
1037 ret = tsl2772_invoke_change(indio_dev);
1038 if (ret < 0)
1039 return ret;
1040
1041 return len;
1042 }
1043
1044 static ssize_t in_proximity0_calibrate_store(struct device *dev,
1045 struct device_attribute *attr,
1046 const char *buf, size_t len)
1047 {
1048 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1049 bool value;
1050 int ret;
1051
1052 if (kstrtobool(buf, &value) || !value)
1053 return -EINVAL;
1054
1055 ret = tsl2772_prox_cal(indio_dev);
1056 if (ret < 0)
1057 return ret;
1058
1059 ret = tsl2772_invoke_change(indio_dev);
1060 if (ret < 0)
1061 return ret;
1062
1063 return len;
1064 }
1065
1066 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
1067 const struct iio_chan_spec *chan,
1068 enum iio_event_type type,
1069 enum iio_event_direction dir)
1070 {
1071 struct tsl2772_chip *chip = iio_priv(indio_dev);
1072
1073 if (chan->type == IIO_INTENSITY)
1074 return chip->settings.als_interrupt_en;
1075 else
1076 return chip->settings.prox_interrupt_en;
1077 }
1078
1079 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
1080 const struct iio_chan_spec *chan,
1081 enum iio_event_type type,
1082 enum iio_event_direction dir,
1083 int val)
1084 {
1085 struct tsl2772_chip *chip = iio_priv(indio_dev);
1086
1087 if (chan->type == IIO_INTENSITY)
1088 chip->settings.als_interrupt_en = val ? true : false;
1089 else
1090 chip->settings.prox_interrupt_en = val ? true : false;
1091
1092 return tsl2772_invoke_change(indio_dev);
1093 }
1094
1095 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
1096 const struct iio_chan_spec *chan,
1097 enum iio_event_type type,
1098 enum iio_event_direction dir,
1099 enum iio_event_info info,
1100 int val, int val2)
1101 {
1102 struct tsl2772_chip *chip = iio_priv(indio_dev);
1103 int ret = -EINVAL, count, persistence;
1104 u8 time;
1105
1106 switch (info) {
1107 case IIO_EV_INFO_VALUE:
1108 if (chan->type == IIO_INTENSITY) {
1109 switch (dir) {
1110 case IIO_EV_DIR_RISING:
1111 chip->settings.als_thresh_high = val;
1112 ret = 0;
1113 break;
1114 case IIO_EV_DIR_FALLING:
1115 chip->settings.als_thresh_low = val;
1116 ret = 0;
1117 break;
1118 default:
1119 break;
1120 }
1121 } else {
1122 switch (dir) {
1123 case IIO_EV_DIR_RISING:
1124 chip->settings.prox_thres_high = val;
1125 ret = 0;
1126 break;
1127 case IIO_EV_DIR_FALLING:
1128 chip->settings.prox_thres_low = val;
1129 ret = 0;
1130 break;
1131 default:
1132 break;
1133 }
1134 }
1135 break;
1136 case IIO_EV_INFO_PERIOD:
1137 if (chan->type == IIO_INTENSITY)
1138 time = chip->settings.als_time;
1139 else
1140 time = chip->settings.prox_time;
1141
1142 count = 256 - time;
1143 persistence = ((val * 1000000) + val2) /
1144 (count * tsl2772_int_time_avail[chip->id][3]);
1145
1146 if (chan->type == IIO_INTENSITY) {
1147
1148 if (persistence > 3)
1149 persistence = (persistence / 5) + 3;
1150
1151 chip->settings.als_persistence = persistence;
1152 } else {
1153 chip->settings.prox_persistence = persistence;
1154 }
1155
1156 ret = 0;
1157 break;
1158 default:
1159 break;
1160 }
1161
1162 if (ret < 0)
1163 return ret;
1164
1165 return tsl2772_invoke_change(indio_dev);
1166 }
1167
1168 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1169 const struct iio_chan_spec *chan,
1170 enum iio_event_type type,
1171 enum iio_event_direction dir,
1172 enum iio_event_info info,
1173 int *val, int *val2)
1174 {
1175 struct tsl2772_chip *chip = iio_priv(indio_dev);
1176 int filter_delay, persistence;
1177 u8 time;
1178
1179 switch (info) {
1180 case IIO_EV_INFO_VALUE:
1181 if (chan->type == IIO_INTENSITY) {
1182 switch (dir) {
1183 case IIO_EV_DIR_RISING:
1184 *val = chip->settings.als_thresh_high;
1185 return IIO_VAL_INT;
1186 case IIO_EV_DIR_FALLING:
1187 *val = chip->settings.als_thresh_low;
1188 return IIO_VAL_INT;
1189 default:
1190 return -EINVAL;
1191 }
1192 } else {
1193 switch (dir) {
1194 case IIO_EV_DIR_RISING:
1195 *val = chip->settings.prox_thres_high;
1196 return IIO_VAL_INT;
1197 case IIO_EV_DIR_FALLING:
1198 *val = chip->settings.prox_thres_low;
1199 return IIO_VAL_INT;
1200 default:
1201 return -EINVAL;
1202 }
1203 }
1204 break;
1205 case IIO_EV_INFO_PERIOD:
1206 if (chan->type == IIO_INTENSITY) {
1207 time = chip->settings.als_time;
1208 persistence = chip->settings.als_persistence;
1209
1210
1211 if (persistence > 3)
1212 persistence = (persistence - 3) * 5;
1213 } else {
1214 time = chip->settings.prox_time;
1215 persistence = chip->settings.prox_persistence;
1216 }
1217
1218 filter_delay = persistence * (256 - time) *
1219 tsl2772_int_time_avail[chip->id][3];
1220
1221 *val = filter_delay / 1000000;
1222 *val2 = filter_delay % 1000000;
1223 return IIO_VAL_INT_PLUS_MICRO;
1224 default:
1225 return -EINVAL;
1226 }
1227 }
1228
1229 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1230 struct iio_chan_spec const *chan,
1231 int *val,
1232 int *val2,
1233 long mask)
1234 {
1235 struct tsl2772_chip *chip = iio_priv(indio_dev);
1236
1237 switch (mask) {
1238 case IIO_CHAN_INFO_PROCESSED:
1239 switch (chan->type) {
1240 case IIO_LIGHT:
1241 tsl2772_get_lux(indio_dev);
1242 *val = chip->als_cur_info.lux;
1243 return IIO_VAL_INT;
1244 default:
1245 return -EINVAL;
1246 }
1247 case IIO_CHAN_INFO_RAW:
1248 switch (chan->type) {
1249 case IIO_INTENSITY:
1250 tsl2772_get_lux(indio_dev);
1251 if (chan->channel == 0)
1252 *val = chip->als_cur_info.als_ch0;
1253 else
1254 *val = chip->als_cur_info.als_ch1;
1255 return IIO_VAL_INT;
1256 case IIO_PROXIMITY:
1257 tsl2772_get_prox(indio_dev);
1258 *val = chip->prox_data;
1259 return IIO_VAL_INT;
1260 default:
1261 return -EINVAL;
1262 }
1263 break;
1264 case IIO_CHAN_INFO_CALIBSCALE:
1265 if (chan->type == IIO_LIGHT)
1266 *val = tsl2772_als_gain[chip->settings.als_gain];
1267 else
1268 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1269 return IIO_VAL_INT;
1270 case IIO_CHAN_INFO_CALIBBIAS:
1271 *val = chip->settings.als_gain_trim;
1272 return IIO_VAL_INT;
1273 case IIO_CHAN_INFO_INT_TIME:
1274 *val = 0;
1275 *val2 = (256 - chip->settings.als_time) *
1276 tsl2772_int_time_avail[chip->id][3];
1277 return IIO_VAL_INT_PLUS_MICRO;
1278 default:
1279 return -EINVAL;
1280 }
1281 }
1282
1283 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1284 struct iio_chan_spec const *chan,
1285 int val,
1286 int val2,
1287 long mask)
1288 {
1289 struct tsl2772_chip *chip = iio_priv(indio_dev);
1290
1291 switch (mask) {
1292 case IIO_CHAN_INFO_CALIBSCALE:
1293 if (chan->type == IIO_INTENSITY) {
1294 switch (val) {
1295 case 1:
1296 chip->settings.als_gain = 0;
1297 break;
1298 case 8:
1299 chip->settings.als_gain = 1;
1300 break;
1301 case 16:
1302 chip->settings.als_gain = 2;
1303 break;
1304 case 120:
1305 chip->settings.als_gain = 3;
1306 break;
1307 default:
1308 return -EINVAL;
1309 }
1310 } else {
1311 switch (val) {
1312 case 1:
1313 chip->settings.prox_gain = 0;
1314 break;
1315 case 2:
1316 chip->settings.prox_gain = 1;
1317 break;
1318 case 4:
1319 chip->settings.prox_gain = 2;
1320 break;
1321 case 8:
1322 chip->settings.prox_gain = 3;
1323 break;
1324 default:
1325 return -EINVAL;
1326 }
1327 }
1328 break;
1329 case IIO_CHAN_INFO_CALIBBIAS:
1330 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1331 val > TSL2772_ALS_GAIN_TRIM_MAX)
1332 return -EINVAL;
1333
1334 chip->settings.als_gain_trim = val;
1335 break;
1336 case IIO_CHAN_INFO_INT_TIME:
1337 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1338 val2 > tsl2772_int_time_avail[chip->id][5])
1339 return -EINVAL;
1340
1341 chip->settings.als_time = 256 -
1342 (val2 / tsl2772_int_time_avail[chip->id][3]);
1343 break;
1344 default:
1345 return -EINVAL;
1346 }
1347
1348 return tsl2772_invoke_change(indio_dev);
1349 }
1350
1351 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1352
1353 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1354
1355 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1356
1357 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1358
1359
1360 static int tsl2772_device_id_verif(int id, int target)
1361 {
1362 switch (target) {
1363 case tsl2571:
1364 case tsl2671:
1365 case tsl2771:
1366 return (id & 0xf0) == TRITON_ID;
1367 case tmd2671:
1368 case tmd2771:
1369 return (id & 0xf0) == HALIBUT_ID;
1370 case tsl2572:
1371 case tsl2672:
1372 case tmd2672:
1373 case tsl2772:
1374 case tmd2772:
1375 case apds9930:
1376 return (id & 0xf0) == SWORDFISH_ID;
1377 }
1378
1379 return -EINVAL;
1380 }
1381
1382 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1383 {
1384 struct iio_dev *indio_dev = private;
1385 struct tsl2772_chip *chip = iio_priv(indio_dev);
1386 s64 timestamp = iio_get_time_ns(indio_dev);
1387 int ret;
1388
1389 ret = tsl2772_read_status(chip);
1390 if (ret < 0)
1391 return IRQ_HANDLED;
1392
1393
1394 if (ret & TSL2772_STA_PRX_INTR) {
1395 iio_push_event(indio_dev,
1396 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1397 0,
1398 IIO_EV_TYPE_THRESH,
1399 IIO_EV_DIR_EITHER),
1400 timestamp);
1401 }
1402
1403 if (ret & TSL2772_STA_ALS_INTR) {
1404 iio_push_event(indio_dev,
1405 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1406 0,
1407 IIO_EV_TYPE_THRESH,
1408 IIO_EV_DIR_EITHER),
1409 timestamp);
1410 }
1411
1412 ret = i2c_smbus_write_byte(chip->client,
1413 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1414 TSL2772_CMD_PROXALS_INT_CLR);
1415 if (ret < 0)
1416 dev_err(&chip->client->dev,
1417 "%s: failed to clear interrupt status: %d\n",
1418 __func__, ret);
1419
1420 return IRQ_HANDLED;
1421 }
1422
1423 static struct attribute *tsl2772_ALS_device_attrs[] = {
1424 &dev_attr_in_illuminance0_target_input.attr,
1425 &dev_attr_in_illuminance0_calibrate.attr,
1426 &dev_attr_in_illuminance0_lux_table.attr,
1427 NULL
1428 };
1429
1430 static struct attribute *tsl2772_PRX_device_attrs[] = {
1431 &dev_attr_in_proximity0_calibrate.attr,
1432 NULL
1433 };
1434
1435 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1436 &dev_attr_in_illuminance0_target_input.attr,
1437 &dev_attr_in_illuminance0_calibrate.attr,
1438 &dev_attr_in_illuminance0_lux_table.attr,
1439 NULL
1440 };
1441
1442 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1443 &dev_attr_in_proximity0_calibrate.attr,
1444 NULL
1445 };
1446
1447 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1448 &dev_attr_in_illuminance0_target_input.attr,
1449 &dev_attr_in_illuminance0_calibrate.attr,
1450 &dev_attr_in_illuminance0_lux_table.attr,
1451 &dev_attr_in_proximity0_calibrate.attr,
1452 NULL
1453 };
1454
1455 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1456 [ALS] = {
1457 .attrs = tsl2772_ALS_device_attrs,
1458 },
1459 [PRX] = {
1460 .attrs = tsl2772_PRX_device_attrs,
1461 },
1462 [ALSPRX] = {
1463 .attrs = tsl2772_ALSPRX_device_attrs,
1464 },
1465 [PRX2] = {
1466 .attrs = tsl2772_PRX2_device_attrs,
1467 },
1468 [ALSPRX2] = {
1469 .attrs = tsl2772_ALSPRX2_device_attrs,
1470 },
1471 };
1472
1473 #define TSL2772_DEVICE_INFO(type)[type] = \
1474 { \
1475 .attrs = &tsl2772_device_attr_group_tbl[type], \
1476 .read_raw = &tsl2772_read_raw, \
1477 .read_avail = &tsl2772_read_avail, \
1478 .write_raw = &tsl2772_write_raw, \
1479 .read_event_value = &tsl2772_read_event_value, \
1480 .write_event_value = &tsl2772_write_event_value, \
1481 .read_event_config = &tsl2772_read_interrupt_config, \
1482 .write_event_config = &tsl2772_write_interrupt_config, \
1483 }
1484
1485 static const struct iio_info tsl2772_device_info[] = {
1486 TSL2772_DEVICE_INFO(ALS),
1487 TSL2772_DEVICE_INFO(PRX),
1488 TSL2772_DEVICE_INFO(ALSPRX),
1489 TSL2772_DEVICE_INFO(PRX2),
1490 TSL2772_DEVICE_INFO(ALSPRX2),
1491 };
1492
1493 static const struct iio_event_spec tsl2772_events[] = {
1494 {
1495 .type = IIO_EV_TYPE_THRESH,
1496 .dir = IIO_EV_DIR_RISING,
1497 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1498 }, {
1499 .type = IIO_EV_TYPE_THRESH,
1500 .dir = IIO_EV_DIR_FALLING,
1501 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1502 }, {
1503 .type = IIO_EV_TYPE_THRESH,
1504 .dir = IIO_EV_DIR_EITHER,
1505 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1506 BIT(IIO_EV_INFO_ENABLE),
1507 },
1508 };
1509
1510 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1511 [ALS] = {
1512 .channel_with_events = {
1513 {
1514 .type = IIO_LIGHT,
1515 .indexed = 1,
1516 .channel = 0,
1517 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1518 }, {
1519 .type = IIO_INTENSITY,
1520 .indexed = 1,
1521 .channel = 0,
1522 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1523 BIT(IIO_CHAN_INFO_INT_TIME) |
1524 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1525 BIT(IIO_CHAN_INFO_CALIBBIAS),
1526 .info_mask_separate_available =
1527 BIT(IIO_CHAN_INFO_INT_TIME) |
1528 BIT(IIO_CHAN_INFO_CALIBSCALE),
1529 .event_spec = tsl2772_events,
1530 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1531 }, {
1532 .type = IIO_INTENSITY,
1533 .indexed = 1,
1534 .channel = 1,
1535 },
1536 },
1537 .channel_without_events = {
1538 {
1539 .type = IIO_LIGHT,
1540 .indexed = 1,
1541 .channel = 0,
1542 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1543 }, {
1544 .type = IIO_INTENSITY,
1545 .indexed = 1,
1546 .channel = 0,
1547 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1548 BIT(IIO_CHAN_INFO_INT_TIME) |
1549 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1550 BIT(IIO_CHAN_INFO_CALIBBIAS),
1551 .info_mask_separate_available =
1552 BIT(IIO_CHAN_INFO_INT_TIME) |
1553 BIT(IIO_CHAN_INFO_CALIBSCALE),
1554 }, {
1555 .type = IIO_INTENSITY,
1556 .indexed = 1,
1557 .channel = 1,
1558 },
1559 },
1560 .chan_table_elements = 3,
1561 .info = &tsl2772_device_info[ALS],
1562 },
1563 [PRX] = {
1564 .channel_with_events = {
1565 {
1566 .type = IIO_PROXIMITY,
1567 .indexed = 1,
1568 .channel = 0,
1569 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1570 .event_spec = tsl2772_events,
1571 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1572 },
1573 },
1574 .channel_without_events = {
1575 {
1576 .type = IIO_PROXIMITY,
1577 .indexed = 1,
1578 .channel = 0,
1579 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1580 },
1581 },
1582 .chan_table_elements = 1,
1583 .info = &tsl2772_device_info[PRX],
1584 },
1585 [ALSPRX] = {
1586 .channel_with_events = {
1587 {
1588 .type = IIO_LIGHT,
1589 .indexed = 1,
1590 .channel = 0,
1591 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1592 }, {
1593 .type = IIO_INTENSITY,
1594 .indexed = 1,
1595 .channel = 0,
1596 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1597 BIT(IIO_CHAN_INFO_INT_TIME) |
1598 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1599 BIT(IIO_CHAN_INFO_CALIBBIAS),
1600 .info_mask_separate_available =
1601 BIT(IIO_CHAN_INFO_INT_TIME) |
1602 BIT(IIO_CHAN_INFO_CALIBSCALE),
1603 .event_spec = tsl2772_events,
1604 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1605 }, {
1606 .type = IIO_INTENSITY,
1607 .indexed = 1,
1608 .channel = 1,
1609 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1610 }, {
1611 .type = IIO_PROXIMITY,
1612 .indexed = 1,
1613 .channel = 0,
1614 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1615 .event_spec = tsl2772_events,
1616 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1617 },
1618 },
1619 .channel_without_events = {
1620 {
1621 .type = IIO_LIGHT,
1622 .indexed = 1,
1623 .channel = 0,
1624 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1625 }, {
1626 .type = IIO_INTENSITY,
1627 .indexed = 1,
1628 .channel = 0,
1629 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1630 BIT(IIO_CHAN_INFO_INT_TIME) |
1631 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1632 BIT(IIO_CHAN_INFO_CALIBBIAS),
1633 .info_mask_separate_available =
1634 BIT(IIO_CHAN_INFO_INT_TIME) |
1635 BIT(IIO_CHAN_INFO_CALIBSCALE),
1636 }, {
1637 .type = IIO_INTENSITY,
1638 .indexed = 1,
1639 .channel = 1,
1640 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1641 }, {
1642 .type = IIO_PROXIMITY,
1643 .indexed = 1,
1644 .channel = 0,
1645 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1646 },
1647 },
1648 .chan_table_elements = 4,
1649 .info = &tsl2772_device_info[ALSPRX],
1650 },
1651 [PRX2] = {
1652 .channel_with_events = {
1653 {
1654 .type = IIO_PROXIMITY,
1655 .indexed = 1,
1656 .channel = 0,
1657 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1658 BIT(IIO_CHAN_INFO_CALIBSCALE),
1659 .info_mask_separate_available =
1660 BIT(IIO_CHAN_INFO_CALIBSCALE),
1661 .event_spec = tsl2772_events,
1662 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1663 },
1664 },
1665 .channel_without_events = {
1666 {
1667 .type = IIO_PROXIMITY,
1668 .indexed = 1,
1669 .channel = 0,
1670 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1671 BIT(IIO_CHAN_INFO_CALIBSCALE),
1672 .info_mask_separate_available =
1673 BIT(IIO_CHAN_INFO_CALIBSCALE),
1674 },
1675 },
1676 .chan_table_elements = 1,
1677 .info = &tsl2772_device_info[PRX2],
1678 },
1679 [ALSPRX2] = {
1680 .channel_with_events = {
1681 {
1682 .type = IIO_LIGHT,
1683 .indexed = 1,
1684 .channel = 0,
1685 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1686 }, {
1687 .type = IIO_INTENSITY,
1688 .indexed = 1,
1689 .channel = 0,
1690 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1691 BIT(IIO_CHAN_INFO_INT_TIME) |
1692 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1693 BIT(IIO_CHAN_INFO_CALIBBIAS),
1694 .info_mask_separate_available =
1695 BIT(IIO_CHAN_INFO_INT_TIME) |
1696 BIT(IIO_CHAN_INFO_CALIBSCALE),
1697 .event_spec = tsl2772_events,
1698 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1699 }, {
1700 .type = IIO_INTENSITY,
1701 .indexed = 1,
1702 .channel = 1,
1703 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1704 }, {
1705 .type = IIO_PROXIMITY,
1706 .indexed = 1,
1707 .channel = 0,
1708 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1709 BIT(IIO_CHAN_INFO_CALIBSCALE),
1710 .info_mask_separate_available =
1711 BIT(IIO_CHAN_INFO_CALIBSCALE),
1712 .event_spec = tsl2772_events,
1713 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1714 },
1715 },
1716 .channel_without_events = {
1717 {
1718 .type = IIO_LIGHT,
1719 .indexed = 1,
1720 .channel = 0,
1721 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1722 }, {
1723 .type = IIO_INTENSITY,
1724 .indexed = 1,
1725 .channel = 0,
1726 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1727 BIT(IIO_CHAN_INFO_INT_TIME) |
1728 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1729 BIT(IIO_CHAN_INFO_CALIBBIAS),
1730 .info_mask_separate_available =
1731 BIT(IIO_CHAN_INFO_INT_TIME) |
1732 BIT(IIO_CHAN_INFO_CALIBSCALE),
1733 }, {
1734 .type = IIO_INTENSITY,
1735 .indexed = 1,
1736 .channel = 1,
1737 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1738 }, {
1739 .type = IIO_PROXIMITY,
1740 .indexed = 1,
1741 .channel = 0,
1742 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1743 BIT(IIO_CHAN_INFO_CALIBSCALE),
1744 .info_mask_separate_available =
1745 BIT(IIO_CHAN_INFO_CALIBSCALE),
1746 },
1747 },
1748 .chan_table_elements = 4,
1749 .info = &tsl2772_device_info[ALSPRX2],
1750 },
1751 };
1752
1753 static int tsl2772_probe(struct i2c_client *clientp,
1754 const struct i2c_device_id *id)
1755 {
1756 struct iio_dev *indio_dev;
1757 struct tsl2772_chip *chip;
1758 int ret;
1759
1760 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1761 if (!indio_dev)
1762 return -ENOMEM;
1763
1764 chip = iio_priv(indio_dev);
1765 chip->client = clientp;
1766 i2c_set_clientdata(clientp, indio_dev);
1767
1768 chip->supplies[TSL2772_SUPPLY_VDD].supply = "vdd";
1769 chip->supplies[TSL2772_SUPPLY_VDDIO].supply = "vddio";
1770
1771 ret = devm_regulator_bulk_get(&clientp->dev,
1772 ARRAY_SIZE(chip->supplies),
1773 chip->supplies);
1774 if (ret < 0)
1775 return dev_err_probe(&clientp->dev, ret, "Failed to get regulators\n");
1776
1777 ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies);
1778 if (ret < 0) {
1779 dev_err(&clientp->dev, "Failed to enable regulators: %d\n",
1780 ret);
1781 return ret;
1782 }
1783
1784 ret = devm_add_action_or_reset(&clientp->dev,
1785 tsl2772_disable_regulators_action,
1786 chip);
1787 if (ret < 0) {
1788 dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n",
1789 ret);
1790 return ret;
1791 }
1792
1793 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1794
1795 ret = i2c_smbus_read_byte_data(chip->client,
1796 TSL2772_CMD_REG | TSL2772_CHIPID);
1797 if (ret < 0)
1798 return ret;
1799
1800 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1801 dev_info(&chip->client->dev,
1802 "%s: i2c device found does not match expected id\n",
1803 __func__);
1804 return -EINVAL;
1805 }
1806
1807 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1808 if (ret < 0) {
1809 dev_err(&clientp->dev,
1810 "%s: Failed to write to CMD register: %d\n",
1811 __func__, ret);
1812 return ret;
1813 }
1814
1815 mutex_init(&chip->als_mutex);
1816 mutex_init(&chip->prox_mutex);
1817
1818 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1819 chip->pdata = dev_get_platdata(&clientp->dev);
1820 chip->id = id->driver_data;
1821 chip->chip_info =
1822 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1823
1824 indio_dev->info = chip->chip_info->info;
1825 indio_dev->modes = INDIO_DIRECT_MODE;
1826 indio_dev->name = chip->client->name;
1827 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1828
1829 if (clientp->irq) {
1830 indio_dev->channels = chip->chip_info->channel_with_events;
1831
1832 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1833 NULL,
1834 &tsl2772_event_handler,
1835 IRQF_TRIGGER_FALLING |
1836 IRQF_ONESHOT,
1837 "TSL2772_event",
1838 indio_dev);
1839 if (ret) {
1840 dev_err(&clientp->dev,
1841 "%s: irq request failed\n", __func__);
1842 return ret;
1843 }
1844 } else {
1845 indio_dev->channels = chip->chip_info->channel_without_events;
1846 }
1847
1848 tsl2772_defaults(chip);
1849 ret = tsl2772_chip_on(indio_dev);
1850 if (ret < 0)
1851 return ret;
1852
1853 ret = devm_add_action_or_reset(&clientp->dev,
1854 tsl2772_chip_off_action,
1855 indio_dev);
1856 if (ret < 0)
1857 return ret;
1858
1859 return devm_iio_device_register(&clientp->dev, indio_dev);
1860 }
1861
1862 static int tsl2772_suspend(struct device *dev)
1863 {
1864 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1865 struct tsl2772_chip *chip = iio_priv(indio_dev);
1866 int ret;
1867
1868 ret = tsl2772_chip_off(indio_dev);
1869 regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies);
1870
1871 return ret;
1872 }
1873
1874 static int tsl2772_resume(struct device *dev)
1875 {
1876 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1877 struct tsl2772_chip *chip = iio_priv(indio_dev);
1878 int ret;
1879
1880 ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies);
1881 if (ret < 0)
1882 return ret;
1883
1884 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1885
1886 return tsl2772_chip_on(indio_dev);
1887 }
1888
1889 static const struct i2c_device_id tsl2772_idtable[] = {
1890 { "tsl2571", tsl2571 },
1891 { "tsl2671", tsl2671 },
1892 { "tmd2671", tmd2671 },
1893 { "tsl2771", tsl2771 },
1894 { "tmd2771", tmd2771 },
1895 { "tsl2572", tsl2572 },
1896 { "tsl2672", tsl2672 },
1897 { "tmd2672", tmd2672 },
1898 { "tsl2772", tsl2772 },
1899 { "tmd2772", tmd2772 },
1900 { "apds9930", apds9930 },
1901 {}
1902 };
1903
1904 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1905
1906 static const struct of_device_id tsl2772_of_match[] = {
1907 { .compatible = "amstaos,tsl2571" },
1908 { .compatible = "amstaos,tsl2671" },
1909 { .compatible = "amstaos,tmd2671" },
1910 { .compatible = "amstaos,tsl2771" },
1911 { .compatible = "amstaos,tmd2771" },
1912 { .compatible = "amstaos,tsl2572" },
1913 { .compatible = "amstaos,tsl2672" },
1914 { .compatible = "amstaos,tmd2672" },
1915 { .compatible = "amstaos,tsl2772" },
1916 { .compatible = "amstaos,tmd2772" },
1917 { .compatible = "avago,apds9930" },
1918 {}
1919 };
1920 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1921
1922 static const struct dev_pm_ops tsl2772_pm_ops = {
1923 .suspend = tsl2772_suspend,
1924 .resume = tsl2772_resume,
1925 };
1926
1927 static struct i2c_driver tsl2772_driver = {
1928 .driver = {
1929 .name = "tsl2772",
1930 .of_match_table = tsl2772_of_match,
1931 .pm = &tsl2772_pm_ops,
1932 },
1933 .id_table = tsl2772_idtable,
1934 .probe = tsl2772_probe,
1935 };
1936
1937 module_i2c_driver(tsl2772_driver);
1938
1939 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1940 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1941 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1942 MODULE_LICENSE("GPL");