pcf50633-adc.c 6.4 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/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/completion.h>
  25. #include <linux/mfd/pcf50633/core.h>
  26. #include <linux/mfd/pcf50633/adc.h>
  27. struct pcf50633_adc_request {
  28. int mux;
  29. int avg;
  30. int result;
  31. void (*callback)(struct pcf50633 *, void *, int);
  32. void *callback_param;
  33. /* Used in case of sync requests */
  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
  89. pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param, int result)
  90. {
  91. struct pcf50633_adc_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_request *req;
  98. int err;
  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. err = adc_enqueue_request(pcf, req);
  109. if (err)
  110. return err;
  111. wait_for_completion(&req->completion);
  112. /* FIXME by this time req might be already freed */
  113. return req->result;
  114. }
  115. EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
  116. int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
  117. void (*callback)(struct pcf50633 *, void *, int),
  118. void *callback_param)
  119. {
  120. struct pcf50633_adc_request *req;
  121. /* req is freed when the result is ready, in interrupt handler */
  122. req = kmalloc(sizeof(*req), GFP_KERNEL);
  123. if (!req)
  124. return -ENOMEM;
  125. req->mux = mux;
  126. req->avg = avg;
  127. req->callback = callback;
  128. req->callback_param = callback_param;
  129. return adc_enqueue_request(pcf, req);
  130. }
  131. EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
  132. static int adc_result(struct pcf50633 *pcf)
  133. {
  134. u8 adcs1, adcs3;
  135. u16 result;
  136. adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1);
  137. adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3);
  138. result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK);
  139. dev_dbg(pcf->dev, "adc result = %d\n", result);
  140. return result;
  141. }
  142. static void pcf50633_adc_irq(int irq, void *data)
  143. {
  144. struct pcf50633_adc *adc = data;
  145. struct pcf50633 *pcf = adc->pcf;
  146. struct pcf50633_adc_request *req;
  147. int head, res;
  148. mutex_lock(&adc->queue_mutex);
  149. head = adc->queue_head;
  150. req = adc->queue[head];
  151. if (WARN_ON(!req)) {
  152. dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n");
  153. mutex_unlock(&adc->queue_mutex);
  154. return;
  155. }
  156. adc->queue[head] = NULL;
  157. adc->queue_head = (head + 1) &
  158. (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
  159. res = adc_result(pcf);
  160. trigger_next_adc_job_if_any(pcf);
  161. mutex_unlock(&adc->queue_mutex);
  162. req->callback(pcf, req->callback_param, res);
  163. kfree(req);
  164. }
  165. static int __devinit pcf50633_adc_probe(struct platform_device *pdev)
  166. {
  167. struct pcf50633_adc *adc;
  168. adc = kzalloc(sizeof(*adc), GFP_KERNEL);
  169. if (!adc)
  170. return -ENOMEM;
  171. adc->pcf = dev_to_pcf50633(pdev->dev.parent);
  172. platform_set_drvdata(pdev, adc);
  173. pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY,
  174. pcf50633_adc_irq, adc);
  175. mutex_init(&adc->queue_mutex);
  176. return 0;
  177. }
  178. static int __devexit pcf50633_adc_remove(struct platform_device *pdev)
  179. {
  180. struct pcf50633_adc *adc = platform_get_drvdata(pdev);
  181. int i, head;
  182. pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
  183. mutex_lock(&adc->queue_mutex);
  184. head = adc->queue_head;
  185. if (WARN_ON(adc->queue[head]))
  186. dev_err(adc->pcf->dev,
  187. "adc driver removed with request pending\n");
  188. for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
  189. kfree(adc->queue[i]);
  190. mutex_unlock(&adc->queue_mutex);
  191. kfree(adc);
  192. return 0;
  193. }
  194. static struct platform_driver pcf50633_adc_driver = {
  195. .driver = {
  196. .name = "pcf50633-adc",
  197. },
  198. .probe = pcf50633_adc_probe,
  199. .remove = __devexit_p(pcf50633_adc_remove),
  200. };
  201. static int __init pcf50633_adc_init(void)
  202. {
  203. return platform_driver_register(&pcf50633_adc_driver);
  204. }
  205. module_init(pcf50633_adc_init);
  206. static void __exit pcf50633_adc_exit(void)
  207. {
  208. platform_driver_unregister(&pcf50633_adc_driver);
  209. }
  210. module_exit(pcf50633_adc_exit);
  211. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  212. MODULE_DESCRIPTION("PCF50633 adc driver");
  213. MODULE_LICENSE("GPL");
  214. MODULE_ALIAS("platform:pcf50633-adc");