blob: 7e9faa211e48b8b6bfcfda4145e05a6e908c759a [file] [log] [blame]
Mike Lockwood80d38ea2008-10-14 12:50:16 -04001/*
2 * drivers/switch/switch_gpio.c
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 *
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.
10 *
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.
15 *
16*/
17
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>
27
28struct gpio_switch_data {
29 struct switch_dev sdev;
30 unsigned gpio;
31 const char *name_on;
32 const char *name_off;
33 const char *state_on;
34 const char *state_off;
35 int irq;
36 struct work_struct work;
37};
38
39static void gpio_switch_work(struct work_struct *work)
40{
41 int state;
42 struct gpio_switch_data *data =
43 container_of(work, struct gpio_switch_data, work);
44
45 state = gpio_get_value(data->gpio);
46 switch_set_state(&data->sdev, state);
47}
48
49static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
50{
51 struct gpio_switch_data *switch_data =
52 (struct gpio_switch_data *)dev_id;
53
54 schedule_work(&switch_data->work);
55 return IRQ_HANDLED;
56}
57
58static ssize_t switch_gpio_print_state(struct switch_dev *sdev, char *buf)
59{
60 struct gpio_switch_data *switch_data =
61 container_of(sdev, struct gpio_switch_data, sdev);
62 const char *state;
63 if (switch_get_state(sdev))
64 state = switch_data->state_on;
65 else
66 state = switch_data->state_off;
67
68 if (state)
69 return sprintf(buf, "%s\n", state);
70 return -1;
71}
72
73static int gpio_switch_probe(struct platform_device *pdev)
74{
75 struct gpio_switch_platform_data *pdata = pdev->dev.platform_data;
76 struct gpio_switch_data *switch_data;
77 int ret = 0;
78
79 if (!pdata)
80 return -EBUSY;
81
82 switch_data = kzalloc(sizeof(struct gpio_switch_data), GFP_KERNEL);
83 if (!switch_data)
84 return -ENOMEM;
85
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;
93
94 ret = switch_dev_register(&switch_data->sdev);
95 if (ret < 0)
96 goto err_switch_dev_register;
97
98 ret = gpio_request(switch_data->gpio, pdev->name);
99 if (ret < 0)
100 goto err_request_gpio;
101
102 ret = gpio_direction_input(switch_data->gpio);
103 if (ret < 0)
104 goto err_set_gpio_input;
105
106 INIT_WORK(&switch_data->work, gpio_switch_work);
107
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;
112 }
113
114 ret = request_irq(switch_data->irq, gpio_irq_handler,
115 IRQF_TRIGGER_LOW, pdev->name, switch_data);
116 if (ret < 0)
117 goto err_request_irq;
118
119 /* Perform initial detection */
120 gpio_switch_work(&switch_data->work);
121
122 return 0;
123
124err_request_irq:
125err_detect_irq_num_failed:
126err_set_gpio_input:
127 gpio_free(switch_data->gpio);
128err_request_gpio:
129 switch_dev_unregister(&switch_data->sdev);
130err_switch_dev_register:
131 kfree(switch_data);
132
133 return ret;
134}
135
136static int __devexit gpio_switch_remove(struct platform_device *pdev)
137{
138 struct gpio_switch_data *switch_data = platform_get_drvdata(pdev);
139
140 cancel_work_sync(&switch_data->work);
141 gpio_free(switch_data->gpio);
142 switch_dev_unregister(&switch_data->sdev);
143 kfree(switch_data);
144
145 return 0;
146}
147
148static struct platform_driver gpio_switch_driver = {
149 .probe = gpio_switch_probe,
150 .remove = __devexit_p(gpio_switch_remove),
151 .driver = {
152 .name = "switch-gpio",
153 .owner = THIS_MODULE,
154 },
155};
156
157static int __init gpio_switch_init(void)
158{
159 return platform_driver_register(&gpio_switch_driver);
160}
161
162static void __exit gpio_switch_exit(void)
163{
164 platform_driver_unregister(&gpio_switch_driver);
165}
166
167module_init(gpio_switch_init);
168module_exit(gpio_switch_exit);
169
170MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
171MODULE_DESCRIPTION("GPIO Switch driver");
172MODULE_LICENSE("GPL");