hdmi_connector.c 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2013 Red Hat
 * Author: Rob Clark <robdclark@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/gpio.h>
19
#include <linux/pinctrl/consumer.h>
20

R
Rob Clark 已提交
21
#include "msm_kms.h"
22 23 24
#include "hdmi.h"

struct hdmi_connector {
R
Rob Clark 已提交
25 26
	struct drm_connector base;
	struct hdmi *hdmi;
27
	struct work_struct hpd_work;
28 29 30 31 32
};
#define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)

static int gpio_config(struct hdmi *hdmi, bool on)
{
33
	struct device *dev = &hdmi->pdev->dev;
34
	const struct hdmi_platform_config *config = hdmi->config;
35 36 37 38 39
	int ret;

	if (on) {
		ret = gpio_request(config->ddc_clk_gpio, "HDMI_DDC_CLK");
		if (ret) {
40
			dev_err(dev, "'%s'(%d) gpio_request failed: %d\n",
41 42 43
				"HDMI_DDC_CLK", config->ddc_clk_gpio, ret);
			goto error1;
		}
44 45
		gpio_set_value_cansleep(config->ddc_clk_gpio, 1);

46 47
		ret = gpio_request(config->ddc_data_gpio, "HDMI_DDC_DATA");
		if (ret) {
48
			dev_err(dev, "'%s'(%d) gpio_request failed: %d\n",
49 50 51
				"HDMI_DDC_DATA", config->ddc_data_gpio, ret);
			goto error2;
		}
52 53
		gpio_set_value_cansleep(config->ddc_data_gpio, 1);

54 55
		ret = gpio_request(config->hpd_gpio, "HDMI_HPD");
		if (ret) {
56
			dev_err(dev, "'%s'(%d) gpio_request failed: %d\n",
57 58 59
				"HDMI_HPD", config->hpd_gpio, ret);
			goto error3;
		}
60 61 62 63 64
		gpio_direction_input(config->hpd_gpio);
		gpio_set_value_cansleep(config->hpd_gpio, 1);

		if (config->mux_en_gpio != -1) {
			ret = gpio_request(config->mux_en_gpio, "HDMI_MUX_EN");
65
			if (ret) {
66
				dev_err(dev, "'%s'(%d) gpio_request failed: %d\n",
67
					"HDMI_MUX_EN", config->mux_en_gpio, ret);
68 69
				goto error4;
			}
70 71 72 73 74 75
			gpio_set_value_cansleep(config->mux_en_gpio, 1);
		}

		if (config->mux_sel_gpio != -1) {
			ret = gpio_request(config->mux_sel_gpio, "HDMI_MUX_SEL");
			if (ret) {
76
				dev_err(dev, "'%s'(%d) gpio_request failed: %d\n",
77 78 79 80
					"HDMI_MUX_SEL", config->mux_sel_gpio, ret);
				goto error5;
			}
			gpio_set_value_cansleep(config->mux_sel_gpio, 0);
81
		}
82 83 84 85 86

		if (config->mux_lpm_gpio != -1) {
			ret = gpio_request(config->mux_lpm_gpio,
					"HDMI_MUX_LPM");
			if (ret) {
87
				dev_err(dev,
88 89 90 91 92 93 94
					"'%s'(%d) gpio_request failed: %d\n",
					"HDMI_MUX_LPM",
					config->mux_lpm_gpio, ret);
				goto error6;
			}
			gpio_set_value_cansleep(config->mux_lpm_gpio, 1);
		}
95 96 97 98 99 100
		DBG("gpio on");
	} else {
		gpio_free(config->ddc_clk_gpio);
		gpio_free(config->ddc_data_gpio);
		gpio_free(config->hpd_gpio);

101 102 103 104 105 106 107 108
		if (config->mux_en_gpio != -1) {
			gpio_set_value_cansleep(config->mux_en_gpio, 0);
			gpio_free(config->mux_en_gpio);
		}

		if (config->mux_sel_gpio != -1) {
			gpio_set_value_cansleep(config->mux_sel_gpio, 1);
			gpio_free(config->mux_sel_gpio);
109
		}
110 111 112 113 114

		if (config->mux_lpm_gpio != -1) {
			gpio_set_value_cansleep(config->mux_lpm_gpio, 0);
			gpio_free(config->mux_lpm_gpio);
		}
115 116 117 118 119
		DBG("gpio off");
	}

	return 0;

120 121 122
error6:
	if (config->mux_sel_gpio != -1)
		gpio_free(config->mux_sel_gpio);
123 124 125
error5:
	if (config->mux_en_gpio != -1)
		gpio_free(config->mux_en_gpio);
126 127 128 129 130 131 132 133 134 135 136 137
error4:
	gpio_free(config->hpd_gpio);
error3:
	gpio_free(config->ddc_data_gpio);
error2:
	gpio_free(config->ddc_clk_gpio);
error1:
	return ret;
}

