PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/iio/adc/envelope-detector.c

https://gitlab.com/kush/linux
C | 418 lines | 279 code | 68 blank | 71 comment | 34 complexity | cc4b9a0af062dfd10172ae7e3545f55f MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for an envelope detector using a DAC and a comparator
  4. *
  5. * Copyright (C) 2016 Axentia Technologies AB
  6. *
  7. * Author: Peter Rosin <peda@axentia.se>
  8. */
  9. /*
  10. * The DAC is used to find the peak level of an alternating voltage input
  11. * signal by a binary search using the output of a comparator wired to
  12. * an interrupt pin. Like so:
  13. * _
  14. * | \
  15. * input +------>-------|+ \
  16. * | \
  17. * .-------. | }---.
  18. * | | | / |
  19. * | dac|-->--|- / |
  20. * | | |_/ |
  21. * | | |
  22. * | | |
  23. * | irq|------<-------'
  24. * | |
  25. * '-------'
  26. */
  27. #include <linux/completion.h>
  28. #include <linux/device.h>
  29. #include <linux/err.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/mutex.h>
  33. #include <linux/iio/consumer.h>
  34. #include <linux/iio/iio.h>
  35. #include <linux/iio/sysfs.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/irq.h>
  38. #include <linux/of.h>
  39. #include <linux/of_device.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/workqueue.h>
  43. struct envelope {
  44. spinlock_t comp_lock; /* protects comp */
  45. int comp;
  46. struct mutex read_lock; /* protects everything else */
  47. int comp_irq;
  48. u32 comp_irq_trigger;
  49. u32 comp_irq_trigger_inv;
  50. struct iio_channel *dac;
  51. struct delayed_work comp_timeout;
  52. unsigned int comp_interval;
  53. bool invert;
  54. u32 dac_max;
  55. int high;
  56. int level;
  57. int low;
  58. struct completion done;
  59. };
  60. /*
  61. * The envelope_detector_comp_latch function works together with the compare
  62. * interrupt service routine below (envelope_detector_comp_isr) as a latch
  63. * (one-bit memory) for if the interrupt has triggered since last calling
  64. * this function.
  65. * The ..._comp_isr function disables the interrupt so that the cpu does not
  66. * need to service a possible interrupt flood from the comparator when no-one
  67. * cares anyway, and this ..._comp_latch function reenables them again if
  68. * needed.
  69. */
  70. static int envelope_detector_comp_latch(struct envelope *env)
  71. {
  72. int comp;
  73. spin_lock_irq(&env->comp_lock);
  74. comp = env->comp;
  75. env->comp = 0;
  76. spin_unlock_irq(&env->comp_lock);
  77. if (!comp)
  78. return 0;
  79. /*
  80. * The irq was disabled, and is reenabled just now.
  81. * But there might have been a pending irq that
  82. * happened while the irq was disabled that fires
  83. * just as the irq is reenabled. That is not what
  84. * is desired.
  85. */
  86. enable_irq(env->comp_irq);
  87. /* So, synchronize this possibly pending irq... */
  88. synchronize_irq(env->comp_irq);
  89. /* ...and redo the whole dance. */
  90. spin_lock_irq(&env->comp_lock);
  91. comp = env->comp;
  92. env->comp = 0;
  93. spin_unlock_irq(&env->comp_lock);
  94. if (comp)
  95. enable_irq(env->comp_irq);
  96. return 1;
  97. }
  98. static irqreturn_t envelope_detector_comp_isr(int irq, void *ctx)
  99. {
  100. struct envelope *env = ctx;
  101. spin_lock(&env->comp_lock);
  102. env->comp = 1;
  103. disable_irq_nosync(env->comp_irq);
  104. spin_unlock(&env->comp_lock);
  105. return IRQ_HANDLED;
  106. }
  107. static void envelope_detector_setup_compare(struct envelope *env)
  108. {
  109. int ret;
  110. /*
  111. * Do a binary search for the peak input level, and stop
  112. * when that level is "trapped" between two adjacent DAC
  113. * values.
  114. * When invert is active, use the midpoint floor so that
  115. * env->level ends up as env->low when the termination
  116. * criteria below is fulfilled, and use the midpoint
  117. * ceiling when invert is not active so that env->level
  118. * ends up as env->high in that case.
  119. */
  120. env->level = (env->high + env->low + !env->invert) / 2;
  121. if (env->high == env->low + 1) {
  122. complete(&env->done);
  123. return;
  124. }
  125. /* Set a "safe" DAC level (if there is such a thing)... */
  126. ret = iio_write_channel_raw(env->dac, env->invert ? 0 : env->dac_max);
  127. if (ret < 0)
  128. goto err;
  129. /* ...clear the comparison result... */
  130. envelope_detector_comp_latch(env);
  131. /* ...set the real DAC level... */
  132. ret = iio_write_channel_raw(env->dac, env->level);
  133. if (ret < 0)
  134. goto err;
  135. /* ...and wait for a bit to see if the latch catches anything. */
  136. schedule_delayed_work(&env->comp_timeout,
  137. msecs_to_jiffies(env->comp_interval));
  138. return;
  139. err:
  140. env->level = ret;
  141. complete(&env->done);
  142. }
  143. static void envelope_detector_timeout(struct work_struct *work)
  144. {
  145. struct envelope *env = container_of(work, struct envelope,
  146. comp_timeout.work);
  147. /* Adjust low/high depending on the latch content... */
  148. if (!envelope_detector_comp_latch(env) ^ !env->invert)
  149. env->low = env->level;
  150. else
  151. env->high = env->level;
  152. /* ...and continue the search. */
  153. envelope_detector_setup_compare(env);
  154. }
  155. static int envelope_detector_read_raw(struct iio_dev *indio_dev,
  156. struct iio_chan_spec const *chan,
  157. int *val, int *val2, long mask)
  158. {
  159. struct envelope *env = iio_priv(indio_dev);
  160. int ret;
  161. switch (mask) {
  162. case IIO_CHAN_INFO_RAW:
  163. /*
  164. * When invert is active, start with high=max+1 and low=0
  165. * since we will end up with the low value when the
  166. * termination criteria is fulfilled (rounding down). And
  167. * start with high=max and low=-1 when invert is not active
  168. * since we will end up with the high value in that case.
  169. * This ensures that the returned value in both cases are
  170. * in the same range as the DAC and is a value that has not
  171. * triggered the comparator.
  172. */
  173. mutex_lock(&env->read_lock);
  174. env->high = env->dac_max + env->invert;
  175. env->low = -1 + env->invert;
  176. envelope_detector_setup_compare(env);
  177. wait_for_completion(&env->done);
  178. if (env->level < 0) {
  179. ret = env->level;
  180. goto err_unlock;
  181. }
  182. *val = env->invert ? env->dac_max - env->level : env->level;
  183. mutex_unlock(&env->read_lock);
  184. return IIO_VAL_INT;
  185. case IIO_CHAN_INFO_SCALE:
  186. return iio_read_channel_scale(env->dac, val, val2);
  187. }
  188. return -EINVAL;
  189. err_unlock:
  190. mutex_unlock(&env->read_lock);
  191. return ret;
  192. }
  193. static ssize_t envelope_show_invert(struct iio_dev *indio_dev,
  194. uintptr_t private,
  195. struct iio_chan_spec const *ch, char *buf)
  196. {
  197. struct envelope *env = iio_priv(indio_dev);
  198. return sprintf(buf, "%u\n", env->invert);
  199. }
  200. static ssize_t envelope_store_invert(struct iio_dev *indio_dev,
  201. uintptr_t private,
  202. struct iio_chan_spec const *ch,
  203. const char *buf, size_t len)
  204. {
  205. struct envelope *env = iio_priv(indio_dev);
  206. unsigned long invert;
  207. int ret;
  208. u32 trigger;
  209. ret = kstrtoul(buf, 0, &invert);
  210. if (ret < 0)
  211. return ret;
  212. if (invert > 1)
  213. return -EINVAL;
  214. trigger = invert ? env->comp_irq_trigger_inv : env->comp_irq_trigger;
  215. mutex_lock(&env->read_lock);
  216. if (invert != env->invert)
  217. ret = irq_set_irq_type(env->comp_irq, trigger);
  218. if (!ret) {
  219. env->invert = invert;
  220. ret = len;
  221. }
  222. mutex_unlock(&env->read_lock);
  223. return ret;
  224. }
  225. static ssize_t envelope_show_comp_interval(struct iio_dev *indio_dev,
  226. uintptr_t private,
  227. struct iio_chan_spec const *ch,
  228. char *buf)
  229. {
  230. struct envelope *env = iio_priv(indio_dev);
  231. return sprintf(buf, "%u\n", env->comp_interval);
  232. }
  233. static ssize_t envelope_store_comp_interval(struct iio_dev *indio_dev,
  234. uintptr_t private,
  235. struct iio_chan_spec const *ch,
  236. const char *buf, size_t len)
  237. {
  238. struct envelope *env = iio_priv(indio_dev);
  239. unsigned long interval;
  240. int ret;
  241. ret = kstrtoul(buf, 0, &interval);
  242. if (ret < 0)
  243. return ret;
  244. if (interval > 1000)
  245. return -EINVAL;
  246. mutex_lock(&env->read_lock);
  247. env->comp_interval = interval;
  248. mutex_unlock(&env->read_lock);
  249. return len;
  250. }
  251. static const struct iio_chan_spec_ext_info envelope_detector_ext_info[] = {
  252. { .name = "invert",
  253. .read = envelope_show_invert,
  254. .write = envelope_store_invert, },
  255. { .name = "compare_interval",
  256. .read = envelope_show_comp_interval,
  257. .write = envelope_store_comp_interval, },
  258. { /* sentinel */ }
  259. };
  260. static const struct iio_chan_spec envelope_detector_iio_channel = {
  261. .type = IIO_ALTVOLTAGE,
  262. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
  263. | BIT(IIO_CHAN_INFO_SCALE),
  264. .ext_info = envelope_detector_ext_info,
  265. .indexed = 1,
  266. };
  267. static const struct iio_info envelope_detector_info = {
  268. .read_raw = &envelope_detector_read_raw,
  269. };
  270. static int envelope_detector_probe(struct platform_device *pdev)
  271. {
  272. struct device *dev = &pdev->dev;
  273. struct iio_dev *indio_dev;
  274. struct envelope *env;
  275. enum iio_chan_type type;
  276. int ret;
  277. indio_dev = devm_iio_device_alloc(dev, sizeof(*env));
  278. if (!indio_dev)
  279. return -ENOMEM;
  280. platform_set_drvdata(pdev, indio_dev);
  281. env = iio_priv(indio_dev);
  282. env->comp_interval = 50; /* some sensible default? */
  283. spin_lock_init(&env->comp_lock);
  284. mutex_init(&env->read_lock);
  285. init_completion(&env->done);
  286. INIT_DELAYED_WORK(&env->comp_timeout, envelope_detector_timeout);
  287. indio_dev->name = dev_name(dev);
  288. indio_dev->dev.parent = dev;
  289. indio_dev->dev.of_node = dev->of_node;
  290. indio_dev->info = &envelope_detector_info;
  291. indio_dev->channels = &envelope_detector_iio_channel;
  292. indio_dev->num_channels = 1;
  293. env->dac = devm_iio_channel_get(dev, "dac");
  294. if (IS_ERR(env->dac)) {
  295. if (PTR_ERR(env->dac) != -EPROBE_DEFER)
  296. dev_err(dev, "failed to get dac input channel\n");
  297. return PTR_ERR(env->dac);
  298. }
  299. env->comp_irq = platform_get_irq_byname(pdev, "comp");
  300. if (env->comp_irq < 0) {
  301. if (env->comp_irq != -EPROBE_DEFER)
  302. dev_err(dev, "failed to get compare interrupt\n");
  303. return env->comp_irq;
  304. }
  305. ret = devm_request_irq(dev, env->comp_irq, envelope_detector_comp_isr,
  306. 0, "envelope-detector", env);
  307. if (ret) {
  308. if (ret != -EPROBE_DEFER)
  309. dev_err(dev, "failed to request interrupt\n");
  310. return ret;
  311. }
  312. env->comp_irq_trigger = irq_get_trigger_type(env->comp_irq);
  313. if (env->comp_irq_trigger & IRQF_TRIGGER_RISING)
  314. env->comp_irq_trigger_inv |= IRQF_TRIGGER_FALLING;
  315. if (env->comp_irq_trigger & IRQF_TRIGGER_FALLING)
  316. env->comp_irq_trigger_inv |= IRQF_TRIGGER_RISING;
  317. if (env->comp_irq_trigger & IRQF_TRIGGER_HIGH)
  318. env->comp_irq_trigger_inv |= IRQF_TRIGGER_LOW;
  319. if (env->comp_irq_trigger & IRQF_TRIGGER_LOW)
  320. env->comp_irq_trigger_inv |= IRQF_TRIGGER_HIGH;
  321. ret = iio_get_channel_type(env->dac, &type);
  322. if (ret < 0)
  323. return ret;
  324. if (type != IIO_VOLTAGE) {
  325. dev_err(dev, "dac is of the wrong type\n");
  326. return -EINVAL;
  327. }
  328. ret = iio_read_max_channel_raw(env->dac, &env->dac_max);
  329. if (ret < 0) {
  330. dev_err(dev, "dac does not indicate its raw maximum value\n");
  331. return ret;
  332. }
  333. return devm_iio_device_register(dev, indio_dev);
  334. }
  335. static const struct of_device_id envelope_detector_match[] = {
  336. { .compatible = "axentia,tse850-envelope-detector", },
  337. { /* sentinel */ }
  338. };
  339. MODULE_DEVICE_TABLE(of, envelope_detector_match);
  340. static struct platform_driver envelope_detector_driver = {
  341. .probe = envelope_detector_probe,
  342. .driver = {
  343. .name = "iio-envelope-detector",
  344. .of_match_table = envelope_detector_match,
  345. },
  346. };
  347. module_platform_driver(envelope_detector_driver);
  348. MODULE_DESCRIPTION("Envelope detector using a DAC and a comparator");
  349. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  350. MODULE_LICENSE("GPL v2");