pcf50633-adc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/module.h>
  20. #include <linux/init.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. int result;
  30. void (*callback)(struct pcf50633 *, void *, int);
  31. void *callback_param;
  32. /* Used in case of sync requests */
  33. struct completion completion;
  34. };
  35. #define PCF50633_MAX_ADC_FIFO_DEPTH 8
  36. struct pcf50633_adc {
  37. struct pcf50633 *pcf;
  38. /* Private stuff */
  39. struct pcf50633_adc_request *queue[PCF50633_MAX_ADC_FIFO_DEPTH];
  40. int queue_head;
  41. int queue_tail;
  42. struct mutex queue_mutex;
  43. };
  44. static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf)
  45. {
  46. return platform_get_drvdata(pcf->adc_pdev);
  47. }
  48. static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
  49. {
  50. channel &= PCF50633_ADCC1_ADCMUX_MASK;
  51. /* kill ratiometric, but enable ACCSW biasing */
  52. pcf50633_reg_write(pcf, PCF50633_REG_ADCC2, 0x00);
  53. pcf50633_reg_write(pcf, PCF50633_REG_ADCC3, 0x01);
  54. /* start ADC conversion on selected channel */
  55. pcf50633_reg_write(pcf, PCF50633_REG_ADCC1, channel | avg |
  56. PCF50633_ADCC1_ADCSTART | PCF50633_ADCC1_RES_10BIT);
  57. }
  58. static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
  59. {
  60. struct pcf50633_adc *adc = __to_adc(pcf);
  61. int head;
  62. head = adc->queue_head;
  63. if (!adc->queue[head])
  64. return;
  65. adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg);
  66. }
  67. static int
  68. adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
  69. {
  70. struct pcf50633_adc *adc = __to_adc(pcf);
  71. int head, tail;
  72. mutex_lock(&adc->queue_mutex);
  73. head = adc->queue_head;
  74. tail = adc->queue_tail;
  75. if (adc->queue[tail]) {
  76. mutex_unlock(&adc->queue_mutex);
  77. dev_err(pcf->dev, "ADC queue is full, dropping request\n");
  78. return -EBUSY;
  79. }
  80. adc->queue[tail] = req;
  81. if (head == tail)
  82. trigger_next_adc_job_if_any(pcf);
  83. adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
  84. mutex_unlock(&adc->queue_mutex);
  85. return 0;
  86. }
  87. static void
  88. pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param, int result)
  89. {
  90. struct pcf50633_adc_request *req = param;
  91. req->result = result;
  92. complete(&req->completion);
  93. }
  94. int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg)
  95. {
  96. struct pcf50633_adc_request *req;
  97. int err;
  98. /* req is freed when the result is ready, in interrupt handler */
  99. req = kzalloc(sizeof(*req), GFP_KERNEL);
  100. if (!req)
  101. return -ENOMEM;
  102. req->mux = mux;
  103. req->avg = avg;
  104. req->callback = pcf50633_adc_sync_read_callback;
  105. req->callback_param = req;
  106. init_completion(&req->completion);
  107. err = adc_enqueue_request(pcf, req);
  108. if (err)
  109. return err;
  110. wait_for_completion(&req->completion);
  111. /* FIXME by this time req might be already freed */
  112. return req->result;
  113. }
  114. EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
  115. int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
  116. void (*callback)(struct pcf50633 *, void *, int),
  117. void *callback_param)
  118. {
  119. struct pcf50633_adc_request *req;
  120. /* req is freed when the result is ready, in interrupt handler */
  121. req = kmalloc(sizeof(*req), GFP_KERNEL);
  122. if (!req)
  123. return -ENOMEM;
  124. req->mux = mux;
  125. req->avg = avg;
  126. req->callback = callback;
  127. req->callback_param = callback_param;
  128. return adc_enqueue_request(pcf, req);
  129. }
  130. EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
  131. static int adc_result(struct pcf50633 *pcf)
  132. {
  133. u8 adcs1, adcs3;
  134. u16 result;
  135. adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1);
  136. adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3);
  137. result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK);
  138. dev_dbg(pcf->dev, "adc result = %d\n", result);
  139. return result;
  140. }
  141. static void pcf50633_adc_irq(int irq, void *data)
  142. {
  143. struct pcf50633_adc *adc = data;
  144. struct pcf50633 *pcf = adc->pcf;
  145. struct pcf50633_adc_request *req;
  146. int head, res;
  147. mutex_lock(&adc->queue_mutex);
  148. head = adc->queue_head;
  149. req = adc->queue[head];
  150. if (WARN_ON(!req)) {
  151. dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n");
  152. mutex_unlock(&adc->queue_mutex);
  153. return;
  154. }
  155. adc->queue[head] = NULL;
  156. adc->queue_head = (head + 1) &
  157. (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
  158. res = adc_result(pcf);
  159. trigger_next_adc_job_if_any(pcf);
  160. mutex_unlock(&adc->queue_mutex);
  161. req->callback(pcf, req->callback_param, res);
  162. kfree(req);
  163. }
  164. static int __devinit pcf50633_adc_probe(struct platform_device *pdev)
  165. {
  166. struct pcf50633_adc *adc;
  167. adc = kzalloc(sizeof(*adc), GFP_KERNEL);
  168. if (!adc)
  169. return -ENOMEM;
  170. adc->pcf = dev_to_pcf50633(pdev->dev.parent);
  171. platform_set_drvdata(pdev, adc);
  172. pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY,
  173. pcf50633_adc_irq, adc);
  174. mutex_init(&adc->queue_mutex);
  175. return 0;
  176. }
  177. static int __devexit pcf50633_adc_remove(struct platform_device *pdev)
  178. {
  179. struct pcf50633_adc *adc = platform_get_drvdata(pdev);
  180. int i, head;
  181. pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
  182. mutex_lock(&adc->queue_mutex);
  183. head = adc->queue_head;
  184. if (WARN_ON(adc->queue[head]))
  185. dev_err(adc->pcf->dev,
  186. "adc driver removed with request pending\n");
  187. for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
  188. kfree(adc->queue[i]);
  189. mutex_unlock(&adc->queue_mutex);
  190. kfree(adc);
  191. return 0;
  192. }
  193. static struct platform_driver pcf50633_adc_driver = {
  194. .driver = {
  195. .name = "pcf50633-adc",
  196. },
  197. .probe = pcf50633_adc_probe,
  198. .remove = __devexit_p(pcf50633_adc_remove),
  199. };
  200. static int __init pcf50633_adc_init(void)
  201. {
  202. return platform_driver_register(&pcf50633_adc_driver);
  203. }
  204. module_init(pcf50633_adc_init);
  205. static void __exit pcf50633_adc_exit(void)
  206. {
  207. platform_driver_unregister(&pcf50633_adc_driver);
  208. }
  209. module_exit(pcf50633_adc_exit);
  210. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  211. MODULE_DESCRIPTION("PCF50633 adc driver");
  212. MODULE_LICENSE("GPL");
  213. MODULE_ALIAS("platform:pcf50633-adc");