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
|
/*
* Freesclae STMP37XX/STMP378X Touchscreen driver
*
* Author: Vitaly Wool <vital@embeddedalley.com>
*
* Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
* Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
*/
/*
* The code contained herein is licensed under the GNU General Public
* License. You may obtain a copy of the GNU General Public License
* Version 2 or later at the following locations:
*
* http://www.opensource.org/licenses/gpl-license.html
* http://www.gnu.org/copyleft/gpl.html
*/
/* #define DEBUG*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/pm.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <mach/lradc.h>
#include <mach/hardware.h>
#include <mach/platform.h>
#include <mach/regs-lradc.h>
#define TOUCH_DEBOUNCE_TOLERANCE 100
struct stmp3xxx_ts_info {
int touch_irq;
int device_irq;
struct input_dev *idev;
enum {
TS_STATE_DISABLED,
TS_STATE_TOUCH_DETECT,
TS_STATE_TOUCH_VERIFY,
TS_STATE_X_PLANE,
TS_STATE_Y_PLANE,
} state;
u16 x;
u16 y;
int sample_count;
};
static inline void enter_state_touch_detect(struct stmp3xxx_ts_info *info)
{
stmp3xxx_clearl(0xFFFFFFFF, REGS_LRADC_BASE + HW_LRADC_CHn(2));
stmp3xxx_clearl(0xFFFFFFFF, REGS_LRADC_BASE + HW_LRADC_CHn(3));
stmp3xxx_clearl(0xFFFFFFFF, REGS_LRADC_BASE + HW_LRADC_CHn(4));
stmp3xxx_clearl(0xFFFFFFFF, REGS_LRADC_BASE + HW_LRADC_CHn(5));
stmp3xxx_clearl(BM_LRADC_CTRL1_LRADC5_IRQ, REGS_LRADC_BASE + HW_LRADC_CTRL1);
stmp3xxx_clearl(BM_LRADC_CTRL1_TOUCH_DETECT_IRQ, REGS_LRADC_BASE + HW_LRADC_CTRL1);
/*
* turn off the yplus and yminus pullup and pulldown, and turn off touch
* detect (enables yminus, and xplus through a resistor.On a press,
* xplus is pulled down)
*/
stmp3xxx_clearl(BM_LRADC_CTRL0_YMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_YPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_XMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_XPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_setl(BM_LRADC_CTRL0_TOUCH_DETECT_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
hw_lradc_set_delay_trigger_kick(LRADC_DELAY_TRIGGER_TOUCHSCREEN, 0);
info->state = TS_STATE_TOUCH_DETECT;
info->sample_count = 0;
}
static inline void enter_state_disabled(struct stmp3xxx_ts_info *info)
{
stmp3xxx_clearl(BM_LRADC_CTRL0_YMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_YPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_XMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_XPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_TOUCH_DETECT_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
hw_lradc_set_delay_trigger_kick(LRADC_DELAY_TRIGGER_TOUCHSCREEN, 0);
info->state = TS_STATE_DISABLED;
info->sample_count = 0;
}
static inline void enter_state_x_plane(struct stmp3xxx_ts_info *info)
{
stmp3xxx_setl(BM_LRADC_CTRL0_YMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_setl(BM_LRADC_CTRL0_YPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_XMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_XPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_TOUCH_DETECT_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
hw_lradc_set_delay_trigger_kick(LRADC_DELAY_TRIGGER_TOUCHSCREEN, 1);
info->state = TS_STATE_X_PLANE;
info->sample_count = 0;
}
static inline void enter_state_y_plane(struct stmp3xxx_ts_info *info)
{
stmp3xxx_clearl(BM_LRADC_CTRL0_YMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_YPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_setl(BM_LRADC_CTRL0_XMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_setl(BM_LRADC_CTRL0_XPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_TOUCH_DETECT_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
hw_lradc_set_delay_trigger_kick(LRADC_DELAY_TRIGGER_TOUCHSCREEN, 1);
info->state = TS_STATE_Y_PLANE;
info->sample_count = 0;
}
static inline void enter_state_touch_verify(struct stmp3xxx_ts_info *info)
{
stmp3xxx_clearl(BM_LRADC_CTRL0_YMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_YPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_XMINUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_clearl(BM_LRADC_CTRL0_XPLUS_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
stmp3xxx_setl(BM_LRADC_CTRL0_TOUCH_DETECT_ENABLE, REGS_LRADC_BASE + HW_LRADC_CTRL0);
info->state = TS_STATE_TOUCH_VERIFY;
hw_lradc_set_delay_trigger_kick(LRADC_DELAY_TRIGGER_TOUCHSCREEN, 1);
info->sample_count = 0;
}
static void process_lradc(struct stmp3xxx_ts_info *info, u16 x, u16 y,
int pressure)
{
switch (info->state) {
case TS_STATE_X_PLANE:
pr_debug("%s: x plane state, sample_count %d\n", __func__,
info->sample_count);
if (info->sample_count < 2) {
info->x = x;
info->sample_count++;
} else {
if (abs(info->x - x) > TOUCH_DEBOUNCE_TOLERANCE)
info->sample_count = 1;
else {
u16 x_c = info->x * (info->sample_count - 1);
info->x = (x_c + x) / info->sample_count;
info->sample_count++;
}
}
if (info->sample_count > 4)
enter_state_y_plane(info);
else
hw_lradc_set_delay_trigger_kick(
LRADC_DELAY_TRIGGER_TOUCHSCREEN, 1);
break;
case TS_STATE_Y_PLANE:
pr_debug("%s: y plane state, sample_count %d\n", __func__,
info->sample_count);
if (info->sample_count < 2) {
info->y = y;
info->sample_count++;
} else {
if (abs(info->y - y) > TOUCH_DEBOUNCE_TOLERANCE)
info->sample_count = 1;
else {
u16 y_c = info->y * (info->sample_count - 1);
info->y = (y_c + y) / info->sample_count;
info->sample_count++;
}
}
if (info->sample_count > 4)
enter_state_touch_verify(info);
else
hw_lradc_set_delay_trigger_kick(
LRADC_DELAY_TRIGGER_TOUCHSCREEN, 1);
break;
case TS_STATE_TOUCH_VERIFY:
pr_debug("%s: touch verify state, sample_count %d\n", __func__,
info->sample_count);
pr_debug("%s: x %d, y %d\n", __func__, info->x, info->y);
input_report_abs(info->idev, ABS_X, info->x);
input_report_abs(info->idev, ABS_Y, info->y);
input_report_abs(info->idev, ABS_PRESSURE, pressure);
input_sync(info->idev);
/* fall through */
case TS_STATE_TOUCH_DETECT:
pr_debug("%s: touch detect state, sample_count %d\n", __func__,
info->sample_count);
if (pressure) {
input_report_abs(info->idev, ABS_PRESSURE, pressure);
enter_state_x_plane(info);
hw_lradc_set_delay_trigger_kick(
LRADC_DELAY_TRIGGER_TOUCHSCREEN, 1);
} else
enter_state_touch_detect(info);
break;
default:
printk(KERN_ERR "%s: unknown touchscreen state %d\n", __func__,
info->state);
}
}
static irqreturn_t ts_handler(int irq, void *dev_id)
{
struct stmp3xxx_ts_info *info = dev_id;
u16 x_plus, y_plus;
int pressure = 0;
if (irq == info->touch_irq)
stmp3xxx_clearl(BM_LRADC_CTRL1_TOUCH_DETECT_IRQ, REGS_LRADC_BASE + HW_LRADC_CTRL1);
else if (irq == info->device_irq)
stmp3xxx_clearl(BM_LRADC_CTRL1_LRADC5_IRQ, REGS_LRADC_BASE + HW_LRADC_CTRL1);
/* get x, y values */
x_plus = __raw_readl(REGS_LRADC_BASE + HW_LRADC_CHn(LRADC_TOUCH_X_PLUS)) & BM_LRADC_CHn_VALUE;
y_plus = __raw_readl(REGS_LRADC_BASE + HW_LRADC_CHn(LRADC_TOUCH_Y_PLUS)) & BM_LRADC_CHn_VALUE;
/* pressed? */
if (__raw_readl(REGS_LRADC_BASE + HW_LRADC_STATUS) & BM_LRADC_STATUS_TOUCH_DETECT_RAW)
pressure = 1;
pr_debug("%s: irq %d, x_plus %d, y_plus %d, pressure %d\n",
__func__, irq, x_plus, y_plus, pressure);
process_lradc(info, x_plus, y_plus, pressure);
return IRQ_HANDLED;
}
static int stmp3xxx_ts_probe(struct platform_device *pdev)
{
struct input_dev *idev;
struct stmp3xxx_ts_info *info;
int ret = 0;
struct resource *res;
idev = input_allocate_device();
info = kzalloc(sizeof(struct stmp3xxx_ts_info), GFP_KERNEL);
if (idev == NULL || info == NULL) {
ret = -ENOMEM;
goto out_nomem;
}
idev->name = "STMP3XXX touchscreen";
idev->evbit[0] = BIT(EV_ABS);
input_set_abs_params(idev, ABS_X, 0, 0xFFF, 0, 0);
input_set_abs_params(idev, ABS_Y, 0, 0xFFF, 0, 0);
input_set_abs_params(idev, ABS_PRESSURE, 0, 1, 0, 0);
ret = input_register_device(idev);
if (ret)
goto out_nomem;
info->idev = idev;
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (!res) {
printk(KERN_ERR "%s: couldn't get IRQ resource\n", __func__);
ret = -ENODEV;
goto out_nodev;
}
info->touch_irq = res->start;
res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
if (!res) {
printk(KERN_ERR "%s: couldn't get IRQ resource\n", __func__);
ret = -ENODEV;
goto out_nodev;
}
info->device_irq = res->start;
ret = request_irq(info->touch_irq, ts_handler, IRQF_DISABLED,
"stmp3xxx_ts_touch", info);
if (ret)
goto out_nodev;
ret = request_irq(info->device_irq, ts_handler, IRQF_DISABLED,
"stmp3xxx_ts_dev", info);
if (ret) {
free_irq(info->touch_irq, info);
goto out_nodev;
}
enter_state_touch_detect(info);
hw_lradc_use_channel(LRADC_CH2);
hw_lradc_use_channel(LRADC_CH3);
hw_lradc_use_channel(LRADC_CH5);
hw_lradc_configure_channel(LRADC_CH2, 0, 0, 0);
hw_lradc_configure_channel(LRADC_CH3, 0, 0, 0);
hw_lradc_configure_channel(LRADC_CH5, 0, 0, 0);
/* Clear the accumulator & NUM_SAMPLES for the channels */
stmp3xxx_clearl(0xFFFFFFFF, REGS_LRADC_BASE + HW_LRADC_CHn(LRADC_CH2));
stmp3xxx_clearl(0xFFFFFFFF, REGS_LRADC_BASE + HW_LRADC_CHn(LRADC_CH3));
stmp3xxx_clearl(0xFFFFFFFF, REGS_LRADC_BASE + HW_LRADC_CHn(LRADC_CH5));
hw_lradc_set_delay_trigger(LRADC_DELAY_TRIGGER_TOUCHSCREEN,
0x3c, 0, 0, 8);
stmp3xxx_clearl(BM_LRADC_CTRL1_LRADC5_IRQ, REGS_LRADC_BASE + HW_LRADC_CTRL1);
stmp3xxx_clearl(BM_LRADC_CTRL1_TOUCH_DETECT_IRQ, REGS_LRADC_BASE + HW_LRADC_CTRL1);
stmp3xxx_setl(BM_LRADC_CTRL1_LRADC5_IRQ_EN, REGS_LRADC_BASE + HW_LRADC_CTRL1);
stmp3xxx_setl(BM_LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, REGS_LRADC_BASE + HW_LRADC_CTRL1);
platform_set_drvdata(pdev, info);
device_init_wakeup(&pdev->dev, 1);
goto out;
out_nodev:
input_free_device(idev);
out_nomem:
kfree(idev);
kfree(info);
out:
return ret;
}
static int stmp3xxx_ts_remove(struct platform_device *pdev)
{
struct stmp3xxx_ts_info *info = platform_get_drvdata(pdev);
platform_set_drvdata(pdev, NULL);
hw_lradc_unuse_channel(LRADC_CH2);
hw_lradc_unuse_channel(LRADC_CH3);
hw_lradc_unuse_channel(LRADC_CH5);
stmp3xxx_clearl(BM_LRADC_CTRL1_LRADC5_IRQ_EN, REGS_LRADC_BASE + HW_LRADC_CTRL1);
stmp3xxx_clearl(BM_LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, REGS_LRADC_BASE + HW_LRADC_CTRL1);
free_irq(info->device_irq, info);
free_irq(info->touch_irq, info);
input_free_device(info->idev);
enter_state_disabled(info);
kfree(info->idev);
kfree(info);
return 0;
}
static int stmp3xxx_ts_suspend(struct platform_device *pdev,
pm_message_t state)
{
#ifdef CONFIG_PM
if (!device_may_wakeup(&pdev->dev)) {
hw_lradc_unuse_channel(LRADC_CH2);
hw_lradc_unuse_channel(LRADC_CH3);
hw_lradc_unuse_channel(LRADC_CH5);
}
#endif
return 0;
}
static int stmp3xxx_ts_resume(struct platform_device *pdev)
{
#ifdef CONFIG_PM
if (!device_may_wakeup(&pdev->dev)) {
hw_lradc_use_channel(LRADC_CH2);
hw_lradc_use_channel(LRADC_CH3);
hw_lradc_use_channel(LRADC_CH5);
}
#endif
return 0;
}
static struct platform_driver stmp3xxx_ts_driver = {
.probe = stmp3xxx_ts_probe,
.remove = stmp3xxx_ts_remove,
.suspend = stmp3xxx_ts_suspend,
.resume = stmp3xxx_ts_resume,
.driver = {
.name = "stmp3xxx_ts",
},
};
static int __init stmp3xxx_ts_init(void)
{
return platform_driver_register(&stmp3xxx_ts_driver);
}
static void __exit stmp3xxx_ts_exit(void)
{
platform_driver_unregister(&stmp3xxx_ts_driver);
}
module_init(stmp3xxx_ts_init);
module_exit(stmp3xxx_ts_exit);
|