.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
---|
1 | 2 | /* |
---|
2 | 3 | * i.MX drm driver - parallel display implementation |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2012 Sascha Hauer, Pengutronix |
---|
5 | | - * |
---|
6 | | - * This program is free software; you can redistribute it and/or |
---|
7 | | - * modify it under the terms of the GNU General Public License |
---|
8 | | - * as published by the Free Software Foundation; either version 2 |
---|
9 | | - * of the License, or (at your option) any later version. |
---|
10 | | - * This program is distributed in the hope that it will be useful, |
---|
11 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | | - * GNU General Public License for more details. |
---|
14 | 6 | */ |
---|
15 | 7 | |
---|
16 | 8 | #include <linux/component.h> |
---|
17 | 9 | #include <linux/module.h> |
---|
18 | | -#include <drm/drmP.h> |
---|
| 10 | +#include <linux/platform_device.h> |
---|
| 11 | +#include <linux/videodev2.h> |
---|
| 12 | + |
---|
| 13 | +#include <video/of_display_timing.h> |
---|
| 14 | + |
---|
19 | 15 | #include <drm/drm_atomic_helper.h> |
---|
| 16 | +#include <drm/drm_bridge.h> |
---|
20 | 17 | #include <drm/drm_fb_helper.h> |
---|
21 | | -#include <drm/drm_crtc_helper.h> |
---|
22 | 18 | #include <drm/drm_of.h> |
---|
23 | 19 | #include <drm/drm_panel.h> |
---|
24 | | -#include <linux/videodev2.h> |
---|
25 | | -#include <video/of_display_timing.h> |
---|
| 20 | +#include <drm/drm_probe_helper.h> |
---|
| 21 | +#include <drm/drm_simple_kms_helper.h> |
---|
26 | 22 | |
---|
27 | 23 | #include "imx-drm.h" |
---|
28 | 24 | |
---|
29 | 25 | struct imx_parallel_display { |
---|
30 | 26 | struct drm_connector connector; |
---|
31 | 27 | struct drm_encoder encoder; |
---|
| 28 | + struct drm_bridge bridge; |
---|
32 | 29 | struct device *dev; |
---|
33 | 30 | void *edid; |
---|
34 | | - int edid_len; |
---|
35 | 31 | u32 bus_format; |
---|
36 | 32 | u32 bus_flags; |
---|
37 | 33 | struct drm_display_mode mode; |
---|
38 | 34 | struct drm_panel *panel; |
---|
39 | | - struct drm_bridge *bridge; |
---|
| 35 | + struct drm_bridge *next_bridge; |
---|
40 | 36 | }; |
---|
41 | 37 | |
---|
42 | 38 | static inline struct imx_parallel_display *con_to_imxpd(struct drm_connector *c) |
---|
.. | .. |
---|
44 | 40 | return container_of(c, struct imx_parallel_display, connector); |
---|
45 | 41 | } |
---|
46 | 42 | |
---|
47 | | -static inline struct imx_parallel_display *enc_to_imxpd(struct drm_encoder *e) |
---|
| 43 | +static inline struct imx_parallel_display *bridge_to_imxpd(struct drm_bridge *b) |
---|
48 | 44 | { |
---|
49 | | - return container_of(e, struct imx_parallel_display, encoder); |
---|
| 45 | + return container_of(b, struct imx_parallel_display, bridge); |
---|
50 | 46 | } |
---|
51 | 47 | |
---|
52 | 48 | static int imx_pd_connector_get_modes(struct drm_connector *connector) |
---|
53 | 49 | { |
---|
54 | 50 | struct imx_parallel_display *imxpd = con_to_imxpd(connector); |
---|
55 | 51 | struct device_node *np = imxpd->dev->of_node; |
---|
56 | | - int num_modes = 0; |
---|
| 52 | + int num_modes; |
---|
57 | 53 | |
---|
58 | | - if (imxpd->panel && imxpd->panel->funcs && |
---|
59 | | - imxpd->panel->funcs->get_modes) { |
---|
60 | | - num_modes = imxpd->panel->funcs->get_modes(imxpd->panel); |
---|
61 | | - if (num_modes > 0) |
---|
62 | | - return num_modes; |
---|
63 | | - } |
---|
| 54 | + num_modes = drm_panel_get_modes(imxpd->panel, connector); |
---|
| 55 | + if (num_modes > 0) |
---|
| 56 | + return num_modes; |
---|
64 | 57 | |
---|
65 | 58 | if (imxpd->edid) { |
---|
66 | 59 | drm_connector_update_edid_property(connector, imxpd->edid); |
---|
.. | .. |
---|
77 | 70 | ret = of_get_drm_display_mode(np, &imxpd->mode, |
---|
78 | 71 | &imxpd->bus_flags, |
---|
79 | 72 | OF_USE_NATIVE_MODE); |
---|
80 | | - if (ret) |
---|
| 73 | + if (ret) { |
---|
| 74 | + drm_mode_destroy(connector->dev, mode); |
---|
81 | 75 | return ret; |
---|
| 76 | + } |
---|
82 | 77 | |
---|
83 | 78 | drm_mode_copy(mode, &imxpd->mode); |
---|
84 | 79 | mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, |
---|
.. | .. |
---|
89 | 84 | return num_modes; |
---|
90 | 85 | } |
---|
91 | 86 | |
---|
92 | | -static struct drm_encoder *imx_pd_connector_best_encoder( |
---|
93 | | - struct drm_connector *connector) |
---|
| 87 | +static void imx_pd_bridge_enable(struct drm_bridge *bridge) |
---|
94 | 88 | { |
---|
95 | | - struct imx_parallel_display *imxpd = con_to_imxpd(connector); |
---|
96 | | - |
---|
97 | | - return &imxpd->encoder; |
---|
98 | | -} |
---|
99 | | - |
---|
100 | | -static void imx_pd_encoder_enable(struct drm_encoder *encoder) |
---|
101 | | -{ |
---|
102 | | - struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
---|
| 89 | + struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge); |
---|
103 | 90 | |
---|
104 | 91 | drm_panel_prepare(imxpd->panel); |
---|
105 | 92 | drm_panel_enable(imxpd->panel); |
---|
106 | 93 | } |
---|
107 | 94 | |
---|
108 | | -static void imx_pd_encoder_disable(struct drm_encoder *encoder) |
---|
| 95 | +static void imx_pd_bridge_disable(struct drm_bridge *bridge) |
---|
109 | 96 | { |
---|
110 | | - struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
---|
| 97 | + struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge); |
---|
111 | 98 | |
---|
112 | 99 | drm_panel_disable(imxpd->panel); |
---|
113 | 100 | drm_panel_unprepare(imxpd->panel); |
---|
114 | 101 | } |
---|
115 | 102 | |
---|
116 | | -static int imx_pd_encoder_atomic_check(struct drm_encoder *encoder, |
---|
117 | | - struct drm_crtc_state *crtc_state, |
---|
118 | | - struct drm_connector_state *conn_state) |
---|
| 103 | +static const u32 imx_pd_bus_fmts[] = { |
---|
| 104 | + MEDIA_BUS_FMT_RGB888_1X24, |
---|
| 105 | + MEDIA_BUS_FMT_BGR888_1X24, |
---|
| 106 | + MEDIA_BUS_FMT_GBR888_1X24, |
---|
| 107 | + MEDIA_BUS_FMT_RGB666_1X18, |
---|
| 108 | + MEDIA_BUS_FMT_RGB666_1X24_CPADHI, |
---|
| 109 | + MEDIA_BUS_FMT_RGB565_1X16, |
---|
| 110 | +}; |
---|
| 111 | + |
---|
| 112 | +static u32 * |
---|
| 113 | +imx_pd_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge, |
---|
| 114 | + struct drm_bridge_state *bridge_state, |
---|
| 115 | + struct drm_crtc_state *crtc_state, |
---|
| 116 | + struct drm_connector_state *conn_state, |
---|
| 117 | + unsigned int *num_output_fmts) |
---|
| 118 | +{ |
---|
| 119 | + struct drm_display_info *di = &conn_state->connector->display_info; |
---|
| 120 | + struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge); |
---|
| 121 | + u32 *output_fmts; |
---|
| 122 | + |
---|
| 123 | + if (!imxpd->bus_format && !di->num_bus_formats) { |
---|
| 124 | + *num_output_fmts = ARRAY_SIZE(imx_pd_bus_fmts); |
---|
| 125 | + return kmemdup(imx_pd_bus_fmts, sizeof(imx_pd_bus_fmts), |
---|
| 126 | + GFP_KERNEL); |
---|
| 127 | + } |
---|
| 128 | + |
---|
| 129 | + *num_output_fmts = 1; |
---|
| 130 | + output_fmts = kmalloc(sizeof(*output_fmts), GFP_KERNEL); |
---|
| 131 | + if (!output_fmts) |
---|
| 132 | + return NULL; |
---|
| 133 | + |
---|
| 134 | + if (!imxpd->bus_format && di->num_bus_formats) |
---|
| 135 | + output_fmts[0] = di->bus_formats[0]; |
---|
| 136 | + else |
---|
| 137 | + output_fmts[0] = imxpd->bus_format; |
---|
| 138 | + |
---|
| 139 | + return output_fmts; |
---|
| 140 | +} |
---|
| 141 | + |
---|
| 142 | +static bool imx_pd_format_supported(u32 output_fmt) |
---|
| 143 | +{ |
---|
| 144 | + unsigned int i; |
---|
| 145 | + |
---|
| 146 | + for (i = 0; i < ARRAY_SIZE(imx_pd_bus_fmts); i++) { |
---|
| 147 | + if (imx_pd_bus_fmts[i] == output_fmt) |
---|
| 148 | + return true; |
---|
| 149 | + } |
---|
| 150 | + |
---|
| 151 | + return false; |
---|
| 152 | +} |
---|
| 153 | + |
---|
| 154 | +static u32 * |
---|
| 155 | +imx_pd_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge, |
---|
| 156 | + struct drm_bridge_state *bridge_state, |
---|
| 157 | + struct drm_crtc_state *crtc_state, |
---|
| 158 | + struct drm_connector_state *conn_state, |
---|
| 159 | + u32 output_fmt, |
---|
| 160 | + unsigned int *num_input_fmts) |
---|
| 161 | +{ |
---|
| 162 | + struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge); |
---|
| 163 | + u32 *input_fmts; |
---|
| 164 | + |
---|
| 165 | + /* |
---|
| 166 | + * If the next bridge does not support bus format negotiation, let's |
---|
| 167 | + * use the static bus format definition (imxpd->bus_format) if it's |
---|
| 168 | + * specified, RGB888 when it's not. |
---|
| 169 | + */ |
---|
| 170 | + if (output_fmt == MEDIA_BUS_FMT_FIXED) |
---|
| 171 | + output_fmt = imxpd->bus_format ? : MEDIA_BUS_FMT_RGB888_1X24; |
---|
| 172 | + |
---|
| 173 | + /* Now make sure the requested output format is supported. */ |
---|
| 174 | + if ((imxpd->bus_format && imxpd->bus_format != output_fmt) || |
---|
| 175 | + !imx_pd_format_supported(output_fmt)) { |
---|
| 176 | + *num_input_fmts = 0; |
---|
| 177 | + return NULL; |
---|
| 178 | + } |
---|
| 179 | + |
---|
| 180 | + *num_input_fmts = 1; |
---|
| 181 | + input_fmts = kmalloc(sizeof(*input_fmts), GFP_KERNEL); |
---|
| 182 | + if (!input_fmts) |
---|
| 183 | + return NULL; |
---|
| 184 | + |
---|
| 185 | + input_fmts[0] = output_fmt; |
---|
| 186 | + return input_fmts; |
---|
| 187 | +} |
---|
| 188 | + |
---|
| 189 | +static int imx_pd_bridge_atomic_check(struct drm_bridge *bridge, |
---|
| 190 | + struct drm_bridge_state *bridge_state, |
---|
| 191 | + struct drm_crtc_state *crtc_state, |
---|
| 192 | + struct drm_connector_state *conn_state) |
---|
119 | 193 | { |
---|
120 | 194 | struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state); |
---|
121 | 195 | struct drm_display_info *di = &conn_state->connector->display_info; |
---|
122 | | - struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
---|
| 196 | + struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge); |
---|
| 197 | + struct drm_bridge_state *next_bridge_state = NULL; |
---|
| 198 | + struct drm_bridge *next_bridge; |
---|
| 199 | + u32 bus_flags, bus_fmt; |
---|
123 | 200 | |
---|
124 | | - if (!imxpd->bus_format && di->num_bus_formats) { |
---|
125 | | - imx_crtc_state->bus_flags = di->bus_flags; |
---|
126 | | - imx_crtc_state->bus_format = di->bus_formats[0]; |
---|
127 | | - } else { |
---|
128 | | - imx_crtc_state->bus_flags = imxpd->bus_flags; |
---|
129 | | - imx_crtc_state->bus_format = imxpd->bus_format; |
---|
130 | | - } |
---|
| 201 | + next_bridge = drm_bridge_get_next_bridge(bridge); |
---|
| 202 | + if (next_bridge) |
---|
| 203 | + next_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state, |
---|
| 204 | + next_bridge); |
---|
| 205 | + |
---|
| 206 | + if (next_bridge_state) |
---|
| 207 | + bus_flags = next_bridge_state->input_bus_cfg.flags; |
---|
| 208 | + else if (di->num_bus_formats) |
---|
| 209 | + bus_flags = di->bus_flags; |
---|
| 210 | + else |
---|
| 211 | + bus_flags = imxpd->bus_flags; |
---|
| 212 | + |
---|
| 213 | + bus_fmt = bridge_state->input_bus_cfg.format; |
---|
| 214 | + if (!imx_pd_format_supported(bus_fmt)) |
---|
| 215 | + return -EINVAL; |
---|
| 216 | + |
---|
| 217 | + bridge_state->output_bus_cfg.flags = bus_flags; |
---|
| 218 | + bridge_state->input_bus_cfg.flags = bus_flags; |
---|
| 219 | + imx_crtc_state->bus_flags = bus_flags; |
---|
| 220 | + imx_crtc_state->bus_format = bridge_state->input_bus_cfg.format; |
---|
131 | 221 | imx_crtc_state->di_hsync_pin = 2; |
---|
132 | 222 | imx_crtc_state->di_vsync_pin = 3; |
---|
133 | 223 | |
---|
.. | .. |
---|
144 | 234 | |
---|
145 | 235 | static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = { |
---|
146 | 236 | .get_modes = imx_pd_connector_get_modes, |
---|
147 | | - .best_encoder = imx_pd_connector_best_encoder, |
---|
148 | 237 | }; |
---|
149 | 238 | |
---|
150 | | -static const struct drm_encoder_funcs imx_pd_encoder_funcs = { |
---|
151 | | - .destroy = imx_drm_encoder_destroy, |
---|
152 | | -}; |
---|
153 | | - |
---|
154 | | -static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = { |
---|
155 | | - .enable = imx_pd_encoder_enable, |
---|
156 | | - .disable = imx_pd_encoder_disable, |
---|
157 | | - .atomic_check = imx_pd_encoder_atomic_check, |
---|
| 239 | +static const struct drm_bridge_funcs imx_pd_bridge_funcs = { |
---|
| 240 | + .enable = imx_pd_bridge_enable, |
---|
| 241 | + .disable = imx_pd_bridge_disable, |
---|
| 242 | + .atomic_reset = drm_atomic_helper_bridge_reset, |
---|
| 243 | + .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, |
---|
| 244 | + .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, |
---|
| 245 | + .atomic_check = imx_pd_bridge_atomic_check, |
---|
| 246 | + .atomic_get_input_bus_fmts = imx_pd_bridge_atomic_get_input_bus_fmts, |
---|
| 247 | + .atomic_get_output_bus_fmts = imx_pd_bridge_atomic_get_output_bus_fmts, |
---|
158 | 248 | }; |
---|
159 | 249 | |
---|
160 | 250 | static int imx_pd_register(struct drm_device *drm, |
---|
.. | .. |
---|
174 | 264 | */ |
---|
175 | 265 | imxpd->connector.dpms = DRM_MODE_DPMS_OFF; |
---|
176 | 266 | |
---|
177 | | - drm_encoder_helper_add(encoder, &imx_pd_encoder_helper_funcs); |
---|
178 | | - drm_encoder_init(drm, encoder, &imx_pd_encoder_funcs, |
---|
179 | | - DRM_MODE_ENCODER_NONE, NULL); |
---|
| 267 | + drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_NONE); |
---|
180 | 268 | |
---|
181 | | - if (!imxpd->bridge) { |
---|
| 269 | + imxpd->bridge.funcs = &imx_pd_bridge_funcs; |
---|
| 270 | + drm_bridge_attach(encoder, &imxpd->bridge, NULL, 0); |
---|
| 271 | + |
---|
| 272 | + if (!imxpd->next_bridge) { |
---|
182 | 273 | drm_connector_helper_add(&imxpd->connector, |
---|
183 | 274 | &imx_pd_connector_helper_funcs); |
---|
184 | 275 | drm_connector_init(drm, &imxpd->connector, |
---|
.. | .. |
---|
186 | 277 | DRM_MODE_CONNECTOR_DPI); |
---|
187 | 278 | } |
---|
188 | 279 | |
---|
189 | | - if (imxpd->panel) |
---|
190 | | - drm_panel_attach(imxpd->panel, &imxpd->connector); |
---|
191 | | - |
---|
192 | | - if (imxpd->bridge) { |
---|
193 | | - ret = drm_bridge_attach(encoder, imxpd->bridge, NULL); |
---|
| 280 | + if (imxpd->next_bridge) { |
---|
| 281 | + ret = drm_bridge_attach(encoder, imxpd->next_bridge, |
---|
| 282 | + &imxpd->bridge, 0); |
---|
194 | 283 | if (ret < 0) { |
---|
195 | 284 | dev_err(imxpd->dev, "failed to attach bridge: %d\n", |
---|
196 | 285 | ret); |
---|
.. | .. |
---|
209 | 298 | struct device_node *np = dev->of_node; |
---|
210 | 299 | const u8 *edidp; |
---|
211 | 300 | struct imx_parallel_display *imxpd; |
---|
| 301 | + int edid_len; |
---|
212 | 302 | int ret; |
---|
213 | 303 | u32 bus_format = 0; |
---|
214 | 304 | const char *fmt; |
---|
215 | 305 | |
---|
216 | | - imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL); |
---|
217 | | - if (!imxpd) |
---|
218 | | - return -ENOMEM; |
---|
| 306 | + imxpd = dev_get_drvdata(dev); |
---|
| 307 | + memset(imxpd, 0, sizeof(*imxpd)); |
---|
219 | 308 | |
---|
220 | | - edidp = of_get_property(np, "edid", &imxpd->edid_len); |
---|
| 309 | + /* port@1 is the output port */ |
---|
| 310 | + ret = drm_of_find_panel_or_bridge(np, 1, 0, &imxpd->panel, |
---|
| 311 | + &imxpd->next_bridge); |
---|
| 312 | + if (ret && ret != -ENODEV) |
---|
| 313 | + return ret; |
---|
| 314 | + |
---|
| 315 | + edidp = of_get_property(np, "edid", &edid_len); |
---|
221 | 316 | if (edidp) |
---|
222 | | - imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL); |
---|
| 317 | + imxpd->edid = devm_kmemdup(dev, edidp, edid_len, GFP_KERNEL); |
---|
223 | 318 | |
---|
224 | 319 | ret = of_property_read_string(np, "interface-pix-fmt", &fmt); |
---|
225 | 320 | if (!ret) { |
---|
.. | .. |
---|
234 | 329 | } |
---|
235 | 330 | imxpd->bus_format = bus_format; |
---|
236 | 331 | |
---|
237 | | - /* port@1 is the output port */ |
---|
238 | | - ret = drm_of_find_panel_or_bridge(np, 1, 0, &imxpd->panel, &imxpd->bridge); |
---|
239 | | - if (ret && ret != -ENODEV) |
---|
240 | | - return ret; |
---|
241 | | - |
---|
242 | 332 | imxpd->dev = dev; |
---|
243 | 333 | |
---|
244 | 334 | ret = imx_pd_register(drm, imxpd); |
---|
245 | 335 | if (ret) |
---|
246 | 336 | return ret; |
---|
247 | 337 | |
---|
248 | | - dev_set_drvdata(dev, imxpd); |
---|
249 | | - |
---|
250 | 338 | return 0; |
---|
251 | | -} |
---|
252 | | - |
---|
253 | | -static void imx_pd_unbind(struct device *dev, struct device *master, |
---|
254 | | - void *data) |
---|
255 | | -{ |
---|
256 | | - struct imx_parallel_display *imxpd = dev_get_drvdata(dev); |
---|
257 | | - |
---|
258 | | - if (imxpd->panel) |
---|
259 | | - drm_panel_detach(imxpd->panel); |
---|
260 | | - |
---|
261 | | - kfree(imxpd->edid); |
---|
262 | 339 | } |
---|
263 | 340 | |
---|
264 | 341 | static const struct component_ops imx_pd_ops = { |
---|
265 | 342 | .bind = imx_pd_bind, |
---|
266 | | - .unbind = imx_pd_unbind, |
---|
267 | 343 | }; |
---|
268 | 344 | |
---|
269 | 345 | static int imx_pd_probe(struct platform_device *pdev) |
---|
270 | 346 | { |
---|
| 347 | + struct imx_parallel_display *imxpd; |
---|
| 348 | + |
---|
| 349 | + imxpd = devm_kzalloc(&pdev->dev, sizeof(*imxpd), GFP_KERNEL); |
---|
| 350 | + if (!imxpd) |
---|
| 351 | + return -ENOMEM; |
---|
| 352 | + |
---|
| 353 | + platform_set_drvdata(pdev, imxpd); |
---|
| 354 | + |
---|
271 | 355 | return component_add(&pdev->dev, &imx_pd_ops); |
---|
272 | 356 | } |
---|
273 | 357 | |
---|