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>
23 #define MAX_SUPPORT_POINT 2// // 4
24 #define PACKGE_BUFLEN 10
26 //#define Singltouch_Mode
29 #define sakura_dbg_msg(fmt,...) do { \
30 printk("sakura dbg msg------>" \
31 " (func-->%s ; line-->%d) " fmt, __func__, __LINE__ , ##__VA_ARGS__); \
33 #define sakura_dbg_report_key_msg(fmt,...) do{ \
34 printk("sakura report " fmt,##__VA_ARGS__); \
37 #define sakura_dbg_msg(fmt,...) do {} while(0)
38 #define sakura_dbg_report_key_msg(fmt,...) do{}while(0)
47 struct multitouch_event{
48 struct point_data point_data[MAX_SUPPORT_POINT];
54 struct input_dev *input;
56 struct delayed_work work;
57 struct workqueue_struct *wq;
59 struct i2c_client *client;
60 struct multitouch_event mt_event;
66 int has_relative_report;
67 int (*get_pendown_state)(void);
68 void (*clear_penirq)(void);
71 int p1003_get_pendown_state(void)
76 static void p1003_report_event(struct ts_p1003 *ts,struct multitouch_event *tc)
78 struct input_dev *input = ts->input;
80 dev_dbg(&ts->client->dev, "UP\n");
82 for(i=0; i<MAX_SUPPORT_POINT;i++){
83 if(tc->point_data[i].status >= 0){
84 pandown |= tc->point_data[i].status;
85 input_report_abs(input, ABS_MT_TRACKING_ID, i);
86 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tc->point_data[i].status);
87 input_report_abs(input, ABS_MT_WIDTH_MAJOR, 0);
88 input_report_abs(input, ABS_MT_POSITION_X, tc->point_data[i].x);
89 input_report_abs(input, ABS_MT_POSITION_Y, tc->point_data[i].y);
92 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);
93 #if defined(CONFIG_HANNSTAR_DEBUG)
94 printk("hannstar p1003 Px = [%d],Py = [%d] \n",tc->point_data[i].x,tc->point_data[i].y);
97 if(tc->point_data[i].status == 0)
98 tc->point_data[i].status--;
103 ts->pendown = pandown;
107 #if defined (Singltouch_Mode)
108 static void p1003_report_single_event(struct ts_p1003 *ts,struct multitouch_event *tc)
110 struct input_dev *input = ts->input;
115 input_report_abs(input, ABS_X, tc->point_data[cid].x);
116 input_report_abs(input, ABS_Y, tc->point_data[cid].y);
119 if(ts->pendown != ts->status){
120 ts->pendown = ts->status;
121 input_report_key(input, BTN_TOUCH, ts->status);
123 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);
128 static inline int p1003_check_firmwork(struct ts_p1003 *ts)
132 char buf[10] = {0x03 , 0x03 , 0x0a , 0x01 , 'D' , 0x00 , 0x00 , 0x00 , 0x00 , 0x00};
136 data = i2c_master_normal_send(ts->client, buf,len, 200*1000);
139 dev_err(&ts->client->dev, "i2c io error %d \n", data);
143 data = i2c_master_normal_recv(ts->client, buf,len, 200*1000);
146 dev_err(&ts->client->dev, "i2c io error %d \n", data);
150 printk("p1003 reg[5] = %c ,reg[6] = %c, reg[7] = %c, reg[8] = %c\n" , buf[5],buf[6],buf[7],buf[8]);
151 printk("p1003 reg[5] = %x ,reg[6] = %x, reg[7] = %x, reg[8] = %x\n" , buf[5],buf[6],buf[7],buf[8]);
156 static inline int p1003_read_values(struct ts_p1003 *ts, struct multitouch_event *tc)
161 char tempbuf[(MAX_SUPPORT_POINT * 10) << 1];
164 for(j = 0; j < MAX_SUPPORT_POINT ; j++){
165 buf = &tempbuf[j*10];
166 data = i2c_master_normal_recv(ts->client, buf, len, 200*1000);
169 if(tempbuf[j*10] != 0x04){
180 contactid = (buf[1]&0x7C)>>2;
181 tc->contactid = contactid;
182 tc->point_data[contactid].status = buf[1]&0x01;
183 tc->point_data[contactid].x = ((buf[3]<<8) + buf[2])>>4;
184 tc->point_data[contactid].y = ((buf[5]<<8) + buf[4])>>4;
185 tc->validtouch = buf[1]&0x80;
186 ts->status = tc->point_data[contactid].status;
189 // printk("validtouch =%d,status= %d,contactid =%d\n",tc->validtouch,tc->point_data[contactid].status,contactid);
194 static void p1003_work(struct work_struct *work)
196 struct ts_p1003 *ts =
197 container_of(to_delayed_work(work), struct ts_p1003, work);
198 struct multitouch_event *tc = &ts->mt_event;
201 rt = p1003_read_values(ts,tc);
206 #if defined (Singltouch_Mode)
207 p1003_report_single_event(ts,tc);
209 p1003_report_event(ts,tc);
214 queue_delayed_work(ts->wq, &ts->work, msecs_to_jiffies(10));
222 static irqreturn_t p1003_irq(int irq, void *handle)
224 struct ts_p1003 *ts = handle;
226 if (!ts->get_pendown_state || likely(ts->get_pendown_state())) {
227 disable_irq_nosync(ts->irq);
228 queue_delayed_work(ts->wq, &ts->work, 0);
235 static void p1003_free_irq(struct ts_p1003 *ts)
237 free_irq(ts->irq, ts);
238 if (cancel_delayed_work_sync(&ts->work)) {
240 * Work was pending, therefore we need to enable
241 * IRQ here to balance the disable_irq() done in the
248 static int __devinit p1003_probe(struct i2c_client *client,
249 const struct i2c_device_id *id)
252 struct p1003_platform_data *pdata = pdata = client->dev.platform_data;
253 struct input_dev *input_dev;
257 dev_err(&client->dev, "platform data is required!\n");
261 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
264 ts = kzalloc(sizeof(struct ts_p1003), GFP_KERNEL);
265 input_dev = input_allocate_device();
266 if (!ts || !input_dev) {
272 ts->irq = client->irq;
273 ts->input = input_dev;
274 ts->status =0 ;// fjp add by 2010-9-30
275 ts->pendown = 0; // fjp add by 2010-10-06
277 ts->wq = create_rt_workqueue("p1003_wq");
278 INIT_DELAYED_WORK(&ts->work, p1003_work);
280 ts->model = pdata->model;
282 snprintf(ts->phys, sizeof(ts->phys),
283 "%s/input0", dev_name(&client->dev));
285 input_dev->name = "p1003 Touchscreen";
286 input_dev->phys = ts->phys;
287 input_dev->id.bustype = BUS_I2C;
289 #if defined (Singltouch_Mode)
290 set_bit(EV_SYN, input_dev->evbit);
291 set_bit(EV_KEY, input_dev->evbit);
292 set_bit(BTN_TOUCH, input_dev->keybit);
293 set_bit(BTN_2, input_dev->keybit);
294 set_bit(EV_ABS, input_dev->evbit);
295 input_set_abs_params(input_dev,ABS_X,0,CONFIG_HANNSTAR_MAX_X,0,0);
296 input_set_abs_params(input_dev,ABS_Y,0,CONFIG_HANNSTAR_MAX_Y,0,0);
298 ts->has_relative_report = 0;
299 input_dev->evbit[0] = BIT_MASK(EV_ABS)|BIT_MASK(EV_KEY)|BIT_MASK(EV_SYN);
300 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
301 input_dev->keybit[BIT_WORD(BTN_2)] = BIT_MASK(BTN_2); //jaocbchen for dual
302 input_set_abs_params(input_dev, ABS_X, 0, CONFIG_HANNSTAR_MAX_X, 0, 0);
303 input_set_abs_params(input_dev, ABS_Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
304 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
305 input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
306 input_set_abs_params(input_dev, ABS_HAT0X, 0, CONFIG_HANNSTAR_MAX_X, 0, 0);
307 input_set_abs_params(input_dev, ABS_HAT0Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
308 input_set_abs_params(input_dev, ABS_MT_POSITION_X,0, CONFIG_HANNSTAR_MAX_X, 0, 0);
309 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
310 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
311 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
312 input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);
315 if (pdata->init_platform_hw)
316 pdata->init_platform_hw();
319 dev_dbg(&ts->client->dev, "no IRQ?\n");
322 ts->irq = gpio_to_irq(ts->irq);
325 err = request_irq(ts->irq, p1003_irq, 0,
326 client->dev.driver->name, ts);
329 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
336 err = set_irq_type(ts->irq,IRQ_TYPE_LEVEL_LOW);
338 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
344 err = input_register_device(input_dev);
348 i2c_set_clientdata(client, ts);
350 p1003_check_firmwork(ts);
356 if (pdata->exit_platform_hw)
357 pdata->exit_platform_hw();
359 input_free_device(input_dev);
364 static int __devexit p1003_remove(struct i2c_client *client)
366 struct ts_p1003 *ts = i2c_get_clientdata(client);
367 struct p1003_platform_data *pdata = client->dev.platform_data;
371 if (pdata->exit_platform_hw)
372 pdata->exit_platform_hw();
374 input_unregister_device(ts->input);
380 static struct i2c_device_id p1003_idtable[] = {
381 { "p1003_touch", 0 },
385 MODULE_DEVICE_TABLE(i2c, p1003_idtable);
387 static struct i2c_driver p1003_driver = {
389 .owner = THIS_MODULE,
390 .name = "p1003_touch"
392 .id_table = p1003_idtable,
393 .probe = p1003_probe,
394 .remove = __devexit_p(p1003_remove),
397 static void __init p1003_init_async(void *unused, async_cookie_t cookie)
399 printk("--------> %s <-------------\n",__func__);
400 i2c_add_driver(&p1003_driver);
403 static int __init p1003_init(void)
405 async_schedule(p1003_init_async, NULL);
409 static void __exit p1003_exit(void)
411 return i2c_del_driver(&p1003_driver);
413 module_init(p1003_init);
414 module_exit(p1003_exit);
415 MODULE_LICENSE("GPL");