2 * Copyright (c) 2015 MediaTek Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/component.h>
17 #include <linux/of_device.h>
18 #include <linux/of_irq.h>
19 #include <linux/platform_device.h>
21 #include "mtk_drm_crtc.h"
22 #include "mtk_drm_ddp_comp.h"
24 #define DISP_REG_OVL_INTEN 0x0004
25 #define OVL_FME_CPL_INT BIT(1)
26 #define DISP_REG_OVL_INTSTA 0x0008
27 #define DISP_REG_OVL_EN 0x000c
28 #define DISP_REG_OVL_RST 0x0014
29 #define DISP_REG_OVL_ROI_SIZE 0x0020
30 #define DISP_REG_OVL_ROI_BGCLR 0x0028
31 #define DISP_REG_OVL_SRC_CON 0x002c
32 #define DISP_REG_OVL_CON(n) (0x0030 + 0x20 * (n))
33 #define DISP_REG_OVL_SRC_SIZE(n) (0x0038 + 0x20 * (n))
34 #define DISP_REG_OVL_OFFSET(n) (0x003c + 0x20 * (n))
35 #define DISP_REG_OVL_PITCH(n) (0x0044 + 0x20 * (n))
36 #define DISP_REG_OVL_RDMA_CTRL(n) (0x00c0 + 0x20 * (n))
37 #define DISP_REG_OVL_RDMA_GMC(n) (0x00c8 + 0x20 * (n))
38 #define DISP_REG_OVL_ADDR_MT2701 0x0040
39 #define DISP_REG_OVL_ADDR_MT8173 0x0f40
40 #define DISP_REG_OVL_ADDR(ovl, n) ((ovl)->data->addr + 0x20 * (n))
42 #define OVL_RDMA_MEM_GMC 0x40402020
44 #define OVL_CON_BYTE_SWAP BIT(24)
45 #define OVL_CON_MTX_YUV_TO_RGB (6 << 16)
46 #define OVL_CON_CLRFMT_RGB (1 << 12)
47 #define OVL_CON_CLRFMT_RGBA8888 (2 << 12)
48 #define OVL_CON_CLRFMT_ARGB8888 (3 << 12)
49 #define OVL_CON_CLRFMT_UYVY (4 << 12)
50 #define OVL_CON_CLRFMT_YUYV (5 << 12)
51 #define OVL_CON_CLRFMT_RGB565(ovl) ((ovl)->data->fmt_rgb565_is_0 ? \
52 0 : OVL_CON_CLRFMT_RGB)
53 #define OVL_CON_CLRFMT_RGB888(ovl) ((ovl)->data->fmt_rgb565_is_0 ? \
54 OVL_CON_CLRFMT_RGB : 0)
55 #define OVL_CON_AEN BIT(8)
56 #define OVL_CON_ALPHA 0xff
58 struct mtk_disp_ovl_data
{
64 * struct mtk_disp_ovl - DISP_OVL driver structure
65 * @ddp_comp - structure containing type enum and hardware resources
66 * @crtc - associated crtc to report vblank events to
69 struct mtk_ddp_comp ddp_comp
;
70 struct drm_crtc
*crtc
;
71 const struct mtk_disp_ovl_data
*data
;
74 static inline struct mtk_disp_ovl
*comp_to_ovl(struct mtk_ddp_comp
*comp
)
76 return container_of(comp
, struct mtk_disp_ovl
, ddp_comp
);
79 static irqreturn_t
mtk_disp_ovl_irq_handler(int irq
, void *dev_id
)
81 struct mtk_disp_ovl
*priv
= dev_id
;
82 struct mtk_ddp_comp
*ovl
= &priv
->ddp_comp
;
84 /* Clear frame completion interrupt */
85 writel(0x0, ovl
->regs
+ DISP_REG_OVL_INTSTA
);
90 mtk_crtc_ddp_irq(priv
->crtc
, ovl
);
95 static void mtk_ovl_enable_vblank(struct mtk_ddp_comp
*comp
,
96 struct drm_crtc
*crtc
)
98 struct mtk_disp_ovl
*ovl
= comp_to_ovl(comp
);
101 writel(0x0, comp
->regs
+ DISP_REG_OVL_INTSTA
);
102 writel_relaxed(OVL_FME_CPL_INT
, comp
->regs
+ DISP_REG_OVL_INTEN
);
105 static void mtk_ovl_disable_vblank(struct mtk_ddp_comp
*comp
)
107 struct mtk_disp_ovl
*ovl
= comp_to_ovl(comp
);
110 writel_relaxed(0x0, comp
->regs
+ DISP_REG_OVL_INTEN
);
113 static void mtk_ovl_start(struct mtk_ddp_comp
*comp
)
115 writel_relaxed(0x1, comp
->regs
+ DISP_REG_OVL_EN
);
118 static void mtk_ovl_stop(struct mtk_ddp_comp
*comp
)
120 writel_relaxed(0x0, comp
->regs
+ DISP_REG_OVL_EN
);
123 static void mtk_ovl_config(struct mtk_ddp_comp
*comp
, unsigned int w
,
124 unsigned int h
, unsigned int vrefresh
,
127 if (w
!= 0 && h
!= 0)
128 writel_relaxed(h
<< 16 | w
, comp
->regs
+ DISP_REG_OVL_ROI_SIZE
);
129 writel_relaxed(0x0, comp
->regs
+ DISP_REG_OVL_ROI_BGCLR
);
131 writel(0x1, comp
->regs
+ DISP_REG_OVL_RST
);
132 writel(0x0, comp
->regs
+ DISP_REG_OVL_RST
);
135 static void mtk_ovl_layer_on(struct mtk_ddp_comp
*comp
, unsigned int idx
)
139 writel(0x1, comp
->regs
+ DISP_REG_OVL_RDMA_CTRL(idx
));
140 writel(OVL_RDMA_MEM_GMC
, comp
->regs
+ DISP_REG_OVL_RDMA_GMC(idx
));
142 reg
= readl(comp
->regs
+ DISP_REG_OVL_SRC_CON
);
143 reg
= reg
| BIT(idx
);
144 writel(reg
, comp
->regs
+ DISP_REG_OVL_SRC_CON
);
147 static void mtk_ovl_layer_off(struct mtk_ddp_comp
*comp
, unsigned int idx
)
151 reg
= readl(comp
->regs
+ DISP_REG_OVL_SRC_CON
);
152 reg
= reg
& ~BIT(idx
);
153 writel(reg
, comp
->regs
+ DISP_REG_OVL_SRC_CON
);
155 writel(0x0, comp
->regs
+ DISP_REG_OVL_RDMA_CTRL(idx
));
158 static unsigned int ovl_fmt_convert(struct mtk_disp_ovl
*ovl
, unsigned int fmt
)
162 case DRM_FORMAT_RGB565
:
163 return OVL_CON_CLRFMT_RGB565(ovl
);
164 case DRM_FORMAT_BGR565
:
165 return OVL_CON_CLRFMT_RGB565(ovl
) | OVL_CON_BYTE_SWAP
;
166 case DRM_FORMAT_RGB888
:
167 return OVL_CON_CLRFMT_RGB888(ovl
);
168 case DRM_FORMAT_BGR888
:
169 return OVL_CON_CLRFMT_RGB888(ovl
) | OVL_CON_BYTE_SWAP
;
170 case DRM_FORMAT_RGBX8888
:
171 case DRM_FORMAT_RGBA8888
:
172 return OVL_CON_CLRFMT_ARGB8888
;
173 case DRM_FORMAT_BGRX8888
:
174 case DRM_FORMAT_BGRA8888
:
175 return OVL_CON_CLRFMT_ARGB8888
| OVL_CON_BYTE_SWAP
;
176 case DRM_FORMAT_XRGB8888
:
177 case DRM_FORMAT_ARGB8888
:
178 return OVL_CON_CLRFMT_RGBA8888
;
179 case DRM_FORMAT_XBGR8888
:
180 case DRM_FORMAT_ABGR8888
:
181 return OVL_CON_CLRFMT_RGBA8888
| OVL_CON_BYTE_SWAP
;
182 case DRM_FORMAT_UYVY
:
183 return OVL_CON_CLRFMT_UYVY
| OVL_CON_MTX_YUV_TO_RGB
;
184 case DRM_FORMAT_YUYV
:
185 return OVL_CON_CLRFMT_YUYV
| OVL_CON_MTX_YUV_TO_RGB
;
189 static void mtk_ovl_layer_config(struct mtk_ddp_comp
*comp
, unsigned int idx
,
190 struct mtk_plane_state
*state
)
192 struct mtk_disp_ovl
*ovl
= comp_to_ovl(comp
);
193 struct mtk_plane_pending_state
*pending
= &state
->pending
;
194 unsigned int addr
= pending
->addr
;
195 unsigned int pitch
= pending
->pitch
& 0xffff;
196 unsigned int fmt
= pending
->format
;
197 unsigned int offset
= (pending
->y
<< 16) | pending
->x
;
198 unsigned int src_size
= (pending
->height
<< 16) | pending
->width
;
201 if (!pending
->enable
)
202 mtk_ovl_layer_off(comp
, idx
);
204 con
= ovl_fmt_convert(ovl
, fmt
);
206 con
|= OVL_CON_AEN
| OVL_CON_ALPHA
;
208 writel_relaxed(con
, comp
->regs
+ DISP_REG_OVL_CON(idx
));
209 writel_relaxed(pitch
, comp
->regs
+ DISP_REG_OVL_PITCH(idx
));
210 writel_relaxed(src_size
, comp
->regs
+ DISP_REG_OVL_SRC_SIZE(idx
));
211 writel_relaxed(offset
, comp
->regs
+ DISP_REG_OVL_OFFSET(idx
));
212 writel_relaxed(addr
, comp
->regs
+ DISP_REG_OVL_ADDR(ovl
, idx
));
215 mtk_ovl_layer_on(comp
, idx
);
218 static const struct mtk_ddp_comp_funcs mtk_disp_ovl_funcs
= {
219 .config
= mtk_ovl_config
,
220 .start
= mtk_ovl_start
,
221 .stop
= mtk_ovl_stop
,
222 .enable_vblank
= mtk_ovl_enable_vblank
,
223 .disable_vblank
= mtk_ovl_disable_vblank
,
224 .layer_on
= mtk_ovl_layer_on
,
225 .layer_off
= mtk_ovl_layer_off
,
226 .layer_config
= mtk_ovl_layer_config
,
229 static int mtk_disp_ovl_bind(struct device
*dev
, struct device
*master
,
232 struct mtk_disp_ovl
*priv
= dev_get_drvdata(dev
);
233 struct drm_device
*drm_dev
= data
;
236 ret
= mtk_ddp_comp_register(drm_dev
, &priv
->ddp_comp
);
238 dev_err(dev
, "Failed to register component %s: %d\n",
239 dev
->of_node
->full_name
, ret
);
246 static void mtk_disp_ovl_unbind(struct device
*dev
, struct device
*master
,
249 struct mtk_disp_ovl
*priv
= dev_get_drvdata(dev
);
250 struct drm_device
*drm_dev
= data
;
252 mtk_ddp_comp_unregister(drm_dev
, &priv
->ddp_comp
);
255 static const struct component_ops mtk_disp_ovl_component_ops
= {
256 .bind
= mtk_disp_ovl_bind
,
257 .unbind
= mtk_disp_ovl_unbind
,
260 static int mtk_disp_ovl_probe(struct platform_device
*pdev
)
262 struct device
*dev
= &pdev
->dev
;
263 struct mtk_disp_ovl
*priv
;
268 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
272 irq
= platform_get_irq(pdev
, 0);
276 comp_id
= mtk_ddp_comp_get_id(dev
->of_node
, MTK_DISP_OVL
);
278 dev_err(dev
, "Failed to identify by alias: %d\n", comp_id
);
282 ret
= mtk_ddp_comp_init(dev
, dev
->of_node
, &priv
->ddp_comp
, comp_id
,
283 &mtk_disp_ovl_funcs
);
285 dev_err(dev
, "Failed to initialize component: %d\n", ret
);
289 priv
->data
= of_device_get_match_data(dev
);
291 platform_set_drvdata(pdev
, priv
);
293 ret
= devm_request_irq(dev
, irq
, mtk_disp_ovl_irq_handler
,
294 IRQF_TRIGGER_NONE
, dev_name(dev
), priv
);
296 dev_err(dev
, "Failed to request irq %d: %d\n", irq
, ret
);
300 ret
= component_add(dev
, &mtk_disp_ovl_component_ops
);
302 dev_err(dev
, "Failed to add component: %d\n", ret
);
307 static int mtk_disp_ovl_remove(struct platform_device
*pdev
)
309 component_del(&pdev
->dev
, &mtk_disp_ovl_component_ops
);
314 static const struct mtk_disp_ovl_data mt2701_ovl_driver_data
= {
315 .addr
= DISP_REG_OVL_ADDR_MT2701
,
316 .fmt_rgb565_is_0
= false,
319 static const struct mtk_disp_ovl_data mt8173_ovl_driver_data
= {
320 .addr
= DISP_REG_OVL_ADDR_MT8173
,
321 .fmt_rgb565_is_0
= true,
324 static const struct of_device_id mtk_disp_ovl_driver_dt_match
[] = {
325 { .compatible
= "mediatek,mt2701-disp-ovl",
326 .data
= &mt2701_ovl_driver_data
},
327 { .compatible
= "mediatek,mt8173-disp-ovl",
328 .data
= &mt8173_ovl_driver_data
},
331 MODULE_DEVICE_TABLE(of
, mtk_disp_ovl_driver_dt_match
);
333 struct platform_driver mtk_disp_ovl_driver
= {
334 .probe
= mtk_disp_ovl_probe
,
335 .remove
= mtk_disp_ovl_remove
,
337 .name
= "mediatek-disp-ovl",
338 .owner
= THIS_MODULE
,
339 .of_match_table
= mtk_disp_ovl_driver_dt_match
,