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/>.
18 #include <linux/gpio.h>
23 struct hdmi_connector
{
24 struct drm_connector base
;
26 struct work_struct hpd_work
;
28 #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
30 static int gpio_config(struct hdmi
*hdmi
, bool on
)
32 struct drm_device
*dev
= hdmi
->dev
;
33 const struct hdmi_platform_config
*config
= hdmi
->config
;
37 ret
= gpio_request(config
->ddc_clk_gpio
, "HDMI_DDC_CLK");
39 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
40 "HDMI_DDC_CLK", config
->ddc_clk_gpio
, ret
);
43 gpio_set_value_cansleep(config
->ddc_clk_gpio
, 1);
45 ret
= gpio_request(config
->ddc_data_gpio
, "HDMI_DDC_DATA");
47 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
48 "HDMI_DDC_DATA", config
->ddc_data_gpio
, ret
);
51 gpio_set_value_cansleep(config
->ddc_data_gpio
, 1);
53 ret
= gpio_request(config
->hpd_gpio
, "HDMI_HPD");
55 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
56 "HDMI_HPD", config
->hpd_gpio
, ret
);
59 gpio_direction_input(config
->hpd_gpio
);
60 gpio_set_value_cansleep(config
->hpd_gpio
, 1);
62 if (config
->mux_en_gpio
!= -1) {
63 ret
= gpio_request(config
->mux_en_gpio
, "HDMI_MUX_EN");
65 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
66 "HDMI_MUX_SEL", config
->mux_en_gpio
, ret
);
69 gpio_set_value_cansleep(config
->mux_en_gpio
, 1);
72 if (config
->mux_sel_gpio
!= -1) {
73 ret
= gpio_request(config
->mux_sel_gpio
, "HDMI_MUX_SEL");
75 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
76 "HDMI_MUX_SEL", config
->mux_sel_gpio
, ret
);
79 gpio_set_value_cansleep(config
->mux_sel_gpio
, 0);
83 gpio_free(config
->ddc_clk_gpio
);
84 gpio_free(config
->ddc_data_gpio
);
85 gpio_free(config
->hpd_gpio
);
87 if (config
->mux_en_gpio
!= -1) {
88 gpio_set_value_cansleep(config
->mux_en_gpio
, 0);
89 gpio_free(config
->mux_en_gpio
);
92 if (config
->mux_sel_gpio
!= -1) {
93 gpio_set_value_cansleep(config
->mux_sel_gpio
, 1);
94 gpio_free(config
->mux_sel_gpio
);
102 if (config
->mux_en_gpio
!= -1)
103 gpio_free(config
->mux_en_gpio
);
105 gpio_free(config
->hpd_gpio
);
107 gpio_free(config
->ddc_data_gpio
);
109 gpio_free(config
->ddc_clk_gpio
);
114 static int hpd_enable(struct hdmi_connector
*hdmi_connector
)
116 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
117 const struct hdmi_platform_config
*config
= hdmi
->config
;
118 struct drm_device
*dev
= hdmi_connector
->base
.dev
;
119 struct hdmi_phy
*phy
= hdmi
->phy
;
123 ret
= gpio_config(hdmi
, true);
125 dev_err(dev
->dev
, "failed to configure GPIOs: %d\n", ret
);
129 for (i
= 0; i
< config
->hpd_clk_cnt
; i
++) {
130 ret
= clk_prepare_enable(hdmi
->hpd_clks
[i
]);
132 dev_err(dev
->dev
, "failed to enable hpd clk: %s (%d)\n",
133 config
->hpd_clk_names
[i
], ret
);
138 for (i
= 0; i
< config
->hpd_reg_cnt
; i
++) {
139 ret
= regulator_enable(hdmi
->hpd_regs
[i
]);
141 dev_err(dev
->dev
, "failed to enable hpd regulator: %s (%d)\n",
142 config
->hpd_reg_names
[i
], ret
);
147 hdmi_set_mode(hdmi
, false);
148 phy
->funcs
->reset(phy
);
149 hdmi_set_mode(hdmi
, true);
151 hdmi_write(hdmi
, REG_HDMI_USEC_REFTIMER
, 0x0001001b);
153 /* enable HPD events: */
154 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
,
155 HDMI_HPD_INT_CTRL_INT_CONNECT
|
156 HDMI_HPD_INT_CTRL_INT_EN
);
158 /* set timeout to 4.1ms (max) for hardware debounce */
159 hpd_ctrl
= hdmi_read(hdmi
, REG_HDMI_HPD_CTRL
);
160 hpd_ctrl
|= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
162 /* Toggle HPD circuit to trigger HPD sense */
163 hdmi_write(hdmi
, REG_HDMI_HPD_CTRL
,
164 ~HDMI_HPD_CTRL_ENABLE
& hpd_ctrl
);
165 hdmi_write(hdmi
, REG_HDMI_HPD_CTRL
,
166 HDMI_HPD_CTRL_ENABLE
| hpd_ctrl
);
174 static int hdp_disable(struct hdmi_connector
*hdmi_connector
)
176 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
177 const struct hdmi_platform_config
*config
= hdmi
->config
;
178 struct drm_device
*dev
= hdmi_connector
->base
.dev
;
181 /* Disable HPD interrupt */
182 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
, 0);
184 hdmi_set_mode(hdmi
, false);
186 for (i
= 0; i
< config
->hpd_reg_cnt
; i
++) {
187 ret
= regulator_disable(hdmi
->hpd_regs
[i
]);
189 dev_err(dev
->dev
, "failed to disable hpd regulator: %s (%d)\n",
190 config
->hpd_reg_names
[i
], ret
);
195 for (i
= 0; i
< config
->hpd_clk_cnt
; i
++)
196 clk_disable_unprepare(hdmi
->hpd_clks
[i
]);
198 ret
= gpio_config(hdmi
, false);
200 dev_err(dev
->dev
, "failed to unconfigure GPIOs: %d\n", ret
);
211 hotplug_work(struct work_struct
*work
)
213 struct hdmi_connector
*hdmi_connector
=
214 container_of(work
, struct hdmi_connector
, hpd_work
);
215 struct drm_connector
*connector
= &hdmi_connector
->base
;
216 drm_helper_hpd_irq_event(connector
->dev
);
219 void hdmi_connector_irq(struct drm_connector
*connector
)
221 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
222 struct msm_drm_private
*priv
= connector
->dev
->dev_private
;
223 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
224 uint32_t hpd_int_status
, hpd_int_ctrl
;
227 hpd_int_status
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_STATUS
);
228 hpd_int_ctrl
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_CTRL
);
230 if ((hpd_int_ctrl
& HDMI_HPD_INT_CTRL_INT_EN
) &&
231 (hpd_int_status
& HDMI_HPD_INT_STATUS_INT
)) {
232 bool detected
= !!(hpd_int_status
& HDMI_HPD_INT_STATUS_CABLE_DETECTED
);
234 DBG("status=%04x, ctrl=%04x", hpd_int_status
, hpd_int_ctrl
);
237 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
,
238 hpd_int_ctrl
| HDMI_HPD_INT_CTRL_INT_ACK
);
240 /* detect disconnect if we are connected or visa versa: */
241 hpd_int_ctrl
= HDMI_HPD_INT_CTRL_INT_EN
;
243 hpd_int_ctrl
|= HDMI_HPD_INT_CTRL_INT_CONNECT
;
244 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
, hpd_int_ctrl
);
246 queue_work(priv
->wq
, &hdmi_connector
->hpd_work
);
250 static enum drm_connector_status
detect_reg(struct hdmi
*hdmi
)
252 uint32_t hpd_int_status
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_STATUS
);
253 return (hpd_int_status
& HDMI_HPD_INT_STATUS_CABLE_DETECTED
) ?
254 connector_status_connected
: connector_status_disconnected
;
257 static enum drm_connector_status
detect_gpio(struct hdmi
*hdmi
)
259 const struct hdmi_platform_config
*config
= hdmi
->config
;
260 return gpio_get_value(config
->hpd_gpio
) ?
261 connector_status_connected
:
262 connector_status_disconnected
;
265 static enum drm_connector_status
hdmi_connector_detect(
266 struct drm_connector
*connector
, bool force
)
268 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
269 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
270 enum drm_connector_status stat_gpio
, stat_reg
;
274 stat_gpio
= detect_gpio(hdmi
);
275 stat_reg
= detect_reg(hdmi
);
277 if (stat_gpio
== stat_reg
)
283 /* the status we get from reading gpio seems to be more reliable,
284 * so trust that one the most if we didn't manage to get hdmi and
285 * gpio status to agree:
287 if (stat_gpio
!= stat_reg
) {
288 DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg
);
289 DBG("hpd gpio tells us: %d", stat_gpio
);
295 static void hdmi_connector_destroy(struct drm_connector
*connector
)
297 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
299 hdp_disable(hdmi_connector
);
301 drm_connector_unregister(connector
);
302 drm_connector_cleanup(connector
);
304 hdmi_unreference(hdmi_connector
->hdmi
);
306 kfree(hdmi_connector
);
309 static int hdmi_connector_get_modes(struct drm_connector
*connector
)
311 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
312 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
317 hdmi_ctrl
= hdmi_read(hdmi
, REG_HDMI_CTRL
);
318 hdmi_write(hdmi
, REG_HDMI_CTRL
, hdmi_ctrl
| HDMI_CTRL_ENABLE
);
320 edid
= drm_get_edid(connector
, hdmi
->i2c
);
322 hdmi_write(hdmi
, REG_HDMI_CTRL
, hdmi_ctrl
);
324 drm_mode_connector_update_edid_property(connector
, edid
);
327 ret
= drm_add_edid_modes(connector
, edid
);
334 static int hdmi_connector_mode_valid(struct drm_connector
*connector
,
335 struct drm_display_mode
*mode
)
337 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
338 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
339 const struct hdmi_platform_config
*config
= hdmi
->config
;
340 struct msm_drm_private
*priv
= connector
->dev
->dev_private
;
341 struct msm_kms
*kms
= priv
->kms
;
342 long actual
, requested
;
344 requested
= 1000 * mode
->clock
;
345 actual
= kms
->funcs
->round_pixclk(kms
,
346 requested
, hdmi_connector
->hdmi
->encoder
);
348 /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
349 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
352 if (config
->pwr_clk_cnt
> 0)
353 actual
= clk_round_rate(hdmi
->pwr_clks
[0], actual
);
355 DBG("requested=%ld, actual=%ld", requested
, actual
);
357 if (actual
!= requested
)
358 return MODE_CLOCK_RANGE
;
363 static struct drm_encoder
*
364 hdmi_connector_best_encoder(struct drm_connector
*connector
)
366 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
367 return hdmi_connector
->hdmi
->encoder
;
370 static const struct drm_connector_funcs hdmi_connector_funcs
= {
371 .dpms
= drm_helper_connector_dpms
,
372 .detect
= hdmi_connector_detect
,
373 .fill_modes
= drm_helper_probe_single_connector_modes
,
374 .destroy
= hdmi_connector_destroy
,
377 static const struct drm_connector_helper_funcs hdmi_connector_helper_funcs
= {
378 .get_modes
= hdmi_connector_get_modes
,
379 .mode_valid
= hdmi_connector_mode_valid
,
380 .best_encoder
= hdmi_connector_best_encoder
,
383 /* initialize connector */
384 struct drm_connector
*hdmi_connector_init(struct hdmi
*hdmi
)
386 struct drm_connector
*connector
= NULL
;
387 struct hdmi_connector
*hdmi_connector
;
390 hdmi_connector
= kzalloc(sizeof(*hdmi_connector
), GFP_KERNEL
);
391 if (!hdmi_connector
) {
396 hdmi_connector
->hdmi
= hdmi_reference(hdmi
);
397 INIT_WORK(&hdmi_connector
->hpd_work
, hotplug_work
);
399 connector
= &hdmi_connector
->base
;
401 drm_connector_init(hdmi
->dev
, connector
, &hdmi_connector_funcs
,
402 DRM_MODE_CONNECTOR_HDMIA
);
403 drm_connector_helper_add(connector
, &hdmi_connector_helper_funcs
);
405 connector
->polled
= DRM_CONNECTOR_POLL_CONNECT
|
406 DRM_CONNECTOR_POLL_DISCONNECT
;
408 connector
->interlace_allowed
= 1;
409 connector
->doublescan_allowed
= 0;
411 drm_connector_register(connector
);
413 ret
= hpd_enable(hdmi_connector
);
415 dev_err(hdmi
->dev
->dev
, "failed to enable HPD: %d\n", ret
);
419 drm_mode_connector_attach_encoder(connector
, hdmi
->encoder
);
425 hdmi_connector_destroy(connector
);