2 * drivers/switch/switch_gpio.c
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/switch.h>
25 #include <linux/workqueue.h>
26 #include <linux/gpio.h>
28 struct gpio_switch_data {
29 struct switch_dev sdev;
34 const char *state_off;
36 struct work_struct work;
39 static void gpio_switch_work(struct work_struct *work)
42 struct gpio_switch_data *data =
43 container_of(work, struct gpio_switch_data, work);
45 state = gpio_get_value(data->gpio);
46 switch_set_state(&data->sdev, state);
49 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
51 struct gpio_switch_data *switch_data =
52 (struct gpio_switch_data *)dev_id;
54 schedule_work(&switch_data->work);
58 static ssize_t switch_gpio_print_state(struct switch_dev *sdev, char *buf)
60 struct gpio_switch_data *switch_data =
61 container_of(sdev, struct gpio_switch_data, sdev);
63 if (switch_get_state(sdev))
64 state = switch_data->state_on;
66 state = switch_data->state_off;
69 return sprintf(buf, "%s\n", state);
73 static int gpio_switch_probe(struct platform_device *pdev)
75 struct gpio_switch_platform_data *pdata = pdev->dev.platform_data;
76 struct gpio_switch_data *switch_data;
82 switch_data = kzalloc(sizeof(struct gpio_switch_data), GFP_KERNEL);
86 switch_data->sdev.name = pdata->name;
87 switch_data->gpio = pdata->gpio;
88 switch_data->name_on = pdata->name_on;
89 switch_data->name_off = pdata->name_off;
90 switch_data->state_on = pdata->state_on;
91 switch_data->state_off = pdata->state_off;
92 switch_data->sdev.print_state = switch_gpio_print_state;
94 ret = switch_dev_register(&switch_data->sdev);
96 goto err_switch_dev_register;
98 ret = gpio_request(switch_data->gpio, pdev->name);
100 goto err_request_gpio;
102 ret = gpio_direction_input(switch_data->gpio);
104 goto err_set_gpio_input;
106 INIT_WORK(&switch_data->work, gpio_switch_work);
108 switch_data->irq = gpio_to_irq(switch_data->gpio);
109 if (switch_data->irq < 0) {
110 ret = switch_data->irq;
111 goto err_detect_irq_num_failed;
114 ret = request_irq(switch_data->irq, gpio_irq_handler,
115 IRQF_TRIGGER_LOW, pdev->name, switch_data);
117 goto err_request_irq;
119 /* Perform initial detection */
120 gpio_switch_work(&switch_data->work);
125 err_detect_irq_num_failed:
127 gpio_free(switch_data->gpio);
129 switch_dev_unregister(&switch_data->sdev);
130 err_switch_dev_register:
136 static int gpio_switch_remove(struct platform_device *pdev)
138 struct gpio_switch_data *switch_data = platform_get_drvdata(pdev);
140 cancel_work_sync(&switch_data->work);
141 gpio_free(switch_data->gpio);
142 switch_dev_unregister(&switch_data->sdev);
148 static struct platform_driver gpio_switch_driver = {
149 .probe = gpio_switch_probe,
150 .remove = gpio_switch_remove,
152 .name = "switch-gpio",
153 .owner = THIS_MODULE,
157 static int __init gpio_switch_init(void)
159 return platform_driver_register(&gpio_switch_driver);
162 static void __exit gpio_switch_exit(void)
164 platform_driver_unregister(&gpio_switch_driver);
167 module_init(gpio_switch_init);
168 module_exit(gpio_switch_exit);
170 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
171 MODULE_DESCRIPTION("GPIO Switch driver");
172 MODULE_LICENSE("GPL");