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>
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 /* Possibly should be moved to hdmi_common.h */
69 static struct fb_videomode tegra_dc_vga_mode = {
73 .pixclock = KHZ2PICOS(25200),
74 .hsync_len = 96, /* h_sync_width */
75 .vsync_len = 2, /* v_sync_width */
76 .left_margin = 48, /* h_back_porch */
77 .upper_margin = 33, /* v_back_porch */
78 .right_margin = 16, /* h_front_porch */
79 .lower_margin = 10, /* v_front_porch */
84 static ssize_t hdmi_ddc_power_toggle(struct kobject *kobj,
85 struct kobj_attribute *attr, const char *buf, size_t count);
87 static ssize_t hdmi_ddc_power_show(struct kobject *kobj,
88 struct kobj_attribute *attr, char *buf);
90 static struct kobj_attribute hdmi_ddc_power_config =
91 __ATTR(config, 0640, hdmi_ddc_power_show, hdmi_ddc_power_toggle);
93 static struct kobject *hdmi_ddc;
95 struct tmds_prod_pair {
100 static struct tmds_prod_pair tmds_config_modes[] = {
111 .name = "prod_c_150M"
115 .name = "prod_c_300M"
119 .name = "prod_c_600M"
123 static struct tegra_hdmi *dc_hdmi;
125 static int tegra_hdmi_controller_enable(struct tegra_hdmi *hdmi);
126 static void tegra_hdmi_config_clk(struct tegra_hdmi *hdmi, u32 clk_type);
127 static long tegra_dc_hdmi_setup_clk(struct tegra_dc *dc, struct clk *clk);
128 static void tegra_hdmi_scdc_worker(struct work_struct *work);
129 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi);
131 static inline bool tegra_hdmi_is_connected(struct tegra_hdmi *hdmi)
133 return (hdmi->mon_spec.misc & FB_MISC_HDMI) ||
134 (hdmi->mon_spec.misc & FB_MISC_HDMI_FORUM);
137 static inline void tegra_hdmi_irq_enable(struct tegra_hdmi *hdmi)
139 if (tegra_platform_is_fpga())
142 enable_irq(hdmi->irq);
145 static inline void tegra_hdmi_irq_disable(struct tegra_hdmi *hdmi)
147 if (tegra_platform_is_fpga())
150 disable_irq(hdmi->irq);
153 static inline bool tegra_hdmi_hpd_asserted(struct tegra_hdmi *hdmi)
155 return tegra_dc_hpd(hdmi->dc);
158 static inline void tegra_hdmi_reset(struct tegra_hdmi *hdmi)
160 if (tegra_platform_is_linsim())
163 tegra_periph_reset_assert(hdmi->sor->sor_clk);
165 tegra_periph_reset_deassert(hdmi->sor->sor_clk);
169 static inline void _tegra_hdmi_ddc_enable(struct tegra_hdmi *hdmi)
171 mutex_lock(&hdmi->ddc_refcount_lock);
172 if (hdmi->ddc_refcount++)
174 tegra_hdmi_get(hdmi->dc);
176 * hdmi uses i2c lane muxed on dpaux1 pad.
177 * Enable dpaux1 pads and configure the mux.
179 tegra_dpaux_config_pad_mode(hdmi->dc, TEGRA_DPAUX_INSTANCE_1,
180 TEGRA_DPAUX_PAD_MODE_I2C);
183 mutex_unlock(&hdmi->ddc_refcount_lock);
186 static inline void _tegra_hdmi_ddc_disable(struct tegra_hdmi *hdmi)
188 mutex_lock(&hdmi->ddc_refcount_lock);
190 if (WARN_ONCE(hdmi->ddc_refcount <= 0, "ddc refcount imbalance"))
192 if (--hdmi->ddc_refcount != 0)
196 * hdmi uses i2c lane muxed on dpaux1 pad.
197 * Disable dpaux1 pads.
199 tegra_dpaux_pad_power(hdmi->dc, TEGRA_DPAUX_INSTANCE_1, false);
200 tegra_hdmi_put(hdmi->dc);
203 mutex_unlock(&hdmi->ddc_refcount_lock);
206 static int tegra_hdmi_ddc_i2c_xfer(struct tegra_dc *dc,
207 struct i2c_msg *msgs, int num)
209 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
212 _tegra_hdmi_ddc_enable(hdmi);
213 ret = i2c_transfer(hdmi->ddc_i2c_client->adapter, msgs, num);
214 _tegra_hdmi_ddc_disable(hdmi);
218 static int tegra_hdmi_ddc_init(struct tegra_hdmi *hdmi, int edid_src)
220 struct tegra_dc *dc = hdmi->dc;
221 struct i2c_adapter *i2c_adap;
223 struct i2c_board_info i2c_dev_info = {
224 .type = "tegra_hdmi2.0",
228 hdmi->edid = tegra_edid_create(dc, tegra_hdmi_ddc_i2c_xfer);
229 else if (edid_src == 1)
230 hdmi->edid = tegra_edid_create(dc, tegra_dc_edid_blob);
231 if (IS_ERR_OR_NULL(hdmi->edid)) {
232 dev_err(&dc->ndev->dev, "hdmi: can't create edid\n");
233 return PTR_ERR(hdmi->edid);
235 tegra_dc_set_edid(dc, hdmi->edid);
237 i2c_adap = i2c_get_adapter(dc->out->ddc_bus);
239 dev_err(&dc->ndev->dev,
240 "hdmi: can't get adpater for ddc bus %d\n",
245 hdmi->ddc_i2c_original_rate = i2c_get_adapter_bus_clk_rate(i2c_adap);
247 hdmi->ddc_i2c_client = i2c_new_device(i2c_adap, &i2c_dev_info);
248 i2c_put_adapter(i2c_adap);
249 if (!hdmi->ddc_i2c_client) {
250 dev_err(&dc->ndev->dev, "hdmi: can't create new i2c device\n");
257 tegra_edid_destroy(hdmi->edid);
261 static int tegra_hdmi_scdc_i2c_xfer(struct tegra_dc *dc,
262 struct i2c_msg *msgs, int num)
264 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
266 return i2c_transfer(hdmi->scdc_i2c_client->adapter, msgs, num);
269 static int tegra_hdmi_scdc_init(struct tegra_hdmi *hdmi)
271 struct tegra_dc *dc = hdmi->dc;
272 struct i2c_adapter *i2c_adap;
274 struct i2c_board_info i2c_dev_info = {
275 .type = "tegra_hdmi_scdc",
279 i2c_adap = i2c_get_adapter(dc->out->ddc_bus);
281 dev_err(&dc->ndev->dev,
282 "hdmi: can't get adpater for scdc bus %d\n",
288 hdmi->scdc_i2c_client = i2c_new_device(i2c_adap, &i2c_dev_info);
289 i2c_put_adapter(i2c_adap);
290 if (!hdmi->scdc_i2c_client) {
291 dev_err(&dc->ndev->dev,
292 "hdmi: can't create scdc i2c device\n");
297 INIT_DELAYED_WORK(&hdmi->scdc_work, tegra_hdmi_scdc_worker);
304 static bool tegra_hdmi_check_dc_constraint(const struct fb_videomode *mode)
306 return (mode->hsync_len >= 1) && (mode->vsync_len >= 1) &&
307 (mode->lower_margin + mode->vsync_len +
308 mode->upper_margin > 1) &&
309 (mode->xres >= 16) && (mode->yres >= 16);
312 /* does not return precise tmds character rate */
313 static u32 tegra_hdmi_mode_min_tmds_rate(const struct fb_videomode *mode)
315 u32 tmds_csc_8bpc_khz = PICOS2KHZ(mode->pixclock);
317 if (mode->vmode & (FB_VMODE_Y420 | FB_VMODE_Y420_ONLY))
318 tmds_csc_8bpc_khz /= 2;
320 return tmds_csc_8bpc_khz;
324 static bool tegra_hdmi_fb_mode_filter(const struct tegra_dc *dc,
325 struct fb_videomode *mode)
327 struct tegra_hdmi *hdmi = dc->out_data;
332 if (mode->xres > 4096)
335 /* some non-compliant edids list 420vdb modes in vdb */
336 if ((mode->vmode & FB_VMODE_Y420) &&
337 !(tegra_edid_is_hfvsdb_present(hdmi->edid) &&
338 tegra_edid_is_scdc_present(hdmi->edid)) &&
339 tegra_edid_is_420db_present(hdmi->edid)) {
340 mode->vmode &= ~FB_VMODE_Y420;
341 mode->vmode |= FB_VMODE_Y420_ONLY;
345 * There are currently many TVs in the market that actually do NOT support
346 * 4k@60fps 4:4:4 (594 MHz), (especially on the HDCP 2.2 ports), but
347 * advertise it in the DTD block in their EDIDs. The workaround for this port
348 * is to disable the 594 MHz mode if no HF-VSDB is present or if no SCDC
349 * support is indicated
351 if (tegra_hdmi_mode_min_tmds_rate(mode) / 1000 >= 340 &&
352 (!tegra_edid_is_hfvsdb_present(hdmi->edid) ||
353 !tegra_edid_is_scdc_present(hdmi->edid)))
357 * Check if mode's pixel clock requirement can be satisfied. Note that
358 * the pixclock value is in pico seconds.
360 if (mode->pixclock && tegra_dc_get_out_max_pixclock(dc) &&
361 mode->pixclock < tegra_dc_get_out_max_pixclock(dc))
365 * Work around for modes that fail the constraint:
366 * V_FRONT_PORCH >= V_REF_TO_SYNC + 1
368 if (mode->lower_margin == 1) {
369 mode->lower_margin++;
370 mode->upper_margin--;
373 if (!tegra_hdmi_check_dc_constraint(mode))
379 static void tegra_hdmi_ddc_power_toggle(int value)
385 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
386 partition_id = tegra_pd_get_powergate_id(tegra_sor_pd);
387 if (partition_id < 0)
390 partition_id = TEGRA_POWERGATE_SOR;
394 _tegra_hdmi_ddc_disable(dc_hdmi);
395 tegra_powergate_partition(partition_id);
396 } else if (value == 1) {
397 tegra_unpowergate_partition(partition_id);
398 _tegra_hdmi_ddc_enable(dc_hdmi);
404 static int tegra_hdmi_get_mon_spec(struct tegra_hdmi *hdmi)
406 #define MAX_RETRY 100
407 #define MIN_RETRY_DELAY_US 200
408 #define MAX_RETRY_DELAY_US (MIN_RETRY_DELAY_US + 200)
410 size_t attempt_cnt = 0;
412 struct i2c_adapter *i2c_adap = i2c_get_adapter(hdmi->dc->out->ddc_bus);
414 if (IS_ERR_OR_NULL(hdmi->edid)) {
415 dev_err(&hdmi->dc->ndev->dev, "hdmi: edid not initialized\n");
416 return PTR_ERR(hdmi->edid);
419 tegra_edid_i2c_adap_change_rate(i2c_adap, hdmi->ddc_i2c_original_rate);
421 hdmi->mon_spec_valid = false;
422 if (hdmi->mon_spec_valid)
423 fb_destroy_modedb(hdmi->mon_spec.modedb);
424 memset(&hdmi->mon_spec, 0, sizeof(hdmi->mon_spec));
427 err = tegra_edid_get_monspecs(hdmi->edid, &hdmi->mon_spec);
429 usleep_range(MIN_RETRY_DELAY_US, MAX_RETRY_DELAY_US);
432 } while (++attempt_cnt < MAX_RETRY);
435 dev_err(&hdmi->dc->ndev->dev, "hdmi: edid read failed\n");
436 /* Try to load and parse the fallback edid */
437 hdmi->edid->errors = EDID_ERRORS_READ_FAILED;
438 err = tegra_edid_get_monspecs(hdmi->edid, &hdmi->mon_spec);
440 dev_err(&hdmi->dc->ndev->dev,
441 "hdmi: parsing fallback edid failed\n");
444 dev_err(&hdmi->dc->ndev->dev,
445 "hdmi: using fallback edid\n");
449 hdmi->mon_spec_valid = true;
452 #undef MAX_RETRY_DELAY_US
453 #undef MIN_RETRY_DELAY_US
457 static inline int tegra_hdmi_edid_read(struct tegra_hdmi *hdmi)
461 err = tegra_hdmi_get_mon_spec(hdmi);
466 static int tegra_hdmi_get_eld(struct tegra_hdmi *hdmi)
470 hdmi->eld_valid = false;
471 memset(&hdmi->eld, 0, sizeof(hdmi->eld));
473 err = tegra_edid_get_eld(hdmi->edid, &hdmi->eld);
475 dev_err(&hdmi->dc->ndev->dev, "hdmi: eld not available\n");
479 hdmi->eld_valid = true;
483 static inline int tegra_hdmi_eld_read(struct tegra_hdmi *hdmi)
485 return tegra_hdmi_get_eld(hdmi);
488 static void tegra_hdmi_edid_config(struct tegra_hdmi *hdmi)
490 #define CM_TO_MM(x) (x * 10)
492 struct tegra_dc *dc = hdmi->dc;
494 if (!hdmi->mon_spec_valid)
497 dc->out->h_size = CM_TO_MM(hdmi->mon_spec.max_x);
498 dc->out->v_size = CM_TO_MM(hdmi->mon_spec.max_y);
500 hdmi->dvi = !tegra_hdmi_is_connected(hdmi);
504 static void tegra_hdmi_hotplug_notify(struct tegra_hdmi *hdmi,
507 struct tegra_dc *dc = hdmi->dc;
508 struct fb_monspecs *mon_spec;
511 mon_spec = &hdmi->mon_spec;
515 #ifdef CONFIG_ADF_TEGRA
517 tegra_adf_process_hotplug_connected(hdmi->dc->adf, mon_spec);
520 tegra_fb_update_monspecs(hdmi->dc->fb, mon_spec,
521 tegra_hdmi_fb_mode_filter,
522 tegra_hdmi_update_vrr_mode);
523 tegra_fb_update_fix(hdmi->dc->fb, mon_spec);
527 dc->connected = is_asserted;
528 tegra_dc_ext_process_hotplug(dc->ndev->id, is_asserted);
531 switch_set_state(&hdmi->hpd_switch, is_asserted ? 1 : 0);
535 static int tegra_hdmi_edid_eld_setup(struct tegra_hdmi *hdmi)
539 tegra_dc_unpowergate_locked(hdmi->dc);
541 err = tegra_hdmi_edid_read(hdmi);
545 err = tegra_hdmi_eld_read(hdmi);
549 err = tegra_hdmivrr_setup(hdmi);
551 dev_info(&hdmi->dc->ndev->dev, "vrr_setup failed\n");
553 tegra_dc_powergate_locked(hdmi->dc);
555 tegra_hdmi_edid_config(hdmi);
558 * eld is configured when audio needs it
559 * via tegra_hdmi_edid_config()
562 tegra_hdmi_hotplug_notify(hdmi, true);
565 tegra_dc_powergate_locked(hdmi->dc);
569 static int tegra_hdmi_controller_disable(struct tegra_hdmi *hdmi)
571 struct tegra_dc_sor_data *sor = hdmi->sor;
572 struct tegra_dc *dc = hdmi->dc;
576 tegra_nvhdcp_set_plug(hdmi->nvhdcp, 0);
577 tegra_dc_sor_detach(sor);
578 tegra_sor_power_lanes(sor, 4, false);
579 tegra_sor_hdmi_pad_power_down(sor);
580 tegra_hdmi_reset(hdmi);
588 static int tegra_hdmi_disable(struct tegra_hdmi *hdmi)
590 struct tegra_dc *dc = hdmi->dc;
592 if (!hdmi->enabled) {
593 dc->connected = false;
594 tegra_dc_ext_process_hotplug(dc->ndev->id, false);
596 switch_set_state(&hdmi->hpd_switch, 0);
601 hdmi->enabled = false;
602 hdmi->eld_valid = false;
603 hdmi->mon_spec_valid = false;
605 tegra_dc_disable(hdmi->dc);
607 tegra_hdmi_hotplug_notify(hdmi, false);
612 static int (*tegra_hdmi_state_func[])(struct tegra_hdmi *) = {
614 tegra_hdmi_edid_eld_setup,
617 enum tegra_hdmi_plug_states {
618 TEGRA_HDMI_MONITOR_DISABLE,
619 TEGRA_HDMI_MONITOR_ENABLE,
622 static int read_edid_into_buffer(struct tegra_hdmi *hdmi,
623 u8 *edid_data, size_t edid_data_len)
626 int extension_blocks;
627 int max_ext_blocks = (edid_data_len / 128) - 1;
629 err = tegra_edid_read_block(hdmi->edid, 0, edid_data);
631 dev_info(&hdmi->dc->ndev->dev, "hdmi: tegra_edid_read_block(0) returned err %d\n",
635 extension_blocks = edid_data[0x7e];
636 dev_info(&hdmi->dc->ndev->dev, "%s: extension_blocks = %d, max_ext_blocks = %d\n",
637 __func__, extension_blocks, max_ext_blocks);
638 if (extension_blocks > max_ext_blocks)
639 extension_blocks = max_ext_blocks;
640 for (i = 1; i <= extension_blocks; i++) {
641 err = tegra_edid_read_block(hdmi->edid, i, edid_data + i * 128);
643 dev_info(&hdmi->dc->ndev->dev, "hdmi: tegra_edid_read_block(%d) returned err %d\n",
651 static int hdmi_recheck_edid(struct tegra_hdmi *hdmi, int *match)
654 u8 tmp[HDMI_EDID_MAX_LENGTH] = {0};
655 ret = read_edid_into_buffer(hdmi, tmp, sizeof(tmp));
656 dev_info(&hdmi->dc->ndev->dev, "%s: read_edid_into_buffer() returned %d\n",
659 struct tegra_dc_edid *data = tegra_edid_get_data(hdmi->edid);
660 dev_info(&hdmi->dc->ndev->dev, "old edid len = %ld\n",
661 (long int)data->len);
662 *match = ((ret == data->len) &&
663 !memcmp(tmp, data->buf, data->len));
665 print_hex_dump(KERN_INFO, "tmp :", DUMP_PREFIX_ADDRESS,
666 16, 4, tmp, ret, true);
667 print_hex_dump(KERN_INFO, "data:", DUMP_PREFIX_ADDRESS,
668 16, 4, data->buf, data->len, true);
670 tegra_edid_put_data(data);
676 static void tegra_hdmi_hpd_worker(struct work_struct *work)
678 struct tegra_hdmi *hdmi = container_of(to_delayed_work(work),
679 struct tegra_hdmi, hpd_worker);
682 enum tegra_hdmi_plug_states orig_state;
685 mutex_lock(&hdmi->hpd_lock);
687 connected = tegra_dc_hpd(hdmi->dc);
688 orig_state = hdmi->plug_state;
690 if ((connected && orig_state == TEGRA_HDMI_MONITOR_ENABLE)) {
691 if (hdmi_recheck_edid(hdmi, &match)) {
692 dev_info(&hdmi->dc->ndev->dev, "hdmi: unable to read EDID\n");
696 dev_info(&hdmi->dc->ndev->dev, "hdmi: No EDID change after HPD bounce, taking no action.\n");
699 dev_info(&hdmi->dc->ndev->dev, "hdmi: EDID change after HPD bounce, resetting\n");
703 if ((!connected && orig_state == TEGRA_HDMI_MONITOR_DISABLE)) {
704 dev_info(&hdmi->dc->ndev->dev, "hdmi: spurious interrupt\n");
705 mutex_unlock(&hdmi->hpd_lock);
710 hdmi->plug_state = TEGRA_HDMI_MONITOR_ENABLE;
712 hdmi->plug_state = TEGRA_HDMI_MONITOR_DISABLE;
714 err = tegra_hdmi_state_func[hdmi->plug_state](hdmi);
717 dev_info(&hdmi->dc->ndev->dev,
718 "hdmi state %d failed during %splug\n",
719 hdmi->plug_state, connected ? "" : "un");
720 hdmi->plug_state = orig_state;
722 dev_info(&hdmi->dc->ndev->dev, "hdmi: %splugged\n",
723 connected ? "" : "un");
727 mutex_unlock(&hdmi->hpd_lock);
731 static irqreturn_t tegra_hdmi_hpd_irq_handler(int irq, void *ptr)
733 struct tegra_dc *dc = ptr;
734 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
736 if (atomic_read(&hdmi->suspended))
739 cancel_delayed_work(&hdmi->hpd_worker);
740 schedule_delayed_work(&hdmi->hpd_worker,
741 msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS));
746 static int tegra_hdmi_hpd_init(struct tegra_hdmi *hdmi)
748 struct tegra_dc *dc = hdmi->dc;
749 int hotplug_gpio = dc->out->hotplug_gpio;
753 if (!gpio_is_valid(hotplug_gpio)) {
754 dev_err(&dc->ndev->dev, "hdmi: invalid hotplug gpio\n");
758 hotplug_irq = gpio_to_irq(hotplug_gpio);
759 if (hotplug_irq < 0) {
760 dev_err(&dc->ndev->dev,
761 "hdmi: hotplug gpio to irq map failed\n");
765 err = gpio_request(hotplug_gpio, "hdmi2.0_hpd");
767 dev_err(&dc->ndev->dev,
768 "hdmi: hpd gpio_request failed %d\n", err);
769 gpio_direction_input(hotplug_gpio);
771 err = request_threaded_irq(hotplug_irq,
772 NULL, tegra_hdmi_hpd_irq_handler,
773 (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
775 dev_name(&dc->ndev->dev), dc);
777 dev_err(&dc->ndev->dev,
778 "hdmi: request_threaded_irq failed: %d\n", err);
783 INIT_DELAYED_WORK(&hdmi->hpd_worker, tegra_hdmi_hpd_worker);
785 mutex_init(&hdmi->hpd_lock);
786 hdmi->irq = hotplug_irq;
790 gpio_free(hotplug_gpio);
794 static int tegra_hdmi_tmds_init(struct tegra_hdmi *hdmi)
796 struct device_node *np_prod = of_find_node_by_path(TMDS_NODE);
799 dev_warn(&hdmi->dc->ndev->dev,
800 "hdmi: find tmds prod node failed\n");
805 tegra_prod_init((const struct device_node *)np_prod);
806 if (IS_ERR(hdmi->prod_list)) {
807 dev_warn(&hdmi->dc->ndev->dev,
808 "hdmi: prod list init failed with error %ld\n",
809 PTR_ERR(hdmi->prod_list));
810 of_node_put(np_prod);
814 of_node_put(np_prod);
818 static int tegra_hdmi_config_tmds(struct tegra_hdmi *hdmi)
824 /* Select mode with smallest clk freq > pclk */
825 tmds_len = ARRAY_SIZE(tmds_config_modes);
826 for (i = 0; i < tmds_len - 1 &&
827 tmds_config_modes[i].clk < hdmi->dc->mode.pclk; i++);
829 if (tegra_platform_is_linsim())
832 err = tegra_prod_set_by_name(&hdmi->sor->base,
833 tmds_config_modes[i].name, hdmi->prod_list);
835 dev_warn(&hdmi->dc->ndev->dev,
836 "hdmi: tmds prod set failed\n");
843 static int tegra_dc_hdmi_init(struct tegra_dc *dc)
845 struct tegra_hdmi *hdmi;
847 struct device_node *np = dc->ndev->dev.of_node;
849 struct device_node *np_hdmi = of_find_node_by_path(HDMI_NODE);
851 struct device_node *np_hdmi = NULL;
853 struct device_node *np_panel = NULL;
856 hdmi = devm_kzalloc(&dc->ndev->dev, sizeof(*hdmi), GFP_KERNEL);
858 of_node_put(np_hdmi);
862 hdmi->sor = tegra_dc_sor_init(dc, NULL);
863 if (IS_ERR_OR_NULL(hdmi->sor)) {
864 err = PTR_ERR(hdmi->sor);
869 if (np_hdmi && of_device_is_available(np_hdmi)) {
870 np_panel = tegra_get_panel_node_out_type_check(dc,
872 if (np_panel && of_device_is_available(np_panel)) {
873 if (of_property_read_bool(np_panel,
876 of_node_put(np_panel);
880 of_node_put(np_panel);
885 hdmi->pdata = dc->pdata->default_out->hdmi_out;
888 hdmi->ddc_refcount = 0; /* assumes this is disabled when starting */
889 mutex_init(&hdmi->ddc_refcount_lock);
891 hdmi->mon_spec_valid = false;
892 hdmi->eld_valid = false;
893 hdmi->device_shutdown = false;
895 /* TODO: seamless boot mode needs initialize the state */
897 hdmi->enabled = false;
898 atomic_set(&hdmi->clock_refcount, 0);
900 atomic_set(&hdmi->suspended, 0);
902 #ifdef CONFIG_TEGRA_HDMIHDCP
903 hdmi->nvhdcp = tegra_nvhdcp_create(hdmi, dc->ndev->id,
905 if (IS_ERR_OR_NULL(hdmi->nvhdcp)) {
906 err = PTR_ERR(hdmi->nvhdcp);
909 tegra_nvhdcp_debugfs_init(hdmi->nvhdcp);
912 tegra_hdmi_ddc_init(hdmi, edid_src);
914 tegra_hdmi_scdc_init(hdmi);
916 tegra_hdmi_hpd_init(hdmi);
918 tegra_hdmi_vrr_init(hdmi);
920 tegra_hdmi_debugfs_init(hdmi);
922 tegra_hdmi_tmds_init(hdmi);
924 tegra_dc_set_outdata(dc, hdmi);
926 /* NOTE: Below code is applicable to L4T or embedded systems and is
927 * protected accordingly. This section early enables DC with first mode
928 * from the monitor specs.
929 * In case there is no hotplug we are falling back
930 * to default VGA mode.
932 if ((config_enabled(CONFIG_FRAMEBUFFER_CONSOLE) ||
933 ((dc->pdata->flags & TEGRA_DC_FLAG_ENABLED) &&
934 (dc->pdata->flags & TEGRA_DC_FLAG_SET_EARLY_MODE))) &&
935 dc->out && (dc->out->type == TEGRA_DC_OUT_HDMI)) {
936 struct fb_monspecs specs;
937 if (tegra_dc_hpd(dc) && (!dc->initialized)) {
938 if (!tegra_edid_get_monspecs(hdmi->edid, &specs))
939 tegra_dc_set_fb_mode(dc, specs.modedb, false);
941 /* if for some reason there is no edid upon hotplug */
942 tegra_dc_set_fb_mode(dc,
943 &tegra_dc_vga_mode, false);
946 tegra_dc_set_fb_mode(dc, &tegra_dc_vga_mode, false);
950 hdmi->hpd_switch.name = "hdmi";
951 err = switch_dev_register(&hdmi->hpd_switch);
953 dev_err(&dc->ndev->dev,
954 "hdmi: failed to register hpd switch %d\n", err);
956 hdmi->audio_switch.name = "hdmi_audio";
957 err = switch_dev_register(&hdmi->audio_switch);
959 dev_err(&dc->ndev->dev,
960 "hdmi: failed to register audio switch %d\n", err);
963 hdmi_ddc = kobject_create_and_add("hdmi_ddc_power_toggle", kernel_kobj);
965 pr_warn("kobject create_and_add hdmi_ddc_power_toggle failed\n");
968 err = sysfs_create_file(hdmi_ddc, &hdmi_ddc_power_config.attr);
970 pr_warn("sysfs create file hdmi_ddc_power_toggle failed\n");
974 of_node_put(np_hdmi);
977 devm_kfree(&dc->ndev->dev, hdmi);
978 of_node_put(np_hdmi);
982 static void tegra_dc_hdmi_destroy(struct tegra_dc *dc)
984 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
986 tegra_dc_sor_destroy(hdmi->sor);
987 tegra_edid_destroy(hdmi->edid);
988 tegra_nvhdcp_destroy(hdmi->nvhdcp);
989 free_irq(gpio_to_irq(dc->out->hotplug_gpio), dc);
990 gpio_free(dc->out->hotplug_gpio);
991 devm_kfree(&dc->ndev->dev, hdmi);
992 tegra_prod_release(&hdmi->prod_list);
995 switch_dev_unregister(&hdmi->hpd_switch);
996 switch_dev_unregister(&hdmi->audio_switch);
1000 static void tegra_hdmi_config(struct tegra_hdmi *hdmi)
1002 struct tegra_dc_sor_data *sor = hdmi->sor;
1003 struct tegra_dc *dc = hdmi->dc;
1004 #ifndef CONFIG_TEGRA_NVDISPLAY
1005 u32 h_pulse_start, h_pulse_end;
1007 u32 hblank, max_ac, rekey;
1009 u32 dispclk_div_8_2;
1011 if (tegra_platform_is_linsim())
1014 tegra_sor_write_field(sor, NV_SOR_INPUT_CONTROL,
1015 NV_SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED |
1016 NV_SOR_INPUT_CONTROL_HDMI_SRC_SELECT_DISPLAYB,
1017 NV_SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_FULL |
1018 NV_SOR_INPUT_CONTROL_HDMI_SRC_SELECT_DISPLAYB);
1020 dispclk_div_8_2 = clk_get_rate(hdmi->sor->sor_clk) / 1000000 * 4;
1021 tegra_sor_writel(sor, NV_SOR_REFCLK,
1022 NV_SOR_REFCLK_DIV_INT(dispclk_div_8_2 >> 2) |
1023 NV_SOR_REFCLK_DIV_FRAC(dispclk_div_8_2));
1025 rekey = NV_SOR_HDMI_CTRL_REKEY_DEFAULT;
1026 hblank = dc->mode.h_sync_width + dc->mode.h_back_porch +
1027 dc->mode.h_front_porch;
1028 max_ac = (hblank - rekey - 18) / 32;
1031 val |= hdmi->dvi ? 0x0 : NV_SOR_HDMI_CTRL_ENABLE;
1032 /* The register wants "-2" of the required rekey val */
1033 val |= NV_SOR_HDMI_CTRL_REKEY(rekey - 2);
1034 val |= NV_SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac);
1035 val |= NV_SOR_HDMI_CTRL_AUDIO_LAYOUT_SELECT;
1036 tegra_sor_writel(sor, NV_SOR_HDMI_CTRL, val);
1038 #ifndef CONFIG_TEGRA_NVDISPLAY
1039 tegra_dc_writel(dc, 0x180, DC_DISP_H_PULSE2_CONTROL);
1040 h_pulse_start = dc->mode.h_ref_to_sync +
1041 dc->mode.h_sync_width +
1042 dc->mode.h_back_porch - 10;
1043 h_pulse_end = h_pulse_start + 8;
1044 tegra_dc_writel(dc, PULSE_START(h_pulse_start) | PULSE_END(h_pulse_end),
1045 DC_DISP_H_PULSE2_POSITION_A);
1047 val = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
1048 val |= H_PULSE_2_ENABLE;
1049 tegra_dc_writel(dc, val, DC_DISP_DISP_SIGNAL_OPTIONS0);
1053 void tegra_hdmi_infoframe_pkt_write(struct tegra_hdmi *hdmi,
1054 u32 header_reg, u8 pkt_type,
1055 u8 pkt_vs, u8 pkt_len,
1057 u32 reg_payload_len,
1060 struct tegra_dc_sor_data *sor = hdmi->sor;
1062 u32 *data = reg_payload;
1063 u32 data_reg = header_reg + 1;
1065 val = NV_SOR_HDMI_INFOFRAME_HEADER_TYPE(pkt_type) |
1066 NV_SOR_HDMI_INFOFRAME_HEADER_VERSION(pkt_vs) |
1067 NV_SOR_HDMI_INFOFRAME_HEADER_LEN(pkt_len);
1068 tegra_sor_writel(sor, header_reg, val);
1071 u8 checksum = pkt_type + pkt_vs + pkt_len;
1073 for (val = 1; val <= pkt_len; val++)
1074 checksum += ((u8 *)reg_payload)[val];
1076 /* The first byte of the payload must always be the checksum
1077 * that we are going to calculate in SW */
1078 ((u8 *)reg_payload)[0] = (256 - checksum);
1081 for (val = 0; val < reg_payload_len; val += 4, data_reg++, data++)
1082 tegra_sor_writel(sor, data_reg, *data);
1085 static u32 tegra_hdmi_get_cea_modedb_size(struct tegra_hdmi *hdmi)
1087 if (!tegra_hdmi_is_connected(hdmi) || !hdmi->mon_spec_valid)
1090 return (hdmi->mon_spec.misc & FB_MISC_HDMI_FORUM) ?
1091 CEA_861_F_MODEDB_SIZE : CEA_861_D_MODEDB_SIZE;
1094 static void tegra_hdmi_get_cea_fb_videomode(struct fb_videomode *m,
1095 struct tegra_hdmi *hdmi)
1097 struct tegra_dc *dc = hdmi->dc;
1098 struct tegra_dc_mode dc_mode;
1101 memcpy(&dc_mode, &dc->mode, sizeof(dc->mode));
1103 /* get CEA video timings */
1104 yuv_flag = dc_mode.vmode & FB_VMODE_YUV_MASK;
1105 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y24) ||
1106 yuv_flag == (FB_VMODE_Y420_ONLY | FB_VMODE_Y24)) {
1107 dc_mode.h_back_porch *= 2;
1108 dc_mode.h_front_porch *= 2;
1109 dc_mode.h_sync_width *= 2;
1110 dc_mode.h_active *= 2;
1112 } else if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30)) {
1113 dc_mode.h_back_porch = (dc_mode.h_back_porch * 8) / 5;
1114 dc_mode.h_front_porch = (dc_mode.h_front_porch * 8) / 5;
1115 dc_mode.h_sync_width = (dc_mode.h_sync_width * 8) / 5;
1116 dc_mode.h_active = (dc_mode.h_active * 8) / 5;
1117 dc_mode.pclk = (dc_mode.pclk / 5) * 8;
1120 if (dc_mode.vmode & FB_VMODE_1000DIV1001) {
1121 dc_mode.pclk = DIV_ROUND_UP((u64)dc_mode.pclk * 1001, 1000);
1122 dc_mode.vmode &= ~FB_VMODE_1000DIV1001;
1125 tegra_dc_to_fb_videomode(m, &dc_mode);
1127 /* only interlaced required for VIC identification */
1128 m->vmode &= FB_VMODE_INTERLACED;
1132 static int tegra_hdmi_find_cea_vic(struct tegra_hdmi *hdmi)
1134 struct fb_videomode m;
1135 struct tegra_dc *dc = hdmi->dc;
1138 u32 modedb_size = tegra_hdmi_get_cea_modedb_size(hdmi);
1140 if (dc->initialized) {
1141 u32 vic = tegra_sor_readl(hdmi->sor,
1142 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_HIGH) & 0xff;
1144 dev_warn(&dc->ndev->dev, "hdmi: BL set VIC 0\n");
1148 tegra_hdmi_get_cea_fb_videomode(&m, hdmi);
1150 m.flag &= ~FB_FLAG_RATIO_MASK;
1151 m.flag |= tegra_dc_get_aspect_ratio(dc);
1153 for (i = 1; i < modedb_size; i++) {
1154 const struct fb_videomode *curr = &cea_modes[i];
1156 if (!((m.refresh == curr->refresh ||
1157 m.refresh + 1 == curr->refresh ||
1158 m.refresh == curr->refresh + 1) &&
1159 m.xres == curr->xres &&
1160 m.yres == curr->yres &&
1161 m.pixclock == curr->pixclock &&
1162 m.hsync_len == curr->hsync_len &&
1163 m.vsync_len == curr->vsync_len &&
1164 m.left_margin == curr->left_margin &&
1165 m.right_margin == curr->right_margin &&
1166 m.upper_margin == curr->upper_margin &&
1167 m.lower_margin == curr->lower_margin &&
1168 m.sync == curr->sync &&
1169 m.vmode == curr->vmode))
1174 /* if either flag is set, then match is required */
1176 (FB_FLAG_RATIO_4_3 | FB_FLAG_RATIO_16_9 |
1177 FB_FLAG_RATIO_64_27 | FB_FLAG_RATIO_256_135)) {
1178 if (m.flag & curr->flag & FB_FLAG_RATIO_4_3)
1180 else if (m.flag & curr->flag & FB_FLAG_RATIO_16_9)
1182 else if (m.flag & curr->flag & FB_FLAG_RATIO_64_27)
1184 else if (m.flag & curr->flag & FB_FLAG_RATIO_256_135)
1193 static u32 tegra_hdmi_get_aspect_ratio(struct tegra_hdmi *hdmi)
1197 switch (hdmi->dc->mode.avi_m) {
1198 case TEGRA_DC_MODE_AVI_M_4_3:
1199 aspect_ratio = HDMI_AVI_ASPECT_RATIO_4_3;
1201 case TEGRA_DC_MODE_AVI_M_16_9:
1202 aspect_ratio = HDMI_AVI_ASPECT_RATIO_16_9;
1205 * no avi_m field for picture aspect ratio 64:27 and 256:135.
1206 * sink detects via VIC, avi_m is 0.
1208 case TEGRA_DC_MODE_AVI_M_64_27: /* fall through */
1209 case TEGRA_DC_MODE_AVI_M_256_135: /* fall through */
1211 aspect_ratio = HDMI_AVI_ASPECT_RATIO_NO_DATA;
1214 /* For seamless HDMI, read aspect ratio parameters from bootloader
1215 * set AVI Infoframe parameters
1217 if ((aspect_ratio == HDMI_AVI_ASPECT_RATIO_NO_DATA) &&
1218 (hdmi->dc->initialized)) {
1220 temp = tegra_sor_readl(hdmi->sor,
1221 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_LOW);
1222 temp = (temp >> 20) & 0x3;
1225 aspect_ratio = HDMI_AVI_ASPECT_RATIO_4_3;
1228 aspect_ratio = HDMI_AVI_ASPECT_RATIO_16_9;
1231 aspect_ratio = HDMI_AVI_ASPECT_RATIO_NO_DATA;
1234 return aspect_ratio;
1237 static u32 tegra_hdmi_get_rgb_ycc(struct tegra_hdmi *hdmi)
1239 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1242 * For seamless HDMI, read YUV flag parameters from bootloader
1243 * set AVI Infoframe parameters
1245 if (hdmi->dc->initialized) {
1247 temp = tegra_sor_readl(hdmi->sor,
1248 NV_SOR_HDMI_AVI_INFOFRAME_SUBPACK0_LOW);
1249 temp = (temp >> 12) & 0x3;
1252 return HDMI_AVI_RGB;
1253 case HDMI_AVI_YCC_420:
1254 return HDMI_AVI_YCC_420;
1256 dev_warn(&hdmi->dc->ndev->dev, "hdmi: BL didn't set RGB/YUV indicator flag\n");
1261 if (yuv_flag & (FB_VMODE_Y420 | FB_VMODE_Y420_ONLY))
1262 return HDMI_AVI_YCC_420;
1263 else if (yuv_flag & FB_VMODE_Y422)
1264 return HDMI_AVI_YCC_422;
1266 return HDMI_AVI_RGB;
1269 static bool tegra_hdmi_is_ex_colorimetry(struct tegra_hdmi *hdmi)
1271 return !!(hdmi->dc->mode.vmode & FB_VMODE_EC_ENABLE);
1274 static u32 tegra_hdmi_get_ex_colorimetry(struct tegra_hdmi *hdmi)
1276 u32 vmode = hdmi->dc->mode.vmode;
1278 return tegra_hdmi_is_ex_colorimetry(hdmi) ?
1279 ((vmode & FB_VMODE_EC_MASK) >> FB_VMODE_EC_SHIFT) :
1280 HDMI_AVI_EXT_COLORIMETRY_INVALID;
1283 static void tegra_hdmi_avi_infoframe_update(struct tegra_hdmi *hdmi)
1285 struct hdmi_avi_infoframe *avi = &hdmi->avi;
1287 memset(&hdmi->avi, 0, sizeof(hdmi->avi));
1289 if (tegra_platform_is_linsim())
1292 avi->scan = HDMI_AVI_UNDERSCAN;
1293 avi->bar_valid = HDMI_AVI_BAR_INVALID;
1294 avi->act_fmt_valid = HDMI_AVI_ACTIVE_FORMAT_VALID;
1295 avi->rgb_ycc = tegra_hdmi_get_rgb_ycc(hdmi);
1297 avi->act_format = HDMI_AVI_ACTIVE_FORMAT_SAME;
1298 avi->aspect_ratio = tegra_hdmi_get_aspect_ratio(hdmi);
1299 avi->colorimetry = tegra_hdmi_is_ex_colorimetry(hdmi) ?
1300 HDMI_AVI_COLORIMETRY_EXTENDED_VALID :
1301 HDMI_AVI_COLORIMETRY_DEFAULT;
1303 avi->scaling = HDMI_AVI_SCALING_UNKNOWN;
1304 avi->rgb_quant = HDMI_AVI_RGB_QUANT_DEFAULT;
1305 avi->ext_colorimetry = tegra_hdmi_get_ex_colorimetry(hdmi);
1306 avi->it_content = HDMI_AVI_IT_CONTENT_FALSE;
1308 /* set correct vic if video format is cea defined else set 0 */
1309 avi->video_format = tegra_hdmi_find_cea_vic(hdmi);
1311 avi->pix_rep = HDMI_AVI_NO_PIX_REPEAT;
1312 avi->it_content_type = HDMI_AVI_IT_CONTENT_NONE;
1313 avi->ycc_quant = HDMI_AVI_YCC_QUANT_NONE;
1315 avi->top_bar_end_line_low_byte = 0;
1316 avi->top_bar_end_line_high_byte = 0;
1318 avi->bot_bar_start_line_low_byte = 0;
1319 avi->bot_bar_start_line_high_byte = 0;
1321 avi->left_bar_end_pixel_low_byte = 0;
1322 avi->left_bar_end_pixel_high_byte = 0;
1324 avi->right_bar_start_pixel_low_byte = 0;
1325 avi->right_bar_start_pixel_high_byte = 0;
1328 static void tegra_hdmi_avi_infoframe(struct tegra_hdmi *hdmi)
1330 struct tegra_dc_sor_data *sor = hdmi->sor;
1335 if (tegra_platform_is_linsim())
1338 /* disable avi infoframe before configuring except for seamless case */
1339 if (!hdmi->dc->initialized)
1340 tegra_sor_writel(sor, NV_SOR_HDMI_AVI_INFOFRAME_CTRL, 0);
1342 tegra_hdmi_avi_infoframe_update(hdmi);
1344 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_AVI_INFOFRAME_HEADER,
1345 HDMI_INFOFRAME_TYPE_AVI,
1346 HDMI_INFOFRAME_VS_AVI,
1347 HDMI_INFOFRAME_LEN_AVI,
1348 &hdmi->avi, sizeof(hdmi->avi),
1351 /* Send infoframe every frame, checksum hw generated */
1352 tegra_sor_writel(sor, NV_SOR_HDMI_AVI_INFOFRAME_CTRL,
1353 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_ENABLE_YES |
1354 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_OTHER_DISABLE |
1355 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_SINGLE_DISABLE |
1356 NV_SOR_HDMI_AVI_INFOFRAME_CTRL_CHECKSUM_ENABLE);
1359 static int tegra_hdmi_get_extended_vic(const struct tegra_dc_mode *mode)
1361 struct fb_videomode m;
1362 struct tegra_dc_mode mode_fixed;
1367 if (mode_fixed.vmode & FB_VMODE_1000DIV1001) {
1368 mode_fixed.pclk = (u64)mode_fixed.pclk * 1001 / 1000;
1369 mode_fixed.vmode &= ~FB_VMODE_1000DIV1001;
1372 tegra_dc_to_fb_videomode(&m, &mode_fixed);
1374 for (i = 1; i < HDMI_EXT_MODEDB_SIZE; i++) {
1375 const struct fb_videomode *curr = &hdmi_ext_modes[i];
1378 * We have to make sure we explicitly don't check m.refresh ==
1379 * curr->refresh (or use the fb_mode_is_equal() which does this
1380 * check). This is because of the lack of accuracy when going
1381 * between fb_videomode -> tegra_dc_mode -> fb_videomode. For
1382 * 1000/1001 modes, the calculated m->refresh is actually not
1383 * accurate. This is honestly a hack and does need to be fixed,
1384 * but is a WAR for now for HDMI certification
1386 if ((m.refresh == curr->refresh ||
1387 m.refresh + 1 == curr->refresh) &&
1388 m.xres == curr->xres &&
1389 m.yres == curr->yres &&
1390 m.pixclock == curr->pixclock &&
1391 m.hsync_len == curr->hsync_len &&
1392 m.vsync_len == curr->vsync_len &&
1393 m.left_margin == curr->left_margin &&
1394 m.right_margin == curr->right_margin &&
1395 m.upper_margin == curr->upper_margin &&
1396 m.lower_margin == curr->lower_margin &&
1397 m.sync == curr->sync &&
1398 m.vmode == curr->vmode)
1404 static void tegra_hdmi_vendor_infoframe_update(struct tegra_hdmi *hdmi)
1406 struct hdmi_vendor_infoframe *vsi = &hdmi->vsi;
1409 memset(&hdmi->vsi, 0, sizeof(hdmi->vsi));
1411 vsi->oui = HDMI_LICENSING_LLC_OUI;
1413 extended_vic = tegra_hdmi_get_extended_vic(&hdmi->dc->mode);
1416 HDMI_VENDOR_VIDEO_FORMAT_EXTENDED;
1417 vsi->extended_vic = extended_vic;
1421 static void tegra_hdmi_vendor_infoframe(struct tegra_hdmi *hdmi)
1423 /* hdmi licensing, LLC vsi playload len as per hdmi1.4b */
1424 #define HDMI_INFOFRAME_LEN_VENDOR_LLC (6)
1426 struct tegra_dc_sor_data *sor = hdmi->sor;
1431 /* disable vsi infoframe before configuring */
1432 tegra_sor_writel(sor, NV_SOR_HDMI_VSI_INFOFRAME_CTRL, 0);
1434 tegra_hdmi_vendor_infoframe_update(hdmi);
1436 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_VSI_INFOFRAME_HEADER,
1437 HDMI_INFOFRAME_TYPE_VENDOR,
1438 HDMI_INFOFRAME_VS_VENDOR,
1439 HDMI_INFOFRAME_LEN_VENDOR_LLC,
1440 &hdmi->vsi, sizeof(hdmi->vsi),
1443 /* Send infoframe every frame, checksum hw generated */
1444 tegra_sor_writel(sor, NV_SOR_HDMI_VSI_INFOFRAME_CTRL,
1445 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_ENABLE_YES |
1446 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_OTHER_DISABLE |
1447 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_SINGLE_DISABLE |
1448 NV_SOR_HDMI_VSI_INFOFRAME_CTRL_CHECKSUM_ENABLE);
1450 #undef HDMI_INFOFRAME_LEN_VENDOR_LLC
1453 static void tegra_hdmi_hdr_infoframe_update(struct tegra_hdmi *hdmi)
1455 struct hdmi_hdr_infoframe *hdr = &hdmi->hdr;
1457 memset(&hdmi->hdr, 0, sizeof(hdmi->hdr));
1459 if (tegra_platform_is_linsim())
1462 hdr->eotf = hdmi->dc->hdr.eotf;
1463 hdr->static_metadata_id = hdmi->dc->hdr.static_metadata_id;
1465 /* PB3-14 : Group 1 : Static Metadata*/
1466 hdr->display_primaries_x_0_lsb = hdmi->dc->hdr.static_metadata[0];
1467 hdr->display_primaries_x_0_msb = hdmi->dc->hdr.static_metadata[1];
1468 hdr->display_primaries_y_0_lsb = hdmi->dc->hdr.static_metadata[2];
1469 hdr->display_primaries_y_0_msb = hdmi->dc->hdr.static_metadata[3];
1470 hdr->display_primaries_x_1_lsb = hdmi->dc->hdr.static_metadata[4];
1471 hdr->display_primaries_x_1_msb = hdmi->dc->hdr.static_metadata[5];
1472 hdr->display_primaries_y_1_lsb = hdmi->dc->hdr.static_metadata[6];
1473 hdr->display_primaries_y_1_msb = hdmi->dc->hdr.static_metadata[7];
1474 hdr->display_primaries_x_2_lsb = hdmi->dc->hdr.static_metadata[8];
1475 hdr->display_primaries_x_2_msb = hdmi->dc->hdr.static_metadata[9];
1476 hdr->display_primaries_y_2_lsb = hdmi->dc->hdr.static_metadata[10];
1477 hdr->display_primaries_y_2_msb = hdmi->dc->hdr.static_metadata[11];
1479 /* PB15-18 : Group 2 : Static Metadata*/
1480 hdr->white_point_x_lsb = hdmi->dc->hdr.static_metadata[12];
1481 hdr->white_point_x_msb = hdmi->dc->hdr.static_metadata[13];
1482 hdr->white_point_y_lsb = hdmi->dc->hdr.static_metadata[14];
1483 hdr->white_point_y_msb = hdmi->dc->hdr.static_metadata[15];
1485 /* PB19-20 : Group 3 : Static Metadata*/
1486 hdr->max_display_mastering_luminance_lsb =
1487 hdmi->dc->hdr.static_metadata[16];
1488 hdr->max_display_mastering_luminance_msb =
1489 hdmi->dc->hdr.static_metadata[17];
1491 /* PB21-22 : Group 4 : Static Metadata*/
1492 hdr->min_display_mastering_luminance_lsb =
1493 hdmi->dc->hdr.static_metadata[18];
1494 hdr->min_display_mastering_luminance_msb =
1495 hdmi->dc->hdr.static_metadata[19];
1497 /* PB23-24 : Group 5 : Static Metadata*/
1498 hdr->max_content_light_level_lsb = hdmi->dc->hdr.static_metadata[20];
1499 hdr->max_content_light_level_msb = hdmi->dc->hdr.static_metadata[21];
1501 /* PB25-26 : Group 6 : Static Metadata*/
1502 hdr->max_frame_avg_light_level_lsb = hdmi->dc->hdr.static_metadata[22];
1503 hdr->min_frame_avg_light_level_msb = hdmi->dc->hdr.static_metadata[23];
1508 static void tegra_hdmi_hdr_infoframe(struct tegra_hdmi *hdmi)
1510 struct tegra_dc_sor_data *sor = hdmi->sor;
1512 /* disable generic infoframe before configuring */
1513 tegra_sor_writel(sor, NV_SOR_HDMI_GENERIC_CTRL, 0);
1515 tegra_hdmi_hdr_infoframe_update(hdmi);
1517 tegra_hdmi_infoframe_pkt_write(hdmi, NV_SOR_HDMI_GENERIC_HEADER,
1518 HDMI_INFOFRAME_TYPE_HDR,
1519 HDMI_INFOFRAME_VS_HDR,
1520 HDMI_INFOFRAME_LEN_HDR,
1521 &hdmi->hdr, sizeof(hdmi->hdr),
1524 /* Send infoframe every frame, checksum hw generated */
1525 tegra_sor_writel(sor, NV_SOR_HDMI_GENERIC_CTRL,
1526 NV_SOR_HDMI_GENERIC_CTRL_ENABLE_YES |
1527 NV_SOR_HDMI_GENERIC_CTRL_OTHER_DISABLE |
1528 NV_SOR_HDMI_GENERIC_CTRL_SINGLE_DISABLE);
1533 static int tegra_hdmi_scdc_read(struct tegra_hdmi *hdmi,
1534 u8 offset_data[][2], u32 n_entries)
1537 struct i2c_msg msg[] = {
1551 _tegra_hdmi_ddc_enable(hdmi);
1553 for (i = 0; i < n_entries; i++) {
1554 msg[0].buf = offset_data[i];
1555 msg[1].buf = &offset_data[i][1];
1556 tegra_hdmi_scdc_i2c_xfer(hdmi->dc, msg, ARRAY_SIZE(msg));
1559 _tegra_hdmi_ddc_disable(hdmi);
1564 static int tegra_hdmi_scdc_write(struct tegra_hdmi *hdmi,
1565 u8 offset_data[][2], u32 n_entries)
1568 struct i2c_msg msg[] = {
1576 _tegra_hdmi_ddc_enable(hdmi);
1578 for (i = 0; i < n_entries; i++) {
1579 msg[0].buf = offset_data[i];
1580 tegra_hdmi_scdc_i2c_xfer(hdmi->dc, msg, ARRAY_SIZE(msg));
1583 _tegra_hdmi_ddc_disable(hdmi);
1588 static int tegra_hdmi_v2_x_mon_config(struct tegra_hdmi *hdmi, bool enable)
1590 u8 tmds_config_en[][2] = {
1592 HDMI_SCDC_TMDS_CONFIG_OFFSET,
1593 (HDMI_SCDC_TMDS_CONFIG_BIT_CLK_RATIO_40 |
1594 HDMI_SCDC_TMDS_CONFIG_SCRAMBLING_EN)
1597 u8 tmds_config_dis[][2] = {
1599 HDMI_SCDC_TMDS_CONFIG_OFFSET,
1604 if (hdmi->dc->vedid)
1607 tegra_hdmi_scdc_write(hdmi,
1608 enable ? tmds_config_en : tmds_config_dis,
1609 ARRAY_SIZE(tmds_config_en));
1615 static void tegra_hdmi_v2_x_host_config(struct tegra_hdmi *hdmi, bool enable)
1617 u32 val = NV_SOR_HDMI2_CTRL_SCRAMBLE_ENABLE |
1618 NV_SOR_HDMI2_CTRL_CLK_MODE_DIV_BY_4;
1620 tegra_sor_write_field(hdmi->sor, NV_SOR_HDMI2_CTRL,
1621 NV_SOR_HDMI2_CTRL_SCRAMBLE_ENABLE |
1622 NV_SOR_HDMI2_CTRL_CLK_MODE_DIV_BY_4,
1626 static int _tegra_hdmi_v2_x_config(struct tegra_hdmi *hdmi)
1628 #define SCDC_STABILIZATION_DELAY_MS (20)
1630 /* disable hdmi2.x config on host and monitor only
1631 * if bootloader didn't initialize hdmi
1633 if (!hdmi->dc->initialized) {
1634 tegra_hdmi_v2_x_mon_config(hdmi, false);
1635 tegra_hdmi_v2_x_host_config(hdmi, false);
1638 /* enable hdmi2.x config on host and monitor */
1639 tegra_hdmi_v2_x_mon_config(hdmi, true);
1640 msleep(SCDC_STABILIZATION_DELAY_MS);
1642 tegra_hdmi_v2_x_host_config(hdmi, true);
1645 #undef SCDC_STABILIZATION_DELAY_MS
1648 static int tegra_hdmi_v2_x_config(struct tegra_hdmi *hdmi)
1650 _tegra_hdmi_v2_x_config(hdmi);
1655 static void tegra_hdmi_scdc_worker(struct work_struct *work)
1657 struct tegra_hdmi *hdmi = container_of(to_delayed_work(work),
1658 struct tegra_hdmi, scdc_work);
1659 u8 rd_status_flags[][2] = {
1660 {HDMI_SCDC_STATUS_FLAGS, 0x0}
1663 if (!hdmi->enabled || hdmi->dc->mode.pclk <= 340000000)
1666 if (hdmi->dc->vedid)
1669 if (!tegra_edid_is_scdc_present(hdmi->dc->edid))
1672 tegra_hdmi_scdc_read(hdmi, rd_status_flags,
1673 ARRAY_SIZE(rd_status_flags));
1674 if (!rd_status_flags[0][1] && (hdmi->dc->mode.pclk > 340000000)) {
1675 dev_info(&hdmi->dc->ndev->dev, "hdmi: scdc scrambling status is reset, "
1676 "trying to reconfigure.\n");
1677 _tegra_hdmi_v2_x_config(hdmi);
1681 /* reschedule the worker */
1682 cancel_delayed_work(&hdmi->scdc_work);
1683 schedule_delayed_work(&hdmi->scdc_work,
1684 msecs_to_jiffies(HDMI_SCDC_MONITOR_TIMEOUT_MS));
1687 static void _tegra_hdmi_clock_enable(struct tegra_hdmi *hdmi)
1689 struct tegra_dc_sor_data *sor = hdmi->sor;
1690 tegra_disp_clk_prepare_enable(sor->safe_clk);
1691 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_SAFE_CLK);
1692 tegra_sor_clk_enable(sor);
1695 static void _tegra_hdmi_clock_disable(struct tegra_hdmi *hdmi)
1697 struct tegra_dc_sor_data *sor = hdmi->sor;
1698 tegra_sor_clk_disable(sor);
1701 void tegra_hdmi_get(struct tegra_dc *dc)
1703 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1705 if (atomic_inc_return(&hdmi->clock_refcount) == 1)
1706 _tegra_hdmi_clock_enable(hdmi);
1709 void tegra_hdmi_put(struct tegra_dc *dc)
1711 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1713 if (WARN_ONCE(atomic_read(&hdmi->clock_refcount) <= 0,
1714 "hdmi: clock refcount imbalance"))
1716 if (atomic_dec_return(&hdmi->clock_refcount) == 0)
1717 _tegra_hdmi_clock_disable(hdmi);
1720 /* TODO: add support for other deep colors */
1721 static inline u32 tegra_hdmi_get_bpp(struct tegra_hdmi *hdmi)
1723 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1725 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30))
1731 static u32 tegra_hdmi_gcp_color_depth(struct tegra_hdmi *hdmi)
1735 switch (tegra_hdmi_get_bpp(hdmi)) {
1736 case 0: /* fall through */
1738 gcp_cd = TEGRA_HDMI_BPP_UNKNOWN;
1741 gcp_cd = TEGRA_HDMI_BPP_30;
1744 gcp_cd = TEGRA_HDMI_BPP_36;
1747 gcp_cd = TEGRA_HDMI_BPP_48;
1750 dev_WARN(&hdmi->dc->ndev->dev,
1751 "hdmi: unknown gcp color depth\n");
1757 /* return packing phase of last pixel in preceding video data period */
1758 static u32 tegra_hdmi_gcp_packing_phase(struct tegra_hdmi *hdmi)
1760 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1762 if (!tegra_hdmi_gcp_color_depth(hdmi))
1765 /* 10P4 for yuv420 10bpc */
1766 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30))
1772 static bool tegra_hdmi_gcp_default_phase_en(struct tegra_hdmi *hdmi)
1774 int yuv_flag = hdmi->dc->mode.vmode & FB_VMODE_YUV_MASK;
1776 if (!tegra_hdmi_gcp_color_depth(hdmi))
1779 if (yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30))
1785 /* general control packet */
1786 static void tegra_hdmi_gcp(struct tegra_hdmi *hdmi)
1788 #define GCP_SB1_PP_SHIFT 4
1790 struct tegra_dc_sor_data *sor = hdmi->sor;
1793 /* disable gcp before configuring */
1794 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_CTRL, 0);
1796 sb1 = tegra_hdmi_gcp_packing_phase(hdmi) << GCP_SB1_PP_SHIFT |
1797 tegra_hdmi_gcp_color_depth(hdmi);
1798 sb2 = !!tegra_hdmi_gcp_default_phase_en(hdmi);
1799 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_SUBPACK(0),
1800 sb1 << NV_SOR_HDMI_GCP_SUBPACK_SB1_SHIFT |
1801 sb2 << NV_SOR_HDMI_GCP_SUBPACK_SB2_SHIFT);
1803 /* Send gcp every frame */
1804 tegra_sor_writel(sor, NV_SOR_HDMI_GCP_CTRL,
1805 NV_SOR_HDMI_GCP_CTRL_ENABLE |
1806 NV_SOR_HDMI_GCP_CTRL_OTHER_DIS |
1807 NV_SOR_HDMI_GCP_CTRL_SINGLE_DIS);
1809 #undef GCP_SB1_PP_SHIFT
1812 static int tegra_hdmi_controller_enable(struct tegra_hdmi *hdmi)
1814 struct tegra_dc *dc = hdmi->dc;
1815 struct tegra_dc_sor_data *sor = hdmi->sor;
1820 tegra_sor_hdmi_pad_power_up(sor);
1822 tegra_sor_power_lanes(sor, 4, true);
1824 tegra_dc_sor_set_internal_panel(sor, false);
1825 tegra_hdmi_config(hdmi);
1826 tegra_hdmi_avi_infoframe(hdmi);
1827 tegra_hdmi_vendor_infoframe(hdmi);
1829 tegra_sor_pad_cal_power(sor, true);
1830 tegra_hdmi_config_tmds(hdmi);
1831 tegra_sor_pad_cal_power(sor, false);
1833 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_BRICK_CLK);
1834 tegra_dc_sor_attach(sor);
1837 tegra_nvhdcp_set_plug(hdmi->nvhdcp, true);
1839 tegra_dc_setup_clk(dc, dc->clk);
1840 tegra_dc_hdmi_setup_clk(dc, hdmi->sor->sor_clk);
1841 tegra_hdmi_config(hdmi);
1843 tegra_sor_config_xbar(hdmi->sor);
1845 /* TODO: Confirm sequence with HW */
1846 tegra_sor_writel(sor, NV_SOR_SEQ_INST(0), 0x8080);
1847 tegra_sor_writel(sor, NV_SOR_PWR, 0x80000001);
1849 if (hdmi->dc->mode.pclk > 340000000) {
1850 tegra_hdmi_v2_x_config(hdmi);
1851 schedule_delayed_work(&hdmi->scdc_work,
1852 msecs_to_jiffies(HDMI_SCDC_MONITOR_TIMEOUT_MS));
1855 tegra_hdmi_gcp(hdmi);
1861 static void tegra_dc_hdmi_enable(struct tegra_dc *dc)
1863 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
1868 tegra_hdmi_controller_enable(hdmi);
1870 hdmi->enabled = true;
1871 tegra_hda_set_data(hdmi, SINK_HDMI);
1872 #ifdef CONFIG_SWITCH
1874 switch_set_state(&hdmi->audio_switch, 1);
1878 static inline u32 tegra_hdmi_get_shift_clk_div(struct tegra_hdmi *hdmi)
1881 * HW does not support deep color yet
1888 static void tegra_hdmi_config_clk(struct tegra_hdmi *hdmi, u32 clk_type)
1890 if (clk_type == hdmi->clk_type)
1893 if (tegra_platform_is_linsim())
1896 if (clk_type == TEGRA_HDMI_BRICK_CLK) {
1898 struct tegra_dc_sor_data *sor = hdmi->sor;
1899 int div = hdmi->dc->mode.pclk < 340000000 ? 1 : 2;
1900 unsigned long rate = clk_get_rate(sor->src_switch_clk);
1901 unsigned long parent_rate =
1902 clk_get_rate(clk_get_parent(sor->src_switch_clk));
1904 /* Set sor divider */
1905 if (rate != parent_rate / div) {
1906 rate = parent_rate / div;
1907 clk_set_rate(sor->src_switch_clk, rate);
1910 /* Select brick muxes */
1911 val = (hdmi->dc->mode.pclk < 340000000) ?
1912 NV_SOR_CLK_CNTRL_DP_LINK_SPEED_G2_7 :
1913 NV_SOR_CLK_CNTRL_DP_LINK_SPEED_G5_4;
1915 val |= NV_SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
1916 tegra_sor_writel(hdmi->sor, NV_SOR_CLK_CNTRL, val);
1917 usleep_range(250, 300); /* sor brick pll stabilization delay */
1920 * Report brick configuration and rate, so that SOR clock tree
1921 * is properly updated. No h/w changes by clock api calls below,
1922 * just sync s/w state with brick h/w.
1924 rate = rate/NV_SOR_HDMI_BRICK_DIV*NV_SOR_HDMI_BRICK_MUL(val);
1925 if (clk_get_parent(sor->brick_clk) != sor->src_switch_clk)
1926 clk_set_parent(sor->brick_clk, sor->src_switch_clk);
1927 clk_set_rate(sor->brick_clk, rate);
1930 * Select primary -- HDMI -- DVFS table for SOR clock (if SOR
1931 * clock has single DVFS table for all modes, nothing changes).
1933 tegra_dvfs_use_alt_freqs_on_clk(sor->sor_clk, false);
1935 /* Select sor clock muxes */
1936 tegra_clk_cfg_ex(sor->sor_clk, TEGRA_CLK_SOR_CLK_SEL, 3);
1938 tegra_dc_writel(hdmi->dc, PIXEL_CLK_DIVIDER_PCD1 |
1939 SHIFT_CLK_DIVIDER(tegra_hdmi_get_shift_clk_div(hdmi)),
1940 DC_DISP_DISP_CLOCK_CONTROL);
1942 hdmi->clk_type = TEGRA_HDMI_BRICK_CLK;
1943 } else if (clk_type == TEGRA_HDMI_SAFE_CLK) {
1944 if (!hdmi->dc->initialized) {
1945 /* Select sor clock muxes */
1946 tegra_clk_cfg_ex(hdmi->sor->sor_clk,
1947 TEGRA_CLK_SOR_CLK_SEL, 0);
1948 hdmi->clk_type = TEGRA_HDMI_SAFE_CLK;
1951 dev_err(&hdmi->dc->ndev->dev, "hdmi: incorrect clk type configured\n");
1955 /* returns exact pixel clock in Hz */
1956 static long tegra_hdmi_get_pclk(struct tegra_dc_mode *mode)
1958 long h_total, v_total;
1960 h_total = mode->h_active + mode->h_front_porch + mode->h_back_porch +
1962 v_total = mode->v_active + mode->v_front_porch + mode->v_back_porch +
1964 refresh = tegra_dc_calc_refresh(mode);
1965 refresh = DIV_ROUND_CLOSEST(refresh, 1000);
1967 pclk = h_total * v_total * refresh;
1969 if (mode->vmode & FB_VMODE_1000DIV1001)
1970 pclk = pclk * 1000 / 1001;
1975 static long tegra_dc_hdmi_setup_clk(struct tegra_dc *dc, struct clk *clk)
1977 #ifdef CONFIG_TEGRA_NVDISPLAY
1978 struct clk *parent_clk = tegra_disp_clk_get(&dc->ndev->dev,
1979 dc->out->parent_clk ? : "plld2");
1981 struct clk *parent_clk = clk_get(NULL,
1982 dc->out->parent_clk ? : "pll_d2");
1985 dc->mode.pclk = tegra_hdmi_get_pclk(&dc->mode);
1987 if (IS_ERR_OR_NULL(parent_clk)) {
1988 dev_err(&dc->ndev->dev, "hdmi: parent clk get failed\n");
1992 if (!tegra_platform_is_silicon())
1993 return dc->mode.pclk;
1995 #ifdef CONFIG_TEGRA_NVDISPLAY
1996 if (clk_get_parent(clk) != parent_clk)
1997 clk_set_parent(clk, parent_clk);
1999 if (clk == dc->clk) {
2000 if (clk_get_parent(clk) != parent_clk) {
2001 if (clk_set_parent(clk, parent_clk)) {
2002 dev_err(&dc->ndev->dev,
2003 "hdmi: set dc parent failed\n");
2008 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2009 struct tegra_dc_sor_data *sor = hdmi->sor;
2011 if (clk_get_parent(sor->src_switch_clk) != parent_clk) {
2012 if (clk_set_parent(sor->src_switch_clk, parent_clk)) {
2013 dev_err(&dc->ndev->dev,
2014 "hdmi: set src switch parent failed\n");
2020 if (dc->initialized)
2022 if (clk_get_rate(parent_clk) != dc->mode.pclk)
2023 clk_set_rate(parent_clk, dc->mode.pclk);
2026 * DC clock divider is controlled by DC driver transparently to clock
2027 * framework -- hence, direct call to DVFS with target mode rate. SOR
2028 * clock rate in clock tree is properly updated, and can be used for
2031 * TODO: tegra_hdmi_controller_enable() procedure 1st configures SOR
2032 * clock via tegra_hdmi_config_clk(), and then calls this function
2033 * that may re-lock parent PLL. That needs to be double-checked:
2034 * in general re-locking PLL while the downstream module is already
2035 * sourced from it is not recommended. If/when the order of enabling
2036 * HDMI controller is changed, we can remove direct DVFS call for SOR
2037 * (but for DC it should be kept, anyway).
2040 tegra_dvfs_set_rate(clk, dc->mode.pclk);
2042 tegra_dvfs_set_rate(clk, clk_get_rate(clk));
2044 return tegra_dc_pclk_round_rate(dc, dc->mode.pclk);
2047 static void tegra_dc_hdmi_shutdown(struct tegra_dc *dc)
2049 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2051 hdmi->device_shutdown = true;
2052 tegra_nvhdcp_shutdown(hdmi->nvhdcp);
2057 static void tegra_dc_hdmi_disable(struct tegra_dc *dc)
2059 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2061 hdmi->enabled = false;
2062 #ifdef CONFIG_SWITCH
2063 switch_set_state(&hdmi->audio_switch, 0);
2066 tegra_hdmi_config_clk(hdmi, TEGRA_HDMI_SAFE_CLK);
2067 tegra_hdmi_controller_disable(hdmi);
2068 tegra_hda_reset_data();
2072 static bool tegra_dc_hdmi_detect(struct tegra_dc *dc)
2074 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2075 unsigned long delay = msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS);
2077 if (tegra_platform_is_linsim())
2080 if (dc->out->hotplug_state != TEGRA_HPD_STATE_NORMAL)
2083 cancel_delayed_work(&hdmi->hpd_worker);
2084 schedule_delayed_work(&hdmi->hpd_worker, delay);
2086 return tegra_dc_hpd(dc);
2089 static void tegra_dc_hdmi_suspend(struct tegra_dc *dc)
2091 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2093 if (dc->out->flags & TEGRA_DC_OUT_HOTPLUG_WAKE_LP0) {
2094 int wake_irq = gpio_to_irq(dc->out->hotplug_gpio);
2097 ret = enable_irq_wake(wake_irq);
2099 dev_err(&dc->ndev->dev,
2100 "%s: Couldn't enable HDMI wakeup, irq=%d, error=%d\n",
2101 __func__, wake_irq, ret);
2105 atomic_set(&hdmi->suspended, 1);
2108 static void tegra_dc_hdmi_resume(struct tegra_dc *dc)
2110 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2112 atomic_set(&hdmi->suspended, 0);
2114 if (dc->out->flags & TEGRA_DC_OUT_HOTPLUG_WAKE_LP0)
2115 disable_irq_wake(gpio_to_irq(dc->out->hotplug_gpio));
2117 cancel_delayed_work(&hdmi->hpd_worker);
2118 schedule_delayed_work(&hdmi->hpd_worker,
2119 msecs_to_jiffies(HDMI_HPD_DEBOUNCE_DELAY_MS + HDMI_HPD_DROP_TIMEOUT_MS));
2122 static int tegra_dc_hdmi_set_hdr(struct tegra_dc *dc)
2125 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2126 ret = tegra_edid_get_ex_hdr_cap(hdmi->edid);
2127 if (ret & FB_CAP_HDR)
2128 tegra_hdmi_hdr_infoframe(hdmi);
2132 static int tegra_dc_hdmi_ddc_enable(struct tegra_dc *dc)
2134 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2135 _tegra_hdmi_ddc_enable(hdmi);
2139 static int tegra_dc_hdmi_ddc_disable(struct tegra_dc *dc)
2141 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2142 _tegra_hdmi_ddc_disable(hdmi);
2146 static void tegra_dc_hdmi_modeset_notifier(struct tegra_dc *dc)
2148 struct tegra_hdmi *hdmi = tegra_dc_get_outdata(dc);
2151 tegra_dc_io_start(dc);
2153 /* disable hdmi2.x config on host and monitor */
2154 if (dc->mode.pclk > 340000000) {
2155 if (tegra_edid_is_scdc_present(dc->edid))
2156 tegra_hdmi_v2_x_mon_config(hdmi, true);
2157 tegra_hdmi_v2_x_host_config(hdmi, true);
2159 if (tegra_edid_is_scdc_present(dc->edid))
2160 tegra_hdmi_v2_x_mon_config(hdmi, false);
2161 tegra_hdmi_v2_x_host_config(hdmi, false);
2164 tegra_dc_io_end(dc);
2168 #ifdef CONFIG_DEBUG_FS
2169 /* show current hpd state */
2170 static int tegra_hdmi_hotplug_dbg_show(struct seq_file *m, void *unused)
2172 struct tegra_hdmi *hdmi = m->private;
2173 struct tegra_dc *dc = hdmi->dc;
2175 if (WARN_ON(!hdmi || !dc || !dc->out))
2179 seq_printf(m, "hdmi hpd state: %d\n", dc->out->hotplug_state);
2184 int tegra_hdmi_get_hotplug_state(struct tegra_hdmi *hdmi)
2187 return hdmi->dc->out->hotplug_state;
2190 void tegra_hdmi_set_hotplug_state(struct tegra_hdmi *hdmi, int new_hpd_state)
2192 struct tegra_dc *dc = hdmi->dc;
2196 hotplug_state = dc->out->hotplug_state;
2198 if (hotplug_state == TEGRA_HPD_STATE_NORMAL &&
2199 new_hpd_state != TEGRA_HPD_STATE_NORMAL &&
2200 tegra_dc_hotplug_supported(dc)) {
2201 disable_irq(gpio_to_irq(dc->out->hotplug_gpio));
2202 } else if (hotplug_state != TEGRA_HPD_STATE_NORMAL &&
2203 new_hpd_state == TEGRA_HPD_STATE_NORMAL &&
2204 tegra_dc_hotplug_supported(dc)) {
2205 enable_irq(gpio_to_irq(dc->out->hotplug_gpio));
2208 dc->out->hotplug_state = new_hpd_state;
2212 * sw controlled plug/unplug.
2213 * wait for any already executing hpd worker thread.
2214 * No debounce delay, schedule immedately
2216 cancel_delayed_work_sync(&hdmi->hpd_worker);
2217 schedule_delayed_work(&hdmi->hpd_worker, 0);
2221 * sw control for hpd.
2222 * 0 is normal state, hw drives hpd.
2223 * -1 is force deassert, sw drives hpd.
2224 * 1 is force assert, sw drives hpd.
2225 * before releasing to hw, sw must ensure hpd state is normal i.e. 0
2227 static ssize_t tegra_hdmi_hotplug_dbg_write(struct file *file,
2228 const char __user *addr,
2229 size_t len, loff_t *pos)
2231 struct seq_file *m = file->private_data;
2232 struct tegra_hdmi *hdmi = m->private;
2233 struct tegra_dc *dc = hdmi->dc;
2237 if (WARN_ON(!hdmi || !dc || !dc->out))
2240 ret = kstrtol_from_user(addr, len, 10, &new_hpd_state);
2244 tegra_hdmi_set_hotplug_state(hdmi, new_hpd_state);
2249 static int tegra_hdmi_hotplug_dbg_open(struct inode *inode, struct file *file)
2251 return single_open(file, tegra_hdmi_hotplug_dbg_show, inode->i_private);
2254 static const struct file_operations tegra_hdmi_hotplug_dbg_ops = {
2255 .open = tegra_hdmi_hotplug_dbg_open,
2257 .write = tegra_hdmi_hotplug_dbg_write,
2258 .llseek = seq_lseek,
2259 .release = single_release,
2262 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi)
2264 struct dentry *dir, *ret;
2266 dir = debugfs_create_dir("tegra_hdmi", NULL);
2267 if (IS_ERR_OR_NULL(dir))
2270 ret = debugfs_create_file("hotplug", S_IRUGO, dir,
2271 hdmi, &tegra_hdmi_hotplug_dbg_ops);
2272 if (IS_ERR_OR_NULL(ret))
2277 debugfs_remove_recursive(dir);
2281 static void tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi)
2287 static ssize_t hdmi_ddc_power_toggle(struct kobject *kobj,
2288 struct kobj_attribute *attr, const char *buf, size_t count)
2291 sscanf(buf, "%du", &value);
2292 tegra_hdmi_ddc_power_toggle(value);
2296 static ssize_t hdmi_ddc_power_show(struct kobject *kobj,
2297 struct kobj_attribute *attr, char *buf)
2299 return sprintf(buf, "%d\n", dc_hdmi->ddc_refcount);
2302 static bool tegra_dc_hdmi_hpd_state(struct tegra_dc *dc)
2308 if (WARN_ON(!dc || !dc->out))
2311 level = gpio_get_value_cansleep(dc->out->hotplug_gpio);
2313 sense = dc->out->flags & TEGRA_DC_OUT_HOTPLUG_MASK;
2315 hpd = (sense == TEGRA_DC_OUT_HOTPLUG_HIGH && level) ||
2316 (sense == TEGRA_DC_OUT_HOTPLUG_LOW && !level);
2321 static void tegra_dc_hdmi_vrr_enable(struct tegra_dc *dc, bool enable)
2323 struct tegra_vrr *vrr = dc->out->vrr;
2326 vrr->enable = enable;
2329 struct tegra_dc_out_ops tegra_dc_hdmi2_0_ops = {
2330 .init = tegra_dc_hdmi_init,
2331 .destroy = tegra_dc_hdmi_destroy,
2332 .enable = tegra_dc_hdmi_enable,
2333 .disable = tegra_dc_hdmi_disable,
2334 .setup_clk = tegra_dc_hdmi_setup_clk,
2335 .detect = tegra_dc_hdmi_detect,
2336 .shutdown = tegra_dc_hdmi_shutdown,
2337 .suspend = tegra_dc_hdmi_suspend,
2338 .resume = tegra_dc_hdmi_resume,
2339 .ddc_enable = tegra_dc_hdmi_ddc_enable,
2340 .ddc_disable = tegra_dc_hdmi_ddc_disable,
2341 .modeset_notifier = tegra_dc_hdmi_modeset_notifier,
2342 .mode_filter = tegra_hdmi_fb_mode_filter,
2343 .hpd_state = tegra_dc_hdmi_hpd_state,
2344 .vrr_enable = tegra_dc_hdmi_vrr_enable,
2345 .vrr_mode = tegra_hdmi_update_vrr_mode,
2346 .set_hdr = tegra_dc_hdmi_set_hdr,