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/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
45 struct s3c_adc_client
{
46 struct platform_device
*pdev
;
47 struct list_head pend
;
48 wait_queue_head_t
*wait
;
50 unsigned int nr_samples
;
53 unsigned char channel
;
55 void (*select_cb
)(struct s3c_adc_client
*c
, unsigned selected
);
56 void (*convert_cb
)(struct s3c_adc_client
*c
,
57 unsigned val1
, unsigned val2
,
58 unsigned *samples_left
);
62 struct platform_device
*pdev
;
63 struct platform_device
*owner
;
65 struct s3c_adc_client
*cur
;
66 struct s3c_adc_client
*ts_pend
;
69 unsigned int prescale
;
74 static struct adc_device
*adc_dev
;
76 static LIST_HEAD(adc_pending
);
78 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
80 static inline void s3c_adc_convert(struct adc_device
*adc
)
82 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
84 con
|= S3C2410_ADCCON_ENABLE_START
;
85 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
88 static inline void s3c_adc_select(struct adc_device
*adc
,
89 struct s3c_adc_client
*client
)
91 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
93 client
->select_cb(client
, 1);
95 con
&= ~S3C2410_ADCCON_MUXMASK
;
96 con
&= ~S3C2410_ADCCON_STDBM
;
97 con
&= ~S3C2410_ADCCON_STARTMASK
;
100 con
|= S3C2410_ADCCON_SELMUX(client
->channel
);
102 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
105 static void s3c_adc_dbgshow(struct adc_device
*adc
)
107 adc_dbg(adc
, "CON=%08x, TSC=%08x, DLY=%08x\n",
108 readl(adc
->regs
+ S3C2410_ADCCON
),
109 readl(adc
->regs
+ S3C2410_ADCTSC
),
110 readl(adc
->regs
+ S3C2410_ADCDLY
));
113 static void s3c_adc_try(struct adc_device
*adc
)
115 struct s3c_adc_client
*next
= adc
->ts_pend
;
117 if (!next
&& !list_empty(&adc_pending
)) {
118 next
= list_first_entry(&adc_pending
,
119 struct s3c_adc_client
, pend
);
120 list_del(&next
->pend
);
125 adc_dbg(adc
, "new client is %p\n", next
);
127 s3c_adc_select(adc
, next
);
128 s3c_adc_convert(adc
);
129 s3c_adc_dbgshow(adc
);
133 int s3c_adc_start(struct s3c_adc_client
*client
,
134 unsigned int channel
, unsigned int nr_samples
)
136 struct adc_device
*adc
= adc_dev
;
140 printk(KERN_ERR
"%s: failed to find adc\n", __func__
);
144 if (client
->is_ts
&& adc
->ts_pend
)
147 local_irq_save(flags
);
149 client
->channel
= channel
;
150 client
->nr_samples
= nr_samples
;
153 adc
->ts_pend
= client
;
155 list_add_tail(&client
->pend
, &adc_pending
);
159 local_irq_restore(flags
);
163 EXPORT_SYMBOL_GPL(s3c_adc_start
);
165 static void s3c_convert_done(struct s3c_adc_client
*client
,
166 unsigned v
, unsigned u
, unsigned *left
)
169 wake_up(client
->wait
);
172 int s3c_adc_read(struct s3c_adc_client
*client
, unsigned int ch
)
174 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake
);
177 client
->convert_cb
= s3c_convert_done
;
178 client
->wait
= &wake
;
181 ret
= s3c_adc_start(client
, ch
, 1);
185 ret
= wait_event_timeout(wake
, client
->result
>= 0, HZ
/ 2);
186 if (client
->result
< 0) {
191 client
->convert_cb
= NULL
;
192 return client
->result
;
197 EXPORT_SYMBOL_GPL(s3c_adc_read
);
199 static void s3c_adc_default_select(struct s3c_adc_client
*client
,
204 struct s3c_adc_client
*s3c_adc_register(struct platform_device
*pdev
,
205 void (*select
)(struct s3c_adc_client
*client
,
206 unsigned int selected
),
207 void (*conv
)(struct s3c_adc_client
*client
,
208 unsigned d0
, unsigned d1
,
209 unsigned *samples_left
),
212 struct s3c_adc_client
*client
;
217 select
= s3c_adc_default_select
;
220 return ERR_PTR(-EINVAL
);
222 client
= kzalloc(sizeof(struct s3c_adc_client
), GFP_KERNEL
);
224 dev_err(&pdev
->dev
, "no memory for adc client\n");
225 return ERR_PTR(-ENOMEM
);
229 client
->is_ts
= is_ts
;
230 client
->select_cb
= select
;
231 client
->convert_cb
= conv
;
235 EXPORT_SYMBOL_GPL(s3c_adc_register
);
237 void s3c_adc_release(struct s3c_adc_client
*client
)
239 /* We should really check that nothing is in progress. */
240 if (adc_dev
->cur
== client
)
242 if (adc_dev
->ts_pend
== client
)
243 adc_dev
->ts_pend
= NULL
;
245 struct list_head
*p
, *n
;
246 struct s3c_adc_client
*tmp
;
248 list_for_each_safe(p
, n
, &adc_pending
) {
249 tmp
= list_entry(p
, struct s3c_adc_client
, pend
);
251 list_del(&tmp
->pend
);
255 if (adc_dev
->cur
== NULL
)
256 s3c_adc_try(adc_dev
);
259 EXPORT_SYMBOL_GPL(s3c_adc_release
);
261 static irqreturn_t
s3c_adc_irq(int irq
, void *pw
)
263 struct adc_device
*adc
= pw
;
264 struct s3c_adc_client
*client
= adc
->cur
;
266 unsigned data0
, data1
;
269 dev_warn(&adc
->pdev
->dev
, "%s: no adc pending\n", __func__
);
273 data0
= readl(adc
->regs
+ S3C2410_ADCDAT0
);
274 data1
= readl(adc
->regs
+ S3C2410_ADCDAT1
);
275 adc_dbg(adc
, "read %d: 0x%04x, 0x%04x\n", client
->nr_samples
, data0
, data1
);
277 client
->nr_samples
--;
279 if (client
->convert_cb
)
280 (client
->convert_cb
)(client
, data0
& 0x3ff, data1
& 0x3ff,
281 &client
->nr_samples
);
283 if (client
->nr_samples
> 0) {
284 /* fire another conversion for this */
286 client
->select_cb(client
, 1);
287 s3c_adc_convert(adc
);
289 local_irq_save(flags
);
290 (client
->select_cb
)(client
, 0);
294 local_irq_restore(flags
);
298 if (platform_get_device_id(client
->pdev
)->driver_data
== TYPE_S3C64XX
) {
299 /* Clear ADC interrupt */
300 writel(0, adc
->regs
+ S3C64XX_ADCCLRINT
);
305 static int s3c_adc_probe(struct platform_device
*pdev
)
307 struct device
*dev
= &pdev
->dev
;
308 struct adc_device
*adc
;
309 struct resource
*regs
;
312 adc
= kzalloc(sizeof(struct adc_device
), GFP_KERNEL
);
314 dev_err(dev
, "failed to allocate adc_device\n");
319 adc
->prescale
= S3C2410_ADCCON_PRSCVL(49);
321 adc
->irq
= platform_get_irq(pdev
, 1);
323 dev_err(dev
, "failed to get adc irq\n");
328 ret
= request_irq(adc
->irq
, s3c_adc_irq
, 0, dev_name(dev
), adc
);
330 dev_err(dev
, "failed to attach adc irq\n");
334 adc
->clk
= clk_get(dev
, "adc");
335 if (IS_ERR(adc
->clk
)) {
336 dev_err(dev
, "failed to get adc clock\n");
337 ret
= PTR_ERR(adc
->clk
);
341 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
343 dev_err(dev
, "failed to find registers\n");
348 adc
->regs
= ioremap(regs
->start
, resource_size(regs
));
350 dev_err(dev
, "failed to map registers\n");
355 clk_enable(adc
->clk
);
357 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
358 adc
->regs
+ S3C2410_ADCCON
);
360 dev_info(dev
, "attached adc driver\n");
362 platform_set_drvdata(pdev
, adc
);
371 free_irq(adc
->irq
, adc
);
378 static int __devexit
s3c_adc_remove(struct platform_device
*pdev
)
380 struct adc_device
*adc
= platform_get_drvdata(pdev
);
383 free_irq(adc
->irq
, adc
);
384 clk_disable(adc
->clk
);
392 static int s3c_adc_suspend(struct platform_device
*pdev
, pm_message_t state
)
394 struct adc_device
*adc
= platform_get_drvdata(pdev
);
397 con
= readl(adc
->regs
+ S3C2410_ADCCON
);
398 con
|= S3C2410_ADCCON_STDBM
;
399 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
401 clk_disable(adc
->clk
);
406 static int s3c_adc_resume(struct platform_device
*pdev
)
408 struct adc_device
*adc
= platform_get_drvdata(pdev
);
410 clk_enable(adc
->clk
);
412 writel(adc
->prescale
| S3C2410_ADCCON_PRSCEN
,
413 adc
->regs
+ S3C2410_ADCCON
);
419 #define s3c_adc_suspend NULL
420 #define s3c_adc_resume NULL
423 static struct platform_device_id s3c_adc_driver_ids
[] = {
425 .name
= "s3c24xx-adc",
426 .driver_data
= TYPE_S3C24XX
,
428 .name
= "s3c64xx-adc",
429 .driver_data
= TYPE_S3C64XX
,
433 MODULE_DEVICE_TABLE(platform
, s3c_adc_driver_ids
);
435 static struct platform_driver s3c_adc_driver
= {
436 .id_table
= s3c_adc_driver_ids
,
439 .owner
= THIS_MODULE
,
441 .probe
= s3c_adc_probe
,
442 .remove
= __devexit_p(s3c_adc_remove
),
443 .suspend
= s3c_adc_suspend
,
444 .resume
= s3c_adc_resume
,
447 static int __init
adc_init(void)
451 ret
= platform_driver_register(&s3c_adc_driver
);
453 printk(KERN_ERR
"%s: failed to add adc driver\n", __func__
);
458 arch_initcall(adc_init
);