blob: c5cb8b6c85a960537bdc418a65f923f02df418b5 [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
Gustavo Padovanb6713952014-11-20 21:42:55 -0200111 /* Probe non kms sub drivers and virtual display driver. */
112 ret = exynos_drm_device_subdrv_probe(dev);
113 if (ret)
114 goto err_cleanup_vblank;
115
Joonyoung Shim4a3ffed2014-09-18 17:50:35 +0900116 /*
117 * enable drm irq mode.
118 * - with irq_enabled = true, we can use the vblank feature.
119 *
120 * P.S. note that we wouldn't use drm irq handler but
121 * just specific driver own one instead because
122 * drm framework supports only one irq handler.
123 */
124 dev->irq_enabled = true;
125
126 /*
127 * with vblank_disable_allowed = true, vblank interrupt will be disabled
128 * by drm timer once a current process gives up ownership of
129 * vblank event.(after drm_vblank_put function is called)
130 */
131 dev->vblank_disable_allowed = true;
132
Andrzej Hajda3cb68302014-10-10 14:31:54 +0200133 /* init kms poll for handling hpd */
134 drm_kms_helper_poll_init(dev);
135
136 /* force connectors detection */
137 drm_helper_hpd_irq_event(dev);
138
Inki Dae1c248b72011-10-04 19:19:01 +0900139 return 0;
140
Gustavo Padovanb6713952014-11-20 21:42:55 -0200141err_cleanup_vblank:
142 drm_vblank_cleanup(dev);
Andrzej Hajdac52142e2014-10-07 22:09:14 +0900143err_unbind_all:
144 component_unbind_all(dev->dev, dev);
Sean Paul080be03d2014-02-19 21:02:55 +0900145err_mode_config_cleanup:
146 drm_mode_config_cleanup(dev);
Inki Dae0519f9a2012-10-20 07:53:42 -0700147 drm_release_iommu_mapping(dev);
Inki Daed2ba65f2014-02-28 18:37:02 +0900148err_free_private:
Inki Dae1c248b72011-10-04 19:19:01 +0900149 kfree(private);
150
151 return ret;
152}
153
154static int exynos_drm_unload(struct drm_device *dev)
155{
Gustavo Padovanb6713952014-11-20 21:42:55 -0200156 exynos_drm_device_subdrv_remove(dev);
157
Inki Dae1c248b72011-10-04 19:19:01 +0900158 exynos_drm_fbdev_fini(dev);
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900159 drm_kms_helper_poll_fini(dev);
Inki Dae1c248b72011-10-04 19:19:01 +0900160
Andrzej Hajda9f3dd7d2014-09-09 15:16:06 +0200161 drm_vblank_cleanup(dev);
Andrzej Hajdac52142e2014-10-07 22:09:14 +0900162 component_unbind_all(dev->dev, dev);
Andrzej Hajda9f3dd7d2014-09-09 15:16:06 +0200163 drm_mode_config_cleanup(dev);
164 drm_release_iommu_mapping(dev);
165
166 kfree(dev->dev_private);
Inki Dae1c248b72011-10-04 19:19:01 +0900167 dev->dev_private = NULL;
168
169 return 0;
170}
171
Sean Paulaf65c802014-01-30 16:19:27 -0500172static int exynos_drm_suspend(struct drm_device *dev, pm_message_t state)
173{
174 struct drm_connector *connector;
175
176 drm_modeset_lock_all(dev);
177 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
178 int old_dpms = connector->dpms;
179
180 if (connector->funcs->dpms)
181 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
182
183 /* Set the old mode back to the connector for resume */
184 connector->dpms = old_dpms;
185 }
186 drm_modeset_unlock_all(dev);
187
188 return 0;
189}
190
191static int exynos_drm_resume(struct drm_device *dev)
192{
193 struct drm_connector *connector;
194
195 drm_modeset_lock_all(dev);
196 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Andrzej Hajda74cfe072014-10-10 14:31:56 +0200197 if (connector->funcs->dpms) {
198 int dpms = connector->dpms;
199
200 connector->dpms = DRM_MODE_DPMS_OFF;
201 connector->funcs->dpms(connector, dpms);
202 }
Sean Paulaf65c802014-01-30 16:19:27 -0500203 }
Takashi Iwaia16f2232014-05-09 08:14:15 +0200204 drm_modeset_unlock_all(dev);
Sean Paulaf65c802014-01-30 16:19:27 -0500205
Sean Paulaf65c802014-01-30 16:19:27 -0500206 return 0;
207}
208
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900209static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
Inki Daeccf4d882011-10-14 13:29:51 +0900210{
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900211 struct drm_exynos_file_private *file_priv;
YoungJun Choba3706c2013-07-01 17:00:47 +0900212 int ret;
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900213
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900214 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
215 if (!file_priv)
216 return -ENOMEM;
217
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900218 file->driver_priv = file_priv;
Inki Daeb2df26c2012-04-23 21:01:28 +0900219
YoungJun Choba3706c2013-07-01 17:00:47 +0900220 ret = exynos_drm_subdrv_open(dev, file);
Sachin Kamat6ca605f2014-01-16 11:31:26 +0530221 if (ret)
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800222 goto err_file_priv_free;
YoungJun Choba3706c2013-07-01 17:00:47 +0900223
224 return ret;
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800225
Daniel Kurtz307ceaf2014-03-17 11:28:06 +0800226err_file_priv_free:
Sachin Kamat6ca605f2014-01-16 11:31:26 +0530227 kfree(file_priv);
228 file->driver_priv = NULL;
229 return ret;
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900230}
231
Inki Daeccf4d882011-10-14 13:29:51 +0900232static void exynos_drm_preclose(struct drm_device *dev,
233 struct drm_file *file)
234{
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900235 exynos_drm_subdrv_close(dev, file);
Inki Daeccf4d882011-10-14 13:29:51 +0900236}
237
Inki Dae53ef2992012-02-15 11:25:22 +0900238static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
239{
Inki Dae0cbc3302013-10-01 14:51:37 +0900240 struct exynos_drm_private *private = dev->dev_private;
241 struct drm_pending_vblank_event *v, *vt;
242 struct drm_pending_event *e, *et;
243 unsigned long flags;
244
Inki Dae53ef2992012-02-15 11:25:22 +0900245 if (!file->driver_priv)
246 return;
247
Inki Dae0cbc3302013-10-01 14:51:37 +0900248 /* Release all events not unhandled by page flip handler. */
249 spin_lock_irqsave(&dev->event_lock, flags);
250 list_for_each_entry_safe(v, vt, &private->pageflip_event_list,
251 base.link) {
252 if (v->base.file_priv == file) {
253 list_del(&v->base.link);
254 drm_vblank_put(dev, v->pipe);
255 v->base.destroy(&v->base);
256 }
257 }
258
259 /* Release all events handled by page flip handler but not freed. */
260 list_for_each_entry_safe(e, et, &file->event_list, link) {
261 list_del(&e->link);
262 e->destroy(e);
263 }
264 spin_unlock_irqrestore(&dev->event_lock, flags);
265
Inki Dae53ef2992012-02-15 11:25:22 +0900266 kfree(file->driver_priv);
267 file->driver_priv = NULL;
268}
269
Inki Dae1c248b72011-10-04 19:19:01 +0900270static void exynos_drm_lastclose(struct drm_device *dev)
271{
Inki Dae1c248b72011-10-04 19:19:01 +0900272 exynos_drm_fbdev_restore_mode(dev);
273}
274
Laurent Pinchart78b68552012-05-17 13:27:22 +0200275static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
Inki Dae1c248b72011-10-04 19:19:01 +0900276 .fault = exynos_drm_gem_fault,
277 .open = drm_gem_vm_open,
278 .close = drm_gem_vm_close,
279};
280
Rob Clarkbaa70942013-08-02 13:27:49 -0400281static const struct drm_ioctl_desc exynos_ioctls[] = {
Inki Dae1c248b72011-10-04 19:19:01 +0900282 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
283 DRM_UNLOCKED | DRM_AUTH),
Inki Dae40cd7e02012-05-04 15:51:17 +0900284 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET,
285 exynos_drm_gem_get_ioctl, DRM_UNLOCKED),
Inki Daeb73d1232012-03-21 10:55:26 +0900286 DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
287 vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
Joonyoung Shimd7f16422012-05-17 20:06:32 +0900288 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER,
289 exynos_g2d_get_ver_ioctl, DRM_UNLOCKED | DRM_AUTH),
290 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST,
291 exynos_g2d_set_cmdlist_ioctl, DRM_UNLOCKED | DRM_AUTH),
292 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC,
293 exynos_g2d_exec_ioctl, DRM_UNLOCKED | DRM_AUTH),
Eunchul Kimcb471f142012-12-14 18:10:31 +0900294 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY,
295 exynos_drm_ipp_get_property, DRM_UNLOCKED | DRM_AUTH),
296 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY,
297 exynos_drm_ipp_set_property, DRM_UNLOCKED | DRM_AUTH),
298 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF,
299 exynos_drm_ipp_queue_buf, DRM_UNLOCKED | DRM_AUTH),
300 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL,
301 exynos_drm_ipp_cmd_ctrl, DRM_UNLOCKED | DRM_AUTH),
Inki Dae1c248b72011-10-04 19:19:01 +0900302};
303
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900304static const struct file_operations exynos_drm_driver_fops = {
305 .owner = THIS_MODULE,
306 .open = drm_open,
307 .mmap = exynos_drm_gem_mmap,
308 .poll = drm_poll,
309 .read = drm_read,
310 .unlocked_ioctl = drm_ioctl,
Keith Packard804d74a2012-07-09 15:40:07 -0700311#ifdef CONFIG_COMPAT
312 .compat_ioctl = drm_compat_ioctl,
313#endif
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900314 .release = drm_release,
315};
316
Inki Dae1c248b72011-10-04 19:19:01 +0900317static struct drm_driver exynos_drm_driver = {
Joonyoung Shimbe9b64a2014-04-12 09:39:52 +0900318 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
Inki Dae1c248b72011-10-04 19:19:01 +0900319 .load = exynos_drm_load,
320 .unload = exynos_drm_unload,
Sean Paulaf65c802014-01-30 16:19:27 -0500321 .suspend = exynos_drm_suspend,
322 .resume = exynos_drm_resume,
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900323 .open = exynos_drm_open,
Inki Daeccf4d882011-10-14 13:29:51 +0900324 .preclose = exynos_drm_preclose,
Inki Dae1c248b72011-10-04 19:19:01 +0900325 .lastclose = exynos_drm_lastclose,
Inki Dae53ef2992012-02-15 11:25:22 +0900326 .postclose = exynos_drm_postclose,
David Herrmann915b4d12014-08-29 12:12:43 +0200327 .set_busid = drm_platform_set_busid,
Inki Dae1c248b72011-10-04 19:19:01 +0900328 .get_vblank_counter = drm_vblank_count,
329 .enable_vblank = exynos_drm_crtc_enable_vblank,
330 .disable_vblank = exynos_drm_crtc_disable_vblank,
Inki Dae1c248b72011-10-04 19:19:01 +0900331 .gem_free_object = exynos_drm_gem_free_object,
332 .gem_vm_ops = &exynos_drm_gem_vm_ops,
333 .dumb_create = exynos_drm_gem_dumb_create,
334 .dumb_map_offset = exynos_drm_gem_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200335 .dumb_destroy = drm_gem_dumb_destroy,
Inki Daeb2df26c2012-04-23 21:01:28 +0900336 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
337 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
338 .gem_prime_export = exynos_dmabuf_prime_export,
339 .gem_prime_import = exynos_dmabuf_prime_import,
Inki Dae1c248b72011-10-04 19:19:01 +0900340 .ioctls = exynos_ioctls,
Rob Clarkbaa70942013-08-02 13:27:49 -0400341 .num_ioctls = ARRAY_SIZE(exynos_ioctls),
Joonyoung Shimac2bdf72011-12-08 15:00:20 +0900342 .fops = &exynos_drm_driver_fops,
Inki Dae1c248b72011-10-04 19:19:01 +0900343 .name = DRIVER_NAME,
344 .desc = DRIVER_DESC,
345 .date = DRIVER_DATE,
346 .major = DRIVER_MAJOR,
347 .minor = DRIVER_MINOR,
348};
349
Sean Paulaf65c802014-01-30 16:19:27 -0500350#ifdef CONFIG_PM_SLEEP
351static int exynos_drm_sys_suspend(struct device *dev)
352{
353 struct drm_device *drm_dev = dev_get_drvdata(dev);
354 pm_message_t message;
355
Krzysztof Kozlowskid50a1902014-06-30 15:25:44 +0200356 if (pm_runtime_suspended(dev) || !drm_dev)
Sean Paulaf65c802014-01-30 16:19:27 -0500357 return 0;
358
359 message.event = PM_EVENT_SUSPEND;
360 return exynos_drm_suspend(drm_dev, message);
361}
362
363static int exynos_drm_sys_resume(struct device *dev)
364{
365 struct drm_device *drm_dev = dev_get_drvdata(dev);
366
Krzysztof Kozlowskid50a1902014-06-30 15:25:44 +0200367 if (pm_runtime_suspended(dev) || !drm_dev)
Sean Paulaf65c802014-01-30 16:19:27 -0500368 return 0;
369
370 return exynos_drm_resume(drm_dev);
371}
372#endif
373
Sean Paulaf65c802014-01-30 16:19:27 -0500374static const struct dev_pm_ops exynos_drm_pm_ops = {
375 SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_sys_suspend, exynos_drm_sys_resume)
Sean Paulaf65c802014-01-30 16:19:27 -0500376};
377
Inki Daef37cd5e2014-05-09 14:25:20 +0900378int exynos_drm_component_add(struct device *dev,
Inki Daedf5225b2014-05-29 18:28:02 +0900379 enum exynos_drm_device_type dev_type,
380 enum exynos_drm_output_type out_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900381{
382 struct component_dev *cdev;
Inki Daedf5225b2014-05-29 18:28:02 +0900383
384 if (dev_type != EXYNOS_DEVICE_TYPE_CRTC &&
385 dev_type != EXYNOS_DEVICE_TYPE_CONNECTOR) {
386 DRM_ERROR("invalid device type.\n");
387 return -EINVAL;
388 }
389
390 mutex_lock(&drm_component_lock);
391
392 /*
393 * Make sure to check if there is a component which has two device
394 * objects, for connector and for encoder/connector.
395 * It should make sure that crtc and encoder/connector drivers are
396 * ready before exynos drm core binds them.
397 */
398 list_for_each_entry(cdev, &drm_component_list, list) {
399 if (cdev->out_type == out_type) {
400 /*
401 * If crtc and encoder/connector device objects are
402 * added already just return.
403 */
404 if (cdev->dev_type_flag == (EXYNOS_DEVICE_TYPE_CRTC |
405 EXYNOS_DEVICE_TYPE_CONNECTOR)) {
406 mutex_unlock(&drm_component_lock);
407 return 0;
408 }
409
410 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
411 cdev->crtc_dev = dev;
412 cdev->dev_type_flag |= dev_type;
413 }
414
415 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
416 cdev->conn_dev = dev;
417 cdev->dev_type_flag |= dev_type;
418 }
419
420 mutex_unlock(&drm_component_lock);
421 return 0;
422 }
423 }
424
425 mutex_unlock(&drm_component_lock);
Inki Daef37cd5e2014-05-09 14:25:20 +0900426
427 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
428 if (!cdev)
429 return -ENOMEM;
430
Inki Daedf5225b2014-05-29 18:28:02 +0900431 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC)
432 cdev->crtc_dev = dev;
433 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR)
434 cdev->conn_dev = dev;
Inki Daef37cd5e2014-05-09 14:25:20 +0900435
Inki Daedf5225b2014-05-29 18:28:02 +0900436 cdev->out_type = out_type;
437 cdev->dev_type_flag = dev_type;
Inki Daef37cd5e2014-05-09 14:25:20 +0900438
439 mutex_lock(&drm_component_lock);
440 list_add_tail(&cdev->list, &drm_component_list);
441 mutex_unlock(&drm_component_lock);
442
443 return 0;
444}
445
446void exynos_drm_component_del(struct device *dev,
Inki Daedf5225b2014-05-29 18:28:02 +0900447 enum exynos_drm_device_type dev_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900448{
449 struct component_dev *cdev, *next;
450
451 mutex_lock(&drm_component_lock);
452
453 list_for_each_entry_safe(cdev, next, &drm_component_list, list) {
Inki Daedf5225b2014-05-29 18:28:02 +0900454 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
455 if (cdev->crtc_dev == dev) {
456 cdev->crtc_dev = NULL;
457 cdev->dev_type_flag &= ~dev_type;
458 }
459 }
460
461 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
462 if (cdev->conn_dev == dev) {
463 cdev->conn_dev = NULL;
464 cdev->dev_type_flag &= ~dev_type;
465 }
466 }
467
468 /*
469 * Release cdev object only in case that both of crtc and
470 * encoder/connector device objects are NULL.
471 */
472 if (!cdev->crtc_dev && !cdev->conn_dev) {
Inki Daef37cd5e2014-05-09 14:25:20 +0900473 list_del(&cdev->list);
474 kfree(cdev);
Inki Daef37cd5e2014-05-09 14:25:20 +0900475 }
Inki Daedf5225b2014-05-29 18:28:02 +0900476
477 break;
Inki Daef37cd5e2014-05-09 14:25:20 +0900478 }
479
480 mutex_unlock(&drm_component_lock);
Inki Daef37cd5e2014-05-09 14:25:20 +0900481}
482
Inki Dae53c55582014-09-11 17:04:03 +0900483static int compare_dev(struct device *dev, void *data)
Inki Daef37cd5e2014-05-09 14:25:20 +0900484{
485 return dev == (struct device *)data;
486}
487
Inki Dae53c55582014-09-11 17:04:03 +0900488static struct component_match *exynos_drm_match_add(struct device *dev)
Inki Daef37cd5e2014-05-09 14:25:20 +0900489{
Inki Dae53c55582014-09-11 17:04:03 +0900490 struct component_match *match = NULL;
Inki Daef37cd5e2014-05-09 14:25:20 +0900491 struct component_dev *cdev;
Inki Daedf5225b2014-05-29 18:28:02 +0900492 unsigned int attach_cnt = 0;
Inki Daef37cd5e2014-05-09 14:25:20 +0900493
494 mutex_lock(&drm_component_lock);
495
Inki Daefbdf0932014-11-06 23:00:37 +0900496 /* Do not retry to probe if there is no any kms driver regitered. */
497 if (list_empty(&drm_component_list)) {
498 mutex_unlock(&drm_component_lock);
499 return ERR_PTR(-ENODEV);
500 }
501
Inki Daef37cd5e2014-05-09 14:25:20 +0900502 list_for_each_entry(cdev, &drm_component_list, list) {
Inki Daedf5225b2014-05-29 18:28:02 +0900503 /*
504 * Add components to master only in case that crtc and
505 * encoder/connector device objects exist.
506 */
507 if (!cdev->crtc_dev || !cdev->conn_dev)
508 continue;
509
510 attach_cnt++;
511
Inki Daef37cd5e2014-05-09 14:25:20 +0900512 mutex_unlock(&drm_component_lock);
513
Inki Daedf5225b2014-05-29 18:28:02 +0900514 /*
515 * fimd and dpi modules have same device object so add
516 * only crtc device object in this case.
Inki Daedf5225b2014-05-29 18:28:02 +0900517 */
518 if (cdev->crtc_dev == cdev->conn_dev) {
Inki Dae53c55582014-09-11 17:04:03 +0900519 component_match_add(dev, &match, compare_dev,
520 cdev->crtc_dev);
Inki Daedf5225b2014-05-29 18:28:02 +0900521 goto out_lock;
522 }
523
524 /*
525 * Do not chage below call order.
526 * crtc device first should be added to master because
527 * connector/encoder need pipe number of crtc when they
528 * are created.
529 */
Inki Dae53c55582014-09-11 17:04:03 +0900530 component_match_add(dev, &match, compare_dev, cdev->crtc_dev);
531 component_match_add(dev, &match, compare_dev, cdev->conn_dev);
Inki Daedf5225b2014-05-29 18:28:02 +0900532
533out_lock:
Inki Daef37cd5e2014-05-09 14:25:20 +0900534 mutex_lock(&drm_component_lock);
535 }
536
537 mutex_unlock(&drm_component_lock);
538
Inki Dae53c55582014-09-11 17:04:03 +0900539 return attach_cnt ? match : ERR_PTR(-EPROBE_DEFER);
Inki Daef37cd5e2014-05-09 14:25:20 +0900540}
541
542static int exynos_drm_bind(struct device *dev)
543{
Inki Daef37cd5e2014-05-09 14:25:20 +0900544 return drm_platform_init(&exynos_drm_driver, to_platform_device(dev));
545}
546
547static void exynos_drm_unbind(struct device *dev)
548{
549 drm_put_dev(dev_get_drvdata(dev));
550}
551
552static const struct component_master_ops exynos_drm_ops = {
Inki Daef37cd5e2014-05-09 14:25:20 +0900553 .bind = exynos_drm_bind,
554 .unbind = exynos_drm_unbind,
Inki Dae1c248b72011-10-04 19:19:01 +0900555};
556
Andrzej Hajda72390672014-11-13 16:37:57 +0900557static struct platform_driver *const exynos_drm_kms_drivers[] = {
558#ifdef CONFIG_DRM_EXYNOS_FIMD
559 &fimd_driver,
560#endif
561#ifdef CONFIG_DRM_EXYNOS_DP
562 &dp_driver,
563#endif
564#ifdef CONFIG_DRM_EXYNOS_DSI
565 &dsi_driver,
566#endif
567#ifdef CONFIG_DRM_EXYNOS_HDMI
568 &mixer_driver,
569 &hdmi_driver,
570#endif
571};
572
573static struct platform_driver *const exynos_drm_non_kms_drivers[] = {
574#ifdef CONFIG_DRM_EXYNOS_G2D
575 &g2d_driver,
576#endif
577#ifdef CONFIG_DRM_EXYNOS_FIMC
578 &fimc_driver,
579#endif
580#ifdef CONFIG_DRM_EXYNOS_ROTATOR
581 &rotator_driver,
582#endif
583#ifdef CONFIG_DRM_EXYNOS_GSC
584 &gsc_driver,
585#endif
586#ifdef CONFIG_DRM_EXYNOS_IPP
587 &ipp_driver,
588#endif
589};
590
Inki Daef37cd5e2014-05-09 14:25:20 +0900591static int exynos_drm_platform_probe(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900592{
Inki Dae53c55582014-09-11 17:04:03 +0900593 struct component_match *match;
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900594
Inki Daef37cd5e2014-05-09 14:25:20 +0900595 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Damien Lespiauf95aeb12014-06-09 14:39:49 +0100596 exynos_drm_driver.num_ioctls = ARRAY_SIZE(exynos_ioctls);
Inki Daef37cd5e2014-05-09 14:25:20 +0900597
Inki Daee9fbdcb2014-11-07 21:32:34 +0900598 match = exynos_drm_match_add(&pdev->dev);
599 if (IS_ERR(match)) {
Gustavo Padovan820687be2014-11-24 16:37:26 +0900600 return PTR_ERR(match);
Inki Daee9fbdcb2014-11-07 21:32:34 +0900601 }
602
Gustavo Padovan820687be2014-11-24 16:37:26 +0900603 return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
604 match);
Inki Dae1c248b72011-10-04 19:19:01 +0900605}
606
Inki Daef37cd5e2014-05-09 14:25:20 +0900607static int exynos_drm_platform_remove(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900608{
Inki Daef37cd5e2014-05-09 14:25:20 +0900609 component_master_del(&pdev->dev, &exynos_drm_ops);
610 return 0;
611}
612
613static struct platform_driver exynos_drm_platform_driver = {
614 .probe = exynos_drm_platform_probe,
615 .remove = exynos_drm_platform_remove,
616 .driver = {
617 .owner = THIS_MODULE,
618 .name = "exynos-drm",
619 .pm = &exynos_drm_pm_ops,
620 },
621};
622
623static int exynos_drm_init(void)
624{
Gustavo Padovan820687be2014-11-24 16:37:26 +0900625 int ret, i, j;
Inki Daef37cd5e2014-05-09 14:25:20 +0900626
Inki Dae5cbb37d2014-11-06 19:23:35 +0900627 /*
628 * Register device object only in case of Exynos SoC.
629 *
630 * Below codes resolves temporarily infinite loop issue incurred
631 * by Exynos drm driver when using multi-platform kernel.
632 * So these codes will be replaced with more generic way later.
633 */
634 if (!of_machine_is_compatible("samsung,exynos3") &&
635 !of_machine_is_compatible("samsung,exynos4") &&
636 !of_machine_is_compatible("samsung,exynos5"))
637 return -ENODEV;
638
Inki Daef37cd5e2014-05-09 14:25:20 +0900639 exynos_drm_pdev = platform_device_register_simple("exynos-drm", -1,
640 NULL, 0);
641 if (IS_ERR(exynos_drm_pdev))
642 return PTR_ERR(exynos_drm_pdev);
643
Inki Daef37cd5e2014-05-09 14:25:20 +0900644 ret = exynos_drm_probe_vidi();
645 if (ret < 0)
646 goto err_unregister_pd;
Inki Daef37cd5e2014-05-09 14:25:20 +0900647
Gustavo Padovan820687be2014-11-24 16:37:26 +0900648 for (i = 0; i < ARRAY_SIZE(exynos_drm_kms_drivers); ++i) {
649 ret = platform_driver_register(exynos_drm_kms_drivers[i]);
650 if (ret < 0)
651 goto err_unregister_kms_drivers;
652 }
653
654 for (j = 0; j < ARRAY_SIZE(exynos_drm_non_kms_drivers); ++j) {
655 ret = platform_driver_register(exynos_drm_non_kms_drivers[j]);
656 if (ret < 0)
657 goto err_unregister_non_kms_drivers;
658 }
659
660#ifdef CONFIG_DRM_EXYNOS_IPP
661 ret = exynos_platform_device_ipp_register();
662 if (ret < 0)
663 goto err_unregister_non_kms_drivers;
664#endif
665
Inki Daef37cd5e2014-05-09 14:25:20 +0900666 ret = platform_driver_register(&exynos_drm_platform_driver);
667 if (ret)
Gustavo Padovan820687be2014-11-24 16:37:26 +0900668 goto err_unregister_resources;
Inki Daef37cd5e2014-05-09 14:25:20 +0900669
670 return 0;
671
Gustavo Padovan820687be2014-11-24 16:37:26 +0900672err_unregister_resources:
673#ifdef CONFIG_DRM_EXYNOS_IPP
674 exynos_platform_device_ipp_unregister();
675#endif
676
677err_unregister_non_kms_drivers:
678 while (--j >= 0)
679 platform_driver_unregister(exynos_drm_non_kms_drivers[j]);
680
681err_unregister_kms_drivers:
682 while (--i >= 0)
683 platform_driver_unregister(exynos_drm_kms_drivers[i]);
684
Inki Daef37cd5e2014-05-09 14:25:20 +0900685 exynos_drm_remove_vidi();
Sachin Kamat0013fc92014-06-17 17:08:07 +0530686
687err_unregister_pd:
Sachin Kamat0013fc92014-06-17 17:08:07 +0530688 platform_device_unregister(exynos_drm_pdev);
Inki Daef37cd5e2014-05-09 14:25:20 +0900689
690 return ret;
691}
692
693static void exynos_drm_exit(void)
694{
Gustavo Padovan820687be2014-11-24 16:37:26 +0900695 int i;
696
697#ifdef CONFIG_DRM_EXYNOS_IPP
698 exynos_platform_device_ipp_unregister();
699#endif
700
701 for (i = ARRAY_SIZE(exynos_drm_non_kms_drivers) - 1; i >= 0; --i)
702 platform_driver_unregister(exynos_drm_non_kms_drivers[i]);
703
704 for (i = ARRAY_SIZE(exynos_drm_kms_drivers) - 1; i >= 0; --i)
705 platform_driver_unregister(exynos_drm_kms_drivers[i]);
706
Sachin Kamat0013fc92014-06-17 17:08:07 +0530707 platform_driver_unregister(&exynos_drm_platform_driver);
Andrzej Hajda72390672014-11-13 16:37:57 +0900708
Inki Daef37cd5e2014-05-09 14:25:20 +0900709 exynos_drm_remove_vidi();
Andrzej Hajda72390672014-11-13 16:37:57 +0900710
Inki Daef37cd5e2014-05-09 14:25:20 +0900711 platform_device_unregister(exynos_drm_pdev);
Inki Dae1c248b72011-10-04 19:19:01 +0900712}
713
714module_init(exynos_drm_init);
715module_exit(exynos_drm_exit);
716
717MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
718MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
719MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
720MODULE_DESCRIPTION("Samsung SoC DRM Driver");
721MODULE_LICENSE("GPL");