Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2015 MediaTek Inc.
4 * Authors:
5 * YT Shen <yt.shen@mediatek.com>
6 * CK Hu <ck.hu@mediatek.com>
7 */
8
9#include <linux/clk.h>
10#include <linux/of.h>
11#include <linux/of_address.h>
12#include <linux/of_platform.h>
13#include <linux/platform_device.h>
14#include <linux/soc/mediatek/mtk-cmdq.h>
15#include <drm/drm_print.h>
16
17#include "mtk_disp_drv.h"
18#include "mtk_drm_drv.h"
19#include "mtk_drm_plane.h"
20#include "mtk_drm_ddp_comp.h"
21#include "mtk_drm_crtc.h"
22
23
24#define DISP_REG_DITHER_EN 0x0000
25#define DITHER_EN BIT(0)
26#define DISP_REG_DITHER_CFG 0x0020
27#define DITHER_RELAY_MODE BIT(0)
28#define DITHER_ENGINE_EN BIT(1)
29#define DISP_DITHERING BIT(2)
30#define DISP_REG_DITHER_SIZE 0x0030
31#define DISP_REG_DITHER_5 0x0114
32#define DISP_REG_DITHER_7 0x011c
33#define DISP_REG_DITHER_15 0x013c
34#define DITHER_LSB_ERR_SHIFT_R(x) (((x) & 0x7) << 28)
35#define DITHER_ADD_LSHIFT_R(x) (((x) & 0x7) << 20)
36#define DITHER_NEW_BIT_MODE BIT(0)
37#define DISP_REG_DITHER_16 0x0140
38#define DITHER_LSB_ERR_SHIFT_B(x) (((x) & 0x7) << 28)
39#define DITHER_ADD_LSHIFT_B(x) (((x) & 0x7) << 20)
40#define DITHER_LSB_ERR_SHIFT_G(x) (((x) & 0x7) << 12)
41#define DITHER_ADD_LSHIFT_G(x) (((x) & 0x7) << 4)
42
43#define DISP_REG_DSC_CON 0x0000
44#define DSC_EN BIT(0)
45#define DSC_DUAL_INOUT BIT(2)
46#define DSC_BYPASS BIT(4)
47#define DSC_UFOE_SEL BIT(16)
48
49#define DISP_REG_OD_EN 0x0000
50#define DISP_REG_OD_CFG 0x0020
51#define OD_RELAYMODE BIT(0)
52#define DISP_REG_OD_SIZE 0x0030
53
54#define DISP_REG_POSTMASK_EN 0x0000
55#define POSTMASK_EN BIT(0)
56#define DISP_REG_POSTMASK_CFG 0x0020
57#define POSTMASK_RELAY_MODE BIT(0)
58#define DISP_REG_POSTMASK_SIZE 0x0030
59
60#define DISP_REG_UFO_START 0x0000
61#define UFO_BYPASS BIT(2)
62
63struct mtk_ddp_comp_dev {
64 struct clk *clk;
65 void __iomem *regs;
66 struct cmdq_client_reg cmdq_reg;
67};
68
69void mtk_ddp_write(struct cmdq_pkt *cmdq_pkt, unsigned int value,
70 struct cmdq_client_reg *cmdq_reg, void __iomem *regs,
71 unsigned int offset)
72{
73#if IS_REACHABLE(CONFIG_MTK_CMDQ)
74 if (cmdq_pkt)
75 cmdq_pkt_write(cmdq_pkt, cmdq_reg->subsys,
76 cmdq_reg->offset + offset, value);
77 else
78#endif
79 writel(value, regs + offset);
80}
81
82void mtk_ddp_write_relaxed(struct cmdq_pkt *cmdq_pkt, unsigned int value,
83 struct cmdq_client_reg *cmdq_reg, void __iomem *regs,
84 unsigned int offset)
85{
86#if IS_REACHABLE(CONFIG_MTK_CMDQ)
87 if (cmdq_pkt)
88 cmdq_pkt_write(cmdq_pkt, cmdq_reg->subsys,
89 cmdq_reg->offset + offset, value);
90 else
91#endif
92 writel_relaxed(value, regs + offset);
93}
94
95void mtk_ddp_write_mask(struct cmdq_pkt *cmdq_pkt, unsigned int value,
96 struct cmdq_client_reg *cmdq_reg, void __iomem *regs,
97 unsigned int offset, unsigned int mask)
98{
99#if IS_REACHABLE(CONFIG_MTK_CMDQ)
100 if (cmdq_pkt) {
101 cmdq_pkt_write_mask(cmdq_pkt, cmdq_reg->subsys,
102 cmdq_reg->offset + offset, value, mask);
103 } else {
104#endif
105 u32 tmp = readl(regs + offset);
106
107 tmp = (tmp & ~mask) | (value & mask);
108 writel(tmp, regs + offset);
109#if IS_REACHABLE(CONFIG_MTK_CMDQ)
110 }
111#endif
112}
113
114static int mtk_ddp_clk_enable(struct device *dev)
115{
116 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
117
118 return clk_prepare_enable(priv->clk);
119}
120
121static void mtk_ddp_clk_disable(struct device *dev)
122{
123 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
124
125 clk_disable_unprepare(priv->clk);
126}
127
128void mtk_dither_set_common(void __iomem *regs, struct cmdq_client_reg *cmdq_reg,
129 unsigned int bpc, unsigned int cfg,
130 unsigned int dither_en, struct cmdq_pkt *cmdq_pkt)
131{
132 /* If bpc equal to 0, the dithering function didn't be enabled */
133 if (bpc == 0)
134 return;
135
136 if (bpc >= MTK_MIN_BPC) {
137 mtk_ddp_write(cmdq_pkt, 0, cmdq_reg, regs, DISP_REG_DITHER_5);
138 mtk_ddp_write(cmdq_pkt, 0, cmdq_reg, regs, DISP_REG_DITHER_7);
139 mtk_ddp_write(cmdq_pkt,
140 DITHER_LSB_ERR_SHIFT_R(MTK_MAX_BPC - bpc) |
141 DITHER_ADD_LSHIFT_R(MTK_MAX_BPC - bpc) |
142 DITHER_NEW_BIT_MODE,
143 cmdq_reg, regs, DISP_REG_DITHER_15);
144 mtk_ddp_write(cmdq_pkt,
145 DITHER_LSB_ERR_SHIFT_B(MTK_MAX_BPC - bpc) |
146 DITHER_ADD_LSHIFT_B(MTK_MAX_BPC - bpc) |
147 DITHER_LSB_ERR_SHIFT_G(MTK_MAX_BPC - bpc) |
148 DITHER_ADD_LSHIFT_G(MTK_MAX_BPC - bpc),
149 cmdq_reg, regs, DISP_REG_DITHER_16);
150 mtk_ddp_write(cmdq_pkt, dither_en, cmdq_reg, regs, cfg);
151 }
152}
153
154static void mtk_dither_config(struct device *dev, unsigned int w,
155 unsigned int h, unsigned int vrefresh,
156 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
157{
158 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
159
160 mtk_ddp_write(cmdq_pkt, w << 16 | h, &priv->cmdq_reg, priv->regs, DISP_REG_DITHER_SIZE);
161 mtk_ddp_write(cmdq_pkt, DITHER_RELAY_MODE, &priv->cmdq_reg, priv->regs,
162 DISP_REG_DITHER_CFG);
163 mtk_dither_set_common(priv->regs, &priv->cmdq_reg, bpc, DISP_REG_DITHER_CFG,
164 DITHER_ENGINE_EN, cmdq_pkt);
165}
166
167static void mtk_dither_start(struct device *dev)
168{
169 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
170
171 writel(DITHER_EN, priv->regs + DISP_REG_DITHER_EN);
172}
173
174static void mtk_dither_stop(struct device *dev)
175{
176 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
177
178 writel_relaxed(0x0, priv->regs + DISP_REG_DITHER_EN);
179}
180
181static void mtk_dither_set(struct device *dev, unsigned int bpc,
182 unsigned int cfg, struct cmdq_pkt *cmdq_pkt)
183{
184 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
185
186 mtk_dither_set_common(priv->regs, &priv->cmdq_reg, bpc, cfg,
187 DISP_DITHERING, cmdq_pkt);
188}
189
190static void mtk_dsc_config(struct device *dev, unsigned int w,
191 unsigned int h, unsigned int vrefresh,
192 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
193{
194 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
195
196 /* dsc bypass mode */
197 mtk_ddp_write_mask(cmdq_pkt, DSC_BYPASS, &priv->cmdq_reg, priv->regs,
198 DISP_REG_DSC_CON, DSC_BYPASS);
199 mtk_ddp_write_mask(cmdq_pkt, DSC_UFOE_SEL, &priv->cmdq_reg, priv->regs,
200 DISP_REG_DSC_CON, DSC_UFOE_SEL);
201 mtk_ddp_write_mask(cmdq_pkt, DSC_DUAL_INOUT, &priv->cmdq_reg, priv->regs,
202 DISP_REG_DSC_CON, DSC_DUAL_INOUT);
203}
204
205static void mtk_dsc_start(struct device *dev)
206{
207 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
208
209 /* write with mask to reserve the value set in mtk_dsc_config */
210 mtk_ddp_write_mask(NULL, DSC_EN, &priv->cmdq_reg, priv->regs, DISP_REG_DSC_CON, DSC_EN);
211}
212
213static void mtk_dsc_stop(struct device *dev)
214{
215 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
216
217 writel_relaxed(0x0, priv->regs + DISP_REG_DSC_CON);
218}
219
220static void mtk_od_config(struct device *dev, unsigned int w,
221 unsigned int h, unsigned int vrefresh,
222 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
223{
224 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
225
226 mtk_ddp_write(cmdq_pkt, w << 16 | h, &priv->cmdq_reg, priv->regs, DISP_REG_OD_SIZE);
227 mtk_ddp_write(cmdq_pkt, OD_RELAYMODE, &priv->cmdq_reg, priv->regs, DISP_REG_OD_CFG);
228 mtk_dither_set(dev, bpc, DISP_REG_OD_CFG, cmdq_pkt);
229}
230
231static void mtk_od_start(struct device *dev)
232{
233 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
234
235 writel(1, priv->regs + DISP_REG_OD_EN);
236}
237
238static void mtk_postmask_config(struct device *dev, unsigned int w,
239 unsigned int h, unsigned int vrefresh,
240 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
241{
242 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
243
244 mtk_ddp_write(cmdq_pkt, w << 16 | h, &priv->cmdq_reg, priv->regs,
245 DISP_REG_POSTMASK_SIZE);
246 mtk_ddp_write(cmdq_pkt, POSTMASK_RELAY_MODE, &priv->cmdq_reg,
247 priv->regs, DISP_REG_POSTMASK_CFG);
248}
249
250static void mtk_postmask_start(struct device *dev)
251{
252 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
253
254 writel(POSTMASK_EN, priv->regs + DISP_REG_POSTMASK_EN);
255}
256
257static void mtk_postmask_stop(struct device *dev)
258{
259 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
260
261 writel_relaxed(0x0, priv->regs + DISP_REG_POSTMASK_EN);
262}
263
264static void mtk_ufoe_start(struct device *dev)
265{
266 struct mtk_ddp_comp_dev *priv = dev_get_drvdata(dev);
267
268 writel(UFO_BYPASS, priv->regs + DISP_REG_UFO_START);
269}
270
271static const struct mtk_ddp_comp_funcs ddp_aal = {
272 .clk_enable = mtk_aal_clk_enable,
273 .clk_disable = mtk_aal_clk_disable,
274 .gamma_get_lut_size = mtk_aal_gamma_get_lut_size,
275 .gamma_set = mtk_aal_gamma_set,
276 .config = mtk_aal_config,
277 .start = mtk_aal_start,
278 .stop = mtk_aal_stop,
279};
280
281static const struct mtk_ddp_comp_funcs ddp_ccorr = {
282 .clk_enable = mtk_ccorr_clk_enable,
283 .clk_disable = mtk_ccorr_clk_disable,
284 .config = mtk_ccorr_config,
285 .start = mtk_ccorr_start,
286 .stop = mtk_ccorr_stop,
287 .ctm_set = mtk_ccorr_ctm_set,
288};
289
290static const struct mtk_ddp_comp_funcs ddp_color = {
291 .clk_enable = mtk_color_clk_enable,
292 .clk_disable = mtk_color_clk_disable,
293 .config = mtk_color_config,
294 .start = mtk_color_start,
295};
296
297static const struct mtk_ddp_comp_funcs ddp_dither = {
298 .clk_enable = mtk_ddp_clk_enable,
299 .clk_disable = mtk_ddp_clk_disable,
300 .config = mtk_dither_config,
301 .start = mtk_dither_start,
302 .stop = mtk_dither_stop,
303};
304
305static const struct mtk_ddp_comp_funcs ddp_dpi = {
306 .start = mtk_dpi_start,
307 .stop = mtk_dpi_stop,
308 .encoder_index = mtk_dpi_encoder_index,
309};
310
311static const struct mtk_ddp_comp_funcs ddp_dsc = {
312 .clk_enable = mtk_ddp_clk_enable,
313 .clk_disable = mtk_ddp_clk_disable,
314 .config = mtk_dsc_config,
315 .start = mtk_dsc_start,
316 .stop = mtk_dsc_stop,
317};
318
319static const struct mtk_ddp_comp_funcs ddp_dsi = {
320 .start = mtk_dsi_ddp_start,
321 .stop = mtk_dsi_ddp_stop,
322 .encoder_index = mtk_dsi_encoder_index,
323};
324
325static const struct mtk_ddp_comp_funcs ddp_gamma = {
326 .clk_enable = mtk_gamma_clk_enable,
327 .clk_disable = mtk_gamma_clk_disable,
328 .gamma_get_lut_size = mtk_gamma_get_lut_size,
329 .gamma_set = mtk_gamma_set,
330 .config = mtk_gamma_config,
331 .start = mtk_gamma_start,
332 .stop = mtk_gamma_stop,
333};
334
335static const struct mtk_ddp_comp_funcs ddp_merge = {
336 .clk_enable = mtk_merge_clk_enable,
337 .clk_disable = mtk_merge_clk_disable,
338 .start = mtk_merge_start,
339 .stop = mtk_merge_stop,
340 .config = mtk_merge_config,
341};
342
343static const struct mtk_ddp_comp_funcs ddp_od = {
344 .clk_enable = mtk_ddp_clk_enable,
345 .clk_disable = mtk_ddp_clk_disable,
346 .config = mtk_od_config,
347 .start = mtk_od_start,
348};
349
350static const struct mtk_ddp_comp_funcs ddp_ovl = {
351 .clk_enable = mtk_ovl_clk_enable,
352 .clk_disable = mtk_ovl_clk_disable,
353 .config = mtk_ovl_config,
354 .start = mtk_ovl_start,
355 .stop = mtk_ovl_stop,
356 .register_vblank_cb = mtk_ovl_register_vblank_cb,
357 .unregister_vblank_cb = mtk_ovl_unregister_vblank_cb,
358 .enable_vblank = mtk_ovl_enable_vblank,
359 .disable_vblank = mtk_ovl_disable_vblank,
360 .supported_rotations = mtk_ovl_supported_rotations,
361 .layer_nr = mtk_ovl_layer_nr,
362 .layer_check = mtk_ovl_layer_check,
363 .layer_config = mtk_ovl_layer_config,
364 .bgclr_in_on = mtk_ovl_bgclr_in_on,
365 .bgclr_in_off = mtk_ovl_bgclr_in_off,
366 .get_formats = mtk_ovl_get_formats,
367 .get_num_formats = mtk_ovl_get_num_formats,
368};
369
370static const struct mtk_ddp_comp_funcs ddp_postmask = {
371 .clk_enable = mtk_ddp_clk_enable,
372 .clk_disable = mtk_ddp_clk_disable,
373 .config = mtk_postmask_config,
374 .start = mtk_postmask_start,
375 .stop = mtk_postmask_stop,
376};
377
378static const struct mtk_ddp_comp_funcs ddp_rdma = {
379 .clk_enable = mtk_rdma_clk_enable,
380 .clk_disable = mtk_rdma_clk_disable,
381 .config = mtk_rdma_config,
382 .start = mtk_rdma_start,
383 .stop = mtk_rdma_stop,
384 .register_vblank_cb = mtk_rdma_register_vblank_cb,
385 .unregister_vblank_cb = mtk_rdma_unregister_vblank_cb,
386 .enable_vblank = mtk_rdma_enable_vblank,
387 .disable_vblank = mtk_rdma_disable_vblank,
388 .layer_nr = mtk_rdma_layer_nr,
389 .layer_config = mtk_rdma_layer_config,
390 .get_formats = mtk_rdma_get_formats,
391 .get_num_formats = mtk_rdma_get_num_formats,
392};
393
394static const struct mtk_ddp_comp_funcs ddp_ufoe = {
395 .clk_enable = mtk_ddp_clk_enable,
396 .clk_disable = mtk_ddp_clk_disable,
397 .start = mtk_ufoe_start,
398};
399
400static const struct mtk_ddp_comp_funcs ddp_ovl_adaptor = {
401 .power_on = mtk_ovl_adaptor_power_on,
402 .power_off = mtk_ovl_adaptor_power_off,
403 .clk_enable = mtk_ovl_adaptor_clk_enable,
404 .clk_disable = mtk_ovl_adaptor_clk_disable,
405 .config = mtk_ovl_adaptor_config,
406 .start = mtk_ovl_adaptor_start,
407 .stop = mtk_ovl_adaptor_stop,
408 .layer_nr = mtk_ovl_adaptor_layer_nr,
409 .layer_config = mtk_ovl_adaptor_layer_config,
410 .register_vblank_cb = mtk_ovl_adaptor_register_vblank_cb,
411 .unregister_vblank_cb = mtk_ovl_adaptor_unregister_vblank_cb,
412 .enable_vblank = mtk_ovl_adaptor_enable_vblank,
413 .disable_vblank = mtk_ovl_adaptor_disable_vblank,
414 .dma_dev_get = mtk_ovl_adaptor_dma_dev_get,
415 .connect = mtk_ovl_adaptor_connect,
416 .disconnect = mtk_ovl_adaptor_disconnect,
417 .add = mtk_ovl_adaptor_add_comp,
418 .remove = mtk_ovl_adaptor_remove_comp,
419 .get_formats = mtk_ovl_adaptor_get_formats,
420 .get_num_formats = mtk_ovl_adaptor_get_num_formats,
421};
422
423static const char * const mtk_ddp_comp_stem[MTK_DDP_COMP_TYPE_MAX] = {
424 [MTK_DISP_AAL] = "aal",
425 [MTK_DISP_BLS] = "bls",
426 [MTK_DISP_CCORR] = "ccorr",
427 [MTK_DISP_COLOR] = "color",
428 [MTK_DISP_DITHER] = "dither",
429 [MTK_DISP_DSC] = "dsc",
430 [MTK_DISP_GAMMA] = "gamma",
431 [MTK_DISP_MERGE] = "merge",
432 [MTK_DISP_MUTEX] = "mutex",
433 [MTK_DISP_OD] = "od",
434 [MTK_DISP_OVL] = "ovl",
435 [MTK_DISP_OVL_2L] = "ovl-2l",
436 [MTK_DISP_OVL_ADAPTOR] = "ovl_adaptor",
437 [MTK_DISP_POSTMASK] = "postmask",
438 [MTK_DISP_PWM] = "pwm",
439 [MTK_DISP_RDMA] = "rdma",
440 [MTK_DISP_UFOE] = "ufoe",
441 [MTK_DISP_WDMA] = "wdma",
442 [MTK_DP_INTF] = "dp-intf",
443 [MTK_DPI] = "dpi",
444 [MTK_DSI] = "dsi",
445};
446
447struct mtk_ddp_comp_match {
448 enum mtk_ddp_comp_type type;
449 int alias_id;
450 const struct mtk_ddp_comp_funcs *funcs;
451};
452
453static const struct mtk_ddp_comp_match mtk_ddp_matches[DDP_COMPONENT_DRM_ID_MAX] = {
454 [DDP_COMPONENT_AAL0] = { MTK_DISP_AAL, 0, &ddp_aal },
455 [DDP_COMPONENT_AAL1] = { MTK_DISP_AAL, 1, &ddp_aal },
456 [DDP_COMPONENT_BLS] = { MTK_DISP_BLS, 0, NULL },
457 [DDP_COMPONENT_CCORR] = { MTK_DISP_CCORR, 0, &ddp_ccorr },
458 [DDP_COMPONENT_COLOR0] = { MTK_DISP_COLOR, 0, &ddp_color },
459 [DDP_COMPONENT_COLOR1] = { MTK_DISP_COLOR, 1, &ddp_color },
460 [DDP_COMPONENT_DITHER0] = { MTK_DISP_DITHER, 0, &ddp_dither },
461 [DDP_COMPONENT_DP_INTF0] = { MTK_DP_INTF, 0, &ddp_dpi },
462 [DDP_COMPONENT_DP_INTF1] = { MTK_DP_INTF, 1, &ddp_dpi },
463 [DDP_COMPONENT_DPI0] = { MTK_DPI, 0, &ddp_dpi },
464 [DDP_COMPONENT_DPI1] = { MTK_DPI, 1, &ddp_dpi },
465 [DDP_COMPONENT_DRM_OVL_ADAPTOR] = { MTK_DISP_OVL_ADAPTOR, 0, &ddp_ovl_adaptor },
466 [DDP_COMPONENT_DSC0] = { MTK_DISP_DSC, 0, &ddp_dsc },
467 [DDP_COMPONENT_DSC1] = { MTK_DISP_DSC, 1, &ddp_dsc },
468 [DDP_COMPONENT_DSI0] = { MTK_DSI, 0, &ddp_dsi },
469 [DDP_COMPONENT_DSI1] = { MTK_DSI, 1, &ddp_dsi },
470 [DDP_COMPONENT_DSI2] = { MTK_DSI, 2, &ddp_dsi },
471 [DDP_COMPONENT_DSI3] = { MTK_DSI, 3, &ddp_dsi },
472 [DDP_COMPONENT_GAMMA] = { MTK_DISP_GAMMA, 0, &ddp_gamma },
473 [DDP_COMPONENT_MERGE0] = { MTK_DISP_MERGE, 0, &ddp_merge },
474 [DDP_COMPONENT_MERGE1] = { MTK_DISP_MERGE, 1, &ddp_merge },
475 [DDP_COMPONENT_MERGE2] = { MTK_DISP_MERGE, 2, &ddp_merge },
476 [DDP_COMPONENT_MERGE3] = { MTK_DISP_MERGE, 3, &ddp_merge },
477 [DDP_COMPONENT_MERGE4] = { MTK_DISP_MERGE, 4, &ddp_merge },
478 [DDP_COMPONENT_MERGE5] = { MTK_DISP_MERGE, 5, &ddp_merge },
479 [DDP_COMPONENT_OD0] = { MTK_DISP_OD, 0, &ddp_od },
480 [DDP_COMPONENT_OD1] = { MTK_DISP_OD, 1, &ddp_od },
481 [DDP_COMPONENT_OVL0] = { MTK_DISP_OVL, 0, &ddp_ovl },
482 [DDP_COMPONENT_OVL1] = { MTK_DISP_OVL, 1, &ddp_ovl },
483 [DDP_COMPONENT_OVL_2L0] = { MTK_DISP_OVL_2L, 0, &ddp_ovl },
484 [DDP_COMPONENT_OVL_2L1] = { MTK_DISP_OVL_2L, 1, &ddp_ovl },
485 [DDP_COMPONENT_OVL_2L2] = { MTK_DISP_OVL_2L, 2, &ddp_ovl },
486 [DDP_COMPONENT_POSTMASK0] = { MTK_DISP_POSTMASK, 0, &ddp_postmask },
487 [DDP_COMPONENT_PWM0] = { MTK_DISP_PWM, 0, NULL },
488 [DDP_COMPONENT_PWM1] = { MTK_DISP_PWM, 1, NULL },
489 [DDP_COMPONENT_PWM2] = { MTK_DISP_PWM, 2, NULL },
490 [DDP_COMPONENT_RDMA0] = { MTK_DISP_RDMA, 0, &ddp_rdma },
491 [DDP_COMPONENT_RDMA1] = { MTK_DISP_RDMA, 1, &ddp_rdma },
492 [DDP_COMPONENT_RDMA2] = { MTK_DISP_RDMA, 2, &ddp_rdma },
493 [DDP_COMPONENT_RDMA4] = { MTK_DISP_RDMA, 4, &ddp_rdma },
494 [DDP_COMPONENT_UFOE] = { MTK_DISP_UFOE, 0, &ddp_ufoe },
495 [DDP_COMPONENT_WDMA0] = { MTK_DISP_WDMA, 0, NULL },
496 [DDP_COMPONENT_WDMA1] = { MTK_DISP_WDMA, 1, NULL },
497};
498
499static bool mtk_drm_find_comp_in_ddp(struct device *dev,
500 const unsigned int *path,
501 unsigned int path_len,
502 struct mtk_ddp_comp *ddp_comp)
503{
504 unsigned int i;
505
506 if (path == NULL)
507 return false;
508
509 for (i = 0U; i < path_len; i++)
510 if (dev == ddp_comp[path[i]].dev)
511 return true;
512
513 return false;
514}
515
516static unsigned int mtk_drm_find_comp_in_ddp_conn_path(struct device *dev,
517 const struct mtk_drm_route *routes,
518 unsigned int num_routes,
519 struct mtk_ddp_comp *ddp_comp)
520{
521 int ret;
522 unsigned int i;
523
524 if (!routes) {
525 ret = -EINVAL;
526 goto err;
527 }
528
529 for (i = 0; i < num_routes; i++)
530 if (dev == ddp_comp[routes[i].route_ddp].dev)
531 return BIT(routes[i].crtc_id);
532
533 ret = -ENODEV;
534err:
535
536 DRM_INFO("Failed to find comp in ddp table, ret = %d\n", ret);
537
538 return 0;
539}
540
541int mtk_ddp_comp_get_id(struct device_node *node,
542 enum mtk_ddp_comp_type comp_type)
543{
544 int id = of_alias_get_id(node, mtk_ddp_comp_stem[comp_type]);
545 int i;
546
547 for (i = 0; i < ARRAY_SIZE(mtk_ddp_matches); i++) {
548 if (comp_type == mtk_ddp_matches[i].type &&
549 (id < 0 || id == mtk_ddp_matches[i].alias_id))
550 return i;
551 }
552
553 return -EINVAL;
554}
555
556unsigned int mtk_drm_find_possible_crtc_by_comp(struct drm_device *drm,
557 struct device *dev)
558{
559 struct mtk_drm_private *private = drm->dev_private;
560 unsigned int ret = 0;
561
562 if (mtk_drm_find_comp_in_ddp(dev, private->data->main_path, private->data->main_len,
563 private->ddp_comp))
564 ret = BIT(0);
565 else if (mtk_drm_find_comp_in_ddp(dev, private->data->ext_path,
566 private->data->ext_len, private->ddp_comp))
567 ret = BIT(1);
568 else if (mtk_drm_find_comp_in_ddp(dev, private->data->third_path,
569 private->data->third_len, private->ddp_comp))
570 ret = BIT(2);
571 else
572 ret = mtk_drm_find_comp_in_ddp_conn_path(dev,
573 private->data->conn_routes,
574 private->data->num_conn_routes,
575 private->ddp_comp);
576
577 return ret;
578}
579
580int mtk_ddp_comp_init(struct device_node *node, struct mtk_ddp_comp *comp,
581 unsigned int comp_id)
582{
583 struct platform_device *comp_pdev;
584 enum mtk_ddp_comp_type type;
585 struct mtk_ddp_comp_dev *priv;
586#if IS_REACHABLE(CONFIG_MTK_CMDQ)
587 int ret;
588#endif
589
590 if (comp_id < 0 || comp_id >= DDP_COMPONENT_DRM_ID_MAX)
591 return -EINVAL;
592
593 type = mtk_ddp_matches[comp_id].type;
594
595 comp->id = comp_id;
596 comp->funcs = mtk_ddp_matches[comp_id].funcs;
597 /* Not all drm components have a DTS device node, such as ovl_adaptor,
598 * which is the drm bring up sub driver
599 */
600 if (!node)
601 return 0;
602
603 comp_pdev = of_find_device_by_node(node);
604 if (!comp_pdev) {
605 DRM_INFO("Waiting for device %s\n", node->full_name);
606 return -EPROBE_DEFER;
607 }
608 comp->dev = &comp_pdev->dev;
609
610 if (type == MTK_DISP_AAL ||
611 type == MTK_DISP_BLS ||
612 type == MTK_DISP_CCORR ||
613 type == MTK_DISP_COLOR ||
614 type == MTK_DISP_GAMMA ||
615 type == MTK_DISP_MERGE ||
616 type == MTK_DISP_OVL ||
617 type == MTK_DISP_OVL_2L ||
618 type == MTK_DISP_PWM ||
619 type == MTK_DISP_RDMA ||
620 type == MTK_DPI ||
621 type == MTK_DP_INTF ||
622 type == MTK_DSI)
623 return 0;
624
625 priv = devm_kzalloc(comp->dev, sizeof(*priv), GFP_KERNEL);
626 if (!priv)
627 return -ENOMEM;
628
629 priv->regs = of_iomap(node, 0);
630 priv->clk = of_clk_get(node, 0);
631 if (IS_ERR(priv->clk))
632 return PTR_ERR(priv->clk);
633
634#if IS_REACHABLE(CONFIG_MTK_CMDQ)
635 ret = cmdq_dev_get_client_reg(comp->dev, &priv->cmdq_reg, 0);
636 if (ret)
637 dev_dbg(comp->dev, "get mediatek,gce-client-reg fail!\n");
638#endif
639
640 platform_set_drvdata(comp_pdev, priv);
641
642 return 0;
643}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2015 MediaTek Inc.
4 * Authors:
5 * YT Shen <yt.shen@mediatek.com>
6 * CK Hu <ck.hu@mediatek.com>
7 */
8
9#include <linux/clk.h>
10#include <linux/of.h>
11#include <linux/of_address.h>
12#include <linux/of_irq.h>
13#include <linux/of_platform.h>
14#include <linux/platform_device.h>
15#include <linux/soc/mediatek/mtk-cmdq.h>
16#include "mtk_drm_drv.h"
17#include "mtk_drm_plane.h"
18#include "mtk_drm_ddp_comp.h"
19#include "mtk_drm_crtc.h"
20
21#define DISP_OD_EN 0x0000
22#define DISP_OD_INTEN 0x0008
23#define DISP_OD_INTSTA 0x000c
24#define DISP_OD_CFG 0x0020
25#define DISP_OD_SIZE 0x0030
26#define DISP_DITHER_5 0x0114
27#define DISP_DITHER_7 0x011c
28#define DISP_DITHER_15 0x013c
29#define DISP_DITHER_16 0x0140
30
31#define DISP_REG_UFO_START 0x0000
32
33#define DISP_AAL_EN 0x0000
34#define DISP_AAL_SIZE 0x0030
35
36#define DISP_CCORR_EN 0x0000
37#define CCORR_EN BIT(0)
38#define DISP_CCORR_CFG 0x0020
39#define CCORR_RELAY_MODE BIT(0)
40#define CCORR_ENGINE_EN BIT(1)
41#define CCORR_GAMMA_OFF BIT(2)
42#define CCORR_WGAMUT_SRC_CLIP BIT(3)
43#define DISP_CCORR_SIZE 0x0030
44#define DISP_CCORR_COEF_0 0x0080
45#define DISP_CCORR_COEF_1 0x0084
46#define DISP_CCORR_COEF_2 0x0088
47#define DISP_CCORR_COEF_3 0x008C
48#define DISP_CCORR_COEF_4 0x0090
49
50#define DISP_DITHER_EN 0x0000
51#define DITHER_EN BIT(0)
52#define DISP_DITHER_CFG 0x0020
53#define DITHER_RELAY_MODE BIT(0)
54#define DISP_DITHER_SIZE 0x0030
55
56#define DISP_GAMMA_EN 0x0000
57#define DISP_GAMMA_CFG 0x0020
58#define DISP_GAMMA_SIZE 0x0030
59#define DISP_GAMMA_LUT 0x0700
60
61#define LUT_10BIT_MASK 0x03ff
62
63#define OD_RELAYMODE BIT(0)
64
65#define UFO_BYPASS BIT(2)
66
67#define AAL_EN BIT(0)
68
69#define GAMMA_EN BIT(0)
70#define GAMMA_LUT_EN BIT(1)
71
72#define DISP_DITHERING BIT(2)
73#define DITHER_LSB_ERR_SHIFT_R(x) (((x) & 0x7) << 28)
74#define DITHER_OVFLW_BIT_R(x) (((x) & 0x7) << 24)
75#define DITHER_ADD_LSHIFT_R(x) (((x) & 0x7) << 20)
76#define DITHER_ADD_RSHIFT_R(x) (((x) & 0x7) << 16)
77#define DITHER_NEW_BIT_MODE BIT(0)
78#define DITHER_LSB_ERR_SHIFT_B(x) (((x) & 0x7) << 28)
79#define DITHER_OVFLW_BIT_B(x) (((x) & 0x7) << 24)
80#define DITHER_ADD_LSHIFT_B(x) (((x) & 0x7) << 20)
81#define DITHER_ADD_RSHIFT_B(x) (((x) & 0x7) << 16)
82#define DITHER_LSB_ERR_SHIFT_G(x) (((x) & 0x7) << 12)
83#define DITHER_OVFLW_BIT_G(x) (((x) & 0x7) << 8)
84#define DITHER_ADD_LSHIFT_G(x) (((x) & 0x7) << 4)
85#define DITHER_ADD_RSHIFT_G(x) (((x) & 0x7) << 0)
86
87void mtk_ddp_write(struct cmdq_pkt *cmdq_pkt, unsigned int value,
88 struct mtk_ddp_comp *comp, unsigned int offset)
89{
90#if IS_REACHABLE(CONFIG_MTK_CMDQ)
91 if (cmdq_pkt)
92 cmdq_pkt_write(cmdq_pkt, comp->subsys,
93 comp->regs_pa + offset, value);
94 else
95#endif
96 writel(value, comp->regs + offset);
97}
98
99void mtk_ddp_write_relaxed(struct cmdq_pkt *cmdq_pkt, unsigned int value,
100 struct mtk_ddp_comp *comp,
101 unsigned int offset)
102{
103#if IS_REACHABLE(CONFIG_MTK_CMDQ)
104 if (cmdq_pkt)
105 cmdq_pkt_write(cmdq_pkt, comp->subsys,
106 comp->regs_pa + offset, value);
107 else
108#endif
109 writel_relaxed(value, comp->regs + offset);
110}
111
112void mtk_ddp_write_mask(struct cmdq_pkt *cmdq_pkt,
113 unsigned int value,
114 struct mtk_ddp_comp *comp,
115 unsigned int offset,
116 unsigned int mask)
117{
118#if IS_REACHABLE(CONFIG_MTK_CMDQ)
119 if (cmdq_pkt) {
120 cmdq_pkt_write_mask(cmdq_pkt, comp->subsys,
121 comp->regs_pa + offset, value, mask);
122 } else {
123#endif
124 u32 tmp = readl(comp->regs + offset);
125
126 tmp = (tmp & ~mask) | (value & mask);
127 writel(tmp, comp->regs + offset);
128#if IS_REACHABLE(CONFIG_MTK_CMDQ)
129 }
130#endif
131}
132
133void mtk_dither_set(struct mtk_ddp_comp *comp, unsigned int bpc,
134 unsigned int CFG, struct cmdq_pkt *cmdq_pkt)
135{
136 /* If bpc equal to 0, the dithering function didn't be enabled */
137 if (bpc == 0)
138 return;
139
140 if (bpc >= MTK_MIN_BPC) {
141 mtk_ddp_write(cmdq_pkt, 0, comp, DISP_DITHER_5);
142 mtk_ddp_write(cmdq_pkt, 0, comp, DISP_DITHER_7);
143 mtk_ddp_write(cmdq_pkt,
144 DITHER_LSB_ERR_SHIFT_R(MTK_MAX_BPC - bpc) |
145 DITHER_ADD_LSHIFT_R(MTK_MAX_BPC - bpc) |
146 DITHER_NEW_BIT_MODE,
147 comp, DISP_DITHER_15);
148 mtk_ddp_write(cmdq_pkt,
149 DITHER_LSB_ERR_SHIFT_B(MTK_MAX_BPC - bpc) |
150 DITHER_ADD_LSHIFT_B(MTK_MAX_BPC - bpc) |
151 DITHER_LSB_ERR_SHIFT_G(MTK_MAX_BPC - bpc) |
152 DITHER_ADD_LSHIFT_G(MTK_MAX_BPC - bpc),
153 comp, DISP_DITHER_16);
154 mtk_ddp_write(cmdq_pkt, DISP_DITHERING, comp, CFG);
155 }
156}
157
158static void mtk_od_config(struct mtk_ddp_comp *comp, unsigned int w,
159 unsigned int h, unsigned int vrefresh,
160 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
161{
162 mtk_ddp_write(cmdq_pkt, w << 16 | h, comp, DISP_OD_SIZE);
163 mtk_ddp_write(cmdq_pkt, OD_RELAYMODE, comp, DISP_OD_CFG);
164 mtk_dither_set(comp, bpc, DISP_OD_CFG, cmdq_pkt);
165}
166
167static void mtk_od_start(struct mtk_ddp_comp *comp)
168{
169 writel(1, comp->regs + DISP_OD_EN);
170}
171
172static void mtk_ufoe_start(struct mtk_ddp_comp *comp)
173{
174 writel(UFO_BYPASS, comp->regs + DISP_REG_UFO_START);
175}
176
177static void mtk_aal_config(struct mtk_ddp_comp *comp, unsigned int w,
178 unsigned int h, unsigned int vrefresh,
179 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
180{
181 mtk_ddp_write(cmdq_pkt, h << 16 | w, comp, DISP_AAL_SIZE);
182}
183
184static void mtk_aal_start(struct mtk_ddp_comp *comp)
185{
186 writel(AAL_EN, comp->regs + DISP_AAL_EN);
187}
188
189static void mtk_aal_stop(struct mtk_ddp_comp *comp)
190{
191 writel_relaxed(0x0, comp->regs + DISP_AAL_EN);
192}
193
194static void mtk_ccorr_config(struct mtk_ddp_comp *comp, unsigned int w,
195 unsigned int h, unsigned int vrefresh,
196 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
197{
198 mtk_ddp_write(cmdq_pkt, h << 16 | w, comp, DISP_CCORR_SIZE);
199 mtk_ddp_write(cmdq_pkt, CCORR_ENGINE_EN, comp, DISP_CCORR_CFG);
200}
201
202static void mtk_ccorr_start(struct mtk_ddp_comp *comp)
203{
204 writel(CCORR_EN, comp->regs + DISP_CCORR_EN);
205}
206
207static void mtk_ccorr_stop(struct mtk_ddp_comp *comp)
208{
209 writel_relaxed(0x0, comp->regs + DISP_CCORR_EN);
210}
211
212/* Converts a DRM S31.32 value to the HW S1.10 format. */
213static u16 mtk_ctm_s31_32_to_s1_10(u64 in)
214{
215 u16 r;
216
217 /* Sign bit. */
218 r = in & BIT_ULL(63) ? BIT(11) : 0;
219
220 if ((in & GENMASK_ULL(62, 33)) > 0) {
221 /* identity value 0x100000000 -> 0x400, */
222 /* if bigger this, set it to max 0x7ff. */
223 r |= GENMASK(10, 0);
224 } else {
225 /* take the 11 most important bits. */
226 r |= (in >> 22) & GENMASK(10, 0);
227 }
228
229 return r;
230}
231
232static void mtk_ccorr_ctm_set(struct mtk_ddp_comp *comp,
233 struct drm_crtc_state *state)
234{
235 struct drm_property_blob *blob = state->ctm;
236 struct drm_color_ctm *ctm;
237 const u64 *input;
238 uint16_t coeffs[9] = { 0 };
239 int i;
240 struct cmdq_pkt *cmdq_pkt = NULL;
241
242 if (!blob)
243 return;
244
245 ctm = (struct drm_color_ctm *)blob->data;
246 input = ctm->matrix;
247
248 for (i = 0; i < ARRAY_SIZE(coeffs); i++)
249 coeffs[i] = mtk_ctm_s31_32_to_s1_10(input[i]);
250
251 mtk_ddp_write(cmdq_pkt, coeffs[0] << 16 | coeffs[1],
252 comp, DISP_CCORR_COEF_0);
253 mtk_ddp_write(cmdq_pkt, coeffs[2] << 16 | coeffs[3],
254 comp, DISP_CCORR_COEF_1);
255 mtk_ddp_write(cmdq_pkt, coeffs[4] << 16 | coeffs[5],
256 comp, DISP_CCORR_COEF_2);
257 mtk_ddp_write(cmdq_pkt, coeffs[6] << 16 | coeffs[7],
258 comp, DISP_CCORR_COEF_3);
259 mtk_ddp_write(cmdq_pkt, coeffs[8] << 16,
260 comp, DISP_CCORR_COEF_4);
261}
262
263static void mtk_dither_config(struct mtk_ddp_comp *comp, unsigned int w,
264 unsigned int h, unsigned int vrefresh,
265 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
266{
267 mtk_ddp_write(cmdq_pkt, h << 16 | w, comp, DISP_DITHER_SIZE);
268 mtk_ddp_write(cmdq_pkt, DITHER_RELAY_MODE, comp, DISP_DITHER_CFG);
269}
270
271static void mtk_dither_start(struct mtk_ddp_comp *comp)
272{
273 writel(DITHER_EN, comp->regs + DISP_DITHER_EN);
274}
275
276static void mtk_dither_stop(struct mtk_ddp_comp *comp)
277{
278 writel_relaxed(0x0, comp->regs + DISP_DITHER_EN);
279}
280
281static void mtk_gamma_config(struct mtk_ddp_comp *comp, unsigned int w,
282 unsigned int h, unsigned int vrefresh,
283 unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
284{
285 mtk_ddp_write(cmdq_pkt, h << 16 | w, comp, DISP_GAMMA_SIZE);
286 mtk_dither_set(comp, bpc, DISP_GAMMA_CFG, cmdq_pkt);
287}
288
289static void mtk_gamma_start(struct mtk_ddp_comp *comp)
290{
291 writel(GAMMA_EN, comp->regs + DISP_GAMMA_EN);
292}
293
294static void mtk_gamma_stop(struct mtk_ddp_comp *comp)
295{
296 writel_relaxed(0x0, comp->regs + DISP_GAMMA_EN);
297}
298
299static void mtk_gamma_set(struct mtk_ddp_comp *comp,
300 struct drm_crtc_state *state)
301{
302 unsigned int i, reg;
303 struct drm_color_lut *lut;
304 void __iomem *lut_base;
305 u32 word;
306
307 if (state->gamma_lut) {
308 reg = readl(comp->regs + DISP_GAMMA_CFG);
309 reg = reg | GAMMA_LUT_EN;
310 writel(reg, comp->regs + DISP_GAMMA_CFG);
311 lut_base = comp->regs + DISP_GAMMA_LUT;
312 lut = (struct drm_color_lut *)state->gamma_lut->data;
313 for (i = 0; i < MTK_LUT_SIZE; i++) {
314 word = (((lut[i].red >> 6) & LUT_10BIT_MASK) << 20) +
315 (((lut[i].green >> 6) & LUT_10BIT_MASK) << 10) +
316 ((lut[i].blue >> 6) & LUT_10BIT_MASK);
317 writel(word, (lut_base + i * 4));
318 }
319 }
320}
321
322static const struct mtk_ddp_comp_funcs ddp_aal = {
323 .gamma_set = mtk_gamma_set,
324 .config = mtk_aal_config,
325 .start = mtk_aal_start,
326 .stop = mtk_aal_stop,
327};
328
329static const struct mtk_ddp_comp_funcs ddp_ccorr = {
330 .config = mtk_ccorr_config,
331 .start = mtk_ccorr_start,
332 .stop = mtk_ccorr_stop,
333 .ctm_set = mtk_ccorr_ctm_set,
334};
335
336static const struct mtk_ddp_comp_funcs ddp_dither = {
337 .config = mtk_dither_config,
338 .start = mtk_dither_start,
339 .stop = mtk_dither_stop,
340};
341
342static const struct mtk_ddp_comp_funcs ddp_gamma = {
343 .gamma_set = mtk_gamma_set,
344 .config = mtk_gamma_config,
345 .start = mtk_gamma_start,
346 .stop = mtk_gamma_stop,
347};
348
349static const struct mtk_ddp_comp_funcs ddp_od = {
350 .config = mtk_od_config,
351 .start = mtk_od_start,
352};
353
354static const struct mtk_ddp_comp_funcs ddp_ufoe = {
355 .start = mtk_ufoe_start,
356};
357
358static const char * const mtk_ddp_comp_stem[MTK_DDP_COMP_TYPE_MAX] = {
359 [MTK_DISP_OVL] = "ovl",
360 [MTK_DISP_OVL_2L] = "ovl_2l",
361 [MTK_DISP_RDMA] = "rdma",
362 [MTK_DISP_WDMA] = "wdma",
363 [MTK_DISP_COLOR] = "color",
364 [MTK_DISP_CCORR] = "ccorr",
365 [MTK_DISP_AAL] = "aal",
366 [MTK_DISP_GAMMA] = "gamma",
367 [MTK_DISP_DITHER] = "dither",
368 [MTK_DISP_UFOE] = "ufoe",
369 [MTK_DSI] = "dsi",
370 [MTK_DPI] = "dpi",
371 [MTK_DISP_PWM] = "pwm",
372 [MTK_DISP_MUTEX] = "mutex",
373 [MTK_DISP_OD] = "od",
374 [MTK_DISP_BLS] = "bls",
375};
376
377struct mtk_ddp_comp_match {
378 enum mtk_ddp_comp_type type;
379 int alias_id;
380 const struct mtk_ddp_comp_funcs *funcs;
381};
382
383static const struct mtk_ddp_comp_match mtk_ddp_matches[DDP_COMPONENT_ID_MAX] = {
384 [DDP_COMPONENT_AAL0] = { MTK_DISP_AAL, 0, &ddp_aal },
385 [DDP_COMPONENT_AAL1] = { MTK_DISP_AAL, 1, &ddp_aal },
386 [DDP_COMPONENT_BLS] = { MTK_DISP_BLS, 0, NULL },
387 [DDP_COMPONENT_CCORR] = { MTK_DISP_CCORR, 0, &ddp_ccorr },
388 [DDP_COMPONENT_COLOR0] = { MTK_DISP_COLOR, 0, NULL },
389 [DDP_COMPONENT_COLOR1] = { MTK_DISP_COLOR, 1, NULL },
390 [DDP_COMPONENT_DITHER] = { MTK_DISP_DITHER, 0, &ddp_dither },
391 [DDP_COMPONENT_DPI0] = { MTK_DPI, 0, NULL },
392 [DDP_COMPONENT_DPI1] = { MTK_DPI, 1, NULL },
393 [DDP_COMPONENT_DSI0] = { MTK_DSI, 0, NULL },
394 [DDP_COMPONENT_DSI1] = { MTK_DSI, 1, NULL },
395 [DDP_COMPONENT_DSI2] = { MTK_DSI, 2, NULL },
396 [DDP_COMPONENT_DSI3] = { MTK_DSI, 3, NULL },
397 [DDP_COMPONENT_GAMMA] = { MTK_DISP_GAMMA, 0, &ddp_gamma },
398 [DDP_COMPONENT_OD0] = { MTK_DISP_OD, 0, &ddp_od },
399 [DDP_COMPONENT_OD1] = { MTK_DISP_OD, 1, &ddp_od },
400 [DDP_COMPONENT_OVL0] = { MTK_DISP_OVL, 0, NULL },
401 [DDP_COMPONENT_OVL1] = { MTK_DISP_OVL, 1, NULL },
402 [DDP_COMPONENT_OVL_2L0] = { MTK_DISP_OVL_2L, 0, NULL },
403 [DDP_COMPONENT_OVL_2L1] = { MTK_DISP_OVL_2L, 1, NULL },
404 [DDP_COMPONENT_PWM0] = { MTK_DISP_PWM, 0, NULL },
405 [DDP_COMPONENT_PWM1] = { MTK_DISP_PWM, 1, NULL },
406 [DDP_COMPONENT_PWM2] = { MTK_DISP_PWM, 2, NULL },
407 [DDP_COMPONENT_RDMA0] = { MTK_DISP_RDMA, 0, NULL },
408 [DDP_COMPONENT_RDMA1] = { MTK_DISP_RDMA, 1, NULL },
409 [DDP_COMPONENT_RDMA2] = { MTK_DISP_RDMA, 2, NULL },
410 [DDP_COMPONENT_UFOE] = { MTK_DISP_UFOE, 0, &ddp_ufoe },
411 [DDP_COMPONENT_WDMA0] = { MTK_DISP_WDMA, 0, NULL },
412 [DDP_COMPONENT_WDMA1] = { MTK_DISP_WDMA, 1, NULL },
413};
414
415int mtk_ddp_comp_get_id(struct device_node *node,
416 enum mtk_ddp_comp_type comp_type)
417{
418 int id = of_alias_get_id(node, mtk_ddp_comp_stem[comp_type]);
419 int i;
420
421 for (i = 0; i < ARRAY_SIZE(mtk_ddp_matches); i++) {
422 if (comp_type == mtk_ddp_matches[i].type &&
423 (id < 0 || id == mtk_ddp_matches[i].alias_id))
424 return i;
425 }
426
427 return -EINVAL;
428}
429
430int mtk_ddp_comp_init(struct device *dev, struct device_node *node,
431 struct mtk_ddp_comp *comp, enum mtk_ddp_comp_id comp_id,
432 const struct mtk_ddp_comp_funcs *funcs)
433{
434 enum mtk_ddp_comp_type type;
435 struct device_node *larb_node;
436 struct platform_device *larb_pdev;
437#if IS_REACHABLE(CONFIG_MTK_CMDQ)
438 struct resource res;
439 struct cmdq_client_reg cmdq_reg;
440 int ret;
441#endif
442
443 if (comp_id < 0 || comp_id >= DDP_COMPONENT_ID_MAX)
444 return -EINVAL;
445
446 type = mtk_ddp_matches[comp_id].type;
447
448 comp->id = comp_id;
449 comp->funcs = funcs ?: mtk_ddp_matches[comp_id].funcs;
450
451 if (comp_id == DDP_COMPONENT_BLS ||
452 comp_id == DDP_COMPONENT_DPI0 ||
453 comp_id == DDP_COMPONENT_DPI1 ||
454 comp_id == DDP_COMPONENT_DSI0 ||
455 comp_id == DDP_COMPONENT_DSI1 ||
456 comp_id == DDP_COMPONENT_DSI2 ||
457 comp_id == DDP_COMPONENT_DSI3 ||
458 comp_id == DDP_COMPONENT_PWM0) {
459 comp->regs = NULL;
460 comp->clk = NULL;
461 comp->irq = 0;
462 return 0;
463 }
464
465 comp->regs = of_iomap(node, 0);
466 comp->irq = of_irq_get(node, 0);
467 comp->clk = of_clk_get(node, 0);
468 if (IS_ERR(comp->clk))
469 return PTR_ERR(comp->clk);
470
471 /* Only DMA capable components need the LARB property */
472 comp->larb_dev = NULL;
473 if (type != MTK_DISP_OVL &&
474 type != MTK_DISP_OVL_2L &&
475 type != MTK_DISP_RDMA &&
476 type != MTK_DISP_WDMA)
477 return 0;
478
479 larb_node = of_parse_phandle(node, "mediatek,larb", 0);
480 if (!larb_node) {
481 dev_err(dev,
482 "Missing mediadek,larb phandle in %pOF node\n", node);
483 return -EINVAL;
484 }
485
486 larb_pdev = of_find_device_by_node(larb_node);
487 if (!larb_pdev) {
488 dev_warn(dev, "Waiting for larb device %pOF\n", larb_node);
489 of_node_put(larb_node);
490 return -EPROBE_DEFER;
491 }
492 of_node_put(larb_node);
493
494 comp->larb_dev = &larb_pdev->dev;
495
496#if IS_REACHABLE(CONFIG_MTK_CMDQ)
497 if (of_address_to_resource(node, 0, &res) != 0) {
498 dev_err(dev, "Missing reg in %s node\n", node->full_name);
499 put_device(&larb_pdev->dev);
500 return -EINVAL;
501 }
502 comp->regs_pa = res.start;
503
504 ret = cmdq_dev_get_client_reg(dev, &cmdq_reg, 0);
505 if (ret)
506 dev_dbg(dev, "get mediatek,gce-client-reg fail!\n");
507 else
508 comp->subsys = cmdq_reg.subsys;
509#endif
510 return 0;
511}
512
513int mtk_ddp_comp_register(struct drm_device *drm, struct mtk_ddp_comp *comp)
514{
515 struct mtk_drm_private *private = drm->dev_private;
516
517 if (private->ddp_comp[comp->id])
518 return -EBUSY;
519
520 private->ddp_comp[comp->id] = comp;
521 return 0;
522}
523
524void mtk_ddp_comp_unregister(struct drm_device *drm, struct mtk_ddp_comp *comp)
525{
526 struct mtk_drm_private *private = drm->dev_private;
527
528 private->ddp_comp[comp->id] = NULL;
529}