Lines Matching +full:i2c +full:- +full:hdmi

1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
10 #include <linux/i2c.h>
47 static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_hdmi *hdmi, in sun4i_hdmi_setup_avi_infoframes() argument
55 &hdmi->connector, mode); in sun4i_hdmi_setup_avi_infoframes()
68 writeb(buffer[i], hdmi->base + SUN4I_HDMI_AVI_INFOFRAME_REG(i)); in sun4i_hdmi_setup_avi_infoframes()
77 struct drm_display_mode *mode = &crtc_state->mode; in sun4i_hdmi_atomic_check()
79 if (mode->flags & DRM_MODE_FLAG_DBLCLK) in sun4i_hdmi_atomic_check()
80 return -EINVAL; in sun4i_hdmi_atomic_check()
87 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder); in sun4i_hdmi_disable() local
90 DRM_DEBUG_DRIVER("Disabling the HDMI Output\n"); in sun4i_hdmi_disable()
92 val = readl(hdmi->base + SUN4I_HDMI_VID_CTRL_REG); in sun4i_hdmi_disable()
94 writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG); in sun4i_hdmi_disable()
96 clk_disable_unprepare(hdmi->tmds_clk); in sun4i_hdmi_disable()
101 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; in sun4i_hdmi_enable()
102 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder); in sun4i_hdmi_enable() local
103 struct drm_display_info *display = &hdmi->connector.display_info; in sun4i_hdmi_enable()
106 DRM_DEBUG_DRIVER("Enabling the HDMI Output\n"); in sun4i_hdmi_enable()
108 clk_prepare_enable(hdmi->tmds_clk); in sun4i_hdmi_enable()
110 sun4i_hdmi_setup_avi_infoframes(hdmi, mode); in sun4i_hdmi_enable()
113 writel(val, hdmi->base + SUN4I_HDMI_PKT_CTRL_REG(0)); in sun4i_hdmi_enable()
116 if (display->is_hdmi) in sun4i_hdmi_enable()
119 writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG); in sun4i_hdmi_enable()
126 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder); in sun4i_hdmi_mode_set() local
130 clk_set_rate(hdmi->mod_clk, mode->crtc_clock * 1000); in sun4i_hdmi_mode_set()
131 clk_set_rate(hdmi->tmds_clk, mode->crtc_clock * 1000); in sun4i_hdmi_mode_set()
135 hdmi->base + SUN4I_HDMI_UNKNOWN_REG); in sun4i_hdmi_mode_set()
147 val = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG); in sun4i_hdmi_mode_set()
149 val |= hdmi->variant->pad_ctrl1_init_val; in sun4i_hdmi_mode_set()
150 writel(val, hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG); in sun4i_hdmi_mode_set()
151 val = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG); in sun4i_hdmi_mode_set()
154 writel(SUN4I_HDMI_VID_TIMING_X(mode->hdisplay) | in sun4i_hdmi_mode_set()
155 SUN4I_HDMI_VID_TIMING_Y(mode->vdisplay), in sun4i_hdmi_mode_set()
156 hdmi->base + SUN4I_HDMI_VID_TIMING_ACT_REG); in sun4i_hdmi_mode_set()
158 x = mode->htotal - mode->hsync_start; in sun4i_hdmi_mode_set()
159 y = mode->vtotal - mode->vsync_start; in sun4i_hdmi_mode_set()
161 hdmi->base + SUN4I_HDMI_VID_TIMING_BP_REG); in sun4i_hdmi_mode_set()
163 x = mode->hsync_start - mode->hdisplay; in sun4i_hdmi_mode_set()
164 y = mode->vsync_start - mode->vdisplay; in sun4i_hdmi_mode_set()
166 hdmi->base + SUN4I_HDMI_VID_TIMING_FP_REG); in sun4i_hdmi_mode_set()
168 x = mode->hsync_end - mode->hsync_start; in sun4i_hdmi_mode_set()
169 y = mode->vsync_end - mode->vsync_start; in sun4i_hdmi_mode_set()
171 hdmi->base + SUN4I_HDMI_VID_TIMING_SPW_REG); in sun4i_hdmi_mode_set()
174 if (mode->flags & DRM_MODE_FLAG_PHSYNC) in sun4i_hdmi_mode_set()
177 if (mode->flags & DRM_MODE_FLAG_PVSYNC) in sun4i_hdmi_mode_set()
180 writel(val, hdmi->base + SUN4I_HDMI_VID_TIMING_POL_REG); in sun4i_hdmi_mode_set()
186 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder); in sun4i_hdmi_mode_valid() local
187 unsigned long rate = mode->clock * 1000; in sun4i_hdmi_mode_valid()
188 unsigned long diff = rate / 200; /* +-0.5% allowed by HDMI spec */ in sun4i_hdmi_mode_valid()
191 /* 165 MHz is the typical max pixelclock frequency for HDMI <= 1.2 */ in sun4i_hdmi_mode_valid()
194 rounded_rate = clk_round_rate(hdmi->tmds_clk, rate); in sun4i_hdmi_mode_valid()
196 max_t(unsigned long, rounded_rate, rate) - in sun4i_hdmi_mode_valid()
212 struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector); in sun4i_hdmi_get_modes() local
216 edid = drm_get_edid(connector, hdmi->ddc_i2c ?: hdmi->i2c); in sun4i_hdmi_get_modes()
221 connector->display_info.is_hdmi ? "an HDMI" : "a DVI"); in sun4i_hdmi_get_modes()
224 cec_s_phys_addr_from_edid(hdmi->cec_adap, edid); in sun4i_hdmi_get_modes()
236 remote = of_graph_get_remote_node(dev->of_node, 1, -1); in sun4i_hdmi_get_ddc()
238 return ERR_PTR(-EINVAL); in sun4i_hdmi_get_ddc()
240 phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0); in sun4i_hdmi_get_ddc()
243 return ERR_PTR(-ENODEV); in sun4i_hdmi_get_ddc()
248 return ERR_PTR(-EPROBE_DEFER); in sun4i_hdmi_get_ddc()
260 struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector); in sun4i_hdmi_connector_detect() local
263 reg = readl(hdmi->base + SUN4I_HDMI_HPD_REG); in sun4i_hdmi_connector_detect()
265 cec_phys_addr_invalidate(hdmi->cec_adap); in sun4i_hdmi_connector_detect()
284 struct sun4i_hdmi *hdmi = cec_get_drvdata(adap); in sun4i_hdmi_cec_pin_read() local
286 return readl(hdmi->base + SUN4I_HDMI_CEC) & SUN4I_HDMI_CEC_RX; in sun4i_hdmi_cec_pin_read()
291 struct sun4i_hdmi *hdmi = cec_get_drvdata(adap); in sun4i_hdmi_cec_pin_low() local
294 writel(SUN4I_HDMI_CEC_ENABLE, hdmi->base + SUN4I_HDMI_CEC); in sun4i_hdmi_cec_pin_low()
299 struct sun4i_hdmi *hdmi = cec_get_drvdata(adap); in sun4i_hdmi_cec_pin_high() local
305 writel(0, hdmi->base + SUN4I_HDMI_CEC); in sun4i_hdmi_cec_pin_high()
491 struct sun4i_drv *drv = drm->dev_private; in sun4i_hdmi_bind()
492 struct sun4i_hdmi *hdmi; in sun4i_hdmi_bind() local
496 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL); in sun4i_hdmi_bind()
497 if (!hdmi) in sun4i_hdmi_bind()
498 return -ENOMEM; in sun4i_hdmi_bind()
499 dev_set_drvdata(dev, hdmi); in sun4i_hdmi_bind()
500 hdmi->dev = dev; in sun4i_hdmi_bind()
501 hdmi->drv = drv; in sun4i_hdmi_bind()
503 hdmi->variant = of_device_get_match_data(dev); in sun4i_hdmi_bind()
504 if (!hdmi->variant) in sun4i_hdmi_bind()
505 return -EINVAL; in sun4i_hdmi_bind()
507 hdmi->base = devm_platform_ioremap_resource(pdev, 0); in sun4i_hdmi_bind()
508 if (IS_ERR(hdmi->base)) { in sun4i_hdmi_bind()
509 dev_err(dev, "Couldn't map the HDMI encoder registers\n"); in sun4i_hdmi_bind()
510 return PTR_ERR(hdmi->base); in sun4i_hdmi_bind()
513 if (hdmi->variant->has_reset_control) { in sun4i_hdmi_bind()
514 hdmi->reset = devm_reset_control_get(dev, NULL); in sun4i_hdmi_bind()
515 if (IS_ERR(hdmi->reset)) { in sun4i_hdmi_bind()
516 dev_err(dev, "Couldn't get the HDMI reset control\n"); in sun4i_hdmi_bind()
517 return PTR_ERR(hdmi->reset); in sun4i_hdmi_bind()
520 ret = reset_control_deassert(hdmi->reset); in sun4i_hdmi_bind()
522 dev_err(dev, "Couldn't deassert HDMI reset\n"); in sun4i_hdmi_bind()
527 hdmi->bus_clk = devm_clk_get(dev, "ahb"); in sun4i_hdmi_bind()
528 if (IS_ERR(hdmi->bus_clk)) { in sun4i_hdmi_bind()
529 dev_err(dev, "Couldn't get the HDMI bus clock\n"); in sun4i_hdmi_bind()
530 ret = PTR_ERR(hdmi->bus_clk); in sun4i_hdmi_bind()
533 clk_prepare_enable(hdmi->bus_clk); in sun4i_hdmi_bind()
535 hdmi->mod_clk = devm_clk_get(dev, "mod"); in sun4i_hdmi_bind()
536 if (IS_ERR(hdmi->mod_clk)) { in sun4i_hdmi_bind()
537 dev_err(dev, "Couldn't get the HDMI mod clock\n"); in sun4i_hdmi_bind()
538 ret = PTR_ERR(hdmi->mod_clk); in sun4i_hdmi_bind()
541 clk_prepare_enable(hdmi->mod_clk); in sun4i_hdmi_bind()
543 hdmi->pll0_clk = devm_clk_get(dev, "pll-0"); in sun4i_hdmi_bind()
544 if (IS_ERR(hdmi->pll0_clk)) { in sun4i_hdmi_bind()
545 dev_err(dev, "Couldn't get the HDMI PLL 0 clock\n"); in sun4i_hdmi_bind()
546 ret = PTR_ERR(hdmi->pll0_clk); in sun4i_hdmi_bind()
550 hdmi->pll1_clk = devm_clk_get(dev, "pll-1"); in sun4i_hdmi_bind()
551 if (IS_ERR(hdmi->pll1_clk)) { in sun4i_hdmi_bind()
552 dev_err(dev, "Couldn't get the HDMI PLL 1 clock\n"); in sun4i_hdmi_bind()
553 ret = PTR_ERR(hdmi->pll1_clk); in sun4i_hdmi_bind()
557 hdmi->regmap = devm_regmap_init_mmio(dev, hdmi->base, in sun4i_hdmi_bind()
559 if (IS_ERR(hdmi->regmap)) { in sun4i_hdmi_bind()
560 dev_err(dev, "Couldn't create HDMI encoder regmap\n"); in sun4i_hdmi_bind()
561 ret = PTR_ERR(hdmi->regmap); in sun4i_hdmi_bind()
565 ret = sun4i_tmds_create(hdmi); in sun4i_hdmi_bind()
571 if (hdmi->variant->has_ddc_parent_clk) { in sun4i_hdmi_bind()
572 hdmi->ddc_parent_clk = devm_clk_get(dev, "ddc"); in sun4i_hdmi_bind()
573 if (IS_ERR(hdmi->ddc_parent_clk)) { in sun4i_hdmi_bind()
574 dev_err(dev, "Couldn't get the HDMI DDC clock\n"); in sun4i_hdmi_bind()
575 ret = PTR_ERR(hdmi->ddc_parent_clk); in sun4i_hdmi_bind()
579 hdmi->ddc_parent_clk = hdmi->tmds_clk; in sun4i_hdmi_bind()
582 writel(SUN4I_HDMI_CTRL_ENABLE, hdmi->base + SUN4I_HDMI_CTRL_REG); in sun4i_hdmi_bind()
584 writel(hdmi->variant->pad_ctrl0_init_val, in sun4i_hdmi_bind()
585 hdmi->base + SUN4I_HDMI_PAD_CTRL0_REG); in sun4i_hdmi_bind()
587 reg = readl(hdmi->base + SUN4I_HDMI_PLL_CTRL_REG); in sun4i_hdmi_bind()
589 reg |= hdmi->variant->pll_ctrl_init_val; in sun4i_hdmi_bind()
590 writel(reg, hdmi->base + SUN4I_HDMI_PLL_CTRL_REG); in sun4i_hdmi_bind()
592 ret = sun4i_hdmi_i2c_create(dev, hdmi); in sun4i_hdmi_bind()
594 dev_err(dev, "Couldn't create the HDMI I2C adapter\n"); in sun4i_hdmi_bind()
598 hdmi->ddc_i2c = sun4i_hdmi_get_ddc(dev); in sun4i_hdmi_bind()
599 if (IS_ERR(hdmi->ddc_i2c)) { in sun4i_hdmi_bind()
600 ret = PTR_ERR(hdmi->ddc_i2c); in sun4i_hdmi_bind()
601 if (ret == -ENODEV) in sun4i_hdmi_bind()
602 hdmi->ddc_i2c = NULL; in sun4i_hdmi_bind()
607 drm_encoder_helper_add(&hdmi->encoder, in sun4i_hdmi_bind()
609 ret = drm_simple_encoder_init(drm, &hdmi->encoder, in sun4i_hdmi_bind()
612 dev_err(dev, "Couldn't initialise the HDMI encoder\n"); in sun4i_hdmi_bind()
616 hdmi->encoder.possible_crtcs = drm_of_find_possible_crtcs(drm, in sun4i_hdmi_bind()
617 dev->of_node); in sun4i_hdmi_bind()
618 if (!hdmi->encoder.possible_crtcs) { in sun4i_hdmi_bind()
619 ret = -EPROBE_DEFER; in sun4i_hdmi_bind()
624 hdmi->cec_adap = cec_pin_allocate_adapter(&sun4i_hdmi_cec_pin_ops, in sun4i_hdmi_bind()
625 hdmi, "sun4i", CEC_CAP_DEFAULTS | CEC_CAP_CONNECTOR_INFO); in sun4i_hdmi_bind()
626 ret = PTR_ERR_OR_ZERO(hdmi->cec_adap); in sun4i_hdmi_bind()
629 writel(readl(hdmi->base + SUN4I_HDMI_CEC) & ~SUN4I_HDMI_CEC_TX, in sun4i_hdmi_bind()
630 hdmi->base + SUN4I_HDMI_CEC); in sun4i_hdmi_bind()
633 drm_connector_helper_add(&hdmi->connector, in sun4i_hdmi_bind()
635 ret = drm_connector_init_with_ddc(drm, &hdmi->connector, in sun4i_hdmi_bind()
638 hdmi->ddc_i2c); in sun4i_hdmi_bind()
641 "Couldn't initialise the HDMI connector\n"); in sun4i_hdmi_bind()
644 cec_fill_conn_info_from_drm(&conn_info, &hdmi->connector); in sun4i_hdmi_bind()
645 cec_s_conn_info(hdmi->cec_adap, &conn_info); in sun4i_hdmi_bind()
648 hdmi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | in sun4i_hdmi_bind()
651 ret = cec_register_adapter(hdmi->cec_adap, dev); in sun4i_hdmi_bind()
654 drm_connector_attach_encoder(&hdmi->connector, &hdmi->encoder); in sun4i_hdmi_bind()
659 cec_delete_adapter(hdmi->cec_adap); in sun4i_hdmi_bind()
660 drm_encoder_cleanup(&hdmi->encoder); in sun4i_hdmi_bind()
662 i2c_put_adapter(hdmi->ddc_i2c); in sun4i_hdmi_bind()
664 i2c_del_adapter(hdmi->i2c); in sun4i_hdmi_bind()
666 clk_disable_unprepare(hdmi->mod_clk); in sun4i_hdmi_bind()
668 clk_disable_unprepare(hdmi->bus_clk); in sun4i_hdmi_bind()
670 reset_control_assert(hdmi->reset); in sun4i_hdmi_bind()
677 struct sun4i_hdmi *hdmi = dev_get_drvdata(dev); in sun4i_hdmi_unbind() local
679 cec_unregister_adapter(hdmi->cec_adap); in sun4i_hdmi_unbind()
680 i2c_del_adapter(hdmi->i2c); in sun4i_hdmi_unbind()
681 i2c_put_adapter(hdmi->ddc_i2c); in sun4i_hdmi_unbind()
682 clk_disable_unprepare(hdmi->mod_clk); in sun4i_hdmi_unbind()
683 clk_disable_unprepare(hdmi->bus_clk); in sun4i_hdmi_unbind()
693 return component_add(&pdev->dev, &sun4i_hdmi_ops); in sun4i_hdmi_probe()
698 component_del(&pdev->dev, &sun4i_hdmi_ops); in sun4i_hdmi_remove()
702 { .compatible = "allwinner,sun4i-a10-hdmi", .data = &sun4i_variant, },
703 { .compatible = "allwinner,sun5i-a10s-hdmi", .data = &sun5i_variant, },
704 { .compatible = "allwinner,sun6i-a31-hdmi", .data = &sun6i_variant, },
713 .name = "sun4i-hdmi",
719 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
720 MODULE_DESCRIPTION("Allwinner A10 HDMI Driver");