2 * rcar_du_kms.c -- R-Car Display Unit Mode Setting
4 * Copyright (C) 2013 Renesas Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_fb_cma_helper.h>
18 #include <drm/drm_gem_cma_helper.h>
20 #include "rcar_du_crtc.h"
21 #include "rcar_du_drv.h"
22 #include "rcar_du_encoder.h"
23 #include "rcar_du_kms.h"
24 #include "rcar_du_lvdsenc.h"
25 #include "rcar_du_regs.h"
27 /* -----------------------------------------------------------------------------
31 static const struct rcar_du_format_info rcar_du_format_infos
[] = {
33 .fourcc
= DRM_FORMAT_RGB565
,
36 .pnmr
= PnMR_SPIM_TP
| PnMR_DDDF_16BPP
,
37 .edf
= PnDDCR4_EDF_NONE
,
39 .fourcc
= DRM_FORMAT_ARGB1555
,
42 .pnmr
= PnMR_SPIM_ALP
| PnMR_DDDF_ARGB
,
43 .edf
= PnDDCR4_EDF_NONE
,
45 .fourcc
= DRM_FORMAT_XRGB1555
,
48 .pnmr
= PnMR_SPIM_ALP
| PnMR_DDDF_ARGB
,
49 .edf
= PnDDCR4_EDF_NONE
,
51 .fourcc
= DRM_FORMAT_XRGB8888
,
54 .pnmr
= PnMR_SPIM_TP
| PnMR_DDDF_16BPP
,
55 .edf
= PnDDCR4_EDF_RGB888
,
57 .fourcc
= DRM_FORMAT_ARGB8888
,
60 .pnmr
= PnMR_SPIM_ALP
| PnMR_DDDF_16BPP
,
61 .edf
= PnDDCR4_EDF_ARGB8888
,
63 .fourcc
= DRM_FORMAT_UYVY
,
66 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
67 .edf
= PnDDCR4_EDF_NONE
,
69 .fourcc
= DRM_FORMAT_YUYV
,
72 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
73 .edf
= PnDDCR4_EDF_NONE
,
75 .fourcc
= DRM_FORMAT_NV12
,
78 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
79 .edf
= PnDDCR4_EDF_NONE
,
81 .fourcc
= DRM_FORMAT_NV21
,
84 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
85 .edf
= PnDDCR4_EDF_NONE
,
87 /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
88 .fourcc
= DRM_FORMAT_NV16
,
91 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
92 .edf
= PnDDCR4_EDF_NONE
,
96 const struct rcar_du_format_info
*rcar_du_format_info(u32 fourcc
)
100 for (i
= 0; i
< ARRAY_SIZE(rcar_du_format_infos
); ++i
) {
101 if (rcar_du_format_infos
[i
].fourcc
== fourcc
)
102 return &rcar_du_format_infos
[i
];
108 /* -----------------------------------------------------------------------------
112 int rcar_du_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
113 struct drm_mode_create_dumb
*args
)
115 struct rcar_du_device
*rcdu
= dev
->dev_private
;
116 unsigned int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
119 /* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
120 * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
122 if (rcar_du_needs(rcdu
, RCAR_DU_QUIRK_ALIGN_128B
))
125 align
= 16 * args
->bpp
/ 8;
127 args
->pitch
= roundup(max(args
->pitch
, min_pitch
), align
);
129 return drm_gem_cma_dumb_create(file
, dev
, args
);
132 static struct drm_framebuffer
*
133 rcar_du_fb_create(struct drm_device
*dev
, struct drm_file
*file_priv
,
134 struct drm_mode_fb_cmd2
*mode_cmd
)
136 struct rcar_du_device
*rcdu
= dev
->dev_private
;
137 const struct rcar_du_format_info
*format
;
138 unsigned int max_pitch
;
142 format
= rcar_du_format_info(mode_cmd
->pixel_format
);
143 if (format
== NULL
) {
144 dev_dbg(dev
->dev
, "unsupported pixel format %08x\n",
145 mode_cmd
->pixel_format
);
146 return ERR_PTR(-EINVAL
);
150 * The pitch and alignment constraints are expressed in pixels on the
151 * hardware side and in bytes in the DRM API.
153 bpp
= format
->planes
== 2 ? 1 : format
->bpp
/ 8;
154 max_pitch
= 4096 * bpp
;
156 if (rcar_du_needs(rcdu
, RCAR_DU_QUIRK_ALIGN_128B
))
161 if (mode_cmd
->pitches
[0] & (align
- 1) ||
162 mode_cmd
->pitches
[0] >= max_pitch
) {
163 dev_dbg(dev
->dev
, "invalid pitch value %u\n",
164 mode_cmd
->pitches
[0]);
165 return ERR_PTR(-EINVAL
);
168 if (format
->planes
== 2) {
169 if (mode_cmd
->pitches
[1] != mode_cmd
->pitches
[0]) {
171 "luma and chroma pitches do not match\n");
172 return ERR_PTR(-EINVAL
);
176 return drm_fb_cma_create(dev
, file_priv
, mode_cmd
);
179 static void rcar_du_output_poll_changed(struct drm_device
*dev
)
181 struct rcar_du_device
*rcdu
= dev
->dev_private
;
183 drm_fbdev_cma_hotplug_event(rcdu
->fbdev
);
186 static const struct drm_mode_config_funcs rcar_du_mode_config_funcs
= {
187 .fb_create
= rcar_du_fb_create
,
188 .output_poll_changed
= rcar_du_output_poll_changed
,
191 int rcar_du_modeset_init(struct rcar_du_device
*rcdu
)
193 static const unsigned int mmio_offsets
[] = {
194 DU0_REG_OFFSET
, DU2_REG_OFFSET
197 struct drm_device
*dev
= rcdu
->ddev
;
198 struct drm_encoder
*encoder
;
199 struct drm_fbdev_cma
*fbdev
;
200 unsigned int num_groups
;
204 drm_mode_config_init(dev
);
206 dev
->mode_config
.min_width
= 0;
207 dev
->mode_config
.min_height
= 0;
208 dev
->mode_config
.max_width
= 4095;
209 dev
->mode_config
.max_height
= 2047;
210 dev
->mode_config
.funcs
= &rcar_du_mode_config_funcs
;
212 rcdu
->num_crtcs
= rcdu
->info
->num_crtcs
;
214 /* Initialize the groups. */
215 num_groups
= DIV_ROUND_UP(rcdu
->num_crtcs
, 2);
217 for (i
= 0; i
< num_groups
; ++i
) {
218 struct rcar_du_group
*rgrp
= &rcdu
->groups
[i
];
221 rgrp
->mmio_offset
= mmio_offsets
[i
];
224 ret
= rcar_du_planes_init(rgrp
);
229 /* Create the CRTCs. */
230 for (i
= 0; i
< rcdu
->num_crtcs
; ++i
) {
231 struct rcar_du_group
*rgrp
= &rcdu
->groups
[i
/ 2];
233 ret
= rcar_du_crtc_create(rgrp
, i
);
238 /* Initialize the encoders. */
239 ret
= rcar_du_lvdsenc_init(rcdu
);
243 for (i
= 0; i
< rcdu
->pdata
->num_encoders
; ++i
) {
244 const struct rcar_du_encoder_data
*pdata
=
245 &rcdu
->pdata
->encoders
[i
];
246 const struct rcar_du_output_routing
*route
=
247 &rcdu
->info
->routes
[pdata
->output
];
249 if (pdata
->type
== RCAR_DU_ENCODER_UNUSED
)
252 if (pdata
->output
>= RCAR_DU_OUTPUT_MAX
||
253 route
->possible_crtcs
== 0) {
255 "encoder %u references unexisting output %u, skipping\n",
260 ret
= rcar_du_encoder_init(rcdu
, pdata
->type
, pdata
->output
,
266 /* Set the possible CRTCs and possible clones. There's always at least
267 * one way for all encoders to clone each other, set all bits in the
268 * possible clones field.
270 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
271 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
272 const struct rcar_du_output_routing
*route
=
273 &rcdu
->info
->routes
[renc
->output
];
275 encoder
->possible_crtcs
= route
->possible_crtcs
;
276 encoder
->possible_clones
= (1 << rcdu
->pdata
->num_encoders
) - 1;
279 /* Now that the CRTCs have been initialized register the planes. */
280 for (i
= 0; i
< num_groups
; ++i
) {
281 ret
= rcar_du_planes_register(&rcdu
->groups
[i
]);
286 drm_kms_helper_poll_init(dev
);
288 drm_helper_disable_unused_functions(dev
);
290 fbdev
= drm_fbdev_cma_init(dev
, 32, dev
->mode_config
.num_crtc
,
291 dev
->mode_config
.num_connector
);
293 return PTR_ERR(fbdev
);
295 #ifndef CONFIG_FRAMEBUFFER_CONSOLE
296 drm_fbdev_cma_restore_mode(fbdev
);