blob: a6fba9cfb0a6bc41a1b9dba6cc6aa984d9342875 [file] [log] [blame]
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +02001/*
2 * Copyright (C) STMicroelectronics SA 2014
3 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
4 * Fabien Dessenne <fabien.dessenne@st.com>
5 * for STMicroelectronics.
6 * License terms: GNU General Public License (GPL), version 2
7 */
8
Vincent Abrioudd86dc22016-02-10 10:48:20 +01009#include <drm/drm_atomic.h>
Vincent Abriou29d1dc62015-08-03 14:22:16 +020010#include <drm/drm_fb_cma_helper.h>
11#include <drm/drm_gem_cma_helper.h>
12
Benjamin Gaignardd2196732014-07-30 19:28:27 +020013#include "sti_compositor.h"
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020014#include "sti_gdp.h"
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020015#include "sti_plane.h"
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020016#include "sti_vtg.h"
17
Benjamin Gaignard4af6b122015-02-02 15:08:45 +010018#define ALPHASWITCH BIT(6)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020019#define ENA_COLOR_FILL BIT(8)
Benjamin Gaignard4af6b122015-02-02 15:08:45 +010020#define BIGNOTLITTLE BIT(23)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020021#define WAIT_NEXT_VSYNC BIT(31)
22
23/* GDP color formats */
24#define GDP_RGB565 0x00
25#define GDP_RGB888 0x01
26#define GDP_RGB888_32 0x02
Fabien Dessenne8adb5772015-02-04 18:12:53 +010027#define GDP_XBGR8888 (GDP_RGB888_32 | BIGNOTLITTLE | ALPHASWITCH)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020028#define GDP_ARGB8565 0x04
29#define GDP_ARGB8888 0x05
Vincent Abriou29d1dc62015-08-03 14:22:16 +020030#define GDP_ABGR8888 (GDP_ARGB8888 | BIGNOTLITTLE | ALPHASWITCH)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020031#define GDP_ARGB1555 0x06
32#define GDP_ARGB4444 0x07
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020033
34#define GAM_GDP_CTL_OFFSET 0x00
35#define GAM_GDP_AGC_OFFSET 0x04
36#define GAM_GDP_VPO_OFFSET 0x0C
37#define GAM_GDP_VPS_OFFSET 0x10
38#define GAM_GDP_PML_OFFSET 0x14
39#define GAM_GDP_PMP_OFFSET 0x18
40#define GAM_GDP_SIZE_OFFSET 0x1C
41#define GAM_GDP_NVN_OFFSET 0x24
42#define GAM_GDP_KEY1_OFFSET 0x28
43#define GAM_GDP_KEY2_OFFSET 0x2C
44#define GAM_GDP_PPT_OFFSET 0x34
45#define GAM_GDP_CML_OFFSET 0x3C
46#define GAM_GDP_MST_OFFSET 0x68
47
48#define GAM_GDP_ALPHARANGE_255 BIT(5)
49#define GAM_GDP_AGC_FULL_RANGE 0x00808080
50#define GAM_GDP_PPT_IGNORE (BIT(1) | BIT(0))
51#define GAM_GDP_SIZE_MAX 0x7FF
52
Vincent Abriou29d1dc62015-08-03 14:22:16 +020053#define GDP_NODE_NB_BANK 2
54#define GDP_NODE_PER_FIELD 2
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020055
56struct sti_gdp_node {
57 u32 gam_gdp_ctl;
58 u32 gam_gdp_agc;
59 u32 reserved1;
60 u32 gam_gdp_vpo;
61 u32 gam_gdp_vps;
62 u32 gam_gdp_pml;
63 u32 gam_gdp_pmp;
64 u32 gam_gdp_size;
65 u32 reserved2;
66 u32 gam_gdp_nvn;
67 u32 gam_gdp_key1;
68 u32 gam_gdp_key2;
69 u32 reserved3;
70 u32 gam_gdp_ppt;
71 u32 reserved4;
72 u32 gam_gdp_cml;
73};
74
75struct sti_gdp_node_list {
76 struct sti_gdp_node *top_field;
Benjamin Gaignarda51fe842014-12-04 11:21:48 +010077 dma_addr_t top_field_paddr;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020078 struct sti_gdp_node *btm_field;
Benjamin Gaignarda51fe842014-12-04 11:21:48 +010079 dma_addr_t btm_field_paddr;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020080};
81
82/**
83 * STI GDP structure
84 *
Vincent Abriou871bcdf2015-07-31 11:32:13 +020085 * @sti_plane: sti_plane structure
86 * @dev: driver device
87 * @regs: gdp registers
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020088 * @clk_pix: pixel clock for the current gdp
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +010089 * @clk_main_parent: gdp parent clock if main path used
90 * @clk_aux_parent: gdp parent clock if aux path used
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020091 * @vtg_field_nb: callback for VTG FIELD (top or bottom) notification
92 * @is_curr_top: true if the current node processed is the top field
Vincent Abriou871bcdf2015-07-31 11:32:13 +020093 * @node_list: array of node list
benjamin.gaignard@linaro.org20c47602016-01-07 14:30:37 +010094 * @vtg: registered vtg
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +020095 */
96struct sti_gdp {
Vincent Abriou871bcdf2015-07-31 11:32:13 +020097 struct sti_plane plane;
98 struct device *dev;
99 void __iomem *regs;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200100 struct clk *clk_pix;
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +0100101 struct clk *clk_main_parent;
102 struct clk *clk_aux_parent;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200103 struct notifier_block vtg_field_nb;
104 bool is_curr_top;
105 struct sti_gdp_node_list node_list[GDP_NODE_NB_BANK];
benjamin.gaignard@linaro.org20c47602016-01-07 14:30:37 +0100106 struct sti_vtg *vtg;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200107};
108
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200109#define to_sti_gdp(x) container_of(x, struct sti_gdp, plane)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200110
111static const uint32_t gdp_supported_formats[] = {
112 DRM_FORMAT_XRGB8888,
Fabien Dessenne8adb5772015-02-04 18:12:53 +0100113 DRM_FORMAT_XBGR8888,
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200114 DRM_FORMAT_ARGB8888,
Benjamin Gaignard4af6b122015-02-02 15:08:45 +0100115 DRM_FORMAT_ABGR8888,
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200116 DRM_FORMAT_ARGB4444,
117 DRM_FORMAT_ARGB1555,
118 DRM_FORMAT_RGB565,
119 DRM_FORMAT_RGB888,
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200120};
121
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200122static int sti_gdp_fourcc2format(int fourcc)
123{
124 switch (fourcc) {
125 case DRM_FORMAT_XRGB8888:
126 return GDP_RGB888_32;
Fabien Dessenne8adb5772015-02-04 18:12:53 +0100127 case DRM_FORMAT_XBGR8888:
128 return GDP_XBGR8888;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200129 case DRM_FORMAT_ARGB8888:
130 return GDP_ARGB8888;
Benjamin Gaignard4af6b122015-02-02 15:08:45 +0100131 case DRM_FORMAT_ABGR8888:
132 return GDP_ABGR8888;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200133 case DRM_FORMAT_ARGB4444:
134 return GDP_ARGB4444;
135 case DRM_FORMAT_ARGB1555:
136 return GDP_ARGB1555;
137 case DRM_FORMAT_RGB565:
138 return GDP_RGB565;
139 case DRM_FORMAT_RGB888:
140 return GDP_RGB888;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200141 }
142 return -1;
143}
144
145static int sti_gdp_get_alpharange(int format)
146{
147 switch (format) {
148 case GDP_ARGB8565:
149 case GDP_ARGB8888:
Benjamin Gaignard4af6b122015-02-02 15:08:45 +0100150 case GDP_ABGR8888:
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200151 return GAM_GDP_ALPHARANGE_255;
152 }
153 return 0;
154}
155
156/**
157 * sti_gdp_get_free_nodes
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200158 * @gdp: gdp pointer
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200159 *
160 * Look for a GDP node list that is not currently read by the HW.
161 *
162 * RETURNS:
163 * Pointer to the free GDP node list
164 */
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200165static struct sti_gdp_node_list *sti_gdp_get_free_nodes(struct sti_gdp *gdp)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200166{
167 int hw_nvn;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200168 unsigned int i;
169
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200170 hw_nvn = readl(gdp->regs + GAM_GDP_NVN_OFFSET);
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200171 if (!hw_nvn)
172 goto end;
173
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200174 for (i = 0; i < GDP_NODE_NB_BANK; i++)
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100175 if ((hw_nvn != gdp->node_list[i].btm_field_paddr) &&
176 (hw_nvn != gdp->node_list[i].top_field_paddr))
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200177 return &gdp->node_list[i];
178
Benjamin Gaignardd2196732014-07-30 19:28:27 +0200179 /* in hazardious cases restart with the first node */
180 DRM_ERROR("inconsistent NVN for %s: 0x%08X\n",
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200181 sti_plane_to_str(&gdp->plane), hw_nvn);
Benjamin Gaignardd2196732014-07-30 19:28:27 +0200182
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200183end:
184 return &gdp->node_list[0];
185}
186
187/**
188 * sti_gdp_get_current_nodes
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200189 * @gdp: gdp pointer
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200190 *
191 * Look for GDP nodes that are currently read by the HW.
192 *
193 * RETURNS:
194 * Pointer to the current GDP node list
195 */
196static
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200197struct sti_gdp_node_list *sti_gdp_get_current_nodes(struct sti_gdp *gdp)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200198{
199 int hw_nvn;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200200 unsigned int i;
201
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200202 hw_nvn = readl(gdp->regs + GAM_GDP_NVN_OFFSET);
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200203 if (!hw_nvn)
204 goto end;
205
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200206 for (i = 0; i < GDP_NODE_NB_BANK; i++)
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100207 if ((hw_nvn == gdp->node_list[i].btm_field_paddr) ||
208 (hw_nvn == gdp->node_list[i].top_field_paddr))
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200209 return &gdp->node_list[i];
210
211end:
Benjamin Gaignardd2196732014-07-30 19:28:27 +0200212 DRM_DEBUG_DRIVER("Warning, NVN 0x%08X for %s does not match any node\n",
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200213 hw_nvn, sti_plane_to_str(&gdp->plane));
Benjamin Gaignardd2196732014-07-30 19:28:27 +0200214
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200215 return NULL;
216}
217
218/**
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200219 * sti_gdp_disable
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200220 * @gdp: gdp pointer
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200221 *
222 * Disable a GDP.
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200223 */
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200224static void sti_gdp_disable(struct sti_gdp *gdp)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200225{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200226 unsigned int i;
Benjamin Gaignardd2196732014-07-30 19:28:27 +0200227
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200228 DRM_DEBUG_DRIVER("%s\n", sti_plane_to_str(&gdp->plane));
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200229
230 /* Set the nodes as 'to be ignored on mixer' */
231 for (i = 0; i < GDP_NODE_NB_BANK; i++) {
232 gdp->node_list[i].top_field->gam_gdp_ppt |= GAM_GDP_PPT_IGNORE;
233 gdp->node_list[i].btm_field->gam_gdp_ppt |= GAM_GDP_PPT_IGNORE;
234 }
235
benjamin.gaignard@linaro.org20c47602016-01-07 14:30:37 +0100236 if (sti_vtg_unregister_client(gdp->vtg, &gdp->vtg_field_nb))
Benjamin Gaignardd2196732014-07-30 19:28:27 +0200237 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
238
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200239 if (gdp->clk_pix)
240 clk_disable_unprepare(gdp->clk_pix);
241
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200242 gdp->plane.status = STI_PLANE_DISABLED;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200243}
244
245/**
246 * sti_gdp_field_cb
247 * @nb: notifier block
248 * @event: event message
249 * @data: private data
250 *
251 * Handle VTG top field and bottom field event.
252 *
253 * RETURNS:
254 * 0 on success.
255 */
256int sti_gdp_field_cb(struct notifier_block *nb,
257 unsigned long event, void *data)
258{
259 struct sti_gdp *gdp = container_of(nb, struct sti_gdp, vtg_field_nb);
260
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200261 if (gdp->plane.status == STI_PLANE_FLUSHING) {
262 /* disable need to be synchronize on vsync event */
263 DRM_DEBUG_DRIVER("Vsync event received => disable %s\n",
264 sti_plane_to_str(&gdp->plane));
265
266 sti_gdp_disable(gdp);
267 }
268
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200269 switch (event) {
270 case VTG_TOP_FIELD_EVENT:
271 gdp->is_curr_top = true;
272 break;
273 case VTG_BOTTOM_FIELD_EVENT:
274 gdp->is_curr_top = false;
275 break;
276 default:
277 DRM_ERROR("unsupported event: %lu\n", event);
278 break;
279 }
280
281 return 0;
282}
283
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200284static void sti_gdp_init(struct sti_gdp *gdp)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200285{
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200286 struct device_node *np = gdp->dev->of_node;
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100287 dma_addr_t dma_addr;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200288 void *base;
289 unsigned int i, size;
290
291 /* Allocate all the nodes within a single memory page */
292 size = sizeof(struct sti_gdp_node) *
293 GDP_NODE_PER_FIELD * GDP_NODE_NB_BANK;
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200294 base = dma_alloc_writecombine(gdp->dev,
295 size, &dma_addr, GFP_KERNEL | GFP_DMA);
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100296
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200297 if (!base) {
298 DRM_ERROR("Failed to allocate memory for GDP node\n");
299 return;
300 }
301 memset(base, 0, size);
302
303 for (i = 0; i < GDP_NODE_NB_BANK; i++) {
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100304 if (dma_addr & 0xF) {
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200305 DRM_ERROR("Mem alignment failed\n");
306 return;
307 }
308 gdp->node_list[i].top_field = base;
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100309 gdp->node_list[i].top_field_paddr = dma_addr;
310
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200311 DRM_DEBUG_DRIVER("node[%d].top_field=%p\n", i, base);
312 base += sizeof(struct sti_gdp_node);
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100313 dma_addr += sizeof(struct sti_gdp_node);
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200314
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100315 if (dma_addr & 0xF) {
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200316 DRM_ERROR("Mem alignment failed\n");
317 return;
318 }
319 gdp->node_list[i].btm_field = base;
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100320 gdp->node_list[i].btm_field_paddr = dma_addr;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200321 DRM_DEBUG_DRIVER("node[%d].btm_field=%p\n", i, base);
322 base += sizeof(struct sti_gdp_node);
Benjamin Gaignarda51fe842014-12-04 11:21:48 +0100323 dma_addr += sizeof(struct sti_gdp_node);
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200324 }
325
326 if (of_device_is_compatible(np, "st,stih407-compositor")) {
327 /* GDP of STiH407 chip have its own pixel clock */
328 char *clk_name;
329
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200330 switch (gdp->plane.desc) {
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200331 case STI_GDP_0:
332 clk_name = "pix_gdp1";
333 break;
334 case STI_GDP_1:
335 clk_name = "pix_gdp2";
336 break;
337 case STI_GDP_2:
338 clk_name = "pix_gdp3";
339 break;
340 case STI_GDP_3:
341 clk_name = "pix_gdp4";
342 break;
343 default:
344 DRM_ERROR("GDP id not recognized\n");
345 return;
346 }
347
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200348 gdp->clk_pix = devm_clk_get(gdp->dev, clk_name);
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200349 if (IS_ERR(gdp->clk_pix))
350 DRM_ERROR("Cannot get %s clock\n", clk_name);
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +0100351
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200352 gdp->clk_main_parent = devm_clk_get(gdp->dev, "main_parent");
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +0100353 if (IS_ERR(gdp->clk_main_parent))
354 DRM_ERROR("Cannot get main_parent clock\n");
355
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200356 gdp->clk_aux_parent = devm_clk_get(gdp->dev, "aux_parent");
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +0100357 if (IS_ERR(gdp->clk_aux_parent))
358 DRM_ERROR("Cannot get aux_parent clock\n");
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200359 }
360}
361
Bich Hemona5b9a712016-01-22 16:17:36 +0100362/**
363 * sti_gdp_get_dst
364 * @dev: device
365 * @dst: requested destination size
366 * @src: source size
367 *
368 * Return the cropped / clamped destination size
369 *
370 * RETURNS:
371 * cropped / clamped destination size
372 */
373static int sti_gdp_get_dst(struct device *dev, int dst, int src)
374{
375 if (dst == src)
376 return dst;
377
378 if (dst < src) {
379 dev_dbg(dev, "WARNING: GDP scale not supported, will crop\n");
380 return dst;
381 }
382
383 dev_dbg(dev, "WARNING: GDP scale not supported, will clamp\n");
384 return src;
385}
386
Vincent Abrioudd86dc22016-02-10 10:48:20 +0100387static int sti_gdp_atomic_check(struct drm_plane *drm_plane,
388 struct drm_plane_state *state)
389{
390 struct sti_plane *plane = to_sti_plane(drm_plane);
391 struct sti_gdp *gdp = to_sti_gdp(plane);
392 struct drm_crtc *crtc = state->crtc;
393 struct sti_compositor *compo = dev_get_drvdata(gdp->dev);
394 struct drm_framebuffer *fb = state->fb;
395 bool first_prepare = plane->status == STI_PLANE_DISABLED ? true : false;
396 struct drm_crtc_state *crtc_state;
397 struct sti_mixer *mixer;
398 struct drm_display_mode *mode;
399 int dst_x, dst_y, dst_w, dst_h;
400 int src_x, src_y, src_w, src_h;
401 int format;
402
403 /* no need for further checks if the plane is being disabled */
404 if (!crtc || !fb)
405 return 0;
406
407 mixer = to_sti_mixer(crtc);
408 crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
409 mode = &crtc_state->mode;
410 dst_x = state->crtc_x;
411 dst_y = state->crtc_y;
412 dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
413 dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
414 /* src_x are in 16.16 format */
415 src_x = state->src_x >> 16;
416 src_y = state->src_y >> 16;
417 src_w = clamp_val(state->src_w >> 16, 0, GAM_GDP_SIZE_MAX);
418 src_h = clamp_val(state->src_h >> 16, 0, GAM_GDP_SIZE_MAX);
419
420 format = sti_gdp_fourcc2format(fb->pixel_format);
421 if (format == -1) {
422 DRM_ERROR("Format not supported by GDP %.4s\n",
423 (char *)&fb->pixel_format);
424 return -EINVAL;
425 }
426
427 if (!drm_fb_cma_get_gem_obj(fb, 0)) {
428 DRM_ERROR("Can't get CMA GEM object for fb\n");
429 return -EINVAL;
430 }
431
432 if (first_prepare) {
433 /* Register gdp callback */
434 gdp->vtg = mixer->id == STI_MIXER_MAIN ?
435 compo->vtg_main : compo->vtg_aux;
436 if (sti_vtg_register_client(gdp->vtg,
437 &gdp->vtg_field_nb, crtc)) {
438 DRM_ERROR("Cannot register VTG notifier\n");
439 return -EINVAL;
440 }
441
442 /* Set and enable gdp clock */
443 if (gdp->clk_pix) {
444 struct clk *clkp;
445 int rate = mode->clock * 1000;
446 int res;
447
448 /*
449 * According to the mixer used, the gdp pixel clock
450 * should have a different parent clock.
451 */
452 if (mixer->id == STI_MIXER_MAIN)
453 clkp = gdp->clk_main_parent;
454 else
455 clkp = gdp->clk_aux_parent;
456
457 if (clkp)
458 clk_set_parent(gdp->clk_pix, clkp);
459
460 res = clk_set_rate(gdp->clk_pix, rate);
461 if (res < 0) {
462 DRM_ERROR("Cannot set rate (%dHz) for gdp\n",
463 rate);
464 return -EINVAL;
465 }
466
467 if (clk_prepare_enable(gdp->clk_pix)) {
468 DRM_ERROR("Failed to prepare/enable gdp\n");
469 return -EINVAL;
470 }
471 }
472 }
473
474 DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
475 crtc->base.id, sti_mixer_to_str(mixer),
476 drm_plane->base.id, sti_plane_to_str(plane));
477 DRM_DEBUG_KMS("%s dst=(%dx%d)@(%d,%d) - src=(%dx%d)@(%d,%d)\n",
478 sti_plane_to_str(plane),
479 dst_w, dst_h, dst_x, dst_y,
480 src_w, src_h, src_x, src_y);
481
482 return 0;
483}
484
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200485static void sti_gdp_atomic_update(struct drm_plane *drm_plane,
486 struct drm_plane_state *oldstate)
487{
488 struct drm_plane_state *state = drm_plane->state;
489 struct sti_plane *plane = to_sti_plane(drm_plane);
490 struct sti_gdp *gdp = to_sti_gdp(plane);
491 struct drm_crtc *crtc = state->crtc;
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200492 struct drm_framebuffer *fb = state->fb;
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200493 struct drm_display_mode *mode;
494 int dst_x, dst_y, dst_w, dst_h;
495 int src_x, src_y, src_w, src_h;
496 struct drm_gem_cma_object *cma_obj;
497 struct sti_gdp_node_list *list;
498 struct sti_gdp_node_list *curr_list;
499 struct sti_gdp_node *top_field, *btm_field;
500 u32 dma_updated_top;
501 u32 dma_updated_btm;
502 int format;
503 unsigned int depth, bpp;
504 u32 ydo, xdo, yds, xds;
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200505
Vincent Abrioudd86dc22016-02-10 10:48:20 +0100506 if (!crtc || !fb)
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200507 return;
508
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200509 mode = &crtc->mode;
510 dst_x = state->crtc_x;
511 dst_y = state->crtc_y;
512 dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
513 dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
514 /* src_x are in 16.16 format */
515 src_x = state->src_x >> 16;
516 src_y = state->src_y >> 16;
Bich Hemona5b9a712016-01-22 16:17:36 +0100517 src_w = clamp_val(state->src_w >> 16, 0, GAM_GDP_SIZE_MAX);
518 src_h = clamp_val(state->src_h >> 16, 0, GAM_GDP_SIZE_MAX);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200519
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200520 list = sti_gdp_get_free_nodes(gdp);
521 top_field = list->top_field;
522 btm_field = list->btm_field;
523
524 dev_dbg(gdp->dev, "%s %s top_node:0x%p btm_node:0x%p\n", __func__,
525 sti_plane_to_str(plane), top_field, btm_field);
526
527 /* build the top field */
528 top_field->gam_gdp_agc = GAM_GDP_AGC_FULL_RANGE;
529 top_field->gam_gdp_ctl = WAIT_NEXT_VSYNC;
530 format = sti_gdp_fourcc2format(fb->pixel_format);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200531 top_field->gam_gdp_ctl |= format;
532 top_field->gam_gdp_ctl |= sti_gdp_get_alpharange(format);
533 top_field->gam_gdp_ppt &= ~GAM_GDP_PPT_IGNORE;
534
535 cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200536
537 DRM_DEBUG_DRIVER("drm FB:%d format:%.4s phys@:0x%lx\n", fb->base.id,
538 (char *)&fb->pixel_format,
539 (unsigned long)cma_obj->paddr);
540
541 /* pixel memory location */
542 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
543 top_field->gam_gdp_pml = (u32)cma_obj->paddr + fb->offsets[0];
544 top_field->gam_gdp_pml += src_x * (bpp >> 3);
545 top_field->gam_gdp_pml += src_y * fb->pitches[0];
546
Bich Hemona5b9a712016-01-22 16:17:36 +0100547 /* output parameters (clamped / cropped) */
548 dst_w = sti_gdp_get_dst(gdp->dev, dst_w, src_w);
549 dst_h = sti_gdp_get_dst(gdp->dev, dst_h, src_h);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200550 ydo = sti_vtg_get_line_number(*mode, dst_y);
551 yds = sti_vtg_get_line_number(*mode, dst_y + dst_h - 1);
552 xdo = sti_vtg_get_pixel_number(*mode, dst_x);
553 xds = sti_vtg_get_pixel_number(*mode, dst_x + dst_w - 1);
554 top_field->gam_gdp_vpo = (ydo << 16) | xdo;
555 top_field->gam_gdp_vps = (yds << 16) | xds;
556
Vincent Abriou704cb302016-02-09 17:08:56 +0100557 /* input parameters */
558 src_w = dst_w;
559 top_field->gam_gdp_pmp = fb->pitches[0];
560 top_field->gam_gdp_size = src_h << 16 | src_w;
561
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200562 /* Same content and chained together */
563 memcpy(btm_field, top_field, sizeof(*btm_field));
564 top_field->gam_gdp_nvn = list->btm_field_paddr;
565 btm_field->gam_gdp_nvn = list->top_field_paddr;
566
567 /* Interlaced mode */
568 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
569 btm_field->gam_gdp_pml = top_field->gam_gdp_pml +
570 fb->pitches[0];
571
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200572 /* Update the NVN field of the 'right' field of the current GDP node
573 * (being used by the HW) with the address of the updated ('free') top
574 * field GDP node.
575 * - In interlaced mode the 'right' field is the bottom field as we
576 * update frames starting from their top field
577 * - In progressive mode, we update both bottom and top fields which
578 * are equal nodes.
579 * At the next VSYNC, the updated node list will be used by the HW.
580 */
581 curr_list = sti_gdp_get_current_nodes(gdp);
582 dma_updated_top = list->top_field_paddr;
583 dma_updated_btm = list->btm_field_paddr;
584
585 dev_dbg(gdp->dev, "Current NVN:0x%X\n",
586 readl(gdp->regs + GAM_GDP_NVN_OFFSET));
587 dev_dbg(gdp->dev, "Posted buff: %lx current buff: %x\n",
588 (unsigned long)cma_obj->paddr,
589 readl(gdp->regs + GAM_GDP_PML_OFFSET));
590
591 if (!curr_list) {
592 /* First update or invalid node should directly write in the
593 * hw register */
594 DRM_DEBUG_DRIVER("%s first update (or invalid node)",
595 sti_plane_to_str(plane));
596
597 writel(gdp->is_curr_top ?
598 dma_updated_btm : dma_updated_top,
599 gdp->regs + GAM_GDP_NVN_OFFSET);
600 goto end;
601 }
602
603 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
604 if (gdp->is_curr_top) {
605 /* Do not update in the middle of the frame, but
606 * postpone the update after the bottom field has
607 * been displayed */
608 curr_list->btm_field->gam_gdp_nvn = dma_updated_top;
609 } else {
610 /* Direct update to avoid one frame delay */
611 writel(dma_updated_top,
612 gdp->regs + GAM_GDP_NVN_OFFSET);
613 }
614 } else {
615 /* Direct update for progressive to avoid one frame delay */
616 writel(dma_updated_top, gdp->regs + GAM_GDP_NVN_OFFSET);
617 }
618
619end:
620 plane->status = STI_PLANE_UPDATED;
621}
622
623static void sti_gdp_atomic_disable(struct drm_plane *drm_plane,
624 struct drm_plane_state *oldstate)
625{
626 struct sti_plane *plane = to_sti_plane(drm_plane);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200627
628 if (!drm_plane->crtc) {
629 DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
630 drm_plane->base.id);
631 return;
632 }
633
634 DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
Vincent Abrioudd86dc22016-02-10 10:48:20 +0100635 drm_plane->crtc->base.id,
636 sti_mixer_to_str(to_sti_mixer(drm_plane->crtc)),
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200637 drm_plane->base.id, sti_plane_to_str(plane));
638
639 plane->status = STI_PLANE_DISABLING;
640}
641
642static const struct drm_plane_helper_funcs sti_gdp_helpers_funcs = {
Vincent Abrioudd86dc22016-02-10 10:48:20 +0100643 .atomic_check = sti_gdp_atomic_check,
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200644 .atomic_update = sti_gdp_atomic_update,
645 .atomic_disable = sti_gdp_atomic_disable,
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200646};
647
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200648struct drm_plane *sti_gdp_create(struct drm_device *drm_dev,
649 struct device *dev, int desc,
650 void __iomem *baseaddr,
651 unsigned int possible_crtcs,
652 enum drm_plane_type type)
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200653{
654 struct sti_gdp *gdp;
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200655 int res;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200656
657 gdp = devm_kzalloc(dev, sizeof(*gdp), GFP_KERNEL);
658 if (!gdp) {
659 DRM_ERROR("Failed to allocate memory for GDP\n");
660 return NULL;
661 }
662
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200663 gdp->dev = dev;
664 gdp->regs = baseaddr;
665 gdp->plane.desc = desc;
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200666 gdp->plane.status = STI_PLANE_DISABLED;
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200667
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200668 gdp->vtg_field_nb.notifier_call = sti_gdp_field_cb;
669
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200670 sti_gdp_init(gdp);
671
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200672 res = drm_universal_plane_init(drm_dev, &gdp->plane.drm_plane,
673 possible_crtcs,
674 &sti_plane_helpers_funcs,
675 gdp_supported_formats,
676 ARRAY_SIZE(gdp_supported_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +0200677 type, NULL);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200678 if (res) {
679 DRM_ERROR("Failed to initialize universal plane\n");
680 goto err;
681 }
682
683 drm_plane_helper_add(&gdp->plane.drm_plane, &sti_gdp_helpers_funcs);
684
685 sti_plane_init_property(&gdp->plane, type);
686
687 return &gdp->plane.drm_plane;
688
689err:
690 devm_kfree(dev, gdp);
691 return NULL;
Benjamin Gaignardba2d53f2014-07-30 18:48:35 +0200692}