pcf50633-adc.c 5.9 KB

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