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> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/mfd/palmas.h> |
| 28 | #include <linux/of.h> |
| 29 | #include <linux/of_platform.h> |
| 30 | |
| 31 | static const char *palmas_extcon_cable[] = { |
| 32 | [0] = "USB", |
| 33 | [1] = "USB-HOST", |
| 34 | NULL, |
| 35 | }; |
| 36 | |
| 37 | static const int mutually_exclusive[] = {0x3, 0x0}; |
| 38 | |
| 39 | static void palmas_usb_wakeup(struct palmas *palmas, int enable) |
| 40 | { |
| 41 | if (enable) |
| 42 | palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP, |
| 43 | PALMAS_USB_WAKEUP_ID_WK_UP_COMP); |
| 44 | else |
| 45 | palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP, 0); |
| 46 | } |
| 47 | |
| 48 | static irqreturn_t palmas_vbus_irq_handler(int irq, void *_palmas_usb) |
| 49 | { |
| 50 | struct palmas_usb *palmas_usb = _palmas_usb; |
| 51 | unsigned int vbus_line_state; |
| 52 | |
| 53 | palmas_read(palmas_usb->palmas, PALMAS_INTERRUPT_BASE, |
| 54 | PALMAS_INT3_LINE_STATE, &vbus_line_state); |
| 55 | |
| 56 | if (vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS) { |
| 57 | if (palmas_usb->linkstat != PALMAS_USB_STATE_VBUS) { |
| 58 | palmas_usb->linkstat = PALMAS_USB_STATE_VBUS; |
| 59 | extcon_set_cable_state(&palmas_usb->edev, "USB", true); |
Laxman Dewangan | 002945f | 2013-07-09 18:34:22 +0530 | [diff] [blame] | 60 | dev_info(palmas_usb->dev, "USB cable is attached\n"); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 61 | } else { |
| 62 | dev_dbg(palmas_usb->dev, |
| 63 | "Spurious connect event detected\n"); |
| 64 | } |
| 65 | } else if (!(vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS)) { |
| 66 | if (palmas_usb->linkstat == PALMAS_USB_STATE_VBUS) { |
| 67 | palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT; |
| 68 | extcon_set_cable_state(&palmas_usb->edev, "USB", false); |
Laxman Dewangan | 002945f | 2013-07-09 18:34:22 +0530 | [diff] [blame] | 69 | dev_info(palmas_usb->dev, "USB cable is detached\n"); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 70 | } else { |
| 71 | dev_dbg(palmas_usb->dev, |
| 72 | "Spurious disconnect event detected\n"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return IRQ_HANDLED; |
| 77 | } |
| 78 | |
| 79 | static irqreturn_t palmas_id_irq_handler(int irq, void *_palmas_usb) |
| 80 | { |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 81 | unsigned int set, id_src; |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 82 | struct palmas_usb *palmas_usb = _palmas_usb; |
| 83 | |
| 84 | palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 85 | PALMAS_USB_ID_INT_LATCH_SET, &set); |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 86 | palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 87 | PALMAS_USB_ID_INT_SRC, &id_src); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 88 | |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 89 | if ((set & PALMAS_USB_ID_INT_SRC_ID_GND) && |
| 90 | (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) { |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 91 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 92 | PALMAS_USB_ID_INT_LATCH_CLR, |
| 93 | PALMAS_USB_ID_INT_EN_HI_CLR_ID_GND); |
| 94 | palmas_usb->linkstat = PALMAS_USB_STATE_ID; |
| 95 | extcon_set_cable_state(&palmas_usb->edev, "USB-HOST", true); |
Laxman Dewangan | 002945f | 2013-07-09 18:34:22 +0530 | [diff] [blame] | 96 | dev_info(palmas_usb->dev, "USB-HOST cable is attached\n"); |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 97 | } else if ((set & PALMAS_USB_ID_INT_SRC_ID_FLOAT) && |
| 98 | (id_src & PALMAS_USB_ID_INT_SRC_ID_FLOAT)) { |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 99 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 100 | PALMAS_USB_ID_INT_LATCH_CLR, |
| 101 | PALMAS_USB_ID_INT_EN_HI_CLR_ID_FLOAT); |
| 102 | palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT; |
| 103 | extcon_set_cable_state(&palmas_usb->edev, "USB-HOST", false); |
Laxman Dewangan | 002945f | 2013-07-09 18:34:22 +0530 | [diff] [blame] | 104 | dev_info(palmas_usb->dev, "USB-HOST cable is detached\n"); |
| 105 | } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_ID) && |
| 106 | (!(set & PALMAS_USB_ID_INT_SRC_ID_GND))) { |
| 107 | palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT; |
| 108 | extcon_set_cable_state(&palmas_usb->edev, "USB-HOST", false); |
| 109 | dev_info(palmas_usb->dev, "USB-HOST cable is detached\n"); |
George Cherian | b7d2d5b | 2013-10-11 00:18:39 +0530 | [diff] [blame] | 110 | } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_DISCONNECT) && |
| 111 | (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) { |
| 112 | palmas_usb->linkstat = PALMAS_USB_STATE_ID; |
| 113 | extcon_set_cable_state(&palmas_usb->edev, "USB-HOST", true); |
| 114 | dev_info(palmas_usb->dev, " USB-HOST cable is attached\n"); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return IRQ_HANDLED; |
| 118 | } |
| 119 | |
| 120 | static void palmas_enable_irq(struct palmas_usb *palmas_usb) |
| 121 | { |
| 122 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 123 | PALMAS_USB_VBUS_CTRL_SET, |
| 124 | PALMAS_USB_VBUS_CTRL_SET_VBUS_ACT_COMP); |
| 125 | |
| 126 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 127 | PALMAS_USB_ID_CTRL_SET, PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP); |
| 128 | |
| 129 | palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, |
| 130 | PALMAS_USB_ID_INT_EN_HI_SET, |
Laxman Dewangan | 002945f | 2013-07-09 18:34:22 +0530 | [diff] [blame] | 131 | PALMAS_USB_ID_INT_EN_HI_SET_ID_GND | |
| 132 | PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 133 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 134 | if (palmas_usb->enable_vbus_detection) |
| 135 | palmas_vbus_irq_handler(palmas_usb->vbus_irq, palmas_usb); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 136 | |
| 137 | /* cold plug for host mode needs this delay */ |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 138 | if (palmas_usb->enable_id_detection) { |
| 139 | msleep(30); |
| 140 | palmas_id_irq_handler(palmas_usb->id_irq, palmas_usb); |
| 141 | } |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int palmas_usb_probe(struct platform_device *pdev) |
| 145 | { |
| 146 | struct palmas *palmas = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | 7c0f6558 | 2013-09-11 13:22:18 +0900 | [diff] [blame] | 147 | struct palmas_usb_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 148 | struct device_node *node = pdev->dev.of_node; |
| 149 | struct palmas_usb *palmas_usb; |
| 150 | int status; |
| 151 | |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 152 | palmas_usb = devm_kzalloc(&pdev->dev, sizeof(*palmas_usb), GFP_KERNEL); |
| 153 | if (!palmas_usb) |
| 154 | return -ENOMEM; |
| 155 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 156 | if (node && !pdata) { |
| 157 | palmas_usb->wakeup = of_property_read_bool(node, "ti,wakeup"); |
| 158 | palmas_usb->enable_id_detection = of_property_read_bool(node, |
| 159 | "ti,enable-id-detection"); |
| 160 | palmas_usb->enable_vbus_detection = of_property_read_bool(node, |
| 161 | "ti,enable-vbus-detection"); |
| 162 | } else { |
| 163 | palmas_usb->wakeup = true; |
| 164 | palmas_usb->enable_id_detection = true; |
| 165 | palmas_usb->enable_vbus_detection = true; |
| 166 | |
| 167 | if (pdata) |
| 168 | palmas_usb->wakeup = pdata->wakeup; |
| 169 | } |
| 170 | |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 171 | palmas->usb = palmas_usb; |
| 172 | palmas_usb->palmas = palmas; |
| 173 | |
| 174 | palmas_usb->dev = &pdev->dev; |
| 175 | |
| 176 | palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data, |
| 177 | PALMAS_ID_OTG_IRQ); |
| 178 | palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data, |
| 179 | PALMAS_ID_IRQ); |
| 180 | palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data, |
| 181 | PALMAS_VBUS_OTG_IRQ); |
| 182 | palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data, |
| 183 | PALMAS_VBUS_IRQ); |
| 184 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 185 | palmas_usb_wakeup(palmas, palmas_usb->wakeup); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 186 | |
| 187 | platform_set_drvdata(pdev, palmas_usb); |
| 188 | |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 189 | palmas_usb->edev.supported_cable = palmas_extcon_cable; |
Chanwoo Choi | 42d7d75 | 2013-09-27 09:20:26 +0900 | [diff] [blame] | 190 | palmas_usb->edev.dev.parent = palmas_usb->dev; |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 191 | palmas_usb->edev.mutually_exclusive = mutually_exclusive; |
| 192 | |
Chanwoo Choi | 42d7d75 | 2013-09-27 09:20:26 +0900 | [diff] [blame] | 193 | status = extcon_dev_register(&palmas_usb->edev); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 194 | if (status) { |
| 195 | dev_err(&pdev->dev, "failed to register extcon device\n"); |
| 196 | return status; |
| 197 | } |
| 198 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 199 | if (palmas_usb->enable_id_detection) { |
| 200 | status = devm_request_threaded_irq(palmas_usb->dev, |
| 201 | palmas_usb->id_irq, |
| 202 | NULL, palmas_id_irq_handler, |
| 203 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | |
| 204 | IRQF_ONESHOT | IRQF_EARLY_RESUME, |
| 205 | "palmas_usb_id", palmas_usb); |
| 206 | if (status < 0) { |
| 207 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 208 | palmas_usb->id_irq, status); |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 209 | goto fail_extcon; |
| 210 | } |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 211 | } |
| 212 | |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 213 | if (palmas_usb->enable_vbus_detection) { |
| 214 | status = devm_request_threaded_irq(palmas_usb->dev, |
| 215 | palmas_usb->vbus_irq, NULL, |
| 216 | palmas_vbus_irq_handler, |
| 217 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | |
| 218 | IRQF_ONESHOT | IRQF_EARLY_RESUME, |
| 219 | "palmas_usb_vbus", palmas_usb); |
| 220 | if (status < 0) { |
| 221 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 222 | palmas_usb->vbus_irq, status); |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 223 | goto fail_extcon; |
| 224 | } |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | palmas_enable_irq(palmas_usb); |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 228 | device_set_wakeup_capable(&pdev->dev, true); |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 229 | return 0; |
| 230 | |
| 231 | fail_extcon: |
| 232 | extcon_dev_unregister(&palmas_usb->edev); |
| 233 | |
| 234 | return status; |
| 235 | } |
| 236 | |
| 237 | static int palmas_usb_remove(struct platform_device *pdev) |
| 238 | { |
| 239 | struct palmas_usb *palmas_usb = platform_get_drvdata(pdev); |
| 240 | |
| 241 | extcon_dev_unregister(&palmas_usb->edev); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 246 | #ifdef CONFIG_PM_SLEEP |
| 247 | static int palmas_usb_suspend(struct device *dev) |
| 248 | { |
| 249 | struct palmas_usb *palmas_usb = dev_get_drvdata(dev); |
| 250 | |
| 251 | if (device_may_wakeup(dev)) { |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 252 | if (palmas_usb->enable_vbus_detection) |
| 253 | enable_irq_wake(palmas_usb->vbus_irq); |
| 254 | if (palmas_usb->enable_id_detection) |
| 255 | enable_irq_wake(palmas_usb->id_irq); |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 256 | } |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int palmas_usb_resume(struct device *dev) |
| 261 | { |
| 262 | struct palmas_usb *palmas_usb = dev_get_drvdata(dev); |
| 263 | |
| 264 | if (device_may_wakeup(dev)) { |
Laxman Dewangan | 7281e05 | 2013-07-10 14:59:06 +0530 | [diff] [blame] | 265 | if (palmas_usb->enable_vbus_detection) |
| 266 | disable_irq_wake(palmas_usb->vbus_irq); |
| 267 | if (palmas_usb->enable_id_detection) |
| 268 | disable_irq_wake(palmas_usb->id_irq); |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 269 | } |
| 270 | return 0; |
| 271 | }; |
| 272 | #endif |
| 273 | |
| 274 | static const struct dev_pm_ops palmas_pm_ops = { |
| 275 | SET_SYSTEM_SLEEP_PM_OPS(palmas_usb_suspend, |
| 276 | palmas_usb_resume) |
| 277 | }; |
| 278 | |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 279 | static struct of_device_id of_palmas_match_tbl[] = { |
| 280 | { .compatible = "ti,palmas-usb", }, |
Kishon Vijay Abraham I | 5fbc77d | 2013-10-10 18:07:27 +0530 | [diff] [blame] | 281 | { .compatible = "ti,palmas-usb-vid", }, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 282 | { .compatible = "ti,twl6035-usb", }, |
Kishon Vijay Abraham I | 5fbc77d | 2013-10-10 18:07:27 +0530 | [diff] [blame] | 283 | { .compatible = "ti,twl6035-usb-vid", }, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 284 | { /* end */ } |
| 285 | }; |
| 286 | |
| 287 | static struct platform_driver palmas_usb_driver = { |
| 288 | .probe = palmas_usb_probe, |
| 289 | .remove = palmas_usb_remove, |
| 290 | .driver = { |
| 291 | .name = "palmas-usb", |
| 292 | .of_match_table = of_palmas_match_tbl, |
| 293 | .owner = THIS_MODULE, |
Laxman Dewangan | 024783e | 2013-07-09 18:34:23 +0530 | [diff] [blame] | 294 | .pm = &palmas_pm_ops, |
Graeme Gregory | b1f254e | 2013-05-28 10:50:11 +0900 | [diff] [blame] | 295 | }, |
| 296 | }; |
| 297 | |
| 298 | module_platform_driver(palmas_usb_driver); |
| 299 | |
| 300 | MODULE_ALIAS("platform:palmas-usb"); |
| 301 | MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>"); |
| 302 | MODULE_DESCRIPTION("Palmas USB transceiver driver"); |
| 303 | MODULE_LICENSE("GPL"); |
| 304 | MODULE_DEVICE_TABLE(of, of_palmas_match_tbl); |