2 * Generic push-switch framework
4 * Copyright (C) 2006 Paul Mundt
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/platform_device.h>
15 #include <asm/push-switch.h>
17 #define DRV_NAME "push-switch"
18 #define DRV_VERSION "0.1.1"
20 static ssize_t switch_show(struct device *dev,
21 struct device_attribute *attr,
24 struct push_switch_platform_info *psw_info = dev->platform_data;
25 return sprintf(buf, "%s\n", psw_info->name);
27 static DEVICE_ATTR_RO(switch);
29 static void switch_timer(struct timer_list *t)
31 struct push_switch *psw = from_timer(psw, t, debounce);
33 schedule_work(&psw->work);
36 static void switch_work_handler(struct work_struct *work)
38 struct push_switch *psw = container_of(work, struct push_switch, work);
39 struct platform_device *pdev = psw->pdev;
43 kobject_uevent(&pdev->dev.kobj, KOBJ_CHANGE);
46 static int switch_drv_probe(struct platform_device *pdev)
48 struct push_switch_platform_info *psw_info;
49 struct push_switch *psw;
52 psw = kzalloc(sizeof(struct push_switch), GFP_KERNEL);
56 irq = platform_get_irq(pdev, 0);
57 if (unlikely(irq < 0)) {
62 psw_info = pdev->dev.platform_data;
65 ret = request_irq(irq, psw_info->irq_handler,
67 psw_info->name ? psw_info->name : DRV_NAME, pdev);
68 if (unlikely(ret < 0))
72 ret = device_create_file(&pdev->dev, &dev_attr_switch);
74 dev_err(&pdev->dev, "Failed creating device attrs\n");
80 INIT_WORK(&psw->work, switch_work_handler);
81 timer_setup(&psw->debounce, switch_timer, 0);
83 /* Workqueue API brain-damage */
86 platform_set_drvdata(pdev, psw);
97 static int switch_drv_remove(struct platform_device *pdev)
99 struct push_switch *psw = platform_get_drvdata(pdev);
100 struct push_switch_platform_info *psw_info = pdev->dev.platform_data;
101 int irq = platform_get_irq(pdev, 0);
104 device_remove_file(&pdev->dev, &dev_attr_switch);
106 platform_set_drvdata(pdev, NULL);
107 flush_work(&psw->work);
108 del_timer_sync(&psw->debounce);
116 static struct platform_driver switch_driver = {
117 .probe = switch_drv_probe,
118 .remove = switch_drv_remove,
124 static int __init switch_init(void)
126 printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
127 return platform_driver_register(&switch_driver);
130 static void __exit switch_exit(void)
132 platform_driver_unregister(&switch_driver);
134 module_init(switch_init);
135 module_exit(switch_exit);
137 MODULE_VERSION(DRV_VERSION);
138 MODULE_AUTHOR("Paul Mundt");
139 MODULE_LICENSE("GPL v2");