blob: 08b9a8caadb7b389bb503ef79527704f0f1ace78 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/*
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 *
Inki Daed81aecb2012-12-18 02:30:17 +09008 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
Inki Dae1c248b72011-10-04 19:19:01 +090012 */
13
Sean Paulaf65c802014-01-30 16:19:27 -050014#include <linux/pm_runtime.h>
David Howells760285e2012-10-02 18:01:07 +010015#include <drm/drmP.h>
16#include <drm/drm_crtc_helper.h>
Inki Dae1c248b72011-10-04 19:19:01 +090017
Inki Daef37cd5e2014-05-09 14:25:20 +090018#include <linux/component.h>
Inki Dae96f54212013-12-20 19:16:24 +090019
Inki Dae1c248b72011-10-04 19:19:01 +090020#include <drm/exynos_drm.h>
21
22#include "exynos_drm_drv.h"
23#include "exynos_drm_crtc.h"
Inki Daed081f562012-02-15 11:25:19 +090024#include "exynos_drm_encoder.h"
Inki Dae1c248b72011-10-04 19:19:01 +090025#include "exynos_drm_fbdev.h"
26#include "exynos_drm_fb.h"
27#include "exynos_drm_gem.h"
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090028#include "exynos_drm_plane.h"
Inki Daeb73d1232012-03-21 10:55:26 +090029#include "exynos_drm_vidi.h"
Inki Daeb2df26c2012-04-23 21:01:28 +090030#include "exynos_drm_dmabuf.h"
Joonyoung Shimd7f16422012-05-17 20:06:32 +090031#include "exynos_drm_g2d.h"
Eunchul Kimcb471f142012-12-14 18:10:31 +090032#include "exynos_drm_ipp.h"
Inki Dae0519f9a2012-10-20 07:53:42 -070033#include "exynos_drm_iommu.h"
Inki Dae1c248b72011-10-04 19:19:01 +090034
Inki Dae0edf9932011-12-15 17:31:24 +090035#define DRIVER_NAME "exynos"
Inki Dae1c248b72011-10-04 19:19:01 +090036#define DRIVER_DESC "Samsung SoC DRM"
37#define DRIVER_DATE "20110530"
38#define DRIVER_MAJOR 1
39#define DRIVER_MINOR 0
40
Rahul Sharma422bd002012-10-16 05:50:12 +053041static struct platform_device *exynos_drm_pdev;
42
Inki Daef37cd5e2014-05-09 14:25:20 +090043static DEFINE_MUTEX(drm_component_lock);
44static LIST_HEAD(drm_component_list);
45
46struct component_dev {
47 struct list_head list;
Inki Daedf5225b2014-05-29 18:28:02 +090048 struct device *crtc_dev;
49 struct device *conn_dev;
50 enum exynos_drm_output_type out_type;
51 unsigned int dev_type_flag;
Inki Daef37cd5e2014-05-09 14:25:20 +090052};
53
Inki Dae1c248b72011-10-04 19:19:01 +090054static int exynos_drm_load(struct drm_device *dev, unsigned long flags)
55{
56 struct exynos_drm_private *private;
57 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +090058
Inki Dae1c248b72011-10-04 19:19:01 +090059 private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +090060 if (!private)
Inki Dae1c248b72011-10-04 19:19:01 +090061 return -ENOMEM;
Inki Dae1c248b72011-10-04 19:19:01 +090062
Sean Paulaf65c802014-01-30 16:19:27 -050063 dev_set_drvdata(dev->dev, dev);
Inki Dae1c248b72011-10-04 19:19:01 +090064 dev->dev_private = (void *)private;
65
Inki Dae0519f9a2012-10-20 07:53:42 -070066 /*
67 * create mapping to manage iommu table and set a pointer to iommu
68 * mapping structure to iommu_mapping of private data.
69 * also this iommu_mapping can be used to check if iommu is supported
70 * or not.
71 */
72 ret = drm_create_iommu_mapping(dev);
73 if (ret < 0) {
74 DRM_ERROR("failed to create iommu mapping.\n");
Inki Daed2ba65f2014-02-28 18:37:02 +090075 goto err_free_private;
Inki Dae0519f9a2012-10-20 07:53:42 -070076 }
77
Inki Dae1c248b72011-10-04 19:19:01 +090078 drm_mode_config_init(dev);
79
80 exynos_drm_mode_config_init(dev);
81
Inki Daed081f562012-02-15 11:25:19 +090082 /* setup possible_clones. */
83 exynos_drm_encoder_setup(dev);
84
Daniel Vettera9a346d2013-12-11 11:34:23 +010085 platform_set_drvdata(dev->platformdev, dev);
86
Inki Daef37cd5e2014-05-09 14:25:20 +090087 /* Try to bind all sub drivers. */
88 ret = component_bind_all(dev->dev, dev);
89 if (ret)
Andrzej Hajdac52142e2014-10-07 22:09:14 +090090 goto err_mode_config_cleanup;
91
92 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
93 if (ret)
94 goto err_unbind_all;
Inki Daef37cd5e2014-05-09 14:25:20 +090095
Inki Daed1afe7d2014-04-14 12:55:55 +090096 /* Probe non kms sub drivers and virtual display driver. */
Inki Daef37cd5e2014-05-09 14:25:20 +090097 ret = exynos_drm_device_subdrv_probe(dev);
98 if (ret)
Andrzej Hajdac52142e2014-10-07 22:09:14 +090099 goto err_cleanup_vblank;
Inki Daef37cd5e2014-05-09 14:25:20 +0900100
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300101 drm_mode_config_reset(dev);
102
Joonyoung Shim4a3ffed2014-09-18 17:50:35 +0900103 /*
104 * enable drm irq mode.
105 * - with irq_enabled = true, we can use the vblank feature.
106 *
107 * P.S. note that we wouldn't use drm irq handler but
108 * just specific driver own one instead because
109 * drm framework supports only one irq handler.
110 */
111 dev->irq_enabled = true;
112
113 /*
114 * with vblank_disable_allowed = true, vblank interrupt will be disabled
115 * by drm timer once a current process gives up ownership of
116 * vblank event.(after drm_vblank_put function is called)
117 */
118 dev->vblank_disable_allowed = true;
119
Andrzej Hajda3cb68302014-10-10 14:31:54 +0200120 /* init kms poll for handling hpd */
121 drm_kms_helper_poll_init(dev);
122
123 /* force connectors detection */
124 drm_helper_hpd_irq_event(dev);
125
Inki Dae1c248b72011-10-04 19:19:01 +0900126 return 0;
127
Inki Daef37cd5e2014-05-09 14:25:20 +0900128err_cleanup_vblank:
Inki Dae1c248b72011-10-04 19:19:01 +0900129 drm_vblank_cleanup(dev);
Andrzej Hajdac52142e2014-10-07 22:09:14 +0900130err_unbind_all:
131 component_unbind_all(dev->dev, dev);
Sean Paul080be03d2014-02-19 21:02:55 +0900132err_mode_config_cleanup:
133 drm_mode_config_cleanup(dev);
Inki Dae0519f9a2012-10-20 07:53:42 -0700134 drm_release_iommu_mapping(dev);
Inki Daed2ba65f2014-02-28 18:37:02 +0900135err_free_private:
Inki Dae1c248b72011-10-04 19:19:01 +0900136 kfree(private);
137
138 return ret;
139}
140
141static int exynos_drm_unload(struct drm_device *dev)
142{
Inki Daef37cd5e2014-05-09 14:25:20 +0900143 exynos_drm_device_subdrv_remove(dev);
144
Inki Dae1c248b72011-10-04 19:19:01 +0900145 exynos_drm_fbdev_fini(dev);
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900146 drm_kms_helper_poll_fini(dev);
Inki Dae1c248b72011-10-04 19:19:01 +0900147
Andrzej Hajda9f3dd7d2014-09-09 15:16:06 +0200148 drm_vblank_cleanup(dev);
Andrzej Hajdac52142e2014-10-07 22:09:14 +0900149 component_unbind_all(dev->dev, dev);
Andrzej Hajda9f3dd7d2014-09-09 15:16:06 +0200150 drm_mode_config_cleanup(dev);
151 drm_release_iommu_mapping(dev);
152
153 kfree(dev->dev_private);
Inki Dae1c248b72011-10-04 19:19:01 +0900154 dev->dev_private = NULL;
155
156 return 0;
157}
158
Sean Paulaf65c802014-01-30 16:19:27 -0500159static int exynos_drm_suspend(struct drm_device *dev, pm_message_t state)
160{
161 struct drm_connector *connector;
162
163 drm_modeset_lock_all(dev);
164 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
165 int old_dpms = connector->dpms;
166
167 if (connector->funcs->dpms)
168 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
169
170 /* Set the old mode back to the connector for resume */
171 connector->dpms = old_dpms;
172 }
173 drm_modeset_unlock_all(dev);
174
175 return 0;
176}
177
178static int exynos_drm_resume(struct drm_device *dev)
179{
180 struct drm_connector *connector;
181
182 drm_modeset_lock_all(dev);
183 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Andrzej Hajda74cfe072014-10-10 14:31:56 +0200184 if (connector->funcs->dpms) {
185 int dpms = connector->dpms;
186
187 connector->dpms = DRM_MODE_DPMS_OFF;
188 connector->funcs->dpms(connector, dpms);
189 }
Sean Paulaf65c802014-01-30 16:19:27 -0500190 }
Takashi Iwaia16f2232014-05-09 08:14:15 +0200191 drm_modeset_unlock_all(dev);
Sean Paulaf65c802014-01-30 16:19:27 -0500192
Sean Paulaf65c802014-01-30 16:19:27 -0500193 return 0;
194}
195
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900196static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
Inki Daeccf4d882011-10-14 13:29:51 +0900197{
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900198 struct drm_exynos_file_private *file_priv;
YoungJun Choba3706c2013-07-01 17:00:47 +0900199 int ret;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900200
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900201 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
202 if (!file_priv)
203 return -ENOMEM;
204
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900205 file->driver_priv = file_priv;
Inki Daeb2df26c2012-04-23 21:01:28 +0900206
YoungJun Choba3706c2013-07-01 17:00:47 +0900207 ret = exynos_drm_subdrv_open(dev, file);
Sachin Kamat6ca605f2014-01-16 11:31:26 +0530208 if (ret)
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800209 goto err_file_priv_free;
YoungJun Choba3706c2013-07-01 17:00:47 +0900210
211 return ret;
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800212
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800213err_file_priv_free:
Sachin Kamat6ca605f2014-01-16 11:31:26 +0530214 kfree(file_priv);
215 file->driver_priv = NULL;
216 return ret;
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900217}
218
Inki Daeccf4d882011-10-14 13:29:51 +0900219static void exynos_drm_preclose(struct drm_device *dev,
220 struct drm_file *file)
221{
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900222 exynos_drm_subdrv_close(dev, file);
Inki Daeccf4d882011-10-14 13:29:51 +0900223}
224
Inki Dae53ef2992012-02-15 11:25:22 +0900225static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
226{
Inki Dae0cbc3302013-10-01 14:51:37 +0900227 struct drm_pending_event *e, *et;
228 unsigned long flags;
229
Inki Dae53ef2992012-02-15 11:25:22 +0900230 if (!file->driver_priv)
231 return;
232
Inki Dae0cbc3302013-10-01 14:51:37 +0900233 spin_lock_irqsave(&dev->event_lock, flags);
Inki Dae0cbc3302013-10-01 14:51:37 +0900234 /* Release all events handled by page flip handler but not freed. */
235 list_for_each_entry_safe(e, et, &file->event_list, link) {
236 list_del(&e->link);
237 e->destroy(e);
238 }
239 spin_unlock_irqrestore(&dev->event_lock, flags);
240
Inki Dae53ef2992012-02-15 11:25:22 +0900241 kfree(file->driver_priv);
242 file->driver_priv = NULL;
243}
244
Inki Dae1c248b72011-10-04 19:19:01 +0900245static void exynos_drm_lastclose(struct drm_device *dev)
246{
Inki Dae1c248b72011-10-04 19:19:01 +0900247 exynos_drm_fbdev_restore_mode(dev);
248}
249
Laurent Pinchart78b68552012-05-17 13:27:22 +0200250static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
Inki Dae1c248b72011-10-04 19:19:01 +0900251 .fault = exynos_drm_gem_fault,
252 .open = drm_gem_vm_open,
253 .close = drm_gem_vm_close,
254};
255
Rob Clarkbaa70942013-08-02 13:27:49 -0400256static const struct drm_ioctl_desc exynos_ioctls[] = {
Inki Dae1c248b72011-10-04 19:19:01 +0900257 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
258 DRM_UNLOCKED | DRM_AUTH),
Inki Dae40cd7e02012-05-04 15:51:17 +0900259 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET,
260 exynos_drm_gem_get_ioctl, DRM_UNLOCKED),
Inki Daeb73d1232012-03-21 10:55:26 +0900261 DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
262 vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900263 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER,
264 exynos_g2d_get_ver_ioctl, DRM_UNLOCKED | DRM_AUTH),
265 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST,
266 exynos_g2d_set_cmdlist_ioctl, DRM_UNLOCKED | DRM_AUTH),
267 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC,
268 exynos_g2d_exec_ioctl, DRM_UNLOCKED | DRM_AUTH),
Eunchul Kimcb471f142012-12-14 18:10:31 +0900269 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY,
270 exynos_drm_ipp_get_property, DRM_UNLOCKED | DRM_AUTH),
271 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY,
272 exynos_drm_ipp_set_property, DRM_UNLOCKED | DRM_AUTH),
273 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF,
274 exynos_drm_ipp_queue_buf, DRM_UNLOCKED | DRM_AUTH),
275 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL,
276 exynos_drm_ipp_cmd_ctrl, DRM_UNLOCKED | DRM_AUTH),
Inki Dae1c248b72011-10-04 19:19:01 +0900277};
278
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900279static const struct file_operations exynos_drm_driver_fops = {
280 .owner = THIS_MODULE,
281 .open = drm_open,
282 .mmap = exynos_drm_gem_mmap,
283 .poll = drm_poll,
284 .read = drm_read,
285 .unlocked_ioctl = drm_ioctl,
Keith Packard804d74a2012-07-09 15:40:07 -0700286#ifdef CONFIG_COMPAT
287 .compat_ioctl = drm_compat_ioctl,
288#endif
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900289 .release = drm_release,
290};
291
Inki Dae1c248b72011-10-04 19:19:01 +0900292static struct drm_driver exynos_drm_driver = {
Joonyoung Shimbe9b64a2014-04-12 09:39:52 +0900293 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
Inki Dae1c248b72011-10-04 19:19:01 +0900294 .load = exynos_drm_load,
295 .unload = exynos_drm_unload,
Sean Paulaf65c802014-01-30 16:19:27 -0500296 .suspend = exynos_drm_suspend,
297 .resume = exynos_drm_resume,
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900298 .open = exynos_drm_open,
Inki Daeccf4d882011-10-14 13:29:51 +0900299 .preclose = exynos_drm_preclose,
Inki Dae1c248b72011-10-04 19:19:01 +0900300 .lastclose = exynos_drm_lastclose,
Inki Dae53ef2992012-02-15 11:25:22 +0900301 .postclose = exynos_drm_postclose,
David Herrmann915b4d12014-08-29 12:12:43 +0200302 .set_busid = drm_platform_set_busid,
Inki Dae1c248b72011-10-04 19:19:01 +0900303 .get_vblank_counter = drm_vblank_count,
304 .enable_vblank = exynos_drm_crtc_enable_vblank,
305 .disable_vblank = exynos_drm_crtc_disable_vblank,
Inki Dae1c248b72011-10-04 19:19:01 +0900306 .gem_free_object = exynos_drm_gem_free_object,
307 .gem_vm_ops = &exynos_drm_gem_vm_ops,
308 .dumb_create = exynos_drm_gem_dumb_create,
309 .dumb_map_offset = exynos_drm_gem_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200310 .dumb_destroy = drm_gem_dumb_destroy,
Inki Daeb2df26c2012-04-23 21:01:28 +0900311 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
312 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
313 .gem_prime_export = exynos_dmabuf_prime_export,
314 .gem_prime_import = exynos_dmabuf_prime_import,
Inki Dae1c248b72011-10-04 19:19:01 +0900315 .ioctls = exynos_ioctls,
Rob Clarkbaa70942013-08-02 13:27:49 -0400316 .num_ioctls = ARRAY_SIZE(exynos_ioctls),
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900317 .fops = &exynos_drm_driver_fops,
Inki Dae1c248b72011-10-04 19:19:01 +0900318 .name = DRIVER_NAME,
319 .desc = DRIVER_DESC,
320 .date = DRIVER_DATE,
321 .major = DRIVER_MAJOR,
322 .minor = DRIVER_MINOR,
323};
324
Sean Paulaf65c802014-01-30 16:19:27 -0500325#ifdef CONFIG_PM_SLEEP
326static int exynos_drm_sys_suspend(struct device *dev)
327{
328 struct drm_device *drm_dev = dev_get_drvdata(dev);
329 pm_message_t message;
330
Krzysztof Kozlowskid50a1902014-06-30 15:25:44 +0200331 if (pm_runtime_suspended(dev) || !drm_dev)
Sean Paulaf65c802014-01-30 16:19:27 -0500332 return 0;
333
334 message.event = PM_EVENT_SUSPEND;
335 return exynos_drm_suspend(drm_dev, message);
336}
337
338static int exynos_drm_sys_resume(struct device *dev)
339{
340 struct drm_device *drm_dev = dev_get_drvdata(dev);
341
Krzysztof Kozlowskid50a1902014-06-30 15:25:44 +0200342 if (pm_runtime_suspended(dev) || !drm_dev)
Sean Paulaf65c802014-01-30 16:19:27 -0500343 return 0;
344
345 return exynos_drm_resume(drm_dev);
346}
347#endif
348
Sean Paulaf65c802014-01-30 16:19:27 -0500349static const struct dev_pm_ops exynos_drm_pm_ops = {
350 SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_sys_suspend, exynos_drm_sys_resume)
Sean Paulaf65c802014-01-30 16:19:27 -0500351};
352
Inki Daef37cd5e2014-05-09 14:25:20 +0900353int exynos_drm_component_add(struct device *dev,
Inki Daedf5225b2014-05-29 18:28:02 +0900354 enum exynos_drm_device_type dev_type,
355 enum exynos_drm_output_type out_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900356{
357 struct component_dev *cdev;
Inki Daedf5225b2014-05-29 18:28:02 +0900358
359 if (dev_type != EXYNOS_DEVICE_TYPE_CRTC &&
360 dev_type != EXYNOS_DEVICE_TYPE_CONNECTOR) {
361 DRM_ERROR("invalid device type.\n");
362 return -EINVAL;
363 }
364
365 mutex_lock(&drm_component_lock);
366
367 /*
368 * Make sure to check if there is a component which has two device
369 * objects, for connector and for encoder/connector.
370 * It should make sure that crtc and encoder/connector drivers are
371 * ready before exynos drm core binds them.
372 */
373 list_for_each_entry(cdev, &drm_component_list, list) {
374 if (cdev->out_type == out_type) {
375 /*
376 * If crtc and encoder/connector device objects are
377 * added already just return.
378 */
379 if (cdev->dev_type_flag == (EXYNOS_DEVICE_TYPE_CRTC |
380 EXYNOS_DEVICE_TYPE_CONNECTOR)) {
381 mutex_unlock(&drm_component_lock);
382 return 0;
383 }
384
385 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
386 cdev->crtc_dev = dev;
387 cdev->dev_type_flag |= dev_type;
388 }
389
390 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
391 cdev->conn_dev = dev;
392 cdev->dev_type_flag |= dev_type;
393 }
394
395 mutex_unlock(&drm_component_lock);
396 return 0;
397 }
398 }
399
400 mutex_unlock(&drm_component_lock);
Inki Daef37cd5e2014-05-09 14:25:20 +0900401
402 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
403 if (!cdev)
404 return -ENOMEM;
405
Inki Daedf5225b2014-05-29 18:28:02 +0900406 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC)
407 cdev->crtc_dev = dev;
408 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR)
409 cdev->conn_dev = dev;
Inki Daef37cd5e2014-05-09 14:25:20 +0900410
Inki Daedf5225b2014-05-29 18:28:02 +0900411 cdev->out_type = out_type;
412 cdev->dev_type_flag = dev_type;
Inki Daef37cd5e2014-05-09 14:25:20 +0900413
414 mutex_lock(&drm_component_lock);
415 list_add_tail(&cdev->list, &drm_component_list);
416 mutex_unlock(&drm_component_lock);
417
418 return 0;
419}
420
421void exynos_drm_component_del(struct device *dev,
Inki Daedf5225b2014-05-29 18:28:02 +0900422 enum exynos_drm_device_type dev_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900423{
424 struct component_dev *cdev, *next;
425
426 mutex_lock(&drm_component_lock);
427
428 list_for_each_entry_safe(cdev, next, &drm_component_list, list) {
Inki Daedf5225b2014-05-29 18:28:02 +0900429 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
430 if (cdev->crtc_dev == dev) {
431 cdev->crtc_dev = NULL;
432 cdev->dev_type_flag &= ~dev_type;
433 }
434 }
435
436 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
437 if (cdev->conn_dev == dev) {
438 cdev->conn_dev = NULL;
439 cdev->dev_type_flag &= ~dev_type;
440 }
441 }
442
443 /*
444 * Release cdev object only in case that both of crtc and
445 * encoder/connector device objects are NULL.
446 */
447 if (!cdev->crtc_dev && !cdev->conn_dev) {
Inki Daef37cd5e2014-05-09 14:25:20 +0900448 list_del(&cdev->list);
449 kfree(cdev);
Inki Daef37cd5e2014-05-09 14:25:20 +0900450 }
451 }
452
453 mutex_unlock(&drm_component_lock);
Inki Daef37cd5e2014-05-09 14:25:20 +0900454}
455
Inki Dae53c55582014-09-11 17:04:03 +0900456static int compare_dev(struct device *dev, void *data)
Inki Daef37cd5e2014-05-09 14:25:20 +0900457{
458 return dev == (struct device *)data;
459}
460
Inki Dae53c55582014-09-11 17:04:03 +0900461static struct component_match *exynos_drm_match_add(struct device *dev)
Inki Daef37cd5e2014-05-09 14:25:20 +0900462{
Inki Dae53c55582014-09-11 17:04:03 +0900463 struct component_match *match = NULL;
Inki Daef37cd5e2014-05-09 14:25:20 +0900464 struct component_dev *cdev;
Inki Daedf5225b2014-05-29 18:28:02 +0900465 unsigned int attach_cnt = 0;
Inki Daef37cd5e2014-05-09 14:25:20 +0900466
467 mutex_lock(&drm_component_lock);
468
Inki Daef7c2f36f2014-11-06 23:00:37 +0900469 /* Do not retry to probe if there is no any kms driver regitered. */
470 if (list_empty(&drm_component_list)) {
471 mutex_unlock(&drm_component_lock);
472 return ERR_PTR(-ENODEV);
473 }
474
Inki Daef37cd5e2014-05-09 14:25:20 +0900475 list_for_each_entry(cdev, &drm_component_list, list) {
Inki Daedf5225b2014-05-29 18:28:02 +0900476 /*
477 * Add components to master only in case that crtc and
478 * encoder/connector device objects exist.
479 */
480 if (!cdev->crtc_dev || !cdev->conn_dev)
481 continue;
482
483 attach_cnt++;
484
Inki Daef37cd5e2014-05-09 14:25:20 +0900485 mutex_unlock(&drm_component_lock);
486
Inki Daedf5225b2014-05-29 18:28:02 +0900487 /*
488 * fimd and dpi modules have same device object so add
489 * only crtc device object in this case.
Inki Daedf5225b2014-05-29 18:28:02 +0900490 */
491 if (cdev->crtc_dev == cdev->conn_dev) {
Inki Dae53c55582014-09-11 17:04:03 +0900492 component_match_add(dev, &match, compare_dev,
493 cdev->crtc_dev);
Inki Daedf5225b2014-05-29 18:28:02 +0900494 goto out_lock;
495 }
496
497 /*
498 * Do not chage below call order.
499 * crtc device first should be added to master because
500 * connector/encoder need pipe number of crtc when they
501 * are created.
502 */
Inki Dae53c55582014-09-11 17:04:03 +0900503 component_match_add(dev, &match, compare_dev, cdev->crtc_dev);
504 component_match_add(dev, &match, compare_dev, cdev->conn_dev);
Inki Daedf5225b2014-05-29 18:28:02 +0900505
506out_lock:
Inki Daef37cd5e2014-05-09 14:25:20 +0900507 mutex_lock(&drm_component_lock);
508 }
509
510 mutex_unlock(&drm_component_lock);
511
Inki Dae53c55582014-09-11 17:04:03 +0900512 return attach_cnt ? match : ERR_PTR(-EPROBE_DEFER);
Inki Daef37cd5e2014-05-09 14:25:20 +0900513}
514
515static int exynos_drm_bind(struct device *dev)
516{
Inki Daef37cd5e2014-05-09 14:25:20 +0900517 return drm_platform_init(&exynos_drm_driver, to_platform_device(dev));
518}
519
520static void exynos_drm_unbind(struct device *dev)
521{
522 drm_put_dev(dev_get_drvdata(dev));
523}
524
525static const struct component_master_ops exynos_drm_ops = {
Inki Daef37cd5e2014-05-09 14:25:20 +0900526 .bind = exynos_drm_bind,
527 .unbind = exynos_drm_unbind,
Inki Dae1c248b72011-10-04 19:19:01 +0900528};
529
Andrzej Hajda72390672014-11-13 16:37:57 +0900530static struct platform_driver *const exynos_drm_kms_drivers[] = {
531#ifdef CONFIG_DRM_EXYNOS_FIMD
532 &fimd_driver,
533#endif
Ajay Kumar96976c32015-02-05 21:24:04 +0530534#ifdef CONFIG_DRM_EXYNOS7_DECON
535 &decon_driver,
536#endif
Andrzej Hajda72390672014-11-13 16:37:57 +0900537#ifdef CONFIG_DRM_EXYNOS_DP
538 &dp_driver,
539#endif
540#ifdef CONFIG_DRM_EXYNOS_DSI
541 &dsi_driver,
542#endif
543#ifdef CONFIG_DRM_EXYNOS_HDMI
544 &mixer_driver,
545 &hdmi_driver,
546#endif
547};
548
549static struct platform_driver *const exynos_drm_non_kms_drivers[] = {
550#ifdef CONFIG_DRM_EXYNOS_G2D
551 &g2d_driver,
552#endif
553#ifdef CONFIG_DRM_EXYNOS_FIMC
554 &fimc_driver,
555#endif
556#ifdef CONFIG_DRM_EXYNOS_ROTATOR
557 &rotator_driver,
558#endif
559#ifdef CONFIG_DRM_EXYNOS_GSC
560 &gsc_driver,
561#endif
562#ifdef CONFIG_DRM_EXYNOS_IPP
563 &ipp_driver,
564#endif
565};
566
Inki Daef37cd5e2014-05-09 14:25:20 +0900567static int exynos_drm_platform_probe(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900568{
Inki Dae53c55582014-09-11 17:04:03 +0900569 struct component_match *match;
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900570
Inki Daef37cd5e2014-05-09 14:25:20 +0900571 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Damien Lespiauf95aeb12014-06-09 14:39:49 +0100572 exynos_drm_driver.num_ioctls = ARRAY_SIZE(exynos_ioctls);
Inki Daef37cd5e2014-05-09 14:25:20 +0900573
Inki Dae7afbfcc2014-11-07 21:32:34 +0900574 match = exynos_drm_match_add(&pdev->dev);
575 if (IS_ERR(match)) {
Gustavo Padovan820687be2014-11-24 16:37:26 +0900576 return PTR_ERR(match);
Inki Dae7afbfcc2014-11-07 21:32:34 +0900577 }
578
Gustavo Padovan820687be2014-11-24 16:37:26 +0900579 return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
580 match);
Inki Dae1c248b72011-10-04 19:19:01 +0900581}
582
Inki Daef37cd5e2014-05-09 14:25:20 +0900583static int exynos_drm_platform_remove(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900584{
Inki Daef37cd5e2014-05-09 14:25:20 +0900585 component_master_del(&pdev->dev, &exynos_drm_ops);
586 return 0;
587}
588
Inki Dae4846e452014-11-24 17:08:00 +0900589static const char * const strings[] = {
590 "samsung,exynos3",
591 "samsung,exynos4",
592 "samsung,exynos5",
Ajay Kumar96976c32015-02-05 21:24:04 +0530593 "samsung,exynos7",
Inki Dae4846e452014-11-24 17:08:00 +0900594};
595
Inki Daef37cd5e2014-05-09 14:25:20 +0900596static struct platform_driver exynos_drm_platform_driver = {
597 .probe = exynos_drm_platform_probe,
598 .remove = exynos_drm_platform_remove,
599 .driver = {
Inki Daef37cd5e2014-05-09 14:25:20 +0900600 .name = "exynos-drm",
601 .pm = &exynos_drm_pm_ops,
602 },
603};
604
605static int exynos_drm_init(void)
606{
Inki Dae4846e452014-11-24 17:08:00 +0900607 bool is_exynos = false;
Gustavo Padovan820687be2014-11-24 16:37:26 +0900608 int ret, i, j;
Inki Daef37cd5e2014-05-09 14:25:20 +0900609
Inki Dae5cbb37d2014-11-06 19:23:35 +0900610 /*
611 * Register device object only in case of Exynos SoC.
612 *
613 * Below codes resolves temporarily infinite loop issue incurred
614 * by Exynos drm driver when using multi-platform kernel.
615 * So these codes will be replaced with more generic way later.
616 */
Inki Dae4846e452014-11-24 17:08:00 +0900617 for (i = 0; i < ARRAY_SIZE(strings); i++) {
618 if (of_machine_is_compatible(strings[i])) {
619 is_exynos = true;
620 break;
621 }
622 }
623
624 if (!is_exynos)
Inki Dae5cbb37d2014-11-06 19:23:35 +0900625 return -ENODEV;
Inki Daef37cd5e2014-05-09 14:25:20 +0900626
627 exynos_drm_pdev = platform_device_register_simple("exynos-drm", -1,
628 NULL, 0);
629 if (IS_ERR(exynos_drm_pdev))
630 return PTR_ERR(exynos_drm_pdev);
631
Inki Daef37cd5e2014-05-09 14:25:20 +0900632 ret = exynos_drm_probe_vidi();
633 if (ret < 0)
634 goto err_unregister_pd;
Inki Daef37cd5e2014-05-09 14:25:20 +0900635
Gustavo Padovan820687be2014-11-24 16:37:26 +0900636 for (i = 0; i < ARRAY_SIZE(exynos_drm_kms_drivers); ++i) {
637 ret = platform_driver_register(exynos_drm_kms_drivers[i]);
638 if (ret < 0)
639 goto err_unregister_kms_drivers;
640 }
641
642 for (j = 0; j < ARRAY_SIZE(exynos_drm_non_kms_drivers); ++j) {
643 ret = platform_driver_register(exynos_drm_non_kms_drivers[j]);
644 if (ret < 0)
645 goto err_unregister_non_kms_drivers;
646 }
647
648#ifdef CONFIG_DRM_EXYNOS_IPP
649 ret = exynos_platform_device_ipp_register();
650 if (ret < 0)
651 goto err_unregister_non_kms_drivers;
Inki Daef37cd5e2014-05-09 14:25:20 +0900652#endif
653
654 ret = platform_driver_register(&exynos_drm_platform_driver);
655 if (ret)
Gustavo Padovan820687be2014-11-24 16:37:26 +0900656 goto err_unregister_resources;
Inki Daef37cd5e2014-05-09 14:25:20 +0900657
658 return 0;
659
Gustavo Padovan820687be2014-11-24 16:37:26 +0900660err_unregister_resources:
661#ifdef CONFIG_DRM_EXYNOS_IPP
662 exynos_platform_device_ipp_unregister();
663#endif
664
665err_unregister_non_kms_drivers:
666 while (--j >= 0)
667 platform_driver_unregister(exynos_drm_non_kms_drivers[j]);
668
669err_unregister_kms_drivers:
670 while (--i >= 0)
671 platform_driver_unregister(exynos_drm_kms_drivers[i]);
672
Inki Daef37cd5e2014-05-09 14:25:20 +0900673 exynos_drm_remove_vidi();
Sachin Kamat0013fc92014-06-17 17:08:07 +0530674
675err_unregister_pd:
Sachin Kamat0013fc92014-06-17 17:08:07 +0530676 platform_device_unregister(exynos_drm_pdev);
Inki Daef37cd5e2014-05-09 14:25:20 +0900677
678 return ret;
679}
680
681static void exynos_drm_exit(void)
682{
Gustavo Padovan820687be2014-11-24 16:37:26 +0900683 int i;
684
685#ifdef CONFIG_DRM_EXYNOS_IPP
686 exynos_platform_device_ipp_unregister();
Inki Daef37cd5e2014-05-09 14:25:20 +0900687#endif
Gustavo Padovan820687be2014-11-24 16:37:26 +0900688
689 for (i = ARRAY_SIZE(exynos_drm_non_kms_drivers) - 1; i >= 0; --i)
690 platform_driver_unregister(exynos_drm_non_kms_drivers[i]);
691
692 for (i = ARRAY_SIZE(exynos_drm_kms_drivers) - 1; i >= 0; --i)
693 platform_driver_unregister(exynos_drm_kms_drivers[i]);
694
Inki Daef37cd5e2014-05-09 14:25:20 +0900695 platform_driver_unregister(&exynos_drm_platform_driver);
Andrzej Hajda72390672014-11-13 16:37:57 +0900696
Inki Daef37cd5e2014-05-09 14:25:20 +0900697 exynos_drm_remove_vidi();
Andrzej Hajda72390672014-11-13 16:37:57 +0900698
Inki Daef37cd5e2014-05-09 14:25:20 +0900699 platform_device_unregister(exynos_drm_pdev);
Inki Dae1c248b72011-10-04 19:19:01 +0900700}
701
702module_init(exynos_drm_init);
703module_exit(exynos_drm_exit);
704
705MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
706MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
707MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
708MODULE_DESCRIPTION("Samsung SoC DRM Driver");
709MODULE_LICENSE("GPL");