2 * Copyright (C) 2014 Traphandler
3 * Copyright (C) 2014 Free Electrons
4 * Copyright (C) 2014 Atmel
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_of.h>
26 #include <drm/drm_bridge.h>
28 #include "atmel_hlcdc_dc.h"
30 struct atmel_hlcdc_rgb_output {
31 struct drm_encoder encoder;
35 static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
36 .destroy = drm_encoder_cleanup,
39 static struct atmel_hlcdc_rgb_output *
40 atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)
42 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
45 int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)
47 struct atmel_hlcdc_rgb_output *output;
49 output = atmel_hlcdc_encoder_to_rgb_output(encoder);
51 return output->bus_fmt;
54 static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)
59 ret = of_property_read_u32(ep, "bus-width", &bus_width);
67 return MEDIA_BUS_FMT_RGB444_1X12;
69 return MEDIA_BUS_FMT_RGB565_1X16;
71 return MEDIA_BUS_FMT_RGB666_1X18;
73 return MEDIA_BUS_FMT_RGB888_1X24;
79 static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)
81 struct atmel_hlcdc_rgb_output *output;
82 struct device_node *ep;
83 struct drm_panel *panel;
84 struct drm_bridge *bridge;
87 ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);
91 ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 0, endpoint,
98 output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
104 output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);
106 if (output->bus_fmt < 0) {
107 dev_err(dev->dev, "endpoint %d: invalid bus width\n", endpoint);
111 ret = drm_encoder_init(dev, &output->encoder,
112 &atmel_hlcdc_panel_encoder_funcs,
113 DRM_MODE_ENCODER_NONE, NULL);
117 output->encoder.possible_crtcs = 0x1;
120 bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_Unknown);
122 return PTR_ERR(bridge);
126 ret = drm_bridge_attach(&output->encoder, bridge, NULL);
131 drm_panel_bridge_remove(bridge);
134 drm_encoder_cleanup(&output->encoder);
139 int atmel_hlcdc_create_outputs(struct drm_device *dev)
141 int endpoint, ret = 0;
145 * Always scan the first few endpoints even if we get -ENODEV,
146 * but keep going after that as long as we keep getting hits.
148 for (endpoint = 0; !ret || endpoint < 4; endpoint++) {
149 ret = atmel_hlcdc_attach_endpoint(dev, endpoint);
157 /* At least one device was successfully attached.*/
158 if (ret == -ENODEV && attached)