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>
25 #include <plat/regs-adc.h>
28 /* This driver is designed to control the usage of the ADC block between
29 * the touchscreen and any other drivers that may need to use it, such as
32 * Priority will be given to the touchscreen driver, but as this itself is
33 * rate limited it should not starve other requests which are processed in
34 * order that they are received.
36 * Each user registers to get a client block which uniquely identifies it
37 * and stores information such as the necessary functions to callback when
46 struct s3c_adc_client
{
47 struct platform_device
*pdev
;
48 struct list_head pend
;
49 wait_queue_head_t
*wait
;
51 unsigned int nr_samples
;
54 unsigned char channel
;
56 void (*select_cb
)(struct s3c_adc_client
*c
, unsigned selected
);
57 void (*convert_cb
)(struct s3c_adc_client
*c
,
58 unsigned val1
, unsigned val2
,
59 unsigned *samples_left
);
63 struct platform_device
*pdev
;
64 struct platform_device
*owner
;
66 struct s3c_adc_client
*cur
;
67 struct s3c_adc_client
*ts_pend
;
70 unsigned int prescale
;
75 static struct adc_device
*adc_dev
;
77 static LIST_HEAD(adc_pending
);
79 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
81 static inline void s3c_adc_convert(struct adc_device
*adc
)
83 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
85 con
|= S3C2410_ADCCON_ENABLE_START
;
86 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
89 static inline void s3c_adc_select(struct adc_device
*adc
,
90 struct s3c_adc_client
*client
)
92 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
94 client
->select_cb(client
, 1);
96 con
&= ~S3C2410_ADCCON_MUXMASK
;
97 con
&= ~S3C2410_ADCCON_STDBM
;
98 con
&= ~S3C2410_ADCCON_STARTMASK
;
101 con
|= S3C2410_ADCCON_SELMUX(client
->channel
);
103 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
106 static void s3c_adc_dbgshow(struct adc_device
*adc
)
108 adc_dbg(adc
, "CON=%08x, TSC=%08x, DLY=%08x\n",
109 readl(adc
->regs
+ S3C2410_ADCCON
),
110 readl(adc
->regs
+ S3C2410_ADCTSC
),
111 readl(adc
->regs
+ S3C2410_ADCDLY
));
114 static void s3c_adc_try(struct adc_device
*adc
)
116 struct s3c_adc_client
*next
= adc
->ts_pend
;
118 if (!next
&& !list_empty(&adc_pending
)) {
119 next
= list_first_entry(&adc_pending
,
120 struct s3c_adc_client
, pend
);
121 list_del(&next
->pend
);
126 adc_dbg(adc
, "new client is %p\n", next
);
128 s3c_adc_select(adc
, next
);
129 s3c_adc_convert(adc
);
130 s3c_adc_dbgshow(adc
);
134 int s3c_adc_start(struct s3c_adc_client
*client
,
135 unsigned int channel
, unsigned int nr_samples
)
137 struct adc_device
*adc
= adc_dev
;
141 printk(KERN_ERR
"%s: failed to find adc\n", __func__
);
145 if (client
->is_ts
&& adc
->ts_pend
)
148 local_irq_save(flags
);
150 client
->channel
= channel
;
151 client
->nr_samples
= nr_samples
;
154 adc
->ts_pend
= client
;
156 list_add_tail(&client
->pend
, &adc_pending
);
160 local_irq_restore(flags
);
164 EXPORT_SYMBOL_GPL(s3c_adc_start
);
166 static void s3c_convert_done(struct s3c_adc_client
*client
,
167 unsigned v
, unsigned u
, unsigned *left
)
170 wake_up(client
->wait
);
173 int s3c_adc_read(struct s3c_adc_client
*client
, unsigned int ch
)
175 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake
);
178 client
->convert_cb
= s3c_convert_done
;
179 client
->wait
= &wake
;
182 ret
= s3c_adc_start(client
, ch
, 1);
186 ret
= wait_event_timeout(wake
, client
->result
>= 0, HZ
/ 2);
187 if (client
->result
< 0) {
192 client
->convert_cb
= NULL
;
193 return client
->result
;
198 EXPORT_SYMBOL_GPL(s3c_adc_read
);
200 static void s3c_adc_default_select(struct s3c_adc_client
*client
,
205 struct s3c_adc_client
*s3c_adc_register(struct platform_device
*pdev
,
206 void (*select
)(struct s3c_adc_client
*client
,
207 unsigned int selected
),
208 void (*conv
)(struct s3c_adc_client
*client
,
209 unsigned d0
, unsigned d1
,
210 unsigned *samples_left
),
213 struct s3c_adc_client
*client
;
218 select
= s3c_adc_default_select
;
221 return ERR_PTR(-EINVAL
);
223 client
= kzalloc(sizeof(struct s3c_adc_client
), GFP_KERNEL
);
225 dev_err(&pdev
->dev
, "no memory for adc client\n");
226 return ERR_PTR(-ENOMEM
);
230 client
->is_ts
= is_ts
;
231 client
->select_cb
= select
;
232 client
->convert_cb
= conv
;
236 EXPORT_SYMBOL_GPL(s3c_adc_register
);
238 void s3c_adc_release(struct s3c_adc_client
*client
)
240 /* We should really check that nothing is in progress. */
241 if (adc_dev
->cur
== client
)
243 if (adc_dev
->ts_pend
== client
)
244 adc_dev
->ts_pend
= NULL
;
246 struct list_head
*p
, *n
;
247 struct s3c_adc_client
*tmp
;
249 list_for_each_safe(p
, n
, &adc_pending
) {
250 tmp
= list_entry(p
, struct s3c_adc_client
, pend
);
252 list_del(&tmp
->pend
);
256 if (adc_dev
->cur
== NULL
)
257 s3c_adc_try(adc_dev
);
260 EXPORT_SYMBOL_GPL(s3c_adc_release
);
262 static irqreturn_t
s3c_adc_irq(int irq
, void *pw
)
264 struct adc_device
*adc
= pw
;
265 struct s3c_adc_client
*client
= adc
->cur
;
266 enum s3c_cpu_type cpu
= platform_get_device_id(adc
->pdev
)->driver_data
;
268 unsigned data0
, data1
;
271 dev_warn(&adc
->pdev
->dev
, "%s: no adc pending\n", __func__
);
275 data0
= readl(adc
->regs
+ S3C2410_ADCDAT0
);
276 data1
= readl(adc
->regs
+ S3C2410_ADCDAT1
);
277 adc_dbg(adc
, "read %d: 0x%04x, 0x%04x\n", client
->nr_samples
, data0
, data1
);
279 client
->nr_samples
--;
281 if (cpu
== TYPE_S3C64XX
) {
282 /* S3C64XX ADC resolution is 12-bit */
290 if (client
->convert_cb
)
291 (client
->convert_cb
)(client
, data0
, data1
, &client
->nr_samples
);
293 if (client
->nr_samples
> 0) {
294 /* fire another conversion for this */
296 client
->select_cb(client
, 1);
297 s3c_adc_convert(adc
);
299 local_irq_save(flags
);
300 (client
->select_cb
)(client
, 0);
304 local_irq_restore(flags
);
308 if (cpu
== TYPE_S3C64XX
) {
309 /* Clear ADC interrupt */
310 writel(0, adc
->regs
+ S3C64XX_ADCCLRINT
);
315 static int s3c_adc_probe(struct platform_device
*pdev
)
317 struct device
*dev
= &pdev
->dev
;
318 struct adc_device
*adc
;
319 struct resource
*regs
;
323 adc
= kzalloc(sizeof(struct adc_device
), GFP_KERNEL
);
325 dev_err(dev
, "failed to allocate adc_device\n");
330 adc
->prescale
= S3C2410_ADCCON_PRSCVL(49);
332 adc
->irq
= platform_get_irq(pdev
, 1);
334 dev_err(dev
, "failed to get adc irq\n");
339 ret
= request_irq(adc
->irq
, s3c_adc_irq
, 0, dev_name(dev
), adc
);
341 dev_err(dev
, "failed to attach adc irq\n");
345 adc
->clk
= clk_get(dev
, "adc");
346 if (IS_ERR(adc
->clk
)) {
347 dev_err(dev
, "failed to get adc clock\n");
348 ret
= PTR_ERR(adc
->clk
);
352 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
354 dev_err(dev
, "failed to find registers\n");
359 adc
->regs
= ioremap(regs
->start
, resource_size(regs
));
361 dev_err(dev
, "failed to map registers\n");
366 clk_enable(adc
->clk
);
368 tmp
= adc
->prescale
| S3C2410_ADCCON_PRSCEN
;
369 if (platform_get_device_id(pdev
)->driver_data
== TYPE_S3C64XX
) {
370 /* Enable 12-bit ADC resolution */
371 tmp
|= S3C64XX_ADCCON_RESSEL
;
373 writel(tmp
, adc
->regs
+ S3C2410_ADCCON
);
375 dev_info(dev
, "attached adc driver\n");
377 platform_set_drvdata(pdev
, adc
);
386 free_irq(adc
->irq
, adc
);
393 static int __devexit
s3c_adc_remove(struct platform_device
*pdev
)
395 struct adc_device
*adc
= platform_get_drvdata(pdev
);
398 free_irq(adc
->irq
, adc
);
399 clk_disable(adc
->clk
);
407 static int s3c_adc_suspend(struct platform_device
*pdev
, pm_message_t state
)
409 struct adc_device
*adc
= platform_get_drvdata(pdev
);
412 con
= readl(adc
->regs
+ S3C2410_ADCCON
);
413 con
|= S3C2410_ADCCON_STDBM
;
414 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
416 disable_irq(adc
->irq
);
417 clk_disable(adc
->clk
);
422 static int s3c_adc_resume(struct platform_device
*pdev
)
424 struct adc_device
*adc
= platform_get_drvdata(pdev
);
426 clk_enable(adc
->clk
);
427 enable_irq(adc
->irq
);
429 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
430 adc
->regs
+ S3C2410_ADCCON
);
436 #define s3c_adc_suspend NULL
437 #define s3c_adc_resume NULL
440 static struct platform_device_id s3c_adc_driver_ids
[] = {
442 .name
= "s3c24xx-adc",
443 .driver_data
= TYPE_S3C24XX
,
445 .name
= "s3c64xx-adc",
446 .driver_data
= TYPE_S3C64XX
,
450 MODULE_DEVICE_TABLE(platform
, s3c_adc_driver_ids
);
452 static struct platform_driver s3c_adc_driver
= {
453 .id_table
= s3c_adc_driver_ids
,
456 .owner
= THIS_MODULE
,
458 .probe
= s3c_adc_probe
,
459 .remove
= __devexit_p(s3c_adc_remove
),
460 .suspend
= s3c_adc_suspend
,
461 .resume
= s3c_adc_resume
,
464 static int __init
adc_init(void)
468 ret
= platform_driver_register(&s3c_adc_driver
);
470 printk(KERN_ERR
"%s: failed to add adc driver\n", __func__
);
475 arch_initcall(adc_init
);