Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [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> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <linux/mfd/palmas.h> |
| 29 | #include <linux/of.h> |
| 30 | #include <linux/of_platform.h> |
| 31 | #include <linux/of_gpio.h> |
| 32 | #include <linux/gpio/consumer.h> |
| 33 | #include <linux/workqueue.h> |
| 34 | |
| 35 | #define USB_GPIO_DEBOUNCE_MS 20 /* ms */ |
| 36 | |
| 37 | static const unsigned int palmas_extcon_cable[] = { |
| 38 | EXTCON_USB, |
| 39 | EXTCON_USB_HOST, |
| 40 | EXTCON_NONE, |
| 41 | }; |
| 42 | |
| 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; |
| 55 | struct extcon_dev *edev = palmas_usb->edev; |
| 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; |
| 64 | extcon_set_cable_state_(edev, EXTCON_USB, true); |
| 65 | dev_info(palmas_usb->dev, "USB cable is attached\n"); |
| 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; |
| 73 | extcon_set_cable_state_(edev, EXTCON_USB, false); |
| 74 | dev_info(palmas_usb->dev, "USB cable is detached\n"); |
| 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 | { |
| 86 | unsigned int set, id_src; |
| 87 | struct palmas_usb *palmas_usb = _palmas_usb; |
| 88 | struct extcon_dev *edev = palmas_usb->edev; |
| 89 | |
| 90 | palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 91 | PALMAS_USB_ID_INT_LATCH_SET, &set); |
| 92 | palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 93 | PALMAS_USB_ID_INT_SRC, &id_src); |
| 94 | |
| 95 | if ((set & PALMAS_USB_ID_INT_SRC_ID_GND) && |
| 96 | (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) { |
| 97 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 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; |
| 101 | extcon_set_cable_state_(edev, EXTCON_USB_HOST, true); |
| 102 | dev_info(palmas_usb->dev, "USB-HOST cable is attached\n"); |
| 103 | } else if ((set & PALMAS_USB_ID_INT_SRC_ID_FLOAT) && |
| 104 | (id_src & PALMAS_USB_ID_INT_SRC_ID_FLOAT)) { |
| 105 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 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; |
| 109 | extcon_set_cable_state_(edev, EXTCON_USB_HOST, false); |
| 110 | dev_info(palmas_usb->dev, "USB-HOST cable is detached\n"); |
| 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; |
| 114 | extcon_set_cable_state_(edev, EXTCON_USB_HOST, false); |
| 115 | dev_info(palmas_usb->dev, "USB-HOST cable is detached\n"); |
| 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; |
| 119 | extcon_set_cable_state_(edev, EXTCON_USB_HOST, true); |
| 120 | dev_info(palmas_usb->dev, " USB-HOST cable is attached\n"); |
| 121 | } |
| 122 | |
| 123 | return IRQ_HANDLED; |
| 124 | } |
| 125 | |
| 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) { |
| 140 | extcon_set_cable_state_(edev, EXTCON_USB_HOST, false); |
| 141 | dev_info(palmas_usb->dev, "USB-HOST cable is detached\n"); |
| 142 | } else { |
| 143 | extcon_set_cable_state_(edev, EXTCON_USB_HOST, true); |
| 144 | dev_info(palmas_usb->dev, "USB-HOST cable is attached\n"); |
| 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 | |
| 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 | |
| 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); |
| 168 | |
| 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 | } |
| 174 | |
| 175 | if (palmas_usb->enable_vbus_detection) |
| 176 | palmas_vbus_irq_handler(palmas_usb->vbus_irq, palmas_usb); |
| 177 | |
| 178 | /* cold plug for host mode needs this delay */ |
| 179 | if (palmas_usb->enable_id_detection) { |
| 180 | msleep(30); |
| 181 | palmas_id_irq_handler(palmas_usb->id_irq, palmas_usb); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static int palmas_usb_probe(struct platform_device *pdev) |
| 186 | { |
| 187 | struct palmas *palmas = dev_get_drvdata(pdev->dev.parent); |
| 188 | struct palmas_usb_platform_data *pdata = dev_get_platdata(&pdev->dev); |
| 189 | struct device_node *node = pdev->dev.of_node; |
| 190 | struct palmas_usb *palmas_usb; |
| 191 | int status; |
| 192 | |
| 193 | if (!palmas) { |
| 194 | dev_err(&pdev->dev, "failed to get valid parent\n"); |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | palmas_usb = devm_kzalloc(&pdev->dev, sizeof(*palmas_usb), GFP_KERNEL); |
| 199 | if (!palmas_usb) |
| 200 | return -ENOMEM; |
| 201 | |
| 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 | |
| 217 | palmas_usb->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id", |
| 218 | GPIOD_IN); |
| 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 | |
| 224 | if (palmas_usb->enable_id_detection && palmas_usb->id_gpiod) { |
| 225 | palmas_usb->enable_id_detection = false; |
| 226 | palmas_usb->enable_gpio_id_detection = true; |
| 227 | } |
| 228 | |
| 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 | |
| 241 | INIT_DELAYED_WORK(&palmas_usb->wq_detectid, palmas_gpio_id_detect); |
| 242 | |
| 243 | palmas->usb = palmas_usb; |
| 244 | palmas_usb->palmas = palmas; |
| 245 | |
| 246 | palmas_usb->dev = &pdev->dev; |
| 247 | |
| 248 | palmas_usb_wakeup(palmas, palmas_usb->wakeup); |
| 249 | |
| 250 | platform_set_drvdata(pdev, palmas_usb); |
| 251 | |
| 252 | palmas_usb->edev = devm_extcon_dev_allocate(&pdev->dev, |
| 253 | palmas_extcon_cable); |
| 254 | if (IS_ERR(palmas_usb->edev)) { |
| 255 | dev_err(&pdev->dev, "failed to allocate extcon device\n"); |
| 256 | return -ENOMEM; |
| 257 | } |
| 258 | |
| 259 | status = devm_extcon_dev_register(&pdev->dev, palmas_usb->edev); |
| 260 | if (status) { |
| 261 | dev_err(&pdev->dev, "failed to register extcon device\n"); |
| 262 | return status; |
| 263 | } |
| 264 | |
| 265 | if (palmas_usb->enable_id_detection) { |
| 266 | palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data, |
| 267 | PALMAS_ID_OTG_IRQ); |
| 268 | palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data, |
| 269 | PALMAS_ID_IRQ); |
| 270 | status = devm_request_threaded_irq(palmas_usb->dev, |
| 271 | palmas_usb->id_irq, |
| 272 | NULL, palmas_id_irq_handler, |
| 273 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | |
| 274 | IRQF_ONESHOT | IRQF_EARLY_RESUME, |
| 275 | "palmas_usb_id", palmas_usb); |
| 276 | if (status < 0) { |
| 277 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
| 278 | palmas_usb->id_irq, status); |
| 279 | return status; |
| 280 | } |
| 281 | } else if (palmas_usb->enable_gpio_id_detection) { |
| 282 | palmas_usb->gpio_id_irq = gpiod_to_irq(palmas_usb->id_gpiod); |
| 283 | if (palmas_usb->gpio_id_irq < 0) { |
| 284 | dev_err(&pdev->dev, "failed to get id irq\n"); |
| 285 | return palmas_usb->gpio_id_irq; |
| 286 | } |
| 287 | status = devm_request_threaded_irq(&pdev->dev, |
| 288 | palmas_usb->gpio_id_irq, |
| 289 | NULL, |
| 290 | palmas_gpio_id_irq_handler, |
| 291 | IRQF_TRIGGER_RISING | |
| 292 | IRQF_TRIGGER_FALLING | |
| 293 | IRQF_ONESHOT, |
| 294 | "palmas_usb_id", |
| 295 | palmas_usb); |
| 296 | if (status < 0) { |
| 297 | dev_err(&pdev->dev, |
| 298 | "failed to request handler for id irq\n"); |
| 299 | return status; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (palmas_usb->enable_vbus_detection) { |
| 304 | palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data, |
| 305 | PALMAS_VBUS_OTG_IRQ); |
| 306 | palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data, |
| 307 | PALMAS_VBUS_IRQ); |
| 308 | status = devm_request_threaded_irq(palmas_usb->dev, |
| 309 | palmas_usb->vbus_irq, NULL, |
| 310 | palmas_vbus_irq_handler, |
| 311 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | |
| 312 | IRQF_ONESHOT | IRQF_EARLY_RESUME, |
| 313 | "palmas_usb_vbus", palmas_usb); |
| 314 | if (status < 0) { |
| 315 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
| 316 | palmas_usb->vbus_irq, status); |
| 317 | return status; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | palmas_enable_irq(palmas_usb); |
| 322 | /* perform initial detection */ |
| 323 | palmas_gpio_id_detect(&palmas_usb->wq_detectid.work); |
| 324 | device_set_wakeup_capable(&pdev->dev, true); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int palmas_usb_remove(struct platform_device *pdev) |
| 329 | { |
| 330 | struct palmas_usb *palmas_usb = platform_get_drvdata(pdev); |
| 331 | |
| 332 | cancel_delayed_work_sync(&palmas_usb->wq_detectid); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | #ifdef CONFIG_PM_SLEEP |
| 338 | static int palmas_usb_suspend(struct device *dev) |
| 339 | { |
| 340 | struct palmas_usb *palmas_usb = dev_get_drvdata(dev); |
| 341 | |
| 342 | if (device_may_wakeup(dev)) { |
| 343 | if (palmas_usb->enable_vbus_detection) |
| 344 | enable_irq_wake(palmas_usb->vbus_irq); |
| 345 | if (palmas_usb->enable_id_detection) |
| 346 | enable_irq_wake(palmas_usb->id_irq); |
| 347 | if (palmas_usb->enable_gpio_id_detection) |
| 348 | enable_irq_wake(palmas_usb->gpio_id_irq); |
| 349 | } |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int palmas_usb_resume(struct device *dev) |
| 354 | { |
| 355 | struct palmas_usb *palmas_usb = dev_get_drvdata(dev); |
| 356 | |
| 357 | if (device_may_wakeup(dev)) { |
| 358 | if (palmas_usb->enable_vbus_detection) |
| 359 | disable_irq_wake(palmas_usb->vbus_irq); |
| 360 | if (palmas_usb->enable_id_detection) |
| 361 | disable_irq_wake(palmas_usb->id_irq); |
| 362 | if (palmas_usb->enable_gpio_id_detection) |
| 363 | disable_irq_wake(palmas_usb->gpio_id_irq); |
| 364 | } |
| 365 | return 0; |
| 366 | }; |
| 367 | #endif |
| 368 | |
| 369 | static SIMPLE_DEV_PM_OPS(palmas_pm_ops, palmas_usb_suspend, palmas_usb_resume); |
| 370 | |
| 371 | static const struct of_device_id of_palmas_match_tbl[] = { |
| 372 | { .compatible = "ti,palmas-usb", }, |
| 373 | { .compatible = "ti,palmas-usb-vid", }, |
| 374 | { .compatible = "ti,twl6035-usb", }, |
| 375 | { .compatible = "ti,twl6035-usb-vid", }, |
| 376 | { /* end */ } |
| 377 | }; |
| 378 | |
| 379 | static struct platform_driver palmas_usb_driver = { |
| 380 | .probe = palmas_usb_probe, |
| 381 | .remove = palmas_usb_remove, |
| 382 | .driver = { |
| 383 | .name = "palmas-usb", |
| 384 | .of_match_table = of_palmas_match_tbl, |
| 385 | .pm = &palmas_pm_ops, |
| 386 | }, |
| 387 | }; |
| 388 | |
| 389 | module_platform_driver(palmas_usb_driver); |
| 390 | |
| 391 | MODULE_ALIAS("platform:palmas-usb"); |
| 392 | MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>"); |
| 393 | MODULE_DESCRIPTION("Palmas USB transceiver driver"); |
| 394 | MODULE_LICENSE("GPL"); |
| 395 | MODULE_DEVICE_TABLE(of, of_palmas_match_tbl); |