2 * Copyright (C) 2014 Spreadtrum Communications Inc.
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.
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.
14 #include <linux/module.h>
16 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/sched.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>
35 #include <linux/input-hook.h>
38 #include <soc/sprd/sec_debug.h>
41 //==================== debug ====================
43 do{ if(debug_level >= 1) printk(KERN_INFO "[SPRD_EIC_KEYS_DBG] func: %s line: %04d\n", __func__, __LINE__); }while(0)
45 #define PRINT_DBG(format,x...) \
46 do{ if(debug_level >= 1) printk(KERN_INFO "[SPRD_EIC_KEYS_DBG] " format, ## x); }while(0)
48 #define PRINT_INFO(format,x...) \
49 do{ printk(KERN_INFO "%s: " format, __func__, ## x); }while(0)
51 #define PRINT_WARN(format,x...) \
52 do{ printk(KERN_INFO "[SPRD_EIC_KEYS_WARN] " format, ## x); }while(0)
54 #define PRINT_ERR(format,x...) \
55 do{ printk(KERN_ERR "%s: " format, __func__, ## x); }while(0)
56 //==================== debug ====================
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)
66 gpio_value = !!gpio_get_value(button->gpio);
68 if(1 == button->active_low) {
80 PRINT_DBG("GPIO_%d=%d, button_state=%d\n", button->gpio, gpio_value, button_state);
82 return button_state; //0==released, 1==pressed
85 static irqreturn_t sprd_eic_irq_handle(int irq, void *dev_id)
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;
92 disable_irq_nosync(button->irq);
93 button_state_current = sprd_eic_button_state(button);
95 if (1 == button_state_current) {//pressed
97 #ifdef ( CONFIG_ARCH_SCX20 || CONFIG_ARCH_SCX35LT8 )
98 if(116 == button->code)
100 input_report_key_hook(this_pdata->input_dev, button->code, button_state_current);
102 input_report_key(this_pdata->input_dev, button->code, button_state_current);
103 input_sync(this_pdata->input_dev);
105 if(1 == button->active_low)
106 irq_set_irq_type(button->irq, IRQF_TRIGGER_HIGH);
108 irq_set_irq_type(button->irq, IRQF_TRIGGER_LOW);
111 #ifdef HOOK_POWER_KEY
112 #ifdef ( CONFIG_ARCH_SCX20 || CONFIG_ARCH_SCX35LT8 )
113 if(116 == button->code)
115 input_report_key_hook(this_pdata->input_dev, button->code, button_state_current);
117 input_report_key(this_pdata->input_dev, button->code, button_state_current);
118 input_sync(this_pdata->input_dev);
120 if(1 == button->active_low)
121 irq_set_irq_type(button->irq, IRQF_TRIGGER_LOW);
123 irq_set_irq_type(button->irq, IRQF_TRIGGER_HIGH);
126 vol_down_keys_pressed = button_state_current;
128 if (KEY_VOLUMEDOWN == button->code)
129 sec_debug_check_crash_key(button->code, button_state_current);
131 button_state_last = button_state_current;
132 enable_irq(button->irq);
136 static int sprd_eic_keys_setup_key(struct sprd_eic_keys_button *button)
138 const char *desc = button->desc ? button->desc : "sprd_eic_keys";
139 unsigned long irqflags = 0;
143 if (gpio_is_valid(button->gpio)) {
145 error = gpio_request_one(button->gpio, GPIOF_IN, desc);
147 PRINT_ERR("Failed to request GPIO %d, error %d\n", button->gpio, error);
151 if (button->debounce_interval) {
152 error = gpio_set_debounce(button->gpio, button->debounce_interval * 1000);
154 PRINT_INFO("Failed to set debounce for GPIO %d\n", button->gpio);
157 button->irq = gpio_to_irq(button->gpio);
158 if (button->irq < 0) {
160 PRINT_ERR("Unable to get irq number for GPIO %d, error %d\n", button->gpio, error);
164 PRINT_ERR("No GPIO specified\n");
168 if (button->type && button->type != EV_KEY) {
169 PRINT_ERR("Only EV_KEY allowed for IRQ buttons\n");
173 input_set_capability(this_pdata->input_dev, button->type ?: EV_KEY, button->code);
175 irqflags = (1 == button->active_low) ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
176 irqflags |= IRQF_NO_SUSPEND;
178 error = request_any_context_irq(button->irq, sprd_eic_irq_handle, irqflags, desc, button);
180 PRINT_ERR("Unable to claim irq %d; error %d\n", button->irq, error);
187 if (gpio_is_valid(button->gpio))
188 gpio_free(button->gpio);
194 static struct sprd_eic_keys_platform_data *sprd_eic_keys_get_devtree_pdata(struct device *dev)
196 struct device_node *node, *pp;
197 struct sprd_eic_keys_platform_data *pdata;
198 struct sprd_eic_keys_button *button;
202 char *input_name = NULL;
211 nbuttons = of_get_child_count(node);
217 pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
224 pdata->buttons = (struct sprd_eic_keys_button *)(pdata + 1);
225 pdata->nbuttons = nbuttons;
227 pdata->rep = !!of_get_property(node, "autorepeat", NULL);
229 ret = of_property_read_string(node, "input-name", &input_name);
231 pdata->name = input_name;
232 PRINT_INFO("input-name=\"%s\"\n", input_name);
235 PRINT_WARN("failed to get input-name\n");
238 for_each_child_of_node(node, pp) {
240 enum of_gpio_flags flags;
242 if (!of_find_property(pp, "gpios", NULL)) {
244 dev_warn(dev, "Found button without gpios\n");
248 gpio = of_get_gpio_flags(pp, 0, &flags);
251 if (error != -EPROBE_DEFER)
253 "Failed to get gpio flags, error: %d\n",
258 button = &pdata->buttons[i++];
261 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
263 if (of_property_read_u32(pp, "linux,code", &button->code)) {
264 dev_err(dev, "Button without keycode: 0x%x\n",
270 button->desc = of_get_property(pp, "label", NULL);
272 if (of_property_read_u32(pp, "linux,input-type", &button->type))
273 button->type = EV_KEY;
275 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
277 if (of_property_read_u32(pp, "debounce-interval",
278 &button->debounce_interval))
279 button->debounce_interval = 5;
282 if (pdata->nbuttons == 0) {
292 return ERR_PTR(error);
295 static struct of_device_id sprd_eic_keys_of_match[] = {
296 { .compatible = "sprd,sprd-eic-keys", },
299 MODULE_DEVICE_TABLE(of, sprd_eic_keys_of_match);
303 static inline struct sprd_eic_keys_platform_data *sprd_eic_keys_get_devtree_pdata(struct device *dev)
305 return ERR_PTR(-ENODEV);
309 static ssize_t sprd_eic_button_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff)
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);
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);
322 static int sprd_eic_button_sysfs_init(void)
326 sprd_eic_button_kobj = kobject_create_and_add("sprd_eic_button", kernel_kobj);
327 if (sprd_eic_button_kobj == NULL) {
329 PRINT_ERR("register sysfs failed. ret = %d\n", ret);
333 ret = sysfs_create_file(sprd_eic_button_kobj, &sprd_eic_button_attr.attr);
335 PRINT_ERR("create sysfs failed. ret = %d\n", ret);
339 PRINT_INFO("sprd_eic_button_sysfs_init success\n");
342 static int sprd_eic_keys_probe(struct platform_device *pdev)
344 struct device *dev = &pdev->dev;
345 struct sprd_eic_keys_button *button = NULL;
346 struct input_dev *input = NULL;
351 this_pdata = dev_get_platdata(dev);
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);
361 input = input_allocate_device();
363 PRINT_ERR("failed to allocate input device!\n");
368 input->name = this_pdata->name ? this_pdata->name : pdev->name;
369 input->id.bustype = BUS_HOST;
370 this_pdata->input_dev = input;
373 __set_bit(EV_REP, input->evbit);
375 for (i = 0; i < this_pdata->nbuttons; i++) {
376 button = &this_pdata->buttons[i];
377 error = sprd_eic_keys_setup_key( button);
381 if(KEY_POWER == button->code)
382 sprd_eic_button_sysfs_init();
385 error = input_register_device(input);
387 PRINT_ERR("Unable to register input device!\n");
390 PRINT_INFO("probe success!\n");
396 button = &this_pdata->buttons[i];
397 free_irq(button->irq, button);
398 if (gpio_is_valid(button->gpio)) {
399 gpio_free(button->gpio);
403 input_free_device(input);
405 PRINT_ERR("probe failed!\n");
410 static int sprd_eic_keys_suspend(struct platform_device *dev, pm_message_t state)
415 static int sprd_eic_keys_resume(struct platform_device *dev)
420 #define sprd_eic_keys_suspend NULL
421 #define sprd_eic_keys_resume NULL
424 static struct platform_driver sprd_eic_keys_device_driver = {
425 .probe = sprd_eic_keys_probe,
429 .name = "sprd-eic-keys",
430 .owner = THIS_MODULE,
431 .of_match_table = of_match_ptr(sprd_eic_keys_of_match),
435 static int __init sprd_eic_keys_init(void)
438 #ifdef HOOK_POWER_KEY
442 /***************************************************************/
443 struct device_node *node = of_find_compatible_node(NULL, NULL, "sprd,sprd-eic-keys");
445 PRINT_INFO("Find the node name:%s\n",node->name);
447 struct platform_device *dev = of_find_device_by_node(node);
449 PRINT_INFO("Find the device name:%s\n",dev->name);
451 PRINT_INFO("Not Find the device\n");
454 PRINT_INFO("Not Find the node\n");
455 /***************************************************************/
457 return platform_driver_register(&sprd_eic_keys_device_driver);
460 static void __exit sprd_eic_keys_exit(void)
462 #ifdef HOOK_POWER_KEY
465 platform_driver_unregister(&sprd_eic_keys_device_driver);
468 late_initcall(sprd_eic_keys_init);
469 module_exit(sprd_eic_keys_exit);
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");