summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/mxc_ts.c
blob: 610f31e657786ff2ed8f28837e22a563a8905919 (plain)
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
/*
 * Freescale touchscreen driver
 *
 * Copyright (C) 2007-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * 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
 */

/*!
 * @file mxc_ts.c
 *
 * @brief Driver for the Freescale Semiconductor MXC touchscreen with calibration support.
 *
 * The touchscreen driver is designed as a standard input driver which is a
 * wrapper over low level PMIC driver. Most of the hardware configuration and
 * touchscreen functionality is implemented in the low level PMIC driver. During
 * initialization, this driver creates a kernel thread. This thread then calls
 * PMIC driver to obtain touchscreen values continously. These values are then
 * passed to the input susbsystem.
 *
 * @ingroup touchscreen
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/freezer.h>
#include <linux/pmic_external.h>
#include <linux/pmic_adc.h>
#include <linux/kthread.h>

#define MXC_TS_NAME	"mxc_ts"

static struct input_dev *mxc_inputdev;
static struct task_struct *tstask;
/**
 * calibration array refers to
 * (delta_x[0], delta_x[1], delta_x[2], delta_y[0], delta_y[1], delta_y[2], delta).
 * Which generated by calibration service.
 * In this driver when we got touch pointer (x', y') from PMIC ADC,
 * we calculate the display pointer (x,y) by:
 * x = (delta_x[0] * x' + delta_x[1] * y' + delta_x[2]) / delta;
 * y = (delta_y[0] * x' + delta_y[1] * y' + delta_y[2]) / delta;
 */
static int calibration[7];
module_param_array(calibration, int, NULL, S_IRUGO | S_IWUSR);

static int ts_thread(void *arg)
{
	t_touch_screen ts_sample;
	s32 wait = 0, wait2 = 0;

	do {
		int x, y;
		static int last_x = -1, last_y = -1, last_press = -1;

		memset(&ts_sample, 0, sizeof(t_touch_screen));

		/* After 2 consecutive samples with the pen up, enable irq waiting */
		if (0 != pmic_adc_get_touch_sample(&ts_sample, !(wait + wait2))) {
			msleep(20);
			continue;
		}
		if (!(ts_sample.contact_resistance || wait))
		{
			msleep(20);
			continue;
		}

		if (ts_sample.x_position == 0 && ts_sample.y_position == 0 &&
			ts_sample.contact_resistance == 0) {
			x = last_x;
			y = last_y;
		} else if (calibration[6] == 0) {
			x = ts_sample.x_position;
			y = ts_sample.y_position;
		} else {
			x = calibration[0] * (int)ts_sample.x_position +
				calibration[1] * (int)ts_sample.y_position +
				calibration[2];
			x /= calibration[6];
			if (x < 0)
				x = 0;
			y = calibration[3] * (int)ts_sample.x_position +
				calibration[4] * (int)ts_sample.y_position +
				calibration[5];
			y /= calibration[6];
			if (y < 0)
				y = 0;
		}

		if (x != last_x) {
			input_report_abs(mxc_inputdev, ABS_X, x);
			last_x = x;
		}
		if (y != last_y) {
			input_report_abs(mxc_inputdev, ABS_Y, y);
			last_y = y;
		}

		/* report pressure */
		input_report_abs(mxc_inputdev, ABS_PRESSURE,
				 ts_sample.contact_resistance);

#ifdef CONFIG_MXC_PMIC_MC13892
		/* workaround for aplite ADC resistance large range value */
		if (ts_sample.contact_resistance > 22)
			ts_sample.contact_resistance = 1;
		else
			ts_sample.contact_resistance = 0;
#endif
		/* report the BTN_TOUCH */
		if (ts_sample.contact_resistance != last_press)
			input_event(mxc_inputdev, EV_KEY,
					BTN_TOUCH, ts_sample.contact_resistance);

		input_sync(mxc_inputdev);
		last_press = ts_sample.contact_resistance;
		wait2 = wait;
		wait = ts_sample.contact_resistance;
		msleep(20);
	} while (!kthread_should_stop());

	return 0;
}

static int __init mxc_ts_init(void)
{
	int retval;

	if (!is_pmic_adc_ready())
		return -ENODEV;

	mxc_inputdev = input_allocate_device();
	if (!mxc_inputdev) {
		printk(KERN_ERR
		       "mxc_ts_init: not enough memory\n");
		return -ENOMEM;
	}

	mxc_inputdev->name = MXC_TS_NAME;
	mxc_inputdev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
	mxc_inputdev->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
	mxc_inputdev->absbit[0] =
	    BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) | BIT_MASK(ABS_PRESSURE);
	retval = input_register_device(mxc_inputdev);
	if (retval < 0) {
		input_free_device(mxc_inputdev);
		return retval;
	}

	tstask = kthread_run(ts_thread, NULL, "mxc_ts");
	if (IS_ERR(tstask)) {
		printk(KERN_ERR
			"mxc_ts_init: failed to create kthread");
		tstask = NULL;
		return -1;
	}
	printk("mxc input touchscreen loaded\n");
	return 0;
}

static void __exit mxc_ts_exit(void)
{
	if (tstask)
		kthread_stop(tstask);

	input_unregister_device(mxc_inputdev);

	if (mxc_inputdev) {
		input_free_device(mxc_inputdev);
		mxc_inputdev = NULL;
	}
}

late_initcall(mxc_ts_init);
module_exit(mxc_ts_exit);

MODULE_DESCRIPTION("MXC touchscreen driver with calibration");
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_LICENSE("GPL");