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