Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 1 | /** |
| 2 | * drivers/extcon/extcon-usb-gpio.c - USB GPIO extcon driver |
| 3 | * |
| 4 | * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com |
| 5 | * Author: Roger Quadros <rogerq@ti.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/extcon.h> |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 18 | #include <linux/gpio.h> |
Geert Uytterhoeven | 638f958 | 2015-05-05 18:32:20 +0200 | [diff] [blame] | 19 | #include <linux/gpio/consumer.h> |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/irq.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/of_gpio.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/workqueue.h> |
Lu Baolu | 942c792 | 2016-04-26 08:35:54 +0900 | [diff] [blame] | 29 | #include <linux/acpi.h> |
Bo Zhang | 3905e7e | 2018-10-15 13:05:50 +0800 | [diff] [blame] | 30 | #include <linux/delay.h> |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 31 | |
| 32 | #define USB_GPIO_DEBOUNCE_MS 20 /* ms */ |
| 33 | |
| 34 | struct usb_extcon_info { |
| 35 | struct device *dev; |
| 36 | struct extcon_dev *edev; |
| 37 | |
| 38 | struct gpio_desc *id_gpiod; |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 39 | struct gpio_desc *vbus_gpiod; |
Bo Zhang | 3905e7e | 2018-10-15 13:05:50 +0800 | [diff] [blame] | 40 | struct gpio_desc *trig_gpiod; |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 41 | int id_irq; |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 42 | int vbus_irq; |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 43 | |
| 44 | unsigned long debounce_jiffies; |
| 45 | struct delayed_work wq_detcable; |
| 46 | }; |
| 47 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 48 | static const unsigned int usb_extcon_cable[] = { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 49 | EXTCON_USB, |
| 50 | EXTCON_USB_HOST, |
| 51 | EXTCON_NONE, |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 54 | /* |
| 55 | * "USB" = VBUS and "USB-HOST" = !ID, so we have: |
| 56 | * Both "USB" and "USB-HOST" can't be set as active at the |
| 57 | * same time so if "USB-HOST" is active (i.e. ID is 0) we keep "USB" inactive |
| 58 | * even if VBUS is on. |
| 59 | * |
| 60 | * State | ID | VBUS |
| 61 | * ---------------------------------------- |
| 62 | * [1] USB | H | H |
| 63 | * [2] none | H | L |
| 64 | * [3] USB-HOST | L | H |
| 65 | * [4] USB-HOST | L | L |
| 66 | * |
| 67 | * In case we have only one of these signals: |
| 68 | * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1. |
| 69 | * - ID only - we want to distinguish between [1] and [4], so VBUS = ID. |
| 70 | */ |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 71 | static void usb_extcon_detect_cable(struct work_struct *work) |
| 72 | { |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 73 | int id, vbus; |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 74 | struct usb_extcon_info *info = container_of(to_delayed_work(work), |
| 75 | struct usb_extcon_info, |
| 76 | wq_detcable); |
| 77 | |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 78 | /* check ID and VBUS and update cable state */ |
| 79 | id = info->id_gpiod ? |
| 80 | gpiod_get_value_cansleep(info->id_gpiod) : 1; |
| 81 | vbus = info->vbus_gpiod ? |
| 82 | gpiod_get_value_cansleep(info->vbus_gpiod) : id; |
| 83 | |
| 84 | /* at first we clean states which are no longer active */ |
| 85 | if (id) |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 86 | extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false); |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 87 | if (!vbus) |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 88 | extcon_set_state_sync(info->edev, EXTCON_USB, false); |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 89 | |
| 90 | if (!id) { |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 91 | extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true); |
Bo Zhang | 3905e7e | 2018-10-15 13:05:50 +0800 | [diff] [blame] | 92 | if (info->trig_gpiod) { |
| 93 | gpiod_set_value(info->trig_gpiod, 1); |
| 94 | msleep(20); |
| 95 | gpiod_set_value(info->trig_gpiod, 0); |
| 96 | msleep(20); |
| 97 | } |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 98 | } else { |
| 99 | if (vbus) |
| 100 | extcon_set_state_sync(info->edev, EXTCON_USB, true); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | static irqreturn_t usb_irq_handler(int irq, void *dev_id) |
| 105 | { |
| 106 | struct usb_extcon_info *info = dev_id; |
| 107 | |
| 108 | queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, |
| 109 | info->debounce_jiffies); |
| 110 | |
| 111 | return IRQ_HANDLED; |
| 112 | } |
| 113 | |
| 114 | static int usb_extcon_probe(struct platform_device *pdev) |
| 115 | { |
| 116 | struct device *dev = &pdev->dev; |
| 117 | struct device_node *np = dev->of_node; |
| 118 | struct usb_extcon_info *info; |
| 119 | int ret; |
| 120 | |
Lu Baolu | 942c792 | 2016-04-26 08:35:54 +0900 | [diff] [blame] | 121 | if (!np && !ACPI_HANDLE(dev)) |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 122 | return -EINVAL; |
| 123 | |
| 124 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 125 | if (!info) |
| 126 | return -ENOMEM; |
| 127 | |
| 128 | info->dev = dev; |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 129 | info->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id", GPIOD_IN); |
| 130 | info->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus", |
| 131 | GPIOD_IN); |
Bo Zhang | 3905e7e | 2018-10-15 13:05:50 +0800 | [diff] [blame] | 132 | info->trig_gpiod = devm_gpiod_get_optional(&pdev->dev, "trig", |
| 133 | GPIOD_OUT_LOW); |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 134 | |
| 135 | if (!info->id_gpiod && !info->vbus_gpiod) { |
| 136 | dev_err(dev, "failed to get gpios\n"); |
| 137 | return -ENODEV; |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 140 | if (IS_ERR(info->id_gpiod)) |
| 141 | return PTR_ERR(info->id_gpiod); |
| 142 | |
| 143 | if (IS_ERR(info->vbus_gpiod)) |
| 144 | return PTR_ERR(info->vbus_gpiod); |
| 145 | |
Bo Zhang | 3905e7e | 2018-10-15 13:05:50 +0800 | [diff] [blame] | 146 | if (IS_ERR(info->trig_gpiod)) |
| 147 | return PTR_ERR(info->trig_gpiod); |
| 148 | |
Robert Baldyga | bc1aaba | 2015-04-02 15:13:02 +0200 | [diff] [blame] | 149 | info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable); |
| 150 | if (IS_ERR(info->edev)) { |
| 151 | dev_err(dev, "failed to allocate extcon device\n"); |
| 152 | return -ENOMEM; |
| 153 | } |
| 154 | |
| 155 | ret = devm_extcon_dev_register(dev, info->edev); |
| 156 | if (ret < 0) { |
| 157 | dev_err(dev, "failed to register extcon device\n"); |
| 158 | return ret; |
| 159 | } |
| 160 | |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 161 | if (info->id_gpiod) |
| 162 | ret = gpiod_set_debounce(info->id_gpiod, |
| 163 | USB_GPIO_DEBOUNCE_MS * 1000); |
| 164 | if (!ret && info->vbus_gpiod) |
| 165 | ret = gpiod_set_debounce(info->vbus_gpiod, |
| 166 | USB_GPIO_DEBOUNCE_MS * 1000); |
| 167 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 168 | if (ret < 0) |
| 169 | info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS); |
| 170 | |
| 171 | INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable); |
| 172 | |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 173 | if (info->id_gpiod) { |
| 174 | info->id_irq = gpiod_to_irq(info->id_gpiod); |
| 175 | if (info->id_irq < 0) { |
| 176 | dev_err(dev, "failed to get ID IRQ\n"); |
| 177 | return info->id_irq; |
| 178 | } |
| 179 | |
| 180 | ret = devm_request_threaded_irq(dev, info->id_irq, NULL, |
| 181 | usb_irq_handler, |
| 182 | IRQF_TRIGGER_RISING | |
| 183 | IRQF_TRIGGER_FALLING | |
| 184 | IRQF_ONESHOT, |
| 185 | pdev->name, info); |
| 186 | if (ret < 0) { |
| 187 | dev_err(dev, "failed to request handler for ID IRQ\n"); |
| 188 | return ret; |
| 189 | } |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 192 | if (info->vbus_gpiod) { |
| 193 | info->vbus_irq = gpiod_to_irq(info->vbus_gpiod); |
| 194 | if (info->vbus_irq < 0) { |
| 195 | dev_err(dev, "failed to get VBUS IRQ\n"); |
| 196 | return info->vbus_irq; |
| 197 | } |
| 198 | |
| 199 | ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL, |
| 200 | usb_irq_handler, |
| 201 | IRQF_TRIGGER_RISING | |
| 202 | IRQF_TRIGGER_FALLING | |
| 203 | IRQF_ONESHOT, |
| 204 | pdev->name, info); |
| 205 | if (ret < 0) { |
| 206 | dev_err(dev, "failed to request handler for VBUS IRQ\n"); |
| 207 | return ret; |
| 208 | } |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 211 | platform_set_drvdata(pdev, info); |
Grygorii Strashko | 12bd0f3 | 2016-04-06 20:32:37 +0300 | [diff] [blame] | 212 | device_init_wakeup(dev, true); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 213 | |
Vijayavardhan Vennapusa | 7127550 | 2018-11-02 13:41:31 +0530 | [diff] [blame] | 214 | if (info->trig_gpiod) |
| 215 | /* Schedule with delay to reset ethernet bridge */ |
| 216 | queue_delayed_work(system_power_efficient_wq, |
| 217 | &info->wq_detcable, msecs_to_jiffies(1500)); |
| 218 | else |
| 219 | /* Perform initial detection */ |
| 220 | usb_extcon_detect_cable(&info->wq_detcable.work); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int usb_extcon_remove(struct platform_device *pdev) |
| 226 | { |
| 227 | struct usb_extcon_info *info = platform_get_drvdata(pdev); |
| 228 | |
| 229 | cancel_delayed_work_sync(&info->wq_detcable); |
Grygorii Strashko | 12bd0f3 | 2016-04-06 20:32:37 +0300 | [diff] [blame] | 230 | device_init_wakeup(&pdev->dev, false); |
| 231 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | #ifdef CONFIG_PM_SLEEP |
| 236 | static int usb_extcon_suspend(struct device *dev) |
| 237 | { |
| 238 | struct usb_extcon_info *info = dev_get_drvdata(dev); |
| 239 | int ret = 0; |
| 240 | |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 241 | if (device_may_wakeup(dev)) { |
| 242 | if (info->id_gpiod) { |
| 243 | ret = enable_irq_wake(info->id_irq); |
| 244 | if (ret) |
| 245 | return ret; |
| 246 | } |
| 247 | if (info->vbus_gpiod) { |
| 248 | ret = enable_irq_wake(info->vbus_irq); |
| 249 | if (ret) { |
| 250 | if (info->id_gpiod) |
| 251 | disable_irq_wake(info->id_irq); |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 258 | /* |
| 259 | * We don't want to process any IRQs after this point |
| 260 | * as GPIOs used behind I2C subsystem might not be |
| 261 | * accessible until resume completes. So disable IRQ. |
| 262 | */ |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 263 | if (info->id_gpiod) |
| 264 | disable_irq(info->id_irq); |
| 265 | if (info->vbus_gpiod) |
| 266 | disable_irq(info->vbus_irq); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static int usb_extcon_resume(struct device *dev) |
| 272 | { |
| 273 | struct usb_extcon_info *info = dev_get_drvdata(dev); |
| 274 | int ret = 0; |
| 275 | |
Roger Quadros | 1c93671 | 2016-09-20 17:53:55 +0300 | [diff] [blame] | 276 | if (device_may_wakeup(dev)) { |
| 277 | if (info->id_gpiod) { |
| 278 | ret = disable_irq_wake(info->id_irq); |
| 279 | if (ret) |
| 280 | return ret; |
| 281 | } |
| 282 | if (info->vbus_gpiod) { |
| 283 | ret = disable_irq_wake(info->vbus_irq); |
| 284 | if (ret) { |
| 285 | if (info->id_gpiod) |
| 286 | enable_irq_wake(info->id_irq); |
| 287 | |
| 288 | return ret; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (info->id_gpiod) |
| 294 | enable_irq(info->id_irq); |
| 295 | if (info->vbus_gpiod) |
| 296 | enable_irq(info->vbus_irq); |
| 297 | |
Sriharsha Allenki | 9a47773 | 2018-06-01 16:00:01 +0530 | [diff] [blame] | 298 | queue_delayed_work(system_power_efficient_wq, |
| 299 | &info->wq_detcable, 0); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | #endif |
| 304 | |
| 305 | static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops, |
| 306 | usb_extcon_suspend, usb_extcon_resume); |
| 307 | |
Chanwoo Choi | 34825e5 | 2015-03-07 01:41:36 +0900 | [diff] [blame] | 308 | static const struct of_device_id usb_extcon_dt_match[] = { |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 309 | { .compatible = "linux,extcon-usb-gpio", }, |
| 310 | { /* sentinel */ } |
| 311 | }; |
| 312 | MODULE_DEVICE_TABLE(of, usb_extcon_dt_match); |
| 313 | |
Lu Baolu | 058b665 | 2016-04-25 16:04:47 +0800 | [diff] [blame] | 314 | static const struct platform_device_id usb_extcon_platform_ids[] = { |
| 315 | { .name = "extcon-usb-gpio", }, |
| 316 | { /* sentinel */ } |
| 317 | }; |
| 318 | MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids); |
| 319 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 320 | static struct platform_driver usb_extcon_driver = { |
| 321 | .probe = usb_extcon_probe, |
| 322 | .remove = usb_extcon_remove, |
| 323 | .driver = { |
| 324 | .name = "extcon-usb-gpio", |
| 325 | .pm = &usb_extcon_pm_ops, |
| 326 | .of_match_table = usb_extcon_dt_match, |
| 327 | }, |
Lu Baolu | 058b665 | 2016-04-25 16:04:47 +0800 | [diff] [blame] | 328 | .id_table = usb_extcon_platform_ids, |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | module_platform_driver(usb_extcon_driver); |
| 332 | |
| 333 | MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>"); |
| 334 | MODULE_DESCRIPTION("USB GPIO extcon driver"); |
| 335 | MODULE_LICENSE("GPL v2"); |