blob: 3f89de19970bfb39c6fc69b6b770640204729fb1 [file] [log] [blame]
Neil Armstrong90f9ecd2020-12-29 14:59:00 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Meson AXG MIPI DPHY driver
4 *
5 * Copyright (C) 2018 Amlogic, Inc. All rights reserved
6 * Copyright (C) 2020 BayLibre, SAS
7 * Author: Neil Armstrong <narmstrong@baylibre.com>
8 */
9
Neil Armstrong90f9ecd2020-12-29 14:59:00 +010010#include <log.h>
11#include <malloc.h>
12#include <asm/io.h>
13#include <bitfield.h>
14#include <dm.h>
15#include <errno.h>
16#include <generic-phy.h>
17#include <regmap.h>
18#include <linux/delay.h>
19#include <power/regulator.h>
20#include <reset.h>
21#include <clk.h>
22#include <phy-mipi-dphy.h>
23
24#include <linux/bitops.h>
25#include <linux/compat.h>
26#include <linux/bitfield.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030027#include <linux/time.h>
Neil Armstrong90f9ecd2020-12-29 14:59:00 +010028
29/* [31] soft reset for the phy.
30 * 1: reset. 0: dessert the reset.
31 * [30] clock lane soft reset.
32 * [29] data byte lane 3 soft reset.
33 * [28] data byte lane 2 soft reset.
34 * [27] data byte lane 1 soft reset.
35 * [26] data byte lane 0 soft reset.
36 * [25] mipi dsi pll clock selection.
37 * 1: clock from fixed 850Mhz clock source. 0: from VID2 PLL.
38 * [12] mipi HSbyteclk enable.
39 * [11] mipi divider clk selection.
40 * 1: select the mipi DDRCLKHS from clock divider.
41 * 0: from PLL clock.
42 * [10] mipi clock divider control.
43 * 1: /4. 0: /2.
44 * [9] mipi divider output enable.
45 * [8] mipi divider counter enable.
46 * [7] PLL clock enable.
47 * [5] LPDT data endian.
48 * 1 = transfer the high bit first. 0 : transfer the low bit first.
49 * [4] HS data endian.
50 * [3] force data byte lane in stop mode.
51 * [2] force data byte lane 0 in receiver mode.
52 * [1] write 1 to sync the txclkesc input. the internal logic have to
53 * use txclkesc to decide Txvalid and Txready.
54 * [0] enalbe the MIPI DPHY TxDDRClk.
55 */
56#define MIPI_DSI_PHY_CTRL 0x0
57
58/* [31] clk lane tx_hs_en control selection.
59 * 1: from register. 0: use clk lane state machine.
60 * [30] register bit for clock lane tx_hs_en.
61 * [29] clk lane tx_lp_en contrl selection.
62 * 1: from register. 0: from clk lane state machine.
63 * [28] register bit for clock lane tx_lp_en.
64 * [27] chan0 tx_hs_en control selection.
65 * 1: from register. 0: from chan0 state machine.
66 * [26] register bit for chan0 tx_hs_en.
67 * [25] chan0 tx_lp_en control selection.
68 * 1: from register. 0: from chan0 state machine.
69 * [24] register bit from chan0 tx_lp_en.
70 * [23] chan0 rx_lp_en control selection.
71 * 1: from register. 0: from chan0 state machine.
72 * [22] register bit from chan0 rx_lp_en.
73 * [21] chan0 contention detection enable control selection.
74 * 1: from register. 0: from chan0 state machine.
75 * [20] register bit from chan0 contention dectection enable.
76 * [19] chan1 tx_hs_en control selection.
77 * 1: from register. 0: from chan0 state machine.
78 * [18] register bit for chan1 tx_hs_en.
79 * [17] chan1 tx_lp_en control selection.
80 * 1: from register. 0: from chan0 state machine.
81 * [16] register bit from chan1 tx_lp_en.
82 * [15] chan2 tx_hs_en control selection.
83 * 1: from register. 0: from chan0 state machine.
84 * [14] register bit for chan2 tx_hs_en.
85 * [13] chan2 tx_lp_en control selection.
86 * 1: from register. 0: from chan0 state machine.
87 * [12] register bit from chan2 tx_lp_en.
88 * [11] chan3 tx_hs_en control selection.
89 * 1: from register. 0: from chan0 state machine.
90 * [10] register bit for chan3 tx_hs_en.
91 * [9] chan3 tx_lp_en control selection.
92 * 1: from register. 0: from chan0 state machine.
93 * [8] register bit from chan3 tx_lp_en.
94 * [4] clk chan power down. this bit is also used as the power down
95 * of the whole MIPI_DSI_PHY.
96 * [3] chan3 power down.
97 * [2] chan2 power down.
98 * [1] chan1 power down.
99 * [0] chan0 power down.
100 */
101#define MIPI_DSI_CHAN_CTRL 0x4
102
103/* [24] rx turn watch dog triggered.
104 * [23] rx esc watchdog triggered.
105 * [22] mbias ready.
106 * [21] txclkesc synced and ready.
107 * [20:17] clk lane state. {mbias_ready, tx_stop, tx_ulps, tx_hs_active}
108 * [16:13] chan3 state{0, tx_stop, tx_ulps, tx_hs_active}
109 * [12:9] chan2 state.{0, tx_stop, tx_ulps, tx_hs_active}
110 * [8:5] chan1 state. {0, tx_stop, tx_ulps, tx_hs_active}
111 * [4:0] chan0 state. {TX_STOP, tx_ULPS, hs_active, direction, rxulpsesc}
112 */
113#define MIPI_DSI_CHAN_STS 0x8
114
115/* [31:24] TCLK_PREPARE.
116 * [23:16] TCLK_ZERO.
117 * [15:8] TCLK_POST.
118 * [7:0] TCLK_TRAIL.
119 */
120#define MIPI_DSI_CLK_TIM 0xc
121
122/* [31:24] THS_PREPARE.
123 * [23:16] THS_ZERO.
124 * [15:8] THS_TRAIL.
125 * [7:0] THS_EXIT.
126 */
127#define MIPI_DSI_HS_TIM 0x10
128
129/* [31:24] tTA_GET.
130 * [23:16] tTA_GO.
131 * [15:8] tTA_SURE.
132 * [7:0] tLPX.
133 */
134#define MIPI_DSI_LP_TIM 0x14
135
136/* wait time to MIPI DIS analog ready. */
137#define MIPI_DSI_ANA_UP_TIM 0x18
138
139/* TINIT. */
140#define MIPI_DSI_INIT_TIM 0x1c
141
142/* TWAKEUP. */
143#define MIPI_DSI_WAKEUP_TIM 0x20
144
145/* when in RxULPS check state, after the the logic enable the analog,
146 * how long we should wait to check the lP state .
147 */
148#define MIPI_DSI_LPOK_TIM 0x24
149
150/* Watchdog for RX low power state no finished. */
151#define MIPI_DSI_LP_WCHDOG 0x28
152
153/* tMBIAS, after send power up signals to analog,
154 * how long we should wait for analog powered up.
155 */
156#define MIPI_DSI_ANA_CTRL 0x2c
157
158/* [31:8] reserved for future.
159 * [7:0] tCLK_PRE.
160 */
161#define MIPI_DSI_CLK_TIM1 0x30
162
163/* watchdog for turn around waiting time. */
164#define MIPI_DSI_TURN_WCHDOG 0x34
165
166/* When in RxULPS state, how frequency we should to check
167 * if the TX side out of ULPS state.
168 */
169#define MIPI_DSI_ULPS_CHECK 0x38
170#define MIPI_DSI_TEST_CTRL0 0x3c
171#define MIPI_DSI_TEST_CTRL1 0x40
172
Neil Armstrong90f9ecd2020-12-29 14:59:00 +0100173struct phy_meson_axg_mipi_dphy_priv {
174 struct regmap *regmap;
175#if CONFIG_IS_ENABLED(CLK)
176 struct clk clk;
177#endif
178 struct reset_ctl reset;
179 struct phy analog;
180 struct phy_configure_opts_mipi_dphy config;
181};
182
183static int phy_meson_axg_mipi_dphy_configure(struct phy *phy, void *params)
184{
185 struct udevice *dev = phy->dev;
186 struct phy_meson_axg_mipi_dphy_priv *priv = dev_get_priv(dev);
187 struct phy_configure_opts_mipi_dphy *config = params;
188 int ret;
189
190 ret = phy_mipi_dphy_config_validate(config);
191 if (ret)
192 return ret;
193
194 ret = generic_phy_configure(&priv->analog, config);
195 if (ret)
196 return ret;
197
198 memcpy(&priv->config, config, sizeof(priv->config));
199
200 return 0;
201}
202
203static int phy_meson_axg_mipi_dphy_power_on(struct phy *phy)
204{
205 struct udevice *dev = phy->dev;
206 struct phy_meson_axg_mipi_dphy_priv *priv = dev_get_priv(dev);
207 unsigned long temp;
208 int ret;
209
210 ret = generic_phy_power_on(&priv->analog);
211 if (ret)
212 return ret;
213
214 /* enable phy clock */
215 regmap_write(priv->regmap, MIPI_DSI_PHY_CTRL, 0x1);
216 regmap_write(priv->regmap, MIPI_DSI_PHY_CTRL,
217 BIT(0) | /* enable the DSI PLL clock . */
218 BIT(7) | /* enable pll clock which connected to DDR clock path */
219 BIT(8)); /* enable the clock divider counter */
220
221 /* enable the divider clock out */
222 regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(9), BIT(9));
223
224 /* enable the byte clock generation. */
225 regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(12), BIT(12));
226 regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(31), BIT(31));
227 regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(31), 0);
228
229 /* Calculate lanebyteclk period in ps */
230 temp = (1000000 * 100) / (priv->config.hs_clk_rate / 1000);
231 temp = temp * 8 * 10;
232
233 regmap_write(priv->regmap, MIPI_DSI_CLK_TIM,
234 DIV_ROUND_UP(priv->config.clk_trail, temp) |
235 (DIV_ROUND_UP(priv->config.clk_post +
236 priv->config.hs_trail, temp) << 8) |
237 (DIV_ROUND_UP(priv->config.clk_zero, temp) << 16) |
238 (DIV_ROUND_UP(priv->config.clk_prepare, temp) << 24));
239 regmap_write(priv->regmap, MIPI_DSI_CLK_TIM1,
240 DIV_ROUND_UP(priv->config.clk_pre, temp));
241
242 regmap_write(priv->regmap, MIPI_DSI_HS_TIM,
243 DIV_ROUND_UP(priv->config.hs_exit, temp) |
244 (DIV_ROUND_UP(priv->config.hs_trail, temp) << 8) |
245 (DIV_ROUND_UP(priv->config.hs_zero, temp) << 16) |
246 (DIV_ROUND_UP(priv->config.hs_prepare, temp) << 24));
247
248 regmap_write(priv->regmap, MIPI_DSI_LP_TIM,
249 DIV_ROUND_UP(priv->config.lpx, temp) |
250 (DIV_ROUND_UP(priv->config.ta_sure, temp) << 8) |
251 (DIV_ROUND_UP(priv->config.ta_go, temp) << 16) |
252 (DIV_ROUND_UP(priv->config.ta_get, temp) << 24));
253
254 regmap_write(priv->regmap, MIPI_DSI_ANA_UP_TIM, 0x0100);
255 regmap_write(priv->regmap, MIPI_DSI_INIT_TIM,
256 DIV_ROUND_UP(priv->config.init * NSEC_PER_MSEC, temp));
257 regmap_write(priv->regmap, MIPI_DSI_WAKEUP_TIM,
258 DIV_ROUND_UP(priv->config.wakeup * NSEC_PER_MSEC, temp));
259 regmap_write(priv->regmap, MIPI_DSI_LPOK_TIM, 0x7C);
260 regmap_write(priv->regmap, MIPI_DSI_ULPS_CHECK, 0x927C);
261 regmap_write(priv->regmap, MIPI_DSI_LP_WCHDOG, 0x1000);
262 regmap_write(priv->regmap, MIPI_DSI_TURN_WCHDOG, 0x1000);
263
264 /* Powerup the analog circuit */
265 switch (priv->config.lanes) {
266 case 1:
267 regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0xe);
268 break;
269 case 2:
270 regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0xc);
271 break;
272 case 3:
273 regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0x8);
274 break;
275 case 4:
276 default:
277 regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0);
278 break;
279 }
280
281 /* Trigger a sync active for esc_clk */
282 regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(1), BIT(1));
283
284 return 0;
285}
286
287static int phy_meson_axg_mipi_dphy_power_off(struct phy *phy)
288{
289 struct udevice *dev = phy->dev;
290 struct phy_meson_axg_mipi_dphy_priv *priv = dev_get_priv(dev);
291
292 regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0xf);
293 regmap_write(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(31));
294
295 return generic_phy_power_off(&priv->analog);
296}
297
298static int phy_meson_axg_mipi_dphy_init(struct phy *phy)
299{
300 struct udevice *dev = phy->dev;
301 struct phy_meson_axg_mipi_dphy_priv *priv = dev_get_priv(dev);
302 int ret;
303
304 ret = generic_phy_init(&priv->analog);
305 if (ret)
306 return ret;
307
308 ret = reset_assert(&priv->reset);
309 udelay(1);
310 ret |= reset_deassert(&priv->reset);
311 if (ret)
312 return ret;
313
314 return 0;
315}
316
317static int phy_meson_axg_mipi_dphy_exit(struct phy *phy)
318{
319 struct udevice *dev = phy->dev;
320 struct phy_meson_axg_mipi_dphy_priv *priv = dev_get_priv(dev);
321 int ret;
322
323 ret = generic_phy_exit(&priv->analog);
324 if (ret)
325 return ret;
326
327 return reset_assert(&priv->reset);
328}
329
330struct phy_ops meson_axg_mipi_dphy_ops = {
331 .init = phy_meson_axg_mipi_dphy_init,
332 .exit = phy_meson_axg_mipi_dphy_exit,
333 .power_on = phy_meson_axg_mipi_dphy_power_on,
334 .power_off = phy_meson_axg_mipi_dphy_power_off,
335 .configure = phy_meson_axg_mipi_dphy_configure,
336};
337
338int meson_axg_mipi_dphy_probe(struct udevice *dev)
339{
340 struct phy_meson_axg_mipi_dphy_priv *priv = dev_get_priv(dev);
341 int ret;
342
343 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
344 if (ret)
345 return ret;
346
347 ret = generic_phy_get_by_index(dev, 0, &priv->analog);
348 if (ret)
349 return ret;
350
351 ret = reset_get_by_index(dev, 0, &priv->reset);
352 if (ret == -ENOTSUPP)
353 return 0;
354 else if (ret)
355 return ret;
356
357 ret = reset_deassert(&priv->reset);
358 if (ret) {
359 reset_release_all(&priv->reset, 1);
360 return ret;
361 }
362
363#if CONFIG_IS_ENABLED(CLK)
364 ret = clk_get_by_index(dev, 0, &priv->clk);
365 if (ret < 0)
366 return ret;
367
368 ret = clk_enable(&priv->clk);
369 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
370 pr_err("failed to enable PHY clock\n");
Neil Armstrong90f9ecd2020-12-29 14:59:00 +0100371 return ret;
372 }
373#endif
374
375 return 0;
376}
377
378static const struct udevice_id meson_axg_mipi_dphy_ids[] = {
379 { .compatible = "amlogic,axg-mipi-dphy" },
380 { }
381};
382
383U_BOOT_DRIVER(meson_axg_mipi_dphy) = {
384 .name = "meson_axg_mipi_dphy",
385 .id = UCLASS_PHY,
386 .of_match = meson_axg_mipi_dphy_ids,
387 .probe = meson_axg_mipi_dphy_probe,
388 .ops = &meson_axg_mipi_dphy_ops,
Alper Nebi Yasakf37193e2021-05-14 23:54:19 +0300389 .priv_auto = sizeof(struct phy_meson_axg_mipi_dphy_priv),
Neil Armstrong90f9ecd2020-12-29 14:59:00 +0100390};