dpaux.c 17.7 KB
Newer Older
T
Thierry Reding 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2013 NVIDIA Corporation
 *
 * 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/clk.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/of_gpio.h>
15 16 17
#include <linux/pinctrl/pinconf-generic.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>
T
Thierry Reding 已提交
18 19 20
#include <linux/platform_device.h>
#include <linux/reset.h>
#include <linux/regulator/consumer.h>
21
#include <linux/workqueue.h>
T
Thierry Reding 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#include <drm/drm_dp_helper.h>
#include <drm/drm_panel.h>

#include "dpaux.h"
#include "drm.h"

static DEFINE_MUTEX(dpaux_lock);
static LIST_HEAD(dpaux_list);

struct tegra_dpaux {
	struct drm_dp_aux aux;
	struct device *dev;

	void __iomem *regs;
	int irq;

	struct tegra_output *output;

	struct reset_control *rst;
	struct clk *clk_parent;
	struct clk *clk;

	struct regulator *vdd;

	struct completion complete;
48
	struct work_struct work;
T
Thierry Reding 已提交
49
	struct list_head list;
50 51 52 53 54

#ifdef CONFIG_GENERIC_PINCONF
	struct pinctrl_dev *pinctrl;
	struct pinctrl_desc desc;
#endif
T
Thierry Reding 已提交
55 56 57 58 59 60 61
};

static inline struct tegra_dpaux *to_dpaux(struct drm_dp_aux *aux)
{
	return container_of(aux, struct tegra_dpaux, aux);
}

62 63 64 65 66
static inline struct tegra_dpaux *work_to_dpaux(struct work_struct *work)
{
	return container_of(work, struct tegra_dpaux, work);
}

67 68
static inline u32 tegra_dpaux_readl(struct tegra_dpaux *dpaux,
				    unsigned long offset)
T
Thierry Reding 已提交
69 70 71 72 73
{
	return readl(dpaux->regs + (offset << 2));
}

static inline void tegra_dpaux_writel(struct tegra_dpaux *dpaux,
74
				      u32 value, unsigned long offset)
T
Thierry Reding 已提交
75 76 77 78 79 80 81 82 83
{
	writel(value, dpaux->regs + (offset << 2));
}

static void tegra_dpaux_write_fifo(struct tegra_dpaux *dpaux, const u8 *buffer,
				   size_t size)
{
	size_t i, j;

84 85
	for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
		size_t num = min_t(size_t, size - i * 4, 4);
86
		u32 value = 0;
T
Thierry Reding 已提交
87 88

		for (j = 0; j < num; j++)
89
			value |= buffer[i * 4 + j] << (j * 8);
T
Thierry Reding 已提交
90

91
		tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXDATA_WRITE(i));
T
Thierry Reding 已提交
92 93 94 95 96 97 98 99
	}
}

static void tegra_dpaux_read_fifo(struct tegra_dpaux *dpaux, u8 *buffer,
				  size_t size)
{
	size_t i, j;

100 101
	for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
		size_t num = min_t(size_t, size - i * 4, 4);
102
		u32 value;
T
Thierry Reding 已提交
103

104
		value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXDATA_READ(i));
T
Thierry Reding 已提交
105 106

		for (j = 0; j < num; j++)
107
			buffer[i * 4 + j] = value >> (j * 8);
T
Thierry Reding 已提交
108 109 110 111 112 113 114 115 116 117
	}
}

static ssize_t tegra_dpaux_transfer(struct drm_dp_aux *aux,
				    struct drm_dp_aux_msg *msg)
{
	unsigned long timeout = msecs_to_jiffies(250);
	struct tegra_dpaux *dpaux = to_dpaux(aux);
	unsigned long status;
	ssize_t ret = 0;
118
	u32 value;
T
Thierry Reding 已提交
119

120 121
	/* Tegra has 4x4 byte DP AUX transmit and receive FIFOs. */
	if (msg->size > 16)
T
Thierry Reding 已提交
122 123
		return -EINVAL;

124 125 126 127 128 129
	/*
	 * Allow zero-sized messages only for I2C, in which case they specify
	 * address-only transactions.
	 */
	if (msg->size < 1) {
		switch (msg->request & ~DP_AUX_I2C_MOT) {
130
		case DP_AUX_I2C_WRITE_STATUS_UPDATE:
131 132 133 134 135 136 137 138 139 140 141 142
		case DP_AUX_I2C_WRITE:
		case DP_AUX_I2C_READ:
			value = DPAUX_DP_AUXCTL_CMD_ADDRESS_ONLY;
			break;

		default:
			return -EINVAL;
		}
	} else {
		/* For non-zero-sized messages, set the CMDLEN field. */
		value = DPAUX_DP_AUXCTL_CMDLEN(msg->size - 1);
	}
T
Thierry Reding 已提交
143 144 145 146

