1 /****************************************************************************************
2 * driver/input/touchscreen/hannstar_p1003.c
3 *Copyright :ROCKCHIP Inc
6 *This driver use for rk28 chip extern touchscreen. Use i2c IF ,the chip is Hannstar
8 ********************************************************************************************/
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/earlysuspend.h>
12 #include <linux/hrtimer.h>
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/async.h>
19 #include <mach/gpio.h>
20 #include <linux/irq.h>
21 #include <mach/board.h>
25 #define MAX_SUPPORT_POINT 2// // 4
26 #define PACKGE_BUFLEN 10
29 #define DBG(x...) printk(KERN_INFO x)
31 #define DBG(x...) do { } while (0)
33 //#define Singltouch_Mode
36 #define sakura_dbg_msg(fmt,...) do { \
37 printk("sakura dbg msg------>" \
38 " (func-->%s ; line-->%d) " fmt, __func__, __LINE__ , ##__VA_ARGS__); \
40 #define sakura_dbg_report_key_msg(fmt,...) do{ \
41 printk("sakura report " fmt,##__VA_ARGS__); \
44 #define sakura_dbg_msg(fmt,...) do {} while(0)
45 #define sakura_dbg_report_key_msg(fmt,...) do{}while(0)
54 struct multitouch_event{
55 struct point_data point_data[MAX_SUPPORT_POINT];
61 struct input_dev *input;
63 struct delayed_work work;
64 struct workqueue_struct *wq;
65 struct i2c_client *client;
66 struct multitouch_event mt_event;
73 int has_relative_report;
74 int (*get_pendown_state)(void);
75 void (*clear_penirq)(void);
78 int p1003_get_pendown_state(void)
83 static void p1003_report_event(struct ts_p1003 *ts,struct multitouch_event *tc)
85 struct input_dev *input = ts->input;
87 dev_dbg(&ts->client->dev, "UP\n");
88 DBG("Enter:%s %d\n",__FUNCTION__,__LINE__);
89 for(i=0; i<MAX_SUPPORT_POINT;i++){
90 if(tc->point_data[i].status >= 0){
91 pandown |= tc->point_data[i].status;
92 input_report_abs(input, ABS_MT_TRACKING_ID, i);
93 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tc->point_data[i].status);
94 input_report_abs(input, ABS_MT_WIDTH_MAJOR, 0);
95 input_report_abs(input, ABS_MT_POSITION_X, tc->point_data[i].x);
96 input_report_abs(input, ABS_MT_POSITION_Y, tc->point_data[i].y);
99 sakura_dbg_report_key_msg("ABS_MT_TRACKING_ID = %x, ABS_MT_TOUCH_MAJOR = %x\n ABS_MT_POSITION_X = %x, ABS_MT_POSITION_Y = %x\n",i,tc->point_data[i].status,tc->point_data[i].x,tc->point_data[i].y);
100 #if defined(CONFIG_HANNSTAR_DEBUG)
101 //printk("hannstar p1003 Px = [%d],Py = [%d] \n",tc->point_data[i].x,tc->point_data[i].y);
104 if(tc->point_data[i].status == 0)
105 tc->point_data[i].status--;
110 ts->pendown = pandown;
114 #if defined (Singltouch_Mode)
115 static void p1003_report_single_event(struct ts_p1003 *ts,struct multitouch_event *tc)
117 struct input_dev *input = ts->input;
122 input_report_abs(input, ABS_X, tc->point_data[cid].x);
123 input_report_abs(input, ABS_Y, tc->point_data[cid].y);
126 if(ts->pendown != ts->status){
127 ts->pendown = ts->status;
128 input_report_key(input, BTN_TOUCH, ts->status);
130 sakura_dbg_report_key_msg("%s x =0x%x,y = 0x%x \n",ts->status?"down":"up",tc->point_data[cid].x,tc->point_data[cid].y);
135 static inline int p1003_check_firmwork(struct ts_p1003 *ts)
139 data = i2c_master_reg8_recv(ts->client, 26, buf, 6, 100*1000);
141 dev_err(&ts->client->dev, "i2c io error %d \n", data);
144 //printk("sintek reg[0] = %x ,reg[1] = %x, reg[2] = %x, reg[3] = %x\n" , buf[0],buf[1],buf[2],buf[3]);
145 //printk("sintek reg[4] = %x ,reg[5] = %x, reg[6] = %x, reg[7] = %x\n" , buf[4],buf[5],buf[6],buf[7]);
146 buf[0] = 0xa4; /*automatically jump sleep mode*/
148 data = i2c_master_reg8_send(ts->client, MALATA_POWER_MODE, buf, 1, 100*1000);
150 printk("i2c io error %d line=%d\n", data,__LINE__);
154 data = i2c_master_reg8_send(ts->client, MALATA_SPECOP, buf, 1, 100*1000);
156 printk( "i2c io error %d line=%d\n", data,__LINE__);
161 data = i2c_master_reg8_send(ts->client, MALATA_INT_MODE, buf, 1, 200*1000);
163 printk( "i2c io error %d line=%d\n", data,__LINE__);
167 data = i2c_master_reg8_recv(ts->client, MALATA_DATA_INFO, buf, 4, 100*1000);
169 printk( "i2c io error %d line=%d\n", data,__LINE__);
172 //printk("sintek reg[0] = %x ,reg[1] = %x, reg[2] = %x, reg[3] = %x\n" , buf[0],buf[1],buf[2],buf[3]);
173 //printk("sintek reg[4] = %x ,reg[5] = %x, reg[6] = %x, reg[7] = %x\n" , buf[4],buf[5],buf[6],buf[7]);
177 #define DATA_START MALATA_DATA_INFO
178 #define DATA_END MALATA_DATA_END
179 #define DATA_LEN (DATA_END - DATA_START)
180 #define DATA_OFF(x) ((x) - DATA_START)
183 static inline int p1003_read_values(struct ts_p1003 *ts, struct multitouch_event *tc)
187 data = i2c_master_reg8_recv(ts->client, 0x00, buf, 10, 100*1000);
189 printk("-->%s i2c io error %d line=%d\n",__FUNCTION__, data,__LINE__);
194 printk("MALATA_TOUCH_NUM is 0xff full\n");
198 ///printk("MALATA_TOUCH_NUM = %x\n",buf[0]);
202 tc->point_data[0].status = 1;
203 tc->point_data[1].status = 0;
205 else if (buf[0] == 2){
207 tc->point_data[0].status = 1;
208 tc->point_data[1].status = 1;
212 tc->point_data[0].status = 0;
213 tc->point_data[1].status = 0;
216 //data = i2c_master_reg8_recv(ts->client, DATA_START, buf, DATA_LEN, 100*1000);
218 //printk("-->%s i2c io error %d line=%d\n",__FUNCTION__, data,__LINE__);
221 tc->point_data[0].x = buf[DATA_OFF(MALATA_POS_X0_HI+2)] << 8;
222 tc->point_data[0].x |= buf[DATA_OFF(MALATA_POS_X0_LO+2)];
223 tc->point_data[0].y = buf[DATA_OFF(MALATA_POS_Y0_HI+2) ]<< 8;
224 tc->point_data[0].y |= buf[DATA_OFF(MALATA_POS_Y0_LO+2)];
225 tc->point_data[1].x = buf[DATA_OFF(MALATA_POS_X1_HI+2) ]<< 8;
226 tc->point_data[1].x |= buf[DATA_OFF(MALATA_POS_X1_LO+2)];
227 tc->point_data[1].y = buf[DATA_OFF(MALATA_POS_Y1_HI+2) ]<< 8;
228 tc->point_data[1].y |= buf[DATA_OFF(MALATA_POS_Y1_LO+2)];
229 //printk("sintek tc->point_data[0].x= %d tc->point_data[0].y=%d\n ",tc->point_data[0].x,tc->point_data[0].y);
230 //printk("sintek tc->point_data[1].x= %d tc->point_data[1].y=%d\n ",tc->point_data[1].x,tc->point_data[1].y);
231 // tc->point_data[0].status = 0;
232 // tc->point_data[1].status = 0;
233 // if (tc->point_data[0].x ||tc->point_data[0].y)
234 // tc->point_data[0].status = 1;
235 // if (tc->point_data[1].x ||tc->point_data[1].y)
236 // tc->point_data[1].status = 1;
241 static void p1003_work(struct work_struct *work)
243 struct ts_p1003 *ts =
244 container_of(to_delayed_work(work), struct ts_p1003, work);
245 struct multitouch_event *tc = &ts->mt_event;
250 DBG("Enter:%s %d\n",__FUNCTION__,__LINE__);
251 if(ts->delayed_work_tp == 1){
252 data = i2c_master_reg8_send(ts->client, MALATA_SPECOP, buf, 1, 100*1000);
254 printk( "i2c io error %d line=%d\n", data,__LINE__);
256 ts->delayed_work_tp = 0;
260 if( p1003_read_values(ts,tc)<0)
262 printk("-->%s p1003_read_values error line=%d\n",__FUNCTION__,__LINE__);
266 #if defined (Singltouch_Mode)
267 p1003_report_single_event(ts,tc);
269 p1003_report_event(ts,tc);
274 schedule_delayed_work(&ts->work, msecs_to_jiffies(21));
283 static irqreturn_t p1003_irq(int irq, void *handle)
285 struct ts_p1003 *ts = handle;
287 DBG("Enter:%s %d\n",__FUNCTION__,__LINE__);
289 spin_lock_irqsave(&ts->lock,flags);
290 if (!ts->get_pendown_state || likely(ts->get_pendown_state())) {
291 disable_irq_nosync(ts->irq);
292 schedule_delayed_work(&ts->work,msecs_to_jiffies(2));
294 spin_unlock_irqrestore(&ts->lock,flags);
298 static void p1003_free_irq(struct ts_p1003 *ts)
300 free_irq(ts->irq, ts);
301 if (cancel_delayed_work_sync(&ts->work)) {
303 * Work was pending, therefore we need to enable
304 * IRQ here to balance the disable_irq() done in the
311 static void Sintek_work_delay(struct work_struct *work)
313 struct ts_p1003 *ts = container_of(to_delayed_work(work), struct ts_p1003, work);
317 data = i2c_master_reg8_send(ts->client, MALATA_SPECOP, buf, 1, 100*1000);
319 printk( "i2c io error %d line=%d\n", data,__LINE__);
322 struct ts_p1003 *ts_pub;
323 static ssize_t pc1003_touchdebug_show(struct device *dev,struct device_attribute *attr,char *_buf)
325 // struct ts_p103 *ts = dev_get_drvdata(dev);
329 printk("Enter:%s %d\n",__FUNCTION__,__LINE__);
330 printk("Touchscreen correct!!\n");
331 data = i2c_master_reg8_send(ts_pub->client, MALATA_SPECOP, buf, 1, 100*1000);
333 printk( "i2c io error %d line=%d\n", data,__LINE__);
335 return sprintf(_buf, "successful\n");
338 static DEVICE_ATTR(touchdebug, 0666, pc1003_touchdebug_show, NULL);
340 static int __devinit sintek_touch_probe(struct i2c_client *client,
341 const struct i2c_device_id *id)
344 struct p1003_platform_data *pdata = pdata = client->dev.platform_data;
345 struct input_dev *input_dev;
347 printk(">--------%s\n",__FUNCTION__);
349 dev_err(&client->dev, "platform data is required!\n");
353 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
356 ts = kzalloc(sizeof(struct ts_p1003), GFP_KERNEL);
357 input_dev = input_allocate_device();
358 if (!ts || !input_dev) {
364 ts->irq = client->irq;
365 ts->input = input_dev;
366 ts->status =0 ;// fjp add by 2010-9-30
367 ts->pendown = 0; // fjp add by 2010-10-06
368 ts->delayed_work_tp = 0; ///1;
369 //ts->wq = create_rt_workqueue("p1003_wq");
370 INIT_DELAYED_WORK(&ts->work, p1003_work);
372 ts->model = pdata->model;
374 snprintf(ts->phys, sizeof(ts->phys),
375 "%s/input0", dev_name(&client->dev));
377 input_dev->name = "p1003 Touchscreen";
378 input_dev->phys = ts->phys;
379 input_dev->id.bustype = BUS_I2C;
381 #if defined (Singltouch_Mode)
382 set_bit(EV_SYN, input_dev->evbit);
383 set_bit(EV_KEY, input_dev->evbit);
384 set_bit(BTN_TOUCH, input_dev->keybit);
385 set_bit(BTN_2, input_dev->keybit);
386 set_bit(EV_ABS, input_dev->evbit);
387 input_set_abs_params(input_dev,ABS_X,0,CONFIG_HANNSTAR_MAX_X,0,0);
388 input_set_abs_params(input_dev,ABS_Y,0,CONFIG_HANNSTAR_MAX_Y,0,0);
390 ts->has_relative_report = 0;
391 input_dev->evbit[0] = BIT_MASK(EV_ABS)|BIT_MASK(EV_KEY)|BIT_MASK(EV_SYN);
392 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
393 input_dev->keybit[BIT_WORD(BTN_2)] = BIT_MASK(BTN_2); //jaocbchen for dual
394 input_set_abs_params(input_dev, ABS_X, 7, 1020/*CONFIG_HANNSTAR_MAX_X*/, 0, 0);
395 input_set_abs_params(input_dev, ABS_Y, 10, 586/* CONFIG_HANNSTAR_MAX_Y*/, 0, 0);
396 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
397 input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
398 input_set_abs_params(input_dev, ABS_HAT0X, 7, 1020/*CONFIG_HANNSTAR_MAX_X*/, 0, 0);
399 input_set_abs_params(input_dev, ABS_HAT0Y, 10,586/* CONFIG_HANNSTAR_MAX_Y*/, 0, 0);
400 input_set_abs_params(input_dev, ABS_MT_POSITION_X,7, 1020/*CONFIG_HANNSTAR_MAX_X*/, 0, 0);
401 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 10, 586/* CONFIG_HANNSTAR_MAX_Y*/, 0, 0);
402 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
403 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
404 input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);
407 if (pdata->init_platform_hw)
408 pdata->init_platform_hw();
412 err = set_irq_type(ts->irq,IRQ_TYPE_LEVEL_LOW);
414 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
420 err = input_register_device(input_dev);
424 i2c_set_clientdata(client, ts);
426 p1003_check_firmwork(ts);
427 //schedule_delayed_work(&ts->work, msecs_to_jiffies(8 * 1000));
429 dev_dbg(&ts->client->dev, "no IRQ?\n");
432 ts->irq = gpio_to_irq(ts->irq);
435 err = request_irq(ts->irq, p1003_irq, IRQF_TRIGGER_LOW,
436 client->dev.driver->name, ts);
439 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
447 err = device_create_file(&ts->client->dev,&dev_attr_touchdebug);
449 printk("%s->%d cannot create status attribute\n",__FUNCTION__,__LINE__);
454 if (pdata->exit_platform_hw)
455 pdata->exit_platform_hw();
457 input_free_device(input_dev);
462 static int __devexit p1003_remove(struct i2c_client *client)
464 struct ts_p1003 *ts = i2c_get_clientdata(client);
465 struct p1003_platform_data *pdata = client->dev.platform_data;
469 if (pdata->exit_platform_hw)
470 pdata->exit_platform_hw();
472 input_unregister_device(ts->input);
478 static struct i2c_device_id sintek_idtable[] = {
479 { "sintek_touch", 0 },
483 MODULE_DEVICE_TABLE(i2c, sintek_idtable);
485 static struct i2c_driver sintek_touch_driver = {
487 .owner = THIS_MODULE,
488 .name = "sintek_touch"
490 .id_table = sintek_idtable,
491 .probe = sintek_touch_probe,
492 .remove = __devexit_p(p1003_remove),
495 static void __init sintek_touch_init_async(void *unused, async_cookie_t cookie)
497 printk("--------> %s <-------------\n",__func__);
498 i2c_add_driver(&sintek_touch_driver);
501 static int __init sintek_touch_init(void)
503 async_schedule(sintek_touch_init_async, NULL);
507 static void __exit sintek_touch_exit(void)
509 return i2c_del_driver(&sintek_touch_driver);
511 module_init(sintek_touch_init);
512 module_exit(sintek_touch_exit);
513 MODULE_LICENSE("GPL");