Lines Matching +full:adc +full:- +full:use +full:- +full:res

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
111 * Translation from PGA bits to full-scale positive and negative input voltage
118 static const int ads1015_scale[] = { /* 12bit ADC */
127 static const int ads1115_scale[] = { /* 16bit ADC */
161 * Compile-time check whether _fitbits can accommodate up to _testbits
165 * double-negation of size of a non-empty structure, i.e. it multiplies
167 * The non-empty structure may contain C11 _Static_assert(), make use of
169 * it performs the compile-time check for _testbits <= _fitbits. Note
170 * that it is not possible to directly use static_assert in compound
229 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
247 * Protects ADC ops, e.g: concurrent sysfs/buffered
259 * Set to true when the ADC is switched to the continuous-conversion
260 * mode and exits from a power-down state. This flag is used to avoid
268 return (data->event_channel != ADS1015_CHANNELS); in ads1015_event_channel_enabled()
276 data->event_channel = chan; in ads1015_event_channel_enable()
277 data->comp_mode = comp_mode; in ads1015_event_channel_enable()
282 data->event_channel = ADS1015_CHANNELS; in ads1015_event_channel_disable()
374 struct device *dev = regmap_get_device(data->regmap); in ads1015_set_power_state()
398 const int *data_rate = data->chip->data_rate; in ads1015_get_adc_result()
403 return -EINVAL; in ads1015_get_adc_result()
405 ret = regmap_read(data->regmap, ADS1015_CFG_REG, &old); in ads1015_get_adc_result()
409 pga = data->channel_data[chan].pga; in ads1015_get_adc_result()
410 dr = data->channel_data[chan].data_rate; in ads1015_get_adc_result()
418 cfg |= data->thresh_data[chan].comp_queue << in ads1015_get_adc_result()
420 data->comp_mode << in ads1015_get_adc_result()
426 ret = regmap_write(data->regmap, ADS1015_CFG_REG, cfg); in ads1015_get_adc_result()
429 data->conv_invalid = true; in ads1015_get_adc_result()
431 if (data->conv_invalid) { in ads1015_get_adc_result()
437 data->conv_invalid = false; in ads1015_get_adc_result()
440 return regmap_read(data->regmap, ADS1015_CONV_REG, val); in ads1015_get_adc_result()
446 struct iio_dev *indio_dev = pf->indio_dev; in ads1015_trigger_handler()
453 int chan, ret, res; in ads1015_trigger_handler() local
457 mutex_lock(&data->lock); in ads1015_trigger_handler()
458 chan = find_first_bit(indio_dev->active_scan_mask, in ads1015_trigger_handler()
460 ret = ads1015_get_adc_result(data, chan, &res); in ads1015_trigger_handler()
462 mutex_unlock(&data->lock); in ads1015_trigger_handler()
466 scan.chan = res; in ads1015_trigger_handler()
467 mutex_unlock(&data->lock); in ads1015_trigger_handler()
473 iio_trigger_notify_done(indio_dev->trig); in ads1015_trigger_handler()
484 (chan->scan_type.realbits - 1), 1000000); in ads1015_set_scale()
488 data->channel_data[chan->address].pga = i; in ads1015_set_scale()
493 return -EINVAL; in ads1015_set_scale()
500 for (i = 0; i < data->chip->data_rate_len; i++) { in ads1015_set_data_rate()
501 if (data->chip->data_rate[i] == rate) { in ads1015_set_data_rate()
502 data->channel_data[chan].data_rate = i; in ads1015_set_data_rate()
507 return -EINVAL; in ads1015_set_data_rate()
517 if (chan->type != IIO_VOLTAGE) in ads1015_read_avail()
518 return -EINVAL; in ads1015_read_avail()
523 *vals = data->chip->scale; in ads1015_read_avail()
524 *length = data->chip->scale_len; in ads1015_read_avail()
528 *vals = data->chip->data_rate; in ads1015_read_avail()
529 *length = data->chip->data_rate_len; in ads1015_read_avail()
532 return -EINVAL; in ads1015_read_avail()
543 mutex_lock(&data->lock); in ads1015_read_raw()
551 data->event_channel != chan->address) { in ads1015_read_raw()
552 ret = -EBUSY; in ads1015_read_raw()
560 ret = ads1015_get_adc_result(data, chan->address, val); in ads1015_read_raw()
566 *val = sign_extend32(*val >> chan->scan_type.shift, in ads1015_read_raw()
567 chan->scan_type.realbits - 1); in ads1015_read_raw()
578 idx = data->channel_data[chan->address].pga; in ads1015_read_raw()
580 *val2 = chan->scan_type.realbits - 1; in ads1015_read_raw()
584 idx = data->channel_data[chan->address].data_rate; in ads1015_read_raw()
585 *val = data->chip->data_rate[idx]; in ads1015_read_raw()
589 ret = -EINVAL; in ads1015_read_raw()
592 mutex_unlock(&data->lock); in ads1015_read_raw()
604 mutex_lock(&data->lock); in ads1015_write_raw()
610 ret = ads1015_set_data_rate(data, chan->address, val); in ads1015_write_raw()
613 ret = -EINVAL; in ads1015_write_raw()
616 mutex_unlock(&data->lock); in ads1015_write_raw()
632 mutex_lock(&data->lock); in ads1015_read_event()
637 data->thresh_data[chan->address].high_thresh : in ads1015_read_event()
638 data->thresh_data[chan->address].low_thresh; in ads1015_read_event()
642 dr = data->channel_data[chan->address].data_rate; in ads1015_read_event()
643 comp_queue = data->thresh_data[chan->address].comp_queue; in ads1015_read_event()
645 USEC_PER_SEC / data->chip->data_rate[dr]; in ads1015_read_event()
652 ret = -EINVAL; in ads1015_read_event()
656 mutex_unlock(&data->lock); in ads1015_read_event()
667 const int *data_rate = data->chip->data_rate; in ads1015_write_event()
668 int realbits = chan->scan_type.realbits; in ads1015_write_event()
674 mutex_lock(&data->lock); in ads1015_write_event()
678 if (val >= 1 << (realbits - 1) || val < -1 << (realbits - 1)) { in ads1015_write_event()
679 ret = -EINVAL; in ads1015_write_event()
683 data->thresh_data[chan->address].high_thresh = val; in ads1015_write_event()
685 data->thresh_data[chan->address].low_thresh = val; in ads1015_write_event()
688 dr = data->channel_data[chan->address].data_rate; in ads1015_write_event()
691 for (i = 0; i < ARRAY_SIZE(ads1015_comp_queue) - 1; i++) { in ads1015_write_event()
696 data->thresh_data[chan->address].comp_queue = i; in ads1015_write_event()
699 ret = -EINVAL; in ads1015_write_event()
703 mutex_unlock(&data->lock); in ads1015_write_event()
715 mutex_lock(&data->lock); in ads1015_read_event_config()
716 if (data->event_channel == chan->address) { in ads1015_read_event_config()
722 ret = (data->comp_mode == ADS1015_CFG_COMP_MODE_WINDOW); in ads1015_read_event_config()
725 ret = -EINVAL; in ads1015_read_event_config()
729 mutex_unlock(&data->lock); in ads1015_read_event_config()
737 int low_thresh = data->thresh_data[chan->address].low_thresh; in ads1015_enable_event_config()
738 int high_thresh = data->thresh_data[chan->address].high_thresh; in ads1015_enable_event_config()
743 if (data->event_channel != chan->address || in ads1015_enable_event_config()
744 (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD && in ads1015_enable_event_config()
746 return -EBUSY; in ads1015_enable_event_config()
752 low_thresh = max(-1 << (chan->scan_type.realbits - 1), in ads1015_enable_event_config()
753 high_thresh - 1); in ads1015_enable_event_config()
755 ret = regmap_write(data->regmap, ADS1015_LO_THRESH_REG, in ads1015_enable_event_config()
756 low_thresh << chan->scan_type.shift); in ads1015_enable_event_config()
760 ret = regmap_write(data->regmap, ADS1015_HI_THRESH_REG, in ads1015_enable_event_config()
761 high_thresh << chan->scan_type.shift); in ads1015_enable_event_config()
769 ads1015_event_channel_enable(data, chan->address, comp_mode); in ads1015_enable_event_config()
771 ret = ads1015_get_adc_result(data, chan->address, &val); in ads1015_enable_event_config()
773 ads1015_event_channel_disable(data, chan->address); in ads1015_enable_event_config()
788 if (data->event_channel != chan->address) in ads1015_disable_event_config()
791 if (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD && in ads1015_disable_event_config()
795 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG, in ads1015_disable_event_config()
802 ads1015_event_channel_disable(data, chan->address); in ads1015_disable_event_config()
816 mutex_lock(&data->lock); in ads1015_write_event_config()
821 mutex_unlock(&data->lock); in ads1015_write_event_config()
831 mutex_unlock(&data->lock); in ads1015_write_event_config()
844 ret = regmap_read(data->regmap, ADS1015_CONV_REG, &val); in ads1015_event_handler()
852 dir = data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD ? in ads1015_event_handler()
854 code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, data->event_channel, in ads1015_event_handler()
868 return -EBUSY; in ads1015_buffer_preenable()
904 struct device *dev = &client->dev; in ads1015_client_get_channels_config()
905 int i = -1; in ads1015_client_get_channels_config()
929 return -EINVAL; in ads1015_client_get_channels_config()
937 return -EINVAL; in ads1015_client_get_channels_config()
941 data->channel_data[channel].pga = pga; in ads1015_client_get_channels_config()
942 data->channel_data[channel].data_rate = data_rate; in ads1015_client_get_channels_config()
947 return i < 0 ? -EINVAL : 0; in ads1015_client_get_channels_config()
962 data->channel_data[k].pga = ADS1015_DEFAULT_PGA; in ads1015_get_channels_config()
963 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE; in ads1015_get_channels_config()
969 return regmap_update_bits(data->regmap, ADS1015_CFG_REG, in ads1015_set_conv_mode()
984 return dev_err_probe(&client->dev, -EINVAL, "Unknown chip\n"); in ads1015_probe()
986 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in ads1015_probe()
988 return -ENOMEM; in ads1015_probe()
993 mutex_init(&data->lock); in ads1015_probe()
995 indio_dev->name = ADS1015_DRV_NAME; in ads1015_probe()
996 indio_dev->modes = INDIO_DIRECT_MODE; in ads1015_probe()
998 indio_dev->channels = chip->channels; in ads1015_probe()
999 indio_dev->num_channels = chip->num_channels; in ads1015_probe()
1000 indio_dev->info = chip->info; in ads1015_probe()
1001 data->chip = chip; in ads1015_probe()
1002 data->event_channel = ADS1015_CHANNELS; in ads1015_probe()
1009 int realbits = indio_dev->channels[i].scan_type.realbits; in ads1015_probe()
1011 data->thresh_data[i].low_thresh = -1 << (realbits - 1); in ads1015_probe()
1012 data->thresh_data[i].high_thresh = (1 << (realbits - 1)) - 1; in ads1015_probe()
1018 data->regmap = devm_regmap_init_i2c(client, chip->has_comparator ? in ads1015_probe()
1021 if (IS_ERR(data->regmap)) { in ads1015_probe()
1022 dev_err(&client->dev, "Failed to allocate register map\n"); in ads1015_probe()
1023 return PTR_ERR(data->regmap); in ads1015_probe()
1026 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, in ads1015_probe()
1030 dev_err(&client->dev, "iio triggered buffer setup failed\n"); in ads1015_probe()
1034 if (client->irq && chip->has_comparator) { in ads1015_probe()
1036 irqd_get_trigger_type(irq_get_irq_data(client->irq)); in ads1015_probe()
1055 return -EINVAL; in ads1015_probe()
1058 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG, in ads1015_probe()
1063 ret = devm_request_threaded_irq(&client->dev, client->irq, in ads1015_probe()
1066 client->name, indio_dev); in ads1015_probe()
1075 data->conv_invalid = true; in ads1015_probe()
1077 ret = pm_runtime_set_active(&client->dev); in ads1015_probe()
1080 pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS); in ads1015_probe()
1081 pm_runtime_use_autosuspend(&client->dev); in ads1015_probe()
1082 pm_runtime_enable(&client->dev); in ads1015_probe()
1086 dev_err(&client->dev, "Failed to register IIO device\n"); in ads1015_probe()
1101 pm_runtime_disable(&client->dev); in ads1015_remove()
1102 pm_runtime_set_suspended(&client->dev); in ads1015_remove()
1107 dev_warn(&client->dev, "Failed to power down (%pe)\n", in ads1015_remove()
1128 data->conv_invalid = true; in ads1015_runtime_resume()
1202 MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");