blob: 062727b177ac8c192d8971d0f82d43bb7c80ddb0 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* drivers/input/touchscreen/touchscreen.c
2*
3* TouchScreen driver.
4*
5* Copyright (c)
6*
7* This software is licensed under the terms of the GNU General Public
8* License version 2, as published by the Free Software Foundation, and
9* may be copied, distributed, and modified under those terms.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16*/
17#include <linux/i2c.h>
18#include <linux/input.h>
19#include <linux/delay.h>
20#include <linux/slab.h>
21#include <linux/interrupt.h>
22#include <mach/irqs.h>
23#include <linux/kernel.h>
24#include <linux/semaphore.h>
25#include <linux/mutex.h>
26#include <linux/module.h>
27#include <linux/gpio.h>
28#include <linux/syscalls.h>
29#include <linux/unistd.h>
30#include <linux/uaccess.h>
31#include <linux/fs.h>
32#include <linux/string.h>
33#include <linux/timer.h>
34#include <linux/of.h>
35#include <linux/of_gpio.h>
36#include <linux/irq.h>
37#include <mach/gpio_cfg.h>
38#include <linux/input/touchscreen.h>
39
40static struct mutex i2c_rw_access;
41static u8 s_pmode = 0;
42static struct input_dev *tp_input_dev;
43static struct touchscreen_operations *ts_operations[MAX_TOUCHSCREEN_DEV_TYPE] = { NULL };
44static struct touchscreen_register *touchscreen_dev_register_tbl[MAX_TOUCHSCREEN_DEV_TYPE] = { NULL };
45static struct ts_data *s_touchscreen_data = NULL;
46
47enum touchscreen_type touchscreen_detected_type = MAX_TOUCHSCREEN_DEV_TYPE;
48
49void touchscreen_set_operations(enum touchscreen_type id, struct touchscreen_operations *pt_operations)
50{
51 ts_operations[id] = pt_operations;
52}
53
54void touchscreen_register(enum touchscreen_type id, struct touchscreen_register *pt_register)
55{
56 touchscreen_dev_register_tbl[id] = pt_register;
57}
58
59int tp_i2c_read(struct i2c_client *client, char *writebuf, int writelen, char *readbuf, int readlen)
60{
61 int ret = 0;
62
63 if (client == NULL)
64 {
65 dev_err(&client->dev, "[IIC][%s]i2c_client==NULL!\n", __func__);
66 return -1;
67 }
68
69 mutex_lock(&i2c_rw_access);
70
71 if (readlen > 0)
72 {
73 if (writelen > 0)
74 {
75 struct i2c_msg msgs[] =
76 {
77 {
78 .addr = client->addr,
79 .flags = 0,
80 .len = writelen,
81 .buf = writebuf,
82 },
83 {
84 .addr = client->addr,
85 .flags = I2C_M_RD,
86 .len = readlen,
87 .buf = readbuf,
88 },
89 };
90 ret = i2c_transfer(client->adapter, msgs, 2);
91 if (ret < 0)
92 {
93 dev_err(&client->dev, "%s [IIC]: i2c_transfer(write) error, ret=%d!!\n", __func__,ret);
94 }
95 }
96 else
97 {
98 struct i2c_msg msgs[] =
99 {
100 {
101 .addr = client->addr,
102 .flags = I2C_M_RD,
103 .len = readlen,
104 .buf = readbuf,
105 },
106 };
107 ret = i2c_transfer(client->adapter, msgs, 1);
108 if (ret < 0)
109 {
110 dev_err(&client->dev, "%s IIC]: i2c_transfer(read) error, ret=%d!!\n", __func__,ret);
111 }
112 }
113 }
114
115 mutex_unlock(&i2c_rw_access);
116
117 return ret;
118}
119
120int tp_i2c_write(struct i2c_client *client, char *writebuf, int writelen)
121{
122 int ret = 0;
123
124 if (client == NULL)
125 {
126 dev_err(&client->dev, "[IIC][%s]i2c_client==NULL!\n", __func__);
127 return -1;
128 }
129
130 mutex_lock(&i2c_rw_access);
131
132 if (writelen > 0)
133 {
134 struct i2c_msg msgs[] =
135 {
136 {
137 .addr = client->addr,
138 .flags = 0,
139 .len = writelen,
140 .buf = writebuf,
141 },
142 };
143 ret = i2c_transfer(client->adapter, msgs, 1);
144 if (ret < 0)
145 {
146 dev_err(&client->dev, "%s [IIC]: i2c_transfer(write) error, ret=%d!!.\n", __func__,ret);
147 }
148 }
149
150 mutex_unlock(&i2c_rw_access);
151
152 return ret;
153}
154
155int tp_read(struct i2c_client *client, u8 regaddr, u8 len, u8 *regvalue) //len:1 hyn_i2c_read_reg
156{
157 return tp_i2c_read(client, &regaddr, 1, regvalue, len);
158}
159
160int tp_write(struct i2c_client *client, u8 regaddr, u8 regvalue) //len: 2( any value) hyn_i2c_write_reg
161{
162 u8 buf[2] = {0};
163
164 buf[0] = regaddr;
165 buf[1] = regvalue;
166 return tp_i2c_write(client, buf, sizeof(buf));
167}
168
169static int tp_read_touchdata(struct ts_data *data)
170{
171 int ret = -1;
172
173 if (NULL == ts_operations[touchscreen_detected_type]) {
174 return -1;
175 }
176
177 if (NULL == ts_operations[touchscreen_detected_type]->ts_read_touchdata) {
178 return -1;
179 }
180
181 ret = ts_operations[touchscreen_detected_type]->ts_read_touchdata(data);
182
183 if (ret >= 0)
184 {
185 return 0;
186 }
187 return ret;
188
189}
190
191static void tp_report_values(struct ts_data *data)
192{
193 struct ts_event *event = data->events;
194
195 if (data->touch_points > 0) {
196 input_report_abs(data->input_dev, ABS_X, event[0].x);
197 input_report_abs(data->input_dev, ABS_Y, event[0].y);
198 input_report_key(data->input_dev, BTN_TOUCH, 1);
199
200 input_sync(data->input_dev);
201 } else if (data->touch_points == 0) {
202 input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 0);
203 input_report_key(data->input_dev, BTN_TOUCH, 0); /*Up*/
204 input_sync(data->input_dev);
205 }
206}
207
208static irqreturn_t tp_ts_interrupt(int irq, void *dev_id)
209{
210 struct ts_data *touchscreen_data = dev_id;
211 int ret = 0;
212 //printk("[TP]tp_ts_interrupt happen \n");
213 ret = tp_read_touchdata(touchscreen_data);
214
215 if (ret == 0)
216 tp_report_values(touchscreen_data);
217
218 return IRQ_HANDLED;
219}
220
221#ifdef CONFIG_SYSFS
222static ssize_t tp_sysfs_show(struct device *dev,
223 struct device_attribute *attr, char *buf);
224static ssize_t tp_sysfs_store(struct device *dev,
225 struct device_attribute *attr, const char *buf, size_t count);
226
227
228DEVICE_ATTR(tp_mode, S_IWUSR | S_IRUGO, tp_sysfs_show, tp_sysfs_store);
229
230static struct attribute * tp_sysfs_attrs[] = {
231 &dev_attr_tp_mode.attr,
232 NULL
233};
234
235static const struct attribute_group tp_sysfs_attr_group = {
236 .attrs = tp_sysfs_attrs,
237};
238
239static int tp_sysfs_create_group(struct ts_data *touchscreen_data)
240{
241 return sysfs_create_group(&touchscreen_data->input_dev->dev.kobj, &tp_sysfs_attr_group);
242}
243
244static void tp_sysfs_remove_group(struct ts_data *touchscreen_data)
245{
246 sysfs_remove_group(&touchscreen_data->client->dev.kobj, &tp_sysfs_attr_group);
247}
248
249static ssize_t tp_sysfs_show(struct device *dev,
250 struct device_attribute *attr, char *buf)
251{
252 u8 val = 0;
253 int ret = 0;
254
255 ret = tp_read(s_touchscreen_data->client, TP_REG_ID_PMODE, 1, &val);
256 if(ret < 0)
257 val = 0xff;
258
259 return scnprintf(buf, PAGE_SIZE, "%d",val);
260}
261
262static ssize_t tp_sysfs_store(struct device *dev,
263 struct device_attribute *attr, const char *buf, size_t count)
264{
265 u8 pmode_sleep = 0x03;
266 unsigned long pmode = 0;
267 u8 read_val = 0xff;
268 if(strict_strtoul(buf, 0, &pmode))
269 return -EINVAL;
270
271 if (pmode) {
272 if(TP_SLEEP_OFF == s_touchscreen_data->pmode)
273 return count;
274 gpio_set_value(PIN_TP_RST, 0);
275 msleep(20);
276 gpio_set_value(PIN_TP_RST, 1);
277 msleep(200);
278 s_touchscreen_data->pmode = TP_SLEEP_OFF;
279 //tp_write(s_touchscreen_data->client, TP_REG_ID_PMODE, &s_pmode);
280 //printk("tp_sysfs_store sleep out read_val = %d, pmode = %d ,s_pmode = %d\n",read_val,pmode,s_pmode);
281 } else {
282 if(TP_SLEEP_ON == s_touchscreen_data->pmode)
283 return count;
284 tp_write(s_touchscreen_data->client, TP_REG_ID_PMODE, pmode_sleep);
285 s_touchscreen_data->pmode = TP_SLEEP_ON;
286 //printk("tp_sysfs_store sleep in read_val = %d, pmode = %d ,s_pmode = %d\n",read_val,pmode,s_pmode);
287 }
288
289 return count;
290}
291#else
292static int tp_sysfs_create_group(struct ts_data *touchscreen_data)
293{
294 return 0;
295}
296
297static void tp_sysfs_remove_group(struct ts_data *touchscreen_data)
298{
299 return;
300}
301#endif
302
303static int ts_init_gpio(void)
304{
305 int err;
306 /* ÅäÖô¥ÃþÆÁʹÓõÄirq reset GPIO Òý½Å*/
307 err = gpio_request(PIN_TP_IRQ, "touchscreen_irq");
308 if (err) {
309 printk( "[TP]request touchscreen irq gpio failed\n");
310 return -1;
311 }
312 zx29_gpio_config(PIN_TP_IRQ, TP_IRQ_FUN_SEL);
313 zx29_gpio_set_inttype(PIN_TP_IRQ, IRQ_TYPE_EDGE_FALLING);
314 zx29_gpio_pd_pu_set(PIN_TP_IRQ, IO_CFG_PULL_DISABLE);
315
316 err = gpio_request(PIN_TP_RST, "touchscreen_reset");
317 if (err) {
318 printk( "[TP]request touchscreen reset gpio failed\n");
319 return -1;
320 }
321 zx29_gpio_config(PIN_TP_RST, TP_RST_GPIO_SEL);
322 gpio_direction_output(PIN_TP_RST, GPIO_OUT);
323
324 gpio_set_value(PIN_TP_RST, 0);
325 msleep(20);
326 gpio_set_value(PIN_TP_RST, 1);
327 msleep(200);
328
329 return 0;
330}
331
332static int touchscreen_dev_detect(struct i2c_client *client)
333{
334 int ret = 0;
335 u32 dev_index = 0;
336
337 ret = ts_init_gpio();
338 if(ret < 0)
339 {
340 return -1;
341 }
342
343 for (dev_index =0; dev_index < MAX_TOUCHSCREEN_DEV_TYPE; dev_index++) {
344 if (touchscreen_detected_type != MAX_TOUCHSCREEN_DEV_TYPE) {
345 printk(KERN_ERR "[TP]touchscreen_dev_detect:touchscreen_type=%d have been detected!\n",touchscreen_detected_type);
346 return 0;
347 }
348
349 if ( NULL == touchscreen_dev_register_tbl[dev_index]) {
350 printk(KERN_ERR "[TP]touchscreen_dev_detect:touchscreen_type=%d have been detected!\n",touchscreen_detected_type);
351 return -1;
352 }
353
354 if (touchscreen_dev_register_tbl[dev_index]->ts_id_detect != NULL) {
355 ret = touchscreen_dev_register_tbl[dev_index]->ts_id_detect(client);
356 printk("[TP]touchscreen_dev_detect dev_index = 0x%x, ret = 0x%x\n",dev_index,ret);
357 if (ret != 0) {
358 }
359 else{
360 break;
361 }
362 }
363 }
364
365 if (touchscreen_detected_type == MAX_TOUCHSCREEN_DEV_TYPE) {
366 printk(KERN_ERR "[TP]touchscreen_dev_detect:do not have the driver for this touchscreen type!\n");
367 return -ENXIO;
368 }
369 return 0;
370}
371
372static int ts_probe(struct i2c_client *client,
373const struct i2c_device_id *id)
374{
375 struct ts_data *touchscreen_data;
376 struct input_dev *input_dev;
377 int err = 0;
378 int ret = -1;
379
380 mutex_init(&i2c_rw_access);
381
382 ret = touchscreen_dev_detect(client);
383 if(ret < 0)
384 {
385 dev_err(&client->dev, "[TP]%s: touchscreen_dev_detect irq failed\n", __func__);
386 return -1;
387 }
388
389 touchscreen_data = kzalloc(sizeof(struct ts_data), GFP_KERNEL);
390 s_touchscreen_data = touchscreen_data;
391
392 if (!touchscreen_data) {
393 err = -ENOMEM;
394 goto exit_alloc_data_failed;
395 }
396
397 i2c_set_clientdata(client, touchscreen_data);
398 touchscreen_data->client = client;
399 touchscreen_data->x_max = 4095;
400 touchscreen_data->y_max = 4095;
401 /* ×¢²á¶ÁÈ¡ÖÐ¶Ï */
402 err = devm_request_threaded_irq(&client->dev, gpio_to_irq(PIN_TP_IRQ),
403 NULL, tp_ts_interrupt, IRQF_ONESHOT,
404 client->dev.driver->name, touchscreen_data);
405
406 if (err < 0) {
407 dev_err(&client->dev, "[TP]%s: request irq failed\n", __func__);
408 goto exit_irq_request_failed;
409 }
410 /* ·ÖÅäÒ»¸öʼþÉϱ¨½á¹¹Ìå */
411 input_dev = input_allocate_device();
412 if (!input_dev) {
413 err = -ENOMEM;
414 dev_err(&client->dev, "[TP]failed to allocate input device\n");
415 goto exit_input_dev_alloc_failed;
416 }
417
418 touchscreen_data->input_dev = input_dev;
419 /* ×¢²áÉ豸֧³ÖµÄevent ÀàÐÍ */
420 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
421
422 set_bit(ABS_MT_TOUCH_MAJOR, input_dev->absbit);
423 set_bit(ABS_MT_POSITION_X, input_dev->absbit);
424 set_bit(ABS_MT_POSITION_Y, input_dev->absbit);
425 set_bit(ABS_MT_PRESSURE, input_dev->absbit);
426
427 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, touchscreen_data->x_max, 0, 0);
428 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, touchscreen_data->y_max, 0, 0);
429 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
430 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, PRESS_MAX, 0, 0);
431 input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, CFG_MAX_TOUCH_POINTS, 0, 0);
432 input_set_abs_params(input_dev, ABS_X, 0, touchscreen_data->x_max, 0, 0);
433 input_set_abs_params(input_dev, ABS_Y, 0, touchscreen_data->y_max, 0, 0);
434 input_set_abs_params(input_dev, ABS_PRESSURE, 0, PRESS_MAX, 0, 0);
435
436 /* EV KEY °´¼üʼþ EV_ABS ¾ø¶Ô×ø±ê£¬Èç´¥ÃþÆÁÉϱ¨µÄ×ø±ê */
437 set_bit(EV_KEY, input_dev->evbit);
438 set_bit(EV_ABS, input_dev->evbit);
439
440 input_dev->name = TOUCHSCREEN_NAME;
441 /* ×¢²áÕâ¸öinput É豸 */
442 err = input_register_device(input_dev);
443 if (err) {
444 dev_err(&client->dev,
445 "[TP]ts_probe: failed to register input device: %s\n",
446 dev_name(&client->dev));
447 goto exit_input_register_device_failed;
448 }
449
450 tp_sysfs_create_group(touchscreen_data);
451
452 return 0;
453
454 exit_input_register_device_failed:
455 input_free_device(input_dev);
456
457 exit_input_dev_alloc_failed:
458 exit_irq_request_failed:
459 i2c_set_clientdata(client, NULL);
460 kfree(touchscreen_data);
461
462 exit_alloc_data_failed:
463 return err;
464}
465
466#ifdef CONFIG_PM
467static int ts_suspend(struct i2c_client *client, pm_message_t mesg)
468{
469 u8 pmode_sleep = 0x03;
470 struct ts_data *ts = i2c_get_clientdata(client);
471 dev_dbg(&ts->client->dev, "[TP]ts_suspend \n");
472 disable_irq(PIN_TP_IRQ);
473
474 tp_write(s_touchscreen_data->client, TP_REG_ID_PMODE, pmode_sleep);
475
476 return 0;
477}
478
479static int ts_resume(struct i2c_client *client)
480{
481 struct ts_data *ts = i2c_get_clientdata(client);
482 dev_dbg(&ts->client->dev, "[TP]ts_resume.\n");
483 if (gpio_is_valid(PIN_TP_RST)) {
484 gpio_set_value(PIN_TP_RST, 0);
485 msleep(20);
486 gpio_set_value(PIN_TP_RST, 1);
487 msleep(200);
488 }
489 enable_irq(PIN_TP_IRQ);
490 return 0;
491}
492#endif
493
494static int ts_remove(struct i2c_client *client)
495{
496 struct ts_data *touchscreen_data;
497 touchscreen_data = i2c_get_clientdata(client);
498 tp_sysfs_remove_group(touchscreen_data);
499 input_unregister_device(touchscreen_data->input_dev);
500 kfree(touchscreen_data);
501 i2c_set_clientdata(client, NULL);
502 return 0;
503}
504
505static const struct i2c_device_id ts_id[] = {
506 {TOUCHSCREEN_NAME, 0},
507 {}
508};
509
510MODULE_DEVICE_TABLE(i2c, ts_id);
511
512#define ts_match_table NULL
513
514static struct i2c_driver ts_driver = {
515 .probe = ts_probe,
516 .remove = ts_remove,
517 .id_table = ts_id,
518 .driver = {
519 .name = TOUCHSCREEN_NAME,
520 .owner = THIS_MODULE,
521 .of_match_table = ts_match_table,
522 },
523#ifdef CONFIG_PM
524 .suspend = ts_suspend,
525 .resume = ts_resume,
526#endif
527};
528
529static int __init ts_init(void)
530{
531 int ret;
532 ret = i2c_add_driver(&ts_driver);
533 if (ret)
534 pr_err("[TP]Adding touchscreen driver failed (errno = %d)\n", ret);
535
536 return ret;
537}
538
539static void __exit ts_exit(void)
540{
541 i2c_del_driver(&ts_driver);
542}
543
544module_init(ts_init);
545module_exit(ts_exit);
546
547MODULE_AUTHOR("Dyy");
548MODULE_DESCRIPTION("TouchScreen driver");
549MODULE_LICENSE("GPL");
550