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