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/platform_device.h>
10 #include <linux/async.h>
11 #include <mach/gpio.h>
12 #include <mach/iomux.h>
13 #include <linux/irq.h>
14 #include <mach/board.h>
15 #include <linux/kthread.h>
16 #include <linux/slab.h>
17 #include <linux/input/mt.h>
18 #include <linux/regulator/rk29-pwm-regulator.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/regulator/machine.h>
23 //#define FT5X0X_DEBUG
25 #define DBG(fmt, args...) printk("*** " fmt, ## args)
27 #define DBG(fmt, args...) do{}while(0)
30 #define EV_MENU KEY_MENU
32 #define I2C_SPEED 200*1000
35 #if defined (CONFIG_TOUCHSCREEN_1024X768)
36 #define SCREEN_MAX_X 1024
37 #define SCREEN_MAX_Y 768
38 #elif defined (CONFIG_TOUCHSCREEN_1024X600)
39 #define SCREEN_MAX_X 1024
40 #define SCREEN_MAX_Y 600
41 #elif defined (CONFIG_TOUCHSCREEN_800X600)
42 #define SCREEN_MAX_X 800
43 #define SCREEN_MAX_Y 600
44 #elif defined (CONFIG_TOUCHSCREEN_800X480)
45 #define SCREEN_MAX_X 800
46 #define SCREEN_MAX_Y 480
48 #define SCREEN_MAX_X 800
49 #define SCREEN_MAX_Y 480
54 #define VID_OF 0x51 //OuFei
55 #define VID_MD 0x53 //MuDong
57 #define VID_BM 0x5D //BaoMing
59 #define VID_DSW 0x8C //DingShengWei
60 #define VID_YM 0x94 //0xC0
61 static unsigned char g_vid;
63 #ifdef CONFIG_HAS_EARLYSUSPEND
64 static struct early_suspend ft5x0x_early_suspend;
67 #ifndef TOUCH_EN_LEVEL
68 #define TOUCH_EN_LEVEL GPIO_HIGH
71 static int ft5x0x_probe(struct i2c_client *client, const struct i2c_device_id *id);
80 static struct ts_event ts_point[MAX_POINT];
84 struct i2c_client *client;
85 struct input_dev *input_dev;
89 struct work_struct pen_event_work;
90 struct workqueue_struct *ts_workqueue;
93 struct i2c_client *g_client;
95 /***********************************************************************************************
96 Name : ft5x0x_i2c_rxdata
105 ***********************************************************************************************/
106 int ft5x0x_i2c_Read(char * writebuf, int writelen, char *readbuf, int readlen)
112 struct i2c_msg msgs[] = {
114 .addr = g_client->addr,
118 .scl_rate = I2C_SPEED,
121 .addr = g_client->addr,
125 .scl_rate = I2C_SPEED,
128 ret = i2c_transfer(g_client->adapter, msgs, 2);
130 DBG("msg %s i2c read error: %d\n", __func__, ret);
134 struct i2c_msg msgs[] = {
136 .addr = g_client->addr,
140 .scl_rate = I2C_SPEED,
143 ret = i2c_transfer(g_client->adapter, msgs, 1);
145 DBG("msg %s i2c read error: %d\n", __func__, ret);
148 }EXPORT_SYMBOL(ft5x0x_i2c_Read);
149 /***********************************************************************************************
150 Name : ft5x0x_i2c_Write
155 Output :0-write success
157 function : write data by i2c
159 ***********************************************************************************************/
160 int ft5x0x_i2c_Write(char *writebuf, int writelen)
164 struct i2c_msg msg[] = {
166 .addr = g_client->addr,
170 .scl_rate = I2C_SPEED,
174 ret = i2c_transfer(g_client->adapter, msg, 1);
176 DBG("%s i2c write error: %d\n", __func__, ret);
179 }EXPORT_SYMBOL(ft5x0x_i2c_Write);
181 int ft5x0x_rx_data(struct i2c_client *client, char *rxData, int length)
184 char reg = rxData[0];
185 ret = i2c_master_reg8_recv(client, reg, rxData, length, I2C_SPEED);
186 return (ret > 0)? 0 : ret;
189 static int ft5x0x_tx_data(struct i2c_client *client, char *txData, int length)
192 char reg = txData[0];
193 ret = i2c_master_reg8_send(client, reg, &txData[1], length-1, I2C_SPEED);
194 return (ret > 0)? 0 : ret;
197 char ft5x0x_read_reg(struct i2c_client *client, int addr)
203 ret = ft5x0x_rx_data(client, &tmp, 1);
210 int ft5x0x_write_reg(struct i2c_client *client,int addr,int value)
217 ret = ft5x0x_tx_data(client, &buffer[0], 2);
221 static void ft5x0x_power_en(struct ft5x0x_data *tsdata, int on)
223 #if defined (TOUCH_POWER_PIN)
225 gpio_direction_output(tsdata->touch_en_gpio, TOUCH_EN_LEVEL);
226 gpio_set_value(tsdata->touch_en_gpio, TOUCH_EN_LEVEL);
229 gpio_direction_output(tsdata->touch_en_gpio, !TOUCH_EN_LEVEL);
230 gpio_set_value(tsdata->touch_en_gpio, !TOUCH_EN_LEVEL);
234 if( tsdata->reset_gpio <= 0 ){
235 struct regulator *vcc_tp;
236 vcc_tp = regulator_get(NULL, "vaux33"); // vcc28_cif
237 if (vcc_tp == NULL || IS_ERR(vcc_tp) ){
238 printk(">>>> get cif vaux33 ldo failed!\n");
243 regulator_set_voltage(vcc_tp, 3300000, 3300000);
244 regulator_enable(vcc_tp);
245 printk(" %s set vpll vcc28_cif=%dmV end\n", __func__, regulator_get_voltage(vcc_tp));
246 regulator_put(vcc_tp);
248 while(regulator_is_enabled(vcc_tp)>0){
249 regulator_disable(vcc_tp);
251 printk(" %s regulator disable vcc tp \n",__func__);
252 regulator_put(vcc_tp);
259 static void ft5x0x_chip_reset(struct ft5x0x_data *tsdata)
261 gpio_direction_output(tsdata->reset_gpio, 0);
262 gpio_set_value(tsdata->reset_gpio, 1);
264 gpio_set_value(tsdata->reset_gpio, 0);
266 gpio_set_value(tsdata->reset_gpio, 1);
269 static int i2c_write_interface(unsigned char* pbt_buf, int dw_lenth)
272 ret = i2c_master_send(g_client, pbt_buf, dw_lenth);
274 printk("i2c_write_interface error\n");
281 static int ft_cmd_write(unsigned char btcmd, unsigned char btPara1, unsigned char btPara2,
282 unsigned char btPara3, int num)
284 unsigned char write_cmd[4] = {0};
286 write_cmd[0] = btcmd;
287 write_cmd[1] = btPara1;
288 write_cmd[2] = btPara2;
289 write_cmd[3] = btPara3;
290 return i2c_write_interface(&write_cmd, num);
294 static int ft5x0x_chip_init(struct i2c_client * client)
302 struct ft5x0x_data *tsdata = i2c_get_clientdata(client);
304 if( tsdata->reset_gpio > 0 ){
305 gpio_free(tsdata->reset_gpio);
306 err = gpio_request(tsdata->reset_gpio, "ft5x0x rst");
308 printk( "failed to request ft5x0x reset GPIO%d\n", tsdata->reset_gpio);
309 goto exit_alloc_gpio_rst_failed;
313 #if defined (TOUCH_POWER_PIN)
314 #if defined (TOUCH_POWER_MUX_NAME)
315 rk29_mux_api_set(TOUCH_POWER_MUX_NAME, TOUCH_POWER_MUX_MODE_GPIO);
317 gpio_free(tsdata->touch_en_gpio);
318 err = gpio_request(tsdata->touch_en_gpio, "ft5x0x power enable");
320 DBG( "failed to request ft5x0x power enable GPIO%d\n", tsdata->touch_en_gpio);
321 goto exit_alloc_gpio_power_failed;
325 ft5x0x_power_en(tsdata, 0);
327 ft5x0x_chip_reset(tsdata);
328 ft5x0x_power_en(tsdata, 1);
330 ft_cmd_write(0x07,0x00,0x00,0x00,1);
337 ret = ft5x0x_write_reg(client, reg, w_value); /* adjust frequency 70Hz */
339 printk(KERN_ERR "ft5x0x i2c txdata failed\n");
343 r_value = ft5x0x_read_reg(client, reg);
345 printk(KERN_ERR "ft5x0x i2c rxdata failed\n");
348 printk("r_value = %d\n, i = %d, flag = %d", r_value, i, flag);
351 if (w_value != r_value) {
354 if (i > 10) { /* test 5 count */
367 ret = ft5x0x_read_reg(client, 0xA8);//read touchpad ID for adjust touchkey place
369 printk(KERN_ERR "ft5x0x i2c rxdata failed\n");
372 printk("ft5406 g_vid = 0x%X\n", ret);
377 exit_alloc_gpio_power_failed:
378 #if defined (TOUCH_POWER_PIN)
379 gpio_free(tsdata->touch_en_gpio);
381 exit_alloc_gpio_rst_failed:
382 gpio_free(tsdata->reset_gpio);
383 printk("%s error\n",__FUNCTION__);
387 static void key_led_ctrl(int on)
390 gpio_set_value(TOUCH_KEY_LED, on);
394 static int g_screen_key=0;
396 static int ft5x0x_process_points(struct ft5x0x_data *data)
398 struct i2c_client *client = data->client;
402 int status = 0, id, x, y, p, w, touch_num;
404 int back_press = 0, search_press=0, menu_press=0, home_press=0;
410 //printk("ft5406 g_vid = 0x%X\n", g_vid);
411 if (MAX_POINT == 5) {
412 ret = ft5x0x_rx_data(client, buf, 31);
414 ret = ft5x0x_rx_data(client, buf, 13);
418 printk("%s read_data i2c_rxdata failed: %d\n", __func__, ret);
423 if (MAX_POINT == 5) {
424 touch_num = buf[2] & 0x07;
426 touch_num = buf[2] & 0x03;
429 if (touch_num == 0) {
430 for (i = 0; i < MAX_POINT; i++) {
431 input_mt_slot(data->input_dev, i);
432 input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, false);
435 input_sync(data->input_dev);
436 DBG("release all points!!!!!!\n");
441 if (data->last_point_num > touch_num) {
442 touch_num = data->last_point_num;
444 data->last_point_num = points;
447 for (i = 0; i < touch_num; i++) {
448 id = buf[5 + offset] >> 4;
449 status = buf[3 + offset] >> 6;
450 x = (s16)(buf[3 + offset] & 0x0F) << 8 | (s16)buf[4 + offset];
451 y = (s16)(buf[5 + offset] & 0x0F) << 8 | (s16)buf[6 + offset];
453 //p = buf[7 + offset];
454 //w = buf[8 + offset];
458 //printk("%d-%d(%d,%d)%d-%d\n", id, status, x, y, p, w);
459 DBG("TOUCH_NO=%d: ID=%d,(X=%d,Y=%d), status=%d, pressure=%d, w=%d\n", i, id, x, y, status, 0, 0);
461 if (x < (SCREEN_MAX_X + 10)) {
463 input_mt_slot(data->input_dev, id);
464 input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, false);
466 input_mt_slot(data->input_dev, id);
467 input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, true);
468 input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 200);
469 input_report_abs(data->input_dev, ABS_MT_POSITION_X, x);
470 input_report_abs(data->input_dev, ABS_MT_POSITION_Y, y);
476 input_sync(data->input_dev);
481 static void ft5x0x_delaywork_func(struct work_struct *work)
483 struct ft5x0x_data *ft5x0x = container_of(work, struct ft5x0x_data, pen_event_work);
484 struct i2c_client *client = ft5x0x->client;
486 ft5x0x_process_points(ft5x0x);
487 enable_irq(client->irq);
490 static irqreturn_t ft5x0x_interrupt(int irq, void *handle)
492 struct ft5x0x_data *ft5x0x_ts = handle;
494 //printk("Enter:%s %d\n",__FUNCTION__,__LINE__);
495 disable_irq_nosync(irq);
496 //if (!work_pending(&ft5x0x_ts->pen_event_work)) {
497 queue_work(ft5x0x_ts->ts_workqueue, &ft5x0x_ts->pen_event_work);
503 static int ft5x0x_remove(struct i2c_client *client)
505 struct ft5x0x_data *ft5x0x = i2c_get_clientdata(client);
507 input_unregister_device(ft5x0x->input_dev);
508 input_free_device(ft5x0x->input_dev);
509 free_irq(client->irq, ft5x0x);
511 #ifdef CONFIG_HAS_EARLYSUSPEND
512 unregister_early_suspend(&ft5x0x_early_suspend);
517 #ifdef CONFIG_HAS_EARLYSUSPEND
518 static void ft5x0x_suspend(struct early_suspend *h)
523 struct ft5x0x_data *ft5x0x = i2c_get_clientdata(g_client);
525 printk("==ft5x0x_ts_suspend=\n");
531 err = ft5x0x_write_reg(g_client, reg, w_value); /* enter sleep mode */
533 printk("ft5x0x enter sleep mode failed\n");
536 disable_irq(g_client->irq);
537 if( ft5x0x->reset_gpio <= 0 ){
538 ft5x0x_power_en(ft5x0x, 0);
542 static void ft5x0x_resume(struct early_suspend *h)
544 struct ft5x0x_data *ft5x0x = i2c_get_clientdata(g_client);
548 printk("==ft5x0x_ts_resume=\n");
549 if( ft5x0x->reset_gpio > 0 ){
550 ft5x0x_chip_reset(ft5x0x);
552 ft5x0x_power_en(ft5x0x, 1);
557 enable_irq(g_client->irq);
560 static int ft5x0x_suspend(struct i2c_client *client, pm_message_t mesg)
564 static int ft5x0x_resume(struct i2c_client *client)
570 static const struct i2c_device_id ft5x0x_id[] = {
575 MODULE_DEVICE_TABLE(i2c, ft5x0x_id);
577 static struct i2c_driver ft5x0x_driver = {
581 .id_table = ft5x0x_id,
582 .probe = ft5x0x_probe,
583 .remove = __devexit_p(ft5x0x_remove),
584 #ifndef CONFIG_HAS_EARLYSUSPEND
585 .suspend = &ft5x0x_suspend,
586 .resume = &ft5x0x_resume,
590 static int ft5x0x_client_init(struct i2c_client *client)
592 struct ft5x0x_data *tsdata = i2c_get_clientdata(client);
595 DBG("gpio_to_irq(%d) is %d\n", client->irq, gpio_to_irq(client->irq));
596 if ( !gpio_is_valid(client->irq)) {
597 DBG("+++++++++++gpio_is_invalid\n");
601 gpio_free(client->irq);
602 ret = gpio_request(client->irq, "ft5x0x_int");
604 DBG( "failed to request ft5x0x GPIO%d\n", gpio_to_irq(client->irq));
608 ret = gpio_direction_input(client->irq);
610 DBG("failed to set ft5x0x gpio input\n");
614 gpio_pull_updown(client->irq, GPIOPullUp);
615 client->irq = gpio_to_irq(client->irq);
616 //ft5x0x->irq = client->irq;
617 ret = request_irq(client->irq, ft5x0x_interrupt, IRQF_TRIGGER_FALLING, client->dev.driver->name, tsdata);
618 DBG("request irq is %d,ret is 0x%x\n", client->irq, ret);
620 DBG(KERN_ERR "ft5x0x_client_init: request irq failed,ret is %d\n", ret);
623 //disable_irq(client->irq);
628 static int ft5x0x_probe(struct i2c_client *client, const struct i2c_device_id *id)
630 struct ft5x0x_data *ft5x0x_ts;
631 struct ts_hw_data *pdata = client->dev.platform_data;
635 printk("%s enter\n",__FUNCTION__);
636 ft5x0x_ts = kzalloc(sizeof(struct ft5x0x_data), GFP_KERNEL);
638 DBG("[ft5x0x]:alloc data failed.\n");
640 goto exit_alloc_data_failed;
643 memset(ts_point, 0x0, sizeof(struct ts_event) * MAX_POINT);
646 ft5x0x_ts->client = client;
647 ft5x0x_ts->last_point_num = 0;
648 ft5x0x_ts->reset_gpio = pdata->reset_gpio;
649 ft5x0x_ts->touch_en_gpio = pdata->touch_en_gpio;
650 i2c_set_clientdata(client, ft5x0x_ts);
652 err = ft5x0x_chip_init(client);
655 "ft5x0x_probe: ft5x0x chip init failed\n");
656 goto exit_request_gpio_irq_failed;
659 err = ft5x0x_client_init(client);
662 "ft5x0x_probe: ft5x0x_client_init failed\n");
663 goto exit_request_gpio_irq_failed;
666 ft5x0x_ts->input_dev = input_allocate_device();
667 if (!ft5x0x_ts->input_dev) {
670 "ft5x0x_probe: Failed to allocate input device\n");
671 goto exit_input_allocate_device_failed;
674 ft5x0x_ts->input_dev->name = "ft5x0x-ts";
675 ft5x0x_ts->input_dev->dev.parent = &client->dev;
677 err = input_register_device(ft5x0x_ts->input_dev);
680 "ft5x0x_probe: Unable to register input device: %s\n",
681 ft5x0x_ts->input_dev->name);
682 goto exit_input_register_device_failed;
685 INIT_WORK(&ft5x0x_ts->pen_event_work, ft5x0x_delaywork_func);
686 ft5x0x_ts->ts_workqueue = create_singlethread_workqueue("ft5x0x_ts");
687 if (!ft5x0x_ts->ts_workqueue) {
689 goto exit_request_gpio_irq_failed;
693 __set_bit(EV_SYN, ft5x0x_ts->input_dev->evbit);
694 __set_bit(EV_KEY, ft5x0x_ts->input_dev->evbit);
695 __set_bit(EV_ABS, ft5x0x_ts->input_dev->evbit);
696 __set_bit(INPUT_PROP_DIRECT, ft5x0x_ts->input_dev->propbit);
698 input_mt_init_slots(ft5x0x_ts->input_dev, MAX_POINT);
699 input_set_abs_params(ft5x0x_ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
700 input_set_abs_params(ft5x0x_ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
701 input_set_abs_params(ft5x0x_ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
703 #ifdef CONFIG_HAS_EARLYSUSPEND
704 ft5x0x_early_suspend.suspend = ft5x0x_suspend;
705 ft5x0x_early_suspend.resume = ft5x0x_resume;
706 ft5x0x_early_suspend.level = 0x2;
707 register_early_suspend(&ft5x0x_early_suspend);
712 exit_input_register_device_failed:
713 input_free_device(ft5x0x_ts->input_dev);
714 exit_input_allocate_device_failed:
715 free_irq(client->irq, ft5x0x_ts);
716 exit_request_gpio_irq_failed:
718 exit_alloc_gpio_power_failed:
719 #if defined (TOUCH_POWER_PIN)
720 gpio_free(ft5x0x_ts->touch_en_gpio);
722 exit_alloc_gpio_rst_failed:
723 gpio_free(ft5x0x_ts->reset_gpio);
724 exit_alloc_data_failed:
725 printk("%s error\n",__FUNCTION__);
729 static void __init ft5x0x_init_async(void *unused, async_cookie_t cookie)
731 i2c_add_driver(&ft5x0x_driver);
734 static int __init ft5x0x_mod_init(void)
736 printk("ft5x0x module init\n");
737 async_schedule(ft5x0x_init_async, NULL);
741 static void __exit ft5x0x_mod_exit(void)
743 i2c_del_driver(&ft5x0x_driver);
746 module_init(ft5x0x_mod_init);
747 module_exit(ft5x0x_mod_exit);
749 MODULE_DESCRIPTION("ft5406 touch driver");
750 MODULE_AUTHOR("zqqu<zqqu@yifangdigital.com>");
751 MODULE_LICENSE("GPL");