OSDN Git Service

Merge "Merge android-4.4.155 (b3f777e) into msm-4.4"
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / gpu / drm / msm / hdmi-staging / sde_hdmi.c
1 /*
2  * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #define pr_fmt(fmt)     "sde-hdmi:[%s] " fmt, __func__
20
21 #include <linux/list.h>
22 #include <linux/of.h>
23 #include <linux/gpio.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_platform.h>
26 #include <linux/irqdomain.h>
27
28 #include "sde_kms.h"
29 #include "sde_connector.h"
30 #include "msm_drv.h"
31 #include "sde_hdmi.h"
32 #include "sde_hdmi_regs.h"
33 #include "hdmi.h"
34
35 static DEFINE_MUTEX(sde_hdmi_list_lock);
36 static LIST_HEAD(sde_hdmi_list);
37
38 /* HDMI SCDC register offsets */
39 #define HDMI_SCDC_UPDATE_0              0x10
40 #define HDMI_SCDC_UPDATE_1              0x11
41 #define HDMI_SCDC_TMDS_CONFIG           0x20
42 #define HDMI_SCDC_SCRAMBLER_STATUS      0x21
43 #define HDMI_SCDC_CONFIG_0              0x30
44 #define HDMI_SCDC_STATUS_FLAGS_0        0x40
45 #define HDMI_SCDC_STATUS_FLAGS_1        0x41
46 #define HDMI_SCDC_ERR_DET_0_L           0x50
47 #define HDMI_SCDC_ERR_DET_0_H           0x51
48 #define HDMI_SCDC_ERR_DET_1_L           0x52
49 #define HDMI_SCDC_ERR_DET_1_H           0x53
50 #define HDMI_SCDC_ERR_DET_2_L           0x54
51 #define HDMI_SCDC_ERR_DET_2_H           0x55
52 #define HDMI_SCDC_ERR_DET_CHECKSUM      0x56
53
54 #define HDMI_DISPLAY_MAX_WIDTH          4096
55 #define HDMI_DISPLAY_MAX_HEIGHT         2160
56
57 static const struct of_device_id sde_hdmi_dt_match[] = {
58         {.compatible = "qcom,hdmi-display"},
59         {}
60 };
61
62 static ssize_t _sde_hdmi_debugfs_dump_info_read(struct file *file,
63                                                 char __user *buff,
64                                                 size_t count,
65                                                 loff_t *ppos)
66 {
67         struct sde_hdmi *display = file->private_data;
68         char *buf;
69         u32 len = 0;
70
71         if (!display)
72                 return -ENODEV;
73
74         if (*ppos)
75                 return 0;
76
77         buf = kzalloc(SZ_1K, GFP_KERNEL);
78         if (!buf)
79                 return -ENOMEM;
80
81         len += snprintf(buf, SZ_4K, "name = %s\n", display->name);
82
83         if (copy_to_user(buff, buf, len)) {
84                 kfree(buf);
85                 return -EFAULT;
86         }
87
88         *ppos += len;
89
90         kfree(buf);
91         return len;
92 }
93
94 static ssize_t _sde_hdmi_debugfs_edid_modes_read(struct file *file,
95                                                 char __user *buff,
96                                                 size_t count,
97                                                 loff_t *ppos)
98 {
99         struct sde_hdmi *display = file->private_data;
100         char *buf;
101         u32 len = 0;
102         struct drm_connector *connector;
103         u32 mode_count = 0;
104         struct drm_display_mode *mode;
105
106         if (!display)
107                 return -ENODEV;
108
109         if (!display->ctrl.ctrl ||
110                 !display->ctrl.ctrl->connector) {
111                 SDE_ERROR("sde_hdmi=%p or hdmi or connector is NULL\n",
112                                   display);
113                 return -ENOMEM;
114         }
115
116         if (*ppos)
117                 return 0;
118
119         connector = display->ctrl.ctrl->connector;
120
121         list_for_each_entry(mode, &connector->modes, head) {
122                 mode_count++;
123         }
124
125         /* Adding one more to store title */
126         mode_count++;
127
128         buf = kzalloc((mode_count * sizeof(*mode)), GFP_KERNEL);
129         if (!buf)
130                 return -ENOMEM;
131
132         len += snprintf(buf + len, PAGE_SIZE - len,
133                                         "name refresh (Hz) hdisp hss hse htot vdisp");
134
135         len += snprintf(buf + len, PAGE_SIZE - len,
136                                         " vss vse vtot flags\n");
137
138         list_for_each_entry(mode, &connector->modes, head) {
139                 len += snprintf(buf + len, SZ_4K - len,
140                 "%s %d %d %d %d %d %d %d %d %d 0x%x\n",
141                 mode->name, mode->vrefresh, mode->hdisplay,
142                 mode->hsync_start, mode->hsync_end, mode->htotal,
143                 mode->vdisplay, mode->vsync_start, mode->vsync_end,
144                 mode->vtotal, mode->flags);
145         }
146
147         if (copy_to_user(buff, buf, len)) {
148                 kfree(buf);
149                 return -EFAULT;
150         }
151
152         *ppos += len;
153
154         kfree(buf);
155         return len;
156 }
157
158 static ssize_t _sde_hdmi_debugfs_edid_vsdb_info_read(struct file *file,
159                                                 char __user *buff,
160                                                 size_t count,
161                                                 loff_t *ppos)
162 {
163         struct sde_hdmi *display = file->private_data;
164         char buf[200];
165         u32 len = 0;
166         struct drm_connector *connector;
167
168         if (!display)
169                 return -ENODEV;
170
171         if (!display->ctrl.ctrl ||
172                 !display->ctrl.ctrl->connector) {
173                 SDE_ERROR("sde_hdmi=%p or hdmi or connector is NULL\n",
174                                   display);
175                 return -ENOMEM;
176         }
177
178         SDE_HDMI_DEBUG("%s +", __func__);
179         if (*ppos)
180                 return 0;
181
182         connector = display->ctrl.ctrl->connector;
183         len += snprintf(buf + len, sizeof(buf) - len,
184                                         "max_tmds_clock = %d\n",
185                                         connector->max_tmds_clock);
186         len += snprintf(buf + len, sizeof(buf) - len,
187                                         "latency_present %d %d\n",
188                                         connector->latency_present[0],
189                                         connector->latency_present[1]);
190         len += snprintf(buf + len, sizeof(buf) - len,
191                                         "video_latency %d %d\n",
192                                         connector->video_latency[0],
193                                         connector->video_latency[1]);
194         len += snprintf(buf + len, sizeof(buf) - len,
195                                         "audio_latency %d %d\n",
196                                         connector->audio_latency[0],
197                                         connector->audio_latency[1]);
198         len += snprintf(buf + len, sizeof(buf) - len,
199                                         "dvi_dual %d\n",
200                                         (int)connector->dvi_dual);
201
202         if (copy_to_user(buff, buf, len))
203                 return -EFAULT;
204
205         *ppos += len;
206         SDE_HDMI_DEBUG("%s - ", __func__);
207         return len;
208 }
209
210 static ssize_t _sde_hdmi_debugfs_edid_hdr_info_read(struct file *file,
211                                                 char __user *buff,
212                                                 size_t count,
213                                                 loff_t *ppos)
214 {
215         struct sde_hdmi *display = file->private_data;
216         char buf[200];
217         u32 len = 0;
218         struct drm_connector *connector;
219
220         if (!display)
221                 return -ENODEV;
222
223         if (!display->ctrl.ctrl ||
224                 !display->ctrl.ctrl->connector) {
225                 SDE_ERROR("sde_hdmi=%p or hdmi or connector is NULL\n",
226                                   display);
227                 return -ENOMEM;
228         }
229
230         SDE_HDMI_DEBUG("%s +", __func__);
231         if (*ppos)
232                 return 0;
233
234         connector = display->ctrl.ctrl->connector;
235         len += snprintf(buf, sizeof(buf), "hdr_eotf = %d\n"
236                                         "hdr_metadata_type_one %d\n"
237                                         "hdr_max_luminance %d\n"
238                                         "hdr_avg_luminance %d\n"
239                                         "hdr_min_luminance %d\n"
240                                         "hdr_supported %d\n",
241                                         connector->hdr_eotf,
242                                         connector->hdr_metadata_type_one,
243                                         connector->hdr_max_luminance,
244                                         connector->hdr_avg_luminance,
245                                         connector->hdr_min_luminance,
246                                         (int)connector->hdr_supported);
247
248         if (copy_to_user(buff, buf, len))
249                 return -EFAULT;
250
251         *ppos += len;
252         SDE_HDMI_DEBUG("%s - ", __func__);
253         return len;
254 }
255
256 static ssize_t _sde_hdmi_debugfs_edid_hfvsdb_info_read(struct file *file,
257                                                 char __user *buff,
258                                                 size_t count,
259                                                 loff_t *ppos)
260 {
261         struct sde_hdmi *display = file->private_data;
262         char buf[200];
263         u32 len = 0;
264         struct drm_connector *connector;
265
266         if (!display)
267                 return -ENODEV;
268
269         if (!display->ctrl.ctrl ||
270                 !display->ctrl.ctrl->connector) {
271                 SDE_ERROR("sde_hdmi=%p or hdmi or connector is NULL\n",
272                                   display);
273                 return -ENOMEM;
274         }
275
276         SDE_HDMI_DEBUG("%s +", __func__);
277         if (*ppos)
278                 return 0;
279
280         connector = display->ctrl.ctrl->connector;
281         len += snprintf(buf, PAGE_SIZE - len, "max_tmds_char = %d\n"
282                                         "scdc_present %d\n"
283                                         "rr_capable %d\n"
284                                         "supports_scramble %d\n"
285                                         "flags_3d %d\n",
286                                         connector->max_tmds_char,
287                                         (int)connector->scdc_present,
288                                         (int)connector->rr_capable,
289                                         (int)connector->supports_scramble,
290                                         connector->flags_3d);
291
292         if (copy_to_user(buff, buf, len))
293                 return -EFAULT;
294
295         *ppos += len;
296         return len;
297 }
298
299 static ssize_t _sde_hdmi_debugfs_edid_vcdb_info_read(struct file *file,
300                                                 char __user *buff,
301                                                 size_t count,
302                                                 loff_t *ppos)
303 {
304         struct sde_hdmi *display = file->private_data;
305         char buf[100];
306         u32 len = 0;
307         struct drm_connector *connector;
308
309         if (!display)
310                 return -ENODEV;
311
312         if (!display->ctrl.ctrl ||
313                 !display->ctrl.ctrl->connector) {
314                 SDE_ERROR("sde_hdmi=%p or hdmi or connector is NULL\n",
315                                   display);
316                 return -ENOMEM;
317         }
318
319         SDE_HDMI_DEBUG("%s +", __func__);
320         if (*ppos)
321                 return 0;
322
323         connector = display->ctrl.ctrl->connector;
324         len += snprintf(buf, PAGE_SIZE - len, "pt_scan_info = %d\n"
325                                         "it_scan_info = %d\n"
326                                         "ce_scan_info = %d\n",
327                                         (int)connector->pt_scan_info,
328                                         (int)connector->it_scan_info,
329                                         (int)connector->ce_scan_info);
330
331         if (copy_to_user(buff, buf, len))
332                 return -EFAULT;
333
334         *ppos += len;
335         SDE_HDMI_DEBUG("%s - ", __func__);
336         return len;
337 }
338
339 static ssize_t _sde_hdmi_edid_vendor_name_read(struct file *file,
340                                                 char __user *buff,
341                                                 size_t count,
342                                                 loff_t *ppos)
343 {
344         struct sde_hdmi *display = file->private_data;
345         char buf[100];
346         u32 len = 0;
347         struct drm_connector *connector;
348
349         if (!display)
350                 return -ENODEV;
351
352         if (!display->ctrl.ctrl ||
353                 !display->ctrl.ctrl->connector) {
354                 SDE_ERROR("sde_hdmi=%p or hdmi or connector is NULL\n",
355                                   display);
356                 return -ENOMEM;
357         }
358
359         SDE_HDMI_DEBUG("%s +", __func__);
360         if (*ppos)
361                 return 0;
362
363         connector = display->ctrl.ctrl->connector;
364         len += snprintf(buf, PAGE_SIZE - len, "Vendor ID is %s\n",
365                                         display->edid_ctrl->vendor_id);
366
367         if (copy_to_user(buff, buf, len))
368                 return -EFAULT;
369
370         *ppos += len;
371         SDE_HDMI_DEBUG("%s - ", __func__);
372         return len;
373 }
374
375 static ssize_t _sde_hdmi_src_hdcp14_support_read(struct file *file,
376                                                 char __user *buff,
377                                                 size_t count,
378                                                 loff_t *ppos)
379 {
380         struct sde_hdmi *display = file->private_data;
381         char buf[SZ_128];
382         u32 len = 0;
383
384         if (!display)
385                 return -ENODEV;
386
387         if (!display->ctrl.ctrl) {
388                 SDE_ERROR("hdmi is NULL\n");
389                 return -ENOMEM;
390         }
391
392         SDE_HDMI_DEBUG("%s +", __func__);
393         if (*ppos)
394                 return 0;
395
396         if (display->hdcp14_present)
397                 len += snprintf(buf, SZ_128 - len, "true\n");
398         else
399                 len += snprintf(buf, SZ_128 - len, "false\n");
400
401         if (copy_to_user(buff, buf, len))
402                 return -EFAULT;
403
404         *ppos += len;
405         SDE_HDMI_DEBUG("%s - ", __func__);
406         return len;
407 }
408
409 static ssize_t _sde_hdmi_src_hdcp22_support_read(struct file *file,
410                                                 char __user *buff,
411                                                 size_t count,
412                                                 loff_t *ppos)
413 {
414         struct sde_hdmi *display = file->private_data;
415         char buf[SZ_128];
416         u32 len = 0;
417
418         if (!display)
419                 return -ENODEV;
420
421         if (!display->ctrl.ctrl) {
422                 SDE_ERROR("hdmi is NULL\n");
423                 return -ENOMEM;
424         }
425
426         SDE_HDMI_DEBUG("%s +", __func__);
427         if (*ppos)
428                 return 0;
429
430         if (display->src_hdcp22_support)
431                 len += snprintf(buf, SZ_128 - len, "true\n");
432         else
433                 len += snprintf(buf, SZ_128 - len, "false\n");
434
435         if (copy_to_user(buff, buf, len))
436                 return -EFAULT;
437
438         *ppos += len;
439         SDE_HDMI_DEBUG("%s - ", __func__);
440         return len;
441 }
442
443 static ssize_t _sde_hdmi_sink_hdcp22_support_read(struct file *file,
444                                                 char __user *buff,
445                                                 size_t count,
446                                                 loff_t *ppos)
447 {
448         struct sde_hdmi *display = file->private_data;
449         char buf[SZ_128];
450         u32 len = 0;
451
452         if (!display)
453                 return -ENODEV;
454
455         if (!display->ctrl.ctrl) {
456                 SDE_ERROR("hdmi is NULL\n");
457                 return -ENOMEM;
458         }
459
460         SDE_HDMI_DEBUG("%s +", __func__);
461         if (*ppos)
462                 return 0;
463
464         if (display->sink_hdcp22_support)
465                 len += snprintf(buf, SZ_128 - len, "true\n");
466         else
467                 len += snprintf(buf, SZ_128 - len, "false\n");
468
469         if (copy_to_user(buff, buf, len))
470                 return -EFAULT;
471
472         *ppos += len;
473         SDE_HDMI_DEBUG("%s - ", __func__);
474         return len;
475 }
476
477 static ssize_t _sde_hdmi_hdcp_state_read(struct file *file,
478                                                 char __user *buff,
479                                                 size_t count,
480                                                 loff_t *ppos)
481 {
482         struct sde_hdmi *display = file->private_data;
483         char buf[SZ_128];
484         u32 len = 0;
485
486         if (!display)
487                 return -ENODEV;
488
489         SDE_HDMI_DEBUG("%s +", __func__);
490         if (*ppos)
491                 return 0;
492
493         len += snprintf(buf, SZ_128 - len, "HDCP state : %s\n",
494                         sde_hdcp_state_name(display->hdcp_status));
495
496         if (copy_to_user(buff, buf, len))
497                 return -EFAULT;
498
499         *ppos += len;
500         SDE_HDMI_DEBUG("%s - ", __func__);
501         return len;
502 }
503
504 static const struct file_operations dump_info_fops = {
505         .open = simple_open,
506         .read = _sde_hdmi_debugfs_dump_info_read,
507 };
508
509 static const struct file_operations edid_modes_fops = {
510         .open = simple_open,
511         .read = _sde_hdmi_debugfs_edid_modes_read,
512 };
513
514 static const struct file_operations edid_vsdb_info_fops = {
515         .open = simple_open,
516         .read = _sde_hdmi_debugfs_edid_vsdb_info_read,
517 };
518
519 static const struct file_operations edid_hdr_info_fops = {
520         .open = simple_open,
521         .read = _sde_hdmi_debugfs_edid_hdr_info_read,
522 };
523
524 static const struct file_operations edid_hfvsdb_info_fops = {
525         .open = simple_open,
526         .read = _sde_hdmi_debugfs_edid_hfvsdb_info_read,
527 };
528
529 static const struct file_operations edid_vcdb_info_fops = {
530         .open = simple_open,
531         .read = _sde_hdmi_debugfs_edid_vcdb_info_read,
532 };
533
534 static const struct file_operations edid_vendor_name_fops = {
535         .open = simple_open,
536         .read = _sde_hdmi_edid_vendor_name_read,
537 };
538
539 static const struct file_operations hdcp_src_14_support_fops = {
540         .open = simple_open,
541         .read = _sde_hdmi_src_hdcp14_support_read,
542 };
543
544 static const struct file_operations hdcp_src_22_support_fops = {
545         .open = simple_open,
546         .read = _sde_hdmi_src_hdcp22_support_read,
547 };
548
549 static const struct file_operations hdcp_sink_22_support_fops = {
550         .open = simple_open,
551         .read = _sde_hdmi_sink_hdcp22_support_read,
552 };
553
554 static const struct file_operations sde_hdmi_hdcp_state_fops = {
555         .open = simple_open,
556         .read = _sde_hdmi_hdcp_state_read,
557 };
558
559 static u64 _sde_hdmi_clip_valid_pclk(struct hdmi *hdmi, u64 pclk_in)
560 {
561         u32 pclk_delta, pclk;
562         u64 pclk_clip = pclk_in;
563
564         /* as per standard, 0.5% of deviation is allowed */
565         pclk = hdmi->pixclock;
566         pclk_delta = pclk * 5 / 1000;
567
568         if (pclk_in < (pclk - pclk_delta))
569                 pclk_clip = pclk - pclk_delta;
570         else if (pclk_in > (pclk + pclk_delta))
571                 pclk_clip = pclk + pclk_delta;
572
573         if (pclk_in != pclk_clip)
574                 pr_warn("clip pclk from %lld to %lld\n", pclk_in, pclk_clip);
575
576         return pclk_clip;
577 }
578
579 static void sde_hdmi_tx_hdcp_cb(void *ptr, enum sde_hdcp_states status)
580 {
581         struct sde_hdmi *hdmi_ctrl = (struct sde_hdmi *)ptr;
582         struct hdmi *hdmi;
583
584         if (!hdmi_ctrl) {
585                 DEV_ERR("%s: invalid input\n", __func__);
586                 return;
587         }
588
589         hdmi = hdmi_ctrl->ctrl.ctrl;
590         hdmi_ctrl->hdcp_status = status;
591         queue_delayed_work(hdmi->workq, &hdmi_ctrl->hdcp_cb_work, HZ/4);
592 }
593
594 void sde_hdmi_hdcp_off(struct sde_hdmi *hdmi_ctrl)
595 {
596
597         if (!hdmi_ctrl) {
598                 SDE_ERROR("%s: invalid input\n", __func__);
599                 return;
600         }
601
602         if (hdmi_ctrl->hdcp_ops)
603                 hdmi_ctrl->hdcp_ops->off(hdmi_ctrl->hdcp_data);
604
605         flush_delayed_work(&hdmi_ctrl->hdcp_cb_work);
606
607         hdmi_ctrl->hdcp_ops = NULL;
608 }
609
610 static void sde_hdmi_tx_hdcp_cb_work(struct work_struct *work)
611 {
612         struct sde_hdmi *hdmi_ctrl = NULL;
613         struct delayed_work *dw = to_delayed_work(work);
614         int rc = 0;
615         struct hdmi *hdmi;
616
617         hdmi_ctrl = container_of(dw, struct sde_hdmi, hdcp_cb_work);
618         if (!hdmi_ctrl) {
619                 DEV_DBG("%s: invalid input\n", __func__);
620                 return;
621         }
622
623         hdmi = hdmi_ctrl->ctrl.ctrl;
624
625         switch (hdmi_ctrl->hdcp_status) {
626         case HDCP_STATE_AUTHENTICATED:
627                 hdmi_ctrl->auth_state = true;
628
629                 if (sde_hdmi_tx_is_panel_on(hdmi_ctrl) &&
630                         sde_hdmi_tx_is_stream_shareable(hdmi_ctrl)) {
631                         rc = sde_hdmi_config_avmute(hdmi, false);
632                 }
633
634                 if (hdmi_ctrl->hdcp1_use_sw_keys &&
635                         hdmi_ctrl->hdcp14_present) {
636                         if (!hdmi_ctrl->hdcp22_present)
637                                 hdcp1_set_enc(true);
638                 }
639                 break;
640         case HDCP_STATE_AUTH_FAIL:
641                 if (hdmi_ctrl->hdcp1_use_sw_keys && hdmi_ctrl->hdcp14_present) {
642                         if (hdmi_ctrl->auth_state && !hdmi_ctrl->hdcp22_present)
643                                 hdcp1_set_enc(false);
644                 }
645
646                 hdmi_ctrl->auth_state = false;
647
648                 if (sde_hdmi_tx_is_encryption_set(hdmi_ctrl) ||
649                         !sde_hdmi_tx_is_stream_shareable(hdmi_ctrl))
650                         rc = sde_hdmi_config_avmute(hdmi, true);
651
652                 if (sde_hdmi_tx_is_panel_on(hdmi_ctrl)) {
653                         pr_debug("%s: Reauthenticating\n", __func__);
654                         if (hdmi_ctrl->hdcp_ops && hdmi_ctrl->hdcp_data) {
655                                 rc = hdmi_ctrl->hdcp_ops->reauthenticate(
656                                          hdmi_ctrl->hdcp_data);
657                                 if (rc)
658                                         pr_err("%s: HDCP reauth failed. rc=%d\n",
659                                                    __func__, rc);
660                         } else
661                                 pr_err("%s: NULL HDCP Ops and Data\n",
662                                            __func__);
663                 } else {
664                         pr_debug("%s: Not reauthenticating. Cable not conn\n",
665                                          __func__);
666                 }
667
668                 break;
669                 case HDCP_STATE_AUTH_FAIL_NOREAUTH:
670                 if (hdmi_ctrl->hdcp1_use_sw_keys && hdmi_ctrl->hdcp14_present) {
671                         if (hdmi_ctrl->auth_state && !hdmi_ctrl->hdcp22_present)
672                                 hdcp1_set_enc(false);
673                 }
674
675                 hdmi_ctrl->auth_state = false;
676
677                 break;
678         case HDCP_STATE_AUTH_ENC_NONE:
679                 hdmi_ctrl->enc_lvl = HDCP_STATE_AUTH_ENC_NONE;
680                 if (sde_hdmi_tx_is_panel_on(hdmi_ctrl))
681                         rc = sde_hdmi_config_avmute(hdmi, false);
682                 break;
683         case HDCP_STATE_AUTH_ENC_1X:
684         case HDCP_STATE_AUTH_ENC_2P2:
685                 hdmi_ctrl->enc_lvl = hdmi_ctrl->hdcp_status;
686
687                 if (sde_hdmi_tx_is_panel_on(hdmi_ctrl) &&
688                         sde_hdmi_tx_is_stream_shareable(hdmi_ctrl)) {
689                         rc = sde_hdmi_config_avmute(hdmi, false);
690                 } else {
691                         rc = sde_hdmi_config_avmute(hdmi, true);
692                 }
693                 break;
694         default:
695                 break;
696                 /* do nothing */
697         }
698 }
699
700 /**
701  * _sde_hdmi_update_pll_delta() - Update the HDMI pixel clock as per input ppm
702  *
703  * @ppm: ppm is parts per million multiplied by 1000.
704  * return: 0 on success, non-zero in case of failure.
705  *
706  * The input ppm will be clipped if it's more than or less than 5% of the TMDS
707  * clock rate defined by HDMI spec.
708  */
709 static int _sde_hdmi_update_pll_delta(struct sde_hdmi *display, s32 ppm)
710 {
711         struct hdmi *hdmi = display->ctrl.ctrl;
712         u64 cur_pclk, dst_pclk;
713         u64 clip_pclk;
714         int rc = 0;
715
716         mutex_lock(&display->display_lock);
717
718         if (!hdmi->power_on || !display->connected) {
719                 SDE_ERROR("HDMI display is not ready\n");
720                 mutex_unlock(&display->display_lock);
721                 return -EINVAL;
722         }
723
724         if (!display->pll_update_enable) {
725                 SDE_ERROR("PLL update function is not enabled\n");
726                 mutex_unlock(&display->display_lock);
727                 return -EINVAL;
728         }
729
730         /* get current pclk */
731         cur_pclk = hdmi->actual_pixclock;
732         /* get desired pclk */
733         dst_pclk = cur_pclk * (1000000000 + ppm);
734         do_div(dst_pclk, 1000000000);
735
736         clip_pclk = _sde_hdmi_clip_valid_pclk(hdmi, dst_pclk);
737
738         /* update pclk */
739         if (clip_pclk != cur_pclk) {
740                 SDE_DEBUG("PCLK changes from %llu to %llu when delta is %d\n",
741                                 cur_pclk, clip_pclk, ppm);
742
743                 rc = clk_set_rate(hdmi->pwr_clks[0], clip_pclk);
744                 if (rc < 0) {
745                         SDE_ERROR("HDMI PLL update failed\n");
746                         mutex_unlock(&display->display_lock);
747                         return rc;
748                 }
749
750                 hdmi->actual_pixclock = clip_pclk;
751         }
752
753         mutex_unlock(&display->display_lock);
754
755         return rc;
756 }
757
758 static ssize_t _sde_hdmi_debugfs_pll_delta_write(struct file *file,
759                     const char __user *user_buf, size_t count, loff_t *ppos)
760 {
761         struct sde_hdmi *display = file->private_data;
762         char buf[10];
763         int ppm = 0;
764
765         if (!display)
766                 return -ENODEV;
767
768         if (count >= sizeof(buf))
769                 return -EFAULT;
770
771         if (copy_from_user(buf, user_buf, count))
772                 return -EFAULT;
773
774         buf[count] = 0; /* end of string */
775
776         if (kstrtoint(buf, 0, &ppm))
777                 return -EFAULT;
778
779         if (ppm)
780                 _sde_hdmi_update_pll_delta(display, ppm);
781
782         return count;
783 }
784
785 static const struct file_operations pll_delta_fops = {
786         .open = simple_open,
787         .write = _sde_hdmi_debugfs_pll_delta_write,
788 };
789
790 /**
791  * _sde_hdmi_enable_pll_update() - Enable the HDMI PLL update function
792  *
793  * @enable: non-zero to enable PLL update function, 0 to disable.
794  * return: 0 on success, non-zero in case of failure.
795  *
796  */
797 static int _sde_hdmi_enable_pll_update(struct sde_hdmi *display, s32 enable)
798 {
799         struct hdmi *hdmi = display->ctrl.ctrl;
800         int rc = 0;
801
802         mutex_lock(&display->display_lock);
803
804         if (!hdmi->power_on || !display->connected) {
805                 SDE_ERROR("HDMI display is not ready\n");
806                 mutex_unlock(&display->display_lock);
807                 return -EINVAL;
808         }
809
810         if (!enable && hdmi->actual_pixclock != hdmi->pixclock) {
811                 /* reset pixel clock when disable */
812                 rc = clk_set_rate(hdmi->pwr_clks[0], hdmi->pixclock);
813                 if (rc < 0) {
814                         SDE_ERROR("reset clock rate failed\n");
815                         mutex_unlock(&display->display_lock);
816                         return rc;
817                 }
818         }
819         hdmi->actual_pixclock = hdmi->pixclock;
820
821         display->pll_update_enable = !!enable;
822
823         mutex_unlock(&display->display_lock);
824
825         SDE_DEBUG("HDMI PLL update: %s\n",
826                         display->pll_update_enable ? "enable" : "disable");
827
828         return rc;
829 }
830
831 static ssize_t _sde_hdmi_debugfs_pll_enable_read(struct file *file,
832                 char __user *buff, size_t count, loff_t *ppos)
833 {
834         struct sde_hdmi *display = file->private_data;
835         char *buf;
836         u32 len = 0;
837
838         if (!display)
839                 return -ENODEV;
840
841         if (*ppos)
842                 return 0;
843
844         buf = kzalloc(SZ_1K, GFP_KERNEL);
845         if (!buf)
846                 return -ENOMEM;
847
848         len += snprintf(buf, SZ_4K, "%s\n",
849                         display->pll_update_enable ? "enable" : "disable");
850
851         if (copy_to_user(buff, buf, len)) {
852                 kfree(buf);
853                 return -EFAULT;
854         }
855
856         *ppos += len;
857
858         kfree(buf);
859         return len;
860 }
861
862 static ssize_t _sde_hdmi_debugfs_pll_enable_write(struct file *file,
863                 const char __user *user_buf, size_t count, loff_t *ppos)
864 {
865         struct sde_hdmi *display = file->private_data;
866         char buf[10];
867         int enable = 0;
868
869         if (!display)
870                 return -ENODEV;
871
872         if (count >= sizeof(buf))
873                 return -EFAULT;
874
875         if (copy_from_user(buf, user_buf, count))
876                 return -EFAULT;
877
878         buf[count] = 0; /* end of string */
879
880         if (kstrtoint(buf, 0, &enable))
881                 return -EFAULT;
882
883         _sde_hdmi_enable_pll_update(display, enable);
884
885         return count;
886 }
887
888 static const struct file_operations pll_enable_fops = {
889         .open = simple_open,
890         .read = _sde_hdmi_debugfs_pll_enable_read,
891         .write = _sde_hdmi_debugfs_pll_enable_write,
892 };
893
894 static int _sde_hdmi_debugfs_init(struct sde_hdmi *display)
895 {
896         int rc = 0;
897         struct dentry *dir, *dump_file, *edid_modes;
898         struct dentry *edid_vsdb_info, *edid_hdr_info, *edid_hfvsdb_info;
899         struct dentry *edid_vcdb_info, *edid_vendor_name;
900         struct dentry *src_hdcp14_support, *src_hdcp22_support;
901         struct dentry *sink_hdcp22_support, *hdmi_hdcp_state;
902         struct dentry *pll_delta_file, *pll_enable_file;
903
904         dir = debugfs_create_dir(display->name, NULL);
905         if (!dir) {
906                 rc = -ENOMEM;
907                 SDE_ERROR("[%s]debugfs create dir failed, rc = %d\n",
908                         display->name, rc);
909                 goto error;
910         }
911
912         dump_file = debugfs_create_file("dump_info",
913                                         0444,
914                                         dir,
915                                         display,
916                                         &dump_info_fops);
917         if (IS_ERR_OR_NULL(dump_file)) {
918                 rc = PTR_ERR(dump_file);
919                 SDE_ERROR("[%s]debugfs create dump_info file failed, rc=%d\n",
920                        display->name, rc);
921                 goto error_remove_dir;
922         }
923
924         pll_delta_file = debugfs_create_file("pll_delta",
925                                         0644,
926                                         dir,
927                                         display,
928                                         &pll_delta_fops);
929         if (IS_ERR_OR_NULL(pll_delta_file)) {
930                 rc = PTR_ERR(pll_delta_file);
931                 SDE_ERROR("[%s]debugfs create pll_delta file failed, rc=%d\n",
932                        display->name, rc);
933                 goto error_remove_dir;
934         }
935
936         pll_enable_file = debugfs_create_file("pll_enable",
937                                         0644,
938                                         dir,
939                                         display,
940                                         &pll_enable_fops);
941         if (IS_ERR_OR_NULL(pll_enable_file)) {
942                 rc = PTR_ERR(pll_enable_file);
943                 SDE_ERROR("[%s]debugfs create pll_enable file failed, rc=%d\n",
944                        display->name, rc);
945                 goto error_remove_dir;
946         }
947
948         edid_modes = debugfs_create_file("edid_modes",
949                                         0444,
950                                         dir,
951                                         display,
952                                         &edid_modes_fops);
953
954         if (IS_ERR_OR_NULL(edid_modes)) {
955                 rc = PTR_ERR(edid_modes);
956                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
957                        display->name, rc);
958                 goto error_remove_dir;
959         }
960
961         edid_vsdb_info = debugfs_create_file("edid_vsdb_info",
962                                                 0444,
963                                                 dir,
964                                                 display,
965                                                 &edid_vsdb_info_fops);
966
967         if (IS_ERR_OR_NULL(edid_vsdb_info)) {
968                 rc = PTR_ERR(edid_vsdb_info);
969                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
970                        display->name, rc);
971                 goto error_remove_dir;
972         }
973
974         edid_hdr_info = debugfs_create_file("edid_hdr_info",
975                                                 0444,
976                                                 dir,
977                                                 display,
978                                                 &edid_hdr_info_fops);
979         if (IS_ERR_OR_NULL(edid_hdr_info)) {
980                 rc = PTR_ERR(edid_hdr_info);
981                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
982                        display->name, rc);
983                 goto error_remove_dir;
984         }
985
986         edid_hfvsdb_info = debugfs_create_file("edid_hfvsdb_info",
987                                                 0444,
988                                                 dir,
989                                                 display,
990                                                 &edid_hfvsdb_info_fops);
991
992         if (IS_ERR_OR_NULL(edid_hfvsdb_info)) {
993                 rc = PTR_ERR(edid_hfvsdb_info);
994                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
995                        display->name, rc);
996                 goto error_remove_dir;
997         }
998
999         edid_vcdb_info = debugfs_create_file("edid_vcdb_info",
1000                                                 0444,
1001                                                 dir,
1002                                                 display,
1003                                                 &edid_vcdb_info_fops);
1004
1005         if (IS_ERR_OR_NULL(edid_vcdb_info)) {
1006                 rc = PTR_ERR(edid_vcdb_info);
1007                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
1008                        display->name, rc);
1009                 goto error_remove_dir;
1010         }
1011
1012         edid_vendor_name = debugfs_create_file("edid_vendor_name",
1013                                                 0444,
1014                                                 dir,
1015                                                 display,
1016                                                 &edid_vendor_name_fops);
1017
1018         if (IS_ERR_OR_NULL(edid_vendor_name)) {
1019                 rc = PTR_ERR(edid_vendor_name);
1020                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
1021                        display->name, rc);
1022                 goto error_remove_dir;
1023         }
1024
1025         src_hdcp14_support = debugfs_create_file("src_hdcp14_support",
1026                                                 0444,
1027                                                 dir,
1028                                                 display,
1029                                                 &hdcp_src_14_support_fops);
1030
1031         if (IS_ERR_OR_NULL(src_hdcp14_support)) {
1032                 rc = PTR_ERR(src_hdcp14_support);
1033                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
1034                        display->name, rc);
1035                 goto error_remove_dir;
1036         }
1037
1038         src_hdcp22_support = debugfs_create_file("src_hdcp22_support",
1039                                                 0444,
1040                                                 dir,
1041                                                 display,
1042                                                 &hdcp_src_22_support_fops);
1043
1044         if (IS_ERR_OR_NULL(src_hdcp22_support)) {
1045                 rc = PTR_ERR(src_hdcp22_support);
1046                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
1047                        display->name, rc);
1048                 goto error_remove_dir;
1049         }
1050
1051         sink_hdcp22_support = debugfs_create_file("sink_hdcp22_support",
1052                                                 0444,
1053                                                 dir,
1054                                                 display,
1055                                                 &hdcp_sink_22_support_fops);
1056
1057         if (IS_ERR_OR_NULL(sink_hdcp22_support)) {
1058                 rc = PTR_ERR(sink_hdcp22_support);
1059                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
1060                        display->name, rc);
1061                 goto error_remove_dir;
1062         }
1063
1064         hdmi_hdcp_state = debugfs_create_file("hdmi_hdcp_state",
1065                                                 0444,
1066                                                 dir,
1067                                                 display,
1068                                                 &sde_hdmi_hdcp_state_fops);
1069
1070         if (IS_ERR_OR_NULL(hdmi_hdcp_state)) {
1071                 rc = PTR_ERR(hdmi_hdcp_state);
1072                 SDE_ERROR("[%s]debugfs create file failed, rc=%d\n",
1073                        display->name, rc);
1074                 goto error_remove_dir;
1075         }
1076
1077         display->root = dir;
1078         return rc;
1079 error_remove_dir:
1080         debugfs_remove(dir);
1081 error:
1082         return rc;
1083 }
1084
1085 static void _sde_hdmi_debugfs_deinit(struct sde_hdmi *display)
1086 {
1087         debugfs_remove(display->root);
1088 }
1089
1090 static void _sde_hdmi_phy_reset(struct hdmi *hdmi)
1091 {
1092         unsigned int val;
1093
1094         val = hdmi_read(hdmi, REG_HDMI_PHY_CTRL);
1095
1096         if (val & HDMI_PHY_CTRL_SW_RESET_LOW)
1097                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
1098                                 val & ~HDMI_PHY_CTRL_SW_RESET);
1099          else
1100                  hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
1101                                 val | HDMI_PHY_CTRL_SW_RESET);
1102
1103         if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW)
1104                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
1105                                 val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
1106         else
1107                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
1108                                 val | HDMI_PHY_CTRL_SW_RESET_PLL);
1109
1110         if (val & HDMI_PHY_CTRL_SW_RESET_LOW)
1111                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
1112                                 val | HDMI_PHY_CTRL_SW_RESET);
1113          else
1114                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
1115                                 val & ~HDMI_PHY_CTRL_SW_RESET);
1116
1117         if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW)
1118                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
1119                                 val | HDMI_PHY_CTRL_SW_RESET_PLL);
1120         else
1121                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
1122                                 val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
1123 }
1124
1125 static int _sde_hdmi_gpio_config(struct hdmi *hdmi, bool on)
1126 {
1127         const struct hdmi_platform_config *config = hdmi->config;
1128         int ret;
1129
1130         if (on) {
1131                 if (config->ddc_clk_gpio != -1) {
1132                         ret = gpio_request(config->ddc_clk_gpio,
1133                                 "HDMI_DDC_CLK");
1134                         if (ret) {
1135                                 SDE_ERROR("'%s'(%d) gpio_request failed: %d\n",
1136                                         "HDMI_DDC_CLK", config->ddc_clk_gpio,
1137                                         ret);
1138                                 goto error_ddc_clk_gpio;
1139                         }
1140                         gpio_set_value_cansleep(config->ddc_clk_gpio, 1);
1141                 }
1142
1143                 if (config->ddc_data_gpio != -1) {
1144                         ret = gpio_request(config->ddc_data_gpio,
1145                                 "HDMI_DDC_DATA");
1146                         if (ret) {
1147                                 SDE_ERROR("'%s'(%d) gpio_request failed: %d\n",
1148                                         "HDMI_DDC_DATA", config->ddc_data_gpio,
1149                                         ret);
1150                                 goto error_ddc_data_gpio;
1151                         }
1152                         gpio_set_value_cansleep(config->ddc_data_gpio, 1);
1153                 }
1154
1155                 ret = gpio_request(config->hpd_gpio, "HDMI_HPD");
1156                 if (ret) {
1157                         SDE_ERROR("'%s'(%d) gpio_request failed: %d\n",
1158                                 "HDMI_HPD", config->hpd_gpio, ret);
1159                         goto error_hpd_gpio;
1160                 }
1161                 gpio_direction_output(config->hpd_gpio, 1);
1162                 if (config->hpd5v_gpio != -1) {
1163                         ret = gpio_request(config->hpd5v_gpio, "HDMI_HPD_5V");
1164                         if (ret) {
1165                                 SDE_ERROR("'%s'(%d) gpio_request failed: %d\n",
1166                                                   "HDMI_HPD_5V",
1167                                                   config->hpd5v_gpio,
1168                                                   ret);
1169                                 goto error_hpd5v_gpio;
1170                         }
1171                         gpio_set_value_cansleep(config->hpd5v_gpio, 1);
1172                 }
1173
1174                 if (config->mux_en_gpio != -1) {
1175                         ret = gpio_request(config->mux_en_gpio, "HDMI_MUX_EN");
1176                         if (ret) {
1177                                 SDE_ERROR("'%s'(%d) gpio_request failed: %d\n",
1178                                         "HDMI_MUX_EN", config->mux_en_gpio,
1179                                         ret);
1180                                 goto error_en_gpio;
1181                         }
1182                         gpio_set_value_cansleep(config->mux_en_gpio, 1);
1183                 }
1184
1185                 if (config->mux_sel_gpio != -1) {
1186                         ret = gpio_request(config->mux_sel_gpio,
1187                                 "HDMI_MUX_SEL");
1188                         if (ret) {
1189                                 SDE_ERROR("'%s'(%d) gpio_request failed: %d\n",
1190                                         "HDMI_MUX_SEL", config->mux_sel_gpio,
1191                                         ret);
1192                                 goto error_sel_gpio;
1193                         }
1194                         gpio_set_value_cansleep(config->mux_sel_gpio, 0);
1195                 }
1196
1197                 if (config->mux_lpm_gpio != -1) {
1198                         ret = gpio_request(config->mux_lpm_gpio,
1199                                         "HDMI_MUX_LPM");
1200                         if (ret) {
1201                                 SDE_ERROR("'%s'(%d) gpio_request failed: %d\n",
1202                                         "HDMI_MUX_LPM",
1203                                         config->mux_lpm_gpio, ret);
1204                                 goto error_lpm_gpio;
1205                         }
1206                         gpio_set_value_cansleep(config->mux_lpm_gpio, 1);
1207                 }
1208                 SDE_DEBUG("gpio on");
1209         } else {
1210                 if (config->ddc_clk_gpio != -1)
1211                         gpio_free(config->ddc_clk_gpio);
1212
1213                 if (config->ddc_data_gpio != -1)
1214                         gpio_free(config->ddc_data_gpio);
1215
1216                 gpio_free(config->hpd_gpio);
1217
1218                 if (config->hpd5v_gpio != -1)
1219                         gpio_free(config->hpd5v_gpio);
1220
1221                 if (config->mux_en_gpio != -1) {
1222                         gpio_set_value_cansleep(config->mux_en_gpio, 0);
1223                         gpio_free(config->mux_en_gpio);
1224                 }
1225
1226                 if (config->mux_sel_gpio != -1) {
1227                         gpio_set_value_cansleep(config->mux_sel_gpio, 1);
1228                         gpio_free(config->mux_sel_gpio);
1229                 }
1230
1231                 if (config->mux_lpm_gpio != -1) {
1232                         gpio_set_value_cansleep(config->mux_lpm_gpio, 0);
1233                         gpio_free(config->mux_lpm_gpio);
1234                 }
1235                 SDE_DEBUG("gpio off");
1236         }
1237
1238         return 0;
1239
1240 error_lpm_gpio:
1241         if (config->mux_sel_gpio != -1)
1242                 gpio_free(config->mux_sel_gpio);
1243 error_sel_gpio:
1244         if (config->mux_en_gpio != -1)
1245                 gpio_free(config->mux_en_gpio);
1246 error_en_gpio:
1247         gpio_free(config->hpd5v_gpio);
1248 error_hpd5v_gpio:
1249         gpio_free(config->hpd_gpio);
1250 error_hpd_gpio:
1251         if (config->ddc_data_gpio != -1)
1252                 gpio_free(config->ddc_data_gpio);
1253 error_ddc_data_gpio:
1254         if (config->ddc_clk_gpio != -1)
1255                 gpio_free(config->ddc_clk_gpio);
1256 error_ddc_clk_gpio:
1257         return ret;
1258 }
1259
1260 static int _sde_hdmi_hpd_enable(struct sde_hdmi *sde_hdmi)
1261 {
1262         struct hdmi *hdmi = sde_hdmi->ctrl.ctrl;
1263         const struct hdmi_platform_config *config = hdmi->config;
1264         struct device *dev = &hdmi->pdev->dev;
1265         uint32_t hpd_ctrl;
1266         int i, ret;
1267         unsigned long flags;
1268         struct drm_connector *connector;
1269         struct msm_drm_private *priv;
1270         struct sde_kms *sde_kms;
1271
1272         connector = hdmi->connector;
1273         priv = connector->dev->dev_private;
1274         sde_kms = to_sde_kms(priv->kms);
1275
1276         for (i = 0; i < config->hpd_reg_cnt; i++) {
1277                 ret = regulator_enable(hdmi->hpd_regs[i]);
1278                 if (ret) {
1279                         SDE_ERROR("failed to enable hpd regulator: %s (%d)\n",
1280                                         config->hpd_reg_names[i], ret);
1281                         goto fail;
1282                 }
1283         }
1284
1285         ret = pinctrl_pm_select_default_state(dev);
1286         if (ret) {
1287                 SDE_ERROR("pinctrl state chg failed: %d\n", ret);
1288                 goto fail;
1289         }
1290
1291         ret = _sde_hdmi_gpio_config(hdmi, true);
1292         if (ret) {
1293                 SDE_ERROR("failed to configure GPIOs: %d\n", ret);
1294                 goto fail;
1295         }
1296
1297         for (i = 0; i < config->hpd_clk_cnt; i++) {
1298                 if (config->hpd_freq && config->hpd_freq[i]) {
1299                         ret = clk_set_rate(hdmi->hpd_clks[i],
1300                                         config->hpd_freq[i]);
1301                         if (ret)
1302                                 pr_warn("failed to set clk %s (%d)\n",
1303                                                 config->hpd_clk_names[i], ret);
1304                 }
1305
1306                 ret = clk_prepare_enable(hdmi->hpd_clks[i]);
1307                 if (ret) {
1308                         SDE_ERROR("failed to enable hpd clk: %s (%d)\n",
1309                                         config->hpd_clk_names[i], ret);
1310                         goto fail;
1311                 }
1312         }
1313
1314         if (!sde_hdmi->cont_splash_enabled) {
1315                 sde_hdmi_set_mode(hdmi, false);
1316                 _sde_hdmi_phy_reset(hdmi);
1317                 sde_hdmi_set_mode(hdmi, true);
1318         }
1319
1320         hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
1321
1322         /* set timeout to 4.1ms (max) for hardware debounce */
1323         spin_lock_irqsave(&hdmi->reg_lock, flags);
1324         hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
1325         hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
1326
1327         hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
1328                         HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
1329
1330         /* enable HPD events: */
1331         hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
1332                         HDMI_HPD_INT_CTRL_INT_CONNECT |
1333                         HDMI_HPD_INT_CTRL_INT_EN);
1334
1335         /* Toggle HPD circuit to trigger HPD sense */
1336         hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
1337                         ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
1338         hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
1339                         HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
1340         spin_unlock_irqrestore(&hdmi->reg_lock, flags);
1341
1342         if (!sde_hdmi->non_pluggable)
1343                 hdmi->hpd_off = false;
1344         SDE_DEBUG("enabled hdmi hpd\n");
1345         return 0;
1346
1347 fail:
1348         return ret;
1349 }
1350
1351 int sde_hdmi_core_enable(struct sde_hdmi *sde_hdmi)
1352 {
1353         struct hdmi *hdmi = sde_hdmi->ctrl.ctrl;
1354         const struct hdmi_platform_config *config = hdmi->config;
1355         struct device *dev = &hdmi->pdev->dev;
1356         int i, ret = 0;
1357
1358         for (i = 0; i < config->hpd_reg_cnt; i++) {
1359                 ret = regulator_enable(hdmi->hpd_regs[i]);
1360                 if (ret) {
1361                         SDE_ERROR("failed to enable hpd regulator: %s (%d)\n",
1362                                         config->hpd_reg_names[i], ret);
1363                         goto err_regulator_enable;
1364                 }
1365         }
1366
1367         ret = pinctrl_pm_select_default_state(dev);
1368         if (ret) {
1369                 SDE_ERROR("pinctrl state chg failed: %d\n", ret);
1370                 goto err_pinctrl_state;
1371         }
1372
1373         ret = _sde_hdmi_gpio_config(hdmi, true);
1374         if (ret) {
1375                 SDE_ERROR("failed to configure GPIOs: %d\n", ret);
1376                 goto err_gpio_config;
1377         }
1378
1379         for (i = 0; i < config->hpd_clk_cnt; i++) {
1380                 if (config->hpd_freq && config->hpd_freq[i]) {
1381                         ret = clk_set_rate(hdmi->hpd_clks[i],
1382                                         config->hpd_freq[i]);
1383                         if (ret)
1384                                 pr_warn("failed to set clk %s (%d)\n",
1385                                                 config->hpd_clk_names[i], ret);
1386                 }
1387
1388                 ret = clk_prepare_enable(hdmi->hpd_clks[i]);
1389                 if (ret) {
1390                         SDE_ERROR("failed to enable hpd clk: %s (%d)\n",
1391                                         config->hpd_clk_names[i], ret);
1392                         goto err_clk_prepare_enable;
1393                 }
1394         }
1395         sde_hdmi_set_mode(hdmi, true);
1396         goto exit;
1397
1398 err_clk_prepare_enable:
1399         for (i = 0; i < config->hpd_clk_cnt; i++)
1400                 clk_disable_unprepare(hdmi->hpd_clks[i]);
1401 err_gpio_config:
1402         _sde_hdmi_gpio_config(hdmi, false);
1403 err_pinctrl_state:
1404         pinctrl_pm_select_sleep_state(dev);
1405 err_regulator_enable:
1406         for (i = 0; i < config->hpd_reg_cnt; i++)
1407                 regulator_disable(hdmi->hpd_regs[i]);
1408 exit:
1409         return ret;
1410 }
1411
1412 static void _sde_hdmi_hpd_disable(struct sde_hdmi *sde_hdmi)
1413 {
1414         struct hdmi *hdmi = sde_hdmi->ctrl.ctrl;
1415         const struct hdmi_platform_config *config = hdmi->config;
1416         struct device *dev = &hdmi->pdev->dev;
1417         int i, ret = 0;
1418         unsigned long flags;
1419
1420         if (!sde_hdmi->non_pluggable && hdmi->hpd_off) {
1421                 pr_warn("hdmi display hpd was already disabled\n");
1422                 return;
1423         }
1424
1425         spin_lock_irqsave(&hdmi->reg_lock, flags);
1426         /* Disable HPD interrupt */
1427         hdmi_write(hdmi, REG_HDMI_HPD_CTRL, 0);
1428         hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
1429         hdmi_write(hdmi, REG_HDMI_HPD_INT_STATUS, 0);
1430         spin_unlock_irqrestore(&hdmi->reg_lock, flags);
1431
1432         sde_hdmi_set_mode(hdmi, false);
1433
1434         for (i = 0; i < config->hpd_clk_cnt; i++)
1435                 clk_disable_unprepare(hdmi->hpd_clks[i]);
1436
1437         ret = _sde_hdmi_gpio_config(hdmi, false);
1438         if (ret)
1439                 pr_warn("failed to unconfigure GPIOs: %d\n", ret);
1440
1441         ret = pinctrl_pm_select_sleep_state(dev);
1442         if (ret)
1443                 pr_warn("pinctrl state chg failed: %d\n", ret);
1444
1445         for (i = 0; i < config->hpd_reg_cnt; i++) {
1446                 ret = regulator_disable(hdmi->hpd_regs[i]);
1447                 if (ret)
1448                         pr_warn("failed to disable hpd regulator: %s (%d)\n",
1449                                         config->hpd_reg_names[i], ret);
1450         }
1451
1452         if (!sde_hdmi->non_pluggable)
1453                 hdmi->hpd_off = true;
1454         SDE_DEBUG("disabled hdmi hpd\n");
1455 }
1456
1457 /**
1458  * _sde_hdmi_update_hpd_state() - Update the HDMI HPD clock state
1459  *
1460  * @state: non-zero to disbale HPD clock, 0 to enable.
1461  * return: 0 on success, non-zero in case of failure.
1462  *
1463  */
1464 static int
1465 _sde_hdmi_update_hpd_state(struct sde_hdmi *hdmi_display, u64 state)
1466 {
1467         struct hdmi *hdmi = hdmi_display->ctrl.ctrl;
1468         int rc = 0;
1469
1470         if (hdmi_display->non_pluggable)
1471                 return 0;
1472
1473         SDE_DEBUG("changing hdmi hpd state to %llu\n", state);
1474
1475         if (state == SDE_MODE_HPD_ON) {
1476                 if (!hdmi->hpd_off)
1477                         pr_warn("hdmi display hpd was already enabled\n");
1478                 rc = _sde_hdmi_hpd_enable(hdmi_display);
1479         } else
1480                 _sde_hdmi_hpd_disable(hdmi_display);
1481
1482         return rc;
1483 }
1484
1485 void sde_hdmi_core_disable(struct sde_hdmi *sde_hdmi)
1486 {
1487         /* HPD contains all the core clock and pwr */
1488         _sde_hdmi_hpd_disable(sde_hdmi);
1489 }
1490
1491 static void _sde_hdmi_cec_update_phys_addr(struct sde_hdmi *display)
1492 {
1493         struct edid *edid = display->edid_ctrl->edid;
1494
1495         if (edid)
1496                 cec_notifier_set_phys_addr_from_edid(display->notifier, edid);
1497         else
1498                 cec_notifier_set_phys_addr(display->notifier,
1499                         CEC_PHYS_ADDR_INVALID);
1500
1501 }
1502
1503 static void _sde_hdmi_init_ddc(struct sde_hdmi *display, struct hdmi *hdmi)
1504 {
1505         display->ddc_ctrl.io = &display->io[HDMI_TX_CORE_IO];
1506         init_completion(&display->ddc_ctrl.rx_status_done);
1507 }
1508
1509 static void _sde_hdmi_map_regs(struct sde_hdmi *display, struct hdmi *hdmi)
1510 {
1511         display->io[HDMI_TX_CORE_IO].base = hdmi->mmio;
1512         display->io[HDMI_TX_CORE_IO].len = hdmi->mmio_len;
1513         display->io[HDMI_TX_QFPROM_IO].base = hdmi->qfprom_mmio;
1514         display->io[HDMI_TX_QFPROM_IO].len = hdmi->qfprom_mmio_len;
1515         display->io[HDMI_TX_HDCP_IO].base = hdmi->hdcp_mmio;
1516         display->io[HDMI_TX_HDCP_IO].len = hdmi->hdcp_mmio_len;
1517 }
1518
1519 static void _sde_hdmi_hotplug_work(struct work_struct *work)
1520 {
1521         struct sde_hdmi *sde_hdmi =
1522                 container_of(work, struct sde_hdmi, hpd_work);
1523         struct drm_connector *connector;
1524         struct hdmi *hdmi = NULL;
1525         u32 hdmi_ctrl;
1526
1527         if (!sde_hdmi || !sde_hdmi->ctrl.ctrl ||
1528                 !sde_hdmi->ctrl.ctrl->connector ||
1529                 !sde_hdmi->edid_ctrl) {
1530                 SDE_ERROR("sde_hdmi=%p or hdmi or connector is NULL\n",
1531                                 sde_hdmi);
1532                 return;
1533         }
1534         hdmi = sde_hdmi->ctrl.ctrl;
1535         connector = sde_hdmi->ctrl.ctrl->connector;
1536
1537         if (sde_hdmi->connected) {
1538                 hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
1539                 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
1540                 sde_get_edid(connector, hdmi->i2c,
1541                 (void **)&sde_hdmi->edid_ctrl);
1542                 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
1543                 hdmi->hdmi_mode = sde_detect_hdmi_monitor(sde_hdmi->edid_ctrl);
1544         } else
1545                 sde_free_edid((void **)&sde_hdmi->edid_ctrl);
1546
1547         drm_helper_hpd_irq_event(connector->dev);
1548         _sde_hdmi_cec_update_phys_addr(sde_hdmi);
1549 }
1550
1551 static void _sde_hdmi_connector_irq(struct sde_hdmi *sde_hdmi)
1552 {
1553         struct hdmi *hdmi = sde_hdmi->ctrl.ctrl;
1554         uint32_t hpd_int_status, hpd_int_ctrl;
1555
1556         /* Process HPD: */
1557         hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
1558         hpd_int_ctrl   = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
1559
1560         if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
1561                         (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
1562                 sde_hdmi->connected = !!(hpd_int_status &
1563                                         HDMI_HPD_INT_STATUS_CABLE_DETECTED);
1564                 /* ack & disable (temporarily) HPD events: */
1565                 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
1566                         HDMI_HPD_INT_CTRL_INT_ACK);
1567
1568                 SDE_HDMI_DEBUG("status=%04x, ctrl=%04x", hpd_int_status,
1569                                 hpd_int_ctrl);
1570
1571                 /* detect disconnect if we are connected or visa versa: */
1572                 hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
1573                 if (!sde_hdmi->connected)
1574                         hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
1575                 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
1576
1577                 queue_work(hdmi->workq, &sde_hdmi->hpd_work);
1578         }
1579 }
1580
1581 static void _sde_hdmi_cec_irq(struct sde_hdmi *sde_hdmi)
1582 {
1583         struct hdmi *hdmi = sde_hdmi->ctrl.ctrl;
1584         u32 cec_intr = hdmi_read(hdmi, REG_HDMI_CEC_INT);
1585
1586         /* Routing interrupt to external CEC drivers */
1587         if (cec_intr)
1588                 generic_handle_irq(irq_find_mapping(
1589                                 sde_hdmi->irq_domain, 1));
1590 }
1591
1592
1593 static irqreturn_t _sde_hdmi_irq(int irq, void *dev_id)
1594 {
1595         struct sde_hdmi *display = dev_id;
1596         struct hdmi *hdmi;
1597
1598         if (!display || !display->ctrl.ctrl) {
1599                 SDE_ERROR("sde_hdmi=%pK or hdmi is NULL\n", display);
1600                 return IRQ_NONE;
1601         }
1602
1603         hdmi = display->ctrl.ctrl;
1604         /* Process HPD: */
1605         _sde_hdmi_connector_irq(display);
1606
1607         /* Process Scrambling ISR */
1608         sde_hdmi_ddc_scrambling_isr((void *)display);
1609
1610         /* Process DDC2 */
1611         sde_hdmi_ddc_hdcp2p2_isr((void *)display);
1612
1613         /* Process DDC: */
1614         hdmi_i2c_irq(hdmi->i2c);
1615
1616         /* Process HDCP: */
1617         if (display->hdcp_ops && display->hdcp_data) {
1618                 if (display->hdcp_ops->isr) {
1619                         if (display->hdcp_ops->isr(
1620                                 display->hdcp_data))
1621                                 DEV_ERR("%s: hdcp_1x_isr failed\n",
1622                                                 __func__);
1623                 }
1624         }
1625
1626         /* Process CEC: */
1627         _sde_hdmi_cec_irq(display);
1628
1629         return IRQ_HANDLED;
1630 }
1631
1632 static int _sde_hdmi_audio_info_setup(struct platform_device *pdev,
1633         struct msm_ext_disp_audio_setup_params *params)
1634 {
1635         int rc = -EPERM;
1636         struct sde_hdmi *display = NULL;
1637         struct hdmi *hdmi = NULL;
1638
1639         display = platform_get_drvdata(pdev);
1640
1641         if (!display || !params) {
1642                 SDE_ERROR("invalid param(s), display %pK, params %pK\n",
1643                                 display, params);
1644                 return -ENODEV;
1645         }
1646
1647         hdmi = display->ctrl.ctrl;
1648
1649         if (hdmi->hdmi_mode)
1650                 rc = sde_hdmi_audio_on(hdmi, params);
1651
1652         return rc;
1653 }
1654
1655 static int _sde_hdmi_get_audio_edid_blk(struct platform_device *pdev,
1656         struct msm_ext_disp_audio_edid_blk *blk)
1657 {
1658         struct sde_hdmi *display = platform_get_drvdata(pdev);
1659
1660         if (!display || !blk) {
1661                 SDE_ERROR("invalid param(s), display %pK, blk %pK\n",
1662                         display, blk);
1663                 return -ENODEV;
1664         }
1665
1666         blk->audio_data_blk = display->edid_ctrl->audio_data_block;
1667         blk->audio_data_blk_size = display->edid_ctrl->adb_size;
1668
1669         blk->spk_alloc_data_blk = display->edid_ctrl->spkr_alloc_data_block;
1670         blk->spk_alloc_data_blk_size = display->edid_ctrl->sadb_size;
1671
1672         return 0;
1673 }
1674
1675 static int _sde_hdmi_get_cable_status(struct platform_device *pdev, u32 vote)
1676 {
1677         struct sde_hdmi *display = NULL;
1678         struct hdmi *hdmi = NULL;
1679
1680         display = platform_get_drvdata(pdev);
1681
1682         if (!display) {
1683                 SDE_ERROR("invalid param(s), display %pK\n", display);
1684                 return -ENODEV;
1685         }
1686
1687         hdmi = display->ctrl.ctrl;
1688
1689         return hdmi->power_on && display->connected;
1690 }
1691
1692 static void _sde_hdmi_audio_codec_ready(struct platform_device *pdev)
1693 {
1694         struct sde_hdmi *display = platform_get_drvdata(pdev);
1695
1696         if (!display) {
1697                 SDE_ERROR("invalid param(s), display %pK\n", display);
1698                 return;
1699         }
1700
1701         mutex_lock(&display->display_lock);
1702         if (!display->codec_ready) {
1703                 display->codec_ready = true;
1704
1705                 if (display->client_notify_pending)
1706                         sde_hdmi_notify_clients(display, display->connected);
1707         }
1708         mutex_unlock(&display->display_lock);
1709 }
1710
1711 static int _sde_hdmi_ext_disp_init(struct sde_hdmi *display)
1712 {
1713         int rc = 0;
1714         struct device_node *pd_np;
1715         const char *phandle = "qcom,msm_ext_disp";
1716
1717         if (!display) {
1718                 SDE_ERROR("Invalid params\n");
1719                 return -EINVAL;
1720         }
1721
1722         display->ext_audio_data.type = EXT_DISPLAY_TYPE_HDMI;
1723         display->ext_audio_data.pdev = display->pdev;
1724         display->ext_audio_data.codec_ops.audio_info_setup =
1725                 _sde_hdmi_audio_info_setup;
1726         display->ext_audio_data.codec_ops.get_audio_edid_blk =
1727                 _sde_hdmi_get_audio_edid_blk;
1728         display->ext_audio_data.codec_ops.cable_status =
1729                 _sde_hdmi_get_cable_status;
1730         display->ext_audio_data.codec_ops.codec_ready =
1731                 _sde_hdmi_audio_codec_ready;
1732
1733         if (!display->pdev->dev.of_node) {
1734                 SDE_ERROR("[%s]cannot find sde_hdmi of_node\n", display->name);
1735                 return -ENODEV;
1736         }
1737
1738         pd_np = of_parse_phandle(display->pdev->dev.of_node, phandle, 0);
1739         if (!pd_np) {
1740                 SDE_ERROR("[%s]cannot find %s device node\n",
1741                         display->name, phandle);
1742                 return -ENODEV;
1743         }
1744
1745         display->ext_pdev = of_find_device_by_node(pd_np);
1746         if (!display->ext_pdev) {
1747                 SDE_ERROR("[%s]cannot find %s platform device\n",
1748                         display->name, phandle);
1749                 return -ENODEV;
1750         }
1751
1752         rc = msm_ext_disp_register_intf(display->ext_pdev,
1753                         &display->ext_audio_data);
1754         if (rc)
1755                 SDE_ERROR("[%s]failed to register disp\n", display->name);
1756
1757         return rc;
1758 }
1759
1760 void sde_hdmi_notify_clients(struct sde_hdmi *display, bool connected)
1761 {
1762         int state = connected ?
1763                 EXT_DISPLAY_CABLE_CONNECT : EXT_DISPLAY_CABLE_DISCONNECT;
1764
1765         if (display && display->ext_audio_data.intf_ops.hpd) {
1766                 struct hdmi *hdmi = display->ctrl.ctrl;
1767                 u32 flags = MSM_EXT_DISP_HPD_ASYNC_VIDEO;
1768
1769                 if (hdmi->hdmi_mode)
1770                         flags |= MSM_EXT_DISP_HPD_AUDIO;
1771
1772                 display->ext_audio_data.intf_ops.hpd(display->ext_pdev,
1773                                 display->ext_audio_data.type, state, flags);
1774         }
1775 }
1776
1777 void sde_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
1778 {
1779         uint32_t ctrl = 0;
1780         unsigned long flags;
1781
1782         spin_lock_irqsave(&hdmi->reg_lock, flags);
1783         ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
1784         if (power_on) {
1785                 ctrl |= HDMI_CTRL_ENABLE;
1786                 if (!hdmi->hdmi_mode) {
1787                         ctrl |= HDMI_CTRL_HDMI;
1788                         hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
1789                         ctrl &= ~HDMI_CTRL_HDMI;
1790                 } else {
1791                         ctrl |= HDMI_CTRL_HDMI;
1792                 }
1793         } else {
1794                 ctrl &= ~HDMI_CTRL_HDMI;
1795         }
1796
1797         hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
1798         spin_unlock_irqrestore(&hdmi->reg_lock, flags);
1799         SDE_HDMI_DEBUG("HDMI Core: %s, HDMI_CTRL=0x%08x\n",
1800                         power_on ? "Enable" : "Disable", ctrl);
1801 }
1802
1803 #define DDC_WRITE_MAX_BYTE_NUM 32
1804
1805 int sde_hdmi_scdc_read(struct hdmi *hdmi, u32 data_type, u32 *val)
1806 {
1807         int rc = 0;
1808         u8 data_buf[2] = {0};
1809         u16 dev_addr, data_len;
1810         u8 offset;
1811
1812         if (!hdmi || !hdmi->i2c || !val) {
1813                 SDE_ERROR("Bad Parameters\n");
1814                 return -EINVAL;
1815         }
1816
1817         if (data_type >= HDMI_TX_SCDC_MAX) {
1818                 SDE_ERROR("Unsupported data type\n");
1819                 return -EINVAL;
1820         }
1821
1822         dev_addr = 0xA8;
1823
1824         switch (data_type) {
1825         case HDMI_TX_SCDC_SCRAMBLING_STATUS:
1826                 data_len = 1;
1827                 offset = HDMI_SCDC_SCRAMBLER_STATUS;
1828                 break;
1829         case HDMI_TX_SCDC_SCRAMBLING_ENABLE:
1830         case HDMI_TX_SCDC_TMDS_BIT_CLOCK_RATIO_UPDATE:
1831                 data_len = 1;
1832                 offset = HDMI_SCDC_TMDS_CONFIG;
1833                 break;
1834         case HDMI_TX_SCDC_CLOCK_DET_STATUS:
1835         case HDMI_TX_SCDC_CH0_LOCK_STATUS:
1836         case HDMI_TX_SCDC_CH1_LOCK_STATUS:
1837         case HDMI_TX_SCDC_CH2_LOCK_STATUS:
1838                 data_len = 1;
1839                 offset = HDMI_SCDC_STATUS_FLAGS_0;
1840                 break;
1841         case HDMI_TX_SCDC_CH0_ERROR_COUNT:
1842                 data_len = 2;
1843                 offset = HDMI_SCDC_ERR_DET_0_L;
1844                 break;
1845         case HDMI_TX_SCDC_CH1_ERROR_COUNT:
1846                 data_len = 2;
1847                 offset = HDMI_SCDC_ERR_DET_1_L;
1848                 break;
1849         case HDMI_TX_SCDC_CH2_ERROR_COUNT:
1850                 data_len = 2;
1851                 offset = HDMI_SCDC_ERR_DET_2_L;
1852                 break;
1853         case HDMI_TX_SCDC_READ_ENABLE:
1854                 data_len = 1;
1855                 offset = HDMI_SCDC_CONFIG_0;
1856                 break;
1857         default:
1858                 break;
1859         }
1860
1861         rc = hdmi_ddc_read(hdmi, dev_addr, offset, data_buf,
1862                                            data_len, true);
1863         if (rc) {
1864                 SDE_ERROR("DDC Read failed for %d\n", data_type);
1865                 return rc;
1866         }
1867
1868         switch (data_type) {
1869         case HDMI_TX_SCDC_SCRAMBLING_STATUS:
1870                 *val = (data_buf[0] & BIT(0)) ? 1 : 0;
1871                 break;
1872         case HDMI_TX_SCDC_SCRAMBLING_ENABLE:
1873                 *val = (data_buf[0] & BIT(0)) ? 1 : 0;
1874                 break;
1875         case HDMI_TX_SCDC_TMDS_BIT_CLOCK_RATIO_UPDATE:
1876                 *val = (data_buf[0] & BIT(1)) ? 1 : 0;
1877                 break;
1878         case HDMI_TX_SCDC_CLOCK_DET_STATUS:
1879                 *val = (data_buf[0] & BIT(0)) ? 1 : 0;
1880                 break;
1881         case HDMI_TX_SCDC_CH0_LOCK_STATUS:
1882                 *val = (data_buf[0] & BIT(1)) ? 1 : 0;
1883                 break;
1884         case HDMI_TX_SCDC_CH1_LOCK_STATUS:
1885                 *val = (data_buf[0] & BIT(2)) ? 1 : 0;
1886                 break;
1887         case HDMI_TX_SCDC_CH2_LOCK_STATUS:
1888                 *val = (data_buf[0] & BIT(3)) ? 1 : 0;
1889                 break;
1890         case HDMI_TX_SCDC_CH0_ERROR_COUNT:
1891         case HDMI_TX_SCDC_CH1_ERROR_COUNT:
1892         case HDMI_TX_SCDC_CH2_ERROR_COUNT:
1893                 if (data_buf[1] & BIT(7))
1894                         *val = (data_buf[0] | ((data_buf[1] & 0x7F) << 8));
1895                 else
1896                         *val = 0;
1897                 break;
1898         case HDMI_TX_SCDC_READ_ENABLE:
1899                 *val = (data_buf[0] & BIT(0)) ? 1 : 0;
1900                 break;
1901         default:
1902                 break;
1903         }
1904
1905         return 0;
1906 }
1907
1908 int sde_hdmi_scdc_write(struct hdmi *hdmi, u32 data_type, u32 val)
1909 {
1910         int rc = 0;
1911         u8 data_buf[2] = {0};
1912         u8 read_val = 0;
1913         u16 dev_addr, data_len;
1914         u8 offset;
1915
1916         if (!hdmi || !hdmi->i2c) {
1917                 SDE_ERROR("Bad Parameters\n");
1918                 return -EINVAL;
1919         }
1920
1921         if (data_type >= HDMI_TX_SCDC_MAX) {
1922                 SDE_ERROR("Unsupported data type\n");
1923                 return -EINVAL;
1924         }
1925
1926         dev_addr = 0xA8;
1927
1928         switch (data_type) {
1929         case HDMI_TX_SCDC_SCRAMBLING_ENABLE:
1930         case HDMI_TX_SCDC_TMDS_BIT_CLOCK_RATIO_UPDATE:
1931                 dev_addr = 0xA8;
1932                 data_len = 1;
1933                 offset = HDMI_SCDC_TMDS_CONFIG;
1934                 rc = hdmi_ddc_read(hdmi, dev_addr, offset, &read_val,
1935                                                    data_len, true);
1936                 if (rc) {
1937                         SDE_ERROR("scdc read failed\n");
1938                         return rc;
1939                 }
1940                 if (data_type == HDMI_TX_SCDC_SCRAMBLING_ENABLE) {
1941                         data_buf[0] = ((((u8)(read_val & 0xFF)) & (~BIT(0))) |
1942                                                    ((u8)(val & BIT(0))));
1943                 } else {
1944                         data_buf[0] = ((((u8)(read_val & 0xFF)) & (~BIT(1))) |
1945                                                    (((u8)(val & BIT(0))) << 1));
1946                 }
1947                 break;
1948         case HDMI_TX_SCDC_READ_ENABLE:
1949                 data_len = 1;
1950                 offset = HDMI_SCDC_CONFIG_0;
1951                 data_buf[0] = (u8)(val & 0x1);
1952                 break;
1953         default:
1954                 SDE_ERROR("Cannot write to read only reg (%d)\n",
1955                                   data_type);
1956                 return -EINVAL;
1957         }
1958
1959         rc = hdmi_ddc_write(hdmi, dev_addr, offset, data_buf,
1960                                                 data_len, true);
1961         if (rc) {
1962                 SDE_ERROR("DDC Read failed for %d\n", data_type);
1963                 return rc;
1964         }
1965         return 0;
1966 }
1967
1968 int sde_hdmi_get_info(struct msm_display_info *info,
1969                                 void *display)
1970 {
1971         int rc = 0;
1972         struct sde_hdmi *hdmi_display = (struct sde_hdmi *)display;
1973         struct hdmi *hdmi = hdmi_display->ctrl.ctrl;
1974
1975         if (!display || !info) {
1976                 SDE_ERROR("display=%p or info=%p is NULL\n", display, info);
1977                 return -EINVAL;
1978         }
1979
1980         mutex_lock(&hdmi_display->display_lock);
1981
1982         info->intf_type = DRM_MODE_CONNECTOR_HDMIA;
1983         info->num_of_h_tiles = 1;
1984         info->h_tile_instance[0] = 0;
1985         if (hdmi_display->non_pluggable) {
1986                 info->capabilities = MSM_DISPLAY_CAP_VID_MODE;
1987                 hdmi_display->connected = true;
1988                 hdmi->hdmi_mode = true;
1989         } else {
1990                 info->capabilities = MSM_DISPLAY_CAP_HOT_PLUG |
1991                                 MSM_DISPLAY_CAP_EDID | MSM_DISPLAY_CAP_VID_MODE;
1992         }
1993         info->is_connected = hdmi_display->connected;
1994         info->max_width = HDMI_DISPLAY_MAX_WIDTH;
1995         info->max_height = HDMI_DISPLAY_MAX_HEIGHT;
1996         info->compression = MSM_DISPLAY_COMPRESS_NONE;
1997
1998         mutex_unlock(&hdmi_display->display_lock);
1999         return rc;
2000 }
2001
2002 static void sde_hdmi_panel_set_hdr_infoframe(struct sde_hdmi *display,
2003 struct drm_msm_ext_panel_hdr_metadata *hdr_meta)
2004 {
2005         u32 packet_payload = 0;
2006         u32 packet_header = 0;
2007         u32 packet_control = 0;
2008         u32 const type_code = 0x87;
2009         u32 const version = 0x01;
2010         u32 const length = 0x1a;
2011         u32 const descriptor_id = 0x00;
2012         struct hdmi *hdmi;
2013         struct drm_connector *connector;
2014
2015         if (!display || !hdr_meta) {
2016                 SDE_ERROR("invalid input\n");
2017                 return;
2018         }
2019
2020         hdmi = display->ctrl.ctrl;
2021         connector = display->ctrl.ctrl->connector;
2022
2023         if (!hdmi || !connector) {
2024                 SDE_ERROR("invalid input\n");
2025                 return;
2026         }
2027
2028         /* Setup Packet header and payload */
2029         packet_header = type_code | (version << 8) | (length << 16);
2030         hdmi_write(hdmi, HDMI_GENERIC0_HDR, packet_header);
2031
2032         packet_payload = (hdr_meta->eotf << 8);
2033         if (connector->hdr_metadata_type_one) {
2034                 packet_payload |= (descriptor_id << 16)
2035                         | (HDMI_GET_LSB(hdr_meta->display_primaries_x[0])
2036                            << 24);
2037                 hdmi_write(hdmi, HDMI_GENERIC0_0, packet_payload);
2038         } else {
2039                 pr_debug("Metadata Type 1 not supported\n");
2040                 hdmi_write(hdmi, HDMI_GENERIC0_0, packet_payload);
2041                 goto enable_packet_control;
2042         }
2043
2044         packet_payload =
2045         (HDMI_GET_MSB(hdr_meta->display_primaries_x[0]))
2046         | (HDMI_GET_LSB(hdr_meta->display_primaries_y[0]) << 8)
2047         | (HDMI_GET_MSB(hdr_meta->display_primaries_y[0]) << 16)
2048         | (HDMI_GET_LSB(hdr_meta->display_primaries_x[1]) << 24);
2049         hdmi_write(hdmi, HDMI_GENERIC0_1, packet_payload);
2050
2051         packet_payload =
2052                 (HDMI_GET_MSB(hdr_meta->display_primaries_x[1]))
2053                 | (HDMI_GET_LSB(hdr_meta->display_primaries_y[1]) << 8)
2054                 | (HDMI_GET_MSB(hdr_meta->display_primaries_y[1]) << 16)
2055                 | (HDMI_GET_LSB(hdr_meta->display_primaries_x[2]) << 24);
2056         hdmi_write(hdmi, HDMI_GENERIC0_2, packet_payload);
2057
2058         packet_payload =
2059                 (HDMI_GET_MSB(hdr_meta->display_primaries_x[2]))
2060                 | (HDMI_GET_LSB(hdr_meta->display_primaries_y[2]) << 8)
2061                 | (HDMI_GET_MSB(hdr_meta->display_primaries_y[2]) << 16)
2062                 | (HDMI_GET_LSB(hdr_meta->white_point_x) << 24);
2063         hdmi_write(hdmi, HDMI_GENERIC0_3, packet_payload);
2064
2065         packet_payload =
2066                 (HDMI_GET_MSB(hdr_meta->white_point_x))
2067                 | (HDMI_GET_LSB(hdr_meta->white_point_y) << 8)
2068                 | (HDMI_GET_MSB(hdr_meta->white_point_y) << 16)
2069                 | (HDMI_GET_LSB(hdr_meta->max_luminance) << 24);
2070         hdmi_write(hdmi, HDMI_GENERIC0_4, packet_payload);
2071
2072         packet_payload =
2073                 (HDMI_GET_MSB(hdr_meta->max_luminance))
2074                 | (HDMI_GET_LSB(hdr_meta->min_luminance) << 8)
2075                 | (HDMI_GET_MSB(hdr_meta->min_luminance) << 16)
2076                 | (HDMI_GET_LSB(hdr_meta->max_content_light_level) << 24);
2077         hdmi_write(hdmi, HDMI_GENERIC0_5, packet_payload);
2078
2079         packet_payload =
2080                 (HDMI_GET_MSB(hdr_meta->max_content_light_level))
2081                 | (HDMI_GET_LSB(hdr_meta->max_average_light_level) << 8)
2082                 | (HDMI_GET_MSB(hdr_meta->max_average_light_level) << 16);
2083         hdmi_write(hdmi, HDMI_GENERIC0_6, packet_payload);
2084
2085 enable_packet_control:
2086         /*
2087          * GENERIC0_LINE | GENERIC0_CONT | GENERIC0_SEND
2088          * Setup HDMI TX generic packet control
2089          * Enable this packet to transmit every frame
2090          * Enable HDMI TX engine to transmit Generic packet 1
2091          */
2092         packet_control = hdmi_read(hdmi, HDMI_GEN_PKT_CTRL);
2093         packet_control |= BIT(0) | BIT(1) | BIT(2) | BIT(16);
2094         hdmi_write(hdmi, HDMI_GEN_PKT_CTRL, packet_control);
2095 }
2096
2097 static void sde_hdmi_update_colorimetry(struct sde_hdmi *display,
2098         bool use_bt2020)
2099 {
2100         struct hdmi *hdmi;
2101         struct drm_connector *connector;
2102         bool mode_is_yuv = false;
2103         struct drm_display_mode *mode;
2104         u32 mode_fmt_flags = 0;
2105         u8 checksum;
2106         u32 avi_info0 = 0;
2107         u32 avi_info1 = 0;
2108         u8 avi_iframe[HDMI_AVI_INFOFRAME_BUFFER_SIZE] = {0};
2109         u8 *avi_frame = &avi_iframe[HDMI_INFOFRAME_HEADER_SIZE];
2110         struct hdmi_avi_infoframe info;
2111
2112         if (!display) {
2113                 SDE_ERROR("invalid input\n");
2114                 return;
2115         }
2116
2117         hdmi = display->ctrl.ctrl;
2118
2119         if (!hdmi) {
2120                 SDE_ERROR("invalid input\n");
2121                 return;
2122         }
2123
2124         connector = display->ctrl.ctrl->connector;
2125
2126         if (!connector) {
2127                 SDE_ERROR("invalid input\n");
2128                 return;
2129         }
2130
2131         if (!connector->hdr_supported) {
2132                 SDE_DEBUG("HDR is not supported\n");
2133                 return;
2134         }
2135
2136         /* If sink doesn't support BT2020, just return */
2137         if (!(connector->color_enc_fmt & DRM_EDID_COLORIMETRY_BT2020_YCC) ||
2138             !(connector->color_enc_fmt & DRM_EDID_COLORIMETRY_BT2020_RGB)) {
2139                 SDE_DEBUG("BT2020 colorimetry is not supported\n");
2140                 return;
2141         }
2142
2143         /* If there is no change in colorimetry, just return */
2144         if (use_bt2020 && display->bt2020_colorimetry)
2145                 return;
2146         else if (!use_bt2020 && !display->bt2020_colorimetry)
2147                 return;
2148
2149         mode = &display->mode;
2150         /* Cache the format flags before clearing */
2151         mode_fmt_flags = mode->flags;
2152         /**
2153          * Clear the RGB/YUV format flags before calling upstream API
2154          * as the API also compares the flags and then returns a mode
2155          */
2156         mode->flags &= ~SDE_DRM_MODE_FLAG_FMT_MASK;
2157         drm_hdmi_avi_infoframe_from_display_mode(&info, mode);
2158         /* Restore the format flags */
2159         mode->flags = mode_fmt_flags;
2160
2161         /* Mode should only support YUV and not both to set the flag */
2162         if ((mode->private_flags & MSM_MODE_FLAG_COLOR_FORMAT_YCBCR420)
2163             && !(mode->private_flags & MSM_MODE_FLAG_COLOR_FORMAT_RGB444)) {
2164                 mode_is_yuv = true;
2165         }
2166
2167
2168         if (!display->bt2020_colorimetry && use_bt2020) {
2169                 /**
2170                  * 1. Update colorimetry to use extended
2171                  * 2. Change extended to use BT2020
2172                  * 3. Change colorspace based on mode
2173                  * 4. Use limited as BT2020 is always limited
2174                  */
2175                 info.colorimetry = SDE_HDMI_USE_EXTENDED_COLORIMETRY;
2176                 info.extended_colorimetry = SDE_HDMI_BT2020_COLORIMETRY;
2177                 if (mode_is_yuv)
2178                         info.colorspace = HDMI_COLORSPACE_YUV420;
2179                 if (connector->yuv_qs)
2180                         info.ycc_quantization_range =
2181                                 HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
2182         } else if (display->bt2020_colorimetry && !use_bt2020) {
2183                 /**
2184                  * 1. Update colorimetry to non-extended
2185                  * 2. Change colorspace based on mode
2186                  * 3. Restore quantization to full if QS
2187                  *        is enabled
2188                  */
2189                 info.colorimetry = SDE_HDMI_DEFAULT_COLORIMETRY;
2190                 if (mode_is_yuv)
2191                         info.colorspace = HDMI_COLORSPACE_YUV420;
2192                 if (connector->yuv_qs)
2193                         info.ycc_quantization_range =
2194                                 HDMI_YCC_QUANTIZATION_RANGE_FULL;
2195         }
2196
2197         hdmi_avi_infoframe_pack(&info, avi_iframe, sizeof(avi_iframe));
2198         checksum = avi_iframe[HDMI_INFOFRAME_HEADER_SIZE - 1];
2199         avi_info0 = checksum |
2200                 LEFT_SHIFT_BYTE(avi_frame[0]) |
2201                 LEFT_SHIFT_WORD(avi_frame[1]) |
2202                 LEFT_SHIFT_24BITS(avi_frame[2]);
2203
2204         avi_info1 = avi_frame[3] |
2205                 LEFT_SHIFT_BYTE(avi_frame[4]) |
2206                 LEFT_SHIFT_WORD(avi_frame[5]) |
2207                 LEFT_SHIFT_24BITS(avi_frame[6]);
2208
2209         hdmi_write(hdmi, REG_HDMI_AVI_INFO(0), avi_info0);
2210         hdmi_write(hdmi, REG_HDMI_AVI_INFO(1), avi_info1);
2211         display->bt2020_colorimetry = use_bt2020;
2212 }
2213
2214 static void sde_hdmi_clear_hdr_infoframe(struct sde_hdmi *display)
2215 {
2216         struct hdmi *hdmi;
2217         struct drm_connector *connector;
2218         u32 packet_control = 0;
2219
2220         if (!display) {
2221                 SDE_ERROR("invalid input\n");
2222                 return;
2223         }
2224
2225         hdmi = display->ctrl.ctrl;
2226         connector = display->ctrl.ctrl->connector;
2227
2228         if (!hdmi || !connector) {
2229                 SDE_ERROR("invalid input\n");
2230                 return;
2231         }
2232
2233         packet_control = hdmi_read(hdmi, HDMI_GEN_PKT_CTRL);
2234         packet_control &= ~HDMI_GEN_PKT_CTRL_CLR_MASK;
2235         hdmi_write(hdmi, HDMI_GEN_PKT_CTRL, packet_control);
2236 }
2237
2238 int sde_hdmi_set_property(struct drm_connector *connector,
2239                         struct drm_connector_state *state,
2240                         int property_index,
2241                         uint64_t value,
2242                         void *display)
2243 {
2244         int rc = 0;
2245
2246         if (!connector || !display) {
2247                 SDE_ERROR("connector=%pK or display=%pK is NULL\n",
2248                         connector, display);
2249                 return -EINVAL;
2250         }
2251
2252         SDE_DEBUG("\n");
2253
2254         if (property_index == CONNECTOR_PROP_PLL_ENABLE)
2255                 rc = _sde_hdmi_enable_pll_update(display, value);
2256         else if (property_index == CONNECTOR_PROP_PLL_DELTA)
2257                 rc = _sde_hdmi_update_pll_delta(display, value);
2258         else if (property_index == CONNECTOR_PROP_HPD_OFF)
2259                 rc = _sde_hdmi_update_hpd_state(display, value);
2260
2261         return rc;
2262 }
2263
2264 int sde_hdmi_get_property(struct drm_connector *connector,
2265         struct drm_connector_state *state,
2266         int property_index,
2267         uint64_t *value,
2268         void *display)
2269 {
2270         struct sde_hdmi *hdmi_display = display;
2271         int rc = 0;
2272
2273         if (!connector || !hdmi_display) {
2274                 SDE_ERROR("connector=%pK or display=%pK is NULL\n",
2275                         connector, hdmi_display);
2276                 return -EINVAL;
2277         }
2278
2279         mutex_lock(&hdmi_display->display_lock);
2280         if (property_index == CONNECTOR_PROP_PLL_ENABLE)
2281                 *value = hdmi_display->pll_update_enable ? 1 : 0;
2282         if (property_index == CONNECTOR_PROP_HDCP_VERSION)
2283                 *value = hdmi_display->sink_hdcp_ver;
2284         mutex_unlock(&hdmi_display->display_lock);
2285
2286         return rc;
2287 }
2288
2289 u32 sde_hdmi_get_num_of_displays(void)
2290 {
2291         u32 count = 0;
2292         struct sde_hdmi *display;
2293
2294         mutex_lock(&sde_hdmi_list_lock);
2295
2296         list_for_each_entry(display, &sde_hdmi_list, list)
2297                 count++;
2298
2299         mutex_unlock(&sde_hdmi_list_lock);
2300         return count;
2301 }
2302
2303 int sde_hdmi_get_displays(void **display_array, u32 max_display_count)
2304 {
2305         struct sde_hdmi *display;
2306         int i = 0;
2307
2308         SDE_DEBUG("\n");
2309
2310         if (!display_array || !max_display_count) {
2311                 if (!display_array)
2312                         SDE_ERROR("invalid param\n");
2313                 return 0;
2314         }
2315
2316         mutex_lock(&sde_hdmi_list_lock);
2317         list_for_each_entry(display, &sde_hdmi_list, list) {
2318                 if (i >= max_display_count)
2319                         break;
2320                 display_array[i++] = display;
2321         }
2322         mutex_unlock(&sde_hdmi_list_lock);
2323
2324         return i;
2325 }
2326
2327 int sde_hdmi_connector_pre_deinit(struct drm_connector *connector,
2328                 void *display)
2329 {
2330         struct sde_hdmi *sde_hdmi = (struct sde_hdmi *)display;
2331
2332         if (!sde_hdmi || !sde_hdmi->ctrl.ctrl) {
2333                 SDE_ERROR("sde_hdmi=%p or hdmi is NULL\n", sde_hdmi);
2334                 return -EINVAL;
2335         }
2336
2337         _sde_hdmi_hpd_disable(sde_hdmi);
2338
2339         return 0;
2340 }
2341
2342 static void _sde_hdmi_get_tx_version(struct sde_hdmi *sde_hdmi)
2343 {
2344         struct hdmi *hdmi = sde_hdmi->ctrl.ctrl;
2345
2346         sde_hdmi->hdmi_tx_version = hdmi_read(hdmi, REG_HDMI_VERSION);
2347         sde_hdmi->hdmi_tx_major_version =
2348                 SDE_GET_MAJOR_VER(sde_hdmi->hdmi_tx_version);
2349
2350         switch (sde_hdmi->hdmi_tx_major_version) {
2351         case (HDMI_TX_VERSION_3):
2352                 sde_hdmi->max_pclk_khz = HDMI_TX_3_MAX_PCLK_RATE;
2353                 break;
2354         case (HDMI_TX_VERSION_4):
2355                 sde_hdmi->max_pclk_khz = HDMI_TX_4_MAX_PCLK_RATE;
2356                 break;
2357         default:
2358                 sde_hdmi->max_pclk_khz = HDMI_DEFAULT_MAX_PCLK_RATE;
2359                 break;
2360         }
2361         SDE_DEBUG("sde_hdmi->hdmi_tx_version = 0x%x\n",
2362                 sde_hdmi->hdmi_tx_version);
2363         SDE_DEBUG("sde_hdmi->hdmi_tx_major_version = 0x%x\n",
2364                 sde_hdmi->hdmi_tx_major_version);
2365         SDE_DEBUG("sde_hdmi->max_pclk_khz = 0x%x\n",
2366                 sde_hdmi->max_pclk_khz);
2367 }
2368
2369 static int sde_hdmi_tx_check_capability(struct sde_hdmi *sde_hdmi)
2370 {
2371         u32 hdmi_disabled, hdcp_disabled, reg_val;
2372         int ret = 0;
2373         struct hdmi *hdmi = sde_hdmi->ctrl.ctrl;
2374
2375         /* check if hdmi and hdcp are disabled */
2376         if (sde_hdmi->hdmi_tx_major_version < HDMI_TX_VERSION_4) {
2377                 hdcp_disabled = hdmi_qfprom_read(hdmi,
2378                 QFPROM_RAW_FEAT_CONFIG_ROW0_LSB) & BIT(31);
2379
2380                 hdmi_disabled = hdmi_qfprom_read(hdmi,
2381                 QFPROM_RAW_FEAT_CONFIG_ROW0_MSB) & BIT(0);
2382         } else {
2383                 reg_val = hdmi_qfprom_read(hdmi,
2384                 QFPROM_RAW_FEAT_CONFIG_ROW0_LSB + QFPROM_RAW_VERSION_4);
2385                 hdcp_disabled = reg_val & BIT(12);
2386
2387                 hdmi_disabled = reg_val & BIT(13);
2388
2389                 reg_val = hdmi_qfprom_read(hdmi, SEC_CTRL_HW_VERSION);
2390
2391                 SDE_DEBUG("SEC_CTRL_HW_VERSION reg_val = 0x%x\n", reg_val);
2392                 /*
2393                  * With HDCP enabled on capable hardware, check if HW
2394                  * or SW keys should be used.
2395                  */
2396                 if (!hdcp_disabled && (reg_val >= HDCP_SEL_MIN_SEC_VERSION)) {
2397                         reg_val = hdmi_qfprom_read(hdmi,
2398                         QFPROM_RAW_FEAT_CONFIG_ROW0_MSB +
2399                         QFPROM_RAW_VERSION_4);
2400
2401                         if (!(reg_val & BIT(23)))
2402                                 sde_hdmi->hdcp1_use_sw_keys = true;
2403                 }
2404         }
2405
2406         if (sde_hdmi->hdmi_tx_major_version >= HDMI_TX_VERSION_4)
2407                 sde_hdmi->dc_feature_supported = true;
2408
2409         SDE_DEBUG("%s: Features <HDMI:%s, HDCP:%s, Deep Color:%s>\n", __func__,
2410                         hdmi_disabled ? "OFF" : "ON",
2411                         hdcp_disabled ? "OFF" : "ON",
2412                         sde_hdmi->dc_feature_supported ? "ON" : "OFF");
2413
2414         if (hdmi_disabled) {
2415                 DEV_ERR("%s: HDMI disabled\n", __func__);
2416                 ret = -ENODEV;
2417                 goto end;
2418         }
2419
2420         sde_hdmi->hdcp14_present = !hdcp_disabled;
2421
2422  end:
2423         return ret;
2424 } /* hdmi_tx_check_capability */
2425
2426 static int _sde_hdmi_init_hdcp(struct sde_hdmi *hdmi_ctrl)
2427 {
2428         struct sde_hdcp_init_data hdcp_init_data;
2429         void *hdcp_data;
2430         int rc = 0;
2431         struct hdmi *hdmi;
2432
2433         if (!hdmi_ctrl) {
2434                 SDE_ERROR("sde_hdmi is NULL\n");
2435                 return -EINVAL;
2436         }
2437
2438         hdmi = hdmi_ctrl->ctrl.ctrl;
2439         hdcp_init_data.phy_addr      = hdmi->mmio_phy_addr;
2440         hdcp_init_data.core_io       = &hdmi_ctrl->io[HDMI_TX_CORE_IO];
2441         hdcp_init_data.qfprom_io     = &hdmi_ctrl->io[HDMI_TX_QFPROM_IO];
2442         hdcp_init_data.hdcp_io       = &hdmi_ctrl->io[HDMI_TX_HDCP_IO];
2443         hdcp_init_data.mutex         = &hdmi_ctrl->hdcp_mutex;
2444         hdcp_init_data.workq         = hdmi->workq;
2445         hdcp_init_data.notify_status = sde_hdmi_tx_hdcp_cb;
2446         hdcp_init_data.cb_data       = (void *)hdmi_ctrl;
2447         hdcp_init_data.hdmi_tx_ver   = hdmi_ctrl->hdmi_tx_major_version;
2448         hdcp_init_data.sec_access    = true;
2449         hdcp_init_data.client_id     = HDCP_CLIENT_HDMI;
2450         hdcp_init_data.ddc_ctrl      = &hdmi_ctrl->ddc_ctrl;
2451
2452         if (hdmi_ctrl->hdcp14_present) {
2453                 hdcp_data = sde_hdcp_1x_init(&hdcp_init_data);
2454
2455                 if (IS_ERR_OR_NULL(hdcp_data)) {
2456                         DEV_ERR("%s: hdcp 1.4 init failed\n", __func__);
2457                         rc = -EINVAL;
2458                         kfree(hdcp_data);
2459                         goto end;
2460                 } else {
2461                         hdmi_ctrl->hdcp_feat_data[SDE_HDCP_1x] = hdcp_data;
2462                         SDE_HDMI_DEBUG("%s: HDCP 1.4 initialized\n", __func__);
2463                 }
2464         }
2465
2466         hdcp_data = sde_hdmi_hdcp2p2_init(&hdcp_init_data);
2467
2468         if (IS_ERR_OR_NULL(hdcp_data)) {
2469                 DEV_ERR("%s: hdcp 2.2 init failed\n", __func__);
2470                 rc = -EINVAL;
2471                 goto end;
2472         } else {
2473                 hdmi_ctrl->hdcp_feat_data[SDE_HDCP_2P2] = hdcp_data;
2474                 SDE_HDMI_DEBUG("%s: HDCP 2.2 initialized\n", __func__);
2475         }
2476
2477 end:
2478         return rc;
2479 }
2480
2481 int sde_hdmi_connector_post_init(struct drm_connector *connector,
2482                 void *info,
2483                 void *display)
2484 {
2485         int rc = 0;
2486         struct sde_hdmi *sde_hdmi = (struct sde_hdmi *)display;
2487         struct hdmi *hdmi;
2488
2489         if (!sde_hdmi) {
2490                 SDE_ERROR("sde_hdmi is NULL\n");
2491                 return -EINVAL;
2492         }
2493
2494         hdmi = sde_hdmi->ctrl.ctrl;
2495         if (!hdmi) {
2496                 SDE_ERROR("hdmi is NULL\n");
2497                 return -EINVAL;
2498         }
2499
2500         if (info)
2501                 sde_kms_info_add_keystr(info,
2502                                 "display type",
2503                                 sde_hdmi->display_type);
2504
2505         hdmi->connector = connector;
2506         INIT_WORK(&sde_hdmi->hpd_work, _sde_hdmi_hotplug_work);
2507
2508         if (sde_hdmi->non_pluggable) {
2509                 /* Disable HPD interrupt */
2510                 hdmi_write(hdmi, REG_HDMI_HPD_CTRL, 0);
2511                 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
2512                 hdmi_write(hdmi, REG_HDMI_HPD_INT_STATUS, 0);
2513         } else {
2514                 /* Enable HPD detection if non_pluggable flag is not defined */
2515                 rc = _sde_hdmi_hpd_enable(sde_hdmi);
2516                 if (rc)
2517                         SDE_ERROR("failed to enable HPD: %d\n", rc);
2518         }
2519
2520         _sde_hdmi_get_tx_version(sde_hdmi);
2521
2522         sde_hdmi_tx_check_capability(sde_hdmi);
2523
2524         _sde_hdmi_init_hdcp(sde_hdmi);
2525
2526         return rc;
2527 }
2528
2529 int sde_hdmi_start_hdcp(struct drm_connector *connector)
2530 {
2531         int rc;
2532         struct sde_connector *c_conn = to_sde_connector(connector);
2533         struct sde_hdmi *display = (struct sde_hdmi *)c_conn->display;
2534         struct hdmi *hdmi = display->ctrl.ctrl;
2535
2536         if (!hdmi) {
2537                 SDE_ERROR("%s: invalid input\n", __func__);
2538                 return -EINVAL;
2539         }
2540
2541         if (!sde_hdmi_tx_is_hdcp_enabled(display))
2542                 return 0;
2543
2544         if (sde_hdmi_tx_is_encryption_set(display))
2545                 sde_hdmi_config_avmute(hdmi, true);
2546
2547         rc = display->hdcp_ops->authenticate(display->hdcp_data);
2548         if (rc)
2549                 SDE_ERROR("%s: hdcp auth failed. rc=%d\n", __func__, rc);
2550
2551         return rc;
2552 }
2553
2554 enum drm_connector_status
2555 sde_hdmi_connector_detect(struct drm_connector *connector,
2556                 bool force,
2557                 void *display)
2558 {
2559         enum drm_connector_status status = connector_status_unknown;
2560         struct msm_display_info info;
2561         int rc;
2562
2563         if (!connector || !display) {
2564                 SDE_ERROR("connector=%p or display=%p is NULL\n",
2565                         connector, display);
2566                 return status;
2567         }
2568
2569         /* get display dsi_info */
2570         memset(&info, 0x0, sizeof(info));
2571         rc = sde_hdmi_get_info(&info, display);
2572         if (rc) {
2573                 SDE_ERROR("failed to get display info, rc=%d\n", rc);
2574                 return connector_status_disconnected;
2575         }
2576
2577         if (info.capabilities & MSM_DISPLAY_CAP_HOT_PLUG)
2578                 status = (info.is_connected ? connector_status_connected :
2579                                               connector_status_disconnected);
2580         else
2581                 status = connector_status_connected;
2582
2583         connector->display_info.width_mm = info.width_mm;
2584         connector->display_info.height_mm = info.height_mm;
2585
2586         return status;
2587 }
2588
2589 int sde_hdmi_pre_kickoff(struct drm_connector *connector,
2590         void *display,
2591         struct msm_display_kickoff_params *params)
2592 {
2593         struct sde_hdmi *hdmi_display = (struct sde_hdmi *)display;
2594         struct drm_msm_ext_panel_hdr_ctrl *hdr_ctrl;
2595         struct drm_msm_ext_panel_hdr_metadata *hdr_meta;
2596         u8 hdr_op;
2597
2598         if (!connector || !display || !params ||
2599                 !params->hdr_ctrl) {
2600                 pr_err("Invalid params\n");
2601                 return -EINVAL;
2602         }
2603
2604         hdr_ctrl = params->hdr_ctrl;
2605         hdr_meta = &hdr_ctrl->hdr_meta;
2606
2607         if (!hdr_meta) {
2608                 SDE_ERROR("Invalid params\n");
2609                 return -EINVAL;
2610         }
2611
2612         hdr_op = sde_hdmi_hdr_get_ops(hdmi_display->curr_hdr_state,
2613                 hdr_ctrl->hdr_state);
2614
2615         if (hdr_op == HDR_SEND_INFO) {
2616                 if (connector->hdr_supported)
2617                         sde_hdmi_panel_set_hdr_infoframe(display,
2618                                 &hdr_ctrl->hdr_meta);
2619                 if (hdr_meta->eotf)
2620                         sde_hdmi_update_colorimetry(hdmi_display,
2621                                 true);
2622                 else
2623                         sde_hdmi_update_colorimetry(hdmi_display,
2624                                 false);
2625         } else if (hdr_op == HDR_CLEAR_INFO)
2626                 sde_hdmi_clear_hdr_infoframe(display);
2627
2628         hdmi_display->curr_hdr_state = hdr_ctrl->hdr_state;
2629
2630         return 0;
2631 }
2632
2633 bool sde_hdmi_mode_needs_full_range(void *display)
2634 {
2635         struct sde_hdmi *hdmi_display = (struct sde_hdmi *)display;
2636         struct drm_display_mode *mode;
2637         u32 mode_fmt_flags = 0;
2638         u32 cea_mode;
2639
2640         if (!hdmi_display) {
2641                 SDE_ERROR("invalid input\n");
2642                 return false;
2643         }
2644
2645         mode = &hdmi_display->mode;
2646         /* Cache the format flags before clearing */
2647         mode_fmt_flags = mode->flags;
2648         /**
2649          * Clear the RGB/YUV format flags before calling upstream API
2650          * as the API also compares the flags and then returns a mode
2651          */
2652         mode->flags &= ~SDE_DRM_MODE_FLAG_FMT_MASK;
2653         cea_mode = drm_match_cea_mode(mode);
2654         /* Restore the format flags */
2655         mode->flags = mode_fmt_flags;
2656
2657         if (cea_mode > SDE_HDMI_VIC_640x480)
2658                 return false;
2659
2660         return true;
2661 }
2662
2663 enum sde_csc_type sde_hdmi_get_csc_type(struct drm_connector *conn,
2664         void *display)
2665 {
2666         struct sde_hdmi *hdmi_display = (struct sde_hdmi *)display;
2667         struct sde_connector_state *c_state;
2668         struct drm_msm_ext_panel_hdr_ctrl *hdr_ctrl;
2669         struct drm_msm_ext_panel_hdr_metadata *hdr_meta;
2670
2671         if (!hdmi_display || !conn) {
2672                 SDE_ERROR("invalid input\n");
2673                 goto error;
2674         }
2675
2676         c_state = to_sde_connector_state(conn->state);
2677
2678         if (!c_state) {
2679                 SDE_ERROR("invalid input\n");
2680                 goto error;
2681         }
2682
2683         hdr_ctrl = &c_state->hdr_ctrl;
2684         hdr_meta = &hdr_ctrl->hdr_meta;
2685
2686         if ((hdr_ctrl->hdr_state == HDR_ENABLE)
2687                 && (hdr_meta->eotf != 0))
2688                 return SDE_CSC_RGB2YUV_2020L;
2689         else if (sde_hdmi_mode_needs_full_range(hdmi_display)
2690                 || conn->yuv_qs)
2691                 return SDE_CSC_RGB2YUV_601FR;
2692
2693 error:
2694         return SDE_CSC_RGB2YUV_601L;
2695 }
2696
2697 int sde_hdmi_connector_get_modes(struct drm_connector *connector, void *display)
2698 {
2699         struct sde_hdmi *hdmi_display = (struct sde_hdmi *)display;
2700         struct drm_display_mode *mode, *m;
2701         int ret = 0;
2702
2703         if (!connector || !display) {
2704                 SDE_ERROR("connector=%p or display=%p is NULL\n",
2705                         connector, display);
2706                 return 0;
2707         }
2708
2709         if (hdmi_display->non_pluggable) {
2710                 list_for_each_entry(mode, &hdmi_display->mode_list, head) {
2711                         m = drm_mode_duplicate(connector->dev, mode);
2712                         if (!m) {
2713                                 SDE_ERROR("failed to add hdmi mode %dx%d\n",
2714                                         mode->hdisplay, mode->vdisplay);
2715                                 break;
2716                         }
2717                         drm_mode_probed_add(connector, m);
2718                 }
2719                 ret = hdmi_display->num_of_modes;
2720         } else {
2721                 /* pluggable case assumes EDID is read when HPD */
2722                 ret = _sde_edid_update_modes(connector,
2723                         hdmi_display->edid_ctrl);
2724         }
2725
2726         return ret;
2727 }
2728
2729 enum drm_mode_status sde_hdmi_mode_valid(struct drm_connector *connector,
2730                 struct drm_display_mode *mode,
2731                 void *display)
2732 {
2733         struct sde_hdmi *hdmi_display = (struct sde_hdmi *)display;
2734         struct hdmi *hdmi;
2735         struct msm_drm_private *priv;
2736         struct msm_kms *kms;
2737         long actual, requested;
2738
2739         if (!connector || !display || !mode) {
2740                 SDE_ERROR("connector=%p or display=%p or mode=%p is NULL\n",
2741                         connector, display, mode);
2742                 return 0;
2743         }
2744
2745         hdmi = hdmi_display->ctrl.ctrl;
2746         priv = connector->dev->dev_private;
2747         kms = priv->kms;
2748         requested = 1000 * mode->clock;
2749         actual = kms->funcs->round_pixclk(kms,
2750                         requested, hdmi->encoder);
2751
2752         SDE_HDMI_DEBUG("requested=%ld, actual=%ld", requested, actual);
2753
2754         if (actual != requested)
2755                 return MODE_CLOCK_RANGE;
2756
2757         /* if no format flags are present remove the mode */
2758         if (!(mode->flags & SDE_DRM_MODE_FLAG_FMT_MASK)) {
2759                 SDE_HDMI_DEBUG("removing following mode from list\n");
2760                 drm_mode_debug_printmodeline(mode);
2761                 return MODE_BAD;
2762         }
2763
2764         return MODE_OK;
2765 }
2766
2767 int sde_hdmi_dev_init(struct sde_hdmi *display)
2768 {
2769         if (!display) {
2770                 SDE_ERROR("Invalid params\n");
2771                 return -EINVAL;
2772         }
2773         return 0;
2774 }
2775
2776 int sde_hdmi_dev_deinit(struct sde_hdmi *display)
2777 {
2778         if (!display) {
2779                 SDE_ERROR("Invalid params\n");
2780                 return -EINVAL;
2781         }
2782         if (display->hdcp_feat_data[SDE_HDCP_1x])
2783                 sde_hdcp_1x_deinit(display->hdcp_feat_data[SDE_HDCP_1x]);
2784
2785         if (display->hdcp_feat_data[SDE_HDCP_2P2])
2786                 sde_hdmi_hdcp2p2_deinit(display->hdcp_feat_data[SDE_HDCP_2P2]);
2787
2788         return 0;
2789 }
2790
2791 static int _sde_hdmi_cec_init(struct sde_hdmi *display)
2792 {
2793         struct platform_device *pdev = display->pdev;
2794
2795         display->notifier = cec_notifier_get(&pdev->dev);
2796         if (!display->notifier) {
2797                 SDE_ERROR("CEC notifier get failed\n");
2798                 return -ENOMEM;
2799         }
2800
2801         return 0;
2802 }
2803
2804 static void _sde_hdmi_cec_deinit(struct sde_hdmi *display)
2805 {
2806         cec_notifier_set_phys_addr(display->notifier, CEC_PHYS_ADDR_INVALID);
2807         cec_notifier_put(display->notifier);
2808 }
2809
2810 static int sde_hdmi_bind(struct device *dev, struct device *master, void *data)
2811 {
2812         int rc = 0;
2813         struct sde_hdmi_ctrl *display_ctrl = NULL;
2814         struct sde_hdmi *display = NULL;
2815         struct drm_device *drm = NULL;
2816         struct msm_drm_private *priv = NULL;
2817         struct platform_device *pdev = to_platform_device(dev);
2818
2819         SDE_HDMI_DEBUG(" %s +\n", __func__);
2820         if (!dev || !pdev || !master) {
2821                 pr_err("invalid param(s), dev %pK, pdev %pK, master %pK\n",
2822                         dev, pdev, master);
2823                 return -EINVAL;
2824         }
2825
2826         drm = dev_get_drvdata(master);
2827         display = platform_get_drvdata(pdev);
2828         if (!drm || !display) {
2829                 pr_err("invalid param(s), drm %pK, display %pK\n",
2830                            drm, display);
2831                 return -EINVAL;
2832         }
2833
2834         priv = drm->dev_private;
2835         mutex_lock(&display->display_lock);
2836
2837         rc = _sde_hdmi_debugfs_init(display);
2838         if (rc) {
2839                 SDE_ERROR("[%s]Debugfs init failed, rc=%d\n",
2840                                 display->name, rc);
2841                 goto debug_error;
2842         }
2843
2844         rc = _sde_hdmi_ext_disp_init(display);
2845         if (rc) {
2846                 SDE_ERROR("[%s]Ext Disp init failed, rc=%d\n",
2847                                 display->name, rc);
2848                 goto ext_error;
2849         }
2850
2851         rc = _sde_hdmi_cec_init(display);
2852         if (rc) {
2853                 SDE_ERROR("[%s]CEC init failed, rc=%d\n",
2854                                 display->name, rc);
2855                 goto ext_error;
2856         }
2857
2858         display->edid_ctrl = sde_edid_init();
2859         if (!display->edid_ctrl) {
2860                 SDE_ERROR("[%s]sde edid init failed\n",
2861                                 display->name);
2862                 rc = -ENOMEM;
2863                 goto cec_error;
2864         }
2865
2866         display_ctrl = &display->ctrl;
2867         display_ctrl->ctrl = priv->hdmi;
2868         display->drm_dev = drm;
2869
2870         _sde_hdmi_map_regs(display, priv->hdmi);
2871         _sde_hdmi_init_ddc(display, priv->hdmi);
2872
2873         display->enc_lvl = HDCP_STATE_AUTH_ENC_NONE;
2874
2875         INIT_DELAYED_WORK(&display->hdcp_cb_work,
2876                                           sde_hdmi_tx_hdcp_cb_work);
2877         mutex_init(&display->hdcp_mutex);
2878         mutex_unlock(&display->display_lock);
2879         return rc;
2880
2881 cec_error:
2882         (void)_sde_hdmi_cec_deinit(display);
2883 ext_error:
2884         (void)_sde_hdmi_debugfs_deinit(display);
2885 debug_error:
2886         mutex_unlock(&display->display_lock);
2887         return rc;
2888 }
2889
2890
2891 static void sde_hdmi_unbind(struct device *dev, struct device *master,
2892                 void *data)
2893 {
2894         struct sde_hdmi *display = NULL;
2895
2896         if (!dev) {
2897                 SDE_ERROR("invalid params\n");
2898                 return;
2899         }
2900
2901         display = platform_get_drvdata(to_platform_device(dev));
2902         if (!display) {
2903                 SDE_ERROR("Invalid display device\n");
2904                 return;
2905         }
2906         mutex_lock(&display->display_lock);
2907         (void)_sde_hdmi_debugfs_deinit(display);
2908         (void)sde_edid_deinit((void **)&display->edid_ctrl);
2909         (void)_sde_hdmi_cec_deinit(display);
2910         display->drm_dev = NULL;
2911         mutex_unlock(&display->display_lock);
2912 }
2913
2914 static const struct component_ops sde_hdmi_comp_ops = {
2915         .bind = sde_hdmi_bind,
2916         .unbind = sde_hdmi_unbind,
2917 };
2918
2919 static int _sde_hdmi_parse_dt_modes(struct device_node *np,
2920                                         struct list_head *head,
2921                                         u32 *num_of_modes)
2922 {
2923         int rc = 0;
2924         struct drm_display_mode *mode;
2925         u32 mode_count = 0;
2926         struct device_node *node = NULL;
2927         struct device_node *root_node = NULL;
2928         const char *name;
2929         u32 h_front_porch, h_pulse_width, h_back_porch;
2930         u32 v_front_porch, v_pulse_width, v_back_porch;
2931         bool h_active_high, v_active_high;
2932         u32 flags = 0;
2933         root_node = of_get_child_by_name(np, "qcom,customize-modes");
2934         if (!root_node) {
2935                 root_node = of_parse_phandle(np, "qcom,customize-modes", 0);
2936                 if (!root_node) {
2937                         DRM_INFO("No entry present for qcom,customize-modes");
2938                         goto end;
2939                 }
2940         }
2941         for_each_child_of_node(root_node, node) {
2942                 rc = 0;
2943                 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
2944                 if (!mode) {
2945                         SDE_ERROR("Out of memory\n");
2946                         rc =  -ENOMEM;
2947                         continue;
2948                 }
2949
2950                 rc = of_property_read_string(node, "qcom,mode-name",
2951                                                 &name);
2952                 if (rc) {
2953                         SDE_ERROR("failed to read qcom,mode-name, rc=%d\n", rc);
2954                         goto fail;
2955                 }
2956                 strlcpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
2957
2958                 rc = of_property_read_u32(node, "qcom,mode-h-active",
2959                                                 &mode->hdisplay);
2960                 if (rc) {
2961                         SDE_ERROR("failed to read h-active, rc=%d\n", rc);
2962                         goto fail;
2963                 }
2964
2965                 rc = of_property_read_u32(node, "qcom,mode-h-front-porch",
2966                                                 &h_front_porch);
2967                 if (rc) {
2968                         SDE_ERROR("failed to read h-front-porch, rc=%d\n", rc);
2969                         goto fail;
2970                 }
2971
2972                 rc = of_property_read_u32(node, "qcom,mode-h-pulse-width",
2973                                                 &h_pulse_width);
2974                 if (rc) {
2975                         SDE_ERROR("failed to read h-pulse-width, rc=%d\n", rc);
2976                         goto fail;
2977                 }
2978
2979                 rc = of_property_read_u32(node, "qcom,mode-h-back-porch",
2980                                                 &h_back_porch);
2981                 if (rc) {
2982                         SDE_ERROR("failed to read h-back-porch, rc=%d\n", rc);
2983                         goto fail;
2984                 }
2985
2986                 h_active_high = of_property_read_bool(node,
2987                                                 "qcom,mode-h-active-high");
2988
2989                 rc = of_property_read_u32(node, "qcom,mode-v-active",
2990                                                 &mode->vdisplay);
2991                 if (rc) {
2992                         SDE_ERROR("failed to read v-active, rc=%d\n", rc);
2993                         goto fail;
2994                 }
2995
2996                 rc = of_property_read_u32(node, "qcom,mode-v-front-porch",
2997                                                 &v_front_porch);
2998                 if (rc) {
2999                         SDE_ERROR("failed to read v-front-porch, rc=%d\n", rc);
3000                         goto fail;
3001                 }
3002
3003                 rc = of_property_read_u32(node, "qcom,mode-v-pulse-width",
3004                                                 &v_pulse_width);
3005                 if (rc) {
3006                         SDE_ERROR("failed to read v-pulse-width, rc=%d\n", rc);
3007                         goto fail;
3008                 }
3009
3010                 rc = of_property_read_u32(node, "qcom,mode-v-back-porch",
3011                                                 &v_back_porch);
3012                 if (rc) {
3013                         SDE_ERROR("failed to read v-back-porch, rc=%d\n", rc);
3014                         goto fail;
3015                 }
3016
3017                 v_active_high = of_property_read_bool(node,
3018                                                 "qcom,mode-v-active-high");
3019
3020                 rc = of_property_read_u32(node, "qcom,mode-refresh-rate",
3021                                                 &mode->vrefresh);
3022                 if (rc) {
3023                         SDE_ERROR("failed to read refresh-rate, rc=%d\n", rc);
3024                         goto fail;
3025                 }
3026
3027                 rc = of_property_read_u32(node, "qcom,mode-clock-in-khz",
3028                                                 &mode->clock);
3029                 if (rc) {
3030                         SDE_ERROR("failed to read clock, rc=%d\n", rc);
3031                         goto fail;
3032                 }
3033
3034                 mode->hsync_start = mode->hdisplay + h_front_porch;
3035                 mode->hsync_end = mode->hsync_start + h_pulse_width;
3036                 mode->htotal = mode->hsync_end + h_back_porch;
3037                 mode->vsync_start = mode->vdisplay + v_front_porch;
3038                 mode->vsync_end = mode->vsync_start + v_pulse_width;
3039                 mode->vtotal = mode->vsync_end + v_back_porch;
3040                 if (h_active_high)
3041                         flags |= DRM_MODE_FLAG_PHSYNC;
3042                 else
3043                         flags |= DRM_MODE_FLAG_NHSYNC;
3044                 if (v_active_high)
3045                         flags |= DRM_MODE_FLAG_PVSYNC;
3046                 else
3047                         flags |= DRM_MODE_FLAG_NVSYNC;
3048
3049                 flags |= DRM_MODE_FLAG_SUPPORTS_RGB;
3050                 mode->flags = flags;
3051
3052                 if (!rc) {
3053                         mode_count++;
3054                         list_add_tail(&mode->head, head);
3055                 }
3056
3057                 SDE_DEBUG("mode[%d] h[%d,%d,%d,%d] v[%d,%d,%d,%d] %d %xH %d\n",
3058                         mode_count - 1, mode->hdisplay, mode->hsync_start,
3059                         mode->hsync_end, mode->htotal, mode->vdisplay,
3060                         mode->vsync_start, mode->vsync_end, mode->vtotal,
3061                         mode->vrefresh, mode->flags, mode->clock);
3062 fail:
3063                 if (rc) {
3064                         kfree(mode);
3065                         continue;
3066                 }
3067         }
3068
3069         if (num_of_modes)
3070                 *num_of_modes = mode_count;
3071
3072 end:
3073         return rc;
3074 }
3075
3076 static int _sde_hdmi_parse_dt(struct device_node *node,
3077                                 struct sde_hdmi *display)
3078 {
3079         int rc = 0;
3080
3081         display->name = of_get_property(node, "label", NULL);
3082
3083         display->display_type = of_get_property(node,
3084                                                 "qcom,display-type", NULL);
3085         if (!display->display_type)
3086                 display->display_type = "unknown";
3087
3088         display->non_pluggable = of_property_read_bool(node,
3089                                                 "qcom,non-pluggable");
3090
3091         display->skip_ddc = of_property_read_bool(node,
3092                                                 "qcom,skip_ddc");
3093
3094         rc = _sde_hdmi_parse_dt_modes(node, &display->mode_list,
3095                                         &display->num_of_modes);
3096         if (rc)
3097                 SDE_ERROR("parse_dt_modes failed rc=%d\n", rc);
3098
3099         return rc;
3100 }
3101
3102 static int _sde_hdmi_dev_probe(struct platform_device *pdev)
3103 {
3104         int rc;
3105         struct sde_hdmi *display;
3106         int ret = 0;
3107
3108
3109         SDE_DEBUG("\n");
3110
3111         if (!pdev || !pdev->dev.of_node) {
3112                 SDE_ERROR("pdev not found\n");
3113                 return -ENODEV;
3114         }
3115
3116         display = devm_kzalloc(&pdev->dev, sizeof(*display), GFP_KERNEL);
3117         if (!display)
3118                 return -ENOMEM;
3119
3120         INIT_LIST_HEAD(&display->mode_list);
3121         rc = _sde_hdmi_parse_dt(pdev->dev.of_node, display);
3122         if (rc)
3123                 SDE_ERROR("parse dt failed, rc=%d\n", rc);
3124
3125         mutex_init(&display->display_lock);
3126         display->pdev = pdev;
3127         platform_set_drvdata(pdev, display);
3128         mutex_lock(&sde_hdmi_list_lock);
3129         list_add(&display->list, &sde_hdmi_list);
3130         mutex_unlock(&sde_hdmi_list_lock);
3131         if (!sde_hdmi_dev_init(display)) {
3132                 ret = component_add(&pdev->dev, &sde_hdmi_comp_ops);
3133                 if (ret) {
3134                         pr_err("component add failed\n");
3135                         goto out;
3136                 }
3137         }
3138         return 0;
3139
3140 out:
3141         if (rc)
3142                 devm_kfree(&pdev->dev, display);
3143         return rc;
3144 }
3145
3146 static int _sde_hdmi_dev_remove(struct platform_device *pdev)
3147 {
3148         struct sde_hdmi *display;
3149         struct sde_hdmi *pos, *tmp;
3150         struct drm_display_mode *mode, *n;
3151
3152         if (!pdev) {
3153                 SDE_ERROR("Invalid device\n");
3154                 return -EINVAL;
3155         }
3156
3157         display = platform_get_drvdata(pdev);
3158
3159         mutex_lock(&sde_hdmi_list_lock);
3160         list_for_each_entry_safe(pos, tmp, &sde_hdmi_list, list) {
3161                 if (pos == display) {
3162                         list_del(&display->list);
3163                         break;
3164                 }
3165         }
3166         mutex_unlock(&sde_hdmi_list_lock);
3167
3168         list_for_each_entry_safe(mode, n, &display->mode_list, head) {
3169                 list_del(&mode->head);
3170                 kfree(mode);
3171         }
3172
3173         platform_set_drvdata(pdev, NULL);
3174         devm_kfree(&pdev->dev, display);
3175         return 0;
3176 }
3177
3178 static struct platform_driver sde_hdmi_driver = {
3179         .probe = _sde_hdmi_dev_probe,
3180         .remove = _sde_hdmi_dev_remove,
3181         .driver = {
3182                 .name = "sde_hdmi",
3183                 .of_match_table = sde_hdmi_dt_match,
3184         },
3185 };
3186
3187 static int sde_hdmi_irqdomain_map(struct irq_domain *domain,
3188                 unsigned int irq, irq_hw_number_t hwirq)
3189 {
3190         struct sde_hdmi *display;
3191         int rc;
3192
3193         if (!domain || !domain->host_data) {
3194                 pr_err("invalid parameters domain\n");
3195                 return -EINVAL;
3196         }
3197         display = domain->host_data;
3198
3199         irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
3200         rc = irq_set_chip_data(irq, display);
3201
3202         return rc;
3203 }
3204
3205 static const struct irq_domain_ops sde_hdmi_irqdomain_ops = {
3206         .map = sde_hdmi_irqdomain_map,
3207         .xlate = irq_domain_xlate_onecell,
3208 };
3209
3210 int sde_hdmi_drm_init(struct sde_hdmi *display, struct drm_encoder *enc)
3211 {
3212         int rc = 0;
3213         struct msm_drm_private *priv = NULL;
3214         struct hdmi *hdmi;
3215         struct platform_device *pdev;
3216
3217         DBG("");
3218         if (!display || !display->drm_dev || !enc) {
3219                 SDE_ERROR("display=%p or enc=%p or drm_dev is NULL\n",
3220                         display, enc);
3221                 return -EINVAL;
3222         }
3223
3224         mutex_lock(&display->display_lock);
3225         priv = display->drm_dev->dev_private;
3226         hdmi = display->ctrl.ctrl;
3227
3228         if (!priv || !hdmi) {
3229                 SDE_ERROR("priv=%p or hdmi=%p is NULL\n",
3230                         priv, hdmi);
3231                 mutex_unlock(&display->display_lock);
3232                 return -EINVAL;
3233         }
3234
3235         pdev = hdmi->pdev;
3236         hdmi->dev = display->drm_dev;
3237         hdmi->encoder = enc;
3238
3239         hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
3240
3241         hdmi->bridge = sde_hdmi_bridge_init(hdmi, display);
3242         if (IS_ERR(hdmi->bridge)) {
3243                 rc = PTR_ERR(hdmi->bridge);
3244                 SDE_ERROR("failed to create HDMI bridge: %d\n", rc);
3245                 hdmi->bridge = NULL;
3246                 goto error;
3247         }
3248         hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
3249         if (hdmi->irq < 0) {
3250                 rc = hdmi->irq;
3251                 SDE_ERROR("failed to get irq: %d\n", rc);
3252                 goto error;
3253         }
3254
3255         rc = devm_request_irq(&pdev->dev, hdmi->irq,
3256                         _sde_hdmi_irq, IRQF_TRIGGER_HIGH,
3257                         "sde_hdmi_isr", display);
3258         if (rc < 0) {
3259                 SDE_ERROR("failed to request IRQ%u: %d\n",
3260                                 hdmi->irq, rc);
3261                 goto error;
3262         }
3263
3264         display->irq_domain = irq_domain_add_linear(pdev->dev.of_node, 8,
3265                                 &sde_hdmi_irqdomain_ops, display);
3266         if (!display->irq_domain) {
3267                 SDE_ERROR("failed to create IRQ domain\n");
3268                 goto error;
3269         }
3270
3271         enc->bridge = hdmi->bridge;
3272         priv->bridges[priv->num_bridges++] = hdmi->bridge;
3273
3274         /*
3275          * After initialising HDMI bridge, we need to check
3276          * whether the early display is enabled for HDMI.
3277          * If yes, we need to increase refcount of hdmi power
3278          * clocks. This can skip the clock disabling operation in
3279          * clock_late_init when finding clk.count == 1.
3280          */
3281         if (display->cont_splash_enabled) {
3282                 sde_hdmi_bridge_power_on(hdmi->bridge);
3283                 hdmi->power_on = true;
3284         }
3285
3286         mutex_unlock(&display->display_lock);
3287         return 0;
3288
3289 error:
3290         /* bridge is normally destroyed by drm: */
3291         if (hdmi->bridge) {
3292                 hdmi_bridge_destroy(hdmi->bridge);
3293                 hdmi->bridge = NULL;
3294         }
3295         mutex_unlock(&display->display_lock);
3296         return rc;
3297 }
3298
3299 int sde_hdmi_drm_deinit(struct sde_hdmi *display)
3300 {
3301         int rc = 0;
3302
3303         if (!display) {
3304                 SDE_ERROR("Invalid params\n");
3305                 return -EINVAL;
3306         }
3307
3308         if (display->irq_domain)
3309                 irq_domain_remove(display->irq_domain);
3310
3311         return rc;
3312 }
3313
3314 static int __init sde_hdmi_register(void)
3315 {
3316         int rc = 0;
3317
3318         DBG("");
3319         rc = platform_driver_register(&sde_hdmi_driver);
3320         return rc;
3321 }
3322
3323 static void __exit sde_hdmi_unregister(void)
3324 {
3325         platform_driver_unregister(&sde_hdmi_driver);
3326 }
3327
3328 module_init(sde_hdmi_register);
3329 module_exit(sde_hdmi_unregister);