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
);
52 struct platform_device
*pdev
;
53 struct platform_device
*owner
;
55 struct s3c_adc_client
*cur
;
56 struct s3c_adc_client
*ts_pend
;
59 unsigned int prescale
;
64 static struct adc_device
*adc_dev
;
66 static LIST_HEAD(adc_pending
);
68 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
70 static inline void s3c_adc_convert(struct adc_device
*adc
)
72 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
74 con
|= S3C2410_ADCCON_ENABLE_START
;
75 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
78 static inline void s3c_adc_select(struct adc_device
*adc
,
79 struct s3c_adc_client
*client
)
81 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
85 con
&= ~S3C2410_ADCCON_MUXMASK
;
86 con
&= ~S3C2410_ADCCON_STDBM
;
87 con
&= ~S3C2410_ADCCON_STARTMASK
;
90 con
|= S3C2410_ADCCON_SELMUX(client
->channel
);
92 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
95 static void s3c_adc_dbgshow(struct adc_device
*adc
)
97 adc_dbg(adc
, "CON=%08x, TSC=%08x, DLY=%08x\n",
98 readl(adc
->regs
+ S3C2410_ADCCON
),
99 readl(adc
->regs
+ S3C2410_ADCTSC
),
100 readl(adc
->regs
+ S3C2410_ADCDLY
));
103 void s3c_adc_try(struct adc_device
*adc
)
105 struct s3c_adc_client
*next
= adc
->ts_pend
;
107 if (!next
&& !list_empty(&adc_pending
)) {
108 next
= list_first_entry(&adc_pending
,
109 struct s3c_adc_client
, pend
);
110 list_del(&next
->pend
);
115 adc_dbg(adc
, "new client is %p\n", next
);
117 s3c_adc_select(adc
, next
);
118 s3c_adc_convert(adc
);
119 s3c_adc_dbgshow(adc
);
123 int s3c_adc_start(struct s3c_adc_client
*client
,
124 unsigned int channel
, unsigned int nr_samples
)
126 struct adc_device
*adc
= adc_dev
;
130 printk(KERN_ERR
"%s: failed to find adc\n", __func__
);
134 if (client
->is_ts
&& adc
->ts_pend
)
137 local_irq_save(flags
);
139 client
->channel
= channel
;
140 client
->nr_samples
= nr_samples
;
143 adc
->ts_pend
= client
;
145 list_add_tail(&client
->pend
, &adc_pending
);
149 local_irq_restore(flags
);
153 EXPORT_SYMBOL_GPL(s3c_adc_start
);
155 static void s3c_adc_default_select(unsigned select
)
159 struct s3c_adc_client
*s3c_adc_register(struct platform_device
*pdev
,
160 void (*select
)(unsigned int selected
),
161 void (*conv
)(unsigned d0
, unsigned d1
),
164 struct s3c_adc_client
*client
;
170 select
= s3c_adc_default_select
;
173 return ERR_PTR(-EINVAL
);
175 client
= kzalloc(sizeof(struct s3c_adc_client
), GFP_KERNEL
);
177 dev_err(&pdev
->dev
, "no memory for adc client\n");
178 return ERR_PTR(-ENOMEM
);
182 client
->is_ts
= is_ts
;
183 client
->select_cb
= select
;
184 client
->convert_cb
= conv
;
188 EXPORT_SYMBOL_GPL(s3c_adc_register
);
190 void s3c_adc_release(struct s3c_adc_client
*client
)
192 /* We should really check that nothing is in progress. */
195 EXPORT_SYMBOL_GPL(s3c_adc_release
);
197 static irqreturn_t
s3c_adc_irq(int irq
, void *pw
)
199 struct adc_device
*adc
= pw
;
200 struct s3c_adc_client
*client
= adc
->cur
;
202 unsigned data0
, data1
;
205 dev_warn(&adc
->pdev
->dev
, "%s: no adc pending\n", __func__
);
209 data0
= readl(adc
->regs
+ S3C2410_ADCDAT0
);
210 data1
= readl(adc
->regs
+ S3C2410_ADCDAT1
);
211 adc_dbg(adc
, "read %d: 0x%04x, 0x%04x\n", client
->nr_samples
, data0
, data1
);
213 (client
->convert_cb
)(data0
& 0x3ff, data1
& 0x3ff);
215 if (--client
->nr_samples
> 0) {
216 /* fire another conversion for this */
218 client
->select_cb(1);
219 s3c_adc_convert(adc
);
221 local_irq_save(flags
);
222 (client
->select_cb
)(0);
226 local_irq_restore(flags
);
232 static int s3c_adc_probe(struct platform_device
*pdev
)
234 struct device
*dev
= &pdev
->dev
;
235 struct adc_device
*adc
;
236 struct resource
*regs
;
239 adc
= kzalloc(sizeof(struct adc_device
), GFP_KERNEL
);
241 dev_err(dev
, "failed to allocate adc_device\n");
246 adc
->prescale
= S3C2410_ADCCON_PRSCVL(49);
248 adc
->irq
= platform_get_irq(pdev
, 1);
250 dev_err(dev
, "failed to get adc irq\n");
255 ret
= request_irq(adc
->irq
, s3c_adc_irq
, 0, dev_name(dev
), adc
);
257 dev_err(dev
, "failed to attach adc irq\n");
261 adc
->clk
= clk_get(dev
, "adc");
262 if (IS_ERR(adc
->clk
)) {
263 dev_err(dev
, "failed to get adc clock\n");
264 ret
= PTR_ERR(adc
->clk
);
268 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
270 dev_err(dev
, "failed to find registers\n");
275 adc
->regs
= ioremap(regs
->start
, resource_size(regs
));
277 dev_err(dev
, "failed to map registers\n");
282 clk_enable(adc
->clk
);
284 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
285 adc
->regs
+ S3C2410_ADCCON
);
287 dev_info(dev
, "attached adc driver\n");
289 platform_set_drvdata(pdev
, adc
);
298 free_irq(adc
->irq
, adc
);
305 static int s3c_adc_remove(struct platform_device
*pdev
)
307 struct adc_device
*adc
= platform_get_drvdata(pdev
);
310 free_irq(adc
->irq
, adc
);
311 clk_disable(adc
->clk
);
319 static int s3c_adc_suspend(struct platform_device
*pdev
, pm_message_t state
)
321 struct adc_device
*adc
= platform_get_drvdata(pdev
);
324 con
= readl(adc
->regs
+ S3C2410_ADCCON
);
325 con
|= S3C2410_ADCCON_STDBM
;
326 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
328 clk_disable(adc
->clk
);
333 static int s3c_adc_resume(struct platform_device
*pdev
)
335 struct adc_device
*adc
= platform_get_drvdata(pdev
);
337 clk_enable(adc
->clk
);
339 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
340 adc
->regs
+ S3C2410_ADCCON
);
346 #define s3c_adc_suspend NULL
347 #define s3c_adc_resume NULL
350 static struct platform_driver s3c_adc_driver
= {
352 .name
= "s3c24xx-adc",
353 .owner
= THIS_MODULE
,
355 .probe
= s3c_adc_probe
,
356 .remove
= __devexit_p(s3c_adc_remove
),
357 .suspend
= s3c_adc_suspend
,
358 .resume
= s3c_adc_resume
,
361 static int __init
adc_init(void)
365 ret
= platform_driver_register(&s3c_adc_driver
);
367 printk(KERN_ERR
"%s: failed to add adc driver\n", __func__
);
372 arch_initcall(adc_init
);