	switch (msg->request & ~DP_AUX_I2C_MOT) {
	case DP_AUX_I2C_WRITE:
		if (msg->request & DP_AUX_I2C_MOT)
147
			value |= DPAUX_DP_AUXCTL_CMD_MOT_WR;
T
Thierry Reding 已提交
148
		else
149
			value |= DPAUX_DP_AUXCTL_CMD_I2C_WR;
T
Thierry Reding 已提交
150 151 152 153 154

		break;

	case DP_AUX_I2C_READ:
		if (msg->request & DP_AUX_I2C_MOT)
155
			value |= DPAUX_DP_AUXCTL_CMD_MOT_RD;
T
Thierry Reding 已提交
156
		else
157
			value |= DPAUX_DP_AUXCTL_CMD_I2C_RD;
T
Thierry Reding 已提交
158 159 160

		break;

161
	case DP_AUX_I2C_WRITE_STATUS_UPDATE:
T
Thierry Reding 已提交
162
		if (msg->request & DP_AUX_I2C_MOT)
163
			value |= DPAUX_DP_AUXCTL_CMD_MOT_RQ;
T
Thierry Reding 已提交
164
		else
165
			value |= DPAUX_DP_AUXCTL_CMD_I2C_RQ;
T
Thierry Reding 已提交
166 167 168 169

		break;

	case DP_AUX_NATIVE_WRITE:
170
		value |= DPAUX_DP_AUXCTL_CMD_AUX_WR;
T
Thierry Reding 已提交
171 172 173
		break;

	case DP_AUX_NATIVE_READ:
174
		value |= DPAUX_DP_AUXCTL_CMD_AUX_RD;
T
Thierry Reding 已提交
175 176 177 178 179 180
		break;

	default:
		return -EINVAL;
	}

181
	tegra_dpaux_writel(dpaux, msg->address, DPAUX_DP_AUXADDR);
T
Thierry Reding 已提交
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);

	if ((msg->request & DP_AUX_I2C_READ) == 0) {
		tegra_dpaux_write_fifo(dpaux, msg->buffer, msg->size);
		ret = msg->size;
	}

	/* start transaction */
	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXCTL);
	value |= DPAUX_DP_AUXCTL_TRANSACTREQ;
	tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);

	status = wait_for_completion_timeout(&dpaux->complete, timeout);
	if (!status)
		return -ETIMEDOUT;

	/* read status and clear errors */
	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
	tegra_dpaux_writel(dpaux, 0xf00, DPAUX_DP_AUXSTAT);

	if (value & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR)
		return -ETIMEDOUT;

	if ((value & DPAUX_DP_AUXSTAT_RX_ERROR) ||
	    (value & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR) ||
	    (value & DPAUX_DP_AUXSTAT_NO_STOP_ERROR))
		return -EIO;

	switch ((value & DPAUX_DP_AUXSTAT_REPLY_TYPE_MASK) >> 16) {
	case 0x00:
		msg->reply = DP_AUX_NATIVE_REPLY_ACK;
		break;

	case 0x01:
		msg->reply = DP_AUX_NATIVE_REPLY_NACK;
		break;

	case 0x02:
		msg->reply = DP_AUX_NATIVE_REPLY_DEFER;
		break;

	case 0x04:
		msg->reply = DP_AUX_I2C_REPLY_NACK;
		break;

	case 0x08:
		msg->reply = DP_AUX_I2C_REPLY_DEFER;
		break;
	}

