2 * drivers/video/tegra/dc/hdmi2.0.c
4 * Copyright (c) 2014-2015, NVIDIA CORPORATION, All rights reserved.
5 * Author: Animesh Kishore <ankishore@nvidia.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/mutex.h>
23 #include <linux/device.h>
24 #include <linux/clk/tegra.h>
25 #include <linux/nvhost.h>
26 #include <linux/gpio.h>
27 #include <linux/interrupt.h>
28 #include <linux/debugfs.h>
29 #include <linux/unistd.h>
31 #include <linux/switch.h>
33 #include <linux/of_address.h>
36 #include <mach/hdmi-audio.h>
38 #include <mach/powergate.h>
48 #ifdef CONFIG_ADF_TEGRA
49 #include "tegra_adf.h"
52 #include <linux/tegra_prod.h>
53 #include "../../../../arch/arm/mach-tegra/iomap.h"
55 #define TMDS_NODE "/host1x/sor1"
57 extern bool G_RECOVERY_HACK;
59 static ssize_t hdmi_ddc_power_toggle(struct kobject *kobj,
60 struct kobj_attribute *attr, const char *buf, size_t count);
62 static ssize_t hdmi_ddc_power_show(struct kobject *kobj,
63 struct kobj_attribute *attr, char *buf);
65 static struct kobj_attribute hdmi_ddc_power_config =
66 __ATTR(config, 0640, hdmi_ddc_power_show, hdmi_ddc_power_toggle);
68 static struct kobject *hdmi_ddc;
70 struct tmds_prod_pair {
75 static struct tmds_prod_pair tmds_config_modes[] = {
98 static struct tegra_hdmi *dc_hdmi;
100 static int tegra_hdmi_controller_enable(struct tegra_hdmi *hdmi);
101 static void tegra_hdmi_config_clk(struct tegra_hdmi *hdmi, u32 clk_type);
102 static long tegra_dc_hdmi_setup_clk(struct tegra_dc *dc, struct clk *clk);
103 static void tegra_hdmi_get(struct tegra_dc *dc);
104 static void tegra_hdmi_put(struct tegra_dc *dc);
105 static void tegra_hdmi_scdc_worker(struct work_struct *work);
106 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi);
108 static inline bool tegra_hdmi_is_connected(struct tegra_hdmi *hdmi)
110 return (hdmi->mon_spec.misc & FB_MISC_HDMI) ||
111 (hdmi->mon_spec.misc & FB_MISC_HDMI_FORUM);
114 static inline void tegra_hdmi_irq_enable(struct tegra_hdmi *hdmi)
116 if (tegra_platform_is_fpga())
119 enable_irq(hdmi->irq);
122 static inline void tegra_hdmi_irq_disable(struct tegra_hdmi *hdmi)
124 if (tegra_platform_is_fpga())
127 disable_irq(hdmi->irq);
130 static inline bool tegra_hdmi_hpd_asserted(struct tegra_hdmi *hdmi)
132 return tegra_dc_hpd(hdmi->dc);
135 static inline void tegra_hdmi_reset(struct tegra_hdmi *hdmi)
137 tegra_periph_reset_assert(hdmi->sor->sor_clk);
139 tegra_periph_reset_deassert(hdmi->sor->sor_clk);
143 static void tegra_hdmi_get_eld_header(struct tegra_hdmi *hdmi,
146 struct tegra_edid_hdmi_eld *eld = &hdmi->eld;
148 eld->baseline_len = HDMI_ELD_MONITOR_NAME_STR + eld->mnl +
149 eld->sad_count * 3 - HDMI_ELD_CEA_EDID_VER_MNL;
151 eld_mem_block[HDMI_ELD_VER] = eld->eld_ver << 3;
152 eld_mem_block[HDMI_ELD_BASELINE_ELD_LEN] =
153 DIV_ROUND_UP(eld->baseline_len, 4);
157 static void tegra_hdmi_get_eld_baseline(struct tegra_hdmi *hdmi,
160 struct tegra_edid_hdmi_eld *eld = &hdmi->eld;
163 tmp = eld->mnl | (eld->cea_edid_ver << 5);
164 eld_mem_block[HDMI_ELD_CEA_EDID_VER_MNL] = tmp;
166 tmp = eld->support_hdcp | (eld->support_ai << 1) |
167 (eld->conn_type << 2) | (eld->sad_count << 4);
168 eld_mem_block[HDMI_ELD_SAD_CNT_CON_TYPE_S_AI_S_HDCP] = tmp;
170 eld_mem_block[HDMI_ELD_AUDIO_SYNC_DELAY] = eld->aud_synch_delay;
172 eld_mem_block[HDMI_ELD_RLRC_FLRC_RC_RLR_FC_LFE_FLR] = eld->spk_alloc;
174 memcpy(&eld_mem_block[HDMI_ELD_PORT_ID], eld->port_id, 8);
176 memcpy(&eld_mem_block[HDMI_ELD_MANUFACTURER_NAME],
177 eld->manufacture_id, 2);
179 memcpy(&eld_mem_block[HDMI_ELD_PRODUCT_CODE], eld->product_id, 2);
181 memcpy(&eld_mem_block[HDMI_ELD_MONITOR_NAME_STR],
182 eld->monitor_name, eld->mnl);
184 memcpy(&eld_mem_block[HDMI_ELD_MONITOR_NAME_STR + eld->mnl],
185 eld->sad, eld->sad_count * 3);
188 static void tegra_hdmi_get_eld_vendor(struct tegra_hdmi *hdmi,
191 struct tegra_edid_hdmi_eld *eld = &hdmi->eld;
192 u32 vendor_block_index = 4 + eld->baseline_len; /* 4 byte header */
194 if (!eld->baseline_len)
195 dev_err(&hdmi->dc->ndev->dev,
196 "hdm: eld baseline length not populated\n");
198 memset(&eld_mem_block[vendor_block_index], 0,
199 HDMI_ELD_BUF - vendor_block_index + 1);
202 static int tegra_hdmi_eld_config(struct tegra_hdmi *hdmi)
207 eld_mem = devm_kzalloc(&hdmi->dc->ndev->dev,
208 HDMI_ELD_BUF, GFP_KERNEL);
210 dev_warn(&hdmi->dc->ndev->dev,
211 "hdmi: eld memory allocation failed\n");
215 tegra_hdmi_get_eld_header(hdmi, eld_mem);
216 tegra_hdmi_get_eld_baseline(hdmi, eld_mem);
217 tegra_hdmi_get_eld_vendor(hdmi, eld_mem);
219 for (cnt = 0; cnt < HDMI_ELD_BUF; cnt++)
220 tegra_sor_writel(hdmi->sor, NV_SOR_AUDIO_HDA_ELD_BUFWR,
221 NV_SOR_AUDIO_HDA_ELD_BUFWR_INDEX(cnt) |
222 NV_SOR_AUDIO_HDA_ELD_BUFWR_DATA(eld_mem[cnt]));
224 devm_kfree(&hdmi->dc->ndev->dev, eld_mem);
228 int tegra_hdmi_setup_hda_presence(void)
230 struct tegra_hdmi *hdmi = dc_hdmi;
241 if (hdmi->enabled && hdmi->eld_valid) {
242 tegra_dc_unpowergate_locked(dc);
243 tegra_hdmi_get(hdmi->dc);
244 tegra_dc_io_start(dc);
246 /* remove hda presence while setting up eld */
247 tegra_sor_writel(hdmi->sor, NV_SOR_AUDIO_HDA_PRESENCE, 0);
249 tegra_hdmi_eld_config(hdmi);
250 tegra_sor_writel(hdmi->sor, NV_SOR_AUDIO_HDA_PRESENCE,
251 NV_SOR_AUDIO_HDA_PRESENCE_ELDV(1) |
252 NV_SOR_AUDIO_HDA_PRESENCE_PD(1));
254 tegra_hdmi_put(hdmi->dc);
256 tegra_dc_powergate_locked(dc);
264 static inline void _tegra_hdmi_ddc_enable(struct tegra_hdmi *hdmi)
266 mutex_lock(&hdmi->ddc_refcount_lock);
267 if (hdmi->ddc_refcount++)
269 tegra_hdmi_get(hdmi->dc);
271 * hdmi uses i2c lane muxed on dpaux1 pad.
272 * Enable dpaux1 pads and configure the mux.
274 tegra_dpaux_config_pad_mode(hdmi->dc, TEGRA_DPAUX_INSTANCE_1,
275 TEGRA_DPAUX_PAD_MODE_I2C);
278 mutex_unlock(&hdmi->ddc_refcount_lock);
281 static inline void _tegra_hdmi_ddc_disable(struct tegra_hdmi *hdmi)
283 mutex_lock(&hdmi->ddc_refcount_lock);
285 if (WARN_ONCE(hdmi->ddc_refcount <= 0, "ddc refcount imbalance"))
287 if (--hdmi->ddc_refcount != 0)
291 * hdmi uses i2c lane muxed on dpaux1 pad.
292 * Disable dpaux1 pads.
294 tegra_dpaux_pad_power(hdmi->dc, TEGRA_DPAUX_INSTANCE_1, false);
295 tegra_hdmi_put(hdmi->dc);
298 mutex_unlock(&hdmi->ddc_refcount_lock);
301 static int tegra_hdmi_ddc_i2c_xfer(struct tegra_dc *dc,
302 struct i2c_msg *msgs, int num)
304 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
307 _tegra_hdmi_ddc_enable(hdmi);
308 ret = i2c_transfer(hdmi->ddc_i2c_client->adapter, msgs, num);
309 _tegra_hdmi_ddc_disable(hdmi);
313 static int tegra_hdmi_ddc_init(struct tegra_hdmi *hdmi, int edid_src)
315 struct tegra_dc *dc = hdmi->dc;
316 struct i2c_adapter *i2c_adap;
318 struct i2c_board_info i2c_dev_info = {
319 .type = "tegra_hdmi2.0",
323 hdmi->edid = tegra_edid_create(dc, tegra_hdmi_ddc_i2c_xfer);
324 else if (edid_src == 1)
325 hdmi->edid = tegra_edid_create(dc, tegra_dc_edid_blob);
326 if (IS_ERR_OR_NULL(hdmi->edid)) {
327 dev_err(&dc->ndev->dev, "hdmi: can't create edid\n");
328 return PTR_ERR(hdmi->edid);
330 tegra_dc_set_edid(dc, hdmi->edid);
332 i2c_adap = i2c_get_adapter(dc->out->ddc_bus);
334 dev_err(&dc->ndev->dev,
335 "hdmi: can't get adpater for ddc bus %d\n",
340 hdmi->ddc_i2c_original_rate = i2c_get_adapter_bus_clk_rate(i2c_adap);
342 hdmi->ddc_i2c_client = i2c_new_device(i2c_adap, &i2c_dev_info);
343 i2c_put_adapter(i2c_adap);
344 if (!hdmi->ddc_i2c_client) {
345 dev_err(&dc->ndev->dev, "hdmi: can't create new i2c device\n");
352 tegra_edid_destroy(hdmi->edid);
356 static int tegra_hdmi_scdc_i2c_xfer(struct tegra_dc *dc,
357 struct i2c_msg *msgs, int num)
359 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
361 return i2c_transfer(hdmi->scdc_i2c_client->adapter, msgs, num);
364 static int tegra_hdmi_scdc_init(struct tegra_hdmi *hdmi)
366 struct tegra_dc *dc = hdmi->dc;
367 struct i2c_adapter *i2c_adap;
369 struct i2c_board_info i2c_dev_info = {
370 .type = "tegra_hdmi_scdc",
374 i2c_adap = i2c_get_adapter(dc->out->ddc_bus);
376 dev_err(&dc->ndev->dev,
377 "hdmi: can't get adpater for scdc bus %d\n",
383 hdmi->scdc_i2c_client = i2c_new_device(i2c_adap, &i2c_dev_info);
384 i2c_put_adapter(i2c_adap);
385 if (!hdmi->scdc_i2c_client) {
386 dev_err(&dc->ndev->dev,
387 "hdmi: can't create scdc i2c device\n");
392 INIT_DELAYED_WORK(&hdmi->scdc_work, tegra_hdmi_scdc_worker);
399 static void tegra_hdmi_hda_clk_enable(struct tegra_hdmi *hdmi)
401 if (tegra_platform_is_linsim())
404 clk_prepare_enable(hdmi->hda_clk);
405 clk_prepare_enable(hdmi->hda2codec_clk);
406 clk_prepare_enable(hdmi->hda2hdmi_clk);
409 static void tegra_hdmi_hda_clk_disable(struct tegra_hdmi *hdmi)
411 if (tegra_platform_is_linsim())
414 clk_disable_unprepare(hdmi->hda2hdmi_clk);
415 clk_disable_unprepare(hdmi->hda2codec_clk);
416 clk_disable_unprepare(hdmi->hda_clk);
419 static int tegra_hdmi_hda_clk_get(struct tegra_hdmi *hdmi)
422 struct tegra_dc *dc = hdmi->dc;
424 if (tegra_platform_is_linsim())
427 hdmi->hda_clk = clk_get_sys("tegra30-hda", "hda");
428 if (IS_ERR_OR_NULL(hdmi->hda_clk)) {
429 dev_err(&dc->ndev->dev, "hdmi: can't get hda clock\n");
434 hdmi->hda2codec_clk = clk_get_sys("tegra30-hda", "hda2codec");
435 if (IS_ERR_OR_NULL(hdmi->hda_clk)) {
436 dev_err(&dc->ndev->dev, "hdmi: can't get hda clock\n");
441 hdmi->hda2hdmi_clk = clk_get_sys("tegra30-hda", "hda2hdmi");
442 if (IS_ERR_OR_NULL(hdmi->hda_clk)) {
443 dev_err(&dc->ndev->dev, "hdmi: can't get hda clock\n");
450 if (!IS_ERR_OR_NULL(hdmi->hda2hdmi_clk))
451 clk_put(hdmi->hda2hdmi_clk);
452 if (!IS_ERR_OR_NULL(hdmi->hda2codec_clk))
453 clk_put(hdmi->hda2codec_clk);
454 if (!IS_ERR_OR_NULL(hdmi->hda_clk))
455 clk_put(hdmi->hda_clk);
459 static bool tegra_hdmi_check_dc_constraint(const struct fb_videomode *mode)
461 return (mode->hsync_len >= 1) && (mode->vsync_len >= 1) &&
462 (mode->lower_margin + mode->vsync_len +
463 mode->upper_margin > 1) &&
464 (mode->xres >= 16) && (mode->yres >= 16);
467 /* does not return precise tmds character rate */
468 static u32 tegra_hdmi_mode_min_tmds_rate(const struct fb_videomode *mode)
470 u32 tmds_csc_8bpc_khz = PICOS2KHZ(mode->pixclock);
472 if (mode->vmode & (FB_VMODE_Y420 | FB_VMODE_Y420_ONLY))
473 tmds_csc_8bpc_khz /= 2;
475 return tmds_csc_8bpc_khz;
479 static bool tegra_hdmi_fb_mode_filter(const struct tegra_dc *dc,
480 struct fb_videomode *mode)
482 struct tegra_hdmi *hdmi = dc->out_data;
487 if (mode->xres > 4096)
490 /* some non-compliant edids list 420vdb modes in vdb */
491 if ((mode->vmode & FB_VMODE_Y420) &&
492 !(tegra_edid_is_hfvsdb_present(hdmi->edid) &&
493 tegra_edid_is_scdc_present(hdmi->edid)) &&
494 tegra_edid_is_420db_present(hdmi->edid)) {
495 mode->vmode &= ~FB_VMODE_Y420;
496 mode->vmode |= FB_VMODE_Y420_ONLY;
500 * There are currently many TVs in the market that actually do NOT support
501 * 4k@60fps 4:4:4 (594 MHz), (especially on the HDCP 2.2 ports), but
502 * advertise it in the DTD block in their EDIDs. The workaround for this port
503 * is to disable the 594 MHz mode if no HF-VSDB is present or if no SCDC
504 * support is indicated
506 if (tegra_hdmi_mode_min_tmds_rate(mode) / 1000 >= 340 &&
507 (!tegra_edid_is_hfvsdb_present(hdmi->edid) ||
508 !tegra_edid_is_scdc_present(hdmi->edid)))
512 * Check if mode's pixel clock requirement can be satisfied. Note that
513 * the pixclock value is in pico seconds.
515 if (mode->pixclock && tegra_dc_get_out_max_pixclock(dc) &&
516 mode->pixclock < tegra_dc_get_out_max_pixclock(dc))
520 * Work around for modes that fail the constraint:
521 * V_FRONT_PORCH >= V_REF_TO_SYNC + 1
523 if (mode->lower_margin == 1) {
524 mode->lower_margin++;
525 mode->upper_margin--;
528 if (!tegra_hdmi_check_dc_constraint(mode))
534 static void tegra_hdmi_ddc_power_toggle(int value)
540 _tegra_hdmi_ddc_disable(dc_hdmi);
541 tegra_powergate_partition(TEGRA_POWERGATE_SOR);
542 } else if (value == 1) {
543 tegra_unpowergate_partition(TEGRA_POWERGATE_SOR);
544 _tegra_hdmi_ddc_enable(dc_hdmi);
550 static int tegra_hdmi_get_mon_spec(struct tegra_hdmi *hdmi)
552 #define MAX_RETRY 100
553 #define MIN_RETRY_DELAY_US 200
554 #define MAX_RETRY_DELAY_US (MIN_RETRY_DELAY_US + 200)
556 size_t attempt_cnt = 0;
558 struct i2c_adapter *i2c_adap = i2c_get_adapter(hdmi->dc->out->ddc_bus);
560 if (IS_ERR_OR_NULL(hdmi->edid)) {
561 dev_err(&hdmi->dc->ndev->dev, "hdmi: edid not initialized\n");
562 return PTR_ERR(hdmi->edid);
565 tegra_edid_i2c_adap_change_rate(i2c_adap, hdmi->ddc_i2c_original_rate);
567 hdmi->mon_spec_valid = false;
568 if (hdmi->mon_spec_valid)
569 fb_destroy_modedb(hdmi->mon_spec.modedb);
570 memset(&hdmi->mon_spec, 0, sizeof(hdmi->mon_spec));
573 err = tegra_edid_get_monspecs(hdmi->edid, &hdmi->mon_spec);
575 usleep_range(MIN_RETRY_DELAY_US, MAX_RETRY_DELAY_US);
578 } while (++attempt_cnt < MAX_RETRY);
581 dev_err(&hdmi->dc->ndev->dev, "hdmi: edid read failed\n");
585 hdmi->mon_spec_valid = true;
588 #undef MAX_RETRY_DELAY_US
589 #undef MIN_RETRY_DELAY_US
593 static inline int tegra_hdmi_edid_read(struct tegra_hdmi *hdmi)
597 err = tegra_hdmi_get_mon_spec(hdmi);
602 static int tegra_hdmi_get_eld(struct tegra_hdmi *hdmi)
606 hdmi->eld_valid = false;
607 memset(&hdmi->eld, 0, sizeof(hdmi->eld));
609 err = tegra_edid_get_eld(hdmi->edid, &hdmi->eld);
611 dev_err(&hdmi->dc->ndev->dev, "hdmi: eld not available\n");
615 hdmi->eld_valid = true;
619 static inline int tegra_hdmi_eld_read(struct tegra_hdmi *hdmi)
621 return tegra_hdmi_get_eld(hdmi);
624 static void tegra_hdmi_edid_config(struct tegra_hdmi *hdmi)
626 #define CM_TO_MM(x) (x * 10)
628 struct tegra_dc *dc = hdmi->dc;
630 if (!hdmi->mon_spec_valid)
633 dc->out->h_size = CM_TO_MM(hdmi->mon_spec.max_x);
634 dc->out->v_size = CM_TO_MM(hdmi->mon_spec.max_y);
636 hdmi->dvi = !tegra_hdmi_is_connected(hdmi);
640 static void tegra_hdmi_hotplug_notify(struct tegra_hdmi *hdmi,
643 struct tegra_dc *dc = hdmi->dc;
644 struct fb_monspecs *mon_spec;
647 mon_spec = &hdmi->mon_spec;
651 #ifdef CONFIG_ADF_TEGRA
653 tegra_adf_process_hotplug_connected(hdmi->dc->adf, mon_spec);
656 tegra_fb_update_monspecs(hdmi->dc->fb, mon_spec,
657 tegra_hdmi_fb_mode_filter);
658 tegra_fb_update_fix(hdmi->dc->fb, mon_spec);
662 dc->connected = is_asserted;
663 tegra_dc_ext_process_hotplug(dc->ndev->id, is_asserted);
666 switch_set_state(&hdmi->hpd_switch, is_asserted ? 1 : 0);
670 static int tegra_hdmi_edid_eld_setup(struct tegra_hdmi *hdmi)
674 tegra_dc_unpowergate_locked(hdmi->dc);
676 err = tegra_hdmi_edid_read(hdmi);
680 err = tegra_hdmi_eld_read(hdmi);
684 tegra_dc_powergate_locked(hdmi->dc);
686 tegra_hdmi_edid_config(hdmi);
689 * eld is configured when audio needs it
690 * via tegra_hdmi_edid_config()
693 tegra_hdmi_hotplug_notify(hdmi, true);
696 tegra_dc_powergate_locked(hdmi->dc);
700 static int tegra_hdmi_controller_disable(struct tegra_hdmi *hdmi)
702 struct tegra_dc_sor_data *sor = hdmi->sor;
703 struct tegra_dc *dc = hdmi->dc;
707 if (!G_RECOVERY_HACK)
708 tegra_nvhdcp_set_plug(hdmi->nvhdcp, 0);
710 tegra_dc_sor_detach(sor);
711 tegra_sor_power_lanes(sor, 4, false);
712 tegra_sor_hdmi_pad_power_down(sor);
713 tegra_hdmi_hda_clk_disable(hdmi);
714 tegra_hdmi_reset(hdmi);
722 static int tegra_hdmi_disable(struct tegra_hdmi *hdmi)
724 struct tegra_dc *dc = hdmi->dc;
726 if (!hdmi->enabled) {
727 dc->connected = false;
728 tegra_dc_ext_process_hotplug(dc->ndev->id, false);
730 switch_set_state(&hdmi->hpd_switch, 0);
735 hdmi->enabled = false;
736 hdmi->eld_valid = false;
737 hdmi->mon_spec_valid = false;
739 tegra_dc_disable(hdmi->dc);
741 tegra_hdmi_hotplug_notify(hdmi, false);
746 static int (*tegra_hdmi_state_func[])(struct tegra_hdmi *) = {
748 tegra_hdmi_edid_eld_setup,
751 enum tegra_hdmi_plug_states {
752 TEGRA_HDMI_MONITOR_DISABLE,
753 TEGRA_HDMI_MONITOR_ENABLE,
756 static int read_edid_into_buffer(struct tegra_hdmi *hdmi,
757 u8 *edid_data, size_t edid_data_len)
760 int extension_blocks;
761 int max_ext_blocks = (edid_data_len / 128) - 1;
763 err = tegra_edid_read_block(hdmi->edid, 0, edid_data);
765 dev_info(&hdmi->dc->ndev->dev, "hdmi: tegra_edid_read_block(0) returned err %d\n",
769 extension_blocks = edid_data[0x7e];
770 dev_info(&hdmi->dc->ndev->dev, "%s: extension_blocks = %d, max_ext_blocks = %d\n",
771 __func__, extension_blocks, max_ext_blocks);
772 if (extension_blocks > max_ext_blocks)
773 extension_blocks = max_ext_blocks;
774 for (i = 1; i <= extension_blocks; i++) {
775 err = tegra_edid_read_block(hdmi->edid, i, edid_data + i * 128);
777 dev_info(&hdmi->dc->ndev->dev, "hdmi: tegra_edid_read_block(%d) returned err %d\n",
785 static int hdmi_recheck_edid(struct tegra_hdmi *hdmi, int *match)
788 u8 tmp[HDMI_EDID_MAX_LENGTH] = {0};
789 ret = read_edid_into_buffer(hdmi, tmp, sizeof(tmp));
790 dev_info(&hdmi->dc->ndev->dev, "%s: read_edid_into_buffer() returned %d\n",
793 struct tegra_dc_edid *data = tegra_edid_get_data(hdmi->edid);
794 dev_info(&hdmi->dc->ndev->dev, "old edid len = %ld\n",
795 (long int)data->len);
796 *match = ((ret == data->len) &&
797 !memcmp(tmp, data->buf, data->len));
799 print_hex_dump(KERN_INFO, "tmp :", DUMP_PREFIX_ADDRESS,
800 16, 4, tmp, ret, true);
801 print_hex_dump(KERN_INFO, "data:", DUMP_PREFIX_ADDRESS,
802 16, 4, data->buf, data->len, true);
804 tegra_edid_put_data(data);
810 static void tegra_hdmi_hpd_worker(struct work_struct *work)
812 struct tegra_hdmi *hdmi = container_of(to_delayed_work(work),
813 struct tegra_hdmi, hpd_worker);
816 enum tegra_hdmi_plug_states orig_state;
819 mutex_lock(&hdmi->hpd_lock);
821 connected = tegra_dc_hpd(hdmi->dc);
822 orig_state = hdmi->plug_state;
824 if ((connected && orig_state == TEGRA_HDMI_MONITOR_ENABLE)) {
825 if (hdmi_recheck_edid(hdmi, &match)) {
826 dev_info(&hdmi->dc->ndev->dev, "hdmi: unable to read EDID\n");
830 dev_info(&hdmi->dc->ndev->dev, "hdmi: No EDID change after HPD bounce, taking no action.\n");
833 dev_info(&hdmi->dc->ndev->dev, "hdmi: EDID change after HPD bounce, resetting\n");
837 if ((!connected && orig_state == TEGRA_HDMI_MONITOR_DISABLE)) {
838 dev_info(&hdmi->dc->ndev->dev, "hdmi: spurious interrupt\n");
839 mutex_unlock(&hdmi->hpd_lock);
844 hdmi->plug_state = TEGRA_HDMI_MONITOR_ENABLE;
846 hdmi->plug_state = TEGRA_HDMI_MONITOR_DISABLE;
848 err = tegra_hdmi_state_func[hdmi->plug_state](hdmi);
851 dev_info(&hdmi->dc->ndev->dev,
852 "hdmi state %d failed during %splug\n",
853 hdmi->plug_state, connected ? "" : "un");
854 hdmi->plug_state = orig_state;
856 dev_info(&hdmi->dc->ndev->dev, "hdmi: %splugged\n",
857 connected ? "" : "un");
861 mutex_unlock(&hdmi->hpd_lock);
865 static irqreturn_t tegra_hdmi_hpd_irq_handler(int irq, void *ptr)
867 struct tegra_dc *dc = ptr;
868 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
873 if (atomic_read(&hdmi->suspended))
876 cancel_delayed_work(&hdmi->hpd_worker);
877 schedule_delayed_work(&hdmi->hpd_worker,
878 msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS));
883 static int tegra_hdmi_hpd_init(struct tegra_hdmi *hdmi)
885 struct tegra_dc *dc = hdmi->dc;
886 int hotplug_gpio = dc->out->hotplug_gpio;
890 if (!gpio_is_valid(hotplug_gpio)) {
891 dev_err(&dc->ndev->dev, "hdmi: invalid hotplug gpio\n");
895 hotplug_irq = gpio_to_irq(hotplug_gpio);
896 if (hotplug_irq < 0) {
897 dev_err(&dc->ndev->dev,
898 "hdmi: hotplug gpio to irq map failed\n");
902 err = gpio_request(hotplug_gpio, "hdmi2.0_hpd");
904 dev_err(&dc->ndev->dev,
905 "hdmi: hpd gpio_request failed %d\n", err);
906 gpio_direction_input(hotplug_gpio);
908 err = request_threaded_irq(hotplug_irq,
909 NULL, tegra_hdmi_hpd_irq_handler,
910 (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
912 dev_name(&dc->ndev->dev), dc);
914 dev_err(&dc->ndev->dev,
915 "hdmi: request_threaded_irq failed: %d\n", err);
920 INIT_DELAYED_WORK(&hdmi->hpd_worker, tegra_hdmi_hpd_worker);
922 mutex_init(&hdmi->hpd_lock);
923 hdmi->irq = hotplug_irq;
927 gpio_free(hotplug_gpio);
931 static int tegra_hdmi_tmds_init(struct tegra_hdmi *hdmi)
933 struct device_node *np_prod = of_find_node_by_path(TMDS_NODE);
936 dev_warn(&hdmi->dc->ndev->dev,
937 "hdmi: find tmds prod node failed\n");
943 tegra_prod_init((const struct device_node *)np_prod);
944 if (IS_ERR(hdmi->prod_list)) {
945 dev_warn(&hdmi->dc->ndev->dev,
946 "hdmi: prod list init failed with error %ld\n",
947 PTR_ERR(hdmi->prod_list));
948 of_node_put(np_prod);
952 of_node_put(np_prod);
956 static int tegra_hdmi_config_tmds(struct tegra_hdmi *hdmi)
962 /* Select mode with smallest clk freq > pclk */
963 tmds_len = ARRAY_SIZE(tmds_config_modes);
964 for (i = 0; i < tmds_len - 1 &&
965 tmds_config_modes[i].clk < hdmi->dc->mode.pclk; i++)
968 if (tegra_platform_is_linsim())
971 err = tegra_prod_set_by_name(&hdmi->sor->base,
972 tmds_config_modes[i].name, hdmi->prod_list);
974 dev_warn(&hdmi->dc->ndev->dev,
975 "hdmi: tmds prod set failed\n");
982 static int tegra_dc_hdmi_init(struct tegra_dc *dc)
984 struct tegra_hdmi *hdmi;
986 struct device_node *np = dc->ndev->dev.of_node;
988 struct device_node *np_hdmi = of_find_node_by_path(HDMI_NODE);
990 struct device_node *np_hdmi = NULL;
992 struct device_node *np_panel = NULL;
995 hdmi = devm_kzalloc(&dc->ndev->dev, sizeof(*hdmi), GFP_KERNEL);
997 of_node_put(np_hdmi);
1001 hdmi->sor = tegra_dc_sor_init(dc, NULL);
1002 if (IS_ERR_OR_NULL(hdmi->sor)) {
1003 err = PTR_ERR(hdmi->sor);
1008 if (np_hdmi && of_device_is_available(np_hdmi)) {
1009 np_panel = tegra_get_panel_node_out_type_check(dc,
1011 if (np_panel && of_device_is_available(np_panel)) {
1012 if (of_property_read_bool(np_panel,
1015 of_node_put(np_panel);
1019 of_node_put(np_panel);
1024 hdmi->pdata = dc->pdata->default_out->hdmi_out;
1027 hdmi->ddc_refcount = 0; /* assumes this is disabled when starting */
1028 mutex_init(&hdmi->ddc_refcount_lock);
1029 hdmi->nvhdcp = NULL;
1030 hdmi->mon_spec_valid = false;
1031 hdmi->eld_valid = false;
1032 hdmi->device_shutdown = false;
1034 /* TODO: seamless boot mode needs initialize the state */
1036 hdmi->enabled = false;
1037 atomic_set(&hdmi->clock_refcount, 0);
1039 atomic_set(&hdmi->suspended, 0);
1041 #ifdef CONFIG_TEGRA_HDMIHDCP
1042 hdmi->nvhdcp = tegra_nvhdcp_create(hdmi, tegra_dc_which_sor(dc),
1044 if (IS_ERR_OR_NULL(hdmi->nvhdcp)) {
1045 err = PTR_ERR(hdmi->nvhdcp);
1048 tegra_nvhdcp_debugfs_init(hdmi->nvhdcp);
1051 tegra_hdmi_hda_clk_get(hdmi);
1053 tegra_hdmi_ddc_init(hdmi, edid_src);
1055 tegra_hdmi_scdc_init(hdmi);
1057 tegra_hdmi_hpd_init(hdmi);
1059 tegra_hdmi_debugfs_init(hdmi);
1061 tegra_hdmi_tmds_init(hdmi);
1063 tegra_dc_set_outdata(dc, hdmi);
1065 /* NOTE: Below code is applicable to L4T or embedded systems and is
1066 * protected accordingly. This section early enables DC with first mode
1067 * from the monitor specs.
1068 * In case there is no hotplug we are falling back
1069 * to default VGA mode.
1071 if ((config_enabled(CONFIG_FRAMEBUFFER_CONSOLE) ||
1072 ((dc->pdata->flags & TEGRA_DC_FLAG_ENABLED) &&
1073 (dc->pdata->flags & TEGRA_DC_FLAG_SET_EARLY_MODE))) &&
1074 dc->out && (dc->out->type == TEGRA_DC_OUT_HDMI)) {
1075 struct fb_monspecs specs;
1076 if (tegra_dc_hpd(dc) && (!dc->initialized)) {
1077 if (!tegra_edid_get_monspecs(hdmi->edid, &specs))
1078 tegra_dc_set_fb_mode(dc, specs.modedb, false);
1080 /* if for some reason there is no edid upon hotplug */
1081 tegra_dc_set_fb_mode(dc,
1082 &tegra_dc_vga_mode, false);
1084 kfree(specs.modedb);
1086 tegra_dc_set_fb_mode(dc, &tegra_dc_vga_mode, false);
1089 #ifdef CONFIG_SWITCH
1090 hdmi->hpd_switch.name = "hdmi";
1091 err = switch_dev_register(&hdmi->hpd_switch);
1093 dev_err(&dc->ndev->dev,
1094 "hdmi: failed to register hpd switch %d\n", err);
1096 hdmi->audio_switch.name = "hdmi_audio";
1097 err = switch_dev_register(&hdmi->audio_switch);
1099 dev_err(&dc->ndev->dev,
1100 "hdmi: failed to register audio switch %d\n", err);
1103 hdmi_ddc = kobject_create_and_add("hdmi_ddc_power_toggle", kernel_kobj);
1105 pr_warn("kobject create_and_add hdmi_ddc_power_toggle failed\n");
1108 err = sysfs_create_file(hdmi_ddc, &hdmi_ddc_power_config.attr);
1110 pr_warn("sysfs create file hdmi_ddc_power_toggle failed\n");
1114 of_node_put(np_hdmi);
1117 devm_kfree(&dc->ndev->dev, hdmi);
1118 of_node_put(np_hdmi);
1122 static void tegra_dc_hdmi_destroy(struct tegra_dc *dc)
1124 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1126 tegra_dc_sor_destroy(hdmi->sor);
1127 tegra_edid_destroy(hdmi->edid);
1128 tegra_nvhdcp_destroy(hdmi->nvhdcp);
1129 free_irq(gpio_to_irq(dc->out->hotplug_gpio), dc);
1130 gpio_free(dc->out->hotplug_gpio);
1131 devm_kfree(&dc->ndev->dev, hdmi);
1132 tegra_prod_release(&hdmi->prod_list);
1134 #ifdef CONFIG_SWITCH
1135 switch_dev_unregister(&hdmi->hpd_switch);
1136 switch_dev_unregister(&hdmi->audio_switch);
1138 clk_put(hdmi->hda_clk);
1139 clk_put(hdmi->hda2codec_clk);
1140 clk_put(hdmi->hda2hdmi_clk);
1143 static void tegra_hdmi_config(struct tegra_hdmi *hdmi)
1145 struct tegra_dc_sor_data *sor = hdmi->sor;
1146 struct tegra_dc *dc = hdmi->dc;
1147 #ifndef CONFIG_TEGRA_NVDISPLAY
1148 u32 h_pulse_start, h_pulse_end;
1150 u32 hblank, max_ac, rekey;
1152 u32 dispclk_div_8_2;
1154 if (tegra_platform_is_linsim())
1157 tegra_sor_write_field(sor, NV_SOR_INPUT_CONTROL,
1158 NV_SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED |
1159 NV_SOR_INPUT_CONTROL_HDMI_SRC_SELECT_DISPLAYB,
1160 NV_SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_FULL |
1161 NV_SOR_INPUT_CONTROL_HDMI_SRC_SELECT_DISPLAYB);
1163 dispclk_div_8_2 = clk_get_rate(hdmi->sor->sor_clk) / 1000000 * 4;
1164 tegra_sor_writel(sor, NV_SOR_REFCLK,
1165 NV_SOR_REFCLK_DIV_INT(dispclk_div_8_2 >> 2) |
1166 NV_SOR_REFCLK_DIV_FRAC(dispclk_div_8_2));
1168 rekey = NV_SOR_HDMI_CTRL_REKEY_DEFAULT;
1169 hblank = dc->mode.h_sync_width + dc->mode.h_back_porch +
1170 dc->mode.h_front_porch;
1171 max_ac = (hblank - rekey - 18) / 32;
1174 val |= hdmi->dvi ? 0x0 : NV_SOR_HDMI_CTRL_ENABLE;
1175 /* The register wants "-2" of the required rekey val */
1176 val |= NV_SOR_HDMI_CTRL_REKEY(rekey - 2);
1177 val |= NV_SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac);
1178 val |= NV_SOR_HDMI_CTRL_AUDIO_LAYOUT_SELECT;
1179 tegra_sor_writel(sor, NV_SOR_HDMI_CTRL, val);
1181 #ifndef CONFIG_TEGRA_NVDISPLAY
1182 tegra_dc_writel(dc, 0x180, DC_DISP_H_PULSE2_CONTROL);
1183 h_pulse_start = dc->mode.h_ref_to_sync +
1184 dc->mode.h_sync_width +
1185 dc->mode.h_back_porch - 10;
1186 h_pulse_end = h_pulse_start + 8;
1187 tegra_dc_writel(dc, PULSE_START(h_pulse_start) | PULSE_END(h_pulse_end),
1188 DC_DISP_H_PULSE2_POSITION_A);
1190 val = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
1191 val |= H_PULSE_2_ENABLE;
1192 tegra_dc_writel(dc, val, DC_DISP_DISP_SIGNAL_OPTIONS0);
1196 static void tegra_hdmi_infoframe_pkt_write(struct tegra_hdmi *hdmi,
1197 u32 header_reg, u8 pkt_type,
1198 u8 pkt_vs, u8 pkt_len,
1200 u32 reg_payload_len)
1202 struct tegra_dc_sor_data *sor = hdmi->sor;
1204 u32 *data = reg_payload;
1205 u32 data_reg = header_reg + 1;
1207 val = NV_SOR_HDMI_INFOFRAME_HEADER_TYPE(pkt_type) |
1208 NV_SOR_HDMI_INFOFRAME_HEADER_VERSION(pkt_vs) |
1209 NV_SOR_HDMI_INFOFRAME_HEADER_LEN(pkt_len);
1210 tegra_sor_writel(sor, header_reg, val);
1212 for (val = 0; val < reg_payload_len; val += 4, data_reg++, data++)
1213 tegra_sor_writel(sor, data_reg, *data);
1216 static u32 tegra_hdmi_get_cea_modedb_size(struct tegra_hdmi *hdmi)
1218 if (!tegra_hdmi_is_connected(hdmi) || !hdmi->mon_spec_valid)
1221 return (hdmi->mon_spec.misc & FB_MISC_HDMI_FORUM) ?
1222 CEA_861_F_MODEDB_SIZE : CEA_861_D_MODEDB_SIZE;
1225 static void tegra_hdmi_get_cea_fb_videomode(struct fb_videomode *m,
1226 struct tegra_hdmi *hdmi)
1228 struct tegra_dc *dc = hdmi->dc;
1229 struct tegra_dc_mode dc_mode;
1232 memcpy(&dc_mode, &dc->mode, sizeof(dc->mode));
1234 /* get CEA video timings */
1235 yuv_flag = dc_mode.vmode & FB_VMODE_YUV_MASK;
1236 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y24) ||
1237 yuv_flag == (FB_VMODE_Y420_ONLY | FB_VMODE_Y24)) {
1238 dc_mode.h_back_porch *= 2;
1239 dc_mode.h_front_porch *= 2;
1240 dc_mode.h_sync_width *= 2;
1241 dc_mode.h_active *= 2;
1243 } else if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30)) {
1244 dc_mode.h_back_porch = (dc_mode.h_back_porch * 8) / 5;
1245 dc_mode.h_front_porch = (dc_mode.h_front_porch * 8) / 5;
1246 dc_mode.h_sync_width = (dc_mode.h_sync_width * 8) / 5;
1247 dc_mode.h_active = (dc_mode.h_active * 8) / 5;
1248 dc_mode.pclk = (dc_mode.pclk / 5) * 8;
1251 if (dc_mode.vmode & FB_VMODE_1000DIV1001) {
1252 dc_mode.pclk = DIV_ROUND_UP((u64)dc_mode.pclk * 1001, 1000);
1253 dc_mode.vmode &= ~FB_VMODE_1000DIV1001;
1256 tegra_dc_to_fb_videomode(m, &dc_mode);
1258 /* only interlaced required for VIC identification */
1259 m->vmode &= FB_VMODE_INTERLACED;
1263 static int tegra_hdmi_find_cea_vic(struct tegra_hdmi *hdmi)
1265 struct fb_videomode m;
1266 struct tegra_dc *dc = hdmi->dc;
1269 u32 modedb_size = tegra_hdmi_get_cea_modedb_size(hdmi);
1271 if (dc->initialized) {
1272 u32 vic = tegra_sor_readl(hdmi->sor,
1273 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_HIGH) & 0xff;
1275 dev_warn(&dc->ndev->dev, "hdmi: BL set VIC 0\n");
1279 tegra_hdmi_get_cea_fb_videomode(&m, hdmi);
1281 m.flag &= ~FB_FLAG_RATIO_MASK;
1282 m.flag |= tegra_dc_get_aspect_ratio(dc);
1284 for (i = 1; i < modedb_size; i++) {
1285 const struct fb_videomode *curr = &cea_modes[i];
1287 if (!((m.refresh == curr->refresh ||
1288 m.refresh + 1 == curr->refresh ||
1289 m.refresh == curr->refresh + 1) &&
1290 m.xres == curr->xres &&
1291 m.yres == curr->yres &&
1292 (m.pixclock == curr->pixclock ||
1293 (m.pixclock * 1001 / 1000) == curr->pixclock) &&
1294 m.hsync_len == curr->hsync_len &&
1295 m.vsync_len == curr->vsync_len &&
1296 m.left_margin == curr->left_margin &&
1297 m.right_margin == curr->right_margin &&
1298 m.upper_margin == curr->upper_margin &&
1299 m.lower_margin == curr->lower_margin &&
1300 m.sync == curr->sync &&
1301 m.vmode == curr->vmode))
1306 /* if either flag is set, then match is required */
1308 (FB_FLAG_RATIO_4_3 | FB_FLAG_RATIO_16_9 |
1309 FB_FLAG_RATIO_64_27 | FB_FLAG_RATIO_256_135)) {
1310 if (m.flag & curr->flag & FB_FLAG_RATIO_4_3)
1312 else if (m.flag & curr->flag & FB_FLAG_RATIO_16_9)
1314 else if (m.flag & curr->flag & FB_FLAG_RATIO_64_27)
1316 else if (m.flag & curr->flag & FB_FLAG_RATIO_256_135)
1325 static u32 tegra_hdmi_get_aspect_ratio(struct tegra_hdmi *hdmi)
1329 switch (hdmi->dc->mode.avi_m) {
1330 case TEGRA_DC_MODE_AVI_M_4_3:
1331 aspect_ratio = HDMI_AVI_ASPECT_RATIO_4_3;
1333 case TEGRA_DC_MODE_AVI_M_16_9:
1334 aspect_ratio = HDMI_AVI_ASPECT_RATIO_16_9;
1337 * no avi_m field for picture aspect ratio 64:27 and 256:135.
1338 * sink detects via VIC, avi_m is 0.
1340 case TEGRA_DC_MODE_AVI_M_64_27: /* fall through */
1341 case TEGRA_DC_MODE_AVI_M_256_135: /* fall through */
1343 aspect_ratio = HDMI_AVI_ASPECT_RATIO_NO_DATA;
1346 /* For seamless HDMI, read aspect ratio parameters from bootloader
1347 * set AVI Infoframe parameters
1349 if ((aspect_ratio == HDMI_AVI_ASPECT_RATIO_NO_DATA) &&
1350 (hdmi->dc->initialized)) {
1352 temp = tegra_sor_readl(hdmi->sor,
1353 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_LOW);
1354 temp = (temp >> 20) & 0x3;
1357 aspect_ratio = HDMI_AVI_ASPECT_RATIO_4_3;
1360 aspect_ratio = HDMI_AVI_ASPECT_RATIO_16_9;
1363 aspect_ratio = HDMI_AVI_ASPECT_RATIO_NO_DATA;
1366 return aspect_ratio;
1369 static u32 tegra_hdmi_get_rgb_ycc(struct tegra_hdmi *hdmi)
1371 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1374 * For seamless HDMI, read YUV flag parameters from bootloader
1375 * set AVI Infoframe parameters
1377 if (hdmi->dc->initialized) {
1379 temp = tegra_sor_readl(hdmi->sor,
1380 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_LOW);
1381 temp = (temp >> 12) & 0x3;
1384 return HDMI_AVI_RGB;
1385 case HDMI_AVI_YCC_420:
1386 return HDMI_AVI_YCC_420;
1388 dev_warn(&hdmi->dc->ndev->dev, "hdmi: BL didn't set RGB/YUV indicator flag\n");
1393 if (yuv_flag & (FB_VMODE_Y420 | FB_VMODE_Y420_ONLY))
1394 return HDMI_AVI_YCC_420;
1395 else if (yuv_flag & FB_VMODE_Y422)
1396 return HDMI_AVI_YCC_422;
1398 return HDMI_AVI_RGB;
1401 static bool tegra_hdmi_is_ex_colorimetry(struct tegra_hdmi *hdmi)
1403 return !!(hdmi->dc->mode.vmode & FB_VMODE_EC_ENABLE);
1406 static u32 tegra_hdmi_get_ex_colorimetry(struct tegra_hdmi *hdmi)
1408 u32 vmode = hdmi->dc->mode.vmode;
1410 return tegra_hdmi_is_ex_colorimetry(hdmi) ?
1411 ((vmode & FB_VMODE_EC_MASK) >> FB_VMODE_EC_SHIFT) :
1412 HDMI_AVI_EXT_COLORIMETRY_INVALID;
1415 static void tegra_hdmi_avi_infoframe_update(struct tegra_hdmi *hdmi)
1417 struct hdmi_avi_infoframe *avi = &hdmi->avi;
1419 memset(&hdmi->avi, 0, sizeof(hdmi->avi));
1421 if (tegra_platform_is_linsim())
1424 avi->scan = HDMI_AVI_UNDERSCAN;
1425 avi->bar_valid = HDMI_AVI_BAR_INVALID;
1426 avi->act_fmt_valid = HDMI_AVI_ACTIVE_FORMAT_VALID;
1427 avi->rgb_ycc = tegra_hdmi_get_rgb_ycc(hdmi);
1429 avi->act_format = HDMI_AVI_ACTIVE_FORMAT_SAME;
1430 avi->aspect_ratio = tegra_hdmi_get_aspect_ratio(hdmi);
1431 avi->colorimetry = tegra_hdmi_is_ex_colorimetry(hdmi) ?
1432 HDMI_AVI_COLORIMETRY_EXTENDED_VALID :
1433 HDMI_AVI_COLORIMETRY_DEFAULT;
1435 avi->scaling = HDMI_AVI_SCALING_UNKNOWN;
1436 avi->rgb_quant = HDMI_AVI_RGB_QUANT_DEFAULT;
1437 avi->ext_colorimetry = tegra_hdmi_get_ex_colorimetry(hdmi);
1438 avi->it_content = HDMI_AVI_IT_CONTENT_FALSE;
1440 /* set correct vic if video format is cea defined else set 0 */
1441 avi->video_format = tegra_hdmi_find_cea_vic(hdmi);
1443 avi->pix_rep = HDMI_AVI_NO_PIX_REPEAT;
1444 avi->it_content_type = HDMI_AVI_IT_CONTENT_NONE;
1445 avi->ycc_quant = HDMI_AVI_YCC_QUANT_NONE;
1447 avi->top_bar_end_line_low_byte = 0;
1448 avi->top_bar_end_line_high_byte = 0;
1450 avi->bot_bar_start_line_low_byte = 0;
1451 avi->bot_bar_start_line_high_byte = 0;
1453 avi->left_bar_end_pixel_low_byte = 0;
1454 avi->left_bar_end_pixel_high_byte = 0;
1456 avi->right_bar_start_pixel_low_byte = 0;
1457 avi->right_bar_start_pixel_high_byte = 0;
1460 static void tegra_hdmi_avi_infoframe(struct tegra_hdmi *hdmi)
1462 struct tegra_dc_sor_data *sor = hdmi->sor;
1467 if (tegra_platform_is_linsim())
1470 /* disable avi infoframe before configuring except for seamless case */
1471 if (!hdmi->dc->initialized)
1472 tegra_sor_writel(sor, NV_SOR_HDMI_AVI_INFOFRAME_CTRL, 0);
1474 tegra_hdmi_avi_infoframe_update(hdmi);
1476 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_AVI_INFOFRAME_HEADER,
1477 HDMI_INFOFRAME_TYPE_AVI,
1478 HDMI_INFOFRAME_VS_AVI,
1479 HDMI_INFOFRAME_LEN_AVI,
1480 &hdmi->avi, sizeof(hdmi->avi));
1482 /* Send infoframe every frame, checksum hw generated */
1483 tegra_sor_writel(sor, NV_SOR_HDMI_AVI_INFOFRAME_CTRL,
1484 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_ENABLE_YES |
1485 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_OTHER_DISABLE |
1486 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_SINGLE_DISABLE |
1487 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_CHECKSUM_ENABLE);
1490 static int tegra_hdmi_get_extended_vic(const struct tegra_dc_mode *mode)
1492 struct fb_videomode m;
1495 tegra_dc_to_fb_videomode(&m, mode);
1497 for (i = 1; i < HDMI_EXT_MODEDB_SIZE; i++) {
1498 const struct fb_videomode *curr = &hdmi_ext_modes[i];
1500 if (fb_mode_is_equal(&m, curr))
1506 static void tegra_hdmi_vendor_infoframe_update(struct tegra_hdmi *hdmi)
1508 struct hdmi_vendor_infoframe *vsi = &hdmi->vsi;
1511 memset(&hdmi->vsi, 0, sizeof(hdmi->vsi));
1513 vsi->oui = HDMI_LICENSING_LLC_OUI;
1515 extended_vic = tegra_hdmi_get_extended_vic(&hdmi->dc->mode);
1518 HDMI_VENDOR_VIDEO_FORMAT_EXTENDED;
1519 vsi->extended_vic = extended_vic;
1523 static void tegra_hdmi_vendor_infoframe(struct tegra_hdmi *hdmi)
1525 /* hdmi licensing, LLC vsi playload len as per hdmi1.4b */
1526 #define HDMI_INFOFRAME_LEN_VENDOR_LLC (6)
1528 struct tegra_dc_sor_data *sor = hdmi->sor;
1533 /* disable vsi infoframe before configuring */
1534 tegra_sor_writel(sor, NV_SOR_HDMI_VSI_INFOFRAME_CTRL, 0);
1536 tegra_hdmi_vendor_infoframe_update(hdmi);
1538 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_VSI_INFOFRAME_HEADER,
1539 HDMI_INFOFRAME_TYPE_VENDOR,
1540 HDMI_INFOFRAME_VS_VENDOR,
1541 HDMI_INFOFRAME_LEN_VENDOR_LLC,
1542 &hdmi->vsi, sizeof(hdmi->vsi));
1544 /* Send infoframe every frame, checksum hw generated */
1545 tegra_sor_writel(sor, NV_SOR_HDMI_VSI_INFOFRAME_CTRL,
1546 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_ENABLE_YES |
1547 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_OTHER_DISABLE |
1548 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_SINGLE_DISABLE |
1549 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_CHECKSUM_ENABLE);
1551 #undef HDMI_INFOFRAME_LEN_VENDOR_LLC
1554 static void tegra_hdmi_audio_infoframe_update(struct tegra_hdmi *hdmi)
1556 hdmi->audio.channel_cnt = HDMI_AUDIO_CHANNEL_CNT_2;
1559 static void tegra_hdmi_audio_infoframe(struct tegra_hdmi *hdmi)
1561 struct tegra_dc_sor_data *sor = hdmi->sor;
1566 /* disable audio infoframe before configuring */
1567 tegra_sor_writel(sor, NV_SOR_HDMI_AUDIO_INFOFRAME_CTRL, 0);
1569 tegra_hdmi_audio_infoframe_update(hdmi);
1571 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_AUDIO_INFOFRAME_HEADER,
1572 HDMI_INFOFRAME_TYPE_AUDIO,
1573 HDMI_INFOFRAME_VS_AUDIO,
1574 HDMI_INFOFRAME_LEN_AUDIO,
1575 &hdmi->audio, sizeof(hdmi->audio));
1577 /* Send infoframe every frame, checksum hw generated */
1578 tegra_sor_writel(sor, NV_SOR_HDMI_AUDIO_INFOFRAME_CTRL,
1579 NV_SOR_HDMI_AUDIO_INFOFRAME_CTRL_ENABLE_YES |
1580 NV_SOR_HDMI_AUDIO_INFOFRAME_CTRL_OTHER_DISABLE |
1581 NV_SOR_HDMI_AUDIO_INFOFRAME_CTRL_SINGLE_DISABLE |
1582 NV_SOR_HDMI_AUDIO_INFOFRAME_CTRL_CHECKSUM_ENABLE);
1585 /* HW generated CTS and N */
1586 static void tegra_hdmi_audio_acr(struct tegra_hdmi *hdmi, u32 audio_freq)
1588 #define GET_AVAL(n, fs_hz) ((24000 * n) / (128 * fs_hz / 1000))
1590 struct tegra_dc_sor_data *sor = hdmi->sor;
1593 tegra_sor_writel(sor, NV_SOR_HDMI_ACR_CTRL, 0x0);
1595 val = NV_SOR_HDMI_SPARE_HW_CTS_ENABLE |
1596 NV_SOR_HDMI_SPARE_CTS_RESET_VAL(1) |
1597 NV_SOR_HDMI_SPARE_ACR_PRIORITY_HIGH;
1598 tegra_sor_writel(sor, NV_SOR_HDMI_SPARE, val);
1600 tegra_sor_writel(sor, NV_SOR_HDMI_ACR_0441_SUBPACK_LOW,
1601 NV_SOR_HDMI_ACR_SUBPACK_USE_HW_CTS);
1602 tegra_sor_writel(sor, NV_SOR_HDMI_ACR_0441_SUBPACK_HIGH,
1603 NV_SOR_HDMI_ACR_SUBPACK_ENABLE);
1605 val = NV_SOR_HDMI_AUDIO_N_RESET_ASSERT |
1606 NV_SOR_HDMI_AUDIO_N_LOOKUP_ENABLE;
1607 tegra_sor_writel(sor, NV_SOR_HDMI_AUDIO_N, val);
1609 /* N from table 7.1, 7.2, 7.3 hdmi spec v1.4 */
1610 tegra_sor_writel(sor, NV_SOR_AUDIO_NVAL_0320, 4096);
1611 tegra_sor_writel(sor, NV_SOR_AUDIO_AVAL_0320,
1612 GET_AVAL(4096, audio_freq));
1613 tegra_sor_writel(sor, NV_SOR_AUDIO_NVAL_0441, 6272);
1614 tegra_sor_writel(sor, NV_SOR_AUDIO_AVAL_0441,
1615 GET_AVAL(6272, audio_freq));
1616 tegra_sor_writel(sor, NV_SOR_AUDIO_NVAL_0882, 12544);
1617 tegra_sor_writel(sor, NV_SOR_AUDIO_AVAL_0882,
1618 GET_AVAL(12544, audio_freq));
1619 tegra_sor_writel(sor, NV_SOR_AUDIO_NVAL_1764, 25088);
1620 tegra_sor_writel(sor, NV_SOR_AUDIO_AVAL_1764,
1621 GET_AVAL(25088, audio_freq));
1622 tegra_sor_writel(sor, NV_SOR_AUDIO_NVAL_0480, 6144);
1623 tegra_sor_writel(sor, NV_SOR_AUDIO_AVAL_0480,
1624 GET_AVAL(6144, audio_freq));
1625 tegra_sor_writel(sor, NV_SOR_AUDIO_NVAL_0960, 12288);
1626 tegra_sor_writel(sor, NV_SOR_AUDIO_AVAL_0960,
1627 GET_AVAL(12288, audio_freq));
1628 tegra_sor_writel(sor, NV_SOR_AUDIO_NVAL_1920, 24576);
1629 tegra_sor_writel(sor, NV_SOR_AUDIO_AVAL_1920,
1630 GET_AVAL(24576, audio_freq));
1632 tegra_sor_write_field(sor, NV_SOR_HDMI_AUDIO_N,
1633 NV_SOR_HDMI_AUDIO_N_RESET_ASSERT,
1634 NV_SOR_HDMI_AUDIO_N_RESET_DEASSERT);
1638 static void tegra_hdmi_audio_config(struct tegra_hdmi *hdmi,
1642 struct tegra_dc_sor_data *sor = hdmi->sor;
1648 /* hda is the only audio source */
1649 val = NV_SOR_AUDIO_CTRL_AFIFO_FLUSH |
1650 NV_SOR_AUDIO_CTRL_SRC_HDA;
1651 if (hdmi->null_sample_inject)
1652 val |= NV_SOR_AUDIO_CTRL_NULL_SAMPLE_EN;
1653 tegra_sor_writel(sor, NV_SOR_AUDIO_CTRL, val);
1655 /* override to advertise HBR capability */
1656 tegra_sor_writel(sor, NV_PDISP_SOR_AUDIO_SPARE0_0,
1657 (1 << HDMI_AUDIO_HBR_ENABLE_SHIFT) |
1658 tegra_sor_readl(sor, NV_PDISP_SOR_AUDIO_SPARE0_0));
1660 tegra_hdmi_audio_acr(hdmi, audio_freq);
1661 tegra_hdmi_audio_infoframe(hdmi);
1664 int tegra_hdmi_setup_audio_freq_source(unsigned audio_freq,
1665 unsigned audio_source)
1667 struct tegra_hdmi *hdmi = dc_hdmi;
1668 struct tegra_dc *dc;
1679 valid_freq = AUDIO_FREQ_32K == audio_freq ||
1680 AUDIO_FREQ_44_1K == audio_freq ||
1681 AUDIO_FREQ_48K == audio_freq ||
1682 AUDIO_FREQ_88_2K == audio_freq ||
1683 AUDIO_FREQ_96K == audio_freq ||
1684 AUDIO_FREQ_176_4K == audio_freq ||
1685 AUDIO_FREQ_192K == audio_freq;
1687 tegra_dc_io_start(dc);
1690 tegra_hdmi_audio_config(hdmi, audio_freq, audio_source);
1691 hdmi->audio_freq = audio_freq;
1694 tegra_dc_io_end(dc);
1700 EXPORT_SYMBOL(tegra_hdmi_setup_audio_freq_source);
1702 int tegra_hdmi_audio_null_sample_inject(bool on)
1704 struct tegra_hdmi *hdmi = dc_hdmi;
1712 if (on && !hdmi->null_sample_inject)
1713 tegra_sor_write_field(hdmi->sor,
1715 NV_SOR_AUDIO_CTRL_NULL_SAMPLE_EN,
1716 NV_SOR_AUDIO_CTRL_NULL_SAMPLE_EN);
1717 else if (!on && hdmi->null_sample_inject)
1718 tegra_sor_write_field(hdmi->sor,
1720 NV_SOR_AUDIO_CTRL_NULL_SAMPLE_EN,
1721 NV_SOR_AUDIO_CTRL_NULL_SAMPLE_DIS);
1723 hdmi->null_sample_inject = on;
1727 EXPORT_SYMBOL(tegra_hdmi_audio_null_sample_inject);
1730 static int tegra_hdmi_scdc_read(struct tegra_hdmi *hdmi,
1731 u8 offset_data[][2], u32 n_entries)
1734 struct i2c_msg msg[] = {
1748 _tegra_hdmi_ddc_enable(hdmi);
1750 for (i = 0; i < n_entries; i++) {
1751 msg[0].buf = offset_data[i];
1752 msg[1].buf = &offset_data[i][1];
1753 tegra_hdmi_scdc_i2c_xfer(hdmi->dc, msg, ARRAY_SIZE(msg));
1756 _tegra_hdmi_ddc_disable(hdmi);
1761 static int tegra_hdmi_scdc_write(struct tegra_hdmi *hdmi,
1762 u8 offset_data[][2], u32 n_entries)
1765 struct i2c_msg msg[] = {
1773 _tegra_hdmi_ddc_enable(hdmi);
1775 for (i = 0; i < n_entries; i++) {
1776 msg[0].buf = offset_data[i];
1777 tegra_hdmi_scdc_i2c_xfer(hdmi->dc, msg, ARRAY_SIZE(msg));
1780 _tegra_hdmi_ddc_disable(hdmi);
1785 static int tegra_hdmi_v2_x_mon_config(struct tegra_hdmi *hdmi, bool enable)
1787 u8 tmds_config_en[][2] = {
1789 HDMI_SCDC_TMDS_CONFIG_OFFSET,
1790 (HDMI_SCDC_TMDS_CONFIG_BIT_CLK_RATIO_40 |
1791 HDMI_SCDC_TMDS_CONFIG_SCRAMBLING_EN)
1794 u8 tmds_config_dis[][2] = {
1796 HDMI_SCDC_TMDS_CONFIG_OFFSET,
1801 if (hdmi->dc->vedid)
1804 tegra_hdmi_scdc_write(hdmi,
1805 enable ? tmds_config_en : tmds_config_dis,
1806 ARRAY_SIZE(tmds_config_en));
1812 static void tegra_hdmi_v2_x_host_config(struct tegra_hdmi *hdmi, bool enable)
1814 u32 val = NV_SOR_HDMI2_CTRL_SCRAMBLE_ENABLE |
1815 NV_SOR_HDMI2_CTRL_CLK_MODE_DIV_BY_4;
1817 tegra_sor_write_field(hdmi->sor, NV_SOR_HDMI2_CTRL,
1818 NV_SOR_HDMI2_CTRL_SCRAMBLE_ENABLE |
1819 NV_SOR_HDMI2_CTRL_CLK_MODE_DIV_BY_4,
1823 static int _tegra_hdmi_v2_x_config(struct tegra_hdmi *hdmi)
1825 #define SCDC_STABILIZATION_DELAY_MS (20)
1827 /* disable hdmi2.x config on host and monitor only
1828 * if bootloader didn't initialize hdmi
1830 if (!hdmi->dc->initialized) {
1831 tegra_hdmi_v2_x_mon_config(hdmi, false);
1832 tegra_hdmi_v2_x_host_config(hdmi, false);
1835 /* enable hdmi2.x config on host and monitor */
1836 tegra_hdmi_v2_x_mon_config(hdmi, true);
1837 msleep(SCDC_STABILIZATION_DELAY_MS);
1839 tegra_hdmi_v2_x_host_config(hdmi, true);
1842 #undef SCDC_STABILIZATION_DELAY_MS
1845 static int tegra_hdmi_v2_x_config(struct tegra_hdmi *hdmi)
1847 _tegra_hdmi_v2_x_config(hdmi);
1852 static void tegra_hdmi_scdc_worker(struct work_struct *work)
1854 struct tegra_hdmi *hdmi = container_of(to_delayed_work(work),
1855 struct tegra_hdmi, scdc_work);
1856 u8 rd_status_flags[][2] = {
1857 {HDMI_SCDC_STATUS_FLAGS, 0x0}
1860 if (!hdmi->enabled || hdmi->dc->mode.pclk <= 340000000)
1863 if (hdmi->dc->vedid)
1866 if (!tegra_edid_is_scdc_present(hdmi->dc->edid))
1869 tegra_hdmi_scdc_read(hdmi, rd_status_flags,
1870 ARRAY_SIZE(rd_status_flags));
1871 if (!rd_status_flags[0][1] && (hdmi->dc->mode.pclk > 340000000)) {
1872 dev_info(&hdmi->dc->ndev->dev, "hdmi: scdc scrambling status is reset, "
1873 "trying to reconfigure.\n");
1874 _tegra_hdmi_v2_x_config(hdmi);
1878 /* reschedule the worker */
1879 cancel_delayed_work(&hdmi->scdc_work);
1880 schedule_delayed_work(&hdmi->scdc_work,
1881 msecs_to_jiffies(HDMI_SCDC_MONITOR_TIMEOUT_MS));
1884 static void _tegra_hdmi_clock_enable(struct tegra_hdmi *hdmi)
1886 struct tegra_dc_sor_data *sor = hdmi->sor;
1887 clk_prepare_enable(sor->safe_clk);
1888 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_SAFE_CLK);
1889 tegra_sor_clk_enable(sor);
1892 static void _tegra_hdmi_clock_disable(struct tegra_hdmi *hdmi)
1894 struct tegra_dc_sor_data *sor = hdmi->sor;
1895 tegra_sor_clk_disable(sor);
1898 static void tegra_hdmi_get(struct tegra_dc *dc)
1900 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1902 if (atomic_inc_return(&hdmi->clock_refcount) == 1)
1903 _tegra_hdmi_clock_enable(hdmi);
1906 static void tegra_hdmi_put(struct tegra_dc *dc)
1908 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1910 if (WARN_ONCE(atomic_read(&hdmi->clock_refcount) <= 0,
1911 "hdmi: clock refcount imbalance"))
1913 if (atomic_dec_return(&hdmi->clock_refcount) == 0)
1914 _tegra_hdmi_clock_disable(hdmi);
1917 /* TODO: add support for other deep colors */
1918 static inline u32 tegra_hdmi_get_bpp(struct tegra_hdmi *hdmi)
1920 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1922 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30))
1928 static u32 tegra_hdmi_gcp_color_depth(struct tegra_hdmi *hdmi)
1932 switch (tegra_hdmi_get_bpp(hdmi)) {
1933 case 0: /* fall through */
1935 gcp_cd = TEGRA_HDMI_BPP_UNKNOWN;
1938 gcp_cd = TEGRA_HDMI_BPP_30;
1941 gcp_cd = TEGRA_HDMI_BPP_36;
1944 gcp_cd = TEGRA_HDMI_BPP_48;
1947 dev_WARN(&hdmi->dc->ndev->dev,
1948 "hdmi: unknown gcp color depth\n");
1954 /* return packing phase of last pixel in preceding video data period */
1955 static u32 tegra_hdmi_gcp_packing_phase(struct tegra_hdmi *hdmi)
1957 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1959 if (!tegra_hdmi_gcp_color_depth(hdmi))
1962 /* 10P4 for yuv420 10bpc */
1963 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30))
1969 static bool tegra_hdmi_gcp_default_phase_en(struct tegra_hdmi *hdmi)
1971 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1973 if (!tegra_hdmi_gcp_color_depth(hdmi))
1976 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30))
1982 /* general control packet */
1983 static void tegra_hdmi_gcp(struct tegra_hdmi *hdmi)
1985 #define GCP_SB1_PP_SHIFT 4
1987 struct tegra_dc_sor_data *sor = hdmi->sor;
1990 /* disable gcp before configuring */
1991 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_CTRL, 0);
1993 sb1 = tegra_hdmi_gcp_packing_phase(hdmi) << GCP_SB1_PP_SHIFT |
1994 tegra_hdmi_gcp_color_depth(hdmi);
1995 sb2 = !!tegra_hdmi_gcp_default_phase_en(hdmi);
1996 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_SUBPACK(0),
1997 sb1 << NV_SOR_HDMI_GCP_SUBPACK_SB1_SHIFT |
1998 sb2 << NV_SOR_HDMI_GCP_SUBPACK_SB2_SHIFT);
2000 /* Send gcp every frame */
2001 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_CTRL,
2002 NV_SOR_HDMI_GCP_CTRL_ENABLE |
2003 NV_SOR_HDMI_GCP_CTRL_OTHER_DIS |
2004 NV_SOR_HDMI_GCP_CTRL_SINGLE_DIS);
2006 #undef GCP_SB1_PP_SHIFT
2009 static int tegra_hdmi_controller_enable(struct tegra_hdmi *hdmi)
2011 struct tegra_dc *dc = hdmi->dc;
2012 struct tegra_dc_sor_data *sor = hdmi->sor;
2017 tegra_hdmi_hda_clk_enable(hdmi);
2019 tegra_sor_hdmi_pad_power_up(sor);
2021 tegra_sor_power_lanes(sor, 4, true);
2023 tegra_dc_sor_set_internal_panel(sor, false);
2024 tegra_hdmi_config(hdmi);
2025 tegra_hdmi_avi_infoframe(hdmi);
2026 tegra_hdmi_vendor_infoframe(hdmi);
2027 tegra_hdmi_audio_config(hdmi, AUDIO_FREQ_32K, HDA);
2029 tegra_sor_pad_cal_power(sor, true);
2030 tegra_hdmi_config_tmds(hdmi);
2031 tegra_sor_pad_cal_power(sor, false);
2033 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_BRICK_CLK);
2034 tegra_dc_sor_attach(sor);
2036 if (!G_RECOVERY_HACK)
2037 tegra_nvhdcp_set_plug(hdmi->nvhdcp, true);
2039 tegra_dc_setup_clk(dc, dc->clk);
2040 tegra_dc_hdmi_setup_clk(dc, hdmi->sor->sor_clk);
2041 tegra_hdmi_config(hdmi);
2043 tegra_sor_config_xbar(hdmi->sor);
2045 /* TODO: Confirm sequence with HW */
2046 tegra_sor_writel(sor, NV_SOR_SEQ_INST(0), 0x8080);
2047 tegra_sor_writel(sor, NV_SOR_PWR, 0x80000001);
2049 if (hdmi->dc->mode.pclk > 340000000) {
2050 tegra_hdmi_v2_x_config(hdmi);
2051 schedule_delayed_work(&hdmi->scdc_work,
2052 msecs_to_jiffies(HDMI_SCDC_MONITOR_TIMEOUT_MS));
2055 tegra_hdmi_gcp(hdmi);
2061 static void tegra_dc_hdmi_enable(struct tegra_dc *dc)
2063 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2068 tegra_hdmi_controller_enable(hdmi);
2070 hdmi->enabled = true;
2072 tegra_hdmi_setup_hda_presence();
2074 #ifdef CONFIG_SWITCH
2076 switch_set_state(&hdmi->audio_switch, 1);
2080 static inline u32 tegra_hdmi_get_shift_clk_div(struct tegra_hdmi *hdmi)
2083 * HW does not support deep color yet
2090 static void tegra_hdmi_config_clk(struct tegra_hdmi *hdmi, u32 clk_type)
2092 if (clk_type == hdmi->clk_type)
2095 if (tegra_platform_is_linsim())
2098 if (clk_type == TEGRA_HDMI_BRICK_CLK) {
2100 struct tegra_dc_sor_data *sor = hdmi->sor;
2101 int div = hdmi->dc->mode.pclk < 340000000 ? 1 : 2;
2102 unsigned long rate = clk_get_rate(sor->src_switch_clk);
2103 unsigned long parent_rate =
2104 clk_get_rate(clk_get_parent(sor->src_switch_clk));
2106 /* Set sor divider */
2107 if (rate != parent_rate / div) {
2108 rate = parent_rate / div;
2109 clk_set_rate(sor->src_switch_clk, rate);
2112 /* Select brick muxes */
2113 val = (hdmi->dc->mode.pclk < 340000000) ?
2114 NV_SOR_CLK_CNTRL_DP_LINK_SPEED_G2_7 :
2115 NV_SOR_CLK_CNTRL_DP_LINK_SPEED_G5_4;
2117 val |= NV_SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2118 tegra_sor_writel(hdmi->sor, NV_SOR_CLK_CNTRL, val);
2119 usleep_range(250, 300); /* sor brick pll stabilization delay */
2122 * Report brick configuration and rate, so that SOR clock tree
2123 * is properly updated. No h/w changes by clock api calls below,
2124 * just sync s/w state with brick h/w.
2126 rate = rate/NV_SOR_HDMI_BRICK_DIV*NV_SOR_HDMI_BRICK_MUL(val);
2127 if (clk_get_parent(sor->brick_clk) != sor->src_switch_clk)
2128 clk_set_parent(sor->brick_clk, sor->src_switch_clk);
2129 clk_set_rate(sor->brick_clk, rate);
2132 * Select primary -- HDMI -- DVFS table for SOR clock (if SOR
2133 * clock has single DVFS table for all modes, nothing changes).
2135 tegra_dvfs_use_alt_freqs_on_clk(sor->sor_clk, false);
2137 /* Select sor clock muxes */
2138 tegra_clk_cfg_ex(sor->sor_clk, TEGRA_CLK_SOR_CLK_SEL, 3);
2140 tegra_dc_writel(hdmi->dc, PIXEL_CLK_DIVIDER_PCD1 |
2141 SHIFT_CLK_DIVIDER(tegra_hdmi_get_shift_clk_div(hdmi)),
2142 DC_DISP_DISP_CLOCK_CONTROL);
2144 hdmi->clk_type = TEGRA_HDMI_BRICK_CLK;
2145 } else if (clk_type == TEGRA_HDMI_SAFE_CLK) {
2146 if (!hdmi->dc->initialized) {
2147 /* Select sor clock muxes */
2148 tegra_clk_cfg_ex(hdmi->sor->sor_clk,
2149 TEGRA_CLK_SOR_CLK_SEL, 0);
2150 hdmi->clk_type = TEGRA_HDMI_SAFE_CLK;
2153 dev_err(&hdmi->dc->ndev->dev, "hdmi: incorrect clk type configured\n");
2157 /* returns exact pixel clock in Hz */
2158 static long tegra_hdmi_get_pclk(struct tegra_dc_mode *mode)
2160 long h_total, v_total;
2162 h_total = mode->h_active + mode->h_front_porch + mode->h_back_porch +
2164 v_total = mode->v_active + mode->v_front_porch + mode->v_back_porch +
2166 refresh = tegra_dc_calc_refresh(mode);
2167 refresh = DIV_ROUND_CLOSEST(refresh, 1000);
2169 pclk = h_total * v_total * refresh;
2171 if (mode->vmode & FB_VMODE_1000DIV1001)
2172 pclk = pclk * 1000 / 1001;
2177 static long tegra_dc_hdmi_setup_clk(struct tegra_dc *dc, struct clk *clk)
2179 struct clk *parent_clk = clk_get(NULL,
2180 dc->out->parent_clk ? : "pll_d2");
2182 dc->mode.pclk = tegra_hdmi_get_pclk(&dc->mode);
2184 if (IS_ERR_OR_NULL(parent_clk)) {
2185 dev_err(&dc->ndev->dev, "hdmi: parent clk get failed\n");
2189 #ifdef CONFIG_TEGRA_NVDISPLAY
2190 if (clk_get_parent(clk) != parent_clk)
2191 clk_set_parent(clk, parent_clk);
2193 if (clk == dc->clk) {
2194 if (clk_get_parent(clk) != parent_clk) {
2195 if (clk_set_parent(clk, parent_clk)) {
2196 dev_err(&dc->ndev->dev,
2197 "hdmi: set dc parent failed\n");
2202 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2203 struct tegra_dc_sor_data *sor = hdmi->sor;
2205 if (clk_get_parent(sor->src_switch_clk) != parent_clk) {
2206 if (clk_set_parent(sor->src_switch_clk, parent_clk)) {
2207 dev_err(&dc->ndev->dev,
2208 "hdmi: set src switch parent failed\n");
2214 if (dc->initialized)
2216 if (clk_get_rate(parent_clk) != dc->mode.pclk)
2217 clk_set_rate(parent_clk, dc->mode.pclk);
2220 * DC clock divider is controlled by DC driver transparently to clock
2221 * framework -- hence, direct call to DVFS with target mode rate. SOR
2222 * clock rate in clock tree is properly updated, and can be used for
2225 * TODO: tegra_hdmi_controller_enable() procedure 1st configures SOR
2226 * clock via tegra_hdmi_config_clk(), and then calls this function
2227 * that may re-lock parent PLL. That needs to be double-checked:
2228 * in general re-locking PLL while the downstream module is already
2229 * sourced from it is not recommended. If/when the order of enabling
2230 * HDMI controller is changed, we can remove direct DVFS call for SOR
2231 * (but for DC it should be kept, anyway).
2234 tegra_dvfs_set_rate(clk, dc->mode.pclk);
2236 tegra_dvfs_set_rate(clk, clk_get_rate(clk));
2238 return tegra_dc_pclk_round_rate(dc, dc->mode.pclk);
2241 static void tegra_dc_hdmi_shutdown(struct tegra_dc *dc)
2243 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2245 hdmi->device_shutdown = true;
2246 tegra_nvhdcp_shutdown(hdmi->nvhdcp);
2251 static void tegra_dc_hdmi_disable(struct tegra_dc *dc)
2253 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2255 hdmi->enabled = false;
2256 #ifdef CONFIG_SWITCH
2257 switch_set_state(&hdmi->audio_switch, 0);
2260 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_SAFE_CLK);
2261 tegra_hdmi_controller_disable(hdmi);
2265 static bool tegra_dc_hdmi_detect(struct tegra_dc *dc)
2267 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2268 unsigned long delay = msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS);
2270 if (tegra_platform_is_linsim())
2273 if (dc->out->hotplug_state != TEGRA_HPD_STATE_NORMAL)
2276 cancel_delayed_work(&hdmi->hpd_worker);
2277 schedule_delayed_work(&hdmi->hpd_worker, delay);
2279 return tegra_dc_hpd(dc);
2282 static void tegra_dc_hdmi_suspend(struct tegra_dc *dc)
2284 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2286 if (dc->out->flags & TEGRA_DC_OUT_HOTPLUG_WAKE_LP0) {
2287 int wake_irq = gpio_to_irq(dc->out->hotplug_gpio);
2290 ret = enable_irq_wake(wake_irq);
2292 dev_err(&dc->ndev->dev,
2293 "%s: Couldn't enable HDMI wakeup, irq=%d, error=%d\n",
2294 __func__, wake_irq, ret);
2298 atomic_set(&hdmi->suspended, 1);
2301 static void tegra_dc_hdmi_resume(struct tegra_dc *dc)
2303 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2305 atomic_set(&hdmi->suspended, 0);
2307 if (dc->out->flags & TEGRA_DC_OUT_HOTPLUG_WAKE_LP0)
2308 disable_irq_wake(gpio_to_irq(dc->out->hotplug_gpio));
2310 cancel_delayed_work(&hdmi->hpd_worker);
2311 schedule_delayed_work(&hdmi->hpd_worker,
2312 msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS + HDMI_HPD_DROP_TIMEOUT_MS));
2315 static int tegra_dc_hdmi_ddc_enable(struct tegra_dc *dc)
2317 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2318 _tegra_hdmi_ddc_enable(hdmi);
2322 static int tegra_dc_hdmi_ddc_disable(struct tegra_dc *dc)
2324 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2325 _tegra_hdmi_ddc_disable(hdmi);
2329 static void tegra_dc_hdmi_modeset_notifier(struct tegra_dc *dc)
2331 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2334 tegra_dc_io_start(dc);
2336 /* disable hdmi2.x config on host and monitor */
2337 if (dc->mode.pclk > 340000000) {
2338 if (tegra_edid_is_scdc_present(dc->edid))
2339 tegra_hdmi_v2_x_mon_config(hdmi, true);
2340 tegra_hdmi_v2_x_host_config(hdmi, true);
2342 if (tegra_edid_is_scdc_present(dc->edid))
2343 tegra_hdmi_v2_x_mon_config(hdmi, false);
2344 tegra_hdmi_v2_x_host_config(hdmi, false);
2347 tegra_dc_io_end(dc);
2351 #ifdef CONFIG_DEBUG_FS
2352 /* show current hpd state */
2353 static int tegra_hdmi_hotplug_dbg_show(struct seq_file *m, void *unused)
2355 struct tegra_hdmi *hdmi = m->private;
2356 struct tegra_dc *dc = hdmi->dc;
2358 if (WARN_ON(!hdmi || !dc || !dc->out))
2361 seq_printf(m, "hdmi hpd state: %d\n", dc->out->hotplug_state);
2367 * sw control for hpd.
2368 * 0 is normal state, hw drives hpd.
2369 * -1 is force deassert, sw drives hpd.
2370 * 1 is force assert, sw drives hpd.
2371 * before releasing to hw, sw must ensure hpd state is normal i.e. 0
2373 static ssize_t tegra_hdmi_hotplug_dbg_write(struct file *file,
2374 const char __user *addr,
2375 size_t len, loff_t *pos)
2377 struct seq_file *m = file->private_data;
2378 struct tegra_hdmi *hdmi = m->private;
2379 struct tegra_dc *dc = hdmi->dc;
2383 if (WARN_ON(!hdmi || !dc || !dc->out))
2386 ret = kstrtol_from_user(addr, len, 10, &new_hpd_state);
2390 if (dc->out->hotplug_state == TEGRA_HPD_STATE_NORMAL &&
2391 new_hpd_state != TEGRA_HPD_STATE_NORMAL &&
2392 tegra_dc_hotplug_supported(dc)) {
2393 disable_irq(gpio_to_irq(dc->out->hotplug_gpio));
2394 } else if (dc->out->hotplug_state != TEGRA_HPD_STATE_NORMAL &&
2395 new_hpd_state == TEGRA_HPD_STATE_NORMAL &&
2396 tegra_dc_hotplug_supported(dc)) {
2397 enable_irq(gpio_to_irq(dc->out->hotplug_gpio));
2400 dc->out->hotplug_state = new_hpd_state;
2403 * sw controlled plug/unplug.
2404 * wait for any already executing hpd worker thread.
2405 * No debounce delay, schedule immedately
2407 cancel_delayed_work_sync(&hdmi->hpd_worker);
2408 schedule_delayed_work(&hdmi->hpd_worker, 0);
2413 static int tegra_hdmi_hotplug_dbg_open(struct inode *inode, struct file *file)
2415 return single_open(file, tegra_hdmi_hotplug_dbg_show, inode->i_private);
2418 static const struct file_operations tegra_hdmi_hotplug_dbg_ops = {
2419 .open = tegra_hdmi_hotplug_dbg_open,
2421 .write = tegra_hdmi_hotplug_dbg_write,
2422 .llseek = seq_lseek,
2423 .release = single_release,
2426 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi)
2428 struct dentry *dir, *ret;
2430 dir = debugfs_create_dir("tegra_hdmi", NULL);
2431 if (IS_ERR_OR_NULL(dir))
2434 ret = debugfs_create_file("hotplug", S_IRUGO, dir,
2435 hdmi, &tegra_hdmi_hotplug_dbg_ops);
2436 if (IS_ERR_OR_NULL(ret))
2441 debugfs_remove_recursive(dir);
2445 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi)
2451 static ssize_t hdmi_ddc_power_toggle(struct kobject *kobj,
2452 struct kobj_attribute *attr, const char *buf, size_t count)
2455 sscanf(buf, "%du", &value);
2456 tegra_hdmi_ddc_power_toggle(value);
2460 static ssize_t hdmi_ddc_power_show(struct kobject *kobj,
2461 struct kobj_attribute *attr, char *buf)
2463 return sprintf(buf, "%d\n", dc_hdmi->ddc_refcount);
2466 static bool tegra_dc_hdmi_hpd_state(struct tegra_dc *dc)
2472 if (WARN_ON(!dc || !dc->out))
2475 level = gpio_get_value_cansleep(dc->out->hotplug_gpio);
2477 sense = dc->out->flags & TEGRA_DC_OUT_HOTPLUG_MASK;
2479 hpd = (sense == TEGRA_DC_OUT_HOTPLUG_HIGH && level) ||
2480 (sense == TEGRA_DC_OUT_HOTPLUG_LOW && !level);
2485 struct tegra_dc_out_ops tegra_dc_hdmi2_0_ops = {
2486 .init = tegra_dc_hdmi_init,
2487 .destroy = tegra_dc_hdmi_destroy,
2488 .enable = tegra_dc_hdmi_enable,
2489 .disable = tegra_dc_hdmi_disable,
2490 .setup_clk = tegra_dc_hdmi_setup_clk,
2491 .detect = tegra_dc_hdmi_detect,
2492 .shutdown = tegra_dc_hdmi_shutdown,
2493 .suspend = tegra_dc_hdmi_suspend,
2494 .resume = tegra_dc_hdmi_resume,
2495 .ddc_enable = tegra_dc_hdmi_ddc_enable,
2496 .ddc_disable = tegra_dc_hdmi_ddc_disable,
2497 .modeset_notifier = tegra_dc_hdmi_modeset_notifier,
2498 .mode_filter = tegra_hdmi_fb_mode_filter,
2499 .hpd_state = tegra_dc_hdmi_hpd_state,