Lines Matching +full:spi +full:- +full:compatible
1 // SPDX-License-Identifier: GPL-2.0-only
14 * ------------
20 * ------------
23 * ------------
42 #include <linux/spi/spi.h>
72 * struct mcp320x - Microchip SPI ADC instance
73 * @spi: SPI slave (parent of the IIO device)
74 * @msg: SPI message to select a channel and receive a value from the ADC
75 * @transfer: SPI transfers used by @msg
76 * @start_conv_msg: SPI message to start a conversion by briefly asserting CS
77 * @start_conv_transfer: SPI transfer used by @start_conv_msg
81 * @tx_buf: buffer for @transfer[0] (not used on single-channel converters)
85 struct spi_device *spi; member
116 return -EINVAL; in mcp320x_channel_to_tx_data()
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()
171 raw <<= 1; /* strip Data Ready bit in SPI mode 0,0 */ 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()
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()
374 static int mcp320x_probe(struct spi_device *spi) in mcp320x_probe() argument
381 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); in mcp320x_probe()
383 return -ENOMEM; in mcp320x_probe()
386 adc->spi = spi; in mcp320x_probe()
388 indio_dev->name = spi_get_device_id(spi)->name; in mcp320x_probe()
389 indio_dev->modes = INDIO_DIRECT_MODE; in mcp320x_probe()
390 indio_dev->info = &mcp320x_info; in mcp320x_probe()
391 spi_set_drvdata(spi, indio_dev); in mcp320x_probe()
393 device_index = spi_get_device_id(spi)->driver_data; in mcp320x_probe()
395 indio_dev->channels = chip_info->channels; in mcp320x_probe()
396 indio_dev->num_channels = chip_info->num_channels; in mcp320x_probe()
398 adc->chip_info = chip_info; in mcp320x_probe()
400 adc->transfer[0].tx_buf = &adc->tx_buf; in mcp320x_probe()
401 adc->transfer[0].len = sizeof(adc->tx_buf); in mcp320x_probe()
402 adc->transfer[1].rx_buf = adc->rx_buf; in mcp320x_probe()
403 adc->transfer[1].len = DIV_ROUND_UP(chip_info->resolution, 8); in mcp320x_probe()
405 if (chip_info->num_channels == 1) in mcp320x_probe()
406 /* single-channel converters are rx only (no MOSI pin) */ in mcp320x_probe()
407 spi_message_init_with_transfers(&adc->msg, in mcp320x_probe()
408 &adc->transfer[1], 1); in mcp320x_probe()
410 spi_message_init_with_transfers(&adc->msg, adc->transfer, in mcp320x_probe()
411 ARRAY_SIZE(adc->transfer)); in mcp320x_probe()
418 /* rx len increases from 24 to 25 bit in SPI mode 0,0 */ in mcp320x_probe()
419 if (!(spi->mode & SPI_CPOL)) in mcp320x_probe()
420 adc->transfer[1].len++; in mcp320x_probe()
423 adc->start_conv_transfer.delay.value = 8; in mcp320x_probe()
424 adc->start_conv_transfer.delay.unit = SPI_DELAY_UNIT_USECS; in mcp320x_probe()
425 spi_message_init_with_transfers(&adc->start_conv_msg, in mcp320x_probe()
426 &adc->start_conv_transfer, 1); in mcp320x_probe()
440 adc->reg = devm_regulator_get(&spi->dev, "vref"); in mcp320x_probe()
441 if (IS_ERR(adc->reg)) in mcp320x_probe()
442 return PTR_ERR(adc->reg); in mcp320x_probe()
444 ret = regulator_enable(adc->reg); in mcp320x_probe()
448 mutex_init(&adc->lock); in mcp320x_probe()
457 regulator_disable(adc->reg); in mcp320x_probe()
462 static void mcp320x_remove(struct spi_device *spi) in mcp320x_remove() argument
464 struct iio_dev *indio_dev = spi_get_drvdata(spi); in mcp320x_remove()
468 regulator_disable(adc->reg); in mcp320x_remove()
473 { .compatible = "mcp3001" },
474 { .compatible = "mcp3002" },
475 { .compatible = "mcp3004" },
476 { .compatible = "mcp3008" },
477 { .compatible = "mcp3201" },
478 { .compatible = "mcp3202" },
479 { .compatible = "mcp3204" },
480 { .compatible = "mcp3208" },
481 { .compatible = "mcp3301" },
482 { .compatible = "microchip,mcp3001" },
483 { .compatible = "microchip,mcp3002" },
484 { .compatible = "microchip,mcp3004" },
485 { .compatible = "microchip,mcp3008" },
486 { .compatible = "microchip,mcp3201" },
487 { .compatible = "microchip,mcp3202" },
488 { .compatible = "microchip,mcp3204" },
489 { .compatible = "microchip,mcp3208" },
490 { .compatible = "microchip,mcp3301" },
491 { .compatible = "microchip,mcp3550-50" },
492 { .compatible = "microchip,mcp3550-60" },
493 { .compatible = "microchip,mcp3551" },
494 { .compatible = "microchip,mcp3553" },
509 { "mcp3550-50", mcp3550_50 },
510 { "mcp3550-60", mcp3550_60 },
515 MODULE_DEVICE_TABLE(spi, mcp320x_id);