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
;
43 unsigned int nr_samples
;
45 unsigned char channel
;
47 void (*select_cb
)(unsigned selected
);
48 void (*convert_cb
)(unsigned val1
, unsigned val2
,
49 unsigned *samples_left
);
53 struct platform_device
*pdev
;
54 struct platform_device
*owner
;
56 struct s3c_adc_client
*cur
;
57 struct s3c_adc_client
*ts_pend
;
60 unsigned int prescale
;
65 static struct adc_device
*adc_dev
;
67 static LIST_HEAD(adc_pending
);
69 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
71 static inline void s3c_adc_convert(struct adc_device
*adc
)
73 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
75 con
|= S3C2410_ADCCON_ENABLE_START
;
76 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
79 static inline void s3c_adc_select(struct adc_device
*adc
,
80 struct s3c_adc_client
*client
)
82 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
86 con
&= ~S3C2410_ADCCON_MUXMASK
;
87 con
&= ~S3C2410_ADCCON_STDBM
;
88 con
&= ~S3C2410_ADCCON_STARTMASK
;
91 con
|= S3C2410_ADCCON_SELMUX(client
->channel
);
93 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
96 static void s3c_adc_dbgshow(struct adc_device
*adc
)
98 adc_dbg(adc
, "CON=%08x, TSC=%08x, DLY=%08x\n",
99 readl(adc
->regs
+ S3C2410_ADCCON
),
100 readl(adc
->regs
+ S3C2410_ADCTSC
),
101 readl(adc
->regs
+ S3C2410_ADCDLY
));
104 static void s3c_adc_try(struct adc_device
*adc
)
106 struct s3c_adc_client
*next
= adc
->ts_pend
;
108 if (!next
&& !list_empty(&adc_pending
)) {
109 next
= list_first_entry(&adc_pending
,
110 struct s3c_adc_client
, pend
);
111 list_del(&next
->pend
);
116 adc_dbg(adc
, "new client is %p\n", next
);
118 s3c_adc_select(adc
, next
);
119 s3c_adc_convert(adc
);
120 s3c_adc_dbgshow(adc
);
124 int s3c_adc_start(struct s3c_adc_client
*client
,
125 unsigned int channel
, unsigned int nr_samples
)
127 struct adc_device
*adc
= adc_dev
;
131 printk(KERN_ERR
"%s: failed to find adc\n", __func__
);
135 if (client
->is_ts
&& adc
->ts_pend
)
138 local_irq_save(flags
);
140 client
->channel
= channel
;
141 client
->nr_samples
= nr_samples
;
144 adc
->ts_pend
= client
;
146 list_add_tail(&client
->pend
, &adc_pending
);
150 local_irq_restore(flags
);
154 EXPORT_SYMBOL_GPL(s3c_adc_start
);
156 static void s3c_adc_default_select(unsigned select
)
160 struct s3c_adc_client
*s3c_adc_register(struct platform_device
*pdev
,
161 void (*select
)(unsigned int selected
),
162 void (*conv
)(unsigned d0
, unsigned d1
,
163 unsigned *samples_left
),
166 struct s3c_adc_client
*client
;
172 select
= s3c_adc_default_select
;
175 return ERR_PTR(-EINVAL
);
177 client
= kzalloc(sizeof(struct s3c_adc_client
), GFP_KERNEL
);
179 dev_err(&pdev
->dev
, "no memory for adc client\n");
180 return ERR_PTR(-ENOMEM
);
184 client
->is_ts
= is_ts
;
185 client
->select_cb
= select
;
186 client
->convert_cb
= conv
;
190 EXPORT_SYMBOL_GPL(s3c_adc_register
);
192 void s3c_adc_release(struct s3c_adc_client
*client
)
194 /* We should really check that nothing is in progress. */
195 if (adc_dev
->cur
== client
)
197 if (adc_dev
->ts_pend
== client
)
198 adc_dev
->ts_pend
= NULL
;
200 struct list_head
*p
, *n
;
201 struct s3c_adc_client
*tmp
;
203 list_for_each_safe(p
, n
, &adc_pending
) {
204 tmp
= list_entry(p
, struct s3c_adc_client
, pend
);
206 list_del(&tmp
->pend
);
210 if (adc_dev
->cur
== NULL
)
211 s3c_adc_try(adc_dev
);
214 EXPORT_SYMBOL_GPL(s3c_adc_release
);
216 static irqreturn_t
s3c_adc_irq(int irq
, void *pw
)
218 struct adc_device
*adc
= pw
;
219 struct s3c_adc_client
*client
= adc
->cur
;
221 unsigned data0
, data1
;
224 dev_warn(&adc
->pdev
->dev
, "%s: no adc pending\n", __func__
);
228 data0
= readl(adc
->regs
+ S3C2410_ADCDAT0
);
229 data1
= readl(adc
->regs
+ S3C2410_ADCDAT1
);
230 adc_dbg(adc
, "read %d: 0x%04x, 0x%04x\n", client
->nr_samples
, data0
, data1
);
232 client
->nr_samples
--;
233 (client
->convert_cb
)(data0
& 0x3ff, data1
& 0x3ff, &client
->nr_samples
);
235 if (client
->nr_samples
> 0) {
236 /* fire another conversion for this */
238 client
->select_cb(1);
239 s3c_adc_convert(adc
);
241 local_irq_save(flags
);
242 (client
->select_cb
)(0);
246 local_irq_restore(flags
);
252 static int s3c_adc_probe(struct platform_device
*pdev
)
254 struct device
*dev
= &pdev
->dev
;
255 struct adc_device
*adc
;
256 struct resource
*regs
;
259 adc
= kzalloc(sizeof(struct adc_device
), GFP_KERNEL
);
261 dev_err(dev
, "failed to allocate adc_device\n");
266 adc
->prescale
= S3C2410_ADCCON_PRSCVL(49);
268 adc
->irq
= platform_get_irq(pdev
, 1);
270 dev_err(dev
, "failed to get adc irq\n");
275 ret
= request_irq(adc
->irq
, s3c_adc_irq
, 0, dev_name(dev
), adc
);
277 dev_err(dev
, "failed to attach adc irq\n");
281 adc
->clk
= clk_get(dev
, "adc");
282 if (IS_ERR(adc
->clk
)) {
283 dev_err(dev
, "failed to get adc clock\n");
284 ret
= PTR_ERR(adc
->clk
);
288 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
290 dev_err(dev
, "failed to find registers\n");
295 adc
->regs
= ioremap(regs
->start
, resource_size(regs
));
297 dev_err(dev
, "failed to map registers\n");
302 clk_enable(adc
->clk
);
304 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
305 adc
->regs
+ S3C2410_ADCCON
);
307 dev_info(dev
, "attached adc driver\n");
309 platform_set_drvdata(pdev
, adc
);
318 free_irq(adc
->irq
, adc
);
325 static int s3c_adc_remove(struct platform_device
*pdev
)
327 struct adc_device
*adc
= platform_get_drvdata(pdev
);
330 free_irq(adc
->irq
, adc
);
331 clk_disable(adc
->clk
);
339 static int s3c_adc_suspend(struct platform_device
*pdev
, pm_message_t state
)
341 struct adc_device
*adc
= platform_get_drvdata(pdev
);
344 con
= readl(adc
->regs
+ S3C2410_ADCCON
);
345 con
|= S3C2410_ADCCON_STDBM
;
346 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
348 clk_disable(adc
->clk
);
353 static int s3c_adc_resume(struct platform_device
*pdev
)
355 struct adc_device
*adc
= platform_get_drvdata(pdev
);
357 clk_enable(adc
->clk
);
359 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
360 adc
->regs
+ S3C2410_ADCCON
);
366 #define s3c_adc_suspend NULL
367 #define s3c_adc_resume NULL
370 static struct platform_driver s3c_adc_driver
= {
372 .name
= "s3c24xx-adc",
373 .owner
= THIS_MODULE
,
375 .probe
= s3c_adc_probe
,
376 .remove
= __devexit_p(s3c_adc_remove
),
377 .suspend
= s3c_adc_suspend
,
378 .resume
= s3c_adc_resume
,
381 static int __init
adc_init(void)
385 ret
= platform_driver_register(&s3c_adc_driver
);
387 printk(KERN_ERR
"%s: failed to add adc driver\n", __func__
);
392 arch_initcall(adc_init
);