blob: dbd2f0d0078e965c5d995d051cbb529c3a5ee2f0 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Balaji Rao08c3e062009-01-09 01:49:26 +010021#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>
26
27#include <linux/mfd/pcf50633/core.h>
28#include <linux/mfd/pcf50633/adc.h>
29
30struct pcf50633_adc_request {
31 int mux;
32 int avg;
Balaji Rao08c3e062009-01-09 01:49:26 +010033 void (*callback)(struct pcf50633 *, void *, int);
34 void *callback_param;
Lars-Peter Clausen6438a692010-05-12 02:10:54 +020035};
Balaji Rao08c3e062009-01-09 01:49:26 +010036
Lars-Peter Clausen6438a692010-05-12 02:10:54 +020037struct pcf50633_adc_sync_request {
38 int result;
Balaji Rao08c3e062009-01-09 01:49:26 +010039 struct completion completion;
Balaji Rao08c3e062009-01-09 01:49:26 +010040};
41
42#define PCF50633_MAX_ADC_FIFO_DEPTH 8
43
44struct pcf50633_adc {
45 struct pcf50633 *pcf;
46
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;
52};
53
54static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf)
55{
56 return platform_get_drvdata(pcf->adc_pdev);
57}
58
59static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
60{
61 channel &= PCF50633_ADCC1_ADCMUX_MASK;
62
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);
66
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);
70}
71
72static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
73{
74 struct pcf50633_adc *adc = __to_adc(pcf);
75 int head;
76
Balaji Rao08c3e062009-01-09 01:49:26 +010077 head = adc->queue_head;
78
Paul Fertserbd8ef102009-07-28 00:58:48 +040079 if (!adc->queue[head])
Balaji Rao08c3e062009-01-09 01:49:26 +010080 return;
Balaji Rao08c3e062009-01-09 01:49:26 +010081
82 adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg);
83}
84
85static int
86adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
87{
88 struct pcf50633_adc *adc = __to_adc(pcf);
89 int head, tail;
90
91 mutex_lock(&adc->queue_mutex);
92
93 head = adc->queue_head;
94 tail = adc->queue_tail;
95
96 if (adc->queue[tail]) {
97 mutex_unlock(&adc->queue_mutex);
Paul Fertserbd8ef102009-07-28 00:58:48 +040098 dev_err(pcf->dev, "ADC queue is full, dropping request\n");
Balaji Rao08c3e062009-01-09 01:49:26 +010099 return -EBUSY;
100 }
101
102 adc->queue[tail] = req;
Paul Fertserbd8ef102009-07-28 00:58:48 +0400103 if (head == tail)
104 trigger_next_adc_job_if_any(pcf);
Balaji Rao08c3e062009-01-09 01:49:26 +0100105 adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
106
107 mutex_unlock(&adc->queue_mutex);
108
Balaji Rao08c3e062009-01-09 01:49:26 +0100109 return 0;
110}
111
Lars-Peter Clausen6438a692010-05-12 02:10:54 +0200112static void pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param,
113 int result)
Balaji Rao08c3e062009-01-09 01:49:26 +0100114{
Lars-Peter Clausen6438a692010-05-12 02:10:54 +0200115 struct pcf50633_adc_sync_request *req = param;
Balaji Rao08c3e062009-01-09 01:49:26 +0100116
117 req->result = result;
118 complete(&req->completion);
119}
120
121int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg)
122{
Lars-Peter Clausen6438a692010-05-12 02:10:54 +0200123 struct pcf50633_adc_sync_request req;
124 int ret;
Balaji Rao08c3e062009-01-09 01:49:26 +0100125
Lars-Peter Clausen6438a692010-05-12 02:10:54 +0200126 init_completion(&req.completion);
Balaji Rao08c3e062009-01-09 01:49:26 +0100127
Lars-Peter Clausen6438a692010-05-12 02:10:54 +0200128 ret = pcf50633_adc_async_read(pcf, mux, avg,
129 pcf50633_adc_sync_read_callback, &req);
130 if (ret)
131 return ret;
Balaji Rao08c3e062009-01-09 01:49:26 +0100132
Lars-Peter Clausen6438a692010-05-12 02:10:54 +0200133 wait_for_completion(&req.completion);
Paul Fertserbd8ef102009-07-28 00:58:48 +0400134
Lars-Peter Clausen6438a692010-05-12 02:10:54 +0200135 return req.result;
Balaji Rao08c3e062009-01-09 01:49:26 +0100136}
137EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
138
139int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
140 void (*callback)(struct pcf50633 *, void *, int),
141 void *callback_param)
142{
143 struct pcf50633_adc_request *req;
144
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;
149
150 req->mux = mux;
151 req->avg = avg;
152 req->callback = callback;
153 req->callback_param = callback_param;
154
Paul Fertserbd8ef102009-07-28 00:58:48 +0400155 return adc_enqueue_request(pcf, req);
Balaji Rao08c3e062009-01-09 01:49:26 +0100156}
157EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
158
159static int adc_result(struct pcf50633 *pcf)
160{
161 u8 adcs1, adcs3;
162 u16 result;
163
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);
167
168 dev_dbg(pcf->dev, "adc result = %d\n", result);
169
170 return result;
171}
172
173static void pcf50633_adc_irq(int irq, void *data)
174{
175 struct pcf50633_adc *adc = data;
176 struct pcf50633 *pcf = adc->pcf;
177 struct pcf50633_adc_request *req;
Paul Fertserbd8ef102009-07-28 00:58:48 +0400178 int head, res;
Balaji Rao08c3e062009-01-09 01:49:26 +0100179
180 mutex_lock(&adc->queue_mutex);
181 head = adc->queue_head;
182
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;
188 }
189 adc->queue[head] = NULL;
190 adc->queue_head = (head + 1) &
191 (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
192
Paul Fertserbd8ef102009-07-28 00:58:48 +0400193 res = adc_result(pcf);
194 trigger_next_adc_job_if_any(pcf);
195
Balaji Rao08c3e062009-01-09 01:49:26 +0100196 mutex_unlock(&adc->queue_mutex);
197
Paul Fertserbd8ef102009-07-28 00:58:48 +0400198 req->callback(pcf, req->callback_param, res);
Balaji Rao08c3e062009-01-09 01:49:26 +0100199 kfree(req);
Balaji Rao08c3e062009-01-09 01:49:26 +0100200}
201
Bill Pembertonf791be42012-11-19 13:23:04 -0500202static int pcf50633_adc_probe(struct platform_device *pdev)
Balaji Rao08c3e062009-01-09 01:49:26 +0100203{
Balaji Rao08c3e062009-01-09 01:49:26 +0100204 struct pcf50633_adc *adc;
205
206 adc = kzalloc(sizeof(*adc), GFP_KERNEL);
207 if (!adc)
208 return -ENOMEM;
209
Lars-Peter Clausen68d641e2009-10-14 02:12:33 +0400210 adc->pcf = dev_to_pcf50633(pdev->dev.parent);
Balaji Rao08c3e062009-01-09 01:49:26 +0100211 platform_set_drvdata(pdev, adc);
212
Lars-Peter Clausen68d641e2009-10-14 02:12:33 +0400213 pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY,
Balaji Rao08c3e062009-01-09 01:49:26 +0100214 pcf50633_adc_irq, adc);
215
216 mutex_init(&adc->queue_mutex);
217
218 return 0;
219}
220
221static int __devexit pcf50633_adc_remove(struct platform_device *pdev)
222{
223 struct pcf50633_adc *adc = platform_get_drvdata(pdev);
224 int i, head;
225
226 pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
227
228 mutex_lock(&adc->queue_mutex);
229 head = adc->queue_head;
230
231 if (WARN_ON(adc->queue[head]))
232 dev_err(adc->pcf->dev,
233 "adc driver removed with request pending\n");
234
235 for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
236 kfree(adc->queue[i]);
237
238 mutex_unlock(&adc->queue_mutex);
239 kfree(adc);
240
241 return 0;
242}
243
244static struct platform_driver pcf50633_adc_driver = {
245 .driver = {
246 .name = "pcf50633-adc",
247 },
248 .probe = pcf50633_adc_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500249 .remove = pcf50633_adc_remove,
Balaji Rao08c3e062009-01-09 01:49:26 +0100250};
251
Mark Brown65349d62011-11-23 22:58:34 +0000252module_platform_driver(pcf50633_adc_driver);
Balaji Rao08c3e062009-01-09 01:49:26 +0100253
254MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
255MODULE_DESCRIPTION("PCF50633 adc driver");
256MODULE_LICENSE("GPL");
257MODULE_ALIAS("platform:pcf50633-adc");
258