blob: c1ea5c36b0061a9220a0c168eab6a8d588054f25 [file] [log] [blame]
Boris Brezillon1a396782015-01-06 11:13:28 +01001/*
2 * Copyright (C) 2014 Traphandler
3 * Copyright (C) 2014 Free Electrons
4 * Copyright (C) 2014 Atmel
5 *
6 * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
7 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <linux/clk.h>
23#include <linux/irq.h>
24#include <linux/irqchip.h>
25#include <linux/module.h>
26#include <linux/pm_runtime.h>
27
28#include "atmel_hlcdc_dc.h"
29
30#define ATMEL_HLCDC_LAYER_IRQS_OFFSET 8
31
Boris Brezillon6b22cad2015-01-07 10:12:41 +010032static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9n12_layers[] = {
33 {
34 .name = "base",
35 .formats = &atmel_hlcdc_plane_rgb_formats,
36 .regs_offset = 0x40,
37 .id = 0,
38 .type = ATMEL_HLCDC_BASE_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +010039 .cfgs_offset = 0x2c,
Boris Brezillon6b22cad2015-01-07 10:12:41 +010040 .layout = {
41 .xstride = { 2 },
42 .default_color = 3,
43 .general_config = 4,
44 },
Peter Rosin364a7bf2017-06-22 07:03:11 +020045 .clut_offset = 0x400,
Boris Brezillon6b22cad2015-01-07 10:12:41 +010046 },
47};
48
49static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9n12 = {
50 .min_width = 0,
51 .min_height = 0,
52 .max_width = 1280,
53 .max_height = 860,
Boris Brezillon79a3fc22016-01-05 18:11:39 +010054 .max_spw = 0x3f,
55 .max_vpw = 0x3f,
56 .max_hpw = 0xff,
Boris Brezillonaca63b72016-01-06 11:14:15 +010057 .conflicting_output_formats = true,
Boris Brezillon6b22cad2015-01-07 10:12:41 +010058 .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9n12_layers),
59 .layers = atmel_hlcdc_at91sam9n12_layers,
60};
61
Boris Brezillon348ef852015-01-07 09:30:20 +010062static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
63 {
64 .name = "base",
65 .formats = &atmel_hlcdc_plane_rgb_formats,
66 .regs_offset = 0x40,
67 .id = 0,
68 .type = ATMEL_HLCDC_BASE_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +010069 .cfgs_offset = 0x2c,
Boris Brezillon348ef852015-01-07 09:30:20 +010070 .layout = {
71 .xstride = { 2 },
72 .default_color = 3,
73 .general_config = 4,
74 .disc_pos = 5,
75 .disc_size = 6,
76 },
Peter Rosin364a7bf2017-06-22 07:03:11 +020077 .clut_offset = 0x400,
Boris Brezillon348ef852015-01-07 09:30:20 +010078 },
79 {
80 .name = "overlay1",
81 .formats = &atmel_hlcdc_plane_rgb_formats,
82 .regs_offset = 0x100,
83 .id = 1,
84 .type = ATMEL_HLCDC_OVERLAY_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +010085 .cfgs_offset = 0x2c,
Boris Brezillon348ef852015-01-07 09:30:20 +010086 .layout = {
87 .pos = 2,
88 .size = 3,
89 .xstride = { 4 },
90 .pstride = { 5 },
91 .default_color = 6,
92 .chroma_key = 7,
93 .chroma_key_mask = 8,
94 .general_config = 9,
95 },
Peter Rosin364a7bf2017-06-22 07:03:11 +020096 .clut_offset = 0x800,
Boris Brezillon348ef852015-01-07 09:30:20 +010097 },
98 {
99 .name = "high-end-overlay",
100 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
101 .regs_offset = 0x280,
102 .id = 2,
103 .type = ATMEL_HLCDC_OVERLAY_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100104 .cfgs_offset = 0x4c,
Boris Brezillon348ef852015-01-07 09:30:20 +0100105 .layout = {
106 .pos = 2,
107 .size = 3,
108 .memsize = 4,
109 .xstride = { 5, 7 },
110 .pstride = { 6, 8 },
111 .default_color = 9,
112 .chroma_key = 10,
113 .chroma_key_mask = 11,
114 .general_config = 12,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100115 .scaler_config = 13,
Boris Brezillon348ef852015-01-07 09:30:20 +0100116 .csc = 14,
117 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200118 .clut_offset = 0x1000,
Boris Brezillon348ef852015-01-07 09:30:20 +0100119 },
120 {
121 .name = "cursor",
122 .formats = &atmel_hlcdc_plane_rgb_formats,
123 .regs_offset = 0x340,
124 .id = 3,
125 .type = ATMEL_HLCDC_CURSOR_LAYER,
Boris Brezillon348ef852015-01-07 09:30:20 +0100126 .max_width = 128,
127 .max_height = 128,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100128 .cfgs_offset = 0x2c,
Boris Brezillon348ef852015-01-07 09:30:20 +0100129 .layout = {
130 .pos = 2,
131 .size = 3,
132 .xstride = { 4 },
133 .default_color = 6,
134 .chroma_key = 7,
135 .chroma_key_mask = 8,
136 .general_config = 9,
137 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200138 .clut_offset = 0x1400,
Boris Brezillon348ef852015-01-07 09:30:20 +0100139 },
140};
141
142static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9x5 = {
143 .min_width = 0,
144 .min_height = 0,
145 .max_width = 800,
146 .max_height = 600,
Boris Brezillon79a3fc22016-01-05 18:11:39 +0100147 .max_spw = 0x3f,
148 .max_vpw = 0x3f,
149 .max_hpw = 0xff,
Boris Brezillonaca63b72016-01-06 11:14:15 +0100150 .conflicting_output_formats = true,
Boris Brezillon348ef852015-01-07 09:30:20 +0100151 .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9x5_layers),
152 .layers = atmel_hlcdc_at91sam9x5_layers,
153};
154
Boris Brezillon1a396782015-01-06 11:13:28 +0100155static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
156 {
157 .name = "base",
158 .formats = &atmel_hlcdc_plane_rgb_formats,
159 .regs_offset = 0x40,
160 .id = 0,
161 .type = ATMEL_HLCDC_BASE_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100162 .cfgs_offset = 0x2c,
Boris Brezillon1a396782015-01-06 11:13:28 +0100163 .layout = {
164 .xstride = { 2 },
165 .default_color = 3,
166 .general_config = 4,
167 .disc_pos = 5,
168 .disc_size = 6,
169 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200170 .clut_offset = 0x600,
Boris Brezillon1a396782015-01-06 11:13:28 +0100171 },
172 {
173 .name = "overlay1",
174 .formats = &atmel_hlcdc_plane_rgb_formats,
175 .regs_offset = 0x140,
176 .id = 1,
177 .type = ATMEL_HLCDC_OVERLAY_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100178 .cfgs_offset = 0x2c,
Boris Brezillon1a396782015-01-06 11:13:28 +0100179 .layout = {
180 .pos = 2,
181 .size = 3,
182 .xstride = { 4 },
183 .pstride = { 5 },
184 .default_color = 6,
185 .chroma_key = 7,
186 .chroma_key_mask = 8,
187 .general_config = 9,
188 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200189 .clut_offset = 0xa00,
Boris Brezillon1a396782015-01-06 11:13:28 +0100190 },
191 {
192 .name = "overlay2",
193 .formats = &atmel_hlcdc_plane_rgb_formats,
194 .regs_offset = 0x240,
195 .id = 2,
196 .type = ATMEL_HLCDC_OVERLAY_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100197 .cfgs_offset = 0x2c,
Boris Brezillon1a396782015-01-06 11:13:28 +0100198 .layout = {
199 .pos = 2,
200 .size = 3,
201 .xstride = { 4 },
202 .pstride = { 5 },
203 .default_color = 6,
204 .chroma_key = 7,
205 .chroma_key_mask = 8,
206 .general_config = 9,
207 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200208 .clut_offset = 0xe00,
Boris Brezillon1a396782015-01-06 11:13:28 +0100209 },
210 {
211 .name = "high-end-overlay",
212 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
213 .regs_offset = 0x340,
214 .id = 3,
215 .type = ATMEL_HLCDC_OVERLAY_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100216 .cfgs_offset = 0x4c,
Boris Brezillon1a396782015-01-06 11:13:28 +0100217 .layout = {
218 .pos = 2,
219 .size = 3,
220 .memsize = 4,
221 .xstride = { 5, 7 },
222 .pstride = { 6, 8 },
223 .default_color = 9,
224 .chroma_key = 10,
225 .chroma_key_mask = 11,
226 .general_config = 12,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100227 .scaler_config = 13,
228 .phicoeffs = {
229 .x = 17,
230 .y = 33,
231 },
Boris Brezillon1a396782015-01-06 11:13:28 +0100232 .csc = 14,
233 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200234 .clut_offset = 0x1200,
Boris Brezillon1a396782015-01-06 11:13:28 +0100235 },
236 {
237 .name = "cursor",
238 .formats = &atmel_hlcdc_plane_rgb_formats,
239 .regs_offset = 0x440,
240 .id = 4,
241 .type = ATMEL_HLCDC_CURSOR_LAYER,
Boris Brezillon1a396782015-01-06 11:13:28 +0100242 .max_width = 128,
243 .max_height = 128,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100244 .cfgs_offset = 0x2c,
Boris Brezillon1a396782015-01-06 11:13:28 +0100245 .layout = {
246 .pos = 2,
247 .size = 3,
248 .xstride = { 4 },
249 .pstride = { 5 },
250 .default_color = 6,
251 .chroma_key = 7,
252 .chroma_key_mask = 8,
253 .general_config = 9,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100254 .scaler_config = 13,
Boris Brezillon1a396782015-01-06 11:13:28 +0100255 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200256 .clut_offset = 0x1600,
Boris Brezillon1a396782015-01-06 11:13:28 +0100257 },
258};
259
260static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d3 = {
261 .min_width = 0,
262 .min_height = 0,
263 .max_width = 2048,
264 .max_height = 2048,
Boris Brezillon79a3fc22016-01-05 18:11:39 +0100265 .max_spw = 0x3f,
266 .max_vpw = 0x3f,
267 .max_hpw = 0x1ff,
Boris Brezillonaca63b72016-01-06 11:14:15 +0100268 .conflicting_output_formats = true,
Boris Brezillon1a396782015-01-06 11:13:28 +0100269 .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d3_layers),
270 .layers = atmel_hlcdc_sama5d3_layers,
271};
272
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100273static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers[] = {
274 {
275 .name = "base",
276 .formats = &atmel_hlcdc_plane_rgb_formats,
277 .regs_offset = 0x40,
278 .id = 0,
279 .type = ATMEL_HLCDC_BASE_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100280 .cfgs_offset = 0x2c,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100281 .layout = {
282 .xstride = { 2 },
283 .default_color = 3,
284 .general_config = 4,
285 .disc_pos = 5,
286 .disc_size = 6,
287 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200288 .clut_offset = 0x600,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100289 },
290 {
291 .name = "overlay1",
292 .formats = &atmel_hlcdc_plane_rgb_formats,
293 .regs_offset = 0x140,
294 .id = 1,
295 .type = ATMEL_HLCDC_OVERLAY_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100296 .cfgs_offset = 0x2c,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100297 .layout = {
298 .pos = 2,
299 .size = 3,
300 .xstride = { 4 },
301 .pstride = { 5 },
302 .default_color = 6,
303 .chroma_key = 7,
304 .chroma_key_mask = 8,
305 .general_config = 9,
306 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200307 .clut_offset = 0xa00,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100308 },
309 {
310 .name = "overlay2",
311 .formats = &atmel_hlcdc_plane_rgb_formats,
312 .regs_offset = 0x240,
313 .id = 2,
314 .type = ATMEL_HLCDC_OVERLAY_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100315 .cfgs_offset = 0x2c,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100316 .layout = {
317 .pos = 2,
318 .size = 3,
319 .xstride = { 4 },
320 .pstride = { 5 },
321 .default_color = 6,
322 .chroma_key = 7,
323 .chroma_key_mask = 8,
324 .general_config = 9,
325 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200326 .clut_offset = 0xe00,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100327 },
328 {
329 .name = "high-end-overlay",
330 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
331 .regs_offset = 0x340,
332 .id = 3,
333 .type = ATMEL_HLCDC_OVERLAY_LAYER,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100334 .cfgs_offset = 0x4c,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100335 .layout = {
336 .pos = 2,
337 .size = 3,
338 .memsize = 4,
339 .xstride = { 5, 7 },
340 .pstride = { 6, 8 },
341 .default_color = 9,
342 .chroma_key = 10,
343 .chroma_key_mask = 11,
344 .general_config = 12,
Boris Brezillon9a45d332017-02-06 18:57:19 +0100345 .scaler_config = 13,
346 .phicoeffs = {
347 .x = 17,
348 .y = 33,
349 },
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100350 .csc = 14,
351 },
Peter Rosin364a7bf2017-06-22 07:03:11 +0200352 .clut_offset = 0x1200,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100353 },
354};
355
356static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d4 = {
357 .min_width = 0,
358 .min_height = 0,
359 .max_width = 2048,
360 .max_height = 2048,
Boris Brezillon79a3fc22016-01-05 18:11:39 +0100361 .max_spw = 0xff,
362 .max_vpw = 0xff,
363 .max_hpw = 0x3ff,
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100364 .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d4_layers),
365 .layers = atmel_hlcdc_sama5d4_layers,
366};
Boris Brezillon1a396782015-01-06 11:13:28 +0100367static const struct of_device_id atmel_hlcdc_of_match[] = {
368 {
Boris Brezillon6b22cad2015-01-07 10:12:41 +0100369 .compatible = "atmel,at91sam9n12-hlcdc",
370 .data = &atmel_hlcdc_dc_at91sam9n12,
371 },
372 {
Boris Brezillon348ef852015-01-07 09:30:20 +0100373 .compatible = "atmel,at91sam9x5-hlcdc",
374 .data = &atmel_hlcdc_dc_at91sam9x5,
375 },
376 {
Nicolas Ferre34649c42015-12-15 12:20:57 +0100377 .compatible = "atmel,sama5d2-hlcdc",
378 .data = &atmel_hlcdc_dc_sama5d4,
379 },
380 {
Boris Brezillon1a396782015-01-06 11:13:28 +0100381 .compatible = "atmel,sama5d3-hlcdc",
382 .data = &atmel_hlcdc_dc_sama5d3,
383 },
Boris Brezillon5b9fb5e2015-01-07 10:25:41 +0100384 {
385 .compatible = "atmel,sama5d4-hlcdc",
386 .data = &atmel_hlcdc_dc_sama5d4,
387 },
Boris Brezillon1a396782015-01-06 11:13:28 +0100388 { /* sentinel */ },
389};
Luis de Bethencourtd6ec5ec2015-12-15 12:21:16 +0100390MODULE_DEVICE_TABLE(of, atmel_hlcdc_of_match);
Boris Brezillon1a396782015-01-06 11:13:28 +0100391
Jose Abreua57bf532017-05-25 15:19:21 +0100392enum drm_mode_status
393atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
394 const struct drm_display_mode *mode)
Boris Brezillon1a396782015-01-06 11:13:28 +0100395{
396 int vfront_porch = mode->vsync_start - mode->vdisplay;
397 int vback_porch = mode->vtotal - mode->vsync_end;
398 int vsync_len = mode->vsync_end - mode->vsync_start;
399 int hfront_porch = mode->hsync_start - mode->hdisplay;
400 int hback_porch = mode->htotal - mode->hsync_end;
401 int hsync_len = mode->hsync_end - mode->hsync_start;
402
Boris Brezillon79a3fc22016-01-05 18:11:39 +0100403 if (hsync_len > dc->desc->max_spw + 1 || hsync_len < 1)
Boris Brezillon1a396782015-01-06 11:13:28 +0100404 return MODE_HSYNC;
405
Boris Brezillon79a3fc22016-01-05 18:11:39 +0100406 if (vsync_len > dc->desc->max_spw + 1 || vsync_len < 1)
Boris Brezillon1a396782015-01-06 11:13:28 +0100407 return MODE_VSYNC;
408
Boris Brezillon79a3fc22016-01-05 18:11:39 +0100409 if (hfront_porch > dc->desc->max_hpw + 1 || hfront_porch < 1 ||
410 hback_porch > dc->desc->max_hpw + 1 || hback_porch < 1 ||
Boris Brezillon1a396782015-01-06 11:13:28 +0100411 mode->hdisplay < 1)
412 return MODE_H_ILLEGAL;
413
Boris Brezillon79a3fc22016-01-05 18:11:39 +0100414 if (vfront_porch > dc->desc->max_vpw + 1 || vfront_porch < 1 ||
415 vback_porch > dc->desc->max_vpw || vback_porch < 0 ||
Boris Brezillon1a396782015-01-06 11:13:28 +0100416 mode->vdisplay < 1)
417 return MODE_V_ILLEGAL;
418
419 return MODE_OK;
420}
421
Boris Brezillon9a45d332017-02-06 18:57:19 +0100422static void atmel_hlcdc_layer_irq(struct atmel_hlcdc_layer *layer)
423{
424 if (!layer)
425 return;
426
427 if (layer->desc->type == ATMEL_HLCDC_BASE_LAYER ||
428 layer->desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
429 layer->desc->type == ATMEL_HLCDC_CURSOR_LAYER)
430 atmel_hlcdc_plane_irq(atmel_hlcdc_layer_to_plane(layer));
431}
432
Boris Brezillon1a396782015-01-06 11:13:28 +0100433static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
434{
435 struct drm_device *dev = data;
436 struct atmel_hlcdc_dc *dc = dev->dev_private;
437 unsigned long status;
438 unsigned int imr, isr;
439 int i;
440
441 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
442 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
443 status = imr & isr;
444 if (!status)
445 return IRQ_NONE;
446
447 if (status & ATMEL_HLCDC_SOF)
448 atmel_hlcdc_crtc_irq(dc->crtc);
449
450 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
Boris Brezillon9a45d332017-02-06 18:57:19 +0100451 if (ATMEL_HLCDC_LAYER_STATUS(i) & status)
452 atmel_hlcdc_layer_irq(dc->layers[i]);
Boris Brezillon1a396782015-01-06 11:13:28 +0100453 }
454
455 return IRQ_HANDLED;
456}
457
458static struct drm_framebuffer *atmel_hlcdc_fb_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200459 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
Boris Brezillon1a396782015-01-06 11:13:28 +0100460{
Noralf Trønnes2a6f7132017-08-13 15:31:50 +0200461 return drm_gem_fb_create(dev, file_priv, mode_cmd);
Boris Brezillon1a396782015-01-06 11:13:28 +0100462}
463
Boris Brezillon9b190612015-10-10 08:22:09 +0200464struct atmel_hlcdc_dc_commit {
465 struct work_struct work;
466 struct drm_device *dev;
467 struct drm_atomic_state *state;
468};
469
470static void
471atmel_hlcdc_dc_atomic_complete(struct atmel_hlcdc_dc_commit *commit)
472{
473 struct drm_device *dev = commit->dev;
474 struct atmel_hlcdc_dc *dc = dev->dev_private;
475 struct drm_atomic_state *old_state = commit->state;
476
477 /* Apply the atomic update. */
478 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Liu Ying2b58e982016-08-29 17:12:03 +0800479 drm_atomic_helper_commit_planes(dev, old_state, 0);
Boris Brezillon9b190612015-10-10 08:22:09 +0200480 drm_atomic_helper_commit_modeset_enables(dev, old_state);
481
482 drm_atomic_helper_wait_for_vblanks(dev, old_state);
483
484 drm_atomic_helper_cleanup_planes(dev, old_state);
485
Chris Wilson08536952016-10-14 13:18:18 +0100486 drm_atomic_state_put(old_state);
Boris Brezillon9b190612015-10-10 08:22:09 +0200487
488 /* Complete the commit, wake up any waiter. */
489 spin_lock(&dc->commit.wait.lock);
490 dc->commit.pending = false;
491 wake_up_all_locked(&dc->commit.wait);
492 spin_unlock(&dc->commit.wait.lock);
493
494 kfree(commit);
495}
496
497static void atmel_hlcdc_dc_atomic_work(struct work_struct *work)
498{
499 struct atmel_hlcdc_dc_commit *commit =
500 container_of(work, struct atmel_hlcdc_dc_commit, work);
501
502 atmel_hlcdc_dc_atomic_complete(commit);
503}
504
505static int atmel_hlcdc_dc_atomic_commit(struct drm_device *dev,
506 struct drm_atomic_state *state,
507 bool async)
508{
509 struct atmel_hlcdc_dc *dc = dev->dev_private;
510 struct atmel_hlcdc_dc_commit *commit;
511 int ret;
512
513 ret = drm_atomic_helper_prepare_planes(dev, state);
514 if (ret)
515 return ret;
516
517 /* Allocate the commit object. */
518 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
519 if (!commit) {
520 ret = -ENOMEM;
521 goto error;
522 }
523
524 INIT_WORK(&commit->work, atmel_hlcdc_dc_atomic_work);
525 commit->dev = dev;
526 commit->state = state;
527
528 spin_lock(&dc->commit.wait.lock);
529 ret = wait_event_interruptible_locked(dc->commit.wait,
530 !dc->commit.pending);
531 if (ret == 0)
532 dc->commit.pending = true;
533 spin_unlock(&dc->commit.wait.lock);
534
Maarten Lankhorstf4d41d92017-07-11 16:33:06 +0200535 if (ret)
536 goto err_free;
Boris Brezillon9b190612015-10-10 08:22:09 +0200537
Maarten Lankhorstf4d41d92017-07-11 16:33:06 +0200538 /* We have our own synchronization through the commit lock. */
539 BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
Boris Brezillon9b190612015-10-10 08:22:09 +0200540
Maarten Lankhorstf4d41d92017-07-11 16:33:06 +0200541 /* Swap state succeeded, this is the point of no return. */
Chris Wilson08536952016-10-14 13:18:18 +0100542 drm_atomic_state_get(state);
Boris Brezillon9b190612015-10-10 08:22:09 +0200543 if (async)
544 queue_work(dc->wq, &commit->work);
545 else
546 atmel_hlcdc_dc_atomic_complete(commit);
547
548 return 0;
549
Maarten Lankhorstf4d41d92017-07-11 16:33:06 +0200550err_free:
551 kfree(commit);
Boris Brezillon9b190612015-10-10 08:22:09 +0200552error:
553 drm_atomic_helper_cleanup_planes(dev, state);
554 return ret;
555}
556
Boris Brezillon1a396782015-01-06 11:13:28 +0100557static const struct drm_mode_config_funcs mode_config_funcs = {
558 .fb_create = atmel_hlcdc_fb_create,
Noralf Trønnesce4eb352017-11-15 15:19:44 +0100559 .output_poll_changed = drm_fb_helper_output_poll_changed,
Boris Brezillon2389fc12015-02-05 16:32:33 +0100560 .atomic_check = drm_atomic_helper_check,
Boris Brezillon9b190612015-10-10 08:22:09 +0200561 .atomic_commit = atmel_hlcdc_dc_atomic_commit,
Boris Brezillon1a396782015-01-06 11:13:28 +0100562};
563
564static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
565{
566 struct atmel_hlcdc_dc *dc = dev->dev_private;
Boris Brezillon1a396782015-01-06 11:13:28 +0100567 int ret;
Boris Brezillon1a396782015-01-06 11:13:28 +0100568
569 drm_mode_config_init(dev);
570
571 ret = atmel_hlcdc_create_outputs(dev);
572 if (ret) {
Boris Brezillon17a8e032016-01-06 10:16:32 +0100573 dev_err(dev->dev, "failed to create HLCDC outputs: %d\n", ret);
Boris Brezillon1a396782015-01-06 11:13:28 +0100574 return ret;
575 }
576
Boris Brezillon9a45d332017-02-06 18:57:19 +0100577 ret = atmel_hlcdc_create_planes(dev);
578 if (ret) {
579 dev_err(dev->dev, "failed to create planes: %d\n", ret);
580 return ret;
Boris Brezillon1a396782015-01-06 11:13:28 +0100581 }
582
Boris Brezillon1a396782015-01-06 11:13:28 +0100583 ret = atmel_hlcdc_crtc_create(dev);
584 if (ret) {
585 dev_err(dev->dev, "failed to create crtc\n");
586 return ret;
587 }
588
589 dev->mode_config.min_width = dc->desc->min_width;
590 dev->mode_config.min_height = dc->desc->min_height;
591 dev->mode_config.max_width = dc->desc->max_width;
592 dev->mode_config.max_height = dc->desc->max_height;
593 dev->mode_config.funcs = &mode_config_funcs;
594
595 return 0;
596}
597
598static int atmel_hlcdc_dc_load(struct drm_device *dev)
599{
600 struct platform_device *pdev = to_platform_device(dev->dev);
601 const struct of_device_id *match;
602 struct atmel_hlcdc_dc *dc;
603 int ret;
604
605 match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
606 if (!match) {
607 dev_err(&pdev->dev, "invalid compatible string\n");
608 return -ENODEV;
609 }
610
611 if (!match->data) {
612 dev_err(&pdev->dev, "invalid hlcdc description\n");
613 return -EINVAL;
614 }
615
616 dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
617 if (!dc)
618 return -ENOMEM;
619
620 dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
621 if (!dc->wq)
622 return -ENOMEM;
623
Boris Brezillon9b190612015-10-10 08:22:09 +0200624 init_waitqueue_head(&dc->commit.wait);
Boris Brezillon1a396782015-01-06 11:13:28 +0100625 dc->desc = match->data;
626 dc->hlcdc = dev_get_drvdata(dev->dev->parent);
627 dev->dev_private = dc;
628
629 ret = clk_prepare_enable(dc->hlcdc->periph_clk);
630 if (ret) {
631 dev_err(dev->dev, "failed to enable periph_clk\n");
632 goto err_destroy_wq;
633 }
634
635 pm_runtime_enable(dev->dev);
636
Boris Brezillon8c4b4b02015-07-16 20:55:34 +0200637 ret = drm_vblank_init(dev, 1);
638 if (ret < 0) {
639 dev_err(dev->dev, "failed to initialize vblank\n");
640 goto err_periph_clk_disable;
641 }
642
Boris Brezillon1a396782015-01-06 11:13:28 +0100643 ret = atmel_hlcdc_dc_modeset_init(dev);
644 if (ret < 0) {
645 dev_err(dev->dev, "failed to initialize mode setting\n");
646 goto err_periph_clk_disable;
647 }
648
Boris Brezillon2389fc12015-02-05 16:32:33 +0100649 drm_mode_config_reset(dev);
650
Boris Brezillon1a396782015-01-06 11:13:28 +0100651 pm_runtime_get_sync(dev->dev);
652 ret = drm_irq_install(dev, dc->hlcdc->irq);
653 pm_runtime_put_sync(dev->dev);
654 if (ret < 0) {
655 dev_err(dev->dev, "failed to install IRQ handler\n");
656 goto err_periph_clk_disable;
657 }
658
659 platform_set_drvdata(pdev, dev);
660
Noralf Trønnesce4eb352017-11-15 15:19:44 +0100661 drm_fb_cma_fbdev_init(dev, 24, 0);
Boris Brezillon1a396782015-01-06 11:13:28 +0100662
Boris Brezillondb02b762016-11-28 15:56:53 +0100663 drm_kms_helper_poll_init(dev);
Boris Brezillon1a396782015-01-06 11:13:28 +0100664
665 return 0;
666
667err_periph_clk_disable:
668 pm_runtime_disable(dev->dev);
669 clk_disable_unprepare(dc->hlcdc->periph_clk);
670
671err_destroy_wq:
672 destroy_workqueue(dc->wq);
673
674 return ret;
675}
676
677static void atmel_hlcdc_dc_unload(struct drm_device *dev)
678{
679 struct atmel_hlcdc_dc *dc = dev->dev_private;
680
Noralf Trønnesce4eb352017-11-15 15:19:44 +0100681 drm_fb_cma_fbdev_fini(dev);
Boris Brezillon1a396782015-01-06 11:13:28 +0100682 flush_workqueue(dc->wq);
683 drm_kms_helper_poll_fini(dev);
684 drm_mode_config_cleanup(dev);
Boris Brezillon1a396782015-01-06 11:13:28 +0100685
686 pm_runtime_get_sync(dev->dev);
687 drm_irq_uninstall(dev);
688 pm_runtime_put_sync(dev->dev);
689
690 dev->dev_private = NULL;
691
692 pm_runtime_disable(dev->dev);
693 clk_disable_unprepare(dc->hlcdc->periph_clk);
694 destroy_workqueue(dc->wq);
695}
696
Boris Brezillon1a396782015-01-06 11:13:28 +0100697static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
698{
699 struct atmel_hlcdc_dc *dc = dev->dev_private;
700 unsigned int cfg = 0;
701 int i;
702
703 /* Enable interrupts on activated layers */
704 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
705 if (dc->layers[i])
706 cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
707 }
708
709 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
710
711 return 0;
712}
713
714static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
715{
716 struct atmel_hlcdc_dc *dc = dev->dev_private;
717 unsigned int isr;
718
719 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
720 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
721}
722
Daniel Vetterd55f7e52017-03-08 15:12:56 +0100723DEFINE_DRM_GEM_CMA_FOPS(fops);
Boris Brezillon1a396782015-01-06 11:13:28 +0100724
725static struct drm_driver atmel_hlcdc_dc_driver = {
Boris Brezillone14c71c2015-04-20 13:43:26 +0200726 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
Boris Brezillonaa690a92015-07-31 15:10:26 +0200727 DRIVER_MODESET | DRIVER_PRIME |
728 DRIVER_ATOMIC,
Noralf Trønnesce4eb352017-11-15 15:19:44 +0100729 .lastclose = drm_fb_helper_lastclose,
Boris Brezillon1a396782015-01-06 11:13:28 +0100730 .irq_handler = atmel_hlcdc_dc_irq_handler,
731 .irq_preinstall = atmel_hlcdc_dc_irq_uninstall,
732 .irq_postinstall = atmel_hlcdc_dc_irq_postinstall,
733 .irq_uninstall = atmel_hlcdc_dc_irq_uninstall,
Daniel Vetter3100e642016-05-30 19:52:55 +0200734 .gem_free_object_unlocked = drm_gem_cma_free_object,
Boris Brezillon1a396782015-01-06 11:13:28 +0100735 .gem_vm_ops = &drm_gem_cma_vm_ops,
Boris Brezillone14c71c2015-04-20 13:43:26 +0200736 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
737 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
738 .gem_prime_import = drm_gem_prime_import,
739 .gem_prime_export = drm_gem_prime_export,
740 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
741 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
742 .gem_prime_vmap = drm_gem_cma_prime_vmap,
743 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
744 .gem_prime_mmap = drm_gem_cma_prime_mmap,
Boris Brezillon1a396782015-01-06 11:13:28 +0100745 .dumb_create = drm_gem_cma_dumb_create,
Boris Brezillon1a396782015-01-06 11:13:28 +0100746 .fops = &fops,
747 .name = "atmel-hlcdc",
748 .desc = "Atmel HLCD Controller DRM",
749 .date = "20141504",
750 .major = 1,
751 .minor = 0,
752};
753
754static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
755{
756 struct drm_device *ddev;
757 int ret;
758
759 ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
Tom Gundersen0f288602016-09-21 16:59:19 +0200760 if (IS_ERR(ddev))
761 return PTR_ERR(ddev);
Boris Brezillon1a396782015-01-06 11:13:28 +0100762
Boris Brezillon1a396782015-01-06 11:13:28 +0100763 ret = atmel_hlcdc_dc_load(ddev);
764 if (ret)
765 goto err_unref;
766
767 ret = drm_dev_register(ddev, 0);
768 if (ret)
769 goto err_unload;
770
Boris Brezillon1a396782015-01-06 11:13:28 +0100771 return 0;
772
Boris Brezillon1a396782015-01-06 11:13:28 +0100773err_unload:
774 atmel_hlcdc_dc_unload(ddev);
775
776err_unref:
777 drm_dev_unref(ddev);
778
779 return ret;
780}
781
782static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
783{
784 struct drm_device *ddev = platform_get_drvdata(pdev);
785
Boris Brezillon1a396782015-01-06 11:13:28 +0100786 drm_dev_unregister(ddev);
787 atmel_hlcdc_dc_unload(ddev);
788 drm_dev_unref(ddev);
789
790 return 0;
791}
792
Thierry Redingdbb3df22015-08-14 13:58:20 +0200793#ifdef CONFIG_PM_SLEEP
Sylvain Rochet58486982015-02-22 18:51:03 +0100794static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
795{
796 struct drm_device *drm_dev = dev_get_drvdata(dev);
Boris Brezillon99ed4d72017-03-01 13:31:01 +0100797 struct atmel_hlcdc_dc *dc = drm_dev->dev_private;
798 struct regmap *regmap = dc->hlcdc->regmap;
799 struct drm_atomic_state *state;
Sylvain Rochet58486982015-02-22 18:51:03 +0100800
Boris Brezillon99ed4d72017-03-01 13:31:01 +0100801 state = drm_atomic_helper_suspend(drm_dev);
802 if (IS_ERR(state))
803 return PTR_ERR(state);
Sylvain Rochet58486982015-02-22 18:51:03 +0100804
Boris Brezillon99ed4d72017-03-01 13:31:01 +0100805 dc->suspend.state = state;
806
807 regmap_read(regmap, ATMEL_HLCDC_IMR, &dc->suspend.imr);
808 regmap_write(regmap, ATMEL_HLCDC_IDR, dc->suspend.imr);
809 clk_disable_unprepare(dc->hlcdc->periph_clk);
810
Sylvain Rochet58486982015-02-22 18:51:03 +0100811 return 0;
812}
813
814static int atmel_hlcdc_dc_drm_resume(struct device *dev)
815{
816 struct drm_device *drm_dev = dev_get_drvdata(dev);
Boris Brezillon99ed4d72017-03-01 13:31:01 +0100817 struct atmel_hlcdc_dc *dc = drm_dev->dev_private;
Sylvain Rochet58486982015-02-22 18:51:03 +0100818
Boris Brezillon99ed4d72017-03-01 13:31:01 +0100819 clk_prepare_enable(dc->hlcdc->periph_clk);
820 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, dc->suspend.imr);
Sylvain Rochet58486982015-02-22 18:51:03 +0100821
Boris Brezillon99ed4d72017-03-01 13:31:01 +0100822 return drm_atomic_helper_resume(drm_dev, dc->suspend.state);
Sylvain Rochet58486982015-02-22 18:51:03 +0100823}
824#endif
825
826static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
827 atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
828
Boris Brezillon1a396782015-01-06 11:13:28 +0100829static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
830 { .compatible = "atmel,hlcdc-display-controller" },
831 { },
832};
833
834static struct platform_driver atmel_hlcdc_dc_platform_driver = {
835 .probe = atmel_hlcdc_dc_drm_probe,
836 .remove = atmel_hlcdc_dc_drm_remove,
837 .driver = {
838 .name = "atmel-hlcdc-display-controller",
Sylvain Rochet58486982015-02-22 18:51:03 +0100839 .pm = &atmel_hlcdc_dc_drm_pm_ops,
Boris Brezillon1a396782015-01-06 11:13:28 +0100840 .of_match_table = atmel_hlcdc_dc_of_match,
841 },
842};
843module_platform_driver(atmel_hlcdc_dc_platform_driver);
844
845MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
846MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
847MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
848MODULE_LICENSE("GPL");
849MODULE_ALIAS("platform:atmel-hlcdc-dc");