dm snapshot: rework writing to origin
[linux-2.6/btrfs-unstable.git] / arch / arm / plat-s3c24xx / adc.c
blobce47627f3368f556b22f0694e7de68325a7e074d
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/sched.h>
18 #include <linux/list.h>
19 #include <linux/err.h>
20 #include <linux/clk.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
24 #include <plat/regs-adc.h>
25 #include <plat/adc.h>
27 /* This driver is designed to control the usage of the ADC block between
28 * the touchscreen and any other drivers that may need to use it, such as
29 * the hwmon driver.
31 * Priority will be given to the touchscreen driver, but as this itself is
32 * rate limited it should not starve other requests which are processed in
33 * order that they are received.
35 * Each user registers to get a client block which uniquely identifies it
36 * and stores information such as the necessary functions to callback when
37 * action is required.
40 struct s3c_adc_client {
41 struct platform_device *pdev;
42 struct list_head pend;
43 wait_queue_head_t *wait;
45 unsigned int nr_samples;
46 int result;
47 unsigned char is_ts;
48 unsigned char channel;
50 void (*select_cb)(struct s3c_adc_client *c, unsigned selected);
51 void (*convert_cb)(struct s3c_adc_client *c,
52 unsigned val1, unsigned val2,
53 unsigned *samples_left);
56 struct adc_device {
57 struct platform_device *pdev;
58 struct platform_device *owner;
59 struct clk *clk;
60 struct s3c_adc_client *cur;
61 struct s3c_adc_client *ts_pend;
62 void __iomem *regs;
64 unsigned int prescale;
66 int irq;
69 static struct adc_device *adc_dev;
71 static LIST_HEAD(adc_pending);
73 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
75 static inline void s3c_adc_convert(struct adc_device *adc)
77 unsigned con = readl(adc->regs + S3C2410_ADCCON);
79 con |= S3C2410_ADCCON_ENABLE_START;
80 writel(con, adc->regs + S3C2410_ADCCON);
83 static inline void s3c_adc_select(struct adc_device *adc,
84 struct s3c_adc_client *client)
86 unsigned con = readl(adc->regs + S3C2410_ADCCON);
88 client->select_cb(client, 1);
90 con &= ~S3C2410_ADCCON_MUXMASK;
91 con &= ~S3C2410_ADCCON_STDBM;
92 con &= ~S3C2410_ADCCON_STARTMASK;
94 if (!client->is_ts)
95 con |= S3C2410_ADCCON_SELMUX(client->channel);
97 writel(con, adc->regs + S3C2410_ADCCON);
100 static void s3c_adc_dbgshow(struct adc_device *adc)
102 adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
103 readl(adc->regs + S3C2410_ADCCON),
104 readl(adc->regs + S3C2410_ADCTSC),
105 readl(adc->regs + S3C2410_ADCDLY));
108 static void s3c_adc_try(struct adc_device *adc)
110 struct s3c_adc_client *next = adc->ts_pend;
112 if (!next && !list_empty(&adc_pending)) {
113 next = list_first_entry(&adc_pending,
114 struct s3c_adc_client, pend);
115 list_del(&next->pend);
116 } else
117 adc->ts_pend = NULL;
119 if (next) {
120 adc_dbg(adc, "new client is %p\n", next);
121 adc->cur = next;
122 s3c_adc_select(adc, next);
123 s3c_adc_convert(adc);
124 s3c_adc_dbgshow(adc);
128 int s3c_adc_start(struct s3c_adc_client *client,
129 unsigned int channel, unsigned int nr_samples)
131 struct adc_device *adc = adc_dev;
132 unsigned long flags;
134 if (!adc) {
135 printk(KERN_ERR "%s: failed to find adc\n", __func__);
136 return -EINVAL;
139 if (client->is_ts && adc->ts_pend)
140 return -EAGAIN;
142 local_irq_save(flags);
144 client->channel = channel;
145 client->nr_samples = nr_samples;
147 if (client->is_ts)
148 adc->ts_pend = client;
149 else
150 list_add_tail(&client->pend, &adc_pending);
152 if (!adc->cur)
153 s3c_adc_try(adc);
154 local_irq_restore(flags);
156 return 0;
158 EXPORT_SYMBOL_GPL(s3c_adc_start);
160 static void s3c_convert_done(struct s3c_adc_client *client,
161 unsigned v, unsigned u, unsigned *left)
163 client->result = v;
164 wake_up(client->wait);
167 int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
169 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
170 int ret;
172 client->convert_cb = s3c_convert_done;
173 client->wait = &wake;
174 client->result = -1;
176 ret = s3c_adc_start(client, ch, 1);
177 if (ret < 0)
178 goto err;
180 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
181 if (client->result < 0) {
182 ret = -ETIMEDOUT;
183 goto err;
186 client->convert_cb = NULL;
187 return client->result;
189 err:
190 return ret;
192 EXPORT_SYMBOL_GPL(s3c_adc_read);
194 static void s3c_adc_default_select(struct s3c_adc_client *client,
195 unsigned select)
199 struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
200 void (*select)(struct s3c_adc_client *client,
201 unsigned int selected),
202 void (*conv)(struct s3c_adc_client *client,
203 unsigned d0, unsigned d1,
204 unsigned *samples_left),
205 unsigned int is_ts)
207 struct s3c_adc_client *client;
209 WARN_ON(!pdev);
211 if (!select)
212 select = s3c_adc_default_select;
214 if (!pdev)
215 return ERR_PTR(-EINVAL);
217 client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
218 if (!client) {
219 dev_err(&pdev->dev, "no memory for adc client\n");
220 return ERR_PTR(-ENOMEM);
223 client->pdev = pdev;
224 client->is_ts = is_ts;
225 client->select_cb = select;
226 client->convert_cb = conv;
228 return client;
230 EXPORT_SYMBOL_GPL(s3c_adc_register);
232 void s3c_adc_release(struct s3c_adc_client *client)
234 /* We should really check that nothing is in progress. */
235 if (adc_dev->cur == client)
236 adc_dev->cur = NULL;
237 if (adc_dev->ts_pend == client)
238 adc_dev->ts_pend = NULL;
239 else {
240 struct list_head *p, *n;
241 struct s3c_adc_client *tmp;
243 list_for_each_safe(p, n, &adc_pending) {
244 tmp = list_entry(p, struct s3c_adc_client, pend);
245 if (tmp == client)
246 list_del(&tmp->pend);
250 if (adc_dev->cur == NULL)
251 s3c_adc_try(adc_dev);
252 kfree(client);
254 EXPORT_SYMBOL_GPL(s3c_adc_release);
256 static irqreturn_t s3c_adc_irq(int irq, void *pw)
258 struct adc_device *adc = pw;
259 struct s3c_adc_client *client = adc->cur;
260 unsigned long flags;
261 unsigned data0, data1;
263 if (!client) {
264 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
265 return IRQ_HANDLED;
268 data0 = readl(adc->regs + S3C2410_ADCDAT0);
269 data1 = readl(adc->regs + S3C2410_ADCDAT1);
270 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
272 client->nr_samples--;
274 if (client->convert_cb)
275 (client->convert_cb)(client, data0 & 0x3ff, data1 & 0x3ff,
276 &client->nr_samples);
278 if (client->nr_samples > 0) {
279 /* fire another conversion for this */
281 client->select_cb(client, 1);
282 s3c_adc_convert(adc);
283 } else {
284 local_irq_save(flags);
285 (client->select_cb)(client, 0);
286 adc->cur = NULL;
288 s3c_adc_try(adc);
289 local_irq_restore(flags);
292 return IRQ_HANDLED;
295 static int s3c_adc_probe(struct platform_device *pdev)
297 struct device *dev = &pdev->dev;
298 struct adc_device *adc;
299 struct resource *regs;
300 int ret;
302 adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
303 if (adc == NULL) {
304 dev_err(dev, "failed to allocate adc_device\n");
305 return -ENOMEM;
308 adc->pdev = pdev;
309 adc->prescale = S3C2410_ADCCON_PRSCVL(49);
311 adc->irq = platform_get_irq(pdev, 1);
312 if (adc->irq <= 0) {
313 dev_err(dev, "failed to get adc irq\n");
314 ret = -ENOENT;
315 goto err_alloc;
318 ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
319 if (ret < 0) {
320 dev_err(dev, "failed to attach adc irq\n");
321 goto err_alloc;
324 adc->clk = clk_get(dev, "adc");
325 if (IS_ERR(adc->clk)) {
326 dev_err(dev, "failed to get adc clock\n");
327 ret = PTR_ERR(adc->clk);
328 goto err_irq;
331 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
332 if (!regs) {
333 dev_err(dev, "failed to find registers\n");
334 ret = -ENXIO;
335 goto err_clk;
338 adc->regs = ioremap(regs->start, resource_size(regs));
339 if (!adc->regs) {
340 dev_err(dev, "failed to map registers\n");
341 ret = -ENXIO;
342 goto err_clk;
345 clk_enable(adc->clk);
347 writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
348 adc->regs + S3C2410_ADCCON);
350 dev_info(dev, "attached adc driver\n");
352 platform_set_drvdata(pdev, adc);
353 adc_dev = adc;
355 return 0;
357 err_clk:
358 clk_put(adc->clk);
360 err_irq:
361 free_irq(adc->irq, adc);
363 err_alloc:
364 kfree(adc);
365 return ret;
368 static int __devexit s3c_adc_remove(struct platform_device *pdev)
370 struct adc_device *adc = platform_get_drvdata(pdev);
372 iounmap(adc->regs);
373 free_irq(adc->irq, adc);
374 clk_disable(adc->clk);
375 clk_put(adc->clk);
376 kfree(adc);
378 return 0;
381 #ifdef CONFIG_PM
382 static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
384 struct adc_device *adc = platform_get_drvdata(pdev);
385 u32 con;
387 con = readl(adc->regs + S3C2410_ADCCON);
388 con |= S3C2410_ADCCON_STDBM;
389 writel(con, adc->regs + S3C2410_ADCCON);
391 clk_disable(adc->clk);
393 return 0;
396 static int s3c_adc_resume(struct platform_device *pdev)
398 struct adc_device *adc = platform_get_drvdata(pdev);
400 clk_enable(adc->clk);
402 writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
403 adc->regs + S3C2410_ADCCON);
405 return 0;
408 #else
409 #define s3c_adc_suspend NULL
410 #define s3c_adc_resume NULL
411 #endif
413 static struct platform_driver s3c_adc_driver = {
414 .driver = {
415 .name = "s3c24xx-adc",
416 .owner = THIS_MODULE,
418 .probe = s3c_adc_probe,
419 .remove = __devexit_p(s3c_adc_remove),
420 .suspend = s3c_adc_suspend,
421 .resume = s3c_adc_resume,
424 static int __init adc_init(void)
426 int ret;
428 ret = platform_driver_register(&s3c_adc_driver);
429 if (ret)
430 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
432 return ret;
435 arch_initcall(adc_init);