blob: c3b9e2b4518516ebcf65133346d8f3bf33142d6e [file] [log] [blame]
Seung-Woo Kimd8408322011-12-21 17:39:39 +09001/*
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
David Howells760285e2012-10-02 18:01:07 +010014#include <drm/drmP.h>
Seung-Woo Kimd8408322011-12-21 17:39:39 +090015
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
Rahul Sharma768c3052012-10-04 20:48:56 +053032/* Common hdmi subdrv needs to access the hdmi and mixer though context.
33* These should be initialied by the repective drivers */
34static struct exynos_drm_hdmi_context *hdmi_ctx;
35static struct exynos_drm_hdmi_context *mixer_ctx;
36
Seung-Woo Kimd8408322011-12-21 17:39:39 +090037/* these callback points shoud be set by specific drivers. */
Joonyoung Shim578b6062012-04-05 20:49:26 +090038static struct exynos_hdmi_ops *hdmi_ops;
39static struct exynos_mixer_ops *mixer_ops;
Seung-Woo Kimd8408322011-12-21 17:39:39 +090040
41struct drm_hdmi_context {
42 struct exynos_drm_subdrv subdrv;
43 struct exynos_drm_hdmi_context *hdmi_ctx;
44 struct exynos_drm_hdmi_context *mixer_ctx;
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +090045
46 bool enabled[MIXER_WIN_NR];
Seung-Woo Kimd8408322011-12-21 17:39:39 +090047};
48
Rahul Sharma768c3052012-10-04 20:48:56 +053049void exynos_hdmi_drv_attach(struct exynos_drm_hdmi_context *ctx)
50{
51 if (ctx)
52 hdmi_ctx = ctx;
53}
54
55void exynos_mixer_drv_attach(struct exynos_drm_hdmi_context *ctx)
56{
57 if (ctx)
58 mixer_ctx = ctx;
59}
60
Joonyoung Shim578b6062012-04-05 20:49:26 +090061void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
Seung-Woo Kimd8408322011-12-21 17:39:39 +090062{
63 DRM_DEBUG_KMS("%s\n", __FILE__);
64
Joonyoung Shim578b6062012-04-05 20:49:26 +090065 if (ops)
66 hdmi_ops = ops;
Seung-Woo Kimd8408322011-12-21 17:39:39 +090067}
Seung-Woo Kimd8408322011-12-21 17:39:39 +090068
Joonyoung Shim578b6062012-04-05 20:49:26 +090069void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
Seung-Woo Kimd8408322011-12-21 17:39:39 +090070{
71 DRM_DEBUG_KMS("%s\n", __FILE__);
72
Joonyoung Shim578b6062012-04-05 20:49:26 +090073 if (ops)
74 mixer_ops = ops;
Seung-Woo Kimd8408322011-12-21 17:39:39 +090075}
Seung-Woo Kimd8408322011-12-21 17:39:39 +090076
77static bool drm_hdmi_is_connected(struct device *dev)
78{
79 struct drm_hdmi_context *ctx = to_context(dev);
80
81 DRM_DEBUG_KMS("%s\n", __FILE__);
82
Joonyoung Shim578b6062012-04-05 20:49:26 +090083 if (hdmi_ops && hdmi_ops->is_connected)
84 return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
Seung-Woo Kimd8408322011-12-21 17:39:39 +090085
86 return false;
87}
88
89static int drm_hdmi_get_edid(struct device *dev,
90 struct drm_connector *connector, u8 *edid, int len)
91{
92 struct drm_hdmi_context *ctx = to_context(dev);
93
94 DRM_DEBUG_KMS("%s\n", __FILE__);
95
Joonyoung Shim578b6062012-04-05 20:49:26 +090096 if (hdmi_ops && hdmi_ops->get_edid)
97 return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector, edid,
98 len);
Seung-Woo Kimd8408322011-12-21 17:39:39 +090099
100 return 0;
101}
102
103static int drm_hdmi_check_timing(struct device *dev, void *timing)
104{
105 struct drm_hdmi_context *ctx = to_context(dev);
106
107 DRM_DEBUG_KMS("%s\n", __FILE__);
108
Joonyoung Shim578b6062012-04-05 20:49:26 +0900109 if (hdmi_ops && hdmi_ops->check_timing)
110 return hdmi_ops->check_timing(ctx->hdmi_ctx->ctx, timing);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900111
112 return 0;
113}
114
115static int drm_hdmi_power_on(struct device *dev, int mode)
116{
117 struct drm_hdmi_context *ctx = to_context(dev);
118
119 DRM_DEBUG_KMS("%s\n", __FILE__);
120
Joonyoung Shim578b6062012-04-05 20:49:26 +0900121 if (hdmi_ops && hdmi_ops->power_on)
122 return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900123
124 return 0;
125}
126
127static struct exynos_drm_display_ops drm_hdmi_display_ops = {
128 .type = EXYNOS_DISPLAY_TYPE_HDMI,
129 .is_connected = drm_hdmi_is_connected,
130 .get_edid = drm_hdmi_get_edid,
131 .check_timing = drm_hdmi_check_timing,
132 .power_on = drm_hdmi_power_on,
133};
134
135static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
136{
137 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
138 struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900139 struct exynos_drm_manager *manager = subdrv->manager;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900140
141 DRM_DEBUG_KMS("%s\n", __FILE__);
142
Joonyoung Shim578b6062012-04-05 20:49:26 +0900143 if (mixer_ops && mixer_ops->enable_vblank)
144 return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
145 manager->pipe);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900146
147 return 0;
148}
149
150static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
151{
152 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
153
154 DRM_DEBUG_KMS("%s\n", __FILE__);
155
Joonyoung Shim578b6062012-04-05 20:49:26 +0900156 if (mixer_ops && mixer_ops->disable_vblank)
157 return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900158}
159
Inki Dae1de425b2012-03-16 18:47:04 +0900160static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
161 struct drm_connector *connector,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200162 const struct drm_display_mode *mode,
Inki Dae1de425b2012-03-16 18:47:04 +0900163 struct drm_display_mode *adjusted_mode)
164{
165 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
166
167 DRM_DEBUG_KMS("%s\n", __FILE__);
168
Joonyoung Shim578b6062012-04-05 20:49:26 +0900169 if (hdmi_ops && hdmi_ops->mode_fixup)
170 hdmi_ops->mode_fixup(ctx->hdmi_ctx->ctx, connector, mode,
171 adjusted_mode);
Inki Dae1de425b2012-03-16 18:47:04 +0900172}
173
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900174static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
175{
176 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
177
178 DRM_DEBUG_KMS("%s\n", __FILE__);
179
Joonyoung Shim578b6062012-04-05 20:49:26 +0900180 if (hdmi_ops && hdmi_ops->mode_set)
181 hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900182}
183
Inki Dae1de425b2012-03-16 18:47:04 +0900184static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
185 unsigned int *width, unsigned int *height)
186{
187 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
188
189 DRM_DEBUG_KMS("%s\n", __FILE__);
190
Joonyoung Shim578b6062012-04-05 20:49:26 +0900191 if (hdmi_ops && hdmi_ops->get_max_resol)
192 hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
Inki Dae1de425b2012-03-16 18:47:04 +0900193}
194
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900195static void drm_hdmi_commit(struct device *subdrv_dev)
196{
197 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
198
199 DRM_DEBUG_KMS("%s\n", __FILE__);
200
Joonyoung Shim578b6062012-04-05 20:49:26 +0900201 if (hdmi_ops && hdmi_ops->commit)
202 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900203}
204
205static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
206{
207 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
208
209 DRM_DEBUG_KMS("%s\n", __FILE__);
210
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900211 if (mixer_ops && mixer_ops->dpms)
212 mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
213
214 if (hdmi_ops && hdmi_ops->dpms)
215 hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
216}
217
218static void drm_hdmi_apply(struct device *subdrv_dev)
219{
220 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
221 int i;
222
223 DRM_DEBUG_KMS("%s\n", __FILE__);
224
225 for (i = 0; i < MIXER_WIN_NR; i++) {
226 if (!ctx->enabled[i])
227 continue;
228 if (mixer_ops && mixer_ops->win_commit)
229 mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900230 }
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900231
232 if (hdmi_ops && hdmi_ops->commit)
233 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900234}
235
236static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
237 .dpms = drm_hdmi_dpms,
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900238 .apply = drm_hdmi_apply,
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900239 .enable_vblank = drm_hdmi_enable_vblank,
240 .disable_vblank = drm_hdmi_disable_vblank,
Inki Dae1de425b2012-03-16 18:47:04 +0900241 .mode_fixup = drm_hdmi_mode_fixup,
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900242 .mode_set = drm_hdmi_mode_set,
Inki Dae1de425b2012-03-16 18:47:04 +0900243 .get_max_resol = drm_hdmi_get_max_resol,
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900244 .commit = drm_hdmi_commit,
245};
246
247static void drm_mixer_mode_set(struct device *subdrv_dev,
248 struct exynos_drm_overlay *overlay)
249{
250 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
251
252 DRM_DEBUG_KMS("%s\n", __FILE__);
253
Joonyoung Shim578b6062012-04-05 20:49:26 +0900254 if (mixer_ops && mixer_ops->win_mode_set)
255 mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900256}
257
258static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
259{
260 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900261 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900262
263 DRM_DEBUG_KMS("%s\n", __FILE__);
264
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900265 if (win < 0 || win > MIXER_WIN_NR) {
266 DRM_ERROR("mixer window[%d] is wrong\n", win);
267 return;
268 }
269
Joonyoung Shim578b6062012-04-05 20:49:26 +0900270 if (mixer_ops && mixer_ops->win_commit)
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900271 mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
272
273 ctx->enabled[win] = true;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900274}
275
276static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
277{
278 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900279 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900280
281 DRM_DEBUG_KMS("%s\n", __FILE__);
282
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900283 if (win < 0 || win > MIXER_WIN_NR) {
284 DRM_ERROR("mixer window[%d] is wrong\n", win);
285 return;
286 }
287
Joonyoung Shim578b6062012-04-05 20:49:26 +0900288 if (mixer_ops && mixer_ops->win_disable)
Joonyoung Shimcf8fc4f2012-04-23 19:35:50 +0900289 mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
290
291 ctx->enabled[win] = false;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900292}
293
Inki Dae3d058592012-08-17 18:35:47 +0900294static void drm_mixer_wait_for_vblank(struct device *subdrv_dev)
295{
296 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
297
298 DRM_DEBUG_KMS("%s\n", __FILE__);
299
300 if (mixer_ops && mixer_ops->wait_for_vblank)
301 mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
302}
303
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900304static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
305 .mode_set = drm_mixer_mode_set,
306 .commit = drm_mixer_commit,
307 .disable = drm_mixer_disable,
Inki Dae3d058592012-08-17 18:35:47 +0900308 .wait_for_vblank = drm_mixer_wait_for_vblank,
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900309};
310
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900311static struct exynos_drm_manager hdmi_manager = {
312 .pipe = -1,
313 .ops = &drm_hdmi_manager_ops,
314 .overlay_ops = &drm_hdmi_overlay_ops,
315 .display_ops = &drm_hdmi_display_ops,
316};
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900317
318static int hdmi_subdrv_probe(struct drm_device *drm_dev,
319 struct device *dev)
320{
321 struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
322 struct drm_hdmi_context *ctx;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900323
324 DRM_DEBUG_KMS("%s\n", __FILE__);
325
Rahul Sharma768c3052012-10-04 20:48:56 +0530326 if (!hdmi_ctx) {
327 DRM_ERROR("hdmi context not initialized.\n");
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900328 return -EFAULT;
329 }
330
Rahul Sharma768c3052012-10-04 20:48:56 +0530331 if (!mixer_ctx) {
332 DRM_ERROR("mixer context not initialized.\n");
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900333 return -EFAULT;
334 }
335
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900336 ctx = get_ctx_from_subdrv(subdrv);
337
Rahul Sharma768c3052012-10-04 20:48:56 +0530338 if (!ctx) {
339 DRM_ERROR("no drm hdmi context.\n");
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900340 return -EFAULT;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900341 }
342
Rahul Sharma768c3052012-10-04 20:48:56 +0530343 ctx->hdmi_ctx = hdmi_ctx;
344 ctx->mixer_ctx = mixer_ctx;
345
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900346 ctx->hdmi_ctx->drm_dev = drm_dev;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900347 ctx->mixer_ctx->drm_dev = drm_dev;
348
349 return 0;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900350}
351
352static int __devinit exynos_drm_hdmi_probe(struct platform_device *pdev)
353{
354 struct device *dev = &pdev->dev;
355 struct exynos_drm_subdrv *subdrv;
356 struct drm_hdmi_context *ctx;
357
358 DRM_DEBUG_KMS("%s\n", __FILE__);
359
Sachin Kamatae182942012-08-06 12:16:19 +0530360 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900361 if (!ctx) {
362 DRM_LOG_KMS("failed to alloc common hdmi context.\n");
363 return -ENOMEM;
364 }
365
366 subdrv = &ctx->subdrv;
367
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900368 subdrv->dev = dev;
369 subdrv->manager = &hdmi_manager;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900370 subdrv->probe = hdmi_subdrv_probe;
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900371
372 platform_set_drvdata(pdev, subdrv);
373
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900374 exynos_drm_subdrv_register(subdrv);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900375
376 return 0;
377}
378
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900379static int __devexit exynos_drm_hdmi_remove(struct platform_device *pdev)
380{
381 struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
382
383 DRM_DEBUG_KMS("%s\n", __FILE__);
384
385 exynos_drm_subdrv_unregister(&ctx->subdrv);
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900386
387 return 0;
388}
389
Joonyoung Shim132a5b92012-03-16 18:47:08 +0900390struct platform_driver exynos_drm_common_hdmi_driver = {
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900391 .probe = exynos_drm_hdmi_probe,
392 .remove = __devexit_p(exynos_drm_hdmi_remove),
393 .driver = {
394 .name = "exynos-drm-hdmi",
395 .owner = THIS_MODULE,
Seung-Woo Kimd8408322011-12-21 17:39:39 +0900396 },
397};