Paul Mundt | 9f5e8ee | 2006-11-24 11:22:57 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Generic push-switch framework |
| 3 | * |
| 4 | * Copyright (C) 2006 Paul Mundt |
| 5 | * |
| 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 |
| 8 | * for more details. |
| 9 | */ |
| 10 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Paul Mundt | 9f5e8ee | 2006-11-24 11:22:57 +0900 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <asm/push-switch.h> |
| 16 | |
| 17 | #define DRV_NAME "push-switch" |
Paul Mundt | fce3a24 | 2006-12-07 13:20:58 +0900 | [diff] [blame] | 18 | #define DRV_VERSION "0.1.1" |
Paul Mundt | 9f5e8ee | 2006-11-24 11:22:57 +0900 | [diff] [blame] | 19 | |
| 20 | static ssize_t switch_show(struct device *dev, |
| 21 | struct device_attribute *attr, |
| 22 | char *buf) |
| 23 | { |
| 24 | struct push_switch_platform_info *psw_info = dev->platform_data; |
| 25 | return sprintf(buf, "%s\n", psw_info->name); |
| 26 | } |
| 27 | static DEVICE_ATTR(switch, S_IRUGO, switch_show, NULL); |
| 28 | |
| 29 | static void switch_timer(unsigned long data) |
| 30 | { |
| 31 | struct push_switch *psw = (struct push_switch *)data; |
| 32 | |
| 33 | schedule_work(&psw->work); |
| 34 | } |
| 35 | |
Paul Mundt | fce3a24 | 2006-12-07 13:20:58 +0900 | [diff] [blame] | 36 | static void switch_work_handler(struct work_struct *work) |
Paul Mundt | 9f5e8ee | 2006-11-24 11:22:57 +0900 | [diff] [blame] | 37 | { |
Paul Mundt | fce3a24 | 2006-12-07 13:20:58 +0900 | [diff] [blame] | 38 | struct push_switch *psw = container_of(work, struct push_switch, work); |
| 39 | struct platform_device *pdev = psw->pdev; |
Paul Mundt | 9f5e8ee | 2006-11-24 11:22:57 +0900 | [diff] [blame] | 40 | |
| 41 | psw->state = 0; |
| 42 | |
| 43 | kobject_uevent(&pdev->dev.kobj, KOBJ_CHANGE); |
| 44 | } |
| 45 | |
| 46 | static int switch_drv_probe(struct platform_device *pdev) |
| 47 | { |
| 48 | struct push_switch_platform_info *psw_info; |
| 49 | struct push_switch *psw; |
| 50 | int ret, irq; |
| 51 | |
| 52 | psw = kzalloc(sizeof(struct push_switch), GFP_KERNEL); |
| 53 | if (unlikely(!psw)) |
| 54 | return -ENOMEM; |
| 55 | |
| 56 | irq = platform_get_irq(pdev, 0); |
| 57 | if (unlikely(irq < 0)) { |
| 58 | ret = -ENODEV; |
| 59 | goto err; |
| 60 | } |
| 61 | |
| 62 | psw_info = pdev->dev.platform_data; |
| 63 | BUG_ON(!psw_info); |
| 64 | |
| 65 | ret = request_irq(irq, psw_info->irq_handler, |
| 66 | IRQF_DISABLED | psw_info->irq_flags, |
| 67 | psw_info->name ? psw_info->name : DRV_NAME, pdev); |
| 68 | if (unlikely(ret < 0)) |
| 69 | goto err; |
| 70 | |
| 71 | if (psw_info->name) { |
| 72 | ret = device_create_file(&pdev->dev, &dev_attr_switch); |
| 73 | if (unlikely(ret)) { |
| 74 | dev_err(&pdev->dev, "Failed creating device attrs\n"); |
| 75 | ret = -EINVAL; |
| 76 | goto err_irq; |
| 77 | } |
| 78 | } |
| 79 | |
Paul Mundt | fce3a24 | 2006-12-07 13:20:58 +0900 | [diff] [blame] | 80 | INIT_WORK(&psw->work, switch_work_handler); |
Paul Mundt | 9f5e8ee | 2006-11-24 11:22:57 +0900 | [diff] [blame] | 81 | init_timer(&psw->debounce); |
| 82 | |
| 83 | psw->debounce.function = switch_timer; |
| 84 | psw->debounce.data = (unsigned long)psw; |
| 85 | |
Paul Mundt | fce3a24 | 2006-12-07 13:20:58 +0900 | [diff] [blame] | 86 | /* Workqueue API brain-damage */ |
| 87 | psw->pdev = pdev; |
| 88 | |
Paul Mundt | 9f5e8ee | 2006-11-24 11:22:57 +0900 | [diff] [blame] | 89 | platform_set_drvdata(pdev, psw); |
| 90 | |
| 91 | return 0; |
| 92 | |
| 93 | err_irq: |
| 94 | free_irq(irq, pdev); |
| 95 | err: |
| 96 | kfree(psw); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int switch_drv_remove(struct platform_device *pdev) |
| 101 | { |
| 102 | struct push_switch *psw = platform_get_drvdata(pdev); |
| 103 | struct push_switch_platform_info *psw_info = pdev->dev.platform_data; |
| 104 | int irq = platform_get_irq(pdev, 0); |
| 105 | |
| 106 | if (psw_info->name) |
| 107 | device_remove_file(&pdev->dev, &dev_attr_switch); |
| 108 | |
| 109 | platform_set_drvdata(pdev, NULL); |
Tejun Heo | 539253f | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 110 | flush_work_sync(&psw->work); |
Paul Mundt | 9f5e8ee | 2006-11-24 11:22:57 +0900 | [diff] [blame] | 111 | del_timer_sync(&psw->debounce); |
| 112 | free_irq(irq, pdev); |
| 113 | |
| 114 | kfree(psw); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static struct platform_driver switch_driver = { |
| 120 | .probe = switch_drv_probe, |
| 121 | .remove = switch_drv_remove, |
| 122 | .driver = { |
| 123 | .name = DRV_NAME, |
| 124 | }, |
| 125 | }; |
| 126 | |
| 127 | static int __init switch_init(void) |
| 128 | { |
| 129 | printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION); |
| 130 | return platform_driver_register(&switch_driver); |
| 131 | } |
| 132 | |
| 133 | static void __exit switch_exit(void) |
| 134 | { |
| 135 | platform_driver_unregister(&switch_driver); |
| 136 | } |
| 137 | module_init(switch_init); |
| 138 | module_exit(switch_exit); |
| 139 | |
| 140 | MODULE_VERSION(DRV_VERSION); |
| 141 | MODULE_AUTHOR("Paul Mundt"); |
Paul Mundt | e5e3dfc | 2007-07-18 18:54:09 +0900 | [diff] [blame] | 142 | MODULE_LICENSE("GPL v2"); |