1 #include <linux/module.h>
2 #include <linux/delay.h>
3 #include <linux/earlysuspend.h>
4 #include <linux/hrtimer.h>
6 #include <linux/input.h>
7 #include <linux/interrupt.h>
9 #include <linux/gpio.h>
10 #include <linux/types.h>
11 #include <mach/iomux.h>
12 #include <linux/platform_device.h>
14 #include "ili2102_ts.h"
18 #define DBG(msg...) printk(msg);
23 #define TOUCH_NUMBER 2
25 static int touch_state[TOUCH_NUMBER] = {TOUCH_UP,TOUCH_UP};
26 static unsigned int g_x[TOUCH_NUMBER] = {0},g_y[TOUCH_NUMBER] = {0};
28 struct ili2102_ts_data {
30 bool swap_xy; /* swap x and y axes */
38 int gpio_reset_active_low;
39 int pendown_iomux_mode;
40 int resetpin_iomux_mode;
41 char pendown_iomux_name[IOMUX_NAME_SIZE];
42 char resetpin_iomux_name[IOMUX_NAME_SIZE];
45 struct i2c_client *client;
46 struct input_dev *input_dev;
48 struct delayed_work work;
49 struct workqueue_struct *ts_wq;
50 struct early_suspend early_suspend;
53 #ifdef CONFIG_HAS_EARLYSUSPEND
54 static void ili2102_ts_early_suspend(struct early_suspend *h);
55 static void ili2102_ts_late_resume(struct early_suspend *h);
58 static int verify_coord(struct ili2102_ts_data *ts,unsigned int *x,unsigned int *y)
61 DBG("%s:(%d/%d)\n",__FUNCTION__,*x, *y);
62 if((*x< ts->x_min) || (*x > ts->x_max))
65 if((*y< ts->y_min) || (*y > ts->y_max))
70 static int ili2102_init_panel(struct ili2102_ts_data *ts)
72 gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
73 gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
75 gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
80 static void ili2102_ts_work_func(struct work_struct *work)
85 struct i2c_msg msg[2];
87 uint8_t buf[9];//uint32_t buf[4];
88 struct ili2102_ts_data *ts = container_of(work, struct ili2102_ts_data, work);
90 DBG("ili2102_ts_work_func\n");
92 /*Touch Information Report*/
95 msg[0].addr = ts->client->addr;
98 msg[0].buf = &start_reg;
99 msg[0].scl_rate = 200*1000;
101 msg[1].addr = ts->client->addr;
102 msg[1].flags = I2C_M_RD;
104 msg[1].buf = buf;//msg[1].buf = (u8*)&buf[0];
105 msg[1].scl_rate = 200*1000;
107 ret = i2c_transfer(ts->client->adapter, msg, 2);
110 //for(i=0; i<msg[1].len; i++)
112 printk("%s:i2c_transfer fail, ret=%d\n",__FUNCTION__,ret);
116 for(i=0; i<TOUCH_NUMBER; i++)
119 if(!((buf[0]>>i)&0x01))
121 if (touch_state[i] == TOUCH_DOWN)
123 DBG("ili2102_ts_work_func:buf[%d]=%d\n",i,buf[i]);
124 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0); //Finger Size
125 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0); //Touch Size
126 input_mt_sync(ts->input_dev);
128 touch_state[i] = TOUCH_UP;
129 printk("touch_up \n");
137 x = buf[1+(i<<2)] | (buf[2+(i<<2)] << 8);
138 y = buf[3+(i<<2)] | (buf[4+(i<<2)] << 8);
143 if (verify_coord(ts,&x,&y))//goto out;
151 input_event(ts->input_dev, EV_ABS, ABS_MT_TRACKING_ID, i);
152 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1); //Finger Size
153 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
154 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
155 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 5); //Touch Size
156 input_mt_sync(ts->input_dev);
158 touch_state[i] = TOUCH_DOWN;
160 printk("touch_down X = %d, Y = %d\n", x, y);
167 input_sync(ts->input_dev);
171 schedule_delayed_work(&ts->work, msecs_to_jiffies(20));
177 enable_irq(ts->client->irq);
180 DBG("pin=%d,level=%d,irq=%d\n",irq_to_gpio(ts->client->irq),gpio_get_value(irq_to_gpio(ts->client->irq)),ts->client->irq);
184 static irqreturn_t ili2102_ts_irq_handler(int irq, void *dev_id)
186 struct ili2102_ts_data *ts = dev_id;
187 DBG("ili2102_ts_irq_handler=%d,%d\n",ts->client->irq,ts->use_irq);
189 disable_irq_nosync(ts->client->irq); //disable_irq(ts->client->irq);
190 schedule_delayed_work(&ts->work, 0);
194 static int __devinit setup_resetPin(struct i2c_client *client, struct ili2102_ts_data *ts)
196 struct ili2102_platform_data *pdata = client->dev.platform_data;
199 ts->gpio_reset = pdata->gpio_reset;
200 strcpy(ts->resetpin_iomux_name,pdata->resetpin_iomux_name);
201 ts->resetpin_iomux_mode = pdata->resetpin_iomux_mode;
202 ts->gpio_reset_active_low = pdata->gpio_reset_active_low;
204 DBG("%s=%d,%s,%d,%d\n",__FUNCTION__,ts->gpio_reset,ts->resetpin_iomux_name,ts->resetpin_iomux_mode,ts->gpio_reset_active_low);
206 if (!gpio_is_valid(ts->gpio_reset)) {
207 dev_err(&client->dev, "no gpio_reset?\n");
211 rk29_mux_api_set(ts->resetpin_iomux_name,ts->resetpin_iomux_mode);
212 err = gpio_request(ts->gpio_reset, "ili2102_resetPin");
214 dev_err(&client->dev, "failed to request resetPin GPIO%d\n",
218 gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
221 err = gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
223 dev_err(&client->dev, "failed to pulldown resetPin GPIO%d\n",
225 gpio_free(ts->gpio_reset);
231 gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
238 static int __devinit setup_pendown(struct i2c_client *client, struct ili2102_ts_data *ts)
241 struct ili2102_platform_data *pdata = client->dev.platform_data;
244 dev_dbg(&client->dev, "no IRQ?\n");
248 if (!gpio_is_valid(pdata->gpio_pendown)) {
249 dev_err(&client->dev, "no gpio_pendown?\n");
253 ts->gpio_pendown = pdata->gpio_pendown;
254 strcpy(ts->pendown_iomux_name,pdata->pendown_iomux_name);
255 ts->pendown_iomux_mode = pdata->pendown_iomux_mode;
257 DBG("%s=%d,%s,%d\n",__FUNCTION__,ts->gpio_pendown,ts->pendown_iomux_name,ts->pendown_iomux_mode);
259 if (!gpio_is_valid(ts->gpio_pendown)) {
260 dev_err(&client->dev, "no gpio_pendown?\n");
264 rk29_mux_api_set(ts->pendown_iomux_name,ts->pendown_iomux_mode);
265 err = gpio_request(ts->gpio_pendown, "ili2102_pendown");
267 dev_err(&client->dev, "failed to request pendown GPIO%d\n",
272 err = gpio_pull_updown(ts->gpio_pendown, GPIOPullUp);
274 dev_err(&client->dev, "failed to pullup pendown GPIO%d\n",
276 gpio_free(ts->gpio_pendown);
282 static int ili2102_chip_Init(struct i2c_client *client)
287 struct i2c_msg msg[2];
289 /* get panel information:6bytes */
291 msg[0].addr =client->addr;
294 msg[0].buf = &start_reg;
295 msg[0].scl_rate = 200*1000;
297 msg[1].addr = client->addr;
298 msg[1].flags = I2C_M_RD;
300 msg[1].buf = (u8*)&buf[0];
301 msg[1].scl_rate = 200*1000;
303 ret = i2c_transfer(client->adapter, msg, 2);
305 printk("%s:err\n",__FUNCTION__);
308 printk("%s:b[0]=0x%x,b[1]=0x%x,b[2]=0x%x,b[3]=0x%x,b[4]=0x%x,b[5]=0x%x\n",
309 __FUNCTION__,buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
311 /*get firmware version:3bytes */
313 msg[0].addr =client->addr;
316 msg[0].buf = &start_reg;
317 msg[0].scl_rate = 200*1000;
319 msg[1].addr = client->addr;
320 msg[1].flags = I2C_M_RD;
322 msg[1].buf = (u8*)&buf[0];
323 msg[1].scl_rate =200*1000;
325 ret = i2c_transfer(client->adapter, msg, 2);
327 printk("%s:err\n",__FUNCTION__);
330 printk("%s:Ver %d.%d.%d\n",__FUNCTION__,buf[0],buf[1],buf[2]);
336 static int ili2102_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
338 struct ili2102_ts_data *ts;
339 struct ili2102_platform_data *pdata = client->dev.platform_data;
342 printk("ili2102 TS probe\n");
344 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
345 printk(KERN_ERR "ili2102_ts_probe: need I2C_FUNC_I2C\n");
347 goto err_check_functionality_failed;
350 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
353 goto err_alloc_data_failed;
356 ts->ts_wq = create_singlethread_workqueue("ts_wq");
359 printk("%s:fail to create ts_wq,ret=0x%x\n",__FUNCTION__, ENOMEM);
362 //INIT_WORK(&ts->work, ili2102_ts_work_func);
363 INIT_DELAYED_WORK(&ts->work, ili2102_ts_work_func);
365 i2c_set_clientdata(client, ts);
367 ret = setup_resetPin(client,ts);
370 printk("ili2102 TS setup_resetPin fail\n");
371 goto err_alloc_data_failed;
374 ret=ili2102_chip_Init(ts->client);
377 printk("%s:chips init failed\n",__FUNCTION__);
378 goto err_resetpin_failed;
381 /* allocate input device */
382 ts->input_dev = input_allocate_device();
383 if (ts->input_dev == NULL) {
385 printk(KERN_ERR "ili2102_ts_probe: Failed to allocate input device\n");
386 goto err_input_dev_alloc_failed;
389 ts->model = pdata->model ? : 801;
390 ts->swap_xy = pdata->swap_xy;
391 ts->x_min = pdata->x_min;
392 ts->x_max = pdata->x_max;
393 ts->y_min = pdata->y_min;
394 ts->y_max = pdata->y_max;
395 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
396 snprintf(ts->name, sizeof(ts->name), "ili%d-touchscreen", ts->model);
397 ts->input_dev->phys = ts->phys;
398 ts->input_dev->name = ts->name;
399 ts->input_dev->dev.parent = &client->dev;
402 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_ABS);
403 //ts->input_dev->absbit[0] =
404 //BIT(ABS_MT_POSITION_X) | BIT(ABS_MT_POSITION_Y) |
405 //BIT(ABS_MT_TOUCH_MAJOR) | BIT(ABS_MT_WIDTH_MAJOR); // for android
406 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
410 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
414 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 1, 0, 0); //Finger Size
415 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 10, 0, 0); //Touch Size
417 /* ts->input_dev->name = ts->keypad_info->name; */
418 ret = input_register_device(ts->input_dev);
420 printk(KERN_ERR "ili2102_ts_probe: Unable to register %s input device\n", ts->input_dev->name);
421 goto err_input_register_device_failed;
424 client->irq = gpio_to_irq(client->irq);
427 ret = setup_pendown(client,ts);
430 printk("ili2102 TS setup_pendown fail\n");
431 goto err_input_register_device_failed;
433 ret = request_irq(client->irq, ili2102_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW, client->name, ts);
435 DBG("ili2102 TS register ISR (irq=%d)\n", client->irq);
439 dev_err(&client->dev, "request_irq failed\n");
442 #ifdef CONFIG_HAS_EARLYSUSPEND
443 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
444 ts->early_suspend.suspend = ili2102_ts_early_suspend;
445 ts->early_suspend.resume = ili2102_ts_late_resume;
446 register_early_suspend(&ts->early_suspend);
449 printk(KERN_INFO "ili2102_ts_probe: Start touchscreen %s in %s mode\n", ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
453 err_input_register_device_failed:
454 input_free_device(ts->input_dev);
456 gpio_free(ts->gpio_reset);
457 err_input_dev_alloc_failed:
459 err_alloc_data_failed:
460 err_check_functionality_failed:
464 static int ili2102_ts_remove(struct i2c_client *client)
466 struct ili2102_ts_data *ts = i2c_get_clientdata(client);
467 unregister_early_suspend(&ts->early_suspend);
469 free_irq(client->irq, ts);
471 hrtimer_cancel(&ts->timer);
472 input_unregister_device(ts->input_dev);
474 cancel_delayed_work_sync(&ts->work);
479 static int ili2102_ts_suspend(struct i2c_client *client, pm_message_t mesg)
482 struct ili2102_ts_data *ts = i2c_get_clientdata(client);
483 uint8_t buf[2] = {0x30,0x30};
484 struct i2c_msg msg[1];
487 disable_irq(client->irq);
489 hrtimer_cancel(&ts->timer);
491 ret = cancel_delayed_work_sync(&ts->work);
492 if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */
493 enable_irq(client->irq);
496 msg[0].addr =client->addr;
501 ret = i2c_transfer(client->adapter, msg, 1);
503 printk("%s:err\n",__FUNCTION__);
506 DBG("%s\n",__FUNCTION__);
511 static int ili2102_ts_resume(struct i2c_client *client)
513 struct ili2102_ts_data *ts = i2c_get_clientdata(client);
516 ili2102_init_panel(ts);
519 printk("enabling IRQ %d\n", client->irq);
520 enable_irq(client->irq);
523 //hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
525 DBG("%s\n",__FUNCTION__);
530 #ifdef CONFIG_HAS_EARLYSUSPEND
531 static void ili2102_ts_early_suspend(struct early_suspend *h)
533 struct ili2102_ts_data *ts;
534 ts = container_of(h, struct ili2102_ts_data, early_suspend);
535 ili2102_ts_suspend(ts->client, PMSG_SUSPEND);
538 static void ili2102_ts_late_resume(struct early_suspend *h)
540 struct ili2102_ts_data *ts;
541 ts = container_of(h, struct ili2102_ts_data, early_suspend);
542 ili2102_ts_resume(ts->client);
546 #define ILI2102_TS_NAME "ili2102_ts"
548 static const struct i2c_device_id ili2102_ts_id[] = {
549 { ILI2102_TS_NAME, 0 },
553 static struct i2c_driver ili2102_ts_driver = {
554 .probe = ili2102_ts_probe,
555 .remove = ili2102_ts_remove,
556 #ifndef CONFIG_HAS_EARLYSUSPEND
557 .suspend = ili2102_ts_early_suspend,
558 .resume = ili2102_ts_late_resume,
560 .id_table = ili2102_ts_id,
562 .name = ILI2102_TS_NAME,
566 static int __devinit ili2102_ts_init(void)
568 return i2c_add_driver(&ili2102_ts_driver);
571 static void __exit ili2102_ts_exit(void)
573 i2c_del_driver(&ili2102_ts_driver);
576 module_init(ili2102_ts_init);
577 module_exit(ili2102_ts_exit);
579 MODULE_DESCRIPTION("ili2102 Touchscreen Driver");
580 MODULE_LICENSE("GPL");