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>
13 #include <linux/of_graph.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_encoder.h>
17 #include <drm/drm_of.h>
18 #include <drm/drm_simple_kms_helper.h>
20 #include "atmel_hlcdc_dc.h"
22 struct atmel_hlcdc_rgb_output {
23 struct drm_encoder encoder;
27 static struct atmel_hlcdc_rgb_output *
28 atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)
30 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
33 int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)
35 struct atmel_hlcdc_rgb_output *output;
37 output = atmel_hlcdc_encoder_to_rgb_output(encoder);
39 return output->bus_fmt;
42 static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)
47 ret = of_property_read_u32(ep, "bus-width", &bus_width);
55 return MEDIA_BUS_FMT_RGB444_1X12;
57 return MEDIA_BUS_FMT_RGB565_1X16;
59 return MEDIA_BUS_FMT_RGB666_1X18;
61 return MEDIA_BUS_FMT_RGB888_1X24;
67 static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)
69 struct atmel_hlcdc_rgb_output *output;
70 struct device_node *ep;
71 struct drm_panel *panel;
72 struct drm_bridge *bridge;
75 ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);
79 ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 0, endpoint,
86 output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
92 output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);
94 if (output->bus_fmt < 0) {
95 dev_err(dev->dev, "endpoint %d: invalid bus width\n", endpoint);
99 ret = drm_simple_encoder_init(dev, &output->encoder,
100 DRM_MODE_ENCODER_NONE);
104 output->encoder.possible_crtcs = 0x1;
107 bridge = drm_panel_bridge_add_typed(panel,
108 DRM_MODE_CONNECTOR_Unknown);
110 return PTR_ERR(bridge);
114 ret = drm_bridge_attach(&output->encoder, bridge, NULL, 0);
119 drm_panel_bridge_remove(bridge);
122 drm_encoder_cleanup(&output->encoder);
127 int atmel_hlcdc_create_outputs(struct drm_device *dev)
129 int endpoint, ret = 0;
133 * Always scan the first few endpoints even if we get -ENODEV,
134 * but keep going after that as long as we keep getting hits.
136 for (endpoint = 0; !ret || endpoint < 4; endpoint++) {
137 ret = atmel_hlcdc_attach_endpoint(dev, endpoint);
145 /* At least one device was successfully attached.*/
146 if (ret == -ENODEV && attached)