This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
forked from Dax89/chuwi-dev
-
Notifications
You must be signed in to change notification settings - Fork 3
/
chipone_main.c
308 lines (244 loc) · 8.62 KB
/
chipone_main.c
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
#include <linux/module.h>
#include <linux/acpi.h>
#include <linux/i2c.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/timekeeping.h>
#include <linux/jiffies.h>
#include "chipone_fw.h"
#include "chipone_regs.h"
#include "chipone_sysfs.h"
#include "chipone_types.h"
#include "chipone.h"
static int screen_max_x = SCREEN_MAX_X;
static int screen_max_y = SCREEN_MAX_Y;
module_param(screen_max_x, int, S_IRUGO | S_IWUSR);
module_param(screen_max_y, int, S_IRUGO | S_IWUSR);
void do_gettimeofday(struct timeval *tv)
{
struct timespec64 ts;
ktime_get_real_ts64(&ts);
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec/1000;
}
static int chipone_ts_create_input_device(struct i2c_client *client, struct chipone_ts_data *data){
struct device* dev = &client->dev;
struct input_dev* input;
int err;
input = devm_input_allocate_device(dev);
if(!input){
return -ENOMEM;
}
input->name = client->name;
set_bit(INPUT_PROP_DIRECT, input->propbit);
set_bit(EV_KEY, input->evbit); // This device supports keys
set_bit(KEY_LEFTMETA, input->keybit);
input_mt_init_slots(input, MAX_POINTS, 0);
input_set_abs_params(input, ABS_MT_POSITION_X, 0, screen_max_x, 0, 0);
input_set_abs_params(input, ABS_MT_POSITION_Y, 0, screen_max_y, 0, 0);
// NOTE: Needs Investigation
input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
err = input_register_device(input);
if(err){
dev_err(dev, "Device '%s' registration failed\n", input->name);
return -ENODEV;
}
dev_info(dev, "Device '%s' registration succeeded\n", input->name);
data->input = input;
return 0;
}
static int chipone_ts_init_device(struct i2c_client* client){
struct device* dev = &client->dev;
int err;
if(chipone_ts_fw_update(client) != 0){
return -EINVAL;
}
err = chipone_ts_regs_set_resolution(client, screen_max_x, screen_max_y);
if(err < 0){
dev_warn(dev, "Failed to set screen resolution.\n");
}
return err;
}
static irqreturn_t chipone_ts_irq_handler(int irq, void* dev_id){
struct chipone_ts_data* data = (struct chipone_ts_data*)dev_id;
struct device* dev = &data->client->dev;
struct chipone_ts_coordinate_area_regs coordinatearea;
bool gesturechanged, needsync = false;
int i, cX, cY;
unsigned int msec_since;
if(chipone_ts_regs_get_header_area(data->client, &data->last_header_area) < 0){
dev_err(dev, "Cannot read header\n");
return IRQ_HANDLED;
}
if(chipone_ts_regs_get_coordinate_area(data->client, &coordinatearea) < 0){
dev_err(dev, "Cannot read coordinates\n");
return IRQ_HANDLED;
}
do_gettimeofday(&data->last_irq_event);
gesturechanged = coordinatearea.gesture_id != data->last_coordinate_area.gesture_id;
if((coordinatearea.gesture_id == 0) && (coordinatearea.num_pointer > 0)){
for(i = 0; i < coordinatearea.num_pointer; i++){
input_mt_slot(data->input, i);
input_mt_report_slot_state(data->input, MT_TOOL_FINGER, chipone_ts_regs_is_finger_down(&coordinatearea, i));
cX = X_POSITION(coordinatearea, i);
cY = Y_POSITION(coordinatearea, i);
#if defined(CONFIG_CW1515)
if(cX == 0 && (cY > 568 || cY < 570)){ // On CW1515 the software super key doesn't register as a key
if(coordinatearea.pointer[i].event_id == POINTER_EVENT_DOWN){
input_report_key(data->input, KEY_LEFTMETA, 1);
data->lastSuperPress = jiffies_to_msecs(jiffies);
}
if(coordinatearea.pointer[i].event_id == POINTER_EVENT_UP){
input_report_key(data->input, KEY_LEFTMETA, 0);
if(data->lastSuperPress > 0){
msec_since = jiffies_to_msecs(jiffies) - data->lastSuperPress;
if(msec_since > 10000){
dev_info(dev, "Held software super for %d msec.\n", msec_since);
dev_info(dev, "Resetting screen resolution.\n");
chipone_ts_init_device(data->client);
}
data->lastSuperPress = 0;
}
}
}else{
#endif
//dev_info(dev, "Orig Location: %d,%d\n", cX, cY);
#ifdef CHIPONE_XORG_HACK
cX = (-cX) + screen_max_x + offset_x;
cY = (-cY) + screen_max_y + offset_y;
#endif
//dev_info(dev, "Report Location: %d,%d\n", cX, cY);
input_report_abs(data->input, ABS_MT_TOUCH_MAJOR, coordinatearea.pointer[i].pressure);
input_report_abs(data->input, ABS_MT_WIDTH_MAJOR, coordinatearea.pointer[i].pressure);
input_report_abs(data->input, ABS_MT_POSITION_X, cX);
input_report_abs(data->input, ABS_MT_POSITION_Y, cY);
#if defined(CONFIG_CW1515)
}
#endif
}
input_mt_sync_frame(data->input);
needsync = true;
}
if(gesturechanged && (coordinatearea.gesture_id & GESTURE_ID_KEY0)){
dev_info(dev, "Super Key Detected\n");
input_report_key(data->input, KEY_LEFTMETA, 1);
input_report_key(data->input, KEY_LEFTMETA, 0);
needsync = true;
}
if(coordinatearea.gesture_id != 0){
dev_info(dev, "Gesture ID: %d\n", coordinatearea.gesture_id);
}
if(needsync)
input_sync(data->input);
data->last_coordinate_area = coordinatearea; // Save last touch information
return IRQ_HANDLED;
}
static int chipone_ts_probe(struct i2c_client* client, const struct i2c_device_id* id){
struct device* dev = &client->dev;
struct chipone_ts_data* data;
int err;
dev_info(dev, "Screen resolution: %dx%d\n", screen_max_x, screen_max_y);
dev_info(dev, "Kernel reports IRQ: 0x%x\n", client->irq);
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
if(!data){
dev_err(dev, "Cannot allocate device data\n");
return -ENOMEM;
}
data->lastSuperPress = 0;
if(client->irq != 0){
dev_info(dev, "Detected IRQ: 0x%x\n", client->irq);
data->irq = client->irq;
}else if(CHIPONE_IRQ){
dev_warn(dev, "Using hardcoded IRQ: 0x%x\n", CHIPONE_IRQ);
data->irq = CHIPONE_IRQ;
}else{
dev_err(dev, "Cannot get IRQ\n");
return -EINVAL;
}
data->client = client;
i2c_set_clientdata(client, data);
err = chipone_ts_create_input_device(client, data);
if(err){
dev_err(dev, "Input device creation failed\n");
return err;
}
err = chipone_ts_init_device(client);
if(err == -EINVAL){
return -EINVAL;
}
err = devm_request_threaded_irq(dev, data->irq, NULL, chipone_ts_irq_handler, IRQF_ONESHOT, client->name, data);
if(err != 0){
dev_err(dev, "IRQ Handler initialization failed for IRQ %x, error: %d\n", data->irq, err);
return -EINVAL;
}
return chipone_ts_sysfs_create(data);
}
static int chipone_ts_remove(struct i2c_client* client){
struct chipone_ts_data* data = (struct chipone_ts_data*)i2c_get_clientdata(client);
if(!data){
dev_err(&client->dev, "%s, cannot get userdata from i2c_client\n", __func__);
return -EINVAL;
}
chipone_ts_sysfs_remove(data);
return 0;
}
static int chipone_ts_suspend(struct device* dev){
struct i2c_client* client = i2c_verify_client(dev);
if(client){
struct chipone_ts_data* data = (struct chipone_ts_data*)i2c_get_clientdata(client);
devm_free_irq(dev, data->irq, data);
}
return 0;
}
static int chipone_ts_resume(struct device* dev){
struct i2c_client* client = i2c_verify_client(dev);
int err;
if(client){
struct chipone_ts_data* data = (struct chipone_ts_data*)i2c_get_clientdata(client);
err = chipone_ts_init_device(client);
if(err == -EINVAL){
return -EINVAL;
}
err = devm_request_threaded_irq(dev, data->irq, NULL, chipone_ts_irq_handler, IRQF_ONESHOT, client->name, data);
if(err != 0){
dev_err(dev, "IRQ Handler initialization failed for IRQ %x, error: %d\n", data->irq, err);
return -EINVAL;
}
}
return 0;
}
static const struct i2c_device_id chipone_ts_id[] = {
{"CHPN0001:00", 0},
{}
};
MODULE_DEVICE_TABLE(i2c, chipone_ts_id);
static const struct acpi_device_id chipone_ts_acpi_id[] = {
{"CHPN0001", 0},
{}
};
MODULE_DEVICE_TABLE(acpi, chipone_ts_acpi_id);
static struct dev_pm_ops chipone_ts_pm_ops = {
.suspend = chipone_ts_suspend,
.resume = chipone_ts_resume
};
static struct i2c_driver chipone_ts_driver = {
.probe = chipone_ts_probe,
.remove = chipone_ts_remove,
.id_table = chipone_ts_id,
.driver = {
.name = CHIPONE_DRIVER_NAME,
.owner = THIS_MODULE,
.acpi_match_table = ACPI_PTR(chipone_ts_acpi_id),
.pm = &chipone_ts_pm_ops
},
};
module_i2c_driver(chipone_ts_driver);
MODULE_SOFTDEP("pre: pinctrl_cherryview");
MODULE_DESCRIPTION("Chipone touchscreen controller driver");
MODULE_AUTHOR("Antonio Davide Trogu <[email protected]>, John M. Harris, Jr. <[email protected]>");
MODULE_VERSION("1.1");
MODULE_LICENSE("GPL");