GNU Linux-libre 4.19.286-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 enum drm_mode_status
288 cdn_dp_connector_mode_valid(struct drm_connector *connector,
289                             struct drm_display_mode *mode)
290 {
291         struct cdn_dp_device *dp = connector_to_dp(connector);
292         struct drm_display_info *display_info = &dp->connector.display_info;
293         u32 requested, actual, rate, sink_max, source_max = 0;
294         u8 lanes, bpc;
295
296         /* If DP is disconnected, every mode is invalid */
297         if (!dp->connected)
298                 return MODE_BAD;
299
300         switch (display_info->bpc) {
301         case 10:
302                 bpc = 10;
303                 break;
304         case 6:
305                 bpc = 6;
306                 break;
307         default:
308                 bpc = 8;
309                 break;
310         }
311
312         requested = mode->clock * bpc * 3 / 1000;
313
314         source_max = dp->lanes;
315         sink_max = drm_dp_max_lane_count(dp->dpcd);
316         lanes = min(source_max, sink_max);
317
318         source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
319         sink_max = drm_dp_max_link_rate(dp->dpcd);
320         rate = min(source_max, sink_max);
321
322         actual = rate * lanes / 100;
323
324         /* efficiency is about 0.8 */
325         actual = actual * 8 / 10;
326
327         if (requested > actual) {
328                 DRM_DEV_DEBUG_KMS(dp->dev,
329                                   "requested=%d, actual=%d, clock=%d\n",
330                                   requested, actual, mode->clock);
331                 return MODE_CLOCK_HIGH;
332         }
333
334         return MODE_OK;
335 }
336
337 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
338         .get_modes = cdn_dp_connector_get_modes,
339         .mode_valid = cdn_dp_connector_mode_valid,
340 };
341
342 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
343 {
344         int ret;
345         const u32 *iram_data, *dram_data;
346         const struct firmware *fw = dp->fw;
347         const struct cdn_firmware_header *hdr;
348
349         hdr = (struct cdn_firmware_header *)fw->data;
350         if (fw->size != le32_to_cpu(hdr->size_bytes)) {
351                 DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
352                 return -EINVAL;
353         }
354
355         iram_data = (const u32 *)(fw->data + hdr->header_size);
356         dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
357
358         ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
359                                    dram_data, hdr->dram_size);
360         if (ret)
361                 return ret;
362
363         ret = cdn_dp_set_firmware_active(dp, true);
364         if (ret) {
365                 DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
366                 return ret;
367         }
368
369         return cdn_dp_event_config(dp);
370 }
371
372 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
373 {
374         int ret;
375
376         if (!cdn_dp_check_sink_connection(dp))
377                 return -ENODEV;
378
379         ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
380                                DP_RECEIVER_CAP_SIZE);
381         if (ret) {
382                 DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
383                 return ret;
384         }
385
386         kfree(dp->edid);
387         dp->edid = drm_do_get_edid(&dp->connector,
388                                    cdn_dp_get_edid_block, dp);
389         return 0;
390 }
391
392 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
393 {
394         union extcon_property_value property;
395         int ret;
396
397         if (!port->phy_enabled) {
398                 ret = phy_power_on(port->phy);
399                 if (ret) {
400                         DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
401                                       ret);
402                         goto err_phy;
403                 }
404                 port->phy_enabled = true;
405         }
406
407         ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
408                                DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
409         if (ret) {
410                 DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
411                 goto err_power_on;
412         }
413
414         ret = cdn_dp_get_hpd_status(dp);
415         if (ret <= 0) {
416                 if (!ret)
417                         DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
418                 goto err_power_on;
419         }
420
421         ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
422                                   EXTCON_PROP_USB_TYPEC_POLARITY, &property);
423         if (ret) {
424                 DRM_DEV_ERROR(dp->dev, "get property failed\n");
425                 goto err_power_on;
426         }
427
428         port->lanes = cdn_dp_get_port_lanes(port);
429         ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
430         if (ret) {
431                 DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
432                               ret);
433                 goto err_power_on;
434         }
435
436         dp->active_port = port->id;
437         return 0;
438
439 err_power_on:
440         if (phy_power_off(port->phy))
441                 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
442         else
443                 port->phy_enabled = false;
444
445 err_phy:
446         cdn_dp_grf_write(dp, GRF_SOC_CON26,
447                          DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
448         return ret;
449 }
450
451 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
452                               struct cdn_dp_port *port)
453 {
454         int ret;
455
456         if (port->phy_enabled) {
457                 ret = phy_power_off(port->phy);
458                 if (ret) {
459                         DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
460                         return ret;
461                 }
462         }
463
464         port->phy_enabled = false;
465         port->lanes = 0;
466         dp->active_port = -1;
467         return 0;
468 }
469
470 static int cdn_dp_disable(struct cdn_dp_device *dp)
471 {
472         int ret, i;
473
474         if (!dp->active)
475                 return 0;
476
477         for (i = 0; i < dp->ports; i++)
478                 cdn_dp_disable_phy(dp, dp->port[i]);
479
480         ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
481                                DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
482         if (ret) {
483                 DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
484                               ret);
485                 return ret;
486         }
487
488         cdn_dp_set_firmware_active(dp, false);
489         cdn_dp_clk_disable(dp);
490         dp->active = false;
491         dp->link.rate = 0;
492         dp->link.num_lanes = 0;
493         if (!dp->connected) {
494                 kfree(dp->edid);
495                 dp->edid = NULL;
496         }
497
498         return 0;
499 }
500
501 static int cdn_dp_enable(struct cdn_dp_device *dp)
502 {
503         int ret, i, lanes;
504         struct cdn_dp_port *port;
505
506         port = cdn_dp_connected_port(dp);
507         if (!port) {
508                 DRM_DEV_ERROR(dp->dev,
509                               "Can't enable without connection\n");
510                 return -ENODEV;
511         }
512
513         if (dp->active)
514                 return 0;
515
516         ret = cdn_dp_clk_enable(dp);
517         if (ret)
518                 return ret;
519
520         ret = cdn_dp_firmware_init(dp);
521         if (ret) {
522                 DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
523                 goto err_clk_disable;
524         }
525
526         /* only enable the port that connected with downstream device */
527         for (i = port->id; i < dp->ports; i++) {
528                 port = dp->port[i];
529                 lanes = cdn_dp_get_port_lanes(port);
530                 if (lanes) {
531                         ret = cdn_dp_enable_phy(dp, port);
532                         if (ret)
533                                 continue;
534
535                         ret = cdn_dp_get_sink_capability(dp);
536                         if (ret) {
537                                 cdn_dp_disable_phy(dp, port);
538                         } else {
539                                 dp->active = true;
540                                 dp->lanes = port->lanes;
541                                 return 0;
542                         }
543                 }
544         }
545
546 err_clk_disable:
547         cdn_dp_clk_disable(dp);
548         return ret;
549 }
550
551 static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
552                                     struct drm_display_mode *mode,
553                                     struct drm_display_mode *adjusted)
554 {
555         struct cdn_dp_device *dp = encoder_to_dp(encoder);
556         struct drm_display_info *display_info = &dp->connector.display_info;
557         struct video_info *video = &dp->video_info;
558
559         switch (display_info->bpc) {
560         case 10:
561                 video->color_depth = 10;
562                 break;
563         case 6:
564                 video->color_depth = 6;
565                 break;
566         default:
567                 video->color_depth = 8;
568                 break;
569         }
570
571         video->color_fmt = PXL_RGB;
572         video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
573         video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
574
575         memcpy(&dp->mode, adjusted, sizeof(*mode));
576 }
577
578 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
579 {
580         u8 link_status[DP_LINK_STATUS_SIZE];
581         struct cdn_dp_port *port = cdn_dp_connected_port(dp);
582         u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
583
584         if (!port || !dp->link.rate || !dp->link.num_lanes)
585                 return false;
586
587         if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
588                              DP_LINK_STATUS_SIZE)) {
589                 DRM_ERROR("Failed to get link status\n");
590                 return false;
591         }
592
593         /* if link training is requested we should perform it always */
594         return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
595 }
596
597 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
598 {
599         struct cdn_dp_device *dp = encoder_to_dp(encoder);
600         int ret, val;
601
602         ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
603         if (ret < 0) {
604                 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
605                 return;
606         }
607
608         DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
609                           (ret) ? "LIT" : "BIG");
610         if (ret)
611                 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
612         else
613                 val = DP_SEL_VOP_LIT << 16;
614
615         ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
616         if (ret)
617                 return;
618
619         mutex_lock(&dp->lock);
620
621         ret = cdn_dp_enable(dp);
622         if (ret) {
623                 DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
624                               ret);
625                 goto out;
626         }
627         if (!cdn_dp_check_link_status(dp)) {
628                 ret = cdn_dp_train_link(dp);
629                 if (ret) {
630                         DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
631                         goto out;
632                 }
633         }
634
635         ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
636         if (ret) {
637                 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
638                 goto out;
639         }
640
641         ret = cdn_dp_config_video(dp);
642         if (ret) {
643                 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
644                 goto out;
645         }
646
647         ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
648         if (ret) {
649                 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
650                 goto out;
651         }
652 out:
653         mutex_unlock(&dp->lock);
654 }
655
656 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
657 {
658         struct cdn_dp_device *dp = encoder_to_dp(encoder);
659         int ret;
660
661         mutex_lock(&dp->lock);
662         if (dp->active) {
663                 ret = cdn_dp_disable(dp);
664                 if (ret) {
665                         DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
666                                       ret);
667                 }
668         }
669         mutex_unlock(&dp->lock);
670
671         /*
672          * In the following 2 cases, we need to run the event_work to re-enable
673          * the DP:
674          * 1. If there is not just one port device is connected, and remove one
675          *    device from a port, the DP will be disabled here, at this case,
676          *    run the event_work to re-open DP for the other port.
677          * 2. If re-training or re-config failed, the DP will be disabled here.
678          *    run the event_work to re-connect it.
679          */
680         if (!dp->connected && cdn_dp_connected_port(dp))
681                 schedule_work(&dp->event_work);
682 }
683
684 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
685                                        struct drm_crtc_state *crtc_state,
686                                        struct drm_connector_state *conn_state)
687 {
688         struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
689
690         s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
691         s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
692
693         return 0;
694 }
695
696 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
697         .mode_set = cdn_dp_encoder_mode_set,
698         .enable = cdn_dp_encoder_enable,
699         .disable = cdn_dp_encoder_disable,
700         .atomic_check = cdn_dp_encoder_atomic_check,
701 };
702
703 static const struct drm_encoder_funcs cdn_dp_encoder_funcs = {
704         .destroy = drm_encoder_cleanup,
705 };
706
707 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
708 {
709         struct device *dev = dp->dev;
710         struct device_node *np = dev->of_node;
711         struct platform_device *pdev = to_platform_device(dev);
712         struct resource *res;
713
714         dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
715         if (IS_ERR(dp->grf)) {
716                 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
717                 return PTR_ERR(dp->grf);
718         }
719
720         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
721         dp->regs = devm_ioremap_resource(dev, res);
722         if (IS_ERR(dp->regs)) {
723                 DRM_DEV_ERROR(dev, "ioremap reg failed\n");
724                 return PTR_ERR(dp->regs);
725         }
726
727         dp->core_clk = devm_clk_get(dev, "core-clk");
728         if (IS_ERR(dp->core_clk)) {
729                 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
730                 return PTR_ERR(dp->core_clk);
731         }
732
733         dp->pclk = devm_clk_get(dev, "pclk");
734         if (IS_ERR(dp->pclk)) {
735                 DRM_DEV_ERROR(dev, "cannot get pclk\n");
736                 return PTR_ERR(dp->pclk);
737         }
738
739         dp->spdif_clk = devm_clk_get(dev, "spdif");
740         if (IS_ERR(dp->spdif_clk)) {
741                 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
742                 return PTR_ERR(dp->spdif_clk);
743         }
744
745         dp->grf_clk = devm_clk_get(dev, "grf");
746         if (IS_ERR(dp->grf_clk)) {
747                 DRM_DEV_ERROR(dev, "cannot get grf clk\n");
748                 return PTR_ERR(dp->grf_clk);
749         }
750
751         dp->spdif_rst = devm_reset_control_get(dev, "spdif");
752         if (IS_ERR(dp->spdif_rst)) {
753                 DRM_DEV_ERROR(dev, "no spdif reset control found\n");
754                 return PTR_ERR(dp->spdif_rst);
755         }
756
757         dp->dptx_rst = devm_reset_control_get(dev, "dptx");
758         if (IS_ERR(dp->dptx_rst)) {
759                 DRM_DEV_ERROR(dev, "no uphy reset control found\n");
760                 return PTR_ERR(dp->dptx_rst);
761         }
762
763         dp->core_rst = devm_reset_control_get(dev, "core");
764         if (IS_ERR(dp->core_rst)) {
765                 DRM_DEV_ERROR(dev, "no core reset control found\n");
766                 return PTR_ERR(dp->core_rst);
767         }
768
769         dp->apb_rst = devm_reset_control_get(dev, "apb");
770         if (IS_ERR(dp->apb_rst)) {
771                 DRM_DEV_ERROR(dev, "no apb reset control found\n");
772                 return PTR_ERR(dp->apb_rst);
773         }
774
775         return 0;
776 }
777
778 static int cdn_dp_audio_hw_params(struct device *dev,  void *data,
779                                   struct hdmi_codec_daifmt *daifmt,
780                                   struct hdmi_codec_params *params)
781 {
782         struct cdn_dp_device *dp = dev_get_drvdata(dev);
783         struct audio_info audio = {
784                 .sample_width = params->sample_width,
785                 .sample_rate = params->sample_rate,
786                 .channels = params->channels,
787         };
788         int ret;
789
790         mutex_lock(&dp->lock);
791         if (!dp->active) {
792                 ret = -ENODEV;
793                 goto out;
794         }
795
796         switch (daifmt->fmt) {
797         case HDMI_I2S:
798                 audio.format = AFMT_I2S;
799                 break;
800         case HDMI_SPDIF:
801                 audio.format = AFMT_SPDIF;
802                 break;
803         default:
804                 DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
805                 ret = -EINVAL;
806                 goto out;
807         }
808
809         ret = cdn_dp_audio_config(dp, &audio);
810         if (!ret)
811                 dp->audio_info = audio;
812
813 out:
814         mutex_unlock(&dp->lock);
815         return ret;
816 }
817
818 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
819 {
820         struct cdn_dp_device *dp = dev_get_drvdata(dev);
821         int ret;
822
823         mutex_lock(&dp->lock);
824         if (!dp->active)
825                 goto out;
826
827         ret = cdn_dp_audio_stop(dp, &dp->audio_info);
828         if (!ret)
829                 dp->audio_info.format = AFMT_UNUSED;
830 out:
831         mutex_unlock(&dp->lock);
832 }
833
834 static int cdn_dp_audio_digital_mute(struct device *dev, void *data,
835                                      bool enable)
836 {
837         struct cdn_dp_device *dp = dev_get_drvdata(dev);
838         int ret;
839
840         mutex_lock(&dp->lock);
841         if (!dp->active) {
842                 ret = -ENODEV;
843                 goto out;
844         }
845
846         ret = cdn_dp_audio_mute(dp, enable);
847
848 out:
849         mutex_unlock(&dp->lock);
850         return ret;
851 }
852
853 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
854                                 u8 *buf, size_t len)
855 {
856         struct cdn_dp_device *dp = dev_get_drvdata(dev);
857
858         memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
859
860         return 0;
861 }
862
863 static const struct hdmi_codec_ops audio_codec_ops = {
864         .hw_params = cdn_dp_audio_hw_params,
865         .audio_shutdown = cdn_dp_audio_shutdown,
866         .digital_mute = cdn_dp_audio_digital_mute,
867         .get_eld = cdn_dp_audio_get_eld,
868 };
869
870 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
871                                    struct device *dev)
872 {
873         struct hdmi_codec_pdata codec_data = {
874                 .i2s = 1,
875                 .spdif = 1,
876                 .ops = &audio_codec_ops,
877                 .max_i2s_channels = 8,
878         };
879
880         dp->audio_pdev = platform_device_register_data(
881                          dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
882                          &codec_data, sizeof(codec_data));
883
884         return PTR_ERR_OR_ZERO(dp->audio_pdev);
885 }
886
887 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
888 {
889         int ret;
890         unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
891         unsigned long sleep = 1000;
892
893         WARN_ON(!mutex_is_locked(&dp->lock));
894
895         if (dp->fw_loaded)
896                 return 0;
897
898         /* Drop the lock before getting the firmware to avoid blocking boot */
899         mutex_unlock(&dp->lock);
900
901         while (time_before(jiffies, timeout)) {
902                 ret = reject_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
903                 if (ret == -ENOENT) {
904                         msleep(sleep);
905                         sleep *= 2;
906                         continue;
907                 } else if (ret) {
908                         DRM_DEV_ERROR(dp->dev,
909                                       "failed to request firmware: %d\n", ret);
910                         goto out;
911                 }
912
913                 dp->fw_loaded = true;
914                 ret = 0;
915                 goto out;
916         }
917
918         DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
919         ret = -ETIMEDOUT;
920 out:
921         mutex_lock(&dp->lock);
922         return ret;
923 }
924
925 static void cdn_dp_pd_event_work(struct work_struct *work)
926 {
927         struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
928                                                 event_work);
929         struct drm_connector *connector = &dp->connector;
930         enum drm_connector_status old_status;
931
932         int ret;
933
934         mutex_lock(&dp->lock);
935
936         if (dp->suspended)
937                 goto out;
938
939         ret = cdn_dp_request_firmware(dp);
940         if (ret)
941                 goto out;
942
943         dp->connected = true;
944
945         /* Not connected, notify userspace to disable the block */
946         if (!cdn_dp_connected_port(dp)) {
947                 DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
948                 dp->connected = false;
949
950         /* Connected but not enabled, enable the block */
951         } else if (!dp->active) {
952                 DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
953                 ret = cdn_dp_enable(dp);
954                 if (ret) {
955                         DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
956                         dp->connected = false;
957                 }
958
959         /* Enabled and connected to a dongle without a sink, notify userspace */
960         } else if (!cdn_dp_check_sink_connection(dp)) {
961                 DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
962                 dp->connected = false;
963
964         /* Enabled and connected with a sink, re-train if requested */
965         } else if (!cdn_dp_check_link_status(dp)) {
966                 unsigned int rate = dp->link.rate;
967                 unsigned int lanes = dp->link.num_lanes;
968                 struct drm_display_mode *mode = &dp->mode;
969
970                 DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
971                 ret = cdn_dp_train_link(dp);
972                 if (ret) {
973                         dp->connected = false;
974                         DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
975                         goto out;
976                 }
977
978                 /* If training result is changed, update the video config */
979                 if (mode->clock &&
980                     (rate != dp->link.rate || lanes != dp->link.num_lanes)) {
981                         ret = cdn_dp_config_video(dp);
982                         if (ret) {
983                                 dp->connected = false;
984                                 DRM_DEV_ERROR(dp->dev,
985                                               "Failed to config video %d\n",
986                                               ret);
987                         }
988                 }
989         }
990
991 out:
992         mutex_unlock(&dp->lock);
993
994         old_status = connector->status;
995         connector->status = connector->funcs->detect(connector, false);
996         if (old_status != connector->status)
997                 drm_kms_helper_hotplug_event(dp->drm_dev);
998 }
999
1000 static int cdn_dp_pd_event(struct notifier_block *nb,
1001                            unsigned long event, void *priv)
1002 {
1003         struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1004                                                 event_nb);
1005         struct cdn_dp_device *dp = port->dp;
1006
1007         /*
1008          * It would be nice to be able to just do the work inline right here.
1009          * However, we need to make a bunch of calls that might sleep in order
1010          * to turn on the block/phy, so use a worker instead.
1011          */
1012         schedule_work(&dp->event_work);
1013
1014         return NOTIFY_DONE;
1015 }
1016
1017 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1018 {
1019         struct cdn_dp_device *dp = dev_get_drvdata(dev);
1020         struct drm_encoder *encoder;
1021         struct drm_connector *connector;
1022         struct cdn_dp_port *port;
1023         struct drm_device *drm_dev = data;
1024         int ret, i;
1025
1026         ret = cdn_dp_parse_dt(dp);
1027         if (ret < 0)
1028                 return ret;
1029
1030         dp->drm_dev = drm_dev;
1031         dp->connected = false;
1032         dp->active = false;
1033         dp->active_port = -1;
1034         dp->fw_loaded = false;
1035
1036         INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1037
1038         encoder = &dp->encoder;
1039
1040         encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1041                                                              dev->of_node);
1042         DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1043
1044         ret = drm_encoder_init(drm_dev, encoder, &cdn_dp_encoder_funcs,
1045                                DRM_MODE_ENCODER_TMDS, NULL);
1046         if (ret) {
1047                 DRM_ERROR("failed to initialize encoder with drm\n");
1048                 return ret;
1049         }
1050
1051         drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1052
1053         connector = &dp->connector;
1054         connector->polled = DRM_CONNECTOR_POLL_HPD;
1055         connector->dpms = DRM_MODE_DPMS_OFF;
1056
1057         ret = drm_connector_init(drm_dev, connector,
1058                                  &cdn_dp_atomic_connector_funcs,
1059                                  DRM_MODE_CONNECTOR_DisplayPort);
1060         if (ret) {
1061                 DRM_ERROR("failed to initialize connector with drm\n");
1062                 goto err_free_encoder;
1063         }
1064
1065         drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1066
1067         ret = drm_connector_attach_encoder(connector, encoder);
1068         if (ret) {
1069                 DRM_ERROR("failed to attach connector and encoder\n");
1070                 goto err_free_connector;
1071         }
1072
1073         for (i = 0; i < dp->ports; i++) {
1074                 port = dp->port[i];
1075
1076                 port->event_nb.notifier_call = cdn_dp_pd_event;
1077                 ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1078                                                     EXTCON_DISP_DP,
1079                                                     &port->event_nb);
1080                 if (ret) {
1081                         DRM_DEV_ERROR(dev,
1082                                       "register EXTCON_DISP_DP notifier err\n");
1083                         goto err_free_connector;
1084                 }
1085         }
1086
1087         pm_runtime_enable(dev);
1088
1089         schedule_work(&dp->event_work);
1090
1091         return 0;
1092
1093 err_free_connector:
1094         drm_connector_cleanup(connector);
1095 err_free_encoder:
1096         drm_encoder_cleanup(encoder);
1097         return ret;
1098 }
1099
1100 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1101 {
1102         struct cdn_dp_device *dp = dev_get_drvdata(dev);
1103         struct drm_encoder *encoder = &dp->encoder;
1104         struct drm_connector *connector = &dp->connector;
1105
1106         cancel_work_sync(&dp->event_work);
1107         cdn_dp_encoder_disable(encoder);
1108         encoder->funcs->destroy(encoder);
1109         connector->funcs->destroy(connector);
1110
1111         pm_runtime_disable(dev);
1112         if (dp->fw_loaded)
1113                 release_firmware(dp->fw);
1114         kfree(dp->edid);
1115         dp->edid = NULL;
1116 }
1117
1118 static const struct component_ops cdn_dp_component_ops = {
1119         .bind = cdn_dp_bind,
1120         .unbind = cdn_dp_unbind,
1121 };
1122
1123 int cdn_dp_suspend(struct device *dev)
1124 {
1125         struct cdn_dp_device *dp = dev_get_drvdata(dev);
1126         int ret = 0;
1127
1128         mutex_lock(&dp->lock);
1129         if (dp->active)
1130                 ret = cdn_dp_disable(dp);
1131         dp->suspended = true;
1132         mutex_unlock(&dp->lock);
1133
1134         return ret;
1135 }
1136
1137 int cdn_dp_resume(struct device *dev)
1138 {
1139         struct cdn_dp_device *dp = dev_get_drvdata(dev);
1140
1141         mutex_lock(&dp->lock);
1142         dp->suspended = false;
1143         if (dp->fw_loaded)
1144                 schedule_work(&dp->event_work);
1145         mutex_unlock(&dp->lock);
1146
1147         return 0;
1148 }
1149
1150 static int cdn_dp_probe(struct platform_device *pdev)
1151 {
1152         struct device *dev = &pdev->dev;
1153         const struct of_device_id *match;
1154         struct cdn_dp_data *dp_data;
1155         struct cdn_dp_port *port;
1156         struct cdn_dp_device *dp;
1157         struct extcon_dev *extcon;
1158         struct phy *phy;
1159         int i;
1160
1161         dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1162         if (!dp)
1163                 return -ENOMEM;
1164         dp->dev = dev;
1165
1166         match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1167         dp_data = (struct cdn_dp_data *)match->data;
1168
1169         for (i = 0; i < dp_data->max_phy; i++) {
1170                 extcon = extcon_get_edev_by_phandle(dev, i);
1171                 phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1172
1173                 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1174                     PTR_ERR(phy) == -EPROBE_DEFER)
1175                         return -EPROBE_DEFER;
1176
1177                 if (IS_ERR(extcon) || IS_ERR(phy))
1178                         continue;
1179
1180                 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1181                 if (!port)
1182                         return -ENOMEM;
1183
1184                 port->extcon = extcon;
1185                 port->phy = phy;
1186                 port->dp = dp;
1187                 port->id = i;
1188                 dp->port[dp->ports++] = port;
1189         }
1190
1191         if (!dp->ports) {
1192                 DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1193                 return -EINVAL;
1194         }
1195
1196         mutex_init(&dp->lock);
1197         dev_set_drvdata(dev, dp);
1198
1199         cdn_dp_audio_codec_init(dp, dev);
1200
1201         return component_add(dev, &cdn_dp_component_ops);
1202 }
1203
1204 static int cdn_dp_remove(struct platform_device *pdev)
1205 {
1206         struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1207
1208         platform_device_unregister(dp->audio_pdev);
1209         cdn_dp_suspend(dp->dev);
1210         component_del(&pdev->dev, &cdn_dp_component_ops);
1211
1212         return 0;
1213 }
1214
1215 static void cdn_dp_shutdown(struct platform_device *pdev)
1216 {
1217         struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1218
1219         cdn_dp_suspend(dp->dev);
1220 }
1221
1222 static const struct dev_pm_ops cdn_dp_pm_ops = {
1223         SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1224                                 cdn_dp_resume)
1225 };
1226
1227 struct platform_driver cdn_dp_driver = {
1228         .probe = cdn_dp_probe,
1229         .remove = cdn_dp_remove,
1230         .shutdown = cdn_dp_shutdown,
1231         .driver = {
1232                    .name = "cdn-dp",
1233                    .owner = THIS_MODULE,
1234                    .of_match_table = of_match_ptr(cdn_dp_dt_ids),
1235                    .pm = &cdn_dp_pm_ops,
1236         },
1237 };