Ofir Cohen | 06789f1 | 2012-01-16 09:43:13 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/wakelock.h> |
| 17 | #include <linux/pm_runtime.h> |
| 18 | #include <linux/regulator/consumer.h> |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/usb.h> |
| 25 | |
| 26 | #include <linux/usb/gadget.h> |
| 27 | #include <linux/usb/msm_hsusb_hw.h> |
| 28 | #include <linux/usb/msm_hsusb.h> |
| 29 | |
| 30 | #include <mach/clk.h> |
| 31 | #include <mach/msm_iomap.h> |
| 32 | #include <mach/msm_xo.h> |
| 33 | |
| 34 | #include "ci13xxx_udc.c" |
| 35 | |
| 36 | #define MSM_USB_BASE (mhsic->regs) |
| 37 | |
| 38 | #define ULPI_IO_TIMEOUT_USEC (10 * 1000) |
| 39 | #define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */ |
| 40 | #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */ |
| 41 | #define USB_PHY_VDD_DIG_LOAD 49360 /* uA */ |
| 42 | #define LINK_RESET_TIMEOUT_USEC (250 * 1000) |
| 43 | #define HSIC_CFG_REG 0x30 |
| 44 | #define HSIC_IO_CAL_PER_REG 0x33 |
| 45 | #define HSIC_DBG1_REG 0x38 |
| 46 | |
| 47 | struct msm_hsic_per *the_mhsic; |
| 48 | |
| 49 | struct msm_hsic_per { |
| 50 | struct device *dev; |
| 51 | struct clk *iface_clk; |
| 52 | struct clk *core_clk; |
| 53 | struct clk *alt_core_clk; |
| 54 | struct clk *phy_clk; |
| 55 | struct clk *cal_clk; |
| 56 | struct regulator *hsic_vddcx; |
| 57 | bool async_int; |
| 58 | void __iomem *regs; |
| 59 | int irq; |
| 60 | }; |
| 61 | |
| 62 | static int msm_hsic_init_vddcx(struct msm_hsic_per *mhsic, int init) |
| 63 | { |
| 64 | int ret = 0; |
| 65 | |
| 66 | if (!init) |
| 67 | goto disable_reg; |
| 68 | |
| 69 | mhsic->hsic_vddcx = regulator_get(mhsic->dev, "HSIC_VDDCX"); |
| 70 | if (IS_ERR(mhsic->hsic_vddcx)) { |
| 71 | dev_err(mhsic->dev, "unable to get hsic vddcx\n"); |
| 72 | return PTR_ERR(mhsic->hsic_vddcx); |
| 73 | } |
| 74 | |
| 75 | ret = regulator_set_voltage(mhsic->hsic_vddcx, |
| 76 | USB_PHY_VDD_DIG_VOL_MIN, |
| 77 | USB_PHY_VDD_DIG_VOL_MAX); |
| 78 | if (ret) { |
| 79 | dev_err(mhsic->dev, "unable to set the voltage" |
| 80 | "for hsic vddcx\n"); |
| 81 | goto reg_set_voltage_err; |
| 82 | } |
| 83 | |
| 84 | ret = regulator_set_optimum_mode(mhsic->hsic_vddcx, |
| 85 | USB_PHY_VDD_DIG_LOAD); |
| 86 | if (ret < 0) { |
| 87 | pr_err("%s: Unable to set optimum mode of the regulator:" |
| 88 | "VDDCX\n", __func__); |
| 89 | goto reg_optimum_mode_err; |
| 90 | } |
| 91 | |
| 92 | ret = regulator_enable(mhsic->hsic_vddcx); |
| 93 | if (ret) { |
| 94 | dev_err(mhsic->dev, "unable to enable hsic vddcx\n"); |
| 95 | goto reg_enable_err; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | |
| 100 | disable_reg: |
| 101 | regulator_disable(mhsic->hsic_vddcx); |
| 102 | reg_enable_err: |
| 103 | regulator_set_optimum_mode(mhsic->hsic_vddcx, 0); |
| 104 | reg_optimum_mode_err: |
| 105 | regulator_set_voltage(mhsic->hsic_vddcx, 0, |
| 106 | USB_PHY_VDD_DIG_VOL_MIN); |
| 107 | reg_set_voltage_err: |
| 108 | regulator_put(mhsic->hsic_vddcx); |
| 109 | |
| 110 | return ret; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | static int ulpi_write(struct msm_hsic_per *mhsic, u32 val, u32 reg) |
| 115 | { |
| 116 | int cnt = 0; |
| 117 | |
| 118 | /* initiate write operation */ |
| 119 | writel_relaxed(ULPI_RUN | ULPI_WRITE | |
| 120 | ULPI_ADDR(reg) | ULPI_DATA(val), |
| 121 | USB_ULPI_VIEWPORT); |
| 122 | |
| 123 | /* wait for completion */ |
| 124 | while (cnt < ULPI_IO_TIMEOUT_USEC) { |
| 125 | if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN)) |
| 126 | break; |
| 127 | udelay(1); |
| 128 | cnt++; |
| 129 | } |
| 130 | |
| 131 | if (cnt >= ULPI_IO_TIMEOUT_USEC) { |
| 132 | dev_err(mhsic->dev, "ulpi_write: timeout\n"); |
| 133 | return -ETIMEDOUT; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int msm_hsic_phy_clk_reset(struct msm_hsic_per *mhsic) |
| 140 | { |
| 141 | int ret; |
| 142 | |
| 143 | ret = clk_reset(mhsic->core_clk, CLK_RESET_ASSERT); |
| 144 | if (ret) { |
| 145 | clk_disable(mhsic->alt_core_clk); |
| 146 | dev_err(mhsic->dev, "usb phy clk assert failed\n"); |
| 147 | return ret; |
| 148 | } |
| 149 | usleep_range(10000, 12000); |
| 150 | clk_disable(mhsic->alt_core_clk); |
| 151 | |
| 152 | ret = clk_reset(mhsic->core_clk, CLK_RESET_DEASSERT); |
| 153 | if (ret) |
| 154 | dev_err(mhsic->dev, "usb phy clk deassert failed\n"); |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static int msm_hsic_phy_reset(struct msm_hsic_per *mhsic) |
| 160 | { |
| 161 | u32 val; |
| 162 | int ret; |
| 163 | |
| 164 | ret = msm_hsic_phy_clk_reset(mhsic); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
| 168 | val = readl_relaxed(USB_PORTSC) & ~PORTSC_PTS_MASK; |
| 169 | writel_relaxed(val | PORTSC_PTS_ULPI, USB_PORTSC); |
| 170 | |
| 171 | /* |
| 172 | * Ensure that RESET operation is completed before |
| 173 | * turning off clock. |
| 174 | */ |
| 175 | mb(); |
| 176 | dev_dbg(mhsic->dev, "phy_reset: success\n"); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int msm_hsic_enable_clocks(struct platform_device *pdev, |
| 182 | struct msm_hsic_per *mhsic, bool enable) |
| 183 | { |
| 184 | int ret = 0; |
| 185 | |
| 186 | if (!enable) |
| 187 | goto put_clocks; |
| 188 | |
| 189 | mhsic->iface_clk = clk_get(&pdev->dev, "iface_clk"); |
| 190 | if (IS_ERR(mhsic->iface_clk)) { |
| 191 | dev_err(mhsic->dev, "failed to get iface_clk\n"); |
| 192 | ret = PTR_ERR(mhsic->iface_clk); |
| 193 | goto put_iface_clk; |
| 194 | } |
| 195 | |
| 196 | mhsic->core_clk = clk_get(&pdev->dev, "core_clk"); |
| 197 | if (IS_ERR(mhsic->core_clk)) { |
| 198 | dev_err(mhsic->dev, "failed to get core_clk\n"); |
| 199 | ret = PTR_ERR(mhsic->core_clk); |
| 200 | goto put_core_clk; |
| 201 | } |
| 202 | |
| 203 | mhsic->phy_clk = clk_get(&pdev->dev, "phy_clk"); |
| 204 | if (IS_ERR(mhsic->phy_clk)) { |
| 205 | dev_err(mhsic->dev, "failed to get phy_clk\n"); |
| 206 | ret = PTR_ERR(mhsic->phy_clk); |
| 207 | goto put_phy_clk; |
| 208 | } |
| 209 | |
| 210 | mhsic->alt_core_clk = clk_get(&pdev->dev, "alt_core_clk"); |
| 211 | if (IS_ERR(mhsic->alt_core_clk)) { |
| 212 | dev_err(mhsic->dev, "failed to get alt_core_clk\n"); |
| 213 | ret = PTR_ERR(mhsic->alt_core_clk); |
| 214 | goto put_alt_core_clk; |
| 215 | } |
| 216 | |
| 217 | mhsic->cal_clk = clk_get(&pdev->dev, "cal_clk"); |
| 218 | if (IS_ERR(mhsic->cal_clk)) { |
| 219 | dev_err(mhsic->dev, "failed to get cal_clk\n"); |
| 220 | ret = PTR_ERR(mhsic->cal_clk); |
| 221 | goto put_cal_clk; |
| 222 | } |
| 223 | |
| 224 | clk_enable(mhsic->iface_clk); |
| 225 | clk_enable(mhsic->core_clk); |
| 226 | clk_enable(mhsic->phy_clk); |
| 227 | clk_enable(mhsic->alt_core_clk); |
| 228 | clk_enable(mhsic->cal_clk); |
| 229 | |
| 230 | return 0; |
| 231 | |
| 232 | put_clocks: |
| 233 | clk_disable(mhsic->iface_clk); |
| 234 | clk_disable(mhsic->core_clk); |
| 235 | clk_disable(mhsic->phy_clk); |
| 236 | clk_disable(mhsic->alt_core_clk); |
| 237 | clk_disable(mhsic->cal_clk); |
| 238 | put_cal_clk: |
| 239 | clk_put(mhsic->cal_clk); |
| 240 | put_alt_core_clk: |
| 241 | clk_put(mhsic->alt_core_clk); |
| 242 | put_phy_clk: |
| 243 | clk_put(mhsic->phy_clk); |
| 244 | put_core_clk: |
| 245 | clk_put(mhsic->core_clk); |
| 246 | put_iface_clk: |
| 247 | clk_put(mhsic->iface_clk); |
| 248 | |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | static int msm_hsic_reset(struct msm_hsic_per *mhsic) |
| 253 | { |
| 254 | int cnt = 0; |
| 255 | int ret; |
| 256 | |
| 257 | ret = msm_hsic_phy_reset(mhsic); |
| 258 | if (ret) { |
| 259 | dev_err(mhsic->dev, "phy_reset failed\n"); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | writel_relaxed(USBCMD_RESET, USB_USBCMD); |
| 264 | while (cnt < LINK_RESET_TIMEOUT_USEC) { |
| 265 | if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET)) |
| 266 | break; |
| 267 | udelay(1); |
| 268 | cnt++; |
| 269 | } |
| 270 | if (cnt >= LINK_RESET_TIMEOUT_USEC) |
| 271 | return -ETIMEDOUT; |
| 272 | |
| 273 | /* Reset PORTSC and select ULPI phy */ |
| 274 | writel_relaxed(0x80000000, USB_PORTSC); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static void msm_hsic_start(void) |
| 279 | { |
| 280 | int ret; |
| 281 | |
| 282 | /* programmable length of connect signaling (33.2ns) */ |
| 283 | ret = ulpi_write(the_mhsic, 3, HSIC_DBG1_REG); |
| 284 | if (ret) { |
| 285 | pr_err("%s: Unable to program length of connect signaling\n", |
| 286 | __func__); |
| 287 | } |
| 288 | |
| 289 | /*set periodic calibration interval to ~2.048sec in HSIC_IO_CAL_REG */ |
| 290 | ret = ulpi_write(the_mhsic, 0xFF, HSIC_IO_CAL_PER_REG); |
| 291 | |
| 292 | if (ret) { |
| 293 | pr_err("%s: Unable to set periodic calibration interval\n", |
| 294 | __func__); |
| 295 | } |
| 296 | |
| 297 | /* Enable periodic IO calibration in HSIC_CFG register */ |
| 298 | ret = ulpi_write(the_mhsic, 0xE9, HSIC_CFG_REG); |
| 299 | if (ret) { |
| 300 | pr_err("%s: Unable to enable periodic IO calibration\n", |
| 301 | __func__); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Dummy match function - will be called only for HSIC msm |
| 307 | * device (msm_device_gadget_hsic_peripheral). |
| 308 | */ |
| 309 | static inline int __match(struct device *dev, void *data) { return 1; } |
| 310 | |
| 311 | static void msm_hsic_connect_peripheral(struct device *msm_udc_dev) |
| 312 | { |
| 313 | struct device *dev; |
| 314 | struct usb_gadget *gadget; |
| 315 | |
| 316 | dev = device_find_child(msm_udc_dev, NULL, __match); |
| 317 | gadget = dev_to_usb_gadget(dev); |
| 318 | usb_gadget_vbus_connect(gadget); |
| 319 | } |
| 320 | |
| 321 | static irqreturn_t msm_udc_hsic_irq(int irq, void *data) |
| 322 | { |
| 323 | return udc_irq(); |
| 324 | } |
| 325 | |
| 326 | static void ci13xxx_msm_hsic_notify_event(struct ci13xxx *udc, unsigned event) |
| 327 | { |
| 328 | struct device *dev = udc->gadget.dev.parent; |
| 329 | struct msm_hsic_per *mhsic = the_mhsic; |
| 330 | |
| 331 | switch (event) { |
| 332 | case CI13XXX_CONTROLLER_RESET_EVENT: |
| 333 | dev_dbg(dev, "CI13XXX_CONTROLLER_RESET_EVENT received\n"); |
| 334 | writel_relaxed(0, USB_AHBBURST); |
| 335 | writel_relaxed(0, USB_AHBMODE); |
| 336 | break; |
| 337 | case CI13XXX_CONTROLLER_CONNECT_EVENT: |
| 338 | dev_dbg(dev, "CI13XXX_CONTROLLER_CONNECT_EVENT received\n"); |
| 339 | msm_hsic_start(); |
| 340 | break; |
| 341 | default: |
| 342 | dev_dbg(dev, "unknown ci13xxx_udc event\n"); |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | static struct ci13xxx_udc_driver ci13xxx_msm_udc_hsic_driver = { |
| 348 | .name = "ci13xxx_msm_hsic", |
| 349 | .flags = CI13XXX_REGS_SHARED | |
| 350 | CI13XXX_PULLUP_ON_VBUS | |
| 351 | CI13XXX_DISABLE_STREAMING | |
| 352 | CI13XXX_ZERO_ITC, |
| 353 | |
| 354 | .notify_event = ci13xxx_msm_hsic_notify_event, |
| 355 | }; |
| 356 | |
| 357 | static int __devinit msm_hsic_probe(struct platform_device *pdev) |
| 358 | { |
| 359 | struct resource *res; |
| 360 | struct msm_hsic_per *mhsic; |
| 361 | int ret = 0; |
| 362 | |
| 363 | dev_dbg(&pdev->dev, "msm-hsic probe\n"); |
| 364 | |
| 365 | mhsic = kzalloc(sizeof(struct msm_hsic_per), GFP_KERNEL); |
| 366 | if (!mhsic) { |
| 367 | dev_err(&pdev->dev, "unable to allocate msm_hsic\n"); |
| 368 | return -ENOMEM; |
| 369 | } |
| 370 | the_mhsic = mhsic; |
| 371 | platform_set_drvdata(pdev, mhsic); |
| 372 | mhsic->dev = &pdev->dev; |
| 373 | |
| 374 | mhsic->irq = platform_get_irq(pdev, 0); |
| 375 | if (mhsic->irq < 0) { |
| 376 | dev_err(&pdev->dev, "Unable to get IRQ resource\n"); |
| 377 | ret = mhsic->irq; |
| 378 | goto error; |
| 379 | } |
| 380 | |
| 381 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 382 | if (!res) { |
| 383 | dev_err(&pdev->dev, "Unable to get memory resource\n"); |
| 384 | ret = -ENODEV; |
| 385 | goto error; |
| 386 | } |
| 387 | mhsic->regs = ioremap(res->start, resource_size(res)); |
| 388 | if (!mhsic->regs) { |
| 389 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 390 | ret = -ENOMEM; |
| 391 | goto unmap; |
| 392 | } |
| 393 | dev_info(&pdev->dev, "HSIC Peripheral regs = %p\n", mhsic->regs); |
| 394 | |
| 395 | ret = msm_hsic_enable_clocks(pdev, mhsic, true); |
| 396 | |
| 397 | if (ret) { |
| 398 | dev_err(&pdev->dev, "msm_hsic_enable_clocks failed\n"); |
| 399 | ret = -ENODEV; |
| 400 | goto deinit_clocks; |
| 401 | } |
| 402 | ret = msm_hsic_init_vddcx(mhsic, 1); |
| 403 | if (ret) { |
| 404 | dev_err(&pdev->dev, "unable to initialize VDDCX\n"); |
| 405 | ret = -ENODEV; |
| 406 | goto deinit_vddcx; |
| 407 | } |
| 408 | |
| 409 | ret = msm_hsic_reset(mhsic); |
| 410 | if (ret) { |
| 411 | dev_err(&pdev->dev, "msm_hsic_reset failed\n"); |
| 412 | ret = -ENODEV; |
| 413 | goto deinit_vddcx; |
| 414 | } |
| 415 | |
| 416 | ret = udc_probe(&ci13xxx_msm_udc_hsic_driver, &pdev->dev, mhsic->regs); |
| 417 | if (ret < 0) { |
| 418 | dev_err(&pdev->dev, "udc_probe failed\n"); |
| 419 | ret = -ENODEV; |
| 420 | goto deinit_vddcx; |
| 421 | } |
| 422 | |
| 423 | msm_hsic_connect_peripheral(&pdev->dev); |
| 424 | |
| 425 | ret = request_irq(mhsic->irq, msm_udc_hsic_irq, |
| 426 | IRQF_SHARED, pdev->name, pdev); |
| 427 | if (ret < 0) { |
| 428 | dev_err(&pdev->dev, "request_irq failed\n"); |
| 429 | ret = -ENODEV; |
| 430 | goto udc_remove; |
| 431 | } |
| 432 | |
| 433 | pm_runtime_no_callbacks(&pdev->dev); |
| 434 | pm_runtime_enable(&pdev->dev); |
| 435 | |
| 436 | return 0; |
| 437 | udc_remove: |
| 438 | udc_remove(); |
| 439 | deinit_vddcx: |
| 440 | msm_hsic_init_vddcx(mhsic, 0); |
| 441 | deinit_clocks: |
| 442 | msm_hsic_enable_clocks(pdev, mhsic, 0); |
| 443 | unmap: |
| 444 | iounmap(mhsic->regs); |
| 445 | error: |
| 446 | kfree(mhsic); |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | static int __devexit hsic_msm_remove(struct platform_device *pdev) |
| 451 | { |
| 452 | struct msm_hsic_per *mhsic = platform_get_drvdata(pdev); |
| 453 | |
| 454 | device_init_wakeup(&pdev->dev, 0); |
| 455 | msm_hsic_init_vddcx(mhsic, 0); |
| 456 | msm_hsic_enable_clocks(pdev, mhsic, 0); |
| 457 | udc_remove(); |
| 458 | iounmap(mhsic->regs); |
| 459 | kfree(mhsic); |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | static struct platform_driver msm_hsic_peripheral_driver = { |
| 464 | .probe = msm_hsic_probe, |
| 465 | .remove = __devexit_p(hsic_msm_remove), |
| 466 | .driver = { |
| 467 | .name = "msm_hsic_peripheral", |
| 468 | }, |
| 469 | }; |
| 470 | |
| 471 | static int __init msm_hsic_peripheral_init(void) |
| 472 | { |
| 473 | return platform_driver_probe(&msm_hsic_peripheral_driver, |
| 474 | msm_hsic_probe); |
| 475 | } |
| 476 | |
| 477 | static void __exit msm_hsic_peripheral_exit(void) |
| 478 | { |
| 479 | platform_driver_unregister(&msm_hsic_peripheral_driver); |
| 480 | } |
| 481 | |
| 482 | module_init(msm_hsic_peripheral_init); |
| 483 | module_exit(msm_hsic_peripheral_exit); |
| 484 | |