1 // SPDX-License-Identifier: GPL-1.0+
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 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/platform_device.h>
13 #include <linux/sched.h>
14 #include <linux/list.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/clk.h>
18 #include <linux/interrupt.h>
20 #include <linux/regulator/consumer.h>
23 #include <linux/soc/samsung/s3c-adc.h>
25 /* This driver is designed to control the usage of the ADC block between
26 * the touchscreen and any other drivers that may need to use it, such as
29 * Priority will be given to the touchscreen driver, but as this itself is
30 * rate limited it should not starve other requests which are processed in
31 * order that they are received.
33 * Each user registers to get a client block which uniquely identifies it
34 * and stores information such as the necessary functions to callback when
39 TYPE_ADCV1
, /* S3C24XX */
40 TYPE_ADCV11
, /* S3C2443 */
41 TYPE_ADCV12
, /* S3C2416, S3C2450 */
42 TYPE_ADCV2
, /* S3C64XX */
43 TYPE_ADCV3
, /* S5PV210, S5PC110, Exynos4210 */
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
;
71 unsigned int prescale
;
74 struct regulator
*vdd
;
77 static struct adc_device
*adc_dev
;
79 static LIST_HEAD(adc_pending
); /* protected by adc_device.lock */
81 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
83 static inline void s3c_adc_convert(struct adc_device
*adc
)
85 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
87 con
|= S3C2410_ADCCON_ENABLE_START
;
88 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
91 static inline void s3c_adc_select(struct adc_device
*adc
,
92 struct s3c_adc_client
*client
)
94 unsigned con
= readl(adc
->regs
+ S3C2410_ADCCON
);
95 enum s3c_cpu_type cpu
= platform_get_device_id(adc
->pdev
)->driver_data
;
97 client
->select_cb(client
, 1);
99 if (cpu
== TYPE_ADCV1
|| cpu
== TYPE_ADCV2
)
100 con
&= ~S3C2410_ADCCON_MUXMASK
;
101 con
&= ~S3C2410_ADCCON_STDBM
;
102 con
&= ~S3C2410_ADCCON_STARTMASK
;
104 if (!client
->is_ts
) {
105 if (cpu
== TYPE_ADCV3
)
106 writel(client
->channel
& 0xf, adc
->regs
+ S5P_ADCMUX
);
107 else if (cpu
== TYPE_ADCV11
|| cpu
== TYPE_ADCV12
)
108 writel(client
->channel
& 0xf,
109 adc
->regs
+ S3C2443_ADCMUX
);
111 con
|= S3C2410_ADCCON_SELMUX(client
->channel
);
114 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
117 static void s3c_adc_dbgshow(struct adc_device
*adc
)
119 adc_dbg(adc
, "CON=%08x, TSC=%08x, DLY=%08x\n",
120 readl(adc
->regs
+ S3C2410_ADCCON
),
121 readl(adc
->regs
+ S3C2410_ADCTSC
),
122 readl(adc
->regs
+ S3C2410_ADCDLY
));
125 static void s3c_adc_try(struct adc_device
*adc
)
127 struct s3c_adc_client
*next
= adc
->ts_pend
;
129 if (!next
&& !list_empty(&adc_pending
)) {
130 next
= list_first_entry(&adc_pending
,
131 struct s3c_adc_client
, pend
);
132 list_del(&next
->pend
);
137 adc_dbg(adc
, "new client is %p\n", next
);
139 s3c_adc_select(adc
, next
);
140 s3c_adc_convert(adc
);
141 s3c_adc_dbgshow(adc
);
145 int s3c_adc_start(struct s3c_adc_client
*client
,
146 unsigned int channel
, unsigned int nr_samples
)
148 struct adc_device
*adc
= adc_dev
;
152 printk(KERN_ERR
"%s: failed to find adc\n", __func__
);
156 spin_lock_irqsave(&adc
->lock
, flags
);
158 if (client
->is_ts
&& adc
->ts_pend
) {
159 spin_unlock_irqrestore(&adc
->lock
, flags
);
163 client
->channel
= channel
;
164 client
->nr_samples
= nr_samples
;
167 adc
->ts_pend
= client
;
169 list_add_tail(&client
->pend
, &adc_pending
);
174 spin_unlock_irqrestore(&adc
->lock
, flags
);
178 EXPORT_SYMBOL_GPL(s3c_adc_start
);
180 static void s3c_convert_done(struct s3c_adc_client
*client
,
181 unsigned v
, unsigned u
, unsigned *left
)
184 wake_up(client
->wait
);
187 int s3c_adc_read(struct s3c_adc_client
*client
, unsigned int ch
)
189 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake
);
192 client
->convert_cb
= s3c_convert_done
;
193 client
->wait
= &wake
;
196 ret
= s3c_adc_start(client
, ch
, 1);
200 ret
= wait_event_timeout(wake
, client
->result
>= 0, HZ
/ 2);
201 if (client
->result
< 0) {
206 client
->convert_cb
= NULL
;
207 return client
->result
;
212 EXPORT_SYMBOL_GPL(s3c_adc_read
);
214 static void s3c_adc_default_select(struct s3c_adc_client
*client
,
219 struct s3c_adc_client
*s3c_adc_register(struct platform_device
*pdev
,
220 void (*select
)(struct s3c_adc_client
*client
,
221 unsigned int selected
),
222 void (*conv
)(struct s3c_adc_client
*client
,
223 unsigned d0
, unsigned d1
,
224 unsigned *samples_left
),
227 struct s3c_adc_client
*client
;
232 select
= s3c_adc_default_select
;
235 return ERR_PTR(-EINVAL
);
237 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
239 return ERR_PTR(-ENOMEM
);
242 client
->is_ts
= is_ts
;
243 client
->select_cb
= select
;
244 client
->convert_cb
= conv
;
248 EXPORT_SYMBOL_GPL(s3c_adc_register
);
250 void s3c_adc_release(struct s3c_adc_client
*client
)
254 spin_lock_irqsave(&adc_dev
->lock
, flags
);
256 /* We should really check that nothing is in progress. */
257 if (adc_dev
->cur
== client
)
259 if (adc_dev
->ts_pend
== client
)
260 adc_dev
->ts_pend
= NULL
;
262 struct list_head
*p
, *n
;
263 struct s3c_adc_client
*tmp
;
265 list_for_each_safe(p
, n
, &adc_pending
) {
266 tmp
= list_entry(p
, struct s3c_adc_client
, pend
);
268 list_del(&tmp
->pend
);
272 if (adc_dev
->cur
== NULL
)
273 s3c_adc_try(adc_dev
);
275 spin_unlock_irqrestore(&adc_dev
->lock
, flags
);
278 EXPORT_SYMBOL_GPL(s3c_adc_release
);
280 static irqreturn_t
s3c_adc_irq(int irq
, void *pw
)
282 struct adc_device
*adc
= pw
;
283 struct s3c_adc_client
*client
= adc
->cur
;
284 enum s3c_cpu_type cpu
= platform_get_device_id(adc
->pdev
)->driver_data
;
285 unsigned data0
, data1
;
288 dev_warn(&adc
->pdev
->dev
, "%s: no adc pending\n", __func__
);
292 data0
= readl(adc
->regs
+ S3C2410_ADCDAT0
);
293 data1
= readl(adc
->regs
+ S3C2410_ADCDAT1
);
294 adc_dbg(adc
, "read %d: 0x%04x, 0x%04x\n", client
->nr_samples
, data0
, data1
);
296 client
->nr_samples
--;
298 if (cpu
== TYPE_ADCV1
|| cpu
== TYPE_ADCV11
) {
302 /* S3C2416/S3C64XX/S5P ADC resolution is 12-bit */
307 if (client
->convert_cb
)
308 (client
->convert_cb
)(client
, data0
, data1
, &client
->nr_samples
);
310 if (client
->nr_samples
> 0) {
311 /* fire another conversion for this */
313 client
->select_cb(client
, 1);
314 s3c_adc_convert(adc
);
316 spin_lock(&adc
->lock
);
317 (client
->select_cb
)(client
, 0);
321 spin_unlock(&adc
->lock
);
325 if (cpu
== TYPE_ADCV2
|| cpu
== TYPE_ADCV3
) {
326 /* Clear ADC interrupt */
327 writel(0, adc
->regs
+ S3C64XX_ADCCLRINT
);
332 static int s3c_adc_probe(struct platform_device
*pdev
)
334 struct device
*dev
= &pdev
->dev
;
335 struct adc_device
*adc
;
336 enum s3c_cpu_type cpu
= platform_get_device_id(pdev
)->driver_data
;
340 adc
= devm_kzalloc(dev
, sizeof(*adc
), GFP_KERNEL
);
344 spin_lock_init(&adc
->lock
);
347 adc
->prescale
= S3C2410_ADCCON_PRSCVL(49);
349 adc
->vdd
= devm_regulator_get(dev
, "vdd");
350 if (IS_ERR(adc
->vdd
)) {
351 dev_err(dev
, "operating without regulator \"vdd\" .\n");
352 return PTR_ERR(adc
->vdd
);
355 adc
->irq
= platform_get_irq(pdev
, 1);
359 ret
= devm_request_irq(dev
, adc
->irq
, s3c_adc_irq
, 0, dev_name(dev
),
362 dev_err(dev
, "failed to attach adc irq\n");
366 adc
->clk
= devm_clk_get(dev
, "adc");
367 if (IS_ERR(adc
->clk
)) {
368 dev_err(dev
, "failed to get adc clock\n");
369 return PTR_ERR(adc
->clk
);
372 adc
->regs
= devm_platform_ioremap_resource(pdev
, 0);
373 if (IS_ERR(adc
->regs
))
374 return PTR_ERR(adc
->regs
);
376 ret
= regulator_enable(adc
->vdd
);
380 clk_prepare_enable(adc
->clk
);
382 tmp
= adc
->prescale
| S3C2410_ADCCON_PRSCEN
;
384 /* Enable 12-bit ADC resolution */
385 if (cpu
== TYPE_ADCV12
)
386 tmp
|= S3C2416_ADCCON_RESSEL
;
387 if (cpu
== TYPE_ADCV2
|| cpu
== TYPE_ADCV3
)
388 tmp
|= S3C64XX_ADCCON_RESSEL
;
390 writel(tmp
, adc
->regs
+ S3C2410_ADCCON
);
392 dev_info(dev
, "attached adc driver\n");
394 platform_set_drvdata(pdev
, adc
);
400 static int s3c_adc_remove(struct platform_device
*pdev
)
402 struct adc_device
*adc
= platform_get_drvdata(pdev
);
404 clk_disable_unprepare(adc
->clk
);
405 regulator_disable(adc
->vdd
);
411 static int s3c_adc_suspend(struct device
*dev
)
413 struct adc_device
*adc
= dev_get_drvdata(dev
);
417 spin_lock_irqsave(&adc
->lock
, flags
);
419 con
= readl(adc
->regs
+ S3C2410_ADCCON
);
420 con
|= S3C2410_ADCCON_STDBM
;
421 writel(con
, adc
->regs
+ S3C2410_ADCCON
);
423 disable_irq(adc
->irq
);
424 spin_unlock_irqrestore(&adc
->lock
, flags
);
425 clk_disable(adc
->clk
);
426 regulator_disable(adc
->vdd
);
431 static int s3c_adc_resume(struct device
*dev
)
433 struct platform_device
*pdev
= to_platform_device(dev
);
434 struct adc_device
*adc
= platform_get_drvdata(pdev
);
435 enum s3c_cpu_type cpu
= platform_get_device_id(pdev
)->driver_data
;
439 ret
= regulator_enable(adc
->vdd
);
442 clk_enable(adc
->clk
);
443 enable_irq(adc
->irq
);
445 tmp
= adc
->prescale
| S3C2410_ADCCON_PRSCEN
;
447 /* Enable 12-bit ADC resolution */
448 if (cpu
== TYPE_ADCV12
)
449 tmp
|= S3C2416_ADCCON_RESSEL
;
450 if (cpu
== TYPE_ADCV2
|| cpu
== TYPE_ADCV3
)
451 tmp
|= S3C64XX_ADCCON_RESSEL
;
453 writel(tmp
, adc
->regs
+ S3C2410_ADCCON
);
459 #define s3c_adc_suspend NULL
460 #define s3c_adc_resume NULL
463 static const struct platform_device_id s3c_adc_driver_ids
[] = {
465 .name
= "s3c24xx-adc",
466 .driver_data
= TYPE_ADCV1
,
468 .name
= "s3c2443-adc",
469 .driver_data
= TYPE_ADCV11
,
471 .name
= "s3c2416-adc",
472 .driver_data
= TYPE_ADCV12
,
474 .name
= "s3c64xx-adc",
475 .driver_data
= TYPE_ADCV2
,
477 .name
= "samsung-adc-v3",
478 .driver_data
= TYPE_ADCV3
,
482 MODULE_DEVICE_TABLE(platform
, s3c_adc_driver_ids
);
484 static const struct dev_pm_ops adc_pm_ops
= {
485 .suspend
= s3c_adc_suspend
,
486 .resume
= s3c_adc_resume
,
489 static struct platform_driver s3c_adc_driver
= {
490 .id_table
= s3c_adc_driver_ids
,
495 .probe
= s3c_adc_probe
,
496 .remove
= s3c_adc_remove
,
499 static int __init
adc_init(void)
503 ret
= platform_driver_register(&s3c_adc_driver
);
505 printk(KERN_ERR
"%s: failed to add adc driver\n", __func__
);
510 module_init(adc_init
);