232
	if ((msg->size > 0) && (msg->reply == DP_AUX_NATIVE_REPLY_ACK)) {
T
Thierry Reding 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246
		if (msg->request & DP_AUX_I2C_READ) {
			size_t count = value & DPAUX_DP_AUXSTAT_REPLY_MASK;

			if (WARN_ON(count != msg->size))
				count = min_t(size_t, count, msg->size);

			tegra_dpaux_read_fifo(dpaux, msg->buffer, count);
			ret = count;
		}
	}

	return ret;
}

247 248 249 250 251 252 253 254
static void tegra_dpaux_hotplug(struct work_struct *work)
{
	struct tegra_dpaux *dpaux = work_to_dpaux(work);

	if (dpaux->output)
		drm_helper_hpd_irq_event(dpaux->output->connector.dev);
}

T
Thierry Reding 已提交
255 256 257 258
static irqreturn_t tegra_dpaux_irq(int irq, void *data)
{
	struct tegra_dpaux *dpaux = data;
	irqreturn_t ret = IRQ_HANDLED;
259
	u32 value;
T
Thierry Reding 已提交
260 261 262 263 264

	/* clear interrupts */
	value = tegra_dpaux_readl(dpaux, DPAUX_INTR_AUX);
	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);

265 266
	if (value & (DPAUX_INTR_PLUG_EVENT | DPAUX_INTR_UNPLUG_EVENT))
		schedule_work(&dpaux->work);
T
Thierry Reding 已提交
267 268 269 270 271 272 273 274 275 276 277

	if (value & DPAUX_INTR_IRQ_EVENT) {
		/* TODO: handle this */
	}

	if (value & DPAUX_INTR_AUX_DONE)
		complete(&dpaux->complete);

	return ret;
}

278 279 280 281 282 283
enum tegra_dpaux_functions {
	DPAUX_PADCTL_FUNC_AUX,
	DPAUX_PADCTL_FUNC_I2C,
	DPAUX_PADCTL_FUNC_OFF,
};

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
static void tegra_dpaux_pad_power_down(struct tegra_dpaux *dpaux)
{
	u32 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);

	value |= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;

	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
}

static void tegra_dpaux_pad_power_up(struct tegra_dpaux *dpaux)
{
	u32 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);

	value &= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;

	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
}

static int tegra_dpaux_pad_config(struct tegra_dpaux *dpaux, unsigned function)
{
	u32 value;

	switch (function) {
307
	case DPAUX_PADCTL_FUNC_AUX:
308 309 310 311 312 313 314
		value = DPAUX_HYBRID_PADCTL_AUX_CMH(2) |
			DPAUX_HYBRID_PADCTL_AUX_DRVZ(4) |
			DPAUX_HYBRID_PADCTL_AUX_DRVI(0x18) |
			DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV |
			DPAUX_HYBRID_PADCTL_MODE_AUX;
		break;

315
	case DPAUX_PADCTL_FUNC_I2C:
316 317 318 319 320
		value = DPAUX_HYBRID_PADCTL_I2C_SDA_INPUT_RCV |
			DPAUX_HYBRID_PADCTL_I2C_SCL_INPUT_RCV |
			DPAUX_HYBRID_PADCTL_MODE_I2C;
		break;

321 322 323 324
	case DPAUX_PADCTL_FUNC_OFF:
		tegra_dpaux_pad_power_down(dpaux);
		return 0;

325 326 327 328 329 330 331 332 333 334
	default:
		return -ENOTSUPP;
	}

	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_PADCTL);
	tegra_dpaux_pad_power_up(dpaux);

	return 0;
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
#ifdef CONFIG_GENERIC_PINCONF
static const struct pinctrl_pin_desc tegra_dpaux_pins[] = {
	PINCTRL_PIN(0, "DP_AUX_CHx_P"),
	PINCTRL_PIN(1, "DP_AUX_CHx_N"),
};

static const unsigned tegra_dpaux_pin_numbers[] = { 0, 1 };

static const char * const tegra_dpaux_groups[] = {
	"dpaux-io",
};

static const char * const tegra_dpaux_functions[] = {
	"aux",
	"i2c",
	"off",
};

static int tegra_dpaux_get_groups_count(struct pinctrl_dev *pinctrl)
{
	return ARRAY_SIZE(tegra_dpaux_groups);
}

static const char *tegra_dpaux_get_group_name(struct pinctrl_dev *pinctrl,
					      unsigned int group)
{
	return tegra_dpaux_groups[group];
}

