blob: 9c33c6e7b4205b488af814baabec1ff191741ccc [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
Vijayavardhan Vennapusa5ad836f2017-01-03 12:25:36 +053048#define QUSB2PHY_PORT_TEST_CTRL 0xB8
49
Mayank Rana3cb43a32016-08-02 12:10:20 -070050#define QUSB2PHY_PWR_CTRL1 0x210
51#define PWR_CTRL1_CLAMP_N_EN BIT(1)
52#define PWR_CTRL1_POWR_DOWN BIT(0)
53
54#define QUSB2PHY_PLL_COMMON_STATUS_ONE 0x1A0
55#define CORE_READY_STATUS BIT(0)
56
57#define QUSB2PHY_PORT_UTMI_CTRL1 0xC0
58#define TERM_SELECT BIT(4)
59#define XCVR_SELECT_FS BIT(2)
60#define OP_MODE_NON_DRIVE BIT(0)
61
62#define QUSB2PHY_PORT_UTMI_CTRL2 0xC4
63#define UTMI_ULPI_SEL BIT(7)
64#define UTMI_TEST_MUX_SEL BIT(6)
65
66#define QUSB2PHY_PLL_TEST 0x04
67#define CLK_REF_SEL BIT(7)
68
69#define QUSB2PHY_PORT_TUNE1 0x80
70#define QUSB2PHY_PORT_TUNE2 0x84
71#define QUSB2PHY_PORT_TUNE3 0x88
72#define QUSB2PHY_PORT_TUNE4 0x8C
Vijayavardhan Vennapusad51acfc2017-02-16 16:25:35 +053073#define QUSB2PHY_PORT_TUNE5 0x90
Mayank Rana3cb43a32016-08-02 12:10:20 -070074
Mayank Rana3cb43a32016-08-02 12:10:20 -070075/* Get TUNE2's high nibble value read from efuse */
76#define TUNE2_HIGH_NIBBLE_VAL(val, pos, mask) ((val >> pos) & mask)
77
78#define QUSB2PHY_PORT_INTR_CTRL 0xBC
79#define CHG_DET_INTR_EN BIT(4)
80#define DMSE_INTR_HIGH_SEL BIT(3)
81#define DMSE_INTR_EN BIT(2)
82#define DPSE_INTR_HIGH_SEL BIT(1)
83#define DPSE_INTR_EN BIT(0)
84
85#define QUSB2PHY_PORT_UTMI_STATUS 0xF4
86#define LINESTATE_DP BIT(0)
87#define LINESTATE_DM BIT(1)
88
89
90#define QUSB2PHY_1P8_VOL_MIN 1800000 /* uV */
91#define QUSB2PHY_1P8_VOL_MAX 1800000 /* uV */
92#define QUSB2PHY_1P8_HPM_LOAD 30000 /* uA */
93
94#define QUSB2PHY_3P3_VOL_MIN 3075000 /* uV */
95#define QUSB2PHY_3P3_VOL_MAX 3200000 /* uV */
96#define QUSB2PHY_3P3_HPM_LOAD 30000 /* uA */
97
98#define QUSB2PHY_REFCLK_ENABLE BIT(0)
99
Vijayavardhan Vennapusad51acfc2017-02-16 16:25:35 +0530100static unsigned int tune1;
101module_param(tune1, uint, 0644);
102MODULE_PARM_DESC(tune1, "QUSB PHY TUNE1");
103
104static unsigned int tune2;
105module_param(tune2, uint, 0644);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700106MODULE_PARM_DESC(tune2, "QUSB PHY TUNE2");
107
Vijayavardhan Vennapusad51acfc2017-02-16 16:25:35 +0530108static unsigned int tune3;
109module_param(tune3, uint, 0644);
110MODULE_PARM_DESC(tune3, "QUSB PHY TUNE3");
111
112static unsigned int tune4;
113module_param(tune4, uint, 0644);
114MODULE_PARM_DESC(tune4, "QUSB PHY TUNE4");
115
116static unsigned int tune5;
117module_param(tune5, uint, 0644);
118MODULE_PARM_DESC(tune5, "QUSB PHY TUNE5");
119
120
Mayank Rana3cb43a32016-08-02 12:10:20 -0700121struct qusb_phy {
122 struct usb_phy phy;
123 void __iomem *base;
124 void __iomem *tune2_efuse_reg;
125 void __iomem *ref_clk_base;
Chandana Kishori Chiluveru94bd0752017-11-08 17:48:03 +0530126 void __iomem *tcsr_clamp_dig_n;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700127
128 struct clk *ref_clk_src;
129 struct clk *ref_clk;
130 struct clk *cfg_ahb_clk;
Amit Nischal4d278212016-06-06 17:54:34 +0530131 struct reset_control *phy_reset;
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530132 struct clk *iface_clk;
133 struct clk *core_clk;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700134
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530135 struct regulator *gdsc;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700136 struct regulator *vdd;
137 struct regulator *vdda33;
138 struct regulator *vdda18;
139 int vdd_levels[3]; /* none, low, high */
140 int init_seq_len;
141 int *qusb_phy_init_seq;
142 u32 major_rev;
143
144 u32 tune2_val;
145 int tune2_efuse_bit_pos;
146 int tune2_efuse_num_of_bits;
Vijayavardhan Vennapusad5954b92017-08-18 11:59:02 +0530147 int tune2_efuse_correction;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700148
149 bool power_enabled;
150 bool clocks_enabled;
151 bool cable_connected;
152 bool suspended;
153 bool ulpi_mode;
Mayank Ranadadae592017-07-21 09:03:32 -0700154 bool dpdm_enable;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700155 bool is_se_clk;
156
157 struct regulator_desc dpdm_rdesc;
158 struct regulator_dev *dpdm_rdev;
159
160 /* emulation targets specific */
161 void __iomem *emu_phy_base;
162 bool emulation;
163 int *emu_init_seq;
164 int emu_init_seq_len;
165 int *phy_pll_reset_seq;
166 int phy_pll_reset_seq_len;
167 int *emu_dcm_reset_seq;
168 int emu_dcm_reset_seq_len;
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530169 bool put_into_high_z_state;
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530170 struct mutex phy_lock;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700171};
172
173static void qusb_phy_enable_clocks(struct qusb_phy *qphy, bool on)
174{
175 dev_dbg(qphy->phy.dev, "%s(): clocks_enabled:%d on:%d\n",
176 __func__, qphy->clocks_enabled, on);
177
178 if (!qphy->clocks_enabled && on) {
179 clk_prepare_enable(qphy->ref_clk_src);
180 clk_prepare_enable(qphy->ref_clk);
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530181 clk_prepare_enable(qphy->iface_clk);
182 clk_prepare_enable(qphy->core_clk);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700183 clk_prepare_enable(qphy->cfg_ahb_clk);
184 qphy->clocks_enabled = true;
185 }
186
187 if (qphy->clocks_enabled && !on) {
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530188 clk_disable_unprepare(qphy->cfg_ahb_clk);
189 /*
190 * FSM depedency beween iface_clk and core_clk.
191 * Hence turned off core_clk before iface_clk.
192 */
193 clk_disable_unprepare(qphy->core_clk);
194 clk_disable_unprepare(qphy->iface_clk);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700195 clk_disable_unprepare(qphy->ref_clk);
196 clk_disable_unprepare(qphy->ref_clk_src);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700197 qphy->clocks_enabled = false;
198 }
199
200 dev_dbg(qphy->phy.dev, "%s(): clocks_enabled:%d\n", __func__,
201 qphy->clocks_enabled);
202}
203
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530204static int qusb_phy_gdsc(struct qusb_phy *qphy, bool on)
205{
206 int ret;
207
208 if (IS_ERR_OR_NULL(qphy->gdsc))
209 return -EPERM;
210
211 if (on) {
212 dev_dbg(qphy->phy.dev, "TURNING ON GDSC\n");
213 ret = regulator_enable(qphy->gdsc);
214 if (ret) {
215 dev_err(qphy->phy.dev, "unable to enable gdsc\n");
216 return ret;
217 }
218 } else {
219 dev_dbg(qphy->phy.dev, "TURNING OFF GDSC\n");
220 ret = regulator_disable(qphy->gdsc);
221 if (ret) {
222 dev_err(qphy->phy.dev, "unable to disable gdsc\n");
223 return ret;
224 }
225 }
226
227 return ret;
228}
229
Mayank Rana3cb43a32016-08-02 12:10:20 -0700230static int qusb_phy_config_vdd(struct qusb_phy *qphy, int high)
231{
232 int min, ret;
233
234 min = high ? 1 : 0; /* low or none? */
235 ret = regulator_set_voltage(qphy->vdd, qphy->vdd_levels[min],
236 qphy->vdd_levels[2]);
237 if (ret) {
238 dev_err(qphy->phy.dev, "unable to set voltage for qusb vdd\n");
239 return ret;
240 }
241
242 dev_dbg(qphy->phy.dev, "min_vol:%d max_vol:%d\n",
243 qphy->vdd_levels[min], qphy->vdd_levels[2]);
244 return ret;
245}
246
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530247static int qusb_phy_enable_power(struct qusb_phy *qphy, bool on)
Mayank Rana3cb43a32016-08-02 12:10:20 -0700248{
249 int ret = 0;
250
251 dev_dbg(qphy->phy.dev, "%s turn %s regulators. power_enabled:%d\n",
252 __func__, on ? "on" : "off", qphy->power_enabled);
253
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530254 if (qphy->power_enabled == on) {
Mayank Rana3cb43a32016-08-02 12:10:20 -0700255 dev_dbg(qphy->phy.dev, "PHYs' regulators are already ON.\n");
256 return 0;
257 }
258
259 if (!on)
260 goto disable_vdda33;
261
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530262 ret = qusb_phy_config_vdd(qphy, true);
263 if (ret) {
264 dev_err(qphy->phy.dev, "Unable to config VDD:%d\n",
265 ret);
266 goto err_vdd;
267 }
Mayank Rana3cb43a32016-08-02 12:10:20 -0700268
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530269 ret = regulator_enable(qphy->vdd);
270 if (ret) {
271 dev_err(qphy->phy.dev, "Unable to enable VDD\n");
272 goto unconfig_vdd;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700273 }
274
275 ret = regulator_set_load(qphy->vdda18, QUSB2PHY_1P8_HPM_LOAD);
276 if (ret < 0) {
277 dev_err(qphy->phy.dev, "Unable to set HPM of vdda18:%d\n", ret);
278 goto disable_vdd;
279 }
280
281 ret = regulator_set_voltage(qphy->vdda18, QUSB2PHY_1P8_VOL_MIN,
282 QUSB2PHY_1P8_VOL_MAX);
283 if (ret) {
284 dev_err(qphy->phy.dev,
285 "Unable to set voltage for vdda18:%d\n", ret);
286 goto put_vdda18_lpm;
287 }
288
289 ret = regulator_enable(qphy->vdda18);
290 if (ret) {
291 dev_err(qphy->phy.dev, "Unable to enable vdda18:%d\n", ret);
292 goto unset_vdda18;
293 }
294
295 ret = regulator_set_load(qphy->vdda33, QUSB2PHY_3P3_HPM_LOAD);
296 if (ret < 0) {
297 dev_err(qphy->phy.dev, "Unable to set HPM of vdda33:%d\n", ret);
298 goto disable_vdda18;
299 }
300
301 ret = regulator_set_voltage(qphy->vdda33, QUSB2PHY_3P3_VOL_MIN,
302 QUSB2PHY_3P3_VOL_MAX);
303 if (ret) {
304 dev_err(qphy->phy.dev,
305 "Unable to set voltage for vdda33:%d\n", ret);
306 goto put_vdda33_lpm;
307 }
308
309 ret = regulator_enable(qphy->vdda33);
310 if (ret) {
311 dev_err(qphy->phy.dev, "Unable to enable vdda33:%d\n", ret);
312 goto unset_vdd33;
313 }
314
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530315 qphy->power_enabled = true;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700316
317 pr_debug("%s(): QUSB PHY's regulators are turned ON.\n", __func__);
318 return ret;
319
320disable_vdda33:
321 ret = regulator_disable(qphy->vdda33);
322 if (ret)
323 dev_err(qphy->phy.dev, "Unable to disable vdda33:%d\n", ret);
324
325unset_vdd33:
326 ret = regulator_set_voltage(qphy->vdda33, 0, QUSB2PHY_3P3_VOL_MAX);
327 if (ret)
328 dev_err(qphy->phy.dev,
329 "Unable to set (0) voltage for vdda33:%d\n", ret);
330
331put_vdda33_lpm:
332 ret = regulator_set_load(qphy->vdda33, 0);
333 if (ret < 0)
334 dev_err(qphy->phy.dev, "Unable to set (0) HPM of vdda33\n");
335
336disable_vdda18:
337 ret = regulator_disable(qphy->vdda18);
338 if (ret)
339 dev_err(qphy->phy.dev, "Unable to disable vdda18:%d\n", ret);
340
341unset_vdda18:
342 ret = regulator_set_voltage(qphy->vdda18, 0, QUSB2PHY_1P8_VOL_MAX);
343 if (ret)
344 dev_err(qphy->phy.dev,
345 "Unable to set (0) voltage for vdda18:%d\n", ret);
346
347put_vdda18_lpm:
348 ret = regulator_set_load(qphy->vdda18, 0);
349 if (ret < 0)
350 dev_err(qphy->phy.dev, "Unable to set LPM of vdda18\n");
351
352disable_vdd:
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530353 ret = regulator_disable(qphy->vdd);
354 if (ret)
355 dev_err(qphy->phy.dev, "Unable to disable vdd:%d\n",
Mayank Rana3cb43a32016-08-02 12:10:20 -0700356 ret);
357
358unconfig_vdd:
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530359 ret = qusb_phy_config_vdd(qphy, false);
360 if (ret)
361 dev_err(qphy->phy.dev, "Unable unconfig VDD:%d\n",
Mayank Rana3cb43a32016-08-02 12:10:20 -0700362 ret);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700363err_vdd:
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530364 qphy->power_enabled = false;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700365 dev_dbg(qphy->phy.dev, "QUSB PHY's regulators are turned OFF.\n");
366 return ret;
367}
368
Mayank Rana3cb43a32016-08-02 12:10:20 -0700369static void qusb_phy_get_tune2_param(struct qusb_phy *qphy)
370{
371 u8 num_of_bits;
372 u32 bit_mask = 1;
Vijayavardhan Vennapusad5954b92017-08-18 11:59:02 +0530373 u8 reg_val;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700374
375 pr_debug("%s(): num_of_bits:%d bit_pos:%d\n", __func__,
376 qphy->tune2_efuse_num_of_bits,
377 qphy->tune2_efuse_bit_pos);
378
379 /* get bit mask based on number of bits to use with efuse reg */
380 if (qphy->tune2_efuse_num_of_bits) {
381 num_of_bits = qphy->tune2_efuse_num_of_bits;
382 bit_mask = (bit_mask << num_of_bits) - 1;
383 }
384
385 /*
386 * Read EFUSE register having TUNE2 parameter's high nibble.
Vijayavardhan Vennapusad5954b92017-08-18 11:59:02 +0530387 * If efuse register shows value as 0x0, then use previous value
388 * as it is. Otherwise use efuse register based value for this purpose.
Mayank Rana3cb43a32016-08-02 12:10:20 -0700389 */
390 qphy->tune2_val = readl_relaxed(qphy->tune2_efuse_reg);
391 pr_debug("%s(): bit_mask:%d efuse based tune2 value:%d\n",
392 __func__, bit_mask, qphy->tune2_val);
393
394 qphy->tune2_val = TUNE2_HIGH_NIBBLE_VAL(qphy->tune2_val,
395 qphy->tune2_efuse_bit_pos, bit_mask);
396
Vijayavardhan Vennapusad5954b92017-08-18 11:59:02 +0530397 /* Update higher nibble of TUNE2 value for better rise/fall times */
398 if (qphy->tune2_efuse_correction && qphy->tune2_val) {
399 if (qphy->tune2_efuse_correction > 5 ||
400 qphy->tune2_efuse_correction < -10)
401 pr_warn("Correction value is out of range : %d\n",
402 qphy->tune2_efuse_correction);
403 else
404 qphy->tune2_val = qphy->tune2_val +
405 qphy->tune2_efuse_correction;
406 }
Mayank Rana3cb43a32016-08-02 12:10:20 -0700407
Vijayavardhan Vennapusad5954b92017-08-18 11:59:02 +0530408 reg_val = readb_relaxed(qphy->base + QUSB2PHY_PORT_TUNE2);
409 if (qphy->tune2_val) {
410 reg_val &= 0x0f;
411 reg_val |= (qphy->tune2_val << 4);
412 }
413
414 qphy->tune2_val = reg_val;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700415}
416
417static void qusb_phy_write_seq(void __iomem *base, u32 *seq, int cnt,
418 unsigned long delay)
419{
420 int i;
421
422 pr_debug("Seq count:%d\n", cnt);
423 for (i = 0; i < cnt; i = i+2) {
424 pr_debug("write 0x%02x to 0x%02x\n", seq[i], seq[i+1]);
425 writel_relaxed(seq[i], base + seq[i+1]);
426 if (delay)
427 usleep_range(delay, (delay + 2000));
428 }
429}
430
431static int qusb_phy_init(struct usb_phy *phy)
432{
433 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
434 int ret, reset_val = 0;
435 u8 reg;
436 bool pll_lock_fail = false;
437
438 dev_dbg(phy->dev, "%s\n", __func__);
439
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530440 ret = qusb_phy_enable_power(qphy, true);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700441 if (ret)
442 return ret;
443
444 qusb_phy_enable_clocks(qphy, true);
445
446 /*
447 * ref clock is enabled by default after power on reset. Linux clock
448 * driver will disable this clock as part of late init if peripheral
449 * driver(s) does not explicitly votes for it. Linux clock driver also
450 * does not disable the clock until late init even if peripheral
451 * driver explicitly requests it and cannot defer the probe until late
452 * init. Hence, Explicitly disable the clock using register write to
453 * allow QUSB PHY PLL to lock properly.
454 */
455 if (qphy->ref_clk_base) {
456 writel_relaxed((readl_relaxed(qphy->ref_clk_base) &
457 ~QUSB2PHY_REFCLK_ENABLE),
458 qphy->ref_clk_base);
459 /* Make sure that above write complete to get ref clk OFF */
460 wmb();
461 }
462
463 /* Perform phy reset */
Amit Nischal4d278212016-06-06 17:54:34 +0530464 ret = reset_control_assert(qphy->phy_reset);
465 if (ret)
466 dev_err(phy->dev, "%s: phy_reset assert failed\n", __func__);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700467 usleep_range(100, 150);
Amit Nischal4d278212016-06-06 17:54:34 +0530468 ret = reset_control_deassert(qphy->phy_reset);
469 if (ret)
470 dev_err(phy->dev, "%s: phy_reset deassert failed\n", __func__);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700471
472 if (qphy->emulation) {
473 if (qphy->emu_init_seq)
474 qusb_phy_write_seq(qphy->emu_phy_base,
475 qphy->emu_init_seq, qphy->emu_init_seq_len, 0);
476
477 if (qphy->qusb_phy_init_seq)
478 qusb_phy_write_seq(qphy->base, qphy->qusb_phy_init_seq,
479 qphy->init_seq_len, 0);
480
481 /* Wait for 5ms as per QUSB2 RUMI sequence */
482 usleep_range(5000, 7000);
483
484 if (qphy->phy_pll_reset_seq)
485 qusb_phy_write_seq(qphy->base, qphy->phy_pll_reset_seq,
486 qphy->phy_pll_reset_seq_len, 10000);
487
488 if (qphy->emu_dcm_reset_seq)
489 qusb_phy_write_seq(qphy->emu_phy_base,
490 qphy->emu_dcm_reset_seq,
491 qphy->emu_dcm_reset_seq_len, 10000);
492
493 return 0;
494 }
495
496 /* Disable the PHY */
497 if (qphy->major_rev < 2)
498 writel_relaxed(CLAMP_N_EN | FREEZIO_N | POWER_DOWN,
499 qphy->base + QUSB2PHY_PORT_POWERDOWN);
500 else
501 writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) |
502 PWR_CTRL1_POWR_DOWN,
503 qphy->base + QUSB2PHY_PWR_CTRL1);
504
505 /* configure for ULPI mode if requested */
506 if (qphy->ulpi_mode)
507 writel_relaxed(0x0, qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
508
509 /* save reset value to override based on clk scheme */
510 if (qphy->ref_clk_base)
511 reset_val = readl_relaxed(qphy->base + QUSB2PHY_PLL_TEST);
512
513 if (qphy->qusb_phy_init_seq)
514 qusb_phy_write_seq(qphy->base, qphy->qusb_phy_init_seq,
515 qphy->init_seq_len, 0);
516
517 /*
518 * Check for EFUSE value only if tune2_efuse_reg is available
519 * and try to read EFUSE value only once i.e. not every USB
520 * cable connect case.
521 */
Vijayavardhan Vennapusad5954b92017-08-18 11:59:02 +0530522 if (qphy->tune2_efuse_reg && !tune2) {
Mayank Rana3cb43a32016-08-02 12:10:20 -0700523 if (!qphy->tune2_val)
524 qusb_phy_get_tune2_param(qphy);
525
526 pr_debug("%s(): Programming TUNE2 parameter as:%x\n", __func__,
527 qphy->tune2_val);
528 writel_relaxed(qphy->tune2_val,
529 qphy->base + QUSB2PHY_PORT_TUNE2);
530 }
531
Vijayavardhan Vennapusad51acfc2017-02-16 16:25:35 +0530532 /* If tune modparam set, override tune value */
533
534 pr_debug("%s():userspecified modparams TUNEX val:0x%x %x %x %x %x\n",
535 __func__, tune1, tune2, tune3, tune4, tune5);
536 if (tune1)
537 writel_relaxed(tune1,
538 qphy->base + QUSB2PHY_PORT_TUNE1);
539
540 if (tune2)
Mayank Rana3cb43a32016-08-02 12:10:20 -0700541 writel_relaxed(tune2,
542 qphy->base + QUSB2PHY_PORT_TUNE2);
Vijayavardhan Vennapusad51acfc2017-02-16 16:25:35 +0530543
544 if (tune3)
545 writel_relaxed(tune3,
546 qphy->base + QUSB2PHY_PORT_TUNE3);
547
548 if (tune4)
549 writel_relaxed(tune4,
550 qphy->base + QUSB2PHY_PORT_TUNE4);
551
552 if (tune5)
553 writel_relaxed(tune5,
554 qphy->base + QUSB2PHY_PORT_TUNE5);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700555
556 /* ensure above writes are completed before re-enabling PHY */
557 wmb();
558
559 /* Enable the PHY */
560 if (qphy->major_rev < 2)
561 writel_relaxed(CLAMP_N_EN | FREEZIO_N,
562 qphy->base + QUSB2PHY_PORT_POWERDOWN);
563 else
564 writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) &
565 ~PWR_CTRL1_POWR_DOWN,
566 qphy->base + QUSB2PHY_PWR_CTRL1);
567
568 /* Ensure above write is completed before turning ON ref clk */
569 wmb();
570
571 /* Require to get phy pll lock successfully */
572 usleep_range(150, 160);
573
574 /* Turn on phy ref_clk if DIFF_CLK else select SE_CLK */
575 if (qphy->ref_clk_base) {
576 if (!qphy->is_se_clk) {
577 reset_val &= ~CLK_REF_SEL;
578 writel_relaxed((readl_relaxed(qphy->ref_clk_base) |
579 QUSB2PHY_REFCLK_ENABLE),
580 qphy->ref_clk_base);
581 } else {
582 reset_val |= CLK_REF_SEL;
583 writel_relaxed(reset_val,
584 qphy->base + QUSB2PHY_PLL_TEST);
585 }
586
587 /* Make sure above write is completed to get PLL source clock */
588 wmb();
589
590 /* Required to get PHY PLL lock successfully */
591 usleep_range(100, 110);
592 }
593
594 if (qphy->major_rev < 2) {
595 reg = readb_relaxed(qphy->base + QUSB2PHY_PLL_STATUS);
596 dev_dbg(phy->dev, "QUSB2PHY_PLL_STATUS:%x\n", reg);
597 if (!(reg & QUSB2PHY_PLL_LOCK))
598 pll_lock_fail = true;
599 } else {
600 reg = readb_relaxed(qphy->base +
601 QUSB2PHY_PLL_COMMON_STATUS_ONE);
602 dev_dbg(phy->dev, "QUSB2PHY_PLL_COMMON_STATUS_ONE:%x\n", reg);
603 if (!(reg & CORE_READY_STATUS))
604 pll_lock_fail = true;
605 }
606
607 if (pll_lock_fail) {
608 dev_err(phy->dev, "QUSB PHY PLL LOCK fails:%x\n", reg);
609 WARN_ON(1);
610 }
611
612 return 0;
613}
614
615static void qusb_phy_shutdown(struct usb_phy *phy)
616{
617 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
618
619 dev_dbg(phy->dev, "%s\n", __func__);
620
621 qusb_phy_enable_clocks(qphy, true);
622
623 /* Disable the PHY */
624 if (qphy->major_rev < 2)
625 writel_relaxed(CLAMP_N_EN | FREEZIO_N | POWER_DOWN,
626 qphy->base + QUSB2PHY_PORT_POWERDOWN);
627 else
628 writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) |
629 PWR_CTRL1_POWR_DOWN,
630 qphy->base + QUSB2PHY_PWR_CTRL1);
631
632 /* Make sure above write complete before turning off clocks */
633 wmb();
634
635 qusb_phy_enable_clocks(qphy, false);
636}
637/**
638 * Performs QUSB2 PHY suspend/resume functionality.
639 *
640 * @uphy - usb phy pointer.
641 * @suspend - to enable suspend or not. 1 - suspend, 0 - resume
642 *
643 */
644static int qusb_phy_set_suspend(struct usb_phy *phy, int suspend)
645{
646 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
647 u32 linestate = 0, intr_mask = 0;
648
649 if (qphy->suspended && suspend) {
650 dev_dbg(phy->dev, "%s: USB PHY is already suspended\n",
651 __func__);
652 return 0;
653 }
654
655 if (suspend) {
656 /* Bus suspend case */
657 if (qphy->cable_connected ||
658 (qphy->phy.flags & PHY_HOST_MODE)) {
659 /* Clear all interrupts */
660 writel_relaxed(0x00,
661 qphy->base + QUSB2PHY_PORT_INTR_CTRL);
662
663 linestate = readl_relaxed(qphy->base +
664 QUSB2PHY_PORT_UTMI_STATUS);
665
666 /*
667 * D+/D- interrupts are level-triggered, but we are
668 * only interested if the line state changes, so enable
669 * the high/low trigger based on current state. In
670 * other words, enable the triggers _opposite_ of what
671 * the current D+/D- levels are.
672 * e.g. if currently D+ high, D- low (HS 'J'/Suspend),
673 * configure the mask to trigger on D+ low OR D- high
674 */
675 intr_mask = DPSE_INTR_EN | DMSE_INTR_EN;
676 if (!(linestate & LINESTATE_DP)) /* D+ low */
677 intr_mask |= DPSE_INTR_HIGH_SEL;
678 if (!(linestate & LINESTATE_DM)) /* D- low */
679 intr_mask |= DMSE_INTR_HIGH_SEL;
680
681 writel_relaxed(intr_mask,
682 qphy->base + QUSB2PHY_PORT_INTR_CTRL);
683
Vijayavardhan Vennapusa71265322017-06-21 12:40:13 +0530684 if (linestate & (LINESTATE_DP | LINESTATE_DM)) {
685 /* enable phy auto-resume */
686 writel_relaxed(0x0C,
Vijayavardhan Vennapusa5ad836f2017-01-03 12:25:36 +0530687 qphy->base + QUSB2PHY_PORT_TEST_CTRL);
Vijayavardhan Vennapusa71265322017-06-21 12:40:13 +0530688 /* flush the previous write before next write */
689 wmb();
690 writel_relaxed(0x04,
691 qphy->base + QUSB2PHY_PORT_TEST_CTRL);
692 }
Vijayavardhan Vennapusa5ad836f2017-01-03 12:25:36 +0530693
694
695 dev_dbg(phy->dev, "%s: intr_mask = %x\n",
696 __func__, intr_mask);
697
698 /* Makes sure that above write goes through */
699 wmb();
700
Mayank Rana3cb43a32016-08-02 12:10:20 -0700701 qusb_phy_enable_clocks(qphy, false);
702 } else { /* Disconnect case */
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530703 mutex_lock(&qphy->phy_lock);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700704 /* Disable all interrupts */
705 writel_relaxed(0x00,
706 qphy->base + QUSB2PHY_PORT_INTR_CTRL);
Vijayavardhan Vennapusa62caca32017-05-04 13:00:54 +0530707
708 /* Disable PHY */
709 writel_relaxed(POWER_DOWN,
710 qphy->base + QUSB2PHY_PORT_POWERDOWN);
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530711 /* Make sure that above write is completed */
712 wmb();
Mayank Rana3cb43a32016-08-02 12:10:20 -0700713
714 qusb_phy_enable_clocks(qphy, false);
Chandana Kishori Chiluveru94bd0752017-11-08 17:48:03 +0530715 if (qphy->tcsr_clamp_dig_n)
716 writel_relaxed(0x0,
717 qphy->tcsr_clamp_dig_n);
Manu Gautam182d67e2016-12-15 11:39:46 +0530718 /* Do not disable power rails if there is vote for it */
719 if (!qphy->dpdm_enable)
720 qusb_phy_enable_power(qphy, false);
721 else
722 dev_dbg(phy->dev, "race with rm_pulldown. Keep ldo ON\n");
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530723 mutex_unlock(&qphy->phy_lock);
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530724
725 /*
726 * Set put_into_high_z_state to true so next USB
727 * cable connect, DPF_DMF request performs PHY
728 * reset and put it into high-z state. For bootup
729 * with or without USB cable, it doesn't require
730 * to put QUSB PHY into high-z state.
731 */
732 qphy->put_into_high_z_state = true;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700733 }
734 qphy->suspended = true;
735 } else {
736 /* Bus suspend case */
737 if (qphy->cable_connected ||
738 (qphy->phy.flags & PHY_HOST_MODE)) {
739 qusb_phy_enable_clocks(qphy, true);
740 /* Clear all interrupts on resume */
741 writel_relaxed(0x00,
742 qphy->base + QUSB2PHY_PORT_INTR_CTRL);
743 } else {
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530744 qusb_phy_enable_power(qphy, true);
Chandana Kishori Chiluveru94bd0752017-11-08 17:48:03 +0530745 if (qphy->tcsr_clamp_dig_n)
746 writel_relaxed(0x1,
747 qphy->tcsr_clamp_dig_n);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700748 qusb_phy_enable_clocks(qphy, true);
749 }
750 qphy->suspended = false;
751 }
752
753 return 0;
754}
755
756static int qusb_phy_notify_connect(struct usb_phy *phy,
757 enum usb_device_speed speed)
758{
759 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
760
761 qphy->cable_connected = true;
762
763 dev_dbg(phy->dev, "QUSB PHY: connect notification cable_connected=%d\n",
764 qphy->cable_connected);
765 return 0;
766}
767
768static int qusb_phy_notify_disconnect(struct usb_phy *phy,
769 enum usb_device_speed speed)
770{
771 struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
772
773 qphy->cable_connected = false;
774
775 dev_dbg(phy->dev, "QUSB PHY: connect notification cable_connected=%d\n",
776 qphy->cable_connected);
777 return 0;
778}
779
780static int qusb_phy_dpdm_regulator_enable(struct regulator_dev *rdev)
781{
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700782 int ret = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700783 struct qusb_phy *qphy = rdev_get_drvdata(rdev);
784
Mayank Ranadadae592017-07-21 09:03:32 -0700785 dev_dbg(qphy->phy.dev, "%s dpdm_enable:%d\n",
786 __func__, qphy->dpdm_enable);
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700787
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530788 mutex_lock(&qphy->phy_lock);
Mayank Ranadadae592017-07-21 09:03:32 -0700789 if (!qphy->dpdm_enable) {
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530790 ret = qusb_phy_enable_power(qphy, true);
Mayank Ranadadae592017-07-21 09:03:32 -0700791 if (ret < 0) {
792 dev_dbg(qphy->phy.dev,
793 "dpdm regulator enable failed:%d\n", ret);
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530794 mutex_unlock(&qphy->phy_lock);
Mayank Ranadadae592017-07-21 09:03:32 -0700795 return ret;
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700796 }
Mayank Ranadadae592017-07-21 09:03:32 -0700797 qphy->dpdm_enable = true;
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530798 if (qphy->put_into_high_z_state) {
Chandana Kishori Chiluveru94bd0752017-11-08 17:48:03 +0530799 if (qphy->tcsr_clamp_dig_n)
800 writel_relaxed(0x1,
801 qphy->tcsr_clamp_dig_n);
802
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +0530803 qusb_phy_gdsc(qphy, true);
804 qusb_phy_enable_clocks(qphy, true);
805
806 dev_dbg(qphy->phy.dev, "RESET QUSB PHY\n");
807 ret = reset_control_assert(qphy->phy_reset);
808 if (ret)
809 dev_err(qphy->phy.dev, "phyassert failed\n");
810 usleep_range(100, 150);
811 ret = reset_control_deassert(qphy->phy_reset);
812 if (ret)
813 dev_err(qphy->phy.dev, "deassert failed\n");
814
815 /*
816 * Phy in non-driving mode leaves Dp and Dm
817 * lines in high-Z state. Controller power
818 * collapse is not switching phy to non-driving
819 * mode causing charger detection failure. Bring
820 * phy to non-driving mode by overriding
821 * controller output via UTMI interface.
822 */
823 writel_relaxed(TERM_SELECT | XCVR_SELECT_FS |
824 OP_MODE_NON_DRIVE,
825 qphy->base + QUSB2PHY_PORT_UTMI_CTRL1);
826 writel_relaxed(UTMI_ULPI_SEL |
827 UTMI_TEST_MUX_SEL,
828 qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
829
830
831 /* Disable PHY */
832 writel_relaxed(CLAMP_N_EN | FREEZIO_N |
833 POWER_DOWN,
834 qphy->base + QUSB2PHY_PORT_POWERDOWN);
835 /* Make sure that above write is completed */
836 wmb();
837
838 qusb_phy_enable_clocks(qphy, false);
839 qusb_phy_gdsc(qphy, false);
840 }
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700841 }
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530842 mutex_unlock(&qphy->phy_lock);
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700843
844 return ret;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700845}
846
847static int qusb_phy_dpdm_regulator_disable(struct regulator_dev *rdev)
848{
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700849 int ret = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700850 struct qusb_phy *qphy = rdev_get_drvdata(rdev);
851
Mayank Ranadadae592017-07-21 09:03:32 -0700852 dev_dbg(qphy->phy.dev, "%s dpdm_enable:%d\n",
853 __func__, qphy->dpdm_enable);
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700854
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530855 mutex_lock(&qphy->phy_lock);
Mayank Ranadadae592017-07-21 09:03:32 -0700856 if (qphy->dpdm_enable) {
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530857 if (!qphy->cable_connected) {
Chandana Kishori Chiluveru94bd0752017-11-08 17:48:03 +0530858 if (qphy->tcsr_clamp_dig_n)
859 writel_relaxed(0x0,
860 qphy->tcsr_clamp_dig_n);
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530861 dev_dbg(qphy->phy.dev, "turn off for HVDCP case\n");
862 ret = qusb_phy_enable_power(qphy, false);
863 if (ret < 0) {
864 dev_dbg(qphy->phy.dev,
865 "dpdm regulator disable failed:%d\n",
866 ret);
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530867 mutex_unlock(&qphy->phy_lock);
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +0530868 return ret;
869 }
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700870 }
Mayank Ranadadae592017-07-21 09:03:32 -0700871 qphy->dpdm_enable = false;
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700872 }
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +0530873 mutex_unlock(&qphy->phy_lock);
Mayank Ranaa69fe6c2016-08-09 18:04:58 -0700874
875 return ret;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700876}
877
878static int qusb_phy_dpdm_regulator_is_enabled(struct regulator_dev *rdev)
879{
880 struct qusb_phy *qphy = rdev_get_drvdata(rdev);
881
Mayank Ranadadae592017-07-21 09:03:32 -0700882 dev_dbg(qphy->phy.dev, "%s qphy->dpdm_enable = %d\n", __func__,
883 qphy->dpdm_enable);
884 return qphy->dpdm_enable;
Mayank Rana3cb43a32016-08-02 12:10:20 -0700885}
886
887static struct regulator_ops qusb_phy_dpdm_regulator_ops = {
888 .enable = qusb_phy_dpdm_regulator_enable,
889 .disable = qusb_phy_dpdm_regulator_disable,
890 .is_enabled = qusb_phy_dpdm_regulator_is_enabled,
891};
892
893static int qusb_phy_regulator_init(struct qusb_phy *qphy)
894{
895 struct device *dev = qphy->phy.dev;
896 struct regulator_config cfg = {};
897 struct regulator_init_data *init_data;
898
899 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
900 if (!init_data)
901 return -ENOMEM;
902
903 init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_STATUS;
904 qphy->dpdm_rdesc.owner = THIS_MODULE;
905 qphy->dpdm_rdesc.type = REGULATOR_VOLTAGE;
906 qphy->dpdm_rdesc.ops = &qusb_phy_dpdm_regulator_ops;
907 qphy->dpdm_rdesc.name = kbasename(dev->of_node->full_name);
908
909 cfg.dev = dev;
910 cfg.init_data = init_data;
911 cfg.driver_data = qphy;
912 cfg.of_node = dev->of_node;
913
914 qphy->dpdm_rdev = devm_regulator_register(dev, &qphy->dpdm_rdesc, &cfg);
915 if (IS_ERR(qphy->dpdm_rdev))
916 return PTR_ERR(qphy->dpdm_rdev);
917
918 return 0;
919}
920
921static int qusb_phy_probe(struct platform_device *pdev)
922{
923 struct qusb_phy *qphy;
924 struct device *dev = &pdev->dev;
925 struct resource *res;
926 int ret = 0, size = 0;
927 const char *phy_type;
928 bool hold_phy_reset;
929
930 qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
931 if (!qphy)
932 return -ENOMEM;
933
934 qphy->phy.dev = dev;
935 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
936 "qusb_phy_base");
937 qphy->base = devm_ioremap_resource(dev, res);
938 if (IS_ERR(qphy->base))
939 return PTR_ERR(qphy->base);
940
941 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
942 "emu_phy_base");
943 if (res) {
944 qphy->emu_phy_base = devm_ioremap_resource(dev, res);
945 if (IS_ERR(qphy->emu_phy_base)) {
946 dev_dbg(dev, "couldn't ioremap emu_phy_base\n");
947 qphy->emu_phy_base = NULL;
948 }
949 }
950
951 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
952 "tune2_efuse_addr");
953 if (res) {
954 qphy->tune2_efuse_reg = devm_ioremap_nocache(dev, res->start,
955 resource_size(res));
956 if (!IS_ERR_OR_NULL(qphy->tune2_efuse_reg)) {
957 ret = of_property_read_u32(dev->of_node,
958 "qcom,tune2-efuse-bit-pos",
959 &qphy->tune2_efuse_bit_pos);
960 if (!ret) {
961 ret = of_property_read_u32(dev->of_node,
962 "qcom,tune2-efuse-num-bits",
963 &qphy->tune2_efuse_num_of_bits);
964 }
Vijayavardhan Vennapusad5954b92017-08-18 11:59:02 +0530965 of_property_read_u32(dev->of_node,
966 "qcom,tune2-efuse-correction",
967 &qphy->tune2_efuse_correction);
Mayank Rana3cb43a32016-08-02 12:10:20 -0700968
969 if (ret) {
970 dev_err(dev, "DT Value for tune2 efuse is invalid.\n");
971 return -EINVAL;
972 }
973 }
974 }
975
976 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
977 "ref_clk_addr");
978 if (res) {
979 qphy->ref_clk_base = devm_ioremap_nocache(dev,
980 res->start, resource_size(res));
981 if (IS_ERR(qphy->ref_clk_base)) {
982 dev_dbg(dev, "ref_clk_address is not available.\n");
983 return PTR_ERR(qphy->ref_clk_base);
984 }
985
986 ret = of_property_read_string(dev->of_node,
987 "qcom,phy-clk-scheme", &phy_type);
988 if (ret) {
989 dev_err(dev, "error need qsub_phy_clk_scheme.\n");
990 return ret;
991 }
992
993 if (!strcasecmp(phy_type, "cml")) {
994 qphy->is_se_clk = false;
995 } else if (!strcasecmp(phy_type, "cmos")) {
996 qphy->is_se_clk = true;
997 } else {
998 dev_err(dev, "erro invalid qusb_phy_clk_scheme\n");
999 return -EINVAL;
1000 }
1001 }
1002
Chandana Kishori Chiluveru94bd0752017-11-08 17:48:03 +05301003 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1004 "tcsr_clamp_dig_n_1p8");
1005 if (res) {
1006 qphy->tcsr_clamp_dig_n = devm_ioremap_nocache(dev,
1007 res->start, resource_size(res));
1008 if (IS_ERR(qphy->tcsr_clamp_dig_n)) {
1009 dev_err(dev, "err reading tcsr_clamp_dig_n\n");
1010 qphy->tcsr_clamp_dig_n = NULL;
1011 }
1012 }
1013
Mayank Rana3cb43a32016-08-02 12:10:20 -07001014 qphy->ref_clk_src = devm_clk_get(dev, "ref_clk_src");
1015 if (IS_ERR(qphy->ref_clk_src))
1016 dev_dbg(dev, "clk get failed for ref_clk_src\n");
1017
1018 qphy->ref_clk = devm_clk_get(dev, "ref_clk");
1019 if (IS_ERR(qphy->ref_clk))
1020 dev_dbg(dev, "clk get failed for ref_clk\n");
1021 else
1022 clk_set_rate(qphy->ref_clk, 19200000);
1023
1024 qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb_clk");
1025 if (IS_ERR(qphy->cfg_ahb_clk))
1026 return PTR_ERR(qphy->cfg_ahb_clk);
1027
Amit Nischal4d278212016-06-06 17:54:34 +05301028 qphy->phy_reset = devm_reset_control_get(dev, "phy_reset");
Mayank Rana3cb43a32016-08-02 12:10:20 -07001029 if (IS_ERR(qphy->phy_reset))
1030 return PTR_ERR(qphy->phy_reset);
1031
Chandana Kishori Chiluveru9e999cc2017-11-08 17:48:03 +05301032 if (of_property_match_string(dev->of_node,
1033 "clock-names", "iface_clk") >= 0) {
1034 qphy->iface_clk = devm_clk_get(dev, "iface_clk");
1035 if (IS_ERR(qphy->iface_clk)) {
1036 ret = PTR_ERR(qphy->iface_clk);
1037 qphy->iface_clk = NULL;
1038 if (ret == -EPROBE_DEFER)
1039 return ret;
1040 dev_err(dev, "couldn't get iface_clk(%d)\n", ret);
1041 }
1042 }
1043
1044 if (of_property_match_string(dev->of_node,
1045 "clock-names", "core_clk") >= 0) {
1046 qphy->core_clk = devm_clk_get(dev, "core_clk");
1047 if (IS_ERR(qphy->core_clk)) {
1048 ret = PTR_ERR(qphy->core_clk);
1049 qphy->core_clk = NULL;
1050 if (ret == -EPROBE_DEFER)
1051 return ret;
1052 dev_err(dev, "couldn't get core_clk(%d)\n", ret);
1053 }
1054 }
1055
1056 qphy->gdsc = devm_regulator_get(dev, "USB3_GDSC");
1057 if (IS_ERR(qphy->gdsc))
1058 qphy->gdsc = NULL;
1059
Mayank Rana3cb43a32016-08-02 12:10:20 -07001060 qphy->emulation = of_property_read_bool(dev->of_node,
1061 "qcom,emulation");
1062
1063 of_get_property(dev->of_node, "qcom,emu-init-seq", &size);
1064 if (size) {
1065 qphy->emu_init_seq = devm_kzalloc(dev,
1066 size, GFP_KERNEL);
1067 if (qphy->emu_init_seq) {
1068 qphy->emu_init_seq_len =
1069 (size / sizeof(*qphy->emu_init_seq));
1070 if (qphy->emu_init_seq_len % 2) {
1071 dev_err(dev, "invalid emu_init_seq_len\n");
1072 return -EINVAL;
1073 }
1074
1075 of_property_read_u32_array(dev->of_node,
Mayank Rana147f41d2016-08-12 10:07:45 -07001076 "qcom,emu-init-seq",
Mayank Rana3cb43a32016-08-02 12:10:20 -07001077 qphy->emu_init_seq,
1078 qphy->emu_init_seq_len);
1079 } else {
1080 dev_dbg(dev, "error allocating memory for emu_init_seq\n");
1081 }
1082 }
1083
Mayank Rana147f41d2016-08-12 10:07:45 -07001084 size = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -07001085 of_get_property(dev->of_node, "qcom,phy-pll-reset-seq", &size);
1086 if (size) {
1087 qphy->phy_pll_reset_seq = devm_kzalloc(dev,
1088 size, GFP_KERNEL);
1089 if (qphy->phy_pll_reset_seq) {
1090 qphy->phy_pll_reset_seq_len =
1091 (size / sizeof(*qphy->phy_pll_reset_seq));
1092 if (qphy->phy_pll_reset_seq_len % 2) {
1093 dev_err(dev, "invalid phy_pll_reset_seq_len\n");
1094 return -EINVAL;
1095 }
1096
1097 of_property_read_u32_array(dev->of_node,
1098 "qcom,phy-pll-reset-seq",
1099 qphy->phy_pll_reset_seq,
1100 qphy->phy_pll_reset_seq_len);
1101 } else {
1102 dev_dbg(dev, "error allocating memory for phy_pll_reset_seq\n");
1103 }
1104 }
1105
Mayank Rana147f41d2016-08-12 10:07:45 -07001106 size = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -07001107 of_get_property(dev->of_node, "qcom,emu-dcm-reset-seq", &size);
1108 if (size) {
1109 qphy->emu_dcm_reset_seq = devm_kzalloc(dev,
1110 size, GFP_KERNEL);
1111 if (qphy->emu_dcm_reset_seq) {
1112 qphy->emu_dcm_reset_seq_len =
1113 (size / sizeof(*qphy->emu_dcm_reset_seq));
1114 if (qphy->emu_dcm_reset_seq_len % 2) {
1115 dev_err(dev, "invalid emu_dcm_reset_seq_len\n");
1116 return -EINVAL;
1117 }
1118
1119 of_property_read_u32_array(dev->of_node,
1120 "qcom,emu-dcm-reset-seq",
1121 qphy->emu_dcm_reset_seq,
1122 qphy->emu_dcm_reset_seq_len);
1123 } else {
1124 dev_dbg(dev, "error allocating memory for emu_dcm_reset_seq\n");
1125 }
1126 }
1127
Mayank Rana147f41d2016-08-12 10:07:45 -07001128 size = 0;
Mayank Rana3cb43a32016-08-02 12:10:20 -07001129 of_get_property(dev->of_node, "qcom,qusb-phy-init-seq", &size);
1130 if (size) {
1131 qphy->qusb_phy_init_seq = devm_kzalloc(dev,
1132 size, GFP_KERNEL);
1133 if (qphy->qusb_phy_init_seq) {
1134 qphy->init_seq_len =
1135 (size / sizeof(*qphy->qusb_phy_init_seq));
1136 if (qphy->init_seq_len % 2) {
1137 dev_err(dev, "invalid init_seq_len\n");
1138 return -EINVAL;
1139 }
1140
1141 of_property_read_u32_array(dev->of_node,
1142 "qcom,qusb-phy-init-seq",
1143 qphy->qusb_phy_init_seq,
1144 qphy->init_seq_len);
1145 } else {
1146 dev_err(dev, "error allocating memory for phy_init_seq\n");
1147 }
1148 }
1149
1150 qphy->ulpi_mode = false;
1151 ret = of_property_read_string(dev->of_node, "phy_type", &phy_type);
1152
1153 if (!ret) {
1154 if (!strcasecmp(phy_type, "ulpi"))
1155 qphy->ulpi_mode = true;
1156 } else {
1157 dev_err(dev, "error reading phy_type property\n");
1158 return ret;
1159 }
1160
1161 hold_phy_reset = of_property_read_bool(dev->of_node, "qcom,hold-reset");
1162
1163 /* use default major revision as 2 */
1164 qphy->major_rev = 2;
1165 ret = of_property_read_u32(dev->of_node, "qcom,major-rev",
1166 &qphy->major_rev);
1167
1168 ret = of_property_read_u32_array(dev->of_node, "qcom,vdd-voltage-level",
1169 (u32 *) qphy->vdd_levels,
1170 ARRAY_SIZE(qphy->vdd_levels));
1171 if (ret) {
1172 dev_err(dev, "error reading qcom,vdd-voltage-level property\n");
1173 return ret;
1174 }
1175
1176 qphy->vdd = devm_regulator_get(dev, "vdd");
1177 if (IS_ERR(qphy->vdd)) {
1178 dev_err(dev, "unable to get vdd supply\n");
1179 return PTR_ERR(qphy->vdd);
1180 }
1181
1182 qphy->vdda33 = devm_regulator_get(dev, "vdda33");
1183 if (IS_ERR(qphy->vdda33)) {
1184 dev_err(dev, "unable to get vdda33 supply\n");
1185 return PTR_ERR(qphy->vdda33);
1186 }
1187
1188 qphy->vdda18 = devm_regulator_get(dev, "vdda18");
1189 if (IS_ERR(qphy->vdda18)) {
1190 dev_err(dev, "unable to get vdda18 supply\n");
1191 return PTR_ERR(qphy->vdda18);
1192 }
1193
Vijayavardhan Vennapusad5779642017-02-03 11:42:36 +05301194 mutex_init(&qphy->phy_lock);
Mayank Rana3cb43a32016-08-02 12:10:20 -07001195 platform_set_drvdata(pdev, qphy);
1196
1197 qphy->phy.label = "msm-qusb-phy";
1198 qphy->phy.init = qusb_phy_init;
1199 qphy->phy.set_suspend = qusb_phy_set_suspend;
1200 qphy->phy.shutdown = qusb_phy_shutdown;
1201 qphy->phy.type = USB_PHY_TYPE_USB2;
1202 qphy->phy.notify_connect = qusb_phy_notify_connect;
1203 qphy->phy.notify_disconnect = qusb_phy_notify_disconnect;
1204
1205 /*
1206 * On some platforms multiple QUSB PHYs are available. If QUSB PHY is
1207 * not used, there is leakage current seen with QUSB PHY related voltage
1208 * rail. Hence keep QUSB PHY into reset state explicitly here.
1209 */
Amit Nischal4d278212016-06-06 17:54:34 +05301210 if (hold_phy_reset) {
1211 ret = reset_control_assert(qphy->phy_reset);
1212 if (ret)
1213 dev_err(dev, "%s:phy_reset assert failed\n", __func__);
1214 }
Mayank Rana3cb43a32016-08-02 12:10:20 -07001215
1216 ret = usb_add_phy_dev(&qphy->phy);
1217 if (ret)
1218 return ret;
1219
1220 ret = qusb_phy_regulator_init(qphy);
1221 if (ret)
1222 usb_remove_phy(&qphy->phy);
1223
Vijayavardhan Vennapusae036a3d2016-12-13 12:44:18 +05301224 /* de-assert clamp dig n to reduce leakage on 1p8 upon boot up */
1225 if (qphy->tcsr_clamp_dig_n)
1226 writel_relaxed(0x0, qphy->tcsr_clamp_dig_n);
1227
Mayank Rana3cb43a32016-08-02 12:10:20 -07001228 return ret;
1229}
1230
1231static int qusb_phy_remove(struct platform_device *pdev)
1232{
1233 struct qusb_phy *qphy = platform_get_drvdata(pdev);
1234
1235 usb_remove_phy(&qphy->phy);
1236
1237 if (qphy->clocks_enabled) {
1238 clk_disable_unprepare(qphy->cfg_ahb_clk);
1239 clk_disable_unprepare(qphy->ref_clk);
1240 clk_disable_unprepare(qphy->ref_clk_src);
1241 qphy->clocks_enabled = false;
1242 }
1243
Vijayavardhan Vennapusa42baddf2016-02-19 13:13:32 +05301244 qusb_phy_enable_power(qphy, false);
Mayank Rana3cb43a32016-08-02 12:10:20 -07001245
1246 return 0;
1247}
1248
1249static const struct of_device_id qusb_phy_id_table[] = {
1250 { .compatible = "qcom,qusb2phy", },
1251 { },
1252};
1253MODULE_DEVICE_TABLE(of, qusb_phy_id_table);
1254
1255static struct platform_driver qusb_phy_driver = {
1256 .probe = qusb_phy_probe,
1257 .remove = qusb_phy_remove,
1258 .driver = {
1259 .name = "msm-qusb-phy",
1260 .of_match_table = of_match_ptr(qusb_phy_id_table),
1261 },
1262};
1263
1264module_platform_driver(qusb_phy_driver);
1265
1266MODULE_DESCRIPTION("MSM QUSB2 PHY driver");
1267MODULE_LICENSE("GPL v2");