Commit | Line | Data |
---|---|---|
c8afe684 RC |
1 | /* |
2 | * Copyright (C) 2013 Red Hat | |
3 | * Author: Rob Clark <robdclark@gmail.com> | |
4 | * | |
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. | |
8 | * | |
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 | |
12 | * more details. | |
13 | * | |
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/>. | |
16 | */ | |
17 | ||
18 | #include <linux/gpio.h> | |
19 | ||
dd2da6e3 | 20 | #include "msm_kms.h" |
c8afe684 RC |
21 | #include "hdmi.h" |
22 | ||
23 | struct hdmi_connector { | |
a3376e3e RC |
24 | struct drm_connector base; |
25 | struct hdmi *hdmi; | |
dada25bd | 26 | struct work_struct hpd_work; |
c8afe684 RC |
27 | }; |
28 | #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base) | |
29 | ||
30 | static int gpio_config(struct hdmi *hdmi, bool on) | |
31 | { | |
32 | struct drm_device *dev = hdmi->dev; | |
dada25bd | 33 | const struct hdmi_platform_config *config = hdmi->config; |
c8afe684 RC |
34 | int ret; |
35 | ||
36 | if (on) { | |
37 | ret = gpio_request(config->ddc_clk_gpio, "HDMI_DDC_CLK"); | |
38 | if (ret) { | |
39 | dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n", | |
40 | "HDMI_DDC_CLK", config->ddc_clk_gpio, ret); | |
41 | goto error1; | |
42 | } | |
dada25bd RC |
43 | gpio_set_value_cansleep(config->ddc_clk_gpio, 1); |
44 | ||
c8afe684 RC |
45 | ret = gpio_request(config->ddc_data_gpio, "HDMI_DDC_DATA"); |
46 | if (ret) { | |
47 | dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n", | |
48 | "HDMI_DDC_DATA", config->ddc_data_gpio, ret); | |
49 | goto error2; | |
50 | } | |
dada25bd RC |
51 | gpio_set_value_cansleep(config->ddc_data_gpio, 1); |
52 | ||
c8afe684 RC |
53 | ret = gpio_request(config->hpd_gpio, "HDMI_HPD"); |
54 | if (ret) { | |
55 | dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n", | |
56 | "HDMI_HPD", config->hpd_gpio, ret); | |
57 | goto error3; | |
58 | } | |
dada25bd RC |
59 | gpio_direction_input(config->hpd_gpio); |
60 | gpio_set_value_cansleep(config->hpd_gpio, 1); | |
61 | ||
62 | if (config->mux_en_gpio != -1) { | |
63 | ret = gpio_request(config->mux_en_gpio, "HDMI_MUX_EN"); | |
c8afe684 RC |
64 | if (ret) { |
65 | dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n", | |
a2fe6cdc | 66 | "HDMI_MUX_EN", config->mux_en_gpio, ret); |
c8afe684 RC |
67 | goto error4; |
68 | } | |
dada25bd RC |
69 | gpio_set_value_cansleep(config->mux_en_gpio, 1); |
70 | } | |
71 | ||
72 | if (config->mux_sel_gpio != -1) { | |
73 | ret = gpio_request(config->mux_sel_gpio, "HDMI_MUX_SEL"); | |
74 | if (ret) { | |
75 | dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n", | |
76 | "HDMI_MUX_SEL", config->mux_sel_gpio, ret); | |
77 | goto error5; | |
78 | } | |
79 | gpio_set_value_cansleep(config->mux_sel_gpio, 0); | |
c8afe684 | 80 | } |
1930f38a BG |
81 | |
82 | if (config->mux_lpm_gpio != -1) { | |
83 | ret = gpio_request(config->mux_lpm_gpio, | |
84 | "HDMI_MUX_LPM"); | |
85 | if (ret) { | |
86 | dev_err(dev->dev, | |
87 | "'%s'(%d) gpio_request failed: %d\n", | |
88 | "HDMI_MUX_LPM", | |
89 | config->mux_lpm_gpio, ret); | |
90 | goto error6; | |
91 | } | |
92 | gpio_set_value_cansleep(config->mux_lpm_gpio, 1); | |
93 | } | |
c8afe684 RC |
94 | DBG("gpio on"); |
95 | } else { | |
96 | gpio_free(config->ddc_clk_gpio); | |
97 | gpio_free(config->ddc_data_gpio); | |
98 | gpio_free(config->hpd_gpio); | |
99 | ||
dada25bd RC |
100 | if (config->mux_en_gpio != -1) { |
101 | gpio_set_value_cansleep(config->mux_en_gpio, 0); | |
102 | gpio_free(config->mux_en_gpio); | |
103 | } | |
104 | ||
105 | if (config->mux_sel_gpio != -1) { | |
106 | gpio_set_value_cansleep(config->mux_sel_gpio, 1); | |
107 | gpio_free(config->mux_sel_gpio); | |
c8afe684 | 108 | } |
1930f38a BG |
109 | |
110 | if (config->mux_lpm_gpio != -1) { | |
111 | gpio_set_value_cansleep(config->mux_lpm_gpio, 0); | |
112 | gpio_free(config->mux_lpm_gpio); | |
113 | } | |
c8afe684 RC |
114 | DBG("gpio off"); |
115 | } | |
116 | ||
117 | return 0; | |
118 | ||
1930f38a BG |
119 | error6: |
120 | if (config->mux_sel_gpio != -1) | |
121 | gpio_free(config->mux_sel_gpio); | |
dada25bd RC |
122 | error5: |
123 | if (config->mux_en_gpio != -1) | |
124 | gpio_free(config->mux_en_gpio); | |
c8afe684 RC |
125 | error4: |
126 | gpio_free(config->hpd_gpio); | |
127 | error3: | |
128 | gpio_free(config->ddc_data_gpio); | |
129 | error2: | |
130 | gpio_free(config->ddc_clk_gpio); | |
131 | error1: | |
132 | return ret; | |
133 | } | |
134 | ||
135 | static int hpd_enable(struct hdmi_connector *hdmi_connector) | |
136 | { | |
a3376e3e | 137 | struct hdmi *hdmi = hdmi_connector->hdmi; |
dada25bd | 138 | const struct hdmi_platform_config *config = hdmi->config; |
a3376e3e | 139 | struct drm_device *dev = hdmi_connector->base.dev; |
c8afe684 RC |
140 | struct hdmi_phy *phy = hdmi->phy; |
141 | uint32_t hpd_ctrl; | |
dada25bd | 142 | int i, ret; |
c8afe684 | 143 | |
e6d7a16f JW |
144 | for (i = 0; i < config->hpd_reg_cnt; i++) { |
145 | ret = regulator_enable(hdmi->hpd_regs[i]); | |
146 | if (ret) { | |
147 | dev_err(dev->dev, "failed to enable hpd regulator: %s (%d)\n", | |
148 | config->hpd_reg_names[i], ret); | |
149 | goto fail; | |
150 | } | |
151 | } | |
152 | ||
c8afe684 RC |
153 | ret = gpio_config(hdmi, true); |
154 | if (ret) { | |
155 | dev_err(dev->dev, "failed to configure GPIOs: %d\n", ret); | |
156 | goto fail; | |
157 | } | |
158 | ||
dada25bd | 159 | for (i = 0; i < config->hpd_clk_cnt; i++) { |
b77f47e7 SV |
160 | if (config->hpd_freq && config->hpd_freq[i]) { |
161 | ret = clk_set_rate(hdmi->hpd_clks[i], | |
162 | config->hpd_freq[i]); | |
163 | if (ret) | |
164 | dev_warn(dev->dev, "failed to set clk %s (%d)\n", | |
165 | config->hpd_clk_names[i], ret); | |
166 | } | |
167 | ||
dada25bd RC |
168 | ret = clk_prepare_enable(hdmi->hpd_clks[i]); |
169 | if (ret) { | |
170 | dev_err(dev->dev, "failed to enable hpd clk: %s (%d)\n", | |
171 | config->hpd_clk_names[i], ret); | |
172 | goto fail; | |
173 | } | |
c8afe684 RC |
174 | } |
175 | ||
c8afe684 RC |
176 | hdmi_set_mode(hdmi, false); |
177 | phy->funcs->reset(phy); | |
178 | hdmi_set_mode(hdmi, true); | |
179 | ||
180 | hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b); | |
181 | ||
182 | /* enable HPD events: */ | |
183 | hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, | |
184 | HDMI_HPD_INT_CTRL_INT_CONNECT | | |
185 | HDMI_HPD_INT_CTRL_INT_EN); | |
186 | ||
187 | /* set timeout to 4.1ms (max) for hardware debounce */ | |
188 | hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL); | |
189 | hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff); | |
190 | ||
191 | /* Toggle HPD circuit to trigger HPD sense */ | |
192 | hdmi_write(hdmi, REG_HDMI_HPD_CTRL, | |
193 | ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl); | |
194 | hdmi_write(hdmi, REG_HDMI_HPD_CTRL, | |
195 | HDMI_HPD_CTRL_ENABLE | hpd_ctrl); | |
196 | ||
197 | return 0; | |
198 | ||
199 | fail: | |
200 | return ret; | |
201 | } | |
202 | ||
e6d7a16f | 203 | static void hdp_disable(struct hdmi_connector *hdmi_connector) |
c8afe684 | 204 | { |
a3376e3e | 205 | struct hdmi *hdmi = hdmi_connector->hdmi; |
dada25bd | 206 | const struct hdmi_platform_config *config = hdmi->config; |
a3376e3e | 207 | struct drm_device *dev = hdmi_connector->base.dev; |
dada25bd | 208 | int i, ret = 0; |
c8afe684 RC |
209 | |
210 | /* Disable HPD interrupt */ | |
211 | hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0); | |
212 | ||
213 | hdmi_set_mode(hdmi, false); | |
214 | ||
dada25bd RC |
215 | for (i = 0; i < config->hpd_clk_cnt; i++) |
216 | clk_disable_unprepare(hdmi->hpd_clks[i]); | |
c8afe684 RC |
217 | |
218 | ret = gpio_config(hdmi, false); | |
e6d7a16f JW |
219 | if (ret) |
220 | dev_warn(dev->dev, "failed to unconfigure GPIOs: %d\n", ret); | |
c8afe684 | 221 | |
e6d7a16f JW |
222 | for (i = 0; i < config->hpd_reg_cnt; i++) { |
223 | ret = regulator_disable(hdmi->hpd_regs[i]); | |
224 | if (ret) | |
225 | dev_warn(dev->dev, "failed to disable hpd regulator: %s (%d)\n", | |
226 | config->hpd_reg_names[i], ret); | |
227 | } | |
c8afe684 RC |
228 | } |
229 | ||
dada25bd RC |
230 | static void |
231 | hotplug_work(struct work_struct *work) | |
232 | { | |
233 | struct hdmi_connector *hdmi_connector = | |
234 | container_of(work, struct hdmi_connector, hpd_work); | |
235 | struct drm_connector *connector = &hdmi_connector->base; | |
236 | drm_helper_hpd_irq_event(connector->dev); | |
237 | } | |
238 | ||
c8afe684 RC |
239 | void hdmi_connector_irq(struct drm_connector *connector) |
240 | { | |
a3376e3e | 241 | struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector); |
dada25bd | 242 | struct msm_drm_private *priv = connector->dev->dev_private; |
a3376e3e | 243 | struct hdmi *hdmi = hdmi_connector->hdmi; |
c8afe684 RC |
244 | uint32_t hpd_int_status, hpd_int_ctrl; |
245 | ||
246 | /* Process HPD: */ | |
247 | hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS); | |
248 | hpd_int_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL); | |
249 | ||
250 | if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) && | |
251 | (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) { | |
252 | bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED); | |
253 | ||
ff2f974e | 254 | /* ack & disable (temporarily) HPD events: */ |
c8afe684 | 255 | hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, |
ff2f974e JW |
256 | HDMI_HPD_INT_CTRL_INT_ACK); |
257 | ||
258 | DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl); | |
c8afe684 | 259 | |
c8afe684 RC |
260 | /* detect disconnect if we are connected or visa versa: */ |
261 | hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN; | |
262 | if (!detected) | |
263 | hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT; | |
264 | hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl); | |
dada25bd RC |
265 | |
266 | queue_work(priv->wq, &hdmi_connector->hpd_work); | |
c8afe684 RC |
267 | } |
268 | } | |
269 | ||
3189650d RC |
270 | static enum drm_connector_status detect_reg(struct hdmi *hdmi) |
271 | { | |
272 | uint32_t hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS); | |
273 | return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ? | |
274 | connector_status_connected : connector_status_disconnected; | |
275 | } | |
276 | ||
277 | static enum drm_connector_status detect_gpio(struct hdmi *hdmi) | |
278 | { | |
279 | const struct hdmi_platform_config *config = hdmi->config; | |
280 | return gpio_get_value(config->hpd_gpio) ? | |
281 | connector_status_connected : | |
282 | connector_status_disconnected; | |
283 | } | |
284 | ||
c8afe684 RC |
285 | static enum drm_connector_status hdmi_connector_detect( |
286 | struct drm_connector *connector, bool force) | |
287 | { | |
a3376e3e RC |
288 | struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector); |
289 | struct hdmi *hdmi = hdmi_connector->hdmi; | |
3189650d | 290 | enum drm_connector_status stat_gpio, stat_reg; |
c8afe684 RC |
291 | int retry = 20; |
292 | ||
3189650d RC |
293 | do { |
294 | stat_gpio = detect_gpio(hdmi); | |
295 | stat_reg = detect_reg(hdmi); | |
c8afe684 | 296 | |
3189650d | 297 | if (stat_gpio == stat_reg) |
dada25bd | 298 | break; |
3189650d | 299 | |
c8afe684 | 300 | mdelay(10); |
3189650d RC |
301 | } while (--retry); |
302 | ||
303 | /* the status we get from reading gpio seems to be more reliable, | |
304 | * so trust that one the most if we didn't manage to get hdmi and | |
305 | * gpio status to agree: | |
306 | */ | |
307 | if (stat_gpio != stat_reg) { | |
308 | DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg); | |
309 | DBG("hpd gpio tells us: %d", stat_gpio); | |
c8afe684 RC |
310 | } |
311 | ||
3189650d | 312 | return stat_gpio; |
c8afe684 RC |
313 | } |
314 | ||
315 | static void hdmi_connector_destroy(struct drm_connector *connector) | |
316 | { | |
a3376e3e | 317 | struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector); |
c8afe684 RC |
318 | |
319 | hdp_disable(hdmi_connector); | |
320 | ||
34ea3d38 | 321 | drm_connector_unregister(connector); |
c8afe684 RC |
322 | drm_connector_cleanup(connector); |
323 | ||
c8afe684 RC |
324 | kfree(hdmi_connector); |
325 | } | |
326 | ||
327 | static int hdmi_connector_get_modes(struct drm_connector *connector) | |
328 | { | |
a3376e3e RC |
329 | struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector); |
330 | struct hdmi *hdmi = hdmi_connector->hdmi; | |
c8afe684 RC |
331 | struct edid *edid; |
332 | uint32_t hdmi_ctrl; | |
333 | int ret = 0; | |
334 | ||
335 | hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL); | |
336 | hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE); | |
337 | ||
338 | edid = drm_get_edid(connector, hdmi->i2c); | |
339 | ||
340 | hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl); | |
341 | ||
342 | drm_mode_connector_update_edid_property(connector, edid); | |
343 | ||
344 | if (edid) { | |
345 | ret = drm_add_edid_modes(connector, edid); | |
346 | kfree(edid); | |
347 | } | |
348 | ||
349 | return ret; | |
350 | } | |
351 | ||
352 | static int hdmi_connector_mode_valid(struct drm_connector *connector, | |
353 | struct drm_display_mode *mode) | |
354 | { | |
a3376e3e | 355 | struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector); |
dada25bd RC |
356 | struct hdmi *hdmi = hdmi_connector->hdmi; |
357 | const struct hdmi_platform_config *config = hdmi->config; | |
c8afe684 RC |
358 | struct msm_drm_private *priv = connector->dev->dev_private; |
359 | struct msm_kms *kms = priv->kms; | |
360 | long actual, requested; | |
361 | ||
362 | requested = 1000 * mode->clock; | |
363 | actual = kms->funcs->round_pixclk(kms, | |
a3376e3e | 364 | requested, hdmi_connector->hdmi->encoder); |
c8afe684 | 365 | |
dada25bd RC |
366 | /* for mdp5/apq8074, we manage our own pixel clk (as opposed to |
367 | * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder | |
368 | * instead): | |
369 | */ | |
370 | if (config->pwr_clk_cnt > 0) | |
371 | actual = clk_round_rate(hdmi->pwr_clks[0], actual); | |
372 | ||
c8afe684 RC |
373 | DBG("requested=%ld, actual=%ld", requested, actual); |
374 | ||
375 | if (actual != requested) | |
376 | return MODE_CLOCK_RANGE; | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
a3376e3e RC |
381 | static struct drm_encoder * |
382 | hdmi_connector_best_encoder(struct drm_connector *connector) | |
383 | { | |
384 | struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector); | |
385 | return hdmi_connector->hdmi->encoder; | |
386 | } | |
387 | ||
c8afe684 | 388 | static const struct drm_connector_funcs hdmi_connector_funcs = { |
0b776d45 | 389 | .dpms = drm_atomic_helper_connector_dpms, |
c8afe684 RC |
390 | .detect = hdmi_connector_detect, |
391 | .fill_modes = drm_helper_probe_single_connector_modes, | |
392 | .destroy = hdmi_connector_destroy, | |
3e7849ef RC |
393 | .reset = drm_atomic_helper_connector_reset, |
394 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, | |
395 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, | |
c8afe684 RC |
396 | }; |
397 | ||
398 | static const struct drm_connector_helper_funcs hdmi_connector_helper_funcs = { | |
399 | .get_modes = hdmi_connector_get_modes, | |
400 | .mode_valid = hdmi_connector_mode_valid, | |
a3376e3e | 401 | .best_encoder = hdmi_connector_best_encoder, |
c8afe684 RC |
402 | }; |
403 | ||
404 | /* initialize connector */ | |
a3376e3e | 405 | struct drm_connector *hdmi_connector_init(struct hdmi *hdmi) |
c8afe684 RC |
406 | { |
407 | struct drm_connector *connector = NULL; | |
408 | struct hdmi_connector *hdmi_connector; | |
409 | int ret; | |
410 | ||
411 | hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL); | |
412 | if (!hdmi_connector) { | |
413 | ret = -ENOMEM; | |
414 | goto fail; | |
415 | } | |
416 | ||
d1a717bd | 417 | hdmi_connector->hdmi = hdmi; |
dada25bd | 418 | INIT_WORK(&hdmi_connector->hpd_work, hotplug_work); |
a3376e3e RC |
419 | |
420 | connector = &hdmi_connector->base; | |
c8afe684 | 421 | |
a3376e3e | 422 | drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs, |
c8afe684 RC |
423 | DRM_MODE_CONNECTOR_HDMIA); |
424 | drm_connector_helper_add(connector, &hdmi_connector_helper_funcs); | |
425 | ||
3189650d RC |
426 | connector->polled = DRM_CONNECTOR_POLL_CONNECT | |
427 | DRM_CONNECTOR_POLL_DISCONNECT; | |
c8afe684 | 428 | |
cddfaebd | 429 | connector->interlace_allowed = 0; |
c8afe684 RC |
430 | connector->doublescan_allowed = 0; |
431 | ||
34ea3d38 | 432 | drm_connector_register(connector); |
c8afe684 | 433 | |
c8afe684 RC |
434 | ret = hpd_enable(hdmi_connector); |
435 | if (ret) { | |
a3376e3e | 436 | dev_err(hdmi->dev->dev, "failed to enable HPD: %d\n", ret); |
c8afe684 RC |
437 | goto fail; |
438 | } | |
439 | ||
a3376e3e | 440 | drm_mode_connector_attach_encoder(connector, hdmi->encoder); |
c8afe684 RC |
441 | |
442 | return connector; | |
443 | ||
444 | fail: | |
445 | if (connector) | |
446 | hdmi_connector_destroy(connector); | |
447 | ||
448 | return ERR_PTR(ret); | |
449 | } |