static int tegra_dpaux_get_group_pins(struct pinctrl_dev *pinctrl,
				      unsigned group, const unsigned **pins,
				      unsigned *num_pins)
{
	*pins = tegra_dpaux_pin_numbers;
	*num_pins = ARRAY_SIZE(tegra_dpaux_pin_numbers);

	return 0;
}

static const struct pinctrl_ops tegra_dpaux_pinctrl_ops = {
	.get_groups_count = tegra_dpaux_get_groups_count,
	.get_group_name = tegra_dpaux_get_group_name,
	.get_group_pins = tegra_dpaux_get_group_pins,
	.dt_node_to_map = pinconf_generic_dt_node_to_map_group,
	.dt_free_map = pinconf_generic_dt_free_map,
};

static int tegra_dpaux_get_functions_count(struct pinctrl_dev *pinctrl)
{
	return ARRAY_SIZE(tegra_dpaux_functions);
}

static const char *tegra_dpaux_get_function_name(struct pinctrl_dev *pinctrl,
						 unsigned int function)
{
	return tegra_dpaux_functions[function];
}

static int tegra_dpaux_get_function_groups(struct pinctrl_dev *pinctrl,
					   unsigned int function,
					   const char * const **groups,
					   unsigned * const num_groups)
{
	*num_groups = ARRAY_SIZE(tegra_dpaux_groups);
	*groups = tegra_dpaux_groups;

	return 0;
}

static int tegra_dpaux_set_mux(struct pinctrl_dev *pinctrl,
			       unsigned int function, unsigned int group)
{
	struct tegra_dpaux *dpaux = pinctrl_dev_get_drvdata(pinctrl);

	return tegra_dpaux_pad_config(dpaux, function);
}

static const struct pinmux_ops tegra_dpaux_pinmux_ops = {
	.get_functions_count = tegra_dpaux_get_functions_count,
	.get_function_name = tegra_dpaux_get_function_name,
	.get_function_groups = tegra_dpaux_get_function_groups,
	.set_mux = tegra_dpaux_set_mux,
};
#endif

