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_kms.h"
23 #include "rcar_du_lvdscon.h"
24 #include "rcar_du_lvdsenc.h"
25 #include "rcar_du_vgacon.h"
27 /* -----------------------------------------------------------------------------
28 * Common connector functions
32 rcar_du_connector_best_encoder(struct drm_connector
*connector
)
34 struct rcar_du_connector
*rcon
= to_rcar_connector(connector
);
36 return &rcon
->encoder
->encoder
;
39 /* -----------------------------------------------------------------------------
43 static void rcar_du_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
45 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
48 rcar_du_lvdsenc_dpms(renc
->lvds
, encoder
->crtc
, mode
);
51 static bool rcar_du_encoder_mode_fixup(struct drm_encoder
*encoder
,
52 const struct drm_display_mode
*mode
,
53 struct drm_display_mode
*adjusted_mode
)
55 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
56 const struct drm_display_mode
*panel_mode
;
57 struct drm_device
*dev
= encoder
->dev
;
58 struct drm_connector
*connector
;
61 /* DAC encoders have currently no restriction on the mode. */
62 if (encoder
->encoder_type
== DRM_MODE_ENCODER_DAC
)
65 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
66 if (connector
->encoder
== encoder
) {
73 dev_dbg(dev
->dev
, "mode_fixup: no connector found\n");
77 if (list_empty(&connector
->modes
)) {
78 dev_dbg(dev
->dev
, "mode_fixup: empty modes list\n");
82 panel_mode
= list_first_entry(&connector
->modes
,
83 struct drm_display_mode
, head
);
85 /* We're not allowed to modify the resolution. */
86 if (mode
->hdisplay
!= panel_mode
->hdisplay
||
87 mode
->vdisplay
!= panel_mode
->vdisplay
)
90 /* The flat panel mode is fixed, just copy it to the adjusted mode. */
91 drm_mode_copy(adjusted_mode
, panel_mode
);
93 /* The internal LVDS encoder has a clock frequency operating range of
94 * 30MHz to 150MHz. Clamp the clock accordingly.
97 adjusted_mode
->clock
= clamp(adjusted_mode
->clock
,
103 static void rcar_du_encoder_mode_prepare(struct drm_encoder
*encoder
)
105 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
108 rcar_du_lvdsenc_dpms(renc
->lvds
, encoder
->crtc
,
112 static void rcar_du_encoder_mode_commit(struct drm_encoder
*encoder
)
114 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
117 rcar_du_lvdsenc_dpms(renc
->lvds
, encoder
->crtc
,
121 static void rcar_du_encoder_mode_set(struct drm_encoder
*encoder
,
122 struct drm_display_mode
*mode
,
123 struct drm_display_mode
*adjusted_mode
)
125 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
127 rcar_du_crtc_route_output(encoder
->crtc
, renc
->output
);
130 static const struct drm_encoder_helper_funcs encoder_helper_funcs
= {
131 .dpms
= rcar_du_encoder_dpms
,
132 .mode_fixup
= rcar_du_encoder_mode_fixup
,
133 .prepare
= rcar_du_encoder_mode_prepare
,
134 .commit
= rcar_du_encoder_mode_commit
,
135 .mode_set
= rcar_du_encoder_mode_set
,
138 static const struct drm_encoder_funcs encoder_funcs
= {
139 .destroy
= drm_encoder_cleanup
,
142 int rcar_du_encoder_init(struct rcar_du_device
*rcdu
,
143 enum rcar_du_encoder_type type
,
144 enum rcar_du_output output
,
145 const struct rcar_du_encoder_data
*data
,
146 struct device_node
*np
)
148 struct rcar_du_encoder
*renc
;
149 unsigned int encoder_type
;
152 renc
= devm_kzalloc(rcdu
->dev
, sizeof(*renc
), GFP_KERNEL
);
156 renc
->output
= output
;
159 case RCAR_DU_OUTPUT_LVDS0
:
160 renc
->lvds
= rcdu
->lvds
[0];
163 case RCAR_DU_OUTPUT_LVDS1
:
164 renc
->lvds
= rcdu
->lvds
[1];
172 case RCAR_DU_ENCODER_VGA
:
173 encoder_type
= DRM_MODE_ENCODER_DAC
;
175 case RCAR_DU_ENCODER_LVDS
:
176 encoder_type
= DRM_MODE_ENCODER_LVDS
;
178 case RCAR_DU_ENCODER_NONE
:
180 /* No external encoder, use the internal encoder type. */
181 encoder_type
= rcdu
->info
->routes
[output
].encoder_type
;
185 ret
= drm_encoder_init(rcdu
->ddev
, &renc
->encoder
, &encoder_funcs
,
190 drm_encoder_helper_add(&renc
->encoder
, &encoder_helper_funcs
);
192 switch (encoder_type
) {
193 case DRM_MODE_ENCODER_LVDS
: {
194 const struct rcar_du_panel_data
*pdata
=
195 data
? &data
->connector
.lvds
.panel
: NULL
;
196 return rcar_du_lvds_connector_init(rcdu
, renc
, pdata
, np
);
199 case DRM_MODE_ENCODER_DAC
:
200 return rcar_du_vga_connector_init(rcdu
, renc
);