output.c 8.4 KB
Newer Older
T
Thierry Reding 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2012 Avionic Design GmbH
 * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
 *
 * 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.
 */

#include <linux/of_gpio.h>

12
#include <drm/drm_panel.h>
T
Thierry Reding 已提交
13 14 15 16 17 18 19 20
#include "drm.h"

static int tegra_connector_get_modes(struct drm_connector *connector)
{
	struct tegra_output *output = connector_to_output(connector);
	struct edid *edid = NULL;
	int err = 0;

21 22 23 24
	/*
	 * If the panel provides one or more modes, use them exclusively and
	 * ignore any other means of obtaining a mode.
	 */
25 26 27 28 29 30
	if (output->panel) {
		err = output->panel->funcs->get_modes(output->panel);
		if (err > 0)
			return err;
	}

T
Thierry Reding 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	if (output->edid)
		edid = kmemdup(output->edid, sizeof(*edid), GFP_KERNEL);
	else if (output->ddc)
		edid = drm_get_edid(connector, output->ddc);

	drm_mode_connector_update_edid_property(connector, edid);

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

	return err;
}

static int tegra_connector_mode_valid(struct drm_connector *connector,
				      struct drm_display_mode *mode)
{
	struct tegra_output *output = connector_to_output(connector);
	enum drm_mode_status status = MODE_OK;
	int err;

	err = tegra_output_check_mode(output, mode, &status);
	if (err < 0)
		return MODE_ERROR;

	return status;
}

static struct drm_encoder *
tegra_connector_best_encoder(struct drm_connector *connector)
{
	struct tegra_output *output = connector_to_output(connector);

	return &output->encoder;
}

static const struct drm_connector_helper_funcs connector_helper_funcs = {
	.get_modes = tegra_connector_get_modes,
	.mode_valid = tegra_connector_mode_valid,
	.best_encoder = tegra_connector_best_encoder,
};

static enum drm_connector_status
tegra_connector_detect(struct drm_connector *connector, bool force)
{
	struct tegra_output *output = connector_to_output(connector);
	enum drm_connector_status status = connector_status_unknown;

T
Thierry Reding 已提交
80 81 82
	if (output->ops->detect)
		return output->ops->detect(output);

T
Thierry Reding 已提交
83 84 85 86 87 88
	if (gpio_is_valid(output->hpd_gpio)) {
		if (gpio_get_value(output->hpd_gpio) == 0)
			status = connector_status_disconnected;
		else
			status = connector_status_connected;
	} else {
89 90 91 92 93
		if (!output->panel)
			status = connector_status_disconnected;
		else
			status = connector_status_connected;

T
Thierry Reding 已提交
94 95 96 97 98 99 100
		if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
			status = connector_status_connected;
	}

	return status;
}

101 102 103 104 105
static void drm_connector_clear(struct drm_connector *connector)
{
	memset(connector, 0, sizeof(*connector));
}

T
Thierry Reding 已提交
106 107
static void tegra_connector_destroy(struct drm_connector *connector)
{
108
	drm_connector_unregister(connector);
T
Thierry Reding 已提交
109
	drm_connector_cleanup(connector);
110
	drm_connector_clear(connector);
T
Thierry Reding 已提交
111 112 113 114 115 116 117 118 119
}

static const struct drm_connector_funcs connector_funcs = {
	.dpms = drm_helper_connector_dpms,
	.detect = tegra_connector_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = tegra_connector_destroy,
};

120 121 122 123 124
static void drm_encoder_clear(struct drm_encoder *encoder)
{
	memset(encoder, 0, sizeof(*encoder));
}

T
Thierry Reding 已提交
125 126 127
static void tegra_encoder_destroy(struct drm_encoder *encoder)
{
	drm_encoder_cleanup(encoder);
128
	drm_encoder_clear(encoder);
T
Thierry Reding 已提交
129 130 131 132 133 134 135 136
}

static const struct drm_encoder_funcs encoder_funcs = {
	.destroy = tegra_encoder_destroy,
};