static int hpd_enable(struct hdmi_connector *hdmi_connector)
{
R
Rob Clark 已提交
138
	struct hdmi *hdmi = hdmi_connector->hdmi;
139
	const struct hdmi_platform_config *config = hdmi->config;
140
	struct device *dev = &hdmi->pdev->dev;
141 142
	struct hdmi_phy *phy = hdmi->phy;
	uint32_t hpd_ctrl;
143
	int i, ret;
144

145 146 147
	for (i = 0; i < config->hpd_reg_cnt; i++) {
		ret = regulator_enable(hdmi->hpd_regs[i]);
		if (ret) {
148
			dev_err(dev, "failed to enable hpd regulator: %s (%d)\n",
149 150 151 152 153
					config->hpd_reg_names[i], ret);
			goto fail;
		}
	}

154 155 156 157 158 159
	ret = pinctrl_pm_select_default_state(dev);
	if (ret) {
		dev_err(dev, "pinctrl state chg failed: %d\n", ret);
		goto fail;
	}

160 161
	ret = gpio_config(hdmi, true);
	if (ret) {
162
		dev_err(dev, "failed to configure GPIOs: %d\n", ret);
163 164 165
		goto fail;
	}

166
	for (i = 0; i < config->hpd_clk_cnt; i++) {
167 168 169 170
		if (config->hpd_freq && config->hpd_freq[i]) {
			ret = clk_set_rate(hdmi->hpd_clks[i],
					config->hpd_freq[i]);
			if (ret)
171
				dev_warn(dev, "failed to set clk %s (%d)\n",
172 173 174
						config->hpd_clk_names[i], ret);
		}

175 176
		ret = clk_prepare_enable(hdmi->hpd_clks[i]);
		if (ret) {
177
			dev_err(dev, "failed to enable hpd clk: %s (%d)\n",
178 179 180
					config->hpd_clk_names[i], ret);
			goto fail;
		}
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	}

	hdmi_set_mode(hdmi, false);
	phy->funcs->reset(phy);
	hdmi_set_mode(hdmi, true);

	hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);

	/* enable HPD events: */
	hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
			HDMI_HPD_INT_CTRL_INT_CONNECT |
			HDMI_HPD_INT_CTRL_INT_EN);

	/* set timeout to 4.1ms (max) for hardware debounce */
	hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
	hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);

	/* Toggle HPD circuit to trigger HPD sense */
	hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
			~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
	hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
			HDMI_HPD_CTRL_ENABLE | hpd_ctrl);

	return 0;

fail:
	return ret;
}

210
static void hdp_disable(struct hdmi_connector *hdmi_connector)
211
{
R
Rob Clark 已提交
212
	struct hdmi *hdmi = hdmi_connector->hdmi;
213
	const struct hdmi_platform_config *config = hdmi->config;
214
	struct device *dev = &hdmi->pdev->dev;
215
	int i, ret = 0;
216 217 218 219 220 221

	/* Disable HPD interrupt */
	hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);

	hdmi_set_mode(hdmi, false);

