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>
22 struct hdmi_connector
{
23 struct drm_connector base
;
26 #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
28 static int gpio_config(struct hdmi
*hdmi
, bool on
)
30 struct drm_device
*dev
= hdmi
->dev
;
31 struct hdmi_platform_config
*config
=
32 hdmi
->pdev
->dev
.platform_data
;
36 ret
= gpio_request(config
->ddc_clk_gpio
, "HDMI_DDC_CLK");
38 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
39 "HDMI_DDC_CLK", config
->ddc_clk_gpio
, ret
);
42 ret
= gpio_request(config
->ddc_data_gpio
, "HDMI_DDC_DATA");
44 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
45 "HDMI_DDC_DATA", config
->ddc_data_gpio
, ret
);
48 ret
= gpio_request(config
->hpd_gpio
, "HDMI_HPD");
50 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
51 "HDMI_HPD", config
->hpd_gpio
, ret
);
54 if (config
->pmic_gpio
!= -1) {
55 ret
= gpio_request(config
->pmic_gpio
, "PMIC_HDMI_MUX_SEL");
57 dev_err(dev
->dev
, "'%s'(%d) gpio_request failed: %d\n",
58 "PMIC_HDMI_MUX_SEL", config
->pmic_gpio
, ret
);
61 gpio_set_value_cansleep(config
->pmic_gpio
, 0);
65 gpio_free(config
->ddc_clk_gpio
);
66 gpio_free(config
->ddc_data_gpio
);
67 gpio_free(config
->hpd_gpio
);
69 if (config
->pmic_gpio
!= -1) {
70 gpio_set_value_cansleep(config
->pmic_gpio
, 1);
71 gpio_free(config
->pmic_gpio
);
79 gpio_free(config
->hpd_gpio
);
81 gpio_free(config
->ddc_data_gpio
);
83 gpio_free(config
->ddc_clk_gpio
);
88 static int hpd_enable(struct hdmi_connector
*hdmi_connector
)
90 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
91 struct drm_device
*dev
= hdmi_connector
->base
.dev
;
92 struct hdmi_phy
*phy
= hdmi
->phy
;
96 ret
= gpio_config(hdmi
, true);
98 dev_err(dev
->dev
, "failed to configure GPIOs: %d\n", ret
);
102 ret
= clk_prepare_enable(hdmi
->clk
);
104 dev_err(dev
->dev
, "failed to enable 'clk': %d\n", ret
);
108 ret
= clk_prepare_enable(hdmi
->m_pclk
);
110 dev_err(dev
->dev
, "failed to enable 'm_pclk': %d\n", ret
);
114 ret
= clk_prepare_enable(hdmi
->s_pclk
);
116 dev_err(dev
->dev
, "failed to enable 's_pclk': %d\n", ret
);
121 ret
= regulator_enable(hdmi
->mpp0
);
123 ret
= regulator_enable(hdmi
->mvs
);
125 dev_err(dev
->dev
, "failed to enable regulators: %d\n", ret
);
129 hdmi_set_mode(hdmi
, false);
130 phy
->funcs
->reset(phy
);
131 hdmi_set_mode(hdmi
, true);
133 hdmi_write(hdmi
, REG_HDMI_USEC_REFTIMER
, 0x0001001b);
135 /* enable HPD events: */
136 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
,
137 HDMI_HPD_INT_CTRL_INT_CONNECT
|
138 HDMI_HPD_INT_CTRL_INT_EN
);
140 /* set timeout to 4.1ms (max) for hardware debounce */
141 hpd_ctrl
= hdmi_read(hdmi
, REG_HDMI_HPD_CTRL
);
142 hpd_ctrl
|= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
144 /* Toggle HPD circuit to trigger HPD sense */
145 hdmi_write(hdmi
, REG_HDMI_HPD_CTRL
,
146 ~HDMI_HPD_CTRL_ENABLE
& hpd_ctrl
);
147 hdmi_write(hdmi
, REG_HDMI_HPD_CTRL
,
148 HDMI_HPD_CTRL_ENABLE
| hpd_ctrl
);
156 static int hdp_disable(struct hdmi_connector
*hdmi_connector
)
158 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
159 struct drm_device
*dev
= hdmi_connector
->base
.dev
;
162 /* Disable HPD interrupt */
163 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
, 0);
165 hdmi_set_mode(hdmi
, false);
168 ret
= regulator_disable(hdmi
->mpp0
);
170 ret
= regulator_disable(hdmi
->mvs
);
172 dev_err(dev
->dev
, "failed to enable regulators: %d\n", ret
);
176 clk_disable_unprepare(hdmi
->clk
);
177 clk_disable_unprepare(hdmi
->m_pclk
);
178 clk_disable_unprepare(hdmi
->s_pclk
);
180 ret
= gpio_config(hdmi
, false);
182 dev_err(dev
->dev
, "failed to unconfigure GPIOs: %d\n", ret
);
192 void hdmi_connector_irq(struct drm_connector
*connector
)
194 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
195 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
196 uint32_t hpd_int_status
, hpd_int_ctrl
;
199 hpd_int_status
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_STATUS
);
200 hpd_int_ctrl
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_CTRL
);
202 if ((hpd_int_ctrl
& HDMI_HPD_INT_CTRL_INT_EN
) &&
203 (hpd_int_status
& HDMI_HPD_INT_STATUS_INT
)) {
204 bool detected
= !!(hpd_int_status
& HDMI_HPD_INT_STATUS_CABLE_DETECTED
);
206 DBG("status=%04x, ctrl=%04x", hpd_int_status
, hpd_int_ctrl
);
209 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
,
210 hpd_int_ctrl
| HDMI_HPD_INT_CTRL_INT_ACK
);
212 drm_helper_hpd_irq_event(connector
->dev
);
214 /* detect disconnect if we are connected or visa versa: */
215 hpd_int_ctrl
= HDMI_HPD_INT_CTRL_INT_EN
;
217 hpd_int_ctrl
|= HDMI_HPD_INT_CTRL_INT_CONNECT
;
218 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
, hpd_int_ctrl
);
222 static enum drm_connector_status
hdmi_connector_detect(
223 struct drm_connector
*connector
, bool force
)
225 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
226 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
227 uint32_t hpd_int_status
;
230 hpd_int_status
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_STATUS
);
232 /* sense seems to in some cases be momentarily de-asserted, don't
233 * let that trick us into thinking the monitor is gone:
235 while (retry
-- && !(hpd_int_status
& HDMI_HPD_INT_STATUS_CABLE_DETECTED
)) {
237 hpd_int_status
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_STATUS
);
238 DBG("status=%08x", hpd_int_status
);
241 return (hpd_int_status
& HDMI_HPD_INT_STATUS_CABLE_DETECTED
) ?
242 connector_status_connected
: connector_status_disconnected
;
245 static void hdmi_connector_destroy(struct drm_connector
*connector
)
247 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
249 hdp_disable(hdmi_connector
);
251 drm_sysfs_connector_remove(connector
);
252 drm_connector_cleanup(connector
);
254 hdmi_unreference(hdmi_connector
->hdmi
);
256 kfree(hdmi_connector
);
259 static int hdmi_connector_get_modes(struct drm_connector
*connector
)
261 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
262 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
267 hdmi_ctrl
= hdmi_read(hdmi
, REG_HDMI_CTRL
);
268 hdmi_write(hdmi
, REG_HDMI_CTRL
, hdmi_ctrl
| HDMI_CTRL_ENABLE
);
270 edid
= drm_get_edid(connector
, hdmi
->i2c
);
272 hdmi_write(hdmi
, REG_HDMI_CTRL
, hdmi_ctrl
);
274 drm_mode_connector_update_edid_property(connector
, edid
);
277 ret
= drm_add_edid_modes(connector
, edid
);
284 static int hdmi_connector_mode_valid(struct drm_connector
*connector
,
285 struct drm_display_mode
*mode
)
287 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
288 struct msm_drm_private
*priv
= connector
->dev
->dev_private
;
289 struct msm_kms
*kms
= priv
->kms
;
290 long actual
, requested
;
292 requested
= 1000 * mode
->clock
;
293 actual
= kms
->funcs
->round_pixclk(kms
,
294 requested
, hdmi_connector
->hdmi
->encoder
);
296 DBG("requested=%ld, actual=%ld", requested
, actual
);
298 if (actual
!= requested
)
299 return MODE_CLOCK_RANGE
;
304 static struct drm_encoder
*
305 hdmi_connector_best_encoder(struct drm_connector
*connector
)
307 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
308 return hdmi_connector
->hdmi
->encoder
;
311 static const struct drm_connector_funcs hdmi_connector_funcs
= {
312 .dpms
= drm_helper_connector_dpms
,
313 .detect
= hdmi_connector_detect
,
314 .fill_modes
= drm_helper_probe_single_connector_modes
,
315 .destroy
= hdmi_connector_destroy
,
318 static const struct drm_connector_helper_funcs hdmi_connector_helper_funcs
= {
319 .get_modes
= hdmi_connector_get_modes
,
320 .mode_valid
= hdmi_connector_mode_valid
,
321 .best_encoder
= hdmi_connector_best_encoder
,
324 /* initialize connector */
325 struct drm_connector
*hdmi_connector_init(struct hdmi
*hdmi
)
327 struct drm_connector
*connector
= NULL
;
328 struct hdmi_connector
*hdmi_connector
;
331 hdmi_connector
= kzalloc(sizeof(*hdmi_connector
), GFP_KERNEL
);
332 if (!hdmi_connector
) {
337 hdmi_connector
->hdmi
= hdmi_reference(hdmi
);
339 connector
= &hdmi_connector
->base
;
341 drm_connector_init(hdmi
->dev
, connector
, &hdmi_connector_funcs
,
342 DRM_MODE_CONNECTOR_HDMIA
);
343 drm_connector_helper_add(connector
, &hdmi_connector_helper_funcs
);
345 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
347 connector
->interlace_allowed
= 1;
348 connector
->doublescan_allowed
= 0;
350 drm_sysfs_connector_add(connector
);
352 ret
= hpd_enable(hdmi_connector
);
354 dev_err(hdmi
->dev
->dev
, "failed to enable HPD: %d\n", ret
);
358 drm_mode_connector_attach_encoder(connector
, hdmi
->encoder
);
364 hdmi_connector_destroy(connector
);