Merge branch 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[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_OR_NULL(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_hdmi_context *ctx = to_context(subdrv_dev);
209
210         DRM_DEBUG_KMS("%s\n", __FILE__);
211
212         if (hdmi_ops && hdmi_ops->mode_fixup)
213                 hdmi_ops->mode_fixup(ctx->hdmi_ctx->ctx, connector, mode,
214                                      adjusted_mode);
215 }
216
217 static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
218 {
219         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
220
221         DRM_DEBUG_KMS("%s\n", __FILE__);
222
223         if (hdmi_ops && hdmi_ops->mode_set)
224                 hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
225 }
226
227 static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
228                                 unsigned int *width, unsigned int *height)
229 {
230         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
231
232         DRM_DEBUG_KMS("%s\n", __FILE__);
233
234         if (hdmi_ops && hdmi_ops->get_max_resol)
235                 hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
236 }
237
238 static void drm_hdmi_commit(struct device *subdrv_dev)
239 {
240         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
241
242         DRM_DEBUG_KMS("%s\n", __FILE__);
243
244         if (hdmi_ops && hdmi_ops->commit)
245                 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
246 }
247
248 static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
249 {
250         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
251
252         DRM_DEBUG_KMS("%s\n", __FILE__);
253
254         if (mixer_ops && mixer_ops->dpms)
255                 mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
256
257         if (hdmi_ops && hdmi_ops->dpms)
258                 hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
259 }
260
261 static void drm_hdmi_apply(struct device *subdrv_dev)
262 {
263         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
264         int i;
265
266         DRM_DEBUG_KMS("%s\n", __FILE__);
267
268         for (i = 0; i < MIXER_WIN_NR; i++) {
269                 if (!ctx->enabled[i])
270                         continue;
271                 if (mixer_ops && mixer_ops->win_commit)
272                         mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
273         }
274
275         if (hdmi_ops && hdmi_ops->commit)
276                 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
277 }
278
279 static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
280         .dpms = drm_hdmi_dpms,
281         .apply = drm_hdmi_apply,
282         .enable_vblank = drm_hdmi_enable_vblank,
283         .disable_vblank = drm_hdmi_disable_vblank,
284         .wait_for_vblank = drm_hdmi_wait_for_vblank,
285         .mode_fixup = drm_hdmi_mode_fixup,
286         .mode_set = drm_hdmi_mode_set,
287         .get_max_resol = drm_hdmi_get_max_resol,
288         .commit = drm_hdmi_commit,
289 };
290
291 static void drm_mixer_mode_set(struct device *subdrv_dev,
292                 struct exynos_drm_overlay *overlay)
293 {
294         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
295
296         DRM_DEBUG_KMS("%s\n", __FILE__);
297
298         if (mixer_ops && mixer_ops->win_mode_set)
299                 mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
300 }
301
302 static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
303 {
304         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
305         int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
306
307         DRM_DEBUG_KMS("%s\n", __FILE__);
308
309         if (win < 0 || win > MIXER_WIN_NR) {
310                 DRM_ERROR("mixer window[%d] is wrong\n", win);
311                 return;
312         }
313
314         if (mixer_ops && mixer_ops->win_commit)
315                 mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
316
317         ctx->enabled[win] = true;
318 }
319
320 static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
321 {
322         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
323         int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
324
325         DRM_DEBUG_KMS("%s\n", __FILE__);
326
327         if (win < 0 || win > MIXER_WIN_NR) {
328                 DRM_ERROR("mixer window[%d] is wrong\n", win);
329                 return;
330         }
331
332         if (mixer_ops && mixer_ops->win_disable)
333                 mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
334
335         ctx->enabled[win] = false;
336 }
337
338 static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
339         .mode_set = drm_mixer_mode_set,
340         .commit = drm_mixer_commit,
341         .disable = drm_mixer_disable,
342 };
343
344 static struct exynos_drm_manager hdmi_manager = {
345         .pipe           = -1,
346         .ops            = &drm_hdmi_manager_ops,
347         .overlay_ops    = &drm_hdmi_overlay_ops,
348         .display_ops    = &drm_hdmi_display_ops,
349 };
350
351 static int hdmi_subdrv_probe(struct drm_device *drm_dev,
352                 struct device *dev)
353 {
354         struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
355         struct drm_hdmi_context *ctx;
356
357         DRM_DEBUG_KMS("%s\n", __FILE__);
358
359         if (!hdmi_ctx) {
360                 DRM_ERROR("hdmi context not initialized.\n");
361                 return -EFAULT;
362         }
363
364         if (!mixer_ctx) {
365                 DRM_ERROR("mixer context not initialized.\n");
366                 return -EFAULT;
367         }
368
369         ctx = get_ctx_from_subdrv(subdrv);
370
371         if (!ctx) {
372                 DRM_ERROR("no drm hdmi context.\n");
373                 return -EFAULT;
374         }
375
376         ctx->hdmi_ctx = hdmi_ctx;
377         ctx->mixer_ctx = mixer_ctx;
378
379         ctx->hdmi_ctx->drm_dev = drm_dev;
380         ctx->mixer_ctx->drm_dev = drm_dev;
381
382         if (mixer_ops->iommu_on)
383                 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);
384
385         return 0;
386 }
387
388 static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
389 {
390         struct drm_hdmi_context *ctx;
391         struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
392
393         ctx = get_ctx_from_subdrv(subdrv);
394
395         if (mixer_ops->iommu_on)
396                 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
397 }
398
399 static int exynos_drm_hdmi_probe(struct platform_device *pdev)
400 {
401         struct device *dev = &pdev->dev;
402         struct exynos_drm_subdrv *subdrv;
403         struct drm_hdmi_context *ctx;
404
405         DRM_DEBUG_KMS("%s\n", __FILE__);
406
407         ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
408         if (!ctx) {
409                 DRM_LOG_KMS("failed to alloc common hdmi context.\n");
410                 return -ENOMEM;
411         }
412
413         subdrv = &ctx->subdrv;
414
415         subdrv->dev = dev;
416         subdrv->manager = &hdmi_manager;
417         subdrv->probe = hdmi_subdrv_probe;
418         subdrv->remove = hdmi_subdrv_remove;
419
420         platform_set_drvdata(pdev, subdrv);
421
422         exynos_drm_subdrv_register(subdrv);
423
424         return 0;
425 }
426
427 static int exynos_drm_hdmi_remove(struct platform_device *pdev)
428 {
429         struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
430
431         DRM_DEBUG_KMS("%s\n", __FILE__);
432
433         exynos_drm_subdrv_unregister(&ctx->subdrv);
434
435         return 0;
436 }
437
438 struct platform_driver exynos_drm_common_hdmi_driver = {
439         .probe          = exynos_drm_hdmi_probe,
440         .remove         = exynos_drm_hdmi_remove,
441         .driver         = {
442                 .name   = "exynos-drm-hdmi",
443                 .owner  = THIS_MODULE,
444         },
445 };