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