aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/plat-samsung/adc.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-03-12 19:00:54 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2010-03-12 19:00:54 -0500
commitdca1d9f6d7ae428c193f32bd3e9a4ca13176648b (patch)
tree02de8c3503c1c811754423d2fa3f3b4978044f6e /arch/arm/plat-samsung/adc.c
parent9ff99339447de403a46be5e3f23d0c794d540b06 (diff)
parent91e013827c0bcbb187ecf02213c5446b6f62d445 (diff)
Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
* 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm: (370 commits) ARM: S3C2443: Add set_rate and round_rate calls for armdiv clock ARM: S3C2443: Remove #if 0 for clk_mpll ARM: S3C2443: Update notes on MPLLREF clock ARM: S3C2443: Further clksrc-clk conversions ARM: S3C2443: Change to using plat-samsung clksrc-clk implementation USB: Fix s3c-hsotg build following Samsung platform header moves ARM: S3C64XX: Reintroduce unconditional build of audio device ARM: 5961/1: ux500: fix CLKRST addresses ARM: 5977/1: arm: Enable backtrace printing on oops when PC is corrupted ASoC: Fix S3C64xx IIS driver for Samsung header reorg ARM: S3C2440: Fix plat-s3c24xx move of s3c2440/s3c2442 support [ARM] pxa: fix typo in mxm8x10.h [ARM] pxa/raumfeld: set GPIO drive bits for LED pins [ARM] pxa/zeus: Add support for mcp2515 CAN bus [ARM] pxa/zeus: Add support for onboard max6369 watchdog [ARM] pxa/zeus: Add Eurotech as the manufacturer [ARM] pxa/zeus: Correct the USB host initialisation flags [ARM] pxa/zeus: Allow usage of 8250-compatible UART in uncompress [ARM] pxa: refactor uncompress.h for non-PXA uarts [ARM] mmp2: fix incorrect calling of chip->mask_ack() for 2nd level cascaded IRQs ...
Diffstat (limited to 'arch/arm/plat-samsung/adc.c')
-rw-r--r--arch/arm/plat-samsung/adc.c474
1 files changed, 474 insertions, 0 deletions
diff --git a/arch/arm/plat-samsung/adc.c b/arch/arm/plat-samsung/adc.c
new file mode 100644
index 000000000000..0b5833b9ac5b
--- /dev/null
+++ b/arch/arm/plat-samsung/adc.c
@@ -0,0 +1,474 @@
1/* arch/arm/plat-samsung/adc.c
2 *
3 * Copyright (c) 2008 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
6 *
7 * Samsung ADC device core
8 *
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.
12*/
13
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>
22#include <linux/io.h>
23
24#include <plat/regs-adc.h>
25#include <plat/adc.h>
26
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
29 * the hwmon driver.
30 *
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.
34 *
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
37 * action is required.
38 */
39
40enum s3c_cpu_type {
41 TYPE_S3C24XX,
42 TYPE_S3C64XX
43};
44
45struct s3c_adc_client {
46 struct platform_device *pdev;
47 struct list_head pend;
48 wait_queue_head_t *wait;
49
50 unsigned int nr_samples;
51 int result;
52 unsigned char is_ts;
53 unsigned char channel;
54
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);
59};
60
61struct adc_device {
62 struct platform_device *pdev;
63 struct platform_device *owner;
64 struct clk *clk;
65 struct s3c_adc_client *cur;
66 struct s3c_adc_client *ts_pend;
67 void __iomem *regs;
68
69 unsigned int prescale;
70
71 int irq;
72};
73
74static struct adc_device *adc_dev;
75
76static LIST_HEAD(adc_pending);
77
78#define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
79
80static inline void s3c_adc_convert(struct adc_device *adc)
81{
82 unsigned con = readl(adc->regs + S3C2410_ADCCON);
83
84 con |= S3C2410_ADCCON_ENABLE_START;
85 writel(con, adc->regs + S3C2410_ADCCON);
86}
87
88static inline void s3c_adc_select(struct adc_device *adc,
89 struct s3c_adc_client *client)
90{
91 unsigned con = readl(adc->regs + S3C2410_ADCCON);
92
93 client->select_cb(client, 1);
94
95 con &= ~S3C2410_ADCCON_MUXMASK;
96 con &= ~S3C2410_ADCCON_STDBM;
97 con &= ~S3C2410_ADCCON_STARTMASK;
98
99 if (!client->is_ts)
100 con |= S3C2410_ADCCON_SELMUX(client->channel);
101
102 writel(con, adc->regs + S3C2410_ADCCON);
103}
104
105static void s3c_adc_dbgshow(struct adc_device *adc)
106{
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));
111}
112
113static void s3c_adc_try(struct adc_device *adc)
114{
115 struct s3c_adc_client *next = adc->ts_pend;
116
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);
121 } else
122 adc->ts_pend = NULL;
123
124 if (next) {
125 adc_dbg(adc, "new client is %p\n", next);
126 adc->cur = next;
127 s3c_adc_select(adc, next);
128 s3c_adc_convert(adc);
129 s3c_adc_dbgshow(adc);
130 }
131}
132
133int s3c_adc_start(struct s3c_adc_client *client,
134 unsigned int channel, unsigned int nr_samples)
135{
136 struct adc_device *adc = adc_dev;
137 unsigned long flags;
138
139 if (!adc) {
140 printk(KERN_ERR "%s: failed to find adc\n", __func__);
141 return -EINVAL;
142 }
143
144 if (client->is_ts && adc->ts_pend)
145 return -EAGAIN;
146
147 local_irq_save(flags);
148
149 client->channel = channel;
150 client->nr_samples = nr_samples;
151
152 if (client->is_ts)
153 adc->ts_pend = client;
154 else
155 list_add_tail(&client->pend, &adc_pending);
156
157 if (!adc->cur)
158 s3c_adc_try(adc);
159 local_irq_restore(flags);
160
161 return 0;
162}
163EXPORT_SYMBOL_GPL(s3c_adc_start);
164
165static void s3c_convert_done(struct s3c_adc_client *client,
166 unsigned v, unsigned u, unsigned *left)
167{
168 client->result = v;
169 wake_up(client->wait);
170}
171
172int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
173{
174 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
175 int ret;
176
177 client->convert_cb = s3c_convert_done;
178 client->wait = &wake;
179 client->result = -1;
180
181 ret = s3c_adc_start(client, ch, 1);
182 if (ret < 0)
183 goto err;
184
185 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
186 if (client->result < 0) {
187 ret = -ETIMEDOUT;
188 goto err;
189 }
190
191 client->convert_cb = NULL;
192 return client->result;
193
194err:
195 return ret;
196}
197EXPORT_SYMBOL_GPL(s3c_adc_read);
198
199static void s3c_adc_default_select(struct s3c_adc_client *client,
200 unsigned select)
201{
202}
203
204struct 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),
210 unsigned int is_ts)
211{
212 struct s3c_adc_client *client;
213
214 WARN_ON(!pdev);
215
216 if (!select)
217 select = s3c_adc_default_select;
218
219 if (!pdev)
220 return ERR_PTR(-EINVAL);
221
222 client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
223 if (!client) {
224 dev_err(&pdev->dev, "no memory for adc client\n");
225 return ERR_PTR(-ENOMEM);
226 }
227
228 client->pdev = pdev;
229 client->is_ts = is_ts;
230 client->select_cb = select;
231 client->convert_cb = conv;
232
233 return client;
234}
235EXPORT_SYMBOL_GPL(s3c_adc_register);
236
237void s3c_adc_release(struct s3c_adc_client *client)
238{
239 /* We should really check that nothing is in progress. */
240 if (adc_dev->cur == client)
241 adc_dev->cur = NULL;
242 if (adc_dev->ts_pend == client)
243 adc_dev->ts_pend = NULL;
244 else {
245 struct list_head *p, *n;
246 struct s3c_adc_client *tmp;
247
248 list_for_each_safe(p, n, &adc_pending) {
249 tmp = list_entry(p, struct s3c_adc_client, pend);
250 if (tmp == client)
251 list_del(&tmp->pend);
252 }
253 }
254
255 if (adc_dev->cur == NULL)
256 s3c_adc_try(adc_dev);
257 kfree(client);
258}
259EXPORT_SYMBOL_GPL(s3c_adc_release);
260
261static irqreturn_t s3c_adc_irq(int irq, void *pw)
262{
263 struct adc_device *adc = pw;
264 struct s3c_adc_client *client = adc->cur;
265 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
266 unsigned long flags;
267 unsigned data0, data1;
268
269 if (!client) {
270 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
271 goto exit;
272 }
273
274 data0 = readl(adc->regs + S3C2410_ADCDAT0);
275 data1 = readl(adc->regs + S3C2410_ADCDAT1);
276 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
277
278 client->nr_samples--;
279
280 if (cpu == TYPE_S3C64XX) {
281 /* S3C64XX ADC resolution is 12-bit */
282 data0 &= 0xfff;
283 data1 &= 0xfff;
284 } else {
285 data0 &= 0x3ff;
286 data1 &= 0x3ff;
287 }
288
289 if (client->convert_cb)
290 (client->convert_cb)(client, data0, data1, &client->nr_samples);
291
292 if (client->nr_samples > 0) {
293 /* fire another conversion for this */
294
295 client->select_cb(client, 1);
296 s3c_adc_convert(adc);
297 } else {
298 local_irq_save(flags);
299 (client->select_cb)(client, 0);
300 adc->cur = NULL;
301
302 s3c_adc_try(adc);
303 local_irq_restore(flags);
304 }
305
306exit:
307 if (cpu == TYPE_S3C64XX) {
308 /* Clear ADC interrupt */
309 writel(0, adc->regs + S3C64XX_ADCCLRINT);
310 }
311 return IRQ_HANDLED;
312}
313
314static int s3c_adc_probe(struct platform_device *pdev)
315{
316 struct device *dev = &pdev->dev;
317 struct adc_device *adc;
318 struct resource *regs;
319 int ret;
320 unsigned tmp;
321
322 adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
323 if (adc == NULL) {
324 dev_err(dev, "failed to allocate adc_device\n");
325 return -ENOMEM;
326 }
327
328 adc->pdev = pdev;
329 adc->prescale = S3C2410_ADCCON_PRSCVL(49);
330
331 adc->irq = platform_get_irq(pdev, 1);
332 if (adc->irq <= 0) {
333 dev_err(dev, "failed to get adc irq\n");
334 ret = -ENOENT;
335 goto err_alloc;
336 }
337
338 ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
339 if (ret < 0) {
340 dev_err(dev, "failed to attach adc irq\n");
341 goto err_alloc;
342 }
343
344 adc->clk = clk_get(dev, "adc");
345 if (IS_ERR(adc->clk)) {
346 dev_err(dev, "failed to get adc clock\n");
347 ret = PTR_ERR(adc->clk);
348 goto err_irq;
349 }
350
351 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
352 if (!regs) {
353 dev_err(dev, "failed to find registers\n");
354 ret = -ENXIO;
355 goto err_clk;
356 }
357
358 adc->regs = ioremap(regs->start, resource_size(regs));
359 if (!adc->regs) {
360 dev_err(dev, "failed to map registers\n");
361 ret = -ENXIO;
362 goto err_clk;
363 }
364
365 clk_enable(adc->clk);
366
367 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
368 if (platform_get_device_id(pdev)->driver_data == TYPE_S3C64XX) {
369 /* Enable 12-bit ADC resolution */
370 tmp |= S3C64XX_ADCCON_RESSEL;
371 }
372 writel(tmp, adc->regs + S3C2410_ADCCON);
373
374 dev_info(dev, "attached adc driver\n");
375
376 platform_set_drvdata(pdev, adc);
377 adc_dev = adc;
378
379 return 0;
380
381 err_clk:
382 clk_put(adc->clk);
383
384 err_irq:
385 free_irq(adc->irq, adc);
386
387 err_alloc:
388 kfree(adc);
389 return ret;
390}
391
392static int __devexit s3c_adc_remove(struct platform_device *pdev)
393{
394 struct adc_device *adc = platform_get_drvdata(pdev);
395
396 iounmap(adc->regs);
397 free_irq(adc->irq, adc);
398 clk_disable(adc->clk);
399 clk_put(adc->clk);
400 kfree(adc);
401
402 return 0;
403}
404
405#ifdef CONFIG_PM
406static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
407{
408 struct adc_device *adc = platform_get_drvdata(pdev);
409 u32 con;
410
411 con = readl(adc->regs + S3C2410_ADCCON);
412 con |= S3C2410_ADCCON_STDBM;
413 writel(con, adc->regs + S3C2410_ADCCON);
414
415 disable_irq(adc->irq);
416 clk_disable(adc->clk);
417
418 return 0;
419}
420
421static int s3c_adc_resume(struct platform_device *pdev)
422{
423 struct adc_device *adc = platform_get_drvdata(pdev);
424
425 clk_enable(adc->clk);
426 enable_irq(adc->irq);
427
428 writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
429 adc->regs + S3C2410_ADCCON);
430
431 return 0;
432}
433
434#else
435#define s3c_adc_suspend NULL
436#define s3c_adc_resume NULL
437#endif
438
439static struct platform_device_id s3c_adc_driver_ids[] = {
440 {
441 .name = "s3c24xx-adc",
442 .driver_data = TYPE_S3C24XX,
443 }, {
444 .name = "s3c64xx-adc",
445 .driver_data = TYPE_S3C64XX,
446 },
447 { }
448};
449MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
450
451static struct platform_driver s3c_adc_driver = {
452 .id_table = s3c_adc_driver_ids,
453 .driver = {
454 .name = "s3c-adc",
455 .owner = THIS_MODULE,
456 },
457 .probe = s3c_adc_probe,
458 .remove = __devexit_p(s3c_adc_remove),
459 .suspend = s3c_adc_suspend,
460 .resume = s3c_adc_resume,
461};
462
463static int __init adc_init(void)
464{
465 int ret;
466
467 ret = platform_driver_register(&s3c_adc_driver);
468 if (ret)
469 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
470
471 return ret;
472}
473
474arch_initcall(adc_init);