Lines Matching +full:adc +full:- +full:channels
1 // SPDX-License-Identifier: GPL-2.0-only
8 * Driver for following ADC chips from Microchip Technology's:
14 * ------------
20 * ------------
23 * ------------
65 const struct iio_chan_spec *channels; member
72 * struct mcp320x - Microchip SPI ADC instance
74 * @msg: SPI message to select a channel and receive a value from the ADC
80 * @chip_info: ADC properties
81 * @tx_buf: buffer for @transfer[0] (not used on single-channel converters)
116 return -EINVAL; in mcp320x_channel_to_tx_data()
120 static int mcp320x_adc_conversion(struct mcp320x *adc, u8 channel, in mcp320x_adc_conversion() argument
125 if (adc->chip_info->conv_time) { in mcp320x_adc_conversion()
126 ret = spi_sync(adc->spi, &adc->start_conv_msg); in mcp320x_adc_conversion()
130 usleep_range(adc->chip_info->conv_time, in mcp320x_adc_conversion()
131 adc->chip_info->conv_time + 100); in mcp320x_adc_conversion()
134 memset(&adc->rx_buf, 0, sizeof(adc->rx_buf)); in mcp320x_adc_conversion()
135 if (adc->chip_info->num_channels > 1) in mcp320x_adc_conversion()
136 adc->tx_buf = mcp320x_channel_to_tx_data(device_index, channel, in mcp320x_adc_conversion()
139 ret = spi_sync(adc->spi, &adc->msg); in mcp320x_adc_conversion()
145 *val = (adc->rx_buf[0] << 5 | adc->rx_buf[1] >> 3); in mcp320x_adc_conversion()
150 *val = (adc->rx_buf[0] << 2 | adc->rx_buf[1] >> 6); in mcp320x_adc_conversion()
153 *val = (adc->rx_buf[0] << 7 | adc->rx_buf[1] >> 1); in mcp320x_adc_conversion()
158 *val = (adc->rx_buf[0] << 4 | adc->rx_buf[1] >> 4); in mcp320x_adc_conversion()
161 *val = sign_extend32((adc->rx_buf[0] & 0x1f) << 8 in mcp320x_adc_conversion()
162 | adc->rx_buf[1], 12); in mcp320x_adc_conversion()
168 u32 raw = be32_to_cpup((__be32 *)adc->rx_buf); in mcp320x_adc_conversion()
170 if (!(adc->spi->mode & SPI_CPOL)) in mcp320x_adc_conversion()
174 * If the input is within -vref and vref, bit 21 is the sign. in mcp320x_adc_conversion()
180 return -EIO; /* cannot have overrange AND underrange */ in mcp320x_adc_conversion()
190 return -EINVAL; in mcp320x_adc_conversion()
198 struct mcp320x *adc = iio_priv(indio_dev); in mcp320x_read_raw() local
199 int ret = -EINVAL; in mcp320x_read_raw()
202 mutex_lock(&adc->lock); in mcp320x_read_raw()
204 device_index = spi_get_device_id(adc->spi)->driver_data; in mcp320x_read_raw()
208 ret = mcp320x_adc_conversion(adc, channel->address, in mcp320x_read_raw()
209 channel->differential, device_index, val); in mcp320x_read_raw()
217 ret = regulator_get_voltage(adc->reg); in mcp320x_read_raw()
223 *val2 = adc->chip_info->resolution; in mcp320x_read_raw()
229 mutex_unlock(&adc->lock); in mcp320x_read_raw()
303 .channels = mcp3201_channels,
308 .channels = mcp3202_channels,
313 .channels = mcp3204_channels,
318 .channels = mcp3208_channels,
323 .channels = mcp3201_channels,
328 .channels = mcp3202_channels,
333 .channels = mcp3204_channels,
338 .channels = mcp3208_channels,
343 .channels = mcp3201_channels,
348 .channels = mcp3201_channels,
355 .channels = mcp3201_channels,
361 .channels = mcp3201_channels,
367 .channels = mcp3201_channels,
382 struct mcp320x *adc; in mcp320x_probe() local
386 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); in mcp320x_probe()
388 return -ENOMEM; in mcp320x_probe()
390 adc = iio_priv(indio_dev); in mcp320x_probe()
391 adc->spi = spi; in mcp320x_probe()
393 indio_dev->name = spi_get_device_id(spi)->name; in mcp320x_probe()
394 indio_dev->modes = INDIO_DIRECT_MODE; in mcp320x_probe()
395 indio_dev->info = &mcp320x_info; in mcp320x_probe()
397 device_index = spi_get_device_id(spi)->driver_data; in mcp320x_probe()
399 indio_dev->channels = chip_info->channels; in mcp320x_probe()
400 indio_dev->num_channels = chip_info->num_channels; in mcp320x_probe()
402 adc->chip_info = chip_info; in mcp320x_probe()
404 adc->transfer[0].tx_buf = &adc->tx_buf; in mcp320x_probe()
405 adc->transfer[0].len = sizeof(adc->tx_buf); in mcp320x_probe()
406 adc->transfer[1].rx_buf = adc->rx_buf; in mcp320x_probe()
407 adc->transfer[1].len = DIV_ROUND_UP(chip_info->resolution, 8); in mcp320x_probe()
409 if (chip_info->num_channels == 1) in mcp320x_probe()
410 /* single-channel converters are rx only (no MOSI pin) */ in mcp320x_probe()
411 spi_message_init_with_transfers(&adc->msg, in mcp320x_probe()
412 &adc->transfer[1], 1); in mcp320x_probe()
414 spi_message_init_with_transfers(&adc->msg, adc->transfer, in mcp320x_probe()
415 ARRAY_SIZE(adc->transfer)); in mcp320x_probe()
423 if (!(spi->mode & SPI_CPOL)) in mcp320x_probe()
424 adc->transfer[1].len++; in mcp320x_probe()
427 adc->start_conv_transfer.delay.value = 8; in mcp320x_probe()
428 adc->start_conv_transfer.delay.unit = SPI_DELAY_UNIT_USECS; in mcp320x_probe()
429 spi_message_init_with_transfers(&adc->start_conv_msg, in mcp320x_probe()
430 &adc->start_conv_transfer, 1); in mcp320x_probe()
440 mcp320x_adc_conversion(adc, 0, 1, device_index, &ret); in mcp320x_probe()
441 mcp320x_adc_conversion(adc, 0, 1, device_index, &ret); in mcp320x_probe()
444 adc->reg = devm_regulator_get(&spi->dev, "vref"); in mcp320x_probe()
445 if (IS_ERR(adc->reg)) in mcp320x_probe()
446 return PTR_ERR(adc->reg); in mcp320x_probe()
448 ret = regulator_enable(adc->reg); in mcp320x_probe()
452 ret = devm_add_action_or_reset(&spi->dev, mcp320x_regulator_disable, adc->reg); in mcp320x_probe()
456 mutex_init(&adc->lock); in mcp320x_probe()
458 return devm_iio_device_register(&spi->dev, indio_dev); in mcp320x_probe()
471 { .compatible = "microchip,mcp3550-50" },
472 { .compatible = "microchip,mcp3550-60" },
489 { "mcp3550-50", mcp3550_50 },
490 { "mcp3550-60", mcp3550_60 },