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_ADCV11, /* S3C2443 */
45 TYPE_ADCV12, /* S3C2416, S3C2450 */
46 TYPE_ADCV2, /* S3C64XX */
47 TYPE_ADCV3, /* S5PV210, S5PC110, EXYNOS4210 */
50 struct s3c_adc_client {
51 struct platform_device *pdev;
52 struct list_head pend;
53 wait_queue_head_t *wait;
55 unsigned int nr_samples;
58 unsigned char channel;
60 void (*select_cb)(struct s3c_adc_client *c, unsigned selected);
61 void (*convert_cb)(struct s3c_adc_client *c,
62 unsigned val1, unsigned val2,
63 unsigned *samples_left);
67 struct platform_device *pdev;
68 struct platform_device *owner;
70 struct s3c_adc_client *cur;
71 struct s3c_adc_client *ts_pend;
75 unsigned int prescale;
78 struct regulator *vdd;
81 static struct adc_device *adc_dev;
83 static LIST_HEAD(adc_pending); /* protected by adc_device.lock */
85 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
87 static inline void s3c_adc_convert(struct adc_device *adc)
89 unsigned con = readl(adc->regs + S3C2410_ADCCON);
91 con |= S3C2410_ADCCON_ENABLE_START;
92 writel(con, adc->regs + S3C2410_ADCCON);
95 static inline void s3c_adc_select(struct adc_device *adc,
96 struct s3c_adc_client *client)
98 unsigned con = readl(adc->regs + S3C2410_ADCCON);
99 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
101 client->select_cb(client, 1);
103 if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV2)
104 con &= ~S3C2410_ADCCON_MUXMASK;
105 con &= ~S3C2410_ADCCON_STDBM;
106 con &= ~S3C2410_ADCCON_STARTMASK;
108 if (!client->is_ts) {
109 if (cpu == TYPE_ADCV3)
110 writel(client->channel & 0xf, adc->regs + S5P_ADCMUX);
111 else if (cpu == TYPE_ADCV11 || cpu == TYPE_ADCV12)
112 writel(client->channel & 0xf,
113 adc->regs + S3C2443_ADCMUX);
115 con |= S3C2410_ADCCON_SELMUX(client->channel);
118 writel(con, adc->regs + S3C2410_ADCCON);
121 static void s3c_adc_dbgshow(struct adc_device *adc)
123 adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
124 readl(adc->regs + S3C2410_ADCCON),
125 readl(adc->regs + S3C2410_ADCTSC),
126 readl(adc->regs + S3C2410_ADCDLY));
129 static void s3c_adc_try(struct adc_device *adc)
131 struct s3c_adc_client *next = adc->ts_pend;
133 if (!next && !list_empty(&adc_pending)) {
134 next = list_first_entry(&adc_pending,
135 struct s3c_adc_client, pend);
136 list_del(&next->pend);
141 adc_dbg(adc, "new client is %p\n", next);
143 s3c_adc_select(adc, next);
144 s3c_adc_convert(adc);
145 s3c_adc_dbgshow(adc);
149 int s3c_adc_start(struct s3c_adc_client *client,
150 unsigned int channel, unsigned int nr_samples)
152 struct adc_device *adc = adc_dev;
156 printk(KERN_ERR "%s: failed to find adc\n", __func__);
160 spin_lock_irqsave(&adc->lock, flags);
162 if (client->is_ts && adc->ts_pend) {
163 spin_unlock_irqrestore(&adc->lock, flags);
167 client->channel = channel;
168 client->nr_samples = nr_samples;
171 adc->ts_pend = client;
173 list_add_tail(&client->pend, &adc_pending);
178 spin_unlock_irqrestore(&adc->lock, flags);
182 EXPORT_SYMBOL_GPL(s3c_adc_start);
184 static void s3c_convert_done(struct s3c_adc_client *client,
185 unsigned v, unsigned u, unsigned *left)
188 wake_up(client->wait);
191 int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
193 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
196 client->convert_cb = s3c_convert_done;
197 client->wait = &wake;
200 ret = s3c_adc_start(client, ch, 1);
204 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
205 if (client->result < 0) {
210 client->convert_cb = NULL;
211 return client->result;
216 EXPORT_SYMBOL_GPL(s3c_adc_read);
218 static void s3c_adc_default_select(struct s3c_adc_client *client,
223 struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
224 void (*select)(struct s3c_adc_client *client,
225 unsigned int selected),
226 void (*conv)(struct s3c_adc_client *client,
227 unsigned d0, unsigned d1,
228 unsigned *samples_left),
231 struct s3c_adc_client *client;
236 select = s3c_adc_default_select;
239 return ERR_PTR(-EINVAL);
241 client = kzalloc(sizeof(*client), GFP_KERNEL);
243 return ERR_PTR(-ENOMEM);
246 client->is_ts = is_ts;
247 client->select_cb = select;
248 client->convert_cb = conv;
252 EXPORT_SYMBOL_GPL(s3c_adc_register);
254 void s3c_adc_release(struct s3c_adc_client *client)
258 spin_lock_irqsave(&adc_dev->lock, flags);
260 /* We should really check that nothing is in progress. */
261 if (adc_dev->cur == client)
263 if (adc_dev->ts_pend == client)
264 adc_dev->ts_pend = NULL;
266 struct list_head *p, *n;
267 struct s3c_adc_client *tmp;
269 list_for_each_safe(p, n, &adc_pending) {
270 tmp = list_entry(p, struct s3c_adc_client, pend);
272 list_del(&tmp->pend);
276 if (adc_dev->cur == NULL)
277 s3c_adc_try(adc_dev);
279 spin_unlock_irqrestore(&adc_dev->lock, flags);
282 EXPORT_SYMBOL_GPL(s3c_adc_release);
284 static irqreturn_t s3c_adc_irq(int irq, void *pw)
286 struct adc_device *adc = pw;
287 struct s3c_adc_client *client = adc->cur;
288 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
289 unsigned data0, data1;
292 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
296 data0 = readl(adc->regs + S3C2410_ADCDAT0);
297 data1 = readl(adc->regs + S3C2410_ADCDAT1);
298 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
300 client->nr_samples--;
302 if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV11) {
306 /* S3C2416/S3C64XX/S5P ADC resolution is 12-bit */
311 if (client->convert_cb)
312 (client->convert_cb)(client, data0, data1, &client->nr_samples);
314 if (client->nr_samples > 0) {
315 /* fire another conversion for this */
317 client->select_cb(client, 1);
318 s3c_adc_convert(adc);
320 spin_lock(&adc->lock);
321 (client->select_cb)(client, 0);
325 spin_unlock(&adc->lock);
329 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3) {
330 /* Clear ADC interrupt */
331 writel(0, adc->regs + S3C64XX_ADCCLRINT);
336 static int s3c_adc_probe(struct platform_device *pdev)
338 struct device *dev = &pdev->dev;
339 struct adc_device *adc;
340 struct resource *regs;
341 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
345 adc = devm_kzalloc(dev, sizeof(*adc), GFP_KERNEL);
349 spin_lock_init(&adc->lock);
352 adc->prescale = S3C2410_ADCCON_PRSCVL(49);
354 adc->vdd = devm_regulator_get(dev, "vdd");
355 if (IS_ERR(adc->vdd)) {
356 dev_err(dev, "operating without regulator \"vdd\" .\n");
357 return PTR_ERR(adc->vdd);
360 adc->irq = platform_get_irq(pdev, 1);
362 dev_err(dev, "failed to get adc irq\n");
366 ret = devm_request_irq(dev, adc->irq, s3c_adc_irq, 0, dev_name(dev),
369 dev_err(dev, "failed to attach adc irq\n");
373 adc->clk = devm_clk_get(dev, "adc");
374 if (IS_ERR(adc->clk)) {
375 dev_err(dev, "failed to get adc clock\n");
376 return PTR_ERR(adc->clk);
379 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
380 adc->regs = devm_ioremap_resource(dev, regs);
381 if (IS_ERR(adc->regs))
382 return PTR_ERR(adc->regs);
384 ret = regulator_enable(adc->vdd);
388 clk_prepare_enable(adc->clk);
390 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
392 /* Enable 12-bit ADC resolution */
393 if (cpu == TYPE_ADCV12)
394 tmp |= S3C2416_ADCCON_RESSEL;
395 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
396 tmp |= S3C64XX_ADCCON_RESSEL;
398 writel(tmp, adc->regs + S3C2410_ADCCON);
400 dev_info(dev, "attached adc driver\n");
402 platform_set_drvdata(pdev, adc);
408 static int s3c_adc_remove(struct platform_device *pdev)
410 struct adc_device *adc = platform_get_drvdata(pdev);
412 clk_disable_unprepare(adc->clk);
413 regulator_disable(adc->vdd);
419 static int s3c_adc_suspend(struct device *dev)
421 struct platform_device *pdev = to_platform_device(dev);
422 struct adc_device *adc = platform_get_drvdata(pdev);
426 spin_lock_irqsave(&adc->lock, flags);
428 con = readl(adc->regs + S3C2410_ADCCON);
429 con |= S3C2410_ADCCON_STDBM;
430 writel(con, adc->regs + S3C2410_ADCCON);
432 disable_irq(adc->irq);
433 spin_unlock_irqrestore(&adc->lock, flags);
434 clk_disable(adc->clk);
435 regulator_disable(adc->vdd);
440 static int s3c_adc_resume(struct device *dev)
442 struct platform_device *pdev = to_platform_device(dev);
443 struct adc_device *adc = platform_get_drvdata(pdev);
444 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
448 ret = regulator_enable(adc->vdd);
451 clk_enable(adc->clk);
452 enable_irq(adc->irq);
454 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
456 /* Enable 12-bit ADC resolution */
457 if (cpu == TYPE_ADCV12)
458 tmp |= S3C2416_ADCCON_RESSEL;
459 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
460 tmp |= S3C64XX_ADCCON_RESSEL;
462 writel(tmp, adc->regs + S3C2410_ADCCON);
468 #define s3c_adc_suspend NULL
469 #define s3c_adc_resume NULL
472 static const struct platform_device_id s3c_adc_driver_ids[] = {
474 .name = "s3c24xx-adc",
475 .driver_data = TYPE_ADCV1,
477 .name = "s3c2443-adc",
478 .driver_data = TYPE_ADCV11,
480 .name = "s3c2416-adc",
481 .driver_data = TYPE_ADCV12,
483 .name = "s3c64xx-adc",
484 .driver_data = TYPE_ADCV2,
486 .name = "samsung-adc-v3",
487 .driver_data = TYPE_ADCV3,
491 MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
493 static const struct dev_pm_ops adc_pm_ops = {
494 .suspend = s3c_adc_suspend,
495 .resume = s3c_adc_resume,
498 static struct platform_driver s3c_adc_driver = {
499 .id_table = s3c_adc_driver_ids,
504 .probe = s3c_adc_probe,
505 .remove = s3c_adc_remove,
508 static int __init adc_init(void)
512 ret = platform_driver_register(&s3c_adc_driver);
514 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
519 module_init(adc_init);