pcf50633-adc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* NXP PCF50633 ADC Driver
  2. *
  3. * (C) 2006-2008 by Openmoko, Inc.
  4. * Author: Balaji Rao <balajirrao@openmoko.org>
  5. * All rights reserved.
  6. *
  7. * Broken down from monstrous PCF50633 driver mainly by
  8. * Harald Welte, Andy Green and Werner Almesberger
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * NOTE: This driver does not yet support subtractive ADC mode, which means
  16. * you can do only one measurement per read request.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/completion.h>
  24. #include <linux/mfd/pcf50633/core.h>
  25. #include <linux/mfd/pcf50633/adc.h>
  26. struct pcf50633_adc_request {
  27. int mux;
  28. int avg;
  29. void (*callback)(struct pcf50633 *, void *, int);
  30. void *callback_param;
  31. };
  32. struct pcf50633_adc_sync_request {
  33. int result;
  34. struct completion completion;
  35. };
  36. #define PCF50633_MAX_ADC_FIFO_DEPTH 8
  37. struct pcf50633_adc {
  38. struct pcf50633 *pcf;
  39. /* Private stuff */
  40. struct pcf50633_adc_request *queue[PCF50633_MAX_ADC_FIFO_DEPTH];
  41. int queue_head;
  42. int queue_tail;
  43. struct mutex queue_mutex;
  44. };
  45. static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf)
  46. {
  47. return platform_get_drvdata(pcf->adc_pdev);
  48. }
  49. static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
  50. {
  51. channel &= PCF50633_ADCC1_ADCMUX_MASK;
  52. /* kill ratiometric, but enable ACCSW biasing */
  53. pcf50633_reg_write(pcf, PCF50633_REG_ADCC2, 0x00);
  54. pcf50633_reg_write(pcf, PCF50633_REG_ADCC3, 0x01);
  55. /* start ADC conversion on selected channel */
  56. pcf50633_reg_write(pcf, PCF50633_REG_ADCC1, channel | avg |
  57. PCF50633_ADCC1_ADCSTART | PCF50633_ADCC1_RES_10BIT);
  58. }
  59. static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
  60. {
  61. struct pcf50633_adc *adc = __to_adc(pcf);
  62. int head;
  63. head = adc->queue_head;
  64. if (!adc->queue[head])
  65. return;
  66. adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg);
  67. }
  68. static int
  69. adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
  70. {
  71. struct pcf50633_adc *adc = __to_adc(pcf);
  72. int head, tail;
  73. mutex_lock(&adc->queue_mutex);
  74. head = adc->queue_head;
  75. tail = adc->queue_tail;
  76. if (adc->queue[tail]) {
  77. mutex_unlock(&adc->queue_mutex);
  78. dev_err(pcf->dev, "ADC queue is full, dropping request\n");
  79. return -EBUSY;
  80. }
  81. adc->queue[tail] = req;
  82. if (head == tail)
  83. trigger_next_adc_job_if_any(pcf);
  84. adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
  85. mutex_unlock(&adc->queue_mutex);
  86. return 0;
  87. }
  88. static void pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param,
  89. int result)
  90. {
  91. struct pcf50633_adc_sync_request *req = param;
  92. req->result = result;
  93. complete(&req->completion);
  94. }
  95. int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg)
  96. {
  97. struct pcf50633_adc_sync_request req;
  98. int ret;
  99. init_completion(&req.completion);
  100. ret = pcf50633_adc_async_read(pcf, mux, avg,
  101. pcf50633_adc_sync_read_callback, &req);
  102. if (ret)
  103. return ret;
  104. wait_for_completion(&req.completion);
  105. return req.result;
  106. }
  107. EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
  108. int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
  109. void (*callback)(struct pcf50633 *, void *, int),
  110. void *callback_param)
  111. {
  112. struct pcf50633_adc_request *req;
  113. /* req is freed when the result is ready, in interrupt handler */
  114. req = kmalloc(sizeof(*req), GFP_KERNEL);
  115. if (!req)
  116. return -ENOMEM;
  117. req->mux = mux;
  118. req->avg = avg;
  119. req->callback = callback;
  120. req->callback_param = callback_param;
  121. return adc_enqueue_request(pcf, req);
  122. }
  123. EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
  124. static int adc_result(struct pcf50633 *pcf)
  125. {
  126. u8 adcs1, adcs3;
  127. u16 result;
  128. adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1);
  129. adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3);
  130. result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK);
  131. dev_dbg(pcf->dev, "adc result = %d\n", result);
  132. return result;
  133. }
  134. static void pcf50633_adc_irq(int irq, void *data)
  135. {
  136. struct pcf50633_adc *adc = data;
  137. struct pcf50633 *pcf = adc->pcf;
  138. struct pcf50633_adc_request *req;
  139. int head, res;
  140. mutex_lock(&adc->queue_mutex);
  141. head = adc->queue_head;
  142. req = adc->queue[head];
  143. if (WARN_ON(!req)) {
  144. dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n");
  145. mutex_unlock(&adc->queue_mutex);
  146. return;
  147. }
  148. adc->queue[head] = NULL;
  149. adc->queue_head = (head + 1) &
  150. (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
  151. res = adc_result(pcf);
  152. trigger_next_adc_job_if_any(pcf);
  153. mutex_unlock(&adc->queue_mutex);
  154. req->callback(pcf, req->callback_param, res);
  155. kfree(req);
  156. }
  157. static int pcf50633_adc_probe(struct platform_device *pdev)
  158. {
  159. struct pcf50633_adc *adc;
  160. adc = devm_kzalloc(&pdev->dev, sizeof(*adc), GFP_KERNEL);
  161. if (!adc)
  162. return -ENOMEM;
  163. adc->pcf = dev_to_pcf50633(pdev->dev.parent);
  164. platform_set_drvdata(pdev, adc);
  165. pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY,
  166. pcf50633_adc_irq, adc);
  167. mutex_init(&adc->queue_mutex);
  168. return 0;
  169. }
  170. static int pcf50633_adc_remove(struct platform_device *pdev)
  171. {
  172. struct pcf50633_adc *adc = platform_get_drvdata(pdev);
  173. int i, head;
  174. pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
  175. mutex_lock(&adc->queue_mutex);
  176. head = adc->queue_head;
  177. if (WARN_ON(adc->queue[head]))
  178. dev_err(adc->pcf->dev,
  179. "adc driver removed with request pending\n");
  180. for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
  181. kfree(adc->queue[i]);
  182. mutex_unlock(&adc->queue_mutex);
  183. return 0;
  184. }
  185. static struct platform_driver pcf50633_adc_driver = {
  186. .driver = {
  187. .name = "pcf50633-adc",
  188. },
  189. .probe = pcf50633_adc_probe,
  190. .remove = pcf50633_adc_remove,
  191. };
  192. module_platform_driver(pcf50633_adc_driver);
  193. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  194. MODULE_DESCRIPTION("PCF50633 adc driver");
  195. MODULE_LICENSE("GPL");
  196. MODULE_ALIAS("platform:pcf50633-adc");