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