2 * rcar_du_encoder.c -- R-Car Display Unit Encoder
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/export.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_crtc_helper.h>
20 #include "rcar_du_drv.h"
21 #include "rcar_du_encoder.h"
22 #include "rcar_du_hdmienc.h"
23 #include "rcar_du_kms.h"
24 #include "rcar_du_lvdscon.h"
25 #include "rcar_du_lvdsenc.h"
26 #include "rcar_du_vgacon.h"
28 /* -----------------------------------------------------------------------------
32 static void rcar_du_encoder_disable(struct drm_encoder
*encoder
)
34 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
37 rcar_du_lvdsenc_enable(renc
->lvds
, encoder
->crtc
, false);
40 static void rcar_du_encoder_enable(struct drm_encoder
*encoder
)
42 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
45 rcar_du_lvdsenc_enable(renc
->lvds
, encoder
->crtc
, true);
48 static int rcar_du_encoder_atomic_check(struct drm_encoder
*encoder
,
49 struct drm_crtc_state
*crtc_state
,
50 struct drm_connector_state
*conn_state
)
52 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
53 struct drm_display_mode
*adjusted_mode
= &crtc_state
->adjusted_mode
;
54 const struct drm_display_mode
*mode
= &crtc_state
->mode
;
55 const struct drm_display_mode
*panel_mode
;
56 struct drm_connector
*connector
= conn_state
->connector
;
57 struct drm_device
*dev
= encoder
->dev
;
59 /* DAC encoders have currently no restriction on the mode. */
60 if (encoder
->encoder_type
== DRM_MODE_ENCODER_DAC
)
63 if (list_empty(&connector
->modes
)) {
64 dev_dbg(dev
->dev
, "encoder: empty modes list\n");
68 panel_mode
= list_first_entry(&connector
->modes
,
69 struct drm_display_mode
, head
);
71 /* We're not allowed to modify the resolution. */
72 if (mode
->hdisplay
!= panel_mode
->hdisplay
||
73 mode
->vdisplay
!= panel_mode
->vdisplay
)
76 /* The flat panel mode is fixed, just copy it to the adjusted mode. */
77 drm_mode_copy(adjusted_mode
, panel_mode
);
80 rcar_du_lvdsenc_atomic_check(renc
->lvds
, adjusted_mode
);
85 static void rcar_du_encoder_mode_set(struct drm_encoder
*encoder
,
86 struct drm_display_mode
*mode
,
87 struct drm_display_mode
*adjusted_mode
)
89 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
91 rcar_du_crtc_route_output(encoder
->crtc
, renc
->output
);
94 static const struct drm_encoder_helper_funcs encoder_helper_funcs
= {
95 .mode_set
= rcar_du_encoder_mode_set
,
96 .disable
= rcar_du_encoder_disable
,
97 .enable
= rcar_du_encoder_enable
,
98 .atomic_check
= rcar_du_encoder_atomic_check
,
101 static const struct drm_encoder_funcs encoder_funcs
= {
102 .destroy
= drm_encoder_cleanup
,
105 int rcar_du_encoder_init(struct rcar_du_device
*rcdu
,
106 enum rcar_du_encoder_type type
,
107 enum rcar_du_output output
,
108 struct device_node
*enc_node
,
109 struct device_node
*con_node
)
111 struct rcar_du_encoder
*renc
;
112 struct drm_encoder
*encoder
;
113 unsigned int encoder_type
;
116 renc
= devm_kzalloc(rcdu
->dev
, sizeof(*renc
), GFP_KERNEL
);
120 renc
->output
= output
;
121 encoder
= rcar_encoder_to_drm_encoder(renc
);
124 case RCAR_DU_OUTPUT_LVDS0
:
125 renc
->lvds
= rcdu
->lvds
[0];
128 case RCAR_DU_OUTPUT_LVDS1
:
129 renc
->lvds
= rcdu
->lvds
[1];
137 case RCAR_DU_ENCODER_VGA
:
138 encoder_type
= DRM_MODE_ENCODER_DAC
;
140 case RCAR_DU_ENCODER_LVDS
:
141 encoder_type
= DRM_MODE_ENCODER_LVDS
;
143 case RCAR_DU_ENCODER_HDMI
:
144 encoder_type
= DRM_MODE_ENCODER_TMDS
;
146 case RCAR_DU_ENCODER_NONE
:
148 /* No external encoder, use the internal encoder type. */
149 encoder_type
= rcdu
->info
->routes
[output
].encoder_type
;
153 if (type
== RCAR_DU_ENCODER_HDMI
) {
154 ret
= rcar_du_hdmienc_init(rcdu
, renc
, enc_node
);
158 ret
= drm_encoder_init(rcdu
->ddev
, encoder
, &encoder_funcs
,
163 drm_encoder_helper_add(encoder
, &encoder_helper_funcs
);
166 switch (encoder_type
) {
167 case DRM_MODE_ENCODER_LVDS
:
168 ret
= rcar_du_lvds_connector_init(rcdu
, renc
, con_node
);
171 case DRM_MODE_ENCODER_DAC
:
172 ret
= rcar_du_vga_connector_init(rcdu
, renc
);
175 case DRM_MODE_ENCODER_TMDS
:
176 /* connector managed by the bridge driver */
187 encoder
->funcs
->destroy(encoder
);
188 devm_kfree(rcdu
->dev
, renc
);