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> |
Grygorii Strashko | 12bd0f3 | 2016-04-06 20:32:37 +0300 | [diff] [blame^] | 27 | #include <linux/pm_wakeirq.h> |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 28 | #include <linux/slab.h> |
| 29 | #include <linux/workqueue.h> |
| 30 | |
| 31 | #define USB_GPIO_DEBOUNCE_MS 20 /* ms */ |
| 32 | |
| 33 | struct usb_extcon_info { |
| 34 | struct device *dev; |
| 35 | struct extcon_dev *edev; |
| 36 | |
| 37 | struct gpio_desc *id_gpiod; |
| 38 | int id_irq; |
| 39 | |
| 40 | unsigned long debounce_jiffies; |
| 41 | struct delayed_work wq_detcable; |
| 42 | }; |
| 43 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 44 | static const unsigned int usb_extcon_cable[] = { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 45 | EXTCON_USB, |
| 46 | EXTCON_USB_HOST, |
| 47 | EXTCON_NONE, |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static void usb_extcon_detect_cable(struct work_struct *work) |
| 51 | { |
| 52 | int id; |
| 53 | struct usb_extcon_info *info = container_of(to_delayed_work(work), |
| 54 | struct usb_extcon_info, |
| 55 | wq_detcable); |
| 56 | |
| 57 | /* check ID and update cable state */ |
| 58 | id = gpiod_get_value_cansleep(info->id_gpiod); |
| 59 | if (id) { |
| 60 | /* |
| 61 | * ID = 1 means USB HOST cable detached. |
| 62 | * As we don't have event for USB peripheral cable attached, |
| 63 | * we simulate USB peripheral attach here. |
| 64 | */ |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 65 | extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, false); |
| 66 | extcon_set_cable_state_(info->edev, EXTCON_USB, true); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 67 | } else { |
| 68 | /* |
| 69 | * ID = 0 means USB HOST cable attached. |
| 70 | * As we don't have event for USB peripheral cable detached, |
| 71 | * we simulate USB peripheral detach here. |
| 72 | */ |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 73 | extcon_set_cable_state_(info->edev, EXTCON_USB, false); |
| 74 | extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, true); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | static irqreturn_t usb_irq_handler(int irq, void *dev_id) |
| 79 | { |
| 80 | struct usb_extcon_info *info = dev_id; |
| 81 | |
| 82 | queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, |
| 83 | info->debounce_jiffies); |
| 84 | |
| 85 | return IRQ_HANDLED; |
| 86 | } |
| 87 | |
| 88 | static int usb_extcon_probe(struct platform_device *pdev) |
| 89 | { |
| 90 | struct device *dev = &pdev->dev; |
| 91 | struct device_node *np = dev->of_node; |
| 92 | struct usb_extcon_info *info; |
| 93 | int ret; |
| 94 | |
| 95 | if (!np) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 99 | if (!info) |
| 100 | return -ENOMEM; |
| 101 | |
| 102 | info->dev = dev; |
Uwe Kleine-König | 35eed7a | 2015-05-19 14:18:30 +0200 | [diff] [blame] | 103 | info->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 104 | if (IS_ERR(info->id_gpiod)) { |
| 105 | dev_err(dev, "failed to get ID GPIO\n"); |
| 106 | return PTR_ERR(info->id_gpiod); |
| 107 | } |
| 108 | |
Robert Baldyga | bc1aaba | 2015-04-02 15:13:02 +0200 | [diff] [blame] | 109 | info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable); |
| 110 | if (IS_ERR(info->edev)) { |
| 111 | dev_err(dev, "failed to allocate extcon device\n"); |
| 112 | return -ENOMEM; |
| 113 | } |
| 114 | |
| 115 | ret = devm_extcon_dev_register(dev, info->edev); |
| 116 | if (ret < 0) { |
| 117 | dev_err(dev, "failed to register extcon device\n"); |
| 118 | return ret; |
| 119 | } |
| 120 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 121 | ret = gpiod_set_debounce(info->id_gpiod, |
| 122 | USB_GPIO_DEBOUNCE_MS * 1000); |
| 123 | if (ret < 0) |
| 124 | info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS); |
| 125 | |
| 126 | INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable); |
| 127 | |
| 128 | info->id_irq = gpiod_to_irq(info->id_gpiod); |
| 129 | if (info->id_irq < 0) { |
| 130 | dev_err(dev, "failed to get ID IRQ\n"); |
| 131 | return info->id_irq; |
| 132 | } |
| 133 | |
| 134 | ret = devm_request_threaded_irq(dev, info->id_irq, NULL, |
| 135 | usb_irq_handler, |
| 136 | IRQF_TRIGGER_RISING | |
| 137 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 138 | pdev->name, info); |
| 139 | if (ret < 0) { |
| 140 | dev_err(dev, "failed to request handler for ID IRQ\n"); |
| 141 | return ret; |
| 142 | } |
| 143 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 144 | platform_set_drvdata(pdev, info); |
Grygorii Strashko | 12bd0f3 | 2016-04-06 20:32:37 +0300 | [diff] [blame^] | 145 | device_init_wakeup(dev, true); |
| 146 | dev_pm_set_wake_irq(dev, info->id_irq); |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 147 | |
| 148 | /* Perform initial detection */ |
| 149 | usb_extcon_detect_cable(&info->wq_detcable.work); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int usb_extcon_remove(struct platform_device *pdev) |
| 155 | { |
| 156 | struct usb_extcon_info *info = platform_get_drvdata(pdev); |
| 157 | |
| 158 | cancel_delayed_work_sync(&info->wq_detcable); |
| 159 | |
Grygorii Strashko | 12bd0f3 | 2016-04-06 20:32:37 +0300 | [diff] [blame^] | 160 | dev_pm_clear_wake_irq(&pdev->dev); |
| 161 | device_init_wakeup(&pdev->dev, false); |
| 162 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | #ifdef CONFIG_PM_SLEEP |
| 167 | static int usb_extcon_suspend(struct device *dev) |
| 168 | { |
| 169 | struct usb_extcon_info *info = dev_get_drvdata(dev); |
| 170 | int ret = 0; |
| 171 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 172 | /* |
| 173 | * We don't want to process any IRQs after this point |
| 174 | * as GPIOs used behind I2C subsystem might not be |
| 175 | * accessible until resume completes. So disable IRQ. |
| 176 | */ |
| 177 | disable_irq(info->id_irq); |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static int usb_extcon_resume(struct device *dev) |
| 183 | { |
| 184 | struct usb_extcon_info *info = dev_get_drvdata(dev); |
| 185 | int ret = 0; |
| 186 | |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 187 | enable_irq(info->id_irq); |
| 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | #endif |
| 192 | |
| 193 | static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops, |
| 194 | usb_extcon_suspend, usb_extcon_resume); |
| 195 | |
Chanwoo Choi | 34825e5 | 2015-03-07 01:41:36 +0900 | [diff] [blame] | 196 | static const struct of_device_id usb_extcon_dt_match[] = { |
Roger Quadros | e52817f | 2015-02-02 12:21:59 +0200 | [diff] [blame] | 197 | { .compatible = "linux,extcon-usb-gpio", }, |
| 198 | { /* sentinel */ } |
| 199 | }; |
| 200 | MODULE_DEVICE_TABLE(of, usb_extcon_dt_match); |
| 201 | |
| 202 | static struct platform_driver usb_extcon_driver = { |
| 203 | .probe = usb_extcon_probe, |
| 204 | .remove = usb_extcon_remove, |
| 205 | .driver = { |
| 206 | .name = "extcon-usb-gpio", |
| 207 | .pm = &usb_extcon_pm_ops, |
| 208 | .of_match_table = usb_extcon_dt_match, |
| 209 | }, |
| 210 | }; |
| 211 | |
| 212 | module_platform_driver(usb_extcon_driver); |
| 213 | |
| 214 | MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>"); |
| 215 | MODULE_DESCRIPTION("USB GPIO extcon driver"); |
| 216 | MODULE_LICENSE("GPL v2"); |