static void tegra_encoder_dpms(struct drm_encoder *encoder, int mode)
{
137 138 139
	struct tegra_output *output = encoder_to_output(encoder);
	struct drm_panel *panel = output->panel;

140 141 142
	if (mode != DRM_MODE_DPMS_ON) {
		drm_panel_disable(panel);
		tegra_output_disable(output);
143
		drm_panel_unprepare(panel);
144
	} else {
145
		drm_panel_prepare(panel);
146 147
		tegra_output_enable(output);
		drm_panel_enable(panel);
148
	}
T
Thierry Reding 已提交
149 150 151 152 153 154 155 156 157 158 159
}

static bool tegra_encoder_mode_fixup(struct drm_encoder *encoder,
				     const struct drm_display_mode *mode,
				     struct drm_display_mode *adjusted)
{
	return true;
}

static void tegra_encoder_prepare(struct drm_encoder *encoder)
{
160
	tegra_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
T
Thierry Reding 已提交
161 162 163 164
}

static void tegra_encoder_commit(struct drm_encoder *encoder)
{
165
	tegra_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
T
Thierry Reding 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
}

static void tegra_encoder_mode_set(struct drm_encoder *encoder,
				   struct drm_display_mode *mode,
				   struct drm_display_mode *adjusted)
{
}

static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
	.dpms = tegra_encoder_dpms,
	.mode_fixup = tegra_encoder_mode_fixup,
	.prepare = tegra_encoder_prepare,
	.commit = tegra_encoder_commit,
	.mode_set = tegra_encoder_mode_set,
};

static irqreturn_t hpd_irq(int irq, void *data)
{
	struct tegra_output *output = data;

186 187
	if (output->connector.dev)
		drm_helper_hpd_irq_event(output->connector.dev);
T
Thierry Reding 已提交
188 189 190 191

	return IRQ_HANDLED;
}

192
int tegra_output_probe(struct tegra_output *output)
T
Thierry Reding 已提交
193
{
194
	struct device_node *ddc, *panel;
T
Thierry Reding 已提交
195
	enum of_gpio_flags flags;
T
Thierry Reding 已提交
196
	int err, size;
T
Thierry Reding 已提交
197 198 199 200

	if (!output->of_node)
		output->of_node = output->dev->of_node;

201 202 203 204 205 206 207 208 209
	panel = of_parse_phandle(output->of_node, "nvidia,panel", 0);
	if (panel) {
		output->panel = of_drm_find_panel(panel);
		if (!output->panel)
			return -EPROBE_DEFER;

		of_node_put(panel);
	}

T
Thierry Reding 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	output->edid = of_get_property(output->of_node, "nvidia,edid", &size);

	ddc = of_parse_phandle(output->of_node, "nvidia,ddc-i2c-bus", 0);
	if (ddc) {
		output->ddc = of_find_i2c_adapter_by_node(ddc);
		if (!output->ddc) {
			err = -EPROBE_DEFER;
			of_node_put(ddc);
			return err;
		}

		of_node_put(ddc);
	}

	output->hpd_gpio = of_get_named_gpio_flags(output->of_node,
						   "nvidia,hpd-gpio", 0,
						   &flags);
	if (gpio_is_valid(output->hpd_gpio)) {
		unsigned long flags;

		err = gpio_request_one(output->hpd_gpio, GPIOF_DIR_IN,
				       "HDMI hotplug detect");
		if (err < 0) {
			dev_err(output->dev, "gpio_request_one(): %d\n", err);
			return err;
		}

		err = gpio_to_irq(output->hpd_gpio);
		if (err < 0) {
			dev_err(output->dev, "gpio_to_irq(): %d\n", err);
240 241
			gpio_free(output->hpd_gpio);
			return err;
T
Thierry Reding 已提交
242 243 244 245 246 247 248 249 250 251 252 253
		}

		output->hpd_irq = err;

		flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
			IRQF_ONESHOT;

		err = request_threaded_irq(output->hpd_irq, NULL, hpd_irq,
					   flags, "hpd", output);
		if (err < 0) {
			dev_err(output->dev, "failed to request IRQ#%u: %d\n",
				output->hpd_irq, err);
254 255
			gpio_free(output->hpd_gpio);
			return err;
T
Thierry Reding 已提交
256 257 258
		}

		output->connector.polled = DRM_CONNECTOR_POLL_HPD;
259 260 261 262 263 264 265

		/*
		 * Disable the interrupt until the connector has been
		 * initialized to avoid a race in the hotplug interrupt
		 * handler.
		 */
		disable_irq(output->hpd_irq);
T
Thierry Reding 已提交
266 267
	}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	return 0;
}