T
Thierry Reding 已提交
420 421 422 423
static int tegra_dpaux_probe(struct platform_device *pdev)
{
	struct tegra_dpaux *dpaux;
	struct resource *regs;
424
	u32 value;
T
Thierry Reding 已提交
425 426 427 428 429 430
	int err;

	dpaux = devm_kzalloc(&pdev->dev, sizeof(*dpaux), GFP_KERNEL);
	if (!dpaux)
		return -ENOMEM;

431
	INIT_WORK(&dpaux->work, tegra_dpaux_hotplug);
T
Thierry Reding 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	init_completion(&dpaux->complete);
	INIT_LIST_HEAD(&dpaux->list);
	dpaux->dev = &pdev->dev;

	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	dpaux->regs = devm_ioremap_resource(&pdev->dev, regs);
	if (IS_ERR(dpaux->regs))
		return PTR_ERR(dpaux->regs);

	dpaux->irq = platform_get_irq(pdev, 0);
	if (dpaux->irq < 0) {
		dev_err(&pdev->dev, "failed to get IRQ\n");
		return -ENXIO;
	}

447 448 449 450 451 452 453 454
	if (!pdev->dev.pm_domain) {
		dpaux->rst = devm_reset_control_get(&pdev->dev, "dpaux");
		if (IS_ERR(dpaux->rst)) {
			dev_err(&pdev->dev,
				"failed to get reset control: %ld\n",
				PTR_ERR(dpaux->rst));
			return PTR_ERR(dpaux->rst);
		}
455
	}
T
Thierry Reding 已提交
456 457

	dpaux->clk = devm_clk_get(&pdev->dev, NULL);
458 459 460
	if (IS_ERR(dpaux->clk)) {
		dev_err(&pdev->dev, "failed to get module clock: %ld\n",
			PTR_ERR(dpaux->clk));
T
Thierry Reding 已提交
461
		return PTR_ERR(dpaux->clk);
462
	}
T
Thierry Reding 已提交
463 464

	err = clk_prepare_enable(dpaux->clk);
465 466 467
	if (err < 0) {
		dev_err(&pdev->dev, "failed to enable module clock: %d\n",
			err);
T
Thierry Reding 已提交
468
		return err;
469
	}
T
Thierry Reding 已提交
470

471 472
	if (dpaux->rst)
		reset_control_deassert(dpaux->rst);
T
Thierry Reding 已提交
473 474

	dpaux->clk_parent = devm_clk_get(&pdev->dev, "parent");
475 476 477
	if (IS_ERR(dpaux->clk_parent)) {
		dev_err(&pdev->dev, "failed to get parent clock: %ld\n",
			PTR_ERR(dpaux->clk_parent));
478 479
		err = PTR_ERR(dpaux->clk_parent);
		goto assert_reset;
480
	}
T
Thierry Reding 已提交
481 482

	err = clk_prepare_enable(dpaux->clk_parent);
483 484 485
	if (err < 0) {
		dev_err(&pdev->dev, "failed to enable parent clock: %d\n",
			err);
486
		goto assert_reset;
487
	}
T
Thierry Reding 已提交
488 489 490 491 492

	err = clk_set_rate(dpaux->clk_parent, 270000000);
	if (err < 0) {
		dev_err(&pdev->dev, "failed to set clock to 270 MHz: %d\n",
			err);
493
		goto disable_parent_clk;
T
Thierry Reding 已提交
494 495 496
	}

	dpaux->vdd = devm_regulator_get(&pdev->dev, "vdd");
497 498 499
	if (IS_ERR(dpaux->vdd)) {
		dev_err(&pdev->dev, "failed to get VDD supply: %ld\n",
			PTR_ERR(dpaux->vdd));
500 501
		err = PTR_ERR(dpaux->vdd);
		goto disable_parent_clk;
502
	}
T
Thierry Reding 已提交
503 504 505 506 507 508

	err = devm_request_irq(dpaux->dev, dpaux->irq, tegra_dpaux_irq, 0,
			       dev_name(dpaux->dev), dpaux);
	if (err < 0) {
		dev_err(dpaux->dev, "failed to request IRQ#%u: %d\n",
			dpaux->irq, err);
509
		goto disable_parent_clk;
T
Thierry Reding 已提交
510 511
	}

512 513
	disable_irq(dpaux->irq);

T
Thierry Reding 已提交
514 515 516
	dpaux->aux.transfer = tegra_dpaux_transfer;
	dpaux->aux.dev = &pdev->dev;

517
	err = drm_dp_aux_register(&dpaux->aux);
T
Thierry Reding 已提交
518
	if (err < 0)
519
		goto disable_parent_clk;
T
Thierry Reding 已提交
520

521 522 523 524 525 526 527 528
	/*
	 * Assume that by default the DPAUX/I2C pads will be used for HDMI,
	 * so power them up and configure them in I2C mode.
	 *
	 * The DPAUX code paths reconfigure the pads in AUX mode, but there
	 * is no possibility to perform the I2C mode configuration in the
	 * HDMI path.
	 */
529 530 531
	err = tegra_dpaux_pad_config(dpaux, DPAUX_HYBRID_PADCTL_MODE_I2C);
	if (err < 0)
		return err;
532

533 534 535 536 537 538 539 540 541 542 543 544 545 546
#ifdef CONFIG_GENERIC_PINCONF
	dpaux->desc.name = dev_name(&pdev->dev);
	dpaux->desc.pins = tegra_dpaux_pins;
	dpaux->desc.npins = ARRAY_SIZE(tegra_dpaux_pins);
	dpaux->desc.pctlops = &tegra_dpaux_pinctrl_ops;
	dpaux->desc.pmxops = &tegra_dpaux_pinmux_ops;
	dpaux->desc.owner = THIS_MODULE;

	dpaux->pinctrl = devm_pinctrl_register(&pdev->dev, &dpaux->desc, dpaux);
	if (!dpaux->pinctrl) {
		dev_err(&pdev->dev, "failed to register pincontrol\n");
		return -ENODEV;
	}
#endif
T
Thierry Reding 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559
	/* enable and clear all interrupts */
	value = DPAUX_INTR_AUX_DONE | DPAUX_INTR_IRQ_EVENT |
		DPAUX_INTR_UNPLUG_EVENT | DPAUX_INTR_PLUG_EVENT;
	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_EN_AUX);
	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);

	mutex_lock(&dpaux_lock);
	list_add_tail(&dpaux->list, &dpaux_list);
	mutex_unlock(&dpaux_lock);

	platform_set_drvdata(pdev, dpaux);

	return 0;
