Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core-2.6
[linux/fpc-iii.git] / arch / arm / plat-s3c24xx / adc.c
blob11117a7ba911e735f911b6ba5dea75d8bf3d2f66
1 /* arch/arm/plat-s3c24xx/adc.c
3 * Copyright (c) 2008 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
7 * S3C24XX ADC device core
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/list.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
23 #include <plat/regs-adc.h>
24 #include <plat/adc.h>
26 /* This driver is designed to control the usage of the ADC block between
27 * the touchscreen and any other drivers that may need to use it, such as
28 * the hwmon driver.
30 * Priority will be given to the touchscreen driver, but as this itself is
31 * rate limited it should not starve other requests which are processed in
32 * order that they are received.
34 * Each user registers to get a client block which uniquely identifies it
35 * and stores information such as the necessary functions to callback when
36 * action is required.
39 struct s3c_adc_client {
40 struct platform_device *pdev;
41 struct list_head pend;
42 wait_queue_head_t *wait;
44 unsigned int nr_samples;
45 int result;
46 unsigned char is_ts;
47 unsigned char channel;
49 void (*select_cb)(struct s3c_adc_client *c, unsigned selected);
50 void (*convert_cb)(struct s3c_adc_client *c,
51 unsigned val1, unsigned val2,
52 unsigned *samples_left);
55 struct adc_device {
56 struct platform_device *pdev;
57 struct platform_device *owner;
58 struct clk *clk;
59 struct s3c_adc_client *cur;
60 struct s3c_adc_client *ts_pend;
61 void __iomem *regs;
63 unsigned int prescale;
65 int irq;
68 static struct adc_device *adc_dev;
70 static LIST_HEAD(adc_pending);
72 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
74 static inline void s3c_adc_convert(struct adc_device *adc)
76 unsigned con = readl(adc->regs + S3C2410_ADCCON);
78 con |= S3C2410_ADCCON_ENABLE_START;
79 writel(con, adc->regs + S3C2410_ADCCON);
82 static inline void s3c_adc_select(struct adc_device *adc,
83 struct s3c_adc_client *client)
85 unsigned con = readl(adc->regs + S3C2410_ADCCON);
87 client->select_cb(client, 1);
89 con &= ~S3C2410_ADCCON_MUXMASK;
90 con &= ~S3C2410_ADCCON_STDBM;
91 con &= ~S3C2410_ADCCON_STARTMASK;
93 if (!client->is_ts)
94 con |= S3C2410_ADCCON_SELMUX(client->channel);
96 writel(con, adc->regs + S3C2410_ADCCON);
99 static void s3c_adc_dbgshow(struct adc_device *adc)
101 adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
102 readl(adc->regs + S3C2410_ADCCON),
103 readl(adc->regs + S3C2410_ADCTSC),
104 readl(adc->regs + S3C2410_ADCDLY));
107 static void s3c_adc_try(struct adc_device *adc)
109 struct s3c_adc_client *next = adc->ts_pend;
111 if (!next && !list_empty(&adc_pending)) {
112 next = list_first_entry(&adc_pending,
113 struct s3c_adc_client, pend);
114 list_del(&next->pend);
115 } else
116 adc->ts_pend = NULL;
118 if (next) {
119 adc_dbg(adc, "new client is %p\n", next);
120 adc->cur = next;
121 s3c_adc_select(adc, next);
122 s3c_adc_convert(adc);
123 s3c_adc_dbgshow(adc);
127 int s3c_adc_start(struct s3c_adc_client *client,
128 unsigned int channel, unsigned int nr_samples)
130 struct adc_device *adc = adc_dev;
131 unsigned long flags;
133 if (!adc) {
134 printk(KERN_ERR "%s: failed to find adc\n", __func__);
135 return -EINVAL;
138 if (client->is_ts && adc->ts_pend)
139 return -EAGAIN;
141 local_irq_save(flags);
143 client->channel = channel;
144 client->nr_samples = nr_samples;
146 if (client->is_ts)
147 adc->ts_pend = client;
148 else
149 list_add_tail(&client->pend, &adc_pending);
151 if (!adc->cur)
152 s3c_adc_try(adc);
153 local_irq_restore(flags);
155 return 0;
157 EXPORT_SYMBOL_GPL(s3c_adc_start);
159 static void s3c_convert_done(struct s3c_adc_client *client,
160 unsigned v, unsigned u, unsigned *left)
162 client->result = v;
163 wake_up(client->wait);
166 int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
168 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
169 int ret;
171 client->convert_cb = s3c_convert_done;
172 client->wait = &wake;
173 client->result = -1;
175 ret = s3c_adc_start(client, ch, 1);
176 if (ret < 0)
177 goto err;
179 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
180 if (client->result < 0) {
181 ret = -ETIMEDOUT;
182 goto err;
185 client->convert_cb = NULL;
186 return client->result;
188 err:
189 return ret;
191 EXPORT_SYMBOL_GPL(s3c_adc_convert);
193 static void s3c_adc_default_select(struct s3c_adc_client *client,
194 unsigned select)
198 struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
199 void (*select)(struct s3c_adc_client *client,
200 unsigned int selected),
201 void (*conv)(struct s3c_adc_client *client,
202 unsigned d0, unsigned d1,
203 unsigned *samples_left),
204 unsigned int is_ts)
206 struct s3c_adc_client *client;
208 WARN_ON(!pdev);
210 if (!select)
211 select = s3c_adc_default_select;
213 if (!pdev)
214 return ERR_PTR(-EINVAL);
216 client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
217 if (!client) {
218 dev_err(&pdev->dev, "no memory for adc client\n");
219 return ERR_PTR(-ENOMEM);
222 client->pdev = pdev;
223 client->is_ts = is_ts;
224 client->select_cb = select;
225 client->convert_cb = conv;
227 return client;
229 EXPORT_SYMBOL_GPL(s3c_adc_register);
231 void s3c_adc_release(struct s3c_adc_client *client)
233 /* We should really check that nothing is in progress. */
234 if (adc_dev->cur == client)
235 adc_dev->cur = NULL;
236 if (adc_dev->ts_pend == client)
237 adc_dev->ts_pend = NULL;
238 else {
239 struct list_head *p, *n;
240 struct s3c_adc_client *tmp;
242 list_for_each_safe(p, n, &adc_pending) {
243 tmp = list_entry(p, struct s3c_adc_client, pend);
244 if (tmp == client)
245 list_del(&tmp->pend);
249 if (adc_dev->cur == NULL)
250 s3c_adc_try(adc_dev);
251 kfree(client);
253 EXPORT_SYMBOL_GPL(s3c_adc_release);
255 static irqreturn_t s3c_adc_irq(int irq, void *pw)
257 struct adc_device *adc = pw;
258 struct s3c_adc_client *client = adc->cur;
259 unsigned long flags;
260 unsigned data0, data1;
262 if (!client) {
263 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
264 return IRQ_HANDLED;
267 data0 = readl(adc->regs + S3C2410_ADCDAT0);
268 data1 = readl(adc->regs + S3C2410_ADCDAT1);
269 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
271 client->nr_samples--;
273 if (client->convert_cb)
274 (client->convert_cb)(client, data0 & 0x3ff, data1 & 0x3ff,
275 &client->nr_samples);
277 if (client->nr_samples > 0) {
278 /* fire another conversion for this */
280 client->select_cb(client, 1);
281 s3c_adc_convert(adc);
282 } else {
283 local_irq_save(flags);
284 (client->select_cb)(client, 0);
285 adc->cur = NULL;
287 s3c_adc_try(adc);
288 local_irq_restore(flags);
291 return IRQ_HANDLED;
294 static int s3c_adc_probe(struct platform_device *pdev)
296 struct device *dev = &pdev->dev;
297 struct adc_device *adc;
298 struct resource *regs;
299 int ret;
301 adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
302 if (adc == NULL) {
303 dev_err(dev, "failed to allocate adc_device\n");
304 return -ENOMEM;
307 adc->pdev = pdev;
308 adc->prescale = S3C2410_ADCCON_PRSCVL(49);
310 adc->irq = platform_get_irq(pdev, 1);
311 if (adc->irq <= 0) {
312 dev_err(dev, "failed to get adc irq\n");
313 ret = -ENOENT;
314 goto err_alloc;
317 ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
318 if (ret < 0) {
319 dev_err(dev, "failed to attach adc irq\n");
320 goto err_alloc;
323 adc->clk = clk_get(dev, "adc");
324 if (IS_ERR(adc->clk)) {
325 dev_err(dev, "failed to get adc clock\n");
326 ret = PTR_ERR(adc->clk);
327 goto err_irq;
330 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
331 if (!regs) {
332 dev_err(dev, "failed to find registers\n");
333 ret = -ENXIO;
334 goto err_clk;
337 adc->regs = ioremap(regs->start, resource_size(regs));
338 if (!adc->regs) {
339 dev_err(dev, "failed to map registers\n");
340 ret = -ENXIO;
341 goto err_clk;
344 clk_enable(adc->clk);
346 writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
347 adc->regs + S3C2410_ADCCON);
349 dev_info(dev, "attached adc driver\n");
351 platform_set_drvdata(pdev, adc);
352 adc_dev = adc;
354 return 0;
356 err_clk:
357 clk_put(adc->clk);
359 err_irq:
360 free_irq(adc->irq, adc);
362 err_alloc:
363 kfree(adc);
364 return ret;
367 static int s3c_adc_remove(struct platform_device *pdev)
369 struct adc_device *adc = platform_get_drvdata(pdev);
371 iounmap(adc->regs);
372 free_irq(adc->irq, adc);
373 clk_disable(adc->clk);
374 clk_put(adc->clk);
375 kfree(adc);
377 return 0;
380 #ifdef CONFIG_PM
381 static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
383 struct adc_device *adc = platform_get_drvdata(pdev);
384 u32 con;
386 con = readl(adc->regs + S3C2410_ADCCON);
387 con |= S3C2410_ADCCON_STDBM;
388 writel(con, adc->regs + S3C2410_ADCCON);
390 clk_disable(adc->clk);
392 return 0;
395 static int s3c_adc_resume(struct platform_device *pdev)
397 struct adc_device *adc = platform_get_drvdata(pdev);
399 clk_enable(adc->clk);
401 writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
402 adc->regs + S3C2410_ADCCON);
404 return 0;
407 #else
408 #define s3c_adc_suspend NULL
409 #define s3c_adc_resume NULL
410 #endif
412 static struct platform_driver s3c_adc_driver = {
413 .driver = {
414 .name = "s3c24xx-adc",
415 .owner = THIS_MODULE,
417 .probe = s3c_adc_probe,
418 .remove = __devexit_p(s3c_adc_remove),
419 .suspend = s3c_adc_suspend,
420 .resume = s3c_adc_resume,
423 static int __init adc_init(void)
425 int ret;
427 ret = platform_driver_register(&s3c_adc_driver);
428 if (ret)
429 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
431 return ret;
434 arch_initcall(adc_init);