Loading...
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 output mode
31 */
32enum atmel_hlcdc_connector_rgb_mode {
33 ATMEL_HLCDC_CONNECTOR_RGB444,
34 ATMEL_HLCDC_CONNECTOR_RGB565,
35 ATMEL_HLCDC_CONNECTOR_RGB666,
36 ATMEL_HLCDC_CONNECTOR_RGB888,
37};
38
39/**
40 * Atmel HLCDC RGB connector structure
41 *
42 * This structure stores RGB slave device information.
43 *
44 * @connector: DRM connector
45 * @encoder: DRM encoder
46 * @dc: pointer to the atmel_hlcdc_dc structure
47 * @dpms: current DPMS mode
48 */
49struct atmel_hlcdc_rgb_output {
50 struct drm_connector connector;
51 struct drm_encoder encoder;
52 struct atmel_hlcdc_dc *dc;
53 int dpms;
54};
55
56static inline struct atmel_hlcdc_rgb_output *
57drm_connector_to_atmel_hlcdc_rgb_output(struct drm_connector *connector)
58{
59 return container_of(connector, struct atmel_hlcdc_rgb_output,
60 connector);
61}
62
63static inline struct atmel_hlcdc_rgb_output *
64drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder *encoder)
65{
66 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
67}
68
69/**
70 * Atmel HLCDC Panel device structure
71 *
72 * This structure is specialization of the slave device structure to
73 * interface with drm panels.
74 *
75 * @base: base slave device fields
76 * @panel: drm panel attached to this slave device
77 */
78struct atmel_hlcdc_panel {
79 struct atmel_hlcdc_rgb_output base;
80 struct drm_panel *panel;
81};
82
83static inline struct atmel_hlcdc_panel *
84atmel_hlcdc_rgb_output_to_panel(struct atmel_hlcdc_rgb_output *output)
85{
86 return container_of(output, struct atmel_hlcdc_panel, base);
87}
88
89static void atmel_hlcdc_panel_encoder_enable(struct drm_encoder *encoder)
90{
91 struct atmel_hlcdc_rgb_output *rgb =
92 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
93 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
94
95 drm_panel_enable(panel->panel);
96}
97
98static void atmel_hlcdc_panel_encoder_disable(struct drm_encoder *encoder)
99{
100 struct atmel_hlcdc_rgb_output *rgb =
101 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
102 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
103
104 drm_panel_disable(panel->panel);
105}
106
107static bool
108atmel_hlcdc_panel_encoder_mode_fixup(struct drm_encoder *encoder,
109 const struct drm_display_mode *mode,
110 struct drm_display_mode *adjusted)
111{
112 return true;
113}
114
115static void
116atmel_hlcdc_rgb_encoder_mode_set(struct drm_encoder *encoder,
117 struct drm_display_mode *mode,
118 struct drm_display_mode *adjusted)
119{
120 struct atmel_hlcdc_rgb_output *rgb =
121 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
122 struct drm_display_info *info = &rgb->connector.display_info;
123 unsigned int cfg;
124
125 cfg = 0;
126
127 if (info->num_bus_formats) {
128 switch (info->bus_formats[0]) {
129 case MEDIA_BUS_FMT_RGB565_1X16:
130 cfg |= ATMEL_HLCDC_CONNECTOR_RGB565 << 8;
131 break;
132 case MEDIA_BUS_FMT_RGB666_1X18:
133 cfg |= ATMEL_HLCDC_CONNECTOR_RGB666 << 8;
134 break;
135 case MEDIA_BUS_FMT_RGB888_1X24:
136 cfg |= ATMEL_HLCDC_CONNECTOR_RGB888 << 8;
137 break;
138 case MEDIA_BUS_FMT_RGB444_1X12:
139 default:
140 break;
141 }
142 }
143
144 regmap_update_bits(rgb->dc->hlcdc->regmap, ATMEL_HLCDC_CFG(5),
145 ATMEL_HLCDC_MODE_MASK,
146 cfg);
147}
148
149static const struct drm_encoder_helper_funcs atmel_hlcdc_panel_encoder_helper_funcs = {
150 .mode_fixup = atmel_hlcdc_panel_encoder_mode_fixup,
151 .mode_set = atmel_hlcdc_rgb_encoder_mode_set,
152 .disable = atmel_hlcdc_panel_encoder_disable,
153 .enable = atmel_hlcdc_panel_encoder_enable,
154};
155
156static void atmel_hlcdc_rgb_encoder_destroy(struct drm_encoder *encoder)
157{
158 drm_encoder_cleanup(encoder);
159 memset(encoder, 0, sizeof(*encoder));
160}
161
162static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
163 .destroy = atmel_hlcdc_rgb_encoder_destroy,
164};
165
166static int atmel_hlcdc_panel_get_modes(struct drm_connector *connector)
167{
168 struct atmel_hlcdc_rgb_output *rgb =
169 drm_connector_to_atmel_hlcdc_rgb_output(connector);
170 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
171
172 return panel->panel->funcs->get_modes(panel->panel);
173}
174
175static int atmel_hlcdc_rgb_mode_valid(struct drm_connector *connector,
176 struct drm_display_mode *mode)
177{
178 struct atmel_hlcdc_rgb_output *rgb =
179 drm_connector_to_atmel_hlcdc_rgb_output(connector);
180
181 return atmel_hlcdc_dc_mode_valid(rgb->dc, mode);
182}
183
184
185
186static struct drm_encoder *
187atmel_hlcdc_rgb_best_encoder(struct drm_connector *connector)
188{
189 struct atmel_hlcdc_rgb_output *rgb =
190 drm_connector_to_atmel_hlcdc_rgb_output(connector);
191
192 return &rgb->encoder;
193}
194
195static const struct drm_connector_helper_funcs atmel_hlcdc_panel_connector_helper_funcs = {
196 .get_modes = atmel_hlcdc_panel_get_modes,
197 .mode_valid = atmel_hlcdc_rgb_mode_valid,
198 .best_encoder = atmel_hlcdc_rgb_best_encoder,
199};
200
201static enum drm_connector_status
202atmel_hlcdc_panel_connector_detect(struct drm_connector *connector, bool force)
203{
204 return connector_status_connected;
205}
206
207static void
208atmel_hlcdc_panel_connector_destroy(struct drm_connector *connector)
209{
210 struct atmel_hlcdc_rgb_output *rgb =
211 drm_connector_to_atmel_hlcdc_rgb_output(connector);
212 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
213
214 drm_panel_detach(panel->panel);
215 drm_connector_cleanup(connector);
216}
217
218static const struct drm_connector_funcs atmel_hlcdc_panel_connector_funcs = {
219 .dpms = drm_atomic_helper_connector_dpms,
220 .detect = atmel_hlcdc_panel_connector_detect,
221 .fill_modes = drm_helper_probe_single_connector_modes,
222 .destroy = atmel_hlcdc_panel_connector_destroy,
223 .reset = drm_atomic_helper_connector_reset,
224 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
225 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
226};
227
228static int atmel_hlcdc_create_panel_output(struct drm_device *dev,
229 struct of_endpoint *ep)
230{
231 struct atmel_hlcdc_dc *dc = dev->dev_private;
232 struct device_node *np;
233 struct drm_panel *p = NULL;
234 struct atmel_hlcdc_panel *panel;
235 int ret;
236
237 np = of_graph_get_remote_port_parent(ep->local_node);
238 if (!np)
239 return -EINVAL;
240
241 p = of_drm_find_panel(np);
242 of_node_put(np);
243
244 if (!p)
245 return -EPROBE_DEFER;
246
247 panel = devm_kzalloc(dev->dev, sizeof(*panel), GFP_KERNEL);
248 if (!panel)
249 return -EINVAL;
250
251 panel->base.dpms = DRM_MODE_DPMS_OFF;
252
253 panel->base.dc = dc;
254
255 drm_encoder_helper_add(&panel->base.encoder,
256 &atmel_hlcdc_panel_encoder_helper_funcs);
257 ret = drm_encoder_init(dev, &panel->base.encoder,
258 &atmel_hlcdc_panel_encoder_funcs,
259 DRM_MODE_ENCODER_LVDS, NULL);
260 if (ret)
261 return ret;
262
263 panel->base.connector.dpms = DRM_MODE_DPMS_OFF;
264 panel->base.connector.polled = DRM_CONNECTOR_POLL_CONNECT;
265 drm_connector_helper_add(&panel->base.connector,
266 &atmel_hlcdc_panel_connector_helper_funcs);
267 ret = drm_connector_init(dev, &panel->base.connector,
268 &atmel_hlcdc_panel_connector_funcs,
269 DRM_MODE_CONNECTOR_LVDS);
270 if (ret)
271 goto err_encoder_cleanup;
272
273 drm_mode_connector_attach_encoder(&panel->base.connector,
274 &panel->base.encoder);
275 panel->base.encoder.possible_crtcs = 0x1;
276
277 drm_panel_attach(p, &panel->base.connector);
278 panel->panel = p;
279
280 return 0;
281
282err_encoder_cleanup:
283 drm_encoder_cleanup(&panel->base.encoder);
284
285 return ret;
286}
287
288int atmel_hlcdc_create_outputs(struct drm_device *dev)
289{
290 struct device_node *port_np, *np;
291 struct of_endpoint ep;
292 int ret;
293
294 port_np = of_get_child_by_name(dev->dev->of_node, "port");
295 if (!port_np)
296 return -EINVAL;
297
298 np = of_get_child_by_name(port_np, "endpoint");
299 of_node_put(port_np);
300
301 if (!np)
302 return -EINVAL;
303
304 ret = of_graph_parse_endpoint(np, &ep);
305 of_node_put(port_np);
306
307 if (ret)
308 return ret;
309
310 /* We currently only support panel output */
311 return atmel_hlcdc_create_panel_output(dev, &ep);
312}
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}