blob: e35e04417cce4a9abc2acd7614874f3d98cac00e [file] [log] [blame]
Krzysztof Kozlowski4490e3c2017-12-25 20:54:35 +01001// 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
Ben Dooks28ab44c5b2008-12-18 14:20:04 +00008
9#include <linux/module.h>
10#include <linux/kernel.h>
Randy Dunlap053a3892018-07-07 14:03:29 -070011#include <linux/mod_devicetable.h>
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000012#include <linux/platform_device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040013#include <linux/sched.h>
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000014#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000016#include <linux/err.h>
17#include <linux/clk.h>
18#include <linux/interrupt.h>
19#include <linux/io.h>
MyungJoo Hamf4629042011-07-20 21:08:17 +090020#include <linux/regulator/consumer.h>
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000021
22#include <plat/regs-adc.h>
Arnd Bergmann7dbad032020-08-06 20:20:39 +020023#include <linux/soc/samsung/s3c-adc.h>
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000024
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
27 * the hwmon driver.
28 *
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.
32 *
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
35 * action is required.
36 */
37
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +010038enum s3c_cpu_type {
MyungJoo Ham64df92e2011-07-20 21:08:18 +090039 TYPE_ADCV1, /* S3C24XX */
Heiko Stuebner6247cea2011-10-12 21:34:12 +090040 TYPE_ADCV11, /* S3C2443 */
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +090041 TYPE_ADCV12, /* S3C2416, S3C2450 */
Kukjin Kimb8529ec2014-07-02 07:51:50 +090042 TYPE_ADCV2, /* S3C64XX */
Krzysztof Kozlowski45984f02020-01-04 16:20:51 +010043 TYPE_ADCV3, /* S5PV210, S5PC110, Exynos4210 */
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +010044};
45
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000046struct s3c_adc_client {
47 struct platform_device *pdev;
48 struct list_head pend;
Ben Dookse170adc2009-07-18 10:12:27 +010049 wait_queue_head_t *wait;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000050
51 unsigned int nr_samples;
Ben Dookse170adc2009-07-18 10:12:27 +010052 int result;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000053 unsigned char is_ts;
54 unsigned char channel;
55
Ben Dookse170adc2009-07-18 10:12:27 +010056 void (*select_cb)(struct s3c_adc_client *c, unsigned selected);
57 void (*convert_cb)(struct s3c_adc_client *c,
58 unsigned val1, unsigned val2,
Nelson Castillo3f7ea462009-05-08 08:10:12 -050059 unsigned *samples_left);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000060};
61
62struct adc_device {
63 struct platform_device *pdev;
64 struct platform_device *owner;
65 struct clk *clk;
66 struct s3c_adc_client *cur;
67 struct s3c_adc_client *ts_pend;
68 void __iomem *regs;
Ben Dooks1f1f5842010-05-10 13:31:36 +090069 spinlock_t lock;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000070
71 unsigned int prescale;
72
73 int irq;
MyungJoo Hamf4629042011-07-20 21:08:17 +090074 struct regulator *vdd;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000075};
76
77static struct adc_device *adc_dev;
78
Ben Dooks1f1f5842010-05-10 13:31:36 +090079static LIST_HEAD(adc_pending); /* protected by adc_device.lock */
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000080
81#define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
82
83static inline void s3c_adc_convert(struct adc_device *adc)
84{
85 unsigned con = readl(adc->regs + S3C2410_ADCCON);
86
87 con |= S3C2410_ADCCON_ENABLE_START;
88 writel(con, adc->regs + S3C2410_ADCCON);
89}
90
91static inline void s3c_adc_select(struct adc_device *adc,
92 struct s3c_adc_client *client)
93{
94 unsigned con = readl(adc->regs + S3C2410_ADCCON);
MyungJoo Ham64df92e2011-07-20 21:08:18 +090095 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000096
Ben Dookse170adc2009-07-18 10:12:27 +010097 client->select_cb(client, 1);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +000098
Heiko Stuebnerdf303e02011-10-12 21:34:11 +090099 if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV2)
100 con &= ~S3C2410_ADCCON_MUXMASK;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000101 con &= ~S3C2410_ADCCON_STDBM;
102 con &= ~S3C2410_ADCCON_STARTMASK;
103
MyungJoo Ham64df92e2011-07-20 21:08:18 +0900104 if (!client->is_ts) {
105 if (cpu == TYPE_ADCV3)
106 writel(client->channel & 0xf, adc->regs + S5P_ADCMUX);
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900107 else if (cpu == TYPE_ADCV11 || cpu == TYPE_ADCV12)
Heiko Stuebner6247cea2011-10-12 21:34:12 +0900108 writel(client->channel & 0xf,
109 adc->regs + S3C2443_ADCMUX);
MyungJoo Ham64df92e2011-07-20 21:08:18 +0900110 else
111 con |= S3C2410_ADCCON_SELMUX(client->channel);
112 }
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000113
114 writel(con, adc->regs + S3C2410_ADCCON);
115}
116
117static void s3c_adc_dbgshow(struct adc_device *adc)
118{
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));
123}
124
Ben Dooksf8c8ac82009-04-17 12:36:49 +0100125static void s3c_adc_try(struct adc_device *adc)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000126{
127 struct s3c_adc_client *next = adc->ts_pend;
128
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);
133 } else
134 adc->ts_pend = NULL;
135
136 if (next) {
137 adc_dbg(adc, "new client is %p\n", next);
138 adc->cur = next;
139 s3c_adc_select(adc, next);
140 s3c_adc_convert(adc);
141 s3c_adc_dbgshow(adc);
142 }
143}
144
145int s3c_adc_start(struct s3c_adc_client *client,
146 unsigned int channel, unsigned int nr_samples)
147{
148 struct adc_device *adc = adc_dev;
149 unsigned long flags;
150
151 if (!adc) {
152 printk(KERN_ERR "%s: failed to find adc\n", __func__);
153 return -EINVAL;
154 }
155
Ben Dooks1f1f5842010-05-10 13:31:36 +0900156 spin_lock_irqsave(&adc->lock, flags);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000157
Todd Poynor8265981b2012-07-13 15:30:48 +0900158 if (client->is_ts && adc->ts_pend) {
159 spin_unlock_irqrestore(&adc->lock, flags);
160 return -EAGAIN;
161 }
162
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000163 client->channel = channel;
164 client->nr_samples = nr_samples;
165
166 if (client->is_ts)
167 adc->ts_pend = client;
168 else
169 list_add_tail(&client->pend, &adc_pending);
170
171 if (!adc->cur)
172 s3c_adc_try(adc);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900173
174 spin_unlock_irqrestore(&adc->lock, flags);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000175
176 return 0;
177}
178EXPORT_SYMBOL_GPL(s3c_adc_start);
179
Ben Dookse170adc2009-07-18 10:12:27 +0100180static void s3c_convert_done(struct s3c_adc_client *client,
181 unsigned v, unsigned u, unsigned *left)
182{
183 client->result = v;
184 wake_up(client->wait);
185}
186
187int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
188{
189 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
190 int ret;
191
192 client->convert_cb = s3c_convert_done;
193 client->wait = &wake;
194 client->result = -1;
195
196 ret = s3c_adc_start(client, ch, 1);
197 if (ret < 0)
198 goto err;
199
200 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
201 if (client->result < 0) {
202 ret = -ETIMEDOUT;
203 goto err;
204 }
205
206 client->convert_cb = NULL;
207 return client->result;
208
209err:
210 return ret;
211}
Ryan Mallond3bf3952009-10-14 09:18:30 +1300212EXPORT_SYMBOL_GPL(s3c_adc_read);
Ben Dookse170adc2009-07-18 10:12:27 +0100213
214static void s3c_adc_default_select(struct s3c_adc_client *client,
215 unsigned select)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000216{
217}
218
219struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
Ben Dookse170adc2009-07-18 10:12:27 +0100220 void (*select)(struct s3c_adc_client *client,
221 unsigned int selected),
222 void (*conv)(struct s3c_adc_client *client,
223 unsigned d0, unsigned d1,
Nelson Castillo3f7ea462009-05-08 08:10:12 -0500224 unsigned *samples_left),
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000225 unsigned int is_ts)
226{
227 struct s3c_adc_client *client;
228
229 WARN_ON(!pdev);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000230
231 if (!select)
232 select = s3c_adc_default_select;
233
Ben Dookse170adc2009-07-18 10:12:27 +0100234 if (!pdev)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000235 return ERR_PTR(-EINVAL);
236
Markus Elfring09e1cf82017-10-04 09:52:33 +0200237 client = kzalloc(sizeof(*client), GFP_KERNEL);
Markus Elfringa3d1d8a2017-10-04 09:33:52 +0200238 if (!client)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000239 return ERR_PTR(-ENOMEM);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000240
241 client->pdev = pdev;
242 client->is_ts = is_ts;
243 client->select_cb = select;
244 client->convert_cb = conv;
245
246 return client;
247}
248EXPORT_SYMBOL_GPL(s3c_adc_register);
249
250void s3c_adc_release(struct s3c_adc_client *client)
251{
Ben Dooks1f1f5842010-05-10 13:31:36 +0900252 unsigned long flags;
253
254 spin_lock_irqsave(&adc_dev->lock, flags);
255
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000256 /* We should really check that nothing is in progress. */
Ramax Lo0c3ee072009-04-14 23:56:18 +0800257 if (adc_dev->cur == client)
258 adc_dev->cur = NULL;
259 if (adc_dev->ts_pend == client)
260 adc_dev->ts_pend = NULL;
261 else {
262 struct list_head *p, *n;
263 struct s3c_adc_client *tmp;
264
265 list_for_each_safe(p, n, &adc_pending) {
266 tmp = list_entry(p, struct s3c_adc_client, pend);
267 if (tmp == client)
268 list_del(&tmp->pend);
269 }
270 }
271
272 if (adc_dev->cur == NULL)
273 s3c_adc_try(adc_dev);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900274
275 spin_unlock_irqrestore(&adc_dev->lock, flags);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000276 kfree(client);
277}
278EXPORT_SYMBOL_GPL(s3c_adc_release);
279
280static irqreturn_t s3c_adc_irq(int irq, void *pw)
281{
282 struct adc_device *adc = pw;
283 struct s3c_adc_client *client = adc->cur;
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100284 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000285 unsigned data0, data1;
286
287 if (!client) {
288 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100289 goto exit;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000290 }
291
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);
295
Nelson Castillo3f7ea462009-05-08 08:10:12 -0500296 client->nr_samples--;
Ben Dookse170adc2009-07-18 10:12:27 +0100297
Heiko Stuebner6247cea2011-10-12 21:34:12 +0900298 if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV11) {
299 data0 &= 0x3ff;
300 data1 &= 0x3ff;
301 } else {
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900302 /* S3C2416/S3C64XX/S5P ADC resolution is 12-bit */
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100303 data0 &= 0xfff;
304 data1 &= 0xfff;
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100305 }
306
Ben Dookse170adc2009-07-18 10:12:27 +0100307 if (client->convert_cb)
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100308 (client->convert_cb)(client, data0, data1, &client->nr_samples);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000309
Nelson Castillo3f7ea462009-05-08 08:10:12 -0500310 if (client->nr_samples > 0) {
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000311 /* fire another conversion for this */
312
Ben Dookse170adc2009-07-18 10:12:27 +0100313 client->select_cb(client, 1);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000314 s3c_adc_convert(adc);
315 } else {
Ben Dooks1f1f5842010-05-10 13:31:36 +0900316 spin_lock(&adc->lock);
Ben Dookse170adc2009-07-18 10:12:27 +0100317 (client->select_cb)(client, 0);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000318 adc->cur = NULL;
319
320 s3c_adc_try(adc);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900321 spin_unlock(&adc->lock);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000322 }
323
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100324exit:
Heiko Stuebner6247cea2011-10-12 21:34:12 +0900325 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3) {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100326 /* Clear ADC interrupt */
327 writel(0, adc->regs + S3C64XX_ADCCLRINT);
328 }
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000329 return IRQ_HANDLED;
330}
331
332static int s3c_adc_probe(struct platform_device *pdev)
333{
334 struct device *dev = &pdev->dev;
335 struct adc_device *adc;
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900336 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000337 int ret;
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100338 unsigned tmp;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000339
Markus Elfring09e1cf82017-10-04 09:52:33 +0200340 adc = devm_kzalloc(dev, sizeof(*adc), GFP_KERNEL);
Markus Elfringa3d1d8a2017-10-04 09:33:52 +0200341 if (!adc)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000342 return -ENOMEM;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000343
Ben Dooks1f1f5842010-05-10 13:31:36 +0900344 spin_lock_init(&adc->lock);
345
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000346 adc->pdev = pdev;
347 adc->prescale = S3C2410_ADCCON_PRSCVL(49);
348
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900349 adc->vdd = devm_regulator_get(dev, "vdd");
MyungJoo Hamf4629042011-07-20 21:08:17 +0900350 if (IS_ERR(adc->vdd)) {
351 dev_err(dev, "operating without regulator \"vdd\" .\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900352 return PTR_ERR(adc->vdd);
MyungJoo Hamf4629042011-07-20 21:08:17 +0900353 }
354
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000355 adc->irq = platform_get_irq(pdev, 1);
Tang Binee9a71a2020-04-19 15:05:41 +0800356 if (adc->irq <= 0)
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900357 return -ENOENT;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000358
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900359 ret = devm_request_irq(dev, adc->irq, s3c_adc_irq, 0, dev_name(dev),
360 adc);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000361 if (ret < 0) {
362 dev_err(dev, "failed to attach adc irq\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900363 return ret;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000364 }
365
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900366 adc->clk = devm_clk_get(dev, "adc");
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000367 if (IS_ERR(adc->clk)) {
368 dev_err(dev, "failed to get adc clock\n");
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900369 return PTR_ERR(adc->clk);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000370 }
371
Tang Bin06b29212020-04-19 15:06:07 +0800372 adc->regs = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding5857bd92013-01-21 11:08:55 +0100373 if (IS_ERR(adc->regs))
374 return PTR_ERR(adc->regs);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000375
MyungJoo Hamf4629042011-07-20 21:08:17 +0900376 ret = regulator_enable(adc->vdd);
377 if (ret)
Eunki Kim2ee8e6f2012-11-20 20:50:54 +0900378 return ret;
MyungJoo Hamf4629042011-07-20 21:08:17 +0900379
Sergiy Kibrik0c5f9892015-04-27 08:29:44 +0300380 clk_prepare_enable(adc->clk);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000381
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100382 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900383
384 /* Enable 12-bit ADC resolution */
385 if (cpu == TYPE_ADCV12)
386 tmp |= S3C2416_ADCCON_RESSEL;
387 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100388 tmp |= S3C64XX_ADCCON_RESSEL;
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900389
Maurus Cuelenaere91492b42010-01-30 18:01:48 +0100390 writel(tmp, adc->regs + S3C2410_ADCCON);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000391
392 dev_info(dev, "attached adc driver\n");
393
394 platform_set_drvdata(pdev, adc);
395 adc_dev = adc;
396
397 return 0;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000398}
399
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800400static int s3c_adc_remove(struct platform_device *pdev)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000401{
402 struct adc_device *adc = platform_get_drvdata(pdev);
403
Sergiy Kibrik0c5f9892015-04-27 08:29:44 +0300404 clk_disable_unprepare(adc->clk);
MyungJoo Hamf4629042011-07-20 21:08:17 +0900405 regulator_disable(adc->vdd);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000406
407 return 0;
408}
409
410#ifdef CONFIG_PM
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900411static int s3c_adc_suspend(struct device *dev)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000412{
Wolfram Sang29092472018-04-19 16:05:31 +0200413 struct adc_device *adc = dev_get_drvdata(dev);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900414 unsigned long flags;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000415 u32 con;
416
Ben Dooks1f1f5842010-05-10 13:31:36 +0900417 spin_lock_irqsave(&adc->lock, flags);
418
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000419 con = readl(adc->regs + S3C2410_ADCCON);
420 con |= S3C2410_ADCCON_STDBM;
421 writel(con, adc->regs + S3C2410_ADCCON);
422
Vasily Khoruzhicka0af8b32010-02-18 18:32:29 +0200423 disable_irq(adc->irq);
Ben Dooks1f1f5842010-05-10 13:31:36 +0900424 spin_unlock_irqrestore(&adc->lock, flags);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000425 clk_disable(adc->clk);
MyungJoo Hamf4629042011-07-20 21:08:17 +0900426 regulator_disable(adc->vdd);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000427
428 return 0;
429}
430
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900431static int s3c_adc_resume(struct device *dev)
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000432{
Geliang Tang81c25842015-12-27 21:17:36 +0800433 struct platform_device *pdev = to_platform_device(dev);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000434 struct adc_device *adc = platform_get_drvdata(pdev);
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900435 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
MyungJoo Hamf4629042011-07-20 21:08:17 +0900436 int ret;
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900437 unsigned long tmp;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000438
MyungJoo Hamf4629042011-07-20 21:08:17 +0900439 ret = regulator_enable(adc->vdd);
440 if (ret)
441 return ret;
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000442 clk_enable(adc->clk);
Vasily Khoruzhicka0af8b32010-02-18 18:32:29 +0200443 enable_irq(adc->irq);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000444
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900445 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900446
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900447 /* Enable 12-bit ADC resolution */
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900448 if (cpu == TYPE_ADCV12)
449 tmp |= S3C2416_ADCCON_RESSEL;
450 if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900451 tmp |= S3C64XX_ADCCON_RESSEL;
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900452
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900453 writel(tmp, adc->regs + S3C2410_ADCCON);
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000454
455 return 0;
456}
457
458#else
459#define s3c_adc_suspend NULL
460#define s3c_adc_resume NULL
461#endif
462
Krzysztof Kozlowskie6350c52015-05-02 00:06:35 +0900463static const struct platform_device_id s3c_adc_driver_ids[] = {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100464 {
465 .name = "s3c24xx-adc",
MyungJoo Ham64df92e2011-07-20 21:08:18 +0900466 .driver_data = TYPE_ADCV1,
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100467 }, {
Heiko Stuebner6247cea2011-10-12 21:34:12 +0900468 .name = "s3c2443-adc",
469 .driver_data = TYPE_ADCV11,
470 }, {
Heiko Stuebner35cc3cea2011-10-12 21:34:12 +0900471 .name = "s3c2416-adc",
472 .driver_data = TYPE_ADCV12,
473 }, {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100474 .name = "s3c64xx-adc",
MyungJoo Ham64df92e2011-07-20 21:08:18 +0900475 .driver_data = TYPE_ADCV2,
476 }, {
477 .name = "samsung-adc-v3",
478 .driver_data = TYPE_ADCV3,
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100479 },
480 { }
481};
482MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
483
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900484static const struct dev_pm_ops adc_pm_ops = {
485 .suspend = s3c_adc_suspend,
486 .resume = s3c_adc_resume,
487};
488
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000489static struct platform_driver s3c_adc_driver = {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100490 .id_table = s3c_adc_driver_ids,
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000491 .driver = {
Maurus Cuelenaerebcedfa92010-01-14 00:30:34 +0100492 .name = "s3c-adc",
MyungJoo Ham67dcaec2011-07-20 21:08:18 +0900493 .pm = &adc_pm_ops,
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000494 },
495 .probe = s3c_adc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800496 .remove = s3c_adc_remove,
Ben Dooks28ab44c5b2008-12-18 14:20:04 +0000497};
498
499static int __init adc_init(void)
500{
501 int ret;
502
503 ret = platform_driver_register(&s3c_adc_driver);
504 if (ret)
505 printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
506
507 return ret;
508}
509
MyungJoo Hamf4629042011-07-20 21:08:17 +0900510module_init(adc_init);