drm/exynos: fix wrong return check for platform_device_register_simple
[platform/kernel/linux-rpi.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 <drm/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 /* platform device pointer for common drm hdmi device. */
33 static struct platform_device *exynos_drm_hdmi_pdev;
34
35 /* Common hdmi subdrv needs to access the hdmi and mixer though context.
36 * These should be initialied by the repective drivers */
37 static struct exynos_drm_hdmi_context *hdmi_ctx;
38 static struct exynos_drm_hdmi_context *mixer_ctx;
39
40 /* these callback points shoud be set by specific drivers. */
41 static struct exynos_hdmi_ops *hdmi_ops;
42 static struct exynos_mixer_ops *mixer_ops;
43
44 struct drm_hdmi_context {
45         struct exynos_drm_subdrv        subdrv;
46         struct exynos_drm_hdmi_context  *hdmi_ctx;
47         struct exynos_drm_hdmi_context  *mixer_ctx;
48
49         bool    enabled[MIXER_WIN_NR];
50 };
51
52 int exynos_platform_device_hdmi_register(void)
53 {
54         if (exynos_drm_hdmi_pdev)
55                 return -EEXIST;
56
57         exynos_drm_hdmi_pdev = platform_device_register_simple(
58                         "exynos-drm-hdmi", -1, NULL, 0);
59         if (IS_ERR(exynos_drm_hdmi_pdev))
60                 return PTR_ERR(exynos_drm_hdmi_pdev);
61
62         return 0;
63 }
64
65 void exynos_platform_device_hdmi_unregister(void)
66 {
67         if (exynos_drm_hdmi_pdev)
68                 platform_device_unregister(exynos_drm_hdmi_pdev);
69 }
70
71 void exynos_hdmi_drv_attach(struct exynos_drm_hdmi_context *ctx)
72 {
73         if (ctx)
74                 hdmi_ctx = ctx;
75 }
76
77 void exynos_mixer_drv_attach(struct exynos_drm_hdmi_context *ctx)
78 {
79         if (ctx)
80                 mixer_ctx = ctx;
81 }
82
83 void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
84 {
85         DRM_DEBUG_KMS("%s\n", __FILE__);
86
87         if (ops)
88                 hdmi_ops = ops;
89 }
90
91 void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
92 {
93         DRM_DEBUG_KMS("%s\n", __FILE__);
94
95         if (ops)
96                 mixer_ops = ops;
97 }
98
99 static bool drm_hdmi_is_connected(struct device *dev)
100 {
101         struct drm_hdmi_context *ctx = to_context(dev);
102
103         DRM_DEBUG_KMS("%s\n", __FILE__);
104
105         if (hdmi_ops && hdmi_ops->is_connected)
106                 return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
107
108         return false;
109 }
110
111 static struct edid *drm_hdmi_get_edid(struct device *dev,
112                         struct drm_connector *connector)
113 {
114         struct drm_hdmi_context *ctx = to_context(dev);
115
116         DRM_DEBUG_KMS("%s\n", __FILE__);
117
118         if (hdmi_ops && hdmi_ops->get_edid)
119                 return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector);
120
121         return NULL;
122 }
123
124 static int drm_hdmi_check_timing(struct device *dev, void *timing)
125 {
126         struct drm_hdmi_context *ctx = to_context(dev);
127         int ret = 0;
128
129         DRM_DEBUG_KMS("%s\n", __FILE__);
130
131         /*
132         * Both, mixer and hdmi should be able to handle the requested mode.
133         * If any of the two fails, return mode as BAD.
134         */
135
136         if (mixer_ops && mixer_ops->check_timing)
137                 ret = mixer_ops->check_timing(ctx->mixer_ctx->ctx, timing);
138
139         if (ret)
140                 return ret;
141
142         if (hdmi_ops && hdmi_ops->check_timing)
143                 return hdmi_ops->check_timing(ctx->hdmi_ctx->ctx, timing);
144
145         return 0;
146 }
147
148 static int drm_hdmi_power_on(struct device *dev, int mode)
149 {
150         struct drm_hdmi_context *ctx = to_context(dev);
151
152         DRM_DEBUG_KMS("%s\n", __FILE__);
153
154         if (hdmi_ops && hdmi_ops->power_on)
155                 return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
156
157         return 0;
158 }
159
160 static struct exynos_drm_display_ops drm_hdmi_display_ops = {
161         .type = EXYNOS_DISPLAY_TYPE_HDMI,
162         .is_connected = drm_hdmi_is_connected,
163         .get_edid = drm_hdmi_get_edid,
164         .check_timing = drm_hdmi_check_timing,
165         .power_on = drm_hdmi_power_on,
166 };
167
168 static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
169 {
170         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
171         struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
172         struct exynos_drm_manager *manager = subdrv->manager;
173
174         DRM_DEBUG_KMS("%s\n", __FILE__);
175
176         if (mixer_ops && mixer_ops->enable_vblank)
177                 return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
178                                                 manager->pipe);
179
180         return 0;
181 }
182
183 static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
184 {
185         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
186
187         DRM_DEBUG_KMS("%s\n", __FILE__);
188
189         if (mixer_ops && mixer_ops->disable_vblank)
190                 return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
191 }
192
193 static void drm_hdmi_wait_for_vblank(struct device *subdrv_dev)
194 {
195         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
196
197         DRM_DEBUG_KMS("%s\n", __FILE__);
198
199         if (mixer_ops && mixer_ops->wait_for_vblank)
200                 mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
201 }
202
203 static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
204                                 struct drm_connector *connector,
205                                 const struct drm_display_mode *mode,
206                                 struct drm_display_mode *adjusted_mode)
207 {
208         struct drm_display_mode *m;
209         int mode_ok;
210
211         DRM_DEBUG_KMS("%s\n", __FILE__);
212
213         drm_mode_set_crtcinfo(adjusted_mode, 0);
214
215         mode_ok = drm_hdmi_check_timing(subdrv_dev, adjusted_mode);
216
217         /* just return if user desired mode exists. */
218         if (mode_ok == 0)
219                 return;
220
221         /*
222          * otherwise, find the most suitable mode among modes and change it
223          * to adjusted_mode.
224          */
225         list_for_each_entry(m, &connector->modes, head) {
226                 mode_ok = drm_hdmi_check_timing(subdrv_dev, m);
227
228                 if (mode_ok == 0) {
229                         struct drm_mode_object base;
230                         struct list_head head;
231
232                         DRM_INFO("desired mode doesn't exist so\n");
233                         DRM_INFO("use the most suitable mode among modes.\n");
234
235                         DRM_DEBUG_KMS("Adjusted Mode: [%d]x[%d] [%d]Hz\n",
236                                 m->hdisplay, m->vdisplay, m->vrefresh);
237
238                         /* preserve display mode header while copying. */
239                         head = adjusted_mode->head;
240                         base = adjusted_mode->base;
241                         memcpy(adjusted_mode, m, sizeof(*m));
242                         adjusted_mode->head = head;
243                         adjusted_mode->base = base;
244                         break;
245                 }
246         }
247 }
248
249 static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
250 {
251         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
252
253         DRM_DEBUG_KMS("%s\n", __FILE__);
254
255         if (hdmi_ops && hdmi_ops->mode_set)
256                 hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
257 }
258
259 static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
260                                 unsigned int *width, unsigned int *height)
261 {
262         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
263
264         DRM_DEBUG_KMS("%s\n", __FILE__);
265
266         if (hdmi_ops && hdmi_ops->get_max_resol)
267                 hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
268 }
269
270 static void drm_hdmi_commit(struct device *subdrv_dev)
271 {
272         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
273
274         DRM_DEBUG_KMS("%s\n", __FILE__);
275
276         if (hdmi_ops && hdmi_ops->commit)
277                 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
278 }
279
280 static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
281 {
282         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
283
284         DRM_DEBUG_KMS("%s\n", __FILE__);
285
286         if (mixer_ops && mixer_ops->dpms)
287                 mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
288
289         if (hdmi_ops && hdmi_ops->dpms)
290                 hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
291 }
292
293 static void drm_hdmi_apply(struct device *subdrv_dev)
294 {
295         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
296         int i;
297
298         DRM_DEBUG_KMS("%s\n", __FILE__);
299
300         for (i = 0; i < MIXER_WIN_NR; i++) {
301                 if (!ctx->enabled[i])
302                         continue;
303                 if (mixer_ops && mixer_ops->win_commit)
304                         mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
305         }
306
307         if (hdmi_ops && hdmi_ops->commit)
308                 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
309 }
310
311 static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
312         .dpms = drm_hdmi_dpms,
313         .apply = drm_hdmi_apply,
314         .enable_vblank = drm_hdmi_enable_vblank,
315         .disable_vblank = drm_hdmi_disable_vblank,
316         .wait_for_vblank = drm_hdmi_wait_for_vblank,
317         .mode_fixup = drm_hdmi_mode_fixup,
318         .mode_set = drm_hdmi_mode_set,
319         .get_max_resol = drm_hdmi_get_max_resol,
320         .commit = drm_hdmi_commit,
321 };
322
323 static void drm_mixer_mode_set(struct device *subdrv_dev,
324                 struct exynos_drm_overlay *overlay)
325 {
326         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
327
328         DRM_DEBUG_KMS("%s\n", __FILE__);
329
330         if (mixer_ops && mixer_ops->win_mode_set)
331                 mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
332 }
333
334 static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
335 {
336         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
337         int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
338
339         DRM_DEBUG_KMS("%s\n", __FILE__);
340
341         if (win < 0 || win > MIXER_WIN_NR) {
342                 DRM_ERROR("mixer window[%d] is wrong\n", win);
343                 return;
344         }
345
346         if (mixer_ops && mixer_ops->win_commit)
347                 mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
348
349         ctx->enabled[win] = true;
350 }
351
352 static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
353 {
354         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
355         int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
356
357         DRM_DEBUG_KMS("%s\n", __FILE__);
358
359         if (win < 0 || win > MIXER_WIN_NR) {
360                 DRM_ERROR("mixer window[%d] is wrong\n", win);
361                 return;
362         }
363
364         if (mixer_ops && mixer_ops->win_disable)
365                 mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
366
367         ctx->enabled[win] = false;
368 }
369
370 static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
371         .mode_set = drm_mixer_mode_set,
372         .commit = drm_mixer_commit,
373         .disable = drm_mixer_disable,
374 };
375
376 static struct exynos_drm_manager hdmi_manager = {
377         .pipe           = -1,
378         .ops            = &drm_hdmi_manager_ops,
379         .overlay_ops    = &drm_hdmi_overlay_ops,
380         .display_ops    = &drm_hdmi_display_ops,
381 };
382
383 static int hdmi_subdrv_probe(struct drm_device *drm_dev,
384                 struct device *dev)
385 {
386         struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
387         struct drm_hdmi_context *ctx;
388
389         DRM_DEBUG_KMS("%s\n", __FILE__);
390
391         if (!hdmi_ctx) {
392                 DRM_ERROR("hdmi context not initialized.\n");
393                 return -EFAULT;
394         }
395
396         if (!mixer_ctx) {
397                 DRM_ERROR("mixer context not initialized.\n");
398                 return -EFAULT;
399         }
400
401         ctx = get_ctx_from_subdrv(subdrv);
402
403         if (!ctx) {
404                 DRM_ERROR("no drm hdmi context.\n");
405                 return -EFAULT;
406         }
407
408         ctx->hdmi_ctx = hdmi_ctx;
409         ctx->mixer_ctx = mixer_ctx;
410
411         ctx->hdmi_ctx->drm_dev = drm_dev;
412         ctx->mixer_ctx->drm_dev = drm_dev;
413
414         if (mixer_ops->iommu_on)
415                 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);
416
417         return 0;
418 }
419
420 static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
421 {
422         struct drm_hdmi_context *ctx;
423         struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
424
425         ctx = get_ctx_from_subdrv(subdrv);
426
427         if (mixer_ops->iommu_on)
428                 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
429 }
430
431 static int exynos_drm_hdmi_probe(struct platform_device *pdev)
432 {
433         struct device *dev = &pdev->dev;
434         struct exynos_drm_subdrv *subdrv;
435         struct drm_hdmi_context *ctx;
436
437         DRM_DEBUG_KMS("%s\n", __FILE__);
438
439         ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
440         if (!ctx) {
441                 DRM_LOG_KMS("failed to alloc common hdmi context.\n");
442                 return -ENOMEM;
443         }
444
445         subdrv = &ctx->subdrv;
446
447         subdrv->dev = dev;
448         subdrv->manager = &hdmi_manager;
449         subdrv->probe = hdmi_subdrv_probe;
450         subdrv->remove = hdmi_subdrv_remove;
451
452         platform_set_drvdata(pdev, subdrv);
453
454         exynos_drm_subdrv_register(subdrv);
455
456         return 0;
457 }
458
459 static int exynos_drm_hdmi_remove(struct platform_device *pdev)
460 {
461         struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
462
463         DRM_DEBUG_KMS("%s\n", __FILE__);
464
465         exynos_drm_subdrv_unregister(&ctx->subdrv);
466
467         return 0;
468 }
469
470 struct platform_driver exynos_drm_common_hdmi_driver = {
471         .probe          = exynos_drm_hdmi_probe,
472         .remove         = exynos_drm_hdmi_remove,
473         .driver         = {
474                 .name   = "exynos-drm-hdmi",
475                 .owner  = THIS_MODULE,
476         },
477 };