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/media-bus-format.h>
12 #include <linux/of_graph.h>
14 #include <drm/drm_encoder.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_bridge.h>
18 #include "atmel_hlcdc_dc.h"
20 struct atmel_hlcdc_rgb_output {
21 struct drm_encoder encoder;
25 static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
26 .destroy = drm_encoder_cleanup,
29 static struct atmel_hlcdc_rgb_output *
30 atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)
32 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
35 int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)
37 struct atmel_hlcdc_rgb_output *output;
39 output = atmel_hlcdc_encoder_to_rgb_output(encoder);
41 return output->bus_fmt;
44 static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)
49 ret = of_property_read_u32(ep, "bus-width", &bus_width);
57 return MEDIA_BUS_FMT_RGB444_1X12;
59 return MEDIA_BUS_FMT_RGB565_1X16;
61 return MEDIA_BUS_FMT_RGB666_1X18;
63 return MEDIA_BUS_FMT_RGB888_1X24;
69 static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)
71 struct atmel_hlcdc_rgb_output *output;
72 struct device_node *ep;
73 struct drm_panel *panel;
74 struct drm_bridge *bridge;
77 ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);
81 ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 0, endpoint,
88 output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
94 output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);
96 if (output->bus_fmt < 0) {
97 dev_err(dev->dev, "endpoint %d: invalid bus width\n", endpoint);
101 ret = drm_encoder_init(dev, &output->encoder,
102 &atmel_hlcdc_panel_encoder_funcs,
103 DRM_MODE_ENCODER_NONE, NULL);
107 output->encoder.possible_crtcs = 0x1;
110 bridge = drm_panel_bridge_add_typed(panel,
111 DRM_MODE_CONNECTOR_Unknown);
113 return PTR_ERR(bridge);
117 ret = drm_bridge_attach(&output->encoder, bridge, NULL);
122 drm_panel_bridge_remove(bridge);
125 drm_encoder_cleanup(&output->encoder);
130 int atmel_hlcdc_create_outputs(struct drm_device *dev)
132 int endpoint, ret = 0;
136 * Always scan the first few endpoints even if we get -ENODEV,
137 * but keep going after that as long as we keep getting hits.
139 for (endpoint = 0; !ret || endpoint < 4; endpoint++) {
140 ret = atmel_hlcdc_attach_endpoint(dev, endpoint);
148 /* At least one device was successfully attached.*/
149 if (ret == -ENODEV && attached)