[media] DocBook/frontend.xml: Recomend the usage of the new API
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mfd / pcf50633-adc.c
blobaed0d2a9b032e0b3e5babe7949037df19470e1c7
1 /* NXP PCF50633 ADC Driver
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Balaji Rao <balajirrao@openmoko.org>
5 * All rights reserved.
7 * Broken down from monstrous PCF50633 driver mainly by
8 * Harald Welte, Andy Green and Werner Almesberger
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.
15 * NOTE: This driver does not yet support subtractive ADC mode, which means
16 * you can do only one measurement per read request.
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
25 #include <linux/completion.h>
27 #include <linux/mfd/pcf50633/core.h>
28 #include <linux/mfd/pcf50633/adc.h>
30 struct pcf50633_adc_request {
31 int mux;
32 int avg;
33 void (*callback)(struct pcf50633 *, void *, int);
34 void *callback_param;
37 struct pcf50633_adc_sync_request {
38 int result;
39 struct completion completion;
42 #define PCF50633_MAX_ADC_FIFO_DEPTH 8
44 struct pcf50633_adc {
45 struct pcf50633 *pcf;
47 /* Private stuff */
48 struct pcf50633_adc_request *queue[PCF50633_MAX_ADC_FIFO_DEPTH];
49 int queue_head;
50 int queue_tail;
51 struct mutex queue_mutex;
54 static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf)
56 return platform_get_drvdata(pcf->adc_pdev);
59 static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
61 channel &= PCF50633_ADCC1_ADCMUX_MASK;
63 /* kill ratiometric, but enable ACCSW biasing */
64 pcf50633_reg_write(pcf, PCF50633_REG_ADCC2, 0x00);
65 pcf50633_reg_write(pcf, PCF50633_REG_ADCC3, 0x01);
67 /* start ADC conversion on selected channel */
68 pcf50633_reg_write(pcf, PCF50633_REG_ADCC1, channel | avg |
69 PCF50633_ADCC1_ADCSTART | PCF50633_ADCC1_RES_10BIT);
72 static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
74 struct pcf50633_adc *adc = __to_adc(pcf);
75 int head;
77 head = adc->queue_head;
79 if (!adc->queue[head])
80 return;
82 adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg);
85 static int
86 adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
88 struct pcf50633_adc *adc = __to_adc(pcf);
89 int head, tail;
91 mutex_lock(&adc->queue_mutex);
93 head = adc->queue_head;
94 tail = adc->queue_tail;
96 if (adc->queue[tail]) {
97 mutex_unlock(&adc->queue_mutex);
98 dev_err(pcf->dev, "ADC queue is full, dropping request\n");
99 return -EBUSY;
102 adc->queue[tail] = req;
103 if (head == tail)
104 trigger_next_adc_job_if_any(pcf);
105 adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
107 mutex_unlock(&adc->queue_mutex);
109 return 0;
112 static void pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param,
113 int result)
115 struct pcf50633_adc_sync_request *req = param;
117 req->result = result;
118 complete(&req->completion);
121 int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg)
123 struct pcf50633_adc_sync_request req;
124 int ret;
126 init_completion(&req.completion);
128 ret = pcf50633_adc_async_read(pcf, mux, avg,
129 pcf50633_adc_sync_read_callback, &req);
130 if (ret)
131 return ret;
133 wait_for_completion(&req.completion);
135 return req.result;
137 EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
139 int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
140 void (*callback)(struct pcf50633 *, void *, int),
141 void *callback_param)
143 struct pcf50633_adc_request *req;
145 /* req is freed when the result is ready, in interrupt handler */
146 req = kmalloc(sizeof(*req), GFP_KERNEL);
147 if (!req)
148 return -ENOMEM;
150 req->mux = mux;
151 req->avg = avg;
152 req->callback = callback;
153 req->callback_param = callback_param;
155 return adc_enqueue_request(pcf, req);
157 EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
159 static int adc_result(struct pcf50633 *pcf)
161 u8 adcs1, adcs3;
162 u16 result;
164 adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1);
165 adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3);
166 result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK);
168 dev_dbg(pcf->dev, "adc result = %d\n", result);
170 return result;
173 static void pcf50633_adc_irq(int irq, void *data)
175 struct pcf50633_adc *adc = data;
176 struct pcf50633 *pcf = adc->pcf;
177 struct pcf50633_adc_request *req;
178 int head, res;
180 mutex_lock(&adc->queue_mutex);
181 head = adc->queue_head;
183 req = adc->queue[head];
184 if (WARN_ON(!req)) {
185 dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n");
186 mutex_unlock(&adc->queue_mutex);
187 return;
189 adc->queue[head] = NULL;
190 adc->queue_head = (head + 1) &
191 (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
193 res = adc_result(pcf);
194 trigger_next_adc_job_if_any(pcf);
196 mutex_unlock(&adc->queue_mutex);
198 req->callback(pcf, req->callback_param, res);
199 kfree(req);
202 static int __devinit pcf50633_adc_probe(struct platform_device *pdev)
204 struct pcf50633_adc *adc;
206 adc = kzalloc(sizeof(*adc), GFP_KERNEL);
207 if (!adc)
208 return -ENOMEM;
210 adc->pcf = dev_to_pcf50633(pdev->dev.parent);
211 platform_set_drvdata(pdev, adc);
213 pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY,
214 pcf50633_adc_irq, adc);
216 mutex_init(&adc->queue_mutex);
218 return 0;
221 static int __devexit pcf50633_adc_remove(struct platform_device *pdev)
223 struct pcf50633_adc *adc = platform_get_drvdata(pdev);
224 int i, head;
226 pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
228 mutex_lock(&adc->queue_mutex);
229 head = adc->queue_head;
231 if (WARN_ON(adc->queue[head]))
232 dev_err(adc->pcf->dev,
233 "adc driver removed with request pending\n");
235 for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
236 kfree(adc->queue[i]);
238 mutex_unlock(&adc->queue_mutex);
239 kfree(adc);
241 return 0;
244 static struct platform_driver pcf50633_adc_driver = {
245 .driver = {
246 .name = "pcf50633-adc",
248 .probe = pcf50633_adc_probe,
249 .remove = __devexit_p(pcf50633_adc_remove),
252 static int __init pcf50633_adc_init(void)
254 return platform_driver_register(&pcf50633_adc_driver);
256 module_init(pcf50633_adc_init);
258 static void __exit pcf50633_adc_exit(void)
260 platform_driver_unregister(&pcf50633_adc_driver);
262 module_exit(pcf50633_adc_exit);
264 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
265 MODULE_DESCRIPTION("PCF50633 adc driver");
266 MODULE_LICENSE("GPL");
267 MODULE_ALIAS("platform:pcf50633-adc");