560 561 562 563

disable_parent_clk:
	clk_disable_unprepare(dpaux->clk_parent);
assert_reset:
564 565 566
	if (dpaux->rst)
		reset_control_assert(dpaux->rst);

567 568 569
	clk_disable_unprepare(dpaux->clk);

	return err;
T
Thierry Reding 已提交
570 571 572 573 574
}

static int tegra_dpaux_remove(struct platform_device *pdev)
{
	struct tegra_dpaux *dpaux = platform_get_drvdata(pdev);
575 576

	/* make sure pads are powered down when not in use */
577
	tegra_dpaux_pad_power_down(dpaux);
T
Thierry Reding 已提交
578

579
	drm_dp_aux_unregister(&dpaux->aux);
T
Thierry Reding 已提交
580 581 582 583 584

	mutex_lock(&dpaux_lock);
	list_del(&dpaux->list);
	mutex_unlock(&dpaux_lock);

585 586
	cancel_work_sync(&dpaux->work);

T
Thierry Reding 已提交
587
	clk_disable_unprepare(dpaux->clk_parent);
588 589 590 591

	if (dpaux->rst)
		reset_control_assert(dpaux->rst);

T
Thierry Reding 已提交
592 593 594 595 596 597
	clk_disable_unprepare(dpaux->clk);

	return 0;
}

static const struct of_device_id tegra_dpaux_of_match[] = {
598
	{ .compatible = "nvidia,tegra210-dpaux", },
T
Thierry Reding 已提交
599 600 601
	{ .compatible = "nvidia,tegra124-dpaux", },
	{ },
};
602
MODULE_DEVICE_TABLE(of, tegra_dpaux_of_match);
T
Thierry Reding 已提交
603 604 605 606 607 608 609 610 611 612

struct platform_driver tegra_dpaux_driver = {
	.driver = {
		.name = "tegra-dpaux",
		.of_match_table = tegra_dpaux_of_match,
	},
	.probe = tegra_dpaux_probe,
	.remove = tegra_dpaux_remove,
};

613
struct drm_dp_aux *drm_dp_aux_find_by_of_node(struct device_node *np)
T
Thierry Reding 已提交
614 615 616 617 618 619 620 621
{
	struct tegra_dpaux *dpaux;

	mutex_lock(&dpaux_lock);

	list_for_each_entry(dpaux, &dpaux_list, list)
		if (np == dpaux->dev->of_node) {
			mutex_unlock(&dpaux_lock);
622
			return &dpaux->aux;
T
Thierry Reding 已提交
623 624 625 626 627 628 629
		}

	mutex_unlock(&dpaux_lock);

	return NULL;
}

630
int drm_dp_aux_attach(struct drm_dp_aux *aux, struct tegra_output *output)
T
Thierry Reding 已提交
631
{
632
	struct tegra_dpaux *dpaux = to_dpaux(aux);
T
Thierry Reding 已提交
633 634 635
	unsigned long timeout;
	int err;

636
	output->connector.polled = DRM_CONNECTOR_POLL_HPD;
T
Thierry Reding 已提交
637 638 639 640 641 642 643 644 645 646 647
	dpaux->output = output;

	err = regulator_enable(dpaux->vdd);
	if (err < 0)
		return err;

	timeout = jiffies + msecs_to_jiffies(250);

	while (time_before(jiffies, timeout)) {
		enum drm_connector_status status;

648
		status = drm_dp_aux_detect(aux);
649 650
		if (status == connector_status_connected) {
			enable_irq(dpaux->irq);
T
Thierry Reding 已提交
651
			return 0;
652
		}
T
Thierry Reding 已提交
653 654 655 656 657 658 659

		usleep_range(1000, 2000);
	}

	return -ETIMEDOUT;
}

