Merge tag 'v4.4'
[firefly-linux-kernel-4.4.55.git] / drivers / input / keyboard / synaptics_so340010.c
1 /****************************************************************************************
2  * driver/input/touchscreen/hannstar_Synaptics.c
3  *Copyright     :ROCKCHIP  Inc
4  *Author        :       sfm
5  *Date          :  2010.2.5
6  *This driver use for rk28 chip extern touchscreen. Use i2c IF ,the chip is Hannstar
7  *description£º
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>
16 #include <linux/io.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>
22
23
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
44
45 #define PACKGE_BUFLEN           10
46 #define KEY0                    0x1
47 #define KEY1            0x2
48 #define KEY2            0x4
49 #define KEY3            0x8
50 #define KEY_ALL                 (KEY0 | KEY1 | KEY2 | KEY3)
51 #define SYN_340010_KEY_MAXNUM           4
52
53 #if 0
54 #define DBG(x...) printk(KERN_INFO x)
55 #else
56 #define DBG(x...) do { } while (0)
57 #endif
58
59 struct so340010_kbd_info {      
60         unsigned int    key_mask;       
61         int                     key_code; 
62 };
63 static struct so340010_kbd_info key_table[SYN_340010_KEY_MAXNUM] = { 
64         { KEY3, KEY_BACK }, 
65         { KEY2, KEY_MENU }, 
66         { KEY1, KEY_HOME }, 
67         { KEY0, KEY_SEARCH },
68 };
69 static int key_num = sizeof(key_table)/sizeof(key_table[0]);
70
71 struct so340010_register {      
72         unsigned short address; 
73         const short  value;
74 };
75
76 static struct so340010_register so340010_register_init_table[] = {      
77 #if 0
78         { 0x0000,  0x0007  }, 
79         { 0x0001,  0x0020  }, 
80         { 0x0004,  0x000F  }, 
81         { 0x0010,  0xA0A0  }, 
82         { 0x0011,  0xA0A0  },
83 #else
84         { 0x0000,  0x0700  }, 
85         { 0x0100,  0x2000  }, 
86         { 0x0400,  0x0F00  }, 
87         { 0x1000,  0xA0A0  }, 
88         { 0x1100,  0xA0A0  },
89 #endif
90 #if (defined(CONFIG_KEYBOARD_SO340010_LED) || defined(CONFIG_KEYBOARD_SO340010_LED_FRAMEWORK))  
91         { 0x0022,  0x000f  },   
92         { 0x0023,  0x0000  }, 
93         { 0x0024,  0x0f0f  },   /* Brightness value 0 ~ 31*/    
94         { 0x0025,  0x1616  }, 
95 #endif
96 };
97
98 /*
99  * Common code for bq3060 devices read
100  */
101 static int Synaptics_touchkey_read(struct i2c_client *client, unsigned short  reg, unsigned short  buf[], unsigned len)
102 {
103         int ret;        
104         struct i2c_adapter *adap=client->adapter;
105         struct i2c_msg msg;
106         char tx_buf[2];
107         
108         tx_buf[0] = reg & 0xff;
109         tx_buf[1] = (reg>>8) & 0xff;
110         msg.addr = client->addr;
111         msg.flags = client->flags;
112         msg.len = 2;
113         msg.buf = (char *)tx_buf;
114         msg.scl_rate = SO340010_IIC_SPEED;
115
116         ret = i2c_transfer(adap, &msg, 1);
117         if(ret < 0)
118                 printk("[%s]: i2c read error\n",__FUNCTION__);
119         
120         msg.addr = client->addr;
121         msg.flags = client->flags | I2C_M_RD;
122         msg.len = 2*len ;
123         msg.buf = (char *)buf;
124         msg.scl_rate = SO340010_IIC_SPEED;
125
126         ret = i2c_transfer(adap, &msg, 1);
127         
128         //return 0;
129         //ret = i2c_master_reg16_recv(client, reg, buf, len, 100*1000);
130         if(ret < 0)
131                 printk("[%s]: i2c read error\n",__FUNCTION__);
132         return ret; 
133 }
134
135 static int Synaptics_touchkey_write(struct i2c_client *client, unsigned short  reg, const short   buf[], unsigned len)
136 {
137
138         int ret; 
139         //return 0;
140         ret = i2c_master_reg16_send(client, reg, buf, (int)len, 100*1000);
141         if(ret < 0)
142                 printk("[%s]: i2c write error\n",__FUNCTION__);
143         return ret;
144 }
145
146
147
148 struct point_data {     
149         short status;   
150         short x;        
151         short y;
152     short z;
153 };
154
155
156
157 struct tk_Synaptics {
158         struct input_dev        *input;
159         char                    phys[32];
160         struct delayed_work     work;
161         struct workqueue_struct *wq;
162
163         struct i2c_client       *client;
164         int    g_code;
165         int             gpio;
166         u16                     model;
167         spinlock_t      lock;
168         bool            pendown;
169         bool            status;
170         int                     irq;
171         int         init_flag;
172         int             has_relative_report;
173         int                     (*get_pendown_state)(void);
174         void            (*clear_penirq)(void);
175 };
176
177 int Synaptics_get_pendown_state(void)
178 {
179         return 0;
180 }
181
182 #if 0 
183 static void Synaptics_report_event(struct tk_Synaptics *tk,struct multitouch_event *tc)
184 {
185         struct input_dev *input = tk->input;
186     int cid;
187
188     cid = tc->contactid;
189     if (tk->status) {
190         input_report_abs(input, ABS_X, tc->point_data[cid].x);
191         input_report_abs(input, ABS_Y, tc->point_data[cid].y);
192         input_sync(input);
193     }
194     if(tk->pendown != tk->status){
195         tk->pendown = tk->status;
196         input_report_key(input, BTN_TOUCH, tk->status);
197         input_sync(input);
198        
199     }
200 }
201 #endif
202
203 static  void Synaptics_check_firmwork(struct tk_Synaptics *tk)
204 {
205         int data=0;
206     short buf[6];
207         int i;
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__);  
211                 }       
212                 DBG("[%s]: config touch key i=%d\n",__FUNCTION__,i);    
213         }       
214         data = Synaptics_touchkey_read(tk->client,0x0901/*0x0109*/,buf,1);
215         if (data<0) {
216                 printk( "error reading current\n");
217                 return ;
218         }
219         DBG("Synaptics_read_values = %x\n",buf[0]);
220         
221         #if 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__);    
225                 }       
226                 printk("[-->%s]: buf[0]=%x\n",__FUNCTION__,buf[0]);
227         }
228         
229         if ( Synaptics_touchkey_read(tk->client, SO340010_REG_BUTTON_STATE, buf,1) < 0)         
230                 printk("[%s]: config touch key error\n",__FUNCTION__);  
231         
232         printk("[-->%s]: buf[0]=%x buf[1]=%x\n",__FUNCTION__,buf[0],buf[1]);
233         #endif
234 }
235
236
237
238 static inline int Synaptics_read_values(struct tk_Synaptics *tk)  ///, struct multitouch_event *tc)
239 {
240         int data=0;
241         short buf;
242         int i;
243         data = Synaptics_touchkey_read(tk->client,0X0B01/*0x010b*/,&buf,1);
244         if (data<0) {
245                 printk( "error reading current\n");
246                 return 0;
247         }
248         DBG("Synaptics_read_values = %x\n",buf);
249         
250         data = Synaptics_touchkey_read(tk->client,0X0901/*0x0108*/,&buf,1);
251         if (data<0) {
252                 printk( "error reading current\n");
253                 return 0;
254         }
255         DBG("Synaptics_read_values = %x\n",buf);
256         buf = buf>>8;
257         if(buf == 0)
258                 goto exit_ret;
259         for(i=0;i<SYN_340010_KEY_MAXNUM;i++){
260                 if(buf == key_table[i].key_mask)
261                         break;
262         }
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);  
268 exit_ret:       
269         if(!gpio_get_value(tk->gpio)){          
270         Synaptics_check_firmwork(tk);
271     }
272     return 10;
273         
274 }
275
276
277 static void Synaptics_work(struct work_struct *work)
278 {
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){
284                 tk->init_flag = 0;
285                 Synaptics_check_firmwork(tk);
286                 return; 
287         }
288                 
289         if( Synaptics_read_values(tk)<0)  //,tc)<0)
290         {
291                 printk("-->%s Synaptics_read_values error  line=%d\n",__FUNCTION__,__LINE__);
292                 goto out ;
293         }
294         //Synaptics_report_event(tk,tc);
295 out:               
296         //if (tk->pendown){
297         //      schedule_delayed_work(&tk->work, msecs_to_jiffies(8));
298         //      tk->pendown = 0;
299         //}
300         //else{
301                 enable_irq(tk->irq);
302         //}
303
304 }
305
306
307 static irqreturn_t Synaptics_irq(int irq, void *handle)
308 {
309         struct tk_Synaptics *tk = handle;
310         unsigned long flags;
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));
316         }
317         spin_unlock_irqrestore(&tk->lock,flags);
318         return IRQ_HANDLED;
319 }
320
321 static void Synaptics_free_irq(struct tk_Synaptics *tk)
322 {
323         free_irq(tk->irq, tk);
324         if (cancel_delayed_work_sync(&tk->work)) {
325                 /*
326                  * Work was pending, therefore we need to enable
327                  * IRQ here to balance the disable_irq() done in the
328                  * interrupt handler.
329                  */
330                 enable_irq(tk->irq);
331         }
332 }
333
334 static int __devinit synaptics_touchkey_probe(struct i2c_client *client,
335                                    const struct i2c_device_id *id)
336 {
337         struct tk_Synaptics *tk;
338         struct synaptics_platform_data *pdata = pdata = client->dev.platform_data;
339         struct input_dev *input_dev;
340         int err,i;
341         //short reg,buff;       
342         if (!pdata) {
343                 dev_err(&client->dev, "platform data is required!\n");
344                 return -EINVAL;
345         }
346         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
347                 return -EIO;
348         tk = kzalloc(sizeof(struct tk_Synaptics), GFP_KERNEL);
349         input_dev = input_allocate_device();
350         if (!tk || !input_dev) {
351                 err = -ENOMEM;
352                 goto err_free_mem;
353         }
354         tk->client = client;
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);       
368         }
369         if (pdata->init_platform_hw)
370                 pdata->init_platform_hw();
371         err = input_register_device(input_dev);
372         if (err)
373                 goto err_free_irq;
374         i2c_set_clientdata(client, tk);
375         
376         //reg = 0x3000;
377         //buff = 0x0001;
378         //Synaptics_touchkey_write(client,reg,&buff, 2);
379         //mdelay(500);
380         Synaptics_check_firmwork(tk);
381         ///schedule_delayed_work(&tk->work,msecs_to_jiffies(8*1000));
382         tk->gpio = tk->irq;
383         if (!tk->irq) {
384                 dev_dbg(&tk->client->dev, "no IRQ?\n");
385                 return -ENODEV;
386         }else{
387                 tk->irq = gpio_to_irq(tk->irq);
388         }
389         err = request_irq(tk->irq, Synaptics_irq, IRQF_TRIGGER_FALLING,client->dev.driver->name, tk);
390         if (err < 0) {
391                 dev_err(&client->dev, "irq %d busy?\n", tk->irq);
392                 goto err_free_mem;
393         }
394         if (err < 0)
395                 goto err_free_irq;
396         return 0;
397  err_free_irq:
398         Synaptics_free_irq(tk);
399         if (pdata->exit_platform_hw)
400                 pdata->exit_platform_hw();
401  err_free_mem:
402         input_free_device(input_dev);
403         kfree(tk);
404         return err;
405 }
406
407 static int __devexit Synaptics_remove(struct i2c_client *client)
408 {
409         struct tk_Synaptics *tk = i2c_get_clientdata(client);
410         struct synaptics_platform_data *pdata = client->dev.platform_data;
411
412         Synaptics_free_irq(tk);
413
414         if (pdata->exit_platform_hw)
415                 pdata->exit_platform_hw();
416
417         input_unregister_device(tk->input);
418         kfree(tk);
419
420         return 0;
421 }
422
423 static struct i2c_device_id synaptics_touchkey_idtable[] = {
424         { "synaptics_touchkey", 0 },
425         { }
426 };
427
428 MODULE_DEVICE_TABLE(i2c, sintek_idtable);
429
430 static struct i2c_driver synaptics_touchkey_driver = {
431         .driver = {
432                 .owner  = THIS_MODULE,
433                 .name   = "synaptics_touchkey"
434         },
435         .id_table       = synaptics_touchkey_idtable,
436         .probe          = synaptics_touchkey_probe,
437         .remove         = __devexit_p(Synaptics_remove),
438 };
439
440 static void __init synaptics_touchkey_init_async(void *unused, async_cookie_t cookie)
441 {
442         DBG("--------> %s <-------------\n",__func__);
443         i2c_add_driver(&synaptics_touchkey_driver);
444 }
445
446 static int __init synaptics_touchkey_init(void)
447 {
448         async_schedule(synaptics_touchkey_init_async, NULL);
449         return 0;
450 }
451
452 static void __exit synaptics_touchkey_exit(void)
453 {
454         return i2c_del_driver(&synaptics_touchkey_driver);
455 }
456 module_init(synaptics_touchkey_init);
457 module_exit(synaptics_touchkey_exit);
458 MODULE_LICENSE("GPL");
459