222 223
	for (i = 0; i < config->hpd_clk_cnt; i++)
		clk_disable_unprepare(hdmi->hpd_clks[i]);
224 225

	ret = gpio_config(hdmi, false);
226
	if (ret)
227
		dev_warn(dev, "failed to unconfigure GPIOs: %d\n", ret);
228

229 230 231 232
	ret = pinctrl_pm_select_sleep_state(dev);
	if (ret)
		dev_warn(dev, "pinctrl state chg failed: %d\n", ret);

233 234 235
	for (i = 0; i < config->hpd_reg_cnt; i++) {
		ret = regulator_disable(hdmi->hpd_regs[i]);
		if (ret)
236
			dev_warn(dev, "failed to disable hpd regulator: %s (%d)\n",
237 238
					config->hpd_reg_names[i], ret);
	}
239 240
}

241 242 243 244 245 246 247 248 249
static void
hotplug_work(struct work_struct *work)
{
	struct hdmi_connector *hdmi_connector =
		container_of(work, struct hdmi_connector, hpd_work);
	struct drm_connector *connector = &hdmi_connector->base;
	drm_helper_hpd_irq_event(connector->dev);
}

250 251
void hdmi_connector_irq(struct drm_connector *connector)
{
R
Rob Clark 已提交
252
	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
253
	struct msm_drm_private *priv = connector->dev->dev_private;
R
Rob Clark 已提交
254
	struct hdmi *hdmi = hdmi_connector->hdmi;
255 256 257 258 259 260 261 262 263 264
	uint32_t hpd_int_status, hpd_int_ctrl;

	/* Process HPD: */
	hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
	hpd_int_ctrl   = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);

	if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
			(hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
		bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);

J
Jilai Wang 已提交
265
		/* ack & disable (temporarily) HPD events: */
266
		hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
J
Jilai Wang 已提交
267 268 269
			HDMI_HPD_INT_CTRL_INT_ACK);

		DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
270 271 272 273 274 275

		/* detect disconnect if we are connected or visa versa: */
		hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
		if (!detected)
			hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
		hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
276 277

		queue_work(priv->wq, &hdmi_connector->hpd_work);
278 279 280
	}
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
static enum drm_connector_status detect_reg(struct hdmi *hdmi)
{
	uint32_t hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
	return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
			connector_status_connected : connector_status_disconnected;
}

static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
{
	const struct hdmi_platform_config *config = hdmi->config;
	return gpio_get_value(config->hpd_gpio) ?
			connector_status_connected :
			connector_status_disconnected;
}

296 297 298
static enum drm_connector_status hdmi_connector_detect(
		struct drm_connector *connector, bool force)
{
R
Rob Clark 已提交
299 300
	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
	struct hdmi *hdmi = hdmi_connector->hdmi;
301
	enum drm_connector_status stat_gpio, stat_reg;
302 303
	int retry = 20;

304 305 306
	do {
		stat_gpio = detect_gpio(hdmi);
		stat_reg  = detect_reg(hdmi);
307

308
		if (stat_gpio == stat_reg)
309
			break;
310

311
		mdelay(10);
312 313 314 315 316 317 318 319 320
	} while (--retry);

	/* the status we get from reading gpio seems to be more reliable,
	 * so trust that one the most if we didn't manage to get hdmi and
	 * gpio status to agree:
	 */
	if (stat_gpio != stat_reg) {
		DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
		DBG("hpd gpio tells us: %d", stat_gpio);
321 322
	}

323
	return stat_gpio;
324 325 326 327
}

static void hdmi_connector_destroy(struct drm_connector *connector)
{
R
Rob Clark 已提交
328
	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
329 330 331

	hdp_disable(hdmi_connector);

332
	drm_connector_unregister(connector);
333 334 335 336 337 338 339
	drm_connector_cleanup(connector);

	kfree(hdmi_connector);
}

