blob: bbd3171580848100c2cd1bb1bab4753d85c363bd [file] [log] [blame]
Yaniv Gardiadaafaa2015-01-15 16:32:35 +02001/*
2 * Copyright (c) 2013-2015, Linux Foundation. All rights reserved.
3 *
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
15#include "phy-qcom-ufs-i.h"
16
17#define MAX_PROP_NAME 32
18#define VDDA_PHY_MIN_UV 1000000
19#define VDDA_PHY_MAX_UV 1000000
20#define VDDA_PLL_MIN_UV 1800000
21#define VDDA_PLL_MAX_UV 1800000
22#define VDDP_REF_CLK_MIN_UV 1200000
23#define VDDP_REF_CLK_MAX_UV 1200000
24
Yaniv Gardiadaafaa2015-01-15 16:32:35 +020025int ufs_qcom_phy_calibrate(struct ufs_qcom_phy *ufs_qcom_phy,
26 struct ufs_qcom_phy_calibration *tbl_A,
27 int tbl_size_A,
28 struct ufs_qcom_phy_calibration *tbl_B,
29 int tbl_size_B, bool is_rate_B)
30{
31 int i;
32 int ret = 0;
33
34 if (!tbl_A) {
35 dev_err(ufs_qcom_phy->dev, "%s: tbl_A is NULL", __func__);
36 ret = EINVAL;
37 goto out;
38 }
39
40 for (i = 0; i < tbl_size_A; i++)
41 writel_relaxed(tbl_A[i].cfg_value,
42 ufs_qcom_phy->mmio + tbl_A[i].reg_offset);
43
44 /*
45 * In case we would like to work in rate B, we need
46 * to override a registers that were configured in rate A table
47 * with registers of rate B table.
48 * table.
49 */
50 if (is_rate_B) {
51 if (!tbl_B) {
52 dev_err(ufs_qcom_phy->dev, "%s: tbl_B is NULL",
53 __func__);
54 ret = EINVAL;
55 goto out;
56 }
57
58 for (i = 0; i < tbl_size_B; i++)
59 writel_relaxed(tbl_B[i].cfg_value,
60 ufs_qcom_phy->mmio + tbl_B[i].reg_offset);
61 }
62
63 /* flush buffered writes */
64 mb();
65
66out:
67 return ret;
68}
Axel Lin358d6c82015-03-23 11:54:50 +080069EXPORT_SYMBOL_GPL(ufs_qcom_phy_calibrate);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +020070
Yaniv Gardiadaafaa2015-01-15 16:32:35 +020071/*
72 * This assumes the embedded phy structure inside generic_phy is of type
73 * struct ufs_qcom_phy. In order to function properly it's crucial
74 * to keep the embedded struct "struct ufs_qcom_phy common_cfg"
75 * as the first inside generic_phy.
76 */
77struct ufs_qcom_phy *get_ufs_qcom_phy(struct phy *generic_phy)
78{
79 return (struct ufs_qcom_phy *)phy_get_drvdata(generic_phy);
80}
Axel Lin358d6c82015-03-23 11:54:50 +080081EXPORT_SYMBOL_GPL(get_ufs_qcom_phy);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +020082
83static
84int ufs_qcom_phy_base_init(struct platform_device *pdev,
85 struct ufs_qcom_phy *phy_common)
86{
87 struct device *dev = &pdev->dev;
88 struct resource *res;
89 int err = 0;
90
91 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_mem");
Yaniv Gardiadaafaa2015-01-15 16:32:35 +020092 phy_common->mmio = devm_ioremap_resource(dev, res);
93 if (IS_ERR((void const *)phy_common->mmio)) {
94 err = PTR_ERR((void const *)phy_common->mmio);
95 phy_common->mmio = NULL;
96 dev_err(dev, "%s: ioremap for phy_mem resource failed %d\n",
97 __func__, err);
Axel Lin52ea7962015-03-23 12:08:18 +080098 return err;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +020099 }
100
101 /* "dev_ref_clk_ctrl_mem" is optional resource */
102 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
103 "dev_ref_clk_ctrl_mem");
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200104 phy_common->dev_ref_clk_ctrl_mmio = devm_ioremap_resource(dev, res);
Axel Lin52ea7962015-03-23 12:08:18 +0800105 if (IS_ERR((void const *)phy_common->dev_ref_clk_ctrl_mmio))
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200106 phy_common->dev_ref_clk_ctrl_mmio = NULL;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200107
Axel Lin52ea7962015-03-23 12:08:18 +0800108 return 0;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200109}
110
Vivek Gautam15887cb2016-11-08 15:37:46 +0530111struct phy *ufs_qcom_phy_generic_probe(struct platform_device *pdev,
112 struct ufs_qcom_phy *common_cfg,
113 const struct phy_ops *ufs_qcom_phy_gen_ops,
114 struct ufs_qcom_phy_specific_ops *phy_spec_ops)
115{
116 int err;
117 struct device *dev = &pdev->dev;
118 struct phy *generic_phy = NULL;
119 struct phy_provider *phy_provider;
120
121 err = ufs_qcom_phy_base_init(pdev, common_cfg);
122 if (err) {
123 dev_err(dev, "%s: phy base init failed %d\n", __func__, err);
124 goto out;
125 }
126
127 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
128 if (IS_ERR(phy_provider)) {
129 err = PTR_ERR(phy_provider);
130 dev_err(dev, "%s: failed to register phy %d\n", __func__, err);
131 goto out;
132 }
133
134 generic_phy = devm_phy_create(dev, NULL, ufs_qcom_phy_gen_ops);
135 if (IS_ERR(generic_phy)) {
136 err = PTR_ERR(generic_phy);
137 dev_err(dev, "%s: failed to create phy %d\n", __func__, err);
138 generic_phy = NULL;
139 goto out;
140 }
141
142 common_cfg->phy_spec_ops = phy_spec_ops;
143 common_cfg->dev = dev;
144
145out:
146 return generic_phy;
147}
148EXPORT_SYMBOL_GPL(ufs_qcom_phy_generic_probe);
149
Vivek Gautam89bd2962016-11-08 15:37:42 +0530150static int __ufs_qcom_phy_clk_get(struct device *dev,
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200151 const char *name, struct clk **clk_out, bool err_print)
152{
153 struct clk *clk;
154 int err = 0;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200155
156 clk = devm_clk_get(dev, name);
157 if (IS_ERR(clk)) {
158 err = PTR_ERR(clk);
159 if (err_print)
160 dev_err(dev, "failed to get %s err %d", name, err);
161 } else {
162 *clk_out = clk;
163 }
164
165 return err;
166}
167
Vivek Gautam89bd2962016-11-08 15:37:42 +0530168static int ufs_qcom_phy_clk_get(struct device *dev,
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200169 const char *name, struct clk **clk_out)
170{
Vivek Gautam89bd2962016-11-08 15:37:42 +0530171 return __ufs_qcom_phy_clk_get(dev, name, clk_out, true);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200172}
173
Vivek Gautam89bd2962016-11-08 15:37:42 +0530174int ufs_qcom_phy_init_clks(struct ufs_qcom_phy *phy_common)
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200175{
176 int err;
177
Vivek Gautam300f9672016-11-08 15:37:44 +0530178 if (of_device_is_compatible(phy_common->dev->of_node,
179 "qcom,msm8996-ufs-phy-qmp-14nm"))
180 goto skip_txrx_clk;
181
Vivek Gautam89bd2962016-11-08 15:37:42 +0530182 err = ufs_qcom_phy_clk_get(phy_common->dev, "tx_iface_clk",
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200183 &phy_common->tx_iface_clk);
184 if (err)
185 goto out;
186
Vivek Gautam89bd2962016-11-08 15:37:42 +0530187 err = ufs_qcom_phy_clk_get(phy_common->dev, "rx_iface_clk",
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200188 &phy_common->rx_iface_clk);
189 if (err)
190 goto out;
191
Vivek Gautam89bd2962016-11-08 15:37:42 +0530192 err = ufs_qcom_phy_clk_get(phy_common->dev, "ref_clk_src",
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200193 &phy_common->ref_clk_src);
194 if (err)
195 goto out;
196
Vivek Gautam300f9672016-11-08 15:37:44 +0530197skip_txrx_clk:
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200198 /*
199 * "ref_clk_parent" is optional hence don't abort init if it's not
200 * found.
201 */
Vivek Gautam89bd2962016-11-08 15:37:42 +0530202 __ufs_qcom_phy_clk_get(phy_common->dev, "ref_clk_parent",
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200203 &phy_common->ref_clk_parent, false);
204
Vivek Gautam89bd2962016-11-08 15:37:42 +0530205 err = ufs_qcom_phy_clk_get(phy_common->dev, "ref_clk",
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200206 &phy_common->ref_clk);
207
208out:
209 return err;
210}
Axel Lin358d6c82015-03-23 11:54:50 +0800211EXPORT_SYMBOL_GPL(ufs_qcom_phy_init_clks);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200212
Bjorn Andersson34714262017-01-22 13:17:47 -0800213static int ufs_qcom_phy_init_vreg(struct device *dev,
214 struct ufs_qcom_phy_vreg *vreg,
215 const char *name)
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200216{
217 int err = 0;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200218
219 char prop_name[MAX_PROP_NAME];
220
Bjorn Anderssone7d5e412017-01-22 13:17:46 -0800221 vreg->name = name;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200222 vreg->reg = devm_regulator_get(dev, name);
223 if (IS_ERR(vreg->reg)) {
224 err = PTR_ERR(vreg->reg);
Bjorn Andersson34714262017-01-22 13:17:47 -0800225 dev_err(dev, "failed to get %s, %d\n", name, err);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200226 goto out;
227 }
228
229 if (dev->of_node) {
230 snprintf(prop_name, MAX_PROP_NAME, "%s-max-microamp", name);
231 err = of_property_read_u32(dev->of_node,
232 prop_name, &vreg->max_uA);
233 if (err && err != -EINVAL) {
234 dev_err(dev, "%s: failed to read %s\n",
235 __func__, prop_name);
236 goto out;
237 } else if (err == -EINVAL || !vreg->max_uA) {
238 if (regulator_count_voltages(vreg->reg) > 0) {
239 dev_err(dev, "%s: %s is mandatory\n",
240 __func__, prop_name);
241 goto out;
242 }
243 err = 0;
244 }
245 snprintf(prop_name, MAX_PROP_NAME, "%s-always-on", name);
Julia Lawall3ea981e2016-08-05 13:25:13 +0200246 vreg->is_always_on = of_property_read_bool(dev->of_node,
247 prop_name);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200248 }
249
250 if (!strcmp(name, "vdda-pll")) {
251 vreg->max_uV = VDDA_PLL_MAX_UV;
252 vreg->min_uV = VDDA_PLL_MIN_UV;
253 } else if (!strcmp(name, "vdda-phy")) {
254 vreg->max_uV = VDDA_PHY_MAX_UV;
255 vreg->min_uV = VDDA_PHY_MIN_UV;
256 } else if (!strcmp(name, "vddp-ref-clk")) {
257 vreg->max_uV = VDDP_REF_CLK_MAX_UV;
258 vreg->min_uV = VDDP_REF_CLK_MIN_UV;
259 }
260
261out:
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200262 return err;
263}
264
Vivek Gautam15887cb2016-11-08 15:37:46 +0530265int ufs_qcom_phy_init_vregulators(struct ufs_qcom_phy *phy_common)
266{
267 int err;
268
269 err = ufs_qcom_phy_init_vreg(phy_common->dev, &phy_common->vdda_pll,
270 "vdda-pll");
271 if (err)
272 goto out;
273
274 err = ufs_qcom_phy_init_vreg(phy_common->dev, &phy_common->vdda_phy,
275 "vdda-phy");
276
277 if (err)
278 goto out;
279
Bjorn Andersson34714262017-01-22 13:17:47 -0800280 err = ufs_qcom_phy_init_vreg(phy_common->dev, &phy_common->vddp_ref_clk,
281 "vddp-ref-clk");
282
Vivek Gautam15887cb2016-11-08 15:37:46 +0530283out:
284 return err;
285}
286EXPORT_SYMBOL_GPL(ufs_qcom_phy_init_vregulators);
287
Vivek Gautam89bd2962016-11-08 15:37:42 +0530288static int ufs_qcom_phy_cfg_vreg(struct device *dev,
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200289 struct ufs_qcom_phy_vreg *vreg, bool on)
290{
291 int ret = 0;
292 struct regulator *reg = vreg->reg;
293 const char *name = vreg->name;
294 int min_uV;
295 int uA_load;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200296
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200297 if (regulator_count_voltages(reg) > 0) {
298 min_uV = on ? vreg->min_uV : 0;
299 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
300 if (ret) {
301 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
302 __func__, name, ret);
303 goto out;
304 }
305 uA_load = on ? vreg->max_uA : 0;
Stephen Rothwell7e476c72015-03-10 13:44:41 +1100306 ret = regulator_set_load(reg, uA_load);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200307 if (ret >= 0) {
308 /*
Stephen Rothwell7e476c72015-03-10 13:44:41 +1100309 * regulator_set_load() returns new regulator
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200310 * mode upon success.
311 */
312 ret = 0;
313 } else {
314 dev_err(dev, "%s: %s set optimum mode(uA_load=%d) failed, err=%d\n",
315 __func__, name, uA_load, ret);
316 goto out;
317 }
318 }
319out:
320 return ret;
321}
322
Vivek Gautam89bd2962016-11-08 15:37:42 +0530323static int ufs_qcom_phy_enable_vreg(struct device *dev,
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200324 struct ufs_qcom_phy_vreg *vreg)
325{
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200326 int ret = 0;
327
328 if (!vreg || vreg->enabled)
329 goto out;
330
Vivek Gautam89bd2962016-11-08 15:37:42 +0530331 ret = ufs_qcom_phy_cfg_vreg(dev, vreg, true);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200332 if (ret) {
333 dev_err(dev, "%s: ufs_qcom_phy_cfg_vreg() failed, err=%d\n",
334 __func__, ret);
335 goto out;
336 }
337
338 ret = regulator_enable(vreg->reg);
339 if (ret) {
340 dev_err(dev, "%s: enable failed, err=%d\n",
341 __func__, ret);
342 goto out;
343 }
344
345 vreg->enabled = true;
346out:
347 return ret;
348}
349
Vivek Gautamfeb3d792016-11-08 15:37:48 +0530350static int ufs_qcom_phy_enable_ref_clk(struct ufs_qcom_phy *phy)
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200351{
352 int ret = 0;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200353
354 if (phy->is_ref_clk_enabled)
355 goto out;
356
357 /*
358 * reference clock is propagated in a daisy-chained manner from
359 * source to phy, so ungate them at each stage.
360 */
361 ret = clk_prepare_enable(phy->ref_clk_src);
362 if (ret) {
363 dev_err(phy->dev, "%s: ref_clk_src enable failed %d\n",
364 __func__, ret);
365 goto out;
366 }
367
368 /*
369 * "ref_clk_parent" is optional clock hence make sure that clk reference
370 * is available before trying to enable the clock.
371 */
372 if (phy->ref_clk_parent) {
373 ret = clk_prepare_enable(phy->ref_clk_parent);
374 if (ret) {
375 dev_err(phy->dev, "%s: ref_clk_parent enable failed %d\n",
376 __func__, ret);
377 goto out_disable_src;
378 }
379 }
380
381 ret = clk_prepare_enable(phy->ref_clk);
382 if (ret) {
383 dev_err(phy->dev, "%s: ref_clk enable failed %d\n",
384 __func__, ret);
385 goto out_disable_parent;
386 }
387
388 phy->is_ref_clk_enabled = true;
389 goto out;
390
391out_disable_parent:
392 if (phy->ref_clk_parent)
393 clk_disable_unprepare(phy->ref_clk_parent);
394out_disable_src:
395 clk_disable_unprepare(phy->ref_clk_src);
396out:
397 return ret;
398}
399
Vivek Gautam89bd2962016-11-08 15:37:42 +0530400static int ufs_qcom_phy_disable_vreg(struct device *dev,
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200401 struct ufs_qcom_phy_vreg *vreg)
402{
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200403 int ret = 0;
404
405 if (!vreg || !vreg->enabled || vreg->is_always_on)
406 goto out;
407
408 ret = regulator_disable(vreg->reg);
409
410 if (!ret) {
411 /* ignore errors on applying disable config */
Vivek Gautam89bd2962016-11-08 15:37:42 +0530412 ufs_qcom_phy_cfg_vreg(dev, vreg, false);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200413 vreg->enabled = false;
414 } else {
415 dev_err(dev, "%s: %s disable failed, err=%d\n",
416 __func__, vreg->name, ret);
417 }
418out:
419 return ret;
420}
421
Vivek Gautamfeb3d792016-11-08 15:37:48 +0530422static void ufs_qcom_phy_disable_ref_clk(struct ufs_qcom_phy *phy)
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200423{
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200424 if (phy->is_ref_clk_enabled) {
425 clk_disable_unprepare(phy->ref_clk);
426 /*
427 * "ref_clk_parent" is optional clock hence make sure that clk
428 * reference is available before trying to disable the clock.
429 */
430 if (phy->ref_clk_parent)
431 clk_disable_unprepare(phy->ref_clk_parent);
432 clk_disable_unprepare(phy->ref_clk_src);
433 phy->is_ref_clk_enabled = false;
434 }
435}
436
437#define UFS_REF_CLK_EN (1 << 5)
438
439static void ufs_qcom_phy_dev_ref_clk_ctrl(struct phy *generic_phy, bool enable)
440{
441 struct ufs_qcom_phy *phy = get_ufs_qcom_phy(generic_phy);
442
443 if (phy->dev_ref_clk_ctrl_mmio &&
444 (enable ^ phy->is_dev_ref_clk_enabled)) {
445 u32 temp = readl_relaxed(phy->dev_ref_clk_ctrl_mmio);
446
447 if (enable)
448 temp |= UFS_REF_CLK_EN;
449 else
450 temp &= ~UFS_REF_CLK_EN;
451
452 /*
453 * If we are here to disable this clock immediately after
454 * entering into hibern8, we need to make sure that device
455 * ref_clk is active atleast 1us after the hibern8 enter.
456 */
457 if (!enable)
458 udelay(1);
459
460 writel_relaxed(temp, phy->dev_ref_clk_ctrl_mmio);
461 /* ensure that ref_clk is enabled/disabled before we return */
462 wmb();
463 /*
464 * If we call hibern8 exit after this, we need to make sure that
465 * device ref_clk is stable for atleast 1us before the hibern8
466 * exit command.
467 */
468 if (enable)
469 udelay(1);
470
471 phy->is_dev_ref_clk_enabled = enable;
472 }
473}
474
475void ufs_qcom_phy_enable_dev_ref_clk(struct phy *generic_phy)
476{
477 ufs_qcom_phy_dev_ref_clk_ctrl(generic_phy, true);
478}
Yaniv Gardi65d49b32015-09-02 11:32:17 +0300479EXPORT_SYMBOL_GPL(ufs_qcom_phy_enable_dev_ref_clk);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200480
481void ufs_qcom_phy_disable_dev_ref_clk(struct phy *generic_phy)
482{
483 ufs_qcom_phy_dev_ref_clk_ctrl(generic_phy, false);
484}
Yaniv Gardi65d49b32015-09-02 11:32:17 +0300485EXPORT_SYMBOL_GPL(ufs_qcom_phy_disable_dev_ref_clk);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200486
487/* Turn ON M-PHY RMMI interface clocks */
Vivek Gautamfeb3d792016-11-08 15:37:48 +0530488static int ufs_qcom_phy_enable_iface_clk(struct ufs_qcom_phy *phy)
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200489{
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200490 int ret = 0;
491
492 if (phy->is_iface_clk_enabled)
493 goto out;
494
495 ret = clk_prepare_enable(phy->tx_iface_clk);
496 if (ret) {
497 dev_err(phy->dev, "%s: tx_iface_clk enable failed %d\n",
498 __func__, ret);
499 goto out;
500 }
501 ret = clk_prepare_enable(phy->rx_iface_clk);
502 if (ret) {
503 clk_disable_unprepare(phy->tx_iface_clk);
504 dev_err(phy->dev, "%s: rx_iface_clk enable failed %d. disabling also tx_iface_clk\n",
505 __func__, ret);
506 goto out;
507 }
508 phy->is_iface_clk_enabled = true;
509
510out:
511 return ret;
512}
513
514/* Turn OFF M-PHY RMMI interface clocks */
Vivek Gautamfeb3d792016-11-08 15:37:48 +0530515void ufs_qcom_phy_disable_iface_clk(struct ufs_qcom_phy *phy)
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200516{
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200517 if (phy->is_iface_clk_enabled) {
518 clk_disable_unprepare(phy->tx_iface_clk);
519 clk_disable_unprepare(phy->rx_iface_clk);
520 phy->is_iface_clk_enabled = false;
521 }
522}
523
524int ufs_qcom_phy_start_serdes(struct phy *generic_phy)
525{
526 struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
527 int ret = 0;
528
529 if (!ufs_qcom_phy->phy_spec_ops->start_serdes) {
530 dev_err(ufs_qcom_phy->dev, "%s: start_serdes() callback is not supported\n",
531 __func__);
532 ret = -ENOTSUPP;
533 } else {
534 ufs_qcom_phy->phy_spec_ops->start_serdes(ufs_qcom_phy);
535 }
536
537 return ret;
538}
Yaniv Gardi65d49b32015-09-02 11:32:17 +0300539EXPORT_SYMBOL_GPL(ufs_qcom_phy_start_serdes);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200540
541int ufs_qcom_phy_set_tx_lane_enable(struct phy *generic_phy, u32 tx_lanes)
542{
543 struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
544 int ret = 0;
545
546 if (!ufs_qcom_phy->phy_spec_ops->set_tx_lane_enable) {
547 dev_err(ufs_qcom_phy->dev, "%s: set_tx_lane_enable() callback is not supported\n",
548 __func__);
549 ret = -ENOTSUPP;
550 } else {
551 ufs_qcom_phy->phy_spec_ops->set_tx_lane_enable(ufs_qcom_phy,
552 tx_lanes);
553 }
554
555 return ret;
556}
Yaniv Gardi65d49b32015-09-02 11:32:17 +0300557EXPORT_SYMBOL_GPL(ufs_qcom_phy_set_tx_lane_enable);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200558
559void ufs_qcom_phy_save_controller_version(struct phy *generic_phy,
560 u8 major, u16 minor, u16 step)
561{
562 struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
563
564 ufs_qcom_phy->host_ctrl_rev_major = major;
565 ufs_qcom_phy->host_ctrl_rev_minor = minor;
566 ufs_qcom_phy->host_ctrl_rev_step = step;
567}
Yaniv Gardi65d49b32015-09-02 11:32:17 +0300568EXPORT_SYMBOL_GPL(ufs_qcom_phy_save_controller_version);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200569
570int ufs_qcom_phy_calibrate_phy(struct phy *generic_phy, bool is_rate_B)
571{
572 struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
573 int ret = 0;
574
575 if (!ufs_qcom_phy->phy_spec_ops->calibrate_phy) {
576 dev_err(ufs_qcom_phy->dev, "%s: calibrate_phy() callback is not supported\n",
577 __func__);
578 ret = -ENOTSUPP;
579 } else {
580 ret = ufs_qcom_phy->phy_spec_ops->
581 calibrate_phy(ufs_qcom_phy, is_rate_B);
582 if (ret)
583 dev_err(ufs_qcom_phy->dev, "%s: calibrate_phy() failed %d\n",
584 __func__, ret);
585 }
586
587 return ret;
588}
Yaniv Gardi65d49b32015-09-02 11:32:17 +0300589EXPORT_SYMBOL_GPL(ufs_qcom_phy_calibrate_phy);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200590
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200591int ufs_qcom_phy_is_pcs_ready(struct phy *generic_phy)
592{
593 struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
594
595 if (!ufs_qcom_phy->phy_spec_ops->is_physical_coding_sublayer_ready) {
596 dev_err(ufs_qcom_phy->dev, "%s: is_physical_coding_sublayer_ready() callback is not supported\n",
597 __func__);
598 return -ENOTSUPP;
599 }
600
601 return ufs_qcom_phy->phy_spec_ops->
602 is_physical_coding_sublayer_ready(ufs_qcom_phy);
603}
Yaniv Gardi65d49b32015-09-02 11:32:17 +0300604EXPORT_SYMBOL_GPL(ufs_qcom_phy_is_pcs_ready);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200605
606int ufs_qcom_phy_power_on(struct phy *generic_phy)
607{
608 struct ufs_qcom_phy *phy_common = get_ufs_qcom_phy(generic_phy);
609 struct device *dev = phy_common->dev;
610 int err;
611
Vivek Gautam3d4640f2016-11-08 15:37:49 +0530612 if (phy_common->is_powered_on)
613 return 0;
614
Vivek Gautam89bd2962016-11-08 15:37:42 +0530615 err = ufs_qcom_phy_enable_vreg(dev, &phy_common->vdda_phy);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200616 if (err) {
617 dev_err(dev, "%s enable vdda_phy failed, err=%d\n",
618 __func__, err);
619 goto out;
620 }
621
622 phy_common->phy_spec_ops->power_control(phy_common, true);
623
624 /* vdda_pll also enables ref clock LDOs so enable it first */
Vivek Gautam89bd2962016-11-08 15:37:42 +0530625 err = ufs_qcom_phy_enable_vreg(dev, &phy_common->vdda_pll);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200626 if (err) {
627 dev_err(dev, "%s enable vdda_pll failed, err=%d\n",
628 __func__, err);
629 goto out_disable_phy;
630 }
631
Vivek Gautamfeb3d792016-11-08 15:37:48 +0530632 err = ufs_qcom_phy_enable_iface_clk(phy_common);
633 if (err) {
634 dev_err(dev, "%s enable phy iface clock failed, err=%d\n",
635 __func__, err);
636 goto out_disable_pll;
637 }
638
639 err = ufs_qcom_phy_enable_ref_clk(phy_common);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200640 if (err) {
641 dev_err(dev, "%s enable phy ref clock failed, err=%d\n",
642 __func__, err);
Vivek Gautamfeb3d792016-11-08 15:37:48 +0530643 goto out_disable_iface_clk;
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200644 }
645
646 /* enable device PHY ref_clk pad rail */
647 if (phy_common->vddp_ref_clk.reg) {
Vivek Gautam89bd2962016-11-08 15:37:42 +0530648 err = ufs_qcom_phy_enable_vreg(dev,
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200649 &phy_common->vddp_ref_clk);
650 if (err) {
651 dev_err(dev, "%s enable vddp_ref_clk failed, err=%d\n",
652 __func__, err);
653 goto out_disable_ref_clk;
654 }
655 }
656
657 phy_common->is_powered_on = true;
658 goto out;
659
660out_disable_ref_clk:
Vivek Gautamfeb3d792016-11-08 15:37:48 +0530661 ufs_qcom_phy_disable_ref_clk(phy_common);
662out_disable_iface_clk:
663 ufs_qcom_phy_disable_iface_clk(phy_common);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200664out_disable_pll:
Vivek Gautam89bd2962016-11-08 15:37:42 +0530665 ufs_qcom_phy_disable_vreg(dev, &phy_common->vdda_pll);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200666out_disable_phy:
Vivek Gautam89bd2962016-11-08 15:37:42 +0530667 ufs_qcom_phy_disable_vreg(dev, &phy_common->vdda_phy);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200668out:
669 return err;
670}
Axel Lin358d6c82015-03-23 11:54:50 +0800671EXPORT_SYMBOL_GPL(ufs_qcom_phy_power_on);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200672
673int ufs_qcom_phy_power_off(struct phy *generic_phy)
674{
675 struct ufs_qcom_phy *phy_common = get_ufs_qcom_phy(generic_phy);
676
Vivek Gautam3d4640f2016-11-08 15:37:49 +0530677 if (!phy_common->is_powered_on)
678 return 0;
679
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200680 phy_common->phy_spec_ops->power_control(phy_common, false);
681
682 if (phy_common->vddp_ref_clk.reg)
Vivek Gautam89bd2962016-11-08 15:37:42 +0530683 ufs_qcom_phy_disable_vreg(phy_common->dev,
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200684 &phy_common->vddp_ref_clk);
Vivek Gautamfeb3d792016-11-08 15:37:48 +0530685 ufs_qcom_phy_disable_ref_clk(phy_common);
686 ufs_qcom_phy_disable_iface_clk(phy_common);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200687
Vivek Gautam89bd2962016-11-08 15:37:42 +0530688 ufs_qcom_phy_disable_vreg(phy_common->dev, &phy_common->vdda_pll);
689 ufs_qcom_phy_disable_vreg(phy_common->dev, &phy_common->vdda_phy);
Yaniv Gardiadaafaa2015-01-15 16:32:35 +0200690 phy_common->is_powered_on = false;
691
692 return 0;
693}
Axel Lin358d6c82015-03-23 11:54:50 +0800694EXPORT_SYMBOL_GPL(ufs_qcom_phy_power_off);