1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3  * Authors:
4  *	Inki Dae <inki.dae@samsung.com>
5  *	Joonyoung Shim <jy0922.shim@samsung.com>
6  *	Seung-Woo Kim <sw0312.kim@samsung.com>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include "drmP.h"
29 #include "drm.h"
30 #include "drm_crtc_helper.h"
31 
32 #include <drm/exynos_drm.h>
33 
34 #include "exynos_drm_drv.h"
35 #include "exynos_drm_crtc.h"
36 #include "exynos_drm_encoder.h"
37 #include "exynos_drm_fbdev.h"
38 #include "exynos_drm_fb.h"
39 #include "exynos_drm_gem.h"
40 #include "exynos_drm_plane.h"
41 #include "exynos_drm_vidi.h"
42 
43 #define DRIVER_NAME	"exynos"
44 #define DRIVER_DESC	"Samsung SoC DRM"
45 #define DRIVER_DATE	"20110530"
46 #define DRIVER_MAJOR	1
47 #define DRIVER_MINOR	0
48 
49 #define VBLANK_OFF_DELAY	50000
50 
51 static int exynos_drm_load(struct drm_device *dev, unsigned long flags)
52 {
53 	struct exynos_drm_private *private;
54 	int ret;
55 	int nr;
56 
57 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
58 
59 	private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
60 	if (!private) {
61 		DRM_ERROR("failed to allocate private\n");
62 		return -ENOMEM;
63 	}
64 
65 	INIT_LIST_HEAD(&private->pageflip_event_list);
66 	dev->dev_private = (void *)private;
67 
68 	drm_mode_config_init(dev);
69 
70 	/* init kms poll for handling hpd */
71 	drm_kms_helper_poll_init(dev);
72 
73 	exynos_drm_mode_config_init(dev);
74 
75 	/*
76 	 * EXYNOS4 is enough to have two CRTCs and each crtc would be used
77 	 * without dependency of hardware.
78 	 */
79 	for (nr = 0; nr < MAX_CRTC; nr++) {
80 		ret = exynos_drm_crtc_create(dev, nr);
81 		if (ret)
82 			goto err_crtc;
83 	}
84 
85 	for (nr = 0; nr < MAX_PLANE; nr++) {
86 		ret = exynos_plane_init(dev, nr);
87 		if (ret)
88 			goto err_crtc;
89 	}
90 
91 	ret = drm_vblank_init(dev, MAX_CRTC);
92 	if (ret)
93 		goto err_crtc;
94 
95 	/*
96 	 * probe sub drivers such as display controller and hdmi driver,
97 	 * that were registered at probe() of platform driver
98 	 * to the sub driver and create encoder and connector for them.
99 	 */
100 	ret = exynos_drm_device_register(dev);
101 	if (ret)
102 		goto err_vblank;
103 
104 	/* setup possible_clones. */
105 	exynos_drm_encoder_setup(dev);
106 
107 	/*
108 	 * create and configure fb helper and also exynos specific
109 	 * fbdev object.
110 	 */
111 	ret = exynos_drm_fbdev_init(dev);
112 	if (ret) {
113 		DRM_ERROR("failed to initialize drm fbdev\n");
114 		goto err_drm_device;
115 	}
116 
117 	drm_vblank_offdelay = VBLANK_OFF_DELAY;
118 
119 	return 0;
120 
121 err_drm_device:
122 	exynos_drm_device_unregister(dev);
123 err_vblank:
124 	drm_vblank_cleanup(dev);
125 err_crtc:
126 	drm_mode_config_cleanup(dev);
127 	kfree(private);
128 
129 	return ret;
130 }
131 
132 static int exynos_drm_unload(struct drm_device *dev)
133 {
134 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
135 
136 	exynos_drm_fbdev_fini(dev);
137 	exynos_drm_device_unregister(dev);
138 	drm_vblank_cleanup(dev);
139 	drm_kms_helper_poll_fini(dev);
140 	drm_mode_config_cleanup(dev);
141 	kfree(dev->dev_private);
142 
143 	dev->dev_private = NULL;
144 
145 	return 0;
146 }
147 
148 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
149 {
150 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
151 
152 	return exynos_drm_subdrv_open(dev, file);
153 }
154 
155 static void exynos_drm_preclose(struct drm_device *dev,
156 					struct drm_file *file)
157 {
158 	struct exynos_drm_private *private = dev->dev_private;
159 	struct drm_pending_vblank_event *e, *t;
160 	unsigned long flags;
161 
162 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
163 
164 	/* release events of current file */
165 	spin_lock_irqsave(&dev->event_lock, flags);
166 	list_for_each_entry_safe(e, t, &private->pageflip_event_list,
167 			base.link) {
168 		if (e->base.file_priv == file) {
169 			list_del(&e->base.link);
170 			e->base.destroy(&e->base);
171 		}
172 	}
173 	spin_unlock_irqrestore(&dev->event_lock, flags);
174 
175 	exynos_drm_subdrv_close(dev, file);
176 }
177 
178 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
179 {
180 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
181 
182 	if (!file->driver_priv)
183 		return;
184 
185 	kfree(file->driver_priv);
186 	file->driver_priv = NULL;
187 }
188 
189 static void exynos_drm_lastclose(struct drm_device *dev)
190 {
191 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
192 
193 	exynos_drm_fbdev_restore_mode(dev);
194 }
195 
196 static struct vm_operations_struct exynos_drm_gem_vm_ops = {
197 	.fault = exynos_drm_gem_fault,
198 	.open = drm_gem_vm_open,
199 	.close = drm_gem_vm_close,
200 };
201 
202 static struct drm_ioctl_desc exynos_ioctls[] = {
203 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
204 			DRM_UNLOCKED | DRM_AUTH),
205 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP_OFFSET,
206 			exynos_drm_gem_map_offset_ioctl, DRM_UNLOCKED |
207 			DRM_AUTH),
208 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MMAP,
209 			exynos_drm_gem_mmap_ioctl, DRM_UNLOCKED | DRM_AUTH),
210 	DRM_IOCTL_DEF_DRV(EXYNOS_PLANE_SET_ZPOS, exynos_plane_set_zpos_ioctl,
211 			DRM_UNLOCKED | DRM_AUTH),
212 	DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
213 			vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
214 };
215 
216 static const struct file_operations exynos_drm_driver_fops = {
217 	.owner		= THIS_MODULE,
218 	.open		= drm_open,
219 	.mmap		= exynos_drm_gem_mmap,
220 	.poll		= drm_poll,
221 	.read		= drm_read,
222 	.unlocked_ioctl	= drm_ioctl,
223 	.release	= drm_release,
224 };
225 
226 static struct drm_driver exynos_drm_driver = {
227 	.driver_features	= DRIVER_HAVE_IRQ | DRIVER_BUS_PLATFORM |
228 				  DRIVER_MODESET | DRIVER_GEM,
229 	.load			= exynos_drm_load,
230 	.unload			= exynos_drm_unload,
231 	.open			= exynos_drm_open,
232 	.preclose		= exynos_drm_preclose,
233 	.lastclose		= exynos_drm_lastclose,
234 	.postclose		= exynos_drm_postclose,
235 	.get_vblank_counter	= drm_vblank_count,
236 	.enable_vblank		= exynos_drm_crtc_enable_vblank,
237 	.disable_vblank		= exynos_drm_crtc_disable_vblank,
238 	.gem_init_object	= exynos_drm_gem_init_object,
239 	.gem_free_object	= exynos_drm_gem_free_object,
240 	.gem_vm_ops		= &exynos_drm_gem_vm_ops,
241 	.dumb_create		= exynos_drm_gem_dumb_create,
242 	.dumb_map_offset	= exynos_drm_gem_dumb_map_offset,
243 	.dumb_destroy		= exynos_drm_gem_dumb_destroy,
244 	.ioctls			= exynos_ioctls,
245 	.fops			= &exynos_drm_driver_fops,
246 	.name	= DRIVER_NAME,
247 	.desc	= DRIVER_DESC,
248 	.date	= DRIVER_DATE,
249 	.major	= DRIVER_MAJOR,
250 	.minor	= DRIVER_MINOR,
251 };
252 
253 static int exynos_drm_platform_probe(struct platform_device *pdev)
254 {
255 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
256 
257 	exynos_drm_driver.num_ioctls = DRM_ARRAY_SIZE(exynos_ioctls);
258 
259 	return drm_platform_init(&exynos_drm_driver, pdev);
260 }
261 
262 static int exynos_drm_platform_remove(struct platform_device *pdev)
263 {
264 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
265 
266 	drm_platform_exit(&exynos_drm_driver, pdev);
267 
268 	return 0;
269 }
270 
271 static struct platform_driver exynos_drm_platform_driver = {
272 	.probe		= exynos_drm_platform_probe,
273 	.remove		= __devexit_p(exynos_drm_platform_remove),
274 	.driver		= {
275 		.owner	= THIS_MODULE,
276 		.name	= "exynos-drm",
277 	},
278 };
279 
280 static int __init exynos_drm_init(void)
281 {
282 	int ret;
283 
284 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
285 
286 #ifdef CONFIG_DRM_EXYNOS_FIMD
287 	ret = platform_driver_register(&fimd_driver);
288 	if (ret < 0)
289 		goto out_fimd;
290 #endif
291 
292 #ifdef CONFIG_DRM_EXYNOS_HDMI
293 	ret = platform_driver_register(&hdmi_driver);
294 	if (ret < 0)
295 		goto out_hdmi;
296 	ret = platform_driver_register(&mixer_driver);
297 	if (ret < 0)
298 		goto out_mixer;
299 	ret = platform_driver_register(&exynos_drm_common_hdmi_driver);
300 	if (ret < 0)
301 		goto out_common_hdmi;
302 #endif
303 
304 #ifdef CONFIG_DRM_EXYNOS_VIDI
305 	ret = platform_driver_register(&vidi_driver);
306 	if (ret < 0)
307 		goto out_vidi;
308 #endif
309 
310 	ret = platform_driver_register(&exynos_drm_platform_driver);
311 	if (ret < 0)
312 		goto out;
313 
314 	return 0;
315 
316 out:
317 #ifdef CONFIG_DRM_EXYNOS_VIDI
318 out_vidi:
319 	platform_driver_unregister(&vidi_driver);
320 #endif
321 
322 #ifdef CONFIG_DRM_EXYNOS_HDMI
323 	platform_driver_unregister(&exynos_drm_common_hdmi_driver);
324 out_common_hdmi:
325 	platform_driver_unregister(&mixer_driver);
326 out_mixer:
327 	platform_driver_unregister(&hdmi_driver);
328 out_hdmi:
329 #endif
330 
331 #ifdef CONFIG_DRM_EXYNOS_FIMD
332 	platform_driver_unregister(&fimd_driver);
333 out_fimd:
334 #endif
335 	return ret;
336 }
337 
338 static void __exit exynos_drm_exit(void)
339 {
340 	DRM_DEBUG_DRIVER("%s\n", __FILE__);
341 
342 	platform_driver_unregister(&exynos_drm_platform_driver);
343 
344 #ifdef CONFIG_DRM_EXYNOS_HDMI
345 	platform_driver_unregister(&exynos_drm_common_hdmi_driver);
346 	platform_driver_unregister(&mixer_driver);
347 	platform_driver_unregister(&hdmi_driver);
348 #endif
349 
350 #ifdef CONFIG_DRM_EXYNOS_VIDI
351 	platform_driver_unregister(&vidi_driver);
352 #endif
353 
354 #ifdef CONFIG_DRM_EXYNOS_FIMD
355 	platform_driver_unregister(&fimd_driver);
356 #endif
357 }
358 
359 module_init(exynos_drm_init);
360 module_exit(exynos_drm_exit);
361 
362 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
363 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
364 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
365 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
366 MODULE_LICENSE("GPL");
367