Ankush Khandelwal | 3fb6bc2 | 2012-01-31 19:09:58 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/irq.h> |
| 21 | #include <media/rc-core.h> |
| 22 | #include <media/gpio-ir-recv.h> |
| 23 | |
| 24 | #define TSOP_DRIVER_NAME "gpio-rc-recv" |
| 25 | #define TSOP_DEVICE_NAME "gpio_ir_recv" |
| 26 | |
| 27 | struct gpio_rc_dev { |
| 28 | struct rc_dev *rcdev; |
| 29 | struct mutex lock; |
| 30 | unsigned int gpio_nr; |
| 31 | bool active_low; |
| 32 | bool can_wakeup; |
| 33 | struct work_struct work; |
| 34 | }; |
| 35 | |
| 36 | static void ir_decoder_work(struct work_struct *work) |
| 37 | { |
| 38 | struct gpio_rc_dev *gpio_dev = container_of(work, |
| 39 | struct gpio_rc_dev, work); |
| 40 | unsigned int gval; |
| 41 | int rc = 0; |
| 42 | enum raw_event_type type = IR_SPACE; |
| 43 | |
| 44 | mutex_lock(&gpio_dev->lock); |
| 45 | gval = gpio_get_value_cansleep(gpio_dev->gpio_nr); |
| 46 | |
| 47 | if (gval < 0) |
| 48 | goto err_get_value; |
| 49 | |
| 50 | if (gpio_dev->active_low) |
| 51 | gval = !gval; |
| 52 | |
| 53 | if (gval == 1) |
| 54 | type = IR_PULSE; |
| 55 | |
| 56 | rc = ir_raw_event_store_edge(gpio_dev->rcdev, type); |
| 57 | if (rc < 0) |
| 58 | goto err_get_value; |
| 59 | |
| 60 | ir_raw_event_handle(gpio_dev->rcdev); |
| 61 | |
| 62 | err_get_value: |
| 63 | mutex_unlock(&gpio_dev->lock); |
| 64 | } |
| 65 | |
| 66 | static irqreturn_t gpio_ir_recv_irq_handler(int irq, void *data) |
| 67 | { |
| 68 | struct gpio_rc_dev *gpio_dev = data; |
| 69 | |
| 70 | schedule_work(&gpio_dev->work); |
| 71 | |
| 72 | return IRQ_HANDLED; |
| 73 | } |
| 74 | |
| 75 | static int __devinit gpio_ir_recv_probe(struct platform_device *pdev) |
| 76 | { |
| 77 | struct gpio_rc_dev *gpio_dev; |
| 78 | struct rc_dev *rcdev; |
| 79 | const struct gpio_ir_recv_platform_data *pdata = |
| 80 | pdev->dev.platform_data; |
| 81 | int rc = 0; |
| 82 | |
| 83 | if (!pdata) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | if (pdata->gpio_nr < 0) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL); |
| 90 | if (!gpio_dev) |
| 91 | return -ENOMEM; |
| 92 | |
| 93 | mutex_init(&gpio_dev->lock); |
| 94 | |
| 95 | rcdev = rc_allocate_device(); |
| 96 | if (!rcdev) { |
| 97 | rc = -ENOMEM; |
| 98 | goto err_allocate_device; |
| 99 | } |
| 100 | |
| 101 | rcdev->driver_type = RC_DRIVER_IR_RAW; |
| 102 | rcdev->allowed_protos = RC_TYPE_NEC; |
| 103 | rcdev->input_name = TSOP_DEVICE_NAME; |
| 104 | rcdev->input_id.bustype = BUS_HOST; |
| 105 | rcdev->driver_name = TSOP_DRIVER_NAME; |
| 106 | rcdev->map_name = RC_MAP_SAMSUNG_NECX; |
| 107 | |
| 108 | gpio_dev->rcdev = rcdev; |
| 109 | gpio_dev->gpio_nr = pdata->gpio_nr; |
| 110 | gpio_dev->active_low = pdata->active_low; |
| 111 | gpio_dev->can_wakeup = pdata->can_wakeup; |
| 112 | |
| 113 | INIT_WORK(&gpio_dev->work, ir_decoder_work); |
| 114 | |
| 115 | rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv"); |
| 116 | if (rc < 0) |
| 117 | goto err_gpio_request; |
| 118 | rc = gpio_direction_input(pdata->gpio_nr); |
| 119 | if (rc < 0) |
| 120 | goto err_gpio_direction_input; |
| 121 | |
| 122 | rc = rc_register_device(rcdev); |
| 123 | if (rc < 0) { |
| 124 | dev_err(&pdev->dev, "failed to register rc device\n"); |
| 125 | goto err_register_rc_device; |
| 126 | } |
| 127 | |
| 128 | platform_set_drvdata(pdev, gpio_dev); |
| 129 | |
| 130 | rc = request_irq(gpio_to_irq(pdata->gpio_nr), gpio_ir_recv_irq_handler, |
| 131 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 132 | "gpio-ir-recv-irq", gpio_dev); |
| 133 | if (rc < 0) |
| 134 | goto err_request_irq; |
| 135 | |
| 136 | if (pdata->can_wakeup == true) { |
| 137 | rc = enable_irq_wake(gpio_to_irq(pdata->gpio_nr)); |
| 138 | if (rc < 0) |
| 139 | goto err_enable_irq_wake; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | |
| 144 | err_enable_irq_wake: |
| 145 | free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev); |
| 146 | err_request_irq: |
| 147 | platform_set_drvdata(pdev, NULL); |
| 148 | rc_unregister_device(rcdev); |
| 149 | err_register_rc_device: |
| 150 | err_gpio_direction_input: |
| 151 | gpio_free(pdata->gpio_nr); |
| 152 | err_gpio_request: |
| 153 | rc_free_device(rcdev); |
| 154 | rcdev = NULL; |
| 155 | err_allocate_device: |
| 156 | mutex_destroy(&gpio_dev->lock); |
| 157 | kfree(gpio_dev); |
| 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | static int __devexit gpio_ir_recv_remove(struct platform_device *pdev) |
| 162 | { |
| 163 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); |
| 164 | |
| 165 | flush_work_sync(&gpio_dev->work); |
| 166 | disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); |
| 167 | free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev); |
| 168 | platform_set_drvdata(pdev, NULL); |
| 169 | rc_unregister_device(gpio_dev->rcdev); |
| 170 | gpio_free(gpio_dev->gpio_nr); |
| 171 | rc_free_device(gpio_dev->rcdev); |
| 172 | mutex_destroy(&gpio_dev->lock); |
| 173 | kfree(gpio_dev); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static struct platform_driver gpio_ir_recv_driver = { |
| 178 | .probe = gpio_ir_recv_probe, |
| 179 | .remove = __devexit_p(gpio_ir_recv_remove), |
| 180 | .driver = { |
| 181 | .name = TSOP_DRIVER_NAME, |
| 182 | .owner = THIS_MODULE, |
| 183 | }, |
| 184 | }; |
| 185 | |
| 186 | static int __init gpio_ir_recv_init(void) |
| 187 | { |
| 188 | return platform_driver_register(&gpio_ir_recv_driver); |
| 189 | } |
| 190 | module_init(gpio_ir_recv_init); |
| 191 | |
| 192 | static void __exit gpio_ir_recv_exit(void) |
| 193 | { |
| 194 | platform_driver_unregister(&gpio_ir_recv_driver); |
| 195 | } |
| 196 | module_exit(gpio_ir_recv_exit); |
| 197 | |
| 198 | MODULE_DESCRIPTION("GPIO IR Receiver driver"); |
| 199 | MODULE_LICENSE("GPL v2"); |