Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Palmas USB transceiver driver |
| 4 | * |
Alexander A. Klimov | f6dfb3c | 2020-07-13 15:19:53 +0200 | [diff] [blame] | 5 | * Copyright (C) 2013 Texas Instruments Incorporated - https://www.ti.com |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 6 | * Author: Graeme Gregory <gg@slimlogic.co.uk> |
| 7 | * Author: Kishon Vijay Abraham I <kishon@ti.com> |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 8 | * Based on twl6030_usb.c |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 9 | * Author: Hema HK <hemahk@ti.com> |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 10 | */ |
| 11 | |
Matti Vaittinen | f94a5be | 2021-03-23 15:57:08 +0200 | [diff] [blame] | 12 | #include <linux/devm-helpers.h> |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/platform_device.h> |
Kishon Vijay Abraham I | f63d32d | 2014-03-28 12:21:02 +0530 | [diff] [blame] | 16 | #include <linux/slab.h> |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 17 | #include <linux/err.h> |
| 18 | #include <linux/mfd/palmas.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_platform.h> |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 21 | #include <linux/of_gpio.h> |
Chanwoo Choi | ac22a1d | 2015-08-13 09:19:43 +0900 | [diff] [blame] | 22 | #include <linux/gpio/consumer.h> |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 23 | #include <linux/workqueue.h> |
| 24 | |
| 25 | #define USB_GPIO_DEBOUNCE_MS 20 /* ms */ |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 26 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 27 | static const unsigned int palmas_extcon_cable[] = { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 28 | EXTCON_USB, |
| 29 | EXTCON_USB_HOST, |
| 30 | EXTCON_NONE, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 31 | }; |
| 32 | |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 33 | static void palmas_usb_wakeup(struct palmas *palmas, int enable) |
| 34 | { |
| 35 | if (enable) |
| 36 | palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP, |
| 37 | PALMAS_USB_WAKEUP_ID_WK_UP_COMP); |
| 38 | else |
| 39 | palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP, 0); |
| 40 | } |
| 41 | |
| 42 | static irqreturn_t palmas_vbus_irq_handler(int irq, void *_palmas_usb) |
| 43 | { |
| 44 | struct palmas_usb *palmas_usb = _palmas_usb; |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 45 | struct extcon_dev *edev = palmas_usb->edev; |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 46 | unsigned int vbus_line_state; |
| 47 | |
| 48 | palmas_read(palmas_usb->palmas, PALMAS_INTERRUPT_BASE, |
| 49 | PALMAS_INT3_LINE_STATE, &vbus_line_state); |
| 50 | |
| 51 | if (vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS) { |
| 52 | if (palmas_usb->linkstat != PALMAS_USB_STATE_VBUS) { |
| 53 | palmas_usb->linkstat = PALMAS_USB_STATE_VBUS; |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 54 | extcon_set_state_sync(edev, EXTCON_USB, true); |
Roger Quadros | 567ab5a | 2017-01-24 16:54:07 +0200 | [diff] [blame] | 55 | dev_dbg(palmas_usb->dev, "USB cable is attached\n"); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 56 | } else { |
| 57 | dev_dbg(palmas_usb->dev, |
| 58 | "Spurious connect event detected\n"); |
| 59 | } |
| 60 | } else if (!(vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS)) { |
| 61 | if (palmas_usb->linkstat == PALMAS_USB_STATE_VBUS) { |
| 62 | palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT; |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 63 | extcon_set_state_sync(edev, EXTCON_USB, false); |
Roger Quadros | 567ab5a | 2017-01-24 16:54:07 +0200 | [diff] [blame] | 64 | dev_dbg(palmas_usb->dev, "USB cable is detached\n"); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 65 | } else { |
| 66 | dev_dbg(palmas_usb->dev, |
| 67 | "Spurious disconnect event detected\n"); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return IRQ_HANDLED; |
| 72 | } |
| 73 | |
| 74 | static irqreturn_t palmas_id_irq_handler(int irq, void *_palmas_usb) |
| 75 | { |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 76 | unsigned int set, id_src; |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 77 | struct palmas_usb *palmas_usb = _palmas_usb; |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 78 | struct extcon_dev *edev = palmas_usb->edev; |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 79 | |
| 80 | palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 81 | PALMAS_USB_ID_INT_LATCH_SET, &set); |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 82 | palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 83 | PALMAS_USB_ID_INT_SRC, &id_src); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 84 | |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 85 | if ((set & PALMAS_USB_ID_INT_SRC_ID_GND) && |
| 86 | (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) { |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 87 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 88 | PALMAS_USB_ID_INT_LATCH_CLR, |
| 89 | PALMAS_USB_ID_INT_EN_HI_CLR_ID_GND); |
| 90 | palmas_usb->linkstat = PALMAS_USB_STATE_ID; |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 91 | extcon_set_state_sync(edev, EXTCON_USB_HOST, true); |
Roger Quadros | 567ab5a | 2017-01-24 16:54:07 +0200 | [diff] [blame] | 92 | dev_dbg(palmas_usb->dev, "USB-HOST cable is attached\n"); |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 93 | } else if ((set & PALMAS_USB_ID_INT_SRC_ID_FLOAT) && |
| 94 | (id_src & PALMAS_USB_ID_INT_SRC_ID_FLOAT)) { |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 95 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 96 | PALMAS_USB_ID_INT_LATCH_CLR, |
| 97 | PALMAS_USB_ID_INT_EN_HI_CLR_ID_FLOAT); |
| 98 | palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT; |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 99 | extcon_set_state_sync(edev, EXTCON_USB_HOST, false); |
Roger Quadros | 567ab5a | 2017-01-24 16:54:07 +0200 | [diff] [blame] | 100 | dev_dbg(palmas_usb->dev, "USB-HOST cable is detached\n"); |
Laxman Dewangan | 002945f | 2013-07-09 18:34:22 +0530 | [diff] [blame] | 101 | } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_ID) && |
| 102 | (!(set & PALMAS_USB_ID_INT_SRC_ID_GND))) { |
| 103 | palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT; |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 104 | extcon_set_state_sync(edev, EXTCON_USB_HOST, false); |
Roger Quadros | 567ab5a | 2017-01-24 16:54:07 +0200 | [diff] [blame] | 105 | dev_dbg(palmas_usb->dev, "USB-HOST cable is detached\n"); |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 106 | } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_DISCONNECT) && |
| 107 | (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) { |
| 108 | palmas_usb->linkstat = PALMAS_USB_STATE_ID; |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 109 | extcon_set_state_sync(edev, EXTCON_USB_HOST, true); |
Roger Quadros | 567ab5a | 2017-01-24 16:54:07 +0200 | [diff] [blame] | 110 | dev_dbg(palmas_usb->dev, " USB-HOST cable is attached\n"); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return IRQ_HANDLED; |
| 114 | } |
| 115 | |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 116 | static void palmas_gpio_id_detect(struct work_struct *work) |
| 117 | { |
| 118 | int id; |
| 119 | struct palmas_usb *palmas_usb = container_of(to_delayed_work(work), |
| 120 | struct palmas_usb, |
| 121 | wq_detectid); |
| 122 | struct extcon_dev *edev = palmas_usb->edev; |
| 123 | |
| 124 | if (!palmas_usb->id_gpiod) |
| 125 | return; |
| 126 | |
| 127 | id = gpiod_get_value_cansleep(palmas_usb->id_gpiod); |
| 128 | |
| 129 | if (id) { |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 130 | extcon_set_state_sync(edev, EXTCON_USB_HOST, false); |
Roger Quadros | 567ab5a | 2017-01-24 16:54:07 +0200 | [diff] [blame] | 131 | dev_dbg(palmas_usb->dev, "USB-HOST cable is detached\n"); |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 132 | } else { |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 133 | extcon_set_state_sync(edev, EXTCON_USB_HOST, true); |
Roger Quadros | 567ab5a | 2017-01-24 16:54:07 +0200 | [diff] [blame] | 134 | dev_dbg(palmas_usb->dev, "USB-HOST cable is attached\n"); |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | static irqreturn_t palmas_gpio_id_irq_handler(int irq, void *_palmas_usb) |
| 139 | { |
| 140 | struct palmas_usb *palmas_usb = _palmas_usb; |
| 141 | |
| 142 | queue_delayed_work(system_power_efficient_wq, &palmas_usb->wq_detectid, |
| 143 | palmas_usb->sw_debounce_jiffies); |
| 144 | |
| 145 | return IRQ_HANDLED; |
| 146 | } |
| 147 | |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 148 | static void palmas_enable_irq(struct palmas_usb *palmas_usb) |
| 149 | { |
| 150 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 151 | PALMAS_USB_VBUS_CTRL_SET, |
| 152 | PALMAS_USB_VBUS_CTRL_SET_VBUS_ACT_COMP); |
| 153 | |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 154 | if (palmas_usb->enable_id_detection) { |
| 155 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 156 | PALMAS_USB_ID_CTRL_SET, |
| 157 | PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 158 | |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 159 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 160 | PALMAS_USB_ID_INT_EN_HI_SET, |
| 161 | PALMAS_USB_ID_INT_EN_HI_SET_ID_GND | |
| 162 | PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT); |
| 163 | } |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 164 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 165 | if (palmas_usb->enable_vbus_detection) |
| 166 | palmas_vbus_irq_handler(palmas_usb->vbus_irq, palmas_usb); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 167 | |
| 168 | /* cold plug for host mode needs this delay */ |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 169 | if (palmas_usb->enable_id_detection) { |
| 170 | msleep(30); |
| 171 | palmas_id_irq_handler(palmas_usb->id_irq, palmas_usb); |
| 172 | } |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static int palmas_usb_probe(struct platform_device *pdev) |
| 176 | { |
| 177 | struct palmas *palmas = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | 7c0f6558 | 2013-09-11 13:22:18 +0900 | [diff] [blame] | 178 | struct palmas_usb_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 179 | struct device_node *node = pdev->dev.of_node; |
| 180 | struct palmas_usb *palmas_usb; |
| 181 | int status; |
| 182 | |
Roger Quadros | 9fe172b | 2016-12-08 10:45:31 +0200 | [diff] [blame] | 183 | if (!palmas) { |
| 184 | dev_err(&pdev->dev, "failed to get valid parent\n"); |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 188 | palmas_usb = devm_kzalloc(&pdev->dev, sizeof(*palmas_usb), GFP_KERNEL); |
| 189 | if (!palmas_usb) |
| 190 | return -ENOMEM; |
| 191 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 192 | if (node && !pdata) { |
| 193 | palmas_usb->wakeup = of_property_read_bool(node, "ti,wakeup"); |
| 194 | palmas_usb->enable_id_detection = of_property_read_bool(node, |
| 195 | "ti,enable-id-detection"); |
| 196 | palmas_usb->enable_vbus_detection = of_property_read_bool(node, |
| 197 | "ti,enable-vbus-detection"); |
| 198 | } else { |
| 199 | palmas_usb->wakeup = true; |
| 200 | palmas_usb->enable_id_detection = true; |
| 201 | palmas_usb->enable_vbus_detection = true; |
| 202 | |
| 203 | if (pdata) |
| 204 | palmas_usb->wakeup = pdata->wakeup; |
| 205 | } |
| 206 | |
Chanwoo Choi | ac22a1d | 2015-08-13 09:19:43 +0900 | [diff] [blame] | 207 | palmas_usb->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id", |
| 208 | GPIOD_IN); |
Krzysztof Kozlowski | d0f6681 | 2020-08-28 17:13:37 +0200 | [diff] [blame] | 209 | if (IS_ERR(palmas_usb->id_gpiod)) |
| 210 | return dev_err_probe(&pdev->dev, PTR_ERR(palmas_usb->id_gpiod), |
| 211 | "failed to get id gpio\n"); |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 212 | |
Felipe Balbi | b7aad8e | 2015-12-30 09:55:45 +0900 | [diff] [blame] | 213 | palmas_usb->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus", |
| 214 | GPIOD_IN); |
Krzysztof Kozlowski | d0f6681 | 2020-08-28 17:13:37 +0200 | [diff] [blame] | 215 | if (IS_ERR(palmas_usb->vbus_gpiod)) |
| 216 | return dev_err_probe(&pdev->dev, PTR_ERR(palmas_usb->vbus_gpiod), |
| 217 | "failed to get id gpio\n"); |
Felipe Balbi | b7aad8e | 2015-12-30 09:55:45 +0900 | [diff] [blame] | 218 | |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 219 | if (palmas_usb->enable_id_detection && palmas_usb->id_gpiod) { |
| 220 | palmas_usb->enable_id_detection = false; |
| 221 | palmas_usb->enable_gpio_id_detection = true; |
| 222 | } |
| 223 | |
Felipe Balbi | b7aad8e | 2015-12-30 09:55:45 +0900 | [diff] [blame] | 224 | if (palmas_usb->enable_vbus_detection && palmas_usb->vbus_gpiod) { |
| 225 | palmas_usb->enable_vbus_detection = false; |
| 226 | palmas_usb->enable_gpio_vbus_detection = true; |
| 227 | } |
| 228 | |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 229 | if (palmas_usb->enable_gpio_id_detection) { |
| 230 | u32 debounce; |
| 231 | |
| 232 | if (of_property_read_u32(node, "debounce-delay-ms", &debounce)) |
| 233 | debounce = USB_GPIO_DEBOUNCE_MS; |
| 234 | |
| 235 | status = gpiod_set_debounce(palmas_usb->id_gpiod, |
| 236 | debounce * 1000); |
| 237 | if (status < 0) |
| 238 | palmas_usb->sw_debounce_jiffies = msecs_to_jiffies(debounce); |
| 239 | } |
| 240 | |
Matti Vaittinen | f94a5be | 2021-03-23 15:57:08 +0200 | [diff] [blame] | 241 | status = devm_delayed_work_autocancel(&pdev->dev, |
| 242 | &palmas_usb->wq_detectid, |
| 243 | palmas_gpio_id_detect); |
| 244 | if (status) |
| 245 | return status; |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 246 | |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 247 | palmas->usb = palmas_usb; |
| 248 | palmas_usb->palmas = palmas; |
| 249 | |
| 250 | palmas_usb->dev = &pdev->dev; |
| 251 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 252 | palmas_usb_wakeup(palmas, palmas_usb->wakeup); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 253 | |
| 254 | platform_set_drvdata(pdev, palmas_usb); |
| 255 | |
Chanwoo Choi | 3f79a3f | 2014-04-21 20:44:53 +0900 | [diff] [blame] | 256 | palmas_usb->edev = devm_extcon_dev_allocate(&pdev->dev, |
| 257 | palmas_extcon_cable); |
| 258 | if (IS_ERR(palmas_usb->edev)) { |
| 259 | dev_err(&pdev->dev, "failed to allocate extcon device\n"); |
| 260 | return -ENOMEM; |
| 261 | } |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 262 | |
Chanwoo Choi | 3f79a3f | 2014-04-21 20:44:53 +0900 | [diff] [blame] | 263 | status = devm_extcon_dev_register(&pdev->dev, palmas_usb->edev); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 264 | if (status) { |
| 265 | dev_err(&pdev->dev, "failed to register extcon device\n"); |
| 266 | return status; |
| 267 | } |
| 268 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 269 | if (palmas_usb->enable_id_detection) { |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 270 | palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data, |
| 271 | PALMAS_ID_OTG_IRQ); |
| 272 | palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data, |
| 273 | PALMAS_ID_IRQ); |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 274 | status = devm_request_threaded_irq(palmas_usb->dev, |
| 275 | palmas_usb->id_irq, |
| 276 | NULL, palmas_id_irq_handler, |
| 277 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | |
Grygorii Strashko | ae64e42 | 2016-02-26 17:42:51 +0200 | [diff] [blame] | 278 | IRQF_ONESHOT, |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 279 | "palmas_usb_id", palmas_usb); |
| 280 | if (status < 0) { |
| 281 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 282 | palmas_usb->id_irq, status); |
Sangjung Woo | b4dad55 | 2014-04-21 19:10:14 +0900 | [diff] [blame] | 283 | return status; |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 284 | } |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 285 | } else if (palmas_usb->enable_gpio_id_detection) { |
| 286 | palmas_usb->gpio_id_irq = gpiod_to_irq(palmas_usb->id_gpiod); |
| 287 | if (palmas_usb->gpio_id_irq < 0) { |
| 288 | dev_err(&pdev->dev, "failed to get id irq\n"); |
| 289 | return palmas_usb->gpio_id_irq; |
| 290 | } |
| 291 | status = devm_request_threaded_irq(&pdev->dev, |
| 292 | palmas_usb->gpio_id_irq, |
| 293 | NULL, |
| 294 | palmas_gpio_id_irq_handler, |
| 295 | IRQF_TRIGGER_RISING | |
| 296 | IRQF_TRIGGER_FALLING | |
| 297 | IRQF_ONESHOT, |
| 298 | "palmas_usb_id", |
| 299 | palmas_usb); |
| 300 | if (status < 0) { |
| 301 | dev_err(&pdev->dev, |
| 302 | "failed to request handler for id irq\n"); |
| 303 | return status; |
| 304 | } |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 305 | } |
| 306 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 307 | if (palmas_usb->enable_vbus_detection) { |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 308 | palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data, |
| 309 | PALMAS_VBUS_OTG_IRQ); |
| 310 | palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data, |
| 311 | PALMAS_VBUS_IRQ); |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 312 | status = devm_request_threaded_irq(palmas_usb->dev, |
| 313 | palmas_usb->vbus_irq, NULL, |
| 314 | palmas_vbus_irq_handler, |
| 315 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | |
Grygorii Strashko | ae64e42 | 2016-02-26 17:42:51 +0200 | [diff] [blame] | 316 | IRQF_ONESHOT, |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 317 | "palmas_usb_vbus", palmas_usb); |
| 318 | if (status < 0) { |
| 319 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 320 | palmas_usb->vbus_irq, status); |
Sangjung Woo | b4dad55 | 2014-04-21 19:10:14 +0900 | [diff] [blame] | 321 | return status; |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 322 | } |
Felipe Balbi | b7aad8e | 2015-12-30 09:55:45 +0900 | [diff] [blame] | 323 | } else if (palmas_usb->enable_gpio_vbus_detection) { |
| 324 | /* remux GPIO_1 as VBUSDET */ |
| 325 | status = palmas_update_bits(palmas, |
| 326 | PALMAS_PU_PD_OD_BASE, |
| 327 | PALMAS_PRIMARY_SECONDARY_PAD1, |
| 328 | PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK, |
| 329 | (1 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT)); |
| 330 | if (status < 0) { |
| 331 | dev_err(&pdev->dev, "can't remux GPIO1\n"); |
| 332 | return status; |
| 333 | } |
| 334 | |
| 335 | palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data, |
| 336 | PALMAS_VBUS_OTG_IRQ); |
| 337 | palmas_usb->gpio_vbus_irq = gpiod_to_irq(palmas_usb->vbus_gpiod); |
| 338 | if (palmas_usb->gpio_vbus_irq < 0) { |
| 339 | dev_err(&pdev->dev, "failed to get vbus irq\n"); |
| 340 | return palmas_usb->gpio_vbus_irq; |
| 341 | } |
| 342 | status = devm_request_threaded_irq(&pdev->dev, |
| 343 | palmas_usb->gpio_vbus_irq, |
| 344 | NULL, |
| 345 | palmas_vbus_irq_handler, |
| 346 | IRQF_TRIGGER_FALLING | |
| 347 | IRQF_TRIGGER_RISING | |
Nishanth Menon | f75587b | 2016-04-01 08:52:47 -0500 | [diff] [blame] | 348 | IRQF_ONESHOT, |
Felipe Balbi | b7aad8e | 2015-12-30 09:55:45 +0900 | [diff] [blame] | 349 | "palmas_usb_vbus", |
| 350 | palmas_usb); |
| 351 | if (status < 0) { |
| 352 | dev_err(&pdev->dev, |
| 353 | "failed to request handler for vbus irq\n"); |
| 354 | return status; |
| 355 | } |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | palmas_enable_irq(palmas_usb); |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 359 | /* perform initial detection */ |
Roger Quadros | 62e6d1e | 2016-06-15 11:12:05 +0300 | [diff] [blame] | 360 | if (palmas_usb->enable_gpio_vbus_detection) |
| 361 | palmas_vbus_irq_handler(palmas_usb->gpio_vbus_irq, palmas_usb); |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 362 | palmas_gpio_id_detect(&palmas_usb->wq_detectid.work); |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 363 | device_set_wakeup_capable(&pdev->dev, true); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 364 | return 0; |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 365 | } |
| 366 | |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 367 | #ifdef CONFIG_PM_SLEEP |
| 368 | static int palmas_usb_suspend(struct device *dev) |
| 369 | { |
| 370 | struct palmas_usb *palmas_usb = dev_get_drvdata(dev); |
| 371 | |
| 372 | if (device_may_wakeup(dev)) { |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 373 | if (palmas_usb->enable_vbus_detection) |
| 374 | enable_irq_wake(palmas_usb->vbus_irq); |
Felipe Balbi | b7aad8e | 2015-12-30 09:55:45 +0900 | [diff] [blame] | 375 | if (palmas_usb->enable_gpio_vbus_detection) |
| 376 | enable_irq_wake(palmas_usb->gpio_vbus_irq); |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 377 | if (palmas_usb->enable_id_detection) |
| 378 | enable_irq_wake(palmas_usb->id_irq); |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 379 | if (palmas_usb->enable_gpio_id_detection) |
| 380 | enable_irq_wake(palmas_usb->gpio_id_irq); |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | static int palmas_usb_resume(struct device *dev) |
| 386 | { |
| 387 | struct palmas_usb *palmas_usb = dev_get_drvdata(dev); |
| 388 | |
| 389 | if (device_may_wakeup(dev)) { |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 390 | if (palmas_usb->enable_vbus_detection) |
| 391 | disable_irq_wake(palmas_usb->vbus_irq); |
Felipe Balbi | b7aad8e | 2015-12-30 09:55:45 +0900 | [diff] [blame] | 392 | if (palmas_usb->enable_gpio_vbus_detection) |
| 393 | disable_irq_wake(palmas_usb->gpio_vbus_irq); |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 394 | if (palmas_usb->enable_id_detection) |
| 395 | disable_irq_wake(palmas_usb->id_irq); |
Roger Quadros | 92b7cb5 | 2015-08-03 17:40:51 +0300 | [diff] [blame] | 396 | if (palmas_usb->enable_gpio_id_detection) |
| 397 | disable_irq_wake(palmas_usb->gpio_id_irq); |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 398 | } |
Roger Quadros | 0194432 | 2017-02-15 14:31:29 +0200 | [diff] [blame] | 399 | |
| 400 | /* check if GPIO states changed while suspend/resume */ |
| 401 | if (palmas_usb->enable_gpio_vbus_detection) |
| 402 | palmas_vbus_irq_handler(palmas_usb->gpio_vbus_irq, palmas_usb); |
| 403 | palmas_gpio_id_detect(&palmas_usb->wq_detectid.work); |
| 404 | |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 405 | return 0; |
| 406 | }; |
| 407 | #endif |
| 408 | |
Jingoo Han | 8d4288f | 2014-02-27 20:36:15 +0900 | [diff] [blame] | 409 | static SIMPLE_DEV_PM_OPS(palmas_pm_ops, palmas_usb_suspend, palmas_usb_resume); |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 410 | |
Krzysztof Kozlowski | 3bc2ac7 | 2014-05-23 11:03:05 +0200 | [diff] [blame] | 411 | static const struct of_device_id of_palmas_match_tbl[] = { |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 412 | { .compatible = "ti,palmas-usb", }, |
Kishon Vijay Abraham I | 5fbc77d | 2013-10-10 18:07:27 +0530 | [diff] [blame] | 413 | { .compatible = "ti,palmas-usb-vid", }, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 414 | { .compatible = "ti,twl6035-usb", }, |
Kishon Vijay Abraham I | 5fbc77d | 2013-10-10 18:07:27 +0530 | [diff] [blame] | 415 | { .compatible = "ti,twl6035-usb-vid", }, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 416 | { /* end */ } |
| 417 | }; |
| 418 | |
| 419 | static struct platform_driver palmas_usb_driver = { |
| 420 | .probe = palmas_usb_probe, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 421 | .driver = { |
| 422 | .name = "palmas-usb", |
| 423 | .of_match_table = of_palmas_match_tbl, |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 424 | .pm = &palmas_pm_ops, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 425 | }, |
| 426 | }; |
| 427 | |
| 428 | module_platform_driver(palmas_usb_driver); |
| 429 | |
| 430 | MODULE_ALIAS("platform:palmas-usb"); |
| 431 | MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>"); |
| 432 | MODULE_DESCRIPTION("Palmas USB transceiver driver"); |
| 433 | MODULE_LICENSE("GPL"); |
| 434 | MODULE_DEVICE_TABLE(of, of_palmas_match_tbl); |