2 * linux/arch/arm/plat-omap/gpio-switch.c
4 * Copyright (C) 2004-2006 Nokia Corporation
5 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
6 * and Paul Mundt <paul.mundt@nokia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/sched.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/timer.h>
21 #include <linux/err.h>
22 #include <mach/hardware.h>
23 #include <mach/gpio.h>
24 #include <mach/irqs.h>
26 #include <mach/board.h>
27 #include <mach/gpio-switch.h>
35 unsigned both_edges:1;
40 void (* notify)(void *data, int state);
43 struct work_struct work;
44 struct timer_list timer;
45 struct platform_device pdev;
47 struct list_head node;
50 static LIST_HEAD(gpio_switches);
51 static struct platform_device *gpio_sw_platform_dev;
52 static struct platform_driver gpio_sw_driver;
54 static const struct omap_gpio_switch *board_gpio_sw_table;
55 static int board_gpio_sw_count;
57 static const char *cover_str[2] = { "open", "closed" };
58 static const char *connection_str[2] = { "disconnected", "connected" };
59 static const char *activity_str[2] = { "inactive", "active" };
62 * GPIO switch state default debounce delay in ms
64 #define OMAP_GPIO_SW_DEFAULT_DEBOUNCE 10
66 static const char **get_sw_str(struct gpio_switch *sw)
69 case OMAP_GPIO_SWITCH_TYPE_COVER:
71 case OMAP_GPIO_SWITCH_TYPE_CONNECTION:
72 return connection_str;
73 case OMAP_GPIO_SWITCH_TYPE_ACTIVITY:
81 static const char *get_sw_type(struct gpio_switch *sw)
84 case OMAP_GPIO_SWITCH_TYPE_COVER:
86 case OMAP_GPIO_SWITCH_TYPE_CONNECTION:
88 case OMAP_GPIO_SWITCH_TYPE_ACTIVITY:
96 static void print_sw_state(struct gpio_switch *sw, int state)
100 str = get_sw_str(sw);
102 printk(KERN_INFO "%s (GPIO %d) is now %s\n", sw->name, sw->gpio, str[state]);
105 static int gpio_sw_get_state(struct gpio_switch *sw)
109 state = gpio_get_value(sw->gpio);
110 if (sw->flags & OMAP_GPIO_SWITCH_FLAG_INVERTED)
116 static ssize_t gpio_sw_state_store(struct device *dev,
117 struct device_attribute *attr,
121 struct gpio_switch *sw = dev_get_drvdata(dev);
126 if (!(sw->flags & OMAP_GPIO_SWITCH_FLAG_OUTPUT))
129 if (sscanf(buf, "%15s", state) != 1)
132 str = get_sw_str(sw);
133 if (strcmp(state, str[0]) == 0)
134 sw->state = enable = 0;
135 else if (strcmp(state, str[1]) == 0)
136 sw->state = enable = 1;
140 if (sw->flags & OMAP_GPIO_SWITCH_FLAG_INVERTED)
142 gpio_set_value(sw->gpio, enable);
147 static ssize_t gpio_sw_state_show(struct device *dev,
148 struct device_attribute *attr,
151 struct gpio_switch *sw = dev_get_drvdata(dev);
154 str = get_sw_str(sw);
155 return sprintf(buf, "%s\n", str[sw->state]);
158 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, gpio_sw_state_show,
159 gpio_sw_state_store);
161 static ssize_t gpio_sw_type_show(struct device *dev,
162 struct device_attribute *attr,
165 struct gpio_switch *sw = dev_get_drvdata(dev);
167 return sprintf(buf, "%s\n", get_sw_type(sw));
170 static DEVICE_ATTR(type, S_IRUGO, gpio_sw_type_show, NULL);
172 static ssize_t gpio_sw_direction_show(struct device *dev,
173 struct device_attribute *attr,
176 struct gpio_switch *sw = dev_get_drvdata(dev);
179 is_output = sw->flags & OMAP_GPIO_SWITCH_FLAG_OUTPUT;
180 return sprintf(buf, "%s\n", is_output ? "output" : "input");
183 static DEVICE_ATTR(direction, S_IRUGO, gpio_sw_direction_show, NULL);
186 static irqreturn_t gpio_sw_irq_handler(int irq, void *arg)
188 struct gpio_switch *sw = arg;
189 unsigned long timeout;
192 if (!sw->both_edges) {
193 if (gpio_get_value(sw->gpio))
194 set_irq_type(gpio_to_irq(sw->gpio), IRQ_TYPE_EDGE_FALLING);
196 set_irq_type(gpio_to_irq(sw->gpio), IRQ_TYPE_EDGE_RISING);
199 state = gpio_sw_get_state(sw);
200 if (sw->state == state)
204 timeout = sw->debounce_rising;
206 timeout = sw->debounce_falling;
208 schedule_work(&sw->work);
210 mod_timer(&sw->timer, jiffies + msecs_to_jiffies(timeout));
215 static void gpio_sw_timer(unsigned long arg)
217 struct gpio_switch *sw = (struct gpio_switch *) arg;
219 schedule_work(&sw->work);
222 static void gpio_sw_handler(struct work_struct *work)
224 struct gpio_switch *sw = container_of(work, struct gpio_switch, work);
227 state = gpio_sw_get_state(sw);
228 if (sw->state == state)
232 if (sw->notify != NULL)
233 sw->notify(sw->notify_data, state);
234 sysfs_notify(&sw->pdev.dev.kobj, NULL, "state");
235 print_sw_state(sw, state);
238 static int __init can_do_both_edges(struct gpio_switch *sw)
240 if (!cpu_class_is_omap1())
242 if (OMAP_GPIO_IS_MPUIO(sw->gpio))
248 static void gpio_sw_release(struct device *dev)
252 static int __init new_switch(struct gpio_switch *sw)
254 int r, direction, trigger;
257 case OMAP_GPIO_SWITCH_TYPE_COVER:
258 case OMAP_GPIO_SWITCH_TYPE_CONNECTION:
259 case OMAP_GPIO_SWITCH_TYPE_ACTIVITY:
262 printk(KERN_ERR "invalid GPIO switch type: %d\n", sw->type);
266 sw->pdev.name = sw->name;
269 sw->pdev.dev.parent = &gpio_sw_platform_dev->dev;
270 sw->pdev.dev.driver = &gpio_sw_driver.driver;
271 sw->pdev.dev.release = gpio_sw_release;
273 r = platform_device_register(&sw->pdev);
275 printk(KERN_ERR "gpio-switch: platform device registration "
276 "failed for %s", sw->name);
279 dev_set_drvdata(&sw->pdev.dev, sw);
281 r = omap_request_gpio(sw->gpio);
283 platform_device_unregister(&sw->pdev);
287 /* input: 1, output: 0 */
288 direction = !(sw->flags & OMAP_GPIO_SWITCH_FLAG_OUTPUT);
290 gpio_direction_input(sw->gpio);
292 gpio_direction_output(sw->gpio, true);
294 sw->state = gpio_sw_get_state(sw);
297 r |= device_create_file(&sw->pdev.dev, &dev_attr_state);
298 r |= device_create_file(&sw->pdev.dev, &dev_attr_type);
299 r |= device_create_file(&sw->pdev.dev, &dev_attr_direction);
301 printk(KERN_ERR "gpio-switch: attribute file creation "
302 "failed for %s\n", sw->name);
307 if (can_do_both_edges(sw)) {
308 trigger = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
311 if (gpio_get_value(sw->gpio))
312 trigger = IRQF_TRIGGER_FALLING;
314 trigger = IRQF_TRIGGER_RISING;
316 r = request_irq(gpio_to_irq(sw->gpio), gpio_sw_irq_handler,
317 IRQF_SHARED | trigger, sw->name, sw);
319 printk(KERN_ERR "gpio-switch: request_irq() failed "
320 "for GPIO %d\n", sw->gpio);
321 platform_device_unregister(&sw->pdev);
322 omap_free_gpio(sw->gpio);
326 INIT_WORK(&sw->work, gpio_sw_handler);
327 init_timer(&sw->timer);
329 sw->timer.function = gpio_sw_timer;
330 sw->timer.data = (unsigned long)sw;
332 list_add(&sw->node, &gpio_switches);
337 static int __init add_atag_switches(void)
339 const struct omap_gpio_switch_config *cfg;
340 struct gpio_switch *sw;
344 cfg = omap_get_nr_config(OMAP_TAG_GPIO_SWITCH,
345 struct omap_gpio_switch_config, i);
348 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
350 printk(KERN_ERR "gpio-switch: kmalloc failed\n");
353 strncpy(sw->name, cfg->name, sizeof(cfg->name));
354 sw->gpio = cfg->gpio;
355 sw->flags = cfg->flags;
356 sw->type = cfg->type;
357 sw->debounce_rising = OMAP_GPIO_SW_DEFAULT_DEBOUNCE;
358 sw->debounce_falling = OMAP_GPIO_SW_DEFAULT_DEBOUNCE;
359 if ((r = new_switch(sw)) < 0) {
367 static struct gpio_switch * __init find_switch(int gpio, const char *name)
369 struct gpio_switch *sw;
371 list_for_each_entry(sw, &gpio_switches, node) {
372 if ((gpio < 0 || sw->gpio != gpio) &&
373 (name == NULL || strcmp(sw->name, name) != 0))
376 if (gpio < 0 || name == NULL)
379 if (strcmp(sw->name, name) != 0)
380 printk("gpio-switch: name mismatch for %d (%s, %s)\n",
381 gpio, name, sw->name);
382 else if (sw->gpio != gpio)
383 printk("gpio-switch: GPIO mismatch for %s (%d, %d)\n",
384 name, gpio, sw->gpio);
391 static int __init add_board_switches(void)
395 for (i = 0; i < board_gpio_sw_count; i++) {
396 const struct omap_gpio_switch *cfg;
397 struct gpio_switch *sw;
400 cfg = board_gpio_sw_table + i;
401 if (strlen(cfg->name) > sizeof(sw->name) - 1)
403 /* Check whether we only update an existing switch
404 * or add a new switch. */
405 sw = find_switch(cfg->gpio, cfg->name);
407 sw->debounce_rising = cfg->debounce_rising;
408 sw->debounce_falling = cfg->debounce_falling;
409 sw->notify = cfg->notify;
410 sw->notify_data = cfg->notify_data;
413 if (cfg->gpio < 0 || cfg->name == NULL) {
414 printk("gpio-switch: required switch not "
415 "found (%d, %s)\n", cfg->gpio,
420 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
422 printk(KERN_ERR "gpio-switch: kmalloc failed\n");
425 strlcpy(sw->name, cfg->name, sizeof(sw->name));
426 sw->gpio = cfg->gpio;
427 sw->flags = cfg->flags;
428 sw->type = cfg->type;
429 sw->debounce_rising = cfg->debounce_rising;
430 sw->debounce_falling = cfg->debounce_falling;
431 sw->notify = cfg->notify;
432 sw->notify_data = cfg->notify_data;
433 if ((r = new_switch(sw)) < 0) {
441 static void gpio_sw_cleanup(void)
443 struct gpio_switch *sw = NULL, *old = NULL;
445 list_for_each_entry(sw, &gpio_switches, node) {
448 flush_scheduled_work();
449 del_timer_sync(&sw->timer);
451 free_irq(gpio_to_irq(sw->gpio), sw);
453 device_remove_file(&sw->pdev.dev, &dev_attr_state);
454 device_remove_file(&sw->pdev.dev, &dev_attr_type);
455 device_remove_file(&sw->pdev.dev, &dev_attr_direction);
457 platform_device_unregister(&sw->pdev);
458 omap_free_gpio(sw->gpio);
464 static void __init report_initial_state(void)
466 struct gpio_switch *sw;
468 list_for_each_entry(sw, &gpio_switches, node) {
471 state = gpio_get_value(sw->gpio);
472 if (sw->flags & OMAP_GPIO_SWITCH_FLAG_INVERTED)
474 if (sw->notify != NULL)
475 sw->notify(sw->notify_data, state);
476 print_sw_state(sw, state);
480 static int gpio_sw_remove(struct platform_device *dev)
485 static struct platform_driver gpio_sw_driver = {
486 .remove = gpio_sw_remove,
488 .name = "gpio-switch",
492 void __init omap_register_gpio_switches(const struct omap_gpio_switch *tbl,
495 BUG_ON(board_gpio_sw_table != NULL);
497 board_gpio_sw_table = tbl;
498 board_gpio_sw_count = count;
501 static int __init gpio_sw_init(void)
505 printk(KERN_INFO "OMAP GPIO switch handler initializing\n");
507 r = platform_driver_register(&gpio_sw_driver);
511 gpio_sw_platform_dev = platform_device_register_simple("gpio-switch",
513 if (IS_ERR(gpio_sw_platform_dev)) {
514 r = PTR_ERR(gpio_sw_platform_dev);
518 r = add_atag_switches();
522 r = add_board_switches();
526 report_initial_state();
531 platform_device_unregister(gpio_sw_platform_dev);
533 platform_driver_unregister(&gpio_sw_driver);
537 static void __exit gpio_sw_exit(void)
540 platform_device_unregister(gpio_sw_platform_dev);
541 platform_driver_unregister(&gpio_sw_driver);
545 late_initcall(gpio_sw_init);
547 module_init(gpio_sw_init);
549 module_exit(gpio_sw_exit);
551 MODULE_AUTHOR("Juha Yrjölä <juha.yrjola@nokia.com>, Paul Mundt <paul.mundt@nokia.com");
552 MODULE_DESCRIPTION("GPIO switch driver");
553 MODULE_LICENSE("GPL");