Lines Matching +full:count +full:- +full:threshold
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Murata IRS-D200 PIR sensor.
30 #define IRS_REG_COUNT 0x05 /* Count of exceeding threshold. */
32 #define IRS_REG_FILTER 0x07 /* High-pass and low-pass filter. */
35 #define IRS_REG_THR_HI 0x0b /* Upper threshold. */
36 #define IRS_REG_THR_LO 0x0c /* Lower threshold. */
43 #define IRS_INTR_COUNT_THR_AND 2 /* Count "AND" threshold. */
44 #define IRS_INTR_COUNT_THR_OR 3 /* Count "OR" threshold. */
51 * Quantization scale value for threshold. Used for conversion from/to register
56 #define IRS_UPPER_COUNT(count) FIELD_GET(GENMASK(7, 4), count) argument
57 #define IRS_LOWER_COUNT(count) FIELD_GET(GENMASK(3, 0), count) argument
87 /* AND count threshold interrupt. */
89 /* OR count threshold interrupt. */
92 /* Low-pass filter frequency. */
94 /* High-pass filter frequency. */
133 ret = regmap_write(data->regmap, IRS_REG_INTR, 0); in irsd200_setup()
135 dev_err(data->dev, "Could not set interrupt sources (%d)\n", in irsd200_setup()
141 ret = regmap_write(data->regmap, IRS_REG_OP, IRS_OP_ACTIVE); in irsd200_setup()
143 dev_err(data->dev, "Could not set operation mode (%d)\n", ret); in irsd200_setup()
147 /* Clear threshold count. */ in irsd200_setup()
148 ret = regmap_read(data->regmap, IRS_REG_COUNT, &val); in irsd200_setup()
150 dev_err(data->dev, "Could not clear threshold count (%d)\n", in irsd200_setup()
156 ret = regmap_write(data->regmap, IRS_REG_STATUS, 0x0f); in irsd200_setup()
158 dev_err(data->dev, "Could not clear status (%d)\n", ret); in irsd200_setup()
178 scale = -IRS_THR_QUANT_SCALE; in irsd200_read_threshold()
181 return -EINVAL; in irsd200_read_threshold()
184 ret = regmap_read(data->regmap, reg, ®val); in irsd200_read_threshold()
186 dev_err(data->dev, "Could not read threshold (%d)\n", ret); in irsd200_read_threshold()
206 return -ERANGE; in irsd200_write_threshold()
212 return -ERANGE; in irsd200_write_threshold()
214 scale = -IRS_THR_QUANT_SCALE; in irsd200_write_threshold()
217 return -EINVAL; in irsd200_write_threshold()
223 return -ERANGE; in irsd200_write_threshold()
225 ret = regmap_write(data->regmap, reg, regval); in irsd200_write_threshold()
227 dev_err(data->dev, "Could not write threshold (%d)\n", ret); in irsd200_write_threshold()
239 ret = regmap_bulk_read(data->regmap, IRS_REG_DATA_LO, &buf, in irsd200_read_data()
242 dev_err(data->dev, "Could not bulk read data (%d)\n", ret); in irsd200_read_data()
256 ret = regmap_read(data->regmap, IRS_REG_DATA_RATE, ®val); in irsd200_read_data_rate()
258 dev_err(data->dev, "Could not read data rate (%d)\n", ret); in irsd200_read_data_rate()
263 return -ERANGE; in irsd200_read_data_rate()
281 return -ERANGE; in irsd200_write_data_rate()
283 ret = regmap_write(data->regmap, IRS_REG_DATA_RATE, idx); in irsd200_write_data_rate()
285 dev_err(data->dev, "Could not write data rate (%d)\n", ret); in irsd200_write_data_rate()
304 ret = regmap_bulk_read(data->regmap, IRS_REG_TIMER_LO, &buf, in irsd200_read_timer()
307 dev_err(data->dev, "Could not bulk read timer (%d)\n", ret); in irsd200_read_timer()
328 return -ERANGE; in irsd200_write_timer()
339 return -ERANGE; in irsd200_write_timer()
343 ret = regmap_bulk_write(data->regmap, IRS_REG_TIMER_LO, &buf, in irsd200_write_timer()
346 dev_err(data->dev, "Could not bulk write timer (%d)\n", ret); in irsd200_write_timer()
358 ret = regmap_read(data->regmap, IRS_REG_NR_COUNT, ®val); in irsd200_read_nr_count()
360 dev_err(data->dev, "Could not read nr count (%d)\n", ret); in irsd200_read_nr_count()
376 return -ERANGE; in irsd200_write_nr_count()
383 * case (i.e. be non-zero). Check and enforce that. in irsd200_write_nr_count()
390 dev_err(data->dev, in irsd200_write_nr_count()
391 "Timer must be non-zero when nr count is %u\n", in irsd200_write_nr_count()
393 return -EPERM; in irsd200_write_nr_count()
397 ret = regmap_write(data->regmap, IRS_REG_NR_COUNT, regval); in irsd200_write_nr_count()
399 dev_err(data->dev, "Could not write nr count (%d)\n", ret); in irsd200_write_nr_count()
411 ret = regmap_field_read(data->regfields[IRS_REGF_LP_FILTER], ®val); in irsd200_read_lp_filter()
413 dev_err(data->dev, "Could not read lp filter frequency (%d)\n", in irsd200_read_lp_filter()
434 return -ERANGE; in irsd200_write_lp_filter()
436 ret = regmap_field_write(data->regfields[IRS_REGF_LP_FILTER], idx); in irsd200_write_lp_filter()
438 dev_err(data->dev, "Could not write lp filter frequency (%d)\n", in irsd200_write_lp_filter()
452 ret = regmap_field_read(data->regfields[IRS_REGF_HP_FILTER], ®val); in irsd200_read_hp_filter()
454 dev_err(data->dev, "Could not read hp filter frequency (%d)\n", in irsd200_read_hp_filter()
479 return -ERANGE; in irsd200_write_hp_filter()
481 ret = regmap_field_write(data->regfields[IRS_REGF_HP_FILTER], idx); in irsd200_write_hp_filter()
483 dev_err(data->dev, "Could not write hp filter frequency (%d)\n", in irsd200_write_hp_filter()
526 return -EINVAL; in irsd200_read_raw()
552 return -EINVAL; in irsd200_read_avail()
570 return -EINVAL; in irsd200_write_raw()
603 return -EINVAL; in irsd200_read_event()
623 return -EINVAL; in irsd200_write_event()
639 data->regfields[IRS_REGF_INTR_COUNT_THR_OR], &val); in irsd200_read_event_config()
645 return -EINVAL; in irsd200_read_event_config()
660 /* Clear the count register (by reading from it). */ in irsd200_write_event_config()
661 ret = regmap_read(data->regmap, IRS_REG_COUNT, &tmp); in irsd200_write_event_config()
666 data->regfields[IRS_REGF_INTR_COUNT_THR_OR], !!state); in irsd200_write_event_config()
668 return -EINVAL; in irsd200_write_event_config()
682 unsigned int count = 0; in irsd200_irq_thread() local
685 ret = regmap_read(data->regmap, IRS_REG_INTR, &source); in irsd200_irq_thread()
687 dev_err(data->dev, "Could not read interrupt source (%d)\n", in irsd200_irq_thread()
692 ret = regmap_read(data->regmap, IRS_REG_STATUS, &status); in irsd200_irq_thread()
694 dev_err(data->dev, "Could not acknowledge interrupt (%d)\n", in irsd200_irq_thread()
700 iio_trigger_poll_nested(indio_dev->trig); in irsd200_irq_thread()
709 * count register fields. in irsd200_irq_thread()
711 ret = regmap_read(data->regmap, IRS_REG_COUNT, &count); in irsd200_irq_thread()
713 dev_err(data->dev, "Could not read count (%d)\n", ret); in irsd200_irq_thread()
715 upper_count = IRS_UPPER_COUNT(count); in irsd200_irq_thread()
716 lower_count = IRS_LOWER_COUNT(count); in irsd200_irq_thread()
721 * upper and lower count is non-zero, and is signaled with in irsd200_irq_thread()
739 * be cleared if data capture _and_ threshold count interrupts in irsd200_irq_thread()
750 ret = regmap_write(data->regmap, IRS_REG_STATUS, clear); in irsd200_irq_thread()
752 dev_err(data->dev, in irsd200_irq_thread()
760 struct iio_dev *indio_dev = ((struct iio_poll_func *)pollf)->indio_dev; in irsd200_trigger_handler()
773 iio_trigger_notify_done(indio_dev->trig); in irsd200_trigger_handler()
783 ret = regmap_field_write(data->regfields[IRS_REGF_INTR_DATA], state); in irsd200_set_trigger_state()
785 dev_err(data->dev, "Could not %s data interrupt source (%d)\n", in irsd200_set_trigger_state()
859 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in irsd200_probe()
861 return dev_err_probe(&client->dev, -ENOMEM, in irsd200_probe()
865 data->dev = &client->dev; in irsd200_probe()
867 data->regmap = devm_regmap_init_i2c(client, &irsd200_regmap_config); in irsd200_probe()
868 if (IS_ERR(data->regmap)) in irsd200_probe()
869 return dev_err_probe(data->dev, PTR_ERR(data->regmap), in irsd200_probe()
873 data->regfields[i] = devm_regmap_field_alloc( in irsd200_probe()
874 data->dev, data->regmap, irsd200_regfields[i]); in irsd200_probe()
875 if (IS_ERR(data->regfields[i])) in irsd200_probe()
877 data->dev, PTR_ERR(data->regfields[i]), in irsd200_probe()
881 ret = devm_regulator_get_enable(data->dev, "vdd"); in irsd200_probe()
884 data->dev, ret, in irsd200_probe()
891 indio_dev->info = &irsd200_info; in irsd200_probe()
892 indio_dev->name = IRS_DRV_NAME; in irsd200_probe()
893 indio_dev->channels = irsd200_channels; in irsd200_probe()
894 indio_dev->num_channels = ARRAY_SIZE(irsd200_channels); in irsd200_probe()
895 indio_dev->modes = INDIO_DIRECT_MODE; in irsd200_probe()
897 if (!client->irq) in irsd200_probe()
898 return dev_err_probe(data->dev, -ENXIO, "No irq available\n"); in irsd200_probe()
900 ret = devm_iio_triggered_buffer_setup(data->dev, indio_dev, NULL, in irsd200_probe()
904 data->dev, ret, in irsd200_probe()
907 ret = devm_request_threaded_irq(data->dev, client->irq, NULL, in irsd200_probe()
912 return dev_err_probe(data->dev, ret, in irsd200_probe()
915 trigger = devm_iio_trigger_alloc(data->dev, "%s-dev%d", indio_dev->name, in irsd200_probe()
918 return dev_err_probe(data->dev, -ENOMEM, in irsd200_probe()
921 trigger->ops = &irsd200_trigger_ops; in irsd200_probe()
924 ret = devm_iio_trigger_register(data->dev, trigger); in irsd200_probe()
926 return dev_err_probe(data->dev, ret, in irsd200_probe()
930 ret = devm_iio_device_register(data->dev, indio_dev); in irsd200_probe()
932 return dev_err_probe(data->dev, ret, in irsd200_probe()
957 MODULE_DESCRIPTION("Murata IRS-D200 PIR sensor driver");