blob: 94026ad76a775d9cfb826fa65f9e2da74d14660f [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* exynos_drm_core.c
2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author:
5 * Inki Dae <inki.dae@samsung.com>
6 * Joonyoung Shim <jy0922.shim@samsung.com>
7 * Seung-Woo Kim <sw0312.kim@samsung.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 */
28
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
Inki Dae1c248b72011-10-04 19:19:01 +090030#include "exynos_drm_drv.h"
31#include "exynos_drm_encoder.h"
32#include "exynos_drm_connector.h"
33#include "exynos_drm_fbdev.h"
34
Inki Dae1c248b72011-10-04 19:19:01 +090035static LIST_HEAD(exynos_drm_subdrv_list);
Inki Dae1c248b72011-10-04 19:19:01 +090036
Inki Dae41fecf32012-08-17 13:23:25 +090037static int exynos_drm_create_enc_conn(struct drm_device *dev,
Inki Dae1c248b72011-10-04 19:19:01 +090038 struct exynos_drm_subdrv *subdrv)
39{
40 struct drm_encoder *encoder;
41 struct drm_connector *connector;
Inki Dae41fecf32012-08-17 13:23:25 +090042 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +090043
44 DRM_DEBUG_DRIVER("%s\n", __FILE__);
45
Joonyoung Shim677e84c2012-04-05 20:49:27 +090046 subdrv->manager->dev = subdrv->dev;
47
Inki Dae1c248b72011-10-04 19:19:01 +090048 /* create and initialize a encoder for this sub driver. */
Joonyoung Shim677e84c2012-04-05 20:49:27 +090049 encoder = exynos_drm_encoder_create(dev, subdrv->manager,
Inki Dae1c248b72011-10-04 19:19:01 +090050 (1 << MAX_CRTC) - 1);
51 if (!encoder) {
52 DRM_ERROR("failed to create encoder\n");
53 return -EFAULT;
54 }
55
56 /*
57 * create and initialize a connector for this sub driver and
58 * attach the encoder created above to the connector.
59 */
60 connector = exynos_drm_connector_create(dev, encoder);
61 if (!connector) {
62 DRM_ERROR("failed to create connector\n");
Inki Dae41fecf32012-08-17 13:23:25 +090063 ret = -EFAULT;
64 goto err_destroy_encoder;
Inki Dae1c248b72011-10-04 19:19:01 +090065 }
66
67 subdrv->encoder = encoder;
68 subdrv->connector = connector;
69
70 return 0;
Inki Dae41fecf32012-08-17 13:23:25 +090071
72err_destroy_encoder:
73 encoder->funcs->destroy(encoder);
74 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +090075}
76
Inki Dae41fecf32012-08-17 13:23:25 +090077static void exynos_drm_destroy_enc_conn(struct exynos_drm_subdrv *subdrv)
Inki Dae1c248b72011-10-04 19:19:01 +090078{
Inki Dae1c248b72011-10-04 19:19:01 +090079 if (subdrv->encoder) {
80 struct drm_encoder *encoder = subdrv->encoder;
81 encoder->funcs->destroy(encoder);
82 subdrv->encoder = NULL;
83 }
84
85 if (subdrv->connector) {
86 struct drm_connector *connector = subdrv->connector;
87 connector->funcs->destroy(connector);
88 subdrv->connector = NULL;
89 }
90}
91
Inki Dae41fecf32012-08-17 13:23:25 +090092static int exynos_drm_subdrv_probe(struct drm_device *dev,
93 struct exynos_drm_subdrv *subdrv)
94{
95 if (subdrv->probe) {
96 int ret;
97
98 subdrv->drm_dev = dev;
99
100 /*
101 * this probe callback would be called by sub driver
102 * after setting of all resources to this sub driver,
103 * such as clock, irq and register map are done or by load()
104 * of exynos drm driver.
105 *
106 * P.S. note that this driver is considered for modularization.
107 */
108 ret = subdrv->probe(dev, subdrv->dev);
109 if (ret)
110 return ret;
111 }
112
113 return 0;
114}
115
116static void exynos_drm_subdrv_remove(struct drm_device *dev,
117 struct exynos_drm_subdrv *subdrv)
118{
119 DRM_DEBUG_DRIVER("%s\n", __FILE__);
120
121 if (subdrv->remove)
122 subdrv->remove(dev, subdrv->dev);
123}
124
Inki Dae1c248b72011-10-04 19:19:01 +0900125int exynos_drm_device_register(struct drm_device *dev)
126{
127 struct exynos_drm_subdrv *subdrv, *n;
Inki Dae41fecf32012-08-17 13:23:25 +0900128 unsigned int fine_cnt = 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900129 int err;
130
131 DRM_DEBUG_DRIVER("%s\n", __FILE__);
132
133 if (!dev)
134 return -EINVAL;
135
Inki Dae1c248b72011-10-04 19:19:01 +0900136 list_for_each_entry_safe(subdrv, n, &exynos_drm_subdrv_list, list) {
137 err = exynos_drm_subdrv_probe(dev, subdrv);
138 if (err) {
139 DRM_DEBUG("exynos drm subdrv probe failed.\n");
140 list_del(&subdrv->list);
Inki Dae41fecf32012-08-17 13:23:25 +0900141 continue;
Inki Dae1c248b72011-10-04 19:19:01 +0900142 }
Inki Dae41fecf32012-08-17 13:23:25 +0900143
144 /*
145 * if manager is null then it means that this sub driver
146 * doesn't need encoder and connector.
147 */
148 if (!subdrv->manager) {
149 fine_cnt++;
150 continue;
151 }
152
153 err = exynos_drm_create_enc_conn(dev, subdrv);
154 if (err) {
155 DRM_DEBUG("failed to create encoder and connector.\n");
156 exynos_drm_subdrv_remove(dev, subdrv);
157 list_del(&subdrv->list);
158 continue;
159 }
160
161 fine_cnt++;
Inki Dae1c248b72011-10-04 19:19:01 +0900162 }
163
Inki Dae41fecf32012-08-17 13:23:25 +0900164 if (!fine_cnt)
165 return -EINVAL;
166
Inki Dae1c248b72011-10-04 19:19:01 +0900167 return 0;
168}
169EXPORT_SYMBOL_GPL(exynos_drm_device_register);
170
171int exynos_drm_device_unregister(struct drm_device *dev)
172{
173 struct exynos_drm_subdrv *subdrv;
174
175 DRM_DEBUG_DRIVER("%s\n", __FILE__);
176
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900177 if (!dev) {
Inki Dae1c248b72011-10-04 19:19:01 +0900178 WARN(1, "Unexpected drm device unregister!\n");
179 return -EINVAL;
180 }
181
Inki Dae41fecf32012-08-17 13:23:25 +0900182 list_for_each_entry(subdrv, &exynos_drm_subdrv_list, list) {
Inki Dae1c248b72011-10-04 19:19:01 +0900183 exynos_drm_subdrv_remove(dev, subdrv);
Inki Dae41fecf32012-08-17 13:23:25 +0900184 exynos_drm_destroy_enc_conn(subdrv);
185 }
Inki Dae1c248b72011-10-04 19:19:01 +0900186
Inki Dae1c248b72011-10-04 19:19:01 +0900187 return 0;
188}
189EXPORT_SYMBOL_GPL(exynos_drm_device_unregister);
190
Inki Dae1c248b72011-10-04 19:19:01 +0900191int exynos_drm_subdrv_register(struct exynos_drm_subdrv *subdrv)
192{
Inki Dae1c248b72011-10-04 19:19:01 +0900193 DRM_DEBUG_DRIVER("%s\n", __FILE__);
194
195 if (!subdrv)
196 return -EINVAL;
197
Inki Dae1c248b72011-10-04 19:19:01 +0900198 list_add_tail(&subdrv->list, &exynos_drm_subdrv_list);
Inki Dae1c248b72011-10-04 19:19:01 +0900199
200 return 0;
201}
202EXPORT_SYMBOL_GPL(exynos_drm_subdrv_register);
203
204int exynos_drm_subdrv_unregister(struct exynos_drm_subdrv *subdrv)
205{
Inki Dae1c248b72011-10-04 19:19:01 +0900206 DRM_DEBUG_DRIVER("%s\n", __FILE__);
207
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900208 if (!subdrv)
209 return -EINVAL;
Inki Dae1c248b72011-10-04 19:19:01 +0900210
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900211 list_del(&subdrv->list);
Inki Dae1c248b72011-10-04 19:19:01 +0900212
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900213 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900214}
215EXPORT_SYMBOL_GPL(exynos_drm_subdrv_unregister);
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900216
217int exynos_drm_subdrv_open(struct drm_device *dev, struct drm_file *file)
218{
219 struct exynos_drm_subdrv *subdrv;
220 int ret;
221
222 list_for_each_entry(subdrv, &exynos_drm_subdrv_list, list) {
223 if (subdrv->open) {
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900224 ret = subdrv->open(dev, subdrv->dev, file);
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900225 if (ret)
226 goto err;
227 }
228 }
229
230 return 0;
231
232err:
233 list_for_each_entry_reverse(subdrv, &subdrv->list, list) {
234 if (subdrv->close)
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900235 subdrv->close(dev, subdrv->dev, file);
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900236 }
237 return ret;
238}
239EXPORT_SYMBOL_GPL(exynos_drm_subdrv_open);
240
241void exynos_drm_subdrv_close(struct drm_device *dev, struct drm_file *file)
242{
243 struct exynos_drm_subdrv *subdrv;
244
245 list_for_each_entry(subdrv, &exynos_drm_subdrv_list, list) {
246 if (subdrv->close)
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900247 subdrv->close(dev, subdrv->dev, file);
Joonyoung Shim9084f7b2012-03-16 18:47:09 +0900248 }
249}
250EXPORT_SYMBOL_GPL(exynos_drm_subdrv_close);