1 /* arch/arm/plat-s3c24xx/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 * S3C24XX 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/list.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/interrupt.h>
23 #include <plat/regs-adc.h>
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
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.
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
39 struct s3c_adc_client
{
40 struct platform_device
*pdev
;
41 struct list_head pend
;
42 wait_queue_head_t
*wait
;
44 unsigned int nr_samples
;
47 unsigned char channel
;
49 void (*select_cb
)(struct s3c_adc_client
*c
, unsigned selected
);
50 void (*convert_cb
)(struct s3c_adc_client
*c
,
51 unsigned val1
, unsigned val2
,
52 unsigned *samples_left
);
56 struct platform_device
*pdev
;
57 struct platform_device
*owner
;
59 struct s3c_adc_client
*cur
;
60 struct s3c_adc_client
*ts_pend
;
63 unsigned int prescale
;
68 static struct adc_device
*adc_dev
;
70 static LIST_HEAD(adc_pending
);
72 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
74 static inline void s3c_adc_convert(struct adc_device
*adc
)
76 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
78 con
|= S3C2410_ADCCON_ENABLE_START
;
79 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
82 static inline void s3c_adc_select(struct adc_device
*adc
,
83 struct s3c_adc_client
*client
)
85 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
87 client
->select_cb(client
, 1);
89 con
&= ~S3C2410_ADCCON_MUXMASK
;
90 con
&= ~S3C2410_ADCCON_STDBM
;
91 con
&= ~S3C2410_ADCCON_STARTMASK
;
94 con
|= S3C2410_ADCCON_SELMUX(client
->channel
);
96 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
99 static void s3c_adc_dbgshow(struct adc_device
*adc
)
101 adc_dbg(adc
, "CON=%08x, TSC=%08x, DLY=%08x\n",
102 readl(adc
->regs
+ S3C2410_ADCCON
),
103 readl(adc
->regs
+ S3C2410_ADCTSC
),
104 readl(adc
->regs
+ S3C2410_ADCDLY
));
107 static void s3c_adc_try(struct adc_device
*adc
)
109 struct s3c_adc_client
*next
= adc
->ts_pend
;
111 if (!next
&& !list_empty(&adc_pending
)) {
112 next
= list_first_entry(&adc_pending
,
113 struct s3c_adc_client
, pend
);
114 list_del(&next
->pend
);
119 adc_dbg(adc
, "new client is %p\n", next
);
121 s3c_adc_select(adc
, next
);
122 s3c_adc_convert(adc
);
123 s3c_adc_dbgshow(adc
);
127 int s3c_adc_start(struct s3c_adc_client
*client
,
128 unsigned int channel
, unsigned int nr_samples
)
130 struct adc_device
*adc
= adc_dev
;
134 printk(KERN_ERR
"%s: failed to find adc\n", __func__
);
138 if (client
->is_ts
&& adc
->ts_pend
)
141 local_irq_save(flags
);
143 client
->channel
= channel
;
144 client
->nr_samples
= nr_samples
;
147 adc
->ts_pend
= client
;
149 list_add_tail(&client
->pend
, &adc_pending
);
153 local_irq_restore(flags
);
157 EXPORT_SYMBOL_GPL(s3c_adc_start
);
159 static void s3c_convert_done(struct s3c_adc_client
*client
,
160 unsigned v
, unsigned u
, unsigned *left
)
163 wake_up(client
->wait
);
166 int s3c_adc_read(struct s3c_adc_client
*client
, unsigned int ch
)
168 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake
);
171 client
->convert_cb
= s3c_convert_done
;
172 client
->wait
= &wake
;
175 ret
= s3c_adc_start(client
, ch
, 1);
179 ret
= wait_event_timeout(wake
, client
->result
>= 0, HZ
/ 2);
180 if (client
->result
< 0) {
185 client
->convert_cb
= NULL
;
186 return client
->result
;
191 EXPORT_SYMBOL_GPL(s3c_adc_convert
);
193 static void s3c_adc_default_select(struct s3c_adc_client
*client
,
198 struct s3c_adc_client
*s3c_adc_register(struct platform_device
*pdev
,
199 void (*select
)(struct s3c_adc_client
*client
,
200 unsigned int selected
),
201 void (*conv
)(struct s3c_adc_client
*client
,
202 unsigned d0
, unsigned d1
,
203 unsigned *samples_left
),
206 struct s3c_adc_client
*client
;
211 select
= s3c_adc_default_select
;
214 return ERR_PTR(-EINVAL
);
216 client
= kzalloc(sizeof(struct s3c_adc_client
), GFP_KERNEL
);
218 dev_err(&pdev
->dev
, "no memory for adc client\n");
219 return ERR_PTR(-ENOMEM
);
223 client
->is_ts
= is_ts
;
224 client
->select_cb
= select
;
225 client
->convert_cb
= conv
;
229 EXPORT_SYMBOL_GPL(s3c_adc_register
);
231 void s3c_adc_release(struct s3c_adc_client
*client
)
233 /* We should really check that nothing is in progress. */
234 if (adc_dev
->cur
== client
)
236 if (adc_dev
->ts_pend
== client
)
237 adc_dev
->ts_pend
= NULL
;
239 struct list_head
*p
, *n
;
240 struct s3c_adc_client
*tmp
;
242 list_for_each_safe(p
, n
, &adc_pending
) {
243 tmp
= list_entry(p
, struct s3c_adc_client
, pend
);
245 list_del(&tmp
->pend
);
249 if (adc_dev
->cur
== NULL
)
250 s3c_adc_try(adc_dev
);
253 EXPORT_SYMBOL_GPL(s3c_adc_release
);
255 static irqreturn_t
s3c_adc_irq(int irq
, void *pw
)
257 struct adc_device
*adc
= pw
;
258 struct s3c_adc_client
*client
= adc
->cur
;
260 unsigned data0
, data1
;
263 dev_warn(&adc
->pdev
->dev
, "%s: no adc pending\n", __func__
);
267 data0
= readl(adc
->regs
+ S3C2410_ADCDAT0
);
268 data1
= readl(adc
->regs
+ S3C2410_ADCDAT1
);
269 adc_dbg(adc
, "read %d: 0x%04x, 0x%04x\n", client
->nr_samples
, data0
, data1
);
271 client
->nr_samples
--;
273 if (client
->convert_cb
)
274 (client
->convert_cb
)(client
, data0
& 0x3ff, data1
& 0x3ff,
275 &client
->nr_samples
);
277 if (client
->nr_samples
> 0) {
278 /* fire another conversion for this */
280 client
->select_cb(client
, 1);
281 s3c_adc_convert(adc
);
283 local_irq_save(flags
);
284 (client
->select_cb
)(client
, 0);
288 local_irq_restore(flags
);
294 static int s3c_adc_probe(struct platform_device
*pdev
)
296 struct device
*dev
= &pdev
->dev
;
297 struct adc_device
*adc
;
298 struct resource
*regs
;
301 adc
= kzalloc(sizeof(struct adc_device
), GFP_KERNEL
);
303 dev_err(dev
, "failed to allocate adc_device\n");
308 adc
->prescale
= S3C2410_ADCCON_PRSCVL(49);
310 adc
->irq
= platform_get_irq(pdev
, 1);
312 dev_err(dev
, "failed to get adc irq\n");
317 ret
= request_irq(adc
->irq
, s3c_adc_irq
, 0, dev_name(dev
), adc
);
319 dev_err(dev
, "failed to attach adc irq\n");
323 adc
->clk
= clk_get(dev
, "adc");
324 if (IS_ERR(adc
->clk
)) {
325 dev_err(dev
, "failed to get adc clock\n");
326 ret
= PTR_ERR(adc
->clk
);
330 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
332 dev_err(dev
, "failed to find registers\n");
337 adc
->regs
= ioremap(regs
->start
, resource_size(regs
));
339 dev_err(dev
, "failed to map registers\n");
344 clk_enable(adc
->clk
);
346 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
347 adc
->regs
+ S3C2410_ADCCON
);
349 dev_info(dev
, "attached adc driver\n");
351 platform_set_drvdata(pdev
, adc
);
360 free_irq(adc
->irq
, adc
);
367 static int s3c_adc_remove(struct platform_device
*pdev
)
369 struct adc_device
*adc
= platform_get_drvdata(pdev
);
372 free_irq(adc
->irq
, adc
);
373 clk_disable(adc
->clk
);
381 static int s3c_adc_suspend(struct platform_device
*pdev
, pm_message_t state
)
383 struct adc_device
*adc
= platform_get_drvdata(pdev
);
386 con
= readl(adc
->regs
+ S3C2410_ADCCON
);
387 con
|= S3C2410_ADCCON_STDBM
;
388 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
390 clk_disable(adc
->clk
);
395 static int s3c_adc_resume(struct platform_device
*pdev
)
397 struct adc_device
*adc
= platform_get_drvdata(pdev
);
399 clk_enable(adc
->clk
);
401 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
402 adc
->regs
+ S3C2410_ADCCON
);
408 #define s3c_adc_suspend NULL
409 #define s3c_adc_resume NULL
412 static struct platform_driver s3c_adc_driver
= {
414 .name
= "s3c24xx-adc",
415 .owner
= THIS_MODULE
,
417 .probe
= s3c_adc_probe
,
418 .remove
= __devexit_p(s3c_adc_remove
),
419 .suspend
= s3c_adc_suspend
,
420 .resume
= s3c_adc_resume
,
423 static int __init
adc_init(void)
427 ret
= platform_driver_register(&s3c_adc_driver
);
429 printk(KERN_ERR
"%s: failed to add adc driver\n", __func__
);
434 arch_initcall(adc_init
);