1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
6 */
7
8 #include <linux/clk.h>
9 #include <linux/component.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_graph.h>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/reset.h>
18
19 #include <media/cec-notifier.h>
20
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_bridge.h>
23 #include <drm/drm_bridge_connector.h>
24 #include <drm/drm_device.h>
25 #include <drm/drm_edid.h>
26 #include <drm/drm_probe_helper.h>
27 #include <drm/drm_simple_kms_helper.h>
28
29 #include <linux/media-bus-format.h>
30 #include <linux/videodev2.h>
31
32 #include "meson_drv.h"
33 #include "meson_registers.h"
34 #include "meson_vclk.h"
35 #include "meson_venc.h"
36 #include "meson_encoder_hdmi.h"
37
38 struct meson_encoder_hdmi {
39 struct drm_encoder encoder;
40 struct drm_bridge bridge;
41 struct drm_bridge *next_bridge;
42 struct drm_connector *connector;
43 struct meson_drm *priv;
44 unsigned long output_bus_fmt;
45 struct cec_notifier *cec_notifier;
46 };
47
48 #define bridge_to_meson_encoder_hdmi(x) \
49 container_of(x, struct meson_encoder_hdmi, bridge)
50
meson_encoder_hdmi_attach(struct drm_bridge * bridge,struct drm_encoder * encoder,enum drm_bridge_attach_flags flags)51 static int meson_encoder_hdmi_attach(struct drm_bridge *bridge,
52 struct drm_encoder *encoder,
53 enum drm_bridge_attach_flags flags)
54 {
55 struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
56
57 return drm_bridge_attach(encoder, encoder_hdmi->next_bridge,
58 &encoder_hdmi->bridge, flags);
59 }
60
meson_encoder_hdmi_detach(struct drm_bridge * bridge)61 static void meson_encoder_hdmi_detach(struct drm_bridge *bridge)
62 {
63 struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
64
65 cec_notifier_conn_unregister(encoder_hdmi->cec_notifier);
66 encoder_hdmi->cec_notifier = NULL;
67 }
68
meson_encoder_hdmi_set_vclk(struct meson_encoder_hdmi * encoder_hdmi,const struct drm_display_mode * mode)69 static void meson_encoder_hdmi_set_vclk(struct meson_encoder_hdmi *encoder_hdmi,
70 const struct drm_display_mode *mode)
71 {
72 struct meson_drm *priv = encoder_hdmi->priv;
73 int vic = drm_match_cea_mode(mode);
74 unsigned long long phy_freq;
75 unsigned long long vclk_freq;
76 unsigned long long venc_freq;
77 unsigned long long hdmi_freq;
78
79 vclk_freq = mode->clock * 1000ULL;
80
81 /* For 420, pixel clock is half unlike venc clock */
82 if (encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYYVYY8_0_5X24)
83 vclk_freq /= 2;
84
85 /* TMDS clock is pixel_clock * 10 */
86 phy_freq = vclk_freq * 10;
87
88 if (!vic) {
89 meson_vclk_setup(priv, MESON_VCLK_TARGET_DMT, phy_freq,
90 vclk_freq, vclk_freq, vclk_freq, false);
91 return;
92 }
93
94 /* 480i/576i needs global pixel doubling */
95 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
96 vclk_freq *= 2;
97
98 venc_freq = vclk_freq;
99 hdmi_freq = vclk_freq;
100
101 /* VENC double pixels for 1080i, 720p and YUV420 modes */
102 if (meson_venc_hdmi_venc_repeat(vic) ||
103 encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYYVYY8_0_5X24)
104 venc_freq *= 2;
105
106 vclk_freq = max(venc_freq, hdmi_freq);
107
108 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
109 venc_freq /= 2;
110
111 dev_dbg(priv->dev,
112 "phy:%lluHz vclk=%lluHz venc=%lluHz hdmi=%lluHz enci=%d\n",
113 phy_freq, vclk_freq, venc_freq, hdmi_freq,
114 priv->venc.hdmi_use_enci);
115
116 meson_vclk_setup(priv, MESON_VCLK_TARGET_HDMI, phy_freq, vclk_freq,
117 venc_freq, hdmi_freq, priv->venc.hdmi_use_enci);
118 }
119
meson_encoder_hdmi_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * display_info,const struct drm_display_mode * mode)120 static enum drm_mode_status meson_encoder_hdmi_mode_valid(struct drm_bridge *bridge,
121 const struct drm_display_info *display_info,
122 const struct drm_display_mode *mode)
123 {
124 struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
125 struct meson_drm *priv = encoder_hdmi->priv;
126 bool is_hdmi2_sink = display_info->hdmi.scdc.supported;
127 unsigned long long clock = mode->clock * 1000ULL;
128 unsigned long long phy_freq;
129 unsigned long long vclk_freq;
130 unsigned long long venc_freq;
131 unsigned long long hdmi_freq;
132 int vic = drm_match_cea_mode(mode);
133 enum drm_mode_status status;
134
135 dev_dbg(priv->dev, "Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
136
137 /* If sink does not support 540MHz, reject the non-420 HDMI2 modes */
138 if (display_info->max_tmds_clock &&
139 mode->clock > display_info->max_tmds_clock &&
140 !drm_mode_is_420_only(display_info, mode) &&
141 !drm_mode_is_420_also(display_info, mode))
142 return MODE_BAD;
143
144 /* Check against non-VIC supported modes */
145 if (!vic) {
146 status = meson_venc_hdmi_supported_mode(mode);
147 if (status != MODE_OK)
148 return status;
149
150 return meson_vclk_dmt_supported_freq(priv, clock);
151 /* Check against supported VIC modes */
152 } else if (!meson_venc_hdmi_supported_vic(vic))
153 return MODE_BAD;
154
155 vclk_freq = clock;
156
157 /* For 420, pixel clock is half unlike venc clock */
158 if (drm_mode_is_420_only(display_info, mode) ||
159 (!is_hdmi2_sink &&
160 drm_mode_is_420_also(display_info, mode)))
161 vclk_freq /= 2;
162
163 /* TMDS clock is pixel_clock * 10 */
164 phy_freq = vclk_freq * 10;
165
166 /* 480i/576i needs global pixel doubling */
167 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
168 vclk_freq *= 2;
169
170 venc_freq = vclk_freq;
171 hdmi_freq = vclk_freq;
172
173 /* VENC double pixels for 1080i, 720p and YUV420 modes */
174 if (meson_venc_hdmi_venc_repeat(vic) ||
175 drm_mode_is_420_only(display_info, mode) ||
176 (!is_hdmi2_sink &&
177 drm_mode_is_420_also(display_info, mode)))
178 venc_freq *= 2;
179
180 vclk_freq = max(venc_freq, hdmi_freq);
181
182 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
183 venc_freq /= 2;
184
185 dev_dbg(priv->dev,
186 "%s: vclk:%lluHz phy=%lluHz venc=%lluHz hdmi=%lluHz\n",
187 __func__, phy_freq, vclk_freq, venc_freq, hdmi_freq);
188
189 return meson_vclk_vic_supported_freq(priv, phy_freq, vclk_freq);
190 }
191
meson_encoder_hdmi_atomic_enable(struct drm_bridge * bridge,struct drm_atomic_state * state)192 static void meson_encoder_hdmi_atomic_enable(struct drm_bridge *bridge,
193 struct drm_atomic_state *state)
194 {
195 struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
196 unsigned int ycrcb_map = VPU_HDMI_OUTPUT_CBYCR;
197 struct meson_drm *priv = encoder_hdmi->priv;
198 struct drm_connector_state *conn_state;
199 const struct drm_display_mode *mode;
200 struct drm_crtc_state *crtc_state;
201 struct drm_connector *connector;
202 bool yuv420_mode = false;
203 int vic;
204
205 connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
206 if (WARN_ON(!connector))
207 return;
208
209 conn_state = drm_atomic_get_new_connector_state(state, connector);
210 if (WARN_ON(!conn_state))
211 return;
212
213 crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
214 if (WARN_ON(!crtc_state))
215 return;
216
217 mode = &crtc_state->adjusted_mode;
218
219 vic = drm_match_cea_mode(mode);
220
221 dev_dbg(priv->dev, "\"%s\" vic %d\n", mode->name, vic);
222
223 if (encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYYVYY8_0_5X24) {
224 ycrcb_map = VPU_HDMI_OUTPUT_CRYCB;
225 yuv420_mode = true;
226 } else if (encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYVY8_1X16)
227 ycrcb_map = VPU_HDMI_OUTPUT_CRYCB;
228
229 /* VENC + VENC-DVI Mode setup */
230 meson_venc_hdmi_mode_set(priv, vic, ycrcb_map, yuv420_mode, mode);
231
232 /* VCLK Set clock */
233 meson_encoder_hdmi_set_vclk(encoder_hdmi, mode);
234
235 if (encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYYVYY8_0_5X24)
236 /* Setup YUV420 to HDMI-TX, no 10bit diphering */
237 writel_relaxed(2 | (2 << 2),
238 priv->io_base + _REG(VPU_HDMI_FMT_CTRL));
239 else if (encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYVY8_1X16)
240 /* Setup YUV422 to HDMI-TX, no 10bit diphering */
241 writel_relaxed(1 | (2 << 2),
242 priv->io_base + _REG(VPU_HDMI_FMT_CTRL));
243 else
244 /* Setup YUV444 to HDMI-TX, no 10bit diphering */
245 writel_relaxed(0, priv->io_base + _REG(VPU_HDMI_FMT_CTRL));
246
247 dev_dbg(priv->dev, "%s\n", priv->venc.hdmi_use_enci ? "VENCI" : "VENCP");
248
249 if (priv->venc.hdmi_use_enci)
250 writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
251 else
252 writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
253 }
254
meson_encoder_hdmi_atomic_disable(struct drm_bridge * bridge,struct drm_atomic_state * state)255 static void meson_encoder_hdmi_atomic_disable(struct drm_bridge *bridge,
256 struct drm_atomic_state *state)
257 {
258 struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
259 struct meson_drm *priv = encoder_hdmi->priv;
260
261 writel_bits_relaxed(0x3, 0,
262 priv->io_base + _REG(VPU_HDMI_SETTING));
263
264 writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
265 writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
266 }
267
268 static const u32 meson_encoder_hdmi_out_bus_fmts[] = {
269 MEDIA_BUS_FMT_YUV8_1X24,
270 MEDIA_BUS_FMT_UYVY8_1X16,
271 MEDIA_BUS_FMT_UYYVYY8_0_5X24,
272 };
273
274 static u32 *
meson_encoder_hdmi_get_inp_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,u32 output_fmt,unsigned int * num_input_fmts)275 meson_encoder_hdmi_get_inp_bus_fmts(struct drm_bridge *bridge,
276 struct drm_bridge_state *bridge_state,
277 struct drm_crtc_state *crtc_state,
278 struct drm_connector_state *conn_state,
279 u32 output_fmt,
280 unsigned int *num_input_fmts)
281 {
282 u32 *input_fmts = NULL;
283 int i;
284
285 *num_input_fmts = 0;
286
287 for (i = 0 ; i < ARRAY_SIZE(meson_encoder_hdmi_out_bus_fmts) ; ++i) {
288 if (output_fmt == meson_encoder_hdmi_out_bus_fmts[i]) {
289 *num_input_fmts = 1;
290 input_fmts = kcalloc(*num_input_fmts,
291 sizeof(*input_fmts),
292 GFP_KERNEL);
293 if (!input_fmts)
294 return NULL;
295
296 input_fmts[0] = output_fmt;
297
298 break;
299 }
300 }
301
302 return input_fmts;
303 }
304
meson_encoder_hdmi_atomic_check(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)305 static int meson_encoder_hdmi_atomic_check(struct drm_bridge *bridge,
306 struct drm_bridge_state *bridge_state,
307 struct drm_crtc_state *crtc_state,
308 struct drm_connector_state *conn_state)
309 {
310 struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
311 struct drm_connector_state *old_conn_state =
312 drm_atomic_get_old_connector_state(conn_state->state, conn_state->connector);
313 struct meson_drm *priv = encoder_hdmi->priv;
314
315 encoder_hdmi->output_bus_fmt = bridge_state->output_bus_cfg.format;
316
317 dev_dbg(priv->dev, "output_bus_fmt %lx\n", encoder_hdmi->output_bus_fmt);
318
319 if (!drm_connector_atomic_hdr_metadata_equal(old_conn_state, conn_state))
320 crtc_state->mode_changed = true;
321
322 return 0;
323 }
324
meson_encoder_hdmi_hpd_notify(struct drm_bridge * bridge,enum drm_connector_status status)325 static void meson_encoder_hdmi_hpd_notify(struct drm_bridge *bridge,
326 enum drm_connector_status status)
327 {
328 struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
329
330 if (!encoder_hdmi->cec_notifier)
331 return;
332
333 if (status == connector_status_connected) {
334 const struct drm_edid *drm_edid;
335 const struct edid *edid;
336
337 drm_edid = drm_bridge_edid_read(encoder_hdmi->next_bridge,
338 encoder_hdmi->connector);
339 if (!drm_edid)
340 return;
341
342 /*
343 * FIXME: The CEC physical address should be set using
344 * cec_notifier_set_phys_addr(encoder_hdmi->cec_notifier,
345 * connector->display_info.source_physical_address) from a path
346 * that has read the EDID and called
347 * drm_edid_connector_update().
348 */
349 edid = drm_edid_raw(drm_edid);
350
351 cec_notifier_set_phys_addr_from_edid(encoder_hdmi->cec_notifier, edid);
352
353 drm_edid_free(drm_edid);
354 } else
355 cec_notifier_phys_addr_invalidate(encoder_hdmi->cec_notifier);
356 }
357
358 static const struct drm_bridge_funcs meson_encoder_hdmi_bridge_funcs = {
359 .attach = meson_encoder_hdmi_attach,
360 .detach = meson_encoder_hdmi_detach,
361 .mode_valid = meson_encoder_hdmi_mode_valid,
362 .hpd_notify = meson_encoder_hdmi_hpd_notify,
363 .atomic_enable = meson_encoder_hdmi_atomic_enable,
364 .atomic_disable = meson_encoder_hdmi_atomic_disable,
365 .atomic_get_input_bus_fmts = meson_encoder_hdmi_get_inp_bus_fmts,
366 .atomic_check = meson_encoder_hdmi_atomic_check,
367 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
368 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
369 .atomic_reset = drm_atomic_helper_bridge_reset,
370 };
371
meson_encoder_hdmi_probe(struct meson_drm * priv)372 int meson_encoder_hdmi_probe(struct meson_drm *priv)
373 {
374 struct meson_encoder_hdmi *meson_encoder_hdmi;
375 struct platform_device *pdev;
376 struct device_node *remote;
377 int ret;
378
379 meson_encoder_hdmi = devm_kzalloc(priv->dev, sizeof(*meson_encoder_hdmi), GFP_KERNEL);
380 if (!meson_encoder_hdmi)
381 return -ENOMEM;
382
383 /* HDMI Transceiver Bridge */
384 remote = of_graph_get_remote_node(priv->dev->of_node, 1, 0);
385 if (!remote) {
386 dev_err(priv->dev, "HDMI transceiver device is disabled");
387 return 0;
388 }
389
390 meson_encoder_hdmi->next_bridge = of_drm_find_bridge(remote);
391 if (!meson_encoder_hdmi->next_bridge) {
392 ret = dev_err_probe(priv->dev, -EPROBE_DEFER,
393 "Failed to find HDMI transceiver bridge\n");
394 goto err_put_node;
395 }
396
397 /* HDMI Encoder Bridge */
398 meson_encoder_hdmi->bridge.funcs = &meson_encoder_hdmi_bridge_funcs;
399 meson_encoder_hdmi->bridge.of_node = priv->dev->of_node;
400 meson_encoder_hdmi->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
401 meson_encoder_hdmi->bridge.interlace_allowed = true;
402
403 drm_bridge_add(&meson_encoder_hdmi->bridge);
404
405 meson_encoder_hdmi->priv = priv;
406
407 /* Encoder */
408 ret = drm_simple_encoder_init(priv->drm, &meson_encoder_hdmi->encoder,
409 DRM_MODE_ENCODER_TMDS);
410 if (ret) {
411 dev_err_probe(priv->dev, ret, "Failed to init HDMI encoder\n");
412 goto err_put_node;
413 }
414
415 meson_encoder_hdmi->encoder.possible_crtcs = BIT(0);
416
417 /* Attach HDMI Encoder Bridge to Encoder */
418 ret = drm_bridge_attach(&meson_encoder_hdmi->encoder, &meson_encoder_hdmi->bridge, NULL,
419 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
420 if (ret) {
421 dev_err_probe(priv->dev, ret, "Failed to attach bridge\n");
422 goto err_put_node;
423 }
424
425 /* Initialize & attach Bridge Connector */
426 meson_encoder_hdmi->connector = drm_bridge_connector_init(priv->drm,
427 &meson_encoder_hdmi->encoder);
428 if (IS_ERR(meson_encoder_hdmi->connector)) {
429 ret = dev_err_probe(priv->dev,
430 PTR_ERR(meson_encoder_hdmi->connector),
431 "Unable to create HDMI bridge connector\n");
432 goto err_put_node;
433 }
434 drm_connector_attach_encoder(meson_encoder_hdmi->connector,
435 &meson_encoder_hdmi->encoder);
436
437 /*
438 * We should have now in place:
439 * encoder->[hdmi encoder bridge]->[dw-hdmi bridge]->[display connector bridge]->[display connector]
440 */
441
442 /*
443 * drm_connector_attach_max_bpc_property() requires the
444 * connector to have a state.
445 */
446 drm_atomic_helper_connector_reset(meson_encoder_hdmi->connector);
447
448 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL) ||
449 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
450 meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A))
451 drm_connector_attach_hdr_output_metadata_property(meson_encoder_hdmi->connector);
452
453 drm_connector_attach_max_bpc_property(meson_encoder_hdmi->connector, 8, 8);
454
455 /* Handle this here until handled by drm_bridge_connector_init() */
456 meson_encoder_hdmi->connector->ycbcr_420_allowed = true;
457
458 pdev = of_find_device_by_node(remote);
459 of_node_put(remote);
460 if (pdev) {
461 struct cec_connector_info conn_info;
462 struct cec_notifier *notifier;
463
464 cec_fill_conn_info_from_drm(&conn_info, meson_encoder_hdmi->connector);
465
466 notifier = cec_notifier_conn_register(&pdev->dev, NULL, &conn_info);
467 if (!notifier) {
468 put_device(&pdev->dev);
469 return -ENOMEM;
470 }
471
472 meson_encoder_hdmi->cec_notifier = notifier;
473 }
474
475 priv->encoders[MESON_ENC_HDMI] = meson_encoder_hdmi;
476
477 dev_dbg(priv->dev, "HDMI encoder initialized\n");
478
479 return 0;
480
481 err_put_node:
482 of_node_put(remote);
483 return ret;
484 }
485
meson_encoder_hdmi_remove(struct meson_drm * priv)486 void meson_encoder_hdmi_remove(struct meson_drm *priv)
487 {
488 struct meson_encoder_hdmi *meson_encoder_hdmi;
489
490 if (priv->encoders[MESON_ENC_HDMI]) {
491 meson_encoder_hdmi = priv->encoders[MESON_ENC_HDMI];
492 drm_bridge_remove(&meson_encoder_hdmi->bridge);
493 }
494 }
495