2 * Copyright (C) 2014 Traphandler
3 * Copyright (C) 2014 Free Electrons
5 * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
6 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/clk.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/pinctrl/consumer.h>
26 #include <drm/drm_crtc.h>
27 #include <drm/drm_probe_helper.h>
30 #include <video/videomode.h>
32 #include "atmel_hlcdc_dc.h"
35 * Atmel HLCDC CRTC state structure
37 * @base: base CRTC state
38 * @output_mode: RGBXXX output mode
40 struct atmel_hlcdc_crtc_state
{
41 struct drm_crtc_state base
;
42 unsigned int output_mode
;
45 static inline struct atmel_hlcdc_crtc_state
*
46 drm_crtc_state_to_atmel_hlcdc_crtc_state(struct drm_crtc_state
*state
)
48 return container_of(state
, struct atmel_hlcdc_crtc_state
, base
);
52 * Atmel HLCDC CRTC structure
54 * @base: base DRM CRTC structure
55 * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device
56 * @event: pointer to the current page flip event
57 * @id: CRTC id (returned by drm_crtc_index)
59 struct atmel_hlcdc_crtc
{
61 struct atmel_hlcdc_dc
*dc
;
62 struct drm_pending_vblank_event
*event
;
66 static inline struct atmel_hlcdc_crtc
*
67 drm_crtc_to_atmel_hlcdc_crtc(struct drm_crtc
*crtc
)
69 return container_of(crtc
, struct atmel_hlcdc_crtc
, base
);
72 static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc
*c
)
74 struct atmel_hlcdc_crtc
*crtc
= drm_crtc_to_atmel_hlcdc_crtc(c
);
75 struct regmap
*regmap
= crtc
->dc
->hlcdc
->regmap
;
76 struct drm_display_mode
*adj
= &c
->state
->adjusted_mode
;
77 struct atmel_hlcdc_crtc_state
*state
;
78 unsigned long mode_rate
;
84 vm
.vfront_porch
= adj
->crtc_vsync_start
- adj
->crtc_vdisplay
;
85 vm
.vback_porch
= adj
->crtc_vtotal
- adj
->crtc_vsync_end
;
86 vm
.vsync_len
= adj
->crtc_vsync_end
- adj
->crtc_vsync_start
;
87 vm
.hfront_porch
= adj
->crtc_hsync_start
- adj
->crtc_hdisplay
;
88 vm
.hback_porch
= adj
->crtc_htotal
- adj
->crtc_hsync_end
;
89 vm
.hsync_len
= adj
->crtc_hsync_end
- adj
->crtc_hsync_start
;
91 regmap_write(regmap
, ATMEL_HLCDC_CFG(1),
92 (vm
.hsync_len
- 1) | ((vm
.vsync_len
- 1) << 16));
94 regmap_write(regmap
, ATMEL_HLCDC_CFG(2),
95 (vm
.vfront_porch
- 1) | (vm
.vback_porch
<< 16));
97 regmap_write(regmap
, ATMEL_HLCDC_CFG(3),
98 (vm
.hfront_porch
- 1) | ((vm
.hback_porch
- 1) << 16));
100 regmap_write(regmap
, ATMEL_HLCDC_CFG(4),
101 (adj
->crtc_hdisplay
- 1) |
102 ((adj
->crtc_vdisplay
- 1) << 16));
104 cfg
= ATMEL_HLCDC_CLKSEL
;
106 prate
= 2 * clk_get_rate(crtc
->dc
->hlcdc
->sys_clk
);
107 mode_rate
= adj
->crtc_clock
* 1000;
109 div
= DIV_ROUND_UP(prate
, mode_rate
);
112 } else if (ATMEL_HLCDC_CLKDIV(div
) & ~ATMEL_HLCDC_CLKDIV_MASK
) {
113 /* The divider ended up too big, try a lower base rate. */
114 cfg
&= ~ATMEL_HLCDC_CLKSEL
;
116 div
= DIV_ROUND_UP(prate
, mode_rate
);
117 if (ATMEL_HLCDC_CLKDIV(div
) & ~ATMEL_HLCDC_CLKDIV_MASK
)
118 div
= ATMEL_HLCDC_CLKDIV_MASK
;
120 int div_low
= prate
/ mode_rate
;
123 ((prate
/ div_low
- mode_rate
) <
124 10 * (mode_rate
- prate
/ div
)))
126 * At least 10 times better when using a higher
127 * frequency than requested, instead of a lower.
133 cfg
|= ATMEL_HLCDC_CLKDIV(div
);
135 regmap_update_bits(regmap
, ATMEL_HLCDC_CFG(0),
136 ATMEL_HLCDC_CLKSEL
| ATMEL_HLCDC_CLKDIV_MASK
|
137 ATMEL_HLCDC_CLKPOL
, cfg
);
141 if (adj
->flags
& DRM_MODE_FLAG_NVSYNC
)
142 cfg
|= ATMEL_HLCDC_VSPOL
;
144 if (adj
->flags
& DRM_MODE_FLAG_NHSYNC
)
145 cfg
|= ATMEL_HLCDC_HSPOL
;
147 state
= drm_crtc_state_to_atmel_hlcdc_crtc_state(c
->state
);
148 cfg
|= state
->output_mode
<< 8;
150 regmap_update_bits(regmap
, ATMEL_HLCDC_CFG(5),
151 ATMEL_HLCDC_HSPOL
| ATMEL_HLCDC_VSPOL
|
152 ATMEL_HLCDC_VSPDLYS
| ATMEL_HLCDC_VSPDLYE
|
153 ATMEL_HLCDC_DISPPOL
| ATMEL_HLCDC_DISPDLY
|
154 ATMEL_HLCDC_VSPSU
| ATMEL_HLCDC_VSPHO
|
155 ATMEL_HLCDC_GUARDTIME_MASK
| ATMEL_HLCDC_MODE_MASK
,
159 static enum drm_mode_status
160 atmel_hlcdc_crtc_mode_valid(struct drm_crtc
*c
,
161 const struct drm_display_mode
*mode
)
163 struct atmel_hlcdc_crtc
*crtc
= drm_crtc_to_atmel_hlcdc_crtc(c
);
165 return atmel_hlcdc_dc_mode_valid(crtc
->dc
, mode
);
168 static void atmel_hlcdc_crtc_atomic_disable(struct drm_crtc
*c
,
169 struct drm_crtc_state
*old_state
)
171 struct drm_device
*dev
= c
->dev
;
172 struct atmel_hlcdc_crtc
*crtc
= drm_crtc_to_atmel_hlcdc_crtc(c
);
173 struct regmap
*regmap
= crtc
->dc
->hlcdc
->regmap
;
176 drm_crtc_vblank_off(c
);
178 pm_runtime_get_sync(dev
->dev
);
180 regmap_write(regmap
, ATMEL_HLCDC_DIS
, ATMEL_HLCDC_DISP
);
181 while (!regmap_read(regmap
, ATMEL_HLCDC_SR
, &status
) &&
182 (status
& ATMEL_HLCDC_DISP
))
185 regmap_write(regmap
, ATMEL_HLCDC_DIS
, ATMEL_HLCDC_SYNC
);
186 while (!regmap_read(regmap
, ATMEL_HLCDC_SR
, &status
) &&
187 (status
& ATMEL_HLCDC_SYNC
))
190 regmap_write(regmap
, ATMEL_HLCDC_DIS
, ATMEL_HLCDC_PIXEL_CLK
);
191 while (!regmap_read(regmap
, ATMEL_HLCDC_SR
, &status
) &&
192 (status
& ATMEL_HLCDC_PIXEL_CLK
))
195 clk_disable_unprepare(crtc
->dc
->hlcdc
->sys_clk
);
196 pinctrl_pm_select_sleep_state(dev
->dev
);
198 pm_runtime_allow(dev
->dev
);
200 pm_runtime_put_sync(dev
->dev
);
203 static void atmel_hlcdc_crtc_atomic_enable(struct drm_crtc
*c
,
204 struct drm_crtc_state
*old_state
)
206 struct drm_device
*dev
= c
->dev
;
207 struct atmel_hlcdc_crtc
*crtc
= drm_crtc_to_atmel_hlcdc_crtc(c
);
208 struct regmap
*regmap
= crtc
->dc
->hlcdc
->regmap
;
211 pm_runtime_get_sync(dev
->dev
);
213 pm_runtime_forbid(dev
->dev
);
215 pinctrl_pm_select_default_state(dev
->dev
);
216 clk_prepare_enable(crtc
->dc
->hlcdc
->sys_clk
);
218 regmap_write(regmap
, ATMEL_HLCDC_EN
, ATMEL_HLCDC_PIXEL_CLK
);
219 while (!regmap_read(regmap
, ATMEL_HLCDC_SR
, &status
) &&
220 !(status
& ATMEL_HLCDC_PIXEL_CLK
))
224 regmap_write(regmap
, ATMEL_HLCDC_EN
, ATMEL_HLCDC_SYNC
);
225 while (!regmap_read(regmap
, ATMEL_HLCDC_SR
, &status
) &&
226 !(status
& ATMEL_HLCDC_SYNC
))
229 regmap_write(regmap
, ATMEL_HLCDC_EN
, ATMEL_HLCDC_DISP
);
230 while (!regmap_read(regmap
, ATMEL_HLCDC_SR
, &status
) &&
231 !(status
& ATMEL_HLCDC_DISP
))
234 pm_runtime_put_sync(dev
->dev
);
236 drm_crtc_vblank_on(c
);
239 #define ATMEL_HLCDC_RGB444_OUTPUT BIT(0)
240 #define ATMEL_HLCDC_RGB565_OUTPUT BIT(1)
241 #define ATMEL_HLCDC_RGB666_OUTPUT BIT(2)
242 #define ATMEL_HLCDC_RGB888_OUTPUT BIT(3)
243 #define ATMEL_HLCDC_OUTPUT_MODE_MASK GENMASK(3, 0)
245 static int atmel_hlcdc_connector_output_mode(struct drm_connector_state
*state
)
247 struct drm_connector
*connector
= state
->connector
;
248 struct drm_display_info
*info
= &connector
->display_info
;
249 struct drm_encoder
*encoder
;
250 unsigned int supported_fmts
= 0;
253 encoder
= state
->best_encoder
;
255 encoder
= connector
->encoder
;
257 switch (atmel_hlcdc_encoder_get_bus_fmt(encoder
)) {
260 case MEDIA_BUS_FMT_RGB444_1X12
:
261 return ATMEL_HLCDC_RGB444_OUTPUT
;
262 case MEDIA_BUS_FMT_RGB565_1X16
:
263 return ATMEL_HLCDC_RGB565_OUTPUT
;
264 case MEDIA_BUS_FMT_RGB666_1X18
:
265 return ATMEL_HLCDC_RGB666_OUTPUT
;
266 case MEDIA_BUS_FMT_RGB888_1X24
:
267 return ATMEL_HLCDC_RGB888_OUTPUT
;
272 for (j
= 0; j
< info
->num_bus_formats
; j
++) {
273 switch (info
->bus_formats
[j
]) {
274 case MEDIA_BUS_FMT_RGB444_1X12
:
275 supported_fmts
|= ATMEL_HLCDC_RGB444_OUTPUT
;
277 case MEDIA_BUS_FMT_RGB565_1X16
:
278 supported_fmts
|= ATMEL_HLCDC_RGB565_OUTPUT
;
280 case MEDIA_BUS_FMT_RGB666_1X18
:
281 supported_fmts
|= ATMEL_HLCDC_RGB666_OUTPUT
;
283 case MEDIA_BUS_FMT_RGB888_1X24
:
284 supported_fmts
|= ATMEL_HLCDC_RGB888_OUTPUT
;
291 return supported_fmts
;
294 static int atmel_hlcdc_crtc_select_output_mode(struct drm_crtc_state
*state
)
296 unsigned int output_fmts
= ATMEL_HLCDC_OUTPUT_MODE_MASK
;
297 struct atmel_hlcdc_crtc_state
*hstate
;
298 struct drm_connector_state
*cstate
;
299 struct drm_connector
*connector
;
300 struct atmel_hlcdc_crtc
*crtc
;
303 crtc
= drm_crtc_to_atmel_hlcdc_crtc(state
->crtc
);
305 for_each_new_connector_in_state(state
->state
, connector
, cstate
, i
) {
306 unsigned int supported_fmts
= 0;
311 supported_fmts
= atmel_hlcdc_connector_output_mode(cstate
);
313 if (crtc
->dc
->desc
->conflicting_output_formats
)
314 output_fmts
&= supported_fmts
;
316 output_fmts
|= supported_fmts
;
322 hstate
= drm_crtc_state_to_atmel_hlcdc_crtc_state(state
);
323 hstate
->output_mode
= fls(output_fmts
) - 1;
328 static int atmel_hlcdc_crtc_atomic_check(struct drm_crtc
*c
,
329 struct drm_crtc_state
*s
)
333 ret
= atmel_hlcdc_crtc_select_output_mode(s
);
337 ret
= atmel_hlcdc_plane_prepare_disc_area(s
);
341 return atmel_hlcdc_plane_prepare_ahb_routing(s
);
344 static void atmel_hlcdc_crtc_atomic_begin(struct drm_crtc
*c
,
345 struct drm_crtc_state
*old_s
)
347 struct atmel_hlcdc_crtc
*crtc
= drm_crtc_to_atmel_hlcdc_crtc(c
);
349 if (c
->state
->event
) {
350 c
->state
->event
->pipe
= drm_crtc_index(c
);
352 WARN_ON(drm_crtc_vblank_get(c
) != 0);
354 crtc
->event
= c
->state
->event
;
355 c
->state
->event
= NULL
;
359 static void atmel_hlcdc_crtc_atomic_flush(struct drm_crtc
*crtc
,
360 struct drm_crtc_state
*old_s
)
362 /* TODO: write common plane control register if available */
365 static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs
= {
366 .mode_valid
= atmel_hlcdc_crtc_mode_valid
,
367 .mode_set_nofb
= atmel_hlcdc_crtc_mode_set_nofb
,
368 .atomic_check
= atmel_hlcdc_crtc_atomic_check
,
369 .atomic_begin
= atmel_hlcdc_crtc_atomic_begin
,
370 .atomic_flush
= atmel_hlcdc_crtc_atomic_flush
,
371 .atomic_enable
= atmel_hlcdc_crtc_atomic_enable
,
372 .atomic_disable
= atmel_hlcdc_crtc_atomic_disable
,
375 static void atmel_hlcdc_crtc_destroy(struct drm_crtc
*c
)
377 struct atmel_hlcdc_crtc
*crtc
= drm_crtc_to_atmel_hlcdc_crtc(c
);
383 static void atmel_hlcdc_crtc_finish_page_flip(struct atmel_hlcdc_crtc
*crtc
)
385 struct drm_device
*dev
= crtc
->base
.dev
;
388 spin_lock_irqsave(&dev
->event_lock
, flags
);
390 drm_crtc_send_vblank_event(&crtc
->base
, crtc
->event
);
391 drm_crtc_vblank_put(&crtc
->base
);
394 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
397 void atmel_hlcdc_crtc_irq(struct drm_crtc
*c
)
399 drm_crtc_handle_vblank(c
);
400 atmel_hlcdc_crtc_finish_page_flip(drm_crtc_to_atmel_hlcdc_crtc(c
));
403 static void atmel_hlcdc_crtc_reset(struct drm_crtc
*crtc
)
405 struct atmel_hlcdc_crtc_state
*state
;
408 __drm_atomic_helper_crtc_destroy_state(crtc
->state
);
409 state
= drm_crtc_state_to_atmel_hlcdc_crtc_state(crtc
->state
);
414 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
416 crtc
->state
= &state
->base
;
417 crtc
->state
->crtc
= crtc
;
421 static struct drm_crtc_state
*
422 atmel_hlcdc_crtc_duplicate_state(struct drm_crtc
*crtc
)
424 struct atmel_hlcdc_crtc_state
*state
, *cur
;
426 if (WARN_ON(!crtc
->state
))
429 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
432 __drm_atomic_helper_crtc_duplicate_state(crtc
, &state
->base
);
434 cur
= drm_crtc_state_to_atmel_hlcdc_crtc_state(crtc
->state
);
435 state
->output_mode
= cur
->output_mode
;
440 static void atmel_hlcdc_crtc_destroy_state(struct drm_crtc
*crtc
,
441 struct drm_crtc_state
*s
)
443 struct atmel_hlcdc_crtc_state
*state
;
445 state
= drm_crtc_state_to_atmel_hlcdc_crtc_state(s
);
446 __drm_atomic_helper_crtc_destroy_state(s
);
450 static int atmel_hlcdc_crtc_enable_vblank(struct drm_crtc
*c
)
452 struct atmel_hlcdc_crtc
*crtc
= drm_crtc_to_atmel_hlcdc_crtc(c
);
453 struct regmap
*regmap
= crtc
->dc
->hlcdc
->regmap
;
455 /* Enable SOF (Start Of Frame) interrupt for vblank counting */
456 regmap_write(regmap
, ATMEL_HLCDC_IER
, ATMEL_HLCDC_SOF
);
461 static void atmel_hlcdc_crtc_disable_vblank(struct drm_crtc
*c
)
463 struct atmel_hlcdc_crtc
*crtc
= drm_crtc_to_atmel_hlcdc_crtc(c
);
464 struct regmap
*regmap
= crtc
->dc
->hlcdc
->regmap
;
466 regmap_write(regmap
, ATMEL_HLCDC_IDR
, ATMEL_HLCDC_SOF
);
469 static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs
= {
470 .page_flip
= drm_atomic_helper_page_flip
,
471 .set_config
= drm_atomic_helper_set_config
,
472 .destroy
= atmel_hlcdc_crtc_destroy
,
473 .reset
= atmel_hlcdc_crtc_reset
,
474 .atomic_duplicate_state
= atmel_hlcdc_crtc_duplicate_state
,
475 .atomic_destroy_state
= atmel_hlcdc_crtc_destroy_state
,
476 .enable_vblank
= atmel_hlcdc_crtc_enable_vblank
,
477 .disable_vblank
= atmel_hlcdc_crtc_disable_vblank
,
478 .gamma_set
= drm_atomic_helper_legacy_gamma_set
,
481 int atmel_hlcdc_crtc_create(struct drm_device
*dev
)
483 struct atmel_hlcdc_plane
*primary
= NULL
, *cursor
= NULL
;
484 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
485 struct atmel_hlcdc_crtc
*crtc
;
489 crtc
= kzalloc(sizeof(*crtc
), GFP_KERNEL
);
495 for (i
= 0; i
< ATMEL_HLCDC_MAX_LAYERS
; i
++) {
499 switch (dc
->layers
[i
]->desc
->type
) {
500 case ATMEL_HLCDC_BASE_LAYER
:
501 primary
= atmel_hlcdc_layer_to_plane(dc
->layers
[i
]);
504 case ATMEL_HLCDC_CURSOR_LAYER
:
505 cursor
= atmel_hlcdc_layer_to_plane(dc
->layers
[i
]);
513 ret
= drm_crtc_init_with_planes(dev
, &crtc
->base
, &primary
->base
,
514 &cursor
->base
, &atmel_hlcdc_crtc_funcs
,
519 crtc
->id
= drm_crtc_index(&crtc
->base
);
521 for (i
= 0; i
< ATMEL_HLCDC_MAX_LAYERS
; i
++) {
522 struct atmel_hlcdc_plane
*overlay
;
525 dc
->layers
[i
]->desc
->type
== ATMEL_HLCDC_OVERLAY_LAYER
) {
526 overlay
= atmel_hlcdc_layer_to_plane(dc
->layers
[i
]);
527 overlay
->base
.possible_crtcs
= 1 << crtc
->id
;
531 drm_crtc_helper_add(&crtc
->base
, &lcdc_crtc_helper_funcs
);
532 drm_crtc_vblank_reset(&crtc
->base
);
534 drm_mode_crtc_set_gamma_size(&crtc
->base
, ATMEL_HLCDC_CLUT_SIZE
);
535 drm_crtc_enable_color_mgmt(&crtc
->base
, 0, false,
536 ATMEL_HLCDC_CLUT_SIZE
);
538 dc
->crtc
= &crtc
->base
;
543 atmel_hlcdc_crtc_destroy(&crtc
->base
);