xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision 0d98f6bbd8d62c2c7a9924e0b3e5068cc28173b0)
178a56aabSPhil Blundell /*
278a56aabSPhil Blundell  * Driver for keys on GPIO lines capable of generating interrupts.
378a56aabSPhil Blundell  *
478a56aabSPhil Blundell  * Copyright 2005 Phil Blundell
578a56aabSPhil Blundell  *
678a56aabSPhil Blundell  * This program is free software; you can redistribute it and/or modify
778a56aabSPhil Blundell  * it under the terms of the GNU General Public License version 2 as
878a56aabSPhil Blundell  * published by the Free Software Foundation.
978a56aabSPhil Blundell  */
1078a56aabSPhil Blundell 
1178a56aabSPhil Blundell #include <linux/module.h>
1278a56aabSPhil Blundell #include <linux/version.h>
1378a56aabSPhil Blundell 
1478a56aabSPhil Blundell #include <linux/init.h>
1578a56aabSPhil Blundell #include <linux/fs.h>
1678a56aabSPhil Blundell #include <linux/interrupt.h>
1778a56aabSPhil Blundell #include <linux/irq.h>
1878a56aabSPhil Blundell #include <linux/sched.h>
1978a56aabSPhil Blundell #include <linux/pm.h>
2078a56aabSPhil Blundell #include <linux/sysctl.h>
2178a56aabSPhil Blundell #include <linux/proc_fs.h>
2278a56aabSPhil Blundell #include <linux/delay.h>
2378a56aabSPhil Blundell #include <linux/platform_device.h>
2478a56aabSPhil Blundell #include <linux/input.h>
2578a56aabSPhil Blundell #include <linux/irq.h>
2678a56aabSPhil Blundell 
27*0d98f6bbSPhilipp Zabel #include <asm/gpio.h>
2878a56aabSPhil Blundell #include <asm/arch/hardware.h>
2978a56aabSPhil Blundell 
3078a56aabSPhil Blundell #include <asm/hardware/gpio_keys.h>
3178a56aabSPhil Blundell 
3278a56aabSPhil Blundell static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
3378a56aabSPhil Blundell {
3478a56aabSPhil Blundell 	int i;
3578a56aabSPhil Blundell 	struct platform_device *pdev = dev_id;
3678a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
3778a56aabSPhil Blundell 	struct input_dev *input = platform_get_drvdata(pdev);
3878a56aabSPhil Blundell 
3978a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
4078a56aabSPhil Blundell 		int gpio = pdata->buttons[i].gpio;
41*0d98f6bbSPhilipp Zabel 		if (irq == gpio_to_irq(gpio)) {
42*0d98f6bbSPhilipp Zabel 			int state = (gpio_get_value(gpio) ? 1 : 0) ^ (pdata->buttons[i].active_low);
4378a56aabSPhil Blundell 
4478a56aabSPhil Blundell 			input_report_key(input, pdata->buttons[i].keycode, state);
4578a56aabSPhil Blundell 			input_sync(input);
4678a56aabSPhil Blundell 		}
4778a56aabSPhil Blundell 	}
4878a56aabSPhil Blundell 
4978a56aabSPhil Blundell 	return IRQ_HANDLED;
5078a56aabSPhil Blundell }
5178a56aabSPhil Blundell 
5278a56aabSPhil Blundell static int __devinit gpio_keys_probe(struct platform_device *pdev)
5378a56aabSPhil Blundell {
5478a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
5578a56aabSPhil Blundell 	struct input_dev *input;
5678a56aabSPhil Blundell 	int i, error;
5778a56aabSPhil Blundell 
5878a56aabSPhil Blundell 	input = input_allocate_device();
5978a56aabSPhil Blundell 	if (!input)
6078a56aabSPhil Blundell 		return -ENOMEM;
6178a56aabSPhil Blundell 
6278a56aabSPhil Blundell 	platform_set_drvdata(pdev, input);
6378a56aabSPhil Blundell 
6478a56aabSPhil Blundell 	input->evbit[0] = BIT(EV_KEY);
6578a56aabSPhil Blundell 
6678a56aabSPhil Blundell 	input->name = pdev->name;
6778a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
6878a56aabSPhil Blundell 	input->cdev.dev = &pdev->dev;
6978a56aabSPhil Blundell 	input->private = pdata;
7078a56aabSPhil Blundell 
7178a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
7278a56aabSPhil Blundell 	input->id.vendor = 0x0001;
7378a56aabSPhil Blundell 	input->id.product = 0x0001;
7478a56aabSPhil Blundell 	input->id.version = 0x0100;
7578a56aabSPhil Blundell 
7678a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
7778a56aabSPhil Blundell 		int code = pdata->buttons[i].keycode;
78*0d98f6bbSPhilipp Zabel 		int irq = gpio_to_irq(pdata->buttons[i].gpio);
7978a56aabSPhil Blundell 
8078a56aabSPhil Blundell 		set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
812db6346fSThomas Gleixner 		error = request_irq(irq, gpio_keys_isr, IRQF_SAMPLE_RANDOM,
8278a56aabSPhil Blundell 				     pdata->buttons[i].desc ? pdata->buttons[i].desc : "gpio_keys",
8378a56aabSPhil Blundell 				     pdev);
8478a56aabSPhil Blundell 		if (error) {
85*0d98f6bbSPhilipp Zabel 			printk(KERN_ERR "gpio-keys: unable to claim irq %d; error %d\n",
86*0d98f6bbSPhilipp Zabel 				irq, error);
8778a56aabSPhil Blundell 			goto fail;
8878a56aabSPhil Blundell 		}
8978a56aabSPhil Blundell 		set_bit(code, input->keybit);
9078a56aabSPhil Blundell 	}
9178a56aabSPhil Blundell 
9278a56aabSPhil Blundell 	error = input_register_device(input);
9378a56aabSPhil Blundell 	if (error) {
9478a56aabSPhil Blundell 		printk(KERN_ERR "Unable to register gpio-keys input device\n");
9578a56aabSPhil Blundell 		goto fail;
9678a56aabSPhil Blundell 	}
9778a56aabSPhil Blundell 
9878a56aabSPhil Blundell 	return 0;
9978a56aabSPhil Blundell 
10078a56aabSPhil Blundell  fail:
10178a56aabSPhil Blundell 	for (i = i - 1; i >= 0; i--)
102*0d98f6bbSPhilipp Zabel 		free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
10378a56aabSPhil Blundell 
10478a56aabSPhil Blundell 	input_free_device(input);
10578a56aabSPhil Blundell 
10678a56aabSPhil Blundell 	return error;
10778a56aabSPhil Blundell }
10878a56aabSPhil Blundell 
10978a56aabSPhil Blundell static int __devexit gpio_keys_remove(struct platform_device *pdev)
11078a56aabSPhil Blundell {
11178a56aabSPhil Blundell 	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
11278a56aabSPhil Blundell 	struct input_dev *input = platform_get_drvdata(pdev);
11378a56aabSPhil Blundell 	int i;
11478a56aabSPhil Blundell 
11578a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
116*0d98f6bbSPhilipp Zabel 		int irq = gpio_to_irq(pdata->buttons[i].gpio);
11778a56aabSPhil Blundell 		free_irq(irq, pdev);
11878a56aabSPhil Blundell 	}
11978a56aabSPhil Blundell 
12078a56aabSPhil Blundell 	input_unregister_device(input);
12178a56aabSPhil Blundell 
12278a56aabSPhil Blundell 	return 0;
12378a56aabSPhil Blundell }
12478a56aabSPhil Blundell 
12578a56aabSPhil Blundell struct platform_driver gpio_keys_device_driver = {
12678a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
12778a56aabSPhil Blundell 	.remove		= __devexit_p(gpio_keys_remove),
12878a56aabSPhil Blundell 	.driver		= {
12978a56aabSPhil Blundell 		.name	= "gpio-keys",
13078a56aabSPhil Blundell 	}
13178a56aabSPhil Blundell };
13278a56aabSPhil Blundell 
13378a56aabSPhil Blundell static int __init gpio_keys_init(void)
13478a56aabSPhil Blundell {
13578a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
13678a56aabSPhil Blundell }
13778a56aabSPhil Blundell 
13878a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
13978a56aabSPhil Blundell {
14078a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
14178a56aabSPhil Blundell }
14278a56aabSPhil Blundell 
14378a56aabSPhil Blundell module_init(gpio_keys_init);
14478a56aabSPhil Blundell module_exit(gpio_keys_exit);
14578a56aabSPhil Blundell 
14678a56aabSPhil Blundell MODULE_LICENSE("GPL");
14778a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
14878a56aabSPhil Blundell MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
149