Blame view

arch/arm/plat-samsung/adc.c 12.2 KB
3929e1e76   Maurus Cuelenaere   ARM: SAMSUNG: Mov...
1
  /* arch/arm/plat-samsung/adc.c
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
2
3
4
5
6
   *
   * Copyright (c) 2008 Simtec Electronics
   *	http://armlinux.simtec.co.uk/
   *	Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
   *
3929e1e76   Maurus Cuelenaere   ARM: SAMSUNG: Mov...
7
   * Samsung ADC device core
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
8
9
10
11
12
13
14
15
16
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License.
  */
  
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/platform_device.h>
d43c36dc6   Alexey Dobriyan   headers: remove s...
17
  #include <linux/sched.h>
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
18
  #include <linux/list.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/slab.h>
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
20
21
22
23
  #include <linux/err.h>
  #include <linux/clk.h>
  #include <linux/interrupt.h>
  #include <linux/io.h>
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
24
  #include <linux/regulator/consumer.h>
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  
  #include <plat/regs-adc.h>
  #include <plat/adc.h>
  
  /* This driver is designed to control the usage of the ADC block between
   * the touchscreen and any other drivers that may need to use it, such as
   * the hwmon driver.
   *
   * Priority will be given to the touchscreen driver, but as this itself is
   * rate limited it should not starve other requests which are processed in
   * order that they are received.
   *
   * Each user registers to get a client block which uniquely identifies it
   * and stores information such as the necessary functions to callback when
   * action is required.
   */
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
41
  enum s3c_cpu_type {
64df92ea7   MyungJoo Ham   ARM: SAMSUNG: ADC...
42
  	TYPE_ADCV1, /* S3C24XX */
6247cea2b   Heiko Stuebner   ARM: SAMSUNG: Add...
43
  	TYPE_ADCV11, /* S3C2443 */
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
44
  	TYPE_ADCV12, /* S3C2416, S3C2450 */
64df92ea7   MyungJoo Ham   ARM: SAMSUNG: ADC...
45
46
  	TYPE_ADCV2, /* S3C64XX, S5P64X0, S5PC100 */
  	TYPE_ADCV3, /* S5PV210, S5PC110, EXYNOS4210 */
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
47
  };
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
48
49
50
  struct s3c_adc_client {
  	struct platform_device	*pdev;
  	struct list_head	 pend;
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
51
  	wait_queue_head_t	*wait;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
52
53
  
  	unsigned int		 nr_samples;
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
54
  	int			 result;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
55
56
  	unsigned char		 is_ts;
  	unsigned char		 channel;
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
57
58
59
  	void	(*select_cb)(struct s3c_adc_client *c, unsigned selected);
  	void	(*convert_cb)(struct s3c_adc_client *c,
  			      unsigned val1, unsigned val2,
3f7ea467b   Nelson Castillo   [ARM] S3C: ADC: E...
60
  			      unsigned *samples_left);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
61
62
63
64
65
66
67
68
69
  };
  
  struct adc_device {
  	struct platform_device	*pdev;
  	struct platform_device	*owner;
  	struct clk		*clk;
  	struct s3c_adc_client	*cur;
  	struct s3c_adc_client	*ts_pend;
  	void __iomem		*regs;
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
70
  	spinlock_t		 lock;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
71
72
73
74
  
  	unsigned int		 prescale;
  
  	int			 irq;
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
75
  	struct regulator	*vdd;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
76
77
78
  };
  
  static struct adc_device *adc_dev;
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
79
  static LIST_HEAD(adc_pending);	/* protected by adc_device.lock */
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  
  #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
  
  static inline void s3c_adc_convert(struct adc_device *adc)
  {
  	unsigned con = readl(adc->regs + S3C2410_ADCCON);
  
  	con |= S3C2410_ADCCON_ENABLE_START;
  	writel(con, adc->regs + S3C2410_ADCCON);
  }
  
  static inline void s3c_adc_select(struct adc_device *adc,
  				  struct s3c_adc_client *client)
  {
  	unsigned con = readl(adc->regs + S3C2410_ADCCON);
64df92ea7   MyungJoo Ham   ARM: SAMSUNG: ADC...
95
  	enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
96

e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
97
  	client->select_cb(client, 1);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
98

df303e023   Heiko Stuebner   ARM: SAMSUNG: Fix...
99
100
  	if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV2)
  		con &= ~S3C2410_ADCCON_MUXMASK;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
