1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 MediaTek Inc.
4  * Author: Jie Qiu <jie.qiu@mediatek.com>
5  */
6 
7 #include <linux/bitfield.h>
8 #include <linux/clk.h>
9 #include <linux/component.h>
10 #include <linux/debugfs.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/media-bus-format.h>
14 #include <linux/of.h>
15 #include <linux/of_graph.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/soc/mediatek/mtk-mmsys.h>
19 #include <linux/types.h>
20 
21 #include <video/videomode.h>
22 
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_bridge.h>
25 #include <drm/drm_bridge_connector.h>
26 #include <drm/drm_crtc.h>
27 #include <drm/drm_edid.h>
28 #include <drm/drm_of.h>
29 #include <drm/drm_simple_kms_helper.h>
30 
31 #include "mtk_ddp_comp.h"
32 #include "mtk_disp_drv.h"
33 #include "mtk_dpi_regs.h"
34 #include "mtk_drm_drv.h"
35 
36 enum mtk_dpi_out_bit_num {
37 	MTK_DPI_OUT_BIT_NUM_8BITS,
38 	MTK_DPI_OUT_BIT_NUM_10BITS,
39 	MTK_DPI_OUT_BIT_NUM_12BITS,
40 	MTK_DPI_OUT_BIT_NUM_16BITS
41 };
42 
43 enum mtk_dpi_out_yc_map {
44 	MTK_DPI_OUT_YC_MAP_RGB,
45 	MTK_DPI_OUT_YC_MAP_CYCY,
46 	MTK_DPI_OUT_YC_MAP_YCYC,
47 	MTK_DPI_OUT_YC_MAP_CY,
48 	MTK_DPI_OUT_YC_MAP_YC
49 };
50 
51 enum mtk_dpi_out_channel_swap {
52 	MTK_DPI_OUT_CHANNEL_SWAP_RGB,
53 	MTK_DPI_OUT_CHANNEL_SWAP_GBR,
54 	MTK_DPI_OUT_CHANNEL_SWAP_BRG,
55 	MTK_DPI_OUT_CHANNEL_SWAP_RBG,
56 	MTK_DPI_OUT_CHANNEL_SWAP_GRB,
57 	MTK_DPI_OUT_CHANNEL_SWAP_BGR
58 };
59 
60 enum mtk_dpi_out_color_format {
61 	MTK_DPI_COLOR_FORMAT_RGB,
62 	MTK_DPI_COLOR_FORMAT_YCBCR_422
63 };
64 
65 struct mtk_dpi {
66 	struct drm_encoder encoder;
67 	struct drm_bridge bridge;
68 	struct drm_bridge *next_bridge;
69 	struct drm_connector *connector;
70 	void __iomem *regs;
71 	struct device *dev;
72 	struct device *mmsys_dev;
73 	struct clk *engine_clk;
74 	struct clk *pixel_clk;
75 	struct clk *tvd_clk;
76 	int irq;
77 	struct drm_display_mode mode;
78 	const struct mtk_dpi_conf *conf;
79 	enum mtk_dpi_out_color_format color_format;
80 	enum mtk_dpi_out_yc_map yc_map;
81 	enum mtk_dpi_out_bit_num bit_num;
82 	enum mtk_dpi_out_channel_swap channel_swap;
83 	struct pinctrl *pinctrl;
84 	struct pinctrl_state *pins_gpio;
85 	struct pinctrl_state *pins_dpi;
86 	u32 output_fmt;
87 	int refcount;
88 };
89 
bridge_to_dpi(struct drm_bridge * b)90 static inline struct mtk_dpi *bridge_to_dpi(struct drm_bridge *b)
91 {
92 	return container_of(b, struct mtk_dpi, bridge);
93 }
94 
95 enum mtk_dpi_polarity {
96 	MTK_DPI_POLARITY_RISING,
97 	MTK_DPI_POLARITY_FALLING,
98 };
99 
100 struct mtk_dpi_polarities {
101 	enum mtk_dpi_polarity de_pol;
102 	enum mtk_dpi_polarity ck_pol;
103 	enum mtk_dpi_polarity hsync_pol;
104 	enum mtk_dpi_polarity vsync_pol;
105 };
106 
107 struct mtk_dpi_sync_param {
108 	u32 sync_width;
109 	u32 front_porch;
110 	u32 back_porch;
111 	bool shift_half_line;
112 };
113 
114 struct mtk_dpi_yc_limit {
115 	u16 y_top;
116 	u16 y_bottom;
117 	u16 c_top;
118 	u16 c_bottom;
119 };
120 
121 struct mtk_dpi_factor {
122 	u32 clock;
123 	u8 factor;
124 };
125 
126 /**
127  * struct mtk_dpi_conf - Configuration of mediatek dpi.
128  * @dpi_factor: SoC-specific pixel clock PLL factor values.
129  * @num_dpi_factor: Number of pixel clock PLL factor values.
130  * @reg_h_fre_con: Register address of frequency control.
131  * @max_clock_khz: Max clock frequency supported for this SoCs in khz units.
132  * @edge_sel_en: Enable of edge selection.
133  * @output_fmts: Array of supported output formats.
134  * @num_output_fmts: Quantity of supported output formats.
135  * @is_ck_de_pol: Support CK/DE polarity.
136  * @swap_input_support: Support input swap function.
137  * @support_direct_pin: IP supports direct connection to dpi panels.
138  * @dimension_mask: Mask used for HWIDTH, HPORCH, VSYNC_WIDTH and VSYNC_PORCH
139  *		    (no shift).
140  * @hvsize_mask: Mask of HSIZE and VSIZE mask (no shift).
141  * @channel_swap_shift: Shift value of channel swap.
142  * @yuv422_en_bit: Enable bit of yuv422.
143  * @csc_enable_bit: Enable bit of CSC.
144  * @input_2p_en_bit: Enable bit for input two pixel per round feature.
145  *		     If present, implies that the feature must be enabled.
146  * @pixels_per_iter: Quantity of transferred pixels per iteration.
147  * @edge_cfg_in_mmsys: If the edge configuration for DPI's output needs to be set in MMSYS.
148  * @clocked_by_hdmi: HDMI IP outputs clock to dpi_pixel_clk input clock, needed
149  *		     for DPI registers access.
150  * @output_1pixel: Enable outputting one pixel per round; if the input is two pixel per
151  *                 round, the DPI hardware will internally transform it to 1T1P.
152  */
153 struct mtk_dpi_conf {
154 	const struct mtk_dpi_factor *dpi_factor;
155 	const u8 num_dpi_factor;
156 	u32 reg_h_fre_con;
157 	u32 max_clock_khz;
158 	bool edge_sel_en;
159 	const u32 *output_fmts;
160 	u32 num_output_fmts;
161 	bool is_ck_de_pol;
162 	bool swap_input_support;
163 	bool support_direct_pin;
164 	u32 dimension_mask;
165 	u32 hvsize_mask;
166 	u32 channel_swap_shift;
167 	u32 yuv422_en_bit;
168 	u32 csc_enable_bit;
169 	u32 input_2p_en_bit;
170 	u32 pixels_per_iter;
171 	bool edge_cfg_in_mmsys;
172 	bool clocked_by_hdmi;
173 	bool output_1pixel;
174 };
175 
mtk_dpi_mask(struct mtk_dpi * dpi,u32 offset,u32 val,u32 mask)176 static void mtk_dpi_mask(struct mtk_dpi *dpi, u32 offset, u32 val, u32 mask)
177 {
178 	u32 tmp = readl(dpi->regs + offset) & ~mask;
179 
180 	tmp |= (val & mask);
181 	writel(tmp, dpi->regs + offset);
182 }
183 
mtk_dpi_test_pattern_en(struct mtk_dpi * dpi,u8 type,bool enable)184 static void mtk_dpi_test_pattern_en(struct mtk_dpi *dpi, u8 type, bool enable)
185 {
186 	u32 val;
187 
188 	if (enable)
189 		val = FIELD_PREP(DPI_PAT_SEL, type) | DPI_PAT_EN;
190 	else
191 		val = 0;
192 
193 	mtk_dpi_mask(dpi, DPI_PATTERN0, val, DPI_PAT_SEL | DPI_PAT_EN);
194 }
195 
mtk_dpi_sw_reset(struct mtk_dpi * dpi,bool reset)196 static void mtk_dpi_sw_reset(struct mtk_dpi *dpi, bool reset)
197 {
198 	mtk_dpi_mask(dpi, DPI_RET, reset ? RST : 0, RST);
199 }
200 
mtk_dpi_enable(struct mtk_dpi * dpi)201 static void mtk_dpi_enable(struct mtk_dpi *dpi)
202 {
203 	mtk_dpi_mask(dpi, DPI_EN, EN, EN);
204 }
205 
mtk_dpi_disable(struct mtk_dpi * dpi)206 static void mtk_dpi_disable(struct mtk_dpi *dpi)
207 {
208 	mtk_dpi_mask(dpi, DPI_EN, 0, EN);
209 }
210 
mtk_dpi_config_hsync(struct mtk_dpi * dpi,struct mtk_dpi_sync_param * sync)211 static void mtk_dpi_config_hsync(struct mtk_dpi *dpi,
212 				 struct mtk_dpi_sync_param *sync)
213 {
214 	mtk_dpi_mask(dpi, DPI_TGEN_HWIDTH, sync->sync_width << HPW,
215 		     dpi->conf->dimension_mask << HPW);
216 	mtk_dpi_mask(dpi, DPI_TGEN_HPORCH, sync->back_porch << HBP,
217 		     dpi->conf->dimension_mask << HBP);
218 	mtk_dpi_mask(dpi, DPI_TGEN_HPORCH, sync->front_porch << HFP,
219 		     dpi->conf->dimension_mask << HFP);
220 }
221 
mtk_dpi_config_vsync(struct mtk_dpi * dpi,struct mtk_dpi_sync_param * sync,u32 width_addr,u32 porch_addr)222 static void mtk_dpi_config_vsync(struct mtk_dpi *dpi,
223 				 struct mtk_dpi_sync_param *sync,
224 				 u32 width_addr, u32 porch_addr)
225 {
226 	mtk_dpi_mask(dpi, width_addr,
227 		     sync->shift_half_line << VSYNC_HALF_LINE_SHIFT,
228 		     VSYNC_HALF_LINE_MASK);
229 	mtk_dpi_mask(dpi, width_addr,
230 		     sync->sync_width << VSYNC_WIDTH_SHIFT,
231 		     dpi->conf->dimension_mask << VSYNC_WIDTH_SHIFT);
232 	mtk_dpi_mask(dpi, porch_addr,
233 		     sync->back_porch << VSYNC_BACK_PORCH_SHIFT,
234 		     dpi->conf->dimension_mask << VSYNC_BACK_PORCH_SHIFT);
235 	mtk_dpi_mask(dpi, porch_addr,
236 		     sync->front_porch << VSYNC_FRONT_PORCH_SHIFT,
237 		     dpi->conf->dimension_mask << VSYNC_FRONT_PORCH_SHIFT);
238 }
239 
mtk_dpi_config_vsync_lodd(struct mtk_dpi * dpi,struct mtk_dpi_sync_param * sync)240 static void mtk_dpi_config_vsync_lodd(struct mtk_dpi *dpi,
241 				      struct mtk_dpi_sync_param *sync)
242 {
243 	mtk_dpi_config_vsync(dpi, sync, DPI_TGEN_VWIDTH, DPI_TGEN_VPORCH);
244 }
245 
mtk_dpi_config_vsync_leven(struct mtk_dpi * dpi,struct mtk_dpi_sync_param * sync)246 static void mtk_dpi_config_vsync_leven(struct mtk_dpi *dpi,
247 				       struct mtk_dpi_sync_param *sync)
248 {
249 	mtk_dpi_config_vsync(dpi, sync, DPI_TGEN_VWIDTH_LEVEN,
250 			     DPI_TGEN_VPORCH_LEVEN);
251 }
252 
mtk_dpi_config_vsync_rodd(struct mtk_dpi * dpi,struct mtk_dpi_sync_param * sync)253 static void mtk_dpi_config_vsync_rodd(struct mtk_dpi *dpi,
254 				      struct mtk_dpi_sync_param *sync)
255 {
256 	mtk_dpi_config_vsync(dpi, sync, DPI_TGEN_VWIDTH_RODD,
257 			     DPI_TGEN_VPORCH_RODD);
258 }
259 
mtk_dpi_config_vsync_reven(struct mtk_dpi * dpi,struct mtk_dpi_sync_param * sync)260 static void mtk_dpi_config_vsync_reven(struct mtk_dpi *dpi,
261 				       struct mtk_dpi_sync_param *sync)
262 {
263 	mtk_dpi_config_vsync(dpi, sync, DPI_TGEN_VWIDTH_REVEN,
264 			     DPI_TGEN_VPORCH_REVEN);
265 }
266 
mtk_dpi_config_pol(struct mtk_dpi * dpi,struct mtk_dpi_polarities * dpi_pol)267 static void mtk_dpi_config_pol(struct mtk_dpi *dpi,
268 			       struct mtk_dpi_polarities *dpi_pol)
269 {
270 	unsigned int pol;
271 	unsigned int mask;
272 
273 	mask = HSYNC_POL | VSYNC_POL;
274 	pol = (dpi_pol->hsync_pol == MTK_DPI_POLARITY_RISING ? 0 : HSYNC_POL) |
275 	      (dpi_pol->vsync_pol == MTK_DPI_POLARITY_RISING ? 0 : VSYNC_POL);
276 	if (dpi->conf->is_ck_de_pol) {
277 		mask |= CK_POL | DE_POL;
278 		pol |= (dpi_pol->ck_pol == MTK_DPI_POLARITY_RISING ?
279 			0 : CK_POL) |
280 		       (dpi_pol->de_pol == MTK_DPI_POLARITY_RISING ?
281 			0 : DE_POL);
282 	}
283 
284 	mtk_dpi_mask(dpi, DPI_OUTPUT_SETTING, pol, mask);
285 }
286 
mtk_dpi_config_3d(struct mtk_dpi * dpi,bool en_3d)287 static void mtk_dpi_config_3d(struct mtk_dpi *dpi, bool en_3d)
288 {
289 	mtk_dpi_mask(dpi, DPI_CON, en_3d ? TDFP_EN : 0, TDFP_EN);
290 }
291 
mtk_dpi_config_interface(struct mtk_dpi * dpi,bool inter)292 static void mtk_dpi_config_interface(struct mtk_dpi *dpi, bool inter)
293 {
294 	mtk_dpi_mask(dpi, DPI_CON, inter ? INTL_EN : 0, INTL_EN);
295 }
296 
mtk_dpi_config_fb_size(struct mtk_dpi * dpi,u32 width,u32 height)297 static void mtk_dpi_config_fb_size(struct mtk_dpi *dpi, u32 width, u32 height)
298 {
299 	mtk_dpi_mask(dpi, DPI_SIZE, width << HSIZE,
300 		     dpi->conf->hvsize_mask << HSIZE);
301 	mtk_dpi_mask(dpi, DPI_SIZE, height << VSIZE,
302 		     dpi->conf->hvsize_mask << VSIZE);
303 }
304 
mtk_dpi_config_channel_limit(struct mtk_dpi * dpi)305 static void mtk_dpi_config_channel_limit(struct mtk_dpi *dpi)
306 {
307 	struct mtk_dpi_yc_limit limit;
308 
309 	if (drm_default_rgb_quant_range(&dpi->mode) ==
310 	    HDMI_QUANTIZATION_RANGE_LIMITED) {
311 		limit.y_bottom = 0x10;
312 		limit.y_top = 0xfe0;
313 		limit.c_bottom = 0x10;
314 		limit.c_top = 0xfe0;
315 	} else {
316 		limit.y_bottom = 0;
317 		limit.y_top = 0xfff;
318 		limit.c_bottom = 0;
319 		limit.c_top = 0xfff;
320 	}
321 
322 	mtk_dpi_mask(dpi, DPI_Y_LIMIT, limit.y_bottom << Y_LIMINT_BOT,
323 		     Y_LIMINT_BOT_MASK);
324 	mtk_dpi_mask(dpi, DPI_Y_LIMIT, limit.y_top << Y_LIMINT_TOP,
325 		     Y_LIMINT_TOP_MASK);
326 	mtk_dpi_mask(dpi, DPI_C_LIMIT, limit.c_bottom << C_LIMIT_BOT,
327 		     C_LIMIT_BOT_MASK);
328 	mtk_dpi_mask(dpi, DPI_C_LIMIT, limit.c_top << C_LIMIT_TOP,
329 		     C_LIMIT_TOP_MASK);
330 }
331 
mtk_dpi_config_bit_num(struct mtk_dpi * dpi,enum mtk_dpi_out_bit_num num)332 static void mtk_dpi_config_bit_num(struct mtk_dpi *dpi,
333 				   enum mtk_dpi_out_bit_num num)
334 {
335 	u32 val;
336 
337 	switch (num) {
338 	case MTK_DPI_OUT_BIT_NUM_8BITS:
339 		val = OUT_BIT_8;
340 		break;
341 	case MTK_DPI_OUT_BIT_NUM_10BITS:
342 		val = OUT_BIT_10;
343 		break;
344 	case MTK_DPI_OUT_BIT_NUM_12BITS:
345 		val = OUT_BIT_12;
346 		break;
347 	case MTK_DPI_OUT_BIT_NUM_16BITS:
348 		val = OUT_BIT_16;
349 		break;
350 	default:
351 		val = OUT_BIT_8;
352 		break;
353 	}
354 	mtk_dpi_mask(dpi, DPI_OUTPUT_SETTING, val << OUT_BIT,
355 		     OUT_BIT_MASK);
356 }
357 
mtk_dpi_config_yc_map(struct mtk_dpi * dpi,enum mtk_dpi_out_yc_map map)358 static void mtk_dpi_config_yc_map(struct mtk_dpi *dpi,
359 				  enum mtk_dpi_out_yc_map map)
360 {
361 	u32 val;
362 
363 	switch (map) {
364 	case MTK_DPI_OUT_YC_MAP_RGB:
365 		val = YC_MAP_RGB;
366 		break;
367 	case MTK_DPI_OUT_YC_MAP_CYCY:
368 		val = YC_MAP_CYCY;
369 		break;
370 	case MTK_DPI_OUT_YC_MAP_YCYC:
371 		val = YC_MAP_YCYC;
372 		break;
373 	case MTK_DPI_OUT_YC_MAP_CY:
374 		val = YC_MAP_CY;
375 		break;
376 	case MTK_DPI_OUT_YC_MAP_YC:
377 		val = YC_MAP_YC;
378 		break;
379 	default:
380 		val = YC_MAP_RGB;
381 		break;
382 	}
383 
384 	mtk_dpi_mask(dpi, DPI_OUTPUT_SETTING, val << YC_MAP, YC_MAP_MASK);
385 }
386 
mtk_dpi_config_channel_swap(struct mtk_dpi * dpi,enum mtk_dpi_out_channel_swap swap)387 static void mtk_dpi_config_channel_swap(struct mtk_dpi *dpi,
388 					enum mtk_dpi_out_channel_swap swap)
389 {
390 	u32 val;
391 
392 	switch (swap) {
393 	case MTK_DPI_OUT_CHANNEL_SWAP_RGB:
394 		val = SWAP_RGB;
395 		break;
396 	case MTK_DPI_OUT_CHANNEL_SWAP_GBR:
397 		val = SWAP_GBR;
398 		break;
399 	case MTK_DPI_OUT_CHANNEL_SWAP_BRG:
400 		val = SWAP_BRG;
401 		break;
402 	case MTK_DPI_OUT_CHANNEL_SWAP_RBG:
403 		val = SWAP_RBG;
404 		break;
405 	case MTK_DPI_OUT_CHANNEL_SWAP_GRB:
406 		val = SWAP_GRB;
407 		break;
408 	case MTK_DPI_OUT_CHANNEL_SWAP_BGR:
409 		val = SWAP_BGR;
410 		break;
411 	default:
412 		val = SWAP_RGB;
413 		break;
414 	}
415 
416 	mtk_dpi_mask(dpi, DPI_OUTPUT_SETTING,
417 		     val << dpi->conf->channel_swap_shift,
418 		     CH_SWAP_MASK << dpi->conf->channel_swap_shift);
419 }
420 
mtk_dpi_config_yuv422_enable(struct mtk_dpi * dpi,bool enable)421 static void mtk_dpi_config_yuv422_enable(struct mtk_dpi *dpi, bool enable)
422 {
423 	mtk_dpi_mask(dpi, DPI_CON, enable ? dpi->conf->yuv422_en_bit : 0,
424 		     dpi->conf->yuv422_en_bit);
425 }
426 
mtk_dpi_config_csc_enable(struct mtk_dpi * dpi,bool enable)427 static void mtk_dpi_config_csc_enable(struct mtk_dpi *dpi, bool enable)
428 {
429 	mtk_dpi_mask(dpi, DPI_CON, enable ? dpi->conf->csc_enable_bit : 0,
430 		     dpi->conf->csc_enable_bit);
431 }
432 
mtk_dpi_config_swap_input(struct mtk_dpi * dpi,bool enable)433 static void mtk_dpi_config_swap_input(struct mtk_dpi *dpi, bool enable)
434 {
435 	mtk_dpi_mask(dpi, DPI_CON, enable ? IN_RB_SWAP : 0, IN_RB_SWAP);
436 }
437 
mtk_dpi_config_2n_h_fre(struct mtk_dpi * dpi)438 static void mtk_dpi_config_2n_h_fre(struct mtk_dpi *dpi)
439 {
440 	if (dpi->conf->reg_h_fre_con)
441 		mtk_dpi_mask(dpi, dpi->conf->reg_h_fre_con, H_FRE_2N, H_FRE_2N);
442 }
443 
mtk_dpi_config_disable_edge(struct mtk_dpi * dpi)444 static void mtk_dpi_config_disable_edge(struct mtk_dpi *dpi)
445 {
446 	if (dpi->conf->edge_sel_en && dpi->conf->reg_h_fre_con)
447 		mtk_dpi_mask(dpi, dpi->conf->reg_h_fre_con, 0, EDGE_SEL_EN);
448 }
449 
mtk_dpi_config_color_format(struct mtk_dpi * dpi,enum mtk_dpi_out_color_format format)450 static void mtk_dpi_config_color_format(struct mtk_dpi *dpi,
451 					enum mtk_dpi_out_color_format format)
452 {
453 	mtk_dpi_config_channel_swap(dpi, MTK_DPI_OUT_CHANNEL_SWAP_RGB);
454 
455 	if (format == MTK_DPI_COLOR_FORMAT_YCBCR_422) {
456 		mtk_dpi_config_yuv422_enable(dpi, true);
457 		mtk_dpi_config_csc_enable(dpi, true);
458 
459 		/*
460 		 * If height is smaller than 720, we need to use RGB_TO_BT601
461 		 * to transfer to yuv422. Otherwise, we use RGB_TO_JPEG.
462 		 */
463 		mtk_dpi_mask(dpi, DPI_MATRIX_SET, dpi->mode.hdisplay <= 720 ?
464 			     MATRIX_SEL_RGB_TO_BT601 : MATRIX_SEL_RGB_TO_JPEG,
465 			     INT_MATRIX_SEL_MASK);
466 	} else {
467 		mtk_dpi_config_yuv422_enable(dpi, false);
468 		mtk_dpi_config_csc_enable(dpi, false);
469 		if (dpi->conf->swap_input_support)
470 			mtk_dpi_config_swap_input(dpi, false);
471 	}
472 }
473 
mtk_dpi_dual_edge(struct mtk_dpi * dpi)474 static void mtk_dpi_dual_edge(struct mtk_dpi *dpi)
475 {
476 	if ((dpi->output_fmt == MEDIA_BUS_FMT_RGB888_2X12_LE) ||
477 	    (dpi->output_fmt == MEDIA_BUS_FMT_RGB888_2X12_BE)) {
478 		mtk_dpi_mask(dpi, DPI_DDR_SETTING, DDR_EN | DDR_4PHASE,
479 			     DDR_EN | DDR_4PHASE);
480 		mtk_dpi_mask(dpi, DPI_OUTPUT_SETTING,
481 			     dpi->output_fmt == MEDIA_BUS_FMT_RGB888_2X12_LE ?
482 			     EDGE_SEL : 0, EDGE_SEL);
483 		if (dpi->conf->edge_cfg_in_mmsys)
484 			mtk_mmsys_ddp_dpi_fmt_config(dpi->mmsys_dev, MTK_DPI_RGB888_DDR_CON);
485 	} else {
486 		mtk_dpi_mask(dpi, DPI_DDR_SETTING, DDR_EN | DDR_4PHASE, 0);
487 		if (dpi->conf->edge_cfg_in_mmsys)
488 			mtk_mmsys_ddp_dpi_fmt_config(dpi->mmsys_dev, MTK_DPI_RGB888_SDR_CON);
489 	}
490 }
491 
mtk_dpi_power_off(struct mtk_dpi * dpi)492 static void mtk_dpi_power_off(struct mtk_dpi *dpi)
493 {
494 	if (WARN_ON(dpi->refcount == 0))
495 		return;
496 
497 	if (--dpi->refcount != 0)
498 		return;
499 
500 	mtk_dpi_disable(dpi);
501 	clk_disable_unprepare(dpi->pixel_clk);
502 	clk_disable_unprepare(dpi->tvd_clk);
503 	clk_disable_unprepare(dpi->engine_clk);
504 }
505 
mtk_dpi_power_on(struct mtk_dpi * dpi)506 static int mtk_dpi_power_on(struct mtk_dpi *dpi)
507 {
508 	int ret;
509 
510 	if (++dpi->refcount != 1)
511 		return 0;
512 
513 	ret = clk_prepare_enable(dpi->engine_clk);
514 	if (ret) {
515 		dev_err(dpi->dev, "Failed to enable engine clock: %d\n", ret);
516 		goto err_refcount;
517 	}
518 
519 	ret = clk_prepare_enable(dpi->tvd_clk);
520 	if (ret) {
521 		dev_err(dpi->dev, "Failed to enable tvd pll: %d\n", ret);
522 		goto err_engine;
523 	}
524 
525 	ret = clk_prepare_enable(dpi->pixel_clk);
526 	if (ret) {
527 		dev_err(dpi->dev, "Failed to enable pixel clock: %d\n", ret);
528 		goto err_pixel;
529 	}
530 
531 	return 0;
532 
533 err_pixel:
534 	clk_disable_unprepare(dpi->tvd_clk);
535 err_engine:
536 	clk_disable_unprepare(dpi->engine_clk);
537 err_refcount:
538 	dpi->refcount--;
539 	return ret;
540 }
541 
mtk_dpi_calculate_factor(struct mtk_dpi * dpi,int mode_clk)542 static unsigned int mtk_dpi_calculate_factor(struct mtk_dpi *dpi, int mode_clk)
543 {
544 	const struct mtk_dpi_factor *dpi_factor = dpi->conf->dpi_factor;
545 	int i;
546 
547 	for (i = 0; i < dpi->conf->num_dpi_factor; i++) {
548 		if (mode_clk <= dpi_factor[i].clock)
549 			return dpi_factor[i].factor;
550 	}
551 
552 	/* If no match try the lowest possible factor */
553 	return dpi_factor[dpi->conf->num_dpi_factor - 1].factor;
554 }
555 
mtk_dpi_set_pixel_clk(struct mtk_dpi * dpi,struct videomode * vm,int mode_clk)556 static void mtk_dpi_set_pixel_clk(struct mtk_dpi *dpi, struct videomode *vm, int mode_clk)
557 {
558 	unsigned long pll_rate;
559 	unsigned int factor;
560 
561 	/* let pll_rate can fix the valid range of tvdpll (1G~2GHz) */
562 	factor = mtk_dpi_calculate_factor(dpi, mode_clk);
563 	pll_rate = vm->pixelclock * factor;
564 
565 	dev_dbg(dpi->dev, "Want PLL %lu Hz, pixel clock %lu Hz\n",
566 		pll_rate, vm->pixelclock);
567 
568 	clk_set_rate(dpi->tvd_clk, pll_rate);
569 	pll_rate = clk_get_rate(dpi->tvd_clk);
570 
571 	/*
572 	 * Depending on the IP version, we may output a different amount of
573 	 * pixels for each iteration: divide the clock by this number and
574 	 * adjust the display porches accordingly.
575 	 */
576 	vm->pixelclock = pll_rate / factor;
577 	vm->pixelclock /= dpi->conf->pixels_per_iter;
578 
579 	if ((dpi->output_fmt == MEDIA_BUS_FMT_RGB888_2X12_LE) ||
580 	    (dpi->output_fmt == MEDIA_BUS_FMT_RGB888_2X12_BE))
581 		clk_set_rate(dpi->pixel_clk, vm->pixelclock * 2);
582 	else
583 		clk_set_rate(dpi->pixel_clk, vm->pixelclock);
584 
585 	vm->pixelclock = clk_get_rate(dpi->pixel_clk);
586 
587 	dev_dbg(dpi->dev, "Got  PLL %lu Hz, pixel clock %lu Hz\n",
588 		pll_rate, vm->pixelclock);
589 }
590 
mtk_dpi_set_display_mode(struct mtk_dpi * dpi,struct drm_display_mode * mode)591 static int mtk_dpi_set_display_mode(struct mtk_dpi *dpi,
592 				    struct drm_display_mode *mode)
593 {
594 	struct mtk_dpi_polarities dpi_pol;
595 	struct mtk_dpi_sync_param hsync;
596 	struct mtk_dpi_sync_param vsync_lodd = { 0 };
597 	struct mtk_dpi_sync_param vsync_leven = { 0 };
598 	struct mtk_dpi_sync_param vsync_rodd = { 0 };
599 	struct mtk_dpi_sync_param vsync_reven = { 0 };
600 	struct videomode vm = { 0 };
601 
602 	drm_display_mode_to_videomode(mode, &vm);
603 
604 	if (!dpi->conf->clocked_by_hdmi)
605 		mtk_dpi_set_pixel_clk(dpi, &vm, mode->clock);
606 
607 	dpi_pol.ck_pol = MTK_DPI_POLARITY_FALLING;
608 	dpi_pol.de_pol = MTK_DPI_POLARITY_RISING;
609 	dpi_pol.hsync_pol = vm.flags & DISPLAY_FLAGS_HSYNC_HIGH ?
610 			    MTK_DPI_POLARITY_FALLING : MTK_DPI_POLARITY_RISING;
611 	dpi_pol.vsync_pol = vm.flags & DISPLAY_FLAGS_VSYNC_HIGH ?
612 			    MTK_DPI_POLARITY_FALLING : MTK_DPI_POLARITY_RISING;
613 
614 	/*
615 	 * Depending on the IP version, we may output a different amount of
616 	 * pixels for each iteration: divide the clock by this number and
617 	 * adjust the display porches accordingly.
618 	 */
619 	hsync.sync_width = vm.hsync_len / dpi->conf->pixels_per_iter;
620 	hsync.back_porch = vm.hback_porch / dpi->conf->pixels_per_iter;
621 	hsync.front_porch = vm.hfront_porch / dpi->conf->pixels_per_iter;
622 
623 	hsync.shift_half_line = false;
624 	vsync_lodd.sync_width = vm.vsync_len;
625 	vsync_lodd.back_porch = vm.vback_porch;
626 	vsync_lodd.front_porch = vm.vfront_porch;
627 	vsync_lodd.shift_half_line = false;
628 
629 	if (vm.flags & DISPLAY_FLAGS_INTERLACED &&
630 	    mode->flags & DRM_MODE_FLAG_3D_MASK) {
631 		vsync_leven = vsync_lodd;
632 		vsync_rodd = vsync_lodd;
633 		vsync_reven = vsync_lodd;
634 		vsync_leven.shift_half_line = true;
635 		vsync_reven.shift_half_line = true;
636 	} else if (vm.flags & DISPLAY_FLAGS_INTERLACED &&
637 		   !(mode->flags & DRM_MODE_FLAG_3D_MASK)) {
638 		vsync_leven = vsync_lodd;
639 		vsync_leven.shift_half_line = true;
640 	} else if (!(vm.flags & DISPLAY_FLAGS_INTERLACED) &&
641 		   mode->flags & DRM_MODE_FLAG_3D_MASK) {
642 		vsync_rodd = vsync_lodd;
643 	}
644 	mtk_dpi_sw_reset(dpi, true);
645 	mtk_dpi_config_pol(dpi, &dpi_pol);
646 
647 	mtk_dpi_config_hsync(dpi, &hsync);
648 	mtk_dpi_config_vsync_lodd(dpi, &vsync_lodd);
649 	mtk_dpi_config_vsync_rodd(dpi, &vsync_rodd);
650 	mtk_dpi_config_vsync_leven(dpi, &vsync_leven);
651 	mtk_dpi_config_vsync_reven(dpi, &vsync_reven);
652 
653 	mtk_dpi_config_3d(dpi, !!(mode->flags & DRM_MODE_FLAG_3D_MASK));
654 	mtk_dpi_config_interface(dpi, !!(vm.flags &
655 					 DISPLAY_FLAGS_INTERLACED));
656 	if (vm.flags & DISPLAY_FLAGS_INTERLACED)
657 		mtk_dpi_config_fb_size(dpi, vm.hactive, vm.vactive >> 1);
658 	else
659 		mtk_dpi_config_fb_size(dpi, vm.hactive, vm.vactive);
660 
661 	mtk_dpi_config_channel_limit(dpi);
662 	mtk_dpi_config_bit_num(dpi, dpi->bit_num);
663 	mtk_dpi_config_channel_swap(dpi, dpi->channel_swap);
664 	mtk_dpi_config_color_format(dpi, dpi->color_format);
665 	if (dpi->conf->support_direct_pin) {
666 		mtk_dpi_config_yc_map(dpi, dpi->yc_map);
667 		mtk_dpi_config_2n_h_fre(dpi);
668 
669 		/* DPI can connect to either an external bridge or the internal HDMI encoder */
670 		if (dpi->conf->output_1pixel)
671 			mtk_dpi_mask(dpi, DPI_CON, DPI_OUTPUT_1T1P_EN, DPI_OUTPUT_1T1P_EN);
672 		else
673 			mtk_dpi_dual_edge(dpi);
674 
675 		mtk_dpi_config_disable_edge(dpi);
676 	}
677 	if (dpi->conf->input_2p_en_bit) {
678 		mtk_dpi_mask(dpi, DPI_CON, dpi->conf->input_2p_en_bit,
679 			     dpi->conf->input_2p_en_bit);
680 	}
681 	mtk_dpi_sw_reset(dpi, false);
682 
683 	return 0;
684 }
685 
mtk_dpi_bridge_atomic_get_output_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,unsigned int * num_output_fmts)686 static u32 *mtk_dpi_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,
687 						      struct drm_bridge_state *bridge_state,
688 						      struct drm_crtc_state *crtc_state,
689 						      struct drm_connector_state *conn_state,
690 						      unsigned int *num_output_fmts)
691 {
692 	struct mtk_dpi *dpi = bridge_to_dpi(bridge);
693 	u32 *output_fmts;
694 
695 	*num_output_fmts = 0;
696 
697 	if (!dpi->conf->output_fmts) {
698 		dev_err(dpi->dev, "output_fmts should not be null\n");
699 		return NULL;
700 	}
701 
702 	output_fmts = kcalloc(dpi->conf->num_output_fmts, sizeof(*output_fmts),
703 			     GFP_KERNEL);
704 	if (!output_fmts)
705 		return NULL;
706 
707 	*num_output_fmts = dpi->conf->num_output_fmts;
708 
709 	memcpy(output_fmts, dpi->conf->output_fmts,
710 	       sizeof(*output_fmts) * dpi->conf->num_output_fmts);
711 
712 	return output_fmts;
713 }
714 
mtk_dpi_bridge_atomic_get_input_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,u32 output_fmt,unsigned int * num_input_fmts)715 static u32 *mtk_dpi_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
716 						     struct drm_bridge_state *bridge_state,
717 						     struct drm_crtc_state *crtc_state,
718 						     struct drm_connector_state *conn_state,
719 						     u32 output_fmt,
720 						     unsigned int *num_input_fmts)
721 {
722 	u32 *input_fmts;
723 
724 	*num_input_fmts = 0;
725 
726 	input_fmts = kcalloc(1, sizeof(*input_fmts),
727 			     GFP_KERNEL);
728 	if (!input_fmts)
729 		return NULL;
730 
731 	*num_input_fmts = 1;
732 	input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
733 
734 	return input_fmts;
735 }
736 
mtk_dpi_bridge_atomic_check(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)737 static int mtk_dpi_bridge_atomic_check(struct drm_bridge *bridge,
738 				       struct drm_bridge_state *bridge_state,
739 				       struct drm_crtc_state *crtc_state,
740 				       struct drm_connector_state *conn_state)
741 {
742 	struct mtk_dpi *dpi = bridge_to_dpi(bridge);
743 	unsigned int out_bus_format;
744 
745 	out_bus_format = bridge_state->output_bus_cfg.format;
746 
747 	if (out_bus_format == MEDIA_BUS_FMT_FIXED)
748 		if (dpi->conf->num_output_fmts)
749 			out_bus_format = dpi->conf->output_fmts[0];
750 
751 	dev_dbg(dpi->dev, "input format 0x%04x, output format 0x%04x\n",
752 		bridge_state->input_bus_cfg.format,
753 		bridge_state->output_bus_cfg.format);
754 
755 	dpi->output_fmt = out_bus_format;
756 	dpi->bit_num = MTK_DPI_OUT_BIT_NUM_8BITS;
757 	dpi->channel_swap = MTK_DPI_OUT_CHANNEL_SWAP_RGB;
758 	dpi->yc_map = MTK_DPI_OUT_YC_MAP_RGB;
759 	if (out_bus_format == MEDIA_BUS_FMT_YUYV8_1X16)
760 		dpi->color_format = MTK_DPI_COLOR_FORMAT_YCBCR_422;
761 	else
762 		dpi->color_format = MTK_DPI_COLOR_FORMAT_RGB;
763 
764 	return 0;
765 }
766 
mtk_dpi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)767 static int mtk_dpi_bridge_attach(struct drm_bridge *bridge,
768 				 enum drm_bridge_attach_flags flags)
769 {
770 	struct mtk_dpi *dpi = bridge_to_dpi(bridge);
771 	int ret;
772 
773 	dpi->next_bridge = devm_drm_of_get_bridge(dpi->dev, dpi->dev->of_node, 1, -1);
774 	if (IS_ERR(dpi->next_bridge)) {
775 		ret = PTR_ERR(dpi->next_bridge);
776 		if (ret == -EPROBE_DEFER)
777 			return ret;
778 
779 		/* Old devicetree has only one endpoint */
780 		dpi->next_bridge = devm_drm_of_get_bridge(dpi->dev, dpi->dev->of_node, 0, 0);
781 		if (IS_ERR(dpi->next_bridge))
782 			return dev_err_probe(dpi->dev, PTR_ERR(dpi->next_bridge),
783 					     "Failed to get bridge\n");
784 	}
785 
786 	return drm_bridge_attach(bridge->encoder, dpi->next_bridge,
787 				 &dpi->bridge, flags);
788 }
789 
mtk_dpi_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adjusted_mode)790 static void mtk_dpi_bridge_mode_set(struct drm_bridge *bridge,
791 				const struct drm_display_mode *mode,
792 				const struct drm_display_mode *adjusted_mode)
793 {
794 	struct mtk_dpi *dpi = bridge_to_dpi(bridge);
795 
796 	drm_mode_copy(&dpi->mode, adjusted_mode);
797 }
798 
mtk_dpi_bridge_disable(struct drm_bridge * bridge)799 static void mtk_dpi_bridge_disable(struct drm_bridge *bridge)
800 {
801 	struct mtk_dpi *dpi = bridge_to_dpi(bridge);
802 
803 	mtk_dpi_power_off(dpi);
804 
805 	if (dpi->pinctrl && dpi->pins_gpio)
806 		pinctrl_select_state(dpi->pinctrl, dpi->pins_gpio);
807 }
808 
mtk_dpi_bridge_enable(struct drm_bridge * bridge)809 static void mtk_dpi_bridge_enable(struct drm_bridge *bridge)
810 {
811 	struct mtk_dpi *dpi = bridge_to_dpi(bridge);
812 
813 	if (dpi->pinctrl && dpi->pins_dpi)
814 		pinctrl_select_state(dpi->pinctrl, dpi->pins_dpi);
815 
816 	mtk_dpi_power_on(dpi);
817 	mtk_dpi_set_display_mode(dpi, &dpi->mode);
818 	mtk_dpi_enable(dpi);
819 }
820 
821 static enum drm_mode_status
mtk_dpi_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)822 mtk_dpi_bridge_mode_valid(struct drm_bridge *bridge,
823 			  const struct drm_display_info *info,
824 			  const struct drm_display_mode *mode)
825 {
826 	struct mtk_dpi *dpi = bridge_to_dpi(bridge);
827 
828 	if (mode->clock > dpi->conf->max_clock_khz)
829 		return MODE_CLOCK_HIGH;
830 
831 	return MODE_OK;
832 }
833 
mtk_dpi_debug_tp_show(struct seq_file * m,void * arg)834 static int mtk_dpi_debug_tp_show(struct seq_file *m, void *arg)
835 {
836 	struct mtk_dpi *dpi = m->private;
837 	bool en;
838 	u32 val;
839 
840 	if (!dpi)
841 		return -EINVAL;
842 
843 	val = readl(dpi->regs + DPI_PATTERN0);
844 	en = val & DPI_PAT_EN;
845 	val = FIELD_GET(DPI_PAT_SEL, val);
846 
847 	seq_printf(m, "DPI Test Pattern: %s\n", en ? "Enabled" : "Disabled");
848 
849 	if (en) {
850 		seq_printf(m, "Internal pattern %d: ", val);
851 		switch (val) {
852 		case 0:
853 			seq_puts(m, "256 Vertical Gray\n");
854 			break;
855 		case 1:
856 			seq_puts(m, "1024 Vertical Gray\n");
857 			break;
858 		case 2:
859 			seq_puts(m, "256 Horizontal Gray\n");
860 			break;
861 		case 3:
862 			seq_puts(m, "1024 Horizontal Gray\n");
863 			break;
864 		case 4:
865 			seq_puts(m, "Vertical Color bars\n");
866 			break;
867 		case 6:
868 			seq_puts(m, "Frame border\n");
869 			break;
870 		case 7:
871 			seq_puts(m, "Dot moire\n");
872 			break;
873 		default:
874 			seq_puts(m, "Invalid selection\n");
875 			break;
876 		}
877 	}
878 
879 	return 0;
880 }
881 
mtk_dpi_debug_tp_write(struct file * file,const char __user * ubuf,size_t len,loff_t * offp)882 static ssize_t mtk_dpi_debug_tp_write(struct file *file, const char __user *ubuf,
883 				      size_t len, loff_t *offp)
884 {
885 	struct seq_file *m = file->private_data;
886 	u32 en, type;
887 	char buf[6];
888 
889 	if (!m || !m->private || *offp || len > sizeof(buf) - 1)
890 		return -EINVAL;
891 
892 	memset(buf, 0, sizeof(buf));
893 	if (copy_from_user(buf, ubuf, len))
894 		return -EFAULT;
895 
896 	if (sscanf(buf, "%u %u", &en, &type) != 2)
897 		return -EINVAL;
898 
899 	if (en < 0 || en > 1 || type < 0 || type > 7)
900 		return -EINVAL;
901 
902 	mtk_dpi_test_pattern_en((struct mtk_dpi *)m->private, type, en);
903 	return len;
904 }
905 
mtk_dpi_debug_tp_open(struct inode * inode,struct file * file)906 static int mtk_dpi_debug_tp_open(struct inode *inode, struct file *file)
907 {
908 	return single_open(file, mtk_dpi_debug_tp_show, inode->i_private);
909 }
910 
911 static const struct file_operations mtk_dpi_debug_tp_fops = {
912 	.owner = THIS_MODULE,
913 	.open = mtk_dpi_debug_tp_open,
914 	.read = seq_read,
915 	.write = mtk_dpi_debug_tp_write,
916 	.llseek = seq_lseek,
917 	.release = single_release,
918 };
919 
mtk_dpi_debugfs_init(struct drm_bridge * bridge,struct dentry * root)920 static void mtk_dpi_debugfs_init(struct drm_bridge *bridge, struct dentry *root)
921 {
922 	struct mtk_dpi *dpi = bridge_to_dpi(bridge);
923 
924 	debugfs_create_file("dpi_test_pattern", 0640, root, dpi, &mtk_dpi_debug_tp_fops);
925 }
926 
927 static const struct drm_bridge_funcs mtk_dpi_bridge_funcs = {
928 	.attach = mtk_dpi_bridge_attach,
929 	.mode_set = mtk_dpi_bridge_mode_set,
930 	.mode_valid = mtk_dpi_bridge_mode_valid,
931 	.disable = mtk_dpi_bridge_disable,
932 	.enable = mtk_dpi_bridge_enable,
933 	.atomic_check = mtk_dpi_bridge_atomic_check,
934 	.atomic_get_output_bus_fmts = mtk_dpi_bridge_atomic_get_output_bus_fmts,
935 	.atomic_get_input_bus_fmts = mtk_dpi_bridge_atomic_get_input_bus_fmts,
936 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
937 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
938 	.atomic_reset = drm_atomic_helper_bridge_reset,
939 	.debugfs_init = mtk_dpi_debugfs_init,
940 };
941 
mtk_dpi_start(struct device * dev)942 void mtk_dpi_start(struct device *dev)
943 {
944 	struct mtk_dpi *dpi = dev_get_drvdata(dev);
945 
946 	if (!dpi->conf->clocked_by_hdmi)
947 		mtk_dpi_power_on(dpi);
948 }
949 
mtk_dpi_stop(struct device * dev)950 void mtk_dpi_stop(struct device *dev)
951 {
952 	struct mtk_dpi *dpi = dev_get_drvdata(dev);
953 
954 	if (!dpi->conf->clocked_by_hdmi)
955 		mtk_dpi_power_off(dpi);
956 }
957 
mtk_dpi_encoder_index(struct device * dev)958 unsigned int mtk_dpi_encoder_index(struct device *dev)
959 {
960 	struct mtk_dpi *dpi = dev_get_drvdata(dev);
961 	unsigned int encoder_index = drm_encoder_index(&dpi->encoder);
962 
963 	dev_dbg(dev, "encoder index:%d\n", encoder_index);
964 	return encoder_index;
965 }
966 
mtk_dpi_bind(struct device * dev,struct device * master,void * data)967 static int mtk_dpi_bind(struct device *dev, struct device *master, void *data)
968 {
969 	struct mtk_dpi *dpi = dev_get_drvdata(dev);
970 	struct drm_device *drm_dev = data;
971 	struct mtk_drm_private *priv = drm_dev->dev_private;
972 	int ret;
973 
974 	dpi->mmsys_dev = priv->mmsys_dev;
975 	ret = drm_simple_encoder_init(drm_dev, &dpi->encoder,
976 				      DRM_MODE_ENCODER_TMDS);
977 	if (ret) {
978 		dev_err(dev, "Failed to initialize decoder: %d\n", ret);
979 		return ret;
980 	}
981 
982 	ret = mtk_find_possible_crtcs(drm_dev, dpi->dev);
983 	if (ret < 0)
984 		goto err_cleanup;
985 	dpi->encoder.possible_crtcs = ret;
986 
987 	ret = drm_bridge_attach(&dpi->encoder, &dpi->bridge, NULL,
988 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
989 	if (ret)
990 		goto err_cleanup;
991 
992 	dpi->connector = drm_bridge_connector_init(drm_dev, &dpi->encoder);
993 	if (IS_ERR(dpi->connector)) {
994 		dev_err(dev, "Unable to create bridge connector\n");
995 		ret = PTR_ERR(dpi->connector);
996 		goto err_cleanup;
997 	}
998 	drm_connector_attach_encoder(dpi->connector, &dpi->encoder);
999 
1000 	return 0;
1001 
1002 err_cleanup:
1003 	drm_encoder_cleanup(&dpi->encoder);
1004 	return ret;
1005 }
1006 
mtk_dpi_unbind(struct device * dev,struct device * master,void * data)1007 static void mtk_dpi_unbind(struct device *dev, struct device *master,
1008 			   void *data)
1009 {
1010 	struct mtk_dpi *dpi = dev_get_drvdata(dev);
1011 
1012 	drm_encoder_cleanup(&dpi->encoder);
1013 }
1014 
1015 static const struct component_ops mtk_dpi_component_ops = {
1016 	.bind = mtk_dpi_bind,
1017 	.unbind = mtk_dpi_unbind,
1018 };
1019 
1020 static const u32 mt8173_output_fmts[] = {
1021 	MEDIA_BUS_FMT_RGB888_1X24,
1022 };
1023 
1024 static const u32 mt8183_output_fmts[] = {
1025 	MEDIA_BUS_FMT_RGB888_2X12_LE,
1026 	MEDIA_BUS_FMT_RGB888_2X12_BE,
1027 };
1028 
1029 static const u32 mt8195_output_fmts[] = {
1030 	MEDIA_BUS_FMT_RGB888_1X24,
1031 	MEDIA_BUS_FMT_YUYV8_1X16,
1032 };
1033 
1034 static const struct mtk_dpi_factor dpi_factor_mt2701[] = {
1035 	{ 64000, 4 }, { 128000, 2 }, { U32_MAX, 1 }
1036 };
1037 
1038 static const struct mtk_dpi_factor dpi_factor_mt8173[] = {
1039 	{ 27000, 48 }, { 84000, 24 }, { 167000, 12 }, { U32_MAX, 6 }
1040 };
1041 
1042 static const struct mtk_dpi_factor dpi_factor_mt8183[] = {
1043 	{ 27000, 8 }, { 167000, 4 }, { U32_MAX, 2 }
1044 };
1045 
1046 static const struct mtk_dpi_factor dpi_factor_mt8195_dp_intf[] = {
1047 	{ 70000 - 1, 4 }, { 200000 - 1, 2 }, { U32_MAX, 1 }
1048 };
1049 
1050 static const struct mtk_dpi_conf mt8173_conf = {
1051 	.dpi_factor = dpi_factor_mt8173,
1052 	.num_dpi_factor = ARRAY_SIZE(dpi_factor_mt8173),
1053 	.reg_h_fre_con = 0xe0,
1054 	.max_clock_khz = 300000,
1055 	.output_fmts = mt8173_output_fmts,
1056 	.num_output_fmts = ARRAY_SIZE(mt8173_output_fmts),
1057 	.pixels_per_iter = 1,
1058 	.is_ck_de_pol = true,
1059 	.swap_input_support = true,
1060 	.support_direct_pin = true,
1061 	.dimension_mask = HPW_MASK,
1062 	.hvsize_mask = HSIZE_MASK,
1063 	.channel_swap_shift = CH_SWAP,
1064 	.yuv422_en_bit = YUV422_EN,
1065 	.csc_enable_bit = CSC_ENABLE,
1066 };
1067 
1068 static const struct mtk_dpi_conf mt2701_conf = {
1069 	.dpi_factor = dpi_factor_mt2701,
1070 	.num_dpi_factor = ARRAY_SIZE(dpi_factor_mt2701),
1071 	.reg_h_fre_con = 0xb0,
1072 	.edge_sel_en = true,
1073 	.max_clock_khz = 150000,
1074 	.output_fmts = mt8173_output_fmts,
1075 	.num_output_fmts = ARRAY_SIZE(mt8173_output_fmts),
1076 	.pixels_per_iter = 1,
1077 	.is_ck_de_pol = true,
1078 	.swap_input_support = true,
1079 	.support_direct_pin = true,
1080 	.dimension_mask = HPW_MASK,
1081 	.hvsize_mask = HSIZE_MASK,
1082 	.channel_swap_shift = CH_SWAP,
1083 	.yuv422_en_bit = YUV422_EN,
1084 	.csc_enable_bit = CSC_ENABLE,
1085 };
1086 
1087 static const struct mtk_dpi_conf mt8183_conf = {
1088 	.dpi_factor = dpi_factor_mt8183,
1089 	.num_dpi_factor = ARRAY_SIZE(dpi_factor_mt8183),
1090 	.reg_h_fre_con = 0xe0,
1091 	.max_clock_khz = 100000,
1092 	.output_fmts = mt8183_output_fmts,
1093 	.num_output_fmts = ARRAY_SIZE(mt8183_output_fmts),
1094 	.pixels_per_iter = 1,
1095 	.is_ck_de_pol = true,
1096 	.swap_input_support = true,
1097 	.support_direct_pin = true,
1098 	.dimension_mask = HPW_MASK,
1099 	.hvsize_mask = HSIZE_MASK,
1100 	.channel_swap_shift = CH_SWAP,
1101 	.yuv422_en_bit = YUV422_EN,
1102 	.csc_enable_bit = CSC_ENABLE,
1103 };
1104 
1105 static const struct mtk_dpi_conf mt8186_conf = {
1106 	.dpi_factor = dpi_factor_mt8183,
1107 	.num_dpi_factor = ARRAY_SIZE(dpi_factor_mt8183),
1108 	.reg_h_fre_con = 0xe0,
1109 	.max_clock_khz = 150000,
1110 	.output_fmts = mt8183_output_fmts,
1111 	.num_output_fmts = ARRAY_SIZE(mt8183_output_fmts),
1112 	.edge_cfg_in_mmsys = true,
1113 	.pixels_per_iter = 1,
1114 	.is_ck_de_pol = true,
1115 	.swap_input_support = true,
1116 	.support_direct_pin = true,
1117 	.dimension_mask = HPW_MASK,
1118 	.hvsize_mask = HSIZE_MASK,
1119 	.channel_swap_shift = CH_SWAP,
1120 	.yuv422_en_bit = YUV422_EN,
1121 	.csc_enable_bit = CSC_ENABLE,
1122 };
1123 
1124 static const struct mtk_dpi_conf mt8192_conf = {
1125 	.dpi_factor = dpi_factor_mt8183,
1126 	.num_dpi_factor = ARRAY_SIZE(dpi_factor_mt8183),
1127 	.reg_h_fre_con = 0xe0,
1128 	.max_clock_khz = 150000,
1129 	.output_fmts = mt8183_output_fmts,
1130 	.num_output_fmts = ARRAY_SIZE(mt8183_output_fmts),
1131 	.pixels_per_iter = 1,
1132 	.is_ck_de_pol = true,
1133 	.swap_input_support = true,
1134 	.support_direct_pin = true,
1135 	.dimension_mask = HPW_MASK,
1136 	.hvsize_mask = HSIZE_MASK,
1137 	.channel_swap_shift = CH_SWAP,
1138 	.yuv422_en_bit = YUV422_EN,
1139 	.csc_enable_bit = CSC_ENABLE,
1140 };
1141 
1142 static const struct mtk_dpi_conf mt8195_conf = {
1143 	.max_clock_khz = 594000,
1144 	.output_fmts = mt8183_output_fmts,
1145 	.num_output_fmts = ARRAY_SIZE(mt8183_output_fmts),
1146 	.pixels_per_iter = 1,
1147 	.is_ck_de_pol = true,
1148 	.swap_input_support = true,
1149 	.support_direct_pin = true,
1150 	.dimension_mask = HPW_MASK,
1151 	.hvsize_mask = HSIZE_MASK,
1152 	.channel_swap_shift = CH_SWAP,
1153 	.yuv422_en_bit = YUV422_EN,
1154 	.csc_enable_bit = CSC_ENABLE,
1155 	.input_2p_en_bit = DPI_INPUT_2P_EN,
1156 	.clocked_by_hdmi = true,
1157 	.output_1pixel = true,
1158 };
1159 
1160 static const struct mtk_dpi_conf mt8195_dpintf_conf = {
1161 	.dpi_factor = dpi_factor_mt8195_dp_intf,
1162 	.num_dpi_factor = ARRAY_SIZE(dpi_factor_mt8195_dp_intf),
1163 	.max_clock_khz = 600000,
1164 	.output_fmts = mt8195_output_fmts,
1165 	.num_output_fmts = ARRAY_SIZE(mt8195_output_fmts),
1166 	.pixels_per_iter = 4,
1167 	.dimension_mask = DPINTF_HPW_MASK,
1168 	.hvsize_mask = DPINTF_HSIZE_MASK,
1169 	.channel_swap_shift = DPINTF_CH_SWAP,
1170 	.yuv422_en_bit = DPINTF_YUV422_EN,
1171 	.csc_enable_bit = DPINTF_CSC_ENABLE,
1172 	.input_2p_en_bit = DPINTF_INPUT_2P_EN,
1173 };
1174 
mtk_dpi_probe(struct platform_device * pdev)1175 static int mtk_dpi_probe(struct platform_device *pdev)
1176 {
1177 	struct device *dev = &pdev->dev;
1178 	struct mtk_dpi *dpi;
1179 	int ret;
1180 
1181 	dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
1182 	if (!dpi)
1183 		return -ENOMEM;
1184 
1185 	dpi->dev = dev;
1186 	dpi->conf = (struct mtk_dpi_conf *)of_device_get_match_data(dev);
1187 	dpi->output_fmt = MEDIA_BUS_FMT_RGB888_1X24;
1188 
1189 	dpi->pinctrl = devm_pinctrl_get(&pdev->dev);
1190 	if (IS_ERR(dpi->pinctrl)) {
1191 		dpi->pinctrl = NULL;
1192 		dev_dbg(&pdev->dev, "Cannot find pinctrl!\n");
1193 	}
1194 	if (dpi->pinctrl) {
1195 		dpi->pins_gpio = pinctrl_lookup_state(dpi->pinctrl, "sleep");
1196 		if (IS_ERR(dpi->pins_gpio)) {
1197 			dpi->pins_gpio = NULL;
1198 			dev_dbg(&pdev->dev, "Cannot find pinctrl idle!\n");
1199 		}
1200 		if (dpi->pins_gpio)
1201 			pinctrl_select_state(dpi->pinctrl, dpi->pins_gpio);
1202 
1203 		dpi->pins_dpi = pinctrl_lookup_state(dpi->pinctrl, "default");
1204 		if (IS_ERR(dpi->pins_dpi)) {
1205 			dpi->pins_dpi = NULL;
1206 			dev_dbg(&pdev->dev, "Cannot find pinctrl active!\n");
1207 		}
1208 	}
1209 	dpi->regs = devm_platform_ioremap_resource(pdev, 0);
1210 	if (IS_ERR(dpi->regs))
1211 		return dev_err_probe(dev, PTR_ERR(dpi->regs),
1212 				     "Failed to ioremap mem resource\n");
1213 
1214 	dpi->engine_clk = devm_clk_get(dev, "engine");
1215 	if (IS_ERR(dpi->engine_clk))
1216 		return dev_err_probe(dev, PTR_ERR(dpi->engine_clk),
1217 				     "Failed to get engine clock\n");
1218 
1219 	dpi->pixel_clk = devm_clk_get(dev, "pixel");
1220 	if (IS_ERR(dpi->pixel_clk))
1221 		return dev_err_probe(dev, PTR_ERR(dpi->pixel_clk),
1222 				     "Failed to get pixel clock\n");
1223 
1224 	dpi->tvd_clk = devm_clk_get(dev, "pll");
1225 	if (IS_ERR(dpi->tvd_clk))
1226 		return dev_err_probe(dev, PTR_ERR(dpi->tvd_clk),
1227 				     "Failed to get tvdpll clock\n");
1228 
1229 	dpi->irq = platform_get_irq(pdev, 0);
1230 	if (dpi->irq < 0)
1231 		return dpi->irq;
1232 
1233 	platform_set_drvdata(pdev, dpi);
1234 
1235 	dpi->bridge.funcs = &mtk_dpi_bridge_funcs;
1236 	dpi->bridge.of_node = dev->of_node;
1237 	dpi->bridge.type = DRM_MODE_CONNECTOR_DPI;
1238 
1239 	ret = devm_drm_bridge_add(dev, &dpi->bridge);
1240 	if (ret)
1241 		return ret;
1242 
1243 	ret = component_add(dev, &mtk_dpi_component_ops);
1244 	if (ret)
1245 		return dev_err_probe(dev, ret, "Failed to add component.\n");
1246 
1247 	return 0;
1248 }
1249 
mtk_dpi_remove(struct platform_device * pdev)1250 static void mtk_dpi_remove(struct platform_device *pdev)
1251 {
1252 	component_del(&pdev->dev, &mtk_dpi_component_ops);
1253 }
1254 
1255 static const struct of_device_id mtk_dpi_of_ids[] = {
1256 	{ .compatible = "mediatek,mt2701-dpi", .data = &mt2701_conf },
1257 	{ .compatible = "mediatek,mt8173-dpi", .data = &mt8173_conf },
1258 	{ .compatible = "mediatek,mt8183-dpi", .data = &mt8183_conf },
1259 	{ .compatible = "mediatek,mt8186-dpi", .data = &mt8186_conf },
1260 	{ .compatible = "mediatek,mt8188-dp-intf", .data = &mt8195_dpintf_conf },
1261 	{ .compatible = "mediatek,mt8192-dpi", .data = &mt8192_conf },
1262 	{ .compatible = "mediatek,mt8195-dp-intf", .data = &mt8195_dpintf_conf },
1263 	{ .compatible = "mediatek,mt8195-dpi", .data = &mt8195_conf },
1264 	{ /* sentinel */ },
1265 };
1266 MODULE_DEVICE_TABLE(of, mtk_dpi_of_ids);
1267 
1268 struct platform_driver mtk_dpi_driver = {
1269 	.probe = mtk_dpi_probe,
1270 	.remove = mtk_dpi_remove,
1271 	.driver = {
1272 		.name = "mediatek-dpi",
1273 		.of_match_table = mtk_dpi_of_ids,
1274 	},
1275 };
1276