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