Lines Matching +full:buffered +full:- +full:positive

1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADS1015 - Texas Instruments Analog-to-Digital Converter
7 * IIO driver for ADS1015 ADC 7-bit I2C slave address:
8 * * 0x48 - ADDR connected to Ground
9 * * 0x49 - ADDR connected to Vdd
10 * * 0x4A - ADDR connected to SDA
11 * * 0x4B - ADDR connected to SCL
106 * Translation from PGA bits to full-scale positive and negative input voltage
178 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
220 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
238 * Protects ADC ops, e.g: concurrent sysfs/buffered
250 * Set to true when the ADC is switched to the continuous-conversion
251 * mode and exits from a power-down state. This flag is used to avoid
259 return (data->event_channel != ADS1015_CHANNELS); in ads1015_event_channel_enabled()
267 data->event_channel = chan; in ads1015_event_channel_enable()
268 data->comp_mode = comp_mode; in ads1015_event_channel_enable()
273 data->event_channel = ADS1015_CHANNELS; in ads1015_event_channel_disable()
323 struct device *dev = regmap_get_device(data->regmap); in ads1015_set_power_state()
351 return -EINVAL; in ads1015_get_adc_result()
353 ret = regmap_read(data->regmap, ADS1015_CFG_REG, &old); in ads1015_get_adc_result()
357 pga = data->channel_data[chan].pga; in ads1015_get_adc_result()
358 dr = data->channel_data[chan].data_rate; in ads1015_get_adc_result()
366 cfg |= data->thresh_data[chan].comp_queue << in ads1015_get_adc_result()
368 data->comp_mode << in ads1015_get_adc_result()
374 ret = regmap_write(data->regmap, ADS1015_CFG_REG, cfg); in ads1015_get_adc_result()
377 data->conv_invalid = true; in ads1015_get_adc_result()
379 if (data->conv_invalid) { in ads1015_get_adc_result()
381 conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr_old]); in ads1015_get_adc_result()
382 conv_time += DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]); in ads1015_get_adc_result()
385 data->conv_invalid = false; in ads1015_get_adc_result()
388 return regmap_read(data->regmap, ADS1015_CONV_REG, val); in ads1015_get_adc_result()
394 struct iio_dev *indio_dev = pf->indio_dev; in ads1015_trigger_handler()
405 mutex_lock(&data->lock); in ads1015_trigger_handler()
406 chan = find_first_bit(indio_dev->active_scan_mask, in ads1015_trigger_handler()
407 indio_dev->masklength); in ads1015_trigger_handler()
410 mutex_unlock(&data->lock); in ads1015_trigger_handler()
415 mutex_unlock(&data->lock); in ads1015_trigger_handler()
421 iio_trigger_notify_done(indio_dev->trig); in ads1015_trigger_handler()
432 (chan->scan_type.realbits - 1), 1000000); in ads1015_set_scale()
436 data->channel_data[chan->address].pga = i; in ads1015_set_scale()
441 return -EINVAL; in ads1015_set_scale()
449 if (data->data_rate[i] == rate) { in ads1015_set_data_rate()
450 data->channel_data[chan].data_rate = i; in ads1015_set_data_rate()
455 return -EINVAL; in ads1015_set_data_rate()
465 mutex_lock(&data->lock); in ads1015_read_raw()
468 int shift = chan->scan_type.shift; in ads1015_read_raw()
475 data->event_channel != chan->address) { in ads1015_read_raw()
476 ret = -EBUSY; in ads1015_read_raw()
484 ret = ads1015_get_adc_result(data, chan->address, val); in ads1015_read_raw()
490 *val = sign_extend32(*val >> shift, 15 - shift); in ads1015_read_raw()
502 idx = data->channel_data[chan->address].pga; in ads1015_read_raw()
504 *val2 = chan->scan_type.realbits - 1; in ads1015_read_raw()
508 idx = data->channel_data[chan->address].data_rate; in ads1015_read_raw()
509 *val = data->data_rate[idx]; in ads1015_read_raw()
513 ret = -EINVAL; in ads1015_read_raw()
516 mutex_unlock(&data->lock); in ads1015_read_raw()
528 mutex_lock(&data->lock); in ads1015_write_raw()
534 ret = ads1015_set_data_rate(data, chan->address, val); in ads1015_write_raw()
537 ret = -EINVAL; in ads1015_write_raw()
540 mutex_unlock(&data->lock); in ads1015_write_raw()
556 mutex_lock(&data->lock); in ads1015_read_event()
561 data->thresh_data[chan->address].high_thresh : in ads1015_read_event()
562 data->thresh_data[chan->address].low_thresh; in ads1015_read_event()
566 dr = data->channel_data[chan->address].data_rate; in ads1015_read_event()
567 comp_queue = data->thresh_data[chan->address].comp_queue; in ads1015_read_event()
569 USEC_PER_SEC / data->data_rate[dr]; in ads1015_read_event()
576 ret = -EINVAL; in ads1015_read_event()
580 mutex_unlock(&data->lock); in ads1015_read_event()
591 int realbits = chan->scan_type.realbits; in ads1015_write_event()
597 mutex_lock(&data->lock); in ads1015_write_event()
601 if (val >= 1 << (realbits - 1) || val < -1 << (realbits - 1)) { in ads1015_write_event()
602 ret = -EINVAL; in ads1015_write_event()
606 data->thresh_data[chan->address].high_thresh = val; in ads1015_write_event()
608 data->thresh_data[chan->address].low_thresh = val; in ads1015_write_event()
611 dr = data->channel_data[chan->address].data_rate; in ads1015_write_event()
614 for (i = 0; i < ARRAY_SIZE(ads1015_comp_queue) - 1; i++) { in ads1015_write_event()
616 USEC_PER_SEC / data->data_rate[dr]) in ads1015_write_event()
619 data->thresh_data[chan->address].comp_queue = i; in ads1015_write_event()
622 ret = -EINVAL; in ads1015_write_event()
626 mutex_unlock(&data->lock); in ads1015_write_event()
638 mutex_lock(&data->lock); in ads1015_read_event_config()
639 if (data->event_channel == chan->address) { in ads1015_read_event_config()
645 ret = (data->comp_mode == ADS1015_CFG_COMP_MODE_WINDOW); in ads1015_read_event_config()
648 ret = -EINVAL; in ads1015_read_event_config()
652 mutex_unlock(&data->lock); in ads1015_read_event_config()
660 int low_thresh = data->thresh_data[chan->address].low_thresh; in ads1015_enable_event_config()
661 int high_thresh = data->thresh_data[chan->address].high_thresh; in ads1015_enable_event_config()
666 if (data->event_channel != chan->address || in ads1015_enable_event_config()
667 (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD && in ads1015_enable_event_config()
669 return -EBUSY; in ads1015_enable_event_config()
675 low_thresh = max(-1 << (chan->scan_type.realbits - 1), in ads1015_enable_event_config()
676 high_thresh - 1); in ads1015_enable_event_config()
678 ret = regmap_write(data->regmap, ADS1015_LO_THRESH_REG, in ads1015_enable_event_config()
679 low_thresh << chan->scan_type.shift); in ads1015_enable_event_config()
683 ret = regmap_write(data->regmap, ADS1015_HI_THRESH_REG, in ads1015_enable_event_config()
684 high_thresh << chan->scan_type.shift); in ads1015_enable_event_config()
692 ads1015_event_channel_enable(data, chan->address, comp_mode); in ads1015_enable_event_config()
694 ret = ads1015_get_adc_result(data, chan->address, &val); in ads1015_enable_event_config()
696 ads1015_event_channel_disable(data, chan->address); in ads1015_enable_event_config()
711 if (data->event_channel != chan->address) in ads1015_disable_event_config()
714 if (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD && in ads1015_disable_event_config()
718 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG, in ads1015_disable_event_config()
725 ads1015_event_channel_disable(data, chan->address); in ads1015_disable_event_config()
739 mutex_lock(&data->lock); in ads1015_write_event_config()
744 mutex_unlock(&data->lock); in ads1015_write_event_config()
754 mutex_unlock(&data->lock); in ads1015_write_event_config()
767 ret = regmap_read(data->regmap, ADS1015_CONV_REG, &val); in ads1015_event_handler()
775 dir = data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD ? in ads1015_event_handler()
777 code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, data->event_channel, in ads1015_event_handler()
791 return -EBUSY; in ads1015_buffer_preenable()
861 struct device *dev = &client->dev; in ads1015_client_get_channels_config()
863 int i = -1; in ads1015_client_get_channels_config()
888 return -EINVAL; in ads1015_client_get_channels_config()
897 return -EINVAL; in ads1015_client_get_channels_config()
901 data->channel_data[channel].pga = pga; in ads1015_client_get_channels_config()
902 data->channel_data[channel].data_rate = data_rate; in ads1015_client_get_channels_config()
907 return i < 0 ? -EINVAL : 0; in ads1015_client_get_channels_config()
922 data->channel_data[k].pga = ADS1015_DEFAULT_PGA; in ads1015_get_channels_config()
923 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE; in ads1015_get_channels_config()
929 return regmap_update_bits(data->regmap, ADS1015_CFG_REG, in ads1015_set_conv_mode()
943 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in ads1015_probe()
945 return -ENOMEM; in ads1015_probe()
950 mutex_init(&data->lock); in ads1015_probe()
952 indio_dev->name = ADS1015_DRV_NAME; in ads1015_probe()
953 indio_dev->modes = INDIO_DIRECT_MODE; in ads1015_probe()
955 chip = (enum chip_ids)device_get_match_data(&client->dev); in ads1015_probe()
957 chip = id->driver_data; in ads1015_probe()
960 indio_dev->channels = ads1015_channels; in ads1015_probe()
961 indio_dev->num_channels = ARRAY_SIZE(ads1015_channels); in ads1015_probe()
962 indio_dev->info = &ads1015_info; in ads1015_probe()
963 data->data_rate = (unsigned int *) &ads1015_data_rate; in ads1015_probe()
966 indio_dev->channels = ads1115_channels; in ads1015_probe()
967 indio_dev->num_channels = ARRAY_SIZE(ads1115_channels); in ads1015_probe()
968 indio_dev->info = &ads1115_info; in ads1015_probe()
969 data->data_rate = (unsigned int *) &ads1115_data_rate; in ads1015_probe()
972 dev_err(&client->dev, "Unknown chip %d\n", chip); in ads1015_probe()
973 return -EINVAL; in ads1015_probe()
976 data->event_channel = ADS1015_CHANNELS; in ads1015_probe()
982 int realbits = indio_dev->channels[i].scan_type.realbits; in ads1015_probe()
984 data->thresh_data[i].low_thresh = -1 << (realbits - 1); in ads1015_probe()
985 data->thresh_data[i].high_thresh = (1 << (realbits - 1)) - 1; in ads1015_probe()
991 data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config); in ads1015_probe()
992 if (IS_ERR(data->regmap)) { in ads1015_probe()
993 dev_err(&client->dev, "Failed to allocate register map\n"); in ads1015_probe()
994 return PTR_ERR(data->regmap); in ads1015_probe()
997 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, in ads1015_probe()
1001 dev_err(&client->dev, "iio triggered buffer setup failed\n"); in ads1015_probe()
1005 if (client->irq) { in ads1015_probe()
1007 irqd_get_trigger_type(irq_get_irq_data(client->irq)); in ads1015_probe()
1024 return -EINVAL; in ads1015_probe()
1027 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG, in ads1015_probe()
1032 ret = devm_request_threaded_irq(&client->dev, client->irq, in ads1015_probe()
1035 client->name, indio_dev); in ads1015_probe()
1044 data->conv_invalid = true; in ads1015_probe()
1046 ret = pm_runtime_set_active(&client->dev); in ads1015_probe()
1049 pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS); in ads1015_probe()
1050 pm_runtime_use_autosuspend(&client->dev); in ads1015_probe()
1051 pm_runtime_enable(&client->dev); in ads1015_probe()
1055 dev_err(&client->dev, "Failed to register IIO device\n"); in ads1015_probe()
1069 pm_runtime_disable(&client->dev); in ads1015_remove()
1070 pm_runtime_set_suspended(&client->dev); in ads1015_remove()
1093 data->conv_invalid = true; in ads1015_runtime_resume()