int tegra_output_remove(struct tegra_output *output)
{
	if (gpio_is_valid(output->hpd_gpio)) {
		free_irq(output->hpd_irq, output);
		gpio_free(output->hpd_gpio);
	}

	if (output->ddc)
		put_device(&output->ddc->dev);

	return 0;
}

int tegra_output_init(struct drm_device *drm, struct tegra_output *output)
{
	int connector, encoder;

T
Thierry Reding 已提交
288 289 290 291 292 293
	switch (output->type) {
	case TEGRA_OUTPUT_RGB:
		connector = DRM_MODE_CONNECTOR_LVDS;
		encoder = DRM_MODE_ENCODER_LVDS;
		break;

T
Thierry Reding 已提交
294 295 296 297 298
	case TEGRA_OUTPUT_HDMI:
		connector = DRM_MODE_CONNECTOR_HDMIA;
		encoder = DRM_MODE_ENCODER_TMDS;
		break;

T
Thierry Reding 已提交
299 300 301 302 303
	case TEGRA_OUTPUT_DSI:
		connector = DRM_MODE_CONNECTOR_DSI;
		encoder = DRM_MODE_ENCODER_DSI;
		break;

T
Thierry Reding 已提交
304 305 306 307 308
	case TEGRA_OUTPUT_EDP:
		connector = DRM_MODE_CONNECTOR_eDP;
		encoder = DRM_MODE_ENCODER_TMDS;
		break;

T
Thierry Reding 已提交
309 310 311 312 313 314 315 316 317
	default:
		connector = DRM_MODE_CONNECTOR_Unknown;
		encoder = DRM_MODE_ENCODER_NONE;
		break;
	}

	drm_connector_init(drm, &output->connector, &connector_funcs,
			   connector);
	drm_connector_helper_add(&output->connector, &connector_helper_funcs);
318
	output->connector.dpms = DRM_MODE_DPMS_OFF;
T
Thierry Reding 已提交
319

320 321 322
	if (output->panel)
		drm_panel_attach(output->panel, &output->connector);

T
Thierry Reding 已提交
323 324 325 326
	drm_encoder_init(drm, &output->encoder, &encoder_funcs, encoder);
	drm_encoder_helper_add(&output->encoder, &encoder_helper_funcs);

	drm_mode_connector_attach_encoder(&output->connector, &output->encoder);
327
	drm_connector_register(&output->connector);
T
Thierry Reding 已提交
328 329 330

	output->encoder.possible_crtcs = 0x3;

331 332 333 334 335 336 337
	/*
	 * The connector is now registered and ready to receive hotplug events
	 * so the hotplug interrupt can be enabled.
	 */
	if (gpio_is_valid(output->hpd_gpio))
		enable_irq(output->hpd_irq);

T
Thierry Reding 已提交
338 339 340 341 342
	return 0;
}

int tegra_output_exit(struct tegra_output *output)
{
343 344 345 346 347 348 349
	/*
	 * The connector is going away, so the interrupt must be disabled to
	 * prevent the hotplug interrupt handler from potentially crashing.
	 */
	if (gpio_is_valid(output->hpd_gpio))
		disable_irq(output->hpd_irq);

T
Thierry Reding 已提交
350 351
	return 0;
}