blob: 92f8beff8e6076ca36e01f36b114ebf5623a200e [file] [log] [blame]
Carlos Palminha51dacf22016-02-19 15:30:26 +03001/*
2 * ARC PGU DRM driver.
3 *
4 * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <drm/drm_atomic_helper.h>
18#include <drm/drm_crtc_helper.h>
19#include <drm/drm_fb_cma_helper.h>
20#include <drm/drm_gem_cma_helper.h>
21#include <drm/drm_plane_helper.h>
22#include <linux/clk.h>
23#include <linux/platform_data/simplefb.h>
24
25#include "arcpgu.h"
26#include "arcpgu_regs.h"
27
28#define ENCODE_PGU_XY(x, y) ((((x) - 1) << 16) | ((y) - 1))
29
30static struct simplefb_format supported_formats[] = {
31 { "r5g6b5", 16, {11, 5}, {5, 6}, {0, 5}, {0, 0}, DRM_FORMAT_RGB565 },
32 { "r8g8b8", 24, {16, 8}, {8, 8}, {0, 8}, {0, 0}, DRM_FORMAT_RGB888 },
33};
34
35static void arc_pgu_set_pxl_fmt(struct drm_crtc *crtc)
36{
37 struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
38 uint32_t pixel_format = crtc->primary->state->fb->pixel_format;
39 struct simplefb_format *format = NULL;
40 int i;
41
42 for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
43 if (supported_formats[i].fourcc == pixel_format)
44 format = &supported_formats[i];
45 }
46
47 if (WARN_ON(!format))
48 return;
49
50 if (format->fourcc == DRM_FORMAT_RGB888)
51 arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
52 arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) |
53 ARCPGU_MODE_RGB888_MASK);
54
55}
56
57static const struct drm_crtc_funcs arc_pgu_crtc_funcs = {
58 .destroy = drm_crtc_cleanup,
59 .set_config = drm_atomic_helper_set_config,
60 .page_flip = drm_atomic_helper_page_flip,
61 .reset = drm_atomic_helper_crtc_reset,
62 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
63 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
64};
65
66static void arc_pgu_crtc_mode_set_nofb(struct drm_crtc *crtc)
67{
68 struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
69 struct drm_display_mode *m = &crtc->state->adjusted_mode;
70 u32 val;
71
72 arc_pgu_write(arcpgu, ARCPGU_REG_FMT,
73 ENCODE_PGU_XY(m->crtc_htotal, m->crtc_vtotal));
74
75 arc_pgu_write(arcpgu, ARCPGU_REG_HSYNC,
76 ENCODE_PGU_XY(m->crtc_hsync_start - m->crtc_hdisplay,
77 m->crtc_hsync_end - m->crtc_hdisplay));
78
79 arc_pgu_write(arcpgu, ARCPGU_REG_VSYNC,
80 ENCODE_PGU_XY(m->crtc_vsync_start - m->crtc_vdisplay,
81 m->crtc_vsync_end - m->crtc_vdisplay));
82
83 arc_pgu_write(arcpgu, ARCPGU_REG_ACTIVE,
84 ENCODE_PGU_XY(m->crtc_hblank_end - m->crtc_hblank_start,
85 m->crtc_vblank_end - m->crtc_vblank_start));
86
87 val = arc_pgu_read(arcpgu, ARCPGU_REG_CTRL);
88
89 if (m->flags & DRM_MODE_FLAG_PVSYNC)
90 val |= ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST;
91 else
92 val &= ~(ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST);
93
94 if (m->flags & DRM_MODE_FLAG_PHSYNC)
95 val |= ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST;
96 else
97 val &= ~(ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST);
98
99 arc_pgu_write(arcpgu, ARCPGU_REG_CTRL, val);
100 arc_pgu_write(arcpgu, ARCPGU_REG_STRIDE, 0);
101 arc_pgu_write(arcpgu, ARCPGU_REG_START_SET, 1);
102
103 arc_pgu_set_pxl_fmt(crtc);
104
105 clk_set_rate(arcpgu->clk, m->crtc_clock * 1000);
106}
107
108static void arc_pgu_crtc_enable(struct drm_crtc *crtc)
109{
110 struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
111
112 clk_prepare_enable(arcpgu->clk);
113 arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
114 arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) |
115 ARCPGU_CTRL_ENABLE_MASK);
116}
117
118static void arc_pgu_crtc_disable(struct drm_crtc *crtc)
119{
120 struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
121
122 if (!crtc->primary->fb)
123 return;
124
125 clk_disable_unprepare(arcpgu->clk);
126 arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
127 arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) &
128 ~ARCPGU_CTRL_ENABLE_MASK);
129}
130
131static int arc_pgu_crtc_atomic_check(struct drm_crtc *crtc,
132 struct drm_crtc_state *state)
133{
134 struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
135 struct drm_display_mode *mode = &state->adjusted_mode;
136 long rate, clk_rate = mode->clock * 1000;
137
138 rate = clk_round_rate(arcpgu->clk, clk_rate);
139 if (rate != clk_rate)
140 return -EINVAL;
141
142 return 0;
143}
144
145static void arc_pgu_crtc_atomic_begin(struct drm_crtc *crtc,
146 struct drm_crtc_state *state)
147{
148 struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
149 unsigned long flags;
150
151 if (crtc->state->event) {
152 struct drm_pending_vblank_event *event = crtc->state->event;
153
154 crtc->state->event = NULL;
155 event->pipe = drm_crtc_index(crtc);
156
157 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
158
159 spin_lock_irqsave(&crtc->dev->event_lock, flags);
160 list_add_tail(&event->base.link, &arcpgu->event_list);
161 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
162 }
163}
164
165static const struct drm_crtc_helper_funcs arc_pgu_crtc_helper_funcs = {
166 .mode_set = drm_helper_crtc_mode_set,
167 .mode_set_base = drm_helper_crtc_mode_set_base,
168 .mode_set_nofb = arc_pgu_crtc_mode_set_nofb,
169 .enable = arc_pgu_crtc_enable,
170 .disable = arc_pgu_crtc_disable,
171 .prepare = arc_pgu_crtc_disable,
172 .commit = arc_pgu_crtc_enable,
173 .atomic_check = arc_pgu_crtc_atomic_check,
174 .atomic_begin = arc_pgu_crtc_atomic_begin,
175};
176
177static void arc_pgu_plane_atomic_update(struct drm_plane *plane,
178 struct drm_plane_state *state)
179{
180 struct arcpgu_drm_private *arcpgu;
181 struct drm_gem_cma_object *gem;
182
183 if (!plane->state->crtc || !plane->state->fb)
184 return;
185
186 arcpgu = crtc_to_arcpgu_priv(plane->state->crtc);
187 gem = drm_fb_cma_get_gem_obj(plane->state->fb, 0);
188 arc_pgu_write(arcpgu, ARCPGU_REG_BUF0_ADDR, gem->paddr);
189}
190
191static const struct drm_plane_helper_funcs arc_pgu_plane_helper_funcs = {
192 .prepare_fb = NULL,
193 .cleanup_fb = NULL,
194 .atomic_update = arc_pgu_plane_atomic_update,
195};
196
197static void arc_pgu_plane_destroy(struct drm_plane *plane)
198{
199 drm_plane_helper_disable(plane);
200 drm_plane_cleanup(plane);
201}
202
203static const struct drm_plane_funcs arc_pgu_plane_funcs = {
204 .update_plane = drm_atomic_helper_update_plane,
205 .disable_plane = drm_atomic_helper_disable_plane,
206 .destroy = arc_pgu_plane_destroy,
207 .reset = drm_atomic_helper_plane_reset,
208 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
209 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
210};
211
212static struct drm_plane *arc_pgu_plane_init(struct drm_device *drm)
213{
214 struct arcpgu_drm_private *arcpgu = drm->dev_private;
215 struct drm_plane *plane = NULL;
216 u32 formats[ARRAY_SIZE(supported_formats)], i;
217 int ret;
218
219 plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
220 if (!plane)
221 return ERR_PTR(-ENOMEM);
222
223 for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
224 formats[i] = supported_formats[i].fourcc;
225
226 ret = drm_universal_plane_init(drm, plane, 0xff, &arc_pgu_plane_funcs,
227 formats, ARRAY_SIZE(formats),
228 DRM_PLANE_TYPE_PRIMARY, NULL);
229 if (ret)
230 return ERR_PTR(ret);
231
232 drm_plane_helper_add(plane, &arc_pgu_plane_helper_funcs);
233 arcpgu->plane = plane;
234
235 return plane;
236}
237
238int arc_pgu_setup_crtc(struct drm_device *drm)
239{
240 struct arcpgu_drm_private *arcpgu = drm->dev_private;
241 struct drm_plane *primary;
242 int ret;
243
244 primary = arc_pgu_plane_init(drm);
245 if (IS_ERR(primary))
246 return PTR_ERR(primary);
247
248 ret = drm_crtc_init_with_planes(drm, &arcpgu->crtc, primary, NULL,
249 &arc_pgu_crtc_funcs, NULL);
250 if (ret) {
251 arc_pgu_plane_destroy(primary);
252 return ret;
253 }
254
255 drm_crtc_helper_add(&arcpgu->crtc, &arc_pgu_crtc_helper_funcs);
256 return 0;
257}