blob: 6d2e8466df1dd804d2943144f9a8564309f76f96 [file] [log] [blame]
Balaji Rao08c3e062009-01-09 01:49:26 +01001/* 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
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>
25
26#include <linux/mfd/pcf50633/core.h>
27#include <linux/mfd/pcf50633/adc.h>
28
29struct pcf50633_adc_request {
30 int mux;
31 int avg;
32 int result;
33 void (*callback)(struct pcf50633 *, void *, int);
34 void *callback_param;
35
36 /* Used in case of sync requests */
37 struct completion completion;
38
39};
40
41#define PCF50633_MAX_ADC_FIFO_DEPTH 8
42
43struct pcf50633_adc {
44 struct pcf50633 *pcf;
45
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;
51};
52
53static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf)
54{
55 return platform_get_drvdata(pcf->adc_pdev);
56}
57
58static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
59{
60 channel &= PCF50633_ADCC1_ADCMUX_MASK;
61
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);
65
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);
69}
70
71static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
72{
73 struct pcf50633_adc *adc = __to_adc(pcf);
74 int head;
75
Balaji Rao08c3e062009-01-09 01:49:26 +010076 head = adc->queue_head;
77
Paul Fertserbd8ef102009-07-28 00:58:48 +040078 if (!adc->queue[head])
Balaji Rao08c3e062009-01-09 01:49:26 +010079 return;
Balaji Rao08c3e062009-01-09 01:49:26 +010080
81 adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg);
82}
83
84static int
85adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
86{
87 struct pcf50633_adc *adc = __to_adc(pcf);
88 int head, tail;
89
90 mutex_lock(&adc->queue_mutex);
91
92 head = adc->queue_head;
93 tail = adc->queue_tail;
94
95 if (adc->queue[tail]) {
96 mutex_unlock(&adc->queue_mutex);
Paul Fertserbd8ef102009-07-28 00:58:48 +040097 dev_err(pcf->dev, "ADC queue is full, dropping request\n");
Balaji Rao08c3e062009-01-09 01:49:26 +010098 return -EBUSY;
99 }
100
101 adc->queue[tail] = req;
Paul Fertserbd8ef102009-07-28 00:58:48 +0400102 if (head == tail)
103 trigger_next_adc_job_if_any(pcf);
Balaji Rao08c3e062009-01-09 01:49:26 +0100104 adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
105
106 mutex_unlock(&adc->queue_mutex);
107
Balaji Rao08c3e062009-01-09 01:49:26 +0100108 return 0;
109}
110
111static void
112pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param, int result)
113{
114 struct pcf50633_adc_request *req = param;
115
116 req->result = result;
117 complete(&req->completion);
118}
119
120int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg)
121{
122 struct pcf50633_adc_request *req;
Paul Fertserbd8ef102009-07-28 00:58:48 +0400123 int err;
Balaji Rao08c3e062009-01-09 01:49:26 +0100124
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;
129
130 req->mux = mux;
131 req->avg = avg;
132 req->callback = pcf50633_adc_sync_read_callback;
133 req->callback_param = req;
134
135 init_completion(&req->completion);
Paul Fertserbd8ef102009-07-28 00:58:48 +0400136 err = adc_enqueue_request(pcf, req);
137 if (err)
138 return err;
139
Balaji Rao08c3e062009-01-09 01:49:26 +0100140 wait_for_completion(&req->completion);
141
Paul Fertserbd8ef102009-07-28 00:58:48 +0400142 /* FIXME by this time req might be already freed */
Balaji Rao08c3e062009-01-09 01:49:26 +0100143 return req->result;
144}
145EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
146
147int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
148 void (*callback)(struct pcf50633 *, void *, int),
149 void *callback_param)
150{
151 struct pcf50633_adc_request *req;
152
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;
157
158 req->mux = mux;
159 req->avg = avg;
160 req->callback = callback;
161 req->callback_param = callback_param;
162
Paul Fertserbd8ef102009-07-28 00:58:48 +0400163 return adc_enqueue_request(pcf, req);
Balaji Rao08c3e062009-01-09 01:49:26 +0100164}
165EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
166
167static int adc_result(struct pcf50633 *pcf)
168{
169 u8 adcs1, adcs3;
170 u16 result;
171
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);
175
176 dev_dbg(pcf->dev, "adc result = %d\n", result);
177
178 return result;
179}
180
181static void pcf50633_adc_irq(int irq, void *data)
182{
183 struct pcf50633_adc *adc = data;
184 struct pcf50633 *pcf = adc->pcf;
185 struct pcf50633_adc_request *req;
Paul Fertserbd8ef102009-07-28 00:58:48 +0400186 int head, res;
Balaji Rao08c3e062009-01-09 01:49:26 +0100187
188 mutex_lock(&adc->queue_mutex);
189 head = adc->queue_head;
190
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;
196 }
197 adc->queue[head] = NULL;
198 adc->queue_head = (head + 1) &
199 (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
200
Paul Fertserbd8ef102009-07-28 00:58:48 +0400201 res = adc_result(pcf);
202 trigger_next_adc_job_if_any(pcf);
203
Balaji Rao08c3e062009-01-09 01:49:26 +0100204 mutex_unlock(&adc->queue_mutex);
205
Paul Fertserbd8ef102009-07-28 00:58:48 +0400206 req->callback(pcf, req->callback_param, res);
Balaji Rao08c3e062009-01-09 01:49:26 +0100207 kfree(req);
Balaji Rao08c3e062009-01-09 01:49:26 +0100208}
209
210static int __devinit pcf50633_adc_probe(struct platform_device *pdev)
211{
Balaji Rao08c3e062009-01-09 01:49:26 +0100212 struct pcf50633_adc *adc;
213
214 adc = kzalloc(sizeof(*adc), GFP_KERNEL);
215 if (!adc)
216 return -ENOMEM;
217
Lars-Peter Clausen68d641e2009-10-14 02:12:33 +0400218 adc->pcf = dev_to_pcf50633(pdev->dev.parent);
Balaji Rao08c3e062009-01-09 01:49:26 +0100219 platform_set_drvdata(pdev, adc);
220
Lars-Peter Clausen68d641e2009-10-14 02:12:33 +0400221 pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY,
Balaji Rao08c3e062009-01-09 01:49:26 +0100222 pcf50633_adc_irq, adc);
223
224 mutex_init(&adc->queue_mutex);
225
226 return 0;
227}
228
229static int __devexit pcf50633_adc_remove(struct platform_device *pdev)
230{
231 struct pcf50633_adc *adc = platform_get_drvdata(pdev);
232 int i, head;
233
234 pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
235
236 mutex_lock(&adc->queue_mutex);
237 head = adc->queue_head;
238
239 if (WARN_ON(adc->queue[head]))
240 dev_err(adc->pcf->dev,
241 "adc driver removed with request pending\n");
242
243 for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
244 kfree(adc->queue[i]);
245
246 mutex_unlock(&adc->queue_mutex);
247 kfree(adc);
248
249 return 0;
250}
251
252static struct platform_driver pcf50633_adc_driver = {
253 .driver = {
254 .name = "pcf50633-adc",
255 },
256 .probe = pcf50633_adc_probe,
257 .remove = __devexit_p(pcf50633_adc_remove),
258};
259
260static int __init pcf50633_adc_init(void)
261{
262 return platform_driver_register(&pcf50633_adc_driver);
263}
264module_init(pcf50633_adc_init);
265
266static void __exit pcf50633_adc_exit(void)
267{
268 platform_driver_unregister(&pcf50633_adc_driver);
269}
270module_exit(pcf50633_adc_exit);
271
272MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
273MODULE_DESCRIPTION("PCF50633 adc driver");
274MODULE_LICENSE("GPL");
275MODULE_ALIAS("platform:pcf50633-adc");
276