adc1x8s102.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * ADC1x8S102 SPI ADC driver
  3. *
  4. * Copyright(c) 2013-2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * This IIO device driver is is designed to work with the following
  16. * analog to digital converters from Texas Instruments:
  17. * ADC108S102
  18. * ADC128S102
  19. * The communication with ADC chip is via the SPI bus (mode 3).
  20. */
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/buffer.h>
  23. #include <linux/iio/types.h>
  24. #include <linux/iio/triggered_buffer.h>
  25. #include <linux/iio/trigger_consumer.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/module.h>
  28. #include <linux/spi/spi.h>
  29. /*#include <linux/platform_data/adc1x8s102.h>*/
  30. #include "adc1x8s102.h"
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/delay.h>
  33. #include <linux/acpi.h>
  34. #include <linux/property.h>
  35. #include <linux/gpio.h>
  36. #include <linux/spi/pxa2xx_spi.h>
  37. /*
  38. * Defining the ADC resolution being 12 bits, we can use the same driver for
  39. * both ADC108S102 (10 bits resolution) and ADC128S102 (12 bits resolution)
  40. * chips. The ADC108S102 effectively returns a 12-bit result with the 2
  41. * least-significant bits unset.
  42. */
  43. #define ADC1x8S102_BITS 12
  44. #define ADC1x8S102_MAX_CHANNELS 8
  45. /* 16-bit SPI command format:
  46. * [15:14] Ignored
  47. * [13:11] 3-bit channel address
  48. * [10:0] Ignored
  49. */
  50. #define ADC1x8S102_CMD(ch) (((ch) << (8)) << (3))
  51. /*
  52. * 16-bit SPI response format:
  53. * [15:12] Zeros
  54. * [11:0] 12-bit ADC sample (for ADC108S102, [1:0] will always be 0).
  55. */
  56. #define ADC1x8S102_RES_DATA(res) (res & ((1 << ADC1x8S102_BITS) - 1))
  57. struct adc1x8s102_state {
  58. struct spi_device *spi;
  59. struct regulator *reg;
  60. u16 ext_vin;
  61. /* SPI transfer used by triggered buffer handler*/
  62. struct spi_transfer ring_xfer;
  63. /* SPI transfer used by direct scan */
  64. struct spi_transfer scan_single_xfer;
  65. /* SPI message used by ring_xfer SPI transfer */
  66. struct spi_message ring_msg;
  67. /* SPI message used by scan_single_xfer SPI transfer */
  68. struct spi_message scan_single_msg;
  69. /* SPI message buffers:
  70. * tx_buf: |C0|C1|C2|C3|C4|C5|C6|C7|XX|
  71. * rx_buf: |XX|R0|R1|R2|R3|R4|R5|R6|R7|tt|tt|tt|tt|
  72. *
  73. * tx_buf: 8 channel read commands, plus 1 dummy command
  74. * rx_buf: 1 dummy response, 8 channel responses, plus 64-bit timestamp
  75. */
  76. __be16 rx_buf[13] ____cacheline_aligned;
  77. __be16 tx_buf[9];
  78. };
  79. #define ADC1X8S102_V_CHAN(index) \
  80. { \
  81. .type = IIO_VOLTAGE, \
  82. .indexed = 1, \
  83. .channel = index, \
  84. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  85. BIT(IIO_CHAN_INFO_SCALE), \
  86. .address = index, \
  87. .scan_index = index, \
  88. .scan_type = { \
  89. .sign = 'u', \
  90. .realbits = ADC1x8S102_BITS, \
  91. .storagebits = 16, \
  92. .endianness = IIO_BE, \
  93. }, \
  94. }
  95. static const struct iio_chan_spec adc1x8s102_channels[] = {
  96. ADC1X8S102_V_CHAN(0),
  97. ADC1X8S102_V_CHAN(1),
  98. ADC1X8S102_V_CHAN(2),
  99. ADC1X8S102_V_CHAN(3),
  100. ADC1X8S102_V_CHAN(4),
  101. ADC1X8S102_V_CHAN(5),
  102. ADC1X8S102_V_CHAN(6),
  103. ADC1X8S102_V_CHAN(7),
  104. IIO_CHAN_SOFT_TIMESTAMP(8),
  105. };
  106. static int adc1x8s102_update_scan_mode(struct iio_dev *indio_dev,
  107. unsigned long const *active_scan_mask)
  108. {
  109. struct adc1x8s102_state *st;
  110. int i, j;
  111. st = iio_priv(indio_dev);
  112. /* Fill in the first x shorts of tx_buf with the number of channels
  113. * enabled for sampling by the triggered buffer
  114. */
  115. for (i = 0, j = 0; i < ADC1x8S102_MAX_CHANNELS; i++) {
  116. if (test_bit(i, active_scan_mask)) {
  117. st->tx_buf[j] = cpu_to_be16(ADC1x8S102_CMD(i));
  118. j++;
  119. }
  120. }
  121. /* One dummy command added, to clock in the last response */
  122. st->tx_buf[j] = 0x00;
  123. /* build SPI ring message */
  124. st->ring_xfer.tx_buf = &st->tx_buf[0];
  125. st->ring_xfer.rx_buf = &st->rx_buf[0];
  126. st->ring_xfer.len = (j + 1) * sizeof(__be16);
  127. spi_message_init(&st->ring_msg);
  128. spi_message_add_tail(&st->ring_xfer, &st->ring_msg);
  129. return 0;
  130. }
  131. static irqreturn_t adc1x8s102_trigger_handler(int irq, void *p)
  132. {
  133. struct iio_poll_func *pf = p;
  134. struct iio_dev *indio_dev;
  135. struct adc1x8s102_state *st;
  136. s64 time_ns = 0;
  137. int b_sent;
  138. indio_dev = pf->indio_dev;
  139. st = iio_priv(indio_dev);
  140. b_sent = spi_sync(st->spi, &st->ring_msg);
  141. if (b_sent)
  142. goto done;
  143. if (indio_dev->scan_timestamp) {
  144. time_ns = iio_get_time_ns();
  145. memcpy((u8 *)st->rx_buf + st->ring_xfer.len, &time_ns,
  146. sizeof(time_ns));
  147. }
  148. /* Skip the dummy response in the first slot */
  149. iio_push_to_buffers(indio_dev, (u8 *)&st->rx_buf[1]);
  150. done:
  151. iio_trigger_notify_done(indio_dev->trig);
  152. return IRQ_HANDLED;
  153. }
  154. /*
  155. * returns:
  156. * positive (>=0) value => SUCCESS
  157. * negative value => FAILURE
  158. */
  159. static int adc1x8s102_scan_direct(struct adc1x8s102_state *st, unsigned ch)
  160. {
  161. int ret;
  162. if (ch >= ADC1x8S102_MAX_CHANNELS) {
  163. dev_err(&st->spi->dev, "AD channel number too big: %u\n", ch);
  164. return -1;
  165. }
  166. st->tx_buf[0] = cpu_to_be16(ADC1x8S102_CMD(ch));
  167. ret = spi_sync(st->spi, &st->scan_single_msg);
  168. if (ret)
  169. return ret;
  170. /* Skip the dummy response in the first slot */
  171. return be16_to_cpu(st->rx_buf[1]);
  172. }
  173. /*
  174. * returns:
  175. * positive (>=0) value => SUCCESS
  176. * negative value => FAILURE
  177. */
  178. static int adc1x8s102_read_raw(struct iio_dev *indio_dev,
  179. struct iio_chan_spec const *chan,
  180. int *val,
  181. int *val2,
  182. long m)
  183. {
  184. int ret;
  185. struct adc1x8s102_state *st;
  186. st = iio_priv(indio_dev);
  187. switch (m) {
  188. case IIO_CHAN_INFO_RAW:
  189. mutex_lock(&indio_dev->mlock);
  190. if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
  191. ret = -EBUSY;
  192. dev_warn(&st->spi->dev,
  193. "indio_dev->currentmode is INDIO_BUFFER_TRIGGERED\n");
  194. } else {
  195. ret = adc1x8s102_scan_direct(st, chan->address);
  196. }
  197. mutex_unlock(&indio_dev->mlock);
  198. if (ret < 0)
  199. return ret;
  200. *val = ADC1x8S102_RES_DATA(ret);
  201. return IIO_VAL_INT;
  202. case IIO_CHAN_INFO_SCALE:
  203. switch (chan->type) {
  204. case IIO_VOLTAGE:
  205. if (NULL != st->reg)
  206. *val = regulator_get_voltage(st->reg) / 1000;
  207. else
  208. *val = st->ext_vin;
  209. *val2 = chan->scan_type.realbits;
  210. return IIO_VAL_FRACTIONAL_LOG2;
  211. default:
  212. dev_warn(&st->spi->dev,
  213. "Invalid channel type %u for channel %d\n",
  214. chan->type, chan->channel);
  215. return -EINVAL;
  216. }
  217. default:
  218. dev_warn(&st->spi->dev, "Invalid IIO_CHAN_INFO: %lu\n", m);
  219. return -EINVAL;
  220. }
  221. }
  222. static const struct iio_info adc1x8s102_info = {
  223. .read_raw = &adc1x8s102_read_raw,
  224. .update_scan_mode = &adc1x8s102_update_scan_mode,
  225. .driver_module = THIS_MODULE,
  226. };
  227. struct adc1x8s102_spi_info {
  228. kernel_ulong_t driver_data;
  229. void (*setup)(struct spi_device *spi);
  230. };
  231. static void adc1x8s102_setup_int3495(struct spi_device *spi)
  232. {
  233. /* Galileo Gen 2 SPI setup */
  234. #define ADC1x8S102_GALILEO2_CS 8
  235. struct pxa2xx_spi_chip *chip_data;
  236. chip_data = devm_kzalloc(&spi->dev, sizeof(*chip_data), GFP_KERNEL);
  237. if (chip_data) {
  238. chip_data->gpio_cs = ADC1x8S102_GALILEO2_CS;
  239. spi->controller_data = chip_data;
  240. dev_info(&spi->dev, "setting GPIO CS value to %d\n", chip_data->gpio_cs);
  241. spi_setup(spi);
  242. }
  243. }
  244. static const struct adc1x8s102_spi_info adc1x8s102_info_int3495 = {
  245. .driver_data = 0,
  246. .setup = adc1x8s102_setup_int3495,
  247. };
  248. static const struct acpi_device_id adc1x8s102_acpi_ids[] = {
  249. { "INT3495", (kernel_ulong_t)&adc1x8s102_info_int3495 },
  250. { }
  251. };
  252. MODULE_DEVICE_TABLE(acpi, adc1x8s102_acpi_ids);
  253. static int adc1x8s102_probe(struct spi_device *spi)
  254. {
  255. struct adc1x8s102_platform_data *pdata = spi->dev.platform_data;
  256. struct adc1x8s102_state *st;
  257. struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
  258. const struct acpi_device_id *id;
  259. int ret;
  260. id = acpi_match_device(adc1x8s102_acpi_ids, &spi->dev);
  261. if (id) {
  262. const struct adc1x8s102_spi_info *info =
  263. (struct adc1x8s102_spi_info *)id->driver_data;
  264. if (!info)
  265. return -ENODEV;
  266. if (info->setup)
  267. info->setup(spi);
  268. }
  269. if (NULL == indio_dev) {
  270. dev_crit(&spi->dev, "Cannot allocate memory for indio_dev\n");
  271. return -ENOMEM;
  272. }
  273. st = iio_priv(indio_dev);
  274. if (NULL == pdata) {
  275. dev_warn(&spi->dev, "Cannot get adc1x8s102 platform data\n");
  276. /* FIXME: make this ACPI-dependent */
  277. st->ext_vin = 5000;
  278. }
  279. else {
  280. st->ext_vin = pdata->ext_vin;
  281. }
  282. /* Use regulator, if available. */
  283. st->reg = regulator_get(&spi->dev, "vref");
  284. if (IS_ERR(st->reg)) {
  285. ret = PTR_ERR(st->reg);
  286. dev_warn(&spi->dev,
  287. "Cannot get 'vref' regulator\n");
  288. goto error_free;
  289. }
  290. ret = regulator_enable(st->reg);
  291. if (ret < 0) {
  292. dev_warn(&spi->dev,
  293. "Cannot enable vref regulator\n");
  294. goto error_put_reg;
  295. }
  296. spi_set_drvdata(spi, indio_dev);
  297. st->spi = spi;
  298. indio_dev->name = spi->modalias;
  299. indio_dev->dev.parent = &spi->dev;
  300. indio_dev->modes = INDIO_DIRECT_MODE;
  301. indio_dev->channels = adc1x8s102_channels;
  302. indio_dev->num_channels = ARRAY_SIZE(adc1x8s102_channels);
  303. indio_dev->info = &adc1x8s102_info;
  304. /* Setup default message */
  305. st->scan_single_xfer.tx_buf = st->tx_buf;
  306. st->scan_single_xfer.rx_buf = st->rx_buf;
  307. st->scan_single_xfer.len = 2 * sizeof(__be16);
  308. st->scan_single_xfer.cs_change = 0;
  309. spi_message_init(&st->scan_single_msg);
  310. spi_message_add_tail(&st->scan_single_xfer, &st->scan_single_msg);
  311. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  312. &adc1x8s102_trigger_handler, NULL);
  313. if (ret)
  314. goto error_disable_reg;
  315. ret = iio_device_register(indio_dev);
  316. if (ret) {
  317. dev_err(&spi->dev,
  318. "Failed to register IIO device\n");
  319. goto error_cleanup_ring;
  320. }
  321. return 0;
  322. error_cleanup_ring:
  323. iio_triggered_buffer_cleanup(indio_dev);
  324. error_disable_reg:
  325. regulator_disable(st->reg);
  326. error_put_reg:
  327. regulator_put(st->reg);
  328. error_free:
  329. iio_device_free(indio_dev);
  330. return ret;
  331. }
  332. static int adc1x8s102_remove(struct spi_device *spi)
  333. {
  334. struct iio_dev *indio_dev;
  335. struct adc1x8s102_state *st;
  336. indio_dev = spi_get_drvdata(spi);
  337. if (NULL == indio_dev) {
  338. dev_err(&spi->dev, "Cannot get spi_device drvdata\n");
  339. return -EFAULT;
  340. }
  341. st = iio_priv(indio_dev);
  342. iio_device_unregister(indio_dev);
  343. iio_triggered_buffer_cleanup(indio_dev);
  344. regulator_disable(st->reg);
  345. regulator_put(st->reg);
  346. iio_device_free(indio_dev);
  347. return 0;
  348. }
  349. static struct spi_driver adc1x8s102_driver = {
  350. .driver = {
  351. .name = "adc1x8s102",
  352. .owner = THIS_MODULE,
  353. .acpi_match_table = ACPI_PTR(adc1x8s102_acpi_ids),
  354. },
  355. .probe = adc1x8s102_probe,
  356. .remove = adc1x8s102_remove,
  357. };
  358. module_spi_driver(adc1x8s102_driver);
  359. MODULE_AUTHOR("Bogdan Pricop <bogdan.pricop@emutex.com>");
  360. MODULE_DESCRIPTION("Texas Instruments ADC1x8S102 driver");
  361. MODULE_LICENSE("GPL v2");