1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
6 #include <linux/clk-provider.h>
7 #include <linux/platform_device.h>
8 #include <dt-bindings/phy/phy.h>
12 #define S_DIV_ROUND_UP(n, d) \
13 (((n) >= 0) ? (((n) + (d) - 1) / (d)) : (((n) - (d) + 1) / (d)))
15 static inline s32 linear_inter(s32 tmax, s32 tmin, s32 percent,
16 s32 min_result, bool even)
20 v = (tmax - tmin) * percent;
21 v = S_DIV_ROUND_UP(v, 100) + tmin;
22 if (even && (v & 0x1))
23 return max_t(s32, min_result, v - 1);
25 return max_t(s32, min_result, v);
28 static void dsi_dphy_timing_calc_clk_zero(struct msm_dsi_dphy_timing *timing,
29 s32 ui, s32 coeff, s32 pcnt)
31 s32 tmax, tmin, clk_z;
35 temp = 300 * coeff - ((timing->clk_prepare >> 1) + 1) * 2 * ui;
36 tmin = S_DIV_ROUND_UP(temp, ui) - 2;
39 clk_z = linear_inter(2 * tmin, tmin, pcnt, 0, true);
42 clk_z = linear_inter(tmax, tmin, pcnt, 0, true);
46 temp = (timing->hs_rqst + timing->clk_prepare + clk_z) & 0x7;
47 timing->clk_zero = clk_z + 8 - temp;
50 int msm_dsi_dphy_timing_calc(struct msm_dsi_dphy_timing *timing,
51 struct msm_dsi_phy_clk_request *clk_req)
53 const unsigned long bit_rate = clk_req->bitclk_rate;
54 const unsigned long esc_rate = clk_req->escclk_rate;
58 s32 pcnt1 = (bit_rate > 1200000000) ? 15 : 10;
60 s32 pcnt3 = (bit_rate > 180000000) ? 10 : 40;
61 s32 coeff = 1000; /* Precision, should avoid overflow */
64 if (!bit_rate || !esc_rate)
67 ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
68 lpx = mult_frac(NSEC_PER_MSEC, coeff, esc_rate / 1000);
70 tmax = S_DIV_ROUND_UP(95 * coeff, ui) - 2;
71 tmin = S_DIV_ROUND_UP(38 * coeff, ui) - 2;
72 timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, true);
76 timing->hs_rqst = temp;
78 timing->hs_rqst = max_t(s32, 0, temp - 2);
80 /* Calculate clk_zero after clk_prepare and hs_rqst */
81 dsi_dphy_timing_calc_clk_zero(timing, ui, coeff, pcnt2);
83 temp = 105 * coeff + 12 * ui - 20 * coeff;
84 tmax = S_DIV_ROUND_UP(temp, ui) - 2;
85 tmin = S_DIV_ROUND_UP(60 * coeff, ui) - 2;
86 timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
88 temp = 85 * coeff + 6 * ui;
89 tmax = S_DIV_ROUND_UP(temp, ui) - 2;
90 temp = 40 * coeff + 4 * ui;
91 tmin = S_DIV_ROUND_UP(temp, ui) - 2;
92 timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, true);
95 temp = ((timing->hs_prepare >> 1) + 1) * 2 * ui + 2 * ui;
96 temp = 145 * coeff + 10 * ui - temp;
97 tmin = S_DIV_ROUND_UP(temp, ui) - 2;
98 timing->hs_zero = linear_inter(tmax, tmin, pcnt2, 24, true);
100 temp = 105 * coeff + 12 * ui - 20 * coeff;
101 tmax = S_DIV_ROUND_UP(temp, ui) - 2;
102 temp = 60 * coeff + 4 * ui;
103 tmin = DIV_ROUND_UP(temp, ui) - 2;
104 timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
107 tmin = S_DIV_ROUND_UP(100 * coeff, ui) - 2;
108 timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, true);
111 temp = ((timing->hs_exit >> 1) + 1) * 2 * ui;
112 temp = 60 * coeff + 52 * ui - 24 * ui - temp;
113 tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
114 timing->shared_timings.clk_post = linear_inter(tmax, tmin, pcnt2, 0,
117 temp = ((timing->clk_prepare >> 1) + 1) * 2 * ui;
118 temp += ((timing->clk_zero >> 1) + 1) * 2 * ui;
119 temp += 8 * ui + lpx;
120 tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
122 temp = linear_inter(2 * tmax, tmin, pcnt2, 0, false);
123 timing->shared_timings.clk_pre = temp >> 1;
124 timing->shared_timings.clk_pre_inc_by_2 = true;
126 timing->shared_timings.clk_pre =
127 linear_inter(tmax, tmin, pcnt2, 0, false);
128 timing->shared_timings.clk_pre_inc_by_2 = false;
135 DBG("PHY timings: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
136 timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
137 timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
138 timing->clk_trail, timing->clk_prepare, timing->hs_exit,
139 timing->hs_zero, timing->hs_prepare, timing->hs_trail,
145 int msm_dsi_dphy_timing_calc_v2(struct msm_dsi_dphy_timing *timing,
146 struct msm_dsi_phy_clk_request *clk_req)
148 const unsigned long bit_rate = clk_req->bitclk_rate;
149 const unsigned long esc_rate = clk_req->escclk_rate;
158 s32 coeff = 1000; /* Precision, should avoid overflow */
159 s32 hb_en, hb_en_ckln, pd_ckln, pd;
163 if (!bit_rate || !esc_rate)
166 timing->hs_halfbyte_en = 0;
168 timing->hs_halfbyte_en_ckln = 0;
170 timing->hs_prep_dly_ckln = (bit_rate > 100000000) ? 0 : 3;
171 pd_ckln = timing->hs_prep_dly_ckln;
172 timing->hs_prep_dly = (bit_rate > 120000000) ? 0 : 1;
173 pd = timing->hs_prep_dly;
175 val = (hb_en << 2) + (pd << 1);
176 val_ckln = (hb_en_ckln << 2) + (pd_ckln << 1);
178 ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
181 temp = S_DIV_ROUND_UP(38 * coeff - val_ckln * ui, ui_x8);
182 tmin = max_t(s32, temp, 0);
183 temp = (95 * coeff - val_ckln * ui) / ui_x8;
184 tmax = max_t(s32, temp, 0);
185 timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, false);
187 temp = 300 * coeff - ((timing->clk_prepare << 3) + val_ckln) * ui;
188 tmin = S_DIV_ROUND_UP(temp - 11 * ui, ui_x8) - 3;
189 tmax = (tmin > 255) ? 511 : 255;
190 timing->clk_zero = linear_inter(tmax, tmin, pcnt5, 0, false);
192 tmin = DIV_ROUND_UP(60 * coeff + 3 * ui, ui_x8);
193 temp = 105 * coeff + 12 * ui - 20 * coeff;
194 tmax = (temp + 3 * ui) / ui_x8;
195 timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
197 temp = S_DIV_ROUND_UP(40 * coeff + 4 * ui - val * ui, ui_x8);
198 tmin = max_t(s32, temp, 0);
199 temp = (85 * coeff + 6 * ui - val * ui) / ui_x8;
200 tmax = max_t(s32, temp, 0);
201 timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, false);
203 temp = 145 * coeff + 10 * ui - ((timing->hs_prepare << 3) + val) * ui;
204 tmin = S_DIV_ROUND_UP(temp - 11 * ui, ui_x8) - 3;
206 timing->hs_zero = linear_inter(tmax, tmin, pcnt4, 0, false);
208 tmin = DIV_ROUND_UP(60 * coeff + 4 * ui + 3 * ui, ui_x8);
209 temp = 105 * coeff + 12 * ui - 20 * coeff;
210 tmax = (temp + 3 * ui) / ui_x8;
211 timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
213 temp = 50 * coeff + ((hb_en << 2) - 8) * ui;
214 timing->hs_rqst = S_DIV_ROUND_UP(temp, ui_x8);
216 tmin = DIV_ROUND_UP(100 * coeff, ui_x8) - 1;
218 timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, false);
220 temp = 50 * coeff + ((hb_en_ckln << 2) - 8) * ui;
221 timing->hs_rqst_ckln = S_DIV_ROUND_UP(temp, ui_x8);
223 temp = 60 * coeff + 52 * ui - 43 * ui;
224 tmin = DIV_ROUND_UP(temp, ui_x8) - 1;
226 timing->shared_timings.clk_post =
227 linear_inter(tmax, tmin, pcnt2, 0, false);
229 temp = 8 * ui + ((timing->clk_prepare << 3) + val_ckln) * ui;
230 temp += (((timing->clk_zero + 3) << 3) + 11 - (pd_ckln << 1)) * ui;
231 temp += hb_en_ckln ? (((timing->hs_rqst_ckln << 3) + 4) * ui) :
232 (((timing->hs_rqst_ckln << 3) + 8) * ui);
233 tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
236 temp = linear_inter(tmax << 1, tmin, pcnt2, 0, false);
237 timing->shared_timings.clk_pre = temp >> 1;
238 timing->shared_timings.clk_pre_inc_by_2 = 1;
240 timing->shared_timings.clk_pre =
241 linear_inter(tmax, tmin, pcnt2, 0, false);
242 timing->shared_timings.clk_pre_inc_by_2 = 0;
249 DBG("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
250 timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
251 timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
252 timing->clk_trail, timing->clk_prepare, timing->hs_exit,
253 timing->hs_zero, timing->hs_prepare, timing->hs_trail,
254 timing->hs_rqst, timing->hs_rqst_ckln, timing->hs_halfbyte_en,
255 timing->hs_halfbyte_en_ckln, timing->hs_prep_dly,
256 timing->hs_prep_dly_ckln);
261 int msm_dsi_dphy_timing_calc_v3(struct msm_dsi_dphy_timing *timing,
262 struct msm_dsi_phy_clk_request *clk_req)
264 const unsigned long bit_rate = clk_req->bitclk_rate;
265 const unsigned long esc_rate = clk_req->escclk_rate;
274 s32 coeff = 1000; /* Precision, should avoid overflow */
275 s32 hb_en, hb_en_ckln;
278 if (!bit_rate || !esc_rate)
281 timing->hs_halfbyte_en = 0;
283 timing->hs_halfbyte_en_ckln = 0;
286 ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
289 temp = S_DIV_ROUND_UP(38 * coeff, ui_x8);
290 tmin = max_t(s32, temp, 0);
291 temp = (95 * coeff) / ui_x8;
292 tmax = max_t(s32, temp, 0);
293 timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, false);
295 temp = 300 * coeff - (timing->clk_prepare << 3) * ui;
296 tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
297 tmax = (tmin > 255) ? 511 : 255;
298 timing->clk_zero = linear_inter(tmax, tmin, pcnt5, 0, false);
300 tmin = DIV_ROUND_UP(60 * coeff + 3 * ui, ui_x8);
301 temp = 105 * coeff + 12 * ui - 20 * coeff;
302 tmax = (temp + 3 * ui) / ui_x8;
303 timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
305 temp = S_DIV_ROUND_UP(40 * coeff + 4 * ui, ui_x8);
306 tmin = max_t(s32, temp, 0);
307 temp = (85 * coeff + 6 * ui) / ui_x8;
308 tmax = max_t(s32, temp, 0);
309 timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, false);
311 temp = 145 * coeff + 10 * ui - (timing->hs_prepare << 3) * ui;
312 tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
314 timing->hs_zero = linear_inter(tmax, tmin, pcnt4, 0, false);
316 tmin = DIV_ROUND_UP(60 * coeff + 4 * ui, ui_x8) - 1;
317 temp = 105 * coeff + 12 * ui - 20 * coeff;
318 tmax = (temp / ui_x8) - 1;
319 timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
321 temp = 50 * coeff + ((hb_en << 2) - 8) * ui;
322 timing->hs_rqst = S_DIV_ROUND_UP(temp, ui_x8);
324 tmin = DIV_ROUND_UP(100 * coeff, ui_x8) - 1;
326 timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, false);
328 temp = 50 * coeff + ((hb_en_ckln << 2) - 8) * ui;
329 timing->hs_rqst_ckln = S_DIV_ROUND_UP(temp, ui_x8);
331 temp = 60 * coeff + 52 * ui - 43 * ui;
332 tmin = DIV_ROUND_UP(temp, ui_x8) - 1;
334 timing->shared_timings.clk_post =
335 linear_inter(tmax, tmin, pcnt2, 0, false);
337 temp = 8 * ui + (timing->clk_prepare << 3) * ui;
338 temp += (((timing->clk_zero + 3) << 3) + 11) * ui;
339 temp += hb_en_ckln ? (((timing->hs_rqst_ckln << 3) + 4) * ui) :
340 (((timing->hs_rqst_ckln << 3) + 8) * ui);
341 tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
344 temp = linear_inter(tmax << 1, tmin, pcnt2, 0, false);
345 timing->shared_timings.clk_pre = temp >> 1;
346 timing->shared_timings.clk_pre_inc_by_2 = 1;
348 timing->shared_timings.clk_pre =
349 linear_inter(tmax, tmin, pcnt2, 0, false);
350 timing->shared_timings.clk_pre_inc_by_2 = 0;
353 timing->shared_timings.byte_intf_clk_div_2 = true;
359 DBG("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
360 timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
361 timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
362 timing->clk_trail, timing->clk_prepare, timing->hs_exit,
363 timing->hs_zero, timing->hs_prepare, timing->hs_trail,
364 timing->hs_rqst, timing->hs_rqst_ckln, timing->hs_halfbyte_en,
365 timing->hs_halfbyte_en_ckln, timing->hs_prep_dly,
366 timing->hs_prep_dly_ckln);
371 int msm_dsi_dphy_timing_calc_v4(struct msm_dsi_dphy_timing *timing,
372 struct msm_dsi_phy_clk_request *clk_req)
374 const unsigned long bit_rate = clk_req->bitclk_rate;
375 const unsigned long esc_rate = clk_req->escclk_rate;
378 s32 pcnt_clk_prep = 50;
379 s32 pcnt_clk_zero = 2;
380 s32 pcnt_clk_trail = 30;
381 s32 pcnt_hs_prep = 50;
382 s32 pcnt_hs_zero = 10;
383 s32 pcnt_hs_trail = 30;
384 s32 pcnt_hs_exit = 10;
385 s32 coeff = 1000; /* Precision, should avoid overflow */
389 if (!bit_rate || !esc_rate)
394 ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
397 /* TODO: verify these calculations against latest downstream driver
398 * everything except clk_post/clk_pre uses calculations from v3 based
399 * on the downstream driver having the same calculations for v3 and v4
402 temp = S_DIV_ROUND_UP(38 * coeff, ui_x8);
403 tmin = max_t(s32, temp, 0);
404 temp = (95 * coeff) / ui_x8;
405 tmax = max_t(s32, temp, 0);
406 timing->clk_prepare = linear_inter(tmax, tmin, pcnt_clk_prep, 0, false);
408 temp = 300 * coeff - (timing->clk_prepare << 3) * ui;
409 tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
410 tmax = (tmin > 255) ? 511 : 255;
411 timing->clk_zero = linear_inter(tmax, tmin, pcnt_clk_zero, 0, false);
413 tmin = DIV_ROUND_UP(60 * coeff + 3 * ui, ui_x8);
414 temp = 105 * coeff + 12 * ui - 20 * coeff;
415 tmax = (temp + 3 * ui) / ui_x8;
416 timing->clk_trail = linear_inter(tmax, tmin, pcnt_clk_trail, 0, false);
418 temp = S_DIV_ROUND_UP(40 * coeff + 4 * ui, ui_x8);
419 tmin = max_t(s32, temp, 0);
420 temp = (85 * coeff + 6 * ui) / ui_x8;
421 tmax = max_t(s32, temp, 0);
422 timing->hs_prepare = linear_inter(tmax, tmin, pcnt_hs_prep, 0, false);
424 temp = 145 * coeff + 10 * ui - (timing->hs_prepare << 3) * ui;
425 tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
427 timing->hs_zero = linear_inter(tmax, tmin, pcnt_hs_zero, 0, false);
429 tmin = DIV_ROUND_UP(60 * coeff + 4 * ui, ui_x8) - 1;
430 temp = 105 * coeff + 12 * ui - 20 * coeff;
431 tmax = (temp / ui_x8) - 1;
432 timing->hs_trail = linear_inter(tmax, tmin, pcnt_hs_trail, 0, false);
434 temp = 50 * coeff + ((hb_en << 2) - 8) * ui;
435 timing->hs_rqst = S_DIV_ROUND_UP(temp, ui_x8);
437 tmin = DIV_ROUND_UP(100 * coeff, ui_x8) - 1;
439 timing->hs_exit = linear_inter(tmax, tmin, pcnt_hs_exit, 0, false);
442 * = roundup((mipi_min_ns + t_hs_trail_ns)/(16*bit_clk_ns), 0) - 1
444 temp = 60 * coeff + 52 * ui + + (timing->hs_trail + 1) * ui_x8;
445 tmin = DIV_ROUND_UP(temp, 16 * ui) - 1;
447 timing->shared_timings.clk_post = linear_inter(tmax, tmin, 5, 0, false);
450 * val1 = (tlpx_ns + clk_prepare_ns + clk_zero_ns + hs_rqst_ns)
451 * val2 = (16 * bit_clk_ns)
452 * final = roundup(val1/val2, 0) - 1
454 temp = 52 * coeff + (timing->clk_prepare + timing->clk_zero + 1) * ui_x8 + 54 * coeff;
455 tmin = DIV_ROUND_UP(temp, 16 * ui) - 1;
457 timing->shared_timings.clk_pre = DIV_ROUND_UP((tmax - tmin) * 125, 10000) + tmin;
459 timing->shared_timings.byte_intf_clk_div_2 = true;
461 DBG("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
462 timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
463 timing->clk_zero, timing->clk_trail, timing->clk_prepare, timing->hs_exit,
464 timing->hs_zero, timing->hs_prepare, timing->hs_trail, timing->hs_rqst);
469 int msm_dsi_cphy_timing_calc_v4(struct msm_dsi_dphy_timing *timing,
470 struct msm_dsi_phy_clk_request *clk_req)
472 const unsigned long bit_rate = clk_req->bitclk_rate;
473 const unsigned long esc_rate = clk_req->escclk_rate;
476 s32 coeff = 1000; /* Precision, should avoid overflow */
479 if (!bit_rate || !esc_rate)
482 ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
485 temp = S_DIV_ROUND_UP(38 * coeff, ui_x7);
486 tmin = max_t(s32, temp, 0);
487 temp = (95 * coeff) / ui_x7;
488 tmax = max_t(s32, temp, 0);
489 timing->clk_prepare = linear_inter(tmax, tmin, 50, 0, false);
491 tmin = DIV_ROUND_UP(50 * coeff, ui_x7);
493 timing->hs_rqst = linear_inter(tmax, tmin, 1, 0, false);
495 tmin = DIV_ROUND_UP(100 * coeff, ui_x7) - 1;
497 timing->hs_exit = linear_inter(tmax, tmin, 10, 0, false);
501 timing->shared_timings.clk_post = linear_inter(tmax, tmin, 80, 0, false);
503 tmin = min_t(s32, 64, S_DIV_ROUND_UP(262 * coeff, ui_x7) - 1);
505 timing->shared_timings.clk_pre = linear_inter(tmax, tmin, 20, 0, false);
507 DBG("%d, %d, %d, %d, %d",
508 timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
509 timing->clk_prepare, timing->hs_exit, timing->hs_rqst);
514 static int dsi_phy_enable_resource(struct msm_dsi_phy *phy)
516 struct device *dev = &phy->pdev->dev;
519 pm_runtime_get_sync(dev);
521 ret = clk_prepare_enable(phy->ahb_clk);
523 DRM_DEV_ERROR(dev, "%s: can't enable ahb clk, %d\n", __func__, ret);
524 pm_runtime_put_sync(dev);
530 static void dsi_phy_disable_resource(struct msm_dsi_phy *phy)
532 clk_disable_unprepare(phy->ahb_clk);
533 pm_runtime_put(&phy->pdev->dev);
536 static const struct of_device_id dsi_phy_dt_match[] = {
537 #ifdef CONFIG_DRM_MSM_DSI_28NM_PHY
538 { .compatible = "qcom,dsi-phy-28nm-hpm",
539 .data = &dsi_phy_28nm_hpm_cfgs },
540 { .compatible = "qcom,dsi-phy-28nm-hpm-fam-b",
541 .data = &dsi_phy_28nm_hpm_famb_cfgs },
542 { .compatible = "qcom,dsi-phy-28nm-lp",
543 .data = &dsi_phy_28nm_lp_cfgs },
544 { .compatible = "qcom,dsi-phy-28nm-8226",
545 .data = &dsi_phy_28nm_8226_cfgs },
547 #ifdef CONFIG_DRM_MSM_DSI_20NM_PHY
548 { .compatible = "qcom,dsi-phy-20nm",
549 .data = &dsi_phy_20nm_cfgs },
551 #ifdef CONFIG_DRM_MSM_DSI_28NM_8960_PHY
552 { .compatible = "qcom,dsi-phy-28nm-8960",
553 .data = &dsi_phy_28nm_8960_cfgs },
555 #ifdef CONFIG_DRM_MSM_DSI_14NM_PHY
556 { .compatible = "qcom,dsi-phy-14nm",
557 .data = &dsi_phy_14nm_cfgs },
558 { .compatible = "qcom,dsi-phy-14nm-2290",
559 .data = &dsi_phy_14nm_2290_cfgs },
560 { .compatible = "qcom,dsi-phy-14nm-660",
561 .data = &dsi_phy_14nm_660_cfgs },
562 { .compatible = "qcom,dsi-phy-14nm-8953",
563 .data = &dsi_phy_14nm_8953_cfgs },
564 { .compatible = "qcom,sm6125-dsi-phy-14nm",
565 .data = &dsi_phy_14nm_2290_cfgs },
567 #ifdef CONFIG_DRM_MSM_DSI_10NM_PHY
568 { .compatible = "qcom,dsi-phy-10nm",
569 .data = &dsi_phy_10nm_cfgs },
570 { .compatible = "qcom,dsi-phy-10nm-8998",
571 .data = &dsi_phy_10nm_8998_cfgs },
573 #ifdef CONFIG_DRM_MSM_DSI_7NM_PHY
574 { .compatible = "qcom,dsi-phy-7nm",
575 .data = &dsi_phy_7nm_cfgs },
576 { .compatible = "qcom,dsi-phy-7nm-8150",
577 .data = &dsi_phy_7nm_8150_cfgs },
578 { .compatible = "qcom,sc7280-dsi-phy-7nm",
579 .data = &dsi_phy_7nm_7280_cfgs },
580 { .compatible = "qcom,sm6375-dsi-phy-7nm",
581 .data = &dsi_phy_7nm_6375_cfgs },
582 { .compatible = "qcom,sm8350-dsi-phy-5nm",
583 .data = &dsi_phy_5nm_8350_cfgs },
584 { .compatible = "qcom,sm8450-dsi-phy-5nm",
585 .data = &dsi_phy_5nm_8450_cfgs },
586 { .compatible = "qcom,sm8550-dsi-phy-4nm",
587 .data = &dsi_phy_4nm_8550_cfgs },
593 * Currently, we only support one SoC for each PHY type. When we have multiple
594 * SoCs for the same PHY, we can try to make the index searching a bit more
597 static int dsi_phy_get_id(struct msm_dsi_phy *phy)
599 struct platform_device *pdev = phy->pdev;
600 const struct msm_dsi_phy_cfg *cfg = phy->cfg;
601 struct resource *res;
604 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_phy");
608 for (i = 0; i < cfg->num_dsi_phy; i++) {
609 if (cfg->io_start[i] == res->start)
616 static int dsi_phy_driver_probe(struct platform_device *pdev)
618 struct msm_dsi_phy *phy;
619 struct device *dev = &pdev->dev;
623 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
627 phy->provided_clocks = devm_kzalloc(dev,
628 struct_size(phy->provided_clocks, hws, NUM_PROVIDED_CLKS),
630 if (!phy->provided_clocks)
633 phy->provided_clocks->num = NUM_PROVIDED_CLKS;
635 phy->cfg = of_device_get_match_data(&pdev->dev);
641 phy->id = dsi_phy_get_id(phy);
643 return dev_err_probe(dev, phy->id,
644 "Couldn't identify PHY index\n");
646 phy->regulator_ldo_mode = of_property_read_bool(dev->of_node,
647 "qcom,dsi-phy-regulator-ldo-mode");
648 if (!of_property_read_u32(dev->of_node, "phy-type", &phy_type))
649 phy->cphy_mode = (phy_type == PHY_TYPE_CPHY);
651 phy->base = msm_ioremap_size(pdev, "dsi_phy", &phy->base_size);
652 if (IS_ERR(phy->base))
653 return dev_err_probe(dev, PTR_ERR(phy->base),
654 "Failed to map phy base\n");
656 phy->pll_base = msm_ioremap_size(pdev, "dsi_pll", &phy->pll_size);
657 if (IS_ERR(phy->pll_base))
658 return dev_err_probe(dev, PTR_ERR(phy->pll_base),
659 "Failed to map pll base\n");
661 if (phy->cfg->has_phy_lane) {
662 phy->lane_base = msm_ioremap_size(pdev, "dsi_phy_lane", &phy->lane_size);
663 if (IS_ERR(phy->lane_base))
664 return dev_err_probe(dev, PTR_ERR(phy->lane_base),
665 "Failed to map phy lane base\n");
668 if (phy->cfg->has_phy_regulator) {
669 phy->reg_base = msm_ioremap_size(pdev, "dsi_phy_regulator", &phy->reg_size);
670 if (IS_ERR(phy->reg_base))
671 return dev_err_probe(dev, PTR_ERR(phy->reg_base),
672 "Failed to map phy regulator base\n");
675 if (phy->cfg->ops.parse_dt_properties) {
676 ret = phy->cfg->ops.parse_dt_properties(phy);
681 ret = devm_regulator_bulk_get_const(dev, phy->cfg->num_regulators,
682 phy->cfg->regulator_data,
687 phy->ahb_clk = msm_clk_get(pdev, "iface");
688 if (IS_ERR(phy->ahb_clk))
689 return dev_err_probe(dev, PTR_ERR(phy->ahb_clk),
690 "Unable to get ahb clk\n");
692 /* PLL init will call into clk_register which requires
693 * register access, so we need to enable power and ahb clock.
695 ret = dsi_phy_enable_resource(phy);
699 if (phy->cfg->ops.pll_init) {
700 ret = phy->cfg->ops.pll_init(phy);
702 return dev_err_probe(dev, ret,
703 "PLL init failed; need separate clk driver\n");
706 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
707 phy->provided_clocks);
709 return dev_err_probe(dev, ret,
710 "Failed to register clk provider\n");
712 dsi_phy_disable_resource(phy);
714 platform_set_drvdata(pdev, phy);
719 static struct platform_driver dsi_phy_platform_driver = {
720 .probe = dsi_phy_driver_probe,
722 .name = "msm_dsi_phy",
723 .of_match_table = dsi_phy_dt_match,
727 void __init msm_dsi_phy_driver_register(void)
729 platform_driver_register(&dsi_phy_platform_driver);
732 void __exit msm_dsi_phy_driver_unregister(void)
734 platform_driver_unregister(&dsi_phy_platform_driver);
737 int msm_dsi_phy_enable(struct msm_dsi_phy *phy,
738 struct msm_dsi_phy_clk_request *clk_req,
739 struct msm_dsi_phy_shared_timings *shared_timings)
744 if (!phy || !phy->cfg->ops.enable)
747 dev = &phy->pdev->dev;
749 ret = dsi_phy_enable_resource(phy);
751 DRM_DEV_ERROR(dev, "%s: resource enable failed, %d\n",
756 ret = regulator_bulk_enable(phy->cfg->num_regulators, phy->supplies);
758 DRM_DEV_ERROR(dev, "%s: regulator enable failed, %d\n",
763 ret = phy->cfg->ops.enable(phy, clk_req);
765 DRM_DEV_ERROR(dev, "%s: phy enable failed, %d\n", __func__, ret);
769 memcpy(shared_timings, &phy->timing.shared_timings,
770 sizeof(*shared_timings));
773 * Resetting DSI PHY silently changes its PLL registers to reset status,
774 * which will confuse clock driver and result in wrong output rate of
775 * link clocks. Restore PLL status if its PLL is being used as clock
778 if (phy->usecase != MSM_DSI_PHY_SLAVE) {
779 ret = msm_dsi_phy_pll_restore_state(phy);
781 DRM_DEV_ERROR(dev, "%s: failed to restore phy state, %d\n",
783 goto pll_restor_fail;
790 if (phy->cfg->ops.disable)
791 phy->cfg->ops.disable(phy);
793 regulator_bulk_disable(phy->cfg->num_regulators, phy->supplies);
795 dsi_phy_disable_resource(phy);
800 void msm_dsi_phy_disable(struct msm_dsi_phy *phy)
802 if (!phy || !phy->cfg->ops.disable)
805 phy->cfg->ops.disable(phy);
807 regulator_bulk_disable(phy->cfg->num_regulators, phy->supplies);
808 dsi_phy_disable_resource(phy);
811 void msm_dsi_phy_set_usecase(struct msm_dsi_phy *phy,
812 enum msm_dsi_phy_usecase uc)
818 /* Returns true if we have to clear DSI_LANE_CTRL.HS_REQ_SEL_PHY */
819 bool msm_dsi_phy_set_continuous_clock(struct msm_dsi_phy *phy, bool enable)
821 if (!phy || !phy->cfg->ops.set_continuous_clock)
824 return phy->cfg->ops.set_continuous_clock(phy, enable);
827 void msm_dsi_phy_pll_save_state(struct msm_dsi_phy *phy)
829 if (phy->cfg->ops.save_pll_state) {
830 phy->cfg->ops.save_pll_state(phy);
831 phy->state_saved = true;
835 int msm_dsi_phy_pll_restore_state(struct msm_dsi_phy *phy)
839 if (phy->cfg->ops.restore_pll_state && phy->state_saved) {
840 ret = phy->cfg->ops.restore_pll_state(phy);
844 phy->state_saved = false;
850 void msm_dsi_phy_snapshot(struct msm_disp_state *disp_state, struct msm_dsi_phy *phy)
852 msm_disp_snapshot_add_block(disp_state,
853 phy->base_size, phy->base,
854 "dsi%d_phy", phy->id);
856 /* Do not try accessing PLL registers if it is switched off */
858 msm_disp_snapshot_add_block(disp_state,
859 phy->pll_size, phy->pll_base,
860 "dsi%d_pll", phy->id);
863 msm_disp_snapshot_add_block(disp_state,
864 phy->lane_size, phy->lane_base,
865 "dsi%d_lane", phy->id);
868 msm_disp_snapshot_add_block(disp_state,
869 phy->reg_size, phy->reg_base,
870 "dsi%d_reg", phy->id);