aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/switch
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
commitfcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch)
treea57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/switch
parent8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff)
Added missing tegra files.HEADmaster
Diffstat (limited to 'drivers/switch')
-rw-r--r--drivers/switch/Kconfig15
-rw-r--r--drivers/switch/Makefile4
-rw-r--r--drivers/switch/switch_class.c175
-rw-r--r--drivers/switch/switch_gpio.c172
4 files changed, 366 insertions, 0 deletions
diff --git a/drivers/switch/Kconfig b/drivers/switch/Kconfig
new file mode 100644
index 00000000000..52385914b9a
--- /dev/null
+++ b/drivers/switch/Kconfig
@@ -0,0 +1,15 @@
1menuconfig SWITCH
2 tristate "Switch class support"
3 help
4 Say Y here to enable switch class support. This allows
5 monitoring switches by userspace via sysfs and uevent.
6
7if SWITCH
8
9config SWITCH_GPIO
10 tristate "GPIO Swith support"
11 depends on GENERIC_GPIO
12 help
13 Say Y here to enable GPIO based switch support.
14
15endif # SWITCH
diff --git a/drivers/switch/Makefile b/drivers/switch/Makefile
new file mode 100644
index 00000000000..f7606ed4a71
--- /dev/null
+++ b/drivers/switch/Makefile
@@ -0,0 +1,4 @@
1# Switch Class Driver
2obj-$(CONFIG_SWITCH) += switch_class.o
3obj-$(CONFIG_SWITCH_GPIO) += switch_gpio.o
4
diff --git a/drivers/switch/switch_class.c b/drivers/switch/switch_class.c
new file mode 100644
index 00000000000..2c94752f645
--- /dev/null
+++ b/drivers/switch/switch_class.c
@@ -0,0 +1,175 @@
1/*
2 * drivers/switch/switch_class.c
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Copyright (C) 2012 - NVIDIA, Inc.
6 * Author: Mike Lockwood <lockwood@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17*/
18
19#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/init.h>
22#include <linux/device.h>
23#include <linux/fs.h>
24#include <linux/err.h>
25#include <linux/switch.h>
26
27struct class *switch_class;
28static atomic_t device_count;
29
30static ssize_t state_show(struct device *dev, struct device_attribute *attr,
31 char *buf)
32{
33 struct switch_dev *sdev = (struct switch_dev *)
34 dev_get_drvdata(dev);
35
36 if (sdev->print_state) {
37 int ret = sdev->print_state(sdev, buf);
38 if (ret >= 0)
39 return ret;
40 }
41 return sprintf(buf, "%d\n", sdev->state);
42}
43
44static ssize_t name_show(struct device *dev, struct device_attribute *attr,
45 char *buf)
46{
47 struct switch_dev *sdev = (struct switch_dev *)
48 dev_get_drvdata(dev);
49
50 if (sdev->print_name) {
51 int ret = sdev->print_name(sdev, buf);
52 if (ret >= 0)
53 return ret;
54 }
55 return sprintf(buf, "%s\n", sdev->name);
56}
57
58static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, state_show, NULL);
59static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, name_show, NULL);
60
61void switch_set_state(struct switch_dev *sdev, int state)
62{
63 char name_buf[120];
64 char state_buf[120];
65 char *prop_buf;
66 char *envp[3];
67 int env_offset = 0;
68 int length;
69
70 if (sdev->state != state) {
71 sdev->state = state;
72
73 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
74 if (prop_buf) {
75 length = name_show(sdev->dev, NULL, prop_buf);
76 if (length > 0) {
77 if (prop_buf[length - 1] == '\n')
78 prop_buf[length - 1] = 0;
79 snprintf(name_buf, sizeof(name_buf),
80 "SWITCH_NAME=%s", prop_buf);
81 envp[env_offset++] = name_buf;
82 }
83 length = state_show(sdev->dev, NULL, prop_buf);
84 if (length > 0) {
85 if (prop_buf[length - 1] == '\n')
86 prop_buf[length - 1] = 0;
87 snprintf(state_buf, sizeof(state_buf),
88 "SWITCH_STATE=%s", prop_buf);
89 envp[env_offset++] = state_buf;
90 }
91 envp[env_offset] = NULL;
92 kobject_uevent_env(&sdev->dev->kobj, KOBJ_CHANGE, envp);
93 free_page((unsigned long)prop_buf);
94 } else {
95 printk(KERN_ERR "out of memory in switch_set_state\n");
96 kobject_uevent(&sdev->dev->kobj, KOBJ_CHANGE);
97 }
98 }
99}
100EXPORT_SYMBOL_GPL(switch_set_state);
101
102static int create_switch_class(void)
103{
104 if (!switch_class) {
105 switch_class = class_create(THIS_MODULE, "switch");
106 if (IS_ERR(switch_class))
107 return PTR_ERR(switch_class);
108 atomic_set(&device_count, 0);
109 }
110
111 return 0;
112}
113
114int switch_dev_register(struct switch_dev *sdev)
115{
116 int ret;
117
118 if (!switch_class) {
119 ret = create_switch_class();
120 if (ret < 0)
121 return ret;
122 }
123
124 sdev->index = atomic_inc_return(&device_count);
125 sdev->dev = device_create(switch_class, NULL,
126 MKDEV(0, sdev->index), NULL, sdev->name);
127 if (IS_ERR(sdev->dev))
128 return PTR_ERR(sdev->dev);
129
130 ret = device_create_file(sdev->dev, &dev_attr_state);
131 if (ret < 0)
132 goto err_create_file_1;
133 ret = device_create_file(sdev->dev, &dev_attr_name);
134 if (ret < 0)
135 goto err_create_file_2;
136
137 dev_set_drvdata(sdev->dev, sdev);
138 sdev->state = 0;
139 return 0;
140
141err_create_file_2:
142 device_remove_file(sdev->dev, &dev_attr_state);
143err_create_file_1:
144 device_destroy(switch_class, MKDEV(0, sdev->index));
145 printk(KERN_ERR "switch: Failed to register driver %s\n", sdev->name);
146
147 return ret;
148}
149EXPORT_SYMBOL_GPL(switch_dev_register);
150
151void switch_dev_unregister(struct switch_dev *sdev)
152{
153 device_remove_file(sdev->dev, &dev_attr_name);
154 device_remove_file(sdev->dev, &dev_attr_state);
155 dev_set_drvdata(sdev->dev, NULL);
156 device_destroy(switch_class, MKDEV(0, sdev->index));
157}
158EXPORT_SYMBOL_GPL(switch_dev_unregister);
159
160static int __init switch_class_init(void)
161{
162 return create_switch_class();
163}
164
165static void __exit switch_class_exit(void)
166{
167 class_destroy(switch_class);
168}
169
170module_init(switch_class_init);
171module_exit(switch_class_exit);
172
173MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
174MODULE_DESCRIPTION("Switch class driver");
175MODULE_LICENSE("GPL");
diff --git a/drivers/switch/switch_gpio.c b/drivers/switch/switch_gpio.c
new file mode 100644
index 00000000000..7e9faa211e4
--- /dev/null
+++ b/drivers/switch/switch_gpio.c
@@ -0,0 +1,172 @@
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/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");