mcp320x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Oskar Andero <[email protected]>
  4. * Copyright (C) 2014 Rose Technology
  5. * Allan Bendorff Jensen <[email protected]>
  6. * Soren Andersen <[email protected]>
  7. *
  8. * Driver for following ADC chips from Microchip Technology's:
  9. * 10 Bit converter
  10. * MCP3001
  11. * MCP3002
  12. * MCP3004
  13. * MCP3008
  14. * ------------
  15. * 12 bit converter
  16. * MCP3201
  17. * MCP3202
  18. * MCP3204
  19. * MCP3208
  20. * ------------
  21. * 13 bit converter
  22. * MCP3301
  23. * ------------
  24. * 22 bit converter
  25. * MCP3550
  26. * MCP3551
  27. * MCP3553
  28. *
  29. * Datasheet can be found here:
  30. * https://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf mcp3001
  31. * https://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf mcp3002
  32. * https://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf mcp3004/08
  33. * http://ww1.microchip.com/downloads/en/DeviceDoc/21290D.pdf mcp3201
  34. * http://ww1.microchip.com/downloads/en/DeviceDoc/21034D.pdf mcp3202
  35. * http://ww1.microchip.com/downloads/en/DeviceDoc/21298c.pdf mcp3204/08
  36. * https://ww1.microchip.com/downloads/en/DeviceDoc/21700E.pdf mcp3301
  37. * http://ww1.microchip.com/downloads/en/DeviceDoc/21950D.pdf mcp3550/1/3
  38. */
  39. #include <linux/err.h>
  40. #include <linux/delay.h>
  41. #include <linux/spi/spi.h>
  42. #include <linux/module.h>
  43. #include <linux/mod_devicetable.h>
  44. #include <linux/iio/iio.h>
  45. #include <linux/regulator/consumer.h>
  46. enum {
  47. mcp3001,
  48. mcp3002,
  49. mcp3004,
  50. mcp3008,
  51. mcp3201,
  52. mcp3202,
  53. mcp3204,
  54. mcp3208,
  55. mcp3301,
  56. mcp3550_50,
  57. mcp3550_60,
  58. mcp3551,
  59. mcp3553,
  60. };
  61. struct mcp320x_chip_info {
  62. const struct iio_chan_spec *channels;
  63. unsigned int num_channels;
  64. unsigned int resolution;
  65. unsigned int conv_time; /* usec */
  66. };
  67. /**
  68. * struct mcp320x - Microchip SPI ADC instance
  69. * @spi: SPI slave (parent of the IIO device)
  70. * @msg: SPI message to select a channel and receive a value from the ADC
  71. * @transfer: SPI transfers used by @msg
  72. * @start_conv_msg: SPI message to start a conversion by briefly asserting CS
  73. * @start_conv_transfer: SPI transfer used by @start_conv_msg
  74. * @reg: regulator generating Vref
  75. * @lock: protects read sequences
  76. * @chip_info: ADC properties
  77. * @tx_buf: buffer for @transfer[0] (not used on single-channel converters)
  78. * @rx_buf: buffer for @transfer[1]
  79. */
  80. struct mcp320x {
  81. struct spi_device *spi;
  82. struct spi_message msg;
  83. struct spi_transfer transfer[2];
  84. struct spi_message start_conv_msg;
  85. struct spi_transfer start_conv_transfer;
  86. struct regulator *reg;
  87. struct mutex lock;
  88. const struct mcp320x_chip_info *chip_info;
  89. u8 tx_buf __aligned(IIO_DMA_MINALIGN);
  90. u8 rx_buf[4];
  91. };
  92. static int mcp320x_channel_to_tx_data(int device_index,
  93. const unsigned int channel, bool differential)
  94. {
  95. int start_bit = 1;
  96. switch (device_index) {
  97. case mcp3002:
  98. case mcp3202:
  99. return ((start_bit << 4) | (!differential << 3) |
  100. (channel << 2));
  101. case mcp3004:
  102. case mcp3204:
  103. case mcp3008:
  104. case mcp3208:
  105. return ((start_bit << 6) | (!differential << 5) |
  106. (channel << 2));
  107. default:
  108. return -EINVAL;
  109. }
  110. }
  111. static int mcp320x_adc_conversion(struct mcp320x *adc, u8 channel,
  112. bool differential, int device_index, int *val)
  113. {
  114. int ret;
  115. if (adc->chip_info->conv_time) {
  116. ret = spi_sync(adc->spi, &adc->start_conv_msg);
  117. if (ret < 0)
  118. return ret;
  119. usleep_range(adc->chip_info->conv_time,
  120. adc->chip_info->conv_time + 100);
  121. }
  122. memset(&adc->rx_buf, 0, sizeof(adc->rx_buf));
  123. if (adc->chip_info->num_channels > 1)
  124. adc->tx_buf = mcp320x_channel_to_tx_data(device_index, channel,
  125. differential);
  126. ret = spi_sync(adc->spi, &adc->msg);
  127. if (ret < 0)
  128. return ret;
  129. switch (device_index) {
  130. case mcp3001:
  131. *val = (adc->rx_buf[0] << 5 | adc->rx_buf[1] >> 3);
  132. return 0;
  133. case mcp3002:
  134. case mcp3004:
  135. case mcp3008:
  136. *val = (adc->rx_buf[0] << 2 | adc->rx_buf[1] >> 6);
  137. return 0;
  138. case mcp3201:
  139. *val = (adc->rx_buf[0] << 7 | adc->rx_buf[1] >> 1);
  140. return 0;
  141. case mcp3202:
  142. case mcp3204:
  143. case mcp3208:
  144. *val = (adc->rx_buf[0] << 4 | adc->rx_buf[1] >> 4);
  145. return 0;
  146. case mcp3301:
  147. *val = sign_extend32((adc->rx_buf[0] & 0x1f) << 8
  148. | adc->rx_buf[1], 12);
  149. return 0;
  150. case mcp3550_50:
  151. case mcp3550_60:
  152. case mcp3551:
  153. case mcp3553: {
  154. u32 raw = be32_to_cpup((__be32 *)adc->rx_buf);
  155. if (!(adc->spi->mode & SPI_CPOL))
  156. raw <<= 1; /* strip Data Ready bit in SPI mode 0,0 */
  157. /*
  158. * If the input is within -vref and vref, bit 21 is the sign.
  159. * Up to 12% overrange or underrange are allowed, in which case
  160. * bit 23 is the sign and bit 0 to 21 is the value.
  161. */
  162. raw >>= 8;
  163. if (raw & BIT(22) && raw & BIT(23))
  164. return -EIO; /* cannot have overrange AND underrange */
  165. else if (raw & BIT(22))
  166. raw &= ~BIT(22); /* overrange */
  167. else if (raw & BIT(23) || raw & BIT(21))
  168. raw |= GENMASK(31, 22); /* underrange or negative */
  169. *val = (s32)raw;
  170. return 0;
  171. }
  172. default:
  173. return -EINVAL;
  174. }
  175. }
  176. static int mcp320x_read_raw(struct iio_dev *indio_dev,
  177. struct iio_chan_spec const *channel, int *val,
  178. int *val2, long mask)
  179. {
  180. struct mcp320x *adc = iio_priv(indio_dev);
  181. int ret = -EINVAL;
  182. int device_index = 0;
  183. mutex_lock(&adc->lock);
  184. device_index = spi_get_device_id(adc->spi)->driver_data;
  185. switch (mask) {
  186. case IIO_CHAN_INFO_RAW:
  187. ret = mcp320x_adc_conversion(adc, channel->address,
  188. channel->differential, device_index, val);
  189. if (ret < 0)
  190. goto out;
  191. ret = IIO_VAL_INT;
  192. break;
  193. case IIO_CHAN_INFO_SCALE:
  194. ret = regulator_get_voltage(adc->reg);
  195. if (ret < 0)
  196. goto out;
  197. /* convert regulator output voltage to mV */
  198. *val = ret / 1000;
  199. *val2 = adc->chip_info->resolution;
  200. ret = IIO_VAL_FRACTIONAL_LOG2;
  201. break;
  202. }
  203. out:
  204. mutex_unlock(&adc->lock);
  205. return ret;
  206. }
  207. #define MCP320X_VOLTAGE_CHANNEL(num) \
  208. { \
  209. .type = IIO_VOLTAGE, \
  210. .indexed = 1, \
  211. .channel = (num), \
  212. .address = (num), \
  213. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  214. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  215. }
  216. #define MCP320X_VOLTAGE_CHANNEL_DIFF(chan1, chan2) \
  217. { \
  218. .type = IIO_VOLTAGE, \
  219. .indexed = 1, \
  220. .channel = (chan1), \
  221. .channel2 = (chan2), \
  222. .address = (chan1), \
  223. .differential = 1, \
  224. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  225. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  226. }
  227. static const struct iio_chan_spec mcp3201_channels[] = {
  228. MCP320X_VOLTAGE_CHANNEL_DIFF(0, 1),
  229. };
  230. static const struct iio_chan_spec mcp3202_channels[] = {
  231. MCP320X_VOLTAGE_CHANNEL(0),
  232. MCP320X_VOLTAGE_CHANNEL(1),
  233. MCP320X_VOLTAGE_CHANNEL_DIFF(0, 1),
  234. MCP320X_VOLTAGE_CHANNEL_DIFF(1, 0),
  235. };
  236. static const struct iio_chan_spec mcp3204_channels[] = {
  237. MCP320X_VOLTAGE_CHANNEL(0),
  238. MCP320X_VOLTAGE_CHANNEL(1),
  239. MCP320X_VOLTAGE_CHANNEL(2),
  240. MCP320X_VOLTAGE_CHANNEL(3),
  241. MCP320X_VOLTAGE_CHANNEL_DIFF(0, 1),
  242. MCP320X_VOLTAGE_CHANNEL_DIFF(1, 0),
  243. MCP320X_VOLTAGE_CHANNEL_DIFF(2, 3),
  244. MCP320X_VOLTAGE_CHANNEL_DIFF(3, 2),
  245. };
  246. static const struct iio_chan_spec mcp3208_channels[] = {
  247. MCP320X_VOLTAGE_CHANNEL(0),
  248. MCP320X_VOLTAGE_CHANNEL(1),
  249. MCP320X_VOLTAGE_CHANNEL(2),
  250. MCP320X_VOLTAGE_CHANNEL(3),
  251. MCP320X_VOLTAGE_CHANNEL(4),
  252. MCP320X_VOLTAGE_CHANNEL(5),
  253. MCP320X_VOLTAGE_CHANNEL(6),
  254. MCP320X_VOLTAGE_CHANNEL(7),
  255. MCP320X_VOLTAGE_CHANNEL_DIFF(0, 1),
  256. MCP320X_VOLTAGE_CHANNEL_DIFF(1, 0),
  257. MCP320X_VOLTAGE_CHANNEL_DIFF(2, 3),
  258. MCP320X_VOLTAGE_CHANNEL_DIFF(3, 2),
  259. MCP320X_VOLTAGE_CHANNEL_DIFF(4, 5),
  260. MCP320X_VOLTAGE_CHANNEL_DIFF(5, 4),
  261. MCP320X_VOLTAGE_CHANNEL_DIFF(6, 7),
  262. MCP320X_VOLTAGE_CHANNEL_DIFF(7, 6),
  263. };
  264. static const struct iio_info mcp320x_info = {
  265. .read_raw = mcp320x_read_raw,
  266. };
  267. static const struct mcp320x_chip_info mcp320x_chip_infos[] = {
  268. [mcp3001] = {
  269. .channels = mcp3201_channels,
  270. .num_channels = ARRAY_SIZE(mcp3201_channels),
  271. .resolution = 10
  272. },
  273. [mcp3002] = {
  274. .channels = mcp3202_channels,
  275. .num_channels = ARRAY_SIZE(mcp3202_channels),
  276. .resolution = 10
  277. },
  278. [mcp3004] = {
  279. .channels = mcp3204_channels,
  280. .num_channels = ARRAY_SIZE(mcp3204_channels),
  281. .resolution = 10
  282. },
  283. [mcp3008] = {
  284. .channels = mcp3208_channels,
  285. .num_channels = ARRAY_SIZE(mcp3208_channels),
  286. .resolution = 10
  287. },
  288. [mcp3201] = {
  289. .channels = mcp3201_channels,
  290. .num_channels = ARRAY_SIZE(mcp3201_channels),
  291. .resolution = 12
  292. },
  293. [mcp3202] = {
  294. .channels = mcp3202_channels,
  295. .num_channels = ARRAY_SIZE(mcp3202_channels),
  296. .resolution = 12
  297. },
  298. [mcp3204] = {
  299. .channels = mcp3204_channels,
  300. .num_channels = ARRAY_SIZE(mcp3204_channels),
  301. .resolution = 12
  302. },
  303. [mcp3208] = {
  304. .channels = mcp3208_channels,
  305. .num_channels = ARRAY_SIZE(mcp3208_channels),
  306. .resolution = 12
  307. },
  308. [mcp3301] = {
  309. .channels = mcp3201_channels,
  310. .num_channels = ARRAY_SIZE(mcp3201_channels),
  311. .resolution = 13
  312. },
  313. [mcp3550_50] = {
  314. .channels = mcp3201_channels,
  315. .num_channels = ARRAY_SIZE(mcp3201_channels),
  316. .resolution = 21,
  317. /* 2% max deviation + 144 clock periods to exit shutdown */
  318. .conv_time = 80000 * 1.02 + 144000 / 102.4,
  319. },
  320. [mcp3550_60] = {
  321. .channels = mcp3201_channels,
  322. .num_channels = ARRAY_SIZE(mcp3201_channels),
  323. .resolution = 21,
  324. .conv_time = 66670 * 1.02 + 144000 / 122.88,
  325. },
  326. [mcp3551] = {
  327. .channels = mcp3201_channels,
  328. .num_channels = ARRAY_SIZE(mcp3201_channels),
  329. .resolution = 21,
  330. .conv_time = 73100 * 1.02 + 144000 / 112.64,
  331. },
  332. [mcp3553] = {
  333. .channels = mcp3201_channels,
  334. .num_channels = ARRAY_SIZE(mcp3201_channels),
  335. .resolution = 21,
  336. .conv_time = 16670 * 1.02 + 144000 / 122.88,
  337. },
  338. };
  339. static int mcp320x_probe(struct spi_device *spi)
  340. {
  341. struct iio_dev *indio_dev;
  342. struct mcp320x *adc;
  343. const struct mcp320x_chip_info *chip_info;
  344. int ret, device_index;
  345. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  346. if (!indio_dev)
  347. return -ENOMEM;
  348. adc = iio_priv(indio_dev);
  349. adc->spi = spi;
  350. indio_dev->name = spi_get_device_id(spi)->name;
  351. indio_dev->modes = INDIO_DIRECT_MODE;
  352. indio_dev->info = &mcp320x_info;
  353. spi_set_drvdata(spi, indio_dev);
  354. device_index = spi_get_device_id(spi)->driver_data;
  355. chip_info = &mcp320x_chip_infos[device_index];
  356. indio_dev->channels = chip_info->channels;
  357. indio_dev->num_channels = chip_info->num_channels;
  358. adc->chip_info = chip_info;
  359. adc->transfer[0].tx_buf = &adc->tx_buf;
  360. adc->transfer[0].len = sizeof(adc->tx_buf);
  361. adc->transfer[1].rx_buf = adc->rx_buf;
  362. adc->transfer[1].len = DIV_ROUND_UP(chip_info->resolution, 8);
  363. if (chip_info->num_channels == 1)
  364. /* single-channel converters are rx only (no MOSI pin) */
  365. spi_message_init_with_transfers(&adc->msg,
  366. &adc->transfer[1], 1);
  367. else
  368. spi_message_init_with_transfers(&adc->msg, adc->transfer,
  369. ARRAY_SIZE(adc->transfer));
  370. switch (device_index) {
  371. case mcp3550_50:
  372. case mcp3550_60:
  373. case mcp3551:
  374. case mcp3553:
  375. /* rx len increases from 24 to 25 bit in SPI mode 0,0 */
  376. if (!(spi->mode & SPI_CPOL))
  377. adc->transfer[1].len++;
  378. /* conversions are started by asserting CS pin for 8 usec */
  379. adc->start_conv_transfer.delay.value = 8;
  380. adc->start_conv_transfer.delay.unit = SPI_DELAY_UNIT_USECS;
  381. spi_message_init_with_transfers(&adc->start_conv_msg,
  382. &adc->start_conv_transfer, 1);
  383. /*
  384. * If CS was previously kept low (continuous conversion mode)
  385. * and then changed to high, the chip is in shutdown.
  386. * Sometimes it fails to wake from shutdown and clocks out
  387. * only 0xffffff. The magic sequence of performing two
  388. * conversions without delay between them resets the chip
  389. * and ensures all subsequent conversions succeed.
  390. */
  391. mcp320x_adc_conversion(adc, 0, 1, device_index, &ret);
  392. mcp320x_adc_conversion(adc, 0, 1, device_index, &ret);
  393. }
  394. adc->reg = devm_regulator_get(&spi->dev, "vref");
  395. if (IS_ERR(adc->reg))
  396. return PTR_ERR(adc->reg);
  397. ret = regulator_enable(adc->reg);
  398. if (ret < 0)
  399. return ret;
  400. mutex_init(&adc->lock);
  401. ret = iio_device_register(indio_dev);
  402. if (ret < 0)
  403. goto reg_disable;
  404. return 0;
  405. reg_disable:
  406. regulator_disable(adc->reg);
  407. return ret;
  408. }
  409. static void mcp320x_remove(struct spi_device *spi)
  410. {
  411. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  412. struct mcp320x *adc = iio_priv(indio_dev);
  413. iio_device_unregister(indio_dev);
  414. regulator_disable(adc->reg);
  415. }
  416. static const struct of_device_id mcp320x_dt_ids[] = {
  417. /* NOTE: The use of compatibles with no vendor prefix is deprecated. */
  418. { .compatible = "mcp3001" },
  419. { .compatible = "mcp3002" },
  420. { .compatible = "mcp3004" },
  421. { .compatible = "mcp3008" },
  422. { .compatible = "mcp3201" },
  423. { .compatible = "mcp3202" },
  424. { .compatible = "mcp3204" },
  425. { .compatible = "mcp3208" },
  426. { .compatible = "mcp3301" },
  427. { .compatible = "microchip,mcp3001" },
  428. { .compatible = "microchip,mcp3002" },
  429. { .compatible = "microchip,mcp3004" },
  430. { .compatible = "microchip,mcp3008" },
  431. { .compatible = "microchip,mcp3201" },
  432. { .compatible = "microchip,mcp3202" },
  433. { .compatible = "microchip,mcp3204" },
  434. { .compatible = "microchip,mcp3208" },
  435. { .compatible = "microchip,mcp3301" },
  436. { .compatible = "microchip,mcp3550-50" },
  437. { .compatible = "microchip,mcp3550-60" },
  438. { .compatible = "microchip,mcp3551" },
  439. { .compatible = "microchip,mcp3553" },
  440. { }
  441. };
  442. MODULE_DEVICE_TABLE(of, mcp320x_dt_ids);
  443. static const struct spi_device_id mcp320x_id[] = {
  444. { "mcp3001", mcp3001 },
  445. { "mcp3002", mcp3002 },
  446. { "mcp3004", mcp3004 },
  447. { "mcp3008", mcp3008 },
  448. { "mcp3201", mcp3201 },
  449. { "mcp3202", mcp3202 },
  450. { "mcp3204", mcp3204 },
  451. { "mcp3208", mcp3208 },
  452. { "mcp3301", mcp3301 },
  453. { "mcp3550-50", mcp3550_50 },
  454. { "mcp3550-60", mcp3550_60 },
  455. { "mcp3551", mcp3551 },
  456. { "mcp3553", mcp3553 },
  457. { }
  458. };
  459. MODULE_DEVICE_TABLE(spi, mcp320x_id);
  460. static struct spi_driver mcp320x_driver = {
  461. .driver = {
  462. .name = "mcp320x",
  463. .of_match_table = mcp320x_dt_ids,
  464. },
  465. .probe = mcp320x_probe,
  466. .remove = mcp320x_remove,
  467. .id_table = mcp320x_id,
  468. };
  469. module_spi_driver(mcp320x_driver);
  470. MODULE_AUTHOR("Oskar Andero <[email protected]>");
  471. MODULE_DESCRIPTION("Microchip Technology MCP3x01/02/04/08 and MCP3550/1/3");
  472. MODULE_LICENSE("GPL v2");