1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ltr501.c - Support for Lite-On LTR501 ambient light and proximity sensor 4 * 5 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net> 6 * 7 * 7-bit I2C slave address 0x23 8 * 9 * TODO: IR LED characteristics 10 */ 11 12 #include <linux/module.h> 13 #include <linux/i2c.h> 14 #include <linux/err.h> 15 #include <linux/delay.h> 16 #include <linux/regmap.h> 17 #include <linux/acpi.h> 18 #include <linux/regulator/consumer.h> 19 20 #include <linux/iio/iio.h> 21 #include <linux/iio/events.h> 22 #include <linux/iio/sysfs.h> 23 #include <linux/iio/trigger_consumer.h> 24 #include <linux/iio/buffer.h> 25 #include <linux/iio/triggered_buffer.h> 26 27 #define LTR501_DRV_NAME "ltr501" 28 29 #define LTR501_ALS_CONTR 0x80 /* ALS operation mode, SW reset */ 30 #define LTR501_PS_CONTR 0x81 /* PS operation mode */ 31 #define LTR501_PS_MEAS_RATE 0x84 /* measurement rate*/ 32 #define LTR501_ALS_MEAS_RATE 0x85 /* ALS integ time, measurement rate*/ 33 #define LTR501_PART_ID 0x86 34 #define LTR501_MANUFAC_ID 0x87 35 #define LTR501_ALS_DATA1 0x88 /* 16-bit, little endian */ 36 #define LTR501_ALS_DATA1_UPPER 0x89 /* upper 8 bits of LTR501_ALS_DATA1 */ 37 #define LTR501_ALS_DATA0 0x8a /* 16-bit, little endian */ 38 #define LTR501_ALS_DATA0_UPPER 0x8b /* upper 8 bits of LTR501_ALS_DATA0 */ 39 #define LTR501_ALS_PS_STATUS 0x8c 40 #define LTR501_PS_DATA 0x8d /* 16-bit, little endian */ 41 #define LTR501_PS_DATA_UPPER 0x8e /* upper 8 bits of LTR501_PS_DATA */ 42 #define LTR501_INTR 0x8f /* output mode, polarity, mode */ 43 #define LTR501_PS_THRESH_UP 0x90 /* 11 bit, ps upper threshold */ 44 #define LTR501_PS_THRESH_LOW 0x92 /* 11 bit, ps lower threshold */ 45 #define LTR501_ALS_THRESH_UP 0x97 /* 16 bit, ALS upper threshold */ 46 #define LTR501_ALS_THRESH_LOW 0x99 /* 16 bit, ALS lower threshold */ 47 #define LTR501_INTR_PRST 0x9e /* ps thresh, als thresh */ 48 #define LTR501_MAX_REG 0x9f 49 50 #define LTR501_ALS_CONTR_SW_RESET BIT(2) 51 #define LTR501_CONTR_PS_GAIN_MASK (BIT(3) | BIT(2)) 52 #define LTR501_CONTR_PS_GAIN_SHIFT 2 53 #define LTR501_CONTR_ALS_GAIN_MASK BIT(3) 54 #define LTR501_CONTR_ACTIVE BIT(1) 55 56 #define LTR501_STATUS_ALS_INTR BIT(3) 57 #define LTR501_STATUS_ALS_RDY BIT(2) 58 #define LTR501_STATUS_PS_INTR BIT(1) 59 #define LTR501_STATUS_PS_RDY BIT(0) 60 61 #define LTR501_PS_DATA_MASK 0x7ff 62 #define LTR501_PS_THRESH_MASK 0x7ff 63 #define LTR501_ALS_THRESH_MASK 0xffff 64 65 #define LTR501_ALS_DEF_PERIOD 500000 66 #define LTR501_PS_DEF_PERIOD 100000 67 68 #define LTR501_REGMAP_NAME "ltr501_regmap" 69 70 #define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \ 71 ((vis_coeff * vis_data) - (ir_coeff * ir_data)) 72 73 static const int int_time_mapping[] = {100000, 50000, 200000, 400000}; 74 75 static const struct reg_field reg_field_it = 76 REG_FIELD(LTR501_ALS_MEAS_RATE, 3, 4); 77 static const struct reg_field reg_field_als_intr = 78 REG_FIELD(LTR501_INTR, 1, 1); 79 static const struct reg_field reg_field_ps_intr = 80 REG_FIELD(LTR501_INTR, 0, 0); 81 static const struct reg_field reg_field_als_rate = 82 REG_FIELD(LTR501_ALS_MEAS_RATE, 0, 2); 83 static const struct reg_field reg_field_ps_rate = 84 REG_FIELD(LTR501_PS_MEAS_RATE, 0, 3); 85 static const struct reg_field reg_field_als_prst = 86 REG_FIELD(LTR501_INTR_PRST, 0, 3); 87 static const struct reg_field reg_field_ps_prst = 88 REG_FIELD(LTR501_INTR_PRST, 4, 7); 89 90 struct ltr501_samp_table { 91 int freq_val; /* repetition frequency in micro HZ*/ 92 int time_val; /* repetition rate in micro seconds */ 93 }; 94 95 #define LTR501_RESERVED_GAIN -1 96 97 enum { 98 ltr501 = 0, 99 ltr559, 100 ltr301, 101 }; 102 103 struct ltr501_gain { 104 int scale; 105 int uscale; 106 }; 107 108 static const struct ltr501_gain ltr501_als_gain_tbl[] = { 109 {1, 0}, 110 {0, 5000}, 111 }; 112 113 static const struct ltr501_gain ltr559_als_gain_tbl[] = { 114 {1, 0}, 115 {0, 500000}, 116 {0, 250000}, 117 {0, 125000}, 118 {LTR501_RESERVED_GAIN, LTR501_RESERVED_GAIN}, 119 {LTR501_RESERVED_GAIN, LTR501_RESERVED_GAIN}, 120 {0, 20000}, 121 {0, 10000}, 122 }; 123 124 static const struct ltr501_gain ltr501_ps_gain_tbl[] = { 125 {1, 0}, 126 {0, 250000}, 127 {0, 125000}, 128 {0, 62500}, 129 }; 130 131 static const struct ltr501_gain ltr559_ps_gain_tbl[] = { 132 {0, 62500}, /* x16 gain */ 133 {0, 31250}, /* x32 gain */ 134 {0, 15625}, /* bits X1 are for x64 gain */ 135 {0, 15624}, 136 }; 137 138 struct ltr501_chip_info { 139 u8 partid; 140 const struct ltr501_gain *als_gain; 141 int als_gain_tbl_size; 142 const struct ltr501_gain *ps_gain; 143 int ps_gain_tbl_size; 144 u8 als_mode_active; 145 u8 als_gain_mask; 146 u8 als_gain_shift; 147 struct iio_chan_spec const *channels; 148 const int no_channels; 149 const struct iio_info *info; 150 const struct iio_info *info_no_irq; 151 }; 152 153 struct ltr501_data { 154 struct i2c_client *client; 155 struct regulator_bulk_data regulators[2]; 156 struct mutex lock_als, lock_ps; 157 const struct ltr501_chip_info *chip_info; 158 u8 als_contr, ps_contr; 159 int als_period, ps_period; /* period in micro seconds */ 160 struct regmap *regmap; 161 struct regmap_field *reg_it; 162 struct regmap_field *reg_als_intr; 163 struct regmap_field *reg_ps_intr; 164 struct regmap_field *reg_als_rate; 165 struct regmap_field *reg_ps_rate; 166 struct regmap_field *reg_als_prst; 167 struct regmap_field *reg_ps_prst; 168 }; 169 170 static const struct ltr501_samp_table ltr501_als_samp_table[] = { 171 {20000000, 50000}, {10000000, 100000}, 172 {5000000, 200000}, {2000000, 500000}, 173 {1000000, 1000000}, {500000, 2000000}, 174 {500000, 2000000}, {500000, 2000000} 175 }; 176 177 static const struct ltr501_samp_table ltr501_ps_samp_table[] = { 178 {20000000, 50000}, {14285714, 70000}, 179 {10000000, 100000}, {5000000, 200000}, 180 {2000000, 500000}, {1000000, 1000000}, 181 {500000, 2000000}, {500000, 2000000}, 182 {500000, 2000000} 183 }; 184 185 static int ltr501_match_samp_freq(const struct ltr501_samp_table *tab, 186 int len, int val, int val2) 187 { 188 int i, freq; 189 190 freq = val * 1000000 + val2; 191 192 for (i = 0; i < len; i++) { 193 if (tab[i].freq_val == freq) 194 return i; 195 } 196 197 return -EINVAL; 198 } 199 200 static int ltr501_als_read_samp_freq(const struct ltr501_data *data, 201 int *val, int *val2) 202 { 203 int ret, i; 204 205 ret = regmap_field_read(data->reg_als_rate, &i); 206 if (ret < 0) 207 return ret; 208 209 if (i < 0 || i >= ARRAY_SIZE(ltr501_als_samp_table)) 210 return -EINVAL; 211 212 *val = ltr501_als_samp_table[i].freq_val / 1000000; 213 *val2 = ltr501_als_samp_table[i].freq_val % 1000000; 214 215 return IIO_VAL_INT_PLUS_MICRO; 216 } 217 218 static int ltr501_ps_read_samp_freq(const struct ltr501_data *data, 219 int *val, int *val2) 220 { 221 int ret, i; 222 223 ret = regmap_field_read(data->reg_ps_rate, &i); 224 if (ret < 0) 225 return ret; 226 227 if (i < 0 || i >= ARRAY_SIZE(ltr501_ps_samp_table)) 228 return -EINVAL; 229 230 *val = ltr501_ps_samp_table[i].freq_val / 1000000; 231 *val2 = ltr501_ps_samp_table[i].freq_val % 1000000; 232 233 return IIO_VAL_INT_PLUS_MICRO; 234 } 235 236 static int ltr501_als_write_samp_freq(struct ltr501_data *data, 237 int val, int val2) 238 { 239 int i, ret; 240 241 i = ltr501_match_samp_freq(ltr501_als_samp_table, 242 ARRAY_SIZE(ltr501_als_samp_table), 243 val, val2); 244 245 if (i < 0) 246 return i; 247 248 mutex_lock(&data->lock_als); 249 ret = regmap_field_write(data->reg_als_rate, i); 250 mutex_unlock(&data->lock_als); 251 252 return ret; 253 } 254 255 static int ltr501_ps_write_samp_freq(struct ltr501_data *data, 256 int val, int val2) 257 { 258 int i, ret; 259 260 i = ltr501_match_samp_freq(ltr501_ps_samp_table, 261 ARRAY_SIZE(ltr501_ps_samp_table), 262 val, val2); 263 264 if (i < 0) 265 return i; 266 267 mutex_lock(&data->lock_ps); 268 ret = regmap_field_write(data->reg_ps_rate, i); 269 mutex_unlock(&data->lock_ps); 270 271 return ret; 272 } 273 274 static int ltr501_als_read_samp_period(const struct ltr501_data *data, int *val) 275 { 276 int ret, i; 277 278 ret = regmap_field_read(data->reg_als_rate, &i); 279 if (ret < 0) 280 return ret; 281 282 if (i < 0 || i >= ARRAY_SIZE(ltr501_als_samp_table)) 283 return -EINVAL; 284 285 *val = ltr501_als_samp_table[i].time_val; 286 287 return IIO_VAL_INT; 288 } 289 290 static int ltr501_ps_read_samp_period(const struct ltr501_data *data, int *val) 291 { 292 int ret, i; 293 294 ret = regmap_field_read(data->reg_ps_rate, &i); 295 if (ret < 0) 296 return ret; 297 298 if (i < 0 || i >= ARRAY_SIZE(ltr501_ps_samp_table)) 299 return -EINVAL; 300 301 *val = ltr501_ps_samp_table[i].time_val; 302 303 return IIO_VAL_INT; 304 } 305 306 /* IR and visible spectrum coeff's are given in data sheet */ 307 static unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data) 308 { 309 unsigned long ratio, lux; 310 311 if (vis_data == 0) 312 return 0; 313 314 /* multiply numerator by 100 to avoid handling ratio < 1 */ 315 ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data); 316 317 if (ratio < 45) 318 lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data); 319 else if (ratio >= 45 && ratio < 64) 320 lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data); 321 else if (ratio >= 64 && ratio < 85) 322 lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data); 323 else 324 lux = 0; 325 326 return lux / 1000; 327 } 328 329 static int ltr501_drdy(const struct ltr501_data *data, u8 drdy_mask) 330 { 331 int tries = 100; 332 int ret, status; 333 334 while (tries--) { 335 ret = regmap_read(data->regmap, LTR501_ALS_PS_STATUS, &status); 336 if (ret < 0) 337 return ret; 338 if ((status & drdy_mask) == drdy_mask) 339 return 0; 340 msleep(25); 341 } 342 343 dev_err(&data->client->dev, "ltr501_drdy() failed, data not ready\n"); 344 return -EIO; 345 } 346 347 static int ltr501_set_it_time(struct ltr501_data *data, int it) 348 { 349 int ret, i, index = -1, status; 350 351 for (i = 0; i < ARRAY_SIZE(int_time_mapping); i++) { 352 if (int_time_mapping[i] == it) { 353 index = i; 354 break; 355 } 356 } 357 /* Make sure integ time index is valid */ 358 if (index < 0) 359 return -EINVAL; 360 361 ret = regmap_read(data->regmap, LTR501_ALS_CONTR, &status); 362 if (ret < 0) 363 return ret; 364 365 if (status & LTR501_CONTR_ALS_GAIN_MASK) { 366 /* 367 * 200 ms and 400 ms integ time can only be 368 * used in dynamic range 1 369 */ 370 if (index > 1) 371 return -EINVAL; 372 } else 373 /* 50 ms integ time can only be used in dynamic range 2 */ 374 if (index == 1) 375 return -EINVAL; 376 377 return regmap_field_write(data->reg_it, index); 378 } 379 380 /* read int time in micro seconds */ 381 static int ltr501_read_it_time(const struct ltr501_data *data, 382 int *val, int *val2) 383 { 384 int ret, index; 385 386 ret = regmap_field_read(data->reg_it, &index); 387 if (ret < 0) 388 return ret; 389 390 /* Make sure integ time index is valid */ 391 if (index < 0 || index >= ARRAY_SIZE(int_time_mapping)) 392 return -EINVAL; 393 394 *val2 = int_time_mapping[index]; 395 *val = 0; 396 397 return IIO_VAL_INT_PLUS_MICRO; 398 } 399 400 static int ltr501_read_als(const struct ltr501_data *data, __le16 buf[2]) 401 { 402 int ret; 403 404 ret = ltr501_drdy(data, LTR501_STATUS_ALS_RDY); 405 if (ret < 0) 406 return ret; 407 /* always read both ALS channels in given order */ 408 return regmap_bulk_read(data->regmap, LTR501_ALS_DATA1, 409 buf, 2 * sizeof(__le16)); 410 } 411 412 static int ltr501_read_ps(const struct ltr501_data *data) 413 { 414 __le16 status; 415 int ret; 416 417 ret = ltr501_drdy(data, LTR501_STATUS_PS_RDY); 418 if (ret < 0) 419 return ret; 420 421 ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA, 422 &status, sizeof(status)); 423 if (ret < 0) 424 return ret; 425 426 return le16_to_cpu(status); 427 } 428 429 static int ltr501_read_intr_prst(const struct ltr501_data *data, 430 enum iio_chan_type type, 431 int *val2) 432 { 433 int ret, samp_period, prst; 434 435 switch (type) { 436 case IIO_INTENSITY: 437 ret = regmap_field_read(data->reg_als_prst, &prst); 438 if (ret < 0) 439 return ret; 440 441 ret = ltr501_als_read_samp_period(data, &samp_period); 442 443 if (ret < 0) 444 return ret; 445 *val2 = samp_period * prst; 446 return IIO_VAL_INT_PLUS_MICRO; 447 case IIO_PROXIMITY: 448 ret = regmap_field_read(data->reg_ps_prst, &prst); 449 if (ret < 0) 450 return ret; 451 452 ret = ltr501_ps_read_samp_period(data, &samp_period); 453 454 if (ret < 0) 455 return ret; 456 457 *val2 = samp_period * prst; 458 return IIO_VAL_INT_PLUS_MICRO; 459 default: 460 return -EINVAL; 461 } 462 463 return -EINVAL; 464 } 465 466 static int ltr501_write_intr_prst(struct ltr501_data *data, 467 enum iio_chan_type type, 468 int val, int val2) 469 { 470 int ret, samp_period, new_val; 471 unsigned long period; 472 473 if (val < 0 || val2 < 0) 474 return -EINVAL; 475 476 /* period in microseconds */ 477 period = ((val * 1000000) + val2); 478 479 switch (type) { 480 case IIO_INTENSITY: 481 ret = ltr501_als_read_samp_period(data, &samp_period); 482 if (ret < 0) 483 return ret; 484 485 /* period should be atleast equal to sampling period */ 486 if (period < samp_period) 487 return -EINVAL; 488 489 new_val = DIV_ROUND_UP(period, samp_period); 490 if (new_val < 0 || new_val > 0x0f) 491 return -EINVAL; 492 493 mutex_lock(&data->lock_als); 494 ret = regmap_field_write(data->reg_als_prst, new_val); 495 mutex_unlock(&data->lock_als); 496 if (ret >= 0) 497 data->als_period = period; 498 499 return ret; 500 case IIO_PROXIMITY: 501 ret = ltr501_ps_read_samp_period(data, &samp_period); 502 if (ret < 0) 503 return ret; 504 505 /* period should be atleast equal to rate */ 506 if (period < samp_period) 507 return -EINVAL; 508 509 new_val = DIV_ROUND_UP(period, samp_period); 510 if (new_val < 0 || new_val > 0x0f) 511 return -EINVAL; 512 513 mutex_lock(&data->lock_ps); 514 ret = regmap_field_write(data->reg_ps_prst, new_val); 515 mutex_unlock(&data->lock_ps); 516 if (ret >= 0) 517 data->ps_period = period; 518 519 return ret; 520 default: 521 return -EINVAL; 522 } 523 524 return -EINVAL; 525 } 526 527 static const struct iio_event_spec ltr501_als_event_spec[] = { 528 { 529 .type = IIO_EV_TYPE_THRESH, 530 .dir = IIO_EV_DIR_RISING, 531 .mask_separate = BIT(IIO_EV_INFO_VALUE), 532 }, { 533 .type = IIO_EV_TYPE_THRESH, 534 .dir = IIO_EV_DIR_FALLING, 535 .mask_separate = BIT(IIO_EV_INFO_VALUE), 536 }, { 537 .type = IIO_EV_TYPE_THRESH, 538 .dir = IIO_EV_DIR_EITHER, 539 .mask_separate = BIT(IIO_EV_INFO_ENABLE) | 540 BIT(IIO_EV_INFO_PERIOD), 541 }, 542 543 }; 544 545 static const struct iio_event_spec ltr501_pxs_event_spec[] = { 546 { 547 .type = IIO_EV_TYPE_THRESH, 548 .dir = IIO_EV_DIR_RISING, 549 .mask_separate = BIT(IIO_EV_INFO_VALUE), 550 }, { 551 .type = IIO_EV_TYPE_THRESH, 552 .dir = IIO_EV_DIR_FALLING, 553 .mask_separate = BIT(IIO_EV_INFO_VALUE), 554 }, { 555 .type = IIO_EV_TYPE_THRESH, 556 .dir = IIO_EV_DIR_EITHER, 557 .mask_separate = BIT(IIO_EV_INFO_ENABLE) | 558 BIT(IIO_EV_INFO_PERIOD), 559 }, 560 }; 561 562 #define LTR501_INTENSITY_CHANNEL(_idx, _addr, _mod, _shared, \ 563 _evspec, _evsize) { \ 564 .type = IIO_INTENSITY, \ 565 .modified = 1, \ 566 .address = (_addr), \ 567 .channel2 = (_mod), \ 568 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 569 .info_mask_shared_by_type = (_shared), \ 570 .scan_index = (_idx), \ 571 .scan_type = { \ 572 .sign = 'u', \ 573 .realbits = 16, \ 574 .storagebits = 16, \ 575 .endianness = IIO_CPU, \ 576 }, \ 577 .event_spec = _evspec,\ 578 .num_event_specs = _evsize,\ 579 } 580 581 #define LTR501_LIGHT_CHANNEL() { \ 582 .type = IIO_LIGHT, \ 583 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ 584 .scan_index = -1, \ 585 } 586 587 static const struct iio_chan_spec ltr501_channels[] = { 588 LTR501_LIGHT_CHANNEL(), 589 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0, 590 ltr501_als_event_spec, 591 ARRAY_SIZE(ltr501_als_event_spec)), 592 LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1, IIO_MOD_LIGHT_IR, 593 BIT(IIO_CHAN_INFO_SCALE) | 594 BIT(IIO_CHAN_INFO_INT_TIME) | 595 BIT(IIO_CHAN_INFO_SAMP_FREQ), 596 NULL, 0), 597 { 598 .type = IIO_PROXIMITY, 599 .address = LTR501_PS_DATA, 600 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 601 BIT(IIO_CHAN_INFO_SCALE), 602 .scan_index = 2, 603 .scan_type = { 604 .sign = 'u', 605 .realbits = 11, 606 .storagebits = 16, 607 .endianness = IIO_CPU, 608 }, 609 .event_spec = ltr501_pxs_event_spec, 610 .num_event_specs = ARRAY_SIZE(ltr501_pxs_event_spec), 611 }, 612 IIO_CHAN_SOFT_TIMESTAMP(3), 613 }; 614 615 static const struct iio_chan_spec ltr301_channels[] = { 616 LTR501_LIGHT_CHANNEL(), 617 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0, 618 ltr501_als_event_spec, 619 ARRAY_SIZE(ltr501_als_event_spec)), 620 LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1, IIO_MOD_LIGHT_IR, 621 BIT(IIO_CHAN_INFO_SCALE) | 622 BIT(IIO_CHAN_INFO_INT_TIME) | 623 BIT(IIO_CHAN_INFO_SAMP_FREQ), 624 NULL, 0), 625 IIO_CHAN_SOFT_TIMESTAMP(2), 626 }; 627 628 static int ltr501_read_raw(struct iio_dev *indio_dev, 629 struct iio_chan_spec const *chan, 630 int *val, int *val2, long mask) 631 { 632 struct ltr501_data *data = iio_priv(indio_dev); 633 __le16 buf[2]; 634 int ret, i; 635 636 switch (mask) { 637 case IIO_CHAN_INFO_PROCESSED: 638 switch (chan->type) { 639 case IIO_LIGHT: 640 ret = iio_device_claim_direct_mode(indio_dev); 641 if (ret) 642 return ret; 643 644 mutex_lock(&data->lock_als); 645 ret = ltr501_read_als(data, buf); 646 mutex_unlock(&data->lock_als); 647 iio_device_release_direct_mode(indio_dev); 648 if (ret < 0) 649 return ret; 650 *val = ltr501_calculate_lux(le16_to_cpu(buf[1]), 651 le16_to_cpu(buf[0])); 652 return IIO_VAL_INT; 653 default: 654 return -EINVAL; 655 } 656 case IIO_CHAN_INFO_RAW: 657 ret = iio_device_claim_direct_mode(indio_dev); 658 if (ret) 659 return ret; 660 661 switch (chan->type) { 662 case IIO_INTENSITY: 663 mutex_lock(&data->lock_als); 664 ret = ltr501_read_als(data, buf); 665 mutex_unlock(&data->lock_als); 666 if (ret < 0) 667 break; 668 *val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ? 669 buf[0] : buf[1]); 670 ret = IIO_VAL_INT; 671 break; 672 case IIO_PROXIMITY: 673 mutex_lock(&data->lock_ps); 674 ret = ltr501_read_ps(data); 675 mutex_unlock(&data->lock_ps); 676 if (ret < 0) 677 break; 678 *val = ret & LTR501_PS_DATA_MASK; 679 ret = IIO_VAL_INT; 680 break; 681 default: 682 ret = -EINVAL; 683 break; 684 } 685 686 iio_device_release_direct_mode(indio_dev); 687 return ret; 688 689 case IIO_CHAN_INFO_SCALE: 690 switch (chan->type) { 691 case IIO_INTENSITY: 692 i = (data->als_contr & data->chip_info->als_gain_mask) 693 >> data->chip_info->als_gain_shift; 694 *val = data->chip_info->als_gain[i].scale; 695 *val2 = data->chip_info->als_gain[i].uscale; 696 return IIO_VAL_INT_PLUS_MICRO; 697 case IIO_PROXIMITY: 698 i = (data->ps_contr & LTR501_CONTR_PS_GAIN_MASK) >> 699 LTR501_CONTR_PS_GAIN_SHIFT; 700 *val = data->chip_info->ps_gain[i].scale; 701 *val2 = data->chip_info->ps_gain[i].uscale; 702 return IIO_VAL_INT_PLUS_MICRO; 703 default: 704 return -EINVAL; 705 } 706 case IIO_CHAN_INFO_INT_TIME: 707 switch (chan->type) { 708 case IIO_INTENSITY: 709 return ltr501_read_it_time(data, val, val2); 710 default: 711 return -EINVAL; 712 } 713 case IIO_CHAN_INFO_SAMP_FREQ: 714 switch (chan->type) { 715 case IIO_INTENSITY: 716 return ltr501_als_read_samp_freq(data, val, val2); 717 case IIO_PROXIMITY: 718 return ltr501_ps_read_samp_freq(data, val, val2); 719 default: 720 return -EINVAL; 721 } 722 } 723 return -EINVAL; 724 } 725 726 static int ltr501_get_gain_index(const struct ltr501_gain *gain, int size, 727 int val, int val2) 728 { 729 int i; 730 731 for (i = 0; i < size; i++) 732 if (val == gain[i].scale && val2 == gain[i].uscale) 733 return i; 734 735 return -1; 736 } 737 738 static int ltr501_write_raw(struct iio_dev *indio_dev, 739 struct iio_chan_spec const *chan, 740 int val, int val2, long mask) 741 { 742 struct ltr501_data *data = iio_priv(indio_dev); 743 int i, ret, freq_val, freq_val2; 744 const struct ltr501_chip_info *info = data->chip_info; 745 746 ret = iio_device_claim_direct_mode(indio_dev); 747 if (ret) 748 return ret; 749 750 switch (mask) { 751 case IIO_CHAN_INFO_SCALE: 752 switch (chan->type) { 753 case IIO_INTENSITY: 754 i = ltr501_get_gain_index(info->als_gain, 755 info->als_gain_tbl_size, 756 val, val2); 757 if (i < 0) { 758 ret = -EINVAL; 759 break; 760 } 761 762 data->als_contr &= ~info->als_gain_mask; 763 data->als_contr |= i << info->als_gain_shift; 764 765 ret = regmap_write(data->regmap, LTR501_ALS_CONTR, 766 data->als_contr); 767 break; 768 case IIO_PROXIMITY: 769 i = ltr501_get_gain_index(info->ps_gain, 770 info->ps_gain_tbl_size, 771 val, val2); 772 if (i < 0) { 773 ret = -EINVAL; 774 break; 775 } 776 data->ps_contr &= ~LTR501_CONTR_PS_GAIN_MASK; 777 data->ps_contr |= i << LTR501_CONTR_PS_GAIN_SHIFT; 778 779 ret = regmap_write(data->regmap, LTR501_PS_CONTR, 780 data->ps_contr); 781 break; 782 default: 783 ret = -EINVAL; 784 break; 785 } 786 break; 787 788 case IIO_CHAN_INFO_INT_TIME: 789 switch (chan->type) { 790 case IIO_INTENSITY: 791 if (val != 0) { 792 ret = -EINVAL; 793 break; 794 } 795 mutex_lock(&data->lock_als); 796 ret = ltr501_set_it_time(data, val2); 797 mutex_unlock(&data->lock_als); 798 break; 799 default: 800 ret = -EINVAL; 801 break; 802 } 803 break; 804 805 case IIO_CHAN_INFO_SAMP_FREQ: 806 switch (chan->type) { 807 case IIO_INTENSITY: 808 ret = ltr501_als_read_samp_freq(data, &freq_val, 809 &freq_val2); 810 if (ret < 0) 811 break; 812 813 ret = ltr501_als_write_samp_freq(data, val, val2); 814 if (ret < 0) 815 break; 816 817 /* update persistence count when changing frequency */ 818 ret = ltr501_write_intr_prst(data, chan->type, 819 0, data->als_period); 820 821 if (ret < 0) 822 ret = ltr501_als_write_samp_freq(data, freq_val, 823 freq_val2); 824 break; 825 case IIO_PROXIMITY: 826 ret = ltr501_ps_read_samp_freq(data, &freq_val, 827 &freq_val2); 828 if (ret < 0) 829 break; 830 831 ret = ltr501_ps_write_samp_freq(data, val, val2); 832 if (ret < 0) 833 break; 834 835 /* update persistence count when changing frequency */ 836 ret = ltr501_write_intr_prst(data, chan->type, 837 0, data->ps_period); 838 839 if (ret < 0) 840 ret = ltr501_ps_write_samp_freq(data, freq_val, 841 freq_val2); 842 break; 843 default: 844 ret = -EINVAL; 845 break; 846 } 847 break; 848 849 default: 850 ret = -EINVAL; 851 break; 852 } 853 854 iio_device_release_direct_mode(indio_dev); 855 return ret; 856 } 857 858 static int ltr501_read_thresh(const struct iio_dev *indio_dev, 859 const struct iio_chan_spec *chan, 860 enum iio_event_type type, 861 enum iio_event_direction dir, 862 enum iio_event_info info, 863 int *val, int *val2) 864 { 865 const struct ltr501_data *data = iio_priv(indio_dev); 866 int ret, thresh_data; 867 868 switch (chan->type) { 869 case IIO_INTENSITY: 870 switch (dir) { 871 case IIO_EV_DIR_RISING: 872 ret = regmap_bulk_read(data->regmap, 873 LTR501_ALS_THRESH_UP, 874 &thresh_data, 2); 875 if (ret < 0) 876 return ret; 877 *val = thresh_data & LTR501_ALS_THRESH_MASK; 878 return IIO_VAL_INT; 879 case IIO_EV_DIR_FALLING: 880 ret = regmap_bulk_read(data->regmap, 881 LTR501_ALS_THRESH_LOW, 882 &thresh_data, 2); 883 if (ret < 0) 884 return ret; 885 *val = thresh_data & LTR501_ALS_THRESH_MASK; 886 return IIO_VAL_INT; 887 default: 888 return -EINVAL; 889 } 890 case IIO_PROXIMITY: 891 switch (dir) { 892 case IIO_EV_DIR_RISING: 893 ret = regmap_bulk_read(data->regmap, 894 LTR501_PS_THRESH_UP, 895 &thresh_data, 2); 896 if (ret < 0) 897 return ret; 898 *val = thresh_data & LTR501_PS_THRESH_MASK; 899 return IIO_VAL_INT; 900 case IIO_EV_DIR_FALLING: 901 ret = regmap_bulk_read(data->regmap, 902 LTR501_PS_THRESH_LOW, 903 &thresh_data, 2); 904 if (ret < 0) 905 return ret; 906 *val = thresh_data & LTR501_PS_THRESH_MASK; 907 return IIO_VAL_INT; 908 default: 909 return -EINVAL; 910 } 911 default: 912 return -EINVAL; 913 } 914 915 return -EINVAL; 916 } 917 918 static int ltr501_write_thresh(struct iio_dev *indio_dev, 919 const struct iio_chan_spec *chan, 920 enum iio_event_type type, 921 enum iio_event_direction dir, 922 enum iio_event_info info, 923 int val, int val2) 924 { 925 struct ltr501_data *data = iio_priv(indio_dev); 926 int ret; 927 928 if (val < 0) 929 return -EINVAL; 930 931 switch (chan->type) { 932 case IIO_INTENSITY: 933 if (val > LTR501_ALS_THRESH_MASK) 934 return -EINVAL; 935 switch (dir) { 936 case IIO_EV_DIR_RISING: 937 mutex_lock(&data->lock_als); 938 ret = regmap_bulk_write(data->regmap, 939 LTR501_ALS_THRESH_UP, 940 &val, 2); 941 mutex_unlock(&data->lock_als); 942 return ret; 943 case IIO_EV_DIR_FALLING: 944 mutex_lock(&data->lock_als); 945 ret = regmap_bulk_write(data->regmap, 946 LTR501_ALS_THRESH_LOW, 947 &val, 2); 948 mutex_unlock(&data->lock_als); 949 return ret; 950 default: 951 return -EINVAL; 952 } 953 case IIO_PROXIMITY: 954 if (val > LTR501_PS_THRESH_MASK) 955 return -EINVAL; 956 switch (dir) { 957 case IIO_EV_DIR_RISING: 958 mutex_lock(&data->lock_ps); 959 ret = regmap_bulk_write(data->regmap, 960 LTR501_PS_THRESH_UP, 961 &val, 2); 962 mutex_unlock(&data->lock_ps); 963 return ret; 964 case IIO_EV_DIR_FALLING: 965 mutex_lock(&data->lock_ps); 966 ret = regmap_bulk_write(data->regmap, 967 LTR501_PS_THRESH_LOW, 968 &val, 2); 969 mutex_unlock(&data->lock_ps); 970 return ret; 971 default: 972 return -EINVAL; 973 } 974 default: 975 return -EINVAL; 976 } 977 978 return -EINVAL; 979 } 980 981 static int ltr501_read_event(struct iio_dev *indio_dev, 982 const struct iio_chan_spec *chan, 983 enum iio_event_type type, 984 enum iio_event_direction dir, 985 enum iio_event_info info, 986 int *val, int *val2) 987 { 988 int ret; 989 990 switch (info) { 991 case IIO_EV_INFO_VALUE: 992 return ltr501_read_thresh(indio_dev, chan, type, dir, 993 info, val, val2); 994 case IIO_EV_INFO_PERIOD: 995 ret = ltr501_read_intr_prst(iio_priv(indio_dev), 996 chan->type, val2); 997 *val = *val2 / 1000000; 998 *val2 = *val2 % 1000000; 999 return ret; 1000 default: 1001 return -EINVAL; 1002 } 1003 1004 return -EINVAL; 1005 } 1006 1007 static int ltr501_write_event(struct iio_dev *indio_dev, 1008 const struct iio_chan_spec *chan, 1009 enum iio_event_type type, 1010 enum iio_event_direction dir, 1011 enum iio_event_info info, 1012 int val, int val2) 1013 { 1014 switch (info) { 1015 case IIO_EV_INFO_VALUE: 1016 if (val2 != 0) 1017 return -EINVAL; 1018 return ltr501_write_thresh(indio_dev, chan, type, dir, 1019 info, val, val2); 1020 case IIO_EV_INFO_PERIOD: 1021 return ltr501_write_intr_prst(iio_priv(indio_dev), chan->type, 1022 val, val2); 1023 default: 1024 return -EINVAL; 1025 } 1026 1027 return -EINVAL; 1028 } 1029 1030 static int ltr501_read_event_config(struct iio_dev *indio_dev, 1031 const struct iio_chan_spec *chan, 1032 enum iio_event_type type, 1033 enum iio_event_direction dir) 1034 { 1035 struct ltr501_data *data = iio_priv(indio_dev); 1036 int ret, status; 1037 1038 switch (chan->type) { 1039 case IIO_INTENSITY: 1040 ret = regmap_field_read(data->reg_als_intr, &status); 1041 if (ret < 0) 1042 return ret; 1043 return status; 1044 case IIO_PROXIMITY: 1045 ret = regmap_field_read(data->reg_ps_intr, &status); 1046 if (ret < 0) 1047 return ret; 1048 return status; 1049 default: 1050 return -EINVAL; 1051 } 1052 1053 return -EINVAL; 1054 } 1055 1056 static int ltr501_write_event_config(struct iio_dev *indio_dev, 1057 const struct iio_chan_spec *chan, 1058 enum iio_event_type type, 1059 enum iio_event_direction dir, int state) 1060 { 1061 struct ltr501_data *data = iio_priv(indio_dev); 1062 int ret; 1063 1064 /* only 1 and 0 are valid inputs */ 1065 if (state != 1 && state != 0) 1066 return -EINVAL; 1067 1068 switch (chan->type) { 1069 case IIO_INTENSITY: 1070 mutex_lock(&data->lock_als); 1071 ret = regmap_field_write(data->reg_als_intr, state); 1072 mutex_unlock(&data->lock_als); 1073 return ret; 1074 case IIO_PROXIMITY: 1075 mutex_lock(&data->lock_ps); 1076 ret = regmap_field_write(data->reg_ps_intr, state); 1077 mutex_unlock(&data->lock_ps); 1078 return ret; 1079 default: 1080 return -EINVAL; 1081 } 1082 1083 return -EINVAL; 1084 } 1085 1086 static ssize_t ltr501_show_proximity_scale_avail(struct device *dev, 1087 struct device_attribute *attr, 1088 char *buf) 1089 { 1090 struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev)); 1091 const struct ltr501_chip_info *info = data->chip_info; 1092 ssize_t len = 0; 1093 int i; 1094 1095 for (i = 0; i < info->ps_gain_tbl_size; i++) { 1096 if (info->ps_gain[i].scale == LTR501_RESERVED_GAIN) 1097 continue; 1098 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", 1099 info->ps_gain[i].scale, 1100 info->ps_gain[i].uscale); 1101 } 1102 1103 buf[len - 1] = '\n'; 1104 1105 return len; 1106 } 1107 1108 static ssize_t ltr501_show_intensity_scale_avail(struct device *dev, 1109 struct device_attribute *attr, 1110 char *buf) 1111 { 1112 struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev)); 1113 const struct ltr501_chip_info *info = data->chip_info; 1114 ssize_t len = 0; 1115 int i; 1116 1117 for (i = 0; i < info->als_gain_tbl_size; i++) { 1118 if (info->als_gain[i].scale == LTR501_RESERVED_GAIN) 1119 continue; 1120 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", 1121 info->als_gain[i].scale, 1122 info->als_gain[i].uscale); 1123 } 1124 1125 buf[len - 1] = '\n'; 1126 1127 return len; 1128 } 1129 1130 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.05 0.1 0.2 0.4"); 1131 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("20 10 5 2 1 0.5"); 1132 1133 static IIO_DEVICE_ATTR(in_proximity_scale_available, S_IRUGO, 1134 ltr501_show_proximity_scale_avail, NULL, 0); 1135 static IIO_DEVICE_ATTR(in_intensity_scale_available, S_IRUGO, 1136 ltr501_show_intensity_scale_avail, NULL, 0); 1137 1138 static struct attribute *ltr501_attributes[] = { 1139 &iio_dev_attr_in_proximity_scale_available.dev_attr.attr, 1140 &iio_dev_attr_in_intensity_scale_available.dev_attr.attr, 1141 &iio_const_attr_integration_time_available.dev_attr.attr, 1142 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 1143 NULL 1144 }; 1145 1146 static struct attribute *ltr301_attributes[] = { 1147 &iio_dev_attr_in_intensity_scale_available.dev_attr.attr, 1148 &iio_const_attr_integration_time_available.dev_attr.attr, 1149 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 1150 NULL 1151 }; 1152 1153 static const struct attribute_group ltr501_attribute_group = { 1154 .attrs = ltr501_attributes, 1155 }; 1156 1157 static const struct attribute_group ltr301_attribute_group = { 1158 .attrs = ltr301_attributes, 1159 }; 1160 1161 static const struct iio_info ltr501_info_no_irq = { 1162 .read_raw = ltr501_read_raw, 1163 .write_raw = ltr501_write_raw, 1164 .attrs = <r501_attribute_group, 1165 }; 1166 1167 static const struct iio_info ltr501_info = { 1168 .read_raw = ltr501_read_raw, 1169 .write_raw = ltr501_write_raw, 1170 .attrs = <r501_attribute_group, 1171 .read_event_value = <r501_read_event, 1172 .write_event_value = <r501_write_event, 1173 .read_event_config = <r501_read_event_config, 1174 .write_event_config = <r501_write_event_config, 1175 }; 1176 1177 static const struct iio_info ltr301_info_no_irq = { 1178 .read_raw = ltr501_read_raw, 1179 .write_raw = ltr501_write_raw, 1180 .attrs = <r301_attribute_group, 1181 }; 1182 1183 static const struct iio_info ltr301_info = { 1184 .read_raw = ltr501_read_raw, 1185 .write_raw = ltr501_write_raw, 1186 .attrs = <r301_attribute_group, 1187 .read_event_value = <r501_read_event, 1188 .write_event_value = <r501_write_event, 1189 .read_event_config = <r501_read_event_config, 1190 .write_event_config = <r501_write_event_config, 1191 }; 1192 1193 static const struct ltr501_chip_info ltr501_chip_info_tbl[] = { 1194 [ltr501] = { 1195 .partid = 0x08, 1196 .als_gain = ltr501_als_gain_tbl, 1197 .als_gain_tbl_size = ARRAY_SIZE(ltr501_als_gain_tbl), 1198 .ps_gain = ltr501_ps_gain_tbl, 1199 .ps_gain_tbl_size = ARRAY_SIZE(ltr501_ps_gain_tbl), 1200 .als_mode_active = BIT(0) | BIT(1), 1201 .als_gain_mask = BIT(3), 1202 .als_gain_shift = 3, 1203 .info = <r501_info, 1204 .info_no_irq = <r501_info_no_irq, 1205 .channels = ltr501_channels, 1206 .no_channels = ARRAY_SIZE(ltr501_channels), 1207 }, 1208 [ltr559] = { 1209 .partid = 0x09, 1210 .als_gain = ltr559_als_gain_tbl, 1211 .als_gain_tbl_size = ARRAY_SIZE(ltr559_als_gain_tbl), 1212 .ps_gain = ltr559_ps_gain_tbl, 1213 .ps_gain_tbl_size = ARRAY_SIZE(ltr559_ps_gain_tbl), 1214 .als_mode_active = BIT(0), 1215 .als_gain_mask = BIT(2) | BIT(3) | BIT(4), 1216 .als_gain_shift = 2, 1217 .info = <r501_info, 1218 .info_no_irq = <r501_info_no_irq, 1219 .channels = ltr501_channels, 1220 .no_channels = ARRAY_SIZE(ltr501_channels), 1221 }, 1222 [ltr301] = { 1223 .partid = 0x08, 1224 .als_gain = ltr501_als_gain_tbl, 1225 .als_gain_tbl_size = ARRAY_SIZE(ltr501_als_gain_tbl), 1226 .als_mode_active = BIT(0) | BIT(1), 1227 .als_gain_mask = BIT(3), 1228 .als_gain_shift = 3, 1229 .info = <r301_info, 1230 .info_no_irq = <r301_info_no_irq, 1231 .channels = ltr301_channels, 1232 .no_channels = ARRAY_SIZE(ltr301_channels), 1233 }, 1234 }; 1235 1236 static int ltr501_write_contr(struct ltr501_data *data, u8 als_val, u8 ps_val) 1237 { 1238 int ret; 1239 1240 ret = regmap_write(data->regmap, LTR501_ALS_CONTR, als_val); 1241 if (ret < 0) 1242 return ret; 1243 1244 return regmap_write(data->regmap, LTR501_PS_CONTR, ps_val); 1245 } 1246 1247 static irqreturn_t ltr501_trigger_handler(int irq, void *p) 1248 { 1249 struct iio_poll_func *pf = p; 1250 struct iio_dev *indio_dev = pf->indio_dev; 1251 struct ltr501_data *data = iio_priv(indio_dev); 1252 struct { 1253 u16 channels[3]; 1254 s64 ts __aligned(8); 1255 } scan; 1256 __le16 als_buf[2]; 1257 u8 mask = 0; 1258 int j = 0; 1259 int ret, psdata; 1260 1261 memset(&scan, 0, sizeof(scan)); 1262 1263 /* figure out which data needs to be ready */ 1264 if (test_bit(0, indio_dev->active_scan_mask) || 1265 test_bit(1, indio_dev->active_scan_mask)) 1266 mask |= LTR501_STATUS_ALS_RDY; 1267 if (test_bit(2, indio_dev->active_scan_mask)) 1268 mask |= LTR501_STATUS_PS_RDY; 1269 1270 ret = ltr501_drdy(data, mask); 1271 if (ret < 0) 1272 goto done; 1273 1274 if (mask & LTR501_STATUS_ALS_RDY) { 1275 ret = regmap_bulk_read(data->regmap, LTR501_ALS_DATA1, 1276 als_buf, sizeof(als_buf)); 1277 if (ret < 0) 1278 goto done; 1279 if (test_bit(0, indio_dev->active_scan_mask)) 1280 scan.channels[j++] = le16_to_cpu(als_buf[1]); 1281 if (test_bit(1, indio_dev->active_scan_mask)) 1282 scan.channels[j++] = le16_to_cpu(als_buf[0]); 1283 } 1284 1285 if (mask & LTR501_STATUS_PS_RDY) { 1286 ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA, 1287 &psdata, 2); 1288 if (ret < 0) 1289 goto done; 1290 scan.channels[j++] = psdata & LTR501_PS_DATA_MASK; 1291 } 1292 1293 iio_push_to_buffers_with_timestamp(indio_dev, &scan, 1294 iio_get_time_ns(indio_dev)); 1295 1296 done: 1297 iio_trigger_notify_done(indio_dev->trig); 1298 1299 return IRQ_HANDLED; 1300 } 1301 1302 static irqreturn_t ltr501_interrupt_handler(int irq, void *private) 1303 { 1304 struct iio_dev *indio_dev = private; 1305 struct ltr501_data *data = iio_priv(indio_dev); 1306 int ret, status; 1307 1308 ret = regmap_read(data->regmap, LTR501_ALS_PS_STATUS, &status); 1309 if (ret < 0) { 1310 dev_err(&data->client->dev, 1311 "irq read int reg failed\n"); 1312 return IRQ_HANDLED; 1313 } 1314 1315 if (status & LTR501_STATUS_ALS_INTR) 1316 iio_push_event(indio_dev, 1317 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0, 1318 IIO_EV_TYPE_THRESH, 1319 IIO_EV_DIR_EITHER), 1320 iio_get_time_ns(indio_dev)); 1321 1322 if (status & LTR501_STATUS_PS_INTR) 1323 iio_push_event(indio_dev, 1324 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0, 1325 IIO_EV_TYPE_THRESH, 1326 IIO_EV_DIR_EITHER), 1327 iio_get_time_ns(indio_dev)); 1328 1329 return IRQ_HANDLED; 1330 } 1331 1332 static int ltr501_init(struct ltr501_data *data) 1333 { 1334 int ret, status; 1335 1336 ret = regmap_read(data->regmap, LTR501_ALS_CONTR, &status); 1337 if (ret < 0) 1338 return ret; 1339 1340 data->als_contr = status | data->chip_info->als_mode_active; 1341 1342 ret = regmap_read(data->regmap, LTR501_PS_CONTR, &status); 1343 if (ret < 0) 1344 return ret; 1345 1346 data->ps_contr = status | LTR501_CONTR_ACTIVE; 1347 1348 ret = ltr501_read_intr_prst(data, IIO_INTENSITY, &data->als_period); 1349 if (ret < 0) 1350 return ret; 1351 1352 ret = ltr501_read_intr_prst(data, IIO_PROXIMITY, &data->ps_period); 1353 if (ret < 0) 1354 return ret; 1355 1356 return ltr501_write_contr(data, data->als_contr, data->ps_contr); 1357 } 1358 1359 static bool ltr501_is_volatile_reg(struct device *dev, unsigned int reg) 1360 { 1361 switch (reg) { 1362 case LTR501_ALS_DATA1: 1363 case LTR501_ALS_DATA1_UPPER: 1364 case LTR501_ALS_DATA0: 1365 case LTR501_ALS_DATA0_UPPER: 1366 case LTR501_ALS_PS_STATUS: 1367 case LTR501_PS_DATA: 1368 case LTR501_PS_DATA_UPPER: 1369 return true; 1370 default: 1371 return false; 1372 } 1373 } 1374 1375 static const struct regmap_config ltr501_regmap_config = { 1376 .name = LTR501_REGMAP_NAME, 1377 .reg_bits = 8, 1378 .val_bits = 8, 1379 .max_register = LTR501_MAX_REG, 1380 .cache_type = REGCACHE_RBTREE, 1381 .volatile_reg = ltr501_is_volatile_reg, 1382 }; 1383 1384 static void ltr501_disable_regulators(void *d) 1385 { 1386 struct ltr501_data *data = d; 1387 1388 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); 1389 } 1390 1391 static int ltr501_powerdown(struct ltr501_data *data) 1392 { 1393 return ltr501_write_contr(data, data->als_contr & 1394 ~data->chip_info->als_mode_active, 1395 data->ps_contr & ~LTR501_CONTR_ACTIVE); 1396 } 1397 1398 static const char *ltr501_match_acpi_device(struct device *dev, int *chip_idx) 1399 { 1400 const struct acpi_device_id *id; 1401 1402 id = acpi_match_device(dev->driver->acpi_match_table, dev); 1403 if (!id) 1404 return NULL; 1405 *chip_idx = id->driver_data; 1406 return dev_name(dev); 1407 } 1408 1409 static int ltr501_probe(struct i2c_client *client, 1410 const struct i2c_device_id *id) 1411 { 1412 struct ltr501_data *data; 1413 struct iio_dev *indio_dev; 1414 struct regmap *regmap; 1415 int ret, partid, chip_idx = 0; 1416 const char *name = NULL; 1417 1418 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 1419 if (!indio_dev) 1420 return -ENOMEM; 1421 1422 regmap = devm_regmap_init_i2c(client, <r501_regmap_config); 1423 if (IS_ERR(regmap)) { 1424 dev_err(&client->dev, "Regmap initialization failed.\n"); 1425 return PTR_ERR(regmap); 1426 } 1427 1428 data = iio_priv(indio_dev); 1429 i2c_set_clientdata(client, indio_dev); 1430 data->client = client; 1431 data->regmap = regmap; 1432 mutex_init(&data->lock_als); 1433 mutex_init(&data->lock_ps); 1434 1435 data->regulators[0].supply = "vdd"; 1436 data->regulators[1].supply = "vddio"; 1437 ret = devm_regulator_bulk_get(&client->dev, 1438 ARRAY_SIZE(data->regulators), 1439 data->regulators); 1440 if (ret) 1441 return dev_err_probe(&client->dev, ret, 1442 "Failed to get regulators\n"); 1443 1444 ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators), 1445 data->regulators); 1446 if (ret) 1447 return ret; 1448 1449 ret = devm_add_action_or_reset(&client->dev, 1450 ltr501_disable_regulators, data); 1451 if (ret) 1452 return ret; 1453 1454 data->reg_it = devm_regmap_field_alloc(&client->dev, regmap, 1455 reg_field_it); 1456 if (IS_ERR(data->reg_it)) { 1457 dev_err(&client->dev, "Integ time reg field init failed.\n"); 1458 return PTR_ERR(data->reg_it); 1459 } 1460 1461 data->reg_als_intr = devm_regmap_field_alloc(&client->dev, regmap, 1462 reg_field_als_intr); 1463 if (IS_ERR(data->reg_als_intr)) { 1464 dev_err(&client->dev, "ALS intr mode reg field init failed\n"); 1465 return PTR_ERR(data->reg_als_intr); 1466 } 1467 1468 data->reg_ps_intr = devm_regmap_field_alloc(&client->dev, regmap, 1469 reg_field_ps_intr); 1470 if (IS_ERR(data->reg_ps_intr)) { 1471 dev_err(&client->dev, "PS intr mode reg field init failed.\n"); 1472 return PTR_ERR(data->reg_ps_intr); 1473 } 1474 1475 data->reg_als_rate = devm_regmap_field_alloc(&client->dev, regmap, 1476 reg_field_als_rate); 1477 if (IS_ERR(data->reg_als_rate)) { 1478 dev_err(&client->dev, "ALS samp rate field init failed.\n"); 1479 return PTR_ERR(data->reg_als_rate); 1480 } 1481 1482 data->reg_ps_rate = devm_regmap_field_alloc(&client->dev, regmap, 1483 reg_field_ps_rate); 1484 if (IS_ERR(data->reg_ps_rate)) { 1485 dev_err(&client->dev, "PS samp rate field init failed.\n"); 1486 return PTR_ERR(data->reg_ps_rate); 1487 } 1488 1489 data->reg_als_prst = devm_regmap_field_alloc(&client->dev, regmap, 1490 reg_field_als_prst); 1491 if (IS_ERR(data->reg_als_prst)) { 1492 dev_err(&client->dev, "ALS prst reg field init failed\n"); 1493 return PTR_ERR(data->reg_als_prst); 1494 } 1495 1496 data->reg_ps_prst = devm_regmap_field_alloc(&client->dev, regmap, 1497 reg_field_ps_prst); 1498 if (IS_ERR(data->reg_ps_prst)) { 1499 dev_err(&client->dev, "PS prst reg field init failed.\n"); 1500 return PTR_ERR(data->reg_ps_prst); 1501 } 1502 1503 ret = regmap_read(data->regmap, LTR501_PART_ID, &partid); 1504 if (ret < 0) 1505 return ret; 1506 1507 if (id) { 1508 name = id->name; 1509 chip_idx = id->driver_data; 1510 } else if (ACPI_HANDLE(&client->dev)) { 1511 name = ltr501_match_acpi_device(&client->dev, &chip_idx); 1512 } else { 1513 return -ENODEV; 1514 } 1515 1516 data->chip_info = <r501_chip_info_tbl[chip_idx]; 1517 1518 if ((partid >> 4) != data->chip_info->partid) 1519 return -ENODEV; 1520 1521 indio_dev->info = data->chip_info->info; 1522 indio_dev->channels = data->chip_info->channels; 1523 indio_dev->num_channels = data->chip_info->no_channels; 1524 indio_dev->name = name; 1525 indio_dev->modes = INDIO_DIRECT_MODE; 1526 1527 ret = ltr501_init(data); 1528 if (ret < 0) 1529 return ret; 1530 1531 if (client->irq > 0) { 1532 ret = devm_request_threaded_irq(&client->dev, client->irq, 1533 NULL, ltr501_interrupt_handler, 1534 IRQF_TRIGGER_FALLING | 1535 IRQF_ONESHOT, 1536 "ltr501_thresh_event", 1537 indio_dev); 1538 if (ret) { 1539 dev_err(&client->dev, "request irq (%d) failed\n", 1540 client->irq); 1541 return ret; 1542 } 1543 } else { 1544 indio_dev->info = data->chip_info->info_no_irq; 1545 } 1546 1547 ret = iio_triggered_buffer_setup(indio_dev, NULL, 1548 ltr501_trigger_handler, NULL); 1549 if (ret) 1550 goto powerdown_on_error; 1551 1552 ret = iio_device_register(indio_dev); 1553 if (ret) 1554 goto error_unreg_buffer; 1555 1556 return 0; 1557 1558 error_unreg_buffer: 1559 iio_triggered_buffer_cleanup(indio_dev); 1560 powerdown_on_error: 1561 ltr501_powerdown(data); 1562 return ret; 1563 } 1564 1565 static int ltr501_remove(struct i2c_client *client) 1566 { 1567 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1568 1569 iio_device_unregister(indio_dev); 1570 iio_triggered_buffer_cleanup(indio_dev); 1571 ltr501_powerdown(iio_priv(indio_dev)); 1572 1573 return 0; 1574 } 1575 1576 #ifdef CONFIG_PM_SLEEP 1577 static int ltr501_suspend(struct device *dev) 1578 { 1579 struct ltr501_data *data = iio_priv(i2c_get_clientdata( 1580 to_i2c_client(dev))); 1581 return ltr501_powerdown(data); 1582 } 1583 1584 static int ltr501_resume(struct device *dev) 1585 { 1586 struct ltr501_data *data = iio_priv(i2c_get_clientdata( 1587 to_i2c_client(dev))); 1588 1589 return ltr501_write_contr(data, data->als_contr, 1590 data->ps_contr); 1591 } 1592 #endif 1593 1594 static SIMPLE_DEV_PM_OPS(ltr501_pm_ops, ltr501_suspend, ltr501_resume); 1595 1596 static const struct acpi_device_id ltr_acpi_match[] = { 1597 {"LTER0501", ltr501}, 1598 {"LTER0559", ltr559}, 1599 {"LTER0301", ltr301}, 1600 { }, 1601 }; 1602 MODULE_DEVICE_TABLE(acpi, ltr_acpi_match); 1603 1604 static const struct i2c_device_id ltr501_id[] = { 1605 { "ltr501", ltr501}, 1606 { "ltr559", ltr559}, 1607 { "ltr301", ltr301}, 1608 { } 1609 }; 1610 MODULE_DEVICE_TABLE(i2c, ltr501_id); 1611 1612 static const struct of_device_id ltr501_of_match[] = { 1613 { .compatible = "liteon,ltr501", }, 1614 { .compatible = "liteon,ltr559", }, 1615 { .compatible = "liteon,ltr301", }, 1616 {} 1617 }; 1618 MODULE_DEVICE_TABLE(of, ltr501_of_match); 1619 1620 static struct i2c_driver ltr501_driver = { 1621 .driver = { 1622 .name = LTR501_DRV_NAME, 1623 .of_match_table = ltr501_of_match, 1624 .pm = <r501_pm_ops, 1625 .acpi_match_table = ACPI_PTR(ltr_acpi_match), 1626 }, 1627 .probe = ltr501_probe, 1628 .remove = ltr501_remove, 1629 .id_table = ltr501_id, 1630 }; 1631 1632 module_i2c_driver(ltr501_driver); 1633 1634 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 1635 MODULE_DESCRIPTION("Lite-On LTR501 ambient light and proximity sensor driver"); 1636 MODULE_LICENSE("GPL"); 1637