Loading...
Note: File does not exist in v4.6.
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 * Author: Chris Zhong <zyw@rock-chips.com>
5 */
6
7#include <linux/clk.h>
8#include <linux/component.h>
9#include <linux/extcon.h>
10#include <linux/firmware.h>
11#include <linux/mfd/syscon.h>
12#include <linux/phy/phy.h>
13#include <linux/regmap.h>
14#include <linux/reset.h>
15
16#include <sound/hdmi-codec.h>
17
18#include <drm/display/drm_dp_helper.h>
19#include <drm/drm_atomic_helper.h>
20#include <drm/drm_edid.h>
21#include <drm/drm_of.h>
22#include <drm/drm_probe_helper.h>
23#include <drm/drm_simple_kms_helper.h>
24
25#include "cdn-dp-core.h"
26#include "cdn-dp-reg.h"
27#include "rockchip_drm_vop.h"
28
29static inline struct cdn_dp_device *connector_to_dp(struct drm_connector *connector)
30{
31 return container_of(connector, struct cdn_dp_device, connector);
32}
33
34static inline struct cdn_dp_device *encoder_to_dp(struct drm_encoder *encoder)
35{
36 struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
37
38 return container_of(rkencoder, struct cdn_dp_device, encoder);
39}
40
41#define GRF_SOC_CON9 0x6224
42#define DP_SEL_VOP_LIT BIT(12)
43#define GRF_SOC_CON26 0x6268
44#define DPTX_HPD_SEL (3 << 12)
45#define DPTX_HPD_DEL (2 << 12)
46#define DPTX_HPD_SEL_MASK (3 << 28)
47
48#define CDN_FW_TIMEOUT_MS (64 * 1000)
49#define CDN_DPCD_TIMEOUT_MS 5000
50#define CDN_DP_FIRMWARE "rockchip/dptx.bin"
51MODULE_FIRMWARE(CDN_DP_FIRMWARE);
52
53struct cdn_dp_data {
54 u8 max_phy;
55};
56
57static struct cdn_dp_data rk3399_cdn_dp = {
58 .max_phy = 2,
59};
60
61static const struct of_device_id cdn_dp_dt_ids[] = {
62 { .compatible = "rockchip,rk3399-cdn-dp",
63 .data = (void *)&rk3399_cdn_dp },
64 {}
65};
66
67MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
68
69static int cdn_dp_grf_write(struct cdn_dp_device *dp,
70 unsigned int reg, unsigned int val)
71{
72 int ret;
73
74 ret = clk_prepare_enable(dp->grf_clk);
75 if (ret) {
76 DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n");
77 return ret;
78 }
79
80 ret = regmap_write(dp->grf, reg, val);
81 if (ret) {
82 DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
83 clk_disable_unprepare(dp->grf_clk);
84 return ret;
85 }
86
87 clk_disable_unprepare(dp->grf_clk);
88
89 return 0;
90}
91
92static int cdn_dp_clk_enable(struct cdn_dp_device *dp)
93{
94 int ret;
95 unsigned long rate;
96
97 ret = clk_prepare_enable(dp->pclk);
98 if (ret < 0) {
99 DRM_DEV_ERROR(dp->dev, "cannot enable dp pclk %d\n", ret);
100 goto err_pclk;
101 }
102
103 ret = clk_prepare_enable(dp->core_clk);
104 if (ret < 0) {
105 DRM_DEV_ERROR(dp->dev, "cannot enable core_clk %d\n", ret);
106 goto err_core_clk;
107 }
108
109 ret = pm_runtime_get_sync(dp->dev);
110 if (ret < 0) {
111 DRM_DEV_ERROR(dp->dev, "cannot get pm runtime %d\n", ret);
112 goto err_pm_runtime_get;
113 }
114
115 reset_control_assert(dp->core_rst);
116 reset_control_assert(dp->dptx_rst);
117 reset_control_assert(dp->apb_rst);
118 reset_control_deassert(dp->core_rst);
119 reset_control_deassert(dp->dptx_rst);
120 reset_control_deassert(dp->apb_rst);
121
122 rate = clk_get_rate(dp->core_clk);
123 if (!rate) {
124 DRM_DEV_ERROR(dp->dev, "get clk rate failed\n");
125 ret = -EINVAL;
126 goto err_set_rate;
127 }
128
129 cdn_dp_set_fw_clk(dp, rate);
130 cdn_dp_clock_reset(dp);
131
132 return 0;
133
134err_set_rate:
135 pm_runtime_put(dp->dev);
136err_pm_runtime_get:
137 clk_disable_unprepare(dp->core_clk);
138err_core_clk:
139 clk_disable_unprepare(dp->pclk);
140err_pclk:
141 return ret;
142}
143
144static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
145{
146 pm_runtime_put_sync(dp->dev);
147 clk_disable_unprepare(dp->pclk);
148 clk_disable_unprepare(dp->core_clk);
149}
150
151static int cdn_dp_get_port_lanes(struct cdn_dp_port *port)
152{
153 struct extcon_dev *edev = port->extcon;
154 union extcon_property_value property;
155 int dptx;
156 u8 lanes;
157
158 dptx = extcon_get_state(edev, EXTCON_DISP_DP);
159 if (dptx > 0) {
160 extcon_get_property(edev, EXTCON_DISP_DP,
161 EXTCON_PROP_USB_SS, &property);
162 if (property.intval)
163 lanes = 2;
164 else
165 lanes = 4;
166 } else {
167 lanes = 0;
168 }
169
170 return lanes;
171}
172
173static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count)
174{
175 int ret;
176 u8 value;
177
178 *sink_count = 0;
179 ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1);
180 if (ret)
181 return ret;
182
183 *sink_count = DP_GET_SINK_COUNT(value);
184 return 0;
185}
186
187static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp)
188{
189 struct cdn_dp_port *port;
190 int i, lanes;
191
192 for (i = 0; i < dp->ports; i++) {
193 port = dp->port[i];
194 lanes = cdn_dp_get_port_lanes(port);
195 if (lanes)
196 return port;
197 }
198 return NULL;
199}
200
201static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp)
202{
203 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS);
204 struct cdn_dp_port *port;
205 u8 sink_count = 0;
206
207 if (dp->active_port < 0 || dp->active_port >= dp->ports) {
208 DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n");
209 return false;
210 }
211
212 port = dp->port[dp->active_port];
213
214 /*
215 * Attempt to read sink count, retry in case the sink may not be ready.
216 *
217 * Sinks are *supposed* to come up within 1ms from an off state, but
218 * some docks need more time to power up.
219 */
220 while (time_before(jiffies, timeout)) {
221 if (!extcon_get_state(port->extcon, EXTCON_DISP_DP))
222 return false;
223
224 if (!cdn_dp_get_sink_count(dp, &sink_count))
225 return sink_count ? true : false;
226
227 usleep_range(5000, 10000);
228 }
229
230 DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n");
231 return false;
232}
233
234static enum drm_connector_status
235cdn_dp_connector_detect(struct drm_connector *connector, bool force)
236{
237 struct cdn_dp_device *dp = connector_to_dp(connector);
238 enum drm_connector_status status = connector_status_disconnected;
239
240 mutex_lock(&dp->lock);
241 if (dp->connected)
242 status = connector_status_connected;
243 mutex_unlock(&dp->lock);
244
245 return status;
246}
247
248static void cdn_dp_connector_destroy(struct drm_connector *connector)
249{
250 drm_connector_unregister(connector);
251 drm_connector_cleanup(connector);
252}
253
254static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs = {
255 .detect = cdn_dp_connector_detect,
256 .destroy = cdn_dp_connector_destroy,
257 .fill_modes = drm_helper_probe_single_connector_modes,
258 .reset = drm_atomic_helper_connector_reset,
259 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
260 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
261};
262
263static int cdn_dp_connector_get_modes(struct drm_connector *connector)
264{
265 struct cdn_dp_device *dp = connector_to_dp(connector);
266 struct edid *edid;
267 int ret = 0;
268
269 mutex_lock(&dp->lock);
270 edid = dp->edid;
271 if (edid) {
272 DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
273 edid->width_cm, edid->height_cm);
274
275 dp->sink_has_audio = drm_detect_monitor_audio(edid);
276 ret = drm_add_edid_modes(connector, edid);
277 if (ret)
278 drm_connector_update_edid_property(connector,
279 edid);
280 }
281 mutex_unlock(&dp->lock);
282
283 return ret;
284}
285
286static enum drm_mode_status
287cdn_dp_connector_mode_valid(struct drm_connector *connector,
288 struct drm_display_mode *mode)
289{
290 struct cdn_dp_device *dp = connector_to_dp(connector);
291 struct drm_display_info *display_info = &dp->connector.display_info;
292 u32 requested, actual, rate, sink_max, source_max = 0;
293 u8 lanes, bpc;
294
295 /* If DP is disconnected, every mode is invalid */
296 if (!dp->connected)
297 return MODE_BAD;
298
299 switch (display_info->bpc) {
300 case 10:
301 bpc = 10;
302 break;
303 case 6:
304 bpc = 6;
305 break;
306 default:
307 bpc = 8;
308 break;
309 }
310
311 requested = mode->clock * bpc * 3 / 1000;
312
313 source_max = dp->lanes;
314 sink_max = drm_dp_max_lane_count(dp->dpcd);
315 lanes = min(source_max, sink_max);
316
317 source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
318 sink_max = drm_dp_max_link_rate(dp->dpcd);
319 rate = min(source_max, sink_max);
320
321 actual = rate * lanes / 100;
322
323 /* efficiency is about 0.8 */
324 actual = actual * 8 / 10;
325
326 if (requested > actual) {
327 DRM_DEV_DEBUG_KMS(dp->dev,
328 "requested=%d, actual=%d, clock=%d\n",
329 requested, actual, mode->clock);
330 return MODE_CLOCK_HIGH;
331 }
332
333 return MODE_OK;
334}
335
336static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
337 .get_modes = cdn_dp_connector_get_modes,
338 .mode_valid = cdn_dp_connector_mode_valid,
339};
340
341static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
342{
343 int ret;
344 const u32 *iram_data, *dram_data;
345 const struct firmware *fw = dp->fw;
346 const struct cdn_firmware_header *hdr;
347
348 hdr = (struct cdn_firmware_header *)fw->data;
349 if (fw->size != le32_to_cpu(hdr->size_bytes)) {
350 DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
351 return -EINVAL;
352 }
353
354 iram_data = (const u32 *)(fw->data + hdr->header_size);
355 dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
356
357 ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
358 dram_data, hdr->dram_size);
359 if (ret)
360 return ret;
361
362 ret = cdn_dp_set_firmware_active(dp, true);
363 if (ret) {
364 DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
365 return ret;
366 }
367
368 return cdn_dp_event_config(dp);
369}
370
371static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
372{
373 int ret;
374
375 if (!cdn_dp_check_sink_connection(dp))
376 return -ENODEV;
377
378 ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
379 DP_RECEIVER_CAP_SIZE);
380 if (ret) {
381 DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
382 return ret;
383 }
384
385 kfree(dp->edid);
386 dp->edid = drm_do_get_edid(&dp->connector,
387 cdn_dp_get_edid_block, dp);
388 return 0;
389}
390
391static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
392{
393 union extcon_property_value property;
394 int ret;
395
396 if (!port->phy_enabled) {
397 ret = phy_power_on(port->phy);
398 if (ret) {
399 DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
400 ret);
401 goto err_phy;
402 }
403 port->phy_enabled = true;
404 }
405
406 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
407 DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
408 if (ret) {
409 DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
410 goto err_power_on;
411 }
412
413 ret = cdn_dp_get_hpd_status(dp);
414 if (ret <= 0) {
415 if (!ret)
416 DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
417 goto err_power_on;
418 }
419
420 ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
421 EXTCON_PROP_USB_TYPEC_POLARITY, &property);
422 if (ret) {
423 DRM_DEV_ERROR(dp->dev, "get property failed\n");
424 goto err_power_on;
425 }
426
427 port->lanes = cdn_dp_get_port_lanes(port);
428 ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
429 if (ret) {
430 DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
431 ret);
432 goto err_power_on;
433 }
434
435 dp->active_port = port->id;
436 return 0;
437
438err_power_on:
439 if (phy_power_off(port->phy))
440 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
441 else
442 port->phy_enabled = false;
443
444err_phy:
445 cdn_dp_grf_write(dp, GRF_SOC_CON26,
446 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
447 return ret;
448}
449
450static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
451 struct cdn_dp_port *port)
452{
453 int ret;
454
455 if (port->phy_enabled) {
456 ret = phy_power_off(port->phy);
457 if (ret) {
458 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
459 return ret;
460 }
461 }
462
463 port->phy_enabled = false;
464 port->lanes = 0;
465 dp->active_port = -1;
466 return 0;
467}
468
469static int cdn_dp_disable(struct cdn_dp_device *dp)
470{
471 int ret, i;
472
473 if (!dp->active)
474 return 0;
475
476 for (i = 0; i < dp->ports; i++)
477 cdn_dp_disable_phy(dp, dp->port[i]);
478
479 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
480 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
481 if (ret) {
482 DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
483 ret);
484 return ret;
485 }
486
487 cdn_dp_set_firmware_active(dp, false);
488 cdn_dp_clk_disable(dp);
489 dp->active = false;
490 dp->max_lanes = 0;
491 dp->max_rate = 0;
492 if (!dp->connected) {
493 kfree(dp->edid);
494 dp->edid = NULL;
495 }
496
497 return 0;
498}
499
500static int cdn_dp_enable(struct cdn_dp_device *dp)
501{
502 int ret, i, lanes;
503 struct cdn_dp_port *port;
504
505 port = cdn_dp_connected_port(dp);
506 if (!port) {
507 DRM_DEV_ERROR(dp->dev,
508 "Can't enable without connection\n");
509 return -ENODEV;
510 }
511
512 if (dp->active)
513 return 0;
514
515 ret = cdn_dp_clk_enable(dp);
516 if (ret)
517 return ret;
518
519 ret = cdn_dp_firmware_init(dp);
520 if (ret) {
521 DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
522 goto err_clk_disable;
523 }
524
525 /* only enable the port that connected with downstream device */
526 for (i = port->id; i < dp->ports; i++) {
527 port = dp->port[i];
528 lanes = cdn_dp_get_port_lanes(port);
529 if (lanes) {
530 ret = cdn_dp_enable_phy(dp, port);
531 if (ret)
532 continue;
533
534 ret = cdn_dp_get_sink_capability(dp);
535 if (ret) {
536 cdn_dp_disable_phy(dp, port);
537 } else {
538 dp->active = true;
539 dp->lanes = port->lanes;
540 return 0;
541 }
542 }
543 }
544
545err_clk_disable:
546 cdn_dp_clk_disable(dp);
547 return ret;
548}
549
550static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
551 struct drm_display_mode *mode,
552 struct drm_display_mode *adjusted)
553{
554 struct cdn_dp_device *dp = encoder_to_dp(encoder);
555 struct drm_display_info *display_info = &dp->connector.display_info;
556 struct video_info *video = &dp->video_info;
557
558 switch (display_info->bpc) {
559 case 10:
560 video->color_depth = 10;
561 break;
562 case 6:
563 video->color_depth = 6;
564 break;
565 default:
566 video->color_depth = 8;
567 break;
568 }
569
570 video->color_fmt = PXL_RGB;
571 video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
572 video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
573
574 drm_mode_copy(&dp->mode, adjusted);
575}
576
577static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
578{
579 u8 link_status[DP_LINK_STATUS_SIZE];
580 struct cdn_dp_port *port = cdn_dp_connected_port(dp);
581 u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
582
583 if (!port || !dp->max_rate || !dp->max_lanes)
584 return false;
585
586 if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
587 DP_LINK_STATUS_SIZE)) {
588 DRM_ERROR("Failed to get link status\n");
589 return false;
590 }
591
592 /* if link training is requested we should perform it always */
593 return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
594}
595
596static void cdn_dp_audio_handle_plugged_change(struct cdn_dp_device *dp,
597 bool plugged)
598{
599 if (dp->codec_dev)
600 dp->plugged_cb(dp->codec_dev, plugged);
601}
602
603static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
604{
605 struct cdn_dp_device *dp = encoder_to_dp(encoder);
606 int ret, val;
607
608 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
609 if (ret < 0) {
610 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
611 return;
612 }
613
614 DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
615 (ret) ? "LIT" : "BIG");
616 if (ret)
617 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
618 else
619 val = DP_SEL_VOP_LIT << 16;
620
621 ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
622 if (ret)
623 return;
624
625 mutex_lock(&dp->lock);
626
627 ret = cdn_dp_enable(dp);
628 if (ret) {
629 DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
630 ret);
631 goto out;
632 }
633 if (!cdn_dp_check_link_status(dp)) {
634 ret = cdn_dp_train_link(dp);
635 if (ret) {
636 DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
637 goto out;
638 }
639 }
640
641 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
642 if (ret) {
643 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
644 goto out;
645 }
646
647 ret = cdn_dp_config_video(dp);
648 if (ret) {
649 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
650 goto out;
651 }
652
653 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
654 if (ret) {
655 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
656 goto out;
657 }
658
659 cdn_dp_audio_handle_plugged_change(dp, true);
660
661out:
662 mutex_unlock(&dp->lock);
663}
664
665static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
666{
667 struct cdn_dp_device *dp = encoder_to_dp(encoder);
668 int ret;
669
670 mutex_lock(&dp->lock);
671 cdn_dp_audio_handle_plugged_change(dp, false);
672
673 if (dp->active) {
674 ret = cdn_dp_disable(dp);
675 if (ret) {
676 DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
677 ret);
678 }
679 }
680 mutex_unlock(&dp->lock);
681
682 /*
683 * In the following 2 cases, we need to run the event_work to re-enable
684 * the DP:
685 * 1. If there is not just one port device is connected, and remove one
686 * device from a port, the DP will be disabled here, at this case,
687 * run the event_work to re-open DP for the other port.
688 * 2. If re-training or re-config failed, the DP will be disabled here.
689 * run the event_work to re-connect it.
690 */
691 if (!dp->connected && cdn_dp_connected_port(dp))
692 schedule_work(&dp->event_work);
693}
694
695static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
696 struct drm_crtc_state *crtc_state,
697 struct drm_connector_state *conn_state)
698{
699 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
700
701 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
702 s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
703
704 return 0;
705}
706
707static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
708 .mode_set = cdn_dp_encoder_mode_set,
709 .enable = cdn_dp_encoder_enable,
710 .disable = cdn_dp_encoder_disable,
711 .atomic_check = cdn_dp_encoder_atomic_check,
712};
713
714static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
715{
716 struct device *dev = dp->dev;
717 struct device_node *np = dev->of_node;
718 struct platform_device *pdev = to_platform_device(dev);
719
720 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
721 if (IS_ERR(dp->grf)) {
722 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
723 return PTR_ERR(dp->grf);
724 }
725
726 dp->regs = devm_platform_ioremap_resource(pdev, 0);
727 if (IS_ERR(dp->regs)) {
728 DRM_DEV_ERROR(dev, "ioremap reg failed\n");
729 return PTR_ERR(dp->regs);
730 }
731
732 dp->core_clk = devm_clk_get(dev, "core-clk");
733 if (IS_ERR(dp->core_clk)) {
734 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
735 return PTR_ERR(dp->core_clk);
736 }
737
738 dp->pclk = devm_clk_get(dev, "pclk");
739 if (IS_ERR(dp->pclk)) {
740 DRM_DEV_ERROR(dev, "cannot get pclk\n");
741 return PTR_ERR(dp->pclk);
742 }
743
744 dp->spdif_clk = devm_clk_get(dev, "spdif");
745 if (IS_ERR(dp->spdif_clk)) {
746 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
747 return PTR_ERR(dp->spdif_clk);
748 }
749
750 dp->grf_clk = devm_clk_get(dev, "grf");
751 if (IS_ERR(dp->grf_clk)) {
752 DRM_DEV_ERROR(dev, "cannot get grf clk\n");
753 return PTR_ERR(dp->grf_clk);
754 }
755
756 dp->spdif_rst = devm_reset_control_get(dev, "spdif");
757 if (IS_ERR(dp->spdif_rst)) {
758 DRM_DEV_ERROR(dev, "no spdif reset control found\n");
759 return PTR_ERR(dp->spdif_rst);
760 }
761
762 dp->dptx_rst = devm_reset_control_get(dev, "dptx");
763 if (IS_ERR(dp->dptx_rst)) {
764 DRM_DEV_ERROR(dev, "no uphy reset control found\n");
765 return PTR_ERR(dp->dptx_rst);
766 }
767
768 dp->core_rst = devm_reset_control_get(dev, "core");
769 if (IS_ERR(dp->core_rst)) {
770 DRM_DEV_ERROR(dev, "no core reset control found\n");
771 return PTR_ERR(dp->core_rst);
772 }
773
774 dp->apb_rst = devm_reset_control_get(dev, "apb");
775 if (IS_ERR(dp->apb_rst)) {
776 DRM_DEV_ERROR(dev, "no apb reset control found\n");
777 return PTR_ERR(dp->apb_rst);
778 }
779
780 return 0;
781}
782
783static int cdn_dp_audio_hw_params(struct device *dev, void *data,
784 struct hdmi_codec_daifmt *daifmt,
785 struct hdmi_codec_params *params)
786{
787 struct cdn_dp_device *dp = dev_get_drvdata(dev);
788 struct audio_info audio = {
789 .sample_width = params->sample_width,
790 .sample_rate = params->sample_rate,
791 .channels = params->channels,
792 };
793 int ret;
794
795 mutex_lock(&dp->lock);
796 if (!dp->active) {
797 ret = -ENODEV;
798 goto out;
799 }
800
801 switch (daifmt->fmt) {
802 case HDMI_I2S:
803 audio.format = AFMT_I2S;
804 break;
805 case HDMI_SPDIF:
806 audio.format = AFMT_SPDIF;
807 break;
808 default:
809 DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
810 ret = -EINVAL;
811 goto out;
812 }
813
814 ret = cdn_dp_audio_config(dp, &audio);
815 if (!ret)
816 dp->audio_info = audio;
817
818out:
819 mutex_unlock(&dp->lock);
820 return ret;
821}
822
823static void cdn_dp_audio_shutdown(struct device *dev, void *data)
824{
825 struct cdn_dp_device *dp = dev_get_drvdata(dev);
826 int ret;
827
828 mutex_lock(&dp->lock);
829 if (!dp->active)
830 goto out;
831
832 ret = cdn_dp_audio_stop(dp, &dp->audio_info);
833 if (!ret)
834 dp->audio_info.format = AFMT_UNUSED;
835out:
836 mutex_unlock(&dp->lock);
837}
838
839static int cdn_dp_audio_mute_stream(struct device *dev, void *data,
840 bool enable, int direction)
841{
842 struct cdn_dp_device *dp = dev_get_drvdata(dev);
843 int ret;
844
845 mutex_lock(&dp->lock);
846 if (!dp->active) {
847 ret = -ENODEV;
848 goto out;
849 }
850
851 ret = cdn_dp_audio_mute(dp, enable);
852
853out:
854 mutex_unlock(&dp->lock);
855 return ret;
856}
857
858static int cdn_dp_audio_get_eld(struct device *dev, void *data,
859 u8 *buf, size_t len)
860{
861 struct cdn_dp_device *dp = dev_get_drvdata(dev);
862
863 memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
864
865 return 0;
866}
867
868static int cdn_dp_audio_hook_plugged_cb(struct device *dev, void *data,
869 hdmi_codec_plugged_cb fn,
870 struct device *codec_dev)
871{
872 struct cdn_dp_device *dp = dev_get_drvdata(dev);
873
874 mutex_lock(&dp->lock);
875 dp->plugged_cb = fn;
876 dp->codec_dev = codec_dev;
877 cdn_dp_audio_handle_plugged_change(dp, dp->connected);
878 mutex_unlock(&dp->lock);
879
880 return 0;
881}
882
883static const struct hdmi_codec_ops audio_codec_ops = {
884 .hw_params = cdn_dp_audio_hw_params,
885 .audio_shutdown = cdn_dp_audio_shutdown,
886 .mute_stream = cdn_dp_audio_mute_stream,
887 .get_eld = cdn_dp_audio_get_eld,
888 .hook_plugged_cb = cdn_dp_audio_hook_plugged_cb,
889 .no_capture_mute = 1,
890};
891
892static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
893 struct device *dev)
894{
895 struct hdmi_codec_pdata codec_data = {
896 .i2s = 1,
897 .spdif = 1,
898 .ops = &audio_codec_ops,
899 .max_i2s_channels = 8,
900 };
901
902 dp->audio_pdev = platform_device_register_data(
903 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
904 &codec_data, sizeof(codec_data));
905
906 return PTR_ERR_OR_ZERO(dp->audio_pdev);
907}
908
909static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
910{
911 int ret;
912 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
913 unsigned long sleep = 1000;
914
915 WARN_ON(!mutex_is_locked(&dp->lock));
916
917 if (dp->fw_loaded)
918 return 0;
919
920 /* Drop the lock before getting the firmware to avoid blocking boot */
921 mutex_unlock(&dp->lock);
922
923 while (time_before(jiffies, timeout)) {
924 ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
925 if (ret == -ENOENT) {
926 msleep(sleep);
927 sleep *= 2;
928 continue;
929 } else if (ret) {
930 DRM_DEV_ERROR(dp->dev,
931 "failed to request firmware: %d\n", ret);
932 goto out;
933 }
934
935 dp->fw_loaded = true;
936 ret = 0;
937 goto out;
938 }
939
940 DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
941 ret = -ETIMEDOUT;
942out:
943 mutex_lock(&dp->lock);
944 return ret;
945}
946
947static void cdn_dp_pd_event_work(struct work_struct *work)
948{
949 struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
950 event_work);
951 struct drm_connector *connector = &dp->connector;
952 enum drm_connector_status old_status;
953
954 int ret;
955
956 mutex_lock(&dp->lock);
957
958 if (dp->suspended)
959 goto out;
960
961 ret = cdn_dp_request_firmware(dp);
962 if (ret)
963 goto out;
964
965 dp->connected = true;
966
967 /* Not connected, notify userspace to disable the block */
968 if (!cdn_dp_connected_port(dp)) {
969 DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
970 dp->connected = false;
971
972 /* Connected but not enabled, enable the block */
973 } else if (!dp->active) {
974 DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
975 ret = cdn_dp_enable(dp);
976 if (ret) {
977 DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
978 dp->connected = false;
979 }
980
981 /* Enabled and connected to a dongle without a sink, notify userspace */
982 } else if (!cdn_dp_check_sink_connection(dp)) {
983 DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
984 dp->connected = false;
985
986 /* Enabled and connected with a sink, re-train if requested */
987 } else if (!cdn_dp_check_link_status(dp)) {
988 unsigned int rate = dp->max_rate;
989 unsigned int lanes = dp->max_lanes;
990 struct drm_display_mode *mode = &dp->mode;
991
992 DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
993 ret = cdn_dp_train_link(dp);
994 if (ret) {
995 dp->connected = false;
996 DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
997 goto out;
998 }
999
1000 /* If training result is changed, update the video config */
1001 if (mode->clock &&
1002 (rate != dp->max_rate || lanes != dp->max_lanes)) {
1003 ret = cdn_dp_config_video(dp);
1004 if (ret) {
1005 dp->connected = false;
1006 DRM_DEV_ERROR(dp->dev,
1007 "Failed to config video %d\n",
1008 ret);
1009 }
1010 }
1011 }
1012
1013out:
1014 mutex_unlock(&dp->lock);
1015
1016 old_status = connector->status;
1017 connector->status = connector->funcs->detect(connector, false);
1018 if (old_status != connector->status)
1019 drm_kms_helper_hotplug_event(dp->drm_dev);
1020}
1021
1022static int cdn_dp_pd_event(struct notifier_block *nb,
1023 unsigned long event, void *priv)
1024{
1025 struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1026 event_nb);
1027 struct cdn_dp_device *dp = port->dp;
1028
1029 /*
1030 * It would be nice to be able to just do the work inline right here.
1031 * However, we need to make a bunch of calls that might sleep in order
1032 * to turn on the block/phy, so use a worker instead.
1033 */
1034 schedule_work(&dp->event_work);
1035
1036 return NOTIFY_DONE;
1037}
1038
1039static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1040{
1041 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1042 struct drm_encoder *encoder;
1043 struct drm_connector *connector;
1044 struct cdn_dp_port *port;
1045 struct drm_device *drm_dev = data;
1046 int ret, i;
1047
1048 ret = cdn_dp_parse_dt(dp);
1049 if (ret < 0)
1050 return ret;
1051
1052 dp->drm_dev = drm_dev;
1053 dp->connected = false;
1054 dp->active = false;
1055 dp->active_port = -1;
1056 dp->fw_loaded = false;
1057
1058 INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1059
1060 encoder = &dp->encoder.encoder;
1061
1062 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1063 dev->of_node);
1064 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1065
1066 ret = drm_simple_encoder_init(drm_dev, encoder,
1067 DRM_MODE_ENCODER_TMDS);
1068 if (ret) {
1069 DRM_ERROR("failed to initialize encoder with drm\n");
1070 return ret;
1071 }
1072
1073 drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1074
1075 connector = &dp->connector;
1076 connector->polled = DRM_CONNECTOR_POLL_HPD;
1077 connector->dpms = DRM_MODE_DPMS_OFF;
1078
1079 ret = drm_connector_init(drm_dev, connector,
1080 &cdn_dp_atomic_connector_funcs,
1081 DRM_MODE_CONNECTOR_DisplayPort);
1082 if (ret) {
1083 DRM_ERROR("failed to initialize connector with drm\n");
1084 goto err_free_encoder;
1085 }
1086
1087 drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1088
1089 ret = drm_connector_attach_encoder(connector, encoder);
1090 if (ret) {
1091 DRM_ERROR("failed to attach connector and encoder\n");
1092 goto err_free_connector;
1093 }
1094
1095 for (i = 0; i < dp->ports; i++) {
1096 port = dp->port[i];
1097
1098 port->event_nb.notifier_call = cdn_dp_pd_event;
1099 ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1100 EXTCON_DISP_DP,
1101 &port->event_nb);
1102 if (ret) {
1103 DRM_DEV_ERROR(dev,
1104 "register EXTCON_DISP_DP notifier err\n");
1105 goto err_free_connector;
1106 }
1107 }
1108
1109 pm_runtime_enable(dev);
1110
1111 schedule_work(&dp->event_work);
1112
1113 return 0;
1114
1115err_free_connector:
1116 drm_connector_cleanup(connector);
1117err_free_encoder:
1118 drm_encoder_cleanup(encoder);
1119 return ret;
1120}
1121
1122static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1123{
1124 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1125 struct drm_encoder *encoder = &dp->encoder.encoder;
1126 struct drm_connector *connector = &dp->connector;
1127
1128 cancel_work_sync(&dp->event_work);
1129 cdn_dp_encoder_disable(encoder);
1130 encoder->funcs->destroy(encoder);
1131 connector->funcs->destroy(connector);
1132
1133 pm_runtime_disable(dev);
1134 if (dp->fw_loaded)
1135 release_firmware(dp->fw);
1136 kfree(dp->edid);
1137 dp->edid = NULL;
1138}
1139
1140static const struct component_ops cdn_dp_component_ops = {
1141 .bind = cdn_dp_bind,
1142 .unbind = cdn_dp_unbind,
1143};
1144
1145static int cdn_dp_suspend(struct device *dev)
1146{
1147 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1148 int ret = 0;
1149
1150 mutex_lock(&dp->lock);
1151 if (dp->active)
1152 ret = cdn_dp_disable(dp);
1153 dp->suspended = true;
1154 mutex_unlock(&dp->lock);
1155
1156 return ret;
1157}
1158
1159static __maybe_unused int cdn_dp_resume(struct device *dev)
1160{
1161 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1162
1163 mutex_lock(&dp->lock);
1164 dp->suspended = false;
1165 if (dp->fw_loaded)
1166 schedule_work(&dp->event_work);
1167 mutex_unlock(&dp->lock);
1168
1169 return 0;
1170}
1171
1172static int cdn_dp_probe(struct platform_device *pdev)
1173{
1174 struct device *dev = &pdev->dev;
1175 const struct of_device_id *match;
1176 struct cdn_dp_data *dp_data;
1177 struct cdn_dp_port *port;
1178 struct cdn_dp_device *dp;
1179 struct extcon_dev *extcon;
1180 struct phy *phy;
1181 int i;
1182
1183 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1184 if (!dp)
1185 return -ENOMEM;
1186 dp->dev = dev;
1187
1188 match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1189 dp_data = (struct cdn_dp_data *)match->data;
1190
1191 for (i = 0; i < dp_data->max_phy; i++) {
1192 extcon = extcon_get_edev_by_phandle(dev, i);
1193 phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1194
1195 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1196 PTR_ERR(phy) == -EPROBE_DEFER)
1197 return -EPROBE_DEFER;
1198
1199 if (IS_ERR(extcon) || IS_ERR(phy))
1200 continue;
1201
1202 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1203 if (!port)
1204 return -ENOMEM;
1205
1206 port->extcon = extcon;
1207 port->phy = phy;
1208 port->dp = dp;
1209 port->id = i;
1210 dp->port[dp->ports++] = port;
1211 }
1212
1213 if (!dp->ports) {
1214 DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1215 return -EINVAL;
1216 }
1217
1218 mutex_init(&dp->lock);
1219 dev_set_drvdata(dev, dp);
1220
1221 cdn_dp_audio_codec_init(dp, dev);
1222
1223 return component_add(dev, &cdn_dp_component_ops);
1224}
1225
1226static int cdn_dp_remove(struct platform_device *pdev)
1227{
1228 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1229
1230 platform_device_unregister(dp->audio_pdev);
1231 cdn_dp_suspend(dp->dev);
1232 component_del(&pdev->dev, &cdn_dp_component_ops);
1233
1234 return 0;
1235}
1236
1237static void cdn_dp_shutdown(struct platform_device *pdev)
1238{
1239 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1240
1241 cdn_dp_suspend(dp->dev);
1242}
1243
1244static const struct dev_pm_ops cdn_dp_pm_ops = {
1245 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1246 cdn_dp_resume)
1247};
1248
1249struct platform_driver cdn_dp_driver = {
1250 .probe = cdn_dp_probe,
1251 .remove = cdn_dp_remove,
1252 .shutdown = cdn_dp_shutdown,
1253 .driver = {
1254 .name = "cdn-dp",
1255 .owner = THIS_MODULE,
1256 .of_match_table = of_match_ptr(cdn_dp_dt_ids),
1257 .pm = &cdn_dp_pm_ops,
1258 },
1259};