1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
7 #include <linux/gpio.h>
8 #include <linux/pinctrl/consumer.h>
13 struct hdmi_connector {
14 struct drm_connector base;
16 struct work_struct hpd_work;
18 #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
20 static void msm_hdmi_phy_reset(struct hdmi *hdmi)
24 val = hdmi_read(hdmi, REG_HDMI_PHY_CTRL);
26 if (val & HDMI_PHY_CTRL_SW_RESET_LOW) {
28 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
29 val & ~HDMI_PHY_CTRL_SW_RESET);
32 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
33 val | HDMI_PHY_CTRL_SW_RESET);
36 if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW) {
38 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
39 val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
42 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
43 val | HDMI_PHY_CTRL_SW_RESET_PLL);
48 if (val & HDMI_PHY_CTRL_SW_RESET_LOW) {
50 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
51 val | HDMI_PHY_CTRL_SW_RESET);
54 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
55 val & ~HDMI_PHY_CTRL_SW_RESET);
58 if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW) {
60 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
61 val | HDMI_PHY_CTRL_SW_RESET_PLL);
64 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
65 val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
69 static int gpio_config(struct hdmi *hdmi, bool on)
71 struct device *dev = &hdmi->pdev->dev;
72 const struct hdmi_platform_config *config = hdmi->config;
76 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
77 struct hdmi_gpio_data gpio = config->gpios[i];
80 ret = gpio_request(gpio.num, gpio.label);
83 "'%s'(%d) gpio_request failed: %d\n",
84 gpio.label, gpio.num, ret);
89 gpio_direction_output(gpio.num,
92 gpio_direction_input(gpio.num);
93 gpio_set_value_cansleep(gpio.num,
101 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
102 struct hdmi_gpio_data gpio = config->gpios[i];
108 int value = gpio.value ? 0 : 1;
110 gpio_set_value_cansleep(gpio.num, value);
122 if (config->gpios[i].num != -1)
123 gpio_free(config->gpios[i].num);
129 static void enable_hpd_clocks(struct hdmi *hdmi, bool enable)
131 const struct hdmi_platform_config *config = hdmi->config;
132 struct device *dev = &hdmi->pdev->dev;
136 for (i = 0; i < config->hpd_clk_cnt; i++) {
137 if (config->hpd_freq && config->hpd_freq[i]) {
138 ret = clk_set_rate(hdmi->hpd_clks[i],
139 config->hpd_freq[i]);
142 "failed to set clk %s (%d)\n",
143 config->hpd_clk_names[i], ret);
146 ret = clk_prepare_enable(hdmi->hpd_clks[i]);
149 "failed to enable hpd clk: %s (%d)\n",
150 config->hpd_clk_names[i], ret);
154 for (i = config->hpd_clk_cnt - 1; i >= 0; i--)
155 clk_disable_unprepare(hdmi->hpd_clks[i]);
159 int msm_hdmi_hpd_enable(struct drm_connector *connector)
161 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
162 struct hdmi *hdmi = hdmi_connector->hdmi;
163 const struct hdmi_platform_config *config = hdmi->config;
164 struct device *dev = &hdmi->pdev->dev;
169 for (i = 0; i < config->hpd_reg_cnt; i++) {
170 ret = regulator_enable(hdmi->hpd_regs[i]);
172 DRM_DEV_ERROR(dev, "failed to enable hpd regulator: %s (%d)\n",
173 config->hpd_reg_names[i], ret);
178 ret = pinctrl_pm_select_default_state(dev);
180 DRM_DEV_ERROR(dev, "pinctrl state chg failed: %d\n", ret);
184 ret = gpio_config(hdmi, true);
186 DRM_DEV_ERROR(dev, "failed to configure GPIOs: %d\n", ret);
190 pm_runtime_get_sync(dev);
191 enable_hpd_clocks(hdmi, true);
193 msm_hdmi_set_mode(hdmi, false);
194 msm_hdmi_phy_reset(hdmi);
195 msm_hdmi_set_mode(hdmi, true);
197 hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
199 /* enable HPD events: */
200 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
201 HDMI_HPD_INT_CTRL_INT_CONNECT |
202 HDMI_HPD_INT_CTRL_INT_EN);
204 /* set timeout to 4.1ms (max) for hardware debounce */
205 spin_lock_irqsave(&hdmi->reg_lock, flags);
206 hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
207 hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
209 /* Toggle HPD circuit to trigger HPD sense */
210 hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
211 ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
212 hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
213 HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
214 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
222 static void hdp_disable(struct hdmi_connector *hdmi_connector)
224 struct hdmi *hdmi = hdmi_connector->hdmi;
225 const struct hdmi_platform_config *config = hdmi->config;
226 struct device *dev = &hdmi->pdev->dev;
229 /* Disable HPD interrupt */
230 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
232 msm_hdmi_set_mode(hdmi, false);
234 enable_hpd_clocks(hdmi, false);
235 pm_runtime_put_autosuspend(dev);
237 ret = gpio_config(hdmi, false);
239 dev_warn(dev, "failed to unconfigure GPIOs: %d\n", ret);
241 ret = pinctrl_pm_select_sleep_state(dev);
243 dev_warn(dev, "pinctrl state chg failed: %d\n", ret);
245 for (i = 0; i < config->hpd_reg_cnt; i++) {
246 ret = regulator_disable(hdmi->hpd_regs[i]);
248 dev_warn(dev, "failed to disable hpd regulator: %s (%d)\n",
249 config->hpd_reg_names[i], ret);
254 msm_hdmi_hotplug_work(struct work_struct *work)
256 struct hdmi_connector *hdmi_connector =
257 container_of(work, struct hdmi_connector, hpd_work);
258 struct drm_connector *connector = &hdmi_connector->base;
259 drm_helper_hpd_irq_event(connector->dev);
262 void msm_hdmi_connector_irq(struct drm_connector *connector)
264 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
265 struct hdmi *hdmi = hdmi_connector->hdmi;
266 uint32_t hpd_int_status, hpd_int_ctrl;
269 hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
270 hpd_int_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
272 if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
273 (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
274 bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
276 /* ack & disable (temporarily) HPD events: */
277 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
278 HDMI_HPD_INT_CTRL_INT_ACK);
280 DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
282 /* detect disconnect if we are connected or visa versa: */
283 hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
285 hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
286 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
288 queue_work(hdmi->workq, &hdmi_connector->hpd_work);
292 static enum drm_connector_status detect_reg(struct hdmi *hdmi)
294 uint32_t hpd_int_status;
296 pm_runtime_get_sync(&hdmi->pdev->dev);
297 enable_hpd_clocks(hdmi, true);
299 hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
301 enable_hpd_clocks(hdmi, false);
302 pm_runtime_put_autosuspend(&hdmi->pdev->dev);
304 return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
305 connector_status_connected : connector_status_disconnected;
308 #define HPD_GPIO_INDEX 2
309 static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
311 const struct hdmi_platform_config *config = hdmi->config;
312 struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
314 return gpio_get_value(hpd_gpio.num) ?
315 connector_status_connected :
316 connector_status_disconnected;
319 static enum drm_connector_status hdmi_connector_detect(
320 struct drm_connector *connector, bool force)
322 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
323 struct hdmi *hdmi = hdmi_connector->hdmi;
324 const struct hdmi_platform_config *config = hdmi->config;
325 struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
326 enum drm_connector_status stat_gpio, stat_reg;
330 * some platforms may not have hpd gpio. Rely only on the status
331 * provided by REG_HDMI_HPD_INT_STATUS in this case.
333 if (hpd_gpio.num == -1)
334 return detect_reg(hdmi);
337 stat_gpio = detect_gpio(hdmi);
338 stat_reg = detect_reg(hdmi);
340 if (stat_gpio == stat_reg)
346 /* the status we get from reading gpio seems to be more reliable,
347 * so trust that one the most if we didn't manage to get hdmi and
348 * gpio status to agree:
350 if (stat_gpio != stat_reg) {
351 DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
352 DBG("hpd gpio tells us: %d", stat_gpio);
358 static void hdmi_connector_destroy(struct drm_connector *connector)
360 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
362 hdp_disable(hdmi_connector);
364 drm_connector_cleanup(connector);
366 kfree(hdmi_connector);
369 static int msm_hdmi_connector_get_modes(struct drm_connector *connector)
371 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
372 struct hdmi *hdmi = hdmi_connector->hdmi;
377 hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
378 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
380 edid = drm_get_edid(connector, hdmi->i2c);
382 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
384 hdmi->hdmi_mode = drm_detect_hdmi_monitor(edid);
385 drm_connector_update_edid_property(connector, edid);
388 ret = drm_add_edid_modes(connector, edid);
395 static int msm_hdmi_connector_mode_valid(struct drm_connector *connector,
396 struct drm_display_mode *mode)
398 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
399 struct hdmi *hdmi = hdmi_connector->hdmi;
400 const struct hdmi_platform_config *config = hdmi->config;
401 struct msm_drm_private *priv = connector->dev->dev_private;
402 struct msm_kms *kms = priv->kms;
403 long actual, requested;
405 requested = 1000 * mode->clock;
406 actual = kms->funcs->round_pixclk(kms,
407 requested, hdmi_connector->hdmi->encoder);
409 /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
410 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
413 if (config->pwr_clk_cnt > 0)
414 actual = clk_round_rate(hdmi->pwr_clks[0], actual);
416 DBG("requested=%ld, actual=%ld", requested, actual);
418 if (actual != requested)
419 return MODE_CLOCK_RANGE;
424 static const struct drm_connector_funcs hdmi_connector_funcs = {
425 .detect = hdmi_connector_detect,
426 .fill_modes = drm_helper_probe_single_connector_modes,
427 .destroy = hdmi_connector_destroy,
428 .reset = drm_atomic_helper_connector_reset,
429 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
430 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
433 static const struct drm_connector_helper_funcs msm_hdmi_connector_helper_funcs = {
434 .get_modes = msm_hdmi_connector_get_modes,
435 .mode_valid = msm_hdmi_connector_mode_valid,
438 /* initialize connector */
439 struct drm_connector *msm_hdmi_connector_init(struct hdmi *hdmi)
441 struct drm_connector *connector = NULL;
442 struct hdmi_connector *hdmi_connector;
444 hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
446 return ERR_PTR(-ENOMEM);
448 hdmi_connector->hdmi = hdmi;
449 INIT_WORK(&hdmi_connector->hpd_work, msm_hdmi_hotplug_work);
451 connector = &hdmi_connector->base;
453 drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
454 DRM_MODE_CONNECTOR_HDMIA);
455 drm_connector_helper_add(connector, &msm_hdmi_connector_helper_funcs);
457 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
458 DRM_CONNECTOR_POLL_DISCONNECT;
460 connector->interlace_allowed = 0;
461 connector->doublescan_allowed = 0;
463 drm_connector_attach_encoder(connector, hdmi->encoder);