Lines Matching +full:adc +full:- +full:chan

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices AD9467 SPI ADC driver
5 * Copyright 2012-2020 Analog Devices Inc.
32 * ADI High-Speed ADC common spi interface registers
33 * See Application-Note AN-877:
34 * https://www.analog.com/media/en/technical-documentation/application-notes/AN-877.pdf
85 * Analog Devices AD9265 16-Bit, 125/105/80 MSPS ADC
93 * Analog Devices AD9434 12-Bit, 370/500 MSPS ADC
101 * Analog Devices AD9467 16-Bit, 200/250 MSPS ADC
109 * Analog Devices AD9643 14-Bit, 170/210/250 MSPS ADC
116 * Analog Devices AD9652 16-bit 310 MSPS ADC
123 * Analog Devices AD9649 14-bit 20/40/65/80 MSPS ADC
131 (!(st)->info->has_dco || (st)->info->has_dco_invert)
171 * https://www.analog.com/media/en/technical-documentation/data-sheets/ad9265.pdf
173 * calibration is done at the backend level. For the ADI axi-adc:
194 ret = spi_write_then_read(st->spi,
207 st->buf[0] = reg >> 8;
208 st->buf[1] = reg & 0xFF;
209 st->buf[2] = val;
211 return spi_write(st->spi, st->buf, ARRAY_SIZE(st->buf));
221 guard(mutex)(&st->lock);
274 const struct ad9467_chip_info *info = st->info;
275 const struct iio_chan_spec *chan = &info->channels[0];
278 tmp = (info->scale_table[index][0] * 1000000ULL) >>
279 chan->scan_type.realbits;
443 const struct ad9467_chip_info *info = st->info;
449 if (info->num_scales == 1)
456 vref_val = ret & info->vref_mask;
458 for (i = 0; i < info->num_scales; i++) {
459 if (vref_val == info->scale_table[i][1])
463 if (i == info->num_scales)
464 return -ERANGE;
474 const struct ad9467_chip_info *info = st->info;
480 return -EINVAL;
481 if (info->num_scales == 1)
482 return -EOPNOTSUPP;
484 for (i = 0; i < info->num_scales; i++) {
489 guard(mutex)(&st->lock);
491 info->scale_table[i][1]);
499 return -EINVAL;
514 static int ad9467_testmode_set(struct ad9467_state *st, unsigned int chan,
519 if (st->info->num_channels > 1) {
521 ret = ad9467_spi_write(st, AN877_ADC_REG_CHAN_INDEX, BIT(chan));
530 if (st->info->num_channels > 1) {
533 GENMASK(st->info->num_channels - 1, 0));
543 unsigned int chan,
551 ret = iio_backend_data_format_set(st->back, chan, &data);
555 ret = iio_backend_test_pattern_set(st->back, chan, pattern);
559 return iio_backend_chan_enable(st->back, chan);
563 unsigned int chan)
571 ret = iio_backend_chan_disable(st->back, chan);
575 ret = iio_backend_test_pattern_set(st->back, chan,
580 return iio_backend_data_format_set(st->back, chan, &data);
588 ret = ad9467_outputmode_set(st, st->info->default_output_mode);
592 for (c = 0; c < st->info->num_channels; c++) {
611 if (st->info->has_dco) {
626 return iio_backend_data_sample_trigger(st->back, trigger);
641 if (end - bit > cnt) {
642 cnt = end - bit;
658 if (st->info->has_dco) {
660 val | st->info->dco_en);
668 for (lane = 0; lane < st->info->num_lanes; lane++) {
669 ret = iio_backend_iodelay_set(st->back, lane, val);
682 for (c = 0; c < st->info->num_channels; c++) {
692 mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;
703 unsigned int test_points = st->info->test_points;
704 unsigned long sample_rate = clk_get_rate(st->clk);
705 struct device *dev = &st->spi->dev;
710 bitmap_fill(st->calib_map, st->calib_map_size);
720 for (point = 0; point < st->info->test_points; point++) {
725 for (c = 0; c < st->info->num_channels; c++) {
726 ret = iio_backend_chan_status(st->back, c, &stat);
741 if (c == st->info->num_channels - 1)
743 st->calib_map);
748 cnt = ad9467_find_optimal_point(st->calib_map, 0, test_points,
761 return -EIO;
764 inv_cnt = ad9467_find_optimal_point(st->calib_map, test_points,
767 return -EIO;
777 * need to re-do any configuration. We just need to "normalize"
780 val = inv_val - test_points;
783 if (st->info->has_dco)
799 struct iio_chan_spec const *chan,
808 *val = clk_get_rate(st->clk);
812 return -EINVAL;
820 ret = clk_set_rate(st->clk, r_clk);
824 guard(mutex)(&st->lock);
829 struct iio_chan_spec const *chan,
833 const struct ad9467_chip_info *info = st->info;
842 r_clk = clk_round_rate(st->clk, val);
843 if (r_clk < 0 || r_clk > info->max_rate) {
844 dev_warn(&st->spi->dev,
845 "Error setting ADC sample rate %ld", r_clk);
846 return -EINVAL;
849 sample_rate = clk_get_rate(st->clk);
858 return -EBUSY;
864 return -EINVAL;
869 struct iio_chan_spec const *chan,
874 const struct ad9467_chip_info *info = st->info;
878 *vals = (const int *)st->scales;
881 *length = info->num_scales * 2;
884 return -EINVAL;
895 for (c = 0; c < st->info->num_channels; c++) {
897 ret = iio_backend_chan_enable(st->back, c);
899 ret = iio_backend_chan_disable(st->back, c);
925 const struct ad9467_chip_info *info = st->info;
928 st->scales = devm_kmalloc_array(&st->spi->dev, info->num_scales,
929 sizeof(*st->scales), GFP_KERNEL);
930 if (!st->scales)
931 return -ENOMEM;
933 for (i = 0; i < info->num_scales; i++) {
935 st->scales[i][0] = val1;
936 st->scales[i][1] = val2;
959 struct device *dev = &st->spi->dev;
962 st->back = devm_iio_backend_get(dev, NULL);
963 if (!IS_ERR(st->back))
966 if (PTR_ERR(st->back) != -ENOENT)
967 return PTR_ERR(st->back);
971 * 'adi,adc-dev' property. So we get all nodes with that property, and
974 * make io-backends mandatory which would break DT ABI.
976 for_each_node_with_property(__back, "adi,adc-dev") {
979 __me = of_parse_phandle(__back, "adi,adc-dev", 0);
989 st->back = __devm_iio_backend_get_from_fwnode_lookup(dev,
992 return PTR_ERR_OR_ZERO(st->back);
995 return -ENODEV;
1000 struct ad9467_state *st = s->private;
1003 for_each_set_bit(bit, &st->info->test_mask, st->info->test_mask_len)
1014 struct ad9467_chan_test_mode *chan = file->private_data;
1015 struct ad9467_state *st = chan->st;
1020 if (chan->mode == AN877_ADC_TESTMODE_PN9_SEQ ||
1021 chan->mode == AN877_ADC_TESTMODE_PN23_SEQ) {
1023 ad9467_test_modes[chan->mode]);
1025 ret = iio_backend_debugfs_print_chan_status(st->back, chan->idx,
1027 sizeof(buf) - len);
1031 } else if (chan->mode == AN877_ADC_TESTMODE_OFF) {
1035 ad9467_test_modes[chan->mode], chan->idx);
1045 struct ad9467_chan_test_mode *chan = file->private_data;
1046 struct ad9467_state *st = chan->st;
1051 ret = simple_write_to_buffer(test_mode, sizeof(test_mode) - 1, ppos,
1056 for_each_set_bit(mode, &st->info->test_mask, st->info->test_mask_len) {
1061 if (mode == st->info->test_mask_len)
1062 return -EINVAL;
1064 guard(mutex)(&st->lock);
1069 if (chan->mode == AN877_ADC_TESTMODE_PN9_SEQ ||
1070 chan->mode == AN877_ADC_TESTMODE_PN23_SEQ) {
1071 ret = ad9467_backend_testmode_off(st, chan->idx);
1076 ret = ad9467_testmode_set(st, chan->idx, mode);
1080 out_mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;
1085 ret = ad9467_outputmode_set(st, st->info->default_output_mode);
1089 ret = ad9467_testmode_set(st, chan->idx, mode);
1095 ret = ad9467_backend_testmode_on(st, chan->idx,
1100 ret = ad9467_backend_testmode_on(st, chan->idx,
1107 chan->mode = mode;
1124 struct ad9467_state *st = file->private_data;
1130 guard(mutex)(&st->lock);
1134 for (bit = 0; bit < st->calib_map_size; bit++) {
1135 if (AD9467_CAN_INVERT(st) && bit == st->calib_map_size / 2)
1136 len += scnprintf(map + len, sizeof(map) - len, "\n");
1138 len += scnprintf(map + len, sizeof(map) - len, "%c",
1139 test_bit(bit, st->calib_map) ? 'x' : 'o');
1142 len += scnprintf(map + len, sizeof(map) - len, "\n");
1159 unsigned int chan;
1164 st->chan_test = devm_kcalloc(&st->spi->dev, st->info->num_channels,
1165 sizeof(*st->chan_test), GFP_KERNEL);
1166 if (!st->chan_test)
1172 for (chan = 0; chan < st->info->num_channels; chan++) {
1174 chan);
1175 st->chan_test[chan].idx = chan;
1176 st->chan_test[chan].st = st;
1177 debugfs_create_file(attr_name, 0600, d, &st->chan_test[chan],
1184 iio_backend_debugfs_add(st->back, indio_dev);
1194 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1196 return -ENOMEM;
1199 st->spi = spi;
1201 st->info = spi_get_device_match_data(spi);
1202 if (!st->info)
1203 return -ENODEV;
1205 st->calib_map_size = st->info->test_points;
1207 st->calib_map_size *= 2;
1209 st->clk = devm_clk_get_enabled(&spi->dev, "adc-clk");
1210 if (IS_ERR(st->clk))
1211 return PTR_ERR(st->clk);
1213 st->pwrdown_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown",
1215 if (IS_ERR(st->pwrdown_gpio))
1216 return PTR_ERR(st->pwrdown_gpio);
1218 ret = ad9467_reset(&spi->dev);
1227 if (id != st->info->id) {
1228 dev_err(&spi->dev, "Mismatch CHIP_ID, got 0x%X, expected 0x%X\n",
1229 id, st->info->id);
1230 return -ENODEV;
1233 if (st->info->num_scales > 1)
1234 indio_dev->info = &ad9467_info;
1236 indio_dev->info = &ad9467_info_no_read_avail;
1237 indio_dev->name = st->info->name;
1238 indio_dev->channels = st->info->channels;
1239 indio_dev->num_channels = st->info->num_channels;
1245 ret = devm_iio_backend_request_buffer(&spi->dev, st->back, indio_dev);
1249 ret = devm_iio_backend_enable(&spi->dev, st->back);
1257 ret = devm_iio_device_register(&spi->dev, indio_dev);
1299 MODULE_DESCRIPTION("Analog Devices AD9467 ADC driver");