2 * Copyright (C) 2014 Traphandler
3 * Copyright (C) 2014 Free Electrons
4 * Copyright (C) 2014 Atmel
6 * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
7 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/of_graph.h>
25 #include <drm/drm_panel.h>
27 #include "atmel_hlcdc_dc.h"
30 * Atmel HLCDC RGB output mode
32 enum atmel_hlcdc_connector_rgb_mode {
33 ATMEL_HLCDC_CONNECTOR_RGB444,
34 ATMEL_HLCDC_CONNECTOR_RGB565,
35 ATMEL_HLCDC_CONNECTOR_RGB666,
36 ATMEL_HLCDC_CONNECTOR_RGB888,
40 * Atmel HLCDC RGB connector structure
42 * This structure stores RGB slave device information.
44 * @connector: DRM connector
45 * @encoder: DRM encoder
46 * @dc: pointer to the atmel_hlcdc_dc structure
47 * @dpms: current DPMS mode
49 struct atmel_hlcdc_rgb_output {
50 struct drm_connector connector;
51 struct drm_encoder encoder;
52 struct atmel_hlcdc_dc *dc;
56 static inline struct atmel_hlcdc_rgb_output *
57 drm_connector_to_atmel_hlcdc_rgb_output(struct drm_connector *connector)
59 return container_of(connector, struct atmel_hlcdc_rgb_output,
63 static inline struct atmel_hlcdc_rgb_output *
64 drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder *encoder)
66 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
70 * Atmel HLCDC Panel device structure
72 * This structure is specialization of the slave device structure to
73 * interface with drm panels.
75 * @base: base slave device fields
76 * @panel: drm panel attached to this slave device
78 struct atmel_hlcdc_panel {
79 struct atmel_hlcdc_rgb_output base;
80 struct drm_panel *panel;
83 static inline struct atmel_hlcdc_panel *
84 atmel_hlcdc_rgb_output_to_panel(struct atmel_hlcdc_rgb_output *output)
86 return container_of(output, struct atmel_hlcdc_panel, base);
89 static void atmel_hlcdc_panel_encoder_dpms(struct drm_encoder *encoder,
92 struct atmel_hlcdc_rgb_output *rgb =
93 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
94 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
96 if (mode != DRM_MODE_DPMS_ON)
97 mode = DRM_MODE_DPMS_OFF;
99 if (mode == rgb->dpms)
102 if (mode != DRM_MODE_DPMS_ON)
103 drm_panel_disable(panel->panel);
105 drm_panel_enable(panel->panel);
111 atmel_hlcdc_panel_encoder_mode_fixup(struct drm_encoder *encoder,
112 const struct drm_display_mode *mode,
113 struct drm_display_mode *adjusted)
118 static void atmel_hlcdc_panel_encoder_prepare(struct drm_encoder *encoder)
120 atmel_hlcdc_panel_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
123 static void atmel_hlcdc_panel_encoder_commit(struct drm_encoder *encoder)
125 atmel_hlcdc_panel_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
129 atmel_hlcdc_rgb_encoder_mode_set(struct drm_encoder *encoder,
130 struct drm_display_mode *mode,
131 struct drm_display_mode *adjusted)
133 struct atmel_hlcdc_rgb_output *rgb =
134 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
135 struct drm_display_info *info = &rgb->connector.display_info;
140 if (info->num_bus_formats) {
141 switch (info->bus_formats[0]) {
142 case MEDIA_BUS_FMT_RGB666_1X18:
143 cfg |= ATMEL_HLCDC_CONNECTOR_RGB666 << 8;
145 case MEDIA_BUS_FMT_RGB888_1X24:
146 cfg |= ATMEL_HLCDC_CONNECTOR_RGB888 << 8;
153 regmap_update_bits(rgb->dc->hlcdc->regmap, ATMEL_HLCDC_CFG(5),
154 ATMEL_HLCDC_MODE_MASK,
158 static struct drm_encoder_helper_funcs atmel_hlcdc_panel_encoder_helper_funcs = {
159 .dpms = atmel_hlcdc_panel_encoder_dpms,
160 .mode_fixup = atmel_hlcdc_panel_encoder_mode_fixup,
161 .prepare = atmel_hlcdc_panel_encoder_prepare,
162 .commit = atmel_hlcdc_panel_encoder_commit,
163 .mode_set = atmel_hlcdc_rgb_encoder_mode_set,
166 static void atmel_hlcdc_rgb_encoder_destroy(struct drm_encoder *encoder)
168 drm_encoder_cleanup(encoder);
169 memset(encoder, 0, sizeof(*encoder));
172 static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
173 .destroy = atmel_hlcdc_rgb_encoder_destroy,
176 static int atmel_hlcdc_panel_get_modes(struct drm_connector *connector)
178 struct atmel_hlcdc_rgb_output *rgb =
179 drm_connector_to_atmel_hlcdc_rgb_output(connector);
180 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
182 return panel->panel->funcs->get_modes(panel->panel);
185 static int atmel_hlcdc_rgb_mode_valid(struct drm_connector *connector,
186 struct drm_display_mode *mode)
188 struct atmel_hlcdc_rgb_output *rgb =
189 drm_connector_to_atmel_hlcdc_rgb_output(connector);
191 return atmel_hlcdc_dc_mode_valid(rgb->dc, mode);
196 static struct drm_encoder *
197 atmel_hlcdc_rgb_best_encoder(struct drm_connector *connector)
199 struct atmel_hlcdc_rgb_output *rgb =
200 drm_connector_to_atmel_hlcdc_rgb_output(connector);
202 return &rgb->encoder;
205 static struct drm_connector_helper_funcs atmel_hlcdc_panel_connector_helper_funcs = {
206 .get_modes = atmel_hlcdc_panel_get_modes,
207 .mode_valid = atmel_hlcdc_rgb_mode_valid,
208 .best_encoder = atmel_hlcdc_rgb_best_encoder,
211 static enum drm_connector_status
212 atmel_hlcdc_panel_connector_detect(struct drm_connector *connector, bool force)
214 return connector_status_connected;
218 atmel_hlcdc_panel_connector_destroy(struct drm_connector *connector)
220 struct atmel_hlcdc_rgb_output *rgb =
221 drm_connector_to_atmel_hlcdc_rgb_output(connector);
222 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
224 drm_panel_detach(panel->panel);
225 drm_connector_cleanup(connector);
228 static const struct drm_connector_funcs atmel_hlcdc_panel_connector_funcs = {
229 .dpms = drm_helper_connector_dpms,
230 .detect = atmel_hlcdc_panel_connector_detect,
231 .fill_modes = drm_helper_probe_single_connector_modes,
232 .destroy = atmel_hlcdc_panel_connector_destroy,
235 static int atmel_hlcdc_create_panel_output(struct drm_device *dev,
236 struct of_endpoint *ep)
238 struct atmel_hlcdc_dc *dc = dev->dev_private;
239 struct device_node *np;
240 struct drm_panel *p = NULL;
241 struct atmel_hlcdc_panel *panel;
244 np = of_graph_get_remote_port_parent(ep->local_node);
248 p = of_drm_find_panel(np);
252 return -EPROBE_DEFER;
254 panel = devm_kzalloc(dev->dev, sizeof(*panel), GFP_KERNEL);
258 panel->base.dpms = DRM_MODE_DPMS_OFF;
262 drm_encoder_helper_add(&panel->base.encoder,
263 &atmel_hlcdc_panel_encoder_helper_funcs);
264 ret = drm_encoder_init(dev, &panel->base.encoder,
265 &atmel_hlcdc_panel_encoder_funcs,
266 DRM_MODE_ENCODER_LVDS);
270 panel->base.connector.dpms = DRM_MODE_DPMS_OFF;
271 panel->base.connector.polled = DRM_CONNECTOR_POLL_CONNECT;
272 drm_connector_helper_add(&panel->base.connector,
273 &atmel_hlcdc_panel_connector_helper_funcs);
274 ret = drm_connector_init(dev, &panel->base.connector,
275 &atmel_hlcdc_panel_connector_funcs,
276 DRM_MODE_CONNECTOR_LVDS);
278 goto err_encoder_cleanup;
280 drm_mode_connector_attach_encoder(&panel->base.connector,
281 &panel->base.encoder);
282 panel->base.encoder.possible_crtcs = 0x1;
284 drm_panel_attach(p, &panel->base.connector);
290 drm_encoder_cleanup(&panel->base.encoder);
295 int atmel_hlcdc_create_outputs(struct drm_device *dev)
297 struct device_node *port_np, *np;
298 struct of_endpoint ep;
301 port_np = of_get_child_by_name(dev->dev->of_node, "port");
305 np = of_get_child_by_name(port_np, "endpoint");
306 of_node_put(port_np);
311 ret = of_graph_parse_endpoint(np, &ep);
312 of_node_put(port_np);
317 /* We currently only support panel output */
318 return atmel_hlcdc_create_panel_output(dev, &ep);