0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/bitfield.h>
0013 #include <linux/debugfs.h>
0014 #include <linux/delay.h>
0015 #include <linux/i2c.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/iopoll.h>
0018 #include <linux/kernel.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/pm_runtime.h>
0022 #include <linux/sysfs.h>
0023
0024 #include <asm/unaligned.h>
0025
0026 #include <linux/iio/events.h>
0027 #include <linux/iio/iio.h>
0028 #include <linux/iio/sysfs.h>
0029
0030
0031 #define TSL2591_FVAL_TO_MSEC(x) (((x) + 1) * 100)
0032
0033 #define TSL2591_FVAL_TO_SEC(x) ((x) + 1)
0034
0035 #define TSL2591_SEC_TO_FVAL(x) ((x) - 1)
0036
0037
0038 #define TSL2591_ENABLE 0x00
0039 #define TSL2591_CONTROL 0x01
0040 #define TSL2591_AILTL 0x04
0041 #define TSL2591_AILTH 0x05
0042 #define TSL2591_AIHTL 0x06
0043 #define TSL2591_AIHTH 0x07
0044 #define TSL2591_NP_AILTL 0x08
0045 #define TSL2591_NP_AILTH 0x09
0046 #define TSL2591_NP_AIHTL 0x0A
0047 #define TSL2591_NP_AIHTH 0x0B
0048 #define TSL2591_PERSIST 0x0C
0049 #define TSL2591_PACKAGE_ID 0x11
0050 #define TSL2591_DEVICE_ID 0x12
0051 #define TSL2591_STATUS 0x13
0052 #define TSL2591_C0_DATAL 0x14
0053 #define TSL2591_C0_DATAH 0x15
0054 #define TSL2591_C1_DATAL 0x16
0055 #define TSL2591_C1_DATAH 0x17
0056
0057
0058 #define TSL2591_CMD_NOP 0xA0
0059 #define TSL2591_CMD_SF_INTSET 0xE4
0060 #define TSL2591_CMD_SF_CALS_I 0xE5
0061 #define TSL2591_CMD_SF_CALS_NPI 0xE7
0062 #define TSL2591_CMD_SF_CNP_ALSI 0xEA
0063
0064
0065 #define TSL2591_PWR_ON 0x01
0066 #define TSL2591_PWR_OFF 0x00
0067 #define TSL2591_ENABLE_ALS 0x02
0068 #define TSL2591_ENABLE_ALS_INT 0x10
0069 #define TSL2591_ENABLE_SLEEP_INT 0x40
0070 #define TSL2591_ENABLE_NP_INT 0x80
0071
0072
0073 #define TSL2591_CTRL_ALS_INTEGRATION_100MS 0x00
0074 #define TSL2591_CTRL_ALS_INTEGRATION_200MS 0x01
0075 #define TSL2591_CTRL_ALS_INTEGRATION_300MS 0x02
0076 #define TSL2591_CTRL_ALS_INTEGRATION_400MS 0x03
0077 #define TSL2591_CTRL_ALS_INTEGRATION_500MS 0x04
0078 #define TSL2591_CTRL_ALS_INTEGRATION_600MS 0x05
0079 #define TSL2591_CTRL_ALS_LOW_GAIN 0x00
0080 #define TSL2591_CTRL_ALS_MED_GAIN 0x10
0081 #define TSL2591_CTRL_ALS_HIGH_GAIN 0x20
0082 #define TSL2591_CTRL_ALS_MAX_GAIN 0x30
0083 #define TSL2591_CTRL_SYS_RESET 0x80
0084
0085
0086 #define TSL2591_PRST_ALS_INT_CYCLE_0 0x00
0087 #define TSL2591_PRST_ALS_INT_CYCLE_ANY 0x01
0088 #define TSL2591_PRST_ALS_INT_CYCLE_2 0x02
0089 #define TSL2591_PRST_ALS_INT_CYCLE_3 0x03
0090 #define TSL2591_PRST_ALS_INT_CYCLE_5 0x04
0091 #define TSL2591_PRST_ALS_INT_CYCLE_10 0x05
0092 #define TSL2591_PRST_ALS_INT_CYCLE_15 0x06
0093 #define TSL2591_PRST_ALS_INT_CYCLE_20 0x07
0094 #define TSL2591_PRST_ALS_INT_CYCLE_25 0x08
0095 #define TSL2591_PRST_ALS_INT_CYCLE_30 0x09
0096 #define TSL2591_PRST_ALS_INT_CYCLE_35 0x0A
0097 #define TSL2591_PRST_ALS_INT_CYCLE_40 0x0B
0098 #define TSL2591_PRST_ALS_INT_CYCLE_45 0x0C
0099 #define TSL2591_PRST_ALS_INT_CYCLE_50 0x0D
0100 #define TSL2591_PRST_ALS_INT_CYCLE_55 0x0E
0101 #define TSL2591_PRST_ALS_INT_CYCLE_60 0x0F
0102 #define TSL2591_PRST_ALS_INT_CYCLE_MAX (BIT(4) - 1)
0103
0104
0105 #define TSL2591_PACKAGE_ID_MASK GENMASK(5, 4)
0106
0107
0108 #define TSL2591_DEVICE_ID_MASK GENMASK(7, 0)
0109
0110
0111 #define TSL2591_STS_ALS_VALID_MASK BIT(0)
0112 #define TSL2591_STS_ALS_INT_MASK BIT(4)
0113 #define TSL2591_STS_NPERS_INT_MASK BIT(5)
0114 #define TSL2591_STS_VAL_HIGH_MASK BIT(0)
0115
0116
0117 #define TSL2591_PACKAGE_ID_VAL 0x00
0118 #define TSL2591_DEVICE_ID_VAL 0x50
0119
0120
0121 #define TSL2591_POWER_OFF_DELAY_MS 2000
0122
0123
0124 #define TSL2591_DEFAULT_ALS_INT_TIME TSL2591_CTRL_ALS_INTEGRATION_300MS
0125 #define TSL2591_DEFAULT_ALS_GAIN TSL2591_CTRL_ALS_MED_GAIN
0126 #define TSL2591_DEFAULT_ALS_PERSIST TSL2591_PRST_ALS_INT_CYCLE_ANY
0127 #define TSL2591_DEFAULT_ALS_LOWER_THRESH 100
0128 #define TSL2591_DEFAULT_ALS_UPPER_THRESH 1500
0129
0130
0131 #define TSL2591_NUM_DATA_REGISTERS 4
0132
0133
0134 #define TSL2591_ALS_STS_VALID_COUNT 10
0135
0136
0137 #define TSL2591_DELAY_PERIOD_US 10000
0138
0139
0140 #define TSL2591_MAX_ALS_INT_TIME_MS 600
0141 #define TSL2591_ALS_MAX_VALUE (BIT(16) - 1)
0142
0143
0144
0145
0146
0147
0148 #define TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER 1
0149 #define TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER 25
0150 #define TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER 428
0151 #define TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER 9876
0152 #define TSL2591_LUX_COEFFICIENT 408
0153
0154 struct tsl2591_als_settings {
0155 u16 als_lower_thresh;
0156 u16 als_upper_thresh;
0157 u8 als_int_time;
0158 u8 als_persist;
0159 u8 als_gain;
0160 };
0161
0162 struct tsl2591_chip {
0163 struct tsl2591_als_settings als_settings;
0164 struct i2c_client *client;
0165
0166
0167
0168
0169 struct mutex als_mutex;
0170 bool events_enabled;
0171 };
0172
0173
0174
0175
0176
0177
0178 static const char * const tsl2591_als_period_list[] = {
0179 "0.1 0.2 0.3 0.5 1.0 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0",
0180 "0.2 0.4 0.6 1.0 2.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0",
0181 "0.3 0.6 0.9 1.5 3.0 6.0 7.5 9.0 10.5 12.0 13.5 15.0 16.5 18.0",
0182 "0.4 0.8 1.2 2.0 4.0 8.0 10.0 12.0 14.0 16.0 18.0 20.0 22.0 24.0",
0183 "0.5 1.0 1.5 2.5 5.0 10.0 12.5 15.0 17.5 20.0 22.5 25.0 27.5 30.0",
0184 "0.6 1.2 1.8 3.0 6.0 12.0 15.0 18.0 21.0 24.0 27.0 30.0 33.0 36.0",
0185 };
0186
0187 static const int tsl2591_int_time_available[] = {
0188 1, 2, 3, 4, 5, 6,
0189 };
0190
0191 static const int tsl2591_calibscale_available[] = {
0192 1, 25, 428, 9876,
0193 };
0194
0195 static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
0196 u16 als_lower_threshold);
0197 static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
0198 u16 als_upper_threshold);
0199
0200 static int tsl2591_gain_to_multiplier(const u8 als_gain)
0201 {
0202 switch (als_gain) {
0203 case TSL2591_CTRL_ALS_LOW_GAIN:
0204 return TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER;
0205 case TSL2591_CTRL_ALS_MED_GAIN:
0206 return TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER;
0207 case TSL2591_CTRL_ALS_HIGH_GAIN:
0208 return TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER;
0209 case TSL2591_CTRL_ALS_MAX_GAIN:
0210 return TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER;
0211 default:
0212 return -EINVAL;
0213 }
0214 }
0215
0216 static int tsl2591_multiplier_to_gain(const u32 multiplier)
0217 {
0218 switch (multiplier) {
0219 case TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER:
0220 return TSL2591_CTRL_ALS_LOW_GAIN;
0221 case TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER:
0222 return TSL2591_CTRL_ALS_MED_GAIN;
0223 case TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER:
0224 return TSL2591_CTRL_ALS_HIGH_GAIN;
0225 case TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER:
0226 return TSL2591_CTRL_ALS_MAX_GAIN;
0227 default:
0228 return -EINVAL;
0229 }
0230 }
0231
0232 static int tsl2591_persist_cycle_to_lit(const u8 als_persist)
0233 {
0234 switch (als_persist) {
0235 case TSL2591_PRST_ALS_INT_CYCLE_ANY:
0236 return 1;
0237 case TSL2591_PRST_ALS_INT_CYCLE_2:
0238 return 2;
0239 case TSL2591_PRST_ALS_INT_CYCLE_3:
0240 return 3;
0241 case TSL2591_PRST_ALS_INT_CYCLE_5:
0242 return 5;
0243 case TSL2591_PRST_ALS_INT_CYCLE_10:
0244 return 10;
0245 case TSL2591_PRST_ALS_INT_CYCLE_15:
0246 return 15;
0247 case TSL2591_PRST_ALS_INT_CYCLE_20:
0248 return 20;
0249 case TSL2591_PRST_ALS_INT_CYCLE_25:
0250 return 25;
0251 case TSL2591_PRST_ALS_INT_CYCLE_30:
0252 return 30;
0253 case TSL2591_PRST_ALS_INT_CYCLE_35:
0254 return 35;
0255 case TSL2591_PRST_ALS_INT_CYCLE_40:
0256 return 40;
0257 case TSL2591_PRST_ALS_INT_CYCLE_45:
0258 return 45;
0259 case TSL2591_PRST_ALS_INT_CYCLE_50:
0260 return 50;
0261 case TSL2591_PRST_ALS_INT_CYCLE_55:
0262 return 55;
0263 case TSL2591_PRST_ALS_INT_CYCLE_60:
0264 return 60;
0265 default:
0266 return -EINVAL;
0267 }
0268 }
0269
0270 static int tsl2591_persist_lit_to_cycle(const u8 als_persist)
0271 {
0272 switch (als_persist) {
0273 case 1:
0274 return TSL2591_PRST_ALS_INT_CYCLE_ANY;
0275 case 2:
0276 return TSL2591_PRST_ALS_INT_CYCLE_2;
0277 case 3:
0278 return TSL2591_PRST_ALS_INT_CYCLE_3;
0279 case 5:
0280 return TSL2591_PRST_ALS_INT_CYCLE_5;
0281 case 10:
0282 return TSL2591_PRST_ALS_INT_CYCLE_10;
0283 case 15:
0284 return TSL2591_PRST_ALS_INT_CYCLE_15;
0285 case 20:
0286 return TSL2591_PRST_ALS_INT_CYCLE_20;
0287 case 25:
0288 return TSL2591_PRST_ALS_INT_CYCLE_25;
0289 case 30:
0290 return TSL2591_PRST_ALS_INT_CYCLE_30;
0291 case 35:
0292 return TSL2591_PRST_ALS_INT_CYCLE_35;
0293 case 40:
0294 return TSL2591_PRST_ALS_INT_CYCLE_40;
0295 case 45:
0296 return TSL2591_PRST_ALS_INT_CYCLE_45;
0297 case 50:
0298 return TSL2591_PRST_ALS_INT_CYCLE_50;
0299 case 55:
0300 return TSL2591_PRST_ALS_INT_CYCLE_55;
0301 case 60:
0302 return TSL2591_PRST_ALS_INT_CYCLE_60;
0303 default:
0304 return -EINVAL;
0305 }
0306 }
0307
0308 static int tsl2591_compatible_int_time(struct tsl2591_chip *chip,
0309 const u32 als_integration_time)
0310 {
0311 switch (als_integration_time) {
0312 case TSL2591_CTRL_ALS_INTEGRATION_100MS:
0313 case TSL2591_CTRL_ALS_INTEGRATION_200MS:
0314 case TSL2591_CTRL_ALS_INTEGRATION_300MS:
0315 case TSL2591_CTRL_ALS_INTEGRATION_400MS:
0316 case TSL2591_CTRL_ALS_INTEGRATION_500MS:
0317 case TSL2591_CTRL_ALS_INTEGRATION_600MS:
0318 return 0;
0319 default:
0320 return -EINVAL;
0321 }
0322 }
0323
0324 static int tsl2591_als_time_to_fval(const u32 als_integration_time)
0325 {
0326 int i;
0327
0328 for (i = 0; i < ARRAY_SIZE(tsl2591_int_time_available); i++) {
0329 if (als_integration_time == tsl2591_int_time_available[i])
0330 return TSL2591_SEC_TO_FVAL(als_integration_time);
0331 }
0332
0333 return -EINVAL;
0334 }
0335
0336 static int tsl2591_compatible_gain(struct tsl2591_chip *chip, const u8 als_gain)
0337 {
0338 switch (als_gain) {
0339 case TSL2591_CTRL_ALS_LOW_GAIN:
0340 case TSL2591_CTRL_ALS_MED_GAIN:
0341 case TSL2591_CTRL_ALS_HIGH_GAIN:
0342 case TSL2591_CTRL_ALS_MAX_GAIN:
0343 return 0;
0344 default:
0345 return -EINVAL;
0346 }
0347 }
0348
0349 static int tsl2591_compatible_als_persist_cycle(struct tsl2591_chip *chip,
0350 const u32 als_persist)
0351 {
0352 switch (als_persist) {
0353 case TSL2591_PRST_ALS_INT_CYCLE_ANY:
0354 case TSL2591_PRST_ALS_INT_CYCLE_2:
0355 case TSL2591_PRST_ALS_INT_CYCLE_3:
0356 case TSL2591_PRST_ALS_INT_CYCLE_5:
0357 case TSL2591_PRST_ALS_INT_CYCLE_10:
0358 case TSL2591_PRST_ALS_INT_CYCLE_15:
0359 case TSL2591_PRST_ALS_INT_CYCLE_20:
0360 case TSL2591_PRST_ALS_INT_CYCLE_25:
0361 case TSL2591_PRST_ALS_INT_CYCLE_30:
0362 case TSL2591_PRST_ALS_INT_CYCLE_35:
0363 case TSL2591_PRST_ALS_INT_CYCLE_40:
0364 case TSL2591_PRST_ALS_INT_CYCLE_45:
0365 case TSL2591_PRST_ALS_INT_CYCLE_50:
0366 case TSL2591_PRST_ALS_INT_CYCLE_55:
0367 case TSL2591_PRST_ALS_INT_CYCLE_60:
0368 return 0;
0369 default:
0370 return -EINVAL;
0371 }
0372 }
0373
0374 static int tsl2591_check_als_valid(struct i2c_client *client)
0375 {
0376 int ret;
0377
0378 ret = i2c_smbus_read_byte_data(client, TSL2591_CMD_NOP | TSL2591_STATUS);
0379 if (ret < 0) {
0380 dev_err(&client->dev, "Failed to read register\n");
0381 return -EINVAL;
0382 }
0383
0384 return FIELD_GET(TSL2591_STS_ALS_VALID_MASK, ret);
0385 }
0386
0387 static int tsl2591_wait_adc_complete(struct tsl2591_chip *chip)
0388 {
0389 struct tsl2591_als_settings settings = chip->als_settings;
0390 struct i2c_client *client = chip->client;
0391 int delay;
0392 int val;
0393 int ret;
0394
0395 delay = TSL2591_FVAL_TO_MSEC(settings.als_int_time);
0396 if (!delay)
0397 return -EINVAL;
0398
0399
0400
0401
0402
0403 msleep(delay);
0404
0405
0406 ret = readx_poll_timeout(tsl2591_check_als_valid, client,
0407 val, val == TSL2591_STS_VAL_HIGH_MASK,
0408 TSL2591_DELAY_PERIOD_US,
0409 TSL2591_DELAY_PERIOD_US * TSL2591_ALS_STS_VALID_COUNT);
0410 if (ret)
0411 dev_err(&client->dev, "Timed out waiting for valid ALS data\n");
0412
0413 return ret;
0414 }
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431 static int tsl2591_read_channel_data(struct iio_dev *indio_dev,
0432 struct iio_chan_spec const *chan,
0433 int *val, int *val2)
0434 {
0435 struct tsl2591_chip *chip = iio_priv(indio_dev);
0436 struct tsl2591_als_settings *settings = &chip->als_settings;
0437 struct i2c_client *client = chip->client;
0438 u8 als_data[TSL2591_NUM_DATA_REGISTERS];
0439 int counts_per_lux, int_time_fval, gain_multi, lux;
0440 u16 als_ch0, als_ch1;
0441 int ret;
0442
0443 ret = tsl2591_wait_adc_complete(chip);
0444 if (ret < 0) {
0445 dev_err(&client->dev, "No data available. Err: %d\n", ret);
0446 return ret;
0447 }
0448
0449 ret = i2c_smbus_read_i2c_block_data(client,
0450 TSL2591_CMD_NOP | TSL2591_C0_DATAL,
0451 sizeof(als_data), als_data);
0452 if (ret < 0) {
0453 dev_err(&client->dev, "Failed to read data bytes");
0454 return ret;
0455 }
0456
0457 als_ch0 = get_unaligned_le16(&als_data[0]);
0458 als_ch1 = get_unaligned_le16(&als_data[2]);
0459
0460 switch (chan->type) {
0461 case IIO_INTENSITY:
0462 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
0463 *val = als_ch0;
0464 else if (chan->channel2 == IIO_MOD_LIGHT_IR)
0465 *val = als_ch1;
0466 else
0467 return -EINVAL;
0468 break;
0469 case IIO_LIGHT:
0470 gain_multi = tsl2591_gain_to_multiplier(settings->als_gain);
0471 if (gain_multi < 0) {
0472 dev_err(&client->dev, "Invalid multiplier");
0473 return gain_multi;
0474 }
0475
0476 int_time_fval = TSL2591_FVAL_TO_MSEC(settings->als_int_time);
0477
0478 counts_per_lux = (int_time_fval * gain_multi) / TSL2591_LUX_COEFFICIENT;
0479
0480 dev_dbg(&client->dev, "Counts Per Lux: %d\n", counts_per_lux);
0481
0482
0483 lux = ((als_ch0 - als_ch1) *
0484 (1000 - ((als_ch1 * 1000) / als_ch0))) / counts_per_lux;
0485
0486 dev_dbg(&client->dev, "Raw lux calculation: %d\n", lux);
0487
0488
0489 *val = lux / 1000;
0490
0491
0492 *val2 = (lux - (*val * 1000)) * 1000;
0493
0494 break;
0495 default:
0496 return -EINVAL;
0497 }
0498
0499 return 0;
0500 }
0501
0502 static int tsl2591_set_als_gain_int_time(struct tsl2591_chip *chip)
0503 {
0504 struct tsl2591_als_settings als_settings = chip->als_settings;
0505 struct i2c_client *client = chip->client;
0506 int ret;
0507
0508 ret = i2c_smbus_write_byte_data(client,
0509 TSL2591_CMD_NOP | TSL2591_CONTROL,
0510 als_settings.als_int_time | als_settings.als_gain);
0511 if (ret)
0512 dev_err(&client->dev, "Failed to set als gain & int time\n");
0513
0514 return ret;
0515 }
0516
0517 static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
0518 u16 als_lower_threshold)
0519 {
0520 struct tsl2591_als_settings als_settings = chip->als_settings;
0521 struct i2c_client *client = chip->client;
0522 u16 als_upper_threshold;
0523 u8 als_lower_l;
0524 u8 als_lower_h;
0525 int ret;
0526
0527 chip->als_settings.als_lower_thresh = als_lower_threshold;
0528
0529
0530
0531
0532
0533
0534 if (als_lower_threshold >= als_settings.als_upper_thresh) {
0535 als_upper_threshold = als_lower_threshold + 1;
0536 tsl2591_set_als_upper_threshold(chip, als_upper_threshold);
0537 }
0538
0539 als_lower_l = als_lower_threshold;
0540 als_lower_h = als_lower_threshold >> 8;
0541
0542 ret = i2c_smbus_write_byte_data(client,
0543 TSL2591_CMD_NOP | TSL2591_AILTL,
0544 als_lower_l);
0545 if (ret) {
0546 dev_err(&client->dev, "Failed to set als lower threshold\n");
0547 return ret;
0548 }
0549
0550 ret = i2c_smbus_write_byte_data(client,
0551 TSL2591_CMD_NOP | TSL2591_AILTH,
0552 als_lower_h);
0553 if (ret) {
0554 dev_err(&client->dev, "Failed to set als lower threshold\n");
0555 return ret;
0556 }
0557
0558 return 0;
0559 }
0560
0561 static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
0562 u16 als_upper_threshold)
0563 {
0564 struct tsl2591_als_settings als_settings = chip->als_settings;
0565 struct i2c_client *client = chip->client;
0566 u16 als_lower_threshold;
0567 u8 als_upper_l;
0568 u8 als_upper_h;
0569 int ret;
0570
0571 if (als_upper_threshold > TSL2591_ALS_MAX_VALUE)
0572 return -EINVAL;
0573
0574 chip->als_settings.als_upper_thresh = als_upper_threshold;
0575
0576
0577
0578
0579
0580
0581 if (als_upper_threshold < als_settings.als_lower_thresh) {
0582 als_lower_threshold = als_upper_threshold - 1;
0583 tsl2591_set_als_lower_threshold(chip, als_lower_threshold);
0584 }
0585
0586 als_upper_l = als_upper_threshold;
0587 als_upper_h = als_upper_threshold >> 8;
0588
0589 ret = i2c_smbus_write_byte_data(client,
0590 TSL2591_CMD_NOP | TSL2591_AIHTL,
0591 als_upper_l);
0592 if (ret) {
0593 dev_err(&client->dev, "Failed to set als upper threshold\n");
0594 return ret;
0595 }
0596
0597 ret = i2c_smbus_write_byte_data(client,
0598 TSL2591_CMD_NOP | TSL2591_AIHTH,
0599 als_upper_h);
0600 if (ret) {
0601 dev_err(&client->dev, "Failed to set als upper threshold\n");
0602 return ret;
0603 }
0604
0605 return 0;
0606 }
0607
0608 static int tsl2591_set_als_persist_cycle(struct tsl2591_chip *chip,
0609 u8 als_persist)
0610 {
0611 struct i2c_client *client = chip->client;
0612 int ret;
0613
0614 ret = i2c_smbus_write_byte_data(client,
0615 TSL2591_CMD_NOP | TSL2591_PERSIST,
0616 als_persist);
0617 if (ret)
0618 dev_err(&client->dev, "Failed to set als persist cycle\n");
0619
0620 chip->als_settings.als_persist = als_persist;
0621
0622 return ret;
0623 }
0624
0625 static int tsl2591_set_power_state(struct tsl2591_chip *chip, u8 state)
0626 {
0627 struct i2c_client *client = chip->client;
0628 int ret;
0629
0630 ret = i2c_smbus_write_byte_data(client,
0631 TSL2591_CMD_NOP | TSL2591_ENABLE,
0632 state);
0633 if (ret)
0634 dev_err(&client->dev,
0635 "Failed to set the power state to %#04x\n", state);
0636
0637 return ret;
0638 }
0639
0640 static ssize_t tsl2591_in_illuminance_period_available_show(struct device *dev,
0641 struct device_attribute *attr,
0642 char *buf)
0643 {
0644 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0645 struct tsl2591_chip *chip = iio_priv(indio_dev);
0646
0647 return sysfs_emit(buf, "%s\n",
0648 tsl2591_als_period_list[chip->als_settings.als_int_time]);
0649 }
0650
0651 static IIO_DEVICE_ATTR_RO(tsl2591_in_illuminance_period_available, 0);
0652
0653 static struct attribute *tsl2591_event_attrs_ctrl[] = {
0654 &iio_dev_attr_tsl2591_in_illuminance_period_available.dev_attr.attr,
0655 NULL
0656 };
0657
0658 static const struct attribute_group tsl2591_event_attribute_group = {
0659 .attrs = tsl2591_event_attrs_ctrl,
0660 };
0661
0662 static const struct iio_event_spec tsl2591_events[] = {
0663 {
0664 .type = IIO_EV_TYPE_THRESH,
0665 .dir = IIO_EV_DIR_RISING,
0666 .mask_separate = BIT(IIO_EV_INFO_VALUE),
0667 }, {
0668 .type = IIO_EV_TYPE_THRESH,
0669 .dir = IIO_EV_DIR_FALLING,
0670 .mask_separate = BIT(IIO_EV_INFO_VALUE),
0671 }, {
0672 .type = IIO_EV_TYPE_THRESH,
0673 .dir = IIO_EV_DIR_EITHER,
0674 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
0675 BIT(IIO_EV_INFO_ENABLE),
0676 },
0677 };
0678
0679 static const struct iio_chan_spec tsl2591_channels[] = {
0680 {
0681 .type = IIO_INTENSITY,
0682 .modified = 1,
0683 .channel2 = IIO_MOD_LIGHT_IR,
0684 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0685 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
0686 BIT(IIO_CHAN_INFO_CALIBSCALE),
0687 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
0688 BIT(IIO_CHAN_INFO_CALIBSCALE)
0689 },
0690 {
0691 .type = IIO_INTENSITY,
0692 .modified = 1,
0693 .channel2 = IIO_MOD_LIGHT_BOTH,
0694 .event_spec = tsl2591_events,
0695 .num_event_specs = ARRAY_SIZE(tsl2591_events),
0696 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0697 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
0698 BIT(IIO_CHAN_INFO_CALIBSCALE),
0699 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
0700 BIT(IIO_CHAN_INFO_CALIBSCALE)
0701 },
0702 {
0703 .type = IIO_LIGHT,
0704 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
0705 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
0706 BIT(IIO_CHAN_INFO_CALIBSCALE),
0707 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
0708 BIT(IIO_CHAN_INFO_CALIBSCALE)
0709 },
0710 };
0711
0712 static int tsl2591_read_raw(struct iio_dev *indio_dev,
0713 struct iio_chan_spec const *chan,
0714 int *val, int *val2, long mask)
0715 {
0716 struct tsl2591_chip *chip = iio_priv(indio_dev);
0717 struct i2c_client *client = chip->client;
0718 int ret;
0719
0720 pm_runtime_get_sync(&client->dev);
0721
0722 mutex_lock(&chip->als_mutex);
0723
0724 switch (mask) {
0725 case IIO_CHAN_INFO_RAW:
0726 if (chan->type != IIO_INTENSITY) {
0727 ret = -EINVAL;
0728 goto err_unlock;
0729 }
0730
0731 ret = tsl2591_read_channel_data(indio_dev, chan, val, val2);
0732 if (ret < 0)
0733 goto err_unlock;
0734
0735 ret = IIO_VAL_INT;
0736 break;
0737 case IIO_CHAN_INFO_PROCESSED:
0738 if (chan->type != IIO_LIGHT) {
0739 ret = -EINVAL;
0740 goto err_unlock;
0741 }
0742
0743 ret = tsl2591_read_channel_data(indio_dev, chan, val, val2);
0744 if (ret < 0)
0745 break;
0746
0747 ret = IIO_VAL_INT_PLUS_MICRO;
0748 break;
0749 case IIO_CHAN_INFO_INT_TIME:
0750 if (chan->type != IIO_INTENSITY) {
0751 ret = -EINVAL;
0752 goto err_unlock;
0753 }
0754
0755 *val = TSL2591_FVAL_TO_SEC(chip->als_settings.als_int_time);
0756 ret = IIO_VAL_INT;
0757 break;
0758 case IIO_CHAN_INFO_CALIBSCALE:
0759 if (chan->type != IIO_INTENSITY) {
0760 ret = -EINVAL;
0761 goto err_unlock;
0762 }
0763
0764 *val = tsl2591_gain_to_multiplier(chip->als_settings.als_gain);
0765 ret = IIO_VAL_INT;
0766 break;
0767 default:
0768 ret = -EINVAL;
0769 break;
0770 }
0771
0772 err_unlock:
0773 mutex_unlock(&chip->als_mutex);
0774
0775 pm_runtime_mark_last_busy(&client->dev);
0776 pm_runtime_put_autosuspend(&client->dev);
0777
0778 return ret;
0779 }
0780
0781 static int tsl2591_write_raw(struct iio_dev *indio_dev,
0782 struct iio_chan_spec const *chan,
0783 int val, int val2, long mask)
0784 {
0785 struct tsl2591_chip *chip = iio_priv(indio_dev);
0786 int int_time;
0787 int gain;
0788 int ret;
0789
0790 mutex_lock(&chip->als_mutex);
0791
0792 switch (mask) {
0793 case IIO_CHAN_INFO_INT_TIME:
0794 int_time = tsl2591_als_time_to_fval(val);
0795 if (int_time < 0) {
0796 ret = int_time;
0797 goto err_unlock;
0798 }
0799 ret = tsl2591_compatible_int_time(chip, int_time);
0800 if (ret < 0)
0801 goto err_unlock;
0802
0803 chip->als_settings.als_int_time = int_time;
0804 break;
0805 case IIO_CHAN_INFO_CALIBSCALE:
0806 gain = tsl2591_multiplier_to_gain(val);
0807 if (gain < 0) {
0808 ret = gain;
0809 goto err_unlock;
0810 }
0811 ret = tsl2591_compatible_gain(chip, gain);
0812 if (ret < 0)
0813 goto err_unlock;
0814
0815 chip->als_settings.als_gain = gain;
0816 break;
0817 default:
0818 ret = -EINVAL;
0819 goto err_unlock;
0820 }
0821
0822 ret = tsl2591_set_als_gain_int_time(chip);
0823
0824 err_unlock:
0825 mutex_unlock(&chip->als_mutex);
0826 return ret;
0827 }
0828
0829 static int tsl2591_read_available(struct iio_dev *indio_dev,
0830 struct iio_chan_spec const *chan,
0831 const int **vals, int *type, int *length,
0832 long mask)
0833 {
0834 switch (mask) {
0835 case IIO_CHAN_INFO_INT_TIME:
0836 *length = ARRAY_SIZE(tsl2591_int_time_available);
0837 *vals = tsl2591_int_time_available;
0838 *type = IIO_VAL_INT;
0839 return IIO_AVAIL_LIST;
0840
0841 case IIO_CHAN_INFO_CALIBSCALE:
0842 *length = ARRAY_SIZE(tsl2591_calibscale_available);
0843 *vals = tsl2591_calibscale_available;
0844 *type = IIO_VAL_INT;
0845 return IIO_AVAIL_LIST;
0846 default:
0847 return -EINVAL;
0848 }
0849 }
0850
0851 static int tsl2591_read_event_value(struct iio_dev *indio_dev,
0852 const struct iio_chan_spec *chan,
0853 enum iio_event_type type,
0854 enum iio_event_direction dir,
0855 enum iio_event_info info, int *val,
0856 int *val2)
0857 {
0858 struct tsl2591_chip *chip = iio_priv(indio_dev);
0859 struct i2c_client *client = chip->client;
0860 int als_persist, int_time, period;
0861 int ret;
0862
0863 mutex_lock(&chip->als_mutex);
0864
0865 switch (info) {
0866 case IIO_EV_INFO_VALUE:
0867 switch (dir) {
0868 case IIO_EV_DIR_RISING:
0869 *val = chip->als_settings.als_upper_thresh;
0870 break;
0871 case IIO_EV_DIR_FALLING:
0872 *val = chip->als_settings.als_lower_thresh;
0873 break;
0874 default:
0875 ret = -EINVAL;
0876 goto err_unlock;
0877 }
0878 ret = IIO_VAL_INT;
0879 break;
0880 case IIO_EV_INFO_PERIOD:
0881 ret = i2c_smbus_read_byte_data(client,
0882 TSL2591_CMD_NOP | TSL2591_PERSIST);
0883 if (ret <= 0 || ret > TSL2591_PRST_ALS_INT_CYCLE_MAX)
0884 goto err_unlock;
0885
0886 als_persist = tsl2591_persist_cycle_to_lit(ret);
0887 int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time);
0888 period = als_persist * (int_time * MSEC_PER_SEC);
0889
0890 *val = period / USEC_PER_SEC;
0891 *val2 = period % USEC_PER_SEC;
0892
0893 ret = IIO_VAL_INT_PLUS_MICRO;
0894 break;
0895 default:
0896 ret = -EINVAL;
0897 break;
0898 }
0899
0900 err_unlock:
0901 mutex_unlock(&chip->als_mutex);
0902 return ret;
0903 }
0904
0905 static int tsl2591_write_event_value(struct iio_dev *indio_dev,
0906 const struct iio_chan_spec *chan,
0907 enum iio_event_type type,
0908 enum iio_event_direction dir,
0909 enum iio_event_info info, int val,
0910 int val2)
0911 {
0912 struct tsl2591_chip *chip = iio_priv(indio_dev);
0913 int period, int_time, als_persist;
0914 int ret;
0915
0916 if (val < 0 || val2 < 0)
0917 return -EINVAL;
0918
0919 mutex_lock(&chip->als_mutex);
0920
0921 switch (info) {
0922 case IIO_EV_INFO_VALUE:
0923 if (val > TSL2591_ALS_MAX_VALUE) {
0924 ret = -EINVAL;
0925 goto err_unlock;
0926 }
0927
0928 switch (dir) {
0929 case IIO_EV_DIR_RISING:
0930 ret = tsl2591_set_als_upper_threshold(chip, val);
0931 if (ret < 0)
0932 goto err_unlock;
0933 break;
0934 case IIO_EV_DIR_FALLING:
0935 ret = tsl2591_set_als_lower_threshold(chip, val);
0936 if (ret < 0)
0937 goto err_unlock;
0938 break;
0939 default:
0940 ret = -EINVAL;
0941 goto err_unlock;
0942 }
0943 break;
0944 case IIO_EV_INFO_PERIOD:
0945 int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time);
0946
0947 period = ((val * MSEC_PER_SEC) +
0948 (val2 / MSEC_PER_SEC)) / int_time;
0949
0950 als_persist = tsl2591_persist_lit_to_cycle(period);
0951 if (als_persist < 0) {
0952 ret = -EINVAL;
0953 goto err_unlock;
0954 }
0955
0956 ret = tsl2591_compatible_als_persist_cycle(chip, als_persist);
0957 if (ret < 0)
0958 goto err_unlock;
0959
0960 ret = tsl2591_set_als_persist_cycle(chip, als_persist);
0961 if (ret < 0)
0962 goto err_unlock;
0963 break;
0964 default:
0965 ret = -EINVAL;
0966 break;
0967 }
0968
0969 err_unlock:
0970 mutex_unlock(&chip->als_mutex);
0971 return ret;
0972 }
0973
0974 static int tsl2591_read_event_config(struct iio_dev *indio_dev,
0975 const struct iio_chan_spec *chan,
0976 enum iio_event_type type,
0977 enum iio_event_direction dir)
0978 {
0979 struct tsl2591_chip *chip = iio_priv(indio_dev);
0980
0981 return chip->events_enabled;
0982 }
0983
0984 static int tsl2591_write_event_config(struct iio_dev *indio_dev,
0985 const struct iio_chan_spec *chan,
0986 enum iio_event_type type,
0987 enum iio_event_direction dir,
0988 int state)
0989 {
0990 struct tsl2591_chip *chip = iio_priv(indio_dev);
0991 struct i2c_client *client = chip->client;
0992
0993 if (state && !chip->events_enabled) {
0994 chip->events_enabled = true;
0995 pm_runtime_get_sync(&client->dev);
0996 } else if (!state && chip->events_enabled) {
0997 chip->events_enabled = false;
0998 pm_runtime_mark_last_busy(&client->dev);
0999 pm_runtime_put_autosuspend(&client->dev);
1000 }
1001
1002 return 0;
1003 }
1004
1005 static const struct iio_info tsl2591_info = {
1006 .event_attrs = &tsl2591_event_attribute_group,
1007 .read_raw = tsl2591_read_raw,
1008 .write_raw = tsl2591_write_raw,
1009 .read_avail = tsl2591_read_available,
1010 .read_event_value = tsl2591_read_event_value,
1011 .write_event_value = tsl2591_write_event_value,
1012 .read_event_config = tsl2591_read_event_config,
1013 .write_event_config = tsl2591_write_event_config,
1014 };
1015
1016 static const struct iio_info tsl2591_info_no_irq = {
1017 .read_raw = tsl2591_read_raw,
1018 .write_raw = tsl2591_write_raw,
1019 .read_avail = tsl2591_read_available,
1020 };
1021
1022 static int tsl2591_suspend(struct device *dev)
1023 {
1024 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1025 struct tsl2591_chip *chip = iio_priv(indio_dev);
1026 int ret;
1027
1028 mutex_lock(&chip->als_mutex);
1029 ret = tsl2591_set_power_state(chip, TSL2591_PWR_OFF);
1030 mutex_unlock(&chip->als_mutex);
1031
1032 return ret;
1033 }
1034
1035 static int tsl2591_resume(struct device *dev)
1036 {
1037 int power_state = TSL2591_PWR_ON | TSL2591_ENABLE_ALS;
1038 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1039 struct tsl2591_chip *chip = iio_priv(indio_dev);
1040 int ret;
1041
1042 if (chip->events_enabled)
1043 power_state |= TSL2591_ENABLE_ALS_INT;
1044
1045 mutex_lock(&chip->als_mutex);
1046 ret = tsl2591_set_power_state(chip, power_state);
1047 mutex_unlock(&chip->als_mutex);
1048
1049 return ret;
1050 }
1051
1052 static DEFINE_RUNTIME_DEV_PM_OPS(tsl2591_pm_ops, tsl2591_suspend,
1053 tsl2591_resume, NULL);
1054
1055 static irqreturn_t tsl2591_event_handler(int irq, void *private)
1056 {
1057 struct iio_dev *dev_info = private;
1058 struct tsl2591_chip *chip = iio_priv(dev_info);
1059 struct i2c_client *client = chip->client;
1060
1061 if (!chip->events_enabled)
1062 return IRQ_NONE;
1063
1064 iio_push_event(dev_info,
1065 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
1066 IIO_EV_TYPE_THRESH,
1067 IIO_EV_DIR_EITHER),
1068 iio_get_time_ns(dev_info));
1069
1070
1071 i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI);
1072
1073 return IRQ_HANDLED;
1074 }
1075
1076 static int tsl2591_load_defaults(struct tsl2591_chip *chip)
1077 {
1078 int ret;
1079
1080 chip->als_settings.als_int_time = TSL2591_DEFAULT_ALS_INT_TIME;
1081 chip->als_settings.als_gain = TSL2591_DEFAULT_ALS_GAIN;
1082 chip->als_settings.als_lower_thresh = TSL2591_DEFAULT_ALS_LOWER_THRESH;
1083 chip->als_settings.als_upper_thresh = TSL2591_DEFAULT_ALS_UPPER_THRESH;
1084
1085 ret = tsl2591_set_als_gain_int_time(chip);
1086 if (ret < 0)
1087 return ret;
1088
1089 ret = tsl2591_set_als_persist_cycle(chip, TSL2591_DEFAULT_ALS_PERSIST);
1090 if (ret < 0)
1091 return ret;
1092
1093 ret = tsl2591_set_als_lower_threshold(chip, TSL2591_DEFAULT_ALS_LOWER_THRESH);
1094 if (ret < 0)
1095 return ret;
1096
1097 ret = tsl2591_set_als_upper_threshold(chip, TSL2591_DEFAULT_ALS_UPPER_THRESH);
1098 if (ret < 0)
1099 return ret;
1100
1101 return 0;
1102 }
1103
1104 static void tsl2591_chip_off(void *data)
1105 {
1106 struct iio_dev *indio_dev = data;
1107 struct tsl2591_chip *chip = iio_priv(indio_dev);
1108 struct i2c_client *client = chip->client;
1109
1110 pm_runtime_disable(&client->dev);
1111 pm_runtime_set_suspended(&client->dev);
1112 pm_runtime_put_noidle(&client->dev);
1113
1114 tsl2591_set_power_state(chip, TSL2591_PWR_OFF);
1115 }
1116
1117 static int tsl2591_probe(struct i2c_client *client)
1118 {
1119 struct tsl2591_chip *chip;
1120 struct iio_dev *indio_dev;
1121 int ret;
1122
1123 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1124 dev_err(&client->dev,
1125 "I2C smbus byte data functionality is not supported\n");
1126 return -EOPNOTSUPP;
1127 }
1128
1129 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
1130 if (!indio_dev)
1131 return -ENOMEM;
1132
1133 chip = iio_priv(indio_dev);
1134 chip->client = client;
1135 i2c_set_clientdata(client, indio_dev);
1136
1137 if (client->irq) {
1138 ret = devm_request_threaded_irq(&client->dev, client->irq,
1139 NULL, tsl2591_event_handler,
1140 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1141 "tsl2591_irq", indio_dev);
1142 if (ret) {
1143 dev_err_probe(&client->dev, ret, "IRQ request error\n");
1144 return -EINVAL;
1145 }
1146 indio_dev->info = &tsl2591_info;
1147 } else {
1148 indio_dev->info = &tsl2591_info_no_irq;
1149 }
1150
1151 mutex_init(&chip->als_mutex);
1152
1153 ret = i2c_smbus_read_byte_data(client,
1154 TSL2591_CMD_NOP | TSL2591_DEVICE_ID);
1155 if (ret < 0) {
1156 dev_err(&client->dev,
1157 "Failed to read the device ID register\n");
1158 return ret;
1159 }
1160 ret = FIELD_GET(TSL2591_DEVICE_ID_MASK, ret);
1161 if (ret != TSL2591_DEVICE_ID_VAL) {
1162 dev_err(&client->dev, "Device ID: %#04x unknown\n", ret);
1163 return -EINVAL;
1164 }
1165
1166 indio_dev->channels = tsl2591_channels;
1167 indio_dev->num_channels = ARRAY_SIZE(tsl2591_channels);
1168 indio_dev->modes = INDIO_DIRECT_MODE;
1169 indio_dev->name = chip->client->name;
1170 chip->events_enabled = false;
1171
1172 pm_runtime_enable(&client->dev);
1173 pm_runtime_set_autosuspend_delay(&client->dev,
1174 TSL2591_POWER_OFF_DELAY_MS);
1175 pm_runtime_use_autosuspend(&client->dev);
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185 ret = devm_add_action_or_reset(&client->dev, tsl2591_chip_off,
1186 indio_dev);
1187 if (ret < 0)
1188 return -EINVAL;
1189
1190 ret = tsl2591_load_defaults(chip);
1191 if (ret < 0) {
1192 dev_err(&client->dev, "Failed to load sensor defaults\n");
1193 return -EINVAL;
1194 }
1195
1196 ret = i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI);
1197 if (ret < 0) {
1198 dev_err(&client->dev, "Failed to clear als irq\n");
1199 return -EINVAL;
1200 }
1201
1202 return devm_iio_device_register(&client->dev, indio_dev);
1203 }
1204
1205 static const struct of_device_id tsl2591_of_match[] = {
1206 { .compatible = "amstaos,tsl2591"},
1207 {}
1208 };
1209 MODULE_DEVICE_TABLE(of, tsl2591_of_match);
1210
1211 static struct i2c_driver tsl2591_driver = {
1212 .driver = {
1213 .name = "tsl2591",
1214 .pm = pm_ptr(&tsl2591_pm_ops),
1215 .of_match_table = tsl2591_of_match,
1216 },
1217 .probe_new = tsl2591_probe
1218 };
1219 module_i2c_driver(tsl2591_driver);
1220
1221 MODULE_AUTHOR("Joe Sandom <joe.g.sandom@gmail.com>");
1222 MODULE_DESCRIPTION("TAOS tsl2591 ambient light sensor driver");
1223 MODULE_LICENSE("GPL");