1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * ARC PGU DRM driver.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com)
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <drm/drm_atomic_helper.h>
9*4882a593Smuzhiyun #include <drm/drm_device.h>
10*4882a593Smuzhiyun #include <drm/drm_fb_cma_helper.h>
11*4882a593Smuzhiyun #include <drm/drm_gem_cma_helper.h>
12*4882a593Smuzhiyun #include <drm/drm_plane_helper.h>
13*4882a593Smuzhiyun #include <drm/drm_probe_helper.h>
14*4882a593Smuzhiyun #include <linux/clk.h>
15*4882a593Smuzhiyun #include <linux/platform_data/simplefb.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include "arcpgu.h"
18*4882a593Smuzhiyun #include "arcpgu_regs.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define ENCODE_PGU_XY(x, y) ((((x) - 1) << 16) | ((y) - 1))
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun static const u32 arc_pgu_supported_formats[] = {
23*4882a593Smuzhiyun DRM_FORMAT_RGB565,
24*4882a593Smuzhiyun DRM_FORMAT_XRGB8888,
25*4882a593Smuzhiyun DRM_FORMAT_ARGB8888,
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun
arc_pgu_set_pxl_fmt(struct drm_crtc * crtc)28*4882a593Smuzhiyun static void arc_pgu_set_pxl_fmt(struct drm_crtc *crtc)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
31*4882a593Smuzhiyun const struct drm_framebuffer *fb = crtc->primary->state->fb;
32*4882a593Smuzhiyun uint32_t pixel_format = fb->format->format;
33*4882a593Smuzhiyun u32 format = DRM_FORMAT_INVALID;
34*4882a593Smuzhiyun int i;
35*4882a593Smuzhiyun u32 reg_ctrl;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(arc_pgu_supported_formats); i++) {
38*4882a593Smuzhiyun if (arc_pgu_supported_formats[i] == pixel_format)
39*4882a593Smuzhiyun format = arc_pgu_supported_formats[i];
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun if (WARN_ON(format == DRM_FORMAT_INVALID))
43*4882a593Smuzhiyun return;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun reg_ctrl = arc_pgu_read(arcpgu, ARCPGU_REG_CTRL);
46*4882a593Smuzhiyun if (format == DRM_FORMAT_RGB565)
47*4882a593Smuzhiyun reg_ctrl &= ~ARCPGU_MODE_XRGB8888;
48*4882a593Smuzhiyun else
49*4882a593Smuzhiyun reg_ctrl |= ARCPGU_MODE_XRGB8888;
50*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_CTRL, reg_ctrl);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun static const struct drm_crtc_funcs arc_pgu_crtc_funcs = {
54*4882a593Smuzhiyun .destroy = drm_crtc_cleanup,
55*4882a593Smuzhiyun .set_config = drm_atomic_helper_set_config,
56*4882a593Smuzhiyun .page_flip = drm_atomic_helper_page_flip,
57*4882a593Smuzhiyun .reset = drm_atomic_helper_crtc_reset,
58*4882a593Smuzhiyun .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
59*4882a593Smuzhiyun .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun
arc_pgu_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)62*4882a593Smuzhiyun static enum drm_mode_status arc_pgu_crtc_mode_valid(struct drm_crtc *crtc,
63*4882a593Smuzhiyun const struct drm_display_mode *mode)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
66*4882a593Smuzhiyun long rate, clk_rate = mode->clock * 1000;
67*4882a593Smuzhiyun long diff = clk_rate / 200; /* +-0.5% allowed by HDMI spec */
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun rate = clk_round_rate(arcpgu->clk, clk_rate);
70*4882a593Smuzhiyun if ((max(rate, clk_rate) - min(rate, clk_rate) < diff) && (rate > 0))
71*4882a593Smuzhiyun return MODE_OK;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun return MODE_NOCLOCK;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
arc_pgu_crtc_mode_set_nofb(struct drm_crtc * crtc)76*4882a593Smuzhiyun static void arc_pgu_crtc_mode_set_nofb(struct drm_crtc *crtc)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
79*4882a593Smuzhiyun struct drm_display_mode *m = &crtc->state->adjusted_mode;
80*4882a593Smuzhiyun u32 val;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_FMT,
83*4882a593Smuzhiyun ENCODE_PGU_XY(m->crtc_htotal, m->crtc_vtotal));
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_HSYNC,
86*4882a593Smuzhiyun ENCODE_PGU_XY(m->crtc_hsync_start - m->crtc_hdisplay,
87*4882a593Smuzhiyun m->crtc_hsync_end - m->crtc_hdisplay));
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_VSYNC,
90*4882a593Smuzhiyun ENCODE_PGU_XY(m->crtc_vsync_start - m->crtc_vdisplay,
91*4882a593Smuzhiyun m->crtc_vsync_end - m->crtc_vdisplay));
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_ACTIVE,
94*4882a593Smuzhiyun ENCODE_PGU_XY(m->crtc_hblank_end - m->crtc_hblank_start,
95*4882a593Smuzhiyun m->crtc_vblank_end - m->crtc_vblank_start));
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun val = arc_pgu_read(arcpgu, ARCPGU_REG_CTRL);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun if (m->flags & DRM_MODE_FLAG_PVSYNC)
100*4882a593Smuzhiyun val |= ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST;
101*4882a593Smuzhiyun else
102*4882a593Smuzhiyun val &= ~(ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun if (m->flags & DRM_MODE_FLAG_PHSYNC)
105*4882a593Smuzhiyun val |= ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST;
106*4882a593Smuzhiyun else
107*4882a593Smuzhiyun val &= ~(ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_CTRL, val);
110*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_STRIDE, 0);
111*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_START_SET, 1);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun arc_pgu_set_pxl_fmt(crtc);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun clk_set_rate(arcpgu->clk, m->crtc_clock * 1000);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
arc_pgu_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)118*4882a593Smuzhiyun static void arc_pgu_crtc_atomic_enable(struct drm_crtc *crtc,
119*4882a593Smuzhiyun struct drm_crtc_state *old_state)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun clk_prepare_enable(arcpgu->clk);
124*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
125*4882a593Smuzhiyun arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) |
126*4882a593Smuzhiyun ARCPGU_CTRL_ENABLE_MASK);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
arc_pgu_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)129*4882a593Smuzhiyun static void arc_pgu_crtc_atomic_disable(struct drm_crtc *crtc,
130*4882a593Smuzhiyun struct drm_crtc_state *old_state)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun clk_disable_unprepare(arcpgu->clk);
135*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
136*4882a593Smuzhiyun arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) &
137*4882a593Smuzhiyun ~ARCPGU_CTRL_ENABLE_MASK);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun static const struct drm_crtc_helper_funcs arc_pgu_crtc_helper_funcs = {
141*4882a593Smuzhiyun .mode_valid = arc_pgu_crtc_mode_valid,
142*4882a593Smuzhiyun .mode_set_nofb = arc_pgu_crtc_mode_set_nofb,
143*4882a593Smuzhiyun .atomic_enable = arc_pgu_crtc_atomic_enable,
144*4882a593Smuzhiyun .atomic_disable = arc_pgu_crtc_atomic_disable,
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun
arc_pgu_plane_atomic_update(struct drm_plane * plane,struct drm_plane_state * state)147*4882a593Smuzhiyun static void arc_pgu_plane_atomic_update(struct drm_plane *plane,
148*4882a593Smuzhiyun struct drm_plane_state *state)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun struct arcpgu_drm_private *arcpgu;
151*4882a593Smuzhiyun struct drm_gem_cma_object *gem;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (!plane->state->crtc || !plane->state->fb)
154*4882a593Smuzhiyun return;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun arcpgu = crtc_to_arcpgu_priv(plane->state->crtc);
157*4882a593Smuzhiyun gem = drm_fb_cma_get_gem_obj(plane->state->fb, 0);
158*4882a593Smuzhiyun arc_pgu_write(arcpgu, ARCPGU_REG_BUF0_ADDR, gem->paddr);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun static const struct drm_plane_helper_funcs arc_pgu_plane_helper_funcs = {
162*4882a593Smuzhiyun .atomic_update = arc_pgu_plane_atomic_update,
163*4882a593Smuzhiyun };
164*4882a593Smuzhiyun
arc_pgu_plane_destroy(struct drm_plane * plane)165*4882a593Smuzhiyun static void arc_pgu_plane_destroy(struct drm_plane *plane)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun drm_plane_cleanup(plane);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun static const struct drm_plane_funcs arc_pgu_plane_funcs = {
171*4882a593Smuzhiyun .update_plane = drm_atomic_helper_update_plane,
172*4882a593Smuzhiyun .disable_plane = drm_atomic_helper_disable_plane,
173*4882a593Smuzhiyun .destroy = arc_pgu_plane_destroy,
174*4882a593Smuzhiyun .reset = drm_atomic_helper_plane_reset,
175*4882a593Smuzhiyun .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
176*4882a593Smuzhiyun .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
177*4882a593Smuzhiyun };
178*4882a593Smuzhiyun
arc_pgu_plane_init(struct drm_device * drm)179*4882a593Smuzhiyun static struct drm_plane *arc_pgu_plane_init(struct drm_device *drm)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun struct arcpgu_drm_private *arcpgu = drm->dev_private;
182*4882a593Smuzhiyun struct drm_plane *plane = NULL;
183*4882a593Smuzhiyun int ret;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
186*4882a593Smuzhiyun if (!plane)
187*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun ret = drm_universal_plane_init(drm, plane, 0xff, &arc_pgu_plane_funcs,
190*4882a593Smuzhiyun arc_pgu_supported_formats,
191*4882a593Smuzhiyun ARRAY_SIZE(arc_pgu_supported_formats),
192*4882a593Smuzhiyun NULL,
193*4882a593Smuzhiyun DRM_PLANE_TYPE_PRIMARY, NULL);
194*4882a593Smuzhiyun if (ret)
195*4882a593Smuzhiyun return ERR_PTR(ret);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun drm_plane_helper_add(plane, &arc_pgu_plane_helper_funcs);
198*4882a593Smuzhiyun arcpgu->plane = plane;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun return plane;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
arc_pgu_setup_crtc(struct drm_device * drm)203*4882a593Smuzhiyun int arc_pgu_setup_crtc(struct drm_device *drm)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun struct arcpgu_drm_private *arcpgu = drm->dev_private;
206*4882a593Smuzhiyun struct drm_plane *primary;
207*4882a593Smuzhiyun int ret;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun primary = arc_pgu_plane_init(drm);
210*4882a593Smuzhiyun if (IS_ERR(primary))
211*4882a593Smuzhiyun return PTR_ERR(primary);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun ret = drm_crtc_init_with_planes(drm, &arcpgu->crtc, primary, NULL,
214*4882a593Smuzhiyun &arc_pgu_crtc_funcs, NULL);
215*4882a593Smuzhiyun if (ret) {
216*4882a593Smuzhiyun arc_pgu_plane_destroy(primary);
217*4882a593Smuzhiyun return ret;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun drm_crtc_helper_add(&arcpgu->crtc, &arc_pgu_crtc_helper_funcs);
221*4882a593Smuzhiyun return 0;
222*4882a593Smuzhiyun }
223