Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: adc: mcp320x: Add support for mcp3550/1/3

These ADCs are marketed as single-channel 22 bit delta-sigma ADCs, but
in reality their resolution is 21 bit with an overrange or underrange
of 12% beyond Vref. In other words, "full scale" means +/- 2^20.

This driver does not explicitly signal back to the user when an
overrange or underrange occurs, but the user can detect it by comparing
the raw value to +/- 2^20 (or the scaled value to Vref).

The chips feature an extended temperature range and high accuracy,
low noise characteristics, but their conversion times are slow with
up to 80 ms +/- 2% (on the MCP3550-50).

Hence, unlike the other ADCs supported by the driver, conversion does
not take place in realtime upon lowering CS. Instead, CS is asserted
for 8 usec to start the conversion. After waiting for the duration of
the conversion, the result can be fetched. While waiting, control of
the bus is ceased so it may be used by a different device.

After the result has been fetched and 10 us have passed, the chip goes
into shutdown and an additional power-up delay of 144 clock periods is
then required to wake the analog circuitry upon the next conversion
(footnote below table 4-1, page 16 in the spec).

Optionally, the chips can be used in so-called "continuous conversion
mode": Conversions then take place continuously and the last result may
be fetched at any time without observing a delay. The mode is enabled
by permanently driving CS low, e.g. by wiring it to ground. The driver
only supports "single conversion mode" for now but should be adaptable
to "continuous conversion mode" with moderate effort.

The chips clock out a 3 byte word, unlike the other ADCs supported by
the driver which all have a lower resolution than 16 bit and thus make
do with 2 bytes. Calculate the word length on probe by rounding up the
resolution to full bytes. Crucially, if the clock idles low, the
transfer is preceded by a useless Data Ready bit which increases its
length from 24 bit to 25 bit = 4 bytes (section 5.5 in the spec).
Autosense this based on the SPI slave's configuration.

Cc: Mathias Duckeck <m.duckeck@kunbus.de>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Lukas Wunner and committed by
Jonathan Cameron
c1375d67 2db82e32

