mcp320x.c 14 KB

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