Linux 5.0.7
[linux/fpc-iii.git] / drivers / phy / phy-core-mipi-dphy.c
blob465fa1b91a5f4e74f3c86efab08d7e02dc88d2de
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2013 NVIDIA Corporation
4 * Copyright (C) 2018 Cadence Design Systems Inc.
5 */
7 #include <linux/errno.h>
8 #include <linux/export.h>
9 #include <linux/kernel.h>
10 #include <linux/time64.h>
12 #include <linux/phy/phy.h>
13 #include <linux/phy/phy-mipi-dphy.h>
15 #define PSEC_PER_SEC 1000000000000LL
18 * Minimum D-PHY timings based on MIPI D-PHY specification. Derived
19 * from the valid ranges specified in Section 6.9, Table 14, Page 41
20 * of the D-PHY specification (v2.1).
22 int phy_mipi_dphy_get_default_config(unsigned long pixel_clock,
23 unsigned int bpp,
24 unsigned int lanes,
25 struct phy_configure_opts_mipi_dphy *cfg)
27 unsigned long long hs_clk_rate;
28 unsigned long long ui;
30 if (!cfg)
31 return -EINVAL;
33 hs_clk_rate = pixel_clock * bpp;
34 do_div(hs_clk_rate, lanes);
36 ui = ALIGN(PSEC_PER_SEC, hs_clk_rate);
37 do_div(ui, hs_clk_rate);
39 cfg->clk_miss = 0;
40 cfg->clk_post = 60000 + 52 * ui;
41 cfg->clk_pre = 8000;
42 cfg->clk_prepare = 38000;
43 cfg->clk_settle = 95000;
44 cfg->clk_term_en = 0;
45 cfg->clk_trail = 60000;
46 cfg->clk_zero = 262000;
47 cfg->d_term_en = 0;
48 cfg->eot = 0;
49 cfg->hs_exit = 100000;
50 cfg->hs_prepare = 40000 + 4 * ui;
51 cfg->hs_zero = 105000 + 6 * ui;
52 cfg->hs_settle = 85000 + 6 * ui;
53 cfg->hs_skip = 40000;
56 * The MIPI D-PHY specification (Section 6.9, v1.2, Table 14, Page 40)
57 * contains this formula as:
59 * T_HS-TRAIL = max(n * 8 * ui, 60 + n * 4 * ui)
61 * where n = 1 for forward-direction HS mode and n = 4 for reverse-
62 * direction HS mode. There's only one setting and this function does
63 * not parameterize on anything other that ui, so this code will
64 * assumes that reverse-direction HS mode is supported and uses n = 4.
66 cfg->hs_trail = max(4 * 8 * ui, 60000 + 4 * 4 * ui);
68 cfg->init = 100000000;
69 cfg->lpx = 60000;
70 cfg->ta_get = 5 * cfg->lpx;
71 cfg->ta_go = 4 * cfg->lpx;
72 cfg->ta_sure = 2 * cfg->lpx;
73 cfg->wakeup = 1000000000;
75 cfg->hs_clk_rate = hs_clk_rate;
76 cfg->lanes = lanes;
78 return 0;
80 EXPORT_SYMBOL(phy_mipi_dphy_get_default_config);
83 * Validate D-PHY configuration according to MIPI D-PHY specification
84 * (v1.2, Section Section 6.9 "Global Operation Timing Parameters").
86 int phy_mipi_dphy_config_validate(struct phy_configure_opts_mipi_dphy *cfg)
88 unsigned long long ui;
90 if (!cfg)
91 return -EINVAL;
93 ui = ALIGN(PSEC_PER_SEC, cfg->hs_clk_rate);
94 do_div(ui, cfg->hs_clk_rate);
96 if (cfg->clk_miss > 60000)
97 return -EINVAL;
99 if (cfg->clk_post < (60000 + 52 * ui))
100 return -EINVAL;
102 if (cfg->clk_pre < 8000)
103 return -EINVAL;
105 if (cfg->clk_prepare < 38000 || cfg->clk_prepare > 95000)
106 return -EINVAL;
108 if (cfg->clk_settle < 95000 || cfg->clk_settle > 300000)
109 return -EINVAL;
111 if (cfg->clk_term_en > 38000)
112 return -EINVAL;
114 if (cfg->clk_trail < 60000)
115 return -EINVAL;
117 if ((cfg->clk_prepare + cfg->clk_zero) < 300000)
118 return -EINVAL;
120 if (cfg->d_term_en > (35000 + 4 * ui))
121 return -EINVAL;
123 if (cfg->eot > (105000 + 12 * ui))
124 return -EINVAL;
126 if (cfg->hs_exit < 100000)
127 return -EINVAL;
129 if (cfg->hs_prepare < (40000 + 4 * ui) ||
130 cfg->hs_prepare > (85000 + 6 * ui))
131 return -EINVAL;
133 if ((cfg->hs_prepare + cfg->hs_zero) < (145000 + 10 * ui))
134 return -EINVAL;
136 if ((cfg->hs_settle < (85000 + 6 * ui)) ||
137 (cfg->hs_settle > (145000 + 10 * ui)))
138 return -EINVAL;
140 if (cfg->hs_skip < 40000 || cfg->hs_skip > (55000 + 4 * ui))
141 return -EINVAL;
143 if (cfg->hs_trail < max(8 * ui, 60000 + 4 * ui))
144 return -EINVAL;
146 if (cfg->init < 100000000)
147 return -EINVAL;
149 if (cfg->lpx < 50000)
150 return -EINVAL;
152 if (cfg->ta_get != (5 * cfg->lpx))
153 return -EINVAL;
155 if (cfg->ta_go != (4 * cfg->lpx))
156 return -EINVAL;
158 if (cfg->ta_sure < cfg->lpx || cfg->ta_sure > (2 * cfg->lpx))
159 return -EINVAL;
161 if (cfg->wakeup < 1000000000)
162 return -EINVAL;
164 return 0;
166 EXPORT_SYMBOL(phy_mipi_dphy_config_validate);