GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / gpu / drm / rockchip / cdn-dp-core.c
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author: Chris Zhong <zyw@rock-chips.com>
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_dp_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_of.h>
21
22 #include <linux/clk.h>
23 #include <linux/component.h>
24 #include <linux/extcon.h>
25 #include <linux/firmware.h>
26 #include <linux/regmap.h>
27 #include <linux/reset.h>
28 #include <linux/mfd/syscon.h>
29 #include <linux/phy/phy.h>
30
31 #include <sound/hdmi-codec.h>
32
33 #include "cdn-dp-core.h"
34 #include "cdn-dp-reg.h"
35 #include "rockchip_drm_vop.h"
36
37 #define connector_to_dp(c) \
38                 container_of(c, struct cdn_dp_device, connector)
39
40 #define encoder_to_dp(c) \
41                 container_of(c, struct cdn_dp_device, encoder)
42
43 #define GRF_SOC_CON9            0x6224
44 #define DP_SEL_VOP_LIT          BIT(12)
45 #define GRF_SOC_CON26           0x6268
46 #define DPTX_HPD_SEL            (3 << 12)
47 #define DPTX_HPD_DEL            (2 << 12)
48 #define DPTX_HPD_SEL_MASK       (3 << 28)
49
50 #define CDN_FW_TIMEOUT_MS       (64 * 1000)
51 #define CDN_DPCD_TIMEOUT_MS     5000
52 #define CDN_DP_FIRMWARE         "/*(DEBLOBBED)*/"
53
54 struct cdn_dp_data {
55         u8 max_phy;
56 };
57
58 struct cdn_dp_data rk3399_cdn_dp = {
59         .max_phy = 2,
60 };
61
62 static const struct of_device_id cdn_dp_dt_ids[] = {
63         { .compatible = "rockchip,rk3399-cdn-dp",
64                 .data = (void *)&rk3399_cdn_dp },
65         {}
66 };
67
68 MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
69
70 static int cdn_dp_grf_write(struct cdn_dp_device *dp,
71                             unsigned int reg, unsigned int val)
72 {
73         int ret;
74
75         ret = clk_prepare_enable(dp->grf_clk);
76         if (ret) {
77                 DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n");
78                 return ret;
79         }
80
81         ret = regmap_write(dp->grf, reg, val);
82         if (ret) {
83                 DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
84                 clk_disable_unprepare(dp->grf_clk);
85                 return ret;
86         }
87
88         clk_disable_unprepare(dp->grf_clk);
89
90         return 0;
91 }
92
93 static int cdn_dp_clk_enable(struct cdn_dp_device *dp)
94 {
95         int ret;
96         unsigned long rate;
97
98         ret = clk_prepare_enable(dp->pclk);
99         if (ret < 0) {
100                 DRM_DEV_ERROR(dp->dev, "cannot enable dp pclk %d\n", ret);
101                 goto err_pclk;
102         }
103
104         ret = clk_prepare_enable(dp->core_clk);
105         if (ret < 0) {
106                 DRM_DEV_ERROR(dp->dev, "cannot enable core_clk %d\n", ret);
107                 goto err_core_clk;
108         }
109
110         ret = pm_runtime_get_sync(dp->dev);
111         if (ret < 0) {
112                 DRM_DEV_ERROR(dp->dev, "cannot get pm runtime %d\n", ret);
113                 goto err_pm_runtime_get;
114         }
115
116         reset_control_assert(dp->core_rst);
117         reset_control_assert(dp->dptx_rst);
118         reset_control_assert(dp->apb_rst);
119         reset_control_deassert(dp->core_rst);
120         reset_control_deassert(dp->dptx_rst);
121         reset_control_deassert(dp->apb_rst);
122
123         rate = clk_get_rate(dp->core_clk);
124         if (!rate) {
125                 DRM_DEV_ERROR(dp->dev, "get clk rate failed\n");
126                 ret = -EINVAL;
127                 goto err_set_rate;
128         }
129
130         cdn_dp_set_fw_clk(dp, rate);
131         cdn_dp_clock_reset(dp);
132
133         return 0;
134
135 err_set_rate:
136         pm_runtime_put(dp->dev);
137 err_pm_runtime_get:
138         clk_disable_unprepare(dp->core_clk);
139 err_core_clk:
140         clk_disable_unprepare(dp->pclk);
141 err_pclk:
142         return ret;
143 }
144
145 static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
146 {
147         pm_runtime_put_sync(dp->dev);
148         clk_disable_unprepare(dp->pclk);
149         clk_disable_unprepare(dp->core_clk);
150 }
151
152 static int cdn_dp_get_port_lanes(struct cdn_dp_port *port)
153 {
154         struct extcon_dev *edev = port->extcon;
155         union extcon_property_value property;
156         int dptx;
157         u8 lanes;
158
159         dptx = extcon_get_state(edev, EXTCON_DISP_DP);
160         if (dptx > 0) {
161                 extcon_get_property(edev, EXTCON_DISP_DP,
162                                     EXTCON_PROP_USB_SS, &property);
163                 if (property.intval)
164                         lanes = 2;
165                 else
166                         lanes = 4;
167         } else {
168                 lanes = 0;
169         }
170
171         return lanes;
172 }
173
174 static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count)
175 {
176         int ret;
177         u8 value;
178
179         *sink_count = 0;
180         ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1);
181         if (ret)
182                 return ret;
183
184         *sink_count = DP_GET_SINK_COUNT(value);
185         return 0;
186 }
187
188 static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp)
189 {
190         struct cdn_dp_port *port;
191         int i, lanes;
192
193         for (i = 0; i < dp->ports; i++) {
194                 port = dp->port[i];
195                 lanes = cdn_dp_get_port_lanes(port);
196                 if (lanes)
197                         return port;
198         }
199         return NULL;
200 }
201
202 static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp)
203 {
204         unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS);
205         struct cdn_dp_port *port;
206         u8 sink_count = 0;
207
208         if (dp->active_port < 0 || dp->active_port >= dp->ports) {
209                 DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n");
210                 return false;
211         }
212
213         port = dp->port[dp->active_port];
214
215         /*
216          * Attempt to read sink count, retry in case the sink may not be ready.
217          *
218          * Sinks are *supposed* to come up within 1ms from an off state, but
219          * some docks need more time to power up.
220          */
221         while (time_before(jiffies, timeout)) {
222                 if (!extcon_get_state(port->extcon, EXTCON_DISP_DP))
223                         return false;
224
225                 if (!cdn_dp_get_sink_count(dp, &sink_count))
226                         return sink_count ? true : false;
227
228                 usleep_range(5000, 10000);
229         }
230
231         DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n");
232         return false;
233 }
234
235 static enum drm_connector_status
236 cdn_dp_connector_detect(struct drm_connector *connector, bool force)
237 {
238         struct cdn_dp_device *dp = connector_to_dp(connector);
239         enum drm_connector_status status = connector_status_disconnected;
240
241         mutex_lock(&dp->lock);
242         if (dp->connected)
243                 status = connector_status_connected;
244         mutex_unlock(&dp->lock);
245
246         return status;
247 }
248
249 static void cdn_dp_connector_destroy(struct drm_connector *connector)
250 {
251         drm_connector_unregister(connector);
252         drm_connector_cleanup(connector);
253 }
254
255 static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs = {
256         .detect = cdn_dp_connector_detect,
257         .destroy = cdn_dp_connector_destroy,
258         .fill_modes = drm_helper_probe_single_connector_modes,
259         .reset = drm_atomic_helper_connector_reset,
260         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
261         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
262 };
263
264 static int cdn_dp_connector_get_modes(struct drm_connector *connector)
265 {
266         struct cdn_dp_device *dp = connector_to_dp(connector);
267         struct edid *edid;
268         int ret = 0;
269
270         mutex_lock(&dp->lock);
271         edid = dp->edid;
272         if (edid) {
273                 DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
274                                   edid->width_cm, edid->height_cm);
275
276                 dp->sink_has_audio = drm_detect_monitor_audio(edid);
277                 ret = drm_add_edid_modes(connector, edid);
278                 if (ret)
279                         drm_connector_update_edid_property(connector,
280                                                                 edid);
281         }
282         mutex_unlock(&dp->lock);
283
284         return ret;
285 }
286
287 static int cdn_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
336 static 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
341 static 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
371 static 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
391 static 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
438 err_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
444 err_phy:
445         cdn_dp_grf_write(dp, GRF_SOC_CON26,
446                          DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
447         return ret;
448 }
449
450 static 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
469 static 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->link.rate = 0;
491         dp->link.num_lanes = 0;
492         if (!dp->connected) {
493                 kfree(dp->edid);
494                 dp->edid = NULL;
495         }
496
497         return 0;
498 }
499
500 static 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
545 err_clk_disable:
546         cdn_dp_clk_disable(dp);
547         return ret;
548 }
549
550 static 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         memcpy(&dp->mode, adjusted, sizeof(*mode));
575 }
576
577 static 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->link.rate || !dp->link.num_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
596 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
597 {
598         struct cdn_dp_device *dp = encoder_to_dp(encoder);
599         int ret, val;
600
601         ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
602         if (ret < 0) {
603                 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
604                 return;
605         }
606
607         DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
608                           (ret) ? "LIT" : "BIG");
609         if (ret)
610                 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
611         else
612                 val = DP_SEL_VOP_LIT << 16;
613
614         ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
615         if (ret)
616                 return;
617
618         mutex_lock(&dp->lock);
619
620         ret = cdn_dp_enable(dp);
621         if (ret) {
622                 DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
623                               ret);
624                 goto out;
625         }
626         if (!cdn_dp_check_link_status(dp)) {
627                 ret = cdn_dp_train_link(dp);
628                 if (ret) {
629                         DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
630                         goto out;
631                 }
632         }
633
634         ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
635         if (ret) {
636                 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
637                 goto out;
638         }
639
640         ret = cdn_dp_config_video(dp);
641         if (ret) {
642                 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
643                 goto out;
644         }
645
646         ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
647         if (ret) {
648                 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
649                 goto out;
650         }
651 out:
652         mutex_unlock(&dp->lock);
653 }
654
655 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
656 {
657         struct cdn_dp_device *dp = encoder_to_dp(encoder);
658         int ret;
659
660         mutex_lock(&dp->lock);
661         if (dp->active) {
662                 ret = cdn_dp_disable(dp);
663                 if (ret) {
664                         DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
665                                       ret);
666                 }
667         }
668         mutex_unlock(&dp->lock);
669
670         /*
671          * In the following 2 cases, we need to run the event_work to re-enable
672          * the DP:
673          * 1. If there is not just one port device is connected, and remove one
674          *    device from a port, the DP will be disabled here, at this case,
675          *    run the event_work to re-open DP for the other port.
676          * 2. If re-training or re-config failed, the DP will be disabled here.
677          *    run the event_work to re-connect it.
678          */
679         if (!dp->connected && cdn_dp_connected_port(dp))
680                 schedule_work(&dp->event_work);
681 }
682
683 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
684                                        struct drm_crtc_state *crtc_state,
685                                        struct drm_connector_state *conn_state)
686 {
687         struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
688
689         s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
690         s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
691
692         return 0;
693 }
694
695 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
696         .mode_set = cdn_dp_encoder_mode_set,
697         .enable = cdn_dp_encoder_enable,
698         .disable = cdn_dp_encoder_disable,
699         .atomic_check = cdn_dp_encoder_atomic_check,
700 };
701
702 static const struct drm_encoder_funcs cdn_dp_encoder_funcs = {
703         .destroy = drm_encoder_cleanup,
704 };
705
706 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
707 {
708         struct device *dev = dp->dev;
709         struct device_node *np = dev->of_node;
710         struct platform_device *pdev = to_platform_device(dev);
711         struct resource *res;
712
713         dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
714         if (IS_ERR(dp->grf)) {
715                 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
716                 return PTR_ERR(dp->grf);
717         }
718
719         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
720         dp->regs = devm_ioremap_resource(dev, res);
721         if (IS_ERR(dp->regs)) {
722                 DRM_DEV_ERROR(dev, "ioremap reg failed\n");
723                 return PTR_ERR(dp->regs);
724         }
725
726         dp->core_clk = devm_clk_get(dev, "core-clk");
727         if (IS_ERR(dp->core_clk)) {
728                 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
729                 return PTR_ERR(dp->core_clk);
730         }
731
732         dp->pclk = devm_clk_get(dev, "pclk");
733         if (IS_ERR(dp->pclk)) {
734                 DRM_DEV_ERROR(dev, "cannot get pclk\n");
735                 return PTR_ERR(dp->pclk);
736         }
737
738         dp->spdif_clk = devm_clk_get(dev, "spdif");
739         if (IS_ERR(dp->spdif_clk)) {
740                 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
741                 return PTR_ERR(dp->spdif_clk);
742         }
743
744         dp->grf_clk = devm_clk_get(dev, "grf");
745         if (IS_ERR(dp->grf_clk)) {
746                 DRM_DEV_ERROR(dev, "cannot get grf clk\n");
747                 return PTR_ERR(dp->grf_clk);
748         }
749
750         dp->spdif_rst = devm_reset_control_get(dev, "spdif");
751         if (IS_ERR(dp->spdif_rst)) {
752                 DRM_DEV_ERROR(dev, "no spdif reset control found\n");
753                 return PTR_ERR(dp->spdif_rst);
754         }
755
756         dp->dptx_rst = devm_reset_control_get(dev, "dptx");
757         if (IS_ERR(dp->dptx_rst)) {
758                 DRM_DEV_ERROR(dev, "no uphy reset control found\n");
759                 return PTR_ERR(dp->dptx_rst);
760         }
761
762         dp->core_rst = devm_reset_control_get(dev, "core");
763         if (IS_ERR(dp->core_rst)) {
764                 DRM_DEV_ERROR(dev, "no core reset control found\n");
765                 return PTR_ERR(dp->core_rst);
766         }
767
768         dp->apb_rst = devm_reset_control_get(dev, "apb");
769         if (IS_ERR(dp->apb_rst)) {
770                 DRM_DEV_ERROR(dev, "no apb reset control found\n");
771                 return PTR_ERR(dp->apb_rst);
772         }
773
774         return 0;
775 }
776
777 static int cdn_dp_audio_hw_params(struct device *dev,  void *data,
778                                   struct hdmi_codec_daifmt *daifmt,
779                                   struct hdmi_codec_params *params)
780 {
781         struct cdn_dp_device *dp = dev_get_drvdata(dev);
782         struct audio_info audio = {
783                 .sample_width = params->sample_width,
784                 .sample_rate = params->sample_rate,
785                 .channels = params->channels,
786         };
787         int ret;
788
789         mutex_lock(&dp->lock);
790         if (!dp->active) {
791                 ret = -ENODEV;
792                 goto out;
793         }
794
795         switch (daifmt->fmt) {
796         case HDMI_I2S:
797                 audio.format = AFMT_I2S;
798                 break;
799         case HDMI_SPDIF:
800                 audio.format = AFMT_SPDIF;
801                 break;
802         default:
803                 DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
804                 ret = -EINVAL;
805                 goto out;
806         }
807
808         ret = cdn_dp_audio_config(dp, &audio);
809         if (!ret)
810                 dp->audio_info = audio;
811
812 out:
813         mutex_unlock(&dp->lock);
814         return ret;
815 }
816
817 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
818 {
819         struct cdn_dp_device *dp = dev_get_drvdata(dev);
820         int ret;
821
822         mutex_lock(&dp->lock);
823         if (!dp->active)
824                 goto out;
825
826         ret = cdn_dp_audio_stop(dp, &dp->audio_info);
827         if (!ret)
828                 dp->audio_info.format = AFMT_UNUSED;
829 out:
830         mutex_unlock(&dp->lock);
831 }
832
833 static int cdn_dp_audio_digital_mute(struct device *dev, void *data,
834                                      bool enable)
835 {
836         struct cdn_dp_device *dp = dev_get_drvdata(dev);
837         int ret;
838
839         mutex_lock(&dp->lock);
840         if (!dp->active) {
841                 ret = -ENODEV;
842                 goto out;
843         }
844
845         ret = cdn_dp_audio_mute(dp, enable);
846
847 out:
848         mutex_unlock(&dp->lock);
849         return ret;
850 }
851
852 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
853                                 u8 *buf, size_t len)
854 {
855         struct cdn_dp_device *dp = dev_get_drvdata(dev);
856
857         memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
858
859         return 0;
860 }
861
862 static const struct hdmi_codec_ops audio_codec_ops = {
863         .hw_params = cdn_dp_audio_hw_params,
864         .audio_shutdown = cdn_dp_audio_shutdown,
865         .digital_mute = cdn_dp_audio_digital_mute,
866         .get_eld = cdn_dp_audio_get_eld,
867 };
868
869 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
870                                    struct device *dev)
871 {
872         struct hdmi_codec_pdata codec_data = {
873                 .i2s = 1,
874                 .spdif = 1,
875                 .ops = &audio_codec_ops,
876                 .max_i2s_channels = 8,
877         };
878
879         dp->audio_pdev = platform_device_register_data(
880                          dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
881                          &codec_data, sizeof(codec_data));
882
883         return PTR_ERR_OR_ZERO(dp->audio_pdev);
884 }
885
886 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
887 {
888         int ret;
889         unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
890         unsigned long sleep = 1000;
891
892         WARN_ON(!mutex_is_locked(&dp->lock));
893
894         if (dp->fw_loaded)
895                 return 0;
896
897         /* Drop the lock before getting the firmware to avoid blocking boot */
898         mutex_unlock(&dp->lock);
899
900         while (time_before(jiffies, timeout)) {
901                 ret = reject_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
902                 if (ret == -ENOENT) {
903                         msleep(sleep);
904                         sleep *= 2;
905                         continue;
906                 } else if (ret) {
907                         DRM_DEV_ERROR(dp->dev,
908                                       "failed to request firmware: %d\n", ret);
909                         goto out;
910                 }
911
912                 dp->fw_loaded = true;
913                 ret = 0;
914                 goto out;
915         }
916
917         DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
918         ret = -ETIMEDOUT;
919 out:
920         mutex_lock(&dp->lock);
921         return ret;
922 }
923
924 static void cdn_dp_pd_event_work(struct work_struct *work)
925 {
926         struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
927                                                 event_work);
928         struct drm_connector *connector = &dp->connector;
929         enum drm_connector_status old_status;
930
931         int ret;
932
933         mutex_lock(&dp->lock);
934
935         if (dp->suspended)
936                 goto out;
937
938         ret = cdn_dp_request_firmware(dp);
939         if (ret)
940                 goto out;
941
942         dp->connected = true;
943
944         /* Not connected, notify userspace to disable the block */
945         if (!cdn_dp_connected_port(dp)) {
946                 DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
947                 dp->connected = false;
948
949         /* Connected but not enabled, enable the block */
950         } else if (!dp->active) {
951                 DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
952                 ret = cdn_dp_enable(dp);
953                 if (ret) {
954                         DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
955                         dp->connected = false;
956                 }
957
958         /* Enabled and connected to a dongle without a sink, notify userspace */
959         } else if (!cdn_dp_check_sink_connection(dp)) {
960                 DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
961                 dp->connected = false;
962
963         /* Enabled and connected with a sink, re-train if requested */
964         } else if (!cdn_dp_check_link_status(dp)) {
965                 unsigned int rate = dp->link.rate;
966                 unsigned int lanes = dp->link.num_lanes;
967                 struct drm_display_mode *mode = &dp->mode;
968
969                 DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
970                 ret = cdn_dp_train_link(dp);
971                 if (ret) {
972                         dp->connected = false;
973                         DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
974                         goto out;
975                 }
976
977                 /* If training result is changed, update the video config */
978                 if (mode->clock &&
979                     (rate != dp->link.rate || lanes != dp->link.num_lanes)) {
980                         ret = cdn_dp_config_video(dp);
981                         if (ret) {
982                                 dp->connected = false;
983                                 DRM_DEV_ERROR(dp->dev,
984                                               "Failed to config video %d\n",
985                                               ret);
986                         }
987                 }
988         }
989
990 out:
991         mutex_unlock(&dp->lock);
992
993         old_status = connector->status;
994         connector->status = connector->funcs->detect(connector, false);
995         if (old_status != connector->status)
996                 drm_kms_helper_hotplug_event(dp->drm_dev);
997 }
998
999 static int cdn_dp_pd_event(struct notifier_block *nb,
1000                            unsigned long event, void *priv)
1001 {
1002         struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1003                                                 event_nb);
1004         struct cdn_dp_device *dp = port->dp;
1005
1006         /*
1007          * It would be nice to be able to just do the work inline right here.
1008          * However, we need to make a bunch of calls that might sleep in order
1009          * to turn on the block/phy, so use a worker instead.
1010          */
1011         schedule_work(&dp->event_work);
1012
1013         return NOTIFY_DONE;
1014 }
1015
1016 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1017 {
1018         struct cdn_dp_device *dp = dev_get_drvdata(dev);
1019         struct drm_encoder *encoder;
1020         struct drm_connector *connector;
1021         struct cdn_dp_port *port;
1022         struct drm_device *drm_dev = data;
1023         int ret, i;
1024
1025         ret = cdn_dp_parse_dt(dp);
1026         if (ret < 0)
1027                 return ret;
1028
1029         dp->drm_dev = drm_dev;
1030         dp->connected = false;
1031         dp->active = false;
1032         dp->active_port = -1;
1033         dp->fw_loaded = false;
1034
1035         INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1036
1037         encoder = &dp->encoder;
1038
1039         encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1040                                                              dev->of_node);
1041         DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1042
1043         ret = drm_encoder_init(drm_dev, encoder, &cdn_dp_encoder_funcs,
1044                                DRM_MODE_ENCODER_TMDS, NULL);
1045         if (ret) {
1046                 DRM_ERROR("failed to initialize encoder with drm\n");
1047                 return ret;
1048         }
1049
1050         drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1051
1052         connector = &dp->connector;
1053         connector->polled = DRM_CONNECTOR_POLL_HPD;
1054         connector->dpms = DRM_MODE_DPMS_OFF;
1055
1056         ret = drm_connector_init(drm_dev, connector,
1057                                  &cdn_dp_atomic_connector_funcs,
1058                                  DRM_MODE_CONNECTOR_DisplayPort);
1059         if (ret) {
1060                 DRM_ERROR("failed to initialize connector with drm\n");
1061                 goto err_free_encoder;
1062         }
1063
1064         drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1065
1066         ret = drm_connector_attach_encoder(connector, encoder);
1067         if (ret) {
1068                 DRM_ERROR("failed to attach connector and encoder\n");
1069                 goto err_free_connector;
1070         }
1071
1072         for (i = 0; i < dp->ports; i++) {
1073                 port = dp->port[i];
1074
1075                 port->event_nb.notifier_call = cdn_dp_pd_event;
1076                 ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1077                                                     EXTCON_DISP_DP,
1078                                                     &port->event_nb);
1079                 if (ret) {
1080                         DRM_DEV_ERROR(dev,
1081                                       "register EXTCON_DISP_DP notifier err\n");
1082                         goto err_free_connector;
1083                 }
1084         }
1085
1086         pm_runtime_enable(dev);
1087
1088         schedule_work(&dp->event_work);
1089
1090         return 0;
1091
1092 err_free_connector:
1093         drm_connector_cleanup(connector);
1094 err_free_encoder:
1095         drm_encoder_cleanup(encoder);
1096         return ret;
1097 }
1098
1099 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1100 {
1101         struct cdn_dp_device *dp = dev_get_drvdata(dev);
1102         struct drm_encoder *encoder = &dp->encoder;
1103         struct drm_connector *connector = &dp->connector;
1104
1105         cancel_work_sync(&dp->event_work);
1106         cdn_dp_encoder_disable(encoder);
1107         encoder->funcs->destroy(encoder);
1108         connector->funcs->destroy(connector);
1109
1110         pm_runtime_disable(dev);
1111         if (dp->fw_loaded)
1112                 release_firmware(dp->fw);
1113         kfree(dp->edid);
1114         dp->edid = NULL;
1115 }
1116
1117 static const struct component_ops cdn_dp_component_ops = {
1118         .bind = cdn_dp_bind,
1119         .unbind = cdn_dp_unbind,
1120 };
1121
1122 int cdn_dp_suspend(struct device *dev)
1123 {
1124         struct cdn_dp_device *dp = dev_get_drvdata(dev);
1125         int ret = 0;
1126
1127         mutex_lock(&dp->lock);
1128         if (dp->active)
1129                 ret = cdn_dp_disable(dp);
1130         dp->suspended = true;
1131         mutex_unlock(&dp->lock);
1132
1133         return ret;
1134 }
1135
1136 int cdn_dp_resume(struct device *dev)
1137 {
1138         struct cdn_dp_device *dp = dev_get_drvdata(dev);
1139
1140         mutex_lock(&dp->lock);
1141         dp->suspended = false;
1142         if (dp->fw_loaded)
1143                 schedule_work(&dp->event_work);
1144         mutex_unlock(&dp->lock);
1145
1146         return 0;
1147 }
1148
1149 static int cdn_dp_probe(struct platform_device *pdev)
1150 {
1151         struct device *dev = &pdev->dev;
1152         const struct of_device_id *match;
1153         struct cdn_dp_data *dp_data;
1154         struct cdn_dp_port *port;
1155         struct cdn_dp_device *dp;
1156         struct extcon_dev *extcon;
1157         struct phy *phy;
1158         int i;
1159
1160         dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1161         if (!dp)
1162                 return -ENOMEM;
1163         dp->dev = dev;
1164
1165         match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1166         dp_data = (struct cdn_dp_data *)match->data;
1167
1168         for (i = 0; i < dp_data->max_phy; i++) {
1169                 extcon = extcon_get_edev_by_phandle(dev, i);
1170                 phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1171
1172                 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1173                     PTR_ERR(phy) == -EPROBE_DEFER)
1174                         return -EPROBE_DEFER;
1175
1176                 if (IS_ERR(extcon) || IS_ERR(phy))
1177                         continue;
1178
1179                 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1180                 if (!port)
1181                         return -ENOMEM;
1182
1183                 port->extcon = extcon;
1184                 port->phy = phy;
1185                 port->dp = dp;
1186                 port->id = i;
1187                 dp->port[dp->ports++] = port;
1188         }
1189
1190         if (!dp->ports) {
1191                 DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1192                 return -EINVAL;
1193         }
1194
1195         mutex_init(&dp->lock);
1196         dev_set_drvdata(dev, dp);
1197
1198         cdn_dp_audio_codec_init(dp, dev);
1199
1200         return component_add(dev, &cdn_dp_component_ops);
1201 }
1202
1203 static int cdn_dp_remove(struct platform_device *pdev)
1204 {
1205         struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1206
1207         platform_device_unregister(dp->audio_pdev);
1208         cdn_dp_suspend(dp->dev);
1209         component_del(&pdev->dev, &cdn_dp_component_ops);
1210
1211         return 0;
1212 }
1213
1214 static void cdn_dp_shutdown(struct platform_device *pdev)
1215 {
1216         struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1217
1218         cdn_dp_suspend(dp->dev);
1219 }
1220
1221 static const struct dev_pm_ops cdn_dp_pm_ops = {
1222         SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1223                                 cdn_dp_resume)
1224 };
1225
1226 struct platform_driver cdn_dp_driver = {
1227         .probe = cdn_dp_probe,
1228         .remove = cdn_dp_remove,
1229         .shutdown = cdn_dp_shutdown,
1230         .driver = {
1231                    .name = "cdn-dp",
1232                    .owner = THIS_MODULE,
1233                    .of_match_table = of_match_ptr(cdn_dp_dt_ids),
1234                    .pm = &cdn_dp_pm_ops,
1235         },
1236 };