pcf50633-adc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. mutex_lock(&adc->queue_mutex);
  63. head = adc->queue_head;
  64. if (!adc->queue[head]) {
  65. mutex_unlock(&adc->queue_mutex);
  66. return;
  67. }
  68. mutex_unlock(&adc->queue_mutex);
  69. adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg);
  70. }
  71. static int
  72. adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
  73. {
  74. struct pcf50633_adc *adc = __to_adc(pcf);
  75. int head, tail;
  76. mutex_lock(&adc->queue_mutex);
  77. head = adc->queue_head;
  78. tail = adc->queue_tail;
  79. if (adc->queue[tail]) {
  80. mutex_unlock(&adc->queue_mutex);
  81. return -EBUSY;
  82. }
  83. adc->queue[tail] = req;
  84. adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
  85. mutex_unlock(&adc->queue_mutex);
  86. trigger_next_adc_job_if_any(pcf);
  87. return 0;
  88. }
  89. static void
  90. pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param, int result)
  91. {
  92. struct pcf50633_adc_request *req = param;
  93. req->result = result;
  94. complete(&req->completion);
  95. }
  96. int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg)
  97. {
  98. struct pcf50633_adc_request *req;
  99. /* req is freed when the result is ready, in interrupt handler */
  100. req = kzalloc(sizeof(*req), GFP_KERNEL);
  101. if (!req)
  102. return -ENOMEM;
  103. req->mux = mux;
  104. req->avg = avg;
  105. req->callback = pcf50633_adc_sync_read_callback;
  106. req->callback_param = req;
  107. init_completion(&req->completion);
  108. adc_enqueue_request(pcf, req);
  109. wait_for_completion(&req->completion);
  110. return req->result;
  111. }
  112. EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
  113. int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
  114. void (*callback)(struct pcf50633 *, void *, int),
  115. void *callback_param)
  116. {
  117. struct pcf50633_adc_request *req;
  118. /* req is freed when the result is ready, in interrupt handler */
  119. req = kmalloc(sizeof(*req), GFP_KERNEL);
  120. if (!req)
  121. return -ENOMEM;
  122. req->mux = mux;
  123. req->avg = avg;
  124. req->callback = callback;
  125. req->callback_param = callback_param;
  126. adc_enqueue_request(pcf, req);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
  130. static int adc_result(struct pcf50633 *pcf)
  131. {
  132. u8 adcs1, adcs3;
  133. u16 result;
  134. adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1);
  135. adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3);
  136. result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK);
  137. dev_dbg(pcf->dev, "adc result = %d\n", result);
  138. return result;
  139. }
  140. static void pcf50633_adc_irq(int irq, void *data)
  141. {
  142. struct pcf50633_adc *adc = data;
  143. struct pcf50633 *pcf = adc->pcf;
  144. struct pcf50633_adc_request *req;
  145. int head;
  146. mutex_lock(&adc->queue_mutex);
  147. head = adc->queue_head;
  148. req = adc->queue[head];
  149. if (WARN_ON(!req)) {
  150. dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n");
  151. mutex_unlock(&adc->queue_mutex);
  152. return;
  153. }
  154. adc->queue[head] = NULL;
  155. adc->queue_head = (head + 1) &
  156. (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
  157. mutex_unlock(&adc->queue_mutex);
  158. req->callback(pcf, req->callback_param, adc_result(pcf));
  159. kfree(req);
  160. trigger_next_adc_job_if_any(pcf);
  161. }
  162. static int __devinit pcf50633_adc_probe(struct platform_device *pdev)
  163. {
  164. struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
  165. struct pcf50633_adc *adc;
  166. adc = kzalloc(sizeof(*adc), GFP_KERNEL);
  167. if (!adc)
  168. return -ENOMEM;
  169. adc->pcf = pdata->pcf;
  170. platform_set_drvdata(pdev, adc);
  171. pcf50633_register_irq(pdata->pcf, PCF50633_IRQ_ADCRDY,
  172. pcf50633_adc_irq, adc);
  173. mutex_init(&adc->queue_mutex);
  174. return 0;
  175. }
  176. static int __devexit pcf50633_adc_remove(struct platform_device *pdev)
  177. {
  178. struct pcf50633_adc *adc = platform_get_drvdata(pdev);
  179. int i, head;
  180. pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
  181. mutex_lock(&adc->queue_mutex);
  182. head = adc->queue_head;
  183. if (WARN_ON(adc->queue[head]))
  184. dev_err(adc->pcf->dev,
  185. "adc driver removed with request pending\n");
  186. for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
  187. kfree(adc->queue[i]);
  188. mutex_unlock(&adc->queue_mutex);
  189. kfree(adc);
  190. return 0;
  191. }
  192. static struct platform_driver pcf50633_adc_driver = {
  193. .driver = {
  194. .name = "pcf50633-adc",
  195. },
  196. .probe = pcf50633_adc_probe,
  197. .remove = __devexit_p(pcf50633_adc_remove),
  198. };
  199. static int __init pcf50633_adc_init(void)
  200. {
  201. return platform_driver_register(&pcf50633_adc_driver);
  202. }
  203. module_init(pcf50633_adc_init);
  204. static void __exit pcf50633_adc_exit(void)
  205. {
  206. platform_driver_unregister(&pcf50633_adc_driver);
  207. }
  208. module_exit(pcf50633_adc_exit);
  209. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  210. MODULE_DESCRIPTION("PCF50633 adc driver");
  211. MODULE_LICENSE("GPL");
  212. MODULE_ALIAS("platform:pcf50633-adc");