]> Pileus Git - ~andy/linux/blob - drivers/gpu/drm/exynos/exynos_drm_hdmi.c
drm/nouveau/bios: parse fan bump/slow periods, and trip points
[~andy/linux] / 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
128         DRM_DEBUG_KMS("%s\n", __FILE__);
129
130         if (hdmi_ops && hdmi_ops->check_timing)
131                 return hdmi_ops->check_timing(ctx->hdmi_ctx->ctx, timing);
132
133         return 0;
134 }
135
136 static int drm_hdmi_power_on(struct device *dev, int mode)
137 {
138         struct drm_hdmi_context *ctx = to_context(dev);
139
140         DRM_DEBUG_KMS("%s\n", __FILE__);
141
142         if (hdmi_ops && hdmi_ops->power_on)
143                 return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
144
145         return 0;
146 }
147
148 static struct exynos_drm_display_ops drm_hdmi_display_ops = {
149         .type = EXYNOS_DISPLAY_TYPE_HDMI,
150         .is_connected = drm_hdmi_is_connected,
151         .get_edid = drm_hdmi_get_edid,
152         .check_timing = drm_hdmi_check_timing,
153         .power_on = drm_hdmi_power_on,
154 };
155
156 static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
157 {
158         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
159         struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
160         struct exynos_drm_manager *manager = subdrv->manager;
161
162         DRM_DEBUG_KMS("%s\n", __FILE__);
163
164         if (mixer_ops && mixer_ops->enable_vblank)
165                 return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
166                                                 manager->pipe);
167
168         return 0;
169 }
170
171 static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
172 {
173         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
174
175         DRM_DEBUG_KMS("%s\n", __FILE__);
176
177         if (mixer_ops && mixer_ops->disable_vblank)
178                 return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
179 }
180
181 static void drm_hdmi_wait_for_vblank(struct device *subdrv_dev)
182 {
183         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
184
185         DRM_DEBUG_KMS("%s\n", __FILE__);
186
187         if (mixer_ops && mixer_ops->wait_for_vblank)
188                 mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
189 }
190
191 static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
192                                 struct drm_connector *connector,
193                                 const struct drm_display_mode *mode,
194                                 struct drm_display_mode *adjusted_mode)
195 {
196         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
197
198         DRM_DEBUG_KMS("%s\n", __FILE__);
199
200         if (hdmi_ops && hdmi_ops->mode_fixup)
201                 hdmi_ops->mode_fixup(ctx->hdmi_ctx->ctx, connector, mode,
202                                      adjusted_mode);
203 }
204
205 static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
206 {
207         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
208
209         DRM_DEBUG_KMS("%s\n", __FILE__);
210
211         if (hdmi_ops && hdmi_ops->mode_set)
212                 hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
213 }
214
215 static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
216                                 unsigned int *width, unsigned int *height)
217 {
218         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
219
220         DRM_DEBUG_KMS("%s\n", __FILE__);
221
222         if (hdmi_ops && hdmi_ops->get_max_resol)
223                 hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
224 }
225
226 static void drm_hdmi_commit(struct device *subdrv_dev)
227 {
228         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
229
230         DRM_DEBUG_KMS("%s\n", __FILE__);
231
232         if (hdmi_ops && hdmi_ops->commit)
233                 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
234 }
235
236 static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
237 {
238         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
239
240         DRM_DEBUG_KMS("%s\n", __FILE__);
241
242         if (mixer_ops && mixer_ops->dpms)
243                 mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
244
245         if (hdmi_ops && hdmi_ops->dpms)
246                 hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
247 }
248
249 static void drm_hdmi_apply(struct device *subdrv_dev)
250 {
251         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
252         int i;
253
254         DRM_DEBUG_KMS("%s\n", __FILE__);
255
256         for (i = 0; i < MIXER_WIN_NR; i++) {
257                 if (!ctx->enabled[i])
258                         continue;
259                 if (mixer_ops && mixer_ops->win_commit)
260                         mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
261         }
262
263         if (hdmi_ops && hdmi_ops->commit)
264                 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
265 }
266
267 static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
268         .dpms = drm_hdmi_dpms,
269         .apply = drm_hdmi_apply,
270         .enable_vblank = drm_hdmi_enable_vblank,
271         .disable_vblank = drm_hdmi_disable_vblank,
272         .wait_for_vblank = drm_hdmi_wait_for_vblank,
273         .mode_fixup = drm_hdmi_mode_fixup,
274         .mode_set = drm_hdmi_mode_set,
275         .get_max_resol = drm_hdmi_get_max_resol,
276         .commit = drm_hdmi_commit,
277 };
278
279 static void drm_mixer_mode_set(struct device *subdrv_dev,
280                 struct exynos_drm_overlay *overlay)
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->win_mode_set)
287                 mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
288 }
289
290 static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
291 {
292         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
293         int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
294
295         DRM_DEBUG_KMS("%s\n", __FILE__);
296
297         if (win < 0 || win > MIXER_WIN_NR) {
298                 DRM_ERROR("mixer window[%d] is wrong\n", win);
299                 return;
300         }
301
302         if (mixer_ops && mixer_ops->win_commit)
303                 mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
304
305         ctx->enabled[win] = true;
306 }
307
308 static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
309 {
310         struct drm_hdmi_context *ctx = to_context(subdrv_dev);
311         int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
312
313         DRM_DEBUG_KMS("%s\n", __FILE__);
314
315         if (win < 0 || win > MIXER_WIN_NR) {
316                 DRM_ERROR("mixer window[%d] is wrong\n", win);
317                 return;
318         }
319
320         if (mixer_ops && mixer_ops->win_disable)
321                 mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
322
323         ctx->enabled[win] = false;
324 }
325
326 static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
327         .mode_set = drm_mixer_mode_set,
328         .commit = drm_mixer_commit,
329         .disable = drm_mixer_disable,
330 };
331
332 static struct exynos_drm_manager hdmi_manager = {
333         .pipe           = -1,
334         .ops            = &drm_hdmi_manager_ops,
335         .overlay_ops    = &drm_hdmi_overlay_ops,
336         .display_ops    = &drm_hdmi_display_ops,
337 };
338
339 static int hdmi_subdrv_probe(struct drm_device *drm_dev,
340                 struct device *dev)
341 {
342         struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
343         struct drm_hdmi_context *ctx;
344
345         DRM_DEBUG_KMS("%s\n", __FILE__);
346
347         if (!hdmi_ctx) {
348                 DRM_ERROR("hdmi context not initialized.\n");
349                 return -EFAULT;
350         }
351
352         if (!mixer_ctx) {
353                 DRM_ERROR("mixer context not initialized.\n");
354                 return -EFAULT;
355         }
356
357         ctx = get_ctx_from_subdrv(subdrv);
358
359         if (!ctx) {
360                 DRM_ERROR("no drm hdmi context.\n");
361                 return -EFAULT;
362         }
363
364         ctx->hdmi_ctx = hdmi_ctx;
365         ctx->mixer_ctx = mixer_ctx;
366
367         ctx->hdmi_ctx->drm_dev = drm_dev;
368         ctx->mixer_ctx->drm_dev = drm_dev;
369
370         if (mixer_ops->iommu_on)
371                 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);
372
373         return 0;
374 }
375
376 static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
377 {
378         struct drm_hdmi_context *ctx;
379         struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
380
381         ctx = get_ctx_from_subdrv(subdrv);
382
383         if (mixer_ops->iommu_on)
384                 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
385 }
386
387 static int exynos_drm_hdmi_probe(struct platform_device *pdev)
388 {
389         struct device *dev = &pdev->dev;
390         struct exynos_drm_subdrv *subdrv;
391         struct drm_hdmi_context *ctx;
392
393         DRM_DEBUG_KMS("%s\n", __FILE__);
394
395         ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
396         if (!ctx) {
397                 DRM_LOG_KMS("failed to alloc common hdmi context.\n");
398                 return -ENOMEM;
399         }
400
401         subdrv = &ctx->subdrv;
402
403         subdrv->dev = dev;
404         subdrv->manager = &hdmi_manager;
405         subdrv->probe = hdmi_subdrv_probe;
406         subdrv->remove = hdmi_subdrv_remove;
407
408         platform_set_drvdata(pdev, subdrv);
409
410         exynos_drm_subdrv_register(subdrv);
411
412         return 0;
413 }
414
415 static int exynos_drm_hdmi_remove(struct platform_device *pdev)
416 {
417         struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
418
419         DRM_DEBUG_KMS("%s\n", __FILE__);
420
421         exynos_drm_subdrv_unregister(&ctx->subdrv);
422
423         return 0;
424 }
425
426 struct platform_driver exynos_drm_common_hdmi_driver = {
427         .probe          = exynos_drm_hdmi_probe,
428         .remove         = exynos_drm_hdmi_remove,
429         .driver         = {
430                 .name   = "exynos-drm-hdmi",
431                 .owner  = THIS_MODULE,
432         },
433 };