static int hdmi_connector_get_modes(struct drm_connector *connector)
{
R
Rob Clark 已提交
340 341
	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
	struct hdmi *hdmi = hdmi_connector->hdmi;
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	struct edid *edid;
	uint32_t hdmi_ctrl;
	int ret = 0;

	hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
	hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);

	edid = drm_get_edid(connector, hdmi->i2c);

	hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);

	drm_mode_connector_update_edid_property(connector, edid);

	if (edid) {
		ret = drm_add_edid_modes(connector, edid);
		kfree(edid);
	}

	return ret;
}

static int hdmi_connector_mode_valid(struct drm_connector *connector,
				 struct drm_display_mode *mode)
{
R
Rob Clark 已提交
366
	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
367 368
	struct hdmi *hdmi = hdmi_connector->hdmi;
	const struct hdmi_platform_config *config = hdmi->config;
369 370 371 372 373 374
	struct msm_drm_private *priv = connector->dev->dev_private;
	struct msm_kms *kms = priv->kms;
	long actual, requested;

	requested = 1000 * mode->clock;
	actual = kms->funcs->round_pixclk(kms,
R
Rob Clark 已提交
375
			requested, hdmi_connector->hdmi->encoder);
376

377 378 379 380 381 382 383
	/* for mdp5/apq8074, we manage our own pixel clk (as opposed to
	 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
	 * instead):
	 */
	if (config->pwr_clk_cnt > 0)
		actual = clk_round_rate(hdmi->pwr_clks[0], actual);

384 385 386 387 388 389 390 391
	DBG("requested=%ld, actual=%ld", requested, actual);

	if (actual != requested)
		return MODE_CLOCK_RANGE;

	return 0;
}

R
Rob Clark 已提交
392 393 394 395 396 397 398
static struct drm_encoder *
hdmi_connector_best_encoder(struct drm_connector *connector)
{
	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
	return hdmi_connector->hdmi->encoder;
}

399
static const struct drm_connector_funcs hdmi_connector_funcs = {
400
	.dpms = drm_atomic_helper_connector_dpms,
401 402 403
	.detect = hdmi_connector_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = hdmi_connector_destroy,
R
Rob Clark 已提交
404 405 406
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
407 408 409 410 411
};

static const struct drm_connector_helper_funcs hdmi_connector_helper_funcs = {
	.get_modes = hdmi_connector_get_modes,
	.mode_valid = hdmi_connector_mode_valid,
R
Rob Clark 已提交
412
	.best_encoder = hdmi_connector_best_encoder,
413 414 415
};

/* initialize connector */
R
Rob Clark 已提交
416
struct drm_connector *hdmi_connector_init(struct hdmi *hdmi)
417 418 419 420 421 422 423 424 425 426 427
{
	struct drm_connector *connector = NULL;
	struct hdmi_connector *hdmi_connector;
	int ret;

	hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
	if (!hdmi_connector) {
		ret = -ENOMEM;
		goto fail;
	}

R
Rob Clark 已提交
428
	hdmi_connector->hdmi = hdmi;
429
	INIT_WORK(&hdmi_connector->hpd_work, hotplug_work);
R
Rob Clark 已提交
430 431

	connector = &hdmi_connector->base;
432

R
Rob Clark 已提交
433
	drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
434 435 436
			DRM_MODE_CONNECTOR_HDMIA);
	drm_connector_helper_add(connector, &hdmi_connector_helper_funcs);

437 438
	connector->polled = DRM_CONNECTOR_POLL_CONNECT |
			DRM_CONNECTOR_POLL_DISCONNECT;
439

R
Rob Clark 已提交
440
	connector->interlace_allowed = 0;
441 442
	connector->doublescan_allowed = 0;

443
	drm_connector_register(connector);
444 445 446

	ret = hpd_enable(hdmi_connector);
	if (ret) {
447
		dev_err(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
448 449 450
		goto fail;
	}

R
Rob Clark 已提交
451
	drm_mode_connector_attach_encoder(connector, hdmi->encoder);
452 453 454 455 456 457 458 459 460

	return connector;

fail:
	if (connector)
		hdmi_connector_destroy(connector);

	return ERR_PTR(ret);
}