drm/exynos: Fix potential NULL pointer dereference
[deliverable/linux.git] / drivers / gpu / drm / exynos / exynos_drm_hdmi.c
1 /*
2 * Copyright (C) 2011 Samsung Electronics Co.Ltd
3 * Authors:
4 * Inki Dae <inki.dae@samsung.com>
5 * Seung-Woo Kim <sw0312.kim@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14 #include "drmP.h"
15
16 #include <linux/kernel.h>
17 #include <linux/wait.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21
22 #include <drm/exynos_drm.h>
23
24 #include "exynos_drm_drv.h"
25 #include "exynos_drm_hdmi.h"
26
27 #define to_context(dev) platform_get_drvdata(to_platform_device(dev))
28 #define to_subdrv(dev) to_context(dev)
29 #define get_ctx_from_subdrv(subdrv) container_of(subdrv,\
30 struct drm_hdmi_context, subdrv);
31
32 /* these callback points shoud be set by specific drivers. */
33 static struct exynos_hdmi_ops *hdmi_ops;
34 static struct exynos_mixer_ops *mixer_ops;
35
36 struct drm_hdmi_context {
37 struct exynos_drm_subdrv subdrv;
38 struct exynos_drm_hdmi_context *hdmi_ctx;
39 struct exynos_drm_hdmi_context *mixer_ctx;
40
41 bool enabled[MIXER_WIN_NR];
42 };
43
44 void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
45 {
46 DRM_DEBUG_KMS("%s\n", __FILE__);
47
48 if (ops)
49 hdmi_ops = ops;
50 }
51
52 void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
53 {
54 DRM_DEBUG_KMS("%s\n", __FILE__);
55
56 if (ops)
57 mixer_ops = ops;
58 }
59
60 static bool drm_hdmi_is_connected(struct device *dev)
61 {
62 struct drm_hdmi_context *ctx = to_context(dev);
63
64 DRM_DEBUG_KMS("%s\n", __FILE__);
65
66 if (hdmi_ops && hdmi_ops->is_connected)
67 return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
68
69 return false;
70 }
71
72 static int drm_hdmi_get_edid(struct device *dev,
73 struct drm_connector *connector, u8 *edid, int len)
74 {
75 struct drm_hdmi_context *ctx = to_context(dev);
76
77 DRM_DEBUG_KMS("%s\n", __FILE__);
78
79 if (hdmi_ops && hdmi_ops->get_edid)
80 return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector, edid,
81 len);
82
83 return 0;
84 }
85
86 static int drm_hdmi_check_timing(struct device *dev, void *timing)
87 {
88 struct drm_hdmi_context *ctx = to_context(dev);
89
90 DRM_DEBUG_KMS("%s\n", __FILE__);
91
92 if (hdmi_ops && hdmi_ops->check_timing)
93 return hdmi_ops->check_timing(ctx->hdmi_ctx->ctx, timing);
94
95 return 0;
96 }
97
98 static int drm_hdmi_power_on(struct device *dev, int mode)
99 {
100 struct drm_hdmi_context *ctx = to_context(dev);
101
102 DRM_DEBUG_KMS("%s\n", __FILE__);
103
104 if (hdmi_ops && hdmi_ops->power_on)
105 return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
106
107 return 0;
108 }
109
110 static struct exynos_drm_display_ops drm_hdmi_display_ops = {
111 .type = EXYNOS_DISPLAY_TYPE_HDMI,
112 .is_connected = drm_hdmi_is_connected,
113 .get_edid = drm_hdmi_get_edid,
114 .check_timing = drm_hdmi_check_timing,
115 .power_on = drm_hdmi_power_on,
116 };
117
118 static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
119 {
120 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
121 struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
122 struct exynos_drm_manager *manager = subdrv->manager;
123
124 DRM_DEBUG_KMS("%s\n", __FILE__);
125
126 if (mixer_ops && mixer_ops->enable_vblank)
127 return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
128 manager->pipe);
129
130 return 0;
131 }
132
133 static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
134 {
135 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
136
137 DRM_DEBUG_KMS("%s\n", __FILE__);
138
139 if (mixer_ops && mixer_ops->disable_vblank)
140 return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
141 }
142
143 static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
144 struct drm_connector *connector,
145 const struct drm_display_mode *mode,
146 struct drm_display_mode *adjusted_mode)
147 {
148 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
149
150 DRM_DEBUG_KMS("%s\n", __FILE__);
151
152 if (hdmi_ops && hdmi_ops->mode_fixup)
153 hdmi_ops->mode_fixup(ctx->hdmi_ctx->ctx, connector, mode,
154 adjusted_mode);
155 }
156
157 static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
158 {
159 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
160
161 DRM_DEBUG_KMS("%s\n", __FILE__);
162
163 if (hdmi_ops && hdmi_ops->mode_set)
164 hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
165 }
166
167 static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
168 unsigned int *width, unsigned int *height)
169 {
170 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
171
172 DRM_DEBUG_KMS("%s\n", __FILE__);
173
174 if (hdmi_ops && hdmi_ops->get_max_resol)
175 hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
176 }
177
178 static void drm_hdmi_commit(struct device *subdrv_dev)
179 {
180 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
181
182 DRM_DEBUG_KMS("%s\n", __FILE__);
183
184 if (hdmi_ops && hdmi_ops->commit)
185 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
186 }
187
188 static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
189 {
190 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
191
192 DRM_DEBUG_KMS("%s\n", __FILE__);
193
194 if (mixer_ops && mixer_ops->dpms)
195 mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
196
197 if (hdmi_ops && hdmi_ops->dpms)
198 hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
199 }
200
201 static void drm_hdmi_apply(struct device *subdrv_dev)
202 {
203 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
204 int i;
205
206 DRM_DEBUG_KMS("%s\n", __FILE__);
207
208 for (i = 0; i < MIXER_WIN_NR; i++) {
209 if (!ctx->enabled[i])
210 continue;
211 if (mixer_ops && mixer_ops->win_commit)
212 mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
213 }
214
215 if (hdmi_ops && hdmi_ops->commit)
216 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
217 }
218
219 static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
220 .dpms = drm_hdmi_dpms,
221 .apply = drm_hdmi_apply,
222 .enable_vblank = drm_hdmi_enable_vblank,
223 .disable_vblank = drm_hdmi_disable_vblank,
224 .mode_fixup = drm_hdmi_mode_fixup,
225 .mode_set = drm_hdmi_mode_set,
226 .get_max_resol = drm_hdmi_get_max_resol,
227 .commit = drm_hdmi_commit,
228 };
229
230 static void drm_mixer_mode_set(struct device *subdrv_dev,
231 struct exynos_drm_overlay *overlay)
232 {
233 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
234
235 DRM_DEBUG_KMS("%s\n", __FILE__);
236
237 if (mixer_ops && mixer_ops->win_mode_set)
238 mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
239 }
240
241 static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
242 {
243 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
244 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
245
246 DRM_DEBUG_KMS("%s\n", __FILE__);
247
248 if (win < 0 || win > MIXER_WIN_NR) {
249 DRM_ERROR("mixer window[%d] is wrong\n", win);
250 return;
251 }
252
253 if (mixer_ops && mixer_ops->win_commit)
254 mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
255
256 ctx->enabled[win] = true;
257 }
258
259 static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
260 {
261 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
262 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
263
264 DRM_DEBUG_KMS("%s\n", __FILE__);
265
266 if (win < 0 || win > MIXER_WIN_NR) {
267 DRM_ERROR("mixer window[%d] is wrong\n", win);
268 return;
269 }
270
271 if (mixer_ops && mixer_ops->win_disable)
272 mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
273
274 ctx->enabled[win] = false;
275 }
276
277 static void drm_mixer_wait_for_vblank(struct device *subdrv_dev)
278 {
279 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
280
281 DRM_DEBUG_KMS("%s\n", __FILE__);
282
283 if (mixer_ops && mixer_ops->wait_for_vblank)
284 mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
285 }
286
287 static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
288 .mode_set = drm_mixer_mode_set,
289 .commit = drm_mixer_commit,
290 .disable = drm_mixer_disable,
291 .wait_for_vblank = drm_mixer_wait_for_vblank,
292 };
293
294 static struct exynos_drm_manager hdmi_manager = {
295 .pipe = -1,
296 .ops = &drm_hdmi_manager_ops,
297 .overlay_ops = &drm_hdmi_overlay_ops,
298 .display_ops = &drm_hdmi_display_ops,
299 };
300
301 static int hdmi_subdrv_probe(struct drm_device *drm_dev,
302 struct device *dev)
303 {
304 struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
305 struct drm_hdmi_context *ctx;
306 struct platform_device *pdev = to_platform_device(dev);
307 struct exynos_drm_common_hdmi_pd *pd;
308
309 DRM_DEBUG_KMS("%s\n", __FILE__);
310
311 pd = pdev->dev.platform_data;
312
313 if (!pd) {
314 DRM_DEBUG_KMS("platform data is null.\n");
315 return -EFAULT;
316 }
317
318 if (!pd->hdmi_dev) {
319 DRM_DEBUG_KMS("hdmi device is null.\n");
320 return -EFAULT;
321 }
322
323 if (!pd->mixer_dev) {
324 DRM_DEBUG_KMS("mixer device is null.\n");
325 return -EFAULT;
326 }
327
328 ctx = get_ctx_from_subdrv(subdrv);
329
330 ctx->hdmi_ctx = (struct exynos_drm_hdmi_context *)
331 to_context(pd->hdmi_dev);
332 if (!ctx->hdmi_ctx) {
333 DRM_DEBUG_KMS("hdmi context is null.\n");
334 return -EFAULT;
335 }
336
337 ctx->hdmi_ctx->drm_dev = drm_dev;
338
339 ctx->mixer_ctx = (struct exynos_drm_hdmi_context *)
340 to_context(pd->mixer_dev);
341 if (!ctx->mixer_ctx) {
342 DRM_DEBUG_KMS("mixer context is null.\n");
343 return -EFAULT;
344 }
345
346 ctx->mixer_ctx->drm_dev = drm_dev;
347
348 return 0;
349 }
350
351 static int __devinit exynos_drm_hdmi_probe(struct platform_device *pdev)
352 {
353 struct device *dev = &pdev->dev;
354 struct exynos_drm_subdrv *subdrv;
355 struct drm_hdmi_context *ctx;
356
357 DRM_DEBUG_KMS("%s\n", __FILE__);
358
359 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
360 if (!ctx) {
361 DRM_LOG_KMS("failed to alloc common hdmi context.\n");
362 return -ENOMEM;
363 }
364
365 subdrv = &ctx->subdrv;
366
367 subdrv->dev = dev;
368 subdrv->manager = &hdmi_manager;
369 subdrv->probe = hdmi_subdrv_probe;
370
371 platform_set_drvdata(pdev, subdrv);
372
373 exynos_drm_subdrv_register(subdrv);
374
375 return 0;
376 }
377
378 static int __devexit exynos_drm_hdmi_remove(struct platform_device *pdev)
379 {
380 struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
381
382 DRM_DEBUG_KMS("%s\n", __FILE__);
383
384 exynos_drm_subdrv_unregister(&ctx->subdrv);
385
386 return 0;
387 }
388
389 struct platform_driver exynos_drm_common_hdmi_driver = {
390 .probe = exynos_drm_hdmi_probe,
391 .remove = __devexit_p(exynos_drm_hdmi_remove),
392 .driver = {
393 .name = "exynos-drm-hdmi",
394 .owner = THIS_MODULE,
395 },
396 };
This page took 0.041294 seconds and 5 git commands to generate.