101
102
  	con &= ~S3C2410_ADCCON_STDBM;
  	con &= ~S3C2410_ADCCON_STARTMASK;
64df92ea7   MyungJoo Ham   ARM: SAMSUNG: ADC...
103
104
105
  	if (!client->is_ts) {
  		if (cpu == TYPE_ADCV3)
  			writel(client->channel & 0xf, adc->regs + S5P_ADCMUX);
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
106
  		else if (cpu == TYPE_ADCV11 || cpu == TYPE_ADCV12)
6247cea2b   Heiko Stuebner   ARM: SAMSUNG: Add...
107
108
  			writel(client->channel & 0xf,
  						adc->regs + S3C2443_ADCMUX);
64df92ea7   MyungJoo Ham   ARM: SAMSUNG: ADC...
109
110
111
  		else
  			con |= S3C2410_ADCCON_SELMUX(client->channel);
  	}
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
112
113
114
115
116
117
118
119
120
121
122
123
  
  	writel(con, adc->regs + S3C2410_ADCCON);
  }
  
  static void s3c_adc_dbgshow(struct adc_device *adc)
  {
  	adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x
  ",
  		readl(adc->regs + S3C2410_ADCCON),
  		readl(adc->regs + S3C2410_ADCTSC),
  		readl(adc->regs + S3C2410_ADCDLY));
  }
f8c8ac810   Ben Dooks   [ARM] S3C: Fix AD...
124
  static void s3c_adc_try(struct adc_device *adc)
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  {
  	struct s3c_adc_client *next = adc->ts_pend;
  
  	if (!next && !list_empty(&adc_pending)) {
  		next = list_first_entry(&adc_pending,
  					struct s3c_adc_client, pend);
  		list_del(&next->pend);
  	} else
  		adc->ts_pend = NULL;
  
  	if (next) {
  		adc_dbg(adc, "new client is %p
  ", next);
  		adc->cur = next;
  		s3c_adc_select(adc, next);
  		s3c_adc_convert(adc);
  		s3c_adc_dbgshow(adc);
  	}
  }
  
  int s3c_adc_start(struct s3c_adc_client *client,
  		  unsigned int channel, unsigned int nr_samples)
  {
  	struct adc_device *adc = adc_dev;
  	unsigned long flags;
  
  	if (!adc) {
  		printk(KERN_ERR "%s: failed to find adc
  ", __func__);
  		return -EINVAL;
  	}
  
  	if (client->is_ts && adc->ts_pend)
  		return -EAGAIN;
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
159
  	spin_lock_irqsave(&adc->lock, flags);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
160
161
162
163
164
165
166
167
168
169
170
  
  	client->channel = channel;
  	client->nr_samples = nr_samples;
  
  	if (client->is_ts)
  		adc->ts_pend = client;
  	else
  		list_add_tail(&client->pend, &adc_pending);
  
  	if (!adc->cur)
  		s3c_adc_try(adc);
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
171
172
  
  	spin_unlock_irqrestore(&adc->lock, flags);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
173
174
175
176
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(s3c_adc_start);
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  static void s3c_convert_done(struct s3c_adc_client *client,
  			     unsigned v, unsigned u, unsigned *left)
  {
  	client->result = v;
  	wake_up(client->wait);
  }
  
  int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
  {
  	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
  	int ret;
  
  	client->convert_cb = s3c_convert_done;
  	client->wait = &wake;
  	client->result = -1;
  
  	ret = s3c_adc_start(client, ch, 1);
  	if (ret < 0)
  		goto err;
  
  	ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
  	if (client->result < 0) {
  		ret = -ETIMEDOUT;
  		goto err;
  	}
  
  	client->convert_cb = NULL;
  	return client->result;
  
  err:
  	return ret;
  }
d3bf3956c   Ryan Mallon   ARM: S3C: Fix adc...
209
  EXPORT_SYMBOL_GPL(s3c_adc_read);
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
210
211
212
  
  static void s3c_adc_default_select(struct s3c_adc_client *client,
  				   unsigned select)
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
213
214
215
216
  {
  }
  
  struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
217
218
219
220
  					void (*select)(struct s3c_adc_client *client,
  						       unsigned int selected),
  					void (*conv)(struct s3c_adc_client *client,
  						     unsigned d0, unsigned d1,
3f7ea467b   Nelson Castillo   [ARM] S3C: ADC: E...
221
  						     unsigned *samples_left),
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
222
223
224
225
226
  					unsigned int is_ts)
  {
  	struct s3c_adc_client *client;
  
  	WARN_ON(!pdev);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
227
228
229
  
  	if (!select)
  		select = s3c_adc_default_select;
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
230
  	if (!pdev)
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  		return ERR_PTR(-EINVAL);
  
  	client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
  	if (!client) {
  		dev_err(&pdev->dev, "no memory for adc client
  ");
  		return ERR_PTR(-ENOMEM);
  	}
  
  	client->pdev = pdev;
  	client->is_ts = is_ts;
  	client->select_cb = select;
  	client->convert_cb = conv;
  
  	return client;
  }
  EXPORT_SYMBOL_GPL(s3c_adc_register);
  
  void s3c_adc_release(struct s3c_adc_client *client)
  {
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
251
252
253
  	unsigned long flags;
  
  	spin_lock_irqsave(&adc_dev->lock, flags);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
254
  	/* We should really check that nothing is in progress. */
0c3ee0782   Ramax Lo   [ARM] S3C24XX: AD...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  	if (adc_dev->cur == client)
  		adc_dev->cur = NULL;
  	if (adc_dev->ts_pend == client)
  		adc_dev->ts_pend = NULL;
  	else {
  		struct list_head *p, *n;
  		struct s3c_adc_client *tmp;
  
  		list_for_each_safe(p, n, &adc_pending) {
  			tmp = list_entry(p, struct s3c_adc_client, pend);
  			if (tmp == client)
  				list_del(&tmp->pend);
  		}
  	}
  
  	if (adc_dev->cur == NULL)
  		s3c_adc_try(adc_dev);
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
272
273
  
  	spin_unlock_irqrestore(&adc_dev->lock, flags);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
274
275
276
277
278
279
280
281
  	kfree(client);
  }
  EXPORT_SYMBOL_GPL(s3c_adc_release);
  
  static irqreturn_t s3c_adc_irq(int irq, void *pw)
  {
  	struct adc_device *adc = pw;
  	struct s3c_adc_client *client = adc->cur;
91492b4a0   Maurus Cuelenaere   ARM: SAMSUNG: Add...
282
  	enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
283
284
285
286
287
  	unsigned data0, data1;
  
  	if (!client) {
  		dev_warn(&adc->pdev->dev, "%s: no adc pending
  ", __func__);
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
288
  		goto exit;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
289
290
291
292
293
294
  	}
  
  	data0 = readl(adc->regs + S3C2410_ADCDAT0);
  	data1 = readl(adc->regs + S3C2410_ADCDAT1);
  	adc_dbg(adc, "read %d: 0x%04x, 0x%04x
  ", client->nr_samples, data0, data1);
3f7ea467b   Nelson Castillo   [ARM] S3C: ADC: E...
295
  	client->nr_samples--;
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
296

6247cea2b   Heiko Stuebner   ARM: SAMSUNG: Add...
297
298
299
300
  	if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV11) {
  		data0 &= 0x3ff;
  		data1 &= 0x3ff;
  	} else {
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
301
  		/* S3C2416/S3C64XX/S5P ADC resolution is 12-bit */
91492b4a0   Maurus Cuelenaere   ARM: SAMSUNG: Add...
302
303
  		data0 &= 0xfff;
  		data1 &= 0xfff;
91492b4a0   Maurus Cuelenaere   ARM: SAMSUNG: Add...
304
  	}
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
305
  	if (client->convert_cb)
91492b4a0   Maurus Cuelenaere   ARM: SAMSUNG: Add...
306
  		(client->convert_cb)(client, data0, data1, &client->nr_samples);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
307

3f7ea467b   Nelson Castillo   [ARM] S3C: ADC: E...
308
  	if (client->nr_samples > 0) {
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
309
  		/* fire another conversion for this */
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
310
  		client->select_cb(client, 1);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
311
312
  		s3c_adc_convert(adc);
  	} else {
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
313
  		spin_lock(&adc->lock);
e170adcb4   Ben Dooks   ARM: S3C: Add ADC...
314
  		(client->select_cb)(client, 0);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
315
316
317
  		adc->cur = NULL;
  
  		s3c_adc_try(adc);
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
318
  		spin_unlock(&adc->lock);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
319
  	}
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
320
  exit:
6247cea2b   Heiko Stuebner   ARM: SAMSUNG: Add...
321
  	if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3) {
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
322
323
324
  		/* Clear ADC interrupt */
  		writel(0, adc->regs + S3C64XX_ADCCLRINT);
  	}
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
325
326
327
328
329
330
331
332
  	return IRQ_HANDLED;
  }
  
  static int s3c_adc_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
  	struct adc_device *adc;
  	struct resource *regs;
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
333
  	enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
334
  	int ret;
91492b4a0   Maurus Cuelenaere   ARM: SAMSUNG: Add...
335
  	unsigned tmp;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
336
337
338
339
340
341
342
  
  	adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
  	if (adc == NULL) {
  		dev_err(dev, "failed to allocate adc_device
  ");
  		return -ENOMEM;
  	}
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
343
  	spin_lock_init(&adc->lock);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
344
345
  	adc->pdev = pdev;
  	adc->prescale = S3C2410_ADCCON_PRSCVL(49);
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
346
347
348
349
350
351
352
  	adc->vdd = regulator_get(dev, "vdd");
  	if (IS_ERR(adc->vdd)) {
  		dev_err(dev, "operating without regulator \"vdd\" .
  ");
  		ret = PTR_ERR(adc->vdd);
  		goto err_alloc;
  	}
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
353
354
355
356
357
  	adc->irq = platform_get_irq(pdev, 1);
  	if (adc->irq <= 0) {
  		dev_err(dev, "failed to get adc irq
  ");
  		ret = -ENOENT;
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
358
  		goto err_reg;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
359
360
361
362
363
364
  	}
  
  	ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
  	if (ret < 0) {
  		dev_err(dev, "failed to attach adc irq
  ");
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
365
  		goto err_reg;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  	}
  
  	adc->clk = clk_get(dev, "adc");
  	if (IS_ERR(adc->clk)) {
  		dev_err(dev, "failed to get adc clock
  ");
  		ret = PTR_ERR(adc->clk);
  		goto err_irq;
  	}
  
  	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (!regs) {
  		dev_err(dev, "failed to find registers
  ");
  		ret = -ENXIO;
  		goto err_clk;
  	}
  
  	adc->regs = ioremap(regs->start, resource_size(regs));
  	if (!adc->regs) {
  		dev_err(dev, "failed to map registers
  ");
  		ret = -ENXIO;
  		goto err_clk;
  	}
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
391
392
393
  	ret = regulator_enable(adc->vdd);
  	if (ret)
  		goto err_ioremap;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
394
  	clk_enable(adc->clk);
91492b4a0   Maurus Cuelenaere   ARM: SAMSUNG: Add...
395
  	tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
396
397
398
399
400
  
  	/* Enable 12-bit ADC resolution */
  	if (cpu == TYPE_ADCV12)
  		tmp |= S3C2416_ADCCON_RESSEL;
  	if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
91492b4a0   Maurus Cuelenaere   ARM: SAMSUNG: Add...
401
  		tmp |= S3C64XX_ADCCON_RESSEL;
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
402

91492b4a0   Maurus Cuelenaere   ARM: SAMSUNG: Add...
403
  	writel(tmp, adc->regs + S3C2410_ADCCON);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
404
405
406
407
408
409
410
411
  
  	dev_info(dev, "attached adc driver
  ");
  
  	platform_set_drvdata(pdev, adc);
  	adc_dev = adc;
  
  	return 0;
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
412
413
   err_ioremap:
  	iounmap(adc->regs);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
414
415
416
417
418
   err_clk:
  	clk_put(adc->clk);
  
   err_irq:
  	free_irq(adc->irq, adc);
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
419
420
   err_reg:
  	regulator_put(adc->vdd);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
421
422
423
424
   err_alloc:
  	kfree(adc);
  	return ret;
  }
ad4e22fa4   Uwe Kleine-König   ARM: S3C: move s3...
425
  static int __devexit s3c_adc_remove(struct platform_device *pdev)
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
426
427
428
429
430
431
  {
  	struct adc_device *adc = platform_get_drvdata(pdev);
  
  	iounmap(adc->regs);
  	free_irq(adc->irq, adc);
  	clk_disable(adc->clk);
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
432
433
  	regulator_disable(adc->vdd);
  	regulator_put(adc->vdd);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
434
435
436
437
438
439
440
  	clk_put(adc->clk);
  	kfree(adc);
  
  	return 0;
  }
  
  #ifdef CONFIG_PM
67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
441
  static int s3c_adc_suspend(struct device *dev)
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
442
  {
67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
443
444
  	struct platform_device *pdev = container_of(dev,
  			struct platform_device, dev);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
445
  	struct adc_device *adc = platform_get_drvdata(pdev);
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
446
  	unsigned long flags;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
447
  	u32 con;
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
448
  	spin_lock_irqsave(&adc->lock, flags);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
449
450
451
  	con = readl(adc->regs + S3C2410_ADCCON);
  	con |= S3C2410_ADCCON_STDBM;
  	writel(con, adc->regs + S3C2410_ADCCON);
a0af8b3c7   Vasily Khoruzhick   ARM: SAMSUNG: adc...
452
  	disable_irq(adc->irq);
1f1f584c9   Ben Dooks   ARM: SAMSUNG: Mak...
453
  	spin_unlock_irqrestore(&adc->lock, flags);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
454
  	clk_disable(adc->clk);
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
455
  	regulator_disable(adc->vdd);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
456
457
458
  
  	return 0;
  }
67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
459
  static int s3c_adc_resume(struct device *dev)
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
460
  {
67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
461
462
  	struct platform_device *pdev = container_of(dev,
  			struct platform_device, dev);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
463
  	struct adc_device *adc = platform_get_drvdata(pdev);
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
464
  	enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data;
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
465
  	int ret;
67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
466
  	unsigned long tmp;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
467

f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
468
469
470
  	ret = regulator_enable(adc->vdd);
  	if (ret)
  		return ret;
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
471
  	clk_enable(adc->clk);
a0af8b3c7   Vasily Khoruzhick   ARM: SAMSUNG: adc...
472
  	enable_irq(adc->irq);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
473

67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
474
  	tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
475

67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
476
  	/* Enable 12-bit ADC resolution */
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
477
478
479
  	if (cpu == TYPE_ADCV12)
  		tmp |= S3C2416_ADCCON_RESSEL;
  	if (cpu == TYPE_ADCV2 || cpu == TYPE_ADCV3)
67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
480
  		tmp |= S3C64XX_ADCCON_RESSEL;
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
481

67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
482
  	writel(tmp, adc->regs + S3C2410_ADCCON);
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
483
484
485
486
487
488
489
490
  
  	return 0;
  }
  
  #else
  #define s3c_adc_suspend NULL
  #define s3c_adc_resume NULL
  #endif
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
491
492
493
  static struct platform_device_id s3c_adc_driver_ids[] = {
  	{
  		.name           = "s3c24xx-adc",
64df92ea7   MyungJoo Ham   ARM: SAMSUNG: ADC...
494
  		.driver_data    = TYPE_ADCV1,
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
495
  	}, {
6247cea2b   Heiko Stuebner   ARM: SAMSUNG: Add...
496
497
498
  		.name		= "s3c2443-adc",
  		.driver_data	= TYPE_ADCV11,
  	}, {
35cc3cea2   Heiko Stuebner   ARM: SAMSUNG: Add...
499
500
501
  		.name		= "s3c2416-adc",
  		.driver_data	= TYPE_ADCV12,
  	}, {
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
502
  		.name           = "s3c64xx-adc",
64df92ea7   MyungJoo Ham   ARM: SAMSUNG: ADC...
503
504
505
506
  		.driver_data    = TYPE_ADCV2,
  	}, {
  		.name		= "samsung-adc-v3",
  		.driver_data	= TYPE_ADCV3,
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
507
508
509
510
  	},
  	{ }
  };
  MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
511
512
513
514
  static const struct dev_pm_ops adc_pm_ops = {
  	.suspend	= s3c_adc_suspend,
  	.resume		= s3c_adc_resume,
  };
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
515
  static struct platform_driver s3c_adc_driver = {
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
516
  	.id_table	= s3c_adc_driver_ids,
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
517
  	.driver		= {
bcedfa98d   Maurus Cuelenaere   ARM: S3C64XX: Add...
518
  		.name	= "s3c-adc",
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
519
  		.owner	= THIS_MODULE,
67dcaec8d   MyungJoo Ham   ARM: SAMSUNG: Rev...
520
  		.pm	= &adc_pm_ops,
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
521
522
523
  	},
  	.probe		= s3c_adc_probe,
  	.remove		= __devexit_p(s3c_adc_remove),
28ab44c5b   Ben Dooks   [ARM] S3C24XX: AD...
524
525
526
527
528
529
530
531
532
533
534
535
536
  };
  
  static int __init adc_init(void)
  {
  	int ret;
  
  	ret = platform_driver_register(&s3c_adc_driver);
  	if (ret)
  		printk(KERN_ERR "%s: failed to add adc driver
  ", __func__);
  
  	return ret;
  }
f462904ef   MyungJoo Ham   ARM: SAMSUNG: use...
537
  module_init(adc_init);