660
int drm_dp_aux_detach(struct drm_dp_aux *aux)
T
Thierry Reding 已提交
661
{
662
	struct tegra_dpaux *dpaux = to_dpaux(aux);
T
Thierry Reding 已提交
663 664 665
	unsigned long timeout;
	int err;

666 667
	disable_irq(dpaux->irq);

T
Thierry Reding 已提交
668 669 670 671 672 673 674 675 676
	err = regulator_disable(dpaux->vdd);
	if (err < 0)
		return err;

	timeout = jiffies + msecs_to_jiffies(250);

	while (time_before(jiffies, timeout)) {
		enum drm_connector_status status;

677
		status = drm_dp_aux_detect(aux);
T
Thierry Reding 已提交
678 679 680 681 682 683 684 685 686 687 688
		if (status == connector_status_disconnected) {
			dpaux->output = NULL;
			return 0;
		}

		usleep_range(1000, 2000);
	}

	return -ETIMEDOUT;
}

689
enum drm_connector_status drm_dp_aux_detect(struct drm_dp_aux *aux)
T
Thierry Reding 已提交
690
{
691
	struct tegra_dpaux *dpaux = to_dpaux(aux);
692
	u32 value;
T
Thierry Reding 已提交
693 694 695 696 697 698 699 700 701

	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);

	if (value & DPAUX_DP_AUXSTAT_HPD_STATUS)
		return connector_status_connected;

	return connector_status_disconnected;
}

702
int drm_dp_aux_enable(struct drm_dp_aux *aux)
T
Thierry Reding 已提交
703
{
704
	struct tegra_dpaux *dpaux = to_dpaux(aux);
T
Thierry Reding 已提交
705

706
	return tegra_dpaux_pad_config(dpaux, DPAUX_PADCTL_FUNC_AUX);
T
Thierry Reding 已提交
707 708
}

709
int drm_dp_aux_disable(struct drm_dp_aux *aux)
T
Thierry Reding 已提交
710
{
711
	struct tegra_dpaux *dpaux = to_dpaux(aux);
T
Thierry Reding 已提交
712

713
	tegra_dpaux_pad_power_down(dpaux);
T
Thierry Reding 已提交
714 715 716 717

	return 0;
}

718
int drm_dp_aux_prepare(struct drm_dp_aux *aux, u8 encoding)
T
Thierry Reding 已提交
719 720 721
{
	int err;

722
	err = drm_dp_dpcd_writeb(aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
T
Thierry Reding 已提交
723 724 725 726 727 728 729
				 encoding);
	if (err < 0)
		return err;

	return 0;
}

730 731
int drm_dp_aux_train(struct drm_dp_aux *aux, struct drm_dp_link *link,
		     u8 pattern)
T
Thierry Reding 已提交
732 733 734 735 736 737
{
	u8 tp = pattern & DP_TRAINING_PATTERN_MASK;
	u8 status[DP_LINK_STATUS_SIZE], values[4];
	unsigned int i;
	int err;

738
	err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, pattern);
T
Thierry Reding 已提交
739 740 741 742 743 744 745 746
	if (err < 0)
		return err;

	if (tp == DP_TRAINING_PATTERN_DISABLE)
		return 0;

	for (i = 0; i < link->num_lanes; i++)
		values[i] = DP_TRAIN_MAX_PRE_EMPHASIS_REACHED |
747
			    DP_TRAIN_PRE_EMPH_LEVEL_0 |
T
Thierry Reding 已提交
748
			    DP_TRAIN_MAX_SWING_REACHED |
749
			    DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
T
Thierry Reding 已提交
750

751
	err = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, values,
T
Thierry Reding 已提交
752 753 754 755 756 757
				link->num_lanes);
	if (err < 0)
		return err;

	usleep_range(500, 1000);

758
	err = drm_dp_dpcd_read_link_status(aux, status);
T
Thierry Reding 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
	if (err < 0)
		return err;

	switch (tp) {
	case DP_TRAINING_PATTERN_1:
		if (!drm_dp_clock_recovery_ok(status, link->num_lanes))
			return -EAGAIN;

		break;

	case DP_TRAINING_PATTERN_2:
		if (!drm_dp_channel_eq_ok(status, link->num_lanes))
			return -EAGAIN;

		break;

	default:
776
		dev_err(aux->dev, "unsupported training pattern %u\n", tp);
T
Thierry Reding 已提交
777 778 779
		return -EINVAL;
	}

780
	err = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, 0);
T
Thierry Reding 已提交
781 782 783 784 785
	if (err < 0)
		return err;

	return 0;
}