1 /* arch/arm/plat-samsung/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 * Samsung 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/slab.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
22 #include <linux/interrupt.h>
24 #include <linux/regulator/consumer.h>
26 #include <plat/regs-adc.h>
29 /* This driver is designed to control the usage of the ADC block between
30 * the touchscreen and any other drivers that may need to use it, such as
33 * Priority will be given to the touchscreen driver, but as this itself is
34 * rate limited it should not starve other requests which are processed in
35 * order that they are received.
37 * Each user registers to get a client block which uniquely identifies it
38 * and stores information such as the necessary functions to callback when
43 TYPE_ADCV1, /* S3C24XX */
44 TYPE_ADCV2, /* S3C64XX, S5P64X0, S5PC100 */
45 TYPE_ADCV3, /* S5PV210, S5PC110, EXYNOS4210 */
48 struct s3c_adc_client {
49 struct platform_device *pdev;
50 struct list_head pend;
51 wait_queue_head_t *wait;
53 unsigned int nr_samples;
56 unsigned char channel;
58 void (*select_cb)(struct s3c_adc_client *c, unsigned selected);
59 void (*convert_cb)(struct s3c_adc_client *c,
60 unsigned val1, unsigned val2,
61 unsigned *samples_left);
65 struct platform_device *pdev;
66 struct platform_device *owner;
68 struct s3c_adc_client *cur;
69 struct s3c_adc_client *ts_pend;
73 unsigned int prescale;
76 struct regulator *vdd;
79 static struct adc_device *adc_dev;
81 static LIST_HEAD(adc_pending); /* protected by adc_device.lock */
83 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
85 static inline void s3c_adc_convert(struct adc_device *adc)
87 unsigned con = readl(adc->regs + S3C2410_ADCCON);
89 con |= S3C2410_ADCCON_ENABLE_START;
90 writel(con, adc->regs + S3C2410_ADCCON);
93 static inline void s3c_adc_select(struct adc_device *adc,
94 struct s3c_adc_client *client)
96 unsigned con = readl(adc->regs + S3C2410_ADCCON);
97 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
99 client->select_cb(client, 1);
101 con &= ~S3C2410_ADCCON_MUXMASK;
102 con &= ~S3C2410_ADCCON_STDBM;
103 con &= ~S3C2410_ADCCON_STARTMASK;
105 if (!client->is_ts) {
106 if (cpu == TYPE_ADCV3)
107 writel(client->channel & 0xf, adc->regs + S5P_ADCMUX);
109 con |= S3C2410_ADCCON_SELMUX(client->channel);
112 writel(con, adc->regs + S3C2410_ADCCON);
115 static void s3c_adc_dbgshow(struct adc_device *adc)
117 adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
118 readl(adc->regs + S3C2410_ADCCON),
119 readl(adc->regs + S3C2410_ADCTSC),
120 readl(adc->regs + S3C2410_ADCDLY));
123 static void s3c_adc_try(struct adc_device *adc)
125 struct s3c_adc_client *next = adc->ts_pend;
127 if (!next && !list_empty(&adc_pending)) {
128 next = list_first_entry(&adc_pending,
129 struct s3c_adc_client, pend);
130 list_del(&next->pend);
135 adc_dbg(adc, "new client is %p\n", next);
137 s3c_adc_select(adc, next);
138 s3c_adc_convert(adc);
139 s3c_adc_dbgshow(adc);
143 int s3c_adc_start(struct s3c_adc_client *client,
144 unsigned int channel, unsigned int nr_samples)
146 struct adc_device *adc = adc_dev;
150 printk(KERN_ERR "%s: failed to find adc\n", __func__);
154 if (client->is_ts && adc->ts_pend)
157 spin_lock_irqsave(&adc->lock, flags);
159 client->channel = channel;
160 client->nr_samples = nr_samples;
163 adc->ts_pend = client;
165 list_add_tail(&client->pend, &adc_pending);
170 spin_unlock_irqrestore(&adc->lock, flags);
174 EXPORT_SYMBOL_GPL(s3c_adc_start);
176 static void s3c_convert_done(struct s3c_adc_client *client,
177 unsigned v, unsigned u, unsigned *left)
180 wake_up(client->wait);
183 int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
185 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
188 client->convert_cb = s3c_convert_done;
189 client->wait = &wake;
192 ret = s3c_adc_start(client, ch, 1);
196 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
197 if (client->result < 0) {
202 client->convert_cb = NULL;
203 return client->result;
208 EXPORT_SYMBOL_GPL(s3c_adc_read);
210 static void s3c_adc_default_select(struct s3c_adc_client *client,
215 struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
216 void (*select)(struct s3c_adc_client *client,
217 unsigned int selected),
218 void (*conv)(struct s3c_adc_client *client,
219 unsigned d0, unsigned d1,
220 unsigned *samples_left),
223 struct s3c_adc_client *client;
228 select = s3c_adc_default_select;
231 return ERR_PTR(-EINVAL);
233 client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
235 dev_err(&pdev->dev, "no memory for adc client\n");
236 return ERR_PTR(-ENOMEM);
240 client->is_ts = is_ts;
241 client->select_cb = select;
242 client->convert_cb = conv;
246 EXPORT_SYMBOL_GPL(s3c_adc_register);
248 void s3c_adc_release(struct s3c_adc_client *client)
252 spin_lock_irqsave(&adc_dev->lock, flags);
254 /* We should really check that nothing is in progress. */
255 if (adc_dev->cur == client)
257 if (adc_dev->ts_pend == client)
258 adc_dev->ts_pend = NULL;
260 struct list_head *p, *n;
261 struct s3c_adc_client *tmp;
263 list_for_each_safe(p, n, &adc_pending) {
264 tmp = list_entry(p, struct s3c_adc_client, pend);
266 list_del(&tmp->pend);
270 if (adc_dev->cur == NULL)
271 s3c_adc_try(adc_dev);
273 spin_unlock_irqrestore(&adc_dev->lock, flags);
276 EXPORT_SYMBOL_GPL(s3c_adc_release);
278 static irqreturn_t s3c_adc_irq(int irq, void *pw)
280 struct adc_device *adc = pw;
281 struct s3c_adc_client *client = adc->cur;
282 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
283 unsigned data0, data1;
286 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
290 data0 = readl(adc->regs + S3C2410_ADCDAT0);
291 data1 = readl(adc->regs + S3C2410_ADCDAT1);
292 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
294 client->nr_samples--;
296 if (cpu != TYPE_ADCV1) {
297 /* S3C64XX/S5P ADC resolution is 12-bit */
305 if (client->convert_cb)
306 (client->convert_cb)(client, data0, data1, &client->nr_samples);
308 if (client->nr_samples > 0) {
309 /* fire another conversion for this */
311 client->select_cb(client, 1);
312 s3c_adc_convert(adc);
314 spin_lock(&adc->lock);
315 (client->select_cb)(client, 0);
319 spin_unlock(&adc->lock);
323 if (cpu != TYPE_ADCV1) {
324 /* Clear ADC interrupt */
325 writel(0, adc->regs + S3C64XX_ADCCLRINT);
330 static int s3c_adc_probe(struct platform_device *pdev)
332 struct device *dev = &pdev->dev;
333 struct adc_device *adc;
334 struct resource *regs;
338 adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
340 dev_err(dev, "failed to allocate adc_device\n");
344 spin_lock_init(&adc->lock);
347 adc->prescale = S3C2410_ADCCON_PRSCVL(49);
349 adc->vdd = regulator_get(dev, "vdd");
350 if (IS_ERR(adc->vdd)) {
351 dev_err(dev, "operating without regulator \"vdd\" .\n");
352 ret = PTR_ERR(adc->vdd);
356 adc->irq = platform_get_irq(pdev, 1);
358 dev_err(dev, "failed to get adc irq\n");
363 ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
365 dev_err(dev, "failed to attach adc irq\n");
369 adc->clk = clk_get(dev, "adc");
370 if (IS_ERR(adc->clk)) {
371 dev_err(dev, "failed to get adc clock\n");
372 ret = PTR_ERR(adc->clk);
376 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
378 dev_err(dev, "failed to find registers\n");
383 adc->regs = ioremap(regs->start, resource_size(regs));
385 dev_err(dev, "failed to map registers\n");
390 ret = regulator_enable(adc->vdd);
394 clk_enable(adc->clk);
396 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
397 if (platform_get_device_id(pdev)->driver_data != TYPE_ADCV1) {
398 /* Enable 12-bit ADC resolution */
399 tmp |= S3C64XX_ADCCON_RESSEL;
401 writel(tmp, adc->regs + S3C2410_ADCCON);
403 dev_info(dev, "attached adc driver\n");
405 platform_set_drvdata(pdev, adc);
416 free_irq(adc->irq, adc);
418 regulator_put(adc->vdd);
424 static int __devexit s3c_adc_remove(struct platform_device *pdev)
426 struct adc_device *adc = platform_get_drvdata(pdev);
429 free_irq(adc->irq, adc);
430 clk_disable(adc->clk);
431 regulator_disable(adc->vdd);
432 regulator_put(adc->vdd);
440 static int s3c_adc_suspend(struct device *dev)
442 struct platform_device *pdev = container_of(dev,
443 struct platform_device, dev);
444 struct adc_device *adc = platform_get_drvdata(pdev);
448 spin_lock_irqsave(&adc->lock, flags);
450 con = readl(adc->regs + S3C2410_ADCCON);
451 con |= S3C2410_ADCCON_STDBM;
452 writel(con, adc->regs + S3C2410_ADCCON);
454 disable_irq(adc->irq);
455 spin_unlock_irqrestore(&adc->lock, flags);
456 clk_disable(adc->clk);
457 regulator_disable(adc->vdd);
462 static int s3c_adc_resume(struct device *dev)
464 struct platform_device *pdev = container_of(dev,
465 struct platform_device, dev);
466 struct adc_device *adc = platform_get_drvdata(pdev);
470 ret = regulator_enable(adc->vdd);
473 clk_enable(adc->clk);
474 enable_irq(adc->irq);
476 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
477 /* Enable 12-bit ADC resolution */
478 if (platform_get_device_id(pdev)->driver_data != TYPE_ADCV1)
479 tmp |= S3C64XX_ADCCON_RESSEL;
480 writel(tmp, adc->regs + S3C2410_ADCCON);
486 #define s3c_adc_suspend NULL
487 #define s3c_adc_resume NULL
490 static struct platform_device_id s3c_adc_driver_ids[] = {
492 .name = "s3c24xx-adc",
493 .driver_data = TYPE_ADCV1,
495 .name = "s3c64xx-adc",
496 .driver_data = TYPE_ADCV2,
498 .name = "samsung-adc-v3",
499 .driver_data = TYPE_ADCV3,
503 MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
505 static const struct dev_pm_ops adc_pm_ops = {
506 .suspend = s3c_adc_suspend,
507 .resume = s3c_adc_resume,
510 static struct platform_driver s3c_adc_driver = {
511 .id_table = s3c_adc_driver_ids,
514 .owner = THIS_MODULE,
517 .probe = s3c_adc_probe,
518 .remove = __devexit_p(s3c_adc_remove),
521 static int __init adc_init(void)
525 ret = platform_driver_register(&s3c_adc_driver);
527 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
532 module_init(adc_init);