1 /*
2  * shmob_drm_kms.c  --  SH Mobile DRM Mode Setting
3  *
4  * Copyright (C) 2012 Renesas Electronics Corporation
5  *
6  * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  *
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.
12  */
13 
14 #include <drm/drmP.h>
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>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 
21 #include "shmob_drm_crtc.h"
22 #include "shmob_drm_drv.h"
23 #include "shmob_drm_kms.h"
24 #include "shmob_drm_regs.h"
25 
26 /* -----------------------------------------------------------------------------
27  * Format helpers
28  */
29 
30 static const struct shmob_drm_format_info shmob_drm_format_infos[] = {
31 	{
32 		.fourcc = DRM_FORMAT_RGB565,
33 		.bpp = 16,
34 		.yuv = false,
35 		.lddfr = LDDFR_PKF_RGB16,
36 	}, {
37 		.fourcc = DRM_FORMAT_RGB888,
38 		.bpp = 24,
39 		.yuv = false,
40 		.lddfr = LDDFR_PKF_RGB24,
41 	}, {
42 		.fourcc = DRM_FORMAT_ARGB8888,
43 		.bpp = 32,
44 		.yuv = false,
45 		.lddfr = LDDFR_PKF_ARGB32,
46 	}, {
47 		.fourcc = DRM_FORMAT_NV12,
48 		.bpp = 12,
49 		.yuv = true,
50 		.lddfr = LDDFR_CC | LDDFR_YF_420,
51 	}, {
52 		.fourcc = DRM_FORMAT_NV21,
53 		.bpp = 12,
54 		.yuv = true,
55 		.lddfr = LDDFR_CC | LDDFR_YF_420,
56 	}, {
57 		.fourcc = DRM_FORMAT_NV16,
58 		.bpp = 16,
59 		.yuv = true,
60 		.lddfr = LDDFR_CC | LDDFR_YF_422,
61 	}, {
62 		.fourcc = DRM_FORMAT_NV61,
63 		.bpp = 16,
64 		.yuv = true,
65 		.lddfr = LDDFR_CC | LDDFR_YF_422,
66 	}, {
67 		.fourcc = DRM_FORMAT_NV24,
68 		.bpp = 24,
69 		.yuv = true,
70 		.lddfr = LDDFR_CC | LDDFR_YF_444,
71 	}, {
72 		.fourcc = DRM_FORMAT_NV42,
73 		.bpp = 24,
74 		.yuv = true,
75 		.lddfr = LDDFR_CC | LDDFR_YF_444,
76 	},
77 };
78 
shmob_drm_format_info(u32 fourcc)79 const struct shmob_drm_format_info *shmob_drm_format_info(u32 fourcc)
80 {
81 	unsigned int i;
82 
83 	for (i = 0; i < ARRAY_SIZE(shmob_drm_format_infos); ++i) {
84 		if (shmob_drm_format_infos[i].fourcc == fourcc)
85 			return &shmob_drm_format_infos[i];
86 	}
87 
88 	return NULL;
89 }
90 
91 /* -----------------------------------------------------------------------------
92  * Frame buffer
93  */
94 
95 static struct drm_framebuffer *
shmob_drm_fb_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_mode_fb_cmd2 * mode_cmd)96 shmob_drm_fb_create(struct drm_device *dev, struct drm_file *file_priv,
97 		    const struct drm_mode_fb_cmd2 *mode_cmd)
98 {
99 	const struct shmob_drm_format_info *format;
100 
101 	format = shmob_drm_format_info(mode_cmd->pixel_format);
102 	if (format == NULL) {
103 		dev_dbg(dev->dev, "unsupported pixel format %08x\n",
104 			mode_cmd->pixel_format);
105 		return ERR_PTR(-EINVAL);
106 	}
107 
108 	if (mode_cmd->pitches[0] & 7 || mode_cmd->pitches[0] >= 65536) {
109 		dev_dbg(dev->dev, "invalid pitch value %u\n",
110 			mode_cmd->pitches[0]);
111 		return ERR_PTR(-EINVAL);
112 	}
113 
114 	if (format->yuv) {
115 		unsigned int chroma_cpp = format->bpp == 24 ? 2 : 1;
116 
117 		if (mode_cmd->pitches[1] != mode_cmd->pitches[0] * chroma_cpp) {
118 			dev_dbg(dev->dev,
119 				"luma and chroma pitches do not match\n");
120 			return ERR_PTR(-EINVAL);
121 		}
122 	}
123 
124 	return drm_gem_fb_create(dev, file_priv, mode_cmd);
125 }
126 
127 static const struct drm_mode_config_funcs shmob_drm_mode_config_funcs = {
128 	.fb_create = shmob_drm_fb_create,
129 };
130 
shmob_drm_modeset_init(struct shmob_drm_device * sdev)131 int shmob_drm_modeset_init(struct shmob_drm_device *sdev)
132 {
133 	drm_mode_config_init(sdev->ddev);
134 
135 	shmob_drm_crtc_create(sdev);
136 	shmob_drm_encoder_create(sdev);
137 	shmob_drm_connector_create(sdev, &sdev->encoder.encoder);
138 
139 	drm_kms_helper_poll_init(sdev->ddev);
140 
141 	sdev->ddev->mode_config.min_width = 0;
142 	sdev->ddev->mode_config.min_height = 0;
143 	sdev->ddev->mode_config.max_width = 4095;
144 	sdev->ddev->mode_config.max_height = 4095;
145 	sdev->ddev->mode_config.funcs = &shmob_drm_mode_config_funcs;
146 
147 	drm_helper_disable_unused_functions(sdev->ddev);
148 
149 	return 0;
150 }
151