1 /****************************************************************************************
2 * driver/input/touchscreen/hannstar_Synaptics.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>
24 #define SO340010_REG_GENERAL_CONFIG 0x0001
25 #define SO340010_REG_GPIO_STATE 0x0108
26 #define SO340010_REG_BUTTON_STATE 0x0109
27 #define SO340010_REG_GPIO_CTRL 0x000E
28 #define SO340010_REG_GPIO_CTRL_DIR 0x0F00
29 #define SO340010_REG_GPIO_CTRL_DATA 0x000F
30 #define SO340010_REG_LED_ENABLE 0x0022
31 #define SO340010_REG_LED_EFFECT_PERIOD 0x0023
32 #define SO340010_REG_LED_CTRL1 0x0024
33 #define SO340010_REG_LED_CTRL1_LED0_EFFECT 0x001F
34 #define SO340010_REG_LED_CTRL1_LED0_BRIGHTNESS 0x0E00
35 #define SO340010_REG_LED_CTRL1_LED1_EFFECT 0x000E
36 #define SO340010_REG_LED_CTRL1_LED1_BRIGHTNESS 0x1F00
37 #define SO340010_REG_LED_CONTROL_2 0x0025
38 #define SO340010_REG_LED_CTRL2_LED2_EFFECT 0x001F
39 #define SO340010_REG_LED_CTRL2_LED2_BRIGHTNESS 0x0E00
40 #define SO340010_REG_LED_CTRL2_LED3_EFFECT 0x000E
41 #define SO340010_REG_LED_CTRL2_LED3_BRIGHTNESS 0x1F00
42 #define SO340010_REG_NUM 74
43 #define SO340010_IIC_SPEED 100*1000
45 #define PACKGE_BUFLEN 10
50 #define KEY_ALL (KEY0 | KEY1 | KEY2 | KEY3)
51 #define SYN_340010_KEY_MAXNUM 4
54 #define DBG(x...) printk(KERN_INFO x)
56 #define DBG(x...) do { } while (0)
59 struct so340010_kbd_info {
60 unsigned int key_mask;
63 static struct so340010_kbd_info key_table[SYN_340010_KEY_MAXNUM] = {
69 static int key_num = sizeof(key_table)/sizeof(key_table[0]);
71 struct so340010_register {
72 unsigned short address;
76 static struct so340010_register so340010_register_init_table[] = {
90 #if (defined(CONFIG_KEYBOARD_SO340010_LED) || defined(CONFIG_KEYBOARD_SO340010_LED_FRAMEWORK))
93 { 0x0024, 0x0f0f }, /* Brightness value 0 ~ 31*/
99 * Common code for bq3060 devices read
101 static int Synaptics_touchkey_read(struct i2c_client *client, unsigned short reg, unsigned short buf[], unsigned len)
104 struct i2c_adapter *adap=client->adapter;
108 tx_buf[0] = reg & 0xff;
109 tx_buf[1] = (reg>>8) & 0xff;
110 msg.addr = client->addr;
111 msg.flags = client->flags;
113 msg.buf = (char *)tx_buf;
114 msg.scl_rate = SO340010_IIC_SPEED;
116 ret = i2c_transfer(adap, &msg, 1);
118 printk("[%s]: i2c read error\n",__FUNCTION__);
120 msg.addr = client->addr;
121 msg.flags = client->flags | I2C_M_RD;
123 msg.buf = (char *)buf;
124 msg.scl_rate = SO340010_IIC_SPEED;
126 ret = i2c_transfer(adap, &msg, 1);
129 //ret = i2c_master_reg16_recv(client, reg, buf, len, 100*1000);
131 printk("[%s]: i2c read error\n",__FUNCTION__);
135 static int Synaptics_touchkey_write(struct i2c_client *client, unsigned short reg, const short buf[], unsigned len)
140 ret = i2c_master_reg16_send(client, reg, buf, (int)len, 100*1000);
142 printk("[%s]: i2c write error\n",__FUNCTION__);
157 struct tk_Synaptics {
158 struct input_dev *input;
160 struct delayed_work work;
161 struct workqueue_struct *wq;
163 struct i2c_client *client;
172 int has_relative_report;
173 int (*get_pendown_state)(void);
174 void (*clear_penirq)(void);
177 int Synaptics_get_pendown_state(void)
183 static void Synaptics_report_event(struct tk_Synaptics *tk,struct multitouch_event *tc)
185 struct input_dev *input = tk->input;
190 input_report_abs(input, ABS_X, tc->point_data[cid].x);
191 input_report_abs(input, ABS_Y, tc->point_data[cid].y);
194 if(tk->pendown != tk->status){
195 tk->pendown = tk->status;
196 input_report_key(input, BTN_TOUCH, tk->status);
203 static void Synaptics_check_firmwork(struct tk_Synaptics *tk)
208 for (i = 0; i < sizeof(so340010_register_init_table)/sizeof(so340010_register_init_table[0]); i++) {
209 if (Synaptics_touchkey_write(tk->client, so340010_register_init_table[i].address, &so340010_register_init_table[i].value, 1) < 0) {
210 printk("[%s]: config touch key error\n",__FUNCTION__);
212 DBG("[%s]: config touch key i=%d\n",__FUNCTION__,i);
214 data = Synaptics_touchkey_read(tk->client,0x0901/*0x0109*/,buf,1);
216 printk( "error reading current\n");
219 DBG("Synaptics_read_values = %x\n",buf[0]);
222 for (i = 0; i < sizeof(so340010_register_init_table)/sizeof(so340010_register_init_table[0]); i++) {
223 if (Synaptics_touchkey_read(tk->client, so340010_register_init_table[i].address, buf, 1) < 0) {
224 printk("[%s]: read config touch key error\n",__FUNCTION__);
226 printk("[-->%s]: buf[0]=%x\n",__FUNCTION__,buf[0]);
229 if ( Synaptics_touchkey_read(tk->client, SO340010_REG_BUTTON_STATE, buf,1) < 0)
230 printk("[%s]: config touch key error\n",__FUNCTION__);
232 printk("[-->%s]: buf[0]=%x buf[1]=%x\n",__FUNCTION__,buf[0],buf[1]);
238 static inline int Synaptics_read_values(struct tk_Synaptics *tk) ///, struct multitouch_event *tc)
243 data = Synaptics_touchkey_read(tk->client,0X0B01/*0x010b*/,&buf,1);
245 printk( "error reading current\n");
248 DBG("Synaptics_read_values = %x\n",buf);
250 data = Synaptics_touchkey_read(tk->client,0X0901/*0x0108*/,&buf,1);
252 printk( "error reading current\n");
255 DBG("Synaptics_read_values = %x\n",buf);
259 for(i=0;i<SYN_340010_KEY_MAXNUM;i++){
260 if(buf == key_table[i].key_mask)
263 tk->g_code = key_table[i].key_code;
264 input_report_key(tk->input, tk->g_code, 1);
265 input_sync(tk->input);
266 input_report_key(tk->input, tk->g_code, 0);
267 input_sync(tk->input);
269 if(!gpio_get_value(tk->gpio)){
270 Synaptics_check_firmwork(tk);
277 static void Synaptics_work(struct work_struct *work)
279 struct tk_Synaptics *tk =
280 container_of(to_delayed_work(work), struct tk_Synaptics, work);
281 //struct multitouch_event *tc = &tk->mt_event;
282 DBG("Enter:%s %d\n",__FUNCTION__,__LINE__);
283 if(tk->init_flag == 1){
285 Synaptics_check_firmwork(tk);
289 if( Synaptics_read_values(tk)<0) //,tc)<0)
291 printk("-->%s Synaptics_read_values error line=%d\n",__FUNCTION__,__LINE__);
294 //Synaptics_report_event(tk,tc);
297 // schedule_delayed_work(&tk->work, msecs_to_jiffies(8));
307 static irqreturn_t Synaptics_irq(int irq, void *handle)
309 struct tk_Synaptics *tk = handle;
311 DBG("Enter:%s %d\n",__FUNCTION__,__LINE__);
312 spin_lock_irqsave(&tk->lock,flags);
313 if (!tk->get_pendown_state || likely(tk->get_pendown_state())) {
314 disable_irq_nosync(tk->irq);
315 schedule_delayed_work(&tk->work,msecs_to_jiffies(20));
317 spin_unlock_irqrestore(&tk->lock,flags);
321 static void Synaptics_free_irq(struct tk_Synaptics *tk)
323 free_irq(tk->irq, tk);
324 if (cancel_delayed_work_sync(&tk->work)) {
326 * Work was pending, therefore we need to enable
327 * IRQ here to balance the disable_irq() done in the
334 static int __devinit synaptics_touchkey_probe(struct i2c_client *client,
335 const struct i2c_device_id *id)
337 struct tk_Synaptics *tk;
338 struct synaptics_platform_data *pdata = pdata = client->dev.platform_data;
339 struct input_dev *input_dev;
343 dev_err(&client->dev, "platform data is required!\n");
346 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
348 tk = kzalloc(sizeof(struct tk_Synaptics), GFP_KERNEL);
349 input_dev = input_allocate_device();
350 if (!tk || !input_dev) {
355 tk->irq = client->irq;
356 tk->input = input_dev;
357 ///tk->init_flag = 1;
358 //tk->wq = create_rt_workqueue("Synaptics_wq");
359 INIT_DELAYED_WORK(&tk->work, Synaptics_work);
360 snprintf(tk->phys, sizeof(tk->phys), "%s/input0", dev_name(&client->dev));
361 input_dev->name = "synaptics_touchkey";
362 input_dev->phys = tk->phys;
363 input_dev->id.bustype = BUS_I2C;
364 input_dev->evbit[0] = BIT_MASK(EV_ABS)|BIT_MASK(EV_KEY)|BIT_MASK(EV_SYN);
365 set_bit(EV_KEY, input_dev->evbit);
366 for (i = 0; i < key_num; i++) {
367 set_bit(key_table[i].key_code,input_dev->keybit);
369 if (pdata->init_platform_hw)
370 pdata->init_platform_hw();
371 err = input_register_device(input_dev);
374 i2c_set_clientdata(client, tk);
378 //Synaptics_touchkey_write(client,reg,&buff, 2);
380 Synaptics_check_firmwork(tk);
381 ///schedule_delayed_work(&tk->work,msecs_to_jiffies(8*1000));
384 dev_dbg(&tk->client->dev, "no IRQ?\n");
387 tk->irq = gpio_to_irq(tk->irq);
389 err = request_irq(tk->irq, Synaptics_irq, IRQF_TRIGGER_FALLING,client->dev.driver->name, tk);
391 dev_err(&client->dev, "irq %d busy?\n", tk->irq);
398 Synaptics_free_irq(tk);
399 if (pdata->exit_platform_hw)
400 pdata->exit_platform_hw();
402 input_free_device(input_dev);
407 static int __devexit Synaptics_remove(struct i2c_client *client)
409 struct tk_Synaptics *tk = i2c_get_clientdata(client);
410 struct synaptics_platform_data *pdata = client->dev.platform_data;
412 Synaptics_free_irq(tk);
414 if (pdata->exit_platform_hw)
415 pdata->exit_platform_hw();
417 input_unregister_device(tk->input);
423 static struct i2c_device_id synaptics_touchkey_idtable[] = {
424 { "synaptics_touchkey", 0 },
428 MODULE_DEVICE_TABLE(i2c, sintek_idtable);
430 static struct i2c_driver synaptics_touchkey_driver = {
432 .owner = THIS_MODULE,
433 .name = "synaptics_touchkey"
435 .id_table = synaptics_touchkey_idtable,
436 .probe = synaptics_touchkey_probe,
437 .remove = __devexit_p(Synaptics_remove),
440 static void __init synaptics_touchkey_init_async(void *unused, async_cookie_t cookie)
442 DBG("--------> %s <-------------\n",__func__);
443 i2c_add_driver(&synaptics_touchkey_driver);
446 static int __init synaptics_touchkey_init(void)
448 async_schedule(synaptics_touchkey_init_async, NULL);
452 static void __exit synaptics_touchkey_exit(void)
454 return i2c_del_driver(&synaptics_touchkey_driver);
456 module_init(synaptics_touchkey_init);
457 module_exit(synaptics_touchkey_exit);
458 MODULE_LICENSE("GPL");