Lines Matching +full:vref +full:- +full:mv
1 // SPDX-License-Identifier: GPL-2.0
2 /* ad7949.c - Analog Devices ADC driver 14/16 bits 4/8 channels
6 * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7949.pdf
33 /* BW: select bandwidth for low-pass filter. Full or Quarter */
68 * struct ad7949_adc_chip - AD ADC chip
70 * @vref: regulator generating Vref
78 * @buf8b: be16 buffer to exchange data with the device in 8-bit transfers
82 struct regulator *vref; member
98 ad7949_adc->cfg = (val & mask) | (ad7949_adc->cfg & ~mask); in ad7949_spi_write_cfg()
100 switch (ad7949_adc->spi->bits_per_word) { in ad7949_spi_write_cfg()
102 ad7949_adc->buffer = ad7949_adc->cfg << 2; in ad7949_spi_write_cfg()
103 ret = spi_write(ad7949_adc->spi, &ad7949_adc->buffer, 2); in ad7949_spi_write_cfg()
106 ad7949_adc->buffer = ad7949_adc->cfg; in ad7949_spi_write_cfg()
107 ret = spi_write(ad7949_adc->spi, &ad7949_adc->buffer, 2); in ad7949_spi_write_cfg()
111 ad7949_adc->buf8b = cpu_to_be16(ad7949_adc->cfg << 2); in ad7949_spi_write_cfg()
112 ret = spi_write(ad7949_adc->spi, &ad7949_adc->buf8b, 2); in ad7949_spi_write_cfg()
115 dev_err(&ad7949_adc->indio_dev->dev, "unsupported BPW\n"); in ad7949_spi_write_cfg()
116 return -EINVAL; in ad7949_spi_write_cfg()
134 * 1: write CFG for sample N and read old data (sample N-2) in ad7949_spi_read_channel()
135 * 2: if CFG was not changed since sample N-1 then we'll get good data in ad7949_spi_read_channel()
137 * and we read garbage (sample N-1 configuration). in ad7949_spi_read_channel()
145 if (channel == ad7949_adc->current_channel) in ad7949_spi_read_channel()
150 if (ad7949_adc->spi->bits_per_word == 8) in ad7949_spi_read_channel()
151 ret = spi_read(ad7949_adc->spi, &ad7949_adc->buf8b, 2); in ad7949_spi_read_channel()
153 ret = spi_read(ad7949_adc->spi, &ad7949_adc->buffer, 2); in ad7949_spi_read_channel()
164 ad7949_adc->current_channel = channel; in ad7949_spi_read_channel()
166 switch (ad7949_adc->spi->bits_per_word) { in ad7949_spi_read_channel()
168 *val = ad7949_adc->buffer; in ad7949_spi_read_channel()
169 /* Shift-out padding bits */ in ad7949_spi_read_channel()
170 *val >>= 16 - ad7949_adc->resolution; in ad7949_spi_read_channel()
173 *val = ad7949_adc->buffer & GENMASK(13, 0); in ad7949_spi_read_channel()
177 *val = be16_to_cpu(ad7949_adc->buf8b); in ad7949_spi_read_channel()
178 /* Shift-out padding bits */ in ad7949_spi_read_channel()
179 *val >>= 16 - ad7949_adc->resolution; in ad7949_spi_read_channel()
182 dev_err(&ad7949_adc->indio_dev->dev, "unsupported BPW\n"); in ad7949_spi_read_channel()
183 return -EINVAL; in ad7949_spi_read_channel()
216 return -EINVAL; in ad7949_spi_read_raw()
220 mutex_lock(&ad7949_adc->lock); in ad7949_spi_read_raw()
221 ret = ad7949_spi_read_channel(ad7949_adc, val, chan->channel); in ad7949_spi_read_raw()
222 mutex_unlock(&ad7949_adc->lock); in ad7949_spi_read_raw()
230 switch (ad7949_adc->refsel) { in ad7949_spi_read_raw()
239 ret = regulator_get_voltage(ad7949_adc->vref); in ad7949_spi_read_raw()
243 /* convert value back to mV */ in ad7949_spi_read_raw()
248 *val2 = (1 << ad7949_adc->resolution) - 1; in ad7949_spi_read_raw()
252 return -EINVAL; in ad7949_spi_read_raw()
263 *readval = ad7949_adc->cfg; in ad7949_spi_reg_access()
282 ad7949_adc->current_channel = 0; in ad7949_spi_init()
286 FIELD_PREP(AD7949_CFG_MASK_INX, ad7949_adc->current_channel) | in ad7949_spi_init()
288 FIELD_PREP(AD7949_CFG_MASK_REF, ad7949_adc->refsel) | in ad7949_spi_init()
298 ad7949_spi_read_channel(ad7949_adc, &val, ad7949_adc->current_channel); in ad7949_spi_init()
299 ad7949_spi_read_channel(ad7949_adc, &val, ad7949_adc->current_channel); in ad7949_spi_init()
311 u32 spi_ctrl_mask = spi->controller->bits_per_word_mask; in ad7949_spi_probe()
312 struct device *dev = &spi->dev; in ad7949_spi_probe()
322 return -ENOMEM; in ad7949_spi_probe()
325 indio_dev->info = &ad7949_spi_info; in ad7949_spi_probe()
326 indio_dev->name = spi_get_device_id(spi)->name; in ad7949_spi_probe()
327 indio_dev->modes = INDIO_DIRECT_MODE; in ad7949_spi_probe()
328 indio_dev->channels = ad7949_adc_channels; in ad7949_spi_probe()
332 ad7949_adc->indio_dev = indio_dev; in ad7949_spi_probe()
333 ad7949_adc->spi = spi; in ad7949_spi_probe()
335 spec = &ad7949_adc_spec[spi_get_device_id(spi)->driver_data]; in ad7949_spi_probe()
336 indio_dev->num_channels = spec->num_channels; in ad7949_spi_probe()
337 ad7949_adc->resolution = spec->resolution; in ad7949_spi_probe()
340 if (spi_ctrl_mask & SPI_BPW_MASK(ad7949_adc->resolution)) { in ad7949_spi_probe()
341 spi->bits_per_word = ad7949_adc->resolution; in ad7949_spi_probe()
343 spi->bits_per_word = 16; in ad7949_spi_probe()
345 spi->bits_per_word = 8; in ad7949_spi_probe()
348 return -EINVAL; in ad7949_spi_probe()
353 device_property_read_u32(dev, "adi,internal-ref-microvolt", &tmp); in ad7949_spi_probe()
357 ad7949_adc->refsel = AD7949_CFG_VAL_REF_INT_2500; in ad7949_spi_probe()
360 ad7949_adc->refsel = AD7949_CFG_VAL_REF_INT_4096; in ad7949_spi_probe()
364 return -EINVAL; in ad7949_spi_probe()
368 ad7949_adc->vref = devm_regulator_get_optional(dev, "vrefin"); in ad7949_spi_probe()
369 if (IS_ERR(ad7949_adc->vref)) { in ad7949_spi_probe()
370 ret = PTR_ERR(ad7949_adc->vref); in ad7949_spi_probe()
371 if (ret != -ENODEV) in ad7949_spi_probe()
374 ad7949_adc->vref = devm_regulator_get_optional(dev, "vref"); in ad7949_spi_probe()
375 if (IS_ERR(ad7949_adc->vref)) { in ad7949_spi_probe()
376 ret = PTR_ERR(ad7949_adc->vref); in ad7949_spi_probe()
377 if (ret != -ENODEV) in ad7949_spi_probe()
380 ad7949_adc->refsel = AD7949_CFG_VAL_REF_EXT_TEMP; in ad7949_spi_probe()
383 ad7949_adc->refsel = AD7949_CFG_VAL_REF_EXT_TEMP_BUF; in ad7949_spi_probe()
386 if (ad7949_adc->refsel & AD7949_CFG_VAL_REF_EXTERNAL) { in ad7949_spi_probe()
387 ret = regulator_enable(ad7949_adc->vref); in ad7949_spi_probe()
394 ad7949_adc->vref); in ad7949_spi_probe()
399 mutex_init(&ad7949_adc->lock); in ad7949_spi_probe()
440 MODULE_AUTHOR("Charles-Antoine Couret <charles-antoine.couret@essensium.com>");
441 MODULE_DESCRIPTION("Analog Devices 14/16-bit 8-channel ADC driver");