blob: f7ff9e8f4ddd7986eaafc461030b9b3c7392422e [file] [log] [blame]
Mayank Rana3cb43a32016-08-02 12:10:20 -07001/*
Mayank Ranadadae592017-07-21 09:03:32 -07002 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Mayank Rana3cb43a32016-08-02 12:10:20 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/err.h>
17#include <linux/slab.h>
18#include <linux/clk.h>
Mayank Rana3cb43a32016-08-02 12:10:20 -070019#include <linux/delay.h>
20#include <linux/io.h>
21#include <linux/of.h>
22#include <linux/platform_device.h>
23#include <linux/power_supply.h>
24#include <linux/regulator/consumer.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27#include <linux/usb/phy.h>
Amit Nischal4d278212016-06-06 17:54:34 +053028#include <linux/reset.h>
Mayank Rana3cb43a32016-08-02 12:10:20 -070029
30#define QUSB2PHY_PLL_STATUS 0x38
31#define QUSB2PHY_PLL_LOCK BIT(5)
32
33#define QUSB2PHY_PORT_QC1 0x70
34#define VDM_SRC_EN BIT(4)
35#define VDP_SRC_EN BIT(2)
36
37#define QUSB2PHY_PORT_QC2 0x74
38#define RDM_UP_EN BIT(1)
39#define RDP_UP_EN BIT(3)
40#define RPUM_LOW_EN BIT(4)
41#define RPUP_LOW_EN BIT(5)
42
43#define QUSB2PHY_PORT_POWERDOWN 0xB4
44#define CLAMP_N_EN BIT(5)
45#define FREEZIO_N BIT(1)
46#define POWER_DOWN BIT(0)
47
48#define QUSB2PHY_PWR_CTRL1 0x210
49#define PWR_CTRL1_CLAMP_N_EN BIT(1)
50#define PWR_CTRL1_POWR_DOWN BIT(0)
51
52#define QUSB2PHY_PLL_COMMON_STATUS_ONE 0x1A0
53#define CORE_READY_STATUS BIT(0)
54
55#define QUSB2PHY_PORT_UTMI_CTRL1 0xC0
56#define TERM_SELECT BIT(4)
57#define XCVR_SELECT_FS BIT(2)
58#define OP_MODE_NON_DRIVE BIT(0)
59
60#define QUSB2PHY_PORT_UTMI_CTRL2 0xC4
61#define UTMI_ULPI_SEL BIT(7)
62#define UTMI_TEST_MUX_SEL BIT(6)
63
64#define QUSB2PHY_PLL_TEST 0x04
65#define CLK_REF_SEL BIT(7)
66
67#define QUSB2PHY_PORT_TUNE1 0x80
68#define QUSB2PHY_PORT_TUNE2 0x84
69#define QUSB2PHY_PORT_TUNE3 0x88
70#define QUSB2PHY_PORT_TUNE4 0x8C
71
72/* In case Efuse register shows zero, use this value */
73#define TUNE2_DEFAULT_HIGH_NIBBLE 0xB
74#define TUNE2_DEFAULT_LOW_NIBBLE 0x3
75
76/* Get TUNE2's high nibble value read from efuse */
77#define TUNE2_HIGH_NIBBLE_VAL(val, pos, mask) ((val >> pos) & mask)
78
79#define QUSB2PHY_PORT_INTR_CTRL 0xBC
80#define CHG_DET_INTR_EN BIT(4)
81#define DMSE_INTR_HIGH_SEL BIT(3)
82#define DMSE_INTR_EN BIT(2)
83#define DPSE_INTR_HIGH_SEL BIT(1)
84#define DPSE_INTR_EN BIT(0)
85
86#define QUSB2PHY_PORT_UTMI_STATUS 0xF4
87#define LINESTATE_DP BIT(0)
88#define LINESTATE_DM BIT(1)
89
90
91#define QUSB2PHY_1P8_VOL_MIN 1800000 /* uV */
92#define QUSB2PHY_1P8_VOL_MAX 1800000 /* uV */
93#define QUSB2PHY_1P8_HPM_LOAD 30000 /* uA */
94
95#define QUSB2PHY_3P3_VOL_MIN 3075000 /* uV */
96#define QUSB2PHY_3P3_VOL_MAX 3200000 /* uV */
97#define QUSB2PHY_3P3_HPM_LOAD 30000 /* uA */
98
99#define QUSB2PHY_REFCLK_ENABLE BIT(0)
100
101unsigned int tune2;
102module_param(tune2, uint, S_IRUGO | S_IWUSR);
103MODULE_PARM_DESC(tune2, "QUSB PHY TUNE2");
104
105struct qusb_phy {
106 struct usb_phy phy;
107 void __iomem *base;
108 void __iomem *tune2_efuse_reg;
109 void __iomem *ref_clk_base;
110
111 struct clk *ref_clk_src;
112 struct clk *ref_clk;
113 struct clk *cfg_ahb_clk;
Amit Nischal4d278212016-06-06 17:54:34 +0530114 struct reset_control *phy_reset;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700115
116 struct regulator *vdd;
117 struct regulator *vdda33;
118 struct regulator *vdda18;
119 int vdd_levels[3]; /* none, low, high */
120 int init_seq_len;
121 int *qusb_phy_init_seq;
122 u32 major_rev;
123
124 u32 tune2_val;
125 int tune2_efuse_bit_pos;
126 int tune2_efuse_num_of_bits;
127
128 bool power_enabled;
129 bool clocks_enabled;
130 bool cable_connected;
131 bool suspended;
132 bool ulpi_mode;
Mayank Ranadadae592017-07-21 09:03:32 -0700133 bool dpdm_enable;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700134 bool is_se_clk;
135
136 struct regulator_desc dpdm_rdesc;
137 struct regulator_dev *dpdm_rdev;
138
139 /* emulation targets specific */
140 void __iomem *emu_phy_base;
141 bool emulation;
142 int *emu_init_seq;
143 int emu_init_seq_len;
144 int *phy_pll_reset_seq;
145 int phy_pll_reset_seq_len;
146 int *emu_dcm_reset_seq;
147 int emu_dcm_reset_seq_len;
148};
149
150static void qusb_phy_enable_clocks(struct qusb_phy *qphy, bool on)
151{
152 dev_dbg(qphy->phy.dev, "%s(): clocks_enabled:%d on:%d\n",
153 __func__, qphy->clocks_enabled, on);
154
155 if (!qphy->clocks_enabled && on) {
156 clk_prepare_enable(qphy->ref_clk_src);
157 clk_prepare_enable(qphy->ref_clk);
158 clk_prepare_enable(qphy->cfg_ahb_clk);
159 qphy->clocks_enabled = true;
160 }
161
162 if (qphy->clocks_enabled && !on) {
163 clk_disable_unprepare(qphy->ref_clk);
164 clk_disable_unprepare(qphy->ref_clk_src);
165 clk_disable_unprepare(qphy->cfg_ahb_clk);
166 qphy->clocks_enabled = false;
167 }
168
169 dev_dbg(qphy->phy.dev, "%s(): clocks_enabled:%d\n", __func__,
170 qphy->clocks_enabled);
171}
172
173static int qusb_phy_config_vdd(struct qusb_phy *qphy, int high)
174{
175 int min, ret;
176
177 min = high ? 1 : 0; /* low or none? */
178 ret = regulator_set_voltage(qphy->vdd, qphy->vdd_levels[min],
179 qphy->vdd_levels[2]);
180 if (ret) {
181 dev_err(qphy->phy.dev, "unable to set voltage for qusb vdd\n");
182 return ret;
183 }
184
185 dev_dbg(qphy->phy.dev, "min_vol:%d max_vol:%d\n",
186 qphy->vdd_levels[min], qphy->vdd_levels[2]);
187 return ret;
188}
189
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530190static int qusb_phy_enable_power(struct qusb_phy *qphy, bool on)
Mayank Rana3cb43a32016-08-02 12:10:20 -0700191{
192 int ret = 0;
193
194 dev_dbg(qphy->phy.dev, "%s turn %s regulators. power_enabled:%d\n",
195 __func__, on ? "on" : "off", qphy->power_enabled);
196
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530197 if (qphy->power_enabled == on) {
Mayank Rana3cb43a32016-08-02 12:10:20 -0700198 dev_dbg(qphy->phy.dev, "PHYs' regulators are already ON.\n");
199 return 0;
200 }
201
202 if (!on)
203 goto disable_vdda33;
204
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530205 ret = qusb_phy_config_vdd(qphy, true);
206 if (ret) {
207 dev_err(qphy->phy.dev, "Unable to config VDD:%d\n",
208 ret);
209 goto err_vdd;
210 }
Mayank Rana3cb43a32016-08-02 12:10:20 -0700211
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530212 ret = regulator_enable(qphy->vdd);
213 if (ret) {
214 dev_err(qphy->phy.dev, "Unable to enable VDD\n");
215 goto unconfig_vdd;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700216 }
217
218 ret = regulator_set_load(qphy->vdda18, QUSB2PHY_1P8_HPM_LOAD);
219 if (ret < 0) {
220 dev_err(qphy->phy.dev, "Unable to set HPM of vdda18:%d\n", ret);
221 goto disable_vdd;
222 }
223
224 ret = regulator_set_voltage(qphy->vdda18, QUSB2PHY_1P8_VOL_MIN,
225 QUSB2PHY_1P8_VOL_MAX);
226 if (ret) {
227 dev_err(qphy->phy.dev,
228 "Unable to set voltage for vdda18:%d\n", ret);
229 goto put_vdda18_lpm;
230 }
231
232 ret = regulator_enable(qphy->vdda18);
233 if (ret) {
234 dev_err(qphy->phy.dev, "Unable to enable vdda18:%d\n", ret);
235 goto unset_vdda18;
236 }
237
238 ret = regulator_set_load(qphy->vdda33, QUSB2PHY_3P3_HPM_LOAD);
239 if (ret < 0) {
240 dev_err(qphy->phy.dev, "Unable to set HPM of vdda33:%d\n", ret);
241 goto disable_vdda18;
242 }
243
244 ret = regulator_set_voltage(qphy->vdda33, QUSB2PHY_3P3_VOL_MIN,
245 QUSB2PHY_3P3_VOL_MAX);
246 if (ret) {
247 dev_err(qphy->phy.dev,
248 "Unable to set voltage for vdda33:%d\n", ret);
249 goto put_vdda33_lpm;
250 }
251
252 ret = regulator_enable(qphy->vdda33);
253 if (ret) {
254 dev_err(qphy->phy.dev, "Unable to enable vdda33:%d\n", ret);
255 goto unset_vdd33;
256 }
257
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530258 qphy->power_enabled = true;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700259
260 pr_debug("%s(): QUSB PHY's regulators are turned ON.\n", __func__);
261 return ret;
262
263disable_vdda33:
264 ret = regulator_disable(qphy->vdda33);
265 if (ret)
266 dev_err(qphy->phy.dev, "Unable to disable vdda33:%d\n", ret);
267
268unset_vdd33:
269 ret = regulator_set_voltage(qphy->vdda33, 0, QUSB2PHY_3P3_VOL_MAX);
270 if (ret)
271 dev_err(qphy->phy.dev,
272 "Unable to set (0) voltage for vdda33:%d\n", ret);
273
274put_vdda33_lpm:
275 ret = regulator_set_load(qphy->vdda33, 0);
276 if (ret < 0)
277 dev_err(qphy->phy.dev, "Unable to set (0) HPM of vdda33\n");
278
279disable_vdda18:
280 ret = regulator_disable(qphy->vdda18);
281 if (ret)
282 dev_err(qphy->phy.dev, "Unable to disable vdda18:%d\n", ret);
283
284unset_vdda18:
285 ret = regulator_set_voltage(qphy->vdda18, 0, QUSB2PHY_1P8_VOL_MAX);
286 if (ret)
287 dev_err(qphy->phy.dev,
288 "Unable to set (0) voltage for vdda18:%d\n", ret);
289
290put_vdda18_lpm:
291 ret = regulator_set_load(qphy->vdda18, 0);
292 if (ret < 0)
293 dev_err(qphy->phy.dev, "Unable to set LPM of vdda18\n");
294
295disable_vdd:
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530296 ret = regulator_disable(qphy->vdd);
297 if (ret)
298 dev_err(qphy->phy.dev, "Unable to disable vdd:%d\n",
Mayank Rana3cb43a32016-08-02 12:10:20 -0700299 ret);
300
301unconfig_vdd:
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530302 ret = qusb_phy_config_vdd(qphy, false);
303 if (ret)
304 dev_err(qphy->phy.dev, "Unable unconfig VDD:%d\n",
Mayank Rana3cb43a32016-08-02 12:10:20 -0700305 ret);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700306err_vdd:
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530307 qphy->power_enabled = false;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700308 dev_dbg(qphy->phy.dev, "QUSB PHY's regulators are turned OFF.\n");
309 return ret;
310}
311
Mayank Rana3cb43a32016-08-02 12:10:20 -0700312static void qusb_phy_get_tune2_param(struct qusb_phy *qphy)
313{
314 u8 num_of_bits;
315 u32 bit_mask = 1;
316
317 pr_debug("%s(): num_of_bits:%d bit_pos:%d\n", __func__,
318 qphy->tune2_efuse_num_of_bits,
319 qphy->tune2_efuse_bit_pos);
320
321 /* get bit mask based on number of bits to use with efuse reg */
322 if (qphy->tune2_efuse_num_of_bits) {
323 num_of_bits = qphy->tune2_efuse_num_of_bits;
324 bit_mask = (bit_mask << num_of_bits) - 1;
325 }
326
327 /*
328 * Read EFUSE register having TUNE2 parameter's high nibble.
329 * If efuse register shows value as 0x0, then use default value
330 * as 0xB as high nibble. Otherwise use efuse register based
331 * value for this purpose.
332 */
333 qphy->tune2_val = readl_relaxed(qphy->tune2_efuse_reg);
334 pr_debug("%s(): bit_mask:%d efuse based tune2 value:%d\n",
335 __func__, bit_mask, qphy->tune2_val);
336
337 qphy->tune2_val = TUNE2_HIGH_NIBBLE_VAL(qphy->tune2_val,
338 qphy->tune2_efuse_bit_pos, bit_mask);
339
340 if (!qphy->tune2_val)
341 qphy->tune2_val = TUNE2_DEFAULT_HIGH_NIBBLE;
342
343 /* Get TUNE2 byte value using high and low nibble value */
344 qphy->tune2_val = ((qphy->tune2_val << 0x4) |
345 TUNE2_DEFAULT_LOW_NIBBLE);
346}
347
348static void qusb_phy_write_seq(void __iomem *base, u32 *seq, int cnt,
349 unsigned long delay)
350{
351 int i;
352
353 pr_debug("Seq count:%d\n", cnt);
354 for (i = 0; i < cnt; i = i+2) {
355 pr_debug("write 0x%02x to 0x%02x\n", seq[i], seq[i+1]);
356 writel_relaxed(seq[i], base + seq[i+1]);
357 if (delay)
358 usleep_range(delay, (delay + 2000));
359 }
360}
361
362static int qusb_phy_init(struct usb_phy *phy)
363{
364 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
365 int ret, reset_val = 0;
366 u8 reg;
367 bool pll_lock_fail = false;
368
369 dev_dbg(phy->dev, "%s\n", __func__);
370
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530371 ret = qusb_phy_enable_power(qphy, true);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700372 if (ret)
373 return ret;
374
375 qusb_phy_enable_clocks(qphy, true);
376
377 /*
378 * ref clock is enabled by default after power on reset. Linux clock
379 * driver will disable this clock as part of late init if peripheral
380 * driver(s) does not explicitly votes for it. Linux clock driver also
381 * does not disable the clock until late init even if peripheral
382 * driver explicitly requests it and cannot defer the probe until late
383 * init. Hence, Explicitly disable the clock using register write to
384 * allow QUSB PHY PLL to lock properly.
385 */
386 if (qphy->ref_clk_base) {
387 writel_relaxed((readl_relaxed(qphy->ref_clk_base) &
388 ~QUSB2PHY_REFCLK_ENABLE),
389 qphy->ref_clk_base);
390 /* Make sure that above write complete to get ref clk OFF */
391 wmb();
392 }
393
394 /* Perform phy reset */
Amit Nischal4d278212016-06-06 17:54:34 +0530395 ret = reset_control_assert(qphy->phy_reset);
396 if (ret)
397 dev_err(phy->dev, "%s: phy_reset assert failed\n", __func__);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700398 usleep_range(100, 150);
Amit Nischal4d278212016-06-06 17:54:34 +0530399 ret = reset_control_deassert(qphy->phy_reset);
400 if (ret)
401 dev_err(phy->dev, "%s: phy_reset deassert failed\n", __func__);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700402
403 if (qphy->emulation) {
404 if (qphy->emu_init_seq)
405 qusb_phy_write_seq(qphy->emu_phy_base,
406 qphy->emu_init_seq, qphy->emu_init_seq_len, 0);
407
408 if (qphy->qusb_phy_init_seq)
409 qusb_phy_write_seq(qphy->base, qphy->qusb_phy_init_seq,
410 qphy->init_seq_len, 0);
411
412 /* Wait for 5ms as per QUSB2 RUMI sequence */
413 usleep_range(5000, 7000);
414
415 if (qphy->phy_pll_reset_seq)
416 qusb_phy_write_seq(qphy->base, qphy->phy_pll_reset_seq,
417 qphy->phy_pll_reset_seq_len, 10000);
418
419 if (qphy->emu_dcm_reset_seq)
420 qusb_phy_write_seq(qphy->emu_phy_base,
421 qphy->emu_dcm_reset_seq,
422 qphy->emu_dcm_reset_seq_len, 10000);
423
424 return 0;
425 }
426
427 /* Disable the PHY */
428 if (qphy->major_rev < 2)
429 writel_relaxed(CLAMP_N_EN | FREEZIO_N | POWER_DOWN,
430 qphy->base + QUSB2PHY_PORT_POWERDOWN);
431 else
432 writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) |
433 PWR_CTRL1_POWR_DOWN,
434 qphy->base + QUSB2PHY_PWR_CTRL1);
435
436 /* configure for ULPI mode if requested */
437 if (qphy->ulpi_mode)
438 writel_relaxed(0x0, qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
439
440 /* save reset value to override based on clk scheme */
441 if (qphy->ref_clk_base)
442 reset_val = readl_relaxed(qphy->base + QUSB2PHY_PLL_TEST);
443
444 if (qphy->qusb_phy_init_seq)
445 qusb_phy_write_seq(qphy->base, qphy->qusb_phy_init_seq,
446 qphy->init_seq_len, 0);
447
448 /*
449 * Check for EFUSE value only if tune2_efuse_reg is available
450 * and try to read EFUSE value only once i.e. not every USB
451 * cable connect case.
452 */
453 if (qphy->tune2_efuse_reg) {
454 if (!qphy->tune2_val)
455 qusb_phy_get_tune2_param(qphy);
456
457 pr_debug("%s(): Programming TUNE2 parameter as:%x\n", __func__,
458 qphy->tune2_val);
459 writel_relaxed(qphy->tune2_val,
460 qphy->base + QUSB2PHY_PORT_TUNE2);
461 }
462
463 /* If tune2 modparam set, override tune2 value */
464 if (tune2) {
465 pr_debug("%s(): (modparam) TUNE2 val:0x%02x\n",
466 __func__, tune2);
467 writel_relaxed(tune2,
468 qphy->base + QUSB2PHY_PORT_TUNE2);
469 }
470
471 /* ensure above writes are completed before re-enabling PHY */
472 wmb();
473
474 /* Enable the PHY */
475 if (qphy->major_rev < 2)
476 writel_relaxed(CLAMP_N_EN | FREEZIO_N,
477 qphy->base + QUSB2PHY_PORT_POWERDOWN);
478 else
479 writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) &
480 ~PWR_CTRL1_POWR_DOWN,
481 qphy->base + QUSB2PHY_PWR_CTRL1);
482
483 /* Ensure above write is completed before turning ON ref clk */
484 wmb();
485
486 /* Require to get phy pll lock successfully */
487 usleep_range(150, 160);
488
489 /* Turn on phy ref_clk if DIFF_CLK else select SE_CLK */
490 if (qphy->ref_clk_base) {
491 if (!qphy->is_se_clk) {
492 reset_val &= ~CLK_REF_SEL;
493 writel_relaxed((readl_relaxed(qphy->ref_clk_base) |
494 QUSB2PHY_REFCLK_ENABLE),
495 qphy->ref_clk_base);
496 } else {
497 reset_val |= CLK_REF_SEL;
498 writel_relaxed(reset_val,
499 qphy->base + QUSB2PHY_PLL_TEST);
500 }
501
502 /* Make sure above write is completed to get PLL source clock */
503 wmb();
504
505 /* Required to get PHY PLL lock successfully */
506 usleep_range(100, 110);
507 }
508
509 if (qphy->major_rev < 2) {
510 reg = readb_relaxed(qphy->base + QUSB2PHY_PLL_STATUS);
511 dev_dbg(phy->dev, "QUSB2PHY_PLL_STATUS:%x\n", reg);
512 if (!(reg & QUSB2PHY_PLL_LOCK))
513 pll_lock_fail = true;
514 } else {
515 reg = readb_relaxed(qphy->base +
516 QUSB2PHY_PLL_COMMON_STATUS_ONE);
517 dev_dbg(phy->dev, "QUSB2PHY_PLL_COMMON_STATUS_ONE:%x\n", reg);
518 if (!(reg & CORE_READY_STATUS))
519 pll_lock_fail = true;
520 }
521
522 if (pll_lock_fail) {
523 dev_err(phy->dev, "QUSB PHY PLL LOCK fails:%x\n", reg);
524 WARN_ON(1);
525 }
526
527 return 0;
528}
529
530static void qusb_phy_shutdown(struct usb_phy *phy)
531{
532 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
533
534 dev_dbg(phy->dev, "%s\n", __func__);
535
536 qusb_phy_enable_clocks(qphy, true);
537
538 /* Disable the PHY */
539 if (qphy->major_rev < 2)
540 writel_relaxed(CLAMP_N_EN | FREEZIO_N | POWER_DOWN,
541 qphy->base + QUSB2PHY_PORT_POWERDOWN);
542 else
543 writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) |
544 PWR_CTRL1_POWR_DOWN,
545 qphy->base + QUSB2PHY_PWR_CTRL1);
546
547 /* Make sure above write complete before turning off clocks */
548 wmb();
549
550 qusb_phy_enable_clocks(qphy, false);
551}
552/**
553 * Performs QUSB2 PHY suspend/resume functionality.
554 *
555 * @uphy - usb phy pointer.
556 * @suspend - to enable suspend or not. 1 - suspend, 0 - resume
557 *
558 */
559static int qusb_phy_set_suspend(struct usb_phy *phy, int suspend)
560{
561 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
562 u32 linestate = 0, intr_mask = 0;
563
564 if (qphy->suspended && suspend) {
565 dev_dbg(phy->dev, "%s: USB PHY is already suspended\n",
566 __func__);
567 return 0;
568 }
569
570 if (suspend) {
571 /* Bus suspend case */
572 if (qphy->cable_connected ||
573 (qphy->phy.flags & PHY_HOST_MODE)) {
574 /* Clear all interrupts */
575 writel_relaxed(0x00,
576 qphy->base + QUSB2PHY_PORT_INTR_CTRL);
577
578 linestate = readl_relaxed(qphy->base +
579 QUSB2PHY_PORT_UTMI_STATUS);
580
581 /*
582 * D+/D- interrupts are level-triggered, but we are
583 * only interested if the line state changes, so enable
584 * the high/low trigger based on current state. In
585 * other words, enable the triggers _opposite_ of what
586 * the current D+/D- levels are.
587 * e.g. if currently D+ high, D- low (HS 'J'/Suspend),
588 * configure the mask to trigger on D+ low OR D- high
589 */
590 intr_mask = DPSE_INTR_EN | DMSE_INTR_EN;
591 if (!(linestate & LINESTATE_DP)) /* D+ low */
592 intr_mask |= DPSE_INTR_HIGH_SEL;
593 if (!(linestate & LINESTATE_DM)) /* D- low */
594 intr_mask |= DMSE_INTR_HIGH_SEL;
595
596 writel_relaxed(intr_mask,
597 qphy->base + QUSB2PHY_PORT_INTR_CTRL);
598
599 qusb_phy_enable_clocks(qphy, false);
600 } else { /* Disconnect case */
601 /* Disable all interrupts */
602 writel_relaxed(0x00,
603 qphy->base + QUSB2PHY_PORT_INTR_CTRL);
604 /*
605 * Phy in non-driving mode leaves Dp and Dm lines in
606 * high-Z state. Controller power collapse is not
607 * switching phy to non-driving mode causing charger
608 * detection failure. Bring phy to non-driving mode by
609 * overriding controller output via UTMI interface.
610 */
611 writel_relaxed(TERM_SELECT | XCVR_SELECT_FS |
612 OP_MODE_NON_DRIVE,
613 qphy->base + QUSB2PHY_PORT_UTMI_CTRL1);
614 writel_relaxed(UTMI_ULPI_SEL | UTMI_TEST_MUX_SEL,
615 qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
616
617
618 qusb_phy_enable_clocks(qphy, false);
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530619 qusb_phy_enable_power(qphy, false);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700620 }
621 qphy->suspended = true;
622 } else {
623 /* Bus suspend case */
624 if (qphy->cable_connected ||
625 (qphy->phy.flags & PHY_HOST_MODE)) {
626 qusb_phy_enable_clocks(qphy, true);
627 /* Clear all interrupts on resume */
628 writel_relaxed(0x00,
629 qphy->base + QUSB2PHY_PORT_INTR_CTRL);
630 } else {
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530631 qusb_phy_enable_power(qphy, true);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700632 qusb_phy_enable_clocks(qphy, true);
633 }
634 qphy->suspended = false;
635 }
636
637 return 0;
638}
639
640static int qusb_phy_notify_connect(struct usb_phy *phy,
641 enum usb_device_speed speed)
642{
643 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
644
645 qphy->cable_connected = true;
646
647 dev_dbg(phy->dev, "QUSB PHY: connect notification cable_connected=%d\n",
648 qphy->cable_connected);
649 return 0;
650}
651
652static int qusb_phy_notify_disconnect(struct usb_phy *phy,
653 enum usb_device_speed speed)
654{
655 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
656
657 qphy->cable_connected = false;
658
659 dev_dbg(phy->dev, "QUSB PHY: connect notification cable_connected=%d\n",
660 qphy->cable_connected);
661 return 0;
662}
663
664static int qusb_phy_dpdm_regulator_enable(struct regulator_dev *rdev)
665{
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700666 int ret = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700667 struct qusb_phy *qphy = rdev_get_drvdata(rdev);
668
Mayank Ranadadae592017-07-21 09:03:32 -0700669 dev_dbg(qphy->phy.dev, "%s dpdm_enable:%d\n",
670 __func__, qphy->dpdm_enable);
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700671
Mayank Ranadadae592017-07-21 09:03:32 -0700672 if (!qphy->dpdm_enable) {
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530673 ret = qusb_phy_enable_power(qphy, true);
Mayank Ranadadae592017-07-21 09:03:32 -0700674 if (ret < 0) {
675 dev_dbg(qphy->phy.dev,
676 "dpdm regulator enable failed:%d\n", ret);
677 return ret;
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700678 }
Mayank Ranadadae592017-07-21 09:03:32 -0700679 qphy->dpdm_enable = true;
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700680 }
681
682 return ret;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700683}
684
685static int qusb_phy_dpdm_regulator_disable(struct regulator_dev *rdev)
686{
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700687 int ret = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700688 struct qusb_phy *qphy = rdev_get_drvdata(rdev);
689
Mayank Ranadadae592017-07-21 09:03:32 -0700690 dev_dbg(qphy->phy.dev, "%s dpdm_enable:%d\n",
691 __func__, qphy->dpdm_enable);
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700692
Mayank Ranadadae592017-07-21 09:03:32 -0700693 if (qphy->dpdm_enable) {
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530694 if (!qphy->cable_connected) {
695 dev_dbg(qphy->phy.dev, "turn off for HVDCP case\n");
696 ret = qusb_phy_enable_power(qphy, false);
697 if (ret < 0) {
698 dev_dbg(qphy->phy.dev,
699 "dpdm regulator disable failed:%d\n",
700 ret);
701 return ret;
702 }
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700703 }
Mayank Ranadadae592017-07-21 09:03:32 -0700704 qphy->dpdm_enable = false;
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700705 }
706
707 return ret;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700708}
709
710static int qusb_phy_dpdm_regulator_is_enabled(struct regulator_dev *rdev)
711{
712 struct qusb_phy *qphy = rdev_get_drvdata(rdev);
713
Mayank Ranadadae592017-07-21 09:03:32 -0700714 dev_dbg(qphy->phy.dev, "%s qphy->dpdm_enable = %d\n", __func__,
715 qphy->dpdm_enable);
716 return qphy->dpdm_enable;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700717}
718
719static struct regulator_ops qusb_phy_dpdm_regulator_ops = {
720 .enable = qusb_phy_dpdm_regulator_enable,
721 .disable = qusb_phy_dpdm_regulator_disable,
722 .is_enabled = qusb_phy_dpdm_regulator_is_enabled,
723};
724
725static int qusb_phy_regulator_init(struct qusb_phy *qphy)
726{
727 struct device *dev = qphy->phy.dev;
728 struct regulator_config cfg = {};
729 struct regulator_init_data *init_data;
730
731 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
732 if (!init_data)
733 return -ENOMEM;
734
735 init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_STATUS;
736 qphy->dpdm_rdesc.owner = THIS_MODULE;
737 qphy->dpdm_rdesc.type = REGULATOR_VOLTAGE;
738 qphy->dpdm_rdesc.ops = &qusb_phy_dpdm_regulator_ops;
739 qphy->dpdm_rdesc.name = kbasename(dev->of_node->full_name);
740
741 cfg.dev = dev;
742 cfg.init_data = init_data;
743 cfg.driver_data = qphy;
744 cfg.of_node = dev->of_node;
745
746 qphy->dpdm_rdev = devm_regulator_register(dev, &qphy->dpdm_rdesc, &cfg);
747 if (IS_ERR(qphy->dpdm_rdev))
748 return PTR_ERR(qphy->dpdm_rdev);
749
750 return 0;
751}
752
753static int qusb_phy_probe(struct platform_device *pdev)
754{
755 struct qusb_phy *qphy;
756 struct device *dev = &pdev->dev;
757 struct resource *res;
758 int ret = 0, size = 0;
759 const char *phy_type;
760 bool hold_phy_reset;
761
762 qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
763 if (!qphy)
764 return -ENOMEM;
765
766 qphy->phy.dev = dev;
767 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
768 "qusb_phy_base");
769 qphy->base = devm_ioremap_resource(dev, res);
770 if (IS_ERR(qphy->base))
771 return PTR_ERR(qphy->base);
772
773 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
774 "emu_phy_base");
775 if (res) {
776 qphy->emu_phy_base = devm_ioremap_resource(dev, res);
777 if (IS_ERR(qphy->emu_phy_base)) {
778 dev_dbg(dev, "couldn't ioremap emu_phy_base\n");
779 qphy->emu_phy_base = NULL;
780 }
781 }
782
783 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
784 "tune2_efuse_addr");
785 if (res) {
786 qphy->tune2_efuse_reg = devm_ioremap_nocache(dev, res->start,
787 resource_size(res));
788 if (!IS_ERR_OR_NULL(qphy->tune2_efuse_reg)) {
789 ret = of_property_read_u32(dev->of_node,
790 "qcom,tune2-efuse-bit-pos",
791 &qphy->tune2_efuse_bit_pos);
792 if (!ret) {
793 ret = of_property_read_u32(dev->of_node,
794 "qcom,tune2-efuse-num-bits",
795 &qphy->tune2_efuse_num_of_bits);
796 }
797
798 if (ret) {
799 dev_err(dev, "DT Value for tune2 efuse is invalid.\n");
800 return -EINVAL;
801 }
802 }
803 }
804
805 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
806 "ref_clk_addr");
807 if (res) {
808 qphy->ref_clk_base = devm_ioremap_nocache(dev,
809 res->start, resource_size(res));
810 if (IS_ERR(qphy->ref_clk_base)) {
811 dev_dbg(dev, "ref_clk_address is not available.\n");
812 return PTR_ERR(qphy->ref_clk_base);
813 }
814
815 ret = of_property_read_string(dev->of_node,
816 "qcom,phy-clk-scheme", &phy_type);
817 if (ret) {
818 dev_err(dev, "error need qsub_phy_clk_scheme.\n");
819 return ret;
820 }
821
822 if (!strcasecmp(phy_type, "cml")) {
823 qphy->is_se_clk = false;
824 } else if (!strcasecmp(phy_type, "cmos")) {
825 qphy->is_se_clk = true;
826 } else {
827 dev_err(dev, "erro invalid qusb_phy_clk_scheme\n");
828 return -EINVAL;
829 }
830 }
831
832 qphy->ref_clk_src = devm_clk_get(dev, "ref_clk_src");
833 if (IS_ERR(qphy->ref_clk_src))
834 dev_dbg(dev, "clk get failed for ref_clk_src\n");
835
836 qphy->ref_clk = devm_clk_get(dev, "ref_clk");
837 if (IS_ERR(qphy->ref_clk))
838 dev_dbg(dev, "clk get failed for ref_clk\n");
839 else
840 clk_set_rate(qphy->ref_clk, 19200000);
841
842 qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb_clk");
843 if (IS_ERR(qphy->cfg_ahb_clk))
844 return PTR_ERR(qphy->cfg_ahb_clk);
845
Amit Nischal4d278212016-06-06 17:54:34 +0530846 qphy->phy_reset = devm_reset_control_get(dev, "phy_reset");
Mayank Rana3cb43a32016-08-02 12:10:20 -0700847 if (IS_ERR(qphy->phy_reset))
848 return PTR_ERR(qphy->phy_reset);
849
850 qphy->emulation = of_property_read_bool(dev->of_node,
851 "qcom,emulation");
852
853 of_get_property(dev->of_node, "qcom,emu-init-seq", &size);
854 if (size) {
855 qphy->emu_init_seq = devm_kzalloc(dev,
856 size, GFP_KERNEL);
857 if (qphy->emu_init_seq) {
858 qphy->emu_init_seq_len =
859 (size / sizeof(*qphy->emu_init_seq));
860 if (qphy->emu_init_seq_len % 2) {
861 dev_err(dev, "invalid emu_init_seq_len\n");
862 return -EINVAL;
863 }
864
865 of_property_read_u32_array(dev->of_node,
Mayank Rana147f41d2016-08-12 10:07:45 -0700866 "qcom,emu-init-seq",
Mayank Rana3cb43a32016-08-02 12:10:20 -0700867 qphy->emu_init_seq,
868 qphy->emu_init_seq_len);
869 } else {
870 dev_dbg(dev, "error allocating memory for emu_init_seq\n");
871 }
872 }
873
Mayank Rana147f41d2016-08-12 10:07:45 -0700874 size = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700875 of_get_property(dev->of_node, "qcom,phy-pll-reset-seq", &size);
876 if (size) {
877 qphy->phy_pll_reset_seq = devm_kzalloc(dev,
878 size, GFP_KERNEL);
879 if (qphy->phy_pll_reset_seq) {
880 qphy->phy_pll_reset_seq_len =
881 (size / sizeof(*qphy->phy_pll_reset_seq));
882 if (qphy->phy_pll_reset_seq_len % 2) {
883 dev_err(dev, "invalid phy_pll_reset_seq_len\n");
884 return -EINVAL;
885 }
886
887 of_property_read_u32_array(dev->of_node,
888 "qcom,phy-pll-reset-seq",
889 qphy->phy_pll_reset_seq,
890 qphy->phy_pll_reset_seq_len);
891 } else {
892 dev_dbg(dev, "error allocating memory for phy_pll_reset_seq\n");
893 }
894 }
895
Mayank Rana147f41d2016-08-12 10:07:45 -0700896 size = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700897 of_get_property(dev->of_node, "qcom,emu-dcm-reset-seq", &size);
898 if (size) {
899 qphy->emu_dcm_reset_seq = devm_kzalloc(dev,
900 size, GFP_KERNEL);
901 if (qphy->emu_dcm_reset_seq) {
902 qphy->emu_dcm_reset_seq_len =
903 (size / sizeof(*qphy->emu_dcm_reset_seq));
904 if (qphy->emu_dcm_reset_seq_len % 2) {
905 dev_err(dev, "invalid emu_dcm_reset_seq_len\n");
906 return -EINVAL;
907 }
908
909 of_property_read_u32_array(dev->of_node,
910 "qcom,emu-dcm-reset-seq",
911 qphy->emu_dcm_reset_seq,
912 qphy->emu_dcm_reset_seq_len);
913 } else {
914 dev_dbg(dev, "error allocating memory for emu_dcm_reset_seq\n");
915 }
916 }
917
Mayank Rana147f41d2016-08-12 10:07:45 -0700918 size = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700919 of_get_property(dev->of_node, "qcom,qusb-phy-init-seq", &size);
920 if (size) {
921 qphy->qusb_phy_init_seq = devm_kzalloc(dev,
922 size, GFP_KERNEL);
923 if (qphy->qusb_phy_init_seq) {
924 qphy->init_seq_len =
925 (size / sizeof(*qphy->qusb_phy_init_seq));
926 if (qphy->init_seq_len % 2) {
927 dev_err(dev, "invalid init_seq_len\n");
928 return -EINVAL;
929 }
930
931 of_property_read_u32_array(dev->of_node,
932 "qcom,qusb-phy-init-seq",
933 qphy->qusb_phy_init_seq,
934 qphy->init_seq_len);
935 } else {
936 dev_err(dev, "error allocating memory for phy_init_seq\n");
937 }
938 }
939
940 qphy->ulpi_mode = false;
941 ret = of_property_read_string(dev->of_node, "phy_type", &phy_type);
942
943 if (!ret) {
944 if (!strcasecmp(phy_type, "ulpi"))
945 qphy->ulpi_mode = true;
946 } else {
947 dev_err(dev, "error reading phy_type property\n");
948 return ret;
949 }
950
951 hold_phy_reset = of_property_read_bool(dev->of_node, "qcom,hold-reset");
952
953 /* use default major revision as 2 */
954 qphy->major_rev = 2;
955 ret = of_property_read_u32(dev->of_node, "qcom,major-rev",
956 &qphy->major_rev);
957
958 ret = of_property_read_u32_array(dev->of_node, "qcom,vdd-voltage-level",
959 (u32 *) qphy->vdd_levels,
960 ARRAY_SIZE(qphy->vdd_levels));
961 if (ret) {
962 dev_err(dev, "error reading qcom,vdd-voltage-level property\n");
963 return ret;
964 }
965
966 qphy->vdd = devm_regulator_get(dev, "vdd");
967 if (IS_ERR(qphy->vdd)) {
968 dev_err(dev, "unable to get vdd supply\n");
969 return PTR_ERR(qphy->vdd);
970 }
971
972 qphy->vdda33 = devm_regulator_get(dev, "vdda33");
973 if (IS_ERR(qphy->vdda33)) {
974 dev_err(dev, "unable to get vdda33 supply\n");
975 return PTR_ERR(qphy->vdda33);
976 }
977
978 qphy->vdda18 = devm_regulator_get(dev, "vdda18");
979 if (IS_ERR(qphy->vdda18)) {
980 dev_err(dev, "unable to get vdda18 supply\n");
981 return PTR_ERR(qphy->vdda18);
982 }
983
984 platform_set_drvdata(pdev, qphy);
985
986 qphy->phy.label = "msm-qusb-phy";
987 qphy->phy.init = qusb_phy_init;
988 qphy->phy.set_suspend = qusb_phy_set_suspend;
989 qphy->phy.shutdown = qusb_phy_shutdown;
990 qphy->phy.type = USB_PHY_TYPE_USB2;
991 qphy->phy.notify_connect = qusb_phy_notify_connect;
992 qphy->phy.notify_disconnect = qusb_phy_notify_disconnect;
993
994 /*
995 * On some platforms multiple QUSB PHYs are available. If QUSB PHY is
996 * not used, there is leakage current seen with QUSB PHY related voltage
997 * rail. Hence keep QUSB PHY into reset state explicitly here.
998 */
Amit Nischal4d278212016-06-06 17:54:34 +0530999 if (hold_phy_reset) {
1000 ret = reset_control_assert(qphy->phy_reset);
1001 if (ret)
1002 dev_err(dev, "%s:phy_reset assert failed\n", __func__);
1003 }
Mayank Rana3cb43a32016-08-02 12:10:20 -07001004
1005 ret = usb_add_phy_dev(&qphy->phy);
1006 if (ret)
1007 return ret;
1008
1009 ret = qusb_phy_regulator_init(qphy);
1010 if (ret)
1011 usb_remove_phy(&qphy->phy);
1012
1013 return ret;
1014}
1015
1016static int qusb_phy_remove(struct platform_device *pdev)
1017{
1018 struct qusb_phy *qphy = platform_get_drvdata(pdev);
1019
1020 usb_remove_phy(&qphy->phy);
1021
1022 if (qphy->clocks_enabled) {
1023 clk_disable_unprepare(qphy->cfg_ahb_clk);
1024 clk_disable_unprepare(qphy->ref_clk);
1025 clk_disable_unprepare(qphy->ref_clk_src);
1026 qphy->clocks_enabled = false;
1027 }
1028
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +05301029 qusb_phy_enable_power(qphy, false);
Mayank Rana3cb43a32016-08-02 12:10:20 -07001030
1031 return 0;
1032}
1033
1034static const struct of_device_id qusb_phy_id_table[] = {
1035 { .compatible = "qcom,qusb2phy", },
1036 { },
1037};
1038MODULE_DEVICE_TABLE(of, qusb_phy_id_table);
1039
1040static struct platform_driver qusb_phy_driver = {
1041 .probe = qusb_phy_probe,
1042 .remove = qusb_phy_remove,
1043 .driver = {
1044 .name = "msm-qusb-phy",
1045 .of_match_table = of_match_ptr(qusb_phy_id_table),
1046 },
1047};
1048
1049module_platform_driver(qusb_phy_driver);
1050
1051MODULE_DESCRIPTION("MSM QUSB2 PHY driver");
1052MODULE_LICENSE("GPL v2");