blob: 2d676ab50f7300c15dd3753fdde2edb1f5570ee6 [file] [log] [blame]
Maurus Cuelenaere3929e1e2010-01-14 00:30:31 +01001/* arch/arm/plat-samsung/adc.c
Ben Dooks28ab44c5b2008-12-18 14:20:04 +00002 *
3 * Copyright (c) 2008 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
6 *
Maurus Cuelenaere3929e1e2010-01-14 00:30:31 +01007 * Samsung ADC device core
Ben Dooks28ab44c5b2008-12-18 14:20:04 +00008 *
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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040017#include <linux/sched.h>
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000018#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000020#include <linux/err.h>
21#include <linux/clk.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
MyungJoo Hamf4629042011-07-20 21:08:17 +090024#include <linux/regulator/consumer.h>
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000025
26#include <plat/regs-adc.h>
27#include <plat/adc.h>
28
29/* This driver is designed to control the usage of the ADC block between
30 * the touchscreen and any other drivers that may need to use it, such as
31 * the hwmon driver.
32 *
33 * Priority will be given to the touchscreen driver, but as this itself is
34 * rate limited it should not starve other requests which are processed in
35 * order that they are received.
36 *
37 * Each user registers to get a client block which uniquely identifies it
38 * and stores information such as the necessary functions to callback when
39 * action is required.
40 */
41
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +010042enum s3c_cpu_type {
MyungJoo Ham64df92e2011-07-20 21:08:18 +090043 TYPE_ADCV1, /* S3C24XX */
Heiko Stuebner6247cea2011-10-12 21:34:12 +090044 TYPE_ADCV11, /* S3C2443 */
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +090045 TYPE_ADCV12, /* S3C2416, S3C2450 */
MyungJoo Ham64df92e2011-07-20 21:08:18 +090046 TYPE_ADCV2, /* S3C64XX, S5P64X0, S5PC100 */
47 TYPE_ADCV3, /* S5PV210, S5PC110, EXYNOS4210 */
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +010048};
49
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000050struct s3c_adc_client {
51 struct platform_device *pdev;
52 struct list_head pend;
Ben Dookse170adc2009-07-18 10:12:27 +010053 wait_queue_head_t *wait;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000054
55 unsigned int nr_samples;
Ben Dookse170adc2009-07-18 10:12:27 +010056 int result;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000057 unsigned char is_ts;
58 unsigned char channel;
59
Ben Dookse170adc2009-07-18 10:12:27 +010060 void (*select_cb)(struct s3c_adc_client *c, unsigned selected);
61 void (*convert_cb)(struct s3c_adc_client *c,
62 unsigned val1, unsigned val2,
Nelson Castillo3f7ea462009-05-08 08:10:12 -050063 unsigned *samples_left);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000064};
65
66struct adc_device {
67 struct platform_device *pdev;
68 struct platform_device *owner;
69 struct clk *clk;
70 struct s3c_adc_client *cur;
71 struct s3c_adc_client *ts_pend;
72 void __iomem *regs;
Ben Dooks1f1f5842010-05-10 13:31:36 +090073 spinlock_t lock;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000074
75 unsigned int prescale;
76
77 int irq;
MyungJoo Hamf4629042011-07-20 21:08:17 +090078 struct regulator *vdd;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000079};
80
81static struct adc_device *adc_dev;
82
Ben Dooks1f1f5842010-05-10 13:31:36 +090083static LIST_HEAD(adc_pending); /* protected by adc_device.lock */
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000084
85#define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
86
87static inline void s3c_adc_convert(struct adc_device *adc)
88{
89 unsigned con = readl(adc->regs + S3C2410_ADCCON);
90
91 con |= S3C2410_ADCCON_ENABLE_START;
92 writel(con, adc->regs + S3C2410_ADCCON);
93}
94
95static inline void s3c_adc_select(struct adc_device *adc,
96 struct s3c_adc_client *client)
97{
98 unsigned con = readl(adc->regs + S3C2410_ADCCON);
MyungJoo Ham64df92e2011-07-20 21:08:18 +090099 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000100
Ben Dookse170adc2009-07-18 10:12:27 +0100101 client->select_cb(client, 1);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000102
Heiko Stuebnerdf303e02011-10-12 21:34:11 +0900103 if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV2)
104 con &= ~S3C2410_ADCCON_MUXMASK;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000105 con &= ~S3C2410_ADCCON_STDBM;
106 con &= ~S3C2410_ADCCON_STARTMASK;
107
MyungJoo Ham64df92e2011-07-20 21:08:18 +0900108 if (!client->is_ts) {
109 if (cpu == TYPE_ADCV3)
110 writel(client->channel & 0xf, adc->regs + S5P_ADCMUX);
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900111 else if (cpu == TYPE_ADCV11 || cpu == TYPE_ADCV12)
Heiko Stuebner6247cea2011-10-12 21:34:12 +0900112 writel(client->channel & 0xf,
113 adc->regs + S3C2443_ADCMUX);
MyungJoo Ham64df92e2011-07-20 21:08:18 +0900114 else
115 con |= S3C2410_ADCCON_SELMUX(client->channel);
116 }
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000117
118 writel(con, adc->regs + S3C2410_ADCCON);
119}
120
121static void s3c_adc_dbgshow(struct adc_device *adc)
122{
123 adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
124 readl(adc->regs + S3C2410_ADCCON),
125 readl(adc->regs + S3C2410_ADCTSC),
126 readl(adc->regs + S3C2410_ADCDLY));
127}
128
Ben Dooksf8c8ac82009-04-17 12:36:49 +0100129static void s3c_adc_try(struct adc_device *adc)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000130{
131 struct s3c_adc_client *next = adc->ts_pend;
132
133 if (!next && !list_empty(&adc_pending)) {
134 next = list_first_entry(&adc_pending,
135 struct s3c_adc_client, pend);
136 list_del(&next->pend);
137 } else
138 adc->ts_pend = NULL;
139
140 if (next) {
141 adc_dbg(adc, "new client is %p\n", next);
142 adc->cur = next;
143 s3c_adc_select(adc, next);
144 s3c_adc_convert(adc);
145 s3c_adc_dbgshow(adc);
146 }
147}
148
149int s3c_adc_start(struct s3c_adc_client *client,
150 unsigned int channel, unsigned int nr_samples)
151{
152 struct adc_device *adc = adc_dev;
153 unsigned long flags;
154
155 if (!adc) {
156 printk(KERN_ERR "%s: failed to find adc\n", __func__);
157 return -EINVAL;
158 }
159
Ben Dooks1f1f5842010-05-10 13:31:36 +0900160 spin_lock_irqsave(&adc->lock, flags);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000161
Todd Poynor8265981b2012-07-13 15:30:48 +0900162 if (client->is_ts && adc->ts_pend) {
163 spin_unlock_irqrestore(&adc->lock, flags);
164 return -EAGAIN;
165 }
166
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000167 client->channel = channel;
168 client->nr_samples = nr_samples;
169
170 if (client->is_ts)
171 adc->ts_pend = client;
172 else
173 list_add_tail(&client->pend, &adc_pending);
174
175 if (!adc->cur)
176 s3c_adc_try(adc);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900177
178 spin_unlock_irqrestore(&adc->lock, flags);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000179
180 return 0;
181}
182EXPORT_SYMBOL_GPL(s3c_adc_start);
183
Ben Dookse170adc2009-07-18 10:12:27 +0100184static void s3c_convert_done(struct s3c_adc_client *client,
185 unsigned v, unsigned u, unsigned *left)
186{
187 client->result = v;
188 wake_up(client->wait);
189}
190
191int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
192{
193 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
194 int ret;
195
196 client->convert_cb = s3c_convert_done;
197 client->wait = &wake;
198 client->result = -1;
199
200 ret = s3c_adc_start(client, ch, 1);
201 if (ret < 0)
202 goto err;
203
204 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
205 if (client->result < 0) {
206 ret = -ETIMEDOUT;
207 goto err;
208 }
209
210 client->convert_cb = NULL;
211 return client->result;
212
213err:
214 return ret;
215}
Ryan Mallond3bf3952009-10-14 09:18:30 +1300216EXPORT_SYMBOL_GPL(s3c_adc_read);
Ben Dookse170adc2009-07-18 10:12:27 +0100217
218static void s3c_adc_default_select(struct s3c_adc_client *client,
219 unsigned select)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000220{
221}
222
223struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
Ben Dookse170adc2009-07-18 10:12:27 +0100224 void (*select)(struct s3c_adc_client *client,
225 unsigned int selected),
226 void (*conv)(struct s3c_adc_client *client,
227 unsigned d0, unsigned d1,
Nelson Castillo3f7ea462009-05-08 08:10:12 -0500228 unsigned *samples_left),
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000229 unsigned int is_ts)
230{
231 struct s3c_adc_client *client;
232
233 WARN_ON(!pdev);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000234
235 if (!select)
236 select = s3c_adc_default_select;
237
Ben Dookse170adc2009-07-18 10:12:27 +0100238 if (!pdev)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000239 return ERR_PTR(-EINVAL);
240
241 client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
242 if (!client) {
243 dev_err(&pdev->dev, "no memory for adc client\n");
244 return ERR_PTR(-ENOMEM);
245 }
246
247 client->pdev = pdev;
248 client->is_ts = is_ts;
249 client->select_cb = select;
250 client->convert_cb = conv;
251
252 return client;
253}
254EXPORT_SYMBOL_GPL(s3c_adc_register);
255
256void s3c_adc_release(struct s3c_adc_client *client)
257{
Ben Dooks1f1f5842010-05-10 13:31:36 +0900258 unsigned long flags;
259
260 spin_lock_irqsave(&adc_dev->lock, flags);
261
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000262 /* We should really check that nothing is in progress. */
Ramax Lo0c3ee072009-04-14 23:56:18 +0800263 if (adc_dev->cur == client)
264 adc_dev->cur = NULL;
265 if (adc_dev->ts_pend == client)
266 adc_dev->ts_pend = NULL;
267 else {
268 struct list_head *p, *n;
269 struct s3c_adc_client *tmp;
270
271 list_for_each_safe(p, n, &adc_pending) {
272 tmp = list_entry(p, struct s3c_adc_client, pend);
273 if (tmp == client)
274 list_del(&tmp->pend);
275 }
276 }
277
278 if (adc_dev->cur == NULL)
279 s3c_adc_try(adc_dev);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900280
281 spin_unlock_irqrestore(&adc_dev->lock, flags);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000282 kfree(client);
283}
284EXPORT_SYMBOL_GPL(s3c_adc_release);
285
286static irqreturn_t s3c_adc_irq(int irq, void *pw)
287{
288 struct adc_device *adc = pw;
289 struct s3c_adc_client *client = adc->cur;
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100290 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000291 unsigned data0, data1;
292
293 if (!client) {
294 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100295 goto exit;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000296 }
297
298 data0 = readl(adc->regs + S3C2410_ADCDAT0);
299 data1 = readl(adc->regs + S3C2410_ADCDAT1);
300 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
301
Nelson Castillo3f7ea462009-05-08 08:10:12 -0500302 client->nr_samples--;
Ben Dookse170adc2009-07-18 10:12:27 +0100303
Heiko Stuebner6247cea2011-10-12 21:34:12 +0900304 if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV11) {
305 data0 &= 0x3ff;
306 data1 &= 0x3ff;
307 } else {
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900308 /* S3C2416/S3C64XX/S5P ADC resolution is 12-bit */
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100309 data0 &= 0xfff;
310 data1 &= 0xfff;
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100311 }
312
Ben Dookse170adc2009-07-18 10:12:27 +0100313 if (client->convert_cb)
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100314 (client->convert_cb)(client, data0, data1, &client->nr_samples);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000315
Nelson Castillo3f7ea462009-05-08 08:10:12 -0500316 if (client->nr_samples > 0) {
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000317 /* fire another conversion for this */
318
Ben Dookse170adc2009-07-18 10:12:27 +0100319 client->select_cb(client, 1);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000320 s3c_adc_convert(adc);
321 } else {
Ben Dooks1f1f5842010-05-10 13:31:36 +0900322 spin_lock(&adc->lock);
Ben Dookse170adc2009-07-18 10:12:27 +0100323 (client->select_cb)(client, 0);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000324 adc->cur = NULL;
325
326 s3c_adc_try(adc);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900327 spin_unlock(&adc->lock);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000328 }
329
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100330exit:
Heiko Stuebner6247cea2011-10-12 21:34:12 +0900331 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3) {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100332 /* Clear ADC interrupt */
333 writel(0, adc->regs + S3C64XX_ADCCLRINT);
334 }
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000335 return IRQ_HANDLED;
336}
337
338static int s3c_adc_probe(struct platform_device *pdev)
339{
340 struct device *dev = &pdev->dev;
341 struct adc_device *adc;
342 struct resource *regs;
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900343 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000344 int ret;
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100345 unsigned tmp;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000346
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900347 adc = devm_kzalloc(dev, sizeof(struct adc_device), GFP_KERNEL);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000348 if (adc == NULL) {
349 dev_err(dev, "failed to allocate adc_device\n");
350 return -ENOMEM;
351 }
352
Ben Dooks1f1f5842010-05-10 13:31:36 +0900353 spin_lock_init(&adc->lock);
354
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000355 adc->pdev = pdev;
356 adc->prescale = S3C2410_ADCCON_PRSCVL(49);
357
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900358 adc->vdd = devm_regulator_get(dev, "vdd");
MyungJoo Hamf4629042011-07-20 21:08:17 +0900359 if (IS_ERR(adc->vdd)) {
360 dev_err(dev, "operating without regulator \"vdd\" .\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900361 return PTR_ERR(adc->vdd);
MyungJoo Hamf4629042011-07-20 21:08:17 +0900362 }
363
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000364 adc->irq = platform_get_irq(pdev, 1);
365 if (adc->irq <= 0) {
366 dev_err(dev, "failed to get adc irq\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900367 return -ENOENT;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000368 }
369
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900370 ret = devm_request_irq(dev, adc->irq, s3c_adc_irq, 0, dev_name(dev),
371 adc);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000372 if (ret < 0) {
373 dev_err(dev, "failed to attach adc irq\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900374 return ret;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000375 }
376
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900377 adc->clk = devm_clk_get(dev, "adc");
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000378 if (IS_ERR(adc->clk)) {
379 dev_err(dev, "failed to get adc clock\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900380 return PTR_ERR(adc->clk);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000381 }
382
383 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
384 if (!regs) {
385 dev_err(dev, "failed to find registers\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900386 return -ENXIO;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000387 }
388
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900389 adc->regs = devm_request_and_ioremap(dev, regs);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000390 if (!adc->regs) {
391 dev_err(dev, "failed to map registers\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900392 return -ENXIO;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000393 }
394
MyungJoo Hamf4629042011-07-20 21:08:17 +0900395 ret = regulator_enable(adc->vdd);
396 if (ret)
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900397 return ret;
MyungJoo Hamf4629042011-07-20 21:08:17 +0900398
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000399 clk_enable(adc->clk);
400
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100401 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900402
403 /* Enable 12-bit ADC resolution */
404 if (cpu == TYPE_ADCV12)
405 tmp |= S3C2416_ADCCON_RESSEL;
406 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100407 tmp |= S3C64XX_ADCCON_RESSEL;
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900408
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100409 writel(tmp, adc->regs + S3C2410_ADCCON);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000410
411 dev_info(dev, "attached adc driver\n");
412
413 platform_set_drvdata(pdev, adc);
414 adc_dev = adc;
415
416 return 0;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000417}
418
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800419static int s3c_adc_remove(struct platform_device *pdev)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000420{
421 struct adc_device *adc = platform_get_drvdata(pdev);
422
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000423 clk_disable(adc->clk);
MyungJoo Hamf4629042011-07-20 21:08:17 +0900424 regulator_disable(adc->vdd);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000425
426 return 0;
427}
428
429#ifdef CONFIG_PM
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900430static int s3c_adc_suspend(struct device *dev)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000431{
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900432 struct platform_device *pdev = container_of(dev,
433 struct platform_device, dev);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000434 struct adc_device *adc = platform_get_drvdata(pdev);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900435 unsigned long flags;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000436 u32 con;
437
Ben Dooks1f1f5842010-05-10 13:31:36 +0900438 spin_lock_irqsave(&adc->lock, flags);
439
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000440 con = readl(adc->regs + S3C2410_ADCCON);
441 con |= S3C2410_ADCCON_STDBM;
442 writel(con, adc->regs + S3C2410_ADCCON);
443
Vasily Khoruzhicka0af8b32010-02-18 18:32:29 +0200444 disable_irq(adc->irq);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900445 spin_unlock_irqrestore(&adc->lock, flags);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000446 clk_disable(adc->clk);
MyungJoo Hamf4629042011-07-20 21:08:17 +0900447 regulator_disable(adc->vdd);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000448
449 return 0;
450}
451
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900452static int s3c_adc_resume(struct device *dev)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000453{
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900454 struct platform_device *pdev = container_of(dev,
455 struct platform_device, dev);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000456 struct adc_device *adc = platform_get_drvdata(pdev);
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900457 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
MyungJoo Hamf4629042011-07-20 21:08:17 +0900458 int ret;
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900459 unsigned long tmp;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000460
MyungJoo Hamf4629042011-07-20 21:08:17 +0900461 ret = regulator_enable(adc->vdd);
462 if (ret)
463 return ret;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000464 clk_enable(adc->clk);
Vasily Khoruzhicka0af8b32010-02-18 18:32:29 +0200465 enable_irq(adc->irq);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000466
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900467 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900468
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900469 /* Enable 12-bit ADC resolution */
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900470 if (cpu == TYPE_ADCV12)
471 tmp |= S3C2416_ADCCON_RESSEL;
472 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900473 tmp |= S3C64XX_ADCCON_RESSEL;
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900474
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900475 writel(tmp, adc->regs + S3C2410_ADCCON);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000476
477 return 0;
478}
479
480#else
481#define s3c_adc_suspend NULL
482#define s3c_adc_resume NULL
483#endif
484
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100485static struct platform_device_id s3c_adc_driver_ids[] = {
486 {
487 .name = "s3c24xx-adc",
MyungJoo Ham64df92e2011-07-20 21:08:18 +0900488 .driver_data = TYPE_ADCV1,
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100489 }, {
Heiko Stuebner6247cea2011-10-12 21:34:12 +0900490 .name = "s3c2443-adc",
491 .driver_data = TYPE_ADCV11,
492 }, {
Heiko Stuebner35cc3ce2011-10-12 21:34:12 +0900493 .name = "s3c2416-adc",
494 .driver_data = TYPE_ADCV12,
495 }, {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100496 .name = "s3c64xx-adc",
MyungJoo Ham64df92e2011-07-20 21:08:18 +0900497 .driver_data = TYPE_ADCV2,
498 }, {
499 .name = "samsung-adc-v3",
500 .driver_data = TYPE_ADCV3,
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100501 },
502 { }
503};
504MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
505
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900506static const struct dev_pm_ops adc_pm_ops = {
507 .suspend = s3c_adc_suspend,
508 .resume = s3c_adc_resume,
509};
510
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000511static struct platform_driver s3c_adc_driver = {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100512 .id_table = s3c_adc_driver_ids,
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000513 .driver = {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100514 .name = "s3c-adc",
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000515 .owner = THIS_MODULE,
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900516 .pm = &adc_pm_ops,
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000517 },
518 .probe = s3c_adc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800519 .remove = s3c_adc_remove,
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000520};
521
522static int __init adc_init(void)
523{
524 int ret;
525
526 ret = platform_driver_register(&s3c_adc_driver);
527 if (ret)
528 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
529
530 return ret;
531}
532
MyungJoo Hamf4629042011-07-20 21:08:17 +0900533module_init(adc_init);