blob: 8aee62902ec69e11e0487859bbeb1dc363eb0203 [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;
58 int nr;
59
Inki Dae1c248b72011-10-04 19:19:01 +090060 private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +090061 if (!private)
Inki Dae1c248b72011-10-04 19:19:01 +090062 return -ENOMEM;
Inki Dae1c248b72011-10-04 19:19:01 +090063
64 INIT_LIST_HEAD(&private->pageflip_event_list);
Sean Paulaf65c802014-01-30 16:19:27 -050065 dev_set_drvdata(dev->dev, dev);
Inki Dae1c248b72011-10-04 19:19:01 +090066 dev->dev_private = (void *)private;
67
Inki Dae0519f9a2012-10-20 07:53:42 -070068 /*
69 * create mapping to manage iommu table and set a pointer to iommu
70 * mapping structure to iommu_mapping of private data.
71 * also this iommu_mapping can be used to check if iommu is supported
72 * or not.
73 */
74 ret = drm_create_iommu_mapping(dev);
75 if (ret < 0) {
76 DRM_ERROR("failed to create iommu mapping.\n");
Inki Daed2ba65f2014-02-28 18:37:02 +090077 goto err_free_private;
Inki Dae0519f9a2012-10-20 07:53:42 -070078 }
79
Inki Dae1c248b72011-10-04 19:19:01 +090080 drm_mode_config_init(dev);
81
82 exynos_drm_mode_config_init(dev);
83
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090084 for (nr = 0; nr < MAX_PLANE; nr++) {
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090085 struct drm_plane *plane;
Sean Paul3f283d92014-01-30 16:19:11 -050086 unsigned long possible_crtcs = (1 << MAX_CRTC) - 1;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090087
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +020088 plane = exynos_plane_init(dev, possible_crtcs,
89 DRM_PLANE_TYPE_OVERLAY);
Andrzej Hajda64f7aed2014-10-10 14:31:53 +020090 if (!IS_ERR(plane))
91 continue;
92
93 ret = PTR_ERR(plane);
94 goto err_mode_config_cleanup;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090095 }
96
Inki Daed081f562012-02-15 11:25:19 +090097 /* setup possible_clones. */
98 exynos_drm_encoder_setup(dev);
99
Daniel Vettera9a346d2013-12-11 11:34:23 +0100100 platform_set_drvdata(dev->platformdev, dev);
101
Inki Daef37cd5e2014-05-09 14:25:20 +0900102 /* Try to bind all sub drivers. */
103 ret = component_bind_all(dev->dev, dev);
104 if (ret)
Andrzej Hajdac52142e2014-10-07 22:09:14 +0900105 goto err_mode_config_cleanup;
106
107 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
108 if (ret)
109 goto err_unbind_all;
Inki Daef37cd5e2014-05-09 14:25:20 +0900110
Joonyoung Shim4a3ffed2014-09-18 17:50:35 +0900111 /*
112 * enable drm irq mode.
113 * - with irq_enabled = true, we can use the vblank feature.
114 *
115 * P.S. note that we wouldn't use drm irq handler but
116 * just specific driver own one instead because
117 * drm framework supports only one irq handler.
118 */
119 dev->irq_enabled = true;
120
121 /*
122 * with vblank_disable_allowed = true, vblank interrupt will be disabled
123 * by drm timer once a current process gives up ownership of
124 * vblank event.(after drm_vblank_put function is called)
125 */
126 dev->vblank_disable_allowed = true;
127
Andrzej Hajda3cb68302014-10-10 14:31:54 +0200128 /* init kms poll for handling hpd */
129 drm_kms_helper_poll_init(dev);
130
131 /* force connectors detection */
132 drm_helper_hpd_irq_event(dev);
133
Inki Dae1c248b72011-10-04 19:19:01 +0900134 return 0;
135
Andrzej Hajdac52142e2014-10-07 22:09:14 +0900136err_unbind_all:
137 component_unbind_all(dev->dev, dev);
Sean Paul080be03d2014-02-19 21:02:55 +0900138err_mode_config_cleanup:
139 drm_mode_config_cleanup(dev);
Inki Dae0519f9a2012-10-20 07:53:42 -0700140 drm_release_iommu_mapping(dev);
Inki Daed2ba65f2014-02-28 18:37:02 +0900141err_free_private:
Inki Dae1c248b72011-10-04 19:19:01 +0900142 kfree(private);
143
144 return ret;
145}
146
147static int exynos_drm_unload(struct drm_device *dev)
148{
Inki Dae1c248b72011-10-04 19:19:01 +0900149 exynos_drm_fbdev_fini(dev);
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900150 drm_kms_helper_poll_fini(dev);
Inki Dae1c248b72011-10-04 19:19:01 +0900151
Andrzej Hajda9f3dd7d2014-09-09 15:16:06 +0200152 drm_vblank_cleanup(dev);
Andrzej Hajdac52142e2014-10-07 22:09:14 +0900153 component_unbind_all(dev->dev, dev);
Andrzej Hajda9f3dd7d2014-09-09 15:16:06 +0200154 drm_mode_config_cleanup(dev);
155 drm_release_iommu_mapping(dev);
156
157 kfree(dev->dev_private);
Inki Dae1c248b72011-10-04 19:19:01 +0900158 dev->dev_private = NULL;
159
160 return 0;
161}
162
Sean Paulaf65c802014-01-30 16:19:27 -0500163static int exynos_drm_suspend(struct drm_device *dev, pm_message_t state)
164{
165 struct drm_connector *connector;
166
167 drm_modeset_lock_all(dev);
168 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
169 int old_dpms = connector->dpms;
170
171 if (connector->funcs->dpms)
172 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
173
174 /* Set the old mode back to the connector for resume */
175 connector->dpms = old_dpms;
176 }
177 drm_modeset_unlock_all(dev);
178
179 return 0;
180}
181
182static int exynos_drm_resume(struct drm_device *dev)
183{
184 struct drm_connector *connector;
185
186 drm_modeset_lock_all(dev);
187 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Andrzej Hajda74cfe072014-10-10 14:31:56 +0200188 if (connector->funcs->dpms) {
189 int dpms = connector->dpms;
190
191 connector->dpms = DRM_MODE_DPMS_OFF;
192 connector->funcs->dpms(connector, dpms);
193 }
Sean Paulaf65c802014-01-30 16:19:27 -0500194 }
Takashi Iwaia16f2232014-05-09 08:14:15 +0200195 drm_modeset_unlock_all(dev);
Sean Paulaf65c802014-01-30 16:19:27 -0500196
197 drm_helper_resume_force_mode(dev);
Sean Paulaf65c802014-01-30 16:19:27 -0500198
199 return 0;
200}
201
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900202static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
Inki Daeccf4d882011-10-14 13:29:51 +0900203{
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900204 struct drm_exynos_file_private *file_priv;
YoungJun Choba3706c2013-07-01 17:00:47 +0900205 int ret;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900206
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900207 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
208 if (!file_priv)
209 return -ENOMEM;
210
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900211 file->driver_priv = file_priv;
Inki Daeb2df26c2012-04-23 21:01:28 +0900212
YoungJun Choba3706c2013-07-01 17:00:47 +0900213 ret = exynos_drm_subdrv_open(dev, file);
Sachin Kamat6ca605f2014-01-16 11:31:26 +0530214 if (ret)
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800215 goto err_file_priv_free;
YoungJun Choba3706c2013-07-01 17:00:47 +0900216
217 return ret;
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800218
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800219err_file_priv_free:
Sachin Kamat6ca605f2014-01-16 11:31:26 +0530220 kfree(file_priv);
221 file->driver_priv = NULL;
222 return ret;
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900223}
224
Inki Daeccf4d882011-10-14 13:29:51 +0900225static void exynos_drm_preclose(struct drm_device *dev,
226 struct drm_file *file)
227{
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900228 exynos_drm_subdrv_close(dev, file);
Inki Daeccf4d882011-10-14 13:29:51 +0900229}
230
Inki Dae53ef2992012-02-15 11:25:22 +0900231static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
232{
Inki Dae0cbc3302013-10-01 14:51:37 +0900233 struct exynos_drm_private *private = dev->dev_private;
234 struct drm_pending_vblank_event *v, *vt;
235 struct drm_pending_event *e, *et;
236 unsigned long flags;
237
Inki Dae53ef2992012-02-15 11:25:22 +0900238 if (!file->driver_priv)
239 return;
240
Inki Dae0cbc3302013-10-01 14:51:37 +0900241 /* Release all events not unhandled by page flip handler. */
242 spin_lock_irqsave(&dev->event_lock, flags);
243 list_for_each_entry_safe(v, vt, &private->pageflip_event_list,
244 base.link) {
245 if (v->base.file_priv == file) {
246 list_del(&v->base.link);
247 drm_vblank_put(dev, v->pipe);
248 v->base.destroy(&v->base);
249 }
250 }
251
252 /* Release all events handled by page flip handler but not freed. */
253 list_for_each_entry_safe(e, et, &file->event_list, link) {
254 list_del(&e->link);
255 e->destroy(e);
256 }
257 spin_unlock_irqrestore(&dev->event_lock, flags);
258
Inki Dae53ef2992012-02-15 11:25:22 +0900259 kfree(file->driver_priv);
260 file->driver_priv = NULL;
261}
262
Inki Dae1c248b72011-10-04 19:19:01 +0900263static void exynos_drm_lastclose(struct drm_device *dev)
264{
Inki Dae1c248b72011-10-04 19:19:01 +0900265 exynos_drm_fbdev_restore_mode(dev);
266}
267
Laurent Pinchart78b68552012-05-17 13:27:22 +0200268static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
Inki Dae1c248b72011-10-04 19:19:01 +0900269 .fault = exynos_drm_gem_fault,
270 .open = drm_gem_vm_open,
271 .close = drm_gem_vm_close,
272};
273
Rob Clarkbaa70942013-08-02 13:27:49 -0400274static const struct drm_ioctl_desc exynos_ioctls[] = {
Inki Dae1c248b72011-10-04 19:19:01 +0900275 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
276 DRM_UNLOCKED | DRM_AUTH),
Inki Dae40cd7e02012-05-04 15:51:17 +0900277 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET,
278 exynos_drm_gem_get_ioctl, DRM_UNLOCKED),
Inki Daeb73d1232012-03-21 10:55:26 +0900279 DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
280 vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900281 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER,
282 exynos_g2d_get_ver_ioctl, DRM_UNLOCKED | DRM_AUTH),
283 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST,
284 exynos_g2d_set_cmdlist_ioctl, DRM_UNLOCKED | DRM_AUTH),
285 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC,
286 exynos_g2d_exec_ioctl, DRM_UNLOCKED | DRM_AUTH),
Eunchul Kimcb471f142012-12-14 18:10:31 +0900287 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY,
288 exynos_drm_ipp_get_property, DRM_UNLOCKED | DRM_AUTH),
289 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY,
290 exynos_drm_ipp_set_property, DRM_UNLOCKED | DRM_AUTH),
291 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF,
292 exynos_drm_ipp_queue_buf, DRM_UNLOCKED | DRM_AUTH),
293 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL,
294 exynos_drm_ipp_cmd_ctrl, DRM_UNLOCKED | DRM_AUTH),
Inki Dae1c248b72011-10-04 19:19:01 +0900295};
296
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900297static const struct file_operations exynos_drm_driver_fops = {
298 .owner = THIS_MODULE,
299 .open = drm_open,
300 .mmap = exynos_drm_gem_mmap,
301 .poll = drm_poll,
302 .read = drm_read,
303 .unlocked_ioctl = drm_ioctl,
Keith Packard804d74a2012-07-09 15:40:07 -0700304#ifdef CONFIG_COMPAT
305 .compat_ioctl = drm_compat_ioctl,
306#endif
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900307 .release = drm_release,
308};
309
Inki Dae1c248b72011-10-04 19:19:01 +0900310static struct drm_driver exynos_drm_driver = {
Joonyoung Shimbe9b64a2014-04-12 09:39:52 +0900311 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
Inki Dae1c248b72011-10-04 19:19:01 +0900312 .load = exynos_drm_load,
313 .unload = exynos_drm_unload,
Sean Paulaf65c802014-01-30 16:19:27 -0500314 .suspend = exynos_drm_suspend,
315 .resume = exynos_drm_resume,
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900316 .open = exynos_drm_open,
Inki Daeccf4d882011-10-14 13:29:51 +0900317 .preclose = exynos_drm_preclose,
Inki Dae1c248b72011-10-04 19:19:01 +0900318 .lastclose = exynos_drm_lastclose,
Inki Dae53ef2992012-02-15 11:25:22 +0900319 .postclose = exynos_drm_postclose,
David Herrmann915b4d12014-08-29 12:12:43 +0200320 .set_busid = drm_platform_set_busid,
Inki Dae1c248b72011-10-04 19:19:01 +0900321 .get_vblank_counter = drm_vblank_count,
322 .enable_vblank = exynos_drm_crtc_enable_vblank,
323 .disable_vblank = exynos_drm_crtc_disable_vblank,
Inki Dae1c248b72011-10-04 19:19:01 +0900324 .gem_free_object = exynos_drm_gem_free_object,
325 .gem_vm_ops = &exynos_drm_gem_vm_ops,
326 .dumb_create = exynos_drm_gem_dumb_create,
327 .dumb_map_offset = exynos_drm_gem_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200328 .dumb_destroy = drm_gem_dumb_destroy,
Inki Daeb2df26c2012-04-23 21:01:28 +0900329 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
330 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
331 .gem_prime_export = exynos_dmabuf_prime_export,
332 .gem_prime_import = exynos_dmabuf_prime_import,
Inki Dae1c248b72011-10-04 19:19:01 +0900333 .ioctls = exynos_ioctls,
Rob Clarkbaa70942013-08-02 13:27:49 -0400334 .num_ioctls = ARRAY_SIZE(exynos_ioctls),
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900335 .fops = &exynos_drm_driver_fops,
Inki Dae1c248b72011-10-04 19:19:01 +0900336 .name = DRIVER_NAME,
337 .desc = DRIVER_DESC,
338 .date = DRIVER_DATE,
339 .major = DRIVER_MAJOR,
340 .minor = DRIVER_MINOR,
341};
342
Sean Paulaf65c802014-01-30 16:19:27 -0500343#ifdef CONFIG_PM_SLEEP
344static int exynos_drm_sys_suspend(struct device *dev)
345{
346 struct drm_device *drm_dev = dev_get_drvdata(dev);
347 pm_message_t message;
348
Krzysztof Kozlowskid50a1902014-06-30 15:25:44 +0200349 if (pm_runtime_suspended(dev) || !drm_dev)
Sean Paulaf65c802014-01-30 16:19:27 -0500350 return 0;
351
352 message.event = PM_EVENT_SUSPEND;
353 return exynos_drm_suspend(drm_dev, message);
354}
355
356static int exynos_drm_sys_resume(struct device *dev)
357{
358 struct drm_device *drm_dev = dev_get_drvdata(dev);
359
Krzysztof Kozlowskid50a1902014-06-30 15:25:44 +0200360 if (pm_runtime_suspended(dev) || !drm_dev)
Sean Paulaf65c802014-01-30 16:19:27 -0500361 return 0;
362
363 return exynos_drm_resume(drm_dev);
364}
365#endif
366
Sean Paulaf65c802014-01-30 16:19:27 -0500367static const struct dev_pm_ops exynos_drm_pm_ops = {
368 SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_sys_suspend, exynos_drm_sys_resume)
Sean Paulaf65c802014-01-30 16:19:27 -0500369};
370
Inki Daef37cd5e2014-05-09 14:25:20 +0900371int exynos_drm_component_add(struct device *dev,
Inki Daedf5225b2014-05-29 18:28:02 +0900372 enum exynos_drm_device_type dev_type,
373 enum exynos_drm_output_type out_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900374{
375 struct component_dev *cdev;
Inki Daedf5225b2014-05-29 18:28:02 +0900376
377 if (dev_type != EXYNOS_DEVICE_TYPE_CRTC &&
378 dev_type != EXYNOS_DEVICE_TYPE_CONNECTOR) {
379 DRM_ERROR("invalid device type.\n");
380 return -EINVAL;
381 }
382
383 mutex_lock(&drm_component_lock);
384
385 /*
386 * Make sure to check if there is a component which has two device
387 * objects, for connector and for encoder/connector.
388 * It should make sure that crtc and encoder/connector drivers are
389 * ready before exynos drm core binds them.
390 */
391 list_for_each_entry(cdev, &drm_component_list, list) {
392 if (cdev->out_type == out_type) {
393 /*
394 * If crtc and encoder/connector device objects are
395 * added already just return.
396 */
397 if (cdev->dev_type_flag == (EXYNOS_DEVICE_TYPE_CRTC |
398 EXYNOS_DEVICE_TYPE_CONNECTOR)) {
399 mutex_unlock(&drm_component_lock);
400 return 0;
401 }
402
403 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
404 cdev->crtc_dev = dev;
405 cdev->dev_type_flag |= dev_type;
406 }
407
408 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
409 cdev->conn_dev = dev;
410 cdev->dev_type_flag |= dev_type;
411 }
412
413 mutex_unlock(&drm_component_lock);
414 return 0;
415 }
416 }
417
418 mutex_unlock(&drm_component_lock);
Inki Daef37cd5e2014-05-09 14:25:20 +0900419
420 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
421 if (!cdev)
422 return -ENOMEM;
423
Inki Daedf5225b2014-05-29 18:28:02 +0900424 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC)
425 cdev->crtc_dev = dev;
426 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR)
427 cdev->conn_dev = dev;
Inki Daef37cd5e2014-05-09 14:25:20 +0900428
Inki Daedf5225b2014-05-29 18:28:02 +0900429 cdev->out_type = out_type;
430 cdev->dev_type_flag = dev_type;
Inki Daef37cd5e2014-05-09 14:25:20 +0900431
432 mutex_lock(&drm_component_lock);
433 list_add_tail(&cdev->list, &drm_component_list);
434 mutex_unlock(&drm_component_lock);
435
436 return 0;
437}
438
439void exynos_drm_component_del(struct device *dev,
Inki Daedf5225b2014-05-29 18:28:02 +0900440 enum exynos_drm_device_type dev_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900441{
442 struct component_dev *cdev, *next;
443
444 mutex_lock(&drm_component_lock);
445
446 list_for_each_entry_safe(cdev, next, &drm_component_list, list) {
Inki Daedf5225b2014-05-29 18:28:02 +0900447 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
448 if (cdev->crtc_dev == dev) {
449 cdev->crtc_dev = NULL;
450 cdev->dev_type_flag &= ~dev_type;
451 }
452 }
453
454 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
455 if (cdev->conn_dev == dev) {
456 cdev->conn_dev = NULL;
457 cdev->dev_type_flag &= ~dev_type;
458 }
459 }
460
461 /*
462 * Release cdev object only in case that both of crtc and
463 * encoder/connector device objects are NULL.
464 */
465 if (!cdev->crtc_dev && !cdev->conn_dev) {
Inki Daef37cd5e2014-05-09 14:25:20 +0900466 list_del(&cdev->list);
467 kfree(cdev);
Inki Daef37cd5e2014-05-09 14:25:20 +0900468 }
Inki Daedf5225b2014-05-29 18:28:02 +0900469
470 break;
Inki Daef37cd5e2014-05-09 14:25:20 +0900471 }
472
473 mutex_unlock(&drm_component_lock);
Inki Daef37cd5e2014-05-09 14:25:20 +0900474}
475
Inki Dae53c55582014-09-11 17:04:03 +0900476static int compare_dev(struct device *dev, void *data)
Inki Daef37cd5e2014-05-09 14:25:20 +0900477{
478 return dev == (struct device *)data;
479}
480
Inki Dae53c55582014-09-11 17:04:03 +0900481static struct component_match *exynos_drm_match_add(struct device *dev)
Inki Daef37cd5e2014-05-09 14:25:20 +0900482{
Inki Dae53c55582014-09-11 17:04:03 +0900483 struct component_match *match = NULL;
Inki Daef37cd5e2014-05-09 14:25:20 +0900484 struct component_dev *cdev;
Inki Daedf5225b2014-05-29 18:28:02 +0900485 unsigned int attach_cnt = 0;
Inki Daef37cd5e2014-05-09 14:25:20 +0900486
487 mutex_lock(&drm_component_lock);
488
Inki Daefbdf0932014-11-06 23:00:37 +0900489 /* Do not retry to probe if there is no any kms driver regitered. */
490 if (list_empty(&drm_component_list)) {
491 mutex_unlock(&drm_component_lock);
492 return ERR_PTR(-ENODEV);
493 }
494
Inki Daef37cd5e2014-05-09 14:25:20 +0900495 list_for_each_entry(cdev, &drm_component_list, list) {
Inki Daedf5225b2014-05-29 18:28:02 +0900496 /*
497 * Add components to master only in case that crtc and
498 * encoder/connector device objects exist.
499 */
500 if (!cdev->crtc_dev || !cdev->conn_dev)
501 continue;
502
503 attach_cnt++;
504
Inki Daef37cd5e2014-05-09 14:25:20 +0900505 mutex_unlock(&drm_component_lock);
506
Inki Daedf5225b2014-05-29 18:28:02 +0900507 /*
508 * fimd and dpi modules have same device object so add
509 * only crtc device object in this case.
Inki Daedf5225b2014-05-29 18:28:02 +0900510 */
511 if (cdev->crtc_dev == cdev->conn_dev) {
Inki Dae53c55582014-09-11 17:04:03 +0900512 component_match_add(dev, &match, compare_dev,
513 cdev->crtc_dev);
Inki Daedf5225b2014-05-29 18:28:02 +0900514 goto out_lock;
515 }
516
517 /*
518 * Do not chage below call order.
519 * crtc device first should be added to master because
520 * connector/encoder need pipe number of crtc when they
521 * are created.
522 */
Inki Dae53c55582014-09-11 17:04:03 +0900523 component_match_add(dev, &match, compare_dev, cdev->crtc_dev);
524 component_match_add(dev, &match, compare_dev, cdev->conn_dev);
Inki Daedf5225b2014-05-29 18:28:02 +0900525
526out_lock:
Inki Daef37cd5e2014-05-09 14:25:20 +0900527 mutex_lock(&drm_component_lock);
528 }
529
530 mutex_unlock(&drm_component_lock);
531
Inki Dae53c55582014-09-11 17:04:03 +0900532 return attach_cnt ? match : ERR_PTR(-EPROBE_DEFER);
Inki Daef37cd5e2014-05-09 14:25:20 +0900533}
534
535static int exynos_drm_bind(struct device *dev)
536{
Inki Daef37cd5e2014-05-09 14:25:20 +0900537 return drm_platform_init(&exynos_drm_driver, to_platform_device(dev));
538}
539
540static void exynos_drm_unbind(struct device *dev)
541{
542 drm_put_dev(dev_get_drvdata(dev));
543}
544
545static const struct component_master_ops exynos_drm_ops = {
Inki Daef37cd5e2014-05-09 14:25:20 +0900546 .bind = exynos_drm_bind,
547 .unbind = exynos_drm_unbind,
Inki Dae1c248b72011-10-04 19:19:01 +0900548};
549
Andrzej Hajda72390672014-11-13 16:37:57 +0900550static struct platform_driver *const exynos_drm_kms_drivers[] = {
551#ifdef CONFIG_DRM_EXYNOS_FIMD
552 &fimd_driver,
553#endif
554#ifdef CONFIG_DRM_EXYNOS_DP
555 &dp_driver,
556#endif
557#ifdef CONFIG_DRM_EXYNOS_DSI
558 &dsi_driver,
559#endif
560#ifdef CONFIG_DRM_EXYNOS_HDMI
561 &mixer_driver,
562 &hdmi_driver,
563#endif
564};
565
566static struct platform_driver *const exynos_drm_non_kms_drivers[] = {
567#ifdef CONFIG_DRM_EXYNOS_G2D
568 &g2d_driver,
569#endif
570#ifdef CONFIG_DRM_EXYNOS_FIMC
571 &fimc_driver,
572#endif
573#ifdef CONFIG_DRM_EXYNOS_ROTATOR
574 &rotator_driver,
575#endif
576#ifdef CONFIG_DRM_EXYNOS_GSC
577 &gsc_driver,
578#endif
579#ifdef CONFIG_DRM_EXYNOS_IPP
580 &ipp_driver,
581#endif
582};
583
Inki Daef37cd5e2014-05-09 14:25:20 +0900584static int exynos_drm_platform_probe(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900585{
Inki Dae53c55582014-09-11 17:04:03 +0900586 struct component_match *match;
Andrzej Hajda72390672014-11-13 16:37:57 +0900587 int ret, i, j;
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900588
Inki Daef37cd5e2014-05-09 14:25:20 +0900589 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Damien Lespiauf95aeb12014-06-09 14:39:49 +0100590 exynos_drm_driver.num_ioctls = ARRAY_SIZE(exynos_ioctls);
Inki Daef37cd5e2014-05-09 14:25:20 +0900591
Andrzej Hajda72390672014-11-13 16:37:57 +0900592 for (i = 0; i < ARRAY_SIZE(exynos_drm_kms_drivers); ++i) {
593 ret = platform_driver_register(exynos_drm_kms_drivers[i]);
594 if (ret < 0)
595 goto err_unregister_kms_drivers;
596 }
Inki Daeb73d1232012-03-21 10:55:26 +0900597
Inki Daee9fbdcb2014-11-07 21:32:34 +0900598 match = exynos_drm_match_add(&pdev->dev);
599 if (IS_ERR(match)) {
600 ret = PTR_ERR(match);
Andrzej Hajda72390672014-11-13 16:37:57 +0900601 goto err_unregister_kms_drivers;
Inki Daee9fbdcb2014-11-07 21:32:34 +0900602 }
603
604 ret = component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
605 match);
606 if (ret < 0)
Andrzej Hajda72390672014-11-13 16:37:57 +0900607 goto err_unregister_kms_drivers;
Inki Daee9fbdcb2014-11-07 21:32:34 +0900608
Andrzej Hajda72390672014-11-13 16:37:57 +0900609 for (j = 0; j < ARRAY_SIZE(exynos_drm_non_kms_drivers); ++j) {
610 ret = platform_driver_register(exynos_drm_non_kms_drivers[j]);
611 if (ret < 0)
612 goto err_del_component_master;
613 }
Seung-Woo Kim43f41902013-04-23 14:02:53 +0900614
615 ret = exynos_platform_device_ipp_register();
616 if (ret < 0)
Andrzej Hajda72390672014-11-13 16:37:57 +0900617 goto err_unregister_non_kms_drivers;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900618
Inki Dae421ee182014-11-13 16:05:12 +0900619 /* Probe non kms sub drivers and virtual display driver. */
620 ret = exynos_drm_device_subdrv_probe(platform_get_drvdata(pdev));
621 if (ret)
622 goto err_unregister_resources;
623
Inki Dae53c55582014-09-11 17:04:03 +0900624 return ret;
625
Inki Dae421ee182014-11-13 16:05:12 +0900626err_unregister_resources:
Eunchul Kimcb471f142012-12-14 18:10:31 +0900627#ifdef CONFIG_DRM_EXYNOS_IPP
Inki Dae421ee182014-11-13 16:05:12 +0900628 exynos_platform_device_ipp_unregister();
Eunchul Kimcb471f142012-12-14 18:10:31 +0900629#endif
Andrzej Hajda72390672014-11-13 16:37:57 +0900630err_unregister_non_kms_drivers:
631 while (--j >= 0)
632 platform_driver_unregister(exynos_drm_non_kms_drivers[j]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900633
Inki Daee9fbdcb2014-11-07 21:32:34 +0900634err_del_component_master:
Inki Daee9fbdcb2014-11-07 21:32:34 +0900635 component_master_del(&pdev->dev, &exynos_drm_ops);
Inki Daeb73d1232012-03-21 10:55:26 +0900636
Andrzej Hajda72390672014-11-13 16:37:57 +0900637err_unregister_kms_drivers:
638 while (--i >= 0)
639 platform_driver_unregister(exynos_drm_kms_drivers[i]);
Andrzej Hajda25928a32014-03-17 11:27:17 +0100640
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900641 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900642}
643
Inki Daef37cd5e2014-05-09 14:25:20 +0900644static int exynos_drm_platform_remove(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900645{
Andrzej Hajda72390672014-11-13 16:37:57 +0900646 int i;
647
Inki Dae421ee182014-11-13 16:05:12 +0900648 exynos_drm_device_subdrv_remove(platform_get_drvdata(pdev));
649
Eunchul Kimcb471f142012-12-14 18:10:31 +0900650#ifdef CONFIG_DRM_EXYNOS_IPP
Seung-Woo Kim43f41902013-04-23 14:02:53 +0900651 exynos_platform_device_ipp_unregister();
Eunchul Kimcb471f142012-12-14 18:10:31 +0900652#endif
653
Andrzej Hajda72390672014-11-13 16:37:57 +0900654 for (i = ARRAY_SIZE(exynos_drm_non_kms_drivers) - 1; i >= 0; --i)
655 platform_driver_unregister(exynos_drm_non_kms_drivers[i]);
Eunchul Kimf2646382012-12-14 17:58:57 +0900656
Inki Daef37cd5e2014-05-09 14:25:20 +0900657 component_master_del(&pdev->dev, &exynos_drm_ops);
Andrzej Hajda72390672014-11-13 16:37:57 +0900658
659 for (i = ARRAY_SIZE(exynos_drm_kms_drivers) - 1; i >= 0; --i)
660 platform_driver_unregister(exynos_drm_kms_drivers[i]);
661
Inki Daef37cd5e2014-05-09 14:25:20 +0900662 return 0;
663}
664
665static struct platform_driver exynos_drm_platform_driver = {
666 .probe = exynos_drm_platform_probe,
667 .remove = exynos_drm_platform_remove,
668 .driver = {
669 .owner = THIS_MODULE,
670 .name = "exynos-drm",
671 .pm = &exynos_drm_pm_ops,
672 },
673};
674
675static int exynos_drm_init(void)
676{
677 int ret;
678
Inki Dae5cbb37d2014-11-06 19:23:35 +0900679 /*
680 * Register device object only in case of Exynos SoC.
681 *
682 * Below codes resolves temporarily infinite loop issue incurred
683 * by Exynos drm driver when using multi-platform kernel.
684 * So these codes will be replaced with more generic way later.
685 */
686 if (!of_machine_is_compatible("samsung,exynos3") &&
687 !of_machine_is_compatible("samsung,exynos4") &&
688 !of_machine_is_compatible("samsung,exynos5"))
689 return -ENODEV;
690
Inki Daef37cd5e2014-05-09 14:25:20 +0900691 exynos_drm_pdev = platform_device_register_simple("exynos-drm", -1,
692 NULL, 0);
693 if (IS_ERR(exynos_drm_pdev))
694 return PTR_ERR(exynos_drm_pdev);
695
Inki Daef37cd5e2014-05-09 14:25:20 +0900696 ret = exynos_drm_probe_vidi();
697 if (ret < 0)
698 goto err_unregister_pd;
Inki Daef37cd5e2014-05-09 14:25:20 +0900699
700 ret = platform_driver_register(&exynos_drm_platform_driver);
701 if (ret)
702 goto err_remove_vidi;
703
704 return 0;
705
Inki Daef37cd5e2014-05-09 14:25:20 +0900706err_remove_vidi:
Inki Daef37cd5e2014-05-09 14:25:20 +0900707 exynos_drm_remove_vidi();
Sachin Kamat0013fc92014-06-17 17:08:07 +0530708
709err_unregister_pd:
Sachin Kamat0013fc92014-06-17 17:08:07 +0530710 platform_device_unregister(exynos_drm_pdev);
Inki Daef37cd5e2014-05-09 14:25:20 +0900711
712 return ret;
713}
714
715static void exynos_drm_exit(void)
716{
Sachin Kamat0013fc92014-06-17 17:08:07 +0530717 platform_driver_unregister(&exynos_drm_platform_driver);
Andrzej Hajda72390672014-11-13 16:37:57 +0900718
Inki Daef37cd5e2014-05-09 14:25:20 +0900719 exynos_drm_remove_vidi();
Andrzej Hajda72390672014-11-13 16:37:57 +0900720
Inki Daef37cd5e2014-05-09 14:25:20 +0900721 platform_device_unregister(exynos_drm_pdev);
Inki Dae1c248b72011-10-04 19:19:01 +0900722}
723
724module_init(exynos_drm_init);
725module_exit(exynos_drm_exit);
726
727MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
728MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
729MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
730MODULE_DESCRIPTION("Samsung SoC DRM Driver");
731MODULE_LICENSE("GPL");