USB: serial: option: reimplement interface masking
[linux/fpc-iii.git] / arch / arm / plat-samsung / adc.c
blob42bac8d5ab5d9166fb3567f8574fd7bc7d2bb6bf
1 // SPDX-License-Identifier: GPL-1.0+
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
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/platform_device.h>
12 #include <linux/sched.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/regulator/consumer.h>
21 #include <plat/regs-adc.h>
22 #include <plat/adc.h>
24 /* This driver is designed to control the usage of the ADC block between
25 * the touchscreen and any other drivers that may need to use it, such as
26 * the hwmon driver.
28 * Priority will be given to the touchscreen driver, but as this itself is
29 * rate limited it should not starve other requests which are processed in
30 * order that they are received.
32 * Each user registers to get a client block which uniquely identifies it
33 * and stores information such as the necessary functions to callback when
34 * action is required.
37 enum s3c_cpu_type {
38 TYPE_ADCV1, /* S3C24XX */
39 TYPE_ADCV11, /* S3C2443 */
40 TYPE_ADCV12, /* S3C2416, S3C2450 */
41 TYPE_ADCV2, /* S3C64XX */
42 TYPE_ADCV3, /* S5PV210, S5PC110, EXYNOS4210 */
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;
51 int result;
52 unsigned char is_ts;
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);
61 struct 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 spinlock_t lock;
70 unsigned int prescale;
72 int irq;
73 struct regulator *vdd;
76 static struct adc_device *adc_dev;
78 static LIST_HEAD(adc_pending); /* protected by adc_device.lock */
80 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
82 static inline void s3c_adc_convert(struct adc_device *adc)
84 unsigned con = readl(adc->regs + S3C2410_ADCCON);
86 con |= S3C2410_ADCCON_ENABLE_START;
87 writel(con, adc->regs + S3C2410_ADCCON);
90 static inline void s3c_adc_select(struct adc_device *adc,
91 struct s3c_adc_client *client)
93 unsigned con = readl(adc->regs + S3C2410_ADCCON);
94 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
96 client->select_cb(client, 1);
98 if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV2)
99 con &= ~S3C2410_ADCCON_MUXMASK;
100 con &= ~S3C2410_ADCCON_STDBM;
101 con &= ~S3C2410_ADCCON_STARTMASK;
103 if (!client->is_ts) {
104 if (cpu == TYPE_ADCV3)
105 writel(client->channel & 0xf, adc->regs + S5P_ADCMUX);
106 else if (cpu == TYPE_ADCV11 || cpu == TYPE_ADCV12)
107 writel(client->channel & 0xf,
108 adc->regs + S3C2443_ADCMUX);
109 else
110 con |= S3C2410_ADCCON_SELMUX(client->channel);
113 writel(con, adc->regs + S3C2410_ADCCON);
116 static void s3c_adc_dbgshow(struct adc_device *adc)
118 adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
119 readl(adc->regs + S3C2410_ADCCON),
120 readl(adc->regs + S3C2410_ADCTSC),
121 readl(adc->regs + S3C2410_ADCDLY));
124 static void s3c_adc_try(struct adc_device *adc)
126 struct s3c_adc_client *next = adc->ts_pend;
128 if (!next && !list_empty(&adc_pending)) {
129 next = list_first_entry(&adc_pending,
130 struct s3c_adc_client, pend);
131 list_del(&next->pend);
132 } else
133 adc->ts_pend = NULL;
135 if (next) {
136 adc_dbg(adc, "new client is %p\n", next);
137 adc->cur = next;
138 s3c_adc_select(adc, next);
139 s3c_adc_convert(adc);
140 s3c_adc_dbgshow(adc);
144 int s3c_adc_start(struct s3c_adc_client *client,
145 unsigned int channel, unsigned int nr_samples)
147 struct adc_device *adc = adc_dev;
148 unsigned long flags;
150 if (!adc) {
151 printk(KERN_ERR "%s: failed to find adc\n", __func__);
152 return -EINVAL;
155 spin_lock_irqsave(&adc->lock, flags);
157 if (client->is_ts && adc->ts_pend) {
158 spin_unlock_irqrestore(&adc->lock, flags);
159 return -EAGAIN;
162 client->channel = channel;
163 client->nr_samples = nr_samples;
165 if (client->is_ts)
166 adc->ts_pend = client;
167 else
168 list_add_tail(&client->pend, &adc_pending);
170 if (!adc->cur)
171 s3c_adc_try(adc);
173 spin_unlock_irqrestore(&adc->lock, flags);
175 return 0;
177 EXPORT_SYMBOL_GPL(s3c_adc_start);
179 static void s3c_convert_done(struct s3c_adc_client *client,
180 unsigned v, unsigned u, unsigned *left)
182 client->result = v;
183 wake_up(client->wait);
186 int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
188 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
189 int ret;
191 client->convert_cb = s3c_convert_done;
192 client->wait = &wake;
193 client->result = -1;
195 ret = s3c_adc_start(client, ch, 1);
196 if (ret < 0)
197 goto err;
199 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
200 if (client->result < 0) {
201 ret = -ETIMEDOUT;
202 goto err;
205 client->convert_cb = NULL;
206 return client->result;
208 err:
209 return ret;
211 EXPORT_SYMBOL_GPL(s3c_adc_read);
213 static void s3c_adc_default_select(struct s3c_adc_client *client,
214 unsigned select)
218 struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
219 void (*select)(struct s3c_adc_client *client,
220 unsigned int selected),
221 void (*conv)(struct s3c_adc_client *client,
222 unsigned d0, unsigned d1,
223 unsigned *samples_left),
224 unsigned int is_ts)
226 struct s3c_adc_client *client;
228 WARN_ON(!pdev);
230 if (!select)
231 select = s3c_adc_default_select;
233 if (!pdev)
234 return ERR_PTR(-EINVAL);
236 client = kzalloc(sizeof(*client), GFP_KERNEL);
237 if (!client)
238 return ERR_PTR(-ENOMEM);
240 client->pdev = pdev;
241 client->is_ts = is_ts;
242 client->select_cb = select;
243 client->convert_cb = conv;
245 return client;
247 EXPORT_SYMBOL_GPL(s3c_adc_register);
249 void s3c_adc_release(struct s3c_adc_client *client)
251 unsigned long flags;
253 spin_lock_irqsave(&adc_dev->lock, flags);
255 /* We should really check that nothing is in progress. */
256 if (adc_dev->cur == client)
257 adc_dev->cur = NULL;
258 if (adc_dev->ts_pend == client)
259 adc_dev->ts_pend = NULL;
260 else {
261 struct list_head *p, *n;
262 struct s3c_adc_client *tmp;
264 list_for_each_safe(p, n, &adc_pending) {
265 tmp = list_entry(p, struct s3c_adc_client, pend);
266 if (tmp == client)
267 list_del(&tmp->pend);
271 if (adc_dev->cur == NULL)
272 s3c_adc_try(adc_dev);
274 spin_unlock_irqrestore(&adc_dev->lock, flags);
275 kfree(client);
277 EXPORT_SYMBOL_GPL(s3c_adc_release);
279 static irqreturn_t s3c_adc_irq(int irq, void *pw)
281 struct adc_device *adc = pw;
282 struct s3c_adc_client *client = adc->cur;
283 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
284 unsigned data0, data1;
286 if (!client) {
287 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
288 goto exit;
291 data0 = readl(adc->regs + S3C2410_ADCDAT0);
292 data1 = readl(adc->regs + S3C2410_ADCDAT1);
293 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
295 client->nr_samples--;
297 if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV11) {
298 data0 &= 0x3ff;
299 data1 &= 0x3ff;
300 } else {
301 /* S3C2416/S3C64XX/S5P ADC resolution is 12-bit */
302 data0 &= 0xfff;
303 data1 &= 0xfff;
306 if (client->convert_cb)
307 (client->convert_cb)(client, data0, data1, &client->nr_samples);
309 if (client->nr_samples > 0) {
310 /* fire another conversion for this */
312 client->select_cb(client, 1);
313 s3c_adc_convert(adc);
314 } else {
315 spin_lock(&adc->lock);
316 (client->select_cb)(client, 0);
317 adc->cur = NULL;
319 s3c_adc_try(adc);
320 spin_unlock(&adc->lock);
323 exit:
324 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3) {
325 /* Clear ADC interrupt */
326 writel(0, adc->regs + S3C64XX_ADCCLRINT);
328 return IRQ_HANDLED;
331 static int s3c_adc_probe(struct platform_device *pdev)
333 struct device *dev = &pdev->dev;
334 struct adc_device *adc;
335 struct resource *regs;
336 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
337 int ret;
338 unsigned tmp;
340 adc = devm_kzalloc(dev, sizeof(*adc), GFP_KERNEL);
341 if (!adc)
342 return -ENOMEM;
344 spin_lock_init(&adc->lock);
346 adc->pdev = pdev;
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);
356 if (adc->irq <= 0) {
357 dev_err(dev, "failed to get adc irq\n");
358 return -ENOENT;
361 ret = devm_request_irq(dev, adc->irq, s3c_adc_irq, 0, dev_name(dev),
362 adc);
363 if (ret < 0) {
364 dev_err(dev, "failed to attach adc irq\n");
365 return ret;
368 adc->clk = devm_clk_get(dev, "adc");
369 if (IS_ERR(adc->clk)) {
370 dev_err(dev, "failed to get adc clock\n");
371 return PTR_ERR(adc->clk);
374 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
375 adc->regs = devm_ioremap_resource(dev, regs);
376 if (IS_ERR(adc->regs))
377 return PTR_ERR(adc->regs);
379 ret = regulator_enable(adc->vdd);
380 if (ret)
381 return ret;
383 clk_prepare_enable(adc->clk);
385 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
387 /* Enable 12-bit ADC resolution */
388 if (cpu == TYPE_ADCV12)
389 tmp |= S3C2416_ADCCON_RESSEL;
390 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
391 tmp |= S3C64XX_ADCCON_RESSEL;
393 writel(tmp, adc->regs + S3C2410_ADCCON);
395 dev_info(dev, "attached adc driver\n");
397 platform_set_drvdata(pdev, adc);
398 adc_dev = adc;
400 return 0;
403 static int s3c_adc_remove(struct platform_device *pdev)
405 struct adc_device *adc = platform_get_drvdata(pdev);
407 clk_disable_unprepare(adc->clk);
408 regulator_disable(adc->vdd);
410 return 0;
413 #ifdef CONFIG_PM
414 static int s3c_adc_suspend(struct device *dev)
416 struct platform_device *pdev = to_platform_device(dev);
417 struct adc_device *adc = platform_get_drvdata(pdev);
418 unsigned long flags;
419 u32 con;
421 spin_lock_irqsave(&adc->lock, flags);
423 con = readl(adc->regs + S3C2410_ADCCON);
424 con |= S3C2410_ADCCON_STDBM;
425 writel(con, adc->regs + S3C2410_ADCCON);
427 disable_irq(adc->irq);
428 spin_unlock_irqrestore(&adc->lock, flags);
429 clk_disable(adc->clk);
430 regulator_disable(adc->vdd);
432 return 0;
435 static int s3c_adc_resume(struct device *dev)
437 struct platform_device *pdev = to_platform_device(dev);
438 struct adc_device *adc = platform_get_drvdata(pdev);
439 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
440 int ret;
441 unsigned long tmp;
443 ret = regulator_enable(adc->vdd);
444 if (ret)
445 return ret;
446 clk_enable(adc->clk);
447 enable_irq(adc->irq);
449 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
451 /* Enable 12-bit ADC resolution */
452 if (cpu == TYPE_ADCV12)
453 tmp |= S3C2416_ADCCON_RESSEL;
454 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
455 tmp |= S3C64XX_ADCCON_RESSEL;
457 writel(tmp, adc->regs + S3C2410_ADCCON);
459 return 0;
462 #else
463 #define s3c_adc_suspend NULL
464 #define s3c_adc_resume NULL
465 #endif
467 static const struct platform_device_id s3c_adc_driver_ids[] = {
469 .name = "s3c24xx-adc",
470 .driver_data = TYPE_ADCV1,
471 }, {
472 .name = "s3c2443-adc",
473 .driver_data = TYPE_ADCV11,
474 }, {
475 .name = "s3c2416-adc",
476 .driver_data = TYPE_ADCV12,
477 }, {
478 .name = "s3c64xx-adc",
479 .driver_data = TYPE_ADCV2,
480 }, {
481 .name = "samsung-adc-v3",
482 .driver_data = TYPE_ADCV3,
486 MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
488 static const struct dev_pm_ops adc_pm_ops = {
489 .suspend = s3c_adc_suspend,
490 .resume = s3c_adc_resume,
493 static struct platform_driver s3c_adc_driver = {
494 .id_table = s3c_adc_driver_ids,
495 .driver = {
496 .name = "s3c-adc",
497 .pm = &adc_pm_ops,
499 .probe = s3c_adc_probe,
500 .remove = s3c_adc_remove,
503 static int __init adc_init(void)
505 int ret;
507 ret = platform_driver_register(&s3c_adc_driver);
508 if (ret)
509 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
511 return ret;
514 module_init(adc_init);