Input: sprd_eic_keys: remove event log
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / input / keyboard / sprd_eic_keys.c
1 /*
2  * Copyright (C) 2014 Spreadtrum Communications Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/module.h>
15
16 #include <linux/init.h>
17 #include <linux/fs.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/sched.h>
21 #include <linux/pm.h>
22 #include <linux/slab.h>
23 #include <linux/sysctl.h>
24 #include <linux/proc_fs.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/input.h>
28 #include <linux/sprd_eic_keys.h>
29 #include <linux/workqueue.h>
30 #include <linux/gpio.h>
31 #include <linux/of_platform.h>
32 #include <linux/of_gpio.h>
33 #include <linux/spinlock.h>
34 #ifdef HOOK_POWER_KEY
35 #include <linux/input-hook.h>
36 #endif
37 #if CONFIG_SEC_DEBUG
38 #include <soc/sprd/sec_debug.h>
39 #endif
40
41 //====================  debug  ====================
42 #define ENTER \
43 do{ if(debug_level >= 1) printk(KERN_INFO "[SPRD_EIC_KEYS_DBG] func: %s  line: %04d\n", __func__, __LINE__); }while(0)
44
45 #define PRINT_DBG(format,x...)  \
46 do{ if(debug_level >= 1) printk(KERN_INFO "[SPRD_EIC_KEYS_DBG] " format, ## x); }while(0)
47
48 #define PRINT_INFO(format,x...)  \
49 do{ printk(KERN_INFO "%s: " format, __func__, ## x); }while(0)
50
51 #define PRINT_WARN(format,x...)  \
52 do{ printk(KERN_INFO "[SPRD_EIC_KEYS_WARN] " format, ## x); }while(0)
53
54 #define PRINT_ERR(format,x...)  \
55 do{ printk(KERN_ERR "%s: " format, __func__, ## x); }while(0)
56 //====================  debug  ====================
57
58 static int debug_level = 0;
59 static struct sprd_eic_keys_platform_data *this_pdata = NULL;
60 uint8_t vol_down_keys_pressed;
61 static int sprd_eic_button_state(struct sprd_eic_keys_button *button)
62 {
63         int button_state = 0;
64         int gpio_value = 0;
65
66         gpio_value = !!gpio_get_value(button->gpio);
67
68         if(1 == button->active_low) {
69                 if(0 == gpio_value)
70                         button_state = 1;
71                 else
72                         button_state = 0;
73         } else {
74                 if(1 == gpio_value)
75                         button_state = 1;
76                 else
77                         button_state = 0;
78         }
79
80         PRINT_DBG("GPIO_%d=%d, button_state=%d\n", button->gpio, gpio_value, button_state);
81
82         return button_state; //0==released, 1==pressed
83 }
84
85 static irqreturn_t sprd_eic_irq_handle(int irq, void *dev_id)
86 {
87         struct sprd_eic_keys_button *button = (struct sprd_eic_keys_button *)dev_id;
88         static int button_state_last = 0;
89         int button_state_current = 0;
90
91         ENTER;
92         disable_irq_nosync(button->irq);
93         button_state_current = sprd_eic_button_state(button);
94
95         if (1 == button_state_current) {//pressed
96 #ifdef HOOK_POWER_KEY
97         #ifdef ( CONFIG_ARCH_SCX20 || CONFIG_ARCH_SCX35LT8 )
98                 if(116 == button->code)
99         #endif
100                 input_report_key_hook(this_pdata->input_dev, button->code, button_state_current);
101 #endif
102                 input_report_key(this_pdata->input_dev, button->code, button_state_current);
103                 input_sync(this_pdata->input_dev);
104
105                 if(1 == button->active_low)
106                         irq_set_irq_type(button->irq, IRQF_TRIGGER_HIGH);
107                 else
108                         irq_set_irq_type(button->irq, IRQF_TRIGGER_LOW);
109
110         } else {//released
111 #ifdef HOOK_POWER_KEY
112         #ifdef ( CONFIG_ARCH_SCX20 || CONFIG_ARCH_SCX35LT8 )
113                 if(116 == button->code)
114         #endif
115                 input_report_key_hook(this_pdata->input_dev, button->code, button_state_current);
116 #endif
117                 input_report_key(this_pdata->input_dev, button->code, button_state_current);
118                 input_sync(this_pdata->input_dev);
119
120                 if(1 == button->active_low)
121                         irq_set_irq_type(button->irq, IRQF_TRIGGER_LOW);
122                 else
123                         irq_set_irq_type(button->irq, IRQF_TRIGGER_HIGH);
124
125         }
126         vol_down_keys_pressed = button_state_current;
127 #if CONFIG_SEC_DEBUG
128         if (KEY_VOLUMEDOWN == button->code)
129                 sec_debug_check_crash_key(button->code, button_state_current);
130 #endif
131         button_state_last = button_state_current;
132         enable_irq(button->irq);
133         return IRQ_HANDLED;
134 }
135
136 static int sprd_eic_keys_setup_key(struct sprd_eic_keys_button *button)
137 {
138         const char *desc = button->desc ? button->desc : "sprd_eic_keys";
139         unsigned long irqflags = 0;
140         int error = -1;
141
142         ENTER;
143         if (gpio_is_valid(button->gpio)) {
144
145                 error = gpio_request_one(button->gpio, GPIOF_IN, desc);
146                 if (error < 0) {
147                         PRINT_ERR("Failed to request GPIO %d, error %d\n", button->gpio, error);
148                         return error;
149                 }
150
151                 if (button->debounce_interval) {
152                         error = gpio_set_debounce(button->gpio, button->debounce_interval * 1000);
153                         if (error < 0)
154                                 PRINT_INFO("Failed to set debounce for GPIO %d\n", button->gpio);
155                 }
156
157                 button->irq = gpio_to_irq(button->gpio);
158                 if (button->irq < 0) {
159                         error = button->irq;
160                         PRINT_ERR("Unable to get irq number for GPIO %d, error %d\n", button->gpio, error);
161                         goto fail;
162                 }
163         } else {
164                 PRINT_ERR("No GPIO specified\n");
165                 return -EINVAL;
166         }
167
168         if (button->type && button->type != EV_KEY) {
169                 PRINT_ERR("Only EV_KEY allowed for IRQ buttons\n");
170                 return -EINVAL;
171         }
172
173         input_set_capability(this_pdata->input_dev, button->type ?: EV_KEY, button->code);
174
175         irqflags = (1 == button->active_low) ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
176         irqflags |= IRQF_NO_SUSPEND;
177
178         error = request_any_context_irq(button->irq, sprd_eic_irq_handle, irqflags, desc, button);
179         if (error < 0) {
180                 PRINT_ERR("Unable to claim irq %d; error %d\n", button->irq, error);
181                 goto fail;
182         }
183
184         return 0;
185
186 fail:
187         if (gpio_is_valid(button->gpio))
188                 gpio_free(button->gpio);
189
190         return error;
191 }
192
193 #ifdef CONFIG_OF
194 static struct sprd_eic_keys_platform_data *sprd_eic_keys_get_devtree_pdata(struct device *dev)
195 {
196         struct device_node *node, *pp;
197         struct sprd_eic_keys_platform_data *pdata;
198         struct sprd_eic_keys_button *button;
199         int error;
200         int nbuttons;
201         int i;
202         char *input_name = NULL;
203         int ret = -1;
204
205         node = dev->of_node;
206         if (!node) {
207                 error = -ENODEV;
208                 goto err_out;
209         }
210
211         nbuttons = of_get_child_count(node);
212         if (nbuttons == 0) {
213                 error = -ENODEV;
214                 goto err_out;
215         }
216
217         pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
218                         GFP_KERNEL);
219         if (!pdata) {
220                 error = -ENOMEM;
221                 goto err_out;
222         }
223
224         pdata->buttons = (struct sprd_eic_keys_button *)(pdata + 1);
225         pdata->nbuttons = nbuttons;
226
227         pdata->rep = !!of_get_property(node, "autorepeat", NULL);
228
229         ret = of_property_read_string(node, "input-name", &input_name);
230         if (0 == ret) {
231                 pdata->name = input_name;
232                 PRINT_INFO("input-name=\"%s\"\n", input_name);
233         }
234         else
235                 PRINT_WARN("failed to get input-name\n");
236
237         i = 0;
238         for_each_child_of_node(node, pp) {
239                 int gpio;
240                 enum of_gpio_flags flags;
241
242                 if (!of_find_property(pp, "gpios", NULL)) {
243                         pdata->nbuttons--;
244                         dev_warn(dev, "Found button without gpios\n");
245                         continue;
246                 }
247
248                 gpio = of_get_gpio_flags(pp, 0, &flags);
249                 if (gpio < 0) {
250                         error = gpio;
251                         if (error != -EPROBE_DEFER)
252                                 dev_err(dev,
253                                         "Failed to get gpio flags, error: %d\n",
254                                         error);
255                         goto err_free_pdata;
256                 }
257
258                 button = &pdata->buttons[i++];
259
260                 button->gpio = gpio;
261                 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
262
263                 if (of_property_read_u32(pp, "linux,code", &button->code)) {
264                         dev_err(dev, "Button without keycode: 0x%x\n",
265                                 button->gpio);
266                         error = -EINVAL;
267                         goto err_free_pdata;
268                 }
269
270                 button->desc = of_get_property(pp, "label", NULL);
271
272                 if (of_property_read_u32(pp, "linux,input-type", &button->type))
273                         button->type = EV_KEY;
274
275                 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
276
277                 if (of_property_read_u32(pp, "debounce-interval",
278                                          &button->debounce_interval))
279                         button->debounce_interval = 5;
280         }
281
282         if (pdata->nbuttons == 0) {
283                 error = -EINVAL;
284                 goto err_free_pdata;
285         }
286
287         return pdata;
288
289 err_free_pdata:
290         kfree(pdata);
291 err_out:
292         return ERR_PTR(error);
293 }
294
295 static struct of_device_id sprd_eic_keys_of_match[] = {
296         { .compatible = "sprd,sprd-eic-keys", },
297         { },
298 };
299 MODULE_DEVICE_TABLE(of, sprd_eic_keys_of_match);
300
301 #else
302
303 static inline struct sprd_eic_keys_platform_data *sprd_eic_keys_get_devtree_pdata(struct device *dev)
304 {
305         return ERR_PTR(-ENODEV);
306 }
307
308 #endif
309 static ssize_t sprd_eic_button_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff)
310 {
311         int status = 0;
312
313         status = sprd_eic_button_state(&this_pdata->buttons[0]);
314         PRINT_INFO("button status = %d (0 == released, 1 == pressed)\n", status);
315         return sprintf(buff, "%d\n", status);
316 }
317
318 static struct kobject *sprd_eic_button_kobj = NULL;
319 static struct kobj_attribute sprd_eic_button_attr =
320         __ATTR(status, 0644, sprd_eic_button_show, NULL);
321
322 static int sprd_eic_button_sysfs_init(void)
323 {
324         int ret = -1;
325
326         sprd_eic_button_kobj = kobject_create_and_add("sprd_eic_button", kernel_kobj);
327         if (sprd_eic_button_kobj == NULL) {
328                 ret = -ENOMEM;
329                 PRINT_ERR("register sysfs failed. ret = %d\n", ret);
330                 return ret;
331         }
332
333         ret = sysfs_create_file(sprd_eic_button_kobj, &sprd_eic_button_attr.attr);
334         if (ret) {
335                 PRINT_ERR("create sysfs failed. ret = %d\n", ret);
336                 return ret;
337         }
338
339         PRINT_INFO("sprd_eic_button_sysfs_init success\n");
340         return ret;
341 }
342 static int sprd_eic_keys_probe(struct platform_device *pdev)
343 {
344         struct device *dev = &pdev->dev;
345         struct sprd_eic_keys_button *button = NULL;
346         struct input_dev *input = NULL;
347         int i = 0;
348         int error = -1;
349
350         ENTER;
351         this_pdata = dev_get_platdata(dev);
352
353         if (!this_pdata) {
354                 this_pdata = sprd_eic_keys_get_devtree_pdata(dev);
355                 if (IS_ERR(this_pdata)) {
356                         PRINT_ERR("failed to get platform data!\n");
357                         return PTR_ERR(this_pdata);
358                 }
359         }
360
361         input = input_allocate_device();
362         if (!input) {
363                 PRINT_ERR("failed to allocate input device!\n");
364                 error = -ENOMEM;
365                 goto fail1;
366         }
367
368         input->name = this_pdata->name ? this_pdata->name : pdev->name;
369         input->id.bustype = BUS_HOST;
370         this_pdata->input_dev = input;
371
372         if (this_pdata->rep)
373                 __set_bit(EV_REP, input->evbit);
374
375         for (i = 0; i < this_pdata->nbuttons; i++) {
376                 button = &this_pdata->buttons[i];
377                 error = sprd_eic_keys_setup_key( button);
378                 if (error)
379                         goto fail2;
380
381                 if(KEY_POWER == button->code)
382                         sprd_eic_button_sysfs_init();
383         }
384
385         error = input_register_device(input);
386         if (error) {
387                 PRINT_ERR("Unable to register input device!\n");
388                 goto fail3;
389         }
390         PRINT_INFO("probe success!\n");
391         return 0;
392
393 fail3:
394 fail2:
395         while (--i >= 0) {
396                 button = &this_pdata->buttons[i];
397                 free_irq(button->irq, button);
398                 if (gpio_is_valid(button->gpio)) {
399                         gpio_free(button->gpio);
400                 }
401         }
402
403         input_free_device(input);
404 fail1:
405         PRINT_ERR("probe failed!\n");
406         return error;
407 }
408
409 #ifdef CONFIG_PM
410 static int sprd_eic_keys_suspend(struct platform_device *dev, pm_message_t state)
411 {
412         return 0;
413 }
414
415 static int sprd_eic_keys_resume(struct platform_device *dev)
416 {
417         return 0;
418 }
419 #else
420 #define sprd_eic_keys_suspend NULL
421 #define sprd_eic_keys_resume NULL
422 #endif
423
424 static struct platform_driver sprd_eic_keys_device_driver = {
425         .probe = sprd_eic_keys_probe,
426         .suspend = NULL,
427         .resume = NULL,
428         .driver = {
429                 .name = "sprd-eic-keys",
430                 .owner = THIS_MODULE,
431                 .of_match_table = of_match_ptr(sprd_eic_keys_of_match),
432         }
433 };
434
435 static int __init sprd_eic_keys_init(void)
436 {
437         ENTER;
438 #ifdef HOOK_POWER_KEY
439         input_hook_init();
440 #endif
441
442 /***************************************************************/
443         struct device_node *node = of_find_compatible_node(NULL, NULL, "sprd,sprd-eic-keys");
444         if(node) {
445                 PRINT_INFO("Find the node name:%s\n",node->name);
446
447                 struct platform_device *dev = of_find_device_by_node(node);
448                 if(dev)
449                         PRINT_INFO("Find the device name:%s\n",dev->name);
450                 else
451                         PRINT_INFO("Not Find the device\n");
452         }
453         else
454                 PRINT_INFO("Not Find the node\n");
455 /***************************************************************/
456
457         return platform_driver_register(&sprd_eic_keys_device_driver);
458 }
459
460 static void __exit sprd_eic_keys_exit(void)
461 {
462 #ifdef HOOK_POWER_KEY
463         input_hook_exit();
464 #endif
465         platform_driver_unregister(&sprd_eic_keys_device_driver);
466 }
467
468 late_initcall(sprd_eic_keys_init);
469 module_exit(sprd_eic_keys_exit);
470
471 MODULE_LICENSE("GPL");
472 MODULE_AUTHOR("Yaochuan Li <yaochuan.li@spreadtrum.com>");
473 MODULE_DESCRIPTION("Keyboard driver for spreadtrum eic keys");
474 MODULE_ALIAS("platform:sprd-eic-keys");