+118 -7
+3 -2
drivers/iio/adc/Kconfig
··· 475 475 called max9611. 476 476 477 477 config MCP320X 478 - tristate "Microchip Technology MCP3x01/02/04/08" 478 + tristate "Microchip Technology MCP3x01/02/04/08 and MCP3550/1/3" 479 479 depends on SPI 480 480 help 481 481 Say yes here to build support for Microchip Technology's 482 482 MCP3001, MCP3002, MCP3004, MCP3008, MCP3201, MCP3202, MCP3204, 483 - MCP3208 or MCP3301 analog to digital converter. 483 + MCP3208, MCP3301, MCP3550, MCP3551 and MCP3553 analog to digital 484 + converters. 484 485 485 486 This driver can also be built as a module. If so, the module will be 486 487 called mcp320x.
+115 -5
drivers/iio/adc/mcp320x.c
··· 19 19 * ------------ 20 20 * 13 bit converter 21 21 * MCP3301 22 + * ------------ 23 + * 22 bit converter 24 + * MCP3550 25 + * MCP3551 26 + * MCP3553 22 27 * 23 28 * Datasheet can be found here: 24 29 * http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf mcp3001 ··· 33 28 * http://ww1.microchip.com/downloads/en/DeviceDoc/21034D.pdf mcp3202 34 29 * http://ww1.microchip.com/downloads/en/DeviceDoc/21298c.pdf mcp3204/08 35 30 * http://ww1.microchip.com/downloads/en/DeviceDoc/21700E.pdf mcp3301 31 + * http://ww1.microchip.com/downloads/en/DeviceDoc/21950D.pdf mcp3550/1/3 36 32 * 37 33 * This program is free software; you can redistribute it and/or modify 38 34 * it under the terms of the GNU General Public License version 2 as ··· 57 51 mcp3204, 58 52 mcp3208, 59 53 mcp3301, 54 + mcp3550_50, 55 + mcp3550_60, 56 + mcp3551, 57 + mcp3553, 60 58 }; 61 59 62 60 struct mcp320x_chip_info { 63 61 const struct iio_chan_spec *channels; 64 62 unsigned int num_channels; 65 63 unsigned int resolution; 64 + unsigned int conv_time; /* usec */ 66 65 }; 67 66 68 67 /** ··· 75 64 * @spi: SPI slave (parent of the IIO device) 76 65 * @msg: SPI message to select a channel and receive a value from the ADC 77 66 * @transfer: SPI transfers used by @msg 67 + * @start_conv_msg: SPI message to start a conversion by briefly asserting CS 68 + * @start_conv_transfer: SPI transfer used by @start_conv_msg 78 69 * @reg: regulator generating Vref 79 70 * @lock: protects read sequences 80 71 * @chip_info: ADC properties ··· 87 74 struct spi_device *spi; 88 75 struct spi_message msg; 89 76 struct spi_transfer transfer[2]; 77 + struct spi_message start_conv_msg; 78 + struct spi_transfer start_conv_transfer; 90 79 91 80 struct regulator *reg; 92 81 struct mutex lock; 93 82 const struct mcp320x_chip_info *chip_info; 94 83 95 84 u8 tx_buf ____cacheline_aligned; 96 - u8 rx_buf[2]; 85 + u8 rx_buf[4]; 97 86 }; 98 87 99 88 static int mcp320x_channel_to_tx_data(int device_index, ··· 123 108 bool differential, int device_index, int *val) 124 109 { 125 110 int ret; 111 + 112 + if (adc->chip_info->conv_time) { 113 + ret = spi_sync(adc->spi, &adc->start_conv_msg); 114 + if (ret < 0) 115 + return ret; 116 + 117 + usleep_range(adc->chip_info->conv_time, 118 + adc->chip_info->conv_time + 100); 119 + } 126 120 127 121 memset(&adc->rx_buf, 0, sizeof(adc->rx_buf)); 128 122 if (adc->chip_info->num_channels > 1) ··· 163 139 *val = sign_extend32((adc->rx_buf[0] & 0x1f) << 8 164 140 | adc->rx_buf[1], 12); 165 141 return 0; 142 + case mcp3550_50: 143 + case mcp3550_60: 144 + case mcp3551: 145 + case mcp3553: { 146 + u32 raw = be32_to_cpup((u32 *)adc->rx_buf); 147 + 148 + if (!(adc->spi->mode & SPI_CPOL)) 149 + raw <<= 1; /* strip Data Ready bit in SPI mode 0,0 */ 150 + 151 + /* 152 + * If the input is within -vref and vref, bit 21 is the sign. 153 + * Up to 12% overrange or underrange are allowed, in which case 154 + * bit 23 is the sign and bit 0 to 21 is the value. 155 + */ 156 + raw >>= 8; 157 + if (raw & BIT(22) && raw & BIT(23)) 158 + return -EIO; /* cannot have overrange AND underrange */ 159 + else if (raw & BIT(22)) 160 + raw &= ~BIT(22); /* overrange */ 161 + else if (raw & BIT(23) || raw & BIT(21)) 162 + raw |= GENMASK(31, 22); /* underrange or negative */ 163 + 164 + *val = (s32)raw; 165 + return 0; 166 + } 166 167 default: 167 168 return -EINVAL; 168 169 } ··· 346 297 .num_channels = ARRAY_SIZE(mcp3201_channels), 347 298 .resolution = 13 348 299 }, 300 + [mcp3550_50] = { 301 + .channels = mcp3201_channels, 302 + .num_channels = ARRAY_SIZE(mcp3201_channels), 303 + .resolution = 21, 304 + /* 2% max deviation + 144 clock periods to exit shutdown */ 305 + .conv_time = 80000 * 1.02 + 144000 / 102.4, 306 + }, 307 + [mcp3550_60] = { 308 + .channels = mcp3201_channels, 309 + .num_channels = ARRAY_SIZE(mcp3201_channels), 310 + .resolution = 21, 311 + .conv_time = 66670 * 1.02 + 144000 / 122.88, 312 + }, 313 + [mcp3551] = { 314 + .channels = mcp3201_channels, 315 + .num_channels = ARRAY_SIZE(mcp3201_channels), 316 + .resolution = 21, 317 + .conv_time = 73100 * 1.02 + 144000 / 112.64, 318 + }, 319 + [mcp3553] = { 320 + .channels = mcp3201_channels, 321 + .num_channels = ARRAY_SIZE(mcp3201_channels), 322 + .resolution = 21, 323 + .conv_time = 16670 * 1.02 + 144000 / 122.88, 324 + }, 349 325 }; 350 326 351 327 static int mcp320x_probe(struct spi_device *spi) ··· 378 304 struct iio_dev *indio_dev; 379 305 struct mcp320x *adc; 380 306 const struct mcp320x_chip_info *chip_info; 381 - int ret; 307 + int ret, device_index; 382 308 383 309 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); 384 310 if (!indio_dev) ··· 394 320 indio_dev->info = &mcp320x_info; 395 321 spi_set_drvdata(spi, indio_dev); 396 322 397 - chip_info = &mcp320x_chip_infos[spi_get_device_id(spi)->driver_data]; 323 + device_index = spi_get_device_id(spi)->driver_data; 324 + chip_info = &mcp320x_chip_infos[device_index]; 398 325 indio_dev->channels = chip_info->channels; 399 326 indio_dev->num_channels = chip_info->num_channels; 400 327 ··· 404 329 adc->transfer[0].tx_buf = &adc->tx_buf; 405 330 adc->transfer[0].len = sizeof(adc->tx_buf); 406 331 adc->transfer[1].rx_buf = adc->rx_buf; 407 - adc->transfer[1].len = sizeof(adc->rx_buf); 332 + adc->transfer[1].len = DIV_ROUND_UP(chip_info->resolution, 8); 333 + 408 334 if (chip_info->num_channels == 1) 409 335 /* single-channel converters are rx only (no MOSI pin) */ 410 336 spi_message_init_with_transfers(&adc->msg, ··· 413 337 else 414 338 spi_message_init_with_transfers(&adc->msg, adc->transfer, 415 339 ARRAY_SIZE(adc->transfer)); 340 + 341 + switch (device_index) { 342 + case mcp3550_50: 343 + case mcp3550_60: 344 + case mcp3551: 345 + case mcp3553: 346 + /* rx len increases from 24 to 25 bit in SPI mode 0,0 */ 347 + if (!(spi->mode & SPI_CPOL)) 348 + adc->transfer[1].len++; 349 + 350 + /* conversions are started by asserting CS pin for 8 usec */ 351 + adc->start_conv_transfer.delay_usecs = 8; 352 + spi_message_init_with_transfers(&adc->start_conv_msg, 353 + &adc->start_conv_transfer, 1); 354 + 355 + /* 356 + * If CS was previously kept low (continuous conversion mode) 357 + * and then changed to high, the chip is in shutdown. 358 + * Sometimes it fails to wake from shutdown and clocks out 359 + * only 0xffffff. The magic sequence of performing two 360 + * conversions without delay between them resets the chip 361 + * and ensures all subsequent conversions succeed. 362 + */ 363 + mcp320x_adc_conversion(adc, 0, 1, device_index, &ret); 364 + mcp320x_adc_conversion(adc, 0, 1, device_index, &ret); 365 + } 416 366 417 367 adc->reg = devm_regulator_get(&spi->dev, "vref"); 418 368 if (IS_ERR(adc->reg)) ··· 494 392 { .compatible = "microchip,mcp3204" }, 495 393 { .compatible = "microchip,mcp3208" }, 496 394 { .compatible = "microchip,mcp3301" }, 395 + { .compatible = "microchip,mcp3550-50" }, 396 + { .compatible = "microchip,mcp3550-60" }, 397 + { .compatible = "microchip,mcp3551" }, 398 + { .compatible = "microchip,mcp3553" }, 497 399 { } 498 400 }; 499 401 MODULE_DEVICE_TABLE(of, mcp320x_dt_ids); ··· 513 407 { "mcp3204", mcp3204 }, 514 408 { "mcp3208", mcp3208 }, 515 409 { "mcp3301", mcp3301 }, 410 + { "mcp3550-50", mcp3550_50 }, 411 + { "mcp3550-60", mcp3550_60 }, 412 + { "mcp3551", mcp3551 }, 413 + { "mcp3553", mcp3553 }, 516 414 { } 517 415 }; 518 416 MODULE_DEVICE_TABLE(spi, mcp320x_id); ··· 533 423 module_spi_driver(mcp320x_driver); 534 424 535 425 MODULE_AUTHOR("Oskar Andero <oskar.andero@gmail.com>"); 536 - MODULE_DESCRIPTION("Microchip Technology MCP3x01/02/04/08"); 426 + MODULE_DESCRIPTION("Microchip Technology MCP3x01/02/04/08 and MCP3550/1/3"); 537 427 MODULE_LICENSE("GPL v2");