dm: dec_pending needs locking to save error value
[linux-2.6/verdex.git] / arch / arm / plat-s3c24xx / adc.c
blobee1baf11ad9e9851754a3514d1c6eb359f7de4ca
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;
43 unsigned int nr_samples;
44 unsigned char is_ts;
45 unsigned char channel;
47 void (*select_cb)(unsigned selected);
48 void (*convert_cb)(unsigned val1, unsigned val2,
49 unsigned *samples_left);
52 struct adc_device {
53 struct platform_device *pdev;
54 struct platform_device *owner;
55 struct clk *clk;
56 struct s3c_adc_client *cur;
57 struct s3c_adc_client *ts_pend;
58 void __iomem *regs;
60 unsigned int prescale;
62 int irq;
65 static struct adc_device *adc_dev;
67 static LIST_HEAD(adc_pending);
69 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
71 static inline void s3c_adc_convert(struct adc_device *adc)
73 unsigned con = readl(adc->regs + S3C2410_ADCCON);
75 con |= S3C2410_ADCCON_ENABLE_START;
76 writel(con, adc->regs + S3C2410_ADCCON);
79 static inline void s3c_adc_select(struct adc_device *adc,
80 struct s3c_adc_client *client)
82 unsigned con = readl(adc->regs + S3C2410_ADCCON);
84 client->select_cb(1);
86 con &= ~S3C2410_ADCCON_MUXMASK;
87 con &= ~S3C2410_ADCCON_STDBM;
88 con &= ~S3C2410_ADCCON_STARTMASK;
90 if (!client->is_ts)
91 con |= S3C2410_ADCCON_SELMUX(client->channel);
93 writel(con, adc->regs + S3C2410_ADCCON);
96 static void s3c_adc_dbgshow(struct adc_device *adc)
98 adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
99 readl(adc->regs + S3C2410_ADCCON),
100 readl(adc->regs + S3C2410_ADCTSC),
101 readl(adc->regs + S3C2410_ADCDLY));
104 static void s3c_adc_try(struct adc_device *adc)
106 struct s3c_adc_client *next = adc->ts_pend;
108 if (!next && !list_empty(&adc_pending)) {
109 next = list_first_entry(&adc_pending,
110 struct s3c_adc_client, pend);
111 list_del(&next->pend);
112 } else
113 adc->ts_pend = NULL;
115 if (next) {
116 adc_dbg(adc, "new client is %p\n", next);
117 adc->cur = next;
118 s3c_adc_select(adc, next);
119 s3c_adc_convert(adc);
120 s3c_adc_dbgshow(adc);
124 int s3c_adc_start(struct s3c_adc_client *client,
125 unsigned int channel, unsigned int nr_samples)
127 struct adc_device *adc = adc_dev;
128 unsigned long flags;
130 if (!adc) {
131 printk(KERN_ERR "%s: failed to find adc\n", __func__);
132 return -EINVAL;
135 if (client->is_ts && adc->ts_pend)
136 return -EAGAIN;
138 local_irq_save(flags);
140 client->channel = channel;
141 client->nr_samples = nr_samples;
143 if (client->is_ts)
144 adc->ts_pend = client;
145 else
146 list_add_tail(&client->pend, &adc_pending);
148 if (!adc->cur)
149 s3c_adc_try(adc);
150 local_irq_restore(flags);
152 return 0;
154 EXPORT_SYMBOL_GPL(s3c_adc_start);
156 static void s3c_adc_default_select(unsigned select)
160 struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
161 void (*select)(unsigned int selected),
162 void (*conv)(unsigned d0, unsigned d1,
163 unsigned *samples_left),
164 unsigned int is_ts)
166 struct s3c_adc_client *client;
168 WARN_ON(!pdev);
169 WARN_ON(!conv);
171 if (!select)
172 select = s3c_adc_default_select;
174 if (!conv || !pdev)
175 return ERR_PTR(-EINVAL);
177 client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
178 if (!client) {
179 dev_err(&pdev->dev, "no memory for adc client\n");
180 return ERR_PTR(-ENOMEM);
183 client->pdev = pdev;
184 client->is_ts = is_ts;
185 client->select_cb = select;
186 client->convert_cb = conv;
188 return client;
190 EXPORT_SYMBOL_GPL(s3c_adc_register);
192 void s3c_adc_release(struct s3c_adc_client *client)
194 /* We should really check that nothing is in progress. */
195 if (adc_dev->cur == client)
196 adc_dev->cur = NULL;
197 if (adc_dev->ts_pend == client)
198 adc_dev->ts_pend = NULL;
199 else {
200 struct list_head *p, *n;
201 struct s3c_adc_client *tmp;
203 list_for_each_safe(p, n, &adc_pending) {
204 tmp = list_entry(p, struct s3c_adc_client, pend);
205 if (tmp == client)
206 list_del(&tmp->pend);
210 if (adc_dev->cur == NULL)
211 s3c_adc_try(adc_dev);
212 kfree(client);
214 EXPORT_SYMBOL_GPL(s3c_adc_release);
216 static irqreturn_t s3c_adc_irq(int irq, void *pw)
218 struct adc_device *adc = pw;
219 struct s3c_adc_client *client = adc->cur;
220 unsigned long flags;
221 unsigned data0, data1;
223 if (!client) {
224 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
225 return IRQ_HANDLED;
228 data0 = readl(adc->regs + S3C2410_ADCDAT0);
229 data1 = readl(adc->regs + S3C2410_ADCDAT1);
230 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
232 client->nr_samples--;
233 (client->convert_cb)(data0 & 0x3ff, data1 & 0x3ff, &client->nr_samples);
235 if (client->nr_samples > 0) {
236 /* fire another conversion for this */
238 client->select_cb(1);
239 s3c_adc_convert(adc);
240 } else {
241 local_irq_save(flags);
242 (client->select_cb)(0);
243 adc->cur = NULL;
245 s3c_adc_try(adc);
246 local_irq_restore(flags);
249 return IRQ_HANDLED;
252 static int s3c_adc_probe(struct platform_device *pdev)
254 struct device *dev = &pdev->dev;
255 struct adc_device *adc;
256 struct resource *regs;
257 int ret;
259 adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
260 if (adc == NULL) {
261 dev_err(dev, "failed to allocate adc_device\n");
262 return -ENOMEM;
265 adc->pdev = pdev;
266 adc->prescale = S3C2410_ADCCON_PRSCVL(49);
268 adc->irq = platform_get_irq(pdev, 1);
269 if (adc->irq <= 0) {
270 dev_err(dev, "failed to get adc irq\n");
271 ret = -ENOENT;
272 goto err_alloc;
275 ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
276 if (ret < 0) {
277 dev_err(dev, "failed to attach adc irq\n");
278 goto err_alloc;
281 adc->clk = clk_get(dev, "adc");
282 if (IS_ERR(adc->clk)) {
283 dev_err(dev, "failed to get adc clock\n");
284 ret = PTR_ERR(adc->clk);
285 goto err_irq;
288 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289 if (!regs) {
290 dev_err(dev, "failed to find registers\n");
291 ret = -ENXIO;
292 goto err_clk;
295 adc->regs = ioremap(regs->start, resource_size(regs));
296 if (!adc->regs) {
297 dev_err(dev, "failed to map registers\n");
298 ret = -ENXIO;
299 goto err_clk;
302 clk_enable(adc->clk);
304 writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
305 adc->regs + S3C2410_ADCCON);
307 dev_info(dev, "attached adc driver\n");
309 platform_set_drvdata(pdev, adc);
310 adc_dev = adc;
312 return 0;
314 err_clk:
315 clk_put(adc->clk);
317 err_irq:
318 free_irq(adc->irq, adc);
320 err_alloc:
321 kfree(adc);
322 return ret;
325 static int s3c_adc_remove(struct platform_device *pdev)
327 struct adc_device *adc = platform_get_drvdata(pdev);
329 iounmap(adc->regs);
330 free_irq(adc->irq, adc);
331 clk_disable(adc->clk);
332 clk_put(adc->clk);
333 kfree(adc);
335 return 0;
338 #ifdef CONFIG_PM
339 static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
341 struct adc_device *adc = platform_get_drvdata(pdev);
342 u32 con;
344 con = readl(adc->regs + S3C2410_ADCCON);
345 con |= S3C2410_ADCCON_STDBM;
346 writel(con, adc->regs + S3C2410_ADCCON);
348 clk_disable(adc->clk);
350 return 0;
353 static int s3c_adc_resume(struct platform_device *pdev)
355 struct adc_device *adc = platform_get_drvdata(pdev);
357 clk_enable(adc->clk);
359 writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
360 adc->regs + S3C2410_ADCCON);
362 return 0;
365 #else
366 #define s3c_adc_suspend NULL
367 #define s3c_adc_resume NULL
368 #endif
370 static struct platform_driver s3c_adc_driver = {
371 .driver = {
372 .name = "s3c24xx-adc",
373 .owner = THIS_MODULE,
375 .probe = s3c_adc_probe,
376 .remove = __devexit_p(s3c_adc_remove),
377 .suspend = s3c_adc_suspend,
378 .resume = s3c_adc_resume,
381 static int __init adc_init(void)
383 int ret;
385 ret = platform_driver_register(&s3c_adc_driver);
386 if (ret)
387 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
389 return ret;
392 arch_initcall(adc_init);