ARM: tegra: fix compilation error due to mach/hardware.h removal
[linux-btrfs-devel.git] / arch / arm / plat-samsung / adc.c
blobee8deef1948156d02c01423ca1fc6b8f771451e2
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>
23 #include <linux/io.h>
24 #include <linux/regulator/consumer.h>
26 #include <plat/regs-adc.h>
27 #include <plat/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
31 * the hwmon driver.
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
39 * action is required.
42 enum s3c_cpu_type {
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;
54 int result;
55 unsigned char is_ts;
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);
64 struct adc_device {
65 struct platform_device *pdev;
66 struct platform_device *owner;
67 struct clk *clk;
68 struct s3c_adc_client *cur;
69 struct s3c_adc_client *ts_pend;
70 void __iomem *regs;
71 spinlock_t lock;
73 unsigned int prescale;
75 int irq;
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);
108 else
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);
131 } else
132 adc->ts_pend = NULL;
134 if (next) {
135 adc_dbg(adc, "new client is %p\n", next);
136 adc->cur = 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;
147 unsigned long flags;
149 if (!adc) {
150 printk(KERN_ERR "%s: failed to find adc\n", __func__);
151 return -EINVAL;
154 if (client->is_ts && adc->ts_pend)
155 return -EAGAIN;
157 spin_lock_irqsave(&adc->lock, flags);
159 client->channel = channel;
160 client->nr_samples = nr_samples;
162 if (client->is_ts)
163 adc->ts_pend = client;
164 else
165 list_add_tail(&client->pend, &adc_pending);
167 if (!adc->cur)
168 s3c_adc_try(adc);
170 spin_unlock_irqrestore(&adc->lock, flags);
172 return 0;
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)
179 client->result = v;
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);
186 int ret;
188 client->convert_cb = s3c_convert_done;
189 client->wait = &wake;
190 client->result = -1;
192 ret = s3c_adc_start(client, ch, 1);
193 if (ret < 0)
194 goto err;
196 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
197 if (client->result < 0) {
198 ret = -ETIMEDOUT;
199 goto err;
202 client->convert_cb = NULL;
203 return client->result;
205 err:
206 return ret;
208 EXPORT_SYMBOL_GPL(s3c_adc_read);
210 static void s3c_adc_default_select(struct s3c_adc_client *client,
211 unsigned select)
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),
221 unsigned int is_ts)
223 struct s3c_adc_client *client;
225 WARN_ON(!pdev);
227 if (!select)
228 select = s3c_adc_default_select;
230 if (!pdev)
231 return ERR_PTR(-EINVAL);
233 client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
234 if (!client) {
235 dev_err(&pdev->dev, "no memory for adc client\n");
236 return ERR_PTR(-ENOMEM);
239 client->pdev = pdev;
240 client->is_ts = is_ts;
241 client->select_cb = select;
242 client->convert_cb = conv;
244 return client;
246 EXPORT_SYMBOL_GPL(s3c_adc_register);
248 void s3c_adc_release(struct s3c_adc_client *client)
250 unsigned long flags;
252 spin_lock_irqsave(&adc_dev->lock, flags);
254 /* We should really check that nothing is in progress. */
255 if (adc_dev->cur == client)
256 adc_dev->cur = NULL;
257 if (adc_dev->ts_pend == client)
258 adc_dev->ts_pend = NULL;
259 else {
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);
265 if (tmp == client)
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);
274 kfree(client);
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;
285 if (!client) {
286 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
287 goto exit;
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 */
298 data0 &= 0xfff;
299 data1 &= 0xfff;
300 } else {
301 data0 &= 0x3ff;
302 data1 &= 0x3ff;
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);
313 } else {
314 spin_lock(&adc->lock);
315 (client->select_cb)(client, 0);
316 adc->cur = NULL;
318 s3c_adc_try(adc);
319 spin_unlock(&adc->lock);
322 exit:
323 if (cpu != TYPE_ADCV1) {
324 /* Clear ADC interrupt */
325 writel(0, adc->regs + S3C64XX_ADCCLRINT);
327 return IRQ_HANDLED;
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;
335 int ret;
336 unsigned tmp;
338 adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
339 if (adc == NULL) {
340 dev_err(dev, "failed to allocate adc_device\n");
341 return -ENOMEM;
344 spin_lock_init(&adc->lock);
346 adc->pdev = pdev;
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);
353 goto err_alloc;
356 adc->irq = platform_get_irq(pdev, 1);
357 if (adc->irq <= 0) {
358 dev_err(dev, "failed to get adc irq\n");
359 ret = -ENOENT;
360 goto err_reg;
363 ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
364 if (ret < 0) {
365 dev_err(dev, "failed to attach adc irq\n");
366 goto err_reg;
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);
373 goto err_irq;
376 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
377 if (!regs) {
378 dev_err(dev, "failed to find registers\n");
379 ret = -ENXIO;
380 goto err_clk;
383 adc->regs = ioremap(regs->start, resource_size(regs));
384 if (!adc->regs) {
385 dev_err(dev, "failed to map registers\n");
386 ret = -ENXIO;
387 goto err_clk;
390 ret = regulator_enable(adc->vdd);
391 if (ret)
392 goto err_ioremap;
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);
406 adc_dev = adc;
408 return 0;
410 err_ioremap:
411 iounmap(adc->regs);
412 err_clk:
413 clk_put(adc->clk);
415 err_irq:
416 free_irq(adc->irq, adc);
417 err_reg:
418 regulator_put(adc->vdd);
419 err_alloc:
420 kfree(adc);
421 return ret;
424 static int __devexit s3c_adc_remove(struct platform_device *pdev)
426 struct adc_device *adc = platform_get_drvdata(pdev);
428 iounmap(adc->regs);
429 free_irq(adc->irq, adc);
430 clk_disable(adc->clk);
431 regulator_disable(adc->vdd);
432 regulator_put(adc->vdd);
433 clk_put(adc->clk);
434 kfree(adc);
436 return 0;
439 #ifdef CONFIG_PM
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);
445 unsigned long flags;
446 u32 con;
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);
459 return 0;
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);
467 int ret;
468 unsigned long tmp;
470 ret = regulator_enable(adc->vdd);
471 if (ret)
472 return ret;
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);
482 return 0;
485 #else
486 #define s3c_adc_suspend NULL
487 #define s3c_adc_resume NULL
488 #endif
490 static struct platform_device_id s3c_adc_driver_ids[] = {
492 .name = "s3c24xx-adc",
493 .driver_data = TYPE_ADCV1,
494 }, {
495 .name = "s3c64xx-adc",
496 .driver_data = TYPE_ADCV2,
497 }, {
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,
512 .driver = {
513 .name = "s3c-adc",
514 .owner = THIS_MODULE,
515 .pm = &adc_pm_ops,
517 .probe = s3c_adc_probe,
518 .remove = __devexit_p(s3c_adc_remove),
521 static int __init adc_init(void)
523 int ret;
525 ret = platform_driver_register(&s3c_adc_driver);
526 if (ret)
527 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
529 return ret;
532 module_init(adc_init);