Lines Matching +full:adc +full:- +full:channels
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices AD7944/85/86 PulSAR ADC family driver.
37 /* datasheet calls this "4-wire mode" */
39 /* datasheet calls this "3-wire mode" (not related to SPI_3WIRE!) */
45 /* maps adi,spi-mode property value to enum */
58 /* Chip-specific timing specifications. */
64 /* Indicates TURBO is hard-wired to be always enabled. */
98 const struct iio_chan_spec channels[2]; member
102 * AD7944_DEFINE_CHIP_INFO - Define a chip info structure for a specific chip
112 .channels = { \
131 /* pseudo-differential with ground sense */
137 static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *adc, in ad7944_3wire_cs_mode_init_msg() argument
140 unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns in ad7944_3wire_cs_mode_init_msg()
141 : adc->timing_spec->conv_ns; in ad7944_3wire_cs_mode_init_msg()
142 struct spi_transfer *xfers = adc->xfers; in ad7944_3wire_cs_mode_init_msg()
148 xfers[0].bits_per_word = chan->scan_type.realbits; in ad7944_3wire_cs_mode_init_msg()
163 xfers[1].bits_per_word = chan->scan_type.realbits; in ad7944_3wire_cs_mode_init_msg()
166 xfers[2].rx_buf = &adc->sample.raw; in ad7944_3wire_cs_mode_init_msg()
167 xfers[2].len = BITS_TO_BYTES(chan->scan_type.storagebits); in ad7944_3wire_cs_mode_init_msg()
168 xfers[2].bits_per_word = chan->scan_type.realbits; in ad7944_3wire_cs_mode_init_msg()
170 spi_message_init_with_transfers(&adc->msg, xfers, 3); in ad7944_3wire_cs_mode_init_msg()
172 return devm_spi_optimize_message(dev, adc->spi, &adc->msg); in ad7944_3wire_cs_mode_init_msg()
175 static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc, in ad7944_4wire_mode_init_msg() argument
178 unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns in ad7944_4wire_mode_init_msg()
179 : adc->timing_spec->conv_ns; in ad7944_4wire_mode_init_msg()
180 struct spi_transfer *xfers = adc->xfers; in ad7944_4wire_mode_init_msg()
186 xfers[0].bits_per_word = chan->scan_type.realbits; in ad7944_4wire_mode_init_msg()
195 xfers[1].rx_buf = &adc->sample.raw; in ad7944_4wire_mode_init_msg()
196 xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits); in ad7944_4wire_mode_init_msg()
197 xfers[1].bits_per_word = chan->scan_type.realbits; in ad7944_4wire_mode_init_msg()
199 spi_message_init_with_transfers(&adc->msg, xfers, 2); in ad7944_4wire_mode_init_msg()
201 return devm_spi_optimize_message(dev, adc->spi, &adc->msg); in ad7944_4wire_mode_init_msg()
204 static int ad7944_chain_mode_init_msg(struct device *dev, struct ad7944_adc *adc, in ad7944_chain_mode_init_msg() argument
208 struct spi_transfer *xfers = adc->xfers; in ad7944_chain_mode_init_msg()
214 if (adc->spi->mode & SPI_CPOL) in ad7944_chain_mode_init_msg()
215 return dev_err_probe(dev, -EINVAL, in ad7944_chain_mode_init_msg()
222 if (!(adc->spi->mode & SPI_CS_HIGH)) in ad7944_chain_mode_init_msg()
223 return dev_err_probe(dev, -EINVAL, in ad7944_chain_mode_init_msg()
227 xfers[0].delay.value = adc->timing_spec->conv_ns; in ad7944_chain_mode_init_msg()
230 xfers[1].rx_buf = adc->chain_mode_buf; in ad7944_chain_mode_init_msg()
231 xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits) * n_chain_dev; in ad7944_chain_mode_init_msg()
232 xfers[1].bits_per_word = chan->scan_type.realbits; in ad7944_chain_mode_init_msg()
234 spi_message_init_with_transfers(&adc->msg, xfers, 2); in ad7944_chain_mode_init_msg()
236 return devm_spi_optimize_message(dev, adc->spi, &adc->msg); in ad7944_chain_mode_init_msg()
240 * ad7944_convert_and_acquire - Perform a single conversion and acquisition
241 * @adc: The ADC device structure
245 * pre-optimized adc->msg.
247 * Upon successful return adc->sample.raw will contain the conversion result
248 * (or adc->chain_mode_buf if the device is using chain mode).
250 static int ad7944_convert_and_acquire(struct ad7944_adc *adc) in ad7944_convert_and_acquire() argument
255 * In 4-wire mode, the CNV line is held high for the entire conversion in ad7944_convert_and_acquire()
256 * and acquisition process. In other modes adc->cnv is NULL and is in ad7944_convert_and_acquire()
259 gpiod_set_value_cansleep(adc->cnv, 1); in ad7944_convert_and_acquire()
260 ret = spi_sync(adc->spi, &adc->msg); in ad7944_convert_and_acquire()
261 gpiod_set_value_cansleep(adc->cnv, 0); in ad7944_convert_and_acquire()
266 static int ad7944_single_conversion(struct ad7944_adc *adc, in ad7944_single_conversion() argument
272 ret = ad7944_convert_and_acquire(adc); in ad7944_single_conversion()
276 if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) { in ad7944_single_conversion()
277 if (chan->scan_type.storagebits > 16) in ad7944_single_conversion()
278 *val = ((u32 *)adc->chain_mode_buf)[chan->scan_index]; in ad7944_single_conversion()
280 *val = ((u16 *)adc->chain_mode_buf)[chan->scan_index]; in ad7944_single_conversion()
282 if (chan->scan_type.storagebits > 16) in ad7944_single_conversion()
283 *val = adc->sample.raw.u32; in ad7944_single_conversion()
285 *val = adc->sample.raw.u16; in ad7944_single_conversion()
288 if (chan->scan_type.sign == 's') in ad7944_single_conversion()
289 *val = sign_extend32(*val, chan->scan_type.realbits - 1); in ad7944_single_conversion()
298 struct ad7944_adc *adc = iio_priv(indio_dev); in ad7944_read_raw() local
307 ret = ad7944_single_conversion(adc, chan, val); in ad7944_read_raw()
312 switch (chan->type) { in ad7944_read_raw()
314 *val = adc->ref_mv; in ad7944_read_raw()
316 if (chan->scan_type.sign == 's') in ad7944_read_raw()
317 *val2 = chan->scan_type.realbits - 1; in ad7944_read_raw()
319 *val2 = chan->scan_type.realbits; in ad7944_read_raw()
323 return -EINVAL; in ad7944_read_raw()
327 return -EINVAL; in ad7944_read_raw()
338 struct iio_dev *indio_dev = pf->indio_dev; in ad7944_trigger_handler()
339 struct ad7944_adc *adc = iio_priv(indio_dev); in ad7944_trigger_handler() local
342 ret = ad7944_convert_and_acquire(adc); in ad7944_trigger_handler()
346 if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) in ad7944_trigger_handler()
347 iio_push_to_buffers_with_timestamp(indio_dev, adc->chain_mode_buf, in ad7944_trigger_handler()
348 pf->timestamp); in ad7944_trigger_handler()
350 iio_push_to_buffers_with_timestamp(indio_dev, &adc->sample.raw, in ad7944_trigger_handler()
351 pf->timestamp); in ad7944_trigger_handler()
354 iio_trigger_notify_done(indio_dev->trig); in ad7944_trigger_handler()
360 * ad7944_chain_mode_alloc - allocate and initialize channel specs and buffers
361 * for daisy-chained devices
363 * @chan_template: The channel template for the devices (array of 2 channels
388 return -ENOMEM; in ad7944_chain_mode_alloc()
416 return -ENOMEM; in ad7944_chain_mode_alloc()
425 return dev_err_probe(dev, -EINVAL, in ad7944_chain_mode_alloc()
430 return -ENOMEM; in ad7944_chain_mode_alloc()
436 scan_masks[0] = GENMASK(n_chain_dev - 1, 0); in ad7944_chain_mode_alloc()
450 struct device *dev = &spi->dev; in ad7944_probe()
452 struct ad7944_adc *adc; in ad7944_probe() local
459 indio_dev = devm_iio_device_alloc(dev, sizeof(*adc)); in ad7944_probe()
461 return -ENOMEM; in ad7944_probe()
463 adc = iio_priv(indio_dev); in ad7944_probe()
464 adc->spi = spi; in ad7944_probe()
468 return dev_err_probe(dev, -EINVAL, "no chip info\n"); in ad7944_probe()
470 adc->timing_spec = chip_info->timing_spec; in ad7944_probe()
472 ret = device_property_match_property_string(dev, "adi,spi-mode", in ad7944_probe()
475 /* absence of adi,spi-mode property means default mode */ in ad7944_probe()
476 if (ret == -EINVAL) in ad7944_probe()
477 adc->spi_mode = AD7944_SPI_MODE_DEFAULT; in ad7944_probe()
480 "getting adi,spi-mode property failed\n"); in ad7944_probe()
482 adc->spi_mode = ret; in ad7944_probe()
488 if (!spi_is_bpw_supported(spi, chip_info->channels[0].scan_type.realbits)) in ad7944_probe()
489 return dev_err_probe(dev, -EINVAL, in ad7944_probe()
491 chip_info->channels[0].scan_type.realbits); in ad7944_probe()
502 * - internal reference: neither REF or REFIN is connected in ad7944_probe()
503 * - internal reference with external buffer: REF not connected, REFIN in ad7944_probe()
505 * - external reference: REF is connected, REFIN is not connected in ad7944_probe()
509 if (ret < 0 && ret != -ENODEV) in ad7944_probe()
512 ref_mv = ret == -ENODEV ? 0 : ret / 1000; in ad7944_probe()
515 if (ret < 0 && ret != -ENODEV) in ad7944_probe()
518 have_refin = ret != -ENODEV; in ad7944_probe()
521 return dev_err_probe(dev, -EINVAL, in ad7944_probe()
524 adc->ref_mv = ref_mv ?: AD7944_INTERNAL_REF_MV; in ad7944_probe()
526 adc->cnv = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW); in ad7944_probe()
527 if (IS_ERR(adc->cnv)) in ad7944_probe()
528 return dev_err_probe(dev, PTR_ERR(adc->cnv), in ad7944_probe()
531 if (!adc->cnv && adc->spi_mode == AD7944_SPI_MODE_DEFAULT) in ad7944_probe()
532 return dev_err_probe(&spi->dev, -EINVAL, "CNV GPIO is required\n"); in ad7944_probe()
533 if (adc->cnv && adc->spi_mode != AD7944_SPI_MODE_DEFAULT) in ad7944_probe()
534 return dev_err_probe(&spi->dev, -EINVAL, in ad7944_probe()
537 adc->turbo = devm_gpiod_get_optional(dev, "turbo", GPIOD_OUT_LOW); in ad7944_probe()
538 if (IS_ERR(adc->turbo)) in ad7944_probe()
539 return dev_err_probe(dev, PTR_ERR(adc->turbo), in ad7944_probe()
542 adc->always_turbo = device_property_present(dev, "adi,always-turbo"); in ad7944_probe()
544 if (adc->turbo && adc->always_turbo) in ad7944_probe()
545 return dev_err_probe(dev, -EINVAL, in ad7944_probe()
546 "cannot have both turbo-gpios and adi,always-turbo\n"); in ad7944_probe()
548 if (adc->spi_mode == AD7944_SPI_MODE_CHAIN && adc->always_turbo) in ad7944_probe()
549 return dev_err_probe(dev, -EINVAL, in ad7944_probe()
552 switch (adc->spi_mode) { in ad7944_probe()
554 ret = ad7944_4wire_mode_init_msg(dev, adc, &chip_info->channels[0]); in ad7944_probe()
560 ret = ad7944_3wire_cs_mode_init_msg(dev, adc, &chip_info->channels[0]); in ad7944_probe()
566 ret = device_property_read_u32(dev, "#daisy-chained-devices", in ad7944_probe()
570 "failed to get #daisy-chained-devices\n"); in ad7944_probe()
572 ret = ad7944_chain_mode_alloc(dev, chip_info->channels, in ad7944_probe()
574 &adc->chain_mode_buf, in ad7944_probe()
579 ret = ad7944_chain_mode_init_msg(dev, adc, &chain_chan[0], in ad7944_probe()
587 indio_dev->name = chip_info->name; in ad7944_probe()
588 indio_dev->modes = INDIO_DIRECT_MODE; in ad7944_probe()
589 indio_dev->info = &ad7944_iio_info; in ad7944_probe()
591 if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) { in ad7944_probe()
592 indio_dev->available_scan_masks = chain_scan_masks; in ad7944_probe()
593 indio_dev->channels = chain_chan; in ad7944_probe()
594 indio_dev->num_channels = n_chain_dev + 1; in ad7944_probe()
596 indio_dev->channels = chip_info->channels; in ad7944_probe()
597 indio_dev->num_channels = ARRAY_SIZE(chip_info->channels); in ad7944_probe()
637 MODULE_DESCRIPTION("Analog Devices AD7944 PulSAR ADC family driver");