Lines Matching +full:ts +full:- +full:coeff +full:- +full:j

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for Lite-On LTR501 and similar ambient light and proximity sensors.
7 * 7-bit I2C slave address 0x23
35 #define LTR501_ALS_DATA1 0x88 /* 16-bit, little endian */
37 #define LTR501_ALS_DATA0 0x8a /* 16-bit, little endian */
40 #define LTR501_PS_DATA 0x8d /* 16-bit, little endian */
71 ((vis_coeff * vis_data) - (ir_coeff * ir_data))
95 #define LTR501_RESERVED_GAIN -1
199 return -EINVAL; in ltr501_match_samp_freq()
207 ret = regmap_field_read(data->reg_als_rate, &i); in ltr501_als_read_samp_freq()
212 return -EINVAL; in ltr501_als_read_samp_freq()
225 ret = regmap_field_read(data->reg_ps_rate, &i); in ltr501_ps_read_samp_freq()
230 return -EINVAL; in ltr501_ps_read_samp_freq()
250 mutex_lock(&data->lock_als); in ltr501_als_write_samp_freq()
251 ret = regmap_field_write(data->reg_als_rate, i); in ltr501_als_write_samp_freq()
252 mutex_unlock(&data->lock_als); in ltr501_als_write_samp_freq()
269 mutex_lock(&data->lock_ps); in ltr501_ps_write_samp_freq()
270 ret = regmap_field_write(data->reg_ps_rate, i); in ltr501_ps_write_samp_freq()
271 mutex_unlock(&data->lock_ps); in ltr501_ps_write_samp_freq()
280 ret = regmap_field_read(data->reg_als_rate, &i); in ltr501_als_read_samp_period()
285 return -EINVAL; in ltr501_als_read_samp_period()
296 ret = regmap_field_read(data->reg_ps_rate, &i); in ltr501_ps_read_samp_period()
301 return -EINVAL; in ltr501_ps_read_samp_period()
308 /* IR and visible spectrum coeff's are given in data sheet */
320 lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data); in ltr501_calculate_lux()
336 while (tries--) { in ltr501_drdy()
337 ret = regmap_read(data->regmap, LTR501_ALS_PS_STATUS, &status); in ltr501_drdy()
345 dev_err(&data->client->dev, "ltr501_drdy() failed, data not ready\n"); in ltr501_drdy()
346 return -EIO; in ltr501_drdy()
351 int ret, i, index = -1, status; in ltr501_set_it_time()
361 return -EINVAL; in ltr501_set_it_time()
363 ret = regmap_read(data->regmap, LTR501_ALS_CONTR, &status); in ltr501_set_it_time()
373 return -EINVAL; in ltr501_set_it_time()
377 return -EINVAL; in ltr501_set_it_time()
379 return regmap_field_write(data->reg_it, index); in ltr501_set_it_time()
388 ret = regmap_field_read(data->reg_it, &index); in ltr501_read_it_time()
394 return -EINVAL; in ltr501_read_it_time()
410 return regmap_bulk_read(data->regmap, LTR501_ALS_DATA1, in ltr501_read_als()
423 ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA, in ltr501_read_ps()
439 ret = regmap_field_read(data->reg_als_prst, &prst); in ltr501_read_intr_prst()
450 ret = regmap_field_read(data->reg_ps_prst, &prst); in ltr501_read_intr_prst()
462 return -EINVAL; in ltr501_read_intr_prst()
465 return -EINVAL; in ltr501_read_intr_prst()
476 return -EINVAL; in ltr501_write_intr_prst()
489 return -EINVAL; in ltr501_write_intr_prst()
493 return -EINVAL; in ltr501_write_intr_prst()
495 mutex_lock(&data->lock_als); in ltr501_write_intr_prst()
496 ret = regmap_field_write(data->reg_als_prst, new_val); in ltr501_write_intr_prst()
497 mutex_unlock(&data->lock_als); in ltr501_write_intr_prst()
499 data->als_period = period; in ltr501_write_intr_prst()
509 return -EINVAL; in ltr501_write_intr_prst()
513 return -EINVAL; in ltr501_write_intr_prst()
515 mutex_lock(&data->lock_ps); in ltr501_write_intr_prst()
516 ret = regmap_field_write(data->reg_ps_prst, new_val); in ltr501_write_intr_prst()
517 mutex_unlock(&data->lock_ps); in ltr501_write_intr_prst()
519 data->ps_period = period; in ltr501_write_intr_prst()
523 return -EINVAL; in ltr501_write_intr_prst()
526 return -EINVAL; in ltr501_write_intr_prst()
536 return sprintf(buf, "%u\n", data->near_level); in ltr501_read_near_level()
605 .scan_index = -1, \
660 switch (chan->type) { in ltr501_read_raw()
666 mutex_lock(&data->lock_als); in ltr501_read_raw()
668 mutex_unlock(&data->lock_als); in ltr501_read_raw()
676 return -EINVAL; in ltr501_read_raw()
683 switch (chan->type) { in ltr501_read_raw()
685 mutex_lock(&data->lock_als); in ltr501_read_raw()
687 mutex_unlock(&data->lock_als); in ltr501_read_raw()
690 *val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ? in ltr501_read_raw()
695 mutex_lock(&data->lock_ps); in ltr501_read_raw()
697 mutex_unlock(&data->lock_ps); in ltr501_read_raw()
704 ret = -EINVAL; in ltr501_read_raw()
712 switch (chan->type) { in ltr501_read_raw()
714 i = (data->als_contr & data->chip_info->als_gain_mask) in ltr501_read_raw()
715 >> data->chip_info->als_gain_shift; in ltr501_read_raw()
716 *val = data->chip_info->als_gain[i].scale; in ltr501_read_raw()
717 *val2 = data->chip_info->als_gain[i].uscale; in ltr501_read_raw()
720 i = (data->ps_contr & LTR501_CONTR_PS_GAIN_MASK) >> in ltr501_read_raw()
722 *val = data->chip_info->ps_gain[i].scale; in ltr501_read_raw()
723 *val2 = data->chip_info->ps_gain[i].uscale; in ltr501_read_raw()
726 return -EINVAL; in ltr501_read_raw()
729 switch (chan->type) { in ltr501_read_raw()
733 return -EINVAL; in ltr501_read_raw()
736 switch (chan->type) { in ltr501_read_raw()
742 return -EINVAL; in ltr501_read_raw()
745 return -EINVAL; in ltr501_read_raw()
757 return -1; in ltr501_get_gain_index()
766 const struct ltr501_chip_info *info = data->chip_info; in ltr501_write_raw()
774 switch (chan->type) { in ltr501_write_raw()
776 i = ltr501_get_gain_index(info->als_gain, in ltr501_write_raw()
777 info->als_gain_tbl_size, in ltr501_write_raw()
780 ret = -EINVAL; in ltr501_write_raw()
784 data->als_contr &= ~info->als_gain_mask; in ltr501_write_raw()
785 data->als_contr |= i << info->als_gain_shift; in ltr501_write_raw()
787 ret = regmap_write(data->regmap, LTR501_ALS_CONTR, in ltr501_write_raw()
788 data->als_contr); in ltr501_write_raw()
791 i = ltr501_get_gain_index(info->ps_gain, in ltr501_write_raw()
792 info->ps_gain_tbl_size, in ltr501_write_raw()
795 ret = -EINVAL; in ltr501_write_raw()
798 data->ps_contr &= ~LTR501_CONTR_PS_GAIN_MASK; in ltr501_write_raw()
799 data->ps_contr |= i << LTR501_CONTR_PS_GAIN_SHIFT; in ltr501_write_raw()
801 ret = regmap_write(data->regmap, LTR501_PS_CONTR, in ltr501_write_raw()
802 data->ps_contr); in ltr501_write_raw()
805 ret = -EINVAL; in ltr501_write_raw()
811 switch (chan->type) { in ltr501_write_raw()
814 ret = -EINVAL; in ltr501_write_raw()
817 mutex_lock(&data->lock_als); in ltr501_write_raw()
819 mutex_unlock(&data->lock_als); in ltr501_write_raw()
822 ret = -EINVAL; in ltr501_write_raw()
828 switch (chan->type) { in ltr501_write_raw()
840 ret = ltr501_write_intr_prst(data, chan->type, in ltr501_write_raw()
841 0, data->als_period); in ltr501_write_raw()
858 ret = ltr501_write_intr_prst(data, chan->type, in ltr501_write_raw()
859 0, data->ps_period); in ltr501_write_raw()
866 ret = -EINVAL; in ltr501_write_raw()
872 ret = -EINVAL; in ltr501_write_raw()
890 switch (chan->type) { in ltr501_read_thresh()
894 ret = regmap_bulk_read(data->regmap, in ltr501_read_thresh()
902 ret = regmap_bulk_read(data->regmap, in ltr501_read_thresh()
910 return -EINVAL; in ltr501_read_thresh()
915 ret = regmap_bulk_read(data->regmap, in ltr501_read_thresh()
923 ret = regmap_bulk_read(data->regmap, in ltr501_read_thresh()
931 return -EINVAL; in ltr501_read_thresh()
934 return -EINVAL; in ltr501_read_thresh()
937 return -EINVAL; in ltr501_read_thresh()
951 return -EINVAL; in ltr501_write_thresh()
953 switch (chan->type) { in ltr501_write_thresh()
956 return -EINVAL; in ltr501_write_thresh()
959 mutex_lock(&data->lock_als); in ltr501_write_thresh()
960 ret = regmap_bulk_write(data->regmap, in ltr501_write_thresh()
963 mutex_unlock(&data->lock_als); in ltr501_write_thresh()
966 mutex_lock(&data->lock_als); in ltr501_write_thresh()
967 ret = regmap_bulk_write(data->regmap, in ltr501_write_thresh()
970 mutex_unlock(&data->lock_als); in ltr501_write_thresh()
973 return -EINVAL; in ltr501_write_thresh()
977 return -EINVAL; in ltr501_write_thresh()
980 mutex_lock(&data->lock_ps); in ltr501_write_thresh()
981 ret = regmap_bulk_write(data->regmap, in ltr501_write_thresh()
984 mutex_unlock(&data->lock_ps); in ltr501_write_thresh()
987 mutex_lock(&data->lock_ps); in ltr501_write_thresh()
988 ret = regmap_bulk_write(data->regmap, in ltr501_write_thresh()
991 mutex_unlock(&data->lock_ps); in ltr501_write_thresh()
994 return -EINVAL; in ltr501_write_thresh()
997 return -EINVAL; in ltr501_write_thresh()
1000 return -EINVAL; in ltr501_write_thresh()
1018 chan->type, val2); in ltr501_read_event()
1023 return -EINVAL; in ltr501_read_event()
1026 return -EINVAL; in ltr501_read_event()
1039 return -EINVAL; in ltr501_write_event()
1043 return ltr501_write_intr_prst(iio_priv(indio_dev), chan->type, in ltr501_write_event()
1046 return -EINVAL; in ltr501_write_event()
1049 return -EINVAL; in ltr501_write_event()
1060 switch (chan->type) { in ltr501_read_event_config()
1062 ret = regmap_field_read(data->reg_als_intr, &status); in ltr501_read_event_config()
1067 ret = regmap_field_read(data->reg_ps_intr, &status); in ltr501_read_event_config()
1072 return -EINVAL; in ltr501_read_event_config()
1075 return -EINVAL; in ltr501_read_event_config()
1088 return -EINVAL; in ltr501_write_event_config()
1090 switch (chan->type) { in ltr501_write_event_config()
1092 mutex_lock(&data->lock_als); in ltr501_write_event_config()
1093 ret = regmap_field_write(data->reg_als_intr, state); in ltr501_write_event_config()
1094 mutex_unlock(&data->lock_als); in ltr501_write_event_config()
1097 mutex_lock(&data->lock_ps); in ltr501_write_event_config()
1098 ret = regmap_field_write(data->reg_ps_intr, state); in ltr501_write_event_config()
1099 mutex_unlock(&data->lock_ps); in ltr501_write_event_config()
1102 return -EINVAL; in ltr501_write_event_config()
1105 return -EINVAL; in ltr501_write_event_config()
1113 const struct ltr501_chip_info *info = data->chip_info; in ltr501_show_proximity_scale_avail()
1117 for (i = 0; i < info->ps_gain_tbl_size; i++) { in ltr501_show_proximity_scale_avail()
1118 if (info->ps_gain[i].scale == LTR501_RESERVED_GAIN) in ltr501_show_proximity_scale_avail()
1120 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", in ltr501_show_proximity_scale_avail()
1121 info->ps_gain[i].scale, in ltr501_show_proximity_scale_avail()
1122 info->ps_gain[i].uscale); in ltr501_show_proximity_scale_avail()
1125 buf[len - 1] = '\n'; in ltr501_show_proximity_scale_avail()
1135 const struct ltr501_chip_info *info = data->chip_info; in ltr501_show_intensity_scale_avail()
1139 for (i = 0; i < info->als_gain_tbl_size; i++) { in ltr501_show_intensity_scale_avail()
1140 if (info->als_gain[i].scale == LTR501_RESERVED_GAIN) in ltr501_show_intensity_scale_avail()
1142 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", in ltr501_show_intensity_scale_avail()
1143 info->als_gain[i].scale, in ltr501_show_intensity_scale_avail()
1144 info->als_gain[i].uscale); in ltr501_show_intensity_scale_avail()
1147 buf[len - 1] = '\n'; in ltr501_show_intensity_scale_avail()
1274 ret = regmap_write(data->regmap, LTR501_ALS_CONTR, als_val); in ltr501_write_contr()
1278 return regmap_write(data->regmap, LTR501_PS_CONTR, ps_val); in ltr501_write_contr()
1284 struct iio_dev *indio_dev = pf->indio_dev; in ltr501_trigger_handler()
1288 s64 ts __aligned(8); in ltr501_trigger_handler()
1292 int j = 0; in ltr501_trigger_handler() local
1298 if (test_bit(0, indio_dev->active_scan_mask) || in ltr501_trigger_handler()
1299 test_bit(1, indio_dev->active_scan_mask)) in ltr501_trigger_handler()
1301 if (test_bit(2, indio_dev->active_scan_mask)) in ltr501_trigger_handler()
1309 ret = regmap_bulk_read(data->regmap, LTR501_ALS_DATA1, in ltr501_trigger_handler()
1313 if (test_bit(0, indio_dev->active_scan_mask)) in ltr501_trigger_handler()
1314 scan.channels[j++] = le16_to_cpu(als_buf[1]); in ltr501_trigger_handler()
1315 if (test_bit(1, indio_dev->active_scan_mask)) in ltr501_trigger_handler()
1316 scan.channels[j++] = le16_to_cpu(als_buf[0]); in ltr501_trigger_handler()
1320 ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA, in ltr501_trigger_handler()
1324 scan.channels[j++] = psdata & LTR501_PS_DATA_MASK; in ltr501_trigger_handler()
1331 iio_trigger_notify_done(indio_dev->trig); in ltr501_trigger_handler()
1342 ret = regmap_read(data->regmap, LTR501_ALS_PS_STATUS, &status); in ltr501_interrupt_handler()
1344 dev_err(&data->client->dev, in ltr501_interrupt_handler()
1370 ret = regmap_read(data->regmap, LTR501_ALS_CONTR, &status); in ltr501_init()
1374 data->als_contr = status | data->chip_info->als_mode_active; in ltr501_init()
1376 ret = regmap_read(data->regmap, LTR501_PS_CONTR, &status); in ltr501_init()
1380 data->ps_contr = status | LTR501_CONTR_ACTIVE; in ltr501_init()
1382 ret = ltr501_read_intr_prst(data, IIO_INTENSITY, &data->als_period); in ltr501_init()
1386 ret = ltr501_read_intr_prst(data, IIO_PROXIMITY, &data->ps_period); in ltr501_init()
1390 return ltr501_write_contr(data, data->als_contr, data->ps_contr); in ltr501_init()
1422 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); in ltr501_disable_regulators()
1427 return ltr501_write_contr(data, data->als_contr & in ltr501_powerdown()
1428 ~data->chip_info->als_mode_active, in ltr501_powerdown()
1429 data->ps_contr & ~LTR501_CONTR_ACTIVE); in ltr501_powerdown()
1436 id = acpi_match_device(dev->driver->acpi_match_table, dev); in ltr501_match_acpi_device()
1439 *chip_idx = id->driver_data; in ltr501_match_acpi_device()
1452 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in ltr501_probe()
1454 return -ENOMEM; in ltr501_probe()
1458 dev_err(&client->dev, "Regmap initialization failed.\n"); in ltr501_probe()
1464 data->client = client; in ltr501_probe()
1465 data->regmap = regmap; in ltr501_probe()
1466 mutex_init(&data->lock_als); in ltr501_probe()
1467 mutex_init(&data->lock_ps); in ltr501_probe()
1469 data->regulators[0].supply = "vdd"; in ltr501_probe()
1470 data->regulators[1].supply = "vddio"; in ltr501_probe()
1471 ret = devm_regulator_bulk_get(&client->dev, in ltr501_probe()
1472 ARRAY_SIZE(data->regulators), in ltr501_probe()
1473 data->regulators); in ltr501_probe()
1475 return dev_err_probe(&client->dev, ret, in ltr501_probe()
1478 ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators), in ltr501_probe()
1479 data->regulators); in ltr501_probe()
1483 ret = devm_add_action_or_reset(&client->dev, in ltr501_probe()
1488 data->reg_it = devm_regmap_field_alloc(&client->dev, regmap, in ltr501_probe()
1490 if (IS_ERR(data->reg_it)) { in ltr501_probe()
1491 dev_err(&client->dev, "Integ time reg field init failed.\n"); in ltr501_probe()
1492 return PTR_ERR(data->reg_it); in ltr501_probe()
1495 data->reg_als_intr = devm_regmap_field_alloc(&client->dev, regmap, in ltr501_probe()
1497 if (IS_ERR(data->reg_als_intr)) { in ltr501_probe()
1498 dev_err(&client->dev, "ALS intr mode reg field init failed\n"); in ltr501_probe()
1499 return PTR_ERR(data->reg_als_intr); in ltr501_probe()
1502 data->reg_ps_intr = devm_regmap_field_alloc(&client->dev, regmap, in ltr501_probe()
1504 if (IS_ERR(data->reg_ps_intr)) { in ltr501_probe()
1505 dev_err(&client->dev, "PS intr mode reg field init failed.\n"); in ltr501_probe()
1506 return PTR_ERR(data->reg_ps_intr); in ltr501_probe()
1509 data->reg_als_rate = devm_regmap_field_alloc(&client->dev, regmap, in ltr501_probe()
1511 if (IS_ERR(data->reg_als_rate)) { in ltr501_probe()
1512 dev_err(&client->dev, "ALS samp rate field init failed.\n"); in ltr501_probe()
1513 return PTR_ERR(data->reg_als_rate); in ltr501_probe()
1516 data->reg_ps_rate = devm_regmap_field_alloc(&client->dev, regmap, in ltr501_probe()
1518 if (IS_ERR(data->reg_ps_rate)) { in ltr501_probe()
1519 dev_err(&client->dev, "PS samp rate field init failed.\n"); in ltr501_probe()
1520 return PTR_ERR(data->reg_ps_rate); in ltr501_probe()
1523 data->reg_als_prst = devm_regmap_field_alloc(&client->dev, regmap, in ltr501_probe()
1525 if (IS_ERR(data->reg_als_prst)) { in ltr501_probe()
1526 dev_err(&client->dev, "ALS prst reg field init failed\n"); in ltr501_probe()
1527 return PTR_ERR(data->reg_als_prst); in ltr501_probe()
1530 data->reg_ps_prst = devm_regmap_field_alloc(&client->dev, regmap, in ltr501_probe()
1532 if (IS_ERR(data->reg_ps_prst)) { in ltr501_probe()
1533 dev_err(&client->dev, "PS prst reg field init failed.\n"); in ltr501_probe()
1534 return PTR_ERR(data->reg_ps_prst); in ltr501_probe()
1537 ret = regmap_read(data->regmap, LTR501_PART_ID, &partid); in ltr501_probe()
1542 name = id->name; in ltr501_probe()
1543 chip_idx = id->driver_data; in ltr501_probe()
1544 } else if (ACPI_HANDLE(&client->dev)) { in ltr501_probe()
1545 name = ltr501_match_acpi_device(&client->dev, &chip_idx); in ltr501_probe()
1547 return -ENODEV; in ltr501_probe()
1550 data->chip_info = &ltr501_chip_info_tbl[chip_idx]; in ltr501_probe()
1552 if ((partid >> 4) != data->chip_info->partid) in ltr501_probe()
1553 return -ENODEV; in ltr501_probe()
1555 if (device_property_read_u32(&client->dev, "proximity-near-level", in ltr501_probe()
1556 &data->near_level)) in ltr501_probe()
1557 data->near_level = 0; in ltr501_probe()
1559 indio_dev->info = data->chip_info->info; in ltr501_probe()
1560 indio_dev->channels = data->chip_info->channels; in ltr501_probe()
1561 indio_dev->num_channels = data->chip_info->no_channels; in ltr501_probe()
1562 indio_dev->name = name; in ltr501_probe()
1563 indio_dev->modes = INDIO_DIRECT_MODE; in ltr501_probe()
1569 if (client->irq > 0) { in ltr501_probe()
1570 ret = devm_request_threaded_irq(&client->dev, client->irq, in ltr501_probe()
1577 dev_err(&client->dev, "request irq (%d) failed\n", in ltr501_probe()
1578 client->irq); in ltr501_probe()
1582 indio_dev->info = data->chip_info->info_no_irq; in ltr501_probe()
1624 return ltr501_write_contr(data, data->als_contr, in ltr501_resume()
1625 data->ps_contr); in ltr501_resume()
1671 MODULE_DESCRIPTION("Lite-On LTR501 ambient light and proximity sensor driver");