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/sched.h>
18 #include <linux/list.h>
19 #include <linux/err.h>
20 #include <linux/clk.h>
21 #include <linux/interrupt.h>
24 #include <plat/regs-adc.h>
27 /* This driver is designed to control the usage of the ADC block between
28 * the touchscreen and any other drivers that may need to use it, such as
31 * Priority will be given to the touchscreen driver, but as this itself is
32 * rate limited it should not starve other requests which are processed in
33 * order that they are received.
35 * Each user registers to get a client block which uniquely identifies it
36 * and stores information such as the necessary functions to callback when
40 struct s3c_adc_client
{
41 struct platform_device
*pdev
;
42 struct list_head pend
;
43 wait_queue_head_t
*wait
;
45 unsigned int nr_samples
;
48 unsigned char channel
;
50 void (*select_cb
)(struct s3c_adc_client
*c
, unsigned selected
);
51 void (*convert_cb
)(struct s3c_adc_client
*c
,
52 unsigned val1
, unsigned val2
,
53 unsigned *samples_left
);
57 struct platform_device
*pdev
;
58 struct platform_device
*owner
;
60 struct s3c_adc_client
*cur
;
61 struct s3c_adc_client
*ts_pend
;
64 unsigned int prescale
;
69 static struct adc_device
*adc_dev
;
71 static LIST_HEAD(adc_pending
);
73 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
75 static inline void s3c_adc_convert(struct adc_device
*adc
)
77 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
79 con
|= S3C2410_ADCCON_ENABLE_START
;
80 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
83 static inline void s3c_adc_select(struct adc_device
*adc
,
84 struct s3c_adc_client
*client
)
86 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
88 client
->select_cb(client
, 1);
90 con
&= ~S3C2410_ADCCON_MUXMASK
;
91 con
&= ~S3C2410_ADCCON_STDBM
;
92 con
&= ~S3C2410_ADCCON_STARTMASK
;
95 con
|= S3C2410_ADCCON_SELMUX(client
->channel
);
97 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
100 static void s3c_adc_dbgshow(struct adc_device
*adc
)
102 adc_dbg(adc
, "CON=%08x, TSC=%08x, DLY=%08x\n",
103 readl(adc
->regs
+ S3C2410_ADCCON
),
104 readl(adc
->regs
+ S3C2410_ADCTSC
),
105 readl(adc
->regs
+ S3C2410_ADCDLY
));
108 static void s3c_adc_try(struct adc_device
*adc
)
110 struct s3c_adc_client
*next
= adc
->ts_pend
;
112 if (!next
&& !list_empty(&adc_pending
)) {
113 next
= list_first_entry(&adc_pending
,
114 struct s3c_adc_client
, pend
);
115 list_del(&next
->pend
);
120 adc_dbg(adc
, "new client is %p\n", next
);
122 s3c_adc_select(adc
, next
);
123 s3c_adc_convert(adc
);
124 s3c_adc_dbgshow(adc
);
128 int s3c_adc_start(struct s3c_adc_client
*client
,
129 unsigned int channel
, unsigned int nr_samples
)
131 struct adc_device
*adc
= adc_dev
;
135 printk(KERN_ERR
"%s: failed to find adc\n", __func__
);
139 if (client
->is_ts
&& adc
->ts_pend
)
142 local_irq_save(flags
);
144 client
->channel
= channel
;
145 client
->nr_samples
= nr_samples
;
148 adc
->ts_pend
= client
;
150 list_add_tail(&client
->pend
, &adc_pending
);
154 local_irq_restore(flags
);
158 EXPORT_SYMBOL_GPL(s3c_adc_start
);
160 static void s3c_convert_done(struct s3c_adc_client
*client
,
161 unsigned v
, unsigned u
, unsigned *left
)
164 wake_up(client
->wait
);
167 int s3c_adc_read(struct s3c_adc_client
*client
, unsigned int ch
)
169 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake
);
172 client
->convert_cb
= s3c_convert_done
;
173 client
->wait
= &wake
;
176 ret
= s3c_adc_start(client
, ch
, 1);
180 ret
= wait_event_timeout(wake
, client
->result
>= 0, HZ
/ 2);
181 if (client
->result
< 0) {
186 client
->convert_cb
= NULL
;
187 return client
->result
;
192 EXPORT_SYMBOL_GPL(s3c_adc_read
);
194 static void s3c_adc_default_select(struct s3c_adc_client
*client
,
199 struct s3c_adc_client
*s3c_adc_register(struct platform_device
*pdev
,
200 void (*select
)(struct s3c_adc_client
*client
,
201 unsigned int selected
),
202 void (*conv
)(struct s3c_adc_client
*client
,
203 unsigned d0
, unsigned d1
,
204 unsigned *samples_left
),
207 struct s3c_adc_client
*client
;
212 select
= s3c_adc_default_select
;
215 return ERR_PTR(-EINVAL
);
217 client
= kzalloc(sizeof(struct s3c_adc_client
), GFP_KERNEL
);
219 dev_err(&pdev
->dev
, "no memory for adc client\n");
220 return ERR_PTR(-ENOMEM
);
224 client
->is_ts
= is_ts
;
225 client
->select_cb
= select
;
226 client
->convert_cb
= conv
;
230 EXPORT_SYMBOL_GPL(s3c_adc_register
);
232 void s3c_adc_release(struct s3c_adc_client
*client
)
234 /* We should really check that nothing is in progress. */
235 if (adc_dev
->cur
== client
)
237 if (adc_dev
->ts_pend
== client
)
238 adc_dev
->ts_pend
= NULL
;
240 struct list_head
*p
, *n
;
241 struct s3c_adc_client
*tmp
;
243 list_for_each_safe(p
, n
, &adc_pending
) {
244 tmp
= list_entry(p
, struct s3c_adc_client
, pend
);
246 list_del(&tmp
->pend
);
250 if (adc_dev
->cur
== NULL
)
251 s3c_adc_try(adc_dev
);
254 EXPORT_SYMBOL_GPL(s3c_adc_release
);
256 static irqreturn_t
s3c_adc_irq(int irq
, void *pw
)
258 struct adc_device
*adc
= pw
;
259 struct s3c_adc_client
*client
= adc
->cur
;
261 unsigned data0
, data1
;
264 dev_warn(&adc
->pdev
->dev
, "%s: no adc pending\n", __func__
);
268 data0
= readl(adc
->regs
+ S3C2410_ADCDAT0
);
269 data1
= readl(adc
->regs
+ S3C2410_ADCDAT1
);
270 adc_dbg(adc
, "read %d: 0x%04x, 0x%04x\n", client
->nr_samples
, data0
, data1
);
272 client
->nr_samples
--;
274 if (client
->convert_cb
)
275 (client
->convert_cb
)(client
, data0
& 0x3ff, data1
& 0x3ff,
276 &client
->nr_samples
);
278 if (client
->nr_samples
> 0) {
279 /* fire another conversion for this */
281 client
->select_cb(client
, 1);
282 s3c_adc_convert(adc
);
284 local_irq_save(flags
);
285 (client
->select_cb
)(client
, 0);
289 local_irq_restore(flags
);
295 static int s3c_adc_probe(struct platform_device
*pdev
)
297 struct device
*dev
= &pdev
->dev
;
298 struct adc_device
*adc
;
299 struct resource
*regs
;
302 adc
= kzalloc(sizeof(struct adc_device
), GFP_KERNEL
);
304 dev_err(dev
, "failed to allocate adc_device\n");
309 adc
->prescale
= S3C2410_ADCCON_PRSCVL(49);
311 adc
->irq
= platform_get_irq(pdev
, 1);
313 dev_err(dev
, "failed to get adc irq\n");
318 ret
= request_irq(adc
->irq
, s3c_adc_irq
, 0, dev_name(dev
), adc
);
320 dev_err(dev
, "failed to attach adc irq\n");
324 adc
->clk
= clk_get(dev
, "adc");
325 if (IS_ERR(adc
->clk
)) {
326 dev_err(dev
, "failed to get adc clock\n");
327 ret
= PTR_ERR(adc
->clk
);
331 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
333 dev_err(dev
, "failed to find registers\n");
338 adc
->regs
= ioremap(regs
->start
, resource_size(regs
));
340 dev_err(dev
, "failed to map registers\n");
345 clk_enable(adc
->clk
);
347 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
348 adc
->regs
+ S3C2410_ADCCON
);
350 dev_info(dev
, "attached adc driver\n");
352 platform_set_drvdata(pdev
, adc
);
361 free_irq(adc
->irq
, adc
);
368 static int __devexit
s3c_adc_remove(struct platform_device
*pdev
)
370 struct adc_device
*adc
= platform_get_drvdata(pdev
);
373 free_irq(adc
->irq
, adc
);
374 clk_disable(adc
->clk
);
382 static int s3c_adc_suspend(struct platform_device
*pdev
, pm_message_t state
)
384 struct adc_device
*adc
= platform_get_drvdata(pdev
);
387 con
= readl(adc
->regs
+ S3C2410_ADCCON
);
388 con
|= S3C2410_ADCCON_STDBM
;
389 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
391 clk_disable(adc
->clk
);
396 static int s3c_adc_resume(struct platform_device
*pdev
)
398 struct adc_device
*adc
= platform_get_drvdata(pdev
);
400 clk_enable(adc
->clk
);
402 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
403 adc
->regs
+ S3C2410_ADCCON
);
409 #define s3c_adc_suspend NULL
410 #define s3c_adc_resume NULL
413 static struct platform_driver s3c_adc_driver
= {
415 .name
= "s3c24xx-adc",
416 .owner
= THIS_MODULE
,
418 .probe
= s3c_adc_probe
,
419 .remove
= __devexit_p(s3c_adc_remove
),
420 .suspend
= s3c_adc_suspend
,
421 .resume
= s3c_adc_resume
,
424 static int __init
adc_init(void)
428 ret
= platform_driver_register(&s3c_adc_driver
);
430 printk(KERN_ERR
"%s: failed to add adc driver\n", __func__
);
435 arch_initcall(adc_init
);