Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 2 | /* |
| 3 | * core.c - ChipIdea USB IP core family device controller |
| 4 | * |
| 5 | * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved. |
| 6 | * |
| 7 | * Author: David Lopo |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * Description: ChipIdea USB IP core family device controller |
| 16 | * |
| 17 | * This driver is composed of several blocks: |
| 18 | * - HW: hardware interface |
| 19 | * - DBG: debug facilities (optional) |
| 20 | * - UTIL: utilities |
| 21 | * - ISR: interrupts handling |
| 22 | * - ENDPT: endpoint operations (Gadget API) |
| 23 | * - GADGET: gadget operations (Gadget API) |
| 24 | * - BUS: bus glue code, bus abstraction layer |
| 25 | * |
| 26 | * Compile Options |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 27 | * - STALL_IN: non-empty bulk-in pipes cannot be halted |
| 28 | * if defined mass storage compliance succeeds but with warnings |
| 29 | * => case 4: Hi > Dn |
| 30 | * => case 5: Hi > Di |
| 31 | * => case 8: Hi <> Do |
| 32 | * if undefined usbtest 13 fails |
| 33 | * - TRACE: enable function tracing (depends on DEBUG) |
| 34 | * |
| 35 | * Main Features |
| 36 | * - Chapter 9 & Mass Storage Compliance with Gadget File Storage |
| 37 | * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined) |
| 38 | * - Normal & LPM support |
| 39 | * |
| 40 | * USBTEST Report |
| 41 | * - OK: 0-12, 13 (STALL_IN defined) & 14 |
| 42 | * - Not Supported: 15 & 16 (ISO) |
| 43 | * |
| 44 | * TODO List |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 45 | * - Suspend & Remote Wakeup |
| 46 | */ |
| 47 | #include <linux/delay.h> |
| 48 | #include <linux/device.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 49 | #include <linux/dma-mapping.h> |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 50 | #include <linux/extcon.h> |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 51 | #include <linux/phy/phy.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 52 | #include <linux/platform_device.h> |
| 53 | #include <linux/module.h> |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 54 | #include <linux/idr.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 55 | #include <linux/interrupt.h> |
| 56 | #include <linux/io.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 57 | #include <linux/kernel.h> |
| 58 | #include <linux/slab.h> |
| 59 | #include <linux/pm_runtime.h> |
| 60 | #include <linux/usb/ch9.h> |
| 61 | #include <linux/usb/gadget.h> |
| 62 | #include <linux/usb/otg.h> |
| 63 | #include <linux/usb/chipidea.h> |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 64 | #include <linux/usb/of.h> |
Michael Grzeschik | 4f6743d | 2014-02-19 13:41:43 +0800 | [diff] [blame] | 65 | #include <linux/of.h> |
Peter Chen | 1542d9c | 2013-08-14 12:44:03 +0300 | [diff] [blame] | 66 | #include <linux/regulator/consumer.h> |
Peter Chen | 8022d3d | 2014-10-30 09:15:15 +0800 | [diff] [blame] | 67 | #include <linux/usb/ehci_def.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 68 | |
| 69 | #include "ci.h" |
| 70 | #include "udc.h" |
| 71 | #include "bits.h" |
Alexander Shishkin | eb70e5a | 2012-05-11 17:25:54 +0300 | [diff] [blame] | 72 | #include "host.h" |
Peter Chen | c10b4f0 | 2013-08-14 12:44:06 +0300 | [diff] [blame] | 73 | #include "otg.h" |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 74 | #include "otg_fsm.h" |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 75 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 76 | /* Controller register map */ |
Marc Kleine-Budde | 987e7bc | 2014-01-06 10:10:39 +0800 | [diff] [blame] | 77 | static const u8 ci_regs_nolpm[] = { |
| 78 | [CAP_CAPLENGTH] = 0x00U, |
| 79 | [CAP_HCCPARAMS] = 0x08U, |
| 80 | [CAP_DCCPARAMS] = 0x24U, |
| 81 | [CAP_TESTMODE] = 0x38U, |
| 82 | [OP_USBCMD] = 0x00U, |
| 83 | [OP_USBSTS] = 0x04U, |
| 84 | [OP_USBINTR] = 0x08U, |
| 85 | [OP_DEVICEADDR] = 0x14U, |
| 86 | [OP_ENDPTLISTADDR] = 0x18U, |
Peter Chen | 2836267 | 2015-06-18 11:51:53 +0800 | [diff] [blame] | 87 | [OP_TTCTRL] = 0x1CU, |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 88 | [OP_BURSTSIZE] = 0x20U, |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 89 | [OP_ULPI_VIEWPORT] = 0x30U, |
Marc Kleine-Budde | 987e7bc | 2014-01-06 10:10:39 +0800 | [diff] [blame] | 90 | [OP_PORTSC] = 0x44U, |
| 91 | [OP_DEVLC] = 0x84U, |
| 92 | [OP_OTGSC] = 0x64U, |
| 93 | [OP_USBMODE] = 0x68U, |
| 94 | [OP_ENDPTSETUPSTAT] = 0x6CU, |
| 95 | [OP_ENDPTPRIME] = 0x70U, |
| 96 | [OP_ENDPTFLUSH] = 0x74U, |
| 97 | [OP_ENDPTSTAT] = 0x78U, |
| 98 | [OP_ENDPTCOMPLETE] = 0x7CU, |
| 99 | [OP_ENDPTCTRL] = 0x80U, |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 100 | }; |
| 101 | |
Marc Kleine-Budde | 987e7bc | 2014-01-06 10:10:39 +0800 | [diff] [blame] | 102 | static const u8 ci_regs_lpm[] = { |
| 103 | [CAP_CAPLENGTH] = 0x00U, |
| 104 | [CAP_HCCPARAMS] = 0x08U, |
| 105 | [CAP_DCCPARAMS] = 0x24U, |
| 106 | [CAP_TESTMODE] = 0xFCU, |
| 107 | [OP_USBCMD] = 0x00U, |
| 108 | [OP_USBSTS] = 0x04U, |
| 109 | [OP_USBINTR] = 0x08U, |
| 110 | [OP_DEVICEADDR] = 0x14U, |
| 111 | [OP_ENDPTLISTADDR] = 0x18U, |
Peter Chen | 2836267 | 2015-06-18 11:51:53 +0800 | [diff] [blame] | 112 | [OP_TTCTRL] = 0x1CU, |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 113 | [OP_BURSTSIZE] = 0x20U, |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 114 | [OP_ULPI_VIEWPORT] = 0x30U, |
Marc Kleine-Budde | 987e7bc | 2014-01-06 10:10:39 +0800 | [diff] [blame] | 115 | [OP_PORTSC] = 0x44U, |
| 116 | [OP_DEVLC] = 0x84U, |
| 117 | [OP_OTGSC] = 0xC4U, |
| 118 | [OP_USBMODE] = 0xC8U, |
| 119 | [OP_ENDPTSETUPSTAT] = 0xD8U, |
| 120 | [OP_ENDPTPRIME] = 0xDCU, |
| 121 | [OP_ENDPTFLUSH] = 0xE0U, |
| 122 | [OP_ENDPTSTAT] = 0xE4U, |
| 123 | [OP_ENDPTCOMPLETE] = 0xE8U, |
| 124 | [OP_ENDPTCTRL] = 0xECU, |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 125 | }; |
| 126 | |
Nicholas Krause | 158ec07 | 2015-06-27 00:34:48 -0400 | [diff] [blame] | 127 | static void hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 128 | { |
| 129 | int i; |
| 130 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 131 | for (i = 0; i < OP_ENDPTCTRL; i++) |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 132 | ci->hw_bank.regmap[i] = |
| 133 | (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) + |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 134 | (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]); |
| 135 | |
| 136 | for (; i <= OP_LAST; i++) |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 137 | ci->hw_bank.regmap[i] = ci->hw_bank.op + |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 138 | 4 * (i - OP_ENDPTCTRL) + |
| 139 | (is_lpm |
| 140 | ? ci_regs_lpm[OP_ENDPTCTRL] |
| 141 | : ci_regs_nolpm[OP_ENDPTCTRL]); |
| 142 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 143 | } |
| 144 | |
Peter Chen | cb271f3 | 2015-02-11 12:44:55 +0800 | [diff] [blame] | 145 | static enum ci_revision ci_get_revision(struct ci_hdrc *ci) |
| 146 | { |
| 147 | int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION); |
| 148 | enum ci_revision rev = CI_REVISION_UNKNOWN; |
| 149 | |
| 150 | if (ver == 0x2) { |
| 151 | rev = hw_read_id_reg(ci, ID_ID, REVISION) |
| 152 | >> __ffs(REVISION); |
| 153 | rev += CI_REVISION_20; |
| 154 | } else if (ver == 0x0) { |
| 155 | rev = CI_REVISION_1X; |
| 156 | } |
| 157 | |
| 158 | return rev; |
| 159 | } |
| 160 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 161 | /** |
Li Jun | 36304b0 | 2014-04-23 15:56:39 +0800 | [diff] [blame] | 162 | * hw_read_intr_enable: returns interrupt enable register |
| 163 | * |
Peter Chen | 1935388 | 2014-09-22 08:14:17 +0800 | [diff] [blame] | 164 | * @ci: the controller |
| 165 | * |
Li Jun | 36304b0 | 2014-04-23 15:56:39 +0800 | [diff] [blame] | 166 | * This function returns register data |
| 167 | */ |
| 168 | u32 hw_read_intr_enable(struct ci_hdrc *ci) |
| 169 | { |
| 170 | return hw_read(ci, OP_USBINTR, ~0); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * hw_read_intr_status: returns interrupt status register |
| 175 | * |
Peter Chen | 1935388 | 2014-09-22 08:14:17 +0800 | [diff] [blame] | 176 | * @ci: the controller |
| 177 | * |
Li Jun | 36304b0 | 2014-04-23 15:56:39 +0800 | [diff] [blame] | 178 | * This function returns register data |
| 179 | */ |
| 180 | u32 hw_read_intr_status(struct ci_hdrc *ci) |
| 181 | { |
| 182 | return hw_read(ci, OP_USBSTS, ~0); |
| 183 | } |
| 184 | |
| 185 | /** |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 186 | * hw_port_test_set: writes port test mode (execute without interruption) |
| 187 | * @mode: new value |
| 188 | * |
| 189 | * This function returns an error code |
| 190 | */ |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 191 | int hw_port_test_set(struct ci_hdrc *ci, u8 mode) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 192 | { |
| 193 | const u8 TEST_MODE_MAX = 7; |
| 194 | |
| 195 | if (mode > TEST_MODE_MAX) |
| 196 | return -EINVAL; |
| 197 | |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 198 | hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC)); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * hw_port_test_get: reads port test mode value |
| 204 | * |
Peter Chen | 1935388 | 2014-09-22 08:14:17 +0800 | [diff] [blame] | 205 | * @ci: the controller |
| 206 | * |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 207 | * This function returns port test mode value |
| 208 | */ |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 209 | u8 hw_port_test_get(struct ci_hdrc *ci) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 210 | { |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 211 | return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 212 | } |
| 213 | |
Peter Chen | b82613c | 2014-11-26 13:44:28 +0800 | [diff] [blame] | 214 | static void hw_wait_phy_stable(void) |
| 215 | { |
| 216 | /* |
| 217 | * The phy needs some delay to output the stable status from low |
| 218 | * power mode. And for OTGSC, the status inputs are debounced |
| 219 | * using a 1 ms time constant, so, delay 2ms for controller to get |
| 220 | * the stable status, like vbus and id when the phy leaves low power. |
| 221 | */ |
| 222 | usleep_range(2000, 2500); |
| 223 | } |
| 224 | |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 225 | /* The PHY enters/leaves low power mode */ |
| 226 | static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable) |
| 227 | { |
| 228 | enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC; |
| 229 | bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm))); |
| 230 | |
Peter Chen | 6d037db | 2014-11-26 13:44:27 +0800 | [diff] [blame] | 231 | if (enable && !lpm) |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 232 | hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm), |
| 233 | PORTSC_PHCD(ci->hw_bank.lpm)); |
Peter Chen | 6d037db | 2014-11-26 13:44:27 +0800 | [diff] [blame] | 234 | else if (!enable && lpm) |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 235 | hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm), |
| 236 | 0); |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 239 | static int hw_device_init(struct ci_hdrc *ci, void __iomem *base) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 240 | { |
| 241 | u32 reg; |
| 242 | |
| 243 | /* bank is a module variable */ |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 244 | ci->hw_bank.abs = base; |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 245 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 246 | ci->hw_bank.cap = ci->hw_bank.abs; |
Richard Zhao | 77c4400 | 2012-06-29 17:48:53 +0800 | [diff] [blame] | 247 | ci->hw_bank.cap += ci->platdata->capoffset; |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 248 | ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 249 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 250 | hw_alloc_regmap(ci, false); |
| 251 | reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >> |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 252 | __ffs(HCCPARAMS_LEN); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 253 | ci->hw_bank.lpm = reg; |
Chris Ruehl | aeb2c12 | 2013-12-06 16:35:12 +0800 | [diff] [blame] | 254 | if (reg) |
| 255 | hw_alloc_regmap(ci, !!reg); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 256 | ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs; |
| 257 | ci->hw_bank.size += OP_LAST; |
| 258 | ci->hw_bank.size /= sizeof(u32); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 259 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 260 | reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >> |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 261 | __ffs(DCCPARAMS_DEN); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 262 | ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */ |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 263 | |
Richard Zhao | 09c94e6 | 2012-05-15 21:58:18 +0800 | [diff] [blame] | 264 | if (ci->hw_ep_max > ENDPT_MAX) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 265 | return -ENODEV; |
| 266 | |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 267 | ci_hdrc_enter_lpm(ci, false); |
| 268 | |
Peter Chen | c344b51 | 2013-08-14 12:44:09 +0300 | [diff] [blame] | 269 | /* Disable all interrupts bits */ |
| 270 | hw_write(ci, OP_USBINTR, 0xffffffff, 0); |
| 271 | |
| 272 | /* Clear all interrupts status bits*/ |
| 273 | hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff); |
| 274 | |
Peter Chen | cb271f3 | 2015-02-11 12:44:55 +0800 | [diff] [blame] | 275 | ci->rev = ci_get_revision(ci); |
| 276 | |
| 277 | dev_dbg(ci->dev, |
| 278 | "ChipIdea HDRC found, revision: %d, lpm: %d; cap: %p op: %p\n", |
| 279 | ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 280 | |
| 281 | /* setup lock mode ? */ |
| 282 | |
| 283 | /* ENDPTSETUPSTAT is '0' by default */ |
| 284 | |
| 285 | /* HCSPARAMS.bf.ppc SHOULD BE zero for device */ |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 290 | void hw_phymode_configure(struct ci_hdrc *ci) |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 291 | { |
Chris Ruehl | 3b5d3e6 | 2014-01-10 13:51:29 +0800 | [diff] [blame] | 292 | u32 portsc, lpm, sts = 0; |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 293 | |
| 294 | switch (ci->platdata->phy_mode) { |
| 295 | case USBPHY_INTERFACE_MODE_UTMI: |
| 296 | portsc = PORTSC_PTS(PTS_UTMI); |
| 297 | lpm = DEVLC_PTS(PTS_UTMI); |
| 298 | break; |
| 299 | case USBPHY_INTERFACE_MODE_UTMIW: |
| 300 | portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW; |
| 301 | lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW; |
| 302 | break; |
| 303 | case USBPHY_INTERFACE_MODE_ULPI: |
| 304 | portsc = PORTSC_PTS(PTS_ULPI); |
| 305 | lpm = DEVLC_PTS(PTS_ULPI); |
| 306 | break; |
| 307 | case USBPHY_INTERFACE_MODE_SERIAL: |
| 308 | portsc = PORTSC_PTS(PTS_SERIAL); |
| 309 | lpm = DEVLC_PTS(PTS_SERIAL); |
| 310 | sts = 1; |
| 311 | break; |
| 312 | case USBPHY_INTERFACE_MODE_HSIC: |
| 313 | portsc = PORTSC_PTS(PTS_HSIC); |
| 314 | lpm = DEVLC_PTS(PTS_HSIC); |
| 315 | break; |
| 316 | default: |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | if (ci->hw_bank.lpm) { |
| 321 | hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm); |
Chris Ruehl | 3b5d3e6 | 2014-01-10 13:51:29 +0800 | [diff] [blame] | 322 | if (sts) |
| 323 | hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS); |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 324 | } else { |
| 325 | hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc); |
Chris Ruehl | 3b5d3e6 | 2014-01-10 13:51:29 +0800 | [diff] [blame] | 326 | if (sts) |
| 327 | hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS); |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 328 | } |
| 329 | } |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 330 | EXPORT_SYMBOL_GPL(hw_phymode_configure); |
Michael Grzeschik | 40dcd0e | 2013-06-13 17:59:56 +0300 | [diff] [blame] | 331 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 332 | /** |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 333 | * _ci_usb_phy_init: initialize phy taking in account both phy and usb_phy |
| 334 | * interfaces |
| 335 | * @ci: the controller |
| 336 | * |
| 337 | * This function returns an error code if the phy failed to init |
| 338 | */ |
| 339 | static int _ci_usb_phy_init(struct ci_hdrc *ci) |
| 340 | { |
| 341 | int ret; |
| 342 | |
| 343 | if (ci->phy) { |
| 344 | ret = phy_init(ci->phy); |
| 345 | if (ret) |
| 346 | return ret; |
| 347 | |
| 348 | ret = phy_power_on(ci->phy); |
| 349 | if (ret) { |
| 350 | phy_exit(ci->phy); |
| 351 | return ret; |
| 352 | } |
| 353 | } else { |
| 354 | ret = usb_phy_init(ci->usb_phy); |
| 355 | } |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * _ci_usb_phy_exit: deinitialize phy taking in account both phy and usb_phy |
| 362 | * interfaces |
| 363 | * @ci: the controller |
| 364 | */ |
| 365 | static void ci_usb_phy_exit(struct ci_hdrc *ci) |
| 366 | { |
Stephen Boyd | 8feb368 | 2016-12-28 14:56:52 -0800 | [diff] [blame] | 367 | if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL) |
| 368 | return; |
| 369 | |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 370 | if (ci->phy) { |
| 371 | phy_power_off(ci->phy); |
| 372 | phy_exit(ci->phy); |
| 373 | } else { |
| 374 | usb_phy_shutdown(ci->usb_phy); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 379 | * ci_usb_phy_init: initialize phy according to different phy type |
| 380 | * @ci: the controller |
Peter Chen | 1935388 | 2014-09-22 08:14:17 +0800 | [diff] [blame] | 381 | * |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 382 | * This function returns an error code if usb_phy_init has failed |
| 383 | */ |
| 384 | static int ci_usb_phy_init(struct ci_hdrc *ci) |
| 385 | { |
| 386 | int ret; |
| 387 | |
Stephen Boyd | 8feb368 | 2016-12-28 14:56:52 -0800 | [diff] [blame] | 388 | if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL) |
| 389 | return 0; |
| 390 | |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 391 | switch (ci->platdata->phy_mode) { |
| 392 | case USBPHY_INTERFACE_MODE_UTMI: |
| 393 | case USBPHY_INTERFACE_MODE_UTMIW: |
| 394 | case USBPHY_INTERFACE_MODE_HSIC: |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 395 | ret = _ci_usb_phy_init(ci); |
Peter Chen | b82613c | 2014-11-26 13:44:28 +0800 | [diff] [blame] | 396 | if (!ret) |
| 397 | hw_wait_phy_stable(); |
| 398 | else |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 399 | return ret; |
| 400 | hw_phymode_configure(ci); |
| 401 | break; |
| 402 | case USBPHY_INTERFACE_MODE_ULPI: |
| 403 | case USBPHY_INTERFACE_MODE_SERIAL: |
| 404 | hw_phymode_configure(ci); |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 405 | ret = _ci_usb_phy_init(ci); |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 406 | if (ret) |
| 407 | return ret; |
| 408 | break; |
| 409 | default: |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 410 | ret = _ci_usb_phy_init(ci); |
Peter Chen | b82613c | 2014-11-26 13:44:28 +0800 | [diff] [blame] | 411 | if (!ret) |
| 412 | hw_wait_phy_stable(); |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
Peter Chen | bf9c85e | 2015-03-17 10:40:50 +0800 | [diff] [blame] | 418 | |
| 419 | /** |
| 420 | * ci_platform_configure: do controller configure |
| 421 | * @ci: the controller |
| 422 | * |
| 423 | */ |
| 424 | void ci_platform_configure(struct ci_hdrc *ci) |
| 425 | { |
Peter Chen | 8022d3d | 2014-10-30 09:15:15 +0800 | [diff] [blame] | 426 | bool is_device_mode, is_host_mode; |
| 427 | |
| 428 | is_device_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_DC; |
| 429 | is_host_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_HC; |
| 430 | |
Stephen Boyd | 490b63e | 2017-01-25 14:32:43 -0800 | [diff] [blame] | 431 | if (is_device_mode) { |
| 432 | phy_set_mode(ci->phy, PHY_MODE_USB_DEVICE); |
Peter Chen | 8022d3d | 2014-10-30 09:15:15 +0800 | [diff] [blame] | 433 | |
Stephen Boyd | 490b63e | 2017-01-25 14:32:43 -0800 | [diff] [blame] | 434 | if (ci->platdata->flags & CI_HDRC_DISABLE_DEVICE_STREAMING) |
| 435 | hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, |
| 436 | USBMODE_CI_SDIS); |
| 437 | } |
| 438 | |
| 439 | if (is_host_mode) { |
| 440 | phy_set_mode(ci->phy, PHY_MODE_USB_HOST); |
| 441 | |
| 442 | if (ci->platdata->flags & CI_HDRC_DISABLE_HOST_STREAMING) |
| 443 | hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, |
| 444 | USBMODE_CI_SDIS); |
| 445 | } |
Peter Chen | bf9c85e | 2015-03-17 10:40:50 +0800 | [diff] [blame] | 446 | |
| 447 | if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) { |
| 448 | if (ci->hw_bank.lpm) |
| 449 | hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC); |
| 450 | else |
| 451 | hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC); |
| 452 | } |
| 453 | |
| 454 | if (ci->platdata->flags & CI_HDRC_SET_NON_ZERO_TTHA) |
| 455 | hw_write(ci, OP_TTCTRL, TTCTRL_TTHA_MASK, TTCTRL_TTHA); |
Peter Chen | df96ed8 | 2014-09-22 16:45:39 +0800 | [diff] [blame] | 456 | |
| 457 | hw_write(ci, OP_USBCMD, 0xff0000, ci->platdata->itc_setting << 16); |
| 458 | |
Peter Chen | 6566871 | 2015-03-17 14:21:00 +0800 | [diff] [blame] | 459 | if (ci->platdata->flags & CI_HDRC_OVERRIDE_AHB_BURST) |
| 460 | hw_write_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK, |
| 461 | ci->platdata->ahb_burst_config); |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 462 | |
| 463 | /* override burst size, take effect only when ahb_burst_config is 0 */ |
| 464 | if (!hw_read_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK)) { |
| 465 | if (ci->platdata->flags & CI_HDRC_OVERRIDE_TX_BURST) |
| 466 | hw_write(ci, OP_BURSTSIZE, TX_BURST_MASK, |
| 467 | ci->platdata->tx_burst_size << __ffs(TX_BURST_MASK)); |
| 468 | |
| 469 | if (ci->platdata->flags & CI_HDRC_OVERRIDE_RX_BURST) |
| 470 | hw_write(ci, OP_BURSTSIZE, RX_BURST_MASK, |
| 471 | ci->platdata->rx_burst_size); |
| 472 | } |
Peter Chen | bf9c85e | 2015-03-17 10:40:50 +0800 | [diff] [blame] | 473 | } |
| 474 | |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 475 | /** |
Peter Chen | cdd278f | 2014-11-26 13:44:32 +0800 | [diff] [blame] | 476 | * hw_controller_reset: do controller reset |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 477 | * @ci: the controller |
| 478 | * |
| 479 | * This function returns an error code |
| 480 | */ |
Peter Chen | cdd278f | 2014-11-26 13:44:32 +0800 | [diff] [blame] | 481 | static int hw_controller_reset(struct ci_hdrc *ci) |
| 482 | { |
| 483 | int count = 0; |
| 484 | |
| 485 | hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST); |
| 486 | while (hw_read(ci, OP_USBCMD, USBCMD_RST)) { |
| 487 | udelay(10); |
| 488 | if (count++ > 1000) |
| 489 | return -ETIMEDOUT; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * hw_device_reset: resets chip (execute without interruption) |
| 497 | * @ci: the controller |
| 498 | * |
| 499 | * This function returns an error code |
| 500 | */ |
Peter Chen | 5b15730 | 2014-11-26 13:44:33 +0800 | [diff] [blame] | 501 | int hw_device_reset(struct ci_hdrc *ci) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 502 | { |
Peter Chen | cdd278f | 2014-11-26 13:44:32 +0800 | [diff] [blame] | 503 | int ret; |
| 504 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 505 | /* should flush & stop before reset */ |
| 506 | hw_write(ci, OP_ENDPTFLUSH, ~0, ~0); |
| 507 | hw_write(ci, OP_USBCMD, USBCMD_RS, 0); |
| 508 | |
Peter Chen | cdd278f | 2014-11-26 13:44:32 +0800 | [diff] [blame] | 509 | ret = hw_controller_reset(ci); |
| 510 | if (ret) { |
| 511 | dev_err(ci->dev, "error resetting controller, ret=%d\n", ret); |
| 512 | return ret; |
| 513 | } |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 514 | |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 515 | if (ci->platdata->notify_event) { |
| 516 | ret = ci->platdata->notify_event(ci, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 517 | CI_HDRC_CONTROLLER_RESET_EVENT); |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 518 | if (ret) |
| 519 | return ret; |
| 520 | } |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 521 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 522 | /* USBMODE should be configured step by step */ |
| 523 | hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE); |
Peter Chen | 5b15730 | 2014-11-26 13:44:33 +0800 | [diff] [blame] | 524 | hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 525 | /* HW >= 2.3 */ |
| 526 | hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); |
| 527 | |
Peter Chen | 5b15730 | 2014-11-26 13:44:33 +0800 | [diff] [blame] | 528 | if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) { |
| 529 | pr_err("cannot enter in %s device mode", ci_role(ci)->name); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 530 | pr_err("lpm = %i", ci->hw_bank.lpm); |
| 531 | return -ENODEV; |
| 532 | } |
| 533 | |
Peter Chen | bf9c85e | 2015-03-17 10:40:50 +0800 | [diff] [blame] | 534 | ci_platform_configure(ci); |
| 535 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 539 | static irqreturn_t ci_irq(int irq, void *data) |
| 540 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 541 | struct ci_hdrc *ci = data; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 542 | irqreturn_t ret = IRQ_NONE; |
Richard Zhao | b183c19 | 2012-09-12 14:58:11 +0300 | [diff] [blame] | 543 | u32 otgsc = 0; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 544 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 545 | if (ci->in_lpm) { |
| 546 | disable_irq_nosync(irq); |
| 547 | ci->wakeup_int = true; |
| 548 | pm_runtime_get(ci->dev); |
| 549 | return IRQ_HANDLED; |
| 550 | } |
| 551 | |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 552 | if (ci->is_otg) { |
Li Jun | 0c33bf7 | 2014-04-23 15:56:38 +0800 | [diff] [blame] | 553 | otgsc = hw_read_otgsc(ci, ~0); |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 554 | if (ci_otg_is_fsm_mode(ci)) { |
| 555 | ret = ci_otg_fsm_irq(ci); |
| 556 | if (ret == IRQ_HANDLED) |
| 557 | return ret; |
| 558 | } |
| 559 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 560 | |
Peter Chen | a107f8c | 2013-08-14 12:44:11 +0300 | [diff] [blame] | 561 | /* |
| 562 | * Handle id change interrupt, it indicates device/host function |
| 563 | * switch. |
| 564 | */ |
| 565 | if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) { |
| 566 | ci->id_event = true; |
Li Jun | 0c33bf7 | 2014-04-23 15:56:38 +0800 | [diff] [blame] | 567 | /* Clear ID change irq status */ |
| 568 | hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS); |
Peter Chen | be6b0c1 | 2014-05-23 08:12:49 +0800 | [diff] [blame] | 569 | ci_otg_queue_work(ci); |
Peter Chen | a107f8c | 2013-08-14 12:44:11 +0300 | [diff] [blame] | 570 | return IRQ_HANDLED; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 571 | } |
| 572 | |
Peter Chen | a107f8c | 2013-08-14 12:44:11 +0300 | [diff] [blame] | 573 | /* |
| 574 | * Handle vbus change interrupt, it indicates device connection |
| 575 | * and disconnection events. |
| 576 | */ |
| 577 | if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) { |
| 578 | ci->b_sess_valid_event = true; |
Li Jun | 0c33bf7 | 2014-04-23 15:56:38 +0800 | [diff] [blame] | 579 | /* Clear BSV irq */ |
| 580 | hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS); |
Peter Chen | be6b0c1 | 2014-05-23 08:12:49 +0800 | [diff] [blame] | 581 | ci_otg_queue_work(ci); |
Peter Chen | a107f8c | 2013-08-14 12:44:11 +0300 | [diff] [blame] | 582 | return IRQ_HANDLED; |
| 583 | } |
| 584 | |
| 585 | /* Handle device/host interrupt */ |
| 586 | if (ci->role != CI_ROLE_END) |
| 587 | ret = ci_role(ci)->irq(ci); |
| 588 | |
Richard Zhao | b183c19 | 2012-09-12 14:58:11 +0300 | [diff] [blame] | 589 | return ret; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 590 | } |
| 591 | |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 592 | static int ci_cable_notifier(struct notifier_block *nb, unsigned long event, |
| 593 | void *ptr) |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 594 | { |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 595 | struct ci_hdrc_cable *cbl = container_of(nb, struct ci_hdrc_cable, nb); |
| 596 | struct ci_hdrc *ci = cbl->ci; |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 597 | |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 598 | cbl->connected = event; |
| 599 | cbl->changed = true; |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 600 | |
| 601 | ci_irq(ci->irq, ci); |
| 602 | return NOTIFY_DONE; |
| 603 | } |
| 604 | |
Peter Chen | 1542d9c | 2013-08-14 12:44:03 +0300 | [diff] [blame] | 605 | static int ci_get_platdata(struct device *dev, |
| 606 | struct ci_hdrc_platform_data *platdata) |
| 607 | { |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 608 | struct extcon_dev *ext_vbus, *ext_id; |
| 609 | struct ci_hdrc_cable *cable; |
Li Jun | 7974235 | 2015-07-09 15:18:45 +0800 | [diff] [blame] | 610 | int ret; |
| 611 | |
Peter Chen | c22600c | 2013-09-17 12:37:22 +0800 | [diff] [blame] | 612 | if (!platdata->phy_mode) |
| 613 | platdata->phy_mode = of_usb_get_phy_mode(dev->of_node); |
| 614 | |
| 615 | if (!platdata->dr_mode) |
Heikki Krogerus | 06e7114 | 2015-09-21 11:14:34 +0300 | [diff] [blame] | 616 | platdata->dr_mode = usb_get_dr_mode(dev); |
Peter Chen | c22600c | 2013-09-17 12:37:22 +0800 | [diff] [blame] | 617 | |
| 618 | if (platdata->dr_mode == USB_DR_MODE_UNKNOWN) |
| 619 | platdata->dr_mode = USB_DR_MODE_OTG; |
| 620 | |
Peter Chen | c2ec3a7 | 2013-10-30 09:19:29 +0800 | [diff] [blame] | 621 | if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) { |
| 622 | /* Get the vbus regulator */ |
| 623 | platdata->reg_vbus = devm_regulator_get(dev, "vbus"); |
| 624 | if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) { |
| 625 | return -EPROBE_DEFER; |
| 626 | } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) { |
Mickael Maison | 6629467 | 2014-11-26 13:44:38 +0800 | [diff] [blame] | 627 | /* no vbus regulator is needed */ |
Peter Chen | c2ec3a7 | 2013-10-30 09:19:29 +0800 | [diff] [blame] | 628 | platdata->reg_vbus = NULL; |
| 629 | } else if (IS_ERR(platdata->reg_vbus)) { |
| 630 | dev_err(dev, "Getting regulator error: %ld\n", |
| 631 | PTR_ERR(platdata->reg_vbus)); |
| 632 | return PTR_ERR(platdata->reg_vbus); |
| 633 | } |
Peter Chen | f6a9ff0 | 2014-08-19 09:51:56 +0800 | [diff] [blame] | 634 | /* Get TPL support */ |
| 635 | if (!platdata->tpl_support) |
| 636 | platdata->tpl_support = |
| 637 | of_usb_host_tpl_support(dev->of_node); |
Peter Chen | c2ec3a7 | 2013-10-30 09:19:29 +0800 | [diff] [blame] | 638 | } |
| 639 | |
Li Jun | 7974235 | 2015-07-09 15:18:45 +0800 | [diff] [blame] | 640 | if (platdata->dr_mode == USB_DR_MODE_OTG) { |
| 641 | /* We can support HNP and SRP of OTG 2.0 */ |
| 642 | platdata->ci_otg_caps.otg_rev = 0x0200; |
| 643 | platdata->ci_otg_caps.hnp_support = true; |
| 644 | platdata->ci_otg_caps.srp_support = true; |
| 645 | |
| 646 | /* Update otg capabilities by DT properties */ |
| 647 | ret = of_usb_update_otg_caps(dev->of_node, |
| 648 | &platdata->ci_otg_caps); |
| 649 | if (ret) |
| 650 | return ret; |
| 651 | } |
| 652 | |
Heikki Krogerus | 63863b9 | 2015-09-21 11:14:32 +0300 | [diff] [blame] | 653 | if (usb_get_maximum_speed(dev) == USB_SPEED_FULL) |
Michael Grzeschik | 4f6743d | 2014-02-19 13:41:43 +0800 | [diff] [blame] | 654 | platdata->flags |= CI_HDRC_FORCE_FULLSPEED; |
| 655 | |
Saurabh Sengar | 4b19b78 | 2015-11-18 09:40:12 +0530 | [diff] [blame] | 656 | of_property_read_u32(dev->of_node, "phy-clkgate-delay-us", |
Fabio Estevam | 1fbf462 | 2015-09-08 22:18:14 -0300 | [diff] [blame] | 657 | &platdata->phy_clkgate_delay_us); |
| 658 | |
Peter Chen | df96ed8 | 2014-09-22 16:45:39 +0800 | [diff] [blame] | 659 | platdata->itc_setting = 1; |
Peter Chen | df96ed8 | 2014-09-22 16:45:39 +0800 | [diff] [blame] | 660 | |
Saurabh Sengar | 4b19b78 | 2015-11-18 09:40:12 +0530 | [diff] [blame] | 661 | of_property_read_u32(dev->of_node, "itc-setting", |
| 662 | &platdata->itc_setting); |
| 663 | |
| 664 | ret = of_property_read_u32(dev->of_node, "ahb-burst-config", |
| 665 | &platdata->ahb_burst_config); |
| 666 | if (!ret) { |
Peter Chen | 6566871 | 2015-03-17 14:21:00 +0800 | [diff] [blame] | 667 | platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST; |
Saurabh Sengar | 4b19b78 | 2015-11-18 09:40:12 +0530 | [diff] [blame] | 668 | } else if (ret != -EINVAL) { |
| 669 | dev_err(dev, "failed to get ahb-burst-config\n"); |
| 670 | return ret; |
Peter Chen | 6566871 | 2015-03-17 14:21:00 +0800 | [diff] [blame] | 671 | } |
| 672 | |
Saurabh Sengar | 4b19b78 | 2015-11-18 09:40:12 +0530 | [diff] [blame] | 673 | ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword", |
| 674 | &platdata->tx_burst_size); |
| 675 | if (!ret) { |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 676 | platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST; |
Saurabh Sengar | 4b19b78 | 2015-11-18 09:40:12 +0530 | [diff] [blame] | 677 | } else if (ret != -EINVAL) { |
| 678 | dev_err(dev, "failed to get tx-burst-size-dword\n"); |
| 679 | return ret; |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 680 | } |
| 681 | |
Saurabh Sengar | 4b19b78 | 2015-11-18 09:40:12 +0530 | [diff] [blame] | 682 | ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword", |
| 683 | &platdata->rx_burst_size); |
| 684 | if (!ret) { |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 685 | platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST; |
Saurabh Sengar | 4b19b78 | 2015-11-18 09:40:12 +0530 | [diff] [blame] | 686 | } else if (ret != -EINVAL) { |
| 687 | dev_err(dev, "failed to get rx-burst-size-dword\n"); |
| 688 | return ret; |
Peter Chen | 96625ea | 2015-03-17 17:32:45 +0800 | [diff] [blame] | 689 | } |
| 690 | |
Peter Chen | aa73818 | 2016-02-01 14:23:44 +0800 | [diff] [blame] | 691 | if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL)) |
| 692 | platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA; |
| 693 | |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 694 | ext_id = ERR_PTR(-ENODEV); |
| 695 | ext_vbus = ERR_PTR(-ENODEV); |
| 696 | if (of_property_read_bool(dev->of_node, "extcon")) { |
| 697 | /* Each one of them is not mandatory */ |
| 698 | ext_vbus = extcon_get_edev_by_phandle(dev, 0); |
| 699 | if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV) |
| 700 | return PTR_ERR(ext_vbus); |
| 701 | |
| 702 | ext_id = extcon_get_edev_by_phandle(dev, 1); |
| 703 | if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV) |
| 704 | return PTR_ERR(ext_id); |
| 705 | } |
| 706 | |
| 707 | cable = &platdata->vbus_extcon; |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 708 | cable->nb.notifier_call = ci_cable_notifier; |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 709 | cable->edev = ext_vbus; |
| 710 | |
| 711 | if (!IS_ERR(ext_vbus)) { |
Chanwoo Choi | 3f991aa | 2016-11-30 14:57:33 +0900 | [diff] [blame] | 712 | ret = extcon_get_state(cable->edev, EXTCON_USB); |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 713 | if (ret) |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 714 | cable->connected = true; |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 715 | else |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 716 | cable->connected = false; |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | cable = &platdata->id_extcon; |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 720 | cable->nb.notifier_call = ci_cable_notifier; |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 721 | cable->edev = ext_id; |
| 722 | |
| 723 | if (!IS_ERR(ext_id)) { |
Chanwoo Choi | 3f991aa | 2016-11-30 14:57:33 +0900 | [diff] [blame] | 724 | ret = extcon_get_state(cable->edev, EXTCON_USB_HOST); |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 725 | if (ret) |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 726 | cable->connected = true; |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 727 | else |
Stephen Boyd | 5cc4926 | 2017-01-20 15:11:55 +0800 | [diff] [blame] | 728 | cable->connected = false; |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 729 | } |
Peter Chen | 1542d9c | 2013-08-14 12:44:03 +0300 | [diff] [blame] | 730 | return 0; |
| 731 | } |
| 732 | |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 733 | static int ci_extcon_register(struct ci_hdrc *ci) |
| 734 | { |
| 735 | struct ci_hdrc_cable *id, *vbus; |
| 736 | int ret; |
| 737 | |
| 738 | id = &ci->platdata->id_extcon; |
| 739 | id->ci = ci; |
Peter Chen | 7c3a8b8 | 2017-06-23 14:39:27 +0800 | [diff] [blame] | 740 | if (!IS_ERR_OR_NULL(id->edev)) { |
Chanwoo Choi | 3f991aa | 2016-11-30 14:57:33 +0900 | [diff] [blame] | 741 | ret = devm_extcon_register_notifier(ci->dev, id->edev, |
| 742 | EXTCON_USB_HOST, &id->nb); |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 743 | if (ret < 0) { |
| 744 | dev_err(ci->dev, "register ID failed\n"); |
| 745 | return ret; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | vbus = &ci->platdata->vbus_extcon; |
| 750 | vbus->ci = ci; |
Peter Chen | 7c3a8b8 | 2017-06-23 14:39:27 +0800 | [diff] [blame] | 751 | if (!IS_ERR_OR_NULL(vbus->edev)) { |
Chanwoo Choi | 3f991aa | 2016-11-30 14:57:33 +0900 | [diff] [blame] | 752 | ret = devm_extcon_register_notifier(ci->dev, vbus->edev, |
| 753 | EXTCON_USB, &vbus->nb); |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 754 | if (ret < 0) { |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 755 | dev_err(ci->dev, "register VBUS failed\n"); |
| 756 | return ret; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 763 | static DEFINE_IDA(ci_ida); |
| 764 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 765 | struct platform_device *ci_hdrc_add_device(struct device *dev, |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 766 | struct resource *res, int nres, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 767 | struct ci_hdrc_platform_data *platdata) |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 768 | { |
| 769 | struct platform_device *pdev; |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 770 | int id, ret; |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 771 | |
Peter Chen | 1542d9c | 2013-08-14 12:44:03 +0300 | [diff] [blame] | 772 | ret = ci_get_platdata(dev, platdata); |
| 773 | if (ret) |
| 774 | return ERR_PTR(ret); |
| 775 | |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 776 | id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL); |
| 777 | if (id < 0) |
| 778 | return ERR_PTR(id); |
| 779 | |
| 780 | pdev = platform_device_alloc("ci_hdrc", id); |
| 781 | if (!pdev) { |
| 782 | ret = -ENOMEM; |
| 783 | goto put_id; |
| 784 | } |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 785 | |
| 786 | pdev->dev.parent = dev; |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 787 | |
| 788 | ret = platform_device_add_resources(pdev, res, nres); |
| 789 | if (ret) |
| 790 | goto err; |
| 791 | |
| 792 | ret = platform_device_add_data(pdev, platdata, sizeof(*platdata)); |
| 793 | if (ret) |
| 794 | goto err; |
| 795 | |
| 796 | ret = platform_device_add(pdev); |
| 797 | if (ret) |
| 798 | goto err; |
| 799 | |
| 800 | return pdev; |
| 801 | |
| 802 | err: |
| 803 | platform_device_put(pdev); |
Richard Zhao | fe6e125 | 2012-07-07 22:56:42 +0800 | [diff] [blame] | 804 | put_id: |
| 805 | ida_simple_remove(&ci_ida, id); |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 806 | return ERR_PTR(ret); |
| 807 | } |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 808 | EXPORT_SYMBOL_GPL(ci_hdrc_add_device); |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 809 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 810 | void ci_hdrc_remove_device(struct platform_device *pdev) |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 811 | { |
Lothar Waßmann | 98c3553 | 2012-11-22 10:11:25 +0100 | [diff] [blame] | 812 | int id = pdev->id; |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 813 | platform_device_unregister(pdev); |
Lothar Waßmann | 98c3553 | 2012-11-22 10:11:25 +0100 | [diff] [blame] | 814 | ida_simple_remove(&ci_ida, id); |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 815 | } |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 816 | EXPORT_SYMBOL_GPL(ci_hdrc_remove_device); |
Richard Zhao | cbc6dc2 | 2012-07-07 22:56:41 +0800 | [diff] [blame] | 817 | |
Peter Chen | 3f124d2 | 2013-08-14 12:44:07 +0300 | [diff] [blame] | 818 | static inline void ci_role_destroy(struct ci_hdrc *ci) |
| 819 | { |
| 820 | ci_hdrc_gadget_destroy(ci); |
| 821 | ci_hdrc_host_destroy(ci); |
Jisheng Zhang | c4a0bbb | 2017-04-26 16:59:34 +0800 | [diff] [blame] | 822 | if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) |
Peter Chen | cbec6bd | 2013-08-14 12:44:10 +0300 | [diff] [blame] | 823 | ci_hdrc_otg_destroy(ci); |
Peter Chen | 3f124d2 | 2013-08-14 12:44:07 +0300 | [diff] [blame] | 824 | } |
| 825 | |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 826 | static void ci_get_otg_capable(struct ci_hdrc *ci) |
| 827 | { |
| 828 | if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG) |
| 829 | ci->is_otg = false; |
| 830 | else |
| 831 | ci->is_otg = (hw_read(ci, CAP_DCCPARAMS, |
| 832 | DCCPARAMS_DC | DCCPARAMS_HC) |
| 833 | == (DCCPARAMS_DC | DCCPARAMS_HC)); |
Peter Chen | 2e37cfd | 2015-02-11 12:44:51 +0800 | [diff] [blame] | 834 | if (ci->is_otg) { |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 835 | dev_dbg(ci->dev, "It is OTG capable controller\n"); |
Peter Chen | 2e37cfd | 2015-02-11 12:44:51 +0800 | [diff] [blame] | 836 | /* Disable and clear all OTG irq */ |
| 837 | hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS, |
| 838 | OTGSC_INT_STATUS_BITS); |
| 839 | } |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 840 | } |
| 841 | |
Peter Chen | a932a80 | 2017-03-27 10:54:27 +0800 | [diff] [blame] | 842 | static ssize_t ci_role_show(struct device *dev, struct device_attribute *attr, |
| 843 | char *buf) |
| 844 | { |
| 845 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 846 | |
Michael Thalmeier | cbb22eb | 2017-05-19 10:32:09 +0200 | [diff] [blame] | 847 | if (ci->role != CI_ROLE_END) |
| 848 | return sprintf(buf, "%s\n", ci_role(ci)->name); |
| 849 | |
| 850 | return 0; |
Peter Chen | a932a80 | 2017-03-27 10:54:27 +0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | static ssize_t ci_role_store(struct device *dev, |
| 854 | struct device_attribute *attr, const char *buf, size_t n) |
| 855 | { |
| 856 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 857 | enum ci_role role; |
| 858 | int ret; |
| 859 | |
| 860 | if (!(ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])) { |
| 861 | dev_warn(dev, "Current configuration is not dual-role, quit\n"); |
| 862 | return -EPERM; |
| 863 | } |
| 864 | |
| 865 | for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++) |
| 866 | if (!strncmp(buf, ci->roles[role]->name, |
| 867 | strlen(ci->roles[role]->name))) |
| 868 | break; |
| 869 | |
| 870 | if (role == CI_ROLE_END || role == ci->role) |
| 871 | return -EINVAL; |
| 872 | |
| 873 | pm_runtime_get_sync(dev); |
| 874 | disable_irq(ci->irq); |
| 875 | ci_role_stop(ci); |
| 876 | ret = ci_role_start(ci, role); |
| 877 | if (!ret && ci->role == CI_ROLE_GADGET) |
| 878 | ci_handle_vbus_change(ci); |
| 879 | enable_irq(ci->irq); |
| 880 | pm_runtime_put_sync(dev); |
| 881 | |
| 882 | return (ret == 0) ? n : ret; |
| 883 | } |
| 884 | static DEVICE_ATTR(role, 0644, ci_role_show, ci_role_store); |
| 885 | |
| 886 | static struct attribute *ci_attrs[] = { |
| 887 | &dev_attr_role.attr, |
| 888 | NULL, |
| 889 | }; |
| 890 | |
Arvind Yadav | a351a2b | 2017-08-04 17:36:38 +0530 | [diff] [blame] | 891 | static const struct attribute_group ci_attr_group = { |
Peter Chen | a932a80 | 2017-03-27 10:54:27 +0800 | [diff] [blame] | 892 | .attrs = ci_attrs, |
| 893 | }; |
| 894 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 895 | static int ci_hdrc_probe(struct platform_device *pdev) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 896 | { |
| 897 | struct device *dev = &pdev->dev; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 898 | struct ci_hdrc *ci; |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 899 | struct resource *res; |
| 900 | void __iomem *base; |
| 901 | int ret; |
Sascha Hauer | 691962d | 2013-06-13 17:59:57 +0300 | [diff] [blame] | 902 | enum usb_dr_mode dr_mode; |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 903 | |
Jingoo Han | fad5674 | 2014-02-19 13:41:42 +0800 | [diff] [blame] | 904 | if (!dev_get_platdata(dev)) { |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 905 | dev_err(dev, "platform data missing\n"); |
| 906 | return -ENODEV; |
| 907 | } |
| 908 | |
| 909 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Felipe Balbi | 1929081 | 2013-03-30 02:46:27 +0200 | [diff] [blame] | 910 | base = devm_ioremap_resource(dev, res); |
| 911 | if (IS_ERR(base)) |
| 912 | return PTR_ERR(base); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 913 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 914 | ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL); |
Fabio Estevam | d0f9924 | 2014-11-26 13:44:23 +0800 | [diff] [blame] | 915 | if (!ci) |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 916 | return -ENOMEM; |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 917 | |
Peter Chen | a5d906b | 2016-11-15 18:05:33 +0800 | [diff] [blame] | 918 | spin_lock_init(&ci->lock); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 919 | ci->dev = dev; |
Jingoo Han | fad5674 | 2014-02-19 13:41:42 +0800 | [diff] [blame] | 920 | ci->platdata = dev_get_platdata(dev); |
Peter Chen | ed8f831 | 2014-01-10 13:51:27 +0800 | [diff] [blame] | 921 | ci->imx28_write_fix = !!(ci->platdata->flags & |
| 922 | CI_HDRC_IMX28_WRITE_FIX); |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 923 | ci->supports_runtime_pm = !!(ci->platdata->flags & |
| 924 | CI_HDRC_SUPPORTS_RUNTIME_PM); |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 925 | platform_set_drvdata(pdev, ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 926 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 927 | ret = hw_device_init(ci, base); |
| 928 | if (ret < 0) { |
| 929 | dev_err(dev, "can't initialize hardware\n"); |
| 930 | return -ENODEV; |
| 931 | } |
| 932 | |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 933 | ret = ci_ulpi_init(ci); |
| 934 | if (ret) |
| 935 | return ret; |
| 936 | |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 937 | if (ci->platdata->phy) { |
| 938 | ci->phy = ci->platdata->phy; |
| 939 | } else if (ci->platdata->usb_phy) { |
Antoine Tenart | ef44cb4 | 2014-10-30 18:41:16 +0100 | [diff] [blame] | 940 | ci->usb_phy = ci->platdata->usb_phy; |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 941 | } else { |
Antoine Tenart | 21a5b57 | 2014-11-26 13:44:35 +0800 | [diff] [blame] | 942 | ci->phy = devm_phy_get(dev->parent, "usb-phy"); |
| 943 | ci->usb_phy = devm_usb_get_phy(dev->parent, USB_PHY_TYPE_USB2); |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 944 | |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 945 | /* if both generic PHY and USB PHY layers aren't enabled */ |
| 946 | if (PTR_ERR(ci->phy) == -ENOSYS && |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 947 | PTR_ERR(ci->usb_phy) == -ENXIO) { |
| 948 | ret = -ENXIO; |
| 949 | goto ulpi_exit; |
| 950 | } |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 951 | |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 952 | if (IS_ERR(ci->phy) && IS_ERR(ci->usb_phy)) { |
| 953 | ret = -EPROBE_DEFER; |
| 954 | goto ulpi_exit; |
| 955 | } |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 956 | |
| 957 | if (IS_ERR(ci->phy)) |
| 958 | ci->phy = NULL; |
| 959 | else if (IS_ERR(ci->usb_phy)) |
| 960 | ci->usb_phy = NULL; |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 961 | } |
| 962 | |
Peter Chen | d03cccf | 2014-04-23 15:56:37 +0800 | [diff] [blame] | 963 | ret = ci_usb_phy_init(ci); |
Peter Chen | 74475ed | 2013-09-24 12:47:53 +0800 | [diff] [blame] | 964 | if (ret) { |
| 965 | dev_err(dev, "unable to init phy: %d\n", ret); |
| 966 | return ret; |
| 967 | } |
| 968 | |
Alexander Shishkin | eb70e5a | 2012-05-11 17:25:54 +0300 | [diff] [blame] | 969 | ci->hw_bank.phys = res->start; |
| 970 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 971 | ci->irq = platform_get_irq(pdev, 0); |
| 972 | if (ci->irq < 0) { |
| 973 | dev_err(dev, "missing IRQ\n"); |
Fabio Estevam | 42d1821 | 2014-02-19 13:41:44 +0800 | [diff] [blame] | 974 | ret = ci->irq; |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 975 | goto deinit_phy; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 976 | } |
| 977 | |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 978 | ci_get_otg_capable(ci); |
| 979 | |
Sascha Hauer | 691962d | 2013-06-13 17:59:57 +0300 | [diff] [blame] | 980 | dr_mode = ci->platdata->dr_mode; |
| 981 | /* initialize role(s) before the interrupt is requested */ |
| 982 | if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) { |
| 983 | ret = ci_hdrc_host_init(ci); |
Jisheng Zhang | c4a0bbb | 2017-04-26 16:59:34 +0800 | [diff] [blame] | 984 | if (ret) { |
| 985 | if (ret == -ENXIO) |
| 986 | dev_info(dev, "doesn't support host\n"); |
| 987 | else |
| 988 | goto deinit_phy; |
| 989 | } |
Sascha Hauer | 691962d | 2013-06-13 17:59:57 +0300 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) { |
| 993 | ret = ci_hdrc_gadget_init(ci); |
Jisheng Zhang | c4a0bbb | 2017-04-26 16:59:34 +0800 | [diff] [blame] | 994 | if (ret) { |
| 995 | if (ret == -ENXIO) |
| 996 | dev_info(dev, "doesn't support gadget\n"); |
| 997 | else |
| 998 | goto deinit_host; |
| 999 | } |
Sascha Hauer | 691962d | 2013-06-13 17:59:57 +0300 | [diff] [blame] | 1000 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1001 | |
| 1002 | if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) { |
| 1003 | dev_err(dev, "no supported roles\n"); |
Peter Chen | 74475ed | 2013-09-24 12:47:53 +0800 | [diff] [blame] | 1004 | ret = -ENODEV; |
Jisheng Zhang | c4a0bbb | 2017-04-26 16:59:34 +0800 | [diff] [blame] | 1005 | goto deinit_gadget; |
Peter Chen | cbec6bd | 2013-08-14 12:44:10 +0300 | [diff] [blame] | 1006 | } |
| 1007 | |
Peter Chen | 27c62c2 | 2014-09-22 08:14:16 +0800 | [diff] [blame] | 1008 | if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) { |
Peter Chen | cbec6bd | 2013-08-14 12:44:10 +0300 | [diff] [blame] | 1009 | ret = ci_hdrc_otg_init(ci); |
| 1010 | if (ret) { |
| 1011 | dev_err(dev, "init otg fails, ret = %d\n", ret); |
Jisheng Zhang | c4a0bbb | 2017-04-26 16:59:34 +0800 | [diff] [blame] | 1012 | goto deinit_gadget; |
Peter Chen | cbec6bd | 2013-08-14 12:44:10 +0300 | [diff] [blame] | 1013 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) { |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 1017 | if (ci->is_otg) { |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 1018 | ci->role = ci_otg_role(ci); |
Li Jun | 0c33bf7 | 2014-04-23 15:56:38 +0800 | [diff] [blame] | 1019 | /* Enable ID change irq */ |
| 1020 | hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE); |
Peter Chen | 577b232 | 2013-08-14 12:44:08 +0300 | [diff] [blame] | 1021 | } else { |
| 1022 | /* |
| 1023 | * If the controller is not OTG capable, but support |
| 1024 | * role switch, the defalt role is gadget, and the |
| 1025 | * user can switch it through debugfs. |
| 1026 | */ |
| 1027 | ci->role = CI_ROLE_GADGET; |
| 1028 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1029 | } else { |
| 1030 | ci->role = ci->roles[CI_ROLE_HOST] |
| 1031 | ? CI_ROLE_HOST |
| 1032 | : CI_ROLE_GADGET; |
| 1033 | } |
| 1034 | |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 1035 | if (!ci_otg_is_fsm_mode(ci)) { |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1036 | /* only update vbus status for peripheral */ |
| 1037 | if (ci->role == CI_ROLE_GADGET) |
| 1038 | ci_handle_vbus_change(ci); |
| 1039 | |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 1040 | ret = ci_role_start(ci, ci->role); |
| 1041 | if (ret) { |
| 1042 | dev_err(dev, "can't start %s role\n", |
| 1043 | ci_role(ci)->name); |
| 1044 | goto stop; |
| 1045 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1046 | } |
| 1047 | |
Peter Chen | 4c503dd | 2014-11-26 13:44:22 +0800 | [diff] [blame] | 1048 | ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED, |
| 1049 | ci->platdata->name, ci); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1050 | if (ret) |
| 1051 | goto stop; |
| 1052 | |
Ivan T. Ivanov | 3ecb3e0 | 2015-09-07 14:45:25 +0300 | [diff] [blame] | 1053 | ret = ci_extcon_register(ci); |
| 1054 | if (ret) |
| 1055 | goto stop; |
| 1056 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1057 | if (ci->supports_runtime_pm) { |
| 1058 | pm_runtime_set_active(&pdev->dev); |
| 1059 | pm_runtime_enable(&pdev->dev); |
| 1060 | pm_runtime_set_autosuspend_delay(&pdev->dev, 2000); |
| 1061 | pm_runtime_mark_last_busy(ci->dev); |
| 1062 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1063 | } |
| 1064 | |
Li Jun | 4dcf720 | 2014-04-23 15:56:50 +0800 | [diff] [blame] | 1065 | if (ci_otg_is_fsm_mode(ci)) |
| 1066 | ci_hdrc_otg_fsm_start(ci); |
| 1067 | |
Peter Chen | f8efa76 | 2015-02-11 12:44:48 +0800 | [diff] [blame] | 1068 | device_set_wakeup_capable(&pdev->dev, true); |
Alexander Shishkin | adf0f73 | 2013-03-30 12:53:53 +0200 | [diff] [blame] | 1069 | ret = dbg_create_files(ci); |
Peter Chen | a932a80 | 2017-03-27 10:54:27 +0800 | [diff] [blame] | 1070 | if (ret) |
| 1071 | goto stop; |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1072 | |
Peter Chen | a932a80 | 2017-03-27 10:54:27 +0800 | [diff] [blame] | 1073 | ret = sysfs_create_group(&dev->kobj, &ci_attr_group); |
| 1074 | if (ret) |
| 1075 | goto remove_debug; |
| 1076 | |
| 1077 | return 0; |
| 1078 | |
| 1079 | remove_debug: |
| 1080 | dbg_remove_files(ci); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1081 | stop: |
Jisheng Zhang | c4a0bbb | 2017-04-26 16:59:34 +0800 | [diff] [blame] | 1082 | if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) |
| 1083 | ci_hdrc_otg_destroy(ci); |
| 1084 | deinit_gadget: |
| 1085 | ci_hdrc_gadget_destroy(ci); |
| 1086 | deinit_host: |
| 1087 | ci_hdrc_host_destroy(ci); |
Peter Chen | c859aa65 | 2014-02-19 13:41:40 +0800 | [diff] [blame] | 1088 | deinit_phy: |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 1089 | ci_usb_phy_exit(ci); |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 1090 | ulpi_exit: |
| 1091 | ci_ulpi_exit(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1092 | |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 1096 | static int ci_hdrc_remove(struct platform_device *pdev) |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1097 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 1098 | struct ci_hdrc *ci = platform_get_drvdata(pdev); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1099 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1100 | if (ci->supports_runtime_pm) { |
| 1101 | pm_runtime_get_sync(&pdev->dev); |
| 1102 | pm_runtime_disable(&pdev->dev); |
| 1103 | pm_runtime_put_noidle(&pdev->dev); |
| 1104 | } |
| 1105 | |
Alexander Shishkin | adf0f73 | 2013-03-30 12:53:53 +0200 | [diff] [blame] | 1106 | dbg_remove_files(ci); |
Peter Chen | a932a80 | 2017-03-27 10:54:27 +0800 | [diff] [blame] | 1107 | sysfs_remove_group(&ci->dev->kobj, &ci_attr_group); |
Peter Chen | 3f124d2 | 2013-08-14 12:44:07 +0300 | [diff] [blame] | 1108 | ci_role_destroy(ci); |
Peter Chen | 864cf94 | 2013-09-24 12:47:55 +0800 | [diff] [blame] | 1109 | ci_hdrc_enter_lpm(ci, true); |
Antoine Tenart | 1e5e2d3 | 2014-10-30 18:41:19 +0100 | [diff] [blame] | 1110 | ci_usb_phy_exit(ci); |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 1111 | ci_ulpi_exit(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1116 | #ifdef CONFIG_PM |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1117 | /* Prepare wakeup by SRP before suspend */ |
| 1118 | static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci) |
| 1119 | { |
| 1120 | if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) && |
| 1121 | !hw_read_otgsc(ci, OTGSC_ID)) { |
| 1122 | hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP, |
| 1123 | PORTSC_PP); |
| 1124 | hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN, |
| 1125 | PORTSC_WKCN); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | /* Handle SRP when wakeup by data pulse */ |
| 1130 | static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci) |
| 1131 | { |
| 1132 | if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) && |
| 1133 | (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) { |
| 1134 | if (!hw_read_otgsc(ci, OTGSC_ID)) { |
| 1135 | ci->fsm.a_srp_det = 1; |
| 1136 | ci->fsm.a_bus_drop = 0; |
| 1137 | } else { |
| 1138 | ci->fsm.id = 1; |
| 1139 | } |
| 1140 | ci_otg_queue_work(ci); |
| 1141 | } |
| 1142 | } |
| 1143 | |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1144 | static void ci_controller_suspend(struct ci_hdrc *ci) |
| 1145 | { |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1146 | disable_irq(ci->irq); |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1147 | ci_hdrc_enter_lpm(ci, true); |
Fabio Estevam | 1fbf462 | 2015-09-08 22:18:14 -0300 | [diff] [blame] | 1148 | if (ci->platdata->phy_clkgate_delay_us) |
| 1149 | usleep_range(ci->platdata->phy_clkgate_delay_us, |
| 1150 | ci->platdata->phy_clkgate_delay_us + 50); |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1151 | usb_phy_set_suspend(ci->usb_phy, 1); |
| 1152 | ci->in_lpm = true; |
| 1153 | enable_irq(ci->irq); |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | static int ci_controller_resume(struct device *dev) |
| 1157 | { |
| 1158 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 1159 | int ret; |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1160 | |
| 1161 | dev_dbg(dev, "at %s\n", __func__); |
| 1162 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1163 | if (!ci->in_lpm) { |
| 1164 | WARN_ON(1); |
| 1165 | return 0; |
| 1166 | } |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1167 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1168 | ci_hdrc_enter_lpm(ci, false); |
Stephen Boyd | 7bb7e9b | 2016-12-28 14:56:55 -0800 | [diff] [blame] | 1169 | |
| 1170 | ret = ci_ulpi_resume(ci); |
| 1171 | if (ret) |
| 1172 | return ret; |
| 1173 | |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1174 | if (ci->usb_phy) { |
| 1175 | usb_phy_set_suspend(ci->usb_phy, 0); |
| 1176 | usb_phy_set_wakeup(ci->usb_phy, false); |
| 1177 | hw_wait_phy_stable(); |
| 1178 | } |
| 1179 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1180 | ci->in_lpm = false; |
| 1181 | if (ci->wakeup_int) { |
| 1182 | ci->wakeup_int = false; |
| 1183 | pm_runtime_mark_last_busy(ci->dev); |
| 1184 | pm_runtime_put_autosuspend(ci->dev); |
| 1185 | enable_irq(ci->irq); |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1186 | if (ci_otg_is_fsm_mode(ci)) |
| 1187 | ci_otg_fsm_wakeup_by_srp(ci); |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1188 | } |
| 1189 | |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1193 | #ifdef CONFIG_PM_SLEEP |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1194 | static int ci_suspend(struct device *dev) |
| 1195 | { |
| 1196 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 1197 | |
| 1198 | if (ci->wq) |
| 1199 | flush_workqueue(ci->wq); |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1200 | /* |
| 1201 | * Controller needs to be active during suspend, otherwise the core |
| 1202 | * may run resume when the parent is at suspend if other driver's |
| 1203 | * suspend fails, it occurs before parent's suspend has not started, |
| 1204 | * but the core suspend has finished. |
| 1205 | */ |
| 1206 | if (ci->in_lpm) |
| 1207 | pm_runtime_resume(dev); |
| 1208 | |
| 1209 | if (ci->in_lpm) { |
| 1210 | WARN_ON(1); |
| 1211 | return 0; |
| 1212 | } |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1213 | |
Peter Chen | f8efa76 | 2015-02-11 12:44:48 +0800 | [diff] [blame] | 1214 | if (device_may_wakeup(dev)) { |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1215 | if (ci_otg_is_fsm_mode(ci)) |
| 1216 | ci_otg_fsm_suspend_for_srp(ci); |
| 1217 | |
Peter Chen | f8efa76 | 2015-02-11 12:44:48 +0800 | [diff] [blame] | 1218 | usb_phy_set_wakeup(ci->usb_phy, true); |
| 1219 | enable_irq_wake(ci->irq); |
| 1220 | } |
| 1221 | |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1222 | ci_controller_suspend(ci); |
| 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | static int ci_resume(struct device *dev) |
| 1228 | { |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1229 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 1230 | int ret; |
| 1231 | |
Peter Chen | f8efa76 | 2015-02-11 12:44:48 +0800 | [diff] [blame] | 1232 | if (device_may_wakeup(dev)) |
| 1233 | disable_irq_wake(ci->irq); |
| 1234 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1235 | ret = ci_controller_resume(dev); |
| 1236 | if (ret) |
| 1237 | return ret; |
| 1238 | |
| 1239 | if (ci->supports_runtime_pm) { |
| 1240 | pm_runtime_disable(dev); |
| 1241 | pm_runtime_set_active(dev); |
| 1242 | pm_runtime_enable(dev); |
| 1243 | } |
| 1244 | |
| 1245 | return ret; |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1246 | } |
| 1247 | #endif /* CONFIG_PM_SLEEP */ |
| 1248 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1249 | static int ci_runtime_suspend(struct device *dev) |
| 1250 | { |
| 1251 | struct ci_hdrc *ci = dev_get_drvdata(dev); |
| 1252 | |
| 1253 | dev_dbg(dev, "at %s\n", __func__); |
| 1254 | |
| 1255 | if (ci->in_lpm) { |
| 1256 | WARN_ON(1); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
Li Jun | 961ea49 | 2015-02-11 12:45:03 +0800 | [diff] [blame] | 1260 | if (ci_otg_is_fsm_mode(ci)) |
| 1261 | ci_otg_fsm_suspend_for_srp(ci); |
| 1262 | |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1263 | usb_phy_set_wakeup(ci->usb_phy, true); |
| 1264 | ci_controller_suspend(ci); |
| 1265 | |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | static int ci_runtime_resume(struct device *dev) |
| 1270 | { |
| 1271 | return ci_controller_resume(dev); |
| 1272 | } |
| 1273 | |
| 1274 | #endif /* CONFIG_PM */ |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1275 | static const struct dev_pm_ops ci_pm_ops = { |
| 1276 | SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume) |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1277 | SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL) |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1278 | }; |
Peter Chen | 1f874ed | 2015-02-11 12:44:45 +0800 | [diff] [blame] | 1279 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1280 | static struct platform_driver ci_hdrc_driver = { |
| 1281 | .probe = ci_hdrc_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 1282 | .remove = ci_hdrc_remove, |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1283 | .driver = { |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1284 | .name = "ci_hdrc", |
Peter Chen | 8076932 | 2014-11-26 13:44:29 +0800 | [diff] [blame] | 1285 | .pm = &ci_pm_ops, |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1286 | }, |
| 1287 | }; |
| 1288 | |
Peter Chen | 2f01a33 | 2015-07-21 09:51:29 +0800 | [diff] [blame] | 1289 | static int __init ci_hdrc_platform_register(void) |
| 1290 | { |
| 1291 | ci_hdrc_host_driver_init(); |
| 1292 | return platform_driver_register(&ci_hdrc_driver); |
| 1293 | } |
| 1294 | module_init(ci_hdrc_platform_register); |
| 1295 | |
| 1296 | static void __exit ci_hdrc_platform_unregister(void) |
| 1297 | { |
| 1298 | platform_driver_unregister(&ci_hdrc_driver); |
| 1299 | } |
| 1300 | module_exit(ci_hdrc_platform_unregister); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1301 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1302 | MODULE_ALIAS("platform:ci_hdrc"); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1303 | MODULE_LICENSE("GPL v2"); |
| 1304 | MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>"); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1305 | MODULE_DESCRIPTION("ChipIdea HDRC Driver"); |