Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2014 Traphandler
4 * Copyright (C) 2014 Free Electrons
5 * Copyright (C) 2014 Atmel
6 *
7 * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
8 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
9 */
10
11#include <linux/media-bus-format.h>
12#include <linux/of.h>
13#include <linux/of_graph.h>
14
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>
19
20#include "atmel_hlcdc_dc.h"
21
22struct atmel_hlcdc_rgb_output {
23 struct drm_encoder encoder;
24 int bus_fmt;
25};
26
27static struct atmel_hlcdc_rgb_output *
28atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)
29{
30 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
31}
32
33int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)
34{
35 struct atmel_hlcdc_rgb_output *output;
36
37 output = atmel_hlcdc_encoder_to_rgb_output(encoder);
38
39 return output->bus_fmt;
40}
41
42static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)
43{
44 u32 bus_width;
45 int ret;
46
47 ret = of_property_read_u32(ep, "bus-width", &bus_width);
48 if (ret == -EINVAL)
49 return 0;
50 if (ret)
51 return ret;
52
53 switch (bus_width) {
54 case 12:
55 return MEDIA_BUS_FMT_RGB444_1X12;
56 case 16:
57 return MEDIA_BUS_FMT_RGB565_1X16;
58 case 18:
59 return MEDIA_BUS_FMT_RGB666_1X18;
60 case 24:
61 return MEDIA_BUS_FMT_RGB888_1X24;
62 default:
63 return -EINVAL;
64 }
65}
66
67static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)
68{
69 struct atmel_hlcdc_rgb_output *output;
70 struct device_node *ep;
71 struct drm_panel *panel;
72 struct drm_bridge *bridge;
73 int ret;
74
75 ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);
76 if (!ep)
77 return -ENODEV;
78
79 ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 0, endpoint,
80 &panel, &bridge);
81 if (ret) {
82 of_node_put(ep);
83 return ret;
84 }
85
86 output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
87 if (!output) {
88 of_node_put(ep);
89 return -ENOMEM;
90 }
91
92 output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);
93 of_node_put(ep);
94 if (output->bus_fmt < 0) {
95 dev_err(dev->dev, "endpoint %d: invalid bus width\n", endpoint);
96 return -EINVAL;
97 }
98
99 ret = drm_simple_encoder_init(dev, &output->encoder,
100 DRM_MODE_ENCODER_NONE);
101 if (ret)
102 return ret;
103
104 output->encoder.possible_crtcs = 0x1;
105
106 if (panel) {
107 bridge = drm_panel_bridge_add_typed(panel,
108 DRM_MODE_CONNECTOR_Unknown);
109 if (IS_ERR(bridge))
110 return PTR_ERR(bridge);
111 }
112
113 if (bridge) {
114 ret = drm_bridge_attach(&output->encoder, bridge, NULL, 0);
115 if (!ret)
116 return 0;
117
118 if (panel)
119 drm_panel_bridge_remove(bridge);
120 }
121
122 drm_encoder_cleanup(&output->encoder);
123
124 return ret;
125}
126
127int atmel_hlcdc_create_outputs(struct drm_device *dev)
128{
129 int endpoint, ret = 0;
130 int attached = 0;
131
132 /*
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.
135 */
136 for (endpoint = 0; !ret || endpoint < 4; endpoint++) {
137 ret = atmel_hlcdc_attach_endpoint(dev, endpoint);
138 if (ret == -ENODEV)
139 continue;
140 if (ret)
141 break;
142 attached++;
143 }
144
145 /* At least one device was successfully attached.*/
146 if (ret == -ENODEV && attached)
147 return 0;
148
149 return ret;
150}
1/*
2 * Copyright (C) 2014 Traphandler
3 * Copyright (C) 2014 Free Electrons
4 * Copyright (C) 2014 Atmel
5 *
6 * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
7 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
8 *
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.
12 *
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
16 * more details.
17 *
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/>.
20 */
21
22#include <linux/of_graph.h>
23
24#include <drm/drmP.h>
25#include <drm/drm_panel.h>
26
27#include "atmel_hlcdc_dc.h"
28
29/**
30 * Atmel HLCDC RGB connector structure
31 *
32 * This structure stores RGB slave device information.
33 *
34 * @connector: DRM connector
35 * @encoder: DRM encoder
36 * @dc: pointer to the atmel_hlcdc_dc structure
37 * @panel: panel connected on the RGB output
38 */
39struct atmel_hlcdc_rgb_output {
40 struct drm_connector connector;
41 struct drm_encoder encoder;
42 struct atmel_hlcdc_dc *dc;
43 struct drm_panel *panel;
44};
45
46static inline struct atmel_hlcdc_rgb_output *
47drm_connector_to_atmel_hlcdc_rgb_output(struct drm_connector *connector)
48{
49 return container_of(connector, struct atmel_hlcdc_rgb_output,
50 connector);
51}
52
53static inline struct atmel_hlcdc_rgb_output *
54drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder *encoder)
55{
56 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
57}
58
59static void atmel_hlcdc_rgb_encoder_enable(struct drm_encoder *encoder)
60{
61 struct atmel_hlcdc_rgb_output *rgb =
62 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
63
64 if (rgb->panel) {
65 drm_panel_prepare(rgb->panel);
66 drm_panel_enable(rgb->panel);
67 }
68}
69
70static void atmel_hlcdc_rgb_encoder_disable(struct drm_encoder *encoder)
71{
72 struct atmel_hlcdc_rgb_output *rgb =
73 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
74
75 if (rgb->panel) {
76 drm_panel_disable(rgb->panel);
77 drm_panel_unprepare(rgb->panel);
78 }
79}
80
81static const struct drm_encoder_helper_funcs atmel_hlcdc_panel_encoder_helper_funcs = {
82 .disable = atmel_hlcdc_rgb_encoder_disable,
83 .enable = atmel_hlcdc_rgb_encoder_enable,
84};
85
86static void atmel_hlcdc_rgb_encoder_destroy(struct drm_encoder *encoder)
87{
88 drm_encoder_cleanup(encoder);
89 memset(encoder, 0, sizeof(*encoder));
90}
91
92static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
93 .destroy = atmel_hlcdc_rgb_encoder_destroy,
94};
95
96static int atmel_hlcdc_panel_get_modes(struct drm_connector *connector)
97{
98 struct atmel_hlcdc_rgb_output *rgb =
99 drm_connector_to_atmel_hlcdc_rgb_output(connector);
100
101 if (rgb->panel)
102 return rgb->panel->funcs->get_modes(rgb->panel);
103
104 return 0;
105}
106
107static int atmel_hlcdc_rgb_mode_valid(struct drm_connector *connector,
108 struct drm_display_mode *mode)
109{
110 struct atmel_hlcdc_rgb_output *rgb =
111 drm_connector_to_atmel_hlcdc_rgb_output(connector);
112
113 return atmel_hlcdc_dc_mode_valid(rgb->dc, mode);
114}
115
116static const struct drm_connector_helper_funcs atmel_hlcdc_panel_connector_helper_funcs = {
117 .get_modes = atmel_hlcdc_panel_get_modes,
118 .mode_valid = atmel_hlcdc_rgb_mode_valid,
119};
120
121static enum drm_connector_status
122atmel_hlcdc_panel_connector_detect(struct drm_connector *connector, bool force)
123{
124 struct atmel_hlcdc_rgb_output *rgb =
125 drm_connector_to_atmel_hlcdc_rgb_output(connector);
126
127 if (rgb->panel)
128 return connector_status_connected;
129
130 return connector_status_disconnected;
131}
132
133static void
134atmel_hlcdc_panel_connector_destroy(struct drm_connector *connector)
135{
136 struct atmel_hlcdc_rgb_output *rgb =
137 drm_connector_to_atmel_hlcdc_rgb_output(connector);
138
139 if (rgb->panel)
140 drm_panel_detach(rgb->panel);
141
142 drm_connector_cleanup(connector);
143}
144
145static const struct drm_connector_funcs atmel_hlcdc_panel_connector_funcs = {
146 .dpms = drm_atomic_helper_connector_dpms,
147 .detect = atmel_hlcdc_panel_connector_detect,
148 .fill_modes = drm_helper_probe_single_connector_modes,
149 .destroy = atmel_hlcdc_panel_connector_destroy,
150 .reset = drm_atomic_helper_connector_reset,
151 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
152 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
153};
154
155static int atmel_hlcdc_check_endpoint(struct drm_device *dev,
156 const struct of_endpoint *ep)
157{
158 struct device_node *np;
159 void *obj;
160
161 np = of_graph_get_remote_port_parent(ep->local_node);
162
163 obj = of_drm_find_panel(np);
164 if (!obj)
165 obj = of_drm_find_bridge(np);
166
167 of_node_put(np);
168
169 return obj ? 0 : -EPROBE_DEFER;
170}
171
172static int atmel_hlcdc_attach_endpoint(struct drm_device *dev,
173 const struct of_endpoint *ep)
174{
175 struct atmel_hlcdc_dc *dc = dev->dev_private;
176 struct atmel_hlcdc_rgb_output *output;
177 struct device_node *np;
178 struct drm_panel *panel;
179 struct drm_bridge *bridge;
180 int ret;
181
182 output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
183 if (!output)
184 return -EINVAL;
185
186 output->dc = dc;
187
188 drm_encoder_helper_add(&output->encoder,
189 &atmel_hlcdc_panel_encoder_helper_funcs);
190 ret = drm_encoder_init(dev, &output->encoder,
191 &atmel_hlcdc_panel_encoder_funcs,
192 DRM_MODE_ENCODER_NONE, NULL);
193 if (ret)
194 return ret;
195
196 output->encoder.possible_crtcs = 0x1;
197
198 np = of_graph_get_remote_port_parent(ep->local_node);
199
200 ret = -EPROBE_DEFER;
201
202 panel = of_drm_find_panel(np);
203 if (panel) {
204 of_node_put(np);
205 output->connector.dpms = DRM_MODE_DPMS_OFF;
206 output->connector.polled = DRM_CONNECTOR_POLL_CONNECT;
207 drm_connector_helper_add(&output->connector,
208 &atmel_hlcdc_panel_connector_helper_funcs);
209 ret = drm_connector_init(dev, &output->connector,
210 &atmel_hlcdc_panel_connector_funcs,
211 DRM_MODE_CONNECTOR_Unknown);
212 if (ret)
213 goto err_encoder_cleanup;
214
215 drm_mode_connector_attach_encoder(&output->connector,
216 &output->encoder);
217
218 ret = drm_panel_attach(panel, &output->connector);
219 if (ret) {
220 drm_connector_cleanup(&output->connector);
221 goto err_encoder_cleanup;
222 }
223
224 output->panel = panel;
225
226 return 0;
227 }
228
229 bridge = of_drm_find_bridge(np);
230 of_node_put(np);
231
232 if (bridge) {
233 output->encoder.bridge = bridge;
234 bridge->encoder = &output->encoder;
235 ret = drm_bridge_attach(dev, bridge);
236 if (!ret)
237 return 0;
238 }
239
240err_encoder_cleanup:
241 drm_encoder_cleanup(&output->encoder);
242
243 return ret;
244}
245
246int atmel_hlcdc_create_outputs(struct drm_device *dev)
247{
248 struct device_node *ep_np = NULL;
249 struct of_endpoint ep;
250 int ret;
251
252 for_each_endpoint_of_node(dev->dev->of_node, ep_np) {
253 ret = of_graph_parse_endpoint(ep_np, &ep);
254 if (!ret)
255 ret = atmel_hlcdc_check_endpoint(dev, &ep);
256
257 if (ret) {
258 of_node_put(ep_np);
259 return ret;
260 }
261 }
262
263 for_each_endpoint_of_node(dev->dev->of_node, ep_np) {
264 ret = of_graph_parse_endpoint(ep_np, &ep);
265 if (!ret)
266 ret = atmel_hlcdc_attach_endpoint(dev, &ep);
267
268 if (ret) {
269 of_node_put(ep_np);
270 return ret;
271 }
272 }
273
274 return 0;
275}