2 * Driver for keys on GPIO lines capable of generating interrupts.
4 * Copyright 2005 Phil Blundell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/sched.h>
19 #include <linux/sysctl.h>
20 #include <linux/proc_fs.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/input.h>
24 #include <linux/workqueue.h>
28 #include <linux/mfd/wm831x/core.h>
29 #include <linux/mfd/wm831x/pdata.h>
31 #define CONFIG_WM831X_GPIO_KEY_DEBUG 0
33 #if (CONFIG_WM831X_GPIO_KEY_DEBUG)
34 #define WM831X_GPIO_KEY_DG(format, ...) printk(format, ## __VA_ARGS__)
36 #define WM831X_GPIO_KEY_DG(format, ...)
38 bool isHSKeyMIC = false;
40 struct wm831x_gpio_keys_button *media_button;
42 extern bool wm8994_set_status(void);
43 extern int headset_status(void);
45 struct wm831x_gpio_button_data {
46 struct wm831x_gpio_keys_button *button;
47 struct input_dev *input;
48 struct timer_list timer;
49 struct work_struct work;
52 struct wm831x_gpio_keys_drvdata {
53 struct input_dev *input;
54 struct wm831x_gpio_button_data data[0];
57 bool isHSKey_MIC(void)
61 EXPORT_SYMBOL_GPL(isHSKey_MIC);
63 void detect_HSMic(void)
66 struct wm831x_gpio_keys_button *button = media_button;
67 WM831X_GPIO_KEY_DG("detect_HSMic\n");
78 state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
80 WM831X_GPIO_KEY_DG("detect_HSMic: code=%d,gpio=%d\n",button->gpio,button->code);
82 WM831X_GPIO_KEY_DG("detect_HSMic:---headset without MIC and HSKey---\n");
85 WM831X_GPIO_KEY_DG("detect_HSMic:---headset with MIC---\n");
91 EXPORT_SYMBOL_GPL(detect_HSMic);
93 static int HSKeyDetect(int state)
95 WM831X_GPIO_KEY_DG("HSKeyDetect\n");
98 WM831X_GPIO_KEY_DG("headset_status() == true !\n");
99 if(pre_state != state && !wm8994_set_status()){
100 WM831X_GPIO_KEY_DG("wm8994_set_status() == true !\n");
108 WM831X_GPIO_KEY_DG("wm8994_set_status() == false !\n");
113 WM831X_GPIO_KEY_DG("headset_status() == false !\n");
121 static void wm831x_gpio_keys_report_event(struct work_struct *work)
123 struct wm831x_gpio_button_data *bdata =
124 container_of(work, struct wm831x_gpio_button_data, work);
125 struct wm831x_gpio_keys_button *button = bdata->button;
126 struct input_dev *input = bdata->input;
127 unsigned int type = button->type ?: EV_KEY;
129 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
130 if(button->code == KEY_MEDIA)
132 state = HSKeyDetect(state);
136 WM831X_GPIO_KEY_DG("wm831x_gpio_keys_report_event:HSKeyDetect=-1\n");
140 printk("wm831x_gpio_keys_report_event:state=%d,code=%d \n",state,button->code);
142 input_event(input, type, button->code, state);
145 enable_irq(gpio_to_irq(button->gpio));
149 static void wm831x_gpio_keys_timer(unsigned long _data)
151 struct wm831x_gpio_button_data *data = (struct wm831x_gpio_button_data *)_data;
153 WM831X_GPIO_KEY_DG("wm831x_gpio_keys_timer\n");
154 schedule_work(&data->work);
157 static irqreturn_t wm831x_gpio_keys_isr(int irq, void *dev_id)
159 struct wm831x_gpio_button_data *bdata = dev_id;
160 struct wm831x_gpio_keys_button *button = bdata->button;
162 //printk("wm831x_gpio_keys_isr:irq=%d,%d \n",irq,button->debounce_interval);
164 BUG_ON(irq != gpio_to_irq(button->gpio));
165 disable_irq_nosync(gpio_to_irq(button->gpio));
166 if (button->debounce_interval)
167 mod_timer(&bdata->timer,
168 jiffies + msecs_to_jiffies(button->debounce_interval));
170 schedule_work(&bdata->work);
175 static int __devinit wm831x_gpio_keys_probe(struct platform_device *pdev)
178 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
179 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
180 struct wm831x_gpio_keys_pdata *gpio_keys;
181 struct wm831x_gpio_keys_drvdata *ddata;
182 struct input_dev *input;
186 printk("wm831x_gpio_keys_probe\n");
188 if (pdata == NULL || pdata->gpio_keys == NULL)
191 gpio_keys = pdata->gpio_keys;
193 ddata = kzalloc(sizeof(struct wm831x_gpio_keys_drvdata) +
194 gpio_keys->nbuttons * sizeof(struct wm831x_gpio_button_data),
196 input = input_allocate_device();
197 if (!ddata || !input) {
202 platform_set_drvdata(pdev, ddata);
204 input->name = pdev->name;
205 input->phys = "wm831x_gpio-keys/input0";
206 input->dev.parent = &pdev->dev;
208 input->id.bustype = BUS_I2C;
209 input->id.vendor = 0x0001;
210 input->id.product = 0x0001;
211 input->id.version = 0x0100;
213 /* Enable auto repeat feature of Linux input subsystem */
215 __set_bit(EV_REP, input->evbit);
217 ddata->input = input;
219 for (i = 0; i < gpio_keys->nbuttons; i++) {
220 struct wm831x_gpio_keys_button *button = &gpio_keys->buttons[i];
221 struct wm831x_gpio_button_data *bdata = &ddata->data[i];
223 unsigned int type = button->type ?: EV_KEY;
225 bdata->input = input;
226 bdata->button = button;
228 if(button->code == KEY_MEDIA)
230 media_button = button;
232 if (button->debounce_interval)
233 setup_timer(&bdata->timer,
234 wm831x_gpio_keys_timer, (unsigned long)bdata);
236 INIT_WORK(&bdata->work, wm831x_gpio_keys_report_event);
238 error = gpio_request(button->gpio, button->desc ?: "wm831x_gpio_keys");
240 pr_err("wm831x_gpio-keys: failed to request GPIO %d,"
241 " error %d\n", button->gpio, error);
245 if(button->gpio >= WM831X_P01 && button->gpio <= WM831X_P12)
247 error = gpio_pull_updown(button->gpio,GPIOPullUp);
249 pr_err("wm831x_gpio-keys: failed to pull up"
250 " for GPIO %d, error %d\n",
251 button->gpio, error);
252 gpio_free(button->gpio);
257 error = gpio_direction_input(button->gpio);
259 pr_err("wm831x_gpio-keys: failed to configure input"
260 " direction for GPIO %d, error %d\n",
261 button->gpio, error);
262 gpio_free(button->gpio);
266 irq = gpio_to_irq(button->gpio);
269 pr_err("wm831x_gpio-keys: Unable to get irq number"
270 " for GPIO %d, error %d\n",
271 button->gpio, error);
272 gpio_free(button->gpio);
275 printk("wm831x_gpio_keys_probe:i=%d,gpio=%d,irq=%d \n",i,button->gpio,irq);
276 enable_irq_wake(irq);
277 if(button->gpio >= WM831X_P01 && button->gpio <= WM831X_P12)
279 error = request_threaded_irq(irq, NULL,wm831x_gpio_keys_isr,
281 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
282 button->desc ? button->desc : "wm831x_gpio_keys",
285 else if(button->gpio >= TCA6424_P00 && button->gpio <= TCA6424_P27)
287 error = request_irq(irq, wm831x_gpio_keys_isr,
288 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
289 button->desc ? button->desc : "tca6424_gpio_keys",
294 pr_err("wm831x_gpio-keys: Unable to claim irq %d; error %d\n",
296 gpio_free(button->gpio);
300 //if (button->wakeup)
303 input_set_capability(input, type, button->code);
306 error = input_register_device(input);
308 pr_err("wm831x_gpio-keys: Unable to register input device, "
309 "error: %d\n", error);
313 //device_init_wakeup(&pdev->dev, wakeup);
319 free_irq(gpio_to_irq(gpio_keys->buttons[i].gpio), &ddata->data[i]);
320 if (gpio_keys->buttons[i].debounce_interval)
321 del_timer_sync(&ddata->data[i].timer);
323 cancel_work_sync(&ddata->data[i].work);
324 gpio_free(gpio_keys->buttons[i].gpio);
327 platform_set_drvdata(pdev, NULL);
329 input_free_device(input);
335 static int __devexit wm831x_gpio_keys_remove(struct platform_device *pdev)
337 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
338 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
339 struct wm831x_gpio_keys_pdata *gpio_keys;
340 struct wm831x_gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
341 struct input_dev *input = ddata->input;
344 if (pdata == NULL || pdata->gpio_keys == NULL)
347 gpio_keys = pdata->gpio_keys;
349 //device_init_wakeup(&pdev->dev, 0);
351 for (i = 0; i < gpio_keys->nbuttons; i++) {
352 int irq = gpio_to_irq(gpio_keys->buttons[i].gpio);
353 free_irq(irq, &ddata->data[i]);
354 if (gpio_keys->buttons[i].debounce_interval)
355 del_timer_sync(&ddata->data[i].timer);
357 cancel_work_sync(&ddata->data[i].work);
358 gpio_free(gpio_keys->buttons[i].gpio);
361 input_unregister_device(input);
367 static int wm831x_gpio_keys_suspend(struct device *dev)
369 struct platform_device *pdev = to_platform_device(dev);
370 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
371 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
372 struct wm831x_gpio_keys_pdata *gpio_keys;
375 if (pdata == NULL || pdata->gpio_keys == NULL)
377 printk("wm831x_gpio_keys_suspend fail\n");
381 //printk("wm831x_gpio_keys_suspend\n");
383 gpio_keys = pdata->gpio_keys;
385 //if (device_may_wakeup(&pdev->dev)) {
386 for (i = 0; i < gpio_keys->nbuttons; i++) {
387 struct wm831x_gpio_keys_button *button = &gpio_keys->buttons[i];
388 if (button->wakeup) {
389 irq = gpio_to_irq(button->gpio);
390 enable_irq_wake(irq);
394 irq = gpio_to_irq(button->gpio);
395 disable_irq_wake(irq);
403 static int wm831x_gpio_keys_resume(struct device *dev)
405 struct platform_device *pdev = to_platform_device(dev);
406 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
407 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
408 struct wm831x_gpio_keys_pdata *gpio_keys;
411 if (pdata == NULL || pdata->gpio_keys == NULL)
413 printk("wm831x_gpio_keys_resume fail\n");
417 //printk("wm831x_gpio_keys_resume\n");
419 gpio_keys = pdata->gpio_keys;
421 //if (device_may_wakeup(&pdev->dev)) {
422 for (i = 0; i < gpio_keys->nbuttons; i++) {
423 struct wm831x_gpio_keys_button *button = &gpio_keys->buttons[i];
424 //if (button->wakeup) {
425 irq = gpio_to_irq(button->gpio);
426 enable_irq_wake(irq);
434 static const struct dev_pm_ops wm831x_gpio_keys_pm_ops = {
435 .suspend = wm831x_gpio_keys_suspend,
436 .resume = wm831x_gpio_keys_resume,
440 static struct platform_driver wm831x_gpio_keys_device_driver = {
441 .probe = wm831x_gpio_keys_probe,
442 .remove = __devexit_p(wm831x_gpio_keys_remove),
444 .name = "wm831x_gpio-keys",
445 .owner = THIS_MODULE,
447 .pm = &wm831x_gpio_keys_pm_ops,
452 static int __init wm831x_gpio_keys_init(void)
454 return platform_driver_register(&wm831x_gpio_keys_device_driver);
457 static void __exit wm831x_gpio_keys_exit(void)
459 platform_driver_unregister(&wm831x_gpio_keys_device_driver);
462 subsys_initcall(wm831x_gpio_keys_init);
463 module_exit(wm831x_gpio_keys_exit);
465 MODULE_LICENSE("GPL");
466 MODULE_AUTHOR("SRT <srt@rock-chip.com>");
467 MODULE_DESCRIPTION("Keyboard driver for WM831x GPIOs");
468 MODULE_ALIAS("platform:wm831x_gpio-keys");