include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / arch / arm / plat-samsung / adc.c
1 /* arch/arm/plat-samsung/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  * Samsung 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/sched.h>
18 #include <linux/list.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24
25 #include <plat/regs-adc.h>
26 #include <plat/adc.h>
27
28 /* This driver is designed to control the usage of the ADC block between
29  * the touchscreen and any other drivers that may need to use it, such as
30  * the hwmon driver.
31  *
32  * Priority will be given to the touchscreen driver, but as this itself is
33  * rate limited it should not starve other requests which are processed in
34  * order that they are received.
35  *
36  * Each user registers to get a client block which uniquely identifies it
37  * and stores information such as the necessary functions to callback when
38  * action is required.
39  */
40
41 enum s3c_cpu_type {
42         TYPE_S3C24XX,
43         TYPE_S3C64XX
44 };
45
46 struct s3c_adc_client {
47         struct platform_device  *pdev;
48         struct list_head         pend;
49         wait_queue_head_t       *wait;
50
51         unsigned int             nr_samples;
52         int                      result;
53         unsigned char            is_ts;
54         unsigned char            channel;
55
56         void    (*select_cb)(struct s3c_adc_client *c, unsigned selected);
57         void    (*convert_cb)(struct s3c_adc_client *c,
58                               unsigned val1, unsigned val2,
59                               unsigned *samples_left);
60 };
61
62 struct adc_device {
63         struct platform_device  *pdev;
64         struct platform_device  *owner;
65         struct clk              *clk;
66         struct s3c_adc_client   *cur;
67         struct s3c_adc_client   *ts_pend;
68         void __iomem            *regs;
69
70         unsigned int             prescale;
71
72         int                      irq;
73 };
74
75 static struct adc_device *adc_dev;
76
77 static LIST_HEAD(adc_pending);
78
79 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
80
81 static inline void s3c_adc_convert(struct adc_device *adc)
82 {
83         unsigned con = readl(adc->regs + S3C2410_ADCCON);
84
85         con |= S3C2410_ADCCON_ENABLE_START;
86         writel(con, adc->regs + S3C2410_ADCCON);
87 }
88
89 static inline void s3c_adc_select(struct adc_device *adc,
90                                   struct s3c_adc_client *client)
91 {
92         unsigned con = readl(adc->regs + S3C2410_ADCCON);
93
94         client->select_cb(client, 1);
95
96         con &= ~S3C2410_ADCCON_MUXMASK;
97         con &= ~S3C2410_ADCCON_STDBM;
98         con &= ~S3C2410_ADCCON_STARTMASK;
99
100         if (!client->is_ts)
101                 con |= S3C2410_ADCCON_SELMUX(client->channel);
102
103         writel(con, adc->regs + S3C2410_ADCCON);
104 }
105
106 static void s3c_adc_dbgshow(struct adc_device *adc)
107 {
108         adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
109                 readl(adc->regs + S3C2410_ADCCON),
110                 readl(adc->regs + S3C2410_ADCTSC),
111                 readl(adc->regs + S3C2410_ADCDLY));
112 }
113
114 static void s3c_adc_try(struct adc_device *adc)
115 {
116         struct s3c_adc_client *next = adc->ts_pend;
117
118         if (!next && !list_empty(&adc_pending)) {
119                 next = list_first_entry(&adc_pending,
120                                         struct s3c_adc_client, pend);
121                 list_del(&next->pend);
122         } else
123                 adc->ts_pend = NULL;
124
125         if (next) {
126                 adc_dbg(adc, "new client is %p\n", next);
127                 adc->cur = next;
128                 s3c_adc_select(adc, next);
129                 s3c_adc_convert(adc);
130                 s3c_adc_dbgshow(adc);
131         }
132 }
133
134 int s3c_adc_start(struct s3c_adc_client *client,
135                   unsigned int channel, unsigned int nr_samples)
136 {
137         struct adc_device *adc = adc_dev;
138         unsigned long flags;
139
140         if (!adc) {
141                 printk(KERN_ERR "%s: failed to find adc\n", __func__);
142                 return -EINVAL;
143         }
144
145         if (client->is_ts && adc->ts_pend)
146                 return -EAGAIN;
147
148         local_irq_save(flags);
149
150         client->channel = channel;
151         client->nr_samples = nr_samples;
152
153         if (client->is_ts)
154                 adc->ts_pend = client;
155         else
156                 list_add_tail(&client->pend, &adc_pending);
157
158         if (!adc->cur)
159                 s3c_adc_try(adc);
160         local_irq_restore(flags);
161
162         return 0;
163 }
164 EXPORT_SYMBOL_GPL(s3c_adc_start);
165
166 static void s3c_convert_done(struct s3c_adc_client *client,
167                              unsigned v, unsigned u, unsigned *left)
168 {
169         client->result = v;
170         wake_up(client->wait);
171 }
172
173 int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
174 {
175         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
176         int ret;
177
178         client->convert_cb = s3c_convert_done;
179         client->wait = &wake;
180         client->result = -1;
181
182         ret = s3c_adc_start(client, ch, 1);
183         if (ret < 0)
184                 goto err;
185
186         ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
187         if (client->result < 0) {
188                 ret = -ETIMEDOUT;
189                 goto err;
190         }
191
192         client->convert_cb = NULL;
193         return client->result;
194
195 err:
196         return ret;
197 }
198 EXPORT_SYMBOL_GPL(s3c_adc_read);
199
200 static void s3c_adc_default_select(struct s3c_adc_client *client,
201                                    unsigned select)
202 {
203 }
204
205 struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
206                                         void (*select)(struct s3c_adc_client *client,
207                                                        unsigned int selected),
208                                         void (*conv)(struct s3c_adc_client *client,
209                                                      unsigned d0, unsigned d1,
210                                                      unsigned *samples_left),
211                                         unsigned int is_ts)
212 {
213         struct s3c_adc_client *client;
214
215         WARN_ON(!pdev);
216
217         if (!select)
218                 select = s3c_adc_default_select;
219
220         if (!pdev)
221                 return ERR_PTR(-EINVAL);
222
223         client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
224         if (!client) {
225                 dev_err(&pdev->dev, "no memory for adc client\n");
226                 return ERR_PTR(-ENOMEM);
227         }
228
229         client->pdev = pdev;
230         client->is_ts = is_ts;
231         client->select_cb = select;
232         client->convert_cb = conv;
233
234         return client;
235 }
236 EXPORT_SYMBOL_GPL(s3c_adc_register);
237
238 void s3c_adc_release(struct s3c_adc_client *client)
239 {
240         /* We should really check that nothing is in progress. */
241         if (adc_dev->cur == client)
242                 adc_dev->cur = NULL;
243         if (adc_dev->ts_pend == client)
244                 adc_dev->ts_pend = NULL;
245         else {
246                 struct list_head *p, *n;
247                 struct s3c_adc_client *tmp;
248
249                 list_for_each_safe(p, n, &adc_pending) {
250                         tmp = list_entry(p, struct s3c_adc_client, pend);
251                         if (tmp == client)
252                                 list_del(&tmp->pend);
253                 }
254         }
255
256         if (adc_dev->cur == NULL)
257                 s3c_adc_try(adc_dev);
258         kfree(client);
259 }
260 EXPORT_SYMBOL_GPL(s3c_adc_release);
261
262 static irqreturn_t s3c_adc_irq(int irq, void *pw)
263 {
264         struct adc_device *adc = pw;
265         struct s3c_adc_client *client = adc->cur;
266         enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
267         unsigned long flags;
268         unsigned data0, data1;
269
270         if (!client) {
271                 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
272                 goto exit;
273         }
274
275         data0 = readl(adc->regs + S3C2410_ADCDAT0);
276         data1 = readl(adc->regs + S3C2410_ADCDAT1);
277         adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
278
279         client->nr_samples--;
280
281         if (cpu == TYPE_S3C64XX) {
282                 /* S3C64XX ADC resolution is 12-bit */
283                 data0 &= 0xfff;
284                 data1 &= 0xfff;
285         } else {
286                 data0 &= 0x3ff;
287                 data1 &= 0x3ff;
288         }
289
290         if (client->convert_cb)
291                 (client->convert_cb)(client, data0, data1, &client->nr_samples);
292
293         if (client->nr_samples > 0) {
294                 /* fire another conversion for this */
295
296                 client->select_cb(client, 1);
297                 s3c_adc_convert(adc);
298         } else {
299                 local_irq_save(flags);
300                 (client->select_cb)(client, 0);
301                 adc->cur = NULL;
302
303                 s3c_adc_try(adc);
304                 local_irq_restore(flags);
305         }
306
307 exit:
308         if (cpu == TYPE_S3C64XX) {
309                 /* Clear ADC interrupt */
310                 writel(0, adc->regs + S3C64XX_ADCCLRINT);
311         }
312         return IRQ_HANDLED;
313 }
314
315 static int s3c_adc_probe(struct platform_device *pdev)
316 {
317         struct device *dev = &pdev->dev;
318         struct adc_device *adc;
319         struct resource *regs;
320         int ret;
321         unsigned tmp;
322
323         adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
324         if (adc == NULL) {
325                 dev_err(dev, "failed to allocate adc_device\n");
326                 return -ENOMEM;
327         }
328
329         adc->pdev = pdev;
330         adc->prescale = S3C2410_ADCCON_PRSCVL(49);
331
332         adc->irq = platform_get_irq(pdev, 1);
333         if (adc->irq <= 0) {
334                 dev_err(dev, "failed to get adc irq\n");
335                 ret = -ENOENT;
336                 goto err_alloc;
337         }
338
339         ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
340         if (ret < 0) {
341                 dev_err(dev, "failed to attach adc irq\n");
342                 goto err_alloc;
343         }
344
345         adc->clk = clk_get(dev, "adc");
346         if (IS_ERR(adc->clk)) {
347                 dev_err(dev, "failed to get adc clock\n");
348                 ret = PTR_ERR(adc->clk);
349                 goto err_irq;
350         }
351
352         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
353         if (!regs) {
354                 dev_err(dev, "failed to find registers\n");
355                 ret = -ENXIO;
356                 goto err_clk;
357         }
358
359         adc->regs = ioremap(regs->start, resource_size(regs));
360         if (!adc->regs) {
361                 dev_err(dev, "failed to map registers\n");
362                 ret = -ENXIO;
363                 goto err_clk;
364         }
365
366         clk_enable(adc->clk);
367
368         tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
369         if (platform_get_device_id(pdev)->driver_data == TYPE_S3C64XX) {
370                 /* Enable 12-bit ADC resolution */
371                 tmp |= S3C64XX_ADCCON_RESSEL;
372         }
373         writel(tmp, adc->regs + S3C2410_ADCCON);
374
375         dev_info(dev, "attached adc driver\n");
376
377         platform_set_drvdata(pdev, adc);
378         adc_dev = adc;
379
380         return 0;
381
382  err_clk:
383         clk_put(adc->clk);
384
385  err_irq:
386         free_irq(adc->irq, adc);
387
388  err_alloc:
389         kfree(adc);
390         return ret;
391 }
392
393 static int __devexit s3c_adc_remove(struct platform_device *pdev)
394 {
395         struct adc_device *adc = platform_get_drvdata(pdev);
396
397         iounmap(adc->regs);
398         free_irq(adc->irq, adc);
399         clk_disable(adc->clk);
400         clk_put(adc->clk);
401         kfree(adc);
402
403         return 0;
404 }
405
406 #ifdef CONFIG_PM
407 static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
408 {
409         struct adc_device *adc = platform_get_drvdata(pdev);
410         u32 con;
411
412         con = readl(adc->regs + S3C2410_ADCCON);
413         con |= S3C2410_ADCCON_STDBM;
414         writel(con, adc->regs + S3C2410_ADCCON);
415
416         disable_irq(adc->irq);
417         clk_disable(adc->clk);
418
419         return 0;
420 }
421
422 static int s3c_adc_resume(struct platform_device *pdev)
423 {
424         struct adc_device *adc = platform_get_drvdata(pdev);
425
426         clk_enable(adc->clk);
427         enable_irq(adc->irq);
428
429         writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
430                adc->regs + S3C2410_ADCCON);
431
432         return 0;
433 }
434
435 #else
436 #define s3c_adc_suspend NULL
437 #define s3c_adc_resume NULL
438 #endif
439
440 static struct platform_device_id s3c_adc_driver_ids[] = {
441         {
442                 .name           = "s3c24xx-adc",
443                 .driver_data    = TYPE_S3C24XX,
444         }, {
445                 .name           = "s3c64xx-adc",
446                 .driver_data    = TYPE_S3C64XX,
447         },
448         { }
449 };
450 MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
451
452 static struct platform_driver s3c_adc_driver = {
453         .id_table       = s3c_adc_driver_ids,
454         .driver         = {
455                 .name   = "s3c-adc",
456                 .owner  = THIS_MODULE,
457         },
458         .probe          = s3c_adc_probe,
459         .remove         = __devexit_p(s3c_adc_remove),
460         .suspend        = s3c_adc_suspend,
461         .resume         = s3c_adc_resume,
462 };
463
464 static int __init adc_init(void)
465 {
466         int ret;
467
468         ret = platform_driver_register(&s3c_adc_driver);
469         if (ret)
470                 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
471
472         return ret;
473 }
474
475 arch_initcall(adc_init);