blob: d1e8d31e37ee64409125b9e0113b14cd9e8e6e64 [file] [log] [blame]
Liviu Dudau8e22d792015-04-02 19:48:39 +01001/*
2 * Copyright (C) 2013-2015 ARM Limited
3 * Author: Liviu Dudau <Liviu.Dudau@arm.com>
4 *
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file COPYING in the main directory of this archive
7 * for more details.
8 *
9 * Implementation of a CRTC class for the HDLCD driver.
10 */
11
12#include <drm/drmP.h>
13#include <drm/drm_atomic_helper.h>
14#include <drm/drm_crtc.h>
15#include <drm/drm_crtc_helper.h>
16#include <drm/drm_fb_helper.h>
17#include <drm/drm_fb_cma_helper.h>
18#include <drm/drm_gem_cma_helper.h>
19#include <drm/drm_of.h>
20#include <drm/drm_plane_helper.h>
21#include <linux/clk.h>
22#include <linux/of_graph.h>
23#include <linux/platform_data/simplefb.h>
24#include <video/videomode.h>
25
26#include "hdlcd_drv.h"
27#include "hdlcd_regs.h"
28
29/*
30 * The HDLCD controller is a dumb RGB streamer that gets connected to
31 * a single HDMI transmitter or in the case of the ARM Models it gets
32 * emulated by the software that does the actual rendering.
33 *
34 */
35
Liviu Dudaua95acec2016-05-17 10:06:54 +010036static void hdlcd_crtc_cleanup(struct drm_crtc *crtc)
37{
38 struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
39
40 /* stop the controller on cleanup */
41 hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
42 drm_crtc_cleanup(crtc);
43}
44
Liviu Dudau8e22d792015-04-02 19:48:39 +010045static const struct drm_crtc_funcs hdlcd_crtc_funcs = {
Liviu Dudaua95acec2016-05-17 10:06:54 +010046 .destroy = hdlcd_crtc_cleanup,
Liviu Dudau8e22d792015-04-02 19:48:39 +010047 .set_config = drm_atomic_helper_set_config,
48 .page_flip = drm_atomic_helper_page_flip,
49 .reset = drm_atomic_helper_crtc_reset,
50 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
51 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
52};
53
54static struct simplefb_format supported_formats[] = SIMPLEFB_FORMATS;
55
56/*
57 * Setup the HDLCD registers for decoding the pixels out of the framebuffer
58 */
59static int hdlcd_set_pxl_fmt(struct drm_crtc *crtc)
60{
61 unsigned int btpp;
62 struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
63 uint32_t pixel_format;
64 struct simplefb_format *format = NULL;
65 int i;
66
67 pixel_format = crtc->primary->state->fb->pixel_format;
68
69 for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
70 if (supported_formats[i].fourcc == pixel_format)
71 format = &supported_formats[i];
72 }
73
74 if (WARN_ON(!format))
75 return 0;
76
77 /* HDLCD uses 'bytes per pixel', zero means 1 byte */
78 btpp = (format->bits_per_pixel + 7) / 8;
79 hdlcd_write(hdlcd, HDLCD_REG_PIXEL_FORMAT, (btpp - 1) << 3);
80
81 /*
82 * The format of the HDLCD_REG_<color>_SELECT register is:
83 * - bits[23:16] - default value for that color component
84 * - bits[11:8] - number of bits to extract for each color component
85 * - bits[4:0] - index of the lowest bit to extract
86 *
87 * The default color value is used when bits[11:8] are zero, when the
88 * pixel is outside the visible frame area or when there is a
89 * buffer underrun.
90 */
91 hdlcd_write(hdlcd, HDLCD_REG_RED_SELECT, format->red.offset |
92#ifdef CONFIG_DRM_HDLCD_SHOW_UNDERRUN
93 0x00ff0000 | /* show underruns in red */
94#endif
95 ((format->red.length & 0xf) << 8));
96 hdlcd_write(hdlcd, HDLCD_REG_GREEN_SELECT, format->green.offset |
97 ((format->green.length & 0xf) << 8));
98 hdlcd_write(hdlcd, HDLCD_REG_BLUE_SELECT, format->blue.offset |
99 ((format->blue.length & 0xf) << 8));
100
101 return 0;
102}
103
104static void hdlcd_crtc_mode_set_nofb(struct drm_crtc *crtc)
105{
106 struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
107 struct drm_display_mode *m = &crtc->state->adjusted_mode;
108 struct videomode vm;
109 unsigned int polarities, line_length, err;
110
111 vm.vfront_porch = m->crtc_vsync_start - m->crtc_vdisplay;
112 vm.vback_porch = m->crtc_vtotal - m->crtc_vsync_end;
113 vm.vsync_len = m->crtc_vsync_end - m->crtc_vsync_start;
114 vm.hfront_porch = m->crtc_hsync_start - m->crtc_hdisplay;
115 vm.hback_porch = m->crtc_htotal - m->crtc_hsync_end;
116 vm.hsync_len = m->crtc_hsync_end - m->crtc_hsync_start;
117
118 polarities = HDLCD_POLARITY_DATAEN | HDLCD_POLARITY_DATA;
119
120 if (m->flags & DRM_MODE_FLAG_PHSYNC)
121 polarities |= HDLCD_POLARITY_HSYNC;
122 if (m->flags & DRM_MODE_FLAG_PVSYNC)
123 polarities |= HDLCD_POLARITY_VSYNC;
124
125 line_length = crtc->primary->state->fb->pitches[0];
126
127 /* Allow max number of outstanding requests and largest burst size */
128 hdlcd_write(hdlcd, HDLCD_REG_BUS_OPTIONS,
129 HDLCD_BUS_MAX_OUTSTAND | HDLCD_BUS_BURST_16);
130
131 hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, line_length);
132 hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, line_length);
133 hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, m->crtc_vdisplay - 1);
134 hdlcd_write(hdlcd, HDLCD_REG_V_DATA, m->crtc_vdisplay - 1);
135 hdlcd_write(hdlcd, HDLCD_REG_V_BACK_PORCH, vm.vback_porch - 1);
136 hdlcd_write(hdlcd, HDLCD_REG_V_FRONT_PORCH, vm.vfront_porch - 1);
137 hdlcd_write(hdlcd, HDLCD_REG_V_SYNC, vm.vsync_len - 1);
138 hdlcd_write(hdlcd, HDLCD_REG_H_BACK_PORCH, vm.hback_porch - 1);
139 hdlcd_write(hdlcd, HDLCD_REG_H_FRONT_PORCH, vm.hfront_porch - 1);
140 hdlcd_write(hdlcd, HDLCD_REG_H_SYNC, vm.hsync_len - 1);
141 hdlcd_write(hdlcd, HDLCD_REG_H_DATA, m->crtc_hdisplay - 1);
142 hdlcd_write(hdlcd, HDLCD_REG_POLARITIES, polarities);
143
144 err = hdlcd_set_pxl_fmt(crtc);
145 if (err)
146 return;
147
148 clk_set_rate(hdlcd->clk, m->crtc_clock * 1000);
149}
150
151static void hdlcd_crtc_enable(struct drm_crtc *crtc)
152{
153 struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
154
155 clk_prepare_enable(hdlcd->clk);
156 hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 1);
157 drm_crtc_vblank_on(crtc);
158}
159
160static void hdlcd_crtc_disable(struct drm_crtc *crtc)
161{
162 struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
163
164 if (!crtc->primary->fb)
165 return;
166
Liviu Dudau8e22d792015-04-02 19:48:39 +0100167 hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
Liviu Dudaua95acec2016-05-17 10:06:54 +0100168 clk_disable_unprepare(hdlcd->clk);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100169 drm_crtc_vblank_off(crtc);
170}
171
172static int hdlcd_crtc_atomic_check(struct drm_crtc *crtc,
173 struct drm_crtc_state *state)
174{
175 struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
176 struct drm_display_mode *mode = &state->adjusted_mode;
177 long rate, clk_rate = mode->clock * 1000;
178
179 rate = clk_round_rate(hdlcd->clk, clk_rate);
180 if (rate != clk_rate) {
181 /* clock required by mode not supported by hardware */
182 return -EINVAL;
183 }
184
185 return 0;
186}
187
188static void hdlcd_crtc_atomic_begin(struct drm_crtc *crtc,
189 struct drm_crtc_state *state)
190{
191 struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
192 unsigned long flags;
193
194 if (crtc->state->event) {
195 struct drm_pending_vblank_event *event = crtc->state->event;
196
197 crtc->state->event = NULL;
198 event->pipe = drm_crtc_index(crtc);
199
200 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
201
202 spin_lock_irqsave(&crtc->dev->event_lock, flags);
203 list_add_tail(&event->base.link, &hdlcd->event_list);
204 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
205 }
206}
207
208static void hdlcd_crtc_atomic_flush(struct drm_crtc *crtc,
209 struct drm_crtc_state *state)
210{
211}
212
213static bool hdlcd_crtc_mode_fixup(struct drm_crtc *crtc,
214 const struct drm_display_mode *mode,
215 struct drm_display_mode *adjusted_mode)
216{
217 return true;
218}
219
220static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = {
221 .mode_fixup = hdlcd_crtc_mode_fixup,
222 .mode_set = drm_helper_crtc_mode_set,
223 .mode_set_base = drm_helper_crtc_mode_set_base,
224 .mode_set_nofb = hdlcd_crtc_mode_set_nofb,
225 .enable = hdlcd_crtc_enable,
226 .disable = hdlcd_crtc_disable,
227 .prepare = hdlcd_crtc_disable,
228 .commit = hdlcd_crtc_enable,
229 .atomic_check = hdlcd_crtc_atomic_check,
230 .atomic_begin = hdlcd_crtc_atomic_begin,
231 .atomic_flush = hdlcd_crtc_atomic_flush,
232};
233
234static int hdlcd_plane_atomic_check(struct drm_plane *plane,
235 struct drm_plane_state *state)
236{
237 return 0;
238}
239
240static void hdlcd_plane_atomic_update(struct drm_plane *plane,
241 struct drm_plane_state *state)
242{
243 struct hdlcd_drm_private *hdlcd;
244 struct drm_gem_cma_object *gem;
245 dma_addr_t scanout_start;
246
247 if (!plane->state->crtc || !plane->state->fb)
248 return;
249
250 hdlcd = crtc_to_hdlcd_priv(plane->state->crtc);
251 gem = drm_fb_cma_get_gem_obj(plane->state->fb, 0);
252 scanout_start = gem->paddr;
253 hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start);
254}
255
256static const struct drm_plane_helper_funcs hdlcd_plane_helper_funcs = {
257 .prepare_fb = NULL,
258 .cleanup_fb = NULL,
259 .atomic_check = hdlcd_plane_atomic_check,
260 .atomic_update = hdlcd_plane_atomic_update,
261};
262
263static void hdlcd_plane_destroy(struct drm_plane *plane)
264{
265 drm_plane_helper_disable(plane);
266 drm_plane_cleanup(plane);
267}
268
269static const struct drm_plane_funcs hdlcd_plane_funcs = {
270 .update_plane = drm_atomic_helper_update_plane,
271 .disable_plane = drm_atomic_helper_disable_plane,
272 .destroy = hdlcd_plane_destroy,
273 .reset = drm_atomic_helper_plane_reset,
274 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
275 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
276};
277
278static struct drm_plane *hdlcd_plane_init(struct drm_device *drm)
279{
280 struct hdlcd_drm_private *hdlcd = drm->dev_private;
281 struct drm_plane *plane = NULL;
282 u32 formats[ARRAY_SIZE(supported_formats)], i;
283 int ret;
284
285 plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
286 if (!plane)
287 return ERR_PTR(-ENOMEM);
288
289 for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
290 formats[i] = supported_formats[i].fourcc;
291
292 ret = drm_universal_plane_init(drm, plane, 0xff, &hdlcd_plane_funcs,
293 formats, ARRAY_SIZE(formats),
294 DRM_PLANE_TYPE_PRIMARY, NULL);
295 if (ret) {
296 devm_kfree(drm->dev, plane);
297 return ERR_PTR(ret);
298 }
299
300 drm_plane_helper_add(plane, &hdlcd_plane_helper_funcs);
301 hdlcd->plane = plane;
302
303 return plane;
304}
305
Liviu Dudau8e22d792015-04-02 19:48:39 +0100306int hdlcd_setup_crtc(struct drm_device *drm)
307{
308 struct hdlcd_drm_private *hdlcd = drm->dev_private;
309 struct drm_plane *primary;
310 int ret;
311
312 primary = hdlcd_plane_init(drm);
313 if (IS_ERR(primary))
314 return PTR_ERR(primary);
315
316 ret = drm_crtc_init_with_planes(drm, &hdlcd->crtc, primary, NULL,
317 &hdlcd_crtc_funcs, NULL);
318 if (ret) {
319 hdlcd_plane_destroy(primary);
320 devm_kfree(drm->dev, primary);
321 return ret;
322 }
323
324 drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs);
325 return 0;
326}