11117a7ba911e735f911b6ba5dea75d8bf3d2f66
[safe/jmp/linux-2.6] / arch / arm / plat-s3c24xx / adc.c
1 /* arch/arm/plat-s3c24xx/adc.c
2  *
3  * Copyright (c) 2008 Simtec Electronics
4  *      http://armlinux.simtec.co.uk/
5  *      Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
6  *
7  * S3C24XX ADC device core
8  *
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.
12 */
13
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>
22
23 #include <plat/regs-adc.h>
24 #include <plat/adc.h>
25
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.
29  *
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.
33  *
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.
37  */
38
39 struct s3c_adc_client {
40         struct platform_device  *pdev;
41         struct list_head         pend;
42         wait_queue_head_t       *wait;
43
44         unsigned int             nr_samples;
45         int                      result;
46         unsigned char            is_ts;
47         unsigned char            channel;
48
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);
53 };
54
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;
62
63         unsigned int             prescale;
64
65         int                      irq;
66 };
67
68 static struct adc_device *adc_dev;
69
70 static LIST_HEAD(adc_pending);
71
72 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
73
74 static inline void s3c_adc_convert(struct adc_device *adc)
75 {
76         unsigned con = readl(adc->regs + S3C2410_ADCCON);
77
78         con |= S3C2410_ADCCON_ENABLE_START;
79         writel(con, adc->regs + S3C2410_ADCCON);
80 }
81
82 static inline void s3c_adc_select(struct adc_device *adc,
83                                   struct s3c_adc_client *client)
84 {
85         unsigned con = readl(adc->regs + S3C2410_ADCCON);
86
87         client->select_cb(client, 1);
88
89         con &= ~S3C2410_ADCCON_MUXMASK;
90         con &= ~S3C2410_ADCCON_STDBM;
91         con &= ~S3C2410_ADCCON_STARTMASK;
92
93         if (!client->is_ts)
94                 con |= S3C2410_ADCCON_SELMUX(client->channel);
95
96         writel(con, adc->regs + S3C2410_ADCCON);
97 }
98
99 static void s3c_adc_dbgshow(struct adc_device *adc)
100 {
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));
105 }
106
107 static void s3c_adc_try(struct adc_device *adc)
108 {
109         struct s3c_adc_client *next = adc->ts_pend;
110
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;
117
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);
124         }
125 }
126
127 int s3c_adc_start(struct s3c_adc_client *client,
128                   unsigned int channel, unsigned int nr_samples)
129 {
130         struct adc_device *adc = adc_dev;
131         unsigned long flags;
132
133         if (!adc) {
134                 printk(KERN_ERR "%s: failed to find adc\n", __func__);
135                 return -EINVAL;
136         }
137
138         if (client->is_ts && adc->ts_pend)
139                 return -EAGAIN;
140
141         local_irq_save(flags);
142
143         client->channel = channel;
144         client->nr_samples = nr_samples;
145
146         if (client->is_ts)
147                 adc->ts_pend = client;
148         else
149                 list_add_tail(&client->pend, &adc_pending);
150
151         if (!adc->cur)
152                 s3c_adc_try(adc);
153         local_irq_restore(flags);
154
155         return 0;
156 }
157 EXPORT_SYMBOL_GPL(s3c_adc_start);
158
159 static void s3c_convert_done(struct s3c_adc_client *client,
160                              unsigned v, unsigned u, unsigned *left)
161 {
162         client->result = v;
163         wake_up(client->wait);
164 }
165
166 int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
167 {
168         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
169         int ret;
170
171         client->convert_cb = s3c_convert_done;
172         client->wait = &wake;
173         client->result = -1;
174
175         ret = s3c_adc_start(client, ch, 1);
176         if (ret < 0)
177                 goto err;
178
179         ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
180         if (client->result < 0) {
181                 ret = -ETIMEDOUT;
182                 goto err;
183         }
184
185         client->convert_cb = NULL;
186         return client->result;
187
188 err:
189         return ret;
190 }
191 EXPORT_SYMBOL_GPL(s3c_adc_convert);
192
193 static void s3c_adc_default_select(struct s3c_adc_client *client,
194                                    unsigned select)
195 {
196 }
197
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)
205 {
206         struct s3c_adc_client *client;
207
208         WARN_ON(!pdev);
209
210         if (!select)
211                 select = s3c_adc_default_select;
212
213         if (!pdev)
214                 return ERR_PTR(-EINVAL);
215
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);
220         }
221
222         client->pdev = pdev;
223         client->is_ts = is_ts;
224         client->select_cb = select;
225         client->convert_cb = conv;
226
227         return client;
228 }
229 EXPORT_SYMBOL_GPL(s3c_adc_register);
230
231 void s3c_adc_release(struct s3c_adc_client *client)
232 {
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;
241
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);
246                 }
247         }
248
249         if (adc_dev->cur == NULL)
250                 s3c_adc_try(adc_dev);
251         kfree(client);
252 }
253 EXPORT_SYMBOL_GPL(s3c_adc_release);
254
255 static irqreturn_t s3c_adc_irq(int irq, void *pw)
256 {
257         struct adc_device *adc = pw;
258         struct s3c_adc_client *client = adc->cur;
259         unsigned long flags;
260         unsigned data0, data1;
261
262         if (!client) {
263                 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
264                 return IRQ_HANDLED;
265         }
266
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);
270
271         client->nr_samples--;
272
273         if (client->convert_cb)
274                 (client->convert_cb)(client, data0 & 0x3ff, data1 & 0x3ff,
275                                      &client->nr_samples);
276
277         if (client->nr_samples > 0) {
278                 /* fire another conversion for this */
279
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;
286
287                 s3c_adc_try(adc);
288                 local_irq_restore(flags);
289         }
290
291         return IRQ_HANDLED;
292 }
293
294 static int s3c_adc_probe(struct platform_device *pdev)
295 {
296         struct device *dev = &pdev->dev;
297         struct adc_device *adc;
298         struct resource *regs;
299         int ret;
300
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;
305         }
306
307         adc->pdev = pdev;
308         adc->prescale = S3C2410_ADCCON_PRSCVL(49);
309
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;
315         }
316
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;
321         }
322
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;
328         }
329
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;
335         }
336
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;
342         }
343
344         clk_enable(adc->clk);
345
346         writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
347                adc->regs + S3C2410_ADCCON);
348
349         dev_info(dev, "attached adc driver\n");
350
351         platform_set_drvdata(pdev, adc);
352         adc_dev = adc;
353
354         return 0;
355
356  err_clk:
357         clk_put(adc->clk);
358
359  err_irq:
360         free_irq(adc->irq, adc);
361
362  err_alloc:
363         kfree(adc);
364         return ret;
365 }
366
367 static int s3c_adc_remove(struct platform_device *pdev)
368 {
369         struct adc_device *adc = platform_get_drvdata(pdev);
370
371         iounmap(adc->regs);
372         free_irq(adc->irq, adc);
373         clk_disable(adc->clk);
374         clk_put(adc->clk);
375         kfree(adc);
376
377         return 0;
378 }
379
380 #ifdef CONFIG_PM
381 static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
382 {
383         struct adc_device *adc = platform_get_drvdata(pdev);
384         u32 con;
385
386         con = readl(adc->regs + S3C2410_ADCCON);
387         con |= S3C2410_ADCCON_STDBM;
388         writel(con, adc->regs + S3C2410_ADCCON);
389
390         clk_disable(adc->clk);
391
392         return 0;
393 }
394
395 static int s3c_adc_resume(struct platform_device *pdev)
396 {
397         struct adc_device *adc = platform_get_drvdata(pdev);
398
399         clk_enable(adc->clk);
400
401         writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
402                adc->regs + S3C2410_ADCCON);
403
404         return 0;
405 }
406
407 #else
408 #define s3c_adc_suspend NULL
409 #define s3c_adc_resume NULL
410 #endif
411
412 static struct platform_driver s3c_adc_driver = {
413         .driver         = {
414                 .name   = "s3c24xx-adc",
415                 .owner  = THIS_MODULE,
416         },
417         .probe          = s3c_adc_probe,
418         .remove         = __devexit_p(s3c_adc_remove),
419         .suspend        = s3c_adc_suspend,
420         .resume         = s3c_adc_resume,
421 };
422
423 static int __init adc_init(void)
424 {
425         int ret;
426
427         ret = platform_driver_register(&s3c_adc_driver);
428         if (ret)
429                 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
430
431         return ret;
432 }
433
434 arch_initcall(adc_init);