Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 ST-Ericsson AB |
| 3 | * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com> |
| 4 | * |
| 5 | * Based on omap2430.c |
| 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 as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/clk.h> |
Kishon Vijay Abraham I | ded017e | 2012-06-26 17:40:32 +0530 | [diff] [blame] | 26 | #include <linux/err.h> |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 27 | #include <linux/io.h> |
| 28 | #include <linux/platform_device.h> |
Fabio Baltieri | af6882b | 2013-03-08 10:27:09 +0800 | [diff] [blame] | 29 | #include <linux/usb/musb-ux500.h> |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 30 | |
| 31 | #include "musb_core.h" |
| 32 | |
| 33 | struct ux500_glue { |
| 34 | struct device *dev; |
| 35 | struct platform_device *musb; |
| 36 | struct clk *clk; |
| 37 | }; |
| 38 | #define glue_to_musb(g) platform_get_drvdata(g->musb) |
| 39 | |
Fabio Baltieri | 996a9d2 | 2013-03-08 10:27:06 +0800 | [diff] [blame] | 40 | static void ux500_musb_set_vbus(struct musb *musb, int is_on) |
| 41 | { |
| 42 | u8 devctl; |
| 43 | unsigned long timeout = jiffies + msecs_to_jiffies(1000); |
| 44 | /* HDRC controls CPEN, but beware current surges during device |
| 45 | * connect. They can trigger transient overcurrent conditions |
| 46 | * that must be ignored. |
| 47 | */ |
| 48 | |
| 49 | devctl = musb_readb(musb->mregs, MUSB_DEVCTL); |
| 50 | |
| 51 | if (is_on) { |
| 52 | if (musb->xceiv->state == OTG_STATE_A_IDLE) { |
| 53 | /* start the session */ |
| 54 | devctl |= MUSB_DEVCTL_SESSION; |
| 55 | musb_writeb(musb->mregs, MUSB_DEVCTL, devctl); |
| 56 | /* |
| 57 | * Wait for the musb to set as A device to enable the |
| 58 | * VBUS |
| 59 | */ |
| 60 | while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) { |
| 61 | |
| 62 | if (time_after(jiffies, timeout)) { |
| 63 | dev_err(musb->controller, |
| 64 | "configured as A device timeout"); |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | } else { |
| 70 | musb->is_active = 1; |
| 71 | musb->xceiv->otg->default_a = 1; |
| 72 | musb->xceiv->state = OTG_STATE_A_WAIT_VRISE; |
| 73 | devctl |= MUSB_DEVCTL_SESSION; |
| 74 | MUSB_HST_MODE(musb); |
| 75 | } |
| 76 | } else { |
| 77 | musb->is_active = 0; |
| 78 | |
| 79 | /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping |
| 80 | * right to B_IDLE... |
| 81 | */ |
| 82 | musb->xceiv->otg->default_a = 0; |
| 83 | devctl &= ~MUSB_DEVCTL_SESSION; |
| 84 | MUSB_DEV_MODE(musb); |
| 85 | } |
| 86 | musb_writeb(musb->mregs, MUSB_DEVCTL, devctl); |
| 87 | |
| 88 | /* |
| 89 | * Devctl values will be updated after vbus goes below |
| 90 | * session_valid. The time taken depends on the capacitance |
| 91 | * on VBUS line. The max discharge time can be upto 1 sec |
| 92 | * as per the spec. Typically on our platform, it is 200ms |
| 93 | */ |
| 94 | if (!is_on) |
| 95 | mdelay(200); |
| 96 | |
| 97 | dev_dbg(musb->controller, "VBUS %s, devctl %02x\n", |
| 98 | usb_otg_state_string(musb->xceiv->state), |
| 99 | musb_readb(musb->mregs, MUSB_DEVCTL)); |
| 100 | } |
| 101 | |
Fabio Baltieri | 0135522 | 2013-03-08 10:27:07 +0800 | [diff] [blame] | 102 | static int musb_otg_notifications(struct notifier_block *nb, |
| 103 | unsigned long event, void *unused) |
| 104 | { |
| 105 | struct musb *musb = container_of(nb, struct musb, nb); |
| 106 | |
| 107 | dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n", |
| 108 | event, usb_otg_state_string(musb->xceiv->state)); |
| 109 | |
| 110 | switch (event) { |
Fabio Baltieri | af6882b | 2013-03-08 10:27:09 +0800 | [diff] [blame] | 111 | case UX500_MUSB_ID: |
Fabio Baltieri | 0135522 | 2013-03-08 10:27:07 +0800 | [diff] [blame] | 112 | dev_dbg(musb->controller, "ID GND\n"); |
| 113 | ux500_musb_set_vbus(musb, 1); |
| 114 | break; |
Fabio Baltieri | af6882b | 2013-03-08 10:27:09 +0800 | [diff] [blame] | 115 | case UX500_MUSB_VBUS: |
Fabio Baltieri | 0135522 | 2013-03-08 10:27:07 +0800 | [diff] [blame] | 116 | dev_dbg(musb->controller, "VBUS Connect\n"); |
Fabio Baltieri | 0135522 | 2013-03-08 10:27:07 +0800 | [diff] [blame] | 117 | break; |
Fabio Baltieri | af6882b | 2013-03-08 10:27:09 +0800 | [diff] [blame] | 118 | case UX500_MUSB_NONE: |
Fabio Baltieri | 0135522 | 2013-03-08 10:27:07 +0800 | [diff] [blame] | 119 | dev_dbg(musb->controller, "VBUS Disconnect\n"); |
| 120 | if (is_host_active(musb)) |
| 121 | ux500_musb_set_vbus(musb, 0); |
| 122 | else |
| 123 | musb->xceiv->state = OTG_STATE_B_IDLE; |
| 124 | break; |
| 125 | default: |
| 126 | dev_dbg(musb->controller, "ID float\n"); |
| 127 | return NOTIFY_DONE; |
| 128 | } |
| 129 | return NOTIFY_OK; |
| 130 | } |
| 131 | |
Philippe De Swert | baef653 | 2012-11-06 15:32:13 +0200 | [diff] [blame] | 132 | static irqreturn_t ux500_musb_interrupt(int irq, void *__hci) |
| 133 | { |
| 134 | unsigned long flags; |
| 135 | irqreturn_t retval = IRQ_NONE; |
| 136 | struct musb *musb = __hci; |
| 137 | |
| 138 | spin_lock_irqsave(&musb->lock, flags); |
| 139 | |
| 140 | musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); |
| 141 | musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); |
| 142 | musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); |
| 143 | |
| 144 | if (musb->int_usb || musb->int_tx || musb->int_rx) |
| 145 | retval = musb_interrupt(musb); |
| 146 | |
| 147 | spin_unlock_irqrestore(&musb->lock, flags); |
| 148 | |
| 149 | return retval; |
| 150 | } |
| 151 | |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 152 | static int ux500_musb_init(struct musb *musb) |
| 153 | { |
Fabio Baltieri | 0135522 | 2013-03-08 10:27:07 +0800 | [diff] [blame] | 154 | int status; |
| 155 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 156 | musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2); |
Kishon Vijay Abraham I | ded017e | 2012-06-26 17:40:32 +0530 | [diff] [blame] | 157 | if (IS_ERR_OR_NULL(musb->xceiv)) { |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 158 | pr_err("HS USB OTG: no transceiver configured\n"); |
Ming Lei | 25736e0 | 2013-01-04 23:13:58 +0800 | [diff] [blame] | 159 | return -EPROBE_DEFER; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Fabio Baltieri | 0135522 | 2013-03-08 10:27:07 +0800 | [diff] [blame] | 162 | musb->nb.notifier_call = musb_otg_notifications; |
| 163 | status = usb_register_notifier(musb->xceiv, &musb->nb); |
| 164 | if (status < 0) { |
| 165 | dev_dbg(musb->controller, "notification register failed\n"); |
| 166 | return status; |
| 167 | } |
| 168 | |
Philippe De Swert | baef653 | 2012-11-06 15:32:13 +0200 | [diff] [blame] | 169 | musb->isr = ux500_musb_interrupt; |
| 170 | |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int ux500_musb_exit(struct musb *musb) |
| 175 | { |
Fabio Baltieri | 0135522 | 2013-03-08 10:27:07 +0800 | [diff] [blame] | 176 | usb_unregister_notifier(musb->xceiv, &musb->nb); |
| 177 | |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 178 | usb_put_phy(musb->xceiv); |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static const struct musb_platform_ops ux500_ops = { |
| 184 | .init = ux500_musb_init, |
| 185 | .exit = ux500_musb_exit, |
Fabio Baltieri | 996a9d2 | 2013-03-08 10:27:06 +0800 | [diff] [blame] | 186 | |
| 187 | .set_vbus = ux500_musb_set_vbus, |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 188 | }; |
| 189 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 190 | static int ux500_probe(struct platform_device *pdev) |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 191 | { |
| 192 | struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data; |
| 193 | struct platform_device *musb; |
| 194 | struct ux500_glue *glue; |
| 195 | struct clk *clk; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 196 | int ret = -ENOMEM; |
| 197 | |
| 198 | glue = kzalloc(sizeof(*glue), GFP_KERNEL); |
| 199 | if (!glue) { |
| 200 | dev_err(&pdev->dev, "failed to allocate glue context\n"); |
| 201 | goto err0; |
| 202 | } |
| 203 | |
Sebastian Andrzej Siewior | 2f77116 | 2012-10-31 16:12:43 +0100 | [diff] [blame] | 204 | musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO); |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 205 | if (!musb) { |
| 206 | dev_err(&pdev->dev, "failed to allocate musb device\n"); |
Sebastian Andrzej Siewior | 2f77116 | 2012-10-31 16:12:43 +0100 | [diff] [blame] | 207 | goto err1; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | clk = clk_get(&pdev->dev, "usb"); |
| 211 | if (IS_ERR(clk)) { |
| 212 | dev_err(&pdev->dev, "failed to get clock\n"); |
| 213 | ret = PTR_ERR(clk); |
B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 214 | goto err3; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Fabio Baltieri | 99d17cf | 2013-01-07 17:47:41 +0100 | [diff] [blame] | 217 | ret = clk_prepare_enable(clk); |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 218 | if (ret) { |
| 219 | dev_err(&pdev->dev, "failed to enable clock\n"); |
B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 220 | goto err4; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | musb->dev.parent = &pdev->dev; |
Mian Yousaf Kaukab | 8726606 | 2011-03-15 16:24:29 +0100 | [diff] [blame] | 224 | musb->dev.dma_mask = pdev->dev.dma_mask; |
| 225 | musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 226 | |
| 227 | glue->dev = &pdev->dev; |
| 228 | glue->musb = musb; |
| 229 | glue->clk = clk; |
| 230 | |
| 231 | pdata->platform_ops = &ux500_ops; |
| 232 | |
| 233 | platform_set_drvdata(pdev, glue); |
| 234 | |
| 235 | ret = platform_device_add_resources(musb, pdev->resource, |
| 236 | pdev->num_resources); |
| 237 | if (ret) { |
| 238 | dev_err(&pdev->dev, "failed to add resources\n"); |
B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 239 | goto err5; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | ret = platform_device_add_data(musb, pdata, sizeof(*pdata)); |
| 243 | if (ret) { |
| 244 | dev_err(&pdev->dev, "failed to add platform_data\n"); |
B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 245 | goto err5; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | ret = platform_device_add(musb); |
| 249 | if (ret) { |
| 250 | dev_err(&pdev->dev, "failed to register musb device\n"); |
B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 251 | goto err5; |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | |
B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 256 | err5: |
Fabio Baltieri | 99d17cf | 2013-01-07 17:47:41 +0100 | [diff] [blame] | 257 | clk_disable_unprepare(clk); |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 258 | |
B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 259 | err4: |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 260 | clk_put(clk); |
| 261 | |
B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 262 | err3: |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 263 | platform_device_put(musb); |
| 264 | |
| 265 | err1: |
| 266 | kfree(glue); |
| 267 | |
| 268 | err0: |
| 269 | return ret; |
| 270 | } |
| 271 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 272 | static int ux500_remove(struct platform_device *pdev) |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 273 | { |
| 274 | struct ux500_glue *glue = platform_get_drvdata(pdev); |
| 275 | |
Wei Yongjun | 4b0de6f | 2012-10-23 13:36:43 +0800 | [diff] [blame] | 276 | platform_device_unregister(glue->musb); |
Fabio Baltieri | 99d17cf | 2013-01-07 17:47:41 +0100 | [diff] [blame] | 277 | clk_disable_unprepare(glue->clk); |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 278 | clk_put(glue->clk); |
| 279 | kfree(glue); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | #ifdef CONFIG_PM |
| 285 | static int ux500_suspend(struct device *dev) |
| 286 | { |
| 287 | struct ux500_glue *glue = dev_get_drvdata(dev); |
| 288 | struct musb *musb = glue_to_musb(glue); |
| 289 | |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 290 | usb_phy_set_suspend(musb->xceiv, 1); |
Fabio Baltieri | 99d17cf | 2013-01-07 17:47:41 +0100 | [diff] [blame] | 291 | clk_disable_unprepare(glue->clk); |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int ux500_resume(struct device *dev) |
| 297 | { |
| 298 | struct ux500_glue *glue = dev_get_drvdata(dev); |
| 299 | struct musb *musb = glue_to_musb(glue); |
| 300 | int ret; |
| 301 | |
Fabio Baltieri | 99d17cf | 2013-01-07 17:47:41 +0100 | [diff] [blame] | 302 | ret = clk_prepare_enable(glue->clk); |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 303 | if (ret) { |
| 304 | dev_err(dev, "failed to enable clock\n"); |
| 305 | return ret; |
| 306 | } |
| 307 | |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 308 | usb_phy_set_suspend(musb->xceiv, 0); |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static const struct dev_pm_ops ux500_pm_ops = { |
| 314 | .suspend = ux500_suspend, |
| 315 | .resume = ux500_resume, |
| 316 | }; |
| 317 | |
| 318 | #define DEV_PM_OPS (&ux500_pm_ops) |
| 319 | #else |
| 320 | #define DEV_PM_OPS NULL |
| 321 | #endif |
| 322 | |
| 323 | static struct platform_driver ux500_driver = { |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 324 | .probe = ux500_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 325 | .remove = ux500_remove, |
Mian Yousaf Kaukab | 4bc36fd | 2010-12-09 13:05:01 +0100 | [diff] [blame] | 326 | .driver = { |
| 327 | .name = "musb-ux500", |
| 328 | .pm = DEV_PM_OPS, |
| 329 | }, |
| 330 | }; |
| 331 | |
| 332 | MODULE_DESCRIPTION("UX500 MUSB Glue Layer"); |
| 333 | MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>"); |
| 334 | MODULE_LICENSE("GPL v2"); |
Srinivas Kandagatla | 0e7090a | 2012-10-10 19:37:21 +0100 | [diff] [blame] | 335 | module_platform_driver(ux500_driver); |