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_hdmicon.h"
23 #include "rcar_du_hdmienc.h"
24 #include "rcar_du_kms.h"
25 #include "rcar_du_lvdscon.h"
26 #include "rcar_du_lvdsenc.h"
27 #include "rcar_du_vgacon.h"
29 /* -----------------------------------------------------------------------------
30 * Common connector functions
34 rcar_du_connector_best_encoder(struct drm_connector
*connector
)
36 struct rcar_du_connector
*rcon
= to_rcar_connector(connector
);
38 return rcar_encoder_to_drm_encoder(rcon
->encoder
);
41 /* -----------------------------------------------------------------------------
45 static void rcar_du_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
47 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
49 if (mode
!= DRM_MODE_DPMS_ON
)
50 mode
= DRM_MODE_DPMS_OFF
;
53 rcar_du_lvdsenc_dpms(renc
->lvds
, encoder
->crtc
, mode
);
56 static bool rcar_du_encoder_mode_fixup(struct drm_encoder
*encoder
,
57 const struct drm_display_mode
*mode
,
58 struct drm_display_mode
*adjusted_mode
)
60 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
61 const struct drm_display_mode
*panel_mode
;
62 struct drm_device
*dev
= encoder
->dev
;
63 struct drm_connector
*connector
;
66 /* DAC encoders have currently no restriction on the mode. */
67 if (encoder
->encoder_type
== DRM_MODE_ENCODER_DAC
)
70 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
71 if (connector
->encoder
== encoder
) {
78 dev_dbg(dev
->dev
, "mode_fixup: no connector found\n");
82 if (list_empty(&connector
->modes
)) {
83 dev_dbg(dev
->dev
, "mode_fixup: empty modes list\n");
87 panel_mode
= list_first_entry(&connector
->modes
,
88 struct drm_display_mode
, head
);
90 /* We're not allowed to modify the resolution. */
91 if (mode
->hdisplay
!= panel_mode
->hdisplay
||
92 mode
->vdisplay
!= panel_mode
->vdisplay
)
95 /* The flat panel mode is fixed, just copy it to the adjusted mode. */
96 drm_mode_copy(adjusted_mode
, panel_mode
);
98 /* The internal LVDS encoder has a clock frequency operating range of
99 * 30MHz to 150MHz. Clamp the clock accordingly.
102 adjusted_mode
->clock
= clamp(adjusted_mode
->clock
,
108 static void rcar_du_encoder_mode_prepare(struct drm_encoder
*encoder
)
110 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
113 rcar_du_lvdsenc_dpms(renc
->lvds
, encoder
->crtc
,
117 static void rcar_du_encoder_mode_commit(struct drm_encoder
*encoder
)
119 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
122 rcar_du_lvdsenc_dpms(renc
->lvds
, encoder
->crtc
,
126 static void rcar_du_encoder_mode_set(struct drm_encoder
*encoder
,
127 struct drm_display_mode
*mode
,
128 struct drm_display_mode
*adjusted_mode
)
130 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
132 rcar_du_crtc_route_output(encoder
->crtc
, renc
->output
);
135 static const struct drm_encoder_helper_funcs encoder_helper_funcs
= {
136 .dpms
= rcar_du_encoder_dpms
,
137 .mode_fixup
= rcar_du_encoder_mode_fixup
,
138 .prepare
= rcar_du_encoder_mode_prepare
,
139 .commit
= rcar_du_encoder_mode_commit
,
140 .mode_set
= rcar_du_encoder_mode_set
,
143 static const struct drm_encoder_funcs encoder_funcs
= {
144 .destroy
= drm_encoder_cleanup
,
147 int rcar_du_encoder_init(struct rcar_du_device
*rcdu
,
148 enum rcar_du_encoder_type type
,
149 enum rcar_du_output output
,
150 struct device_node
*enc_node
,
151 struct device_node
*con_node
)
153 struct rcar_du_encoder
*renc
;
154 struct drm_encoder
*encoder
;
155 unsigned int encoder_type
;
158 renc
= devm_kzalloc(rcdu
->dev
, sizeof(*renc
), GFP_KERNEL
);
162 renc
->output
= output
;
163 encoder
= rcar_encoder_to_drm_encoder(renc
);
166 case RCAR_DU_OUTPUT_LVDS0
:
167 renc
->lvds
= rcdu
->lvds
[0];
170 case RCAR_DU_OUTPUT_LVDS1
:
171 renc
->lvds
= rcdu
->lvds
[1];
179 case RCAR_DU_ENCODER_VGA
:
180 encoder_type
= DRM_MODE_ENCODER_DAC
;
182 case RCAR_DU_ENCODER_LVDS
:
183 encoder_type
= DRM_MODE_ENCODER_LVDS
;
185 case RCAR_DU_ENCODER_HDMI
:
186 encoder_type
= DRM_MODE_ENCODER_TMDS
;
188 case RCAR_DU_ENCODER_NONE
:
190 /* No external encoder, use the internal encoder type. */
191 encoder_type
= rcdu
->info
->routes
[output
].encoder_type
;
195 if (type
== RCAR_DU_ENCODER_HDMI
) {
196 ret
= rcar_du_hdmienc_init(rcdu
, renc
, enc_node
);
200 ret
= drm_encoder_init(rcdu
->ddev
, encoder
, &encoder_funcs
,
205 drm_encoder_helper_add(encoder
, &encoder_helper_funcs
);
208 switch (encoder_type
) {
209 case DRM_MODE_ENCODER_LVDS
:
210 ret
= rcar_du_lvds_connector_init(rcdu
, renc
, con_node
);
213 case DRM_MODE_ENCODER_DAC
:
214 ret
= rcar_du_vga_connector_init(rcdu
, renc
);
217 case DRM_MODE_ENCODER_TMDS
:
218 ret
= rcar_du_hdmi_connector_init(rcdu
, renc
);
229 encoder
->funcs
->destroy(encoder
);
230 devm_kfree(rcdu
->dev
, renc
);