Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [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> |
Sachin Kamat | 8ab1aa8 | 2013-10-18 00:07:15 -0300 | [diff] [blame] | 19 | #include <linux/of.h> |
Sebastian Hesselbarth | 2fd7f39 | 2013-02-08 18:47:30 -0300 | [diff] [blame] | 20 | #include <linux/of_gpio.h> |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/irq.h> |
| 23 | #include <media/rc-core.h> |
Mauro Carvalho Chehab | eb4b0ec | 2015-11-16 08:35:53 -0200 | [diff] [blame] | 24 | #include <linux/platform_data/media/gpio-ir-recv.h> |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 25 | |
| 26 | #define GPIO_IR_DRIVER_NAME "gpio-rc-recv" |
| 27 | #define GPIO_IR_DEVICE_NAME "gpio_ir_recv" |
| 28 | |
| 29 | struct gpio_rc_dev { |
| 30 | struct rc_dev *rcdev; |
Dan Carpenter | 955b443 | 2012-03-10 04:58:18 -0300 | [diff] [blame] | 31 | int gpio_nr; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 32 | bool active_low; |
Eric Nelson | 3fb136f | 2015-09-23 11:07:08 -0300 | [diff] [blame] | 33 | struct timer_list flush_timer; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 34 | }; |
| 35 | |
Sebastian Hesselbarth | 2fd7f39 | 2013-02-08 18:47:30 -0300 | [diff] [blame] | 36 | #ifdef CONFIG_OF |
| 37 | /* |
| 38 | * Translate OpenFirmware node properties into platform_data |
| 39 | */ |
| 40 | static int gpio_ir_recv_get_devtree_pdata(struct device *dev, |
| 41 | struct gpio_ir_recv_platform_data *pdata) |
| 42 | { |
| 43 | struct device_node *np = dev->of_node; |
| 44 | enum of_gpio_flags flags; |
| 45 | int gpio; |
| 46 | |
| 47 | gpio = of_get_gpio_flags(np, 0, &flags); |
| 48 | if (gpio < 0) { |
| 49 | if (gpio != -EPROBE_DEFER) |
| 50 | dev_err(dev, "Failed to get gpio flags (%d)\n", gpio); |
| 51 | return gpio; |
| 52 | } |
| 53 | |
| 54 | pdata->gpio_nr = gpio; |
| 55 | pdata->active_low = (flags & OF_GPIO_ACTIVE_LOW); |
| 56 | /* probe() takes care of map_name == NULL or allowed_protos == 0 */ |
| 57 | pdata->map_name = of_get_property(np, "linux,rc-map-name", NULL); |
| 58 | pdata->allowed_protos = 0; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Fabian Frederick | 7f099a7 | 2015-03-16 16:54:33 -0300 | [diff] [blame] | 63 | static const struct of_device_id gpio_ir_recv_of_match[] = { |
Sebastian Hesselbarth | 2fd7f39 | 2013-02-08 18:47:30 -0300 | [diff] [blame] | 64 | { .compatible = "gpio-ir-receiver", }, |
| 65 | { }, |
| 66 | }; |
| 67 | MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match); |
| 68 | |
| 69 | #else /* !CONFIG_OF */ |
| 70 | |
| 71 | #define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS) |
| 72 | |
| 73 | #endif |
| 74 | |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 75 | static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id) |
| 76 | { |
| 77 | struct gpio_rc_dev *gpio_dev = dev_id; |
Dan Carpenter | 955b443 | 2012-03-10 04:58:18 -0300 | [diff] [blame] | 78 | int gval; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 79 | int rc = 0; |
| 80 | enum raw_event_type type = IR_SPACE; |
| 81 | |
Heiko Stübner | 64657066 | 2015-06-02 11:43:50 -0300 | [diff] [blame] | 82 | gval = gpio_get_value(gpio_dev->gpio_nr); |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 83 | |
| 84 | if (gval < 0) |
| 85 | goto err_get_value; |
| 86 | |
| 87 | if (gpio_dev->active_low) |
| 88 | gval = !gval; |
| 89 | |
| 90 | if (gval == 1) |
| 91 | type = IR_PULSE; |
| 92 | |
| 93 | rc = ir_raw_event_store_edge(gpio_dev->rcdev, type); |
| 94 | if (rc < 0) |
| 95 | goto err_get_value; |
| 96 | |
Eric Nelson | 3fb136f | 2015-09-23 11:07:08 -0300 | [diff] [blame] | 97 | mod_timer(&gpio_dev->flush_timer, |
| 98 | jiffies + nsecs_to_jiffies(gpio_dev->rcdev->timeout)); |
| 99 | |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 100 | ir_raw_event_handle(gpio_dev->rcdev); |
| 101 | |
| 102 | err_get_value: |
| 103 | return IRQ_HANDLED; |
| 104 | } |
| 105 | |
Eric Nelson | 3fb136f | 2015-09-23 11:07:08 -0300 | [diff] [blame] | 106 | static void flush_timer(unsigned long arg) |
| 107 | { |
| 108 | struct gpio_rc_dev *gpio_dev = (struct gpio_rc_dev *)arg; |
| 109 | DEFINE_IR_RAW_EVENT(ev); |
| 110 | |
| 111 | ev.timeout = true; |
| 112 | ev.duration = gpio_dev->rcdev->timeout; |
| 113 | ir_raw_event_store(gpio_dev->rcdev, &ev); |
| 114 | ir_raw_event_handle(gpio_dev->rcdev); |
| 115 | } |
| 116 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 117 | static int gpio_ir_recv_probe(struct platform_device *pdev) |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 118 | { |
| 119 | struct gpio_rc_dev *gpio_dev; |
| 120 | struct rc_dev *rcdev; |
| 121 | const struct gpio_ir_recv_platform_data *pdata = |
| 122 | pdev->dev.platform_data; |
| 123 | int rc; |
| 124 | |
Sebastian Hesselbarth | 2fd7f39 | 2013-02-08 18:47:30 -0300 | [diff] [blame] | 125 | if (pdev->dev.of_node) { |
| 126 | struct gpio_ir_recv_platform_data *dtpdata = |
| 127 | devm_kzalloc(&pdev->dev, sizeof(*dtpdata), GFP_KERNEL); |
| 128 | if (!dtpdata) |
| 129 | return -ENOMEM; |
| 130 | rc = gpio_ir_recv_get_devtree_pdata(&pdev->dev, dtpdata); |
| 131 | if (rc) |
| 132 | return rc; |
| 133 | pdata = dtpdata; |
| 134 | } |
| 135 | |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 136 | if (!pdata) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | if (pdata->gpio_nr < 0) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL); |
| 143 | if (!gpio_dev) |
| 144 | return -ENOMEM; |
| 145 | |
| 146 | rcdev = rc_allocate_device(); |
| 147 | if (!rcdev) { |
| 148 | rc = -ENOMEM; |
| 149 | goto err_allocate_device; |
| 150 | } |
| 151 | |
Benoît Thébaudeau | 975ef32 | 2012-06-18 15:02:20 -0300 | [diff] [blame] | 152 | rcdev->priv = gpio_dev; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 153 | rcdev->driver_type = RC_DRIVER_IR_RAW; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 154 | rcdev->input_name = GPIO_IR_DEVICE_NAME; |
Benoît Thébaudeau | 975ef32 | 2012-06-18 15:02:20 -0300 | [diff] [blame] | 155 | rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0"; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 156 | rcdev->input_id.bustype = BUS_HOST; |
Benoît Thébaudeau | 975ef32 | 2012-06-18 15:02:20 -0300 | [diff] [blame] | 157 | rcdev->input_id.vendor = 0x0001; |
| 158 | rcdev->input_id.product = 0x0001; |
| 159 | rcdev->input_id.version = 0x0100; |
| 160 | rcdev->dev.parent = &pdev->dev; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 161 | rcdev->driver_name = GPIO_IR_DRIVER_NAME; |
Eric Nelson | 3fb136f | 2015-09-23 11:07:08 -0300 | [diff] [blame] | 162 | rcdev->min_timeout = 0; |
| 163 | rcdev->timeout = IR_DEFAULT_TIMEOUT; |
| 164 | rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT; |
Du, Changbin | 500c320 | 2012-07-03 06:27:19 -0300 | [diff] [blame] | 165 | if (pdata->allowed_protos) |
David Härdeman | c5540fb | 2014-04-03 20:32:21 -0300 | [diff] [blame] | 166 | rcdev->allowed_protocols = pdata->allowed_protos; |
Du, Changbin | 500c320 | 2012-07-03 06:27:19 -0300 | [diff] [blame] | 167 | else |
David Härdeman | c5540fb | 2014-04-03 20:32:21 -0300 | [diff] [blame] | 168 | rcdev->allowed_protocols = RC_BIT_ALL; |
Benoît Thébaudeau | 2bd237b | 2012-06-18 15:02:44 -0300 | [diff] [blame] | 169 | rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 170 | |
| 171 | gpio_dev->rcdev = rcdev; |
| 172 | gpio_dev->gpio_nr = pdata->gpio_nr; |
| 173 | gpio_dev->active_low = pdata->active_low; |
| 174 | |
Eric Nelson | 3fb136f | 2015-09-23 11:07:08 -0300 | [diff] [blame] | 175 | setup_timer(&gpio_dev->flush_timer, flush_timer, |
| 176 | (unsigned long)gpio_dev); |
| 177 | |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 178 | rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv"); |
| 179 | if (rc < 0) |
| 180 | goto err_gpio_request; |
| 181 | rc = gpio_direction_input(pdata->gpio_nr); |
| 182 | if (rc < 0) |
| 183 | goto err_gpio_direction_input; |
| 184 | |
| 185 | rc = rc_register_device(rcdev); |
| 186 | if (rc < 0) { |
| 187 | dev_err(&pdev->dev, "failed to register rc device\n"); |
| 188 | goto err_register_rc_device; |
| 189 | } |
| 190 | |
| 191 | platform_set_drvdata(pdev, gpio_dev); |
| 192 | |
| 193 | rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr), |
| 194 | gpio_ir_recv_irq, |
| 195 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 196 | "gpio-ir-recv-irq", gpio_dev); |
| 197 | if (rc < 0) |
| 198 | goto err_request_irq; |
| 199 | |
| 200 | return 0; |
| 201 | |
| 202 | err_request_irq: |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 203 | rc_unregister_device(rcdev); |
Jesper Juhl | e5d85b9 | 2008-11-25 10:57:30 -0300 | [diff] [blame] | 204 | rcdev = NULL; |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 205 | err_register_rc_device: |
| 206 | err_gpio_direction_input: |
| 207 | gpio_free(pdata->gpio_nr); |
| 208 | err_gpio_request: |
| 209 | rc_free_device(rcdev); |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 210 | err_allocate_device: |
| 211 | kfree(gpio_dev); |
| 212 | return rc; |
| 213 | } |
| 214 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 215 | static int gpio_ir_recv_remove(struct platform_device *pdev) |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 216 | { |
| 217 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); |
| 218 | |
| 219 | free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev); |
Eric Nelson | 3fb136f | 2015-09-23 11:07:08 -0300 | [diff] [blame] | 220 | del_timer_sync(&gpio_dev->flush_timer); |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 221 | rc_unregister_device(gpio_dev->rcdev); |
| 222 | gpio_free(gpio_dev->gpio_nr); |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 223 | kfree(gpio_dev); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | #ifdef CONFIG_PM |
| 228 | static int gpio_ir_recv_suspend(struct device *dev) |
| 229 | { |
| 230 | struct platform_device *pdev = to_platform_device(dev); |
| 231 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); |
| 232 | |
| 233 | if (device_may_wakeup(dev)) |
| 234 | enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); |
| 235 | else |
| 236 | disable_irq(gpio_to_irq(gpio_dev->gpio_nr)); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int gpio_ir_recv_resume(struct device *dev) |
| 242 | { |
| 243 | struct platform_device *pdev = to_platform_device(dev); |
| 244 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); |
| 245 | |
| 246 | if (device_may_wakeup(dev)) |
| 247 | disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); |
| 248 | else |
| 249 | enable_irq(gpio_to_irq(gpio_dev->gpio_nr)); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static const struct dev_pm_ops gpio_ir_recv_pm_ops = { |
| 255 | .suspend = gpio_ir_recv_suspend, |
| 256 | .resume = gpio_ir_recv_resume, |
| 257 | }; |
| 258 | #endif |
| 259 | |
| 260 | static struct platform_driver gpio_ir_recv_driver = { |
| 261 | .probe = gpio_ir_recv_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 262 | .remove = gpio_ir_recv_remove, |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 263 | .driver = { |
| 264 | .name = GPIO_IR_DRIVER_NAME, |
Sebastian Hesselbarth | 2fd7f39 | 2013-02-08 18:47:30 -0300 | [diff] [blame] | 265 | .of_match_table = of_match_ptr(gpio_ir_recv_of_match), |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 266 | #ifdef CONFIG_PM |
| 267 | .pm = &gpio_ir_recv_pm_ops, |
| 268 | #endif |
| 269 | }, |
| 270 | }; |
Benoît Thébaudeau | 81d75e9 | 2012-06-18 15:03:06 -0300 | [diff] [blame] | 271 | module_platform_driver(gpio_ir_recv_driver); |
Ravi Kumar V | fd0f685 | 2012-02-28 01:51:40 -0300 | [diff] [blame] | 272 | |
| 273 | MODULE_DESCRIPTION("GPIO IR Receiver driver"); |
| 274 | MODULE_LICENSE("GPL v2"); |