Lines Matching +full:reset +full:- +full:pin +full:- +full:assert +full:- +full:time +full:- +full:ms

1 // SPDX-License-Identifier: GPL-2.0-only
3 * RPR-0521 ROHM Ambient Light and Proximity Sensor
7 * IIO driver for RPR-0521RS (7-bit I2C slave address 0x38).
31 #define RPR0521_REG_PXS_DATA 0x44 /* 16-bit, little endian */
32 #define RPR0521_REG_ALS_DATA0 0x46 /* 16-bit, little endian */
33 #define RPR0521_REG_ALS_DATA1 0x48 /* 16-bit, little endian */
68 #define RPR0521_DEFAULT_MEAS_TIME 0x06 /* ALS - 100ms, PXS - 100ms */
170 {2, 500000, 20, 0}, /* 1000, measurement 100ms, sleep 300ms */
171 {2, 500000, 10, 0}, /* 1001, measurement 100ms, sleep 300ms */
190 /* optimize runtime pm ops - enable/disable device only if needed */
297 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL, in rpr0521_als_enable()
304 data->als_dev_en = true; in rpr0521_als_enable()
306 data->als_dev_en = false; in rpr0521_als_enable()
315 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL, in rpr0521_pxs_enable()
322 data->pxs_dev_en = true; in rpr0521_pxs_enable()
324 data->pxs_dev_en = false; in rpr0521_pxs_enable()
330 * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
346 data->als_ps_need_en = on; in rpr0521_set_power_state()
347 data->als_need_dis = !on; in rpr0521_set_power_state()
351 data->pxs_ps_need_en = on; in rpr0521_set_power_state()
352 data->pxs_need_dis = !on; in rpr0521_set_power_state()
359 * Note: If either measurement is re-enabled before _suspend(), in rpr0521_set_power_state()
363 ret = pm_runtime_resume_and_get(&data->client->dev); in rpr0521_set_power_state()
365 pm_runtime_mark_last_busy(&data->client->dev); in rpr0521_set_power_state()
366 ret = pm_runtime_put_autosuspend(&data->client->dev); in rpr0521_set_power_state()
369 dev_err(&data->client->dev, in rpr0521_set_power_state()
377 if (data->als_ps_need_en) { in rpr0521_set_power_state()
381 data->als_ps_need_en = false; in rpr0521_set_power_state()
384 if (data->pxs_ps_need_en) { in rpr0521_set_power_state()
388 data->pxs_ps_need_en = false; in rpr0521_set_power_state()
401 ret = regmap_read(data->regmap, RPR0521_REG_INTERRUPT, &reg); in rpr0521_is_triggered()
418 data->irq_timestamp = iio_get_time_ns(indio_dev); in rpr0521_drdy_irq_handler()
434 iio_trigger_poll_nested(data->drdy_trigger0); in rpr0521_drdy_irq_thread()
444 struct iio_dev *indio_dev = pf->indio_dev; in rpr0521_trigger_consumer_store_time()
446 /* Other trigger polls store time here. */ in rpr0521_trigger_consumer_store_time()
448 pf->timestamp = iio_get_time_ns(indio_dev); in rpr0521_trigger_consumer_store_time()
456 struct iio_dev *indio_dev = pf->indio_dev; in rpr0521_trigger_consumer_handler()
461 if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) { in rpr0521_trigger_consumer_handler()
462 pf->timestamp = data->irq_timestamp; in rpr0521_trigger_consumer_handler()
463 data->irq_timestamp = 0; in rpr0521_trigger_consumer_handler()
466 if (!pf->timestamp) in rpr0521_trigger_consumer_handler()
467 pf->timestamp = iio_get_time_ns(indio_dev); in rpr0521_trigger_consumer_handler()
469 err = regmap_bulk_read(data->regmap, RPR0521_REG_PXS_DATA, in rpr0521_trigger_consumer_handler()
470 data->scan.channels, in rpr0521_trigger_consumer_handler()
471 (3 * 2) + 1); /* 3 * 16-bit + (discarded) int clear reg. */ in rpr0521_trigger_consumer_handler()
474 &data->scan, pf->timestamp); in rpr0521_trigger_consumer_handler()
476 dev_err(&data->client->dev, in rpr0521_trigger_consumer_handler()
478 pf->timestamp = 0; in rpr0521_trigger_consumer_handler()
480 iio_trigger_notify_done(indio_dev->trig); in rpr0521_trigger_consumer_handler()
490 err = regmap_update_bits(data->regmap, RPR0521_REG_PXS_CTRL, in rpr0521_write_int_enable()
494 dev_err(&data->client->dev, "PS control reg write fail.\n"); in rpr0521_write_int_enable()
495 return -EBUSY; in rpr0521_write_int_enable()
499 err = regmap_write(data->regmap, RPR0521_REG_INTERRUPT, in rpr0521_write_int_enable()
505 dev_err(&data->client->dev, "Interrupt setup write fail.\n"); in rpr0521_write_int_enable()
506 return -EBUSY; in rpr0521_write_int_enable()
514 /* Don't care of clearing mode, assert and latch. */ in rpr0521_write_int_disable()
515 return regmap_write(data->regmap, RPR0521_REG_INTERRUPT, in rpr0521_write_int_disable()
537 dev_err(&data->client->dev, "rpr0521_pxs_drdy_set_state failed\n"); in rpr0521_pxs_drdy_set_state()
552 mutex_lock(&data->lock); in rpr0521_buffer_preenable()
555 mutex_unlock(&data->lock); in rpr0521_buffer_preenable()
557 dev_err(&data->client->dev, "_buffer_preenable fail\n"); in rpr0521_buffer_preenable()
567 mutex_lock(&data->lock); in rpr0521_buffer_postdisable()
570 mutex_unlock(&data->lock); in rpr0521_buffer_postdisable()
572 dev_err(&data->client->dev, "_buffer_postdisable fail\n"); in rpr0521_buffer_postdisable()
587 ret = regmap_read(data->regmap, rpr0521_gain[chan].reg, &reg); in rpr0521_get_gain()
601 int i, idx = -EINVAL; in rpr0521_set_gain()
614 return regmap_update_bits(data->regmap, rpr0521_gain[chan].reg, in rpr0521_set_gain()
625 ret = regmap_read(data->regmap, RPR0521_REG_MODE_CTRL, &reg); in rpr0521_read_samp_freq()
631 return -EINVAL; in rpr0521_read_samp_freq()
645 return -EINVAL; in rpr0521_read_samp_freq()
666 return -EINVAL; in rpr0521_write_samp_freq_common()
676 return -EINVAL; in rpr0521_write_samp_freq_common()
679 return regmap_update_bits(data->regmap, in rpr0521_write_samp_freq_common()
690 ret = regmap_bulk_read(data->regmap, in rpr0521_read_ps_offset()
694 dev_err(&data->client->dev, "Failed to read PS OFFSET register\n"); in rpr0521_read_ps_offset()
708 ret = regmap_raw_write(data->regmap, in rpr0521_write_ps_offset()
712 dev_err(&data->client->dev, "Failed to write PS OFFSET register\n"); in rpr0521_write_ps_offset()
731 if (chan->type != IIO_INTENSITY && chan->type != IIO_PROXIMITY) in rpr0521_read_raw()
732 return -EINVAL; in rpr0521_read_raw()
736 return -EBUSY; in rpr0521_read_raw()
738 device_mask = rpr0521_data_reg[chan->address].device_mask; in rpr0521_read_raw()
740 mutex_lock(&data->lock); in rpr0521_read_raw()
745 ret = regmap_bulk_read(data->regmap, in rpr0521_read_raw()
746 rpr0521_data_reg[chan->address].address, in rpr0521_read_raw()
756 mutex_unlock(&data->lock); in rpr0521_read_raw()
766 mutex_lock(&data->lock); in rpr0521_read_raw()
767 ret = rpr0521_get_gain(data, chan->address, val, val2); in rpr0521_read_raw()
768 mutex_unlock(&data->lock); in rpr0521_read_raw()
775 mutex_lock(&data->lock); in rpr0521_read_raw()
776 ret = rpr0521_read_samp_freq(data, chan->type, val, val2); in rpr0521_read_raw()
777 mutex_unlock(&data->lock); in rpr0521_read_raw()
784 mutex_lock(&data->lock); in rpr0521_read_raw()
786 mutex_unlock(&data->lock); in rpr0521_read_raw()
793 return -EINVAL; in rpr0521_read_raw()
806 mutex_lock(&data->lock); in rpr0521_write_raw()
807 ret = rpr0521_set_gain(data, chan->address, val, val2); in rpr0521_write_raw()
808 mutex_unlock(&data->lock); in rpr0521_write_raw()
813 mutex_lock(&data->lock); in rpr0521_write_raw()
814 ret = rpr0521_write_samp_freq_common(data, chan->type, in rpr0521_write_raw()
816 mutex_unlock(&data->lock); in rpr0521_write_raw()
821 mutex_lock(&data->lock); in rpr0521_write_raw()
823 mutex_unlock(&data->lock); in rpr0521_write_raw()
828 return -EINVAL; in rpr0521_write_raw()
843 ret = regmap_read(data->regmap, RPR0521_REG_ID, &id); in rpr0521_init()
845 dev_err(&data->client->dev, "Failed to read REG_ID register\n"); in rpr0521_init()
850 dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n", in rpr0521_init()
852 return -ENODEV; in rpr0521_init()
855 /* set default measurement time - 100 ms for both ALS and PS */ in rpr0521_init()
856 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL, in rpr0521_init()
873 data->irq_timestamp = 0; in rpr0521_init()
883 ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL, in rpr0521_poweroff()
891 data->als_dev_en = false; in rpr0521_poweroff()
892 data->pxs_dev_en = false; in rpr0521_poweroff()
895 * Int pin keeps state after power off. Set pin to high impedance in rpr0521_poweroff()
898 ret = regmap_read(data->regmap, RPR0521_REG_INTERRUPT, &tmp); in rpr0521_poweroff()
900 dev_err(&data->client->dev, "Failed to reset int pin.\n"); in rpr0521_poweroff()
937 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in rpr0521_probe()
939 return -ENOMEM; in rpr0521_probe()
943 dev_err(&client->dev, "regmap_init failed!\n"); in rpr0521_probe()
949 data->client = client; in rpr0521_probe()
950 data->regmap = regmap; in rpr0521_probe()
952 mutex_init(&data->lock); in rpr0521_probe()
954 indio_dev->info = &rpr0521_info; in rpr0521_probe()
955 indio_dev->name = RPR0521_DRV_NAME; in rpr0521_probe()
956 indio_dev->channels = rpr0521_channels; in rpr0521_probe()
957 indio_dev->num_channels = ARRAY_SIZE(rpr0521_channels); in rpr0521_probe()
958 indio_dev->modes = INDIO_DIRECT_MODE; in rpr0521_probe()
962 dev_err(&client->dev, "rpr0521 chip init failed\n"); in rpr0521_probe()
966 ret = pm_runtime_set_active(&client->dev); in rpr0521_probe()
970 pm_runtime_enable(&client->dev); in rpr0521_probe()
971 pm_runtime_set_autosuspend_delay(&client->dev, RPR0521_SLEEP_DELAY_MS); in rpr0521_probe()
972 pm_runtime_use_autosuspend(&client->dev); in rpr0521_probe()
980 if (client->irq) { in rpr0521_probe()
982 data->drdy_trigger0 = devm_iio_trigger_alloc( in rpr0521_probe()
983 indio_dev->dev.parent, in rpr0521_probe()
984 "%s-dev%d", indio_dev->name, iio_device_id(indio_dev)); in rpr0521_probe()
985 if (!data->drdy_trigger0) { in rpr0521_probe()
986 ret = -ENOMEM; in rpr0521_probe()
989 data->drdy_trigger0->ops = &rpr0521_trigger_ops; in rpr0521_probe()
990 indio_dev->available_scan_masks = rpr0521_available_scan_masks; in rpr0521_probe()
991 iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev); in rpr0521_probe()
994 ret = devm_request_threaded_irq(&client->dev, client->irq, in rpr0521_probe()
999 dev_err(&client->dev, "request irq %d for trigger0 failed\n", in rpr0521_probe()
1000 client->irq); in rpr0521_probe()
1004 ret = devm_iio_trigger_register(indio_dev->dev.parent, in rpr0521_probe()
1005 data->drdy_trigger0); in rpr0521_probe()
1007 dev_err(&client->dev, "iio trigger register failed\n"); in rpr0521_probe()
1017 ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent, in rpr0521_probe()
1023 dev_err(&client->dev, "iio triggered buffer setup failed\n"); in rpr0521_probe()
1035 pm_runtime_disable(&client->dev); in rpr0521_probe()
1036 pm_runtime_set_suspended(&client->dev); in rpr0521_probe()
1049 pm_runtime_disable(&client->dev); in rpr0521_remove()
1050 pm_runtime_set_suspended(&client->dev); in rpr0521_remove()
1061 mutex_lock(&data->lock); in rpr0521_runtime_suspend()
1063 if (!data->als_need_dis) in rpr0521_runtime_suspend()
1064 data->als_ps_need_en = data->als_dev_en; in rpr0521_runtime_suspend()
1065 if (!data->pxs_need_dis) in rpr0521_runtime_suspend()
1066 data->pxs_ps_need_en = data->pxs_dev_en; in rpr0521_runtime_suspend()
1070 regcache_mark_dirty(data->regmap); in rpr0521_runtime_suspend()
1071 mutex_unlock(&data->lock); in rpr0521_runtime_suspend()
1082 regcache_sync(data->regmap); in rpr0521_runtime_resume()
1083 if (data->als_ps_need_en) { in rpr0521_runtime_resume()
1087 data->als_ps_need_en = false; in rpr0521_runtime_resume()
1090 if (data->pxs_ps_need_en) { in rpr0521_runtime_resume()
1094 data->pxs_ps_need_en = false; in rpr0521_runtime_resume()