2 * drivers/video/tegra/dc/hdmi2.0.c
4 * Copyright (c) 2014-2016, 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>
34 #include <linux/tegra_pm_domains.h>
38 #include <mach/powergate.h>
48 #ifdef CONFIG_ADF_TEGRA
49 #include "tegra_adf.h"
54 #include <linux/tegra_prod.h>
55 #include "../../../../arch/arm/mach-tegra/iomap.h"
57 #define TMDS_NODE "/host1x/sor1"
59 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
60 static struct of_device_id tegra_sor_pd[] = {
61 { .compatible = "nvidia, tegra210-sor-pd", },
62 { .compatible = "nvidia, tegra132-sor-pd", },
63 { .compatible = "nvidia, tegra124-sor-pd", },
68 static ssize_t hdmi_ddc_power_toggle(struct kobject *kobj,
69 struct kobj_attribute *attr, const char *buf, size_t count);
71 static ssize_t hdmi_ddc_power_show(struct kobject *kobj,
72 struct kobj_attribute *attr, char *buf);
74 static struct kobj_attribute hdmi_ddc_power_config =
75 __ATTR(config, 0640, hdmi_ddc_power_show, hdmi_ddc_power_toggle);
77 static struct kobject *hdmi_ddc;
79 struct tmds_prod_pair {
84 static struct tmds_prod_pair tmds_config_modes[] = {
103 .name = "prod_c_600M"
107 static struct tegra_hdmi *dc_hdmi;
109 static int tegra_hdmi_controller_enable(struct tegra_hdmi *hdmi);
110 static void tegra_hdmi_config_clk(struct tegra_hdmi *hdmi, u32 clk_type);
111 static long tegra_dc_hdmi_setup_clk(struct tegra_dc *dc, struct clk *clk);
112 static void tegra_hdmi_scdc_worker(struct work_struct *work);
113 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi);
115 static inline bool tegra_hdmi_is_connected(struct tegra_hdmi *hdmi)
117 return (hdmi->mon_spec.misc & FB_MISC_HDMI) ||
118 (hdmi->mon_spec.misc & FB_MISC_HDMI_FORUM);
121 static inline void tegra_hdmi_irq_enable(struct tegra_hdmi *hdmi)
123 if (tegra_platform_is_fpga())
126 enable_irq(hdmi->irq);
129 static inline void tegra_hdmi_irq_disable(struct tegra_hdmi *hdmi)
131 if (tegra_platform_is_fpga())
134 disable_irq(hdmi->irq);
137 static inline bool tegra_hdmi_hpd_asserted(struct tegra_hdmi *hdmi)
139 return tegra_dc_hpd(hdmi->dc);
142 static inline void tegra_hdmi_reset(struct tegra_hdmi *hdmi)
144 if (tegra_platform_is_linsim())
147 tegra_periph_reset_assert(hdmi->sor->sor_clk);
149 tegra_periph_reset_deassert(hdmi->sor->sor_clk);
153 static inline void _tegra_hdmi_ddc_enable(struct tegra_hdmi *hdmi)
155 mutex_lock(&hdmi->ddc_refcount_lock);
156 if (hdmi->ddc_refcount++)
158 tegra_hdmi_get(hdmi->dc);
160 * hdmi uses i2c lane muxed on dpaux1 pad.
161 * Enable dpaux1 pads and configure the mux.
163 tegra_dpaux_config_pad_mode(hdmi->dc, TEGRA_DPAUX_INSTANCE_1,
164 TEGRA_DPAUX_PAD_MODE_I2C);
167 mutex_unlock(&hdmi->ddc_refcount_lock);
170 static inline void _tegra_hdmi_ddc_disable(struct tegra_hdmi *hdmi)
172 mutex_lock(&hdmi->ddc_refcount_lock);
174 if (WARN_ONCE(hdmi->ddc_refcount <= 0, "ddc refcount imbalance"))
176 if (--hdmi->ddc_refcount != 0)
180 * hdmi uses i2c lane muxed on dpaux1 pad.
181 * Disable dpaux1 pads.
183 tegra_dpaux_pad_power(hdmi->dc, TEGRA_DPAUX_INSTANCE_1, false);
184 tegra_hdmi_put(hdmi->dc);
187 mutex_unlock(&hdmi->ddc_refcount_lock);
190 static int tegra_hdmi_ddc_i2c_xfer(struct tegra_dc *dc,
191 struct i2c_msg *msgs, int num)
193 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
196 _tegra_hdmi_ddc_enable(hdmi);
197 ret = i2c_transfer(hdmi->ddc_i2c_client->adapter, msgs, num);
198 _tegra_hdmi_ddc_disable(hdmi);
202 static int tegra_hdmi_ddc_init(struct tegra_hdmi *hdmi, int edid_src)
204 struct tegra_dc *dc = hdmi->dc;
205 struct i2c_adapter *i2c_adap;
207 struct i2c_board_info i2c_dev_info = {
208 .type = "tegra_hdmi2.0",
212 hdmi->edid = tegra_edid_create(dc, tegra_hdmi_ddc_i2c_xfer);
213 else if (edid_src == 1)
214 hdmi->edid = tegra_edid_create(dc, tegra_dc_edid_blob);
215 if (IS_ERR_OR_NULL(hdmi->edid)) {
216 dev_err(&dc->ndev->dev, "hdmi: can't create edid\n");
217 return PTR_ERR(hdmi->edid);
219 tegra_dc_set_edid(dc, hdmi->edid);
221 i2c_adap = i2c_get_adapter(dc->out->ddc_bus);
223 dev_err(&dc->ndev->dev,
224 "hdmi: can't get adpater for ddc bus %d\n",
229 hdmi->ddc_i2c_original_rate = i2c_get_adapter_bus_clk_rate(i2c_adap);
231 hdmi->ddc_i2c_client = i2c_new_device(i2c_adap, &i2c_dev_info);
232 i2c_put_adapter(i2c_adap);
233 if (!hdmi->ddc_i2c_client) {
234 dev_err(&dc->ndev->dev, "hdmi: can't create new i2c device\n");
241 tegra_edid_destroy(hdmi->edid);
245 static int tegra_hdmi_scdc_i2c_xfer(struct tegra_dc *dc,
246 struct i2c_msg *msgs, int num)
248 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
250 return i2c_transfer(hdmi->scdc_i2c_client->adapter, msgs, num);
253 static int tegra_hdmi_scdc_init(struct tegra_hdmi *hdmi)
255 struct tegra_dc *dc = hdmi->dc;
256 struct i2c_adapter *i2c_adap;
258 struct i2c_board_info i2c_dev_info = {
259 .type = "tegra_hdmi_scdc",
263 i2c_adap = i2c_get_adapter(dc->out->ddc_bus);
265 dev_err(&dc->ndev->dev,
266 "hdmi: can't get adpater for scdc bus %d\n",
272 hdmi->scdc_i2c_client = i2c_new_device(i2c_adap, &i2c_dev_info);
273 i2c_put_adapter(i2c_adap);
274 if (!hdmi->scdc_i2c_client) {
275 dev_err(&dc->ndev->dev,
276 "hdmi: can't create scdc i2c device\n");
281 INIT_DELAYED_WORK(&hdmi->scdc_work, tegra_hdmi_scdc_worker);
288 static bool tegra_hdmi_check_dc_constraint(const struct fb_videomode *mode)
290 return (mode->hsync_len >= 1) && (mode->vsync_len >= 1) &&
291 (mode->lower_margin + mode->vsync_len +
292 mode->upper_margin > 1) &&
293 (mode->xres >= 16) && (mode->yres >= 16);
296 /* does not return precise tmds character rate */
297 static u32 tegra_hdmi_mode_min_tmds_rate(const struct fb_videomode *mode)
299 u32 tmds_csc_8bpc_khz = PICOS2KHZ(mode->pixclock);
301 if (mode->vmode & (FB_VMODE_Y420 | FB_VMODE_Y420_ONLY))
302 tmds_csc_8bpc_khz /= 2;
304 return tmds_csc_8bpc_khz;
308 static bool tegra_hdmi_fb_mode_filter(const struct tegra_dc *dc,
309 struct fb_videomode *mode)
311 struct tegra_hdmi *hdmi = dc->out_data;
316 if (mode->xres > 4096)
319 /* some non-compliant edids list 420vdb modes in vdb */
320 if ((mode->vmode & FB_VMODE_Y420) &&
321 !(tegra_edid_is_hfvsdb_present(hdmi->edid) &&
322 tegra_edid_is_scdc_present(hdmi->edid)) &&
323 tegra_edid_is_420db_present(hdmi->edid)) {
324 mode->vmode &= ~FB_VMODE_Y420;
325 mode->vmode |= FB_VMODE_Y420_ONLY;
328 if (mode->vmode & FB_VMODE_YUV_MASK &&
329 tegra_edid_get_quirks(hdmi->edid) & TEGRA_EDID_QUIRK_NO_YUV)
333 * There are currently many TVs in the market that actually do NOT support
334 * 4k@60fps 4:4:4 (594 MHz), (especially on the HDCP 2.2 ports), but
335 * advertise it in the DTD block in their EDIDs. The workaround for this port
336 * is to disable the 594 MHz mode if no HF-VSDB is present or if no SCDC
337 * support is indicated
339 if (tegra_hdmi_mode_min_tmds_rate(mode) / 1000 >= 340 &&
340 (!tegra_edid_is_hfvsdb_present(hdmi->edid) ||
341 !tegra_edid_is_scdc_present(hdmi->edid)))
345 * Check if mode's pixel clock requirement can be satisfied. Note that
346 * the pixclock value is in pico seconds.
348 if (mode->pixclock && tegra_dc_get_out_max_pixclock(dc) &&
349 mode->pixclock < tegra_dc_get_out_max_pixclock(dc))
353 * Work around for modes that fail the constraint:
354 * V_FRONT_PORCH >= V_REF_TO_SYNC + 1
356 if (mode->lower_margin == 1) {
357 mode->lower_margin++;
358 mode->upper_margin--;
359 mode->vmode |= FB_VMODE_ADJUSTED;
362 if (!tegra_hdmi_check_dc_constraint(mode))
368 static void tegra_hdmi_ddc_power_toggle(int value)
374 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
375 partition_id = tegra_pd_get_powergate_id(tegra_sor_pd);
376 if (partition_id < 0)
379 partition_id = TEGRA_POWERGATE_SOR;
383 _tegra_hdmi_ddc_disable(dc_hdmi);
384 tegra_powergate_partition(partition_id);
385 } else if (value == 1) {
386 tegra_unpowergate_partition(partition_id);
387 _tegra_hdmi_ddc_enable(dc_hdmi);
393 static int tegra_hdmi_get_mon_spec(struct tegra_hdmi *hdmi)
395 #define MAX_RETRY 100
396 #define MIN_RETRY_DELAY_US 200
397 #define MAX_RETRY_DELAY_US (MIN_RETRY_DELAY_US + 200)
399 size_t attempt_cnt = 0;
401 struct i2c_adapter *i2c_adap = i2c_get_adapter(hdmi->dc->out->ddc_bus);
403 if (IS_ERR_OR_NULL(hdmi->edid)) {
404 dev_err(&hdmi->dc->ndev->dev, "hdmi: edid not initialized\n");
405 return PTR_ERR(hdmi->edid);
408 tegra_edid_i2c_adap_change_rate(i2c_adap, hdmi->ddc_i2c_original_rate);
410 hdmi->mon_spec_valid = false;
411 if (hdmi->mon_spec_valid)
412 fb_destroy_modedb(hdmi->mon_spec.modedb);
413 memset(&hdmi->mon_spec, 0, sizeof(hdmi->mon_spec));
416 err = tegra_edid_get_monspecs(hdmi->edid, &hdmi->mon_spec);
418 usleep_range(MIN_RETRY_DELAY_US, MAX_RETRY_DELAY_US);
421 } while (++attempt_cnt < MAX_RETRY);
424 dev_err(&hdmi->dc->ndev->dev, "hdmi: edid read failed\n");
425 /* Try to load and parse the fallback edid */
426 hdmi->edid->errors = EDID_ERRORS_READ_FAILED;
427 err = tegra_edid_get_monspecs(hdmi->edid, &hdmi->mon_spec);
429 dev_err(&hdmi->dc->ndev->dev,
430 "hdmi: parsing fallback edid failed\n");
433 dev_err(&hdmi->dc->ndev->dev,
434 "hdmi: using fallback edid\n");
438 hdmi->mon_spec_valid = true;
441 #undef MAX_RETRY_DELAY_US
442 #undef MIN_RETRY_DELAY_US
446 static inline int tegra_hdmi_edid_read(struct tegra_hdmi *hdmi)
450 err = tegra_hdmi_get_mon_spec(hdmi);
455 static int tegra_hdmi_get_eld(struct tegra_hdmi *hdmi)
459 hdmi->eld_valid = false;
460 memset(&hdmi->eld, 0, sizeof(hdmi->eld));
462 err = tegra_edid_get_eld(hdmi->edid, &hdmi->eld);
464 dev_err(&hdmi->dc->ndev->dev, "hdmi: eld not available\n");
468 hdmi->eld_valid = true;
472 static inline int tegra_hdmi_eld_read(struct tegra_hdmi *hdmi)
474 return tegra_hdmi_get_eld(hdmi);
477 static void tegra_hdmi_edid_config(struct tegra_hdmi *hdmi)
479 #define CM_TO_MM(x) (x * 10)
481 struct tegra_dc *dc = hdmi->dc;
483 if (!hdmi->mon_spec_valid)
486 dc->out->h_size = CM_TO_MM(hdmi->mon_spec.max_x);
487 dc->out->v_size = CM_TO_MM(hdmi->mon_spec.max_y);
489 hdmi->dvi = !tegra_hdmi_is_connected(hdmi);
493 static void tegra_hdmi_hotplug_notify(struct tegra_hdmi *hdmi,
496 struct tegra_dc *dc = hdmi->dc;
497 struct fb_monspecs *mon_spec;
500 mon_spec = &hdmi->mon_spec;
504 #ifdef CONFIG_ADF_TEGRA
506 tegra_adf_process_hotplug_connected(hdmi->dc->adf, mon_spec);
509 tegra_fb_update_monspecs(hdmi->dc->fb, mon_spec,
510 tegra_hdmi_fb_mode_filter);
511 tegra_fb_update_fix(hdmi->dc->fb, mon_spec);
515 dc->connected = is_asserted;
516 tegra_dc_ext_process_hotplug(dc->ndev->id, is_asserted);
519 switch_set_state(&hdmi->hpd_switch, is_asserted ? 1 : 0);
523 static int tegra_hdmi_edid_eld_setup(struct tegra_hdmi *hdmi)
527 tegra_dc_unpowergate_locked(hdmi->dc);
529 err = tegra_hdmi_edid_read(hdmi);
533 err = tegra_hdmi_eld_read(hdmi);
537 err = tegra_hdmivrr_setup(hdmi);
539 dev_info(&hdmi->dc->ndev->dev, "vrr_setup failed\n");
541 tegra_dc_powergate_locked(hdmi->dc);
543 tegra_hdmi_edid_config(hdmi);
546 * eld is configured when audio needs it
547 * via tegra_hdmi_edid_config()
550 tegra_hdmi_hotplug_notify(hdmi, true);
553 tegra_dc_powergate_locked(hdmi->dc);
557 static int tegra_hdmi_controller_disable(struct tegra_hdmi *hdmi)
559 struct tegra_dc_sor_data *sor = hdmi->sor;
560 struct tegra_dc *dc = hdmi->dc;
564 tegra_nvhdcp_set_plug(hdmi->nvhdcp, 0);
565 tegra_dc_sor_detach(sor);
566 tegra_sor_power_lanes(sor, 4, false);
567 tegra_sor_hdmi_pad_power_down(sor);
568 tegra_hdmi_reset(hdmi);
576 static int tegra_hdmi_disable(struct tegra_hdmi *hdmi)
578 struct tegra_dc *dc = hdmi->dc;
580 if (!hdmi->enabled) {
581 dc->connected = false;
582 tegra_dc_ext_process_hotplug(dc->ndev->id, false);
584 switch_set_state(&hdmi->hpd_switch, 0);
589 hdmi->enabled = false;
590 hdmi->eld_valid = false;
591 hdmi->mon_spec_valid = false;
593 tegra_dc_disable(hdmi->dc);
595 tegra_hdmi_hotplug_notify(hdmi, false);
600 static int (*tegra_hdmi_state_func[])(struct tegra_hdmi *) = {
602 tegra_hdmi_edid_eld_setup,
605 enum tegra_hdmi_plug_states {
606 TEGRA_HDMI_MONITOR_DISABLE,
607 TEGRA_HDMI_MONITOR_ENABLE,
610 static int read_edid_into_buffer(struct tegra_hdmi *hdmi,
611 u8 *edid_data, size_t edid_data_len)
614 int extension_blocks;
615 int max_ext_blocks = (edid_data_len / 128) - 1;
617 err = tegra_edid_read_block(hdmi->edid, 0, edid_data);
619 dev_info(&hdmi->dc->ndev->dev, "hdmi: tegra_edid_read_block(0) returned err %d\n",
623 extension_blocks = edid_data[0x7e];
624 dev_info(&hdmi->dc->ndev->dev, "%s: extension_blocks = %d, max_ext_blocks = %d\n",
625 __func__, extension_blocks, max_ext_blocks);
626 if (extension_blocks > max_ext_blocks)
627 extension_blocks = max_ext_blocks;
628 for (i = 1; i <= extension_blocks; i++) {
629 err = tegra_edid_read_block(hdmi->edid, i, edid_data + i * 128);
631 dev_info(&hdmi->dc->ndev->dev, "hdmi: tegra_edid_read_block(%d) returned err %d\n",
639 static int hdmi_recheck_edid(struct tegra_hdmi *hdmi, int *match)
642 u8 tmp[HDMI_EDID_MAX_LENGTH] = {0};
643 ret = read_edid_into_buffer(hdmi, tmp, sizeof(tmp));
644 dev_info(&hdmi->dc->ndev->dev, "%s: read_edid_into_buffer() returned %d\n",
647 struct tegra_dc_edid *data = tegra_edid_get_data(hdmi->edid);
648 dev_info(&hdmi->dc->ndev->dev, "old edid len = %ld\n",
649 (long int)data->len);
650 *match = ((ret == data->len) &&
651 !memcmp(tmp, data->buf, data->len));
653 print_hex_dump(KERN_INFO, "tmp :", DUMP_PREFIX_ADDRESS,
654 16, 4, tmp, ret, true);
655 print_hex_dump(KERN_INFO, "data:", DUMP_PREFIX_ADDRESS,
656 16, 4, data->buf, data->len, true);
658 tegra_edid_put_data(data);
664 static void tegra_hdmi_hpd_worker(struct work_struct *work)
666 struct tegra_hdmi *hdmi = container_of(to_delayed_work(work),
667 struct tegra_hdmi, hpd_worker);
670 enum tegra_hdmi_plug_states orig_state;
673 mutex_lock(&hdmi->hpd_lock);
675 connected = tegra_dc_hpd(hdmi->dc);
676 orig_state = hdmi->plug_state;
678 if ((connected && orig_state == TEGRA_HDMI_MONITOR_ENABLE)) {
679 if (hdmi_recheck_edid(hdmi, &match)) {
680 dev_info(&hdmi->dc->ndev->dev, "hdmi: unable to read EDID\n");
684 dev_info(&hdmi->dc->ndev->dev, "hdmi: No EDID change after HPD bounce, taking no action.\n");
687 dev_info(&hdmi->dc->ndev->dev, "hdmi: EDID change after HPD bounce, resetting\n");
691 if ((!connected && orig_state == TEGRA_HDMI_MONITOR_DISABLE)) {
692 dev_info(&hdmi->dc->ndev->dev, "hdmi: spurious interrupt\n");
693 mutex_unlock(&hdmi->hpd_lock);
698 hdmi->plug_state = TEGRA_HDMI_MONITOR_ENABLE;
700 hdmi->plug_state = TEGRA_HDMI_MONITOR_DISABLE;
702 err = tegra_hdmi_state_func[hdmi->plug_state](hdmi);
705 dev_info(&hdmi->dc->ndev->dev,
706 "hdmi state %d failed during %splug\n",
707 hdmi->plug_state, connected ? "" : "un");
708 hdmi->plug_state = orig_state;
710 dev_info(&hdmi->dc->ndev->dev, "hdmi: %splugged\n",
711 connected ? "" : "un");
715 mutex_unlock(&hdmi->hpd_lock);
719 static irqreturn_t tegra_hdmi_hpd_irq_handler(int irq, void *ptr)
721 struct tegra_dc *dc = ptr;
722 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
724 if (atomic_read(&hdmi->suspended))
727 cancel_delayed_work(&hdmi->hpd_worker);
728 schedule_delayed_work(&hdmi->hpd_worker,
729 msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS));
734 static int tegra_hdmi_hpd_init(struct tegra_hdmi *hdmi)
736 struct tegra_dc *dc = hdmi->dc;
737 int hotplug_gpio = dc->out->hotplug_gpio;
741 if (!gpio_is_valid(hotplug_gpio)) {
742 dev_err(&dc->ndev->dev, "hdmi: invalid hotplug gpio\n");
746 hotplug_irq = gpio_to_irq(hotplug_gpio);
747 if (hotplug_irq < 0) {
748 dev_err(&dc->ndev->dev,
749 "hdmi: hotplug gpio to irq map failed\n");
753 err = gpio_request(hotplug_gpio, "hdmi2.0_hpd");
755 dev_err(&dc->ndev->dev,
756 "hdmi: hpd gpio_request failed %d\n", err);
757 gpio_direction_input(hotplug_gpio);
759 err = request_threaded_irq(hotplug_irq,
760 NULL, tegra_hdmi_hpd_irq_handler,
761 (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
763 dev_name(&dc->ndev->dev), dc);
765 dev_err(&dc->ndev->dev,
766 "hdmi: request_threaded_irq failed: %d\n", err);
771 INIT_DELAYED_WORK(&hdmi->hpd_worker, tegra_hdmi_hpd_worker);
773 mutex_init(&hdmi->hpd_lock);
774 hdmi->irq = hotplug_irq;
778 gpio_free(hotplug_gpio);
782 static int tegra_hdmi_tmds_init(struct tegra_hdmi *hdmi)
784 struct device_node *np_prod = of_find_node_by_path(TMDS_NODE);
787 dev_warn(&hdmi->dc->ndev->dev,
788 "hdmi: find tmds prod node failed\n");
793 tegra_prod_init((const struct device_node *)np_prod);
794 if (IS_ERR(hdmi->prod_list)) {
795 dev_warn(&hdmi->dc->ndev->dev,
796 "hdmi: prod list init failed with error %ld\n",
797 PTR_ERR(hdmi->prod_list));
798 of_node_put(np_prod);
802 of_node_put(np_prod);
806 static int tegra_hdmi_config_tmds(struct tegra_hdmi *hdmi)
812 /* Select mode with smallest clk freq > pclk */
813 tmds_len = ARRAY_SIZE(tmds_config_modes);
814 for (i = 0; i < tmds_len - 1 &&
815 tmds_config_modes[i].clk < hdmi->dc->mode.pclk; i++);
817 if (tegra_platform_is_linsim())
820 err = tegra_prod_set_by_name(&hdmi->sor->base,
821 tmds_config_modes[i].name, hdmi->prod_list);
823 dev_warn(&hdmi->dc->ndev->dev,
824 "hdmi: tmds prod set failed\n");
831 static int tegra_dc_hdmi_init(struct tegra_dc *dc)
833 struct tegra_hdmi *hdmi;
835 struct device_node *np = dc->ndev->dev.of_node;
837 struct device_node *np_hdmi = of_find_node_by_path(HDMI_NODE);
839 struct device_node *np_hdmi = NULL;
841 struct device_node *np_panel = NULL;
844 hdmi = devm_kzalloc(&dc->ndev->dev, sizeof(*hdmi), GFP_KERNEL);
846 of_node_put(np_hdmi);
850 hdmi->sor = tegra_dc_sor_init(dc, NULL);
851 if (IS_ERR_OR_NULL(hdmi->sor)) {
852 err = PTR_ERR(hdmi->sor);
857 if (np_hdmi && of_device_is_available(np_hdmi)) {
858 np_panel = tegra_get_panel_node_out_type_check(dc,
860 if (np_panel && of_device_is_available(np_panel)) {
861 if (of_property_read_bool(np_panel,
864 of_node_put(np_panel);
868 of_node_put(np_panel);
873 hdmi->pdata = dc->pdata->default_out->hdmi_out;
876 hdmi->ddc_refcount = 0; /* assumes this is disabled when starting */
877 mutex_init(&hdmi->ddc_refcount_lock);
879 hdmi->mon_spec_valid = false;
880 hdmi->eld_valid = false;
881 hdmi->device_shutdown = false;
883 /* TODO: seamless boot mode needs initialize the state */
885 hdmi->enabled = false;
886 atomic_set(&hdmi->clock_refcount, 0);
888 atomic_set(&hdmi->suspended, 0);
890 #ifdef CONFIG_TEGRA_HDMIHDCP
891 hdmi->nvhdcp = tegra_nvhdcp_create(hdmi, dc->ndev->id,
893 if (IS_ERR_OR_NULL(hdmi->nvhdcp)) {
894 err = PTR_ERR(hdmi->nvhdcp);
897 tegra_nvhdcp_debugfs_init(hdmi->nvhdcp);
900 tegra_hdmi_ddc_init(hdmi, edid_src);
902 tegra_hdmi_scdc_init(hdmi);
904 tegra_hdmi_hpd_init(hdmi);
906 tegra_hdmi_vrr_init(hdmi);
908 tegra_hdmi_debugfs_init(hdmi);
910 tegra_hdmi_tmds_init(hdmi);
912 tegra_dc_set_outdata(dc, hdmi);
914 /* NOTE: Below code is applicable to L4T or embedded systems and is
915 * protected accordingly. This section early enables DC with first mode
916 * from the monitor specs.
917 * In case there is no hotplug we are falling back
918 * to default VGA mode.
920 if ((config_enabled(CONFIG_FRAMEBUFFER_CONSOLE) ||
921 ((dc->pdata->flags & TEGRA_DC_FLAG_ENABLED) &&
922 (dc->pdata->flags & TEGRA_DC_FLAG_SET_EARLY_MODE))) &&
923 dc->out && (dc->out->type == TEGRA_DC_OUT_HDMI)) {
924 struct fb_monspecs specs;
925 if (tegra_dc_hpd(dc) && (!dc->initialized)) {
926 if (!tegra_edid_get_monspecs(hdmi->edid, &specs))
927 tegra_dc_set_fb_mode(dc, specs.modedb, false);
929 /* if for some reason there is no edid upon hotplug */
930 tegra_dc_set_fb_mode(dc,
931 &tegra_dc_vga_mode, false);
934 tegra_dc_set_fb_mode(dc, &tegra_dc_vga_mode, false);
938 hdmi->hpd_switch.name = "hdmi";
939 err = switch_dev_register(&hdmi->hpd_switch);
941 dev_err(&dc->ndev->dev,
942 "hdmi: failed to register hpd switch %d\n", err);
944 hdmi->audio_switch.name = "hdmi_audio";
945 err = switch_dev_register(&hdmi->audio_switch);
947 dev_err(&dc->ndev->dev,
948 "hdmi: failed to register audio switch %d\n", err);
951 hdmi_ddc = kobject_create_and_add("hdmi_ddc_power_toggle", kernel_kobj);
953 pr_warn("kobject create_and_add hdmi_ddc_power_toggle failed\n");
956 err = sysfs_create_file(hdmi_ddc, &hdmi_ddc_power_config.attr);
958 pr_warn("sysfs create file hdmi_ddc_power_toggle failed\n");
962 of_node_put(np_hdmi);
965 devm_kfree(&dc->ndev->dev, hdmi);
966 of_node_put(np_hdmi);
970 static void tegra_dc_hdmi_destroy(struct tegra_dc *dc)
972 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
974 tegra_dc_sor_destroy(hdmi->sor);
975 tegra_edid_destroy(hdmi->edid);
976 tegra_nvhdcp_destroy(hdmi->nvhdcp);
977 free_irq(gpio_to_irq(dc->out->hotplug_gpio), dc);
978 gpio_free(dc->out->hotplug_gpio);
979 devm_kfree(&dc->ndev->dev, hdmi);
980 tegra_prod_release(&hdmi->prod_list);
983 switch_dev_unregister(&hdmi->hpd_switch);
984 switch_dev_unregister(&hdmi->audio_switch);
988 static void tegra_hdmi_config(struct tegra_hdmi *hdmi)
990 struct tegra_dc_sor_data *sor = hdmi->sor;
991 struct tegra_dc *dc = hdmi->dc;
992 #ifndef CONFIG_TEGRA_NVDISPLAY
993 u32 h_pulse_start, h_pulse_end;
995 u32 hblank, max_ac, rekey;
999 if (tegra_platform_is_linsim())
1002 tegra_sor_write_field(sor, NV_SOR_INPUT_CONTROL,
1003 NV_SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED |
1004 NV_SOR_INPUT_CONTROL_HDMI_SRC_SELECT_DISPLAYB,
1005 NV_SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_FULL |
1006 NV_SOR_INPUT_CONTROL_HDMI_SRC_SELECT_DISPLAYB);
1008 dispclk_div_8_2 = clk_get_rate(hdmi->sor->sor_clk) / 1000000 * 4;
1009 tegra_sor_writel(sor, NV_SOR_REFCLK,
1010 NV_SOR_REFCLK_DIV_INT(dispclk_div_8_2 >> 2) |
1011 NV_SOR_REFCLK_DIV_FRAC(dispclk_div_8_2));
1013 rekey = NV_SOR_HDMI_CTRL_REKEY_DEFAULT;
1014 hblank = dc->mode.h_sync_width + dc->mode.h_back_porch +
1015 dc->mode.h_front_porch;
1016 max_ac = (hblank - rekey - 18) / 32;
1019 val |= hdmi->dvi ? 0x0 : NV_SOR_HDMI_CTRL_ENABLE;
1020 /* The register wants "-2" of the required rekey val */
1021 val |= NV_SOR_HDMI_CTRL_REKEY(rekey - 2);
1022 val |= NV_SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac);
1023 val |= NV_SOR_HDMI_CTRL_AUDIO_LAYOUT_SELECT;
1024 tegra_sor_writel(sor, NV_SOR_HDMI_CTRL, val);
1026 #ifndef CONFIG_TEGRA_NVDISPLAY
1027 tegra_dc_writel(dc, 0x180, DC_DISP_H_PULSE2_CONTROL);
1028 h_pulse_start = dc->mode.h_ref_to_sync +
1029 dc->mode.h_sync_width +
1030 dc->mode.h_back_porch - 10;
1031 h_pulse_end = h_pulse_start + 8;
1032 tegra_dc_writel(dc, PULSE_START(h_pulse_start) | PULSE_END(h_pulse_end),
1033 DC_DISP_H_PULSE2_POSITION_A);
1035 val = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
1036 val |= H_PULSE_2_ENABLE;
1037 tegra_dc_writel(dc, val, DC_DISP_DISP_SIGNAL_OPTIONS0);
1041 void tegra_hdmi_infoframe_pkt_write(struct tegra_hdmi *hdmi,
1042 u32 header_reg, u8 pkt_type,
1043 u8 pkt_vs, u8 pkt_len,
1045 u32 reg_payload_len,
1048 struct tegra_dc_sor_data *sor = hdmi->sor;
1050 u32 *data = reg_payload;
1051 u32 data_reg = header_reg + 1;
1053 val = NV_SOR_HDMI_INFOFRAME_HEADER_TYPE(pkt_type) |
1054 NV_SOR_HDMI_INFOFRAME_HEADER_VERSION(pkt_vs) |
1055 NV_SOR_HDMI_INFOFRAME_HEADER_LEN(pkt_len);
1056 tegra_sor_writel(sor, header_reg, val);
1059 u8 checksum = pkt_type + pkt_vs + pkt_len;
1061 for (val = 1; val <= pkt_len; val++)
1062 checksum += ((u8 *)reg_payload)[val];
1064 /* The first byte of the payload must always be the checksum
1065 * that we are going to calculate in SW */
1066 ((u8 *)reg_payload)[0] = (256 - checksum);
1069 for (val = 0; val < reg_payload_len; val += 4, data_reg++, data++)
1070 tegra_sor_writel(sor, data_reg, *data);
1073 u32 tegra_hdmi_get_cea_modedb_size(struct tegra_hdmi *hdmi)
1075 if (!tegra_hdmi_is_connected(hdmi) || !hdmi->mon_spec_valid)
1078 return (hdmi->mon_spec.misc & FB_MISC_HDMI_FORUM) ?
1079 CEA_861_F_MODEDB_SIZE : CEA_861_D_MODEDB_SIZE;
1082 static void tegra_hdmi_get_cea_fb_videomode(struct fb_videomode *m,
1083 struct tegra_hdmi *hdmi)
1085 struct tegra_dc *dc = hdmi->dc;
1086 struct tegra_dc_mode dc_mode;
1089 memcpy(&dc_mode, &dc->mode, sizeof(dc->mode));
1091 /* get CEA video timings */
1092 yuv_flag = dc_mode.vmode & FB_VMODE_YUV_MASK;
1093 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y24) ||
1094 yuv_flag == (FB_VMODE_Y420_ONLY | FB_VMODE_Y24)) {
1095 dc_mode.h_back_porch *= 2;
1096 dc_mode.h_front_porch *= 2;
1097 dc_mode.h_sync_width *= 2;
1098 dc_mode.h_active *= 2;
1100 } else if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30)) {
1101 dc_mode.h_back_porch = (dc_mode.h_back_porch * 8) / 5;
1102 dc_mode.h_front_porch = (dc_mode.h_front_porch * 8) / 5;
1103 dc_mode.h_sync_width = (dc_mode.h_sync_width * 8) / 5;
1104 dc_mode.h_active = (dc_mode.h_active * 8) / 5;
1105 dc_mode.pclk = (dc_mode.pclk / 5) * 8;
1108 tegra_dc_to_fb_videomode(m, &dc_mode);
1110 /* only interlaced required for VIC identification */
1111 m->vmode &= FB_VMODE_INTERLACED;
1115 static int tegra_hdmi_find_cea_vic(struct tegra_hdmi *hdmi)
1117 struct fb_videomode m;
1118 struct tegra_dc *dc = hdmi->dc;
1121 u32 modedb_size = tegra_hdmi_get_cea_modedb_size(hdmi);
1123 if (dc->initialized) {
1124 u32 vic = tegra_sor_readl(hdmi->sor,
1125 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_HIGH) & 0xff;
1127 dev_warn(&dc->ndev->dev, "hdmi: BL set VIC 0\n");
1131 tegra_hdmi_get_cea_fb_videomode(&m, hdmi);
1133 m.flag &= ~FB_FLAG_RATIO_MASK;
1134 m.flag |= tegra_dc_get_aspect_ratio(dc);
1136 for (i = 1; i < modedb_size; i++) {
1137 const struct fb_videomode *curr = &cea_modes[i];
1139 if (!fb_mode_is_equal_tolerance(curr, &m,
1140 FB_MODE_TOLERANCE_DEFAULT))
1145 /* if either flag is set, then match is required */
1147 (FB_FLAG_RATIO_4_3 | FB_FLAG_RATIO_16_9 |
1148 FB_FLAG_RATIO_64_27 | FB_FLAG_RATIO_256_135)) {
1149 if (m.flag & curr->flag & FB_FLAG_RATIO_4_3)
1151 else if (m.flag & curr->flag & FB_FLAG_RATIO_16_9)
1153 else if (m.flag & curr->flag & FB_FLAG_RATIO_64_27)
1155 else if (m.flag & curr->flag & FB_FLAG_RATIO_256_135)
1164 static u32 tegra_hdmi_get_aspect_ratio(struct tegra_hdmi *hdmi)
1168 switch (hdmi->dc->mode.avi_m) {
1169 case TEGRA_DC_MODE_AVI_M_4_3:
1170 aspect_ratio = HDMI_AVI_ASPECT_RATIO_4_3;
1172 case TEGRA_DC_MODE_AVI_M_16_9:
1173 aspect_ratio = HDMI_AVI_ASPECT_RATIO_16_9;
1176 * no avi_m field for picture aspect ratio 64:27 and 256:135.
1177 * sink detects via VIC, avi_m is 0.
1179 case TEGRA_DC_MODE_AVI_M_64_27: /* fall through */
1180 case TEGRA_DC_MODE_AVI_M_256_135: /* fall through */
1182 aspect_ratio = HDMI_AVI_ASPECT_RATIO_NO_DATA;
1185 /* For seamless HDMI, read aspect ratio parameters from bootloader
1186 * set AVI Infoframe parameters
1188 if ((aspect_ratio == HDMI_AVI_ASPECT_RATIO_NO_DATA) &&
1189 (hdmi->dc->initialized)) {
1191 temp = tegra_sor_readl(hdmi->sor,
1192 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_LOW);
1193 temp = (temp >> 20) & 0x3;
1196 aspect_ratio = HDMI_AVI_ASPECT_RATIO_4_3;
1199 aspect_ratio = HDMI_AVI_ASPECT_RATIO_16_9;
1202 aspect_ratio = HDMI_AVI_ASPECT_RATIO_NO_DATA;
1205 return aspect_ratio;
1208 static u32 tegra_hdmi_get_rgb_ycc(struct tegra_hdmi *hdmi)
1210 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1213 * For seamless HDMI, read YUV flag parameters from bootloader
1214 * set AVI Infoframe parameters
1216 if (hdmi->dc->initialized) {
1218 temp = tegra_sor_readl(hdmi->sor,
1219 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_LOW);
1220 temp = (temp >> 12) & 0x3;
1223 return HDMI_AVI_RGB;
1224 case HDMI_AVI_YCC_420:
1225 return HDMI_AVI_YCC_420;
1227 dev_warn(&hdmi->dc->ndev->dev, "hdmi: BL didn't set RGB/YUV indicator flag\n");
1232 if (yuv_flag & (FB_VMODE_Y420 | FB_VMODE_Y420_ONLY))
1233 return HDMI_AVI_YCC_420;
1234 else if (yuv_flag & FB_VMODE_Y422)
1235 return HDMI_AVI_YCC_422;
1237 return HDMI_AVI_RGB;
1240 static bool tegra_hdmi_is_ex_colorimetry(struct tegra_hdmi *hdmi)
1242 return !!(hdmi->dc->mode.vmode & FB_VMODE_EC_ENABLE);
1245 static u32 tegra_hdmi_get_ex_colorimetry(struct tegra_hdmi *hdmi)
1247 u32 vmode = hdmi->dc->mode.vmode;
1249 return tegra_hdmi_is_ex_colorimetry(hdmi) ?
1250 ((vmode & FB_VMODE_EC_MASK) >> FB_VMODE_EC_SHIFT) :
1251 HDMI_AVI_EXT_COLORIMETRY_INVALID;
1254 static void tegra_hdmi_avi_infoframe_update(struct tegra_hdmi *hdmi)
1256 struct hdmi_avi_infoframe *avi = &hdmi->avi;
1258 memset(&hdmi->avi, 0, sizeof(hdmi->avi));
1260 if (tegra_platform_is_linsim())
1263 avi->scan = HDMI_AVI_UNDERSCAN;
1264 avi->bar_valid = HDMI_AVI_BAR_INVALID;
1265 avi->act_fmt_valid = HDMI_AVI_ACTIVE_FORMAT_VALID;
1266 avi->rgb_ycc = tegra_hdmi_get_rgb_ycc(hdmi);
1268 avi->act_format = HDMI_AVI_ACTIVE_FORMAT_SAME;
1269 avi->aspect_ratio = tegra_hdmi_get_aspect_ratio(hdmi);
1270 avi->colorimetry = tegra_hdmi_is_ex_colorimetry(hdmi) ?
1271 HDMI_AVI_COLORIMETRY_EXTENDED_VALID :
1272 HDMI_AVI_COLORIMETRY_DEFAULT;
1274 avi->scaling = HDMI_AVI_SCALING_UNKNOWN;
1275 avi->rgb_quant = HDMI_AVI_RGB_QUANT_DEFAULT;
1276 avi->ext_colorimetry = tegra_hdmi_get_ex_colorimetry(hdmi);
1277 avi->it_content = HDMI_AVI_IT_CONTENT_FALSE;
1279 /* set correct vic if video format is cea defined else set 0 */
1280 avi->video_format = tegra_hdmi_find_cea_vic(hdmi);
1282 avi->pix_rep = HDMI_AVI_NO_PIX_REPEAT;
1283 avi->it_content_type = HDMI_AVI_IT_CONTENT_NONE;
1284 avi->ycc_quant = HDMI_AVI_YCC_QUANT_NONE;
1286 avi->top_bar_end_line_low_byte = 0;
1287 avi->top_bar_end_line_high_byte = 0;
1289 avi->bot_bar_start_line_low_byte = 0;
1290 avi->bot_bar_start_line_high_byte = 0;
1292 avi->left_bar_end_pixel_low_byte = 0;
1293 avi->left_bar_end_pixel_high_byte = 0;
1295 avi->right_bar_start_pixel_low_byte = 0;
1296 avi->right_bar_start_pixel_high_byte = 0;
1299 static void tegra_hdmi_avi_infoframe(struct tegra_hdmi *hdmi)
1301 struct tegra_dc_sor_data *sor = hdmi->sor;
1306 if (tegra_platform_is_linsim())
1309 /* disable avi infoframe before configuring except for seamless case */
1310 if (!hdmi->dc->initialized)
1311 tegra_sor_writel(sor, NV_SOR_HDMI_AVI_INFOFRAME_CTRL, 0);
1313 tegra_hdmi_avi_infoframe_update(hdmi);
1315 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_AVI_INFOFRAME_HEADER,
1316 HDMI_INFOFRAME_TYPE_AVI,
1317 HDMI_INFOFRAME_VS_AVI,
1318 HDMI_INFOFRAME_LEN_AVI,
1319 &hdmi->avi, sizeof(hdmi->avi),
1322 /* Send infoframe every frame, checksum hw generated */
1323 tegra_sor_writel(sor, NV_SOR_HDMI_AVI_INFOFRAME_CTRL,
1324 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_ENABLE_YES |
1325 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_OTHER_DISABLE |
1326 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_SINGLE_DISABLE |
1327 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_CHECKSUM_ENABLE);
1330 static int tegra_hdmi_get_extended_vic(const struct tegra_dc_mode *mode)
1332 struct fb_videomode m;
1335 tegra_dc_to_fb_videomode(&m, mode);
1337 /* only interlaced required for VIC identification */
1338 m.vmode &= FB_VMODE_INTERLACED;
1340 for (i = 1; i < HDMI_EXT_MODEDB_SIZE; i++) {
1341 const struct fb_videomode *curr = &hdmi_ext_modes[i];
1343 if (fb_mode_is_equal_tolerance(curr, &m,
1344 FB_MODE_TOLERANCE_DEFAULT))
1350 static void tegra_hdmi_vendor_infoframe_update(struct tegra_hdmi *hdmi)
1352 struct hdmi_vendor_infoframe *vsi = &hdmi->vsi;
1355 memset(&hdmi->vsi, 0, sizeof(hdmi->vsi));
1357 vsi->oui = HDMI_LICENSING_LLC_OUI;
1359 extended_vic = tegra_hdmi_get_extended_vic(&hdmi->dc->mode);
1362 HDMI_VENDOR_VIDEO_FORMAT_EXTENDED;
1363 vsi->extended_vic = extended_vic;
1367 static void tegra_hdmi_vendor_infoframe(struct tegra_hdmi *hdmi)
1369 /* hdmi licensing, LLC vsi playload len as per hdmi1.4b */
1370 #define HDMI_INFOFRAME_LEN_VENDOR_LLC (6)
1372 struct tegra_dc_sor_data *sor = hdmi->sor;
1377 /* disable vsi infoframe before configuring */
1378 tegra_sor_writel(sor, NV_SOR_HDMI_VSI_INFOFRAME_CTRL, 0);
1380 tegra_hdmi_vendor_infoframe_update(hdmi);
1382 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_VSI_INFOFRAME_HEADER,
1383 HDMI_INFOFRAME_TYPE_VENDOR,
1384 HDMI_INFOFRAME_VS_VENDOR,
1385 HDMI_INFOFRAME_LEN_VENDOR_LLC,
1386 &hdmi->vsi, sizeof(hdmi->vsi),
1389 /* Send infoframe every frame, checksum hw generated */
1390 tegra_sor_writel(sor, NV_SOR_HDMI_VSI_INFOFRAME_CTRL,
1391 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_ENABLE_YES |
1392 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_OTHER_DISABLE |
1393 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_SINGLE_DISABLE |
1394 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_CHECKSUM_ENABLE);
1396 #undef HDMI_INFOFRAME_LEN_VENDOR_LLC
1399 static void tegra_hdmi_hdr_infoframe_update(struct tegra_hdmi *hdmi)
1401 struct hdmi_hdr_infoframe *hdr = &hdmi->hdr;
1403 memset(&hdmi->hdr, 0, sizeof(hdmi->hdr));
1405 if (tegra_platform_is_linsim())
1408 hdr->eotf = hdmi->dc->hdr.eotf;
1409 hdr->static_metadata_id = hdmi->dc->hdr.static_metadata_id;
1411 /* PB3-14 : Group 1 : Static Metadata*/
1412 hdr->display_primaries_x_0_lsb = hdmi->dc->hdr.static_metadata[0];
1413 hdr->display_primaries_x_0_msb = hdmi->dc->hdr.static_metadata[1];
1414 hdr->display_primaries_y_0_lsb = hdmi->dc->hdr.static_metadata[2];
1415 hdr->display_primaries_y_0_msb = hdmi->dc->hdr.static_metadata[3];
1416 hdr->display_primaries_x_1_lsb = hdmi->dc->hdr.static_metadata[4];
1417 hdr->display_primaries_x_1_msb = hdmi->dc->hdr.static_metadata[5];
1418 hdr->display_primaries_y_1_lsb = hdmi->dc->hdr.static_metadata[6];
1419 hdr->display_primaries_y_1_msb = hdmi->dc->hdr.static_metadata[7];
1420 hdr->display_primaries_x_2_lsb = hdmi->dc->hdr.static_metadata[8];
1421 hdr->display_primaries_x_2_msb = hdmi->dc->hdr.static_metadata[9];
1422 hdr->display_primaries_y_2_lsb = hdmi->dc->hdr.static_metadata[10];
1423 hdr->display_primaries_y_2_msb = hdmi->dc->hdr.static_metadata[11];
1425 /* PB15-18 : Group 2 : Static Metadata*/
1426 hdr->white_point_x_lsb = hdmi->dc->hdr.static_metadata[12];
1427 hdr->white_point_x_msb = hdmi->dc->hdr.static_metadata[13];
1428 hdr->white_point_y_lsb = hdmi->dc->hdr.static_metadata[14];
1429 hdr->white_point_y_msb = hdmi->dc->hdr.static_metadata[15];
1431 /* PB19-20 : Group 3 : Static Metadata*/
1432 hdr->max_display_mastering_luminance_lsb =
1433 hdmi->dc->hdr.static_metadata[16];
1434 hdr->max_display_mastering_luminance_msb =
1435 hdmi->dc->hdr.static_metadata[17];
1437 /* PB21-22 : Group 4 : Static Metadata*/
1438 hdr->min_display_mastering_luminance_lsb =
1439 hdmi->dc->hdr.static_metadata[18];
1440 hdr->min_display_mastering_luminance_msb =
1441 hdmi->dc->hdr.static_metadata[19];
1443 /* PB23-24 : Group 5 : Static Metadata*/
1444 hdr->max_content_light_level_lsb = hdmi->dc->hdr.static_metadata[20];
1445 hdr->max_content_light_level_msb = hdmi->dc->hdr.static_metadata[21];
1447 /* PB25-26 : Group 6 : Static Metadata*/
1448 hdr->max_frame_avg_light_level_lsb = hdmi->dc->hdr.static_metadata[22];
1449 hdr->min_frame_avg_light_level_msb = hdmi->dc->hdr.static_metadata[23];
1454 static void tegra_hdmi_hdr_infoframe(struct tegra_hdmi *hdmi)
1456 struct tegra_dc_sor_data *sor = hdmi->sor;
1459 /* set_bits = contains all the bits to be set
1460 * for NV_SOR_HDMI_GENERIC_CTRL reg */
1461 u32 set_bits = (NV_SOR_HDMI_GENERIC_CTRL_ENABLE_YES |
1462 NV_SOR_HDMI_GENERIC_CTRL_OTHER_DISABLE |
1463 NV_SOR_HDMI_GENERIC_CTRL_SINGLE_DISABLE);
1465 /* read the current value to restore some bit values */
1466 val = (tegra_sor_readl(sor, NV_SOR_HDMI_GENERIC_CTRL)
1469 /* disable generic infoframe before configuring */
1470 tegra_sor_writel(sor, NV_SOR_HDMI_GENERIC_CTRL, 0);
1472 tegra_hdmi_hdr_infoframe_update(hdmi);
1474 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_GENERIC_HEADER,
1475 HDMI_INFOFRAME_TYPE_HDR,
1476 HDMI_INFOFRAME_VS_HDR,
1477 HDMI_INFOFRAME_LEN_HDR,
1478 &hdmi->hdr, sizeof(hdmi->hdr),
1481 /* set the required bits in NV_SOR_HDMI_GENERIC_CTRL*/
1482 val = val | set_bits;
1484 tegra_sor_writel(sor, NV_SOR_HDMI_GENERIC_CTRL, val);
1490 static int tegra_hdmi_scdc_read(struct tegra_hdmi *hdmi,
1491 u8 offset_data[][2], u32 n_entries)
1494 struct i2c_msg msg[] = {
1508 _tegra_hdmi_ddc_enable(hdmi);
1510 for (i = 0; i < n_entries; i++) {
1511 msg[0].buf = offset_data[i];
1512 msg[1].buf = &offset_data[i][1];
1513 tegra_hdmi_scdc_i2c_xfer(hdmi->dc, msg, ARRAY_SIZE(msg));
1516 _tegra_hdmi_ddc_disable(hdmi);
1521 static int tegra_hdmi_scdc_write(struct tegra_hdmi *hdmi,
1522 u8 offset_data[][2], u32 n_entries)
1525 struct i2c_msg msg[] = {
1533 _tegra_hdmi_ddc_enable(hdmi);
1535 for (i = 0; i < n_entries; i++) {
1536 msg[0].buf = offset_data[i];
1537 tegra_hdmi_scdc_i2c_xfer(hdmi->dc, msg, ARRAY_SIZE(msg));
1540 _tegra_hdmi_ddc_disable(hdmi);
1545 static int tegra_hdmi_v2_x_mon_config(struct tegra_hdmi *hdmi, bool enable)
1547 u8 tmds_config_en[][2] = {
1549 HDMI_SCDC_TMDS_CONFIG_OFFSET,
1550 (HDMI_SCDC_TMDS_CONFIG_BIT_CLK_RATIO_40 |
1551 HDMI_SCDC_TMDS_CONFIG_SCRAMBLING_EN)
1554 u8 tmds_config_dis[][2] = {
1556 HDMI_SCDC_TMDS_CONFIG_OFFSET,
1561 if (hdmi->dc->vedid)
1564 tegra_hdmi_scdc_write(hdmi,
1565 enable ? tmds_config_en : tmds_config_dis,
1566 ARRAY_SIZE(tmds_config_en));
1572 static void tegra_hdmi_v2_x_host_config(struct tegra_hdmi *hdmi, bool enable)
1574 u32 val = NV_SOR_HDMI2_CTRL_SCRAMBLE_ENABLE |
1575 NV_SOR_HDMI2_CTRL_CLK_MODE_DIV_BY_4;
1577 tegra_sor_write_field(hdmi->sor, NV_SOR_HDMI2_CTRL,
1578 NV_SOR_HDMI2_CTRL_SCRAMBLE_ENABLE |
1579 NV_SOR_HDMI2_CTRL_CLK_MODE_DIV_BY_4,
1583 static int _tegra_hdmi_v2_x_config(struct tegra_hdmi *hdmi)
1585 #define SCDC_STABILIZATION_DELAY_MS (20)
1587 /* disable hdmi2.x config on host and monitor only
1588 * if bootloader didn't initialize hdmi
1590 if (!hdmi->dc->initialized) {
1591 tegra_hdmi_v2_x_mon_config(hdmi, false);
1592 tegra_hdmi_v2_x_host_config(hdmi, false);
1595 /* enable hdmi2.x config on host and monitor */
1596 tegra_hdmi_v2_x_mon_config(hdmi, true);
1597 msleep(SCDC_STABILIZATION_DELAY_MS);
1599 tegra_hdmi_v2_x_host_config(hdmi, true);
1602 #undef SCDC_STABILIZATION_DELAY_MS
1605 static int tegra_hdmi_v2_x_config(struct tegra_hdmi *hdmi)
1607 _tegra_hdmi_v2_x_config(hdmi);
1612 static void tegra_hdmi_scdc_worker(struct work_struct *work)
1614 struct tegra_hdmi *hdmi = container_of(to_delayed_work(work),
1615 struct tegra_hdmi, scdc_work);
1616 u8 rd_status_flags[][2] = {
1617 {HDMI_SCDC_STATUS_FLAGS, 0x0}
1620 if (!hdmi->enabled || hdmi->dc->mode.pclk <= 340000000)
1623 if (hdmi->dc->vedid)
1626 if (!tegra_edid_is_scdc_present(hdmi->dc->edid))
1629 tegra_hdmi_scdc_read(hdmi, rd_status_flags,
1630 ARRAY_SIZE(rd_status_flags));
1631 if (!rd_status_flags[0][1] && (hdmi->dc->mode.pclk > 340000000)) {
1632 dev_info(&hdmi->dc->ndev->dev, "hdmi: scdc scrambling status is reset, "
1633 "trying to reconfigure.\n");
1634 _tegra_hdmi_v2_x_config(hdmi);
1638 /* reschedule the worker */
1639 cancel_delayed_work(&hdmi->scdc_work);
1640 schedule_delayed_work(&hdmi->scdc_work,
1641 msecs_to_jiffies(HDMI_SCDC_MONITOR_TIMEOUT_MS));
1644 static void _tegra_hdmi_clock_enable(struct tegra_hdmi *hdmi)
1646 struct tegra_dc_sor_data *sor = hdmi->sor;
1647 tegra_disp_clk_prepare_enable(sor->safe_clk);
1648 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_SAFE_CLK);
1649 tegra_sor_clk_enable(sor);
1652 static void _tegra_hdmi_clock_disable(struct tegra_hdmi *hdmi)
1654 struct tegra_dc_sor_data *sor = hdmi->sor;
1655 tegra_sor_clk_disable(sor);
1658 void tegra_hdmi_get(struct tegra_dc *dc)
1660 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1662 if (atomic_inc_return(&hdmi->clock_refcount) == 1)
1663 _tegra_hdmi_clock_enable(hdmi);
1666 void tegra_hdmi_put(struct tegra_dc *dc)
1668 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1670 if (WARN_ONCE(atomic_read(&hdmi->clock_refcount) <= 0,
1671 "hdmi: clock refcount imbalance"))
1673 if (atomic_dec_return(&hdmi->clock_refcount) == 0)
1674 _tegra_hdmi_clock_disable(hdmi);
1677 /* TODO: add support for other deep colors */
1678 static inline u32 tegra_hdmi_get_bpp(struct tegra_hdmi *hdmi)
1680 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1682 if ((yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30)) ||
1683 (!(yuv_flag & YUV_MASK) && (yuv_flag == FB_VMODE_Y30)))
1685 else if (!(yuv_flag & YUV_MASK) && (yuv_flag == FB_VMODE_Y36))
1687 else if (yuv_flag == (FB_VMODE_Y422 | FB_VMODE_Y36))
1693 static u32 tegra_hdmi_gcp_color_depth(struct tegra_hdmi *hdmi)
1697 switch (tegra_hdmi_get_bpp(hdmi)) {
1699 gcp_cd = TEGRA_HDMI_BPP_UNKNOWN;
1702 gcp_cd = TEGRA_HDMI_BPP_24;
1705 gcp_cd = TEGRA_HDMI_BPP_30;
1708 gcp_cd = TEGRA_HDMI_BPP_36;
1711 gcp_cd = TEGRA_HDMI_BPP_48;
1714 dev_WARN(&hdmi->dc->ndev->dev,
1715 "hdmi: unknown gcp color depth\n");
1721 /* return packing phase of last pixel in preceding video data period */
1722 static u32 tegra_hdmi_gcp_packing_phase(struct tegra_hdmi *hdmi)
1724 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1726 if (!tegra_hdmi_gcp_color_depth(hdmi))
1729 if (!(yuv_flag & YUV_MASK) && (yuv_flag == FB_VMODE_Y36))
1735 static bool tegra_hdmi_gcp_default_phase_en(struct tegra_hdmi *hdmi)
1737 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1739 if (!tegra_hdmi_gcp_color_depth(hdmi))
1742 if ((yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30)) ||
1743 (!(yuv_flag & YUV_MASK) && (yuv_flag == FB_VMODE_Y36)))
1749 /* general control packet */
1750 static void tegra_hdmi_gcp(struct tegra_hdmi *hdmi)
1752 #define GCP_SB1_PP_SHIFT 4
1754 struct tegra_dc_sor_data *sor = hdmi->sor;
1757 /* disable gcp before configuring */
1758 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_CTRL, 0);
1760 sb1 = tegra_hdmi_gcp_packing_phase(hdmi) << GCP_SB1_PP_SHIFT |
1761 tegra_hdmi_gcp_color_depth(hdmi);
1762 sb2 = !!tegra_hdmi_gcp_default_phase_en(hdmi);
1763 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_SUBPACK(0),
1764 sb1 << NV_SOR_HDMI_GCP_SUBPACK_SB1_SHIFT |
1765 sb2 << NV_SOR_HDMI_GCP_SUBPACK_SB2_SHIFT);
1767 /* Send gcp every frame */
1768 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_CTRL,
1769 NV_SOR_HDMI_GCP_CTRL_ENABLE |
1770 NV_SOR_HDMI_GCP_CTRL_OTHER_DIS |
1771 NV_SOR_HDMI_GCP_CTRL_SINGLE_DIS);
1773 #undef GCP_SB1_PP_SHIFT
1776 static int tegra_hdmi_controller_enable(struct tegra_hdmi *hdmi)
1778 struct tegra_dc *dc = hdmi->dc;
1779 struct tegra_dc_sor_data *sor = hdmi->sor;
1784 tegra_sor_hdmi_pad_power_up(sor);
1786 tegra_sor_power_lanes(sor, 4, true);
1788 tegra_dc_sor_set_internal_panel(sor, false);
1789 tegra_hdmi_config(hdmi);
1790 tegra_hdmi_avi_infoframe(hdmi);
1791 tegra_hdmi_vendor_infoframe(hdmi);
1793 tegra_sor_pad_cal_power(sor, true);
1794 tegra_hdmi_config_tmds(hdmi);
1795 tegra_sor_pad_cal_power(sor, false);
1797 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_BRICK_CLK);
1798 tegra_dc_sor_attach(sor);
1799 tegra_nvhdcp_set_plug(hdmi->nvhdcp, true);
1801 tegra_dc_setup_clk(dc, dc->clk);
1802 tegra_dc_hdmi_setup_clk(dc, hdmi->sor->sor_clk);
1803 tegra_hdmi_config(hdmi);
1805 tegra_sor_config_xbar(hdmi->sor);
1807 /* TODO: Confirm sequence with HW */
1808 tegra_sor_writel(sor, NV_SOR_SEQ_INST(0), 0x8080);
1809 tegra_sor_writel(sor, NV_SOR_PWR, 0x80000001);
1811 if (hdmi->dc->mode.pclk > 340000000) {
1812 tegra_hdmi_v2_x_config(hdmi);
1813 schedule_delayed_work(&hdmi->scdc_work,
1814 msecs_to_jiffies(HDMI_SCDC_MONITOR_TIMEOUT_MS));
1817 tegra_hdmi_gcp(hdmi);
1823 static void tegra_dc_hdmi_enable(struct tegra_dc *dc)
1825 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1830 tegra_hdmi_controller_enable(hdmi);
1832 hdmi->enabled = true;
1833 tegra_hda_set_data(hdmi, SINK_HDMI);
1834 #ifdef CONFIG_SWITCH
1836 switch_set_state(&hdmi->audio_switch, 1);
1840 static inline u32 tegra_hdmi_get_shift_clk_div(struct tegra_hdmi *hdmi)
1843 * HW does not support deep color yet
1850 static void tegra_hdmi_config_clk(struct tegra_hdmi *hdmi, u32 clk_type)
1852 if (clk_type == hdmi->clk_type)
1855 if (tegra_platform_is_linsim())
1858 if (clk_type == TEGRA_HDMI_BRICK_CLK) {
1860 struct tegra_dc_sor_data *sor = hdmi->sor;
1861 int div = hdmi->dc->mode.pclk < 340000000 ? 1 : 2;
1862 unsigned long rate = clk_get_rate(sor->src_switch_clk);
1863 unsigned long parent_rate =
1864 clk_get_rate(clk_get_parent(sor->src_switch_clk));
1866 /* Set sor divider */
1867 if (rate != DIV_ROUND_UP(parent_rate, div)) {
1868 rate = DIV_ROUND_UP(parent_rate, div);
1869 clk_set_rate(sor->src_switch_clk, rate);
1872 /* Select brick muxes */
1873 val = (hdmi->dc->mode.pclk < 340000000) ?
1874 NV_SOR_CLK_CNTRL_DP_LINK_SPEED_G2_7 :
1875 NV_SOR_CLK_CNTRL_DP_LINK_SPEED_G5_4;
1877 val |= NV_SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
1878 tegra_sor_writel(hdmi->sor, NV_SOR_CLK_CNTRL, val);
1879 usleep_range(250, 300); /* sor brick pll stabilization delay */
1882 * Report brick configuration and rate, so that SOR clock tree
1883 * is properly updated. No h/w changes by clock api calls below,
1884 * just sync s/w state with brick h/w.
1886 rate = rate/NV_SOR_HDMI_BRICK_DIV*NV_SOR_HDMI_BRICK_MUL(val);
1887 if (clk_get_parent(sor->brick_clk) != sor->src_switch_clk)
1888 clk_set_parent(sor->brick_clk, sor->src_switch_clk);
1889 clk_set_rate(sor->brick_clk, rate);
1892 * Select primary -- HDMI -- DVFS table for SOR clock (if SOR
1893 * clock has single DVFS table for all modes, nothing changes).
1895 tegra_dvfs_use_alt_freqs_on_clk(sor->sor_clk, false);
1897 /* Select sor clock muxes */
1898 tegra_clk_cfg_ex(sor->sor_clk, TEGRA_CLK_SOR_CLK_SEL, 3);
1900 tegra_dc_writel(hdmi->dc, PIXEL_CLK_DIVIDER_PCD1 |
1901 SHIFT_CLK_DIVIDER(tegra_hdmi_get_shift_clk_div(hdmi)),
1902 DC_DISP_DISP_CLOCK_CONTROL);
1904 hdmi->clk_type = TEGRA_HDMI_BRICK_CLK;
1905 } else if (clk_type == TEGRA_HDMI_SAFE_CLK) {
1906 if (!hdmi->dc->initialized) {
1907 /* Select sor clock muxes */
1908 tegra_clk_cfg_ex(hdmi->sor->sor_clk,
1909 TEGRA_CLK_SOR_CLK_SEL, 0);
1910 hdmi->clk_type = TEGRA_HDMI_SAFE_CLK;
1913 dev_err(&hdmi->dc->ndev->dev, "hdmi: incorrect clk type configured\n");
1917 /* returns exact pixel clock in Hz */
1918 static long tegra_hdmi_get_pclk(struct tegra_dc_mode *mode)
1920 long h_total, v_total;
1922 h_total = mode->h_active + mode->h_front_porch + mode->h_back_porch +
1924 v_total = mode->v_active + mode->v_front_porch + mode->v_back_porch +
1926 refresh = tegra_dc_calc_refresh(mode);
1927 refresh = DIV_ROUND_CLOSEST(refresh, 1000);
1929 if (mode->vmode & FB_VMODE_1000DIV1001) {
1930 refresh = refresh * 1000 * 1000 / 1001;
1931 refresh = DIV_ROUND_CLOSEST(refresh, 10);
1932 refresh = refresh * 10;
1933 pclk = h_total * v_total * refresh / 1000;
1934 pclk = DIV_ROUND_CLOSEST(pclk, 10000) * 10000;
1936 pclk = h_total * v_total * refresh;
1942 static long tegra_dc_hdmi_setup_clk(struct tegra_dc *dc, struct clk *clk)
1944 #ifdef CONFIG_TEGRA_NVDISPLAY
1945 struct clk *parent_clk = tegra_disp_clk_get(&dc->ndev->dev,
1946 dc->out->parent_clk ? : "plld2");
1948 struct clk *parent_clk = clk_get(NULL,
1949 dc->out->parent_clk ? : "pll_d2");
1952 dc->mode.pclk = tegra_hdmi_get_pclk(&dc->mode);
1954 if (IS_ERR_OR_NULL(parent_clk)) {
1955 dev_err(&dc->ndev->dev, "hdmi: parent clk get failed\n");
1959 if (!tegra_platform_is_silicon())
1960 return dc->mode.pclk;
1962 #ifdef CONFIG_TEGRA_NVDISPLAY
1963 if (clk_get_parent(clk) != parent_clk)
1964 clk_set_parent(clk, parent_clk);
1966 if (clk == dc->clk) {
1967 if (clk_get_parent(clk) != parent_clk) {
1968 if (clk_set_parent(clk, parent_clk)) {
1969 dev_err(&dc->ndev->dev,
1970 "hdmi: set dc parent failed\n");
1975 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1976 struct tegra_dc_sor_data *sor = hdmi->sor;
1978 if (clk_get_parent(sor->src_switch_clk) != parent_clk) {
1979 if (clk_set_parent(sor->src_switch_clk, parent_clk)) {
1980 dev_err(&dc->ndev->dev,
1981 "hdmi: set src switch parent failed\n");
1987 if (dc->initialized)
1989 if (clk_get_rate(parent_clk) != dc->mode.pclk)
1990 clk_set_rate(parent_clk, dc->mode.pclk);
1993 * DC clock divider is controlled by DC driver transparently to clock
1994 * framework -- hence, direct call to DVFS with target mode rate. SOR
1995 * clock rate in clock tree is properly updated, and can be used for
1998 * TODO: tegra_hdmi_controller_enable() procedure 1st configures SOR
1999 * clock via tegra_hdmi_config_clk(), and then calls this function
2000 * that may re-lock parent PLL. That needs to be double-checked:
2001 * in general re-locking PLL while the downstream module is already
2002 * sourced from it is not recommended. If/when the order of enabling
2003 * HDMI controller is changed, we can remove direct DVFS call for SOR
2004 * (but for DC it should be kept, anyway).
2007 tegra_dvfs_set_rate(clk, dc->mode.pclk);
2009 tegra_dvfs_set_rate(clk, clk_get_rate(clk));
2011 return tegra_dc_pclk_round_rate(dc, dc->mode.pclk);
2014 static void tegra_dc_hdmi_shutdown(struct tegra_dc *dc)
2016 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2018 _tegra_hdmivrr_activate(hdmi, false);
2019 hdmi->device_shutdown = true;
2020 tegra_nvhdcp_shutdown(hdmi->nvhdcp);
2025 static void tegra_dc_hdmi_disable(struct tegra_dc *dc)
2027 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2029 _tegra_hdmivrr_activate(hdmi, false);
2030 hdmi->enabled = false;
2031 #ifdef CONFIG_SWITCH
2032 switch_set_state(&hdmi->audio_switch, 0);
2035 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_SAFE_CLK);
2036 tegra_hdmi_controller_disable(hdmi);
2037 tegra_hda_reset_data();
2041 static bool tegra_dc_hdmi_detect(struct tegra_dc *dc)
2043 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2044 unsigned long delay = msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS);
2046 if (tegra_platform_is_linsim())
2049 if (dc->out->hotplug_state != TEGRA_HPD_STATE_NORMAL)
2052 cancel_delayed_work(&hdmi->hpd_worker);
2053 schedule_delayed_work(&hdmi->hpd_worker, delay);
2055 return tegra_dc_hpd(dc);
2058 static void tegra_dc_hdmi_suspend(struct tegra_dc *dc)
2060 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2062 _tegra_hdmivrr_activate(hdmi, false);
2064 if (dc->out->flags & TEGRA_DC_OUT_HOTPLUG_WAKE_LP0) {
2065 int wake_irq = gpio_to_irq(dc->out->hotplug_gpio);
2068 ret = enable_irq_wake(wake_irq);
2070 dev_err(&dc->ndev->dev,
2071 "%s: Couldn't enable HDMI wakeup, irq=%d, error=%d\n",
2072 __func__, wake_irq, ret);
2076 atomic_set(&hdmi->suspended, 1);
2079 static void tegra_dc_hdmi_resume(struct tegra_dc *dc)
2081 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2083 atomic_set(&hdmi->suspended, 0);
2085 if (dc->out->flags & TEGRA_DC_OUT_HOTPLUG_WAKE_LP0)
2086 disable_irq_wake(gpio_to_irq(dc->out->hotplug_gpio));
2088 cancel_delayed_work(&hdmi->hpd_worker);
2090 /* If resume happens with a non-VRR monitor, the HPD
2091 worker will correct the mode based on the new EDID */
2092 _tegra_hdmivrr_activate(hdmi, true);
2094 schedule_delayed_work(&hdmi->hpd_worker,
2095 msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS + HDMI_HPD_DROP_TIMEOUT_MS));
2098 static int tegra_dc_hdmi_set_hdr(struct tegra_dc *dc)
2101 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2102 ret = tegra_edid_get_ex_hdr_cap(hdmi->edid);
2103 if (ret & FB_CAP_HDR)
2104 tegra_hdmi_hdr_infoframe(hdmi);
2108 static int tegra_dc_hdmi_ddc_enable(struct tegra_dc *dc)
2110 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2111 _tegra_hdmi_ddc_enable(hdmi);
2115 static int tegra_dc_hdmi_ddc_disable(struct tegra_dc *dc)
2117 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2118 _tegra_hdmi_ddc_disable(hdmi);
2122 static void tegra_dc_hdmi_modeset_notifier(struct tegra_dc *dc)
2124 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2127 tegra_dc_io_start(dc);
2129 /* disable hdmi2.x config on host and monitor */
2130 if (dc->mode.pclk > 340000000) {
2131 if (tegra_edid_is_scdc_present(dc->edid))
2132 tegra_hdmi_v2_x_mon_config(hdmi, true);
2133 tegra_hdmi_v2_x_host_config(hdmi, true);
2135 if (tegra_edid_is_scdc_present(dc->edid))
2136 tegra_hdmi_v2_x_mon_config(hdmi, false);
2137 tegra_hdmi_v2_x_host_config(hdmi, false);
2140 tegra_dc_io_end(dc);
2144 #ifdef CONFIG_DEBUG_FS
2145 /* show current hpd state */
2146 static int tegra_hdmi_hotplug_dbg_show(struct seq_file *m, void *unused)
2148 struct tegra_hdmi *hdmi = m->private;
2149 struct tegra_dc *dc = hdmi->dc;
2151 if (WARN_ON(!hdmi || !dc || !dc->out))
2155 seq_printf(m, "hdmi hpd state: %d\n", dc->out->hotplug_state);
2160 int tegra_hdmi_get_hotplug_state(struct tegra_hdmi *hdmi)
2163 return hdmi->dc->out->hotplug_state;
2166 void tegra_hdmi_set_hotplug_state(struct tegra_hdmi *hdmi, int new_hpd_state)
2168 struct tegra_dc *dc = hdmi->dc;
2172 hotplug_state = dc->out->hotplug_state;
2174 if (hotplug_state == TEGRA_HPD_STATE_NORMAL &&
2175 new_hpd_state != TEGRA_HPD_STATE_NORMAL &&
2176 tegra_dc_hotplug_supported(dc)) {
2177 disable_irq(gpio_to_irq(dc->out->hotplug_gpio));
2178 } else if (hotplug_state != TEGRA_HPD_STATE_NORMAL &&
2179 new_hpd_state == TEGRA_HPD_STATE_NORMAL &&
2180 tegra_dc_hotplug_supported(dc)) {
2181 enable_irq(gpio_to_irq(dc->out->hotplug_gpio));
2184 dc->out->hotplug_state = new_hpd_state;
2188 * sw controlled plug/unplug.
2189 * wait for any already executing hpd worker thread.
2190 * No debounce delay, schedule immedately
2192 cancel_delayed_work_sync(&hdmi->hpd_worker);
2193 schedule_delayed_work(&hdmi->hpd_worker, 0);
2197 * sw control for hpd.
2198 * 0 is normal state, hw drives hpd.
2199 * -1 is force deassert, sw drives hpd.
2200 * 1 is force assert, sw drives hpd.
2201 * before releasing to hw, sw must ensure hpd state is normal i.e. 0
2203 static ssize_t tegra_hdmi_hotplug_dbg_write(struct file *file,
2204 const char __user *addr,
2205 size_t len, loff_t *pos)
2207 struct seq_file *m = file->private_data;
2208 struct tegra_hdmi *hdmi = m->private;
2209 struct tegra_dc *dc = hdmi->dc;
2213 if (WARN_ON(!hdmi || !dc || !dc->out))
2216 ret = kstrtol_from_user(addr, len, 10, &new_hpd_state);
2220 tegra_hdmi_set_hotplug_state(hdmi, new_hpd_state);
2225 static int tegra_hdmi_hotplug_dbg_open(struct inode *inode, struct file *file)
2227 return single_open(file, tegra_hdmi_hotplug_dbg_show, inode->i_private);
2230 static const struct file_operations tegra_hdmi_hotplug_dbg_ops = {
2231 .open = tegra_hdmi_hotplug_dbg_open,
2233 .write = tegra_hdmi_hotplug_dbg_write,
2234 .llseek = seq_lseek,
2235 .release = single_release,
2238 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi)
2240 struct dentry *dir, *ret;
2242 dir = debugfs_create_dir("tegra_hdmi", NULL);
2243 if (IS_ERR_OR_NULL(dir))
2246 ret = debugfs_create_file("hotplug", S_IRUGO, dir,
2247 hdmi, &tegra_hdmi_hotplug_dbg_ops);
2248 if (IS_ERR_OR_NULL(ret))
2253 debugfs_remove_recursive(dir);
2257 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi)
2263 static ssize_t hdmi_ddc_power_toggle(struct kobject *kobj,
2264 struct kobj_attribute *attr, const char *buf, size_t count)
2267 sscanf(buf, "%du", &value);
2268 tegra_hdmi_ddc_power_toggle(value);
2272 static ssize_t hdmi_ddc_power_show(struct kobject *kobj,
2273 struct kobj_attribute *attr, char *buf)
2275 return sprintf(buf, "%d\n", dc_hdmi->ddc_refcount);
2278 static bool tegra_dc_hdmi_hpd_state(struct tegra_dc *dc)
2284 if (WARN_ON(!dc || !dc->out))
2287 level = gpio_get_value_cansleep(dc->out->hotplug_gpio);
2289 sense = dc->out->flags & TEGRA_DC_OUT_HOTPLUG_MASK;
2291 hpd = (sense == TEGRA_DC_OUT_HOTPLUG_HIGH && level) ||
2292 (sense == TEGRA_DC_OUT_HOTPLUG_LOW && !level);
2297 static void tegra_dc_hdmi_vrr_enable(struct tegra_dc *dc, bool enable)
2299 struct tegra_vrr *vrr = dc->out->vrr;
2304 if (!(dc->mode.vmode & FB_VMODE_VRR)) {
2305 WARN(enable, "VRR enable request in non-VRR mode\n");
2309 vrr->enable = enable;
2312 static void tegra_dc_hdmi_postpoweron(struct tegra_dc *dc)
2314 _tegra_hdmivrr_activate(tegra_dc_get_outdata(dc), true);
2317 struct tegra_dc_out_ops tegra_dc_hdmi2_0_ops = {
2318 .init = tegra_dc_hdmi_init,
2319 .destroy = tegra_dc_hdmi_destroy,
2320 .enable = tegra_dc_hdmi_enable,
2321 .disable = tegra_dc_hdmi_disable,
2322 .setup_clk = tegra_dc_hdmi_setup_clk,
2323 .detect = tegra_dc_hdmi_detect,
2324 .shutdown = tegra_dc_hdmi_shutdown,
2325 .suspend = tegra_dc_hdmi_suspend,
2326 .resume = tegra_dc_hdmi_resume,
2327 .ddc_enable = tegra_dc_hdmi_ddc_enable,
2328 .ddc_disable = tegra_dc_hdmi_ddc_disable,
2329 .modeset_notifier = tegra_dc_hdmi_modeset_notifier,
2330 .mode_filter = tegra_hdmi_fb_mode_filter,
2331 .hpd_state = tegra_dc_hdmi_hpd_state,
2332 .vrr_enable = tegra_dc_hdmi_vrr_enable,
2333 .vrr_update_monspecs = tegra_hdmivrr_update_monspecs,
2334 .set_hdr = tegra_dc_hdmi_set_hdr,
2335 .postpoweron = tegra_dc_hdmi_postpoweron,