• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4  * Author:
5  *      Chris Zhong <zyw@rock-chips.com>
6  *      Nickey Yang <nickey.yang@rock-chips.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/iopoll.h>
11 #include <linux/math64.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/phy/phy.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 
19 #include <drm/drm_dsc.h>
20 #include <video/mipi_display.h>
21 #include <uapi/linux/videodev2.h>
22 #include <drm/bridge/dw_mipi_dsi.h>
23 #include <drm/drm_mipi_dsi.h>
24 #include <drm/drm_of.h>
25 #include <drm/drm_simple_kms_helper.h>
26 
27 #include "rockchip_drm_drv.h"
28 #include "rockchip_drm_vop.h"
29 
30 #define DSI_PHY_RSTZ 0xa0
31 #define PHY_DISFORCEPLL 0
32 #define PHY_ENFORCEPLL BIT(3)
33 #define PHY_DISABLECLK 0
34 #define PHY_ENABLECLK BIT(2)
35 #define PHY_RSTZ 0
36 #define PHY_UNRSTZ BIT(1)
37 #define PHY_SHUTDOWNZ 0
38 #define PHY_UNSHUTDOWNZ BIT(0)
39 
40 #define DSI_PHY_IF_CFG 0xa4
41 #define N_LANES(n) ((((n)-1) & 0x3) << 0)
42 #define PHY_STOP_WAIT_TIME(cycle) (((cycle)&0xff) << 8)
43 
44 #define DSI_PHY_STATUS 0xb0
45 #define LOCK BIT(0)
46 #define STOP_STATE_CLK_LANE BIT(2)
47 
48 #define DSI_PHY_TST_CTRL0 0xb4
49 #define PHY_TESTCLK BIT(1)
50 #define PHY_UNTESTCLK 0
51 #define PHY_TESTCLR BIT(0)
52 #define PHY_UNTESTCLR 0
53 
54 #define DSI_PHY_TST_CTRL1 0xb8
55 #define PHY_TESTEN BIT(16)
56 #define PHY_UNTESTEN 0
57 #define PHY_TESTDOUT(n) (((n)&0xff) << 8)
58 #define PHY_TESTDIN(n) (((n)&0xff) << 0)
59 
60 #define DSI_INT_ST0 0xbc
61 #define DSI_INT_ST1 0xc0
62 #define DSI_INT_MSK0 0xc4
63 #define DSI_INT_MSK1 0xc8
64 
65 #define PHY_STATUS_TIMEOUT_US 10000
66 #define CMD_PKT_STATUS_TIMEOUT_US 20000
67 
68 #define BYPASS_VCO_RANGE BIT(7)
69 #define VCO_RANGE_CON_SEL(val) (((val)&0x7) << 3)
70 #define VCO_IN_CAP_CON_DEFAULT (0x0 << 1)
71 #define VCO_IN_CAP_CON_LOW (0x1 << 1)
72 #define VCO_IN_CAP_CON_HIGH (0x2 << 1)
73 #define REF_BIAS_CUR_SEL BIT(0)
74 
75 #define CP_CURRENT_3UA 0x1
76 #define CP_CURRENT_4_5UA 0x2
77 #define CP_CURRENT_7_5UA 0x6
78 #define CP_CURRENT_6UA 0x9
79 #define CP_CURRENT_12UA 0xb
80 #define CP_CURRENT_SEL(val) ((val)&0xf)
81 #define CP_PROGRAM_EN BIT(7)
82 
83 #define LPF_RESISTORS_15_5KOHM 0x1
84 #define LPF_RESISTORS_13KOHM 0x2
85 #define LPF_RESISTORS_11_5KOHM 0x4
86 #define LPF_RESISTORS_10_5KOHM 0x8
87 #define LPF_RESISTORS_8KOHM 0x10
88 #define LPF_PROGRAM_EN BIT(6)
89 #define LPF_RESISTORS_SEL(val) ((val)&0x3f)
90 
91 #define HSFREQRANGE_SEL(val) (((val)&0x3f) << 1)
92 
93 #define INPUT_DIVIDER(val) (((val)-1) & 0x7f)
94 #define LOW_PROGRAM_EN 0
95 #define HIGH_PROGRAM_EN BIT(7)
96 #define LOOP_DIV_LOW_SEL(val) (((val)-1) & 0x1f)
97 #define LOOP_DIV_HIGH_SEL(val) ((((val)-1) >> 5) & 0xf)
98 #define PLL_LOOP_DIV_EN BIT(5)
99 #define PLL_INPUT_DIV_EN BIT(4)
100 
101 #define POWER_CONTROL BIT(6)
102 #define INTERNAL_REG_CURRENT BIT(3)
103 #define BIAS_BLOCK_ON BIT(2)
104 #define BANDGAP_ON BIT(0)
105 
106 #define TER_RESISTOR_HIGH BIT(7)
107 #define TER_RESISTOR_LOW 0
108 #define LEVEL_SHIFTERS_ON BIT(6)
109 #define TER_CAL_DONE BIT(5)
110 #define SETRD_MAX (0x7 << 2)
111 #define POWER_MANAGE BIT(1)
112 #define TER_RESISTORS_ON BIT(0)
113 
114 #define BIASEXTR_SEL(val) ((val)&0x7)
115 #define BANDGAP_SEL(val) ((val)&0x7)
116 #define TLP_PROGRAM_EN BIT(7)
117 #define THS_PRE_PROGRAM_EN BIT(7)
118 #define THS_ZERO_PROGRAM_EN BIT(6)
119 
120 #define PLL_BIAS_CUR_SEL_CAP_VCO_CONTROL 0x10
121 #define PLL_CP_CONTROL_PLL_LOCK_BYPASS 0x11
122 #define PLL_LPF_AND_CP_CONTROL 0x12
123 #define PLL_INPUT_DIVIDER_RATIO 0x17
124 #define PLL_LOOP_DIVIDER_RATIO 0x18
125 #define PLL_INPUT_AND_LOOP_DIVIDER_RATIOS_CONTROL 0x19
126 #define BANDGAP_AND_BIAS_CONTROL 0x20
127 #define TERMINATION_RESISTER_CONTROL 0x21
128 #define AFE_BIAS_BANDGAP_ANALOG_PROGRAMMABILITY 0x22
129 #define HS_RX_CONTROL_OF_LANE_0 0x44
130 #define HS_TX_CLOCK_LANE_REQUEST_STATE_TIME_CONTROL 0x60
131 #define HS_TX_CLOCK_LANE_PREPARE_STATE_TIME_CONTROL 0x61
132 #define HS_TX_CLOCK_LANE_HS_ZERO_STATE_TIME_CONTROL 0x62
133 #define HS_TX_CLOCK_LANE_TRAIL_STATE_TIME_CONTROL 0x63
134 #define HS_TX_CLOCK_LANE_EXIT_STATE_TIME_CONTROL 0x64
135 #define HS_TX_CLOCK_LANE_POST_TIME_CONTROL 0x65
136 #define HS_TX_DATA_LANE_REQUEST_STATE_TIME_CONTROL 0x70
137 #define HS_TX_DATA_LANE_PREPARE_STATE_TIME_CONTROL 0x71
138 #define HS_TX_DATA_LANE_HS_ZERO_STATE_TIME_CONTROL 0x72
139 #define HS_TX_DATA_LANE_TRAIL_STATE_TIME_CONTROL 0x73
140 #define HS_TX_DATA_LANE_EXIT_STATE_TIME_CONTROL 0x74
141 
142 #define DW_MIPI_NEEDS_PHY_CFG_CLK BIT(0)
143 #define DW_MIPI_NEEDS_GRF_CLK BIT(1)
144 #define DW_MIPI_NEEDS_HCLK BIT(2)
145 
146 #define PX30_GRF_PD_VO_CON1 0x0438
147 #define PX30_DSI_FORCETXSTOPMODE (0xf << 7)
148 #define PX30_DSI_FORCERXMODE BIT(6)
149 #define PX30_DSI_TURNDISABLE BIT(5)
150 #define PX30_DSI_LCDC_SEL BIT(0)
151 
152 #define RK3288_GRF_SOC_CON6 0x025c
153 #define RK3288_DSI0_LCDC_SEL BIT(6)
154 #define RK3288_DSI1_LCDC_SEL BIT(9)
155 
156 #define RK3399_GRF_SOC_CON20 0x6250
157 #define RK3399_DSI0_LCDC_SEL BIT(0)
158 #define RK3399_DSI1_LCDC_SEL BIT(4)
159 
160 #define RK3399_GRF_SOC_CON22 0x6258
161 #define RK3399_DSI0_TURNREQUEST (0xf << 12)
162 #define RK3399_DSI0_TURNDISABLE (0xf << 8)
163 #define RK3399_DSI0_FORCETXSTOPMODE (0xf << 4)
164 #define RK3399_DSI0_FORCERXMODE (0xf << 0)
165 
166 #define RK3399_GRF_SOC_CON23 0x625c
167 #define RK3399_DSI1_TURNDISABLE (0xf << 12)
168 #define RK3399_DSI1_FORCETXSTOPMODE (0xf << 8)
169 #define RK3399_DSI1_FORCERXMODE (0xf << 4)
170 #define RK3399_DSI1_ENABLE (0xf << 0)
171 
172 #define RK3399_GRF_SOC_CON24 0x6260
173 #define RK3399_TXRX_MASTERSLAVEZ BIT(7)
174 #define RK3399_TXRX_ENABLECLK BIT(6)
175 #define RK3399_TXRX_BASEDIR BIT(5)
176 
177 #define RK3568_GRF_VO_CON2 0x0368
178 #define RK3568_GRF_VO_CON3 0x036c
179 #define RK3568_DSI_FORCETXSTOPMODE (0xf << 4)
180 #define RK3568_DSI_TURNDISABLE (0x1 << 2)
181 #define RK3568_DSI_FORCERXMODE (0x1 << 0)
182 
183 #define HIWORD_UPDATE(val, mask) ((val) | (mask) << 16)
184 
185 #define to_dsi(nm) container_of(nm, struct dw_mipi_dsi_rockchip, nm)
186 
187 enum {
188     BANDGAP_97_07,
189     BANDGAP_98_05,
190     BANDGAP_99_02,
191     BANDGAP_100_00,
192     BANDGAP_93_17,
193     BANDGAP_94_15,
194     BANDGAP_95_12,
195     BANDGAP_96_10,
196 };
197 
198 enum {
199     BIASEXTR_87_1,
200     BIASEXTR_91_5,
201     BIASEXTR_95_9,
202     BIASEXTR_100,
203     BIASEXTR_105_94,
204     BIASEXTR_111_88,
205     BIASEXTR_118_8,
206     BIASEXTR_127_7,
207 };
208 
209 enum soc_type {
210     PX30,
211     RK3288,
212     RK3399,
213     RK3568,
214 };
215 
216 struct cmd_header {
217     u8 cmd_type;
218     u8 delay;
219     u8 payload_length;
220 };
221 
222 struct rockchip_dw_dsi_chip_data {
223     u32 reg;
224 
225     u32 lcdsel_grf_reg;
226     u32 lcdsel_big;
227     u32 lcdsel_lit;
228 
229     u32 enable_grf_reg;
230     u32 enable;
231 
232     u32 lanecfg1_grf_reg;
233     u32 lanecfg1;
234     u32 lanecfg2_grf_reg;
235     u32 lanecfg2;
236 
237     enum soc_type soc_type;
238     unsigned int flags;
239     unsigned int max_data_lanes;
240     unsigned long max_bit_rate_per_lane;
241 };
242 
243 struct dw_mipi_dsi_rockchip {
244     struct device *dev;
245     struct drm_encoder encoder;
246     void __iomem *base;
247     int id;
248 
249     bool c_option;
250     bool scrambling_en;
251     unsigned int slice_width;
252     unsigned int slice_height;
253     unsigned int slice_per_pkt;
254     bool block_pred_enable;
255     bool dsc_enable;
256     u8 version_major;
257     u8 version_minor;
258 
259     struct drm_dsc_picture_parameter_set *pps;
260     struct regmap *grf_regmap;
261     struct clk *pllref_clk;
262     struct clk *pclk;
263     struct clk *grf_clk;
264     struct clk *phy_cfg_clk;
265     struct clk *hclk;
266 
267     /* dual-channel */
268     bool is_slave;
269     struct dw_mipi_dsi_rockchip *slave;
270 
271     /* optional external dphy */
272     bool phy_enabled;
273     struct phy *phy;
274     union phy_configure_opts phy_opts;
275 
276     unsigned int lane_mbps; /* per lane */
277     u16 input_div;
278     u16 feedback_div;
279     u32 format;
280 
281     struct dw_mipi_dsi *dmd;
282     const struct rockchip_dw_dsi_chip_data *cdata;
283     struct dw_mipi_dsi_plat_data pdata;
284     int devcnt;
285     struct rockchip_drm_sub_dev sub_dev;
286     struct drm_panel *panel;
287 };
288 
289 struct dphy_pll_parameter_map {
290     unsigned int max_mbps;
291     u8 hsfreqrange;
292     u8 icpctrl;
293     u8 lpfctrl;
294 };
295 
296 /* The table is based on 27MHz DPHY pll reference clock. */
297 static const struct dphy_pll_parameter_map dppa_map[] = {
298     {89, 0x00, CP_CURRENT_3UA, LPF_RESISTORS_13KOHM},      {99, 0x10, CP_CURRENT_3UA, LPF_RESISTORS_13KOHM},
299     {109, 0x20, CP_CURRENT_3UA, LPF_RESISTORS_13KOHM},     {129, 0x01, CP_CURRENT_3UA, LPF_RESISTORS_15_5KOHM},
300     {139, 0x11, CP_CURRENT_3UA, LPF_RESISTORS_15_5KOHM},   {149, 0x21, CP_CURRENT_3UA, LPF_RESISTORS_15_5KOHM},
301     {169, 0x02, CP_CURRENT_6UA, LPF_RESISTORS_13KOHM},     {179, 0x12, CP_CURRENT_6UA, LPF_RESISTORS_13KOHM},
302     {199, 0x22, CP_CURRENT_6UA, LPF_RESISTORS_13KOHM},     {219, 0x03, CP_CURRENT_4_5UA, LPF_RESISTORS_13KOHM},
303     {239, 0x13, CP_CURRENT_4_5UA, LPF_RESISTORS_13KOHM},   {249, 0x23, CP_CURRENT_4_5UA, LPF_RESISTORS_13KOHM},
304     {269, 0x04, CP_CURRENT_6UA, LPF_RESISTORS_11_5KOHM},   {299, 0x14, CP_CURRENT_6UA, LPF_RESISTORS_11_5KOHM},
305     {329, 0x05, CP_CURRENT_3UA, LPF_RESISTORS_15_5KOHM},   {359, 0x15, CP_CURRENT_3UA, LPF_RESISTORS_15_5KOHM},
306     {399, 0x25, CP_CURRENT_3UA, LPF_RESISTORS_15_5KOHM},   {449, 0x06, CP_CURRENT_7_5UA, LPF_RESISTORS_11_5KOHM},
307     {499, 0x16, CP_CURRENT_7_5UA, LPF_RESISTORS_11_5KOHM}, {549, 0x07, CP_CURRENT_7_5UA, LPF_RESISTORS_10_5KOHM},
308     {599, 0x17, CP_CURRENT_7_5UA, LPF_RESISTORS_10_5KOHM}, {649, 0x08, CP_CURRENT_7_5UA, LPF_RESISTORS_11_5KOHM},
309     {699, 0x18, CP_CURRENT_7_5UA, LPF_RESISTORS_11_5KOHM}, {749, 0x09, CP_CURRENT_7_5UA, LPF_RESISTORS_11_5KOHM},
310     {799, 0x19, CP_CURRENT_7_5UA, LPF_RESISTORS_11_5KOHM}, {849, 0x29, CP_CURRENT_7_5UA, LPF_RESISTORS_11_5KOHM},
311     {899, 0x39, CP_CURRENT_7_5UA, LPF_RESISTORS_11_5KOHM}, {949, 0x0a, CP_CURRENT_12UA, LPF_RESISTORS_8KOHM},
312     {999, 0x1a, CP_CURRENT_12UA, LPF_RESISTORS_8KOHM},     {1049, 0x2a, CP_CURRENT_12UA, LPF_RESISTORS_8KOHM},
313     {1099, 0x3a, CP_CURRENT_12UA, LPF_RESISTORS_8KOHM},    {1149, 0x0b, CP_CURRENT_12UA, LPF_RESISTORS_10_5KOHM},
314     {1199, 0x1b, CP_CURRENT_12UA, LPF_RESISTORS_10_5KOHM}, {1249, 0x2b, CP_CURRENT_12UA, LPF_RESISTORS_10_5KOHM},
315     {1299, 0x3b, CP_CURRENT_12UA, LPF_RESISTORS_10_5KOHM}, {1349, 0x0c, CP_CURRENT_12UA, LPF_RESISTORS_10_5KOHM},
316     {1399, 0x1c, CP_CURRENT_12UA, LPF_RESISTORS_10_5KOHM}, {1449, 0x2c, CP_CURRENT_12UA, LPF_RESISTORS_10_5KOHM},
317     {1500, 0x3c, CP_CURRENT_12UA, LPF_RESISTORS_10_5KOHM}};
318 
max_mbps_to_parameter(unsigned int max_mbps)319 static int max_mbps_to_parameter(unsigned int max_mbps)
320 {
321     int i;
322 
323     for (i = 0; i < ARRAY_SIZE(dppa_map); i++) {
324         if (dppa_map[i].max_mbps >= max_mbps) {
325             return i;
326         }
327     }
328 
329     return -EINVAL;
330 }
331 
dsi_write(struct dw_mipi_dsi_rockchip * dsi,u32 reg,u32 val)332 static inline void dsi_write(struct dw_mipi_dsi_rockchip *dsi, u32 reg, u32 val)
333 {
334     writel(val, dsi->base + reg);
335 }
336 
dsi_read(struct dw_mipi_dsi_rockchip * dsi,u32 reg)337 static inline u32 dsi_read(struct dw_mipi_dsi_rockchip *dsi, u32 reg)
338 {
339     return readl(dsi->base + reg);
340 }
341 
dsi_set(struct dw_mipi_dsi_rockchip * dsi,u32 reg,u32 mask)342 static inline void dsi_set(struct dw_mipi_dsi_rockchip *dsi, u32 reg, u32 mask)
343 {
344     dsi_write(dsi, reg, dsi_read(dsi, reg) | mask);
345 }
346 
dsi_update_bits(struct dw_mipi_dsi_rockchip * dsi,u32 reg,u32 mask,u32 val)347 static inline void dsi_update_bits(struct dw_mipi_dsi_rockchip *dsi, u32 reg, u32 mask, u32 val)
348 {
349     dsi_write(dsi, reg, (dsi_read(dsi, reg) & ~mask) | val);
350 }
351 
dw_mipi_dsi_phy_write(struct dw_mipi_dsi_rockchip * dsi,u8 test_code,u8 test_data)352 static void dw_mipi_dsi_phy_write(struct dw_mipi_dsi_rockchip *dsi, u8 test_code, u8 test_data)
353 {
354     /*
355      * With the falling edge on TESTCLK, the TESTDIN[7:0] signal content
356      * is latched internally as the current test code. Test data is
357      * programmed internally by rising edge on TESTCLK.
358      */
359     dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLK | PHY_UNTESTCLR);
360 
361     dsi_write(dsi, DSI_PHY_TST_CTRL1, PHY_TESTEN | PHY_TESTDOUT(0) | PHY_TESTDIN(test_code));
362 
363     dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLK | PHY_UNTESTCLR);
364 
365     dsi_write(dsi, DSI_PHY_TST_CTRL1, PHY_UNTESTEN | PHY_TESTDOUT(0) | PHY_TESTDIN(test_data));
366 
367     dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLK | PHY_UNTESTCLR);
368 }
369 
370 /**
371  * ns2bc - Nanoseconds to byte clock cycles
372  */
ns2bc(struct dw_mipi_dsi_rockchip * dsi,int ns)373 static inline unsigned int ns2bc(struct dw_mipi_dsi_rockchip *dsi, int ns)
374 {
375     return DIV_ROUND_UP(ns * dsi->lane_mbps / 0x8, 0x3e8);
376 }
377 
378 /**
379  * ns2ui - Nanoseconds to UI time periods
380  */
ns2ui(struct dw_mipi_dsi_rockchip * dsi,int ns)381 static inline unsigned int ns2ui(struct dw_mipi_dsi_rockchip *dsi, int ns)
382 {
383     return DIV_ROUND_UP(ns * dsi->lane_mbps, 0x3e8);
384 }
385 
dw_mipi_dsi_phy_tx_config(struct dw_mipi_dsi_rockchip * dsi)386 static void dw_mipi_dsi_phy_tx_config(struct dw_mipi_dsi_rockchip *dsi)
387 {
388     if (dsi->cdata->lanecfg1_grf_reg) {
389         regmap_write(dsi->grf_regmap, dsi->cdata->lanecfg1_grf_reg, dsi->cdata->lanecfg1);
390     }
391 
392     if (dsi->cdata->lanecfg2_grf_reg) {
393         regmap_write(dsi->grf_regmap, dsi->cdata->lanecfg2_grf_reg, dsi->cdata->lanecfg2);
394     }
395 
396     if (dsi->cdata->enable_grf_reg) {
397         regmap_write(dsi->grf_regmap, dsi->cdata->enable_grf_reg, dsi->cdata->enable);
398     }
399 }
400 
dw_mipi_dsi_phy_init(void * priv_data)401 static int dw_mipi_dsi_phy_init(void *priv_data)
402 {
403     struct dw_mipi_dsi_rockchip *dsi = priv_data;
404     int i, vco;
405 
406     dw_mipi_dsi_phy_tx_config(dsi);
407 
408     if (dsi->phy) {
409         return 0;
410     }
411 
412     /*
413      * Get vco from frequency(lane_mbps)
414      * vco    frequency table
415      * 000 - between   80 and  200 MHz
416      * 001 - between  200 and  300 MHz
417      * 010 - between  300 and  500 MHz
418      * 011 - between  500 and  700 MHz
419      * 100 - between  700 and  900 MHz
420      * 101 - between  900 and 1100 MHz
421      * 110 - between 1100 and 1300 MHz
422      * 111 - between 1300 and 1500 MHz
423      */
424     vco = (dsi->lane_mbps < 0xc8) ? 0 : (dsi->lane_mbps + 0x64) / 0xc8;
425 
426     i = max_mbps_to_parameter(dsi->lane_mbps);
427     if (i < 0) {
428         DRM_DEV_ERROR(dsi->dev, "failed to get parameter for %dmbps clock\n", dsi->lane_mbps);
429         return i;
430     }
431 
432     dw_mipi_dsi_phy_write(dsi, PLL_BIAS_CUR_SEL_CAP_VCO_CONTROL,
433                           BYPASS_VCO_RANGE | VCO_RANGE_CON_SEL(vco) | VCO_IN_CAP_CON_LOW | REF_BIAS_CUR_SEL);
434 
435     dw_mipi_dsi_phy_write(dsi, PLL_CP_CONTROL_PLL_LOCK_BYPASS, CP_CURRENT_SEL(dppa_map[i].icpctrl));
436     dw_mipi_dsi_phy_write(dsi, PLL_LPF_AND_CP_CONTROL,
437                           CP_PROGRAM_EN | LPF_PROGRAM_EN | LPF_RESISTORS_SEL(dppa_map[i].lpfctrl));
438 
439     dw_mipi_dsi_phy_write(dsi, HS_RX_CONTROL_OF_LANE_0, HSFREQRANGE_SEL(dppa_map[i].hsfreqrange));
440 
441     dw_mipi_dsi_phy_write(dsi, PLL_INPUT_DIVIDER_RATIO, INPUT_DIVIDER(dsi->input_div));
442     dw_mipi_dsi_phy_write(dsi, PLL_LOOP_DIVIDER_RATIO, LOOP_DIV_LOW_SEL(dsi->feedback_div) | LOW_PROGRAM_EN);
443     /*
444      * We need set PLL_INPUT_AND_LOOP_DIVIDER_RATIOS_CONTROL immediately
445      * to make the configured LSB effective according to IP simulation
446      * and lab test results.
447      * Only in this way can we get correct mipi phy pll frequency.
448      */
449     dw_mipi_dsi_phy_write(dsi, PLL_INPUT_AND_LOOP_DIVIDER_RATIOS_CONTROL, PLL_LOOP_DIV_EN | PLL_INPUT_DIV_EN);
450     dw_mipi_dsi_phy_write(dsi, PLL_LOOP_DIVIDER_RATIO, LOOP_DIV_HIGH_SEL(dsi->feedback_div) | HIGH_PROGRAM_EN);
451     dw_mipi_dsi_phy_write(dsi, PLL_INPUT_AND_LOOP_DIVIDER_RATIOS_CONTROL, PLL_LOOP_DIV_EN | PLL_INPUT_DIV_EN);
452 
453     dw_mipi_dsi_phy_write(dsi, AFE_BIAS_BANDGAP_ANALOG_PROGRAMMABILITY, LOW_PROGRAM_EN | BIASEXTR_SEL(BIASEXTR_127_7));
454     dw_mipi_dsi_phy_write(dsi, AFE_BIAS_BANDGAP_ANALOG_PROGRAMMABILITY, HIGH_PROGRAM_EN | BANDGAP_SEL(BANDGAP_96_10));
455 
456     dw_mipi_dsi_phy_write(dsi, BANDGAP_AND_BIAS_CONTROL,
457                           POWER_CONTROL | INTERNAL_REG_CURRENT | BIAS_BLOCK_ON | BANDGAP_ON);
458 
459     dw_mipi_dsi_phy_write(dsi, TERMINATION_RESISTER_CONTROL,
460                           TER_RESISTOR_LOW | TER_CAL_DONE | SETRD_MAX | TER_RESISTORS_ON);
461     dw_mipi_dsi_phy_write(dsi, TERMINATION_RESISTER_CONTROL,
462                           TER_RESISTOR_HIGH | LEVEL_SHIFTERS_ON | SETRD_MAX | POWER_MANAGE | TER_RESISTORS_ON);
463 
464     dw_mipi_dsi_phy_write(dsi, HS_TX_CLOCK_LANE_REQUEST_STATE_TIME_CONTROL, TLP_PROGRAM_EN | ns2bc(dsi, 0x3c));
465     dw_mipi_dsi_phy_write(dsi, HS_TX_CLOCK_LANE_PREPARE_STATE_TIME_CONTROL, THS_PRE_PROGRAM_EN | ns2ui(dsi, 0x28));
466     dw_mipi_dsi_phy_write(dsi, HS_TX_CLOCK_LANE_HS_ZERO_STATE_TIME_CONTROL, THS_ZERO_PROGRAM_EN | ns2bc(dsi, 0x12c));
467     dw_mipi_dsi_phy_write(dsi, HS_TX_CLOCK_LANE_TRAIL_STATE_TIME_CONTROL, THS_PRE_PROGRAM_EN | ns2ui(dsi, 0x64));
468     dw_mipi_dsi_phy_write(dsi, HS_TX_CLOCK_LANE_EXIT_STATE_TIME_CONTROL, BIT(0x5) | ns2bc(dsi, 0x64));
469     dw_mipi_dsi_phy_write(dsi, HS_TX_CLOCK_LANE_POST_TIME_CONTROL, BIT(0x5) | (ns2bc(dsi, 0x3c) + 0x7));
470 
471     dw_mipi_dsi_phy_write(dsi, HS_TX_DATA_LANE_REQUEST_STATE_TIME_CONTROL, TLP_PROGRAM_EN | ns2bc(dsi, 0x3c));
472     dw_mipi_dsi_phy_write(dsi, HS_TX_DATA_LANE_PREPARE_STATE_TIME_CONTROL,
473                           THS_PRE_PROGRAM_EN | (ns2ui(dsi, 0x32) + 0x14));
474     dw_mipi_dsi_phy_write(dsi, HS_TX_DATA_LANE_HS_ZERO_STATE_TIME_CONTROL,
475                           THS_ZERO_PROGRAM_EN | (ns2bc(dsi, 0x8c) + 0x2));
476     dw_mipi_dsi_phy_write(dsi, HS_TX_DATA_LANE_TRAIL_STATE_TIME_CONTROL, THS_PRE_PROGRAM_EN | (ns2ui(dsi, 0x3c) + 0x8));
477     dw_mipi_dsi_phy_write(dsi, HS_TX_DATA_LANE_EXIT_STATE_TIME_CONTROL, BIT(0x5) | ns2bc(dsi, 0x64));
478 
479     return 0;
480 }
481 
dw_mipi_dsi_phy_power_on(void * priv_data)482 static void dw_mipi_dsi_phy_power_on(void *priv_data)
483 {
484     struct dw_mipi_dsi_rockchip *dsi = priv_data;
485 
486     if (dsi->phy_enabled) {
487         return;
488     }
489 
490     phy_power_on(dsi->phy);
491     dsi->phy_enabled = true;
492 }
493 
dw_mipi_dsi_phy_power_off(void * priv_data)494 static void dw_mipi_dsi_phy_power_off(void *priv_data)
495 {
496     struct dw_mipi_dsi_rockchip *dsi = priv_data;
497 
498     if (!dsi->phy_enabled) {
499         return;
500     }
501 
502     phy_power_off(dsi->phy);
503     dsi->phy_enabled = false;
504 }
505 
dw_mipi_dsi_get_lane_mbps(void * priv_data,const struct drm_display_mode * mode,unsigned long mode_flags,u32 lanes,u32 format,unsigned int * lane_mbps)506 static int dw_mipi_dsi_get_lane_mbps(void *priv_data, const struct drm_display_mode *mode, unsigned long mode_flags,
507                                      u32 lanes, u32 format, unsigned int *lane_mbps)
508 {
509     struct dw_mipi_dsi_rockchip *dsi = priv_data;
510     struct device *dev = dsi->dev;
511     int bpp;
512     unsigned long mpclk, tmp;
513     unsigned int target_mbps = 0x3e8;
514     unsigned int max_mbps;
515     unsigned long best_freq = 0;
516     unsigned long fvco_min, fvco_max, fin, fout;
517     unsigned int min_prediv, max_prediv;
518     unsigned int _prediv, best_prediv;
519     unsigned long _fbdiv, best_fbdiv;
520     unsigned long min_delta = ULONG_MAX;
521     unsigned long target_pclk, hs_clk_rate;
522     unsigned int value;
523     int ret;
524 
525     max_mbps = dsi->cdata->max_bit_rate_per_lane / USEC_PER_SEC;
526     dsi->format = format;
527     bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
528     if (bpp < 0) {
529         DRM_DEV_ERROR(dsi->dev, "failed to get bpp for pixel format %d\n", dsi->format);
530         return bpp;
531     }
532 
533     /* optional override of the desired bandwidth */
534     if (!of_property_read_u32(dev->of_node, "rockchip,lane-rate", &value)) {
535         target_mbps = value;
536     } else {
537         mpclk = DIV_ROUND_UP(mode->clock, MSEC_PER_SEC);
538         if (mpclk) {
539             /* take 1 / 0.9, since mbps must big than bandwidth of RGB */
540             if (lanes == 0) {
541                 return -EPERM;
542             }
543             tmp = mpclk * (bpp / lanes) * 0xa / 0x9;
544             if (tmp < (unsigned long)max_mbps) {
545                 target_mbps = tmp;
546             } else {
547                 DRM_DEV_ERROR(dsi->dev, "DPHY clock frequency is out of range\n");
548                 target_mbps = max_mbps;
549             }
550         }
551     }
552 
553     /* for external phy only a the mipi_dphy_config is necessary */
554     if (dsi->phy) {
555         target_pclk = DIV_ROUND_CLOSEST_ULL(target_mbps * lanes, bpp);
556         phy_mipi_dphy_get_default_config(target_pclk * USEC_PER_SEC, bpp, lanes, &dsi->phy_opts.mipi_dphy);
557         ret = phy_set_mode(dsi->phy, PHY_MODE_MIPI_DPHY);
558         if (ret) {
559             DRM_DEV_ERROR(dsi->dev, "failed to set phy mode: %d\n", ret);
560             return ret;
561         }
562 
563         phy_configure(dsi->phy, &dsi->phy_opts);
564         hs_clk_rate = dsi->phy_opts.mipi_dphy.hs_clk_rate;
565         dsi->lane_mbps = DIV_ROUND_UP(hs_clk_rate, USEC_PER_SEC);
566         *lane_mbps = dsi->lane_mbps;
567 
568         return 0;
569     }
570 
571     fin = clk_get_rate(dsi->pllref_clk);
572     fout = target_mbps * USEC_PER_SEC;
573 
574     /* constraint: 5Mhz <= Fref / N <= 40MHz */
575     min_prediv = DIV_ROUND_UP(fin, 0x28 * USEC_PER_SEC);
576     max_prediv = fin / (0x5 * USEC_PER_SEC);
577 
578     /* constraint: 80MHz <= Fvco <= 1500Mhz */
579     fvco_min = 0x50 * USEC_PER_SEC;
580     fvco_max = 0x5dc * USEC_PER_SEC;
581 
582     for (_prediv = min_prediv; _prediv <= max_prediv; _prediv++) {
583         u64 tmp;
584         u32 delta;
585         /* Fvco = Fref * M / N */
586         tmp = (u64)fout * _prediv;
587         do_div(tmp, fin);
588         _fbdiv = tmp;
589         /*
590          * Due to the use of a "by 2 pre-scaler," the range of the
591          * feedback multiplication value M is limited to even division
592          * numbers, and m must be greater than 6, not bigger than 512.
593          */
594         if (_fbdiv < 0x6 || _fbdiv > 0x200) {
595             continue;
596         }
597 
598         _fbdiv += _fbdiv % 0x2;
599 
600         tmp = (u64)_fbdiv * fin;
601         do_div(tmp, _prediv);
602         if (tmp < fvco_min || tmp > fvco_max) {
603             continue;
604         }
605 
606         delta = abs(fout - tmp);
607         if (delta < min_delta) {
608             best_prediv = _prediv;
609             best_fbdiv = _fbdiv;
610             min_delta = delta;
611             best_freq = tmp;
612         }
613     }
614 
615     if (best_freq) {
616         dsi->lane_mbps = DIV_ROUND_UP(best_freq, USEC_PER_SEC);
617         *lane_mbps = dsi->lane_mbps;
618         dsi->input_div = best_prediv;
619         dsi->feedback_div = best_fbdiv;
620     } else {
621         DRM_DEV_ERROR(dsi->dev, "Can not find best_freq for DPHY\n");
622         return -EINVAL;
623     }
624 
625     return 0;
626 }
627 
628 struct hstt {
629     unsigned int maxfreq;
630     struct dw_mipi_dsi_dphy_timing timing;
631 };
632 
633 struct dw_mipi_dsi_dphy_timing dphy_hstt = {
634     .clk_lp2hs = 0x40,
635     .clk_hs2lp = 0x40,
636     .data_lp2hs = 0x10,
637     .data_hs2lp = 0x14,
638 };
639 
dw_mipi_dsi_phy_get_timing(void * priv_data,unsigned int lane_mbps,struct dw_mipi_dsi_dphy_timing * timing)640 static int dw_mipi_dsi_phy_get_timing(void *priv_data, unsigned int lane_mbps, struct dw_mipi_dsi_dphy_timing *timing)
641 {
642     *timing = dphy_hstt;
643 
644     return 0;
645 }
646 
647 static const struct dw_mipi_dsi_phy_ops dw_mipi_dsi_rockchip_phy_ops = {
648     .init = dw_mipi_dsi_phy_init,
649     .power_on = dw_mipi_dsi_phy_power_on,
650     .power_off = dw_mipi_dsi_phy_power_off,
651     .get_lane_mbps = dw_mipi_dsi_get_lane_mbps,
652     .get_timing = dw_mipi_dsi_phy_get_timing,
653 };
654 
dw_mipi_dsi_rockchip_vop_routing(struct dw_mipi_dsi_rockchip * dsi)655 static void dw_mipi_dsi_rockchip_vop_routing(struct dw_mipi_dsi_rockchip *dsi)
656 {
657     int mux;
658 
659     mux = drm_of_encoder_active_endpoint_id(dsi->dev->of_node, &dsi->encoder);
660     if (mux < 0) {
661         return;
662     }
663 
664     if (dsi->cdata->lcdsel_grf_reg) {
665         regmap_write(dsi->grf_regmap, dsi->cdata->lcdsel_grf_reg,
666                      mux ? dsi->cdata->lcdsel_lit : dsi->cdata->lcdsel_big);
667 
668         if (dsi->slave && dsi->slave->cdata->lcdsel_grf_reg) {
669             regmap_write(dsi->slave->grf_regmap, dsi->slave->cdata->lcdsel_grf_reg,
670                          mux ? dsi->slave->cdata->lcdsel_lit : dsi->slave->cdata->lcdsel_big);
671         }
672     }
673 }
674 
dw_mipi_dsi_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)675 static int dw_mipi_dsi_encoder_atomic_check(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state,
676                                             struct drm_connector_state *conn_state)
677 {
678     struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
679     struct dw_mipi_dsi_rockchip *dsi = to_dsi(encoder);
680     struct drm_connector *connector = conn_state->connector;
681     struct drm_display_info *info = &connector->display_info;
682 
683     switch (dsi->format) {
684         case MIPI_DSI_FMT_RGB888:
685             s->output_mode = ROCKCHIP_OUT_MODE_P888;
686             break;
687         case MIPI_DSI_FMT_RGB666:
688             s->output_mode = ROCKCHIP_OUT_MODE_P666;
689             break;
690         case MIPI_DSI_FMT_RGB565:
691             s->output_mode = ROCKCHIP_OUT_MODE_P565;
692             break;
693         default:
694             WARN_ON(1);
695             return -EINVAL;
696     }
697 
698     if (info->num_bus_formats) {
699         s->bus_format = info->bus_formats[0];
700     } else {
701         s->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
702     }
703 
704     /* rk356x series drive mipi pixdata on posedge */
705     if (dsi->cdata->soc_type == RK3568) {
706         s->bus_flags &= ~DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
707         s->bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
708     }
709 
710     s->output_type = DRM_MODE_CONNECTOR_DSI;
711     s->color_space = V4L2_COLORSPACE_DEFAULT;
712     s->output_if = dsi->id ? VOP_OUTPUT_IF_MIPI1 : VOP_OUTPUT_IF_MIPI0;
713     if (dsi->slave) {
714         s->output_flags |= ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE;
715         s->output_if |= VOP_OUTPUT_IF_MIPI1;
716     }
717 
718     /* dual link dsi for rk3399 */
719     if (dsi->id && dsi->cdata->soc_type == RK3399) {
720         s->output_flags |= ROCKCHIP_OUTPUT_DATA_SWAP;
721     }
722 
723     if (dsi->dsc_enable) {
724         s->dsc_enable = 1;
725         s->dsc_sink_cap.version_major = dsi->version_major;
726         s->dsc_sink_cap.version_minor = dsi->version_minor;
727         s->dsc_sink_cap.slice_width = dsi->slice_width;
728         s->dsc_sink_cap.slice_height = dsi->slice_height;
729         /* only can support rgb888 panel now */
730         s->dsc_sink_cap.target_bits_per_pixel_x16 = 0x8 << 0x4;
731         s->dsc_sink_cap.block_pred = dsi->block_pred_enable;
732         s->dsc_sink_cap.native_420 = 0;
733 
734         memcpy(&s->pps, dsi->pps, sizeof(struct drm_dsc_picture_parameter_set));
735     }
736 
737     return 0;
738 }
739 
dw_mipi_dsi_encoder_enable(struct drm_encoder * encoder)740 static void dw_mipi_dsi_encoder_enable(struct drm_encoder *encoder)
741 {
742     struct dw_mipi_dsi_rockchip *dsi = to_dsi(encoder);
743 
744     dw_mipi_dsi_rockchip_vop_routing(dsi);
745 }
746 
dw_mipi_dsi_encoder_disable(struct drm_encoder * encoder)747 static void dw_mipi_dsi_encoder_disable(struct drm_encoder *encoder)
748 {
749 }
750 
dw_mipi_dsi_rockchip_loader_protect(struct dw_mipi_dsi_rockchip * dsi,bool on)751 static void dw_mipi_dsi_rockchip_loader_protect(struct dw_mipi_dsi_rockchip *dsi, bool on)
752 {
753     if (on) {
754         pm_runtime_get_sync(dsi->dev);
755         phy_init(dsi->phy);
756         dsi->phy_enabled = true;
757         if (dsi->phy) {
758             dsi->phy->power_count++;
759         }
760     } else {
761         pm_runtime_put(dsi->dev);
762         phy_exit(dsi->phy);
763         dsi->phy_enabled = false;
764         if (dsi->phy) {
765             dsi->phy->power_count--;
766         }
767     }
768 
769     if (dsi->slave) {
770         dw_mipi_dsi_rockchip_loader_protect(dsi->slave, on);
771     }
772 }
773 
dw_mipi_dsi_rockchip_encoder_loader_protect(struct drm_encoder * encoder,bool on)774 static void dw_mipi_dsi_rockchip_encoder_loader_protect(struct drm_encoder *encoder, bool on)
775 {
776     struct dw_mipi_dsi_rockchip *dsi = to_dsi(encoder);
777 
778     if (dsi->panel) {
779         panel_simple_loader_protect(dsi->panel);
780     }
781 
782     dw_mipi_dsi_rockchip_loader_protect(dsi, on);
783 }
784 
785 static const struct drm_encoder_helper_funcs dw_mipi_dsi_encoder_helper_funcs = {
786     .atomic_check = dw_mipi_dsi_encoder_atomic_check,
787     .enable = dw_mipi_dsi_encoder_enable,
788     .disable = dw_mipi_dsi_encoder_disable,
789 };
790 
rockchip_dsi_drm_create_encoder(struct dw_mipi_dsi_rockchip * dsi,struct drm_device * drm_dev)791 static int rockchip_dsi_drm_create_encoder(struct dw_mipi_dsi_rockchip *dsi, struct drm_device *drm_dev)
792 {
793     struct drm_encoder *encoder = &dsi->encoder;
794     int ret;
795 
796     encoder->possible_crtcs = rockchip_drm_of_find_possible_crtcs(drm_dev, dsi->dev->of_node);
797 
798     ret = drm_simple_encoder_init(drm_dev, encoder, DRM_MODE_ENCODER_DSI);
799     if (ret) {
800         DRM_ERROR("Failed to initialize encoder with drm\n");
801         return ret;
802     }
803 
804     drm_encoder_helper_add(encoder, &dw_mipi_dsi_encoder_helper_funcs);
805 
806     return 0;
807 }
808 
dw_mipi_dsi_rockchip_find_second(struct dw_mipi_dsi_rockchip * dsi)809 static struct device *dw_mipi_dsi_rockchip_find_second(struct dw_mipi_dsi_rockchip *dsi)
810 {
811     struct device_node *node = NULL;
812     struct platform_device *pdev;
813     struct dw_mipi_dsi_rockchip *dsi2;
814 
815     node = of_parse_phandle(dsi->dev->of_node, "rockchip,dual-channel", 0);
816     if (node) {
817         pdev = of_find_device_by_node(node);
818         if (!pdev) {
819             return ERR_PTR(-EPROBE_DEFER);
820         }
821 
822         dsi2 = platform_get_drvdata(pdev);
823         if (!dsi2) {
824             platform_device_put(pdev);
825             return ERR_PTR(-EPROBE_DEFER);
826         }
827 
828         return &pdev->dev;
829     }
830 
831     return NULL;
832 }
833 
dw_mipi_dsi_get_dsc_info_from_sink(struct dw_mipi_dsi_rockchip * dsi,struct drm_panel * panel,struct drm_bridge * bridge)834 static int dw_mipi_dsi_get_dsc_info_from_sink(struct dw_mipi_dsi_rockchip *dsi, struct drm_panel *panel,
835                                               struct drm_bridge *bridge)
836 {
837     struct drm_dsc_picture_parameter_set *pps = NULL;
838     struct device_node *np = NULL;
839     struct cmd_header *header;
840     const void *data;
841     char *d;
842     uint8_t *dsc_packed_pps;
843     int len;
844 
845     if (!panel && !bridge) {
846         return -ENODEV;
847     }
848 
849     if (panel) {
850         np = panel->dev->of_node;
851     } else {
852         np = bridge->of_node;
853     }
854 
855     dsi->c_option = of_property_read_bool(np, "phy-c-option");
856     dsi->scrambling_en = of_property_read_bool(np, "scrambling-enable");
857     dsi->dsc_enable = of_property_read_bool(np, "compressed-data");
858     dsi->block_pred_enable = of_property_read_bool(np, "blk-pred-enable");
859     of_property_read_u32(np, "slice-width", &dsi->slice_width);
860     of_property_read_u32(np, "slice-height", &dsi->slice_height);
861     of_property_read_u32(np, "slice-per-pkt", &dsi->slice_per_pkt);
862     of_property_read_u8(np, "version-major", &dsi->version_major);
863     of_property_read_u8(np, "version-minor", &dsi->version_minor);
864 
865     data = of_get_property(np, "panel-init-sequence", &len);
866     if (!data) {
867         return -EINVAL;
868     }
869 
870     d = devm_kmemdup(dsi->dev, data, len, GFP_KERNEL);
871     if (!d) {
872         return -ENOMEM;
873     }
874 
875     while (len > sizeof(*header)) {
876         header = (struct cmd_header *)d;
877         d += sizeof(*header);
878         len -= sizeof(*header);
879 
880         if (header->payload_length > len) {
881             return -EINVAL;
882         }
883 
884         if (header->cmd_type == MIPI_DSI_PICTURE_PARAMETER_SET) {
885             dsc_packed_pps = devm_kmemdup(dsi->dev, d, header->payload_length, GFP_KERNEL);
886             if (!dsc_packed_pps) {
887                 return -ENOMEM;
888             }
889 
890             pps = (struct drm_dsc_picture_parameter_set *)dsc_packed_pps;
891             break;
892         }
893 
894         d += header->payload_length;
895         len -= header->payload_length;
896     }
897     dsi->pps = pps;
898 
899     return 0;
900 }
901 
dw_mipi_dsi_rockchip_bind(struct device * dev,struct device * master,void * data)902 static int dw_mipi_dsi_rockchip_bind(struct device *dev, struct device *master, void *data)
903 {
904     struct dw_mipi_dsi_rockchip *dsi = dev_get_drvdata(dev);
905     struct drm_device *drm_dev = data;
906     struct device *second;
907     int ret;
908 
909     second = dw_mipi_dsi_rockchip_find_second(dsi);
910     if (IS_ERR(second)) {
911         return PTR_ERR(second);
912     }
913 
914     if (second) {
915         /* we are the slave in dual-DSI */
916         dsi->slave = dev_get_drvdata(second);
917         if (!dsi->slave) {
918             DRM_DEV_ERROR(dev, "could not get slaves data\n");
919             return -ENODEV;
920         }
921 
922         dsi->slave->is_slave = true;
923         dw_mipi_dsi_set_slave(dsi->dmd, dsi->slave->dmd);
924         put_device(second);
925     }
926 
927     if (dsi->is_slave) {
928         return 0;
929     }
930 
931     ret = clk_prepare_enable(dsi->pllref_clk);
932     if (ret) {
933         DRM_DEV_ERROR(dev, "Failed to enable pllref_clk: %d\n", ret);
934         return ret;
935     }
936 
937     ret = rockchip_dsi_drm_create_encoder(dsi, drm_dev);
938     if (ret) {
939         DRM_DEV_ERROR(dev, "Failed to create drm encoder\n");
940         return ret;
941     }
942 
943     ret = dw_mipi_dsi_bind(dsi->dmd, &dsi->encoder);
944     if (ret) {
945         DRM_DEV_ERROR(dev, "Failed to bind: %d\n", ret);
946         return ret;
947     }
948 
949     ret = drm_of_find_panel_or_bridge(dsi->dev->of_node, 1, 0, &dsi->panel, NULL);
950     if (ret) {
951         dev_err(dsi->dev, "failed to find panel\n");
952     }
953 
954     dw_mipi_dsi_get_dsc_info_from_sink(dsi, dsi->panel, NULL);
955 
956     dsi->sub_dev.connector = dw_mipi_dsi_get_connector(dsi->dmd);
957     if (dsi->sub_dev.connector) {
958         dsi->sub_dev.of_node = dev->of_node;
959         dsi->sub_dev.loader_protect = dw_mipi_dsi_rockchip_encoder_loader_protect;
960         rockchip_drm_register_sub_dev(&dsi->sub_dev);
961     }
962 
963     return 0;
964 }
965 
dw_mipi_dsi_rockchip_unbind(struct device * dev,struct device * master,void * data)966 static void dw_mipi_dsi_rockchip_unbind(struct device *dev, struct device *master, void *data)
967 {
968     struct dw_mipi_dsi_rockchip *dsi = dev_get_drvdata(dev);
969 
970     if (dsi->is_slave) {
971         return;
972     }
973 
974     if (dsi->sub_dev.connector) {
975         rockchip_drm_unregister_sub_dev(&dsi->sub_dev);
976     }
977 
978     dw_mipi_dsi_unbind(dsi->dmd);
979 
980     clk_disable_unprepare(dsi->pllref_clk);
981 }
982 
983 static const struct component_ops dw_mipi_dsi_rockchip_ops = {
984     .bind = dw_mipi_dsi_rockchip_bind,
985     .unbind = dw_mipi_dsi_rockchip_unbind,
986 };
987 
dw_mipi_dsi_rockchip_host_attach(void * priv_data,struct mipi_dsi_device * device)988 static int dw_mipi_dsi_rockchip_host_attach(void *priv_data, struct mipi_dsi_device *device)
989 {
990     struct dw_mipi_dsi_rockchip *dsi = priv_data;
991     struct device *second;
992     int ret;
993 
994     ret = component_add(dsi->dev, &dw_mipi_dsi_rockchip_ops);
995     if (ret) {
996         DRM_DEV_ERROR(dsi->dev, "Failed to register component: %d\n", ret);
997         return ret;
998     }
999 
1000     second = dw_mipi_dsi_rockchip_find_second(dsi);
1001     if (IS_ERR(second)) {
1002         return PTR_ERR(second);
1003     }
1004     if (second) {
1005         ret = component_add(second, &dw_mipi_dsi_rockchip_ops);
1006         if (ret) {
1007             DRM_DEV_ERROR(second, "Failed to register component: %d\n", ret);
1008             return ret;
1009         }
1010     }
1011 
1012     return 0;
1013 }
1014 
dw_mipi_dsi_rockchip_host_detach(void * priv_data,struct mipi_dsi_device * device)1015 static int dw_mipi_dsi_rockchip_host_detach(void *priv_data, struct mipi_dsi_device *device)
1016 {
1017     struct dw_mipi_dsi_rockchip *dsi = priv_data;
1018     struct device *second;
1019 
1020     second = dw_mipi_dsi_rockchip_find_second(dsi);
1021     if (second && !IS_ERR(second)) {
1022         component_del(second, &dw_mipi_dsi_rockchip_ops);
1023     }
1024 
1025     component_del(dsi->dev, &dw_mipi_dsi_rockchip_ops);
1026 
1027     return 0;
1028 }
1029 
1030 static const struct dw_mipi_dsi_host_ops dw_mipi_dsi_rockchip_host_ops = {
1031     .attach = dw_mipi_dsi_rockchip_host_attach,
1032     .detach = dw_mipi_dsi_rockchip_host_detach,
1033 };
1034 
dw_mipi_dsi_rockchip_probe(struct platform_device * pdev)1035 static int dw_mipi_dsi_rockchip_probe(struct platform_device *pdev)
1036 {
1037     struct device *dev = &pdev->dev;
1038     struct device_node *np = dev->of_node;
1039     struct dw_mipi_dsi_rockchip *dsi;
1040     struct resource *res;
1041     const struct rockchip_dw_dsi_chip_data *cdata = of_device_get_match_data(dev);
1042     int ret, i;
1043 
1044     dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1045     if (!dsi) {
1046         return -ENOMEM;
1047     }
1048 
1049     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1050     dsi->base = devm_ioremap_resource(dev, res);
1051     if (IS_ERR(dsi->base)) {
1052         DRM_DEV_ERROR(dev, "Unable to get dsi registers\n");
1053         return PTR_ERR(dsi->base);
1054     }
1055 
1056     i = 0;
1057     while (cdata[i].reg) {
1058         if (cdata[i].reg == res->start) {
1059             dsi->cdata = &cdata[i];
1060             dsi->id = i;
1061             break;
1062         }
1063 
1064         i++;
1065     }
1066 
1067     if (!dsi->cdata) {
1068         DRM_DEV_ERROR(dev, "no dsi-config for %s node\n", np->name);
1069         return -EINVAL;
1070     }
1071 
1072     /* try to get a possible external dphy */
1073     dsi->phy = devm_phy_optional_get(dev, "dphy");
1074     if (IS_ERR(dsi->phy)) {
1075         ret = PTR_ERR(dsi->phy);
1076         DRM_DEV_ERROR(dev, "failed to get mipi dphy: %d\n", ret);
1077         return ret;
1078     }
1079 
1080     dsi->pclk = devm_clk_get(dev, "pclk");
1081     if (IS_ERR(dsi->pclk)) {
1082         ret = PTR_ERR(dsi->pclk);
1083         dev_err(dev, "Unable to get pclk: %d\n", ret);
1084         return ret;
1085     }
1086 
1087     dsi->pllref_clk = devm_clk_get(dev, "ref");
1088     if (IS_ERR(dsi->pllref_clk)) {
1089         if (dsi->phy) {
1090             /*
1091              * if external phy is present, pll will be
1092              * generated there.
1093              */
1094             dsi->pllref_clk = NULL;
1095         } else {
1096             ret = PTR_ERR(dsi->pllref_clk);
1097             DRM_DEV_ERROR(dev, "Unable to get pll reference clock: %d\n", ret);
1098             return ret;
1099         }
1100     }
1101 
1102     if (dsi->cdata->flags & DW_MIPI_NEEDS_PHY_CFG_CLK) {
1103         dsi->phy_cfg_clk = devm_clk_get(dev, "phy_cfg");
1104         if (IS_ERR(dsi->phy_cfg_clk)) {
1105             ret = PTR_ERR(dsi->phy_cfg_clk);
1106             DRM_DEV_ERROR(dev, "Unable to get phy_cfg_clk: %d\n", ret);
1107             return ret;
1108         }
1109     }
1110 
1111     if (dsi->cdata->flags & DW_MIPI_NEEDS_GRF_CLK) {
1112         dsi->grf_clk = devm_clk_get(dev, "grf");
1113         if (IS_ERR(dsi->grf_clk)) {
1114             ret = PTR_ERR(dsi->grf_clk);
1115             DRM_DEV_ERROR(dev, "Unable to get grf_clk: %d\n", ret);
1116             return ret;
1117         }
1118     }
1119 
1120     if (dsi->cdata->flags & DW_MIPI_NEEDS_HCLK) {
1121         dsi->hclk = devm_clk_get(dev, "hclk");
1122         if (IS_ERR(dsi->hclk)) {
1123             ret = PTR_ERR(dsi->hclk);
1124             DRM_DEV_ERROR(dev, "Unable to get hclk: %d\n", ret);
1125             return ret;
1126         }
1127     }
1128 
1129     dsi->grf_regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
1130     if (IS_ERR(dsi->grf_regmap)) {
1131         DRM_DEV_ERROR(dsi->dev, "Unable to get rockchip,grf\n");
1132         return PTR_ERR(dsi->grf_regmap);
1133     }
1134 
1135     dsi->dev = dev;
1136     dsi->pdata.base = dsi->base;
1137     dsi->pdata.max_data_lanes = dsi->cdata->max_data_lanes;
1138     dsi->pdata.phy_ops = &dw_mipi_dsi_rockchip_phy_ops;
1139     dsi->pdata.host_ops = &dw_mipi_dsi_rockchip_host_ops;
1140     dsi->pdata.priv_data = dsi;
1141     platform_set_drvdata(pdev, dsi);
1142 
1143     dsi->dmd = dw_mipi_dsi_probe(pdev, &dsi->pdata);
1144     if (IS_ERR(dsi->dmd)) {
1145         ret = PTR_ERR(dsi->dmd);
1146         if (ret != -EPROBE_DEFER) {
1147             DRM_DEV_ERROR(dev, "Failed to probe dw_mipi_dsi: %d\n", ret);
1148         }
1149         goto err_clkdisable;
1150     }
1151 
1152     return 0;
1153 
1154 err_clkdisable:
1155     clk_disable_unprepare(dsi->pllref_clk);
1156     return ret;
1157 }
1158 
dw_mipi_dsi_rockchip_remove(struct platform_device * pdev)1159 static int dw_mipi_dsi_rockchip_remove(struct platform_device *pdev)
1160 {
1161     struct dw_mipi_dsi_rockchip *dsi = platform_get_drvdata(pdev);
1162 
1163     if (dsi->devcnt == 0) {
1164         component_del(dsi->dev, &dw_mipi_dsi_rockchip_ops);
1165     }
1166 
1167     dw_mipi_dsi_remove(dsi->dmd);
1168 
1169     return 0;
1170 }
1171 
dw_mipi_dsi_runtime_suspend(struct device * dev)1172 static __maybe_unused int dw_mipi_dsi_runtime_suspend(struct device *dev)
1173 {
1174     struct dw_mipi_dsi_rockchip *dsi = dev_get_drvdata(dev);
1175 
1176     clk_disable_unprepare(dsi->grf_clk);
1177     clk_disable_unprepare(dsi->pclk);
1178     clk_disable_unprepare(dsi->hclk);
1179     clk_disable_unprepare(dsi->phy_cfg_clk);
1180 
1181     return 0;
1182 }
1183 
dw_mipi_dsi_runtime_resume(struct device * dev)1184 static __maybe_unused int dw_mipi_dsi_runtime_resume(struct device *dev)
1185 {
1186     struct dw_mipi_dsi_rockchip *dsi = dev_get_drvdata(dev);
1187 
1188     clk_prepare_enable(dsi->phy_cfg_clk);
1189     clk_prepare_enable(dsi->hclk);
1190     clk_prepare_enable(dsi->pclk);
1191     clk_prepare_enable(dsi->grf_clk);
1192 
1193     return 0;
1194 }
1195 
1196 static const struct dev_pm_ops dw_mipi_dsi_rockchip_pm_ops = {
1197     SET_RUNTIME_PM_OPS(dw_mipi_dsi_runtime_suspend, dw_mipi_dsi_runtime_resume, NULL)};
1198 
1199 static const struct rockchip_dw_dsi_chip_data px30_chip_data[] = {
1200     {
1201         .reg = 0xff450000,
1202         .lcdsel_grf_reg = PX30_GRF_PD_VO_CON1,
1203         .lcdsel_big = HIWORD_UPDATE(0, PX30_DSI_LCDC_SEL),
1204         .lcdsel_lit = HIWORD_UPDATE(PX30_DSI_LCDC_SEL, PX30_DSI_LCDC_SEL),
1205 
1206         .lanecfg1_grf_reg = PX30_GRF_PD_VO_CON1,
1207         .lanecfg1 = HIWORD_UPDATE(0, PX30_DSI_TURNDISABLE | PX30_DSI_FORCERXMODE | PX30_DSI_FORCETXSTOPMODE),
1208 
1209         .max_data_lanes = 0x4,
1210         .max_bit_rate_per_lane = 1000000000UL,
1211         .soc_type = PX30,
1212     },
1213     {}};
1214 
1215 static const struct rockchip_dw_dsi_chip_data rk3288_chip_data[] = {
1216     {
1217         .reg = 0xff960000,
1218         .lcdsel_grf_reg = RK3288_GRF_SOC_CON6,
1219         .lcdsel_big = HIWORD_UPDATE(0, RK3288_DSI0_LCDC_SEL),
1220         .lcdsel_lit = HIWORD_UPDATE(RK3288_DSI0_LCDC_SEL, RK3288_DSI0_LCDC_SEL),
1221 
1222         .max_data_lanes = 0x4,
1223         .max_bit_rate_per_lane = 1500000000UL,
1224         .soc_type = RK3288,
1225     },
1226     {
1227         .reg = 0xff964000,
1228         .lcdsel_grf_reg = RK3288_GRF_SOC_CON6,
1229         .lcdsel_big = HIWORD_UPDATE(0, RK3288_DSI1_LCDC_SEL),
1230         .lcdsel_lit = HIWORD_UPDATE(RK3288_DSI1_LCDC_SEL, RK3288_DSI1_LCDC_SEL),
1231 
1232         .max_data_lanes = 0x4,
1233         .max_bit_rate_per_lane = 1500000000UL,
1234         .soc_type = RK3288,
1235     },
1236     {}};
1237 
1238 static const struct rockchip_dw_dsi_chip_data rk3399_chip_data[] = {
1239     {
1240         .reg = 0xff960000,
1241         .lcdsel_grf_reg = RK3399_GRF_SOC_CON20,
1242         .lcdsel_big = HIWORD_UPDATE(0, RK3399_DSI0_LCDC_SEL),
1243         .lcdsel_lit = HIWORD_UPDATE(RK3399_DSI0_LCDC_SEL, RK3399_DSI0_LCDC_SEL),
1244 
1245         .lanecfg1_grf_reg = RK3399_GRF_SOC_CON22,
1246         .lanecfg1 = HIWORD_UPDATE(0, RK3399_DSI0_TURNREQUEST | RK3399_DSI0_TURNDISABLE | RK3399_DSI0_FORCETXSTOPMODE |
1247                                          RK3399_DSI0_FORCERXMODE),
1248 
1249         .flags = DW_MIPI_NEEDS_PHY_CFG_CLK | DW_MIPI_NEEDS_GRF_CLK,
1250         .max_data_lanes = 0x4,
1251         .max_bit_rate_per_lane = 1500000000UL,
1252         .soc_type = RK3399,
1253     },
1254     {
1255         .reg = 0xff968000,
1256         .lcdsel_grf_reg = RK3399_GRF_SOC_CON20,
1257         .lcdsel_big = HIWORD_UPDATE(0, RK3399_DSI1_LCDC_SEL),
1258         .lcdsel_lit = HIWORD_UPDATE(RK3399_DSI1_LCDC_SEL, RK3399_DSI1_LCDC_SEL),
1259 
1260         .lanecfg1_grf_reg = RK3399_GRF_SOC_CON23,
1261         .lanecfg1 = HIWORD_UPDATE(0, RK3399_DSI1_TURNDISABLE | RK3399_DSI1_FORCETXSTOPMODE | RK3399_DSI1_FORCERXMODE |
1262                                          RK3399_DSI1_ENABLE),
1263 
1264         .lanecfg2_grf_reg = RK3399_GRF_SOC_CON24,
1265         .lanecfg2 = HIWORD_UPDATE(RK3399_TXRX_MASTERSLAVEZ | RK3399_TXRX_ENABLECLK,
1266                                   RK3399_TXRX_MASTERSLAVEZ | RK3399_TXRX_ENABLECLK | RK3399_TXRX_BASEDIR),
1267 
1268         .enable_grf_reg = RK3399_GRF_SOC_CON23,
1269         .enable = HIWORD_UPDATE(RK3399_DSI1_ENABLE, RK3399_DSI1_ENABLE),
1270 
1271         .flags = DW_MIPI_NEEDS_PHY_CFG_CLK | DW_MIPI_NEEDS_GRF_CLK,
1272         .max_data_lanes = 0x4,
1273         .max_bit_rate_per_lane = 1500000000UL,
1274         .soc_type = RK3399,
1275     },
1276     {}};
1277 
1278 static const struct rockchip_dw_dsi_chip_data rk3568_chip_data[] = {
1279     {
1280         .reg = 0xfe060000,
1281 
1282         .lanecfg1_grf_reg = RK3568_GRF_VO_CON2,
1283         .lanecfg1 = HIWORD_UPDATE(0, RK3568_DSI_TURNDISABLE | RK3568_DSI_FORCERXMODE | RK3568_DSI_FORCETXSTOPMODE),
1284 
1285         .flags = DW_MIPI_NEEDS_HCLK,
1286         .max_data_lanes = 0x4,
1287         .max_bit_rate_per_lane = 1200000000UL,
1288         .soc_type = RK3568,
1289     },
1290     {
1291         .reg = 0xfe070000,
1292 
1293         .lanecfg1_grf_reg = RK3568_GRF_VO_CON3,
1294         .lanecfg1 = HIWORD_UPDATE(0, RK3568_DSI_TURNDISABLE | RK3568_DSI_FORCERXMODE | RK3568_DSI_FORCETXSTOPMODE),
1295 
1296         .flags = DW_MIPI_NEEDS_HCLK,
1297         .max_data_lanes = 0x4,
1298         .max_bit_rate_per_lane = 1200000000UL,
1299         .soc_type = RK3568,
1300     },
1301     {}};
1302 
1303 static const struct of_device_id dw_mipi_dsi_rockchip_dt_ids[] = {
1304     {
1305         .compatible = "rockchip,px30-mipi-dsi",
1306         .data = &px30_chip_data,
1307     },
1308     {
1309         .compatible = "rockchip,rk3288-mipi-dsi",
1310         .data = &rk3288_chip_data,
1311     },
1312     {
1313         .compatible = "rockchip,rk3399-mipi-dsi",
1314         .data = &rk3399_chip_data,
1315     },
1316     {
1317         .compatible = "rockchip,rk3568-mipi-dsi",
1318         .data = &rk3568_chip_data,
1319     },
1320     {}
1321 };
1322 
1323 MODULE_DEVICE_TABLE(of, dw_mipi_dsi_rockchip_dt_ids);
1324 
1325 struct platform_driver dw_mipi_dsi_rockchip_driver = {
1326     .probe = dw_mipi_dsi_rockchip_probe,
1327     .remove = dw_mipi_dsi_rockchip_remove,
1328     .driver =
1329         {
1330             .of_match_table = dw_mipi_dsi_rockchip_dt_ids,
1331             .pm = &dw_mipi_dsi_rockchip_pm_ops,
1332             .name = "dw-mipi-dsi-rockchip",
1333         },
1334 };
1335