Merge commit 'v2.6.32.11' into mini2440-stable-v2.6.32
[linux-2.6/mini2440.git] / drivers / mfd / pcf50633-adc.c
blob3d31e97d6a4553ab18f1752deec8ef5a76ce12de
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/module.h>
21 #include <linux/init.h>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
24 #include <linux/completion.h>
26 #include <linux/mfd/pcf50633/core.h>
27 #include <linux/mfd/pcf50633/adc.h>
29 struct pcf50633_adc_request {
30 int mux;
31 int avg;
32 int result;
33 void (*callback)(struct pcf50633 *, void *, int);
34 void *callback_param;
36 /* Used in case of sync requests */
37 struct completion completion;
41 #define PCF50633_MAX_ADC_FIFO_DEPTH 8
43 struct pcf50633_adc {
44 struct pcf50633 *pcf;
46 /* Private stuff */
47 struct pcf50633_adc_request *queue[PCF50633_MAX_ADC_FIFO_DEPTH];
48 int queue_head;
49 int queue_tail;
50 struct mutex queue_mutex;
53 static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf)
55 return platform_get_drvdata(pcf->adc_pdev);
58 static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
60 channel &= PCF50633_ADCC1_ADCMUX_MASK;
62 /* kill ratiometric, but enable ACCSW biasing */
63 pcf50633_reg_write(pcf, PCF50633_REG_ADCC2, 0x00);
64 pcf50633_reg_write(pcf, PCF50633_REG_ADCC3, 0x01);
66 /* start ADC conversion on selected channel */
67 pcf50633_reg_write(pcf, PCF50633_REG_ADCC1, channel | avg |
68 PCF50633_ADCC1_ADCSTART | PCF50633_ADCC1_RES_10BIT);
71 static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
73 struct pcf50633_adc *adc = __to_adc(pcf);
74 int head;
76 head = adc->queue_head;
78 if (!adc->queue[head])
79 return;
81 adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg);
84 static int
85 adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
87 struct pcf50633_adc *adc = __to_adc(pcf);
88 int head, tail;
90 mutex_lock(&adc->queue_mutex);
92 head = adc->queue_head;
93 tail = adc->queue_tail;
95 if (adc->queue[tail]) {
96 mutex_unlock(&adc->queue_mutex);
97 dev_err(pcf->dev, "ADC queue is full, dropping request\n");
98 return -EBUSY;
101 adc->queue[tail] = req;
102 if (head == tail)
103 trigger_next_adc_job_if_any(pcf);
104 adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
106 mutex_unlock(&adc->queue_mutex);
108 return 0;
111 static void
112 pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param, int result)
114 struct pcf50633_adc_request *req = param;
116 req->result = result;
117 complete(&req->completion);
120 int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg)
122 struct pcf50633_adc_request *req;
123 int err;
125 /* req is freed when the result is ready, in interrupt handler */
126 req = kzalloc(sizeof(*req), GFP_KERNEL);
127 if (!req)
128 return -ENOMEM;
130 req->mux = mux;
131 req->avg = avg;
132 req->callback = pcf50633_adc_sync_read_callback;
133 req->callback_param = req;
135 init_completion(&req->completion);
136 err = adc_enqueue_request(pcf, req);
137 if (err)
138 return err;
140 wait_for_completion(&req->completion);
142 /* FIXME by this time req might be already freed */
143 return req->result;
145 EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
147 int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
148 void (*callback)(struct pcf50633 *, void *, int),
149 void *callback_param)
151 struct pcf50633_adc_request *req;
153 /* req is freed when the result is ready, in interrupt handler */
154 req = kmalloc(sizeof(*req), GFP_KERNEL);
155 if (!req)
156 return -ENOMEM;
158 req->mux = mux;
159 req->avg = avg;
160 req->callback = callback;
161 req->callback_param = callback_param;
163 return adc_enqueue_request(pcf, req);
165 EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
167 static int adc_result(struct pcf50633 *pcf)
169 u8 adcs1, adcs3;
170 u16 result;
172 adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1);
173 adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3);
174 result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK);
176 dev_dbg(pcf->dev, "adc result = %d\n", result);
178 return result;
181 static void pcf50633_adc_irq(int irq, void *data)
183 struct pcf50633_adc *adc = data;
184 struct pcf50633 *pcf = adc->pcf;
185 struct pcf50633_adc_request *req;
186 int head, res;
188 mutex_lock(&adc->queue_mutex);
189 head = adc->queue_head;
191 req = adc->queue[head];
192 if (WARN_ON(!req)) {
193 dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n");
194 mutex_unlock(&adc->queue_mutex);
195 return;
197 adc->queue[head] = NULL;
198 adc->queue_head = (head + 1) &
199 (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
201 res = adc_result(pcf);
202 trigger_next_adc_job_if_any(pcf);
204 mutex_unlock(&adc->queue_mutex);
206 req->callback(pcf, req->callback_param, res);
207 kfree(req);
210 static int __devinit pcf50633_adc_probe(struct platform_device *pdev)
212 struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
213 struct pcf50633_adc *adc;
215 adc = kzalloc(sizeof(*adc), GFP_KERNEL);
216 if (!adc)
217 return -ENOMEM;
219 adc->pcf = pdata->pcf;
220 platform_set_drvdata(pdev, adc);
222 pcf50633_register_irq(pdata->pcf, PCF50633_IRQ_ADCRDY,
223 pcf50633_adc_irq, adc);
225 mutex_init(&adc->queue_mutex);
227 return 0;
230 static int __devexit pcf50633_adc_remove(struct platform_device *pdev)
232 struct pcf50633_adc *adc = platform_get_drvdata(pdev);
233 int i, head;
235 pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
237 mutex_lock(&adc->queue_mutex);
238 head = adc->queue_head;
240 if (WARN_ON(adc->queue[head]))
241 dev_err(adc->pcf->dev,
242 "adc driver removed with request pending\n");
244 for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
245 kfree(adc->queue[i]);
247 mutex_unlock(&adc->queue_mutex);
248 kfree(adc);
250 return 0;
253 static struct platform_driver pcf50633_adc_driver = {
254 .driver = {
255 .name = "pcf50633-adc",
257 .probe = pcf50633_adc_probe,
258 .remove = __devexit_p(pcf50633_adc_remove),
261 static int __init pcf50633_adc_init(void)
263 return platform_driver_register(&pcf50633_adc_driver);
265 module_init(pcf50633_adc_init);
267 static void __exit pcf50633_adc_exit(void)
269 platform_driver_unregister(&pcf50633_adc_driver);
271 module_exit(pcf50633_adc_exit);
273 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
274 MODULE_DESCRIPTION("PCF50633 adc driver");
275 MODULE_LICENSE("GPL");
276 MODULE_ALIAS("platform:pcf50633-adc");