1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
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
391
392
393
394
395
396
397
398
399
400
401
|
/* linux/arch/arm/mach-s5pv210/adc.c
*
* 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, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copyright (c) 2004 Arnaud Patard <arnaud.patard@rtp-net.org>
* iPAQ H1940 touchscreen support
*
* ChangeLog
*
* 2004-09-05: Herbert Pƶtzl <herbert@13thfloor.at>
* - added clock (de-)allocation code
*
* 2005-03-06: Arnaud Patard <arnaud.patard@rtp-net.org>
* - h1940_ -> s3c24xx (this driver is now also used on the n30
* machines :P)
* - Debug messages are now enabled with the config option
* TOUCHSCREEN_S3C_DEBUG
* - Changed the way the value are read
* - Input subsystem should now work
* - Use ioremap and readl/writel
*
* 2005-03-23: Arnaud Patard <arnaud.patard@rtp-net.org>
* - Make use of some undocumented features of the touchscreen
* controller
*
*/
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/clk.h>
#include <linux/mutex.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <asm/irq.h>
#include <mach/hardware.h>
#include <linux/uaccess.h>
#include <mach/regs-adc.h>
#include <mach/adc.h>
#include <mach/irqs.h>
#define ADC_MINOR 131
#define ADC_INPUT_PIN _IOW('S', 0x0c, unsigned long)
#define ADC_WITH_TOUCHSCREEN
static struct clk *adc_clock;
static void __iomem *base_addr;
static int adc_port;
struct s3c_adc_mach_info *plat_data;
#ifdef ADC_WITH_TOUCHSCREEN
static DEFINE_MUTEX(adc_mutex);
static unsigned long data_for_ADCCON;
static unsigned long data_for_ADCTSC;
static void s3c_adc_save_SFR_on_ADC(void)
{
data_for_ADCCON = readl(base_addr + S3C_ADCCON);
data_for_ADCTSC = readl(base_addr + S3C_ADCTSC);
}
static void s3c_adc_restore_SFR_on_ADC(void)
{
writel(data_for_ADCCON, base_addr + S3C_ADCCON);
writel(data_for_ADCTSC, base_addr + S3C_ADCTSC);
}
#else
static struct resource *adc_mem;
#endif
static int s3c_adc_open(struct inode *inode, struct file *file)
{
return 0;
}
static unsigned int s3c_adc_convert(void)
{
unsigned int adc_return = 0;
unsigned long data0;
unsigned long data1;
writel((readl(base_addr + S3C_ADCCON) | S3C_ADCCON_PRSCEN) & ~S3C_ADCCON_STDBM,
base_addr + S3C_ADCCON);
writel((adc_port & 0xF), base_addr + S3C_ADCMUX);
udelay(10);
writel(readl(base_addr + S3C_ADCCON) | S3C_ADCCON_ENABLE_START,
base_addr + S3C_ADCCON);
do {
data0 = readl(base_addr + S3C_ADCCON);
} while (!(data0 & S3C_ADCCON_ECFLG));
data1 = readl(base_addr + S3C_ADCDAT0);
writel((readl(base_addr + S3C_ADCCON) | S3C_ADCCON_STDBM) & ~S3C_ADCCON_PRSCEN,
base_addr + S3C_ADCCON);
if (plat_data->resolution == 12)
adc_return = data1 & S3C_ADCDAT0_XPDATA_MASK_12BIT;
else
adc_return = data1 & S3C_ADCDAT0_XPDATA_MASK;
return adc_return;
}
int s3c_adc_get_adc_data(int channel)
{
int adc_value = 0;
int cur_adc_port = 0;
#ifdef ADC_WITH_TOUCHSCREEN
mutex_lock(&adc_mutex);
s3c_adc_save_SFR_on_ADC();
#else
mutex_lock(&adc_mutex);
#endif
cur_adc_port = adc_port;
adc_port = channel;
adc_value = s3c_adc_convert();
adc_port = cur_adc_port;
#ifdef ADC_WITH_TOUCHSCREEN
s3c_adc_restore_SFR_on_ADC();
mutex_unlock(&adc_mutex);
#else
mutex_unlock(&adc_mutex);
#endif
pr_debug("%s : Converted Value: %03d\n", __func__, adc_value);
return adc_value;
}
EXPORT_SYMBOL(s3c_adc_get_adc_data);
int s3c_adc_get(struct s3c_adc_request *req)
{
unsigned adc_channel = req->channel;
int adc_value_ret = 0;
adc_value_ret = s3c_adc_convert();
req->callback(adc_channel, req->param, adc_value_ret);
return 0;
}
EXPORT_SYMBOL(s3c_adc_get);
static ssize_t
s3c_adc_read(struct file *file, char __user *buffer,
size_t size, loff_t *pos)
{
int adc_value = 0;
#ifdef ADC_WITH_TOUCHSCREEN
mutex_lock(&adc_mutex);
s3c_adc_save_SFR_on_ADC();
#endif
printk(KERN_INFO "## delay: %d\n", readl(base_addr + S3C_ADCDLY));
adc_value = s3c_adc_convert();
#ifdef ADC_WITH_TOUCHSCREEN
s3c_adc_restore_SFR_on_ADC();
mutex_unlock(&adc_mutex);
#endif
if (copy_to_user(buffer, &adc_value, sizeof(unsigned int)))
return -EFAULT;
return sizeof(unsigned int);
}
static long s3c_adc_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case ADC_INPUT_PIN:
adc_port = (unsigned int) arg;
if (adc_port >= 4)
printk(KERN_WARNING
" %d is already reserved for TouchScreen\n",
adc_port);
return 0;
default:
return -ENOIOCTLCMD;
}
}
static const struct file_operations s3c_adc_fops = {
.owner = THIS_MODULE,
.read = s3c_adc_read,
.open = s3c_adc_open,
.unlocked_ioctl = s3c_adc_ioctl,
};
static struct miscdevice s3c_adc_miscdev = {
.minor = ADC_MINOR,
.name = "adc",
.fops = &s3c_adc_fops,
};
static struct s3c_adc_mach_info *s3c_adc_get_platdata(struct device *dev)
{
if (dev->platform_data != NULL)
return (struct s3c_adc_mach_info *) dev->platform_data;
else
return 0;
}
/*
* The functions for inserting/removing us as a module.
*/
static int __devinit s3c_adc_probe(struct platform_device *pdev)
{
struct resource *res;
struct device *dev;
int ret;
int size;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
dev = &pdev->dev;
if (res == NULL) {
dev_err(dev, "no memory resource specified\n");
return -ENOENT;
}
size = (res->end - res->start) + 1;
#if !defined(ADC_WITH_TOUCHSCREEN)
adc_mem = request_mem_region(res->start, size, pdev->name);
if (adc_mem == NULL) {
dev_err(dev, "failed to get memory region\n");
ret = -ENOENT;
goto err_req;
}
#endif
base_addr = ioremap(res->start, size);
if (base_addr == NULL) {
dev_err(dev, "fail to ioremap() region\n");
ret = -ENOENT;
goto err_map;
}
adc_clock = clk_get(&pdev->dev, "adc");
if (IS_ERR(adc_clock)) {
dev_err(dev, "failed to fine ADC clock source\n");
ret = PTR_ERR(adc_clock);
goto err_clk;
}
clk_enable(adc_clock);
/* read platform data from device struct */
plat_data = s3c_adc_get_platdata(&pdev->dev);
if ((plat_data->presc & 0xff) > 0)
writel(S3C_ADCCON_PRSCEN |
S3C_ADCCON_PRSCVL(plat_data->presc & 0xff),
base_addr + S3C_ADCCON);
else
writel(0, base_addr + S3C_ADCCON);
/* Initialise registers */
if ((plat_data->delay & 0xffff) > 0)
writel(plat_data->delay & 0xffff, base_addr + S3C_ADCDLY);
if (plat_data->resolution == 12)
writel(readl(base_addr + S3C_ADCCON) |
S3C_ADCCON_RESSEL_12BIT, base_addr + S3C_ADCCON);
writel((readl(base_addr + S3C_ADCCON) | S3C_ADCCON_STDBM) & ~S3C_ADCCON_PRSCEN,
base_addr + S3C_ADCCON);
ret = misc_register(&s3c_adc_miscdev);
if (ret) {
printk(KERN_ERR "cannot register miscdev on minor=%d (%d)\n",
ADC_MINOR, ret);
goto err_clk;
}
return 0;
err_clk:
clk_disable(adc_clock);
clk_put(adc_clock);
err_map:
iounmap(base_addr);
#if !defined(ADC_WITH_TOUCHSCREEN)
err_req:
release_resource(adc_mem);
kfree(adc_mem);
#endif
return ret;
}
static int s3c_adc_remove(struct platform_device *dev)
{
clk_disable(adc_clock);
clk_put(adc_clock);
return 0;
}
#ifdef CONFIG_PM
static unsigned int adccon, adctsc, adcdly;
static int s3c_adc_suspend(struct platform_device *dev, pm_message_t state)
{
adccon = readl(base_addr + S3C_ADCCON);
adctsc = readl(base_addr + S3C_ADCTSC);
adcdly = readl(base_addr + S3C_ADCDLY);
return 0;
}
static int s3c_adc_resume(struct platform_device *pdev)
{
writel(adccon, base_addr + S3C_ADCCON);
writel(adctsc, base_addr + S3C_ADCTSC);
writel(adcdly, base_addr + S3C_ADCDLY);
return 0;
}
#else
#define s3c_adc_suspend NULL
#define s3c_adc_resume NULL
#endif
static struct platform_driver s3c_adc_driver = {
.probe = s3c_adc_probe,
.remove = s3c_adc_remove,
.suspend = s3c_adc_suspend,
.resume = s3c_adc_resume,
.driver = {
.owner = THIS_MODULE,
.name = "s3c-adc",
},
};
static char banner[] __initdata = KERN_INFO \
"S5PV210 ADC driver, (c) 2010 Samsung Electronics\n";
int __init s3c_adc_init(void)
{
printk(banner);
return platform_driver_register(&s3c_adc_driver);
}
void __exit s3c_adc_exit(void)
{
platform_driver_unregister(&s3c_adc_driver);
}
module_init(s3c_adc_init);
module_exit(s3c_adc_exit);
MODULE_AUTHOR("dsfine.ha@samsung.com");
MODULE_DESCRIPTION("S5PV210 ADC driver");
MODULE_LICENSE("GPL");
|