video: rockchip: dp: remove dp phy select operation in dp driver.
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / dp / rockchip_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 <linux/clk.h>
16 #include <linux/component.h>
17 #include <linux/extcon.h>
18 #include <linux/firmware.h>
19 #include <linux/regmap.h>
20 #include <linux/reset.h>
21 #include <linux/mfd/syscon.h>
22 #include <linux/phy/phy.h>
23 #include <sound/hdmi-codec.h>
24 #include <video/of_videomode.h>
25 #include <video/videomode.h>
26
27 #include <linux/fb.h>
28 #include <linux/platform_device.h>
29 #include "rockchip_dp_core.h"
30 #include "cdn-dp-reg.h"
31
32 static struct cdn_dp_data rk3399_cdn_dp = {
33         .max_phy = 2,
34 };
35
36 static const struct of_device_id cdn_dp_dt_ids[] = {
37         { .compatible = "rockchip,rk3399-cdn-dp-fb",
38                 .data = (void *)&rk3399_cdn_dp },
39         {}
40 };
41
42 MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
43
44 static int cdn_dp_grf_write(struct cdn_dp_device *dp,
45                             unsigned int reg, unsigned int val)
46 {
47         int ret;
48
49         ret = clk_prepare_enable(dp->grf_clk);
50         if (ret) {
51                 dev_err(dp->dev, "Failed to prepare_enable grf clock\n");
52                 return ret;
53         }
54
55         ret = regmap_write(dp->grf, reg, val);
56         if (ret) {
57                 dev_err(dp->dev, "Could not write to GRF: %d\n", ret);
58                 return ret;
59         }
60
61         clk_disable_unprepare(dp->grf_clk);
62
63         return 0;
64 }
65
66 static int cdn_dp_set_fw_rate(struct cdn_dp_device *dp)
67 {
68         u32 rate;
69
70         if (!dp->fw_clk_enabled) {
71                 rate = clk_get_rate(dp->core_clk);
72                 if (rate < 0) {
73                         dev_err(dp->dev, "get clk rate failed: %d\n", rate);
74                         return rate;
75                 }
76                 cdn_dp_set_fw_clk(dp, rate);
77                 cdn_dp_clock_reset(dp);
78                 dp->fw_clk_enabled = true;
79         }
80
81         return 0;
82 }
83
84 static int cdn_dp_clk_enable(struct cdn_dp_device *dp)
85 {
86         int ret;
87
88         ret = clk_prepare_enable(dp->pclk);
89         if (ret < 0) {
90                 dev_err(dp->dev, "cannot enable dp pclk %d\n", ret);
91                 goto runtime_get_pm;
92         }
93
94         ret = clk_prepare_enable(dp->core_clk);
95         if (ret < 0) {
96                 dev_err(dp->dev, "cannot enable core_clk %d\n", ret);
97                 goto err_core_clk;
98         }
99
100         ret = pm_runtime_get_sync(dp->dev);
101         if (ret < 0) {
102                 dev_err(dp->dev, "cannot get pm runtime %d\n", ret);
103                 return ret;
104         }
105
106         ret = cdn_dp_set_fw_rate(dp);
107         if (ret < 0) {
108                 dev_err(dp->dev, "cannot get set fw rate %d\n", ret);
109                 goto err_set_rate;
110         }
111
112         return 0;
113
114 err_set_rate:
115         clk_disable_unprepare(dp->core_clk);
116 err_core_clk:
117         clk_disable_unprepare(dp->pclk);
118 runtime_get_pm:
119         pm_runtime_put_sync(dp->dev);
120         return ret;
121 }
122
123 static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
124 {
125         pm_runtime_put_sync(dp->dev);
126         clk_disable_unprepare(dp->pclk);
127         clk_disable_unprepare(dp->core_clk);
128 }
129
130 int cdn_dp_get_edid(void *dp, u8 *buf, int block)
131 {
132         int ret;
133         struct cdn_dp_device *dp_dev = dp;
134         char guid[16];
135         char start_read_edid = -1;
136         u32 readed_size = 0;
137         u32 left_size = EDID_BLOCK_SIZE;
138
139         mutex_lock(&dp_dev->lock);
140         ret = cdn_dp_dpcd_read(dp, 0x0030, guid, 8);
141         if (ret == 0 && guid[0] == 'n' && guid[1] == 'a' &&
142             guid[2] == 'n' && guid[3] == 'o' && guid[4] == 'c') {
143                 int try_times = 0;
144
145                 cdn_dp_dpcd_write(dp, 0x0038, 0x55);
146                 while (left_size > 0) {
147                         u32 length = (left_size > 8) ? 8 : left_size;
148
149                         ret = cdn_dp_dpcd_read(dp, 0x0038, &start_read_edid, 1);
150                         if (ret != 0) {
151                                 dev_err(dp_dev->dev, "read edid sync number error!\n");
152                                 break;
153                         } else if (start_read_edid == 0xaa) {
154                                 try_times = 0;
155                                 ret = cdn_dp_dpcd_read(dp, 0x0030,
156                                                        buf + readed_size,
157                                                        length);
158                                 if (ret != 0) {
159                                         dev_err(dp_dev->dev,
160                                                 "read edid bytes [%d~%d] error!\n",
161                                                 readed_size,
162                                                 readed_size + length);
163                                         break;
164                                 }
165
166                                 readed_size += length;
167                                 left_size -= length;
168                                 cdn_dp_dpcd_write(dp, 0x0038, 0x55);
169                         } else {
170                                 if (try_times++ >= 100) {
171                                         dev_err(dp_dev->dev, "read edid from NanoC failed!\n");
172                                         break;
173                                 }
174                                 continue;
175                         }
176                 }
177         } else {
178                 ret = cdn_dp_get_edid_block(dp_dev, buf,
179                                             block, EDID_BLOCK_SIZE);
180         }
181         mutex_unlock(&dp_dev->lock);
182
183         return ret;
184 }
185
186 int cdn_dp_connector_detect(void *dp)
187 {
188         struct cdn_dp_device *dp_dev = dp;
189         bool ret = false;
190
191         mutex_lock(&dp_dev->lock);
192         if (dp_dev->hpd_status == connector_status_connected)
193                 ret = true;
194         mutex_unlock(&dp_dev->lock);
195
196         return ret;
197 }
198
199 int cdn_dp_encoder_disable(void *dp)
200 {
201         struct cdn_dp_device *dp_dev = dp;
202         int ret = 0;
203
204         mutex_lock(&dp_dev->lock);
205         memset(&dp_dev->mode, 0, sizeof(dp_dev->mode));
206         if (dp_dev->hpd_status == connector_status_disconnected) {
207                 dp_dev->dpms_mode = DRM_MODE_DPMS_OFF;
208                 mutex_unlock(&dp_dev->lock);
209                 return ret;
210         }
211
212         if (dp_dev->dpms_mode == DRM_MODE_DPMS_ON) {
213                 dp_dev->dpms_mode = DRM_MODE_DPMS_OFF;
214         } else{
215                 dev_warn(dp_dev->dev, "wrong dpms status,dp encoder has already been disabled\n");
216                 ret = -1;
217         }
218         mutex_unlock(&dp_dev->lock);
219
220         return ret;
221 }
222
223 static void cdn_dp_commit(struct cdn_dp_device *dp)
224 {
225         char guid[16];
226         int ret = cdn_dp_training_start(dp);
227
228         if (ret) {
229                 dev_err(dp->dev, "link training failed: %d\n", ret);
230                 return;
231         }
232
233         ret = cdn_dp_get_training_status(dp);
234         if (ret) {
235                 dev_err(dp->dev, "get link training status failed: %d\n", ret);
236                 return;
237         }
238
239         dev_info(dp->dev, "rate:%d, lanes:%d\n",
240                         dp->link.rate, dp->link.num_lanes);
241
242         /**
243         * Use dpcd@0x0030~0x003f(which is GUID registers) to sync with NanoC
244         * to make sure training is ok. Nanoc will write "nanoc" in GUID registers
245         * when booting, and then we will use these registers to decide whether
246         * need to sync with device which plugged in.
247         * The sync register is 0x0035, firstly we write 0xaa to sync register,
248         * nanoc will read this register and then start the part2 code of DP.
249         */
250         ret = cdn_dp_dpcd_read(dp, 0x0030, guid, 8);
251         if (ret == 0 && guid[0] == 'n' && guid[1] == 'a' && guid[2] == 'n' &&
252                         guid[3] == 'o' && guid[4] == 'c') {
253                 u8 sync_number = 0xaa;
254
255                 cdn_dp_dpcd_write(dp, 0x0035, sync_number);
256         }
257
258         if (cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE))
259                 return;
260
261         if (cdn_dp_config_video(dp)) {
262                 dev_err(dp->dev, "unable to config video\n");
263                 return;
264         }
265
266         if (cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID))
267                 return;
268
269         dp->dpms_mode = DRM_MODE_DPMS_ON;
270 }
271
272 int cdn_dp_encoder_mode_set(void *dp, struct dp_disp_info *disp_info)
273 {
274         int ret, val;
275         struct cdn_dp_device *dp_dev = dp;
276         struct video_info *video = &dp_dev->video_info;
277         struct drm_display_mode disp_mode;
278         struct fb_videomode *mode = disp_info->mode;
279
280         mutex_lock(&dp_dev->lock);
281         disp_mode.clock = mode->pixclock / 1000;
282         disp_mode.hdisplay = mode->xres;
283         disp_mode.hsync_start = disp_mode.hdisplay + mode->right_margin;
284         disp_mode.hsync_end = disp_mode.hsync_start + mode->hsync_len;
285         disp_mode.htotal = disp_mode.hsync_end + mode->left_margin;
286         disp_mode.vdisplay = mode->yres;
287         disp_mode.vsync_start = disp_mode.vdisplay + mode->lower_margin;
288         disp_mode.vsync_end = disp_mode.vsync_start + mode->vsync_len;
289         disp_mode.vtotal = disp_mode.vsync_end + mode->upper_margin;
290
291         switch (disp_info->color_depth) {
292         case 16:
293         case 12:
294         case 10:
295                 video->color_depth = 10;
296                 break;
297         case 6:
298                 video->color_depth = 6;
299                 break;
300         default:
301                 video->color_depth = 8;
302                 break;
303         }
304
305         video->color_fmt = PXL_RGB;
306
307         video->v_sync_polarity = disp_info->vsync_polarity;
308         video->h_sync_polarity = disp_info->hsync_polarity;
309
310         if (disp_info->vop_sel)
311                 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
312         else
313                 val = DP_SEL_VOP_LIT << 16;
314
315         ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
316         if (ret != 0) {
317                 dev_err(dp_dev->dev, "Could not write to GRF: %d\n", ret);
318                 mutex_unlock(&dp_dev->lock);
319                 return ret;
320         }
321         memcpy(&dp_dev->mode, &disp_mode, sizeof(disp_mode));
322
323         mutex_unlock(&dp_dev->lock);
324
325         return 0;
326 }
327
328 int cdn_dp_encoder_enable(void *dp)
329 {
330         struct cdn_dp_device *dp_dev = dp;
331         int ret = 0;
332
333         mutex_lock(&dp_dev->lock);
334
335         if (dp_dev->dpms_mode == DRM_MODE_DPMS_OFF) {
336                 /**
337                 * the mode info of dp device will be cleared when dp encoder is disabled
338                 * so if clock value of mode is 0, means rockchip_dp_config_video is not
339                 * return success, so we don't do cdn_dp_commit.
340                 */
341                 if (dp_dev->mode.clock == 0) {
342                         dev_err(dp_dev->dev, "Error !Please make sure function cdn_dp_encoder_mode_set return success!\n");
343                         mutex_unlock(&dp_dev->lock);
344                         return -1;
345                 }
346                 cdn_dp_commit(dp_dev);
347         } else {
348                 dev_warn(dp_dev->dev, "wrong dpms status,dp encoder has already been enabled\n");
349                 ret = -1;
350         }
351         mutex_unlock(&dp_dev->lock);
352
353         return ret;
354 }
355
356 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
357 {
358         int ret;
359         const u32 *iram_data, *dram_data;
360         const struct firmware *fw = dp->fw;
361         const struct cdn_firmware_header *hdr;
362
363         hdr = (struct cdn_firmware_header *)fw->data;
364         if (fw->size != le32_to_cpu(hdr->size_bytes)) {
365                 dev_err(dp->dev, "firmware is invalid\n");
366                 return -EINVAL;
367         }
368
369         iram_data = (const u32 *)(fw->data + hdr->header_size);
370         dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
371
372         ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
373                                    dram_data, hdr->dram_size);
374         if (ret)
375                 return ret;
376
377         ret = cdn_dp_set_firmware_active(dp, true);
378         if (ret) {
379                 dev_err(dp->dev, "active ucpu failed: %d\n", ret);
380                 return ret;
381         }
382
383         dp->fw_loaded = 1;
384         return cdn_dp_event_config(dp);
385 }
386
387 static int cdn_dp_init(struct cdn_dp_device *dp)
388 {
389         struct device *dev = dp->dev;
390         struct device_node *np = dev->of_node;
391         struct platform_device *pdev = to_platform_device(dev);
392         struct resource *res;
393
394         dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
395         if (IS_ERR(dp->grf)) {
396                 dev_err(dev, "cdn-dp needs rockchip,grf property\n");
397                 return PTR_ERR(dp->grf);
398         }
399
400         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
401         dp->regs = devm_ioremap_resource(dev, res);
402         if (IS_ERR(dp->regs)) {
403                 dev_err(dev, "ioremap reg failed\n");
404                 return PTR_ERR(dp->regs);
405         }
406
407         dp->core_clk = devm_clk_get(dev, "core-clk");
408         if (IS_ERR(dp->core_clk)) {
409                 dev_err(dev, "cannot get core_clk_dp\n");
410                 return PTR_ERR(dp->core_clk);
411         }
412
413         dp->pclk = devm_clk_get(dev, "pclk");
414         if (IS_ERR(dp->pclk)) {
415                 dev_err(dev, "cannot get pclk\n");
416                 return PTR_ERR(dp->pclk);
417         }
418
419         dp->spdif_clk = devm_clk_get(dev, "spdif");
420         if (IS_ERR(dp->spdif_clk)) {
421                 dev_err(dev, "cannot get spdif_clk\n");
422                 return PTR_ERR(dp->spdif_clk);
423         }
424
425         dp->grf_clk = devm_clk_get(dev, "grf");
426         if (IS_ERR(dp->grf_clk)) {
427                 dev_err(dev, "cannot get grf clk\n");
428                 return PTR_ERR(dp->grf_clk);
429         }
430
431         dp->spdif_rst = devm_reset_control_get(dev, "spdif");
432         if (IS_ERR(dp->spdif_rst)) {
433                 dev_err(dev, "no spdif reset control found\n");
434                 return PTR_ERR(dp->spdif_rst);
435         }
436
437         dp->dptx_rst = devm_reset_control_get(dev, "dptx");
438         if (IS_ERR(dp->dptx_rst)) {
439                 dev_err(dev, "no uphy reset control found\n");
440                 return PTR_ERR(dp->dptx_rst);
441         }
442
443         dp->apb_rst = devm_reset_control_get(dev, "apb");
444         if (IS_ERR(dp->apb_rst)) {
445                 dev_err(dev, "no apb reset control found\n");
446                 return PTR_ERR(dp->apb_rst);
447         }
448
449         dp->dpms_mode = DRM_MODE_DPMS_OFF;
450         dp->fw_clk_enabled = false;
451
452         pm_runtime_enable(dev);
453
454         reset_control_assert(dp->dptx_rst);
455         udelay(15);
456         reset_control_deassert(dp->dptx_rst);
457         reset_control_assert(dp->apb_rst);
458         udelay(15);
459         reset_control_deassert(dp->apb_rst);
460
461         mutex_init(&dp->lock);
462         wake_lock_init(&dp->wake_lock, WAKE_LOCK_SUSPEND, "cdn_dp_fb");
463         return 0;
464 }
465
466 struct cdn_dp_device *g_dp;
467 static int cdn_dp_audio_hw_params(struct device *dev,  void *data,
468                                   struct hdmi_codec_daifmt *daifmt,
469                                   struct hdmi_codec_params *params)
470 {
471         struct dp_dev *dp_dev = dev_get_drvdata(dev);
472         struct cdn_dp_device *dp = dp_dev->dp;
473         int ret;
474         struct audio_info audio = {
475                 .sample_width = 16,
476                 .sample_rate = 44100,
477                 .channels = 8,
478         };
479
480         if (!cdn_dp_connector_detect(dp))
481                 return 0;
482
483         switch (HDMI_I2S) {
484         case HDMI_I2S:
485                 audio.format = AFMT_I2S;
486                 break;
487         case HDMI_SPDIF:
488                 audio.format = AFMT_SPDIF;
489                 break;
490         default:
491                 dev_err(dev, "%s: Invalid format %d\n", __func__, daifmt->fmt);
492                 return -EINVAL;
493         }
494
495         ret = cdn_dp_audio_config(dp, &audio);
496         if (!ret)
497                 dp->audio_info = audio;
498
499         return ret;
500 }
501
502 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
503 {
504         struct dp_dev *dp_dev = dev_get_drvdata(dev);
505         struct cdn_dp_device *dp = dp_dev->dp;
506         int ret;
507
508         if (cdn_dp_connector_detect(dp)) {
509                 ret = cdn_dp_audio_stop(dp, &dp->audio_info);
510                 if (!ret)
511                         dp->audio_info.format = AFMT_UNUSED;
512         }
513 }
514
515 static int cdn_dp_audio_digital_mute(struct device *dev, void *data,
516                                      bool enable)
517 {
518         struct dp_dev *dp_dev = dev_get_drvdata(dev);
519         struct cdn_dp_device *dp = dp_dev->dp;
520
521         if (!cdn_dp_connector_detect(dp))
522                 return 0;
523         return cdn_dp_audio_mute(dp, enable);
524 }
525
526 static const struct hdmi_codec_ops audio_codec_ops = {
527         .hw_params = cdn_dp_audio_hw_params,
528         .audio_shutdown = cdn_dp_audio_shutdown,
529         .digital_mute = cdn_dp_audio_digital_mute,
530 };
531
532 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
533                                    struct device *dev)
534 {
535         struct hdmi_codec_pdata codec_data = {
536                 .i2s = 1,
537                 .spdif = 1,
538                 .ops = &audio_codec_ops,
539                 .max_i2s_channels = 8,
540         };
541
542         dp->audio_pdev = platform_device_register_data(
543                          dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
544                          &codec_data, sizeof(codec_data));
545
546         return PTR_ERR_OR_ZERO(dp->audio_pdev);
547 }
548
549 static int cdn_dp_get_cap_lanes(struct cdn_dp_device *dp,
550                                 struct extcon_dev *edev)
551 {
552         union extcon_property_value property;
553         u8 lanes = 0;
554         int dptx;
555
556         if (dp->suspend)
557                 return 0;
558
559         dptx = extcon_get_state(edev, EXTCON_DISP_DP);
560         if (dptx > 0) {
561                 extcon_get_property(edev, EXTCON_DISP_DP,
562                                     EXTCON_PROP_USB_SS, &property);
563                 if (property.intval)
564                         lanes = 2;
565                 else
566                         lanes = 4;
567         }
568
569         return lanes;
570 }
571
572 static int cdn_dp_get_dpcd(struct cdn_dp_device *dp, struct cdn_dp_port *port)
573 {
574         u8 sink_count;
575         int i, ret;
576         int retry = 5;
577
578         /*
579          * Native read with retry for link status and receiver capability reads
580          * for cases where the sink may still not be ready.
581          *
582          * Sinks are *supposed* to come up within 1ms from an off state, but
583          * some DOCKs need about 5 seconds to power up, so read the dpcd every
584          * 100ms, if can not get a good dpcd in 10 seconds, give up.
585          */
586         for (i = 0; i < 100; i++) {
587                 ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT,
588                                        &sink_count, 1);
589                 if (!ret) {
590                         dev_dbg(dp->dev, "get dpcd success!\n");
591
592                         sink_count = DP_GET_SINK_COUNT(sink_count);
593                         if (!sink_count) {
594                                 if (retry-- <= 0) {
595                                         dev_err(dp->dev, "sink cout is 0, no sink device!\n");
596                                         return -ENODEV;
597                                 }
598                                 mdelay(50);
599                                 continue;
600                         }
601
602                         ret = cdn_dp_dpcd_read(dp, 0x000, dp->dpcd,
603                                                DP_RECEIVER_CAP_SIZE);
604                         if (ret)
605                                 continue;
606
607                         return ret;
608                 } else if (!extcon_get_state(port->extcon, EXTCON_DISP_DP)) {
609                         break;
610                 }
611
612                 msleep(100);
613         }
614
615         dev_err(dp->dev, "get dpcd failed!\n");
616
617         return -ETIMEDOUT;
618 }
619
620 static void cdn_dp_enter_standy(struct cdn_dp_device *dp,
621                                 struct cdn_dp_port *port)
622 {
623         int i, ret;
624
625         ret = phy_power_off(port->phy);
626         if (ret) {
627                 dev_err(dp->dev, "phy power off failed: %d", ret);
628                 return;
629         }
630
631         port->phy_status = false;
632         port->cap_lanes = 0;
633         for (i = 0; i < dp->ports; i++)
634                 if (dp->port[i]->phy_status)
635                         return;
636
637         memset(dp->dpcd, 0, DP_RECEIVER_CAP_SIZE);
638         if (dp->fw_loaded)
639                 cdn_dp_set_firmware_active(dp, false);
640         cdn_dp_clk_disable(dp);
641         dp->hpd_status = connector_status_disconnected;
642
643         hpd_change(dp->dev, 0);
644 }
645
646 static int cdn_dp_start_work(struct cdn_dp_device *dp,
647                              struct cdn_dp_port *port,
648                              u8 cap_lanes)
649 {
650         union extcon_property_value property;
651         int ret;
652
653         if (!dp->fw_loaded) {
654                 ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
655                 if (ret) {
656                         if (ret == -ENOENT && dp->fw_wait <= MAX_FW_WAIT_SECS) {
657                                 unsigned long time = msecs_to_jiffies(dp->fw_wait * HZ);
658
659                                 /*
660                                  * Keep trying to load the firmware for up to 1 minute,
661                                  * if can not find the file.
662                                  */
663                                 schedule_delayed_work(&port->event_wq, time);
664                                 dp->fw_wait *= 2;
665                         } else {
666                                 dev_err(dp->dev, "failed to request firmware: %d\n",
667                                         ret);
668                         }
669
670                         return ret;
671                 }
672         }
673
674         ret = cdn_dp_clk_enable(dp);
675         if (ret < 0) {
676                 dev_err(dp->dev, "failed to enable clock for dp: %d\n", ret);
677                 return ret;
678         }
679         if (dp->fw_loaded)
680                 cdn_dp_set_firmware_active(dp, true);
681
682         ret = phy_power_on(port->phy);
683         if (ret) {
684                 dev_err(dp->dev, "phy power on failed: %d\n", ret);
685                 goto err_phy;
686         }
687
688         port->phy_status = true;
689
690         if (!dp->fw_loaded) {
691                 ret = cdn_dp_firmware_init(dp);
692                 if (ret) {
693                         dev_err(dp->dev, "firmware init failed: %d", ret);
694                         goto err_firmware;
695                 }
696         }
697
698         ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
699                                DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
700         if (ret)
701                 goto err_grf;
702
703         ret = cdn_dp_get_hpd_status(dp);
704         if (ret <= 0) {
705                 if (!ret)
706                         dev_err(dp->dev, "hpd does not exist\n");
707                 goto err_hpd;
708         }
709
710         ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
711                                   EXTCON_PROP_USB_TYPEC_POLARITY, &property);
712         if (ret) {
713                 dev_err(dp->dev, "get property failed\n");
714                 goto err_hpd;
715         }
716
717         ret = cdn_dp_set_host_cap(dp, cap_lanes, property.intval);
718         if (ret) {
719                 dev_err(dp->dev, "set host capabilities failed: %d\n", ret);
720                 goto err_hpd;
721         }
722
723         ret = cdn_dp_get_dpcd(dp, port);
724         if (ret)
725                 goto err_hpd;
726
727         return 0;
728
729 err_hpd:
730         cdn_dp_grf_write(dp, GRF_SOC_CON26,
731                          DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
732
733 err_grf:
734         cdn_dp_set_firmware_active(dp, false);
735
736 err_firmware:
737         if (phy_power_off(port->phy))
738                 dev_err(dp->dev, "phy power off failed: %d", ret);
739         else
740                 port->phy_status = false;
741
742 err_phy:
743         if (dp->fw_loaded)
744                 cdn_dp_set_firmware_active(dp, false);
745         cdn_dp_clk_disable(dp);
746         return ret;
747 }
748
749 static int cdn_dp_pd_event(struct notifier_block *nb,
750                            unsigned long event, void *priv)
751 {
752         struct cdn_dp_port *port;
753
754         port = container_of(nb, struct cdn_dp_port, event_nb);
755         schedule_delayed_work(&port->event_wq, 0);
756         return 0;
757 }
758
759 static void cdn_dp_pd_event_wq(struct work_struct *work)
760 {
761         struct cdn_dp_port *port = container_of(work, struct cdn_dp_port,
762                                                 event_wq.work);
763         struct cdn_dp_device *dp = port->dp;
764         u8 new_cap_lanes, sink_count, i;
765         int ret;
766
767         mutex_lock(&dp->lock);
768         wake_lock_timeout(&dp->wake_lock, msecs_to_jiffies(1000));
769
770         new_cap_lanes = cdn_dp_get_cap_lanes(dp, port->extcon);
771
772         if (new_cap_lanes == port->cap_lanes) {
773                 if (!new_cap_lanes) {
774                         dev_err(dp->dev, "dp lanes is 0, and same with last time\n");
775                         goto out;
776                 }
777
778                 /*
779                  * If HPD interrupt is triggered, and cable states is still
780                  * attached, that means something on the Type-C Dock/Dongle
781                  * changed, check the sink count by DPCD. If sink count became
782                  * 0, this port phy can be powered off; if the sink count does
783                  * not change, it means the sink device status has update,
784                  * re-training to make it work again.
785                  */
786                 ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &sink_count, 1);
787                 if (ret || sink_count) {
788                         if (dp->dpms_mode == DRM_MODE_DPMS_ON) {
789                                 dev_warn(dp->dev,
790                                         "hpd interrupt is triggered when dp is already connected successfully\n");
791                                 ret = cdn_dp_training_start(dp);
792                                 if (!ret)
793                                         cdn_dp_get_training_status(dp);
794                         }
795                         goto out;
796                 }
797                 new_cap_lanes = 0;
798         }
799
800         if (dp->hpd_status == connector_status_connected && new_cap_lanes) {
801                 dev_err(dp->dev, "error, dp connector has already been connected\n");
802                 goto out;
803         }
804
805         if (!new_cap_lanes) {
806                 dev_info(dp->dev, "dp lanes is 0, enter standby\n");
807                 cdn_dp_enter_standy(dp, port);
808                 goto out;
809         }
810
811         /* if other phy is running, do not do anything, just return */
812         for (i = 0; i < dp->ports; i++) {
813                 if (dp->port[i]->phy_status) {
814                         dev_warn(dp->dev, "busy, phy[%d] is running",
815                                  dp->port[i]->id);
816                         goto out;
817                 }
818         }
819
820         ret = cdn_dp_start_work(dp, port, new_cap_lanes);
821         if (ret) {
822                 dev_err(dp->dev, "dp failed to connect ,error = %d\n", ret);
823                 goto out;
824         }
825         port->cap_lanes = new_cap_lanes;
826         dp->hpd_status = connector_status_connected;
827         wake_unlock(&dp->wake_lock);
828         mutex_unlock(&dp->lock);
829         hpd_change(dp->dev, new_cap_lanes);
830
831         return;
832 out:
833         wake_unlock(&dp->wake_lock);
834         mutex_unlock(&dp->lock);
835 }
836
837 static int cdn_dp_bind(struct cdn_dp_device *dp)
838 {
839         struct cdn_dp_port *port;
840         int ret, i;
841
842         ret = cdn_dp_init(dp);
843         if (ret < 0)
844                 return ret;
845
846         dp->hpd_status = connector_status_disconnected;
847         dp->fw_wait = 1;
848         cdn_dp_audio_codec_init(dp, dp->dev);
849
850         for (i = 0; i < dp->ports; i++) {
851                 port = dp->port[i];
852
853                 port->event_nb.notifier_call = cdn_dp_pd_event;
854                 INIT_DELAYED_WORK(&port->event_wq, cdn_dp_pd_event_wq);
855                 ret = extcon_register_notifier(port->extcon, EXTCON_DISP_DP,
856                                                &port->event_nb);
857                 if (ret) {
858                         dev_err(dp->dev, "regitster EXTCON_DISP_DP notifier err\n");
859                         return ret;
860                 }
861
862                 if (extcon_get_state(port->extcon, EXTCON_DISP_DP))
863                         schedule_delayed_work(&port->event_wq,
864                                                         msecs_to_jiffies(2000));
865         }
866
867         return 0;
868 }
869
870 int cdn_dp_suspend(void *dp_dev)
871 {
872         struct cdn_dp_device *dp = dp_dev;
873         struct cdn_dp_port *port;
874         int i;
875
876         for (i = 0; i < dp->ports; i++) {
877                 port = dp->port[i];
878                 if (port->phy_status) {
879                         cdn_dp_dpcd_write(dp, DP_SET_POWER, DP_SET_POWER_D3);
880                         cdn_dp_enter_standy(dp, port);
881                 }
882         }
883
884         /*
885          * if dp has been suspended, need to download firmware
886          * and set fw clk again.
887          */
888         dp->fw_clk_enabled = false;
889         dp->fw_loaded = false;
890         dp->suspend = true;
891         return 0;
892 }
893
894 int cdn_dp_resume(void *dp_dev)
895 {
896         struct cdn_dp_device *dp = dp_dev;
897         struct cdn_dp_port *port;
898         int i;
899         if (dp->suspend) {
900                 dp->suspend = false;
901                 reset_control_assert(dp->dptx_rst);
902                 udelay(15);
903                 reset_control_deassert(dp->dptx_rst);
904                 reset_control_assert(dp->apb_rst);
905                 udelay(15);
906                 reset_control_deassert(dp->apb_rst);
907
908                 for (i = 0; i < dp->ports; i++) {
909                         port = dp->port[i];
910                         schedule_delayed_work(&port->event_wq, 0);
911                         flush_delayed_work(&port->event_wq);
912                 }
913         }
914
915         return 0;
916 }
917
918 static int cdn_dp_probe(struct platform_device *pdev)
919 {
920         struct device *dev = &pdev->dev;
921         const struct of_device_id *match;
922         struct cdn_dp_data *dp_data;
923         struct cdn_dp_port *port;
924         struct cdn_dp_device *dp;
925         struct extcon_dev *extcon;
926         struct phy *phy;
927         int i, ret;
928
929         dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
930         if (!dp)
931                 return -ENOMEM;
932         dp->dev = dev;
933         g_dp = dp;
934
935         match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
936         dp_data = (struct cdn_dp_data *)match->data;
937
938         for (i = 0; i < dp_data->max_phy; i++) {
939                 extcon = extcon_get_edev_by_phandle(dev, i);
940                 phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
941
942                 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
943                     PTR_ERR(phy) == -EPROBE_DEFER){
944                         /* don't exit if there already has one port */
945                         if(dp->ports)
946                                 continue;
947                         return -EPROBE_DEFER;
948
949                 }
950
951                 if (IS_ERR(extcon) || IS_ERR(phy))
952                         continue;
953
954                 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
955                 if (!port)
956                         return -ENOMEM;
957
958                 port->extcon = extcon;
959                 port->phy = phy;
960                 port->dp = dp;
961                 port->id = i;
962                 dp->port[dp->ports++] = port;
963         }
964
965         if (!dp->ports) {
966                 dev_err(dev, "missing extcon or phy\n");
967                 return -EINVAL;
968         }
969
970         cdn_dp_bind(dp);
971         ret = cdn_dp_fb_register(pdev, dp);
972
973         return ret;
974 }
975
976 static struct platform_driver cdn_dp_driver = {
977         .probe = cdn_dp_probe,
978         .driver = {
979                    .name = "cdn-dp-fb",
980                    .owner = THIS_MODULE,
981                    .of_match_table = of_match_ptr(cdn_dp_dt_ids),
982         },
983 };
984
985 module_platform_driver(cdn_dp_driver);
986
987 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
988 MODULE_DESCRIPTION("cdn DP Driver");
989 MODULE_LICENSE("GPL v2");