ad_sigma_delta.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * Support code for Analog Devices Sigma-Delta ADCs
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/iio/buffer.h>
  19. #include <linux/iio/trigger.h>
  20. #include <linux/iio/trigger_consumer.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #include <linux/iio/adc/ad_sigma_delta.h>
  23. #include <asm/unaligned.h>
  24. #define AD_SD_COMM_CHAN_MASK 0x3
  25. #define AD_SD_REG_COMM 0x00
  26. #define AD_SD_REG_DATA 0x03
  27. /**
  28. * ad_sd_set_comm() - Set communications register
  29. *
  30. * @sigma_delta: The sigma delta device
  31. * @comm: New value for the communications register
  32. */
  33. void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm)
  34. {
  35. /* Some variants use the lower two bits of the communications register
  36. * to select the channel */
  37. sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK;
  38. }
  39. EXPORT_SYMBOL_GPL(ad_sd_set_comm);
  40. /**
  41. * ad_sd_write_reg() - Write a register
  42. *
  43. * @sigma_delta: The sigma delta device
  44. * @reg: Address of the register
  45. * @size: Size of the register (0-3)
  46. * @val: Value to write to the register
  47. *
  48. * Returns 0 on success, an error code otherwise.
  49. **/
  50. int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
  51. unsigned int size, unsigned int val)
  52. {
  53. uint8_t *data = sigma_delta->data;
  54. struct spi_transfer t = {
  55. .tx_buf = data,
  56. .len = size + 1,
  57. .cs_change = sigma_delta->bus_locked,
  58. };
  59. struct spi_message m;
  60. int ret;
  61. data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm;
  62. switch (size) {
  63. case 3:
  64. data[1] = val >> 16;
  65. data[2] = val >> 8;
  66. data[3] = val;
  67. break;
  68. case 2:
  69. put_unaligned_be16(val, &data[1]);
  70. break;
  71. case 1:
  72. data[1] = val;
  73. break;
  74. case 0:
  75. break;
  76. default:
  77. return -EINVAL;
  78. }
  79. spi_message_init(&m);
  80. spi_message_add_tail(&t, &m);
  81. if (sigma_delta->bus_locked)
  82. ret = spi_sync_locked(sigma_delta->spi, &m);
  83. else
  84. ret = spi_sync(sigma_delta->spi, &m);
  85. return ret;
  86. }
  87. EXPORT_SYMBOL_GPL(ad_sd_write_reg);
  88. static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
  89. unsigned int reg, unsigned int size, uint8_t *val)
  90. {
  91. uint8_t *data = sigma_delta->data;
  92. int ret;
  93. struct spi_transfer t[] = {
  94. {
  95. .tx_buf = data,
  96. .len = 1,
  97. }, {
  98. .rx_buf = val,
  99. .len = size,
  100. .cs_change = sigma_delta->bus_locked,
  101. },
  102. };
  103. struct spi_message m;
  104. spi_message_init(&m);
  105. if (sigma_delta->info->has_registers) {
  106. data[0] = reg << sigma_delta->info->addr_shift;
  107. data[0] |= sigma_delta->info->read_mask;
  108. data[0] |= sigma_delta->comm;
  109. spi_message_add_tail(&t[0], &m);
  110. }
  111. spi_message_add_tail(&t[1], &m);
  112. if (sigma_delta->bus_locked)
  113. ret = spi_sync_locked(sigma_delta->spi, &m);
  114. else
  115. ret = spi_sync(sigma_delta->spi, &m);
  116. return ret;
  117. }
  118. /**
  119. * ad_sd_read_reg() - Read a register
  120. *
  121. * @sigma_delta: The sigma delta device
  122. * @reg: Address of the register
  123. * @size: Size of the register (1-4)
  124. * @val: Read value
  125. *
  126. * Returns 0 on success, an error code otherwise.
  127. **/
  128. int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
  129. unsigned int reg, unsigned int size, unsigned int *val)
  130. {
  131. int ret;
  132. ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
  133. if (ret < 0)
  134. goto out;
  135. switch (size) {
  136. case 4:
  137. *val = get_unaligned_be32(sigma_delta->data);
  138. break;
  139. case 3:
  140. *val = (sigma_delta->data[0] << 16) |
  141. (sigma_delta->data[1] << 8) |
  142. sigma_delta->data[2];
  143. break;
  144. case 2:
  145. *val = get_unaligned_be16(sigma_delta->data);
  146. break;
  147. case 1:
  148. *val = sigma_delta->data[0];
  149. break;
  150. default:
  151. ret = -EINVAL;
  152. break;
  153. }
  154. out:
  155. return ret;
  156. }
  157. EXPORT_SYMBOL_GPL(ad_sd_read_reg);
  158. /**
  159. * ad_sd_reset() - Reset the serial interface
  160. *
  161. * @sigma_delta: The sigma delta device
  162. * @reset_length: Number of SCLKs with DIN = 1
  163. *
  164. * Returns 0 on success, an error code otherwise.
  165. **/
  166. int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
  167. unsigned int reset_length)
  168. {
  169. uint8_t *buf;
  170. unsigned int size;
  171. int ret;
  172. size = DIV_ROUND_UP(reset_length, 8);
  173. buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
  174. if (!buf)
  175. return -ENOMEM;
  176. memset(buf, 0xff, size);
  177. ret = spi_write(sigma_delta->spi, buf, size);
  178. kfree(buf);
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(ad_sd_reset);
  182. static int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
  183. unsigned int mode, unsigned int channel)
  184. {
  185. int ret;
  186. ret = ad_sigma_delta_set_channel(sigma_delta, channel);
  187. if (ret)
  188. return ret;
  189. spi_bus_lock(sigma_delta->spi->master);
  190. sigma_delta->bus_locked = true;
  191. reinit_completion(&sigma_delta->completion);
  192. ret = ad_sigma_delta_set_mode(sigma_delta, mode);
  193. if (ret < 0)
  194. goto out;
  195. sigma_delta->irq_dis = false;
  196. enable_irq(sigma_delta->spi->irq);
  197. ret = wait_for_completion_timeout(&sigma_delta->completion, 2*HZ);
  198. if (ret == 0) {
  199. sigma_delta->irq_dis = true;
  200. disable_irq_nosync(sigma_delta->spi->irq);
  201. ret = -EIO;
  202. } else {
  203. ret = 0;
  204. }
  205. out:
  206. sigma_delta->bus_locked = false;
  207. spi_bus_unlock(sigma_delta->spi->master);
  208. ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
  209. return ret;
  210. }
  211. /**
  212. * ad_sd_calibrate_all() - Performs channel calibration
  213. * @sigma_delta: The sigma delta device
  214. * @cb: Array of channels and calibration type to perform
  215. * @n: Number of items in cb
  216. *
  217. * Returns 0 on success, an error code otherwise.
  218. **/
  219. int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
  220. const struct ad_sd_calib_data *cb, unsigned int n)
  221. {
  222. unsigned int i;
  223. int ret;
  224. for (i = 0; i < n; i++) {
  225. ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
  226. if (ret)
  227. return ret;
  228. }
  229. return 0;
  230. }
  231. EXPORT_SYMBOL_GPL(ad_sd_calibrate_all);
  232. /**
  233. * ad_sigma_delta_single_conversion() - Performs a single data conversion
  234. * @indio_dev: The IIO device
  235. * @chan: The conversion is done for this channel
  236. * @val: Pointer to the location where to store the read value
  237. *
  238. * Returns: 0 on success, an error value otherwise.
  239. */
  240. int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
  241. const struct iio_chan_spec *chan, int *val)
  242. {
  243. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  244. unsigned int sample, raw_sample;
  245. int ret = 0;
  246. if (iio_buffer_enabled(indio_dev))
  247. return -EBUSY;
  248. mutex_lock(&indio_dev->mlock);
  249. ad_sigma_delta_set_channel(sigma_delta, chan->address);
  250. spi_bus_lock(sigma_delta->spi->master);
  251. sigma_delta->bus_locked = true;
  252. reinit_completion(&sigma_delta->completion);
  253. ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
  254. sigma_delta->irq_dis = false;
  255. enable_irq(sigma_delta->spi->irq);
  256. ret = wait_for_completion_interruptible_timeout(
  257. &sigma_delta->completion, HZ);
  258. sigma_delta->bus_locked = false;
  259. spi_bus_unlock(sigma_delta->spi->master);
  260. if (ret == 0)
  261. ret = -EIO;
  262. if (ret < 0)
  263. goto out;
  264. ret = ad_sd_read_reg(sigma_delta, AD_SD_REG_DATA,
  265. DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
  266. &raw_sample);
  267. out:
  268. if (!sigma_delta->irq_dis) {
  269. disable_irq_nosync(sigma_delta->spi->irq);
  270. sigma_delta->irq_dis = true;
  271. }
  272. ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
  273. mutex_unlock(&indio_dev->mlock);
  274. if (ret)
  275. return ret;
  276. sample = raw_sample >> chan->scan_type.shift;
  277. sample &= (1 << chan->scan_type.realbits) - 1;
  278. *val = sample;
  279. ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
  280. if (ret)
  281. return ret;
  282. return IIO_VAL_INT;
  283. }
  284. EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion);
  285. static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
  286. {
  287. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  288. unsigned int channel;
  289. int ret;
  290. ret = iio_triggered_buffer_postenable(indio_dev);
  291. if (ret < 0)
  292. return ret;
  293. channel = find_first_bit(indio_dev->active_scan_mask,
  294. indio_dev->masklength);
  295. ret = ad_sigma_delta_set_channel(sigma_delta,
  296. indio_dev->channels[channel].address);
  297. if (ret)
  298. goto err_predisable;
  299. spi_bus_lock(sigma_delta->spi->master);
  300. sigma_delta->bus_locked = true;
  301. ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
  302. if (ret)
  303. goto err_unlock;
  304. sigma_delta->irq_dis = false;
  305. enable_irq(sigma_delta->spi->irq);
  306. return 0;
  307. err_unlock:
  308. spi_bus_unlock(sigma_delta->spi->master);
  309. err_predisable:
  310. return ret;
  311. }
  312. static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
  313. {
  314. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  315. reinit_completion(&sigma_delta->completion);
  316. wait_for_completion_timeout(&sigma_delta->completion, HZ);
  317. if (!sigma_delta->irq_dis) {
  318. disable_irq_nosync(sigma_delta->spi->irq);
  319. sigma_delta->irq_dis = true;
  320. }
  321. ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
  322. sigma_delta->bus_locked = false;
  323. return spi_bus_unlock(sigma_delta->spi->master);
  324. }
  325. static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
  326. {
  327. struct iio_poll_func *pf = p;
  328. struct iio_dev *indio_dev = pf->indio_dev;
  329. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  330. unsigned int reg_size;
  331. uint8_t data[16];
  332. int ret;
  333. memset(data, 0x00, 16);
  334. reg_size = indio_dev->channels[0].scan_type.realbits +
  335. indio_dev->channels[0].scan_type.shift;
  336. reg_size = DIV_ROUND_UP(reg_size, 8);
  337. switch (reg_size) {
  338. case 4:
  339. case 2:
  340. case 1:
  341. ret = ad_sd_read_reg_raw(sigma_delta, AD_SD_REG_DATA,
  342. reg_size, &data[0]);
  343. break;
  344. case 3:
  345. /* We store 24 bit samples in a 32 bit word. Keep the upper
  346. * byte set to zero. */
  347. ret = ad_sd_read_reg_raw(sigma_delta, AD_SD_REG_DATA,
  348. reg_size, &data[1]);
  349. break;
  350. }
  351. iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
  352. iio_trigger_notify_done(indio_dev->trig);
  353. sigma_delta->irq_dis = false;
  354. enable_irq(sigma_delta->spi->irq);
  355. return IRQ_HANDLED;
  356. }
  357. static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
  358. .postenable = &ad_sd_buffer_postenable,
  359. .predisable = &iio_triggered_buffer_predisable,
  360. .postdisable = &ad_sd_buffer_postdisable,
  361. .validate_scan_mask = &iio_validate_scan_mask_onehot,
  362. };
  363. static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
  364. {
  365. struct ad_sigma_delta *sigma_delta = private;
  366. complete(&sigma_delta->completion);
  367. disable_irq_nosync(irq);
  368. sigma_delta->irq_dis = true;
  369. iio_trigger_poll(sigma_delta->trig);
  370. return IRQ_HANDLED;
  371. }
  372. /**
  373. * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
  374. * @indio_dev: The IIO device
  375. * @trig: The new trigger
  376. *
  377. * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
  378. * device, -EINVAL otherwise.
  379. */
  380. int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
  381. {
  382. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  383. if (sigma_delta->trig != trig)
  384. return -EINVAL;
  385. return 0;
  386. }
  387. EXPORT_SYMBOL_GPL(ad_sd_validate_trigger);
  388. static const struct iio_trigger_ops ad_sd_trigger_ops = {
  389. .owner = THIS_MODULE,
  390. };
  391. static int ad_sd_probe_trigger(struct iio_dev *indio_dev)
  392. {
  393. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  394. int ret;
  395. sigma_delta->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
  396. indio_dev->id);
  397. if (sigma_delta->trig == NULL) {
  398. ret = -ENOMEM;
  399. goto error_ret;
  400. }
  401. sigma_delta->trig->ops = &ad_sd_trigger_ops;
  402. init_completion(&sigma_delta->completion);
  403. ret = request_irq(sigma_delta->spi->irq,
  404. ad_sd_data_rdy_trig_poll,
  405. IRQF_TRIGGER_LOW,
  406. indio_dev->name,
  407. sigma_delta);
  408. if (ret)
  409. goto error_free_trig;
  410. if (!sigma_delta->irq_dis) {
  411. sigma_delta->irq_dis = true;
  412. disable_irq_nosync(sigma_delta->spi->irq);
  413. }
  414. sigma_delta->trig->dev.parent = &sigma_delta->spi->dev;
  415. iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
  416. ret = iio_trigger_register(sigma_delta->trig);
  417. if (ret)
  418. goto error_free_irq;
  419. /* select default trigger */
  420. indio_dev->trig = iio_trigger_get(sigma_delta->trig);
  421. return 0;
  422. error_free_irq:
  423. free_irq(sigma_delta->spi->irq, sigma_delta);
  424. error_free_trig:
  425. iio_trigger_free(sigma_delta->trig);
  426. error_ret:
  427. return ret;
  428. }
  429. static void ad_sd_remove_trigger(struct iio_dev *indio_dev)
  430. {
  431. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  432. iio_trigger_unregister(sigma_delta->trig);
  433. free_irq(sigma_delta->spi->irq, sigma_delta);
  434. iio_trigger_free(sigma_delta->trig);
  435. }
  436. /**
  437. * ad_sd_setup_buffer_and_trigger() -
  438. * @indio_dev: The IIO device
  439. */
  440. int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev)
  441. {
  442. int ret;
  443. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  444. &ad_sd_trigger_handler, &ad_sd_buffer_setup_ops);
  445. if (ret)
  446. return ret;
  447. ret = ad_sd_probe_trigger(indio_dev);
  448. if (ret) {
  449. iio_triggered_buffer_cleanup(indio_dev);
  450. return ret;
  451. }
  452. return 0;
  453. }
  454. EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger);
  455. /**
  456. * ad_sd_cleanup_buffer_and_trigger() -
  457. * @indio_dev: The IIO device
  458. */
  459. void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev)
  460. {
  461. ad_sd_remove_trigger(indio_dev);
  462. iio_triggered_buffer_cleanup(indio_dev);
  463. }
  464. EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger);
  465. /**
  466. * ad_sd_init() - Initializes a ad_sigma_delta struct
  467. * @sigma_delta: The ad_sigma_delta device
  468. * @indio_dev: The IIO device which the Sigma Delta device is used for
  469. * @spi: The SPI device for the ad_sigma_delta device
  470. * @info: Device specific callbacks and options
  471. *
  472. * This function needs to be called before any other operations are performed on
  473. * the ad_sigma_delta struct.
  474. */
  475. int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
  476. struct spi_device *spi, const struct ad_sigma_delta_info *info)
  477. {
  478. sigma_delta->spi = spi;
  479. sigma_delta->info = info;
  480. iio_device_set_drvdata(indio_dev, sigma_delta);
  481. return 0;
  482. }
  483. EXPORT_SYMBOL_GPL(ad_sd_init);
  484. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  485. MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
  486. MODULE_LICENSE("GPL v2");