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>
19 #include <linux/pinctrl/consumer.h>
24 struct hdmi_connector
{
25 struct drm_connector base
;
27 struct work_struct hpd_work
;
29 #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
31 static void msm_hdmi_phy_reset(struct hdmi
*hdmi
)
35 val
= hdmi_read(hdmi
, REG_HDMI_PHY_CTRL
);
37 if (val
& HDMI_PHY_CTRL_SW_RESET_LOW
) {
39 hdmi_write(hdmi
, REG_HDMI_PHY_CTRL
,
40 val
& ~HDMI_PHY_CTRL_SW_RESET
);
43 hdmi_write(hdmi
, REG_HDMI_PHY_CTRL
,
44 val
| HDMI_PHY_CTRL_SW_RESET
);
47 if (val
& HDMI_PHY_CTRL_SW_RESET_PLL_LOW
) {
49 hdmi_write(hdmi
, REG_HDMI_PHY_CTRL
,
50 val
& ~HDMI_PHY_CTRL_SW_RESET_PLL
);
53 hdmi_write(hdmi
, REG_HDMI_PHY_CTRL
,
54 val
| HDMI_PHY_CTRL_SW_RESET_PLL
);
59 if (val
& HDMI_PHY_CTRL_SW_RESET_LOW
) {
61 hdmi_write(hdmi
, REG_HDMI_PHY_CTRL
,
62 val
| HDMI_PHY_CTRL_SW_RESET
);
65 hdmi_write(hdmi
, REG_HDMI_PHY_CTRL
,
66 val
& ~HDMI_PHY_CTRL_SW_RESET
);
69 if (val
& HDMI_PHY_CTRL_SW_RESET_PLL_LOW
) {
71 hdmi_write(hdmi
, REG_HDMI_PHY_CTRL
,
72 val
| HDMI_PHY_CTRL_SW_RESET_PLL
);
75 hdmi_write(hdmi
, REG_HDMI_PHY_CTRL
,
76 val
& ~HDMI_PHY_CTRL_SW_RESET_PLL
);
80 static int gpio_config(struct hdmi
*hdmi
, bool on
)
82 struct device
*dev
= &hdmi
->pdev
->dev
;
83 const struct hdmi_platform_config
*config
= hdmi
->config
;
87 for (i
= 0; i
< HDMI_MAX_NUM_GPIO
; i
++) {
88 struct hdmi_gpio_data gpio
= config
->gpios
[i
];
91 ret
= gpio_request(gpio
.num
, gpio
.label
);
94 "'%s'(%d) gpio_request failed: %d\n",
95 gpio
.label
, gpio
.num
, ret
);
100 gpio_direction_output(gpio
.num
,
103 gpio_direction_input(gpio
.num
);
104 gpio_set_value_cansleep(gpio
.num
,
112 for (i
= 0; i
< HDMI_MAX_NUM_GPIO
; i
++) {
113 struct hdmi_gpio_data gpio
= config
->gpios
[i
];
119 int value
= gpio
.value
? 0 : 1;
121 gpio_set_value_cansleep(gpio
.num
, value
);
133 if (config
->gpios
[i
].num
!= -1)
134 gpio_free(config
->gpios
[i
].num
);
140 static int hpd_enable(struct hdmi_connector
*hdmi_connector
)
142 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
143 const struct hdmi_platform_config
*config
= hdmi
->config
;
144 struct device
*dev
= &hdmi
->pdev
->dev
;
149 for (i
= 0; i
< config
->hpd_reg_cnt
; i
++) {
150 ret
= regulator_enable(hdmi
->hpd_regs
[i
]);
152 dev_err(dev
, "failed to enable hpd regulator: %s (%d)\n",
153 config
->hpd_reg_names
[i
], ret
);
158 ret
= pinctrl_pm_select_default_state(dev
);
160 dev_err(dev
, "pinctrl state chg failed: %d\n", ret
);
164 ret
= gpio_config(hdmi
, true);
166 dev_err(dev
, "failed to configure GPIOs: %d\n", ret
);
170 for (i
= 0; i
< config
->hpd_clk_cnt
; i
++) {
171 if (config
->hpd_freq
&& config
->hpd_freq
[i
]) {
172 ret
= clk_set_rate(hdmi
->hpd_clks
[i
],
173 config
->hpd_freq
[i
]);
175 dev_warn(dev
, "failed to set clk %s (%d)\n",
176 config
->hpd_clk_names
[i
], ret
);
179 ret
= clk_prepare_enable(hdmi
->hpd_clks
[i
]);
181 dev_err(dev
, "failed to enable hpd clk: %s (%d)\n",
182 config
->hpd_clk_names
[i
], ret
);
187 msm_hdmi_set_mode(hdmi
, false);
188 msm_hdmi_phy_reset(hdmi
);
189 msm_hdmi_set_mode(hdmi
, true);
191 hdmi_write(hdmi
, REG_HDMI_USEC_REFTIMER
, 0x0001001b);
193 /* enable HPD events: */
194 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
,
195 HDMI_HPD_INT_CTRL_INT_CONNECT
|
196 HDMI_HPD_INT_CTRL_INT_EN
);
198 /* set timeout to 4.1ms (max) for hardware debounce */
199 spin_lock_irqsave(&hdmi
->reg_lock
, flags
);
200 hpd_ctrl
= hdmi_read(hdmi
, REG_HDMI_HPD_CTRL
);
201 hpd_ctrl
|= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
203 /* Toggle HPD circuit to trigger HPD sense */
204 hdmi_write(hdmi
, REG_HDMI_HPD_CTRL
,
205 ~HDMI_HPD_CTRL_ENABLE
& hpd_ctrl
);
206 hdmi_write(hdmi
, REG_HDMI_HPD_CTRL
,
207 HDMI_HPD_CTRL_ENABLE
| hpd_ctrl
);
208 spin_unlock_irqrestore(&hdmi
->reg_lock
, flags
);
216 static void hdp_disable(struct hdmi_connector
*hdmi_connector
)
218 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
219 const struct hdmi_platform_config
*config
= hdmi
->config
;
220 struct device
*dev
= &hdmi
->pdev
->dev
;
223 /* Disable HPD interrupt */
224 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
, 0);
226 msm_hdmi_set_mode(hdmi
, false);
228 for (i
= 0; i
< config
->hpd_clk_cnt
; i
++)
229 clk_disable_unprepare(hdmi
->hpd_clks
[i
]);
231 ret
= gpio_config(hdmi
, false);
233 dev_warn(dev
, "failed to unconfigure GPIOs: %d\n", ret
);
235 ret
= pinctrl_pm_select_sleep_state(dev
);
237 dev_warn(dev
, "pinctrl state chg failed: %d\n", ret
);
239 for (i
= 0; i
< config
->hpd_reg_cnt
; i
++) {
240 ret
= regulator_disable(hdmi
->hpd_regs
[i
]);
242 dev_warn(dev
, "failed to disable hpd regulator: %s (%d)\n",
243 config
->hpd_reg_names
[i
], ret
);
248 msm_hdmi_hotplug_work(struct work_struct
*work
)
250 struct hdmi_connector
*hdmi_connector
=
251 container_of(work
, struct hdmi_connector
, hpd_work
);
252 struct drm_connector
*connector
= &hdmi_connector
->base
;
253 drm_helper_hpd_irq_event(connector
->dev
);
256 void msm_hdmi_connector_irq(struct drm_connector
*connector
)
258 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
259 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
260 uint32_t hpd_int_status
, hpd_int_ctrl
;
263 hpd_int_status
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_STATUS
);
264 hpd_int_ctrl
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_CTRL
);
266 if ((hpd_int_ctrl
& HDMI_HPD_INT_CTRL_INT_EN
) &&
267 (hpd_int_status
& HDMI_HPD_INT_STATUS_INT
)) {
268 bool detected
= !!(hpd_int_status
& HDMI_HPD_INT_STATUS_CABLE_DETECTED
);
270 /* ack & disable (temporarily) HPD events: */
271 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
,
272 HDMI_HPD_INT_CTRL_INT_ACK
);
274 DBG("status=%04x, ctrl=%04x", hpd_int_status
, hpd_int_ctrl
);
276 /* detect disconnect if we are connected or visa versa: */
277 hpd_int_ctrl
= HDMI_HPD_INT_CTRL_INT_EN
;
279 hpd_int_ctrl
|= HDMI_HPD_INT_CTRL_INT_CONNECT
;
280 hdmi_write(hdmi
, REG_HDMI_HPD_INT_CTRL
, hpd_int_ctrl
);
282 queue_work(hdmi
->workq
, &hdmi_connector
->hpd_work
);
286 static enum drm_connector_status
detect_reg(struct hdmi
*hdmi
)
288 uint32_t hpd_int_status
= hdmi_read(hdmi
, REG_HDMI_HPD_INT_STATUS
);
289 return (hpd_int_status
& HDMI_HPD_INT_STATUS_CABLE_DETECTED
) ?
290 connector_status_connected
: connector_status_disconnected
;
293 #define HPD_GPIO_INDEX 2
294 static enum drm_connector_status
detect_gpio(struct hdmi
*hdmi
)
296 const struct hdmi_platform_config
*config
= hdmi
->config
;
297 struct hdmi_gpio_data hpd_gpio
= config
->gpios
[HPD_GPIO_INDEX
];
299 return gpio_get_value(hpd_gpio
.num
) ?
300 connector_status_connected
:
301 connector_status_disconnected
;
304 static enum drm_connector_status
hdmi_connector_detect(
305 struct drm_connector
*connector
, bool force
)
307 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
308 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
309 const struct hdmi_platform_config
*config
= hdmi
->config
;
310 struct hdmi_gpio_data hpd_gpio
= config
->gpios
[HPD_GPIO_INDEX
];
311 enum drm_connector_status stat_gpio
, stat_reg
;
315 * some platforms may not have hpd gpio. Rely only on the status
316 * provided by REG_HDMI_HPD_INT_STATUS in this case.
318 if (hpd_gpio
.num
== -1)
319 return detect_reg(hdmi
);
322 stat_gpio
= detect_gpio(hdmi
);
323 stat_reg
= detect_reg(hdmi
);
325 if (stat_gpio
== stat_reg
)
331 /* the status we get from reading gpio seems to be more reliable,
332 * so trust that one the most if we didn't manage to get hdmi and
333 * gpio status to agree:
335 if (stat_gpio
!= stat_reg
) {
336 DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg
);
337 DBG("hpd gpio tells us: %d", stat_gpio
);
343 static void hdmi_connector_destroy(struct drm_connector
*connector
)
345 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
347 hdp_disable(hdmi_connector
);
349 drm_connector_cleanup(connector
);
351 kfree(hdmi_connector
);
354 static int msm_hdmi_connector_get_modes(struct drm_connector
*connector
)
356 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
357 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
362 hdmi_ctrl
= hdmi_read(hdmi
, REG_HDMI_CTRL
);
363 hdmi_write(hdmi
, REG_HDMI_CTRL
, hdmi_ctrl
| HDMI_CTRL_ENABLE
);
365 edid
= drm_get_edid(connector
, hdmi
->i2c
);
367 hdmi_write(hdmi
, REG_HDMI_CTRL
, hdmi_ctrl
);
369 hdmi
->hdmi_mode
= drm_detect_hdmi_monitor(edid
);
370 drm_mode_connector_update_edid_property(connector
, edid
);
373 ret
= drm_add_edid_modes(connector
, edid
);
380 static int msm_hdmi_connector_mode_valid(struct drm_connector
*connector
,
381 struct drm_display_mode
*mode
)
383 struct hdmi_connector
*hdmi_connector
= to_hdmi_connector(connector
);
384 struct hdmi
*hdmi
= hdmi_connector
->hdmi
;
385 const struct hdmi_platform_config
*config
= hdmi
->config
;
386 struct msm_drm_private
*priv
= connector
->dev
->dev_private
;
387 struct msm_kms
*kms
= priv
->kms
;
388 long actual
, requested
;
390 requested
= 1000 * mode
->clock
;
391 actual
= kms
->funcs
->round_pixclk(kms
,
392 requested
, hdmi_connector
->hdmi
->encoder
);
394 /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
395 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
398 if (config
->pwr_clk_cnt
> 0)
399 actual
= clk_round_rate(hdmi
->pwr_clks
[0], actual
);
401 DBG("requested=%ld, actual=%ld", requested
, actual
);
403 if (actual
!= requested
)
404 return MODE_CLOCK_RANGE
;
409 static const struct drm_connector_funcs hdmi_connector_funcs
= {
410 .dpms
= drm_atomic_helper_connector_dpms
,
411 .detect
= hdmi_connector_detect
,
412 .fill_modes
= drm_helper_probe_single_connector_modes
,
413 .destroy
= hdmi_connector_destroy
,
414 .reset
= drm_atomic_helper_connector_reset
,
415 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
416 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
419 static const struct drm_connector_helper_funcs msm_hdmi_connector_helper_funcs
= {
420 .get_modes
= msm_hdmi_connector_get_modes
,
421 .mode_valid
= msm_hdmi_connector_mode_valid
,
424 /* initialize connector */
425 struct drm_connector
*msm_hdmi_connector_init(struct hdmi
*hdmi
)
427 struct drm_connector
*connector
= NULL
;
428 struct hdmi_connector
*hdmi_connector
;
431 hdmi_connector
= kzalloc(sizeof(*hdmi_connector
), GFP_KERNEL
);
433 return ERR_PTR(-ENOMEM
);
435 hdmi_connector
->hdmi
= hdmi
;
436 INIT_WORK(&hdmi_connector
->hpd_work
, msm_hdmi_hotplug_work
);
438 connector
= &hdmi_connector
->base
;
440 drm_connector_init(hdmi
->dev
, connector
, &hdmi_connector_funcs
,
441 DRM_MODE_CONNECTOR_HDMIA
);
442 drm_connector_helper_add(connector
, &msm_hdmi_connector_helper_funcs
);
444 connector
->polled
= DRM_CONNECTOR_POLL_CONNECT
|
445 DRM_CONNECTOR_POLL_DISCONNECT
;
447 connector
->interlace_allowed
= 0;
448 connector
->doublescan_allowed
= 0;
450 ret
= hpd_enable(hdmi_connector
);
452 dev_err(&hdmi
->pdev
->dev
, "failed to enable HPD: %d\n", ret
);
456 drm_mode_connector_attach_encoder(connector
, hdmi
->encoder
);