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 void enable_hpd_clocks(struct hdmi *hdmi, bool enable)
142 const struct hdmi_platform_config *config = hdmi->config;
143 struct device *dev = &hdmi->pdev->dev;
147 for (i = 0; i < config->hpd_clk_cnt; i++) {
148 if (config->hpd_freq && config->hpd_freq[i]) {
149 ret = clk_set_rate(hdmi->hpd_clks[i],
150 config->hpd_freq[i]);
153 "failed to set clk %s (%d)\n",
154 config->hpd_clk_names[i], ret);
157 ret = clk_prepare_enable(hdmi->hpd_clks[i]);
160 "failed to enable hpd clk: %s (%d)\n",
161 config->hpd_clk_names[i], ret);
165 for (i = config->hpd_clk_cnt - 1; i >= 0; i--)
166 clk_disable_unprepare(hdmi->hpd_clks[i]);
170 static int hpd_enable(struct hdmi_connector *hdmi_connector)
172 struct hdmi *hdmi = hdmi_connector->hdmi;
173 const struct hdmi_platform_config *config = hdmi->config;
174 struct device *dev = &hdmi->pdev->dev;
179 for (i = 0; i < config->hpd_reg_cnt; i++) {
180 ret = regulator_enable(hdmi->hpd_regs[i]);
182 dev_err(dev, "failed to enable hpd regulator: %s (%d)\n",
183 config->hpd_reg_names[i], ret);
188 ret = pinctrl_pm_select_default_state(dev);
190 dev_err(dev, "pinctrl state chg failed: %d\n", ret);
194 ret = gpio_config(hdmi, true);
196 dev_err(dev, "failed to configure GPIOs: %d\n", ret);
200 pm_runtime_get_sync(dev);
201 enable_hpd_clocks(hdmi, true);
203 msm_hdmi_set_mode(hdmi, false);
204 msm_hdmi_phy_reset(hdmi);
205 msm_hdmi_set_mode(hdmi, true);
207 hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
209 /* enable HPD events: */
210 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
211 HDMI_HPD_INT_CTRL_INT_CONNECT |
212 HDMI_HPD_INT_CTRL_INT_EN);
214 /* set timeout to 4.1ms (max) for hardware debounce */
215 spin_lock_irqsave(&hdmi->reg_lock, flags);
216 hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
217 hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
219 /* Toggle HPD circuit to trigger HPD sense */
220 hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
221 ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
222 hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
223 HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
224 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
232 static void hdp_disable(struct hdmi_connector *hdmi_connector)
234 struct hdmi *hdmi = hdmi_connector->hdmi;
235 const struct hdmi_platform_config *config = hdmi->config;
236 struct device *dev = &hdmi->pdev->dev;
239 /* Disable HPD interrupt */
240 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
242 msm_hdmi_set_mode(hdmi, false);
244 enable_hpd_clocks(hdmi, false);
245 pm_runtime_put_autosuspend(dev);
247 ret = gpio_config(hdmi, false);
249 dev_warn(dev, "failed to unconfigure GPIOs: %d\n", ret);
251 ret = pinctrl_pm_select_sleep_state(dev);
253 dev_warn(dev, "pinctrl state chg failed: %d\n", ret);
255 for (i = 0; i < config->hpd_reg_cnt; i++) {
256 ret = regulator_disable(hdmi->hpd_regs[i]);
258 dev_warn(dev, "failed to disable hpd regulator: %s (%d)\n",
259 config->hpd_reg_names[i], ret);
264 msm_hdmi_hotplug_work(struct work_struct *work)
266 struct hdmi_connector *hdmi_connector =
267 container_of(work, struct hdmi_connector, hpd_work);
268 struct drm_connector *connector = &hdmi_connector->base;
269 drm_helper_hpd_irq_event(connector->dev);
272 void msm_hdmi_connector_irq(struct drm_connector *connector)
274 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
275 struct hdmi *hdmi = hdmi_connector->hdmi;
276 uint32_t hpd_int_status, hpd_int_ctrl;
279 hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
280 hpd_int_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
282 if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
283 (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
284 bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
286 /* ack & disable (temporarily) HPD events: */
287 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
288 HDMI_HPD_INT_CTRL_INT_ACK);
290 DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
292 /* detect disconnect if we are connected or visa versa: */
293 hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
295 hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
296 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
298 queue_work(hdmi->workq, &hdmi_connector->hpd_work);
302 static enum drm_connector_status detect_reg(struct hdmi *hdmi)
304 uint32_t hpd_int_status;
306 pm_runtime_get_sync(&hdmi->pdev->dev);
307 enable_hpd_clocks(hdmi, true);
309 hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
311 enable_hpd_clocks(hdmi, false);
312 pm_runtime_put_autosuspend(&hdmi->pdev->dev);
314 return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
315 connector_status_connected : connector_status_disconnected;
318 #define HPD_GPIO_INDEX 2
319 static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
321 const struct hdmi_platform_config *config = hdmi->config;
322 struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
324 return gpio_get_value(hpd_gpio.num) ?
325 connector_status_connected :
326 connector_status_disconnected;
329 static enum drm_connector_status hdmi_connector_detect(
330 struct drm_connector *connector, bool force)
332 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
333 struct hdmi *hdmi = hdmi_connector->hdmi;
334 const struct hdmi_platform_config *config = hdmi->config;
335 struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
336 enum drm_connector_status stat_gpio, stat_reg;
340 * some platforms may not have hpd gpio. Rely only on the status
341 * provided by REG_HDMI_HPD_INT_STATUS in this case.
343 if (hpd_gpio.num == -1)
344 return detect_reg(hdmi);
347 stat_gpio = detect_gpio(hdmi);
348 stat_reg = detect_reg(hdmi);
350 if (stat_gpio == stat_reg)
356 /* the status we get from reading gpio seems to be more reliable,
357 * so trust that one the most if we didn't manage to get hdmi and
358 * gpio status to agree:
360 if (stat_gpio != stat_reg) {
361 DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
362 DBG("hpd gpio tells us: %d", stat_gpio);
368 static void hdmi_connector_destroy(struct drm_connector *connector)
370 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
372 hdp_disable(hdmi_connector);
374 drm_connector_cleanup(connector);
376 kfree(hdmi_connector);
379 static int msm_hdmi_connector_get_modes(struct drm_connector *connector)
381 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
382 struct hdmi *hdmi = hdmi_connector->hdmi;
387 hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
388 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
390 edid = drm_get_edid(connector, hdmi->i2c);
392 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
394 hdmi->hdmi_mode = drm_detect_hdmi_monitor(edid);
395 drm_mode_connector_update_edid_property(connector, edid);
398 ret = drm_add_edid_modes(connector, edid);
405 static int msm_hdmi_connector_mode_valid(struct drm_connector *connector,
406 struct drm_display_mode *mode)
408 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
409 struct hdmi *hdmi = hdmi_connector->hdmi;
410 const struct hdmi_platform_config *config = hdmi->config;
411 struct msm_drm_private *priv = connector->dev->dev_private;
412 struct msm_kms *kms = priv->kms;
413 long actual, requested;
415 requested = 1000 * mode->clock;
416 actual = kms->funcs->round_pixclk(kms,
417 requested, hdmi_connector->hdmi->encoder);
419 /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
420 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
423 if (config->pwr_clk_cnt > 0)
424 actual = clk_round_rate(hdmi->pwr_clks[0], actual);
426 DBG("requested=%ld, actual=%ld", requested, actual);
428 if (actual != requested)
429 return MODE_CLOCK_RANGE;
434 static const struct drm_connector_funcs hdmi_connector_funcs = {
435 .detect = hdmi_connector_detect,
436 .fill_modes = drm_helper_probe_single_connector_modes,
437 .destroy = hdmi_connector_destroy,
438 .reset = drm_atomic_helper_connector_reset,
439 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
440 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
443 static const struct drm_connector_helper_funcs msm_hdmi_connector_helper_funcs = {
444 .get_modes = msm_hdmi_connector_get_modes,
445 .mode_valid = msm_hdmi_connector_mode_valid,
448 /* initialize connector */
449 struct drm_connector *msm_hdmi_connector_init(struct hdmi *hdmi)
451 struct drm_connector *connector = NULL;
452 struct hdmi_connector *hdmi_connector;
455 hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
457 return ERR_PTR(-ENOMEM);
459 hdmi_connector->hdmi = hdmi;
460 INIT_WORK(&hdmi_connector->hpd_work, msm_hdmi_hotplug_work);
462 connector = &hdmi_connector->base;
464 drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
465 DRM_MODE_CONNECTOR_HDMIA);
466 drm_connector_helper_add(connector, &msm_hdmi_connector_helper_funcs);
468 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
469 DRM_CONNECTOR_POLL_DISCONNECT;
471 connector->interlace_allowed = 0;
472 connector->doublescan_allowed = 0;
474 ret = hpd_enable(hdmi_connector);
476 dev_err(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
480 drm_mode_connector_attach_encoder(connector, hdmi->encoder);