2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
21 struct drm_bridge base;
24 #define to_hdmi_bridge(x) container_of(x, struct hdmi_bridge, base)
26 static void hdmi_bridge_destroy(struct drm_bridge *bridge)
28 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
29 hdmi_unreference(hdmi_bridge->hdmi);
30 drm_bridge_cleanup(bridge);
34 static void power_on(struct drm_bridge *bridge)
36 struct drm_device *dev = bridge->dev;
37 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
38 struct hdmi *hdmi = hdmi_bridge->hdmi;
39 const struct hdmi_platform_config *config = hdmi->config;
42 for (i = 0; i < config->pwr_reg_cnt; i++) {
43 ret = regulator_enable(hdmi->pwr_regs[i]);
45 dev_err(dev->dev, "failed to enable pwr regulator: %s (%d)\n",
46 config->pwr_reg_names[i], ret);
50 if (config->pwr_clk_cnt > 0) {
51 DBG("pixclock: %lu", hdmi->pixclock);
52 ret = clk_set_rate(hdmi->pwr_clks[0], hdmi->pixclock);
54 dev_err(dev->dev, "failed to set pixel clk: %s (%d)\n",
55 config->pwr_clk_names[0], ret);
59 for (i = 0; i < config->pwr_clk_cnt; i++) {
60 ret = clk_prepare_enable(hdmi->pwr_clks[i]);
62 dev_err(dev->dev, "failed to enable pwr clk: %s (%d)\n",
63 config->pwr_clk_names[i], ret);
68 static void power_off(struct drm_bridge *bridge)
70 struct drm_device *dev = bridge->dev;
71 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
72 struct hdmi *hdmi = hdmi_bridge->hdmi;
73 const struct hdmi_platform_config *config = hdmi->config;
76 /* TODO do we need to wait for final vblank somewhere before
81 for (i = 0; i < config->pwr_clk_cnt; i++)
82 clk_disable_unprepare(hdmi->pwr_clks[i]);
84 for (i = 0; i < config->pwr_reg_cnt; i++) {
85 ret = regulator_disable(hdmi->pwr_regs[i]);
87 dev_err(dev->dev, "failed to disable pwr regulator: %s (%d)\n",
88 config->pwr_reg_names[i], ret);
93 static void hdmi_bridge_pre_enable(struct drm_bridge *bridge)
95 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
96 struct hdmi *hdmi = hdmi_bridge->hdmi;
97 struct hdmi_phy *phy = hdmi->phy;
101 if (!hdmi->power_on) {
103 hdmi->power_on = true;
104 hdmi_audio_update(hdmi);
107 phy->funcs->powerup(phy, hdmi->pixclock);
108 hdmi_set_mode(hdmi, true);
111 static void hdmi_bridge_enable(struct drm_bridge *bridge)
115 static void hdmi_bridge_disable(struct drm_bridge *bridge)
119 static void hdmi_bridge_post_disable(struct drm_bridge *bridge)
121 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
122 struct hdmi *hdmi = hdmi_bridge->hdmi;
123 struct hdmi_phy *phy = hdmi->phy;
126 hdmi_set_mode(hdmi, false);
127 phy->funcs->powerdown(phy);
129 if (hdmi->power_on) {
131 hdmi->power_on = false;
132 hdmi_audio_update(hdmi);
136 static void hdmi_bridge_mode_set(struct drm_bridge *bridge,
137 struct drm_display_mode *mode,
138 struct drm_display_mode *adjusted_mode)
140 struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
141 struct hdmi *hdmi = hdmi_bridge->hdmi;
142 int hstart, hend, vstart, vend;
145 mode = adjusted_mode;
147 hdmi->pixclock = mode->clock * 1000;
149 hdmi->hdmi_mode = drm_match_cea_mode(mode) > 1;
151 hstart = mode->htotal - mode->hsync_start;
152 hend = mode->htotal - mode->hsync_start + mode->hdisplay;
154 vstart = mode->vtotal - mode->vsync_start - 1;
155 vend = mode->vtotal - mode->vsync_start + mode->vdisplay - 1;
157 DBG("htotal=%d, vtotal=%d, hstart=%d, hend=%d, vstart=%d, vend=%d",
158 mode->htotal, mode->vtotal, hstart, hend, vstart, vend);
160 hdmi_write(hdmi, REG_HDMI_TOTAL,
161 HDMI_TOTAL_H_TOTAL(mode->htotal - 1) |
162 HDMI_TOTAL_V_TOTAL(mode->vtotal - 1));
164 hdmi_write(hdmi, REG_HDMI_ACTIVE_HSYNC,
165 HDMI_ACTIVE_HSYNC_START(hstart) |
166 HDMI_ACTIVE_HSYNC_END(hend));
167 hdmi_write(hdmi, REG_HDMI_ACTIVE_VSYNC,
168 HDMI_ACTIVE_VSYNC_START(vstart) |
169 HDMI_ACTIVE_VSYNC_END(vend));
171 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
172 hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2,
173 HDMI_VSYNC_TOTAL_F2_V_TOTAL(mode->vtotal));
174 hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2,
175 HDMI_VSYNC_ACTIVE_F2_START(vstart + 1) |
176 HDMI_VSYNC_ACTIVE_F2_END(vend + 1));
178 hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2,
179 HDMI_VSYNC_TOTAL_F2_V_TOTAL(0));
180 hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2,
181 HDMI_VSYNC_ACTIVE_F2_START(0) |
182 HDMI_VSYNC_ACTIVE_F2_END(0));
186 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
187 frame_ctrl |= HDMI_FRAME_CTRL_HSYNC_LOW;
188 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
189 frame_ctrl |= HDMI_FRAME_CTRL_VSYNC_LOW;
190 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
191 frame_ctrl |= HDMI_FRAME_CTRL_INTERLACED_EN;
192 DBG("frame_ctrl=%08x", frame_ctrl);
193 hdmi_write(hdmi, REG_HDMI_FRAME_CTRL, frame_ctrl);
195 hdmi_audio_update(hdmi);
198 static const struct drm_bridge_funcs hdmi_bridge_funcs = {
199 .pre_enable = hdmi_bridge_pre_enable,
200 .enable = hdmi_bridge_enable,
201 .disable = hdmi_bridge_disable,
202 .post_disable = hdmi_bridge_post_disable,
203 .mode_set = hdmi_bridge_mode_set,
204 .destroy = hdmi_bridge_destroy,
208 /* initialize bridge */
209 struct drm_bridge *hdmi_bridge_init(struct hdmi *hdmi)
211 struct drm_bridge *bridge = NULL;
212 struct hdmi_bridge *hdmi_bridge;
215 hdmi_bridge = kzalloc(sizeof(*hdmi_bridge), GFP_KERNEL);
221 hdmi_bridge->hdmi = hdmi_reference(hdmi);
223 bridge = &hdmi_bridge->base;
225 drm_bridge_init(hdmi->dev, bridge, &hdmi_bridge_funcs);
231 hdmi_bridge_destroy(bridge);