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/of.h> 20 #include <linux/of_gpio.h> 21 #include <linux/platform_device.h> 22 #include <linux/irq.h> 23 #include <media/rc-core.h> 24 #include <linux/platform_data/media/gpio-ir-recv.h> 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; 31 int gpio_nr; 32 bool active_low; 33 }; 34 35 #ifdef CONFIG_OF 36 /* 37 * Translate OpenFirmware node properties into platform_data 38 */ 39 static int gpio_ir_recv_get_devtree_pdata(struct device *dev, 40 struct gpio_ir_recv_platform_data *pdata) 41 { 42 struct device_node *np = dev->of_node; 43 enum of_gpio_flags flags; 44 int gpio; 45 46 gpio = of_get_gpio_flags(np, 0, &flags); 47 if (gpio < 0) { 48 if (gpio != -EPROBE_DEFER) 49 dev_err(dev, "Failed to get gpio flags (%d)\n", gpio); 50 return gpio; 51 } 52 53 pdata->gpio_nr = gpio; 54 pdata->active_low = (flags & OF_GPIO_ACTIVE_LOW); 55 /* probe() takes care of map_name == NULL or allowed_protos == 0 */ 56 pdata->map_name = of_get_property(np, "linux,rc-map-name", NULL); 57 pdata->allowed_protos = 0; 58 59 return 0; 60 } 61 62 static const struct of_device_id gpio_ir_recv_of_match[] = { 63 { .compatible = "gpio-ir-receiver", }, 64 { }, 65 }; 66 MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match); 67 68 #else /* !CONFIG_OF */ 69 70 #define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS) 71 72 #endif 73 74 static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id) 75 { 76 struct gpio_rc_dev *gpio_dev = dev_id; 77 int gval; 78 int rc = 0; 79 80 gval = gpio_get_value(gpio_dev->gpio_nr); 81 82 if (gval < 0) 83 goto err_get_value; 84 85 if (gpio_dev->active_low) 86 gval = !gval; 87 88 rc = ir_raw_event_store_edge(gpio_dev->rcdev, gval == 1); 89 if (rc < 0) 90 goto err_get_value; 91 92 err_get_value: 93 return IRQ_HANDLED; 94 } 95 96 static int gpio_ir_recv_probe(struct platform_device *pdev) 97 { 98 struct gpio_rc_dev *gpio_dev; 99 struct rc_dev *rcdev; 100 const struct gpio_ir_recv_platform_data *pdata = 101 pdev->dev.platform_data; 102 int rc; 103 104 if (pdev->dev.of_node) { 105 struct gpio_ir_recv_platform_data *dtpdata = 106 devm_kzalloc(&pdev->dev, sizeof(*dtpdata), GFP_KERNEL); 107 if (!dtpdata) 108 return -ENOMEM; 109 rc = gpio_ir_recv_get_devtree_pdata(&pdev->dev, dtpdata); 110 if (rc) 111 return rc; 112 pdata = dtpdata; 113 } 114 115 if (!pdata) 116 return -EINVAL; 117 118 if (pdata->gpio_nr < 0) 119 return -EINVAL; 120 121 gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL); 122 if (!gpio_dev) 123 return -ENOMEM; 124 125 rcdev = rc_allocate_device(RC_DRIVER_IR_RAW); 126 if (!rcdev) { 127 rc = -ENOMEM; 128 goto err_allocate_device; 129 } 130 131 rcdev->priv = gpio_dev; 132 rcdev->device_name = GPIO_IR_DEVICE_NAME; 133 rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0"; 134 rcdev->input_id.bustype = BUS_HOST; 135 rcdev->input_id.vendor = 0x0001; 136 rcdev->input_id.product = 0x0001; 137 rcdev->input_id.version = 0x0100; 138 rcdev->dev.parent = &pdev->dev; 139 rcdev->driver_name = GPIO_IR_DRIVER_NAME; 140 rcdev->min_timeout = 1; 141 rcdev->timeout = IR_DEFAULT_TIMEOUT; 142 rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT; 143 if (pdata->allowed_protos) 144 rcdev->allowed_protocols = pdata->allowed_protos; 145 else 146 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER; 147 rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY; 148 149 gpio_dev->rcdev = rcdev; 150 gpio_dev->gpio_nr = pdata->gpio_nr; 151 gpio_dev->active_low = pdata->active_low; 152 153 rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv"); 154 if (rc < 0) 155 goto err_gpio_request; 156 rc = gpio_direction_input(pdata->gpio_nr); 157 if (rc < 0) 158 goto err_gpio_direction_input; 159 160 rc = rc_register_device(rcdev); 161 if (rc < 0) { 162 dev_err(&pdev->dev, "failed to register rc device\n"); 163 goto err_register_rc_device; 164 } 165 166 platform_set_drvdata(pdev, gpio_dev); 167 168 rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr), 169 gpio_ir_recv_irq, 170 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 171 "gpio-ir-recv-irq", gpio_dev); 172 if (rc < 0) 173 goto err_request_irq; 174 175 return 0; 176 177 err_request_irq: 178 rc_unregister_device(rcdev); 179 rcdev = NULL; 180 err_register_rc_device: 181 err_gpio_direction_input: 182 gpio_free(pdata->gpio_nr); 183 err_gpio_request: 184 rc_free_device(rcdev); 185 err_allocate_device: 186 kfree(gpio_dev); 187 return rc; 188 } 189 190 static int gpio_ir_recv_remove(struct platform_device *pdev) 191 { 192 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); 193 194 free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev); 195 rc_unregister_device(gpio_dev->rcdev); 196 gpio_free(gpio_dev->gpio_nr); 197 kfree(gpio_dev); 198 return 0; 199 } 200 201 #ifdef CONFIG_PM 202 static int gpio_ir_recv_suspend(struct device *dev) 203 { 204 struct platform_device *pdev = to_platform_device(dev); 205 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); 206 207 if (device_may_wakeup(dev)) 208 enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); 209 else 210 disable_irq(gpio_to_irq(gpio_dev->gpio_nr)); 211 212 return 0; 213 } 214 215 static int gpio_ir_recv_resume(struct device *dev) 216 { 217 struct platform_device *pdev = to_platform_device(dev); 218 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); 219 220 if (device_may_wakeup(dev)) 221 disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); 222 else 223 enable_irq(gpio_to_irq(gpio_dev->gpio_nr)); 224 225 return 0; 226 } 227 228 static const struct dev_pm_ops gpio_ir_recv_pm_ops = { 229 .suspend = gpio_ir_recv_suspend, 230 .resume = gpio_ir_recv_resume, 231 }; 232 #endif 233 234 static struct platform_driver gpio_ir_recv_driver = { 235 .probe = gpio_ir_recv_probe, 236 .remove = gpio_ir_recv_remove, 237 .driver = { 238 .name = GPIO_IR_DRIVER_NAME, 239 .of_match_table = of_match_ptr(gpio_ir_recv_of_match), 240 #ifdef CONFIG_PM 241 .pm = &gpio_ir_recv_pm_ops, 242 #endif 243 }, 244 }; 245 module_platform_driver(gpio_ir_recv_driver); 246 247 MODULE_DESCRIPTION("GPIO IR Receiver driver"); 248 MODULE_LICENSE("GPL v2"); 249