1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Traphandler
4 * Copyright (C) 2014 Free Electrons
5 * Copyright (C) 2014 Atmel
11 #include <linux/of_graph.h>
14 #include <drm/drm_of.h>
15 #include <drm/drm_bridge.h>
17 #include "atmel_hlcdc_dc.h"
19 struct atmel_hlcdc_rgb_output {
20 struct drm_encoder encoder;
24 static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
25 .destroy = drm_encoder_cleanup,
28 static struct atmel_hlcdc_rgb_output *
29 atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)
31 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
34 int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)
36 struct atmel_hlcdc_rgb_output *output;
38 output = atmel_hlcdc_encoder_to_rgb_output(encoder);
40 return output->bus_fmt;
43 static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)
48 ret = of_property_read_u32(ep, "bus-width", &bus_width);
56 return MEDIA_BUS_FMT_RGB444_1X12;
58 return MEDIA_BUS_FMT_RGB565_1X16;
60 return MEDIA_BUS_FMT_RGB666_1X18;
62 return MEDIA_BUS_FMT_RGB888_1X24;
68 static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)
70 struct atmel_hlcdc_rgb_output *output;
71 struct device_node *ep;
72 struct drm_panel *panel;
73 struct drm_bridge *bridge;
76 ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);
80 ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 0, endpoint,
87 output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
93 output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);
95 if (output->bus_fmt < 0) {
96 dev_err(dev->dev, "endpoint %d: invalid bus width\n", endpoint);
100 ret = drm_encoder_init(dev, &output->encoder,
101 &atmel_hlcdc_panel_encoder_funcs,
102 DRM_MODE_ENCODER_NONE, NULL);
106 output->encoder.possible_crtcs = 0x1;
109 bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_Unknown);
111 return PTR_ERR(bridge);
115 ret = drm_bridge_attach(&output->encoder, bridge, NULL);
120 drm_panel_bridge_remove(bridge);
123 drm_encoder_cleanup(&output->encoder);
128 int atmel_hlcdc_create_outputs(struct drm_device *dev)
130 int endpoint, ret = 0;
134 * Always scan the first few endpoints even if we get -ENODEV,
135 * but keep going after that as long as we keep getting hits.
137 for (endpoint = 0; !ret || endpoint < 4; endpoint++) {
138 ret = atmel_hlcdc_attach_endpoint(dev, endpoint);
146 /* At least one device was successfully attached.*/
147 if (ret == -ENODEV && attached)