Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[pandora-kernel.git] / 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
43         unsigned int             nr_samples;
44         unsigned char            is_ts;
45         unsigned char            channel;
46
47         void    (*select_cb)(unsigned selected);
48         void    (*convert_cb)(unsigned val1, unsigned val2);
49 };
50
51 struct adc_device {
52         struct platform_device  *pdev;
53         struct platform_device  *owner;
54         struct clk              *clk;
55         struct s3c_adc_client   *cur;
56         struct s3c_adc_client   *ts_pend;
57         void __iomem            *regs;
58
59         unsigned int             prescale;
60
61         int                      irq;
62 };
63
64 static struct adc_device *adc_dev;
65
66 static LIST_HEAD(adc_pending);
67
68 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
69
70 static inline void s3c_adc_convert(struct adc_device *adc)
71 {
72         unsigned con = readl(adc->regs + S3C2410_ADCCON);
73
74         con |= S3C2410_ADCCON_ENABLE_START;
75         writel(con, adc->regs + S3C2410_ADCCON);
76 }
77
78 static inline void s3c_adc_select(struct adc_device *adc,
79                                   struct s3c_adc_client *client)
80 {
81         unsigned con = readl(adc->regs + S3C2410_ADCCON);
82
83         client->select_cb(1);
84
85         con &= ~S3C2410_ADCCON_MUXMASK;
86         con &= ~S3C2410_ADCCON_STDBM;
87         con &= ~S3C2410_ADCCON_STARTMASK;
88
89         if (!client->is_ts)
90                 con |= S3C2410_ADCCON_SELMUX(client->channel);
91
92         writel(con, adc->regs + S3C2410_ADCCON);
93 }
94
95 static void s3c_adc_dbgshow(struct adc_device *adc)
96 {
97         adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
98                 readl(adc->regs + S3C2410_ADCCON),
99                 readl(adc->regs + S3C2410_ADCTSC),
100                 readl(adc->regs + S3C2410_ADCDLY));
101 }
102
103 static void s3c_adc_try(struct adc_device *adc)
104 {
105         struct s3c_adc_client *next = adc->ts_pend;
106
107         if (!next && !list_empty(&adc_pending)) {
108                 next = list_first_entry(&adc_pending,
109                                         struct s3c_adc_client, pend);
110                 list_del(&next->pend);
111         } else
112                 adc->ts_pend = NULL;
113
114         if (next) {
115                 adc_dbg(adc, "new client is %p\n", next);
116                 adc->cur = next;
117                 s3c_adc_select(adc, next);
118                 s3c_adc_convert(adc);
119                 s3c_adc_dbgshow(adc);
120         }
121 }
122
123 int s3c_adc_start(struct s3c_adc_client *client,
124                   unsigned int channel, unsigned int nr_samples)
125 {
126         struct adc_device *adc = adc_dev;
127         unsigned long flags;
128
129         if (!adc) {
130                 printk(KERN_ERR "%s: failed to find adc\n", __func__);
131                 return -EINVAL;
132         }
133
134         if (client->is_ts && adc->ts_pend)
135                 return -EAGAIN;
136
137         local_irq_save(flags);
138
139         client->channel = channel;
140         client->nr_samples = nr_samples;
141
142         if (client->is_ts)
143                 adc->ts_pend = client;
144         else
145                 list_add_tail(&client->pend, &adc_pending);
146
147         if (!adc->cur)
148                 s3c_adc_try(adc);
149         local_irq_restore(flags);
150
151         return 0;
152 }
153 EXPORT_SYMBOL_GPL(s3c_adc_start);
154
155 static void s3c_adc_default_select(unsigned select)
156 {
157 }
158
159 struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
160                                         void (*select)(unsigned int selected),
161                                         void (*conv)(unsigned d0, unsigned d1),
162                                         unsigned int is_ts)
163 {
164         struct s3c_adc_client *client;
165
166         WARN_ON(!pdev);
167         WARN_ON(!conv);
168
169         if (!select)
170                 select = s3c_adc_default_select;
171
172         if (!conv || !pdev)
173                 return ERR_PTR(-EINVAL);
174
175         client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
176         if (!client) {
177                 dev_err(&pdev->dev, "no memory for adc client\n");
178                 return ERR_PTR(-ENOMEM);
179         }
180
181         client->pdev = pdev;
182         client->is_ts = is_ts;
183         client->select_cb = select;
184         client->convert_cb = conv;
185
186         return client;
187 }
188 EXPORT_SYMBOL_GPL(s3c_adc_register);
189
190 void s3c_adc_release(struct s3c_adc_client *client)
191 {
192         /* We should really check that nothing is in progress. */
193         if (adc_dev->cur == client)
194                 adc_dev->cur = NULL;
195         if (adc_dev->ts_pend == client)
196                 adc_dev->ts_pend = NULL;
197         else {
198                 struct list_head *p, *n;
199                 struct s3c_adc_client *tmp;
200
201                 list_for_each_safe(p, n, &adc_pending) {
202                         tmp = list_entry(p, struct s3c_adc_client, pend);
203                         if (tmp == client)
204                                 list_del(&tmp->pend);
205                 }
206         }
207
208         if (adc_dev->cur == NULL)
209                 s3c_adc_try(adc_dev);
210         kfree(client);
211 }
212 EXPORT_SYMBOL_GPL(s3c_adc_release);
213
214 static irqreturn_t s3c_adc_irq(int irq, void *pw)
215 {
216         struct adc_device *adc = pw;
217         struct s3c_adc_client *client = adc->cur;
218         unsigned long flags;
219         unsigned data0, data1;
220
221         if (!client) {
222                 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
223                 return IRQ_HANDLED;
224         }
225
226         data0 = readl(adc->regs + S3C2410_ADCDAT0);
227         data1 = readl(adc->regs + S3C2410_ADCDAT1);
228         adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
229
230         (client->convert_cb)(data0 & 0x3ff, data1 & 0x3ff);
231
232         if (--client->nr_samples > 0) {
233                 /* fire another conversion for this */
234
235                 client->select_cb(1);
236                 s3c_adc_convert(adc);
237         } else {
238                 local_irq_save(flags);
239                 (client->select_cb)(0);
240                 adc->cur = NULL;
241
242                 s3c_adc_try(adc);
243                 local_irq_restore(flags);
244         }
245
246         return IRQ_HANDLED;
247 }
248
249 static int s3c_adc_probe(struct platform_device *pdev)
250 {
251         struct device *dev = &pdev->dev;
252         struct adc_device *adc;
253         struct resource *regs;
254         int ret;
255
256         adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
257         if (adc == NULL) {
258                 dev_err(dev, "failed to allocate adc_device\n");
259                 return -ENOMEM;
260         }
261
262         adc->pdev = pdev;
263         adc->prescale = S3C2410_ADCCON_PRSCVL(49);
264
265         adc->irq = platform_get_irq(pdev, 1);
266         if (adc->irq <= 0) {
267                 dev_err(dev, "failed to get adc irq\n");
268                 ret = -ENOENT;
269                 goto err_alloc;
270         }
271
272         ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
273         if (ret < 0) {
274                 dev_err(dev, "failed to attach adc irq\n");
275                 goto err_alloc;
276         }
277
278         adc->clk = clk_get(dev, "adc");
279         if (IS_ERR(adc->clk)) {
280                 dev_err(dev, "failed to get adc clock\n");
281                 ret = PTR_ERR(adc->clk);
282                 goto err_irq;
283         }
284
285         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
286         if (!regs) {
287                 dev_err(dev, "failed to find registers\n");
288                 ret = -ENXIO;
289                 goto err_clk;
290         }
291
292         adc->regs = ioremap(regs->start, resource_size(regs));
293         if (!adc->regs) {
294                 dev_err(dev, "failed to map registers\n");
295                 ret = -ENXIO;
296                 goto err_clk;
297         }
298
299         clk_enable(adc->clk);
300
301         writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
302                adc->regs + S3C2410_ADCCON);
303
304         dev_info(dev, "attached adc driver\n");
305
306         platform_set_drvdata(pdev, adc);
307         adc_dev = adc;
308
309         return 0;
310
311  err_clk:
312         clk_put(adc->clk);
313
314  err_irq:
315         free_irq(adc->irq, adc);
316
317  err_alloc:
318         kfree(adc);
319         return ret;
320 }
321
322 static int s3c_adc_remove(struct platform_device *pdev)
323 {
324         struct adc_device *adc = platform_get_drvdata(pdev);
325
326         iounmap(adc->regs);
327         free_irq(adc->irq, adc);
328         clk_disable(adc->clk);
329         clk_put(adc->clk);
330         kfree(adc);
331
332         return 0;
333 }
334
335 #ifdef CONFIG_PM
336 static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
337 {
338         struct adc_device *adc = platform_get_drvdata(pdev);
339         u32 con;
340
341         con = readl(adc->regs + S3C2410_ADCCON);
342         con |= S3C2410_ADCCON_STDBM;
343         writel(con, adc->regs + S3C2410_ADCCON);
344
345         clk_disable(adc->clk);
346
347         return 0;
348 }
349
350 static int s3c_adc_resume(struct platform_device *pdev)
351 {
352         struct adc_device *adc = platform_get_drvdata(pdev);
353
354         clk_enable(adc->clk);
355
356         writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
357                adc->regs + S3C2410_ADCCON);
358
359         return 0;
360 }
361
362 #else
363 #define s3c_adc_suspend NULL
364 #define s3c_adc_resume NULL
365 #endif
366
367 static struct platform_driver s3c_adc_driver = {
368         .driver         = {
369                 .name   = "s3c24xx-adc",
370                 .owner  = THIS_MODULE,
371         },
372         .probe          = s3c_adc_probe,
373         .remove         = __devexit_p(s3c_adc_remove),
374         .suspend        = s3c_adc_suspend,
375         .resume         = s3c_adc_resume,
376 };
377
378 static int __init adc_init(void)
379 {
380         int ret;
381
382         ret = platform_driver_register(&s3c_adc_driver);
383         if (ret)
384                 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
385
386         return ret;
387 }
388
389 arch_initcall(adc_init);