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