Loading...
1/*
2 * Samsung S3C24XX touchscreen driver
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the term of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Copyright 2004 Arnaud Patard <arnaud.patard@rtp-net.org>
19 * Copyright 2008 Ben Dooks <ben-linux@fluff.org>
20 * Copyright 2009 Simtec Electronics <linux@simtec.co.uk>
21 *
22 * Additional work by Herbert Pƶtzl <herbert@13thfloor.at> and
23 * Harald Welte <laforge@openmoko.org>
24 */
25
26#include <linux/errno.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/gpio.h>
30#include <linux/input.h>
31#include <linux/delay.h>
32#include <linux/interrupt.h>
33#include <linux/platform_device.h>
34#include <linux/clk.h>
35#include <linux/io.h>
36
37#include <plat/adc.h>
38#include <plat/regs-adc.h>
39#include <linux/platform_data/touchscreen-s3c2410.h>
40
41#define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
42
43#define INT_DOWN (0)
44#define INT_UP (1 << 8)
45
46#define WAIT4INT (S3C2410_ADCTSC_YM_SEN | \
47 S3C2410_ADCTSC_YP_SEN | \
48 S3C2410_ADCTSC_XP_SEN | \
49 S3C2410_ADCTSC_XY_PST(3))
50
51#define AUTOPST (S3C2410_ADCTSC_YM_SEN | \
52 S3C2410_ADCTSC_YP_SEN | \
53 S3C2410_ADCTSC_XP_SEN | \
54 S3C2410_ADCTSC_AUTO_PST | \
55 S3C2410_ADCTSC_XY_PST(0))
56
57#define FEAT_PEN_IRQ (1 << 0) /* HAS ADCCLRINTPNDNUP */
58
59/* Per-touchscreen data. */
60
61/**
62 * struct s3c2410ts - driver touchscreen state.
63 * @client: The ADC client we registered with the core driver.
64 * @dev: The device we are bound to.
65 * @input: The input device we registered with the input subsystem.
66 * @clock: The clock for the adc.
67 * @io: Pointer to the IO base.
68 * @xp: The accumulated X position data.
69 * @yp: The accumulated Y position data.
70 * @irq_tc: The interrupt number for pen up/down interrupt
71 * @count: The number of samples collected.
72 * @shift: The log2 of the maximum count to read in one go.
73 * @features: The features supported by the TSADC MOdule.
74 */
75struct s3c2410ts {
76 struct s3c_adc_client *client;
77 struct device *dev;
78 struct input_dev *input;
79 struct clk *clock;
80 void __iomem *io;
81 unsigned long xp;
82 unsigned long yp;
83 int irq_tc;
84 int count;
85 int shift;
86 int features;
87};
88
89static struct s3c2410ts ts;
90
91/**
92 * get_down - return the down state of the pen
93 * @data0: The data read from ADCDAT0 register.
94 * @data1: The data read from ADCDAT1 register.
95 *
96 * Return non-zero if both readings show that the pen is down.
97 */
98static inline bool get_down(unsigned long data0, unsigned long data1)
99{
100 /* returns true if both data values show stylus down */
101 return (!(data0 & S3C2410_ADCDAT0_UPDOWN) &&
102 !(data1 & S3C2410_ADCDAT0_UPDOWN));
103}
104
105static void touch_timer_fire(unsigned long data)
106{
107 unsigned long data0;
108 unsigned long data1;
109 bool down;
110
111 data0 = readl(ts.io + S3C2410_ADCDAT0);
112 data1 = readl(ts.io + S3C2410_ADCDAT1);
113
114 down = get_down(data0, data1);
115
116 if (down) {
117 if (ts.count == (1 << ts.shift)) {
118 ts.xp >>= ts.shift;
119 ts.yp >>= ts.shift;
120
121 dev_dbg(ts.dev, "%s: X=%lu, Y=%lu, count=%d\n",
122 __func__, ts.xp, ts.yp, ts.count);
123
124 input_report_abs(ts.input, ABS_X, ts.xp);
125 input_report_abs(ts.input, ABS_Y, ts.yp);
126
127 input_report_key(ts.input, BTN_TOUCH, 1);
128 input_sync(ts.input);
129
130 ts.xp = 0;
131 ts.yp = 0;
132 ts.count = 0;
133 }
134
135 s3c_adc_start(ts.client, 0, 1 << ts.shift);
136 } else {
137 ts.xp = 0;
138 ts.yp = 0;
139 ts.count = 0;
140
141 input_report_key(ts.input, BTN_TOUCH, 0);
142 input_sync(ts.input);
143
144 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
145 }
146}
147
148static DEFINE_TIMER(touch_timer, touch_timer_fire, 0, 0);
149
150/**
151 * stylus_irq - touchscreen stylus event interrupt
152 * @irq: The interrupt number
153 * @dev_id: The device ID.
154 *
155 * Called when the IRQ_TC is fired for a pen up or down event.
156 */
157static irqreturn_t stylus_irq(int irq, void *dev_id)
158{
159 unsigned long data0;
160 unsigned long data1;
161 bool down;
162
163 data0 = readl(ts.io + S3C2410_ADCDAT0);
164 data1 = readl(ts.io + S3C2410_ADCDAT1);
165
166 down = get_down(data0, data1);
167
168 /* TODO we should never get an interrupt with down set while
169 * the timer is running, but maybe we ought to verify that the
170 * timer isn't running anyways. */
171
172 if (down)
173 s3c_adc_start(ts.client, 0, 1 << ts.shift);
174 else
175 dev_dbg(ts.dev, "%s: count=%d\n", __func__, ts.count);
176
177 if (ts.features & FEAT_PEN_IRQ) {
178 /* Clear pen down/up interrupt */
179 writel(0x0, ts.io + S3C64XX_ADCCLRINTPNDNUP);
180 }
181
182 return IRQ_HANDLED;
183}
184
185/**
186 * s3c24xx_ts_conversion - ADC conversion callback
187 * @client: The client that was registered with the ADC core.
188 * @data0: The reading from ADCDAT0.
189 * @data1: The reading from ADCDAT1.
190 * @left: The number of samples left.
191 *
192 * Called when a conversion has finished.
193 */
194static void s3c24xx_ts_conversion(struct s3c_adc_client *client,
195 unsigned data0, unsigned data1,
196 unsigned *left)
197{
198 dev_dbg(ts.dev, "%s: %d,%d\n", __func__, data0, data1);
199
200 ts.xp += data0;
201 ts.yp += data1;
202
203 ts.count++;
204
205 /* From tests, it seems that it is unlikely to get a pen-up
206 * event during the conversion process which means we can
207 * ignore any pen-up events with less than the requisite
208 * count done.
209 *
210 * In several thousand conversions, no pen-ups where detected
211 * before count completed.
212 */
213}
214
215/**
216 * s3c24xx_ts_select - ADC selection callback.
217 * @client: The client that was registered with the ADC core.
218 * @select: The reason for select.
219 *
220 * Called when the ADC core selects (or deslects) us as a client.
221 */
222static void s3c24xx_ts_select(struct s3c_adc_client *client, unsigned select)
223{
224 if (select) {
225 writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
226 ts.io + S3C2410_ADCTSC);
227 } else {
228 mod_timer(&touch_timer, jiffies+1);
229 writel(WAIT4INT | INT_UP, ts.io + S3C2410_ADCTSC);
230 }
231}
232
233/**
234 * s3c2410ts_probe - device core probe entry point
235 * @pdev: The device we are being bound to.
236 *
237 * Initialise, find and allocate any resources we need to run and then
238 * register with the ADC and input systems.
239 */
240static int s3c2410ts_probe(struct platform_device *pdev)
241{
242 struct s3c2410_ts_mach_info *info;
243 struct device *dev = &pdev->dev;
244 struct input_dev *input_dev;
245 struct resource *res;
246 int ret = -EINVAL;
247
248 /* Initialise input stuff */
249 memset(&ts, 0, sizeof(struct s3c2410ts));
250
251 ts.dev = dev;
252
253 info = dev_get_platdata(&pdev->dev);
254 if (!info) {
255 dev_err(dev, "no platform data, cannot attach\n");
256 return -EINVAL;
257 }
258
259 dev_dbg(dev, "initialising touchscreen\n");
260
261 ts.clock = clk_get(dev, "adc");
262 if (IS_ERR(ts.clock)) {
263 dev_err(dev, "cannot get adc clock source\n");
264 return -ENOENT;
265 }
266
267 clk_prepare_enable(ts.clock);
268 dev_dbg(dev, "got and enabled clocks\n");
269
270 ts.irq_tc = ret = platform_get_irq(pdev, 0);
271 if (ret < 0) {
272 dev_err(dev, "no resource for interrupt\n");
273 goto err_clk;
274 }
275
276 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
277 if (!res) {
278 dev_err(dev, "no resource for registers\n");
279 ret = -ENOENT;
280 goto err_clk;
281 }
282
283 ts.io = ioremap(res->start, resource_size(res));
284 if (ts.io == NULL) {
285 dev_err(dev, "cannot map registers\n");
286 ret = -ENOMEM;
287 goto err_clk;
288 }
289
290 /* inititalise the gpio */
291 if (info->cfg_gpio)
292 info->cfg_gpio(to_platform_device(ts.dev));
293
294 ts.client = s3c_adc_register(pdev, s3c24xx_ts_select,
295 s3c24xx_ts_conversion, 1);
296 if (IS_ERR(ts.client)) {
297 dev_err(dev, "failed to register adc client\n");
298 ret = PTR_ERR(ts.client);
299 goto err_iomap;
300 }
301
302 /* Initialise registers */
303 if ((info->delay & 0xffff) > 0)
304 writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
305
306 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
307
308 input_dev = input_allocate_device();
309 if (!input_dev) {
310 dev_err(dev, "Unable to allocate the input device !!\n");
311 ret = -ENOMEM;
312 goto err_iomap;
313 }
314
315 ts.input = input_dev;
316 ts.input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
317 ts.input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
318 input_set_abs_params(ts.input, ABS_X, 0, 0x3FF, 0, 0);
319 input_set_abs_params(ts.input, ABS_Y, 0, 0x3FF, 0, 0);
320
321 ts.input->name = "S3C24XX TouchScreen";
322 ts.input->id.bustype = BUS_HOST;
323 ts.input->id.vendor = 0xDEAD;
324 ts.input->id.product = 0xBEEF;
325 ts.input->id.version = 0x0102;
326
327 ts.shift = info->oversampling_shift;
328 ts.features = platform_get_device_id(pdev)->driver_data;
329
330 ret = request_irq(ts.irq_tc, stylus_irq, 0,
331 "s3c2410_ts_pen", ts.input);
332 if (ret) {
333 dev_err(dev, "cannot get TC interrupt\n");
334 goto err_inputdev;
335 }
336
337 dev_info(dev, "driver attached, registering input device\n");
338
339 /* All went ok, so register to the input system */
340 ret = input_register_device(ts.input);
341 if (ret < 0) {
342 dev_err(dev, "failed to register input device\n");
343 ret = -EIO;
344 goto err_tcirq;
345 }
346
347 return 0;
348
349 err_tcirq:
350 free_irq(ts.irq_tc, ts.input);
351 err_inputdev:
352 input_free_device(ts.input);
353 err_iomap:
354 iounmap(ts.io);
355 err_clk:
356 del_timer_sync(&touch_timer);
357 clk_put(ts.clock);
358 return ret;
359}
360
361/**
362 * s3c2410ts_remove - device core removal entry point
363 * @pdev: The device we are being removed from.
364 *
365 * Free up our state ready to be removed.
366 */
367static int s3c2410ts_remove(struct platform_device *pdev)
368{
369 free_irq(ts.irq_tc, ts.input);
370 del_timer_sync(&touch_timer);
371
372 clk_disable_unprepare(ts.clock);
373 clk_put(ts.clock);
374
375 input_unregister_device(ts.input);
376 iounmap(ts.io);
377
378 return 0;
379}
380
381#ifdef CONFIG_PM
382static int s3c2410ts_suspend(struct device *dev)
383{
384 writel(TSC_SLEEP, ts.io + S3C2410_ADCTSC);
385 disable_irq(ts.irq_tc);
386 clk_disable(ts.clock);
387
388 return 0;
389}
390
391static int s3c2410ts_resume(struct device *dev)
392{
393 struct platform_device *pdev = to_platform_device(dev);
394 struct s3c2410_ts_mach_info *info = dev_get_platdata(&pdev->dev);
395
396 clk_enable(ts.clock);
397 enable_irq(ts.irq_tc);
398
399 /* Initialise registers */
400 if ((info->delay & 0xffff) > 0)
401 writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
402
403 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
404
405 return 0;
406}
407
408static const struct dev_pm_ops s3c_ts_pmops = {
409 .suspend = s3c2410ts_suspend,
410 .resume = s3c2410ts_resume,
411};
412#endif
413
414static const struct platform_device_id s3cts_driver_ids[] = {
415 { "s3c2410-ts", 0 },
416 { "s3c2440-ts", 0 },
417 { "s3c64xx-ts", FEAT_PEN_IRQ },
418 { }
419};
420MODULE_DEVICE_TABLE(platform, s3cts_driver_ids);
421
422static struct platform_driver s3c_ts_driver = {
423 .driver = {
424 .name = "samsung-ts",
425#ifdef CONFIG_PM
426 .pm = &s3c_ts_pmops,
427#endif
428 },
429 .id_table = s3cts_driver_ids,
430 .probe = s3c2410ts_probe,
431 .remove = s3c2410ts_remove,
432};
433module_platform_driver(s3c_ts_driver);
434
435MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
436 "Ben Dooks <ben@simtec.co.uk>, "
437 "Simtec Electronics <linux@simtec.co.uk>");
438MODULE_DESCRIPTION("S3C24XX Touchscreen driver");
439MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Samsung S3C24XX touchscreen driver
4 *
5 * Copyright 2004 Arnaud Patard <arnaud.patard@rtp-net.org>
6 * Copyright 2008 Ben Dooks <ben-linux@fluff.org>
7 * Copyright 2009 Simtec Electronics <linux@simtec.co.uk>
8 *
9 * Additional work by Herbert Pƶtzl <herbert@13thfloor.at> and
10 * Harald Welte <laforge@openmoko.org>
11 */
12
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/input.h>
17#include <linux/delay.h>
18#include <linux/interrupt.h>
19#include <linux/platform_device.h>
20#include <linux/clk.h>
21#include <linux/io.h>
22
23#include <linux/soc/samsung/s3c-adc.h>
24#include <linux/platform_data/touchscreen-s3c2410.h>
25
26#define S3C2410_ADCCON (0x00)
27#define S3C2410_ADCTSC (0x04)
28#define S3C2410_ADCDLY (0x08)
29#define S3C2410_ADCDAT0 (0x0C)
30#define S3C2410_ADCDAT1 (0x10)
31#define S3C64XX_ADCUPDN (0x14)
32#define S3C2443_ADCMUX (0x18)
33#define S3C64XX_ADCCLRINT (0x18)
34#define S5P_ADCMUX (0x1C)
35#define S3C64XX_ADCCLRINTPNDNUP (0x20)
36
37/* ADCTSC Register Bits */
38#define S3C2443_ADCTSC_UD_SEN (1 << 8)
39#define S3C2410_ADCTSC_YM_SEN (1<<7)
40#define S3C2410_ADCTSC_YP_SEN (1<<6)
41#define S3C2410_ADCTSC_XM_SEN (1<<5)
42#define S3C2410_ADCTSC_XP_SEN (1<<4)
43#define S3C2410_ADCTSC_PULL_UP_DISABLE (1<<3)
44#define S3C2410_ADCTSC_AUTO_PST (1<<2)
45#define S3C2410_ADCTSC_XY_PST(x) (((x)&0x3)<<0)
46
47/* ADCDAT0 Bits */
48#define S3C2410_ADCDAT0_UPDOWN (1<<15)
49#define S3C2410_ADCDAT0_AUTO_PST (1<<14)
50#define S3C2410_ADCDAT0_XY_PST (0x3<<12)
51#define S3C2410_ADCDAT0_XPDATA_MASK (0x03FF)
52
53/* ADCDAT1 Bits */
54#define S3C2410_ADCDAT1_UPDOWN (1<<15)
55#define S3C2410_ADCDAT1_AUTO_PST (1<<14)
56#define S3C2410_ADCDAT1_XY_PST (0x3<<12)
57#define S3C2410_ADCDAT1_YPDATA_MASK (0x03FF)
58
59
60#define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
61
62#define INT_DOWN (0)
63#define INT_UP (1 << 8)
64
65#define WAIT4INT (S3C2410_ADCTSC_YM_SEN | \
66 S3C2410_ADCTSC_YP_SEN | \
67 S3C2410_ADCTSC_XP_SEN | \
68 S3C2410_ADCTSC_XY_PST(3))
69
70#define AUTOPST (S3C2410_ADCTSC_YM_SEN | \
71 S3C2410_ADCTSC_YP_SEN | \
72 S3C2410_ADCTSC_XP_SEN | \
73 S3C2410_ADCTSC_AUTO_PST | \
74 S3C2410_ADCTSC_XY_PST(0))
75
76#define FEAT_PEN_IRQ (1 << 0) /* HAS ADCCLRINTPNDNUP */
77
78/* Per-touchscreen data. */
79
80/**
81 * struct s3c2410ts - driver touchscreen state.
82 * @client: The ADC client we registered with the core driver.
83 * @dev: The device we are bound to.
84 * @input: The input device we registered with the input subsystem.
85 * @clock: The clock for the adc.
86 * @io: Pointer to the IO base.
87 * @xp: The accumulated X position data.
88 * @yp: The accumulated Y position data.
89 * @irq_tc: The interrupt number for pen up/down interrupt
90 * @count: The number of samples collected.
91 * @shift: The log2 of the maximum count to read in one go.
92 * @features: The features supported by the TSADC MOdule.
93 */
94struct s3c2410ts {
95 struct s3c_adc_client *client;
96 struct device *dev;
97 struct input_dev *input;
98 struct clk *clock;
99 void __iomem *io;
100 unsigned long xp;
101 unsigned long yp;
102 int irq_tc;
103 int count;
104 int shift;
105 int features;
106};
107
108static struct s3c2410ts ts;
109
110/**
111 * get_down - return the down state of the pen
112 * @data0: The data read from ADCDAT0 register.
113 * @data1: The data read from ADCDAT1 register.
114 *
115 * Return non-zero if both readings show that the pen is down.
116 */
117static inline bool get_down(unsigned long data0, unsigned long data1)
118{
119 /* returns true if both data values show stylus down */
120 return (!(data0 & S3C2410_ADCDAT0_UPDOWN) &&
121 !(data1 & S3C2410_ADCDAT0_UPDOWN));
122}
123
124static void touch_timer_fire(struct timer_list *unused)
125{
126 unsigned long data0;
127 unsigned long data1;
128 bool down;
129
130 data0 = readl(ts.io + S3C2410_ADCDAT0);
131 data1 = readl(ts.io + S3C2410_ADCDAT1);
132
133 down = get_down(data0, data1);
134
135 if (down) {
136 if (ts.count == (1 << ts.shift)) {
137 ts.xp >>= ts.shift;
138 ts.yp >>= ts.shift;
139
140 dev_dbg(ts.dev, "%s: X=%lu, Y=%lu, count=%d\n",
141 __func__, ts.xp, ts.yp, ts.count);
142
143 input_report_abs(ts.input, ABS_X, ts.xp);
144 input_report_abs(ts.input, ABS_Y, ts.yp);
145
146 input_report_key(ts.input, BTN_TOUCH, 1);
147 input_sync(ts.input);
148
149 ts.xp = 0;
150 ts.yp = 0;
151 ts.count = 0;
152 }
153
154 s3c_adc_start(ts.client, 0, 1 << ts.shift);
155 } else {
156 ts.xp = 0;
157 ts.yp = 0;
158 ts.count = 0;
159
160 input_report_key(ts.input, BTN_TOUCH, 0);
161 input_sync(ts.input);
162
163 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
164 }
165}
166
167static DEFINE_TIMER(touch_timer, touch_timer_fire);
168
169/**
170 * stylus_irq - touchscreen stylus event interrupt
171 * @irq: The interrupt number
172 * @dev_id: The device ID.
173 *
174 * Called when the IRQ_TC is fired for a pen up or down event.
175 */
176static irqreturn_t stylus_irq(int irq, void *dev_id)
177{
178 unsigned long data0;
179 unsigned long data1;
180 bool down;
181
182 data0 = readl(ts.io + S3C2410_ADCDAT0);
183 data1 = readl(ts.io + S3C2410_ADCDAT1);
184
185 down = get_down(data0, data1);
186
187 /* TODO we should never get an interrupt with down set while
188 * the timer is running, but maybe we ought to verify that the
189 * timer isn't running anyways. */
190
191 if (down)
192 s3c_adc_start(ts.client, 0, 1 << ts.shift);
193 else
194 dev_dbg(ts.dev, "%s: count=%d\n", __func__, ts.count);
195
196 if (ts.features & FEAT_PEN_IRQ) {
197 /* Clear pen down/up interrupt */
198 writel(0x0, ts.io + S3C64XX_ADCCLRINTPNDNUP);
199 }
200
201 return IRQ_HANDLED;
202}
203
204/**
205 * s3c24xx_ts_conversion - ADC conversion callback
206 * @client: The client that was registered with the ADC core.
207 * @data0: The reading from ADCDAT0.
208 * @data1: The reading from ADCDAT1.
209 * @left: The number of samples left.
210 *
211 * Called when a conversion has finished.
212 */
213static void s3c24xx_ts_conversion(struct s3c_adc_client *client,
214 unsigned data0, unsigned data1,
215 unsigned *left)
216{
217 dev_dbg(ts.dev, "%s: %d,%d\n", __func__, data0, data1);
218
219 ts.xp += data0;
220 ts.yp += data1;
221
222 ts.count++;
223
224 /* From tests, it seems that it is unlikely to get a pen-up
225 * event during the conversion process which means we can
226 * ignore any pen-up events with less than the requisite
227 * count done.
228 *
229 * In several thousand conversions, no pen-ups where detected
230 * before count completed.
231 */
232}
233
234/**
235 * s3c24xx_ts_select - ADC selection callback.
236 * @client: The client that was registered with the ADC core.
237 * @select: The reason for select.
238 *
239 * Called when the ADC core selects (or deslects) us as a client.
240 */
241static void s3c24xx_ts_select(struct s3c_adc_client *client, unsigned select)
242{
243 if (select) {
244 writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
245 ts.io + S3C2410_ADCTSC);
246 } else {
247 mod_timer(&touch_timer, jiffies+1);
248 writel(WAIT4INT | INT_UP, ts.io + S3C2410_ADCTSC);
249 }
250}
251
252/**
253 * s3c2410ts_probe - device core probe entry point
254 * @pdev: The device we are being bound to.
255 *
256 * Initialise, find and allocate any resources we need to run and then
257 * register with the ADC and input systems.
258 */
259static int s3c2410ts_probe(struct platform_device *pdev)
260{
261 struct s3c2410_ts_mach_info *info;
262 struct device *dev = &pdev->dev;
263 struct input_dev *input_dev;
264 struct resource *res;
265 int ret = -EINVAL;
266
267 /* Initialise input stuff */
268 memset(&ts, 0, sizeof(struct s3c2410ts));
269
270 ts.dev = dev;
271
272 info = dev_get_platdata(dev);
273 if (!info) {
274 dev_err(dev, "no platform data, cannot attach\n");
275 return -EINVAL;
276 }
277
278 dev_dbg(dev, "initialising touchscreen\n");
279
280 ts.clock = clk_get(dev, "adc");
281 if (IS_ERR(ts.clock)) {
282 dev_err(dev, "cannot get adc clock source\n");
283 return -ENOENT;
284 }
285
286 ret = clk_prepare_enable(ts.clock);
287 if (ret) {
288 dev_err(dev, "Failed! to enabled clocks\n");
289 goto err_clk_get;
290 }
291 dev_dbg(dev, "got and enabled clocks\n");
292
293 ts.irq_tc = ret = platform_get_irq(pdev, 0);
294 if (ret < 0) {
295 dev_err(dev, "no resource for interrupt\n");
296 goto err_clk;
297 }
298
299 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
300 if (!res) {
301 dev_err(dev, "no resource for registers\n");
302 ret = -ENOENT;
303 goto err_clk;
304 }
305
306 ts.io = ioremap(res->start, resource_size(res));
307 if (ts.io == NULL) {
308 dev_err(dev, "cannot map registers\n");
309 ret = -ENOMEM;
310 goto err_clk;
311 }
312
313 /* inititalise the gpio */
314 if (info->cfg_gpio)
315 info->cfg_gpio(to_platform_device(ts.dev));
316
317 ts.client = s3c_adc_register(pdev, s3c24xx_ts_select,
318 s3c24xx_ts_conversion, 1);
319 if (IS_ERR(ts.client)) {
320 dev_err(dev, "failed to register adc client\n");
321 ret = PTR_ERR(ts.client);
322 goto err_iomap;
323 }
324
325 /* Initialise registers */
326 if ((info->delay & 0xffff) > 0)
327 writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
328
329 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
330
331 input_dev = input_allocate_device();
332 if (!input_dev) {
333 dev_err(dev, "Unable to allocate the input device !!\n");
334 ret = -ENOMEM;
335 goto err_iomap;
336 }
337
338 ts.input = input_dev;
339 ts.input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
340 ts.input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
341 input_set_abs_params(ts.input, ABS_X, 0, 0x3FF, 0, 0);
342 input_set_abs_params(ts.input, ABS_Y, 0, 0x3FF, 0, 0);
343
344 ts.input->name = "S3C24XX TouchScreen";
345 ts.input->id.bustype = BUS_HOST;
346 ts.input->id.vendor = 0xDEAD;
347 ts.input->id.product = 0xBEEF;
348 ts.input->id.version = 0x0102;
349
350 ts.shift = info->oversampling_shift;
351 ts.features = platform_get_device_id(pdev)->driver_data;
352
353 ret = request_irq(ts.irq_tc, stylus_irq, 0,
354 "s3c2410_ts_pen", ts.input);
355 if (ret) {
356 dev_err(dev, "cannot get TC interrupt\n");
357 goto err_inputdev;
358 }
359
360 dev_info(dev, "driver attached, registering input device\n");
361
362 /* All went ok, so register to the input system */
363 ret = input_register_device(ts.input);
364 if (ret < 0) {
365 dev_err(dev, "failed to register input device\n");
366 ret = -EIO;
367 goto err_tcirq;
368 }
369
370 return 0;
371
372 err_tcirq:
373 free_irq(ts.irq_tc, ts.input);
374 err_inputdev:
375 input_free_device(ts.input);
376 err_iomap:
377 iounmap(ts.io);
378 err_clk:
379 clk_disable_unprepare(ts.clock);
380 del_timer_sync(&touch_timer);
381 err_clk_get:
382 clk_put(ts.clock);
383 return ret;
384}
385
386/**
387 * s3c2410ts_remove - device core removal entry point
388 * @pdev: The device we are being removed from.
389 *
390 * Free up our state ready to be removed.
391 */
392static int s3c2410ts_remove(struct platform_device *pdev)
393{
394 free_irq(ts.irq_tc, ts.input);
395 del_timer_sync(&touch_timer);
396
397 clk_disable_unprepare(ts.clock);
398 clk_put(ts.clock);
399
400 input_unregister_device(ts.input);
401 iounmap(ts.io);
402
403 return 0;
404}
405
406#ifdef CONFIG_PM
407static int s3c2410ts_suspend(struct device *dev)
408{
409 writel(TSC_SLEEP, ts.io + S3C2410_ADCTSC);
410 disable_irq(ts.irq_tc);
411 clk_disable(ts.clock);
412
413 return 0;
414}
415
416static int s3c2410ts_resume(struct device *dev)
417{
418 struct platform_device *pdev = to_platform_device(dev);
419 struct s3c2410_ts_mach_info *info = dev_get_platdata(&pdev->dev);
420
421 clk_enable(ts.clock);
422 enable_irq(ts.irq_tc);
423
424 /* Initialise registers */
425 if ((info->delay & 0xffff) > 0)
426 writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
427
428 writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
429
430 return 0;
431}
432
433static const struct dev_pm_ops s3c_ts_pmops = {
434 .suspend = s3c2410ts_suspend,
435 .resume = s3c2410ts_resume,
436};
437#endif
438
439static const struct platform_device_id s3cts_driver_ids[] = {
440 { "s3c2410-ts", 0 },
441 { "s3c2440-ts", 0 },
442 { "s3c64xx-ts", FEAT_PEN_IRQ },
443 { }
444};
445MODULE_DEVICE_TABLE(platform, s3cts_driver_ids);
446
447static struct platform_driver s3c_ts_driver = {
448 .driver = {
449 .name = "samsung-ts",
450#ifdef CONFIG_PM
451 .pm = &s3c_ts_pmops,
452#endif
453 },
454 .id_table = s3cts_driver_ids,
455 .probe = s3c2410ts_probe,
456 .remove = s3c2410ts_remove,
457};
458module_platform_driver(s3c_ts_driver);
459
460MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
461 "Ben Dooks <ben@simtec.co.uk>, "
462 "Simtec Electronics <linux@simtec.co.uk>");
463MODULE_DESCRIPTION("S3C24XX Touchscreen driver");
464MODULE_LICENSE("GPL v2");