Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 * Copyright (C) 2013 NVIDIA Corporation
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License version 2 as
  6 * published by the Free Software Foundation.
  7 */
  8
  9#include <linux/clk.h>
 10#include <linux/delay.h>
 11#include <linux/gpio.h>
 12#include <linux/interrupt.h>
 13#include <linux/io.h>
 14#include <linux/of_gpio.h>
 
 15#include <linux/pinctrl/pinconf-generic.h>
 16#include <linux/pinctrl/pinctrl.h>
 17#include <linux/pinctrl/pinmux.h>
 18#include <linux/platform_device.h>
 19#include <linux/reset.h>
 20#include <linux/regulator/consumer.h>
 
 21#include <linux/workqueue.h>
 22
 23#include <drm/drm_dp_helper.h>
 
 24#include <drm/drm_panel.h>
 25
 
 26#include "dpaux.h"
 27#include "drm.h"
 
 28
 29static DEFINE_MUTEX(dpaux_lock);
 30static LIST_HEAD(dpaux_list);
 31
 
 
 
 
 
 
 32struct tegra_dpaux {
 33	struct drm_dp_aux aux;
 34	struct device *dev;
 35
 
 
 36	void __iomem *regs;
 37	int irq;
 38
 39	struct tegra_output *output;
 40
 41	struct reset_control *rst;
 42	struct clk *clk_parent;
 43	struct clk *clk;
 44
 45	struct regulator *vdd;
 46
 47	struct completion complete;
 48	struct work_struct work;
 49	struct list_head list;
 50
 51#ifdef CONFIG_GENERIC_PINCONF
 52	struct pinctrl_dev *pinctrl;
 53	struct pinctrl_desc desc;
 54#endif
 55};
 56
 57static inline struct tegra_dpaux *to_dpaux(struct drm_dp_aux *aux)
 58{
 59	return container_of(aux, struct tegra_dpaux, aux);
 60}
 61
 62static inline struct tegra_dpaux *work_to_dpaux(struct work_struct *work)
 63{
 64	return container_of(work, struct tegra_dpaux, work);
 65}
 66
 67static inline u32 tegra_dpaux_readl(struct tegra_dpaux *dpaux,
 68				    unsigned long offset)
 69{
 70	return readl(dpaux->regs + (offset << 2));
 
 
 
 
 71}
 72
 73static inline void tegra_dpaux_writel(struct tegra_dpaux *dpaux,
 74				      u32 value, unsigned long offset)
 75{
 
 76	writel(value, dpaux->regs + (offset << 2));
 77}
 78
 79static void tegra_dpaux_write_fifo(struct tegra_dpaux *dpaux, const u8 *buffer,
 80				   size_t size)
 81{
 82	size_t i, j;
 83
 84	for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
 85		size_t num = min_t(size_t, size - i * 4, 4);
 86		u32 value = 0;
 87
 88		for (j = 0; j < num; j++)
 89			value |= buffer[i * 4 + j] << (j * 8);
 90
 91		tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXDATA_WRITE(i));
 92	}
 93}
 94
 95static void tegra_dpaux_read_fifo(struct tegra_dpaux *dpaux, u8 *buffer,
 96				  size_t size)
 97{
 98	size_t i, j;
 99
100	for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
101		size_t num = min_t(size_t, size - i * 4, 4);
102		u32 value;
103
104		value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXDATA_READ(i));
105
106		for (j = 0; j < num; j++)
107			buffer[i * 4 + j] = value >> (j * 8);
108	}
109}
110
111static ssize_t tegra_dpaux_transfer(struct drm_dp_aux *aux,
112				    struct drm_dp_aux_msg *msg)
113{
114	unsigned long timeout = msecs_to_jiffies(250);
115	struct tegra_dpaux *dpaux = to_dpaux(aux);
116	unsigned long status;
117	ssize_t ret = 0;
 
118	u32 value;
119
120	/* Tegra has 4x4 byte DP AUX transmit and receive FIFOs. */
121	if (msg->size > 16)
122		return -EINVAL;
123
124	/*
125	 * Allow zero-sized messages only for I2C, in which case they specify
126	 * address-only transactions.
127	 */
128	if (msg->size < 1) {
129		switch (msg->request & ~DP_AUX_I2C_MOT) {
130		case DP_AUX_I2C_WRITE_STATUS_UPDATE:
131		case DP_AUX_I2C_WRITE:
132		case DP_AUX_I2C_READ:
133			value = DPAUX_DP_AUXCTL_CMD_ADDRESS_ONLY;
134			break;
135
136		default:
137			return -EINVAL;
138		}
139	} else {
140		/* For non-zero-sized messages, set the CMDLEN field. */
141		value = DPAUX_DP_AUXCTL_CMDLEN(msg->size - 1);
142	}
143
144	switch (msg->request & ~DP_AUX_I2C_MOT) {
145	case DP_AUX_I2C_WRITE:
146		if (msg->request & DP_AUX_I2C_MOT)
147			value |= DPAUX_DP_AUXCTL_CMD_MOT_WR;
148		else
149			value |= DPAUX_DP_AUXCTL_CMD_I2C_WR;
150
151		break;
152
153	case DP_AUX_I2C_READ:
154		if (msg->request & DP_AUX_I2C_MOT)
155			value |= DPAUX_DP_AUXCTL_CMD_MOT_RD;
156		else
157			value |= DPAUX_DP_AUXCTL_CMD_I2C_RD;
158
159		break;
160
161	case DP_AUX_I2C_WRITE_STATUS_UPDATE:
162		if (msg->request & DP_AUX_I2C_MOT)
163			value |= DPAUX_DP_AUXCTL_CMD_MOT_RQ;
164		else
165			value |= DPAUX_DP_AUXCTL_CMD_I2C_RQ;
166
167		break;
168
169	case DP_AUX_NATIVE_WRITE:
170		value |= DPAUX_DP_AUXCTL_CMD_AUX_WR;
171		break;
172
173	case DP_AUX_NATIVE_READ:
174		value |= DPAUX_DP_AUXCTL_CMD_AUX_RD;
175		break;
176
177	default:
178		return -EINVAL;
179	}
180
181	tegra_dpaux_writel(dpaux, msg->address, DPAUX_DP_AUXADDR);
182	tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
183
184	if ((msg->request & DP_AUX_I2C_READ) == 0) {
185		tegra_dpaux_write_fifo(dpaux, msg->buffer, msg->size);
186		ret = msg->size;
187	}
188
189	/* start transaction */
190	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXCTL);
191	value |= DPAUX_DP_AUXCTL_TRANSACTREQ;
192	tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
193
194	status = wait_for_completion_timeout(&dpaux->complete, timeout);
195	if (!status)
196		return -ETIMEDOUT;
197
198	/* read status and clear errors */
199	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
200	tegra_dpaux_writel(dpaux, 0xf00, DPAUX_DP_AUXSTAT);
201
202	if (value & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR)
203		return -ETIMEDOUT;
204
205	if ((value & DPAUX_DP_AUXSTAT_RX_ERROR) ||
206	    (value & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR) ||
207	    (value & DPAUX_DP_AUXSTAT_NO_STOP_ERROR))
208		return -EIO;
209
210	switch ((value & DPAUX_DP_AUXSTAT_REPLY_TYPE_MASK) >> 16) {
211	case 0x00:
212		msg->reply = DP_AUX_NATIVE_REPLY_ACK;
213		break;
214
215	case 0x01:
216		msg->reply = DP_AUX_NATIVE_REPLY_NACK;
217		break;
218
219	case 0x02:
220		msg->reply = DP_AUX_NATIVE_REPLY_DEFER;
221		break;
222
223	case 0x04:
224		msg->reply = DP_AUX_I2C_REPLY_NACK;
225		break;
226
227	case 0x08:
228		msg->reply = DP_AUX_I2C_REPLY_DEFER;
229		break;
230	}
231
232	if ((msg->size > 0) && (msg->reply == DP_AUX_NATIVE_REPLY_ACK)) {
233		if (msg->request & DP_AUX_I2C_READ) {
234			size_t count = value & DPAUX_DP_AUXSTAT_REPLY_MASK;
235
236			if (WARN_ON(count != msg->size))
237				count = min_t(size_t, count, msg->size);
 
 
 
 
 
 
 
238
239			tegra_dpaux_read_fifo(dpaux, msg->buffer, count);
240			ret = count;
241		}
242	}
243
 
 
 
244	return ret;
245}
246
247static void tegra_dpaux_hotplug(struct work_struct *work)
248{
249	struct tegra_dpaux *dpaux = work_to_dpaux(work);
250
251	if (dpaux->output)
252		drm_helper_hpd_irq_event(dpaux->output->connector.dev);
253}
254
255static irqreturn_t tegra_dpaux_irq(int irq, void *data)
256{
257	struct tegra_dpaux *dpaux = data;
258	irqreturn_t ret = IRQ_HANDLED;
259	u32 value;
260
261	/* clear interrupts */
262	value = tegra_dpaux_readl(dpaux, DPAUX_INTR_AUX);
263	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
264
265	if (value & (DPAUX_INTR_PLUG_EVENT | DPAUX_INTR_UNPLUG_EVENT))
266		schedule_work(&dpaux->work);
267
268	if (value & DPAUX_INTR_IRQ_EVENT) {
269		/* TODO: handle this */
270	}
271
272	if (value & DPAUX_INTR_AUX_DONE)
273		complete(&dpaux->complete);
274
275	return ret;
276}
277
278enum tegra_dpaux_functions {
279	DPAUX_PADCTL_FUNC_AUX,
280	DPAUX_PADCTL_FUNC_I2C,
281	DPAUX_PADCTL_FUNC_OFF,
282};
283
284static void tegra_dpaux_pad_power_down(struct tegra_dpaux *dpaux)
285{
286	u32 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
287
288	value |= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
289
290	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
291}
292
293static void tegra_dpaux_pad_power_up(struct tegra_dpaux *dpaux)
294{
295	u32 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
296
297	value &= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
298
299	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
300}
301
302static int tegra_dpaux_pad_config(struct tegra_dpaux *dpaux, unsigned function)
303{
304	u32 value;
305
306	switch (function) {
307	case DPAUX_PADCTL_FUNC_AUX:
308		value = DPAUX_HYBRID_PADCTL_AUX_CMH(2) |
309			DPAUX_HYBRID_PADCTL_AUX_DRVZ(4) |
310			DPAUX_HYBRID_PADCTL_AUX_DRVI(0x18) |
311			DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV |
312			DPAUX_HYBRID_PADCTL_MODE_AUX;
313		break;
314
315	case DPAUX_PADCTL_FUNC_I2C:
316		value = DPAUX_HYBRID_PADCTL_I2C_SDA_INPUT_RCV |
317			DPAUX_HYBRID_PADCTL_I2C_SCL_INPUT_RCV |
 
 
 
318			DPAUX_HYBRID_PADCTL_MODE_I2C;
319		break;
320
321	case DPAUX_PADCTL_FUNC_OFF:
322		tegra_dpaux_pad_power_down(dpaux);
323		return 0;
324
325	default:
326		return -ENOTSUPP;
327	}
328
329	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_PADCTL);
330	tegra_dpaux_pad_power_up(dpaux);
331
332	return 0;
333}
334
335#ifdef CONFIG_GENERIC_PINCONF
336static const struct pinctrl_pin_desc tegra_dpaux_pins[] = {
337	PINCTRL_PIN(0, "DP_AUX_CHx_P"),
338	PINCTRL_PIN(1, "DP_AUX_CHx_N"),
339};
340
341static const unsigned tegra_dpaux_pin_numbers[] = { 0, 1 };
342
343static const char * const tegra_dpaux_groups[] = {
344	"dpaux-io",
345};
346
347static const char * const tegra_dpaux_functions[] = {
348	"aux",
349	"i2c",
350	"off",
351};
352
353static int tegra_dpaux_get_groups_count(struct pinctrl_dev *pinctrl)
354{
355	return ARRAY_SIZE(tegra_dpaux_groups);
356}
357
358static const char *tegra_dpaux_get_group_name(struct pinctrl_dev *pinctrl,
359					      unsigned int group)
360{
361	return tegra_dpaux_groups[group];
362}
363
364static int tegra_dpaux_get_group_pins(struct pinctrl_dev *pinctrl,
365				      unsigned group, const unsigned **pins,
366				      unsigned *num_pins)
367{
368	*pins = tegra_dpaux_pin_numbers;
369	*num_pins = ARRAY_SIZE(tegra_dpaux_pin_numbers);
370
371	return 0;
372}
373
374static const struct pinctrl_ops tegra_dpaux_pinctrl_ops = {
375	.get_groups_count = tegra_dpaux_get_groups_count,
376	.get_group_name = tegra_dpaux_get_group_name,
377	.get_group_pins = tegra_dpaux_get_group_pins,
378	.dt_node_to_map = pinconf_generic_dt_node_to_map_group,
379	.dt_free_map = pinconf_generic_dt_free_map,
380};
381
382static int tegra_dpaux_get_functions_count(struct pinctrl_dev *pinctrl)
383{
384	return ARRAY_SIZE(tegra_dpaux_functions);
385}
386
387static const char *tegra_dpaux_get_function_name(struct pinctrl_dev *pinctrl,
388						 unsigned int function)
389{
390	return tegra_dpaux_functions[function];
391}
392
393static int tegra_dpaux_get_function_groups(struct pinctrl_dev *pinctrl,
394					   unsigned int function,
395					   const char * const **groups,
396					   unsigned * const num_groups)
397{
398	*num_groups = ARRAY_SIZE(tegra_dpaux_groups);
399	*groups = tegra_dpaux_groups;
400
401	return 0;
402}
403
404static int tegra_dpaux_set_mux(struct pinctrl_dev *pinctrl,
405			       unsigned int function, unsigned int group)
406{
407	struct tegra_dpaux *dpaux = pinctrl_dev_get_drvdata(pinctrl);
408
409	return tegra_dpaux_pad_config(dpaux, function);
410}
411
412static const struct pinmux_ops tegra_dpaux_pinmux_ops = {
413	.get_functions_count = tegra_dpaux_get_functions_count,
414	.get_function_name = tegra_dpaux_get_function_name,
415	.get_function_groups = tegra_dpaux_get_function_groups,
416	.set_mux = tegra_dpaux_set_mux,
417};
418#endif
419
420static int tegra_dpaux_probe(struct platform_device *pdev)
421{
422	struct tegra_dpaux *dpaux;
423	struct resource *regs;
424	u32 value;
425	int err;
426
427	dpaux = devm_kzalloc(&pdev->dev, sizeof(*dpaux), GFP_KERNEL);
428	if (!dpaux)
429		return -ENOMEM;
430
 
431	INIT_WORK(&dpaux->work, tegra_dpaux_hotplug);
432	init_completion(&dpaux->complete);
433	INIT_LIST_HEAD(&dpaux->list);
434	dpaux->dev = &pdev->dev;
435
436	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
437	dpaux->regs = devm_ioremap_resource(&pdev->dev, regs);
438	if (IS_ERR(dpaux->regs))
439		return PTR_ERR(dpaux->regs);
440
441	dpaux->irq = platform_get_irq(pdev, 0);
442	if (dpaux->irq < 0) {
443		dev_err(&pdev->dev, "failed to get IRQ\n");
444		return -ENXIO;
445	}
446
447	if (!pdev->dev.pm_domain) {
448		dpaux->rst = devm_reset_control_get(&pdev->dev, "dpaux");
449		if (IS_ERR(dpaux->rst)) {
450			dev_err(&pdev->dev,
451				"failed to get reset control: %ld\n",
452				PTR_ERR(dpaux->rst));
453			return PTR_ERR(dpaux->rst);
454		}
455	}
456
457	dpaux->clk = devm_clk_get(&pdev->dev, NULL);
458	if (IS_ERR(dpaux->clk)) {
459		dev_err(&pdev->dev, "failed to get module clock: %ld\n",
460			PTR_ERR(dpaux->clk));
461		return PTR_ERR(dpaux->clk);
462	}
463
464	err = clk_prepare_enable(dpaux->clk);
465	if (err < 0) {
466		dev_err(&pdev->dev, "failed to enable module clock: %d\n",
467			err);
468		return err;
469	}
470
471	if (dpaux->rst)
472		reset_control_deassert(dpaux->rst);
473
474	dpaux->clk_parent = devm_clk_get(&pdev->dev, "parent");
475	if (IS_ERR(dpaux->clk_parent)) {
476		dev_err(&pdev->dev, "failed to get parent clock: %ld\n",
477			PTR_ERR(dpaux->clk_parent));
478		err = PTR_ERR(dpaux->clk_parent);
479		goto assert_reset;
480	}
481
482	err = clk_prepare_enable(dpaux->clk_parent);
483	if (err < 0) {
484		dev_err(&pdev->dev, "failed to enable parent clock: %d\n",
485			err);
486		goto assert_reset;
487	}
488
489	err = clk_set_rate(dpaux->clk_parent, 270000000);
490	if (err < 0) {
491		dev_err(&pdev->dev, "failed to set clock to 270 MHz: %d\n",
492			err);
493		goto disable_parent_clk;
494	}
495
496	dpaux->vdd = devm_regulator_get(&pdev->dev, "vdd");
497	if (IS_ERR(dpaux->vdd)) {
498		dev_err(&pdev->dev, "failed to get VDD supply: %ld\n",
499			PTR_ERR(dpaux->vdd));
500		err = PTR_ERR(dpaux->vdd);
501		goto disable_parent_clk;
 
 
 
 
 
 
502	}
503
 
 
 
 
504	err = devm_request_irq(dpaux->dev, dpaux->irq, tegra_dpaux_irq, 0,
505			       dev_name(dpaux->dev), dpaux);
506	if (err < 0) {
507		dev_err(dpaux->dev, "failed to request IRQ#%u: %d\n",
508			dpaux->irq, err);
509		goto disable_parent_clk;
510	}
511
512	disable_irq(dpaux->irq);
513
514	dpaux->aux.transfer = tegra_dpaux_transfer;
515	dpaux->aux.dev = &pdev->dev;
516
517	err = drm_dp_aux_register(&dpaux->aux);
518	if (err < 0)
519		goto disable_parent_clk;
520
521	/*
522	 * Assume that by default the DPAUX/I2C pads will be used for HDMI,
523	 * so power them up and configure them in I2C mode.
524	 *
525	 * The DPAUX code paths reconfigure the pads in AUX mode, but there
526	 * is no possibility to perform the I2C mode configuration in the
527	 * HDMI path.
528	 */
529	err = tegra_dpaux_pad_config(dpaux, DPAUX_HYBRID_PADCTL_MODE_I2C);
530	if (err < 0)
531		return err;
532
533#ifdef CONFIG_GENERIC_PINCONF
534	dpaux->desc.name = dev_name(&pdev->dev);
535	dpaux->desc.pins = tegra_dpaux_pins;
536	dpaux->desc.npins = ARRAY_SIZE(tegra_dpaux_pins);
537	dpaux->desc.pctlops = &tegra_dpaux_pinctrl_ops;
538	dpaux->desc.pmxops = &tegra_dpaux_pinmux_ops;
539	dpaux->desc.owner = THIS_MODULE;
540
541	dpaux->pinctrl = devm_pinctrl_register(&pdev->dev, &dpaux->desc, dpaux);
542	if (IS_ERR(dpaux->pinctrl)) {
543		dev_err(&pdev->dev, "failed to register pincontrol\n");
544		return PTR_ERR(dpaux->pinctrl);
545	}
546#endif
547	/* enable and clear all interrupts */
548	value = DPAUX_INTR_AUX_DONE | DPAUX_INTR_IRQ_EVENT |
549		DPAUX_INTR_UNPLUG_EVENT | DPAUX_INTR_PLUG_EVENT;
550	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_EN_AUX);
551	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
552
553	mutex_lock(&dpaux_lock);
554	list_add_tail(&dpaux->list, &dpaux_list);
555	mutex_unlock(&dpaux_lock);
556
557	platform_set_drvdata(pdev, dpaux);
 
 
 
 
558
559	return 0;
560
561disable_parent_clk:
562	clk_disable_unprepare(dpaux->clk_parent);
563assert_reset:
564	if (dpaux->rst)
565		reset_control_assert(dpaux->rst);
566
567	clk_disable_unprepare(dpaux->clk);
568
569	return err;
570}
571
572static int tegra_dpaux_remove(struct platform_device *pdev)
573{
574	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);
578
579	drm_dp_aux_unregister(&dpaux->aux);
 
580
581	mutex_lock(&dpaux_lock);
582	list_del(&dpaux->list);
583	mutex_unlock(&dpaux_lock);
 
584
585	cancel_work_sync(&dpaux->work);
 
 
 
586
587	clk_disable_unprepare(dpaux->clk_parent);
 
 
 
 
 
 
588
589	if (dpaux->rst)
590		reset_control_assert(dpaux->rst);
591
 
592	clk_disable_unprepare(dpaux->clk);
593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594	return 0;
 
 
 
 
 
 
595}
596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597static const struct of_device_id tegra_dpaux_of_match[] = {
598	{ .compatible = "nvidia,tegra210-dpaux", },
599	{ .compatible = "nvidia,tegra124-dpaux", },
 
 
600	{ },
601};
602MODULE_DEVICE_TABLE(of, tegra_dpaux_of_match);
603
604struct platform_driver tegra_dpaux_driver = {
605	.driver = {
606		.name = "tegra-dpaux",
607		.of_match_table = tegra_dpaux_of_match,
 
608	},
609	.probe = tegra_dpaux_probe,
610	.remove = tegra_dpaux_remove,
611};
612
613struct drm_dp_aux *drm_dp_aux_find_by_of_node(struct device_node *np)
614{
615	struct tegra_dpaux *dpaux;
616
617	mutex_lock(&dpaux_lock);
618
619	list_for_each_entry(dpaux, &dpaux_list, list)
620		if (np == dpaux->dev->of_node) {
621			mutex_unlock(&dpaux_lock);
622			return &dpaux->aux;
623		}
624
625	mutex_unlock(&dpaux_lock);
626
627	return NULL;
628}
629
630int drm_dp_aux_attach(struct drm_dp_aux *aux, struct tegra_output *output)
631{
632	struct tegra_dpaux *dpaux = to_dpaux(aux);
633	unsigned long timeout;
634	int err;
635
636	output->connector.polled = DRM_CONNECTOR_POLL_HPD;
637	dpaux->output = output;
638
639	err = regulator_enable(dpaux->vdd);
640	if (err < 0)
641		return err;
642
643	timeout = jiffies + msecs_to_jiffies(250);
 
644
645	while (time_before(jiffies, timeout)) {
646		enum drm_connector_status status;
647
648		status = drm_dp_aux_detect(aux);
649		if (status == connector_status_connected) {
650			enable_irq(dpaux->irq);
651			return 0;
652		}
653
654		usleep_range(1000, 2000);
 
 
 
 
 
 
 
 
 
 
 
 
655	}
656
657	return -ETIMEDOUT;
 
658}
659
660int drm_dp_aux_detach(struct drm_dp_aux *aux)
661{
662	struct tegra_dpaux *dpaux = to_dpaux(aux);
663	unsigned long timeout;
664	int err;
665
 
666	disable_irq(dpaux->irq);
667
668	err = regulator_disable(dpaux->vdd);
669	if (err < 0)
670		return err;
 
 
 
 
 
671
672	timeout = jiffies + msecs_to_jiffies(250);
673
674	while (time_before(jiffies, timeout)) {
675		enum drm_connector_status status;
676
677		status = drm_dp_aux_detect(aux);
678		if (status == connector_status_disconnected) {
679			dpaux->output = NULL;
680			return 0;
681		}
682
683		usleep_range(1000, 2000);
 
 
 
684	}
685
686	return -ETIMEDOUT;
687}
688
689enum drm_connector_status drm_dp_aux_detect(struct drm_dp_aux *aux)
690{
691	struct tegra_dpaux *dpaux = to_dpaux(aux);
692	u32 value;
693
694	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
695
696	if (value & DPAUX_DP_AUXSTAT_HPD_STATUS)
697		return connector_status_connected;
698
699	return connector_status_disconnected;
700}
701
702int drm_dp_aux_enable(struct drm_dp_aux *aux)
703{
704	struct tegra_dpaux *dpaux = to_dpaux(aux);
705
706	return tegra_dpaux_pad_config(dpaux, DPAUX_PADCTL_FUNC_AUX);
707}
708
709int drm_dp_aux_disable(struct drm_dp_aux *aux)
710{
711	struct tegra_dpaux *dpaux = to_dpaux(aux);
712
713	tegra_dpaux_pad_power_down(dpaux);
714
715	return 0;
716}
717
718int drm_dp_aux_prepare(struct drm_dp_aux *aux, u8 encoding)
719{
720	int err;
721
722	err = drm_dp_dpcd_writeb(aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
723				 encoding);
724	if (err < 0)
725		return err;
726
727	return 0;
728}
729
730int drm_dp_aux_train(struct drm_dp_aux *aux, struct drm_dp_link *link,
731		     u8 pattern)
732{
733	u8 tp = pattern & DP_TRAINING_PATTERN_MASK;
734	u8 status[DP_LINK_STATUS_SIZE], values[4];
735	unsigned int i;
736	int err;
737
738	err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, pattern);
739	if (err < 0)
740		return err;
741
742	if (tp == DP_TRAINING_PATTERN_DISABLE)
743		return 0;
744
745	for (i = 0; i < link->num_lanes; i++)
746		values[i] = DP_TRAIN_MAX_PRE_EMPHASIS_REACHED |
747			    DP_TRAIN_PRE_EMPH_LEVEL_0 |
748			    DP_TRAIN_MAX_SWING_REACHED |
749			    DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
750
751	err = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, values,
752				link->num_lanes);
753	if (err < 0)
754		return err;
755
756	usleep_range(500, 1000);
757
758	err = drm_dp_dpcd_read_link_status(aux, status);
759	if (err < 0)
760		return err;
761
762	switch (tp) {
763	case DP_TRAINING_PATTERN_1:
764		if (!drm_dp_clock_recovery_ok(status, link->num_lanes))
765			return -EAGAIN;
766
767		break;
768
769	case DP_TRAINING_PATTERN_2:
770		if (!drm_dp_channel_eq_ok(status, link->num_lanes))
771			return -EAGAIN;
772
773		break;
774
775	default:
776		dev_err(aux->dev, "unsupported training pattern %u\n", tp);
777		return -EINVAL;
778	}
779
780	err = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, 0);
781	if (err < 0)
782		return err;
783
784	return 0;
785}
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2013 NVIDIA Corporation
 
 
 
 
  4 */
  5
  6#include <linux/clk.h>
  7#include <linux/delay.h>
 
  8#include <linux/interrupt.h>
  9#include <linux/io.h>
 10#include <linux/module.h>
 11#include <linux/of.h>
 12#include <linux/pinctrl/pinconf-generic.h>
 13#include <linux/pinctrl/pinctrl.h>
 14#include <linux/pinctrl/pinmux.h>
 15#include <linux/platform_device.h>
 16#include <linux/pm_runtime.h>
 17#include <linux/regulator/consumer.h>
 18#include <linux/reset.h>
 19#include <linux/workqueue.h>
 20
 21#include <drm/display/drm_dp_helper.h>
 22#include <drm/display/drm_dp_aux_bus.h>
 23#include <drm/drm_panel.h>
 24
 25#include "dp.h"
 26#include "dpaux.h"
 27#include "drm.h"
 28#include "trace.h"
 29
 30static DEFINE_MUTEX(dpaux_lock);
 31static LIST_HEAD(dpaux_list);
 32
 33struct tegra_dpaux_soc {
 34	unsigned int cmh;
 35	unsigned int drvz;
 36	unsigned int drvi;
 37};
 38
 39struct tegra_dpaux {
 40	struct drm_dp_aux aux;
 41	struct device *dev;
 42
 43	const struct tegra_dpaux_soc *soc;
 44
 45	void __iomem *regs;
 46	int irq;
 47
 48	struct tegra_output *output;
 49
 50	struct reset_control *rst;
 51	struct clk *clk_parent;
 52	struct clk *clk;
 53
 54	struct regulator *vdd;
 55
 56	struct completion complete;
 57	struct work_struct work;
 58	struct list_head list;
 59
 60#ifdef CONFIG_GENERIC_PINCONF
 61	struct pinctrl_dev *pinctrl;
 62	struct pinctrl_desc desc;
 63#endif
 64};
 65
 66static inline struct tegra_dpaux *to_dpaux(struct drm_dp_aux *aux)
 67{
 68	return container_of(aux, struct tegra_dpaux, aux);
 69}
 70
 71static inline struct tegra_dpaux *work_to_dpaux(struct work_struct *work)
 72{
 73	return container_of(work, struct tegra_dpaux, work);
 74}
 75
 76static inline u32 tegra_dpaux_readl(struct tegra_dpaux *dpaux,
 77				    unsigned int offset)
 78{
 79	u32 value = readl(dpaux->regs + (offset << 2));
 80
 81	trace_dpaux_readl(dpaux->dev, offset, value);
 82
 83	return value;
 84}
 85
 86static inline void tegra_dpaux_writel(struct tegra_dpaux *dpaux,
 87				      u32 value, unsigned int offset)
 88{
 89	trace_dpaux_writel(dpaux->dev, offset, value);
 90	writel(value, dpaux->regs + (offset << 2));
 91}
 92
 93static void tegra_dpaux_write_fifo(struct tegra_dpaux *dpaux, const u8 *buffer,
 94				   size_t size)
 95{
 96	size_t i, j;
 97
 98	for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
 99		size_t num = min_t(size_t, size - i * 4, 4);
100		u32 value = 0;
101
102		for (j = 0; j < num; j++)
103			value |= buffer[i * 4 + j] << (j * 8);
104
105		tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXDATA_WRITE(i));
106	}
107}
108
109static void tegra_dpaux_read_fifo(struct tegra_dpaux *dpaux, u8 *buffer,
110				  size_t size)
111{
112	size_t i, j;
113
114	for (i = 0; i < DIV_ROUND_UP(size, 4); i++) {
115		size_t num = min_t(size_t, size - i * 4, 4);
116		u32 value;
117
118		value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXDATA_READ(i));
119
120		for (j = 0; j < num; j++)
121			buffer[i * 4 + j] = value >> (j * 8);
122	}
123}
124
125static ssize_t tegra_dpaux_transfer(struct drm_dp_aux *aux,
126				    struct drm_dp_aux_msg *msg)
127{
128	unsigned long timeout = msecs_to_jiffies(250);
129	struct tegra_dpaux *dpaux = to_dpaux(aux);
130	unsigned long status;
131	ssize_t ret = 0;
132	u8 reply = 0;
133	u32 value;
134
135	/* Tegra has 4x4 byte DP AUX transmit and receive FIFOs. */
136	if (msg->size > 16)
137		return -EINVAL;
138
139	/*
140	 * Allow zero-sized messages only for I2C, in which case they specify
141	 * address-only transactions.
142	 */
143	if (msg->size < 1) {
144		switch (msg->request & ~DP_AUX_I2C_MOT) {
145		case DP_AUX_I2C_WRITE_STATUS_UPDATE:
146		case DP_AUX_I2C_WRITE:
147		case DP_AUX_I2C_READ:
148			value = DPAUX_DP_AUXCTL_CMD_ADDRESS_ONLY;
149			break;
150
151		default:
152			return -EINVAL;
153		}
154	} else {
155		/* For non-zero-sized messages, set the CMDLEN field. */
156		value = DPAUX_DP_AUXCTL_CMDLEN(msg->size - 1);
157	}
158
159	switch (msg->request & ~DP_AUX_I2C_MOT) {
160	case DP_AUX_I2C_WRITE:
161		if (msg->request & DP_AUX_I2C_MOT)
162			value |= DPAUX_DP_AUXCTL_CMD_MOT_WR;
163		else
164			value |= DPAUX_DP_AUXCTL_CMD_I2C_WR;
165
166		break;
167
168	case DP_AUX_I2C_READ:
169		if (msg->request & DP_AUX_I2C_MOT)
170			value |= DPAUX_DP_AUXCTL_CMD_MOT_RD;
171		else
172			value |= DPAUX_DP_AUXCTL_CMD_I2C_RD;
173
174		break;
175
176	case DP_AUX_I2C_WRITE_STATUS_UPDATE:
177		if (msg->request & DP_AUX_I2C_MOT)
178			value |= DPAUX_DP_AUXCTL_CMD_MOT_RQ;
179		else
180			value |= DPAUX_DP_AUXCTL_CMD_I2C_RQ;
181
182		break;
183
184	case DP_AUX_NATIVE_WRITE:
185		value |= DPAUX_DP_AUXCTL_CMD_AUX_WR;
186		break;
187
188	case DP_AUX_NATIVE_READ:
189		value |= DPAUX_DP_AUXCTL_CMD_AUX_RD;
190		break;
191
192	default:
193		return -EINVAL;
194	}
195
196	tegra_dpaux_writel(dpaux, msg->address, DPAUX_DP_AUXADDR);
197	tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
198
199	if ((msg->request & DP_AUX_I2C_READ) == 0) {
200		tegra_dpaux_write_fifo(dpaux, msg->buffer, msg->size);
201		ret = msg->size;
202	}
203
204	/* start transaction */
205	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXCTL);
206	value |= DPAUX_DP_AUXCTL_TRANSACTREQ;
207	tegra_dpaux_writel(dpaux, value, DPAUX_DP_AUXCTL);
208
209	status = wait_for_completion_timeout(&dpaux->complete, timeout);
210	if (!status)
211		return -ETIMEDOUT;
212
213	/* read status and clear errors */
214	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
215	tegra_dpaux_writel(dpaux, 0xf00, DPAUX_DP_AUXSTAT);
216
217	if (value & DPAUX_DP_AUXSTAT_TIMEOUT_ERROR)
218		return -ETIMEDOUT;
219
220	if ((value & DPAUX_DP_AUXSTAT_RX_ERROR) ||
221	    (value & DPAUX_DP_AUXSTAT_SINKSTAT_ERROR) ||
222	    (value & DPAUX_DP_AUXSTAT_NO_STOP_ERROR))
223		return -EIO;
224
225	switch ((value & DPAUX_DP_AUXSTAT_REPLY_TYPE_MASK) >> 16) {
226	case 0x00:
227		reply = DP_AUX_NATIVE_REPLY_ACK;
228		break;
229
230	case 0x01:
231		reply = DP_AUX_NATIVE_REPLY_NACK;
232		break;
233
234	case 0x02:
235		reply = DP_AUX_NATIVE_REPLY_DEFER;
236		break;
237
238	case 0x04:
239		reply = DP_AUX_I2C_REPLY_NACK;
240		break;
241
242	case 0x08:
243		reply = DP_AUX_I2C_REPLY_DEFER;
244		break;
245	}
246
247	if ((msg->size > 0) && (msg->reply == DP_AUX_NATIVE_REPLY_ACK)) {
248		if (msg->request & DP_AUX_I2C_READ) {
249			size_t count = value & DPAUX_DP_AUXSTAT_REPLY_MASK;
250
251			/*
252			 * There might be a smarter way to do this, but since
253			 * the DP helpers will already retry transactions for
254			 * an -EBUSY return value, simply reuse that instead.
255			 */
256			if (count != msg->size) {
257				ret = -EBUSY;
258				goto out;
259			}
260
261			tegra_dpaux_read_fifo(dpaux, msg->buffer, count);
262			ret = count;
263		}
264	}
265
266	msg->reply = reply;
267
268out:
269	return ret;
270}
271
272static void tegra_dpaux_hotplug(struct work_struct *work)
273{
274	struct tegra_dpaux *dpaux = work_to_dpaux(work);
275
276	if (dpaux->output)
277		drm_helper_hpd_irq_event(dpaux->output->connector.dev);
278}
279
280static irqreturn_t tegra_dpaux_irq(int irq, void *data)
281{
282	struct tegra_dpaux *dpaux = data;
 
283	u32 value;
284
285	/* clear interrupts */
286	value = tegra_dpaux_readl(dpaux, DPAUX_INTR_AUX);
287	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
288
289	if (value & (DPAUX_INTR_PLUG_EVENT | DPAUX_INTR_UNPLUG_EVENT))
290		schedule_work(&dpaux->work);
291
292	if (value & DPAUX_INTR_IRQ_EVENT) {
293		/* TODO: handle this */
294	}
295
296	if (value & DPAUX_INTR_AUX_DONE)
297		complete(&dpaux->complete);
298
299	return IRQ_HANDLED;
300}
301
302enum tegra_dpaux_functions {
303	DPAUX_PADCTL_FUNC_AUX,
304	DPAUX_PADCTL_FUNC_I2C,
305	DPAUX_PADCTL_FUNC_OFF,
306};
307
308static void tegra_dpaux_pad_power_down(struct tegra_dpaux *dpaux)
309{
310	u32 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
311
312	value |= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
313
314	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
315}
316
317static void tegra_dpaux_pad_power_up(struct tegra_dpaux *dpaux)
318{
319	u32 value = tegra_dpaux_readl(dpaux, DPAUX_HYBRID_SPARE);
320
321	value &= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN;
322
323	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_SPARE);
324}
325
326static int tegra_dpaux_pad_config(struct tegra_dpaux *dpaux, unsigned function)
327{
328	u32 value;
329
330	switch (function) {
331	case DPAUX_PADCTL_FUNC_AUX:
332		value = DPAUX_HYBRID_PADCTL_AUX_CMH(dpaux->soc->cmh) |
333			DPAUX_HYBRID_PADCTL_AUX_DRVZ(dpaux->soc->drvz) |
334			DPAUX_HYBRID_PADCTL_AUX_DRVI(dpaux->soc->drvi) |
335			DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV |
336			DPAUX_HYBRID_PADCTL_MODE_AUX;
337		break;
338
339	case DPAUX_PADCTL_FUNC_I2C:
340		value = DPAUX_HYBRID_PADCTL_I2C_SDA_INPUT_RCV |
341			DPAUX_HYBRID_PADCTL_I2C_SCL_INPUT_RCV |
342			DPAUX_HYBRID_PADCTL_AUX_CMH(dpaux->soc->cmh) |
343			DPAUX_HYBRID_PADCTL_AUX_DRVZ(dpaux->soc->drvz) |
344			DPAUX_HYBRID_PADCTL_AUX_DRVI(dpaux->soc->drvi) |
345			DPAUX_HYBRID_PADCTL_MODE_I2C;
346		break;
347
348	case DPAUX_PADCTL_FUNC_OFF:
349		tegra_dpaux_pad_power_down(dpaux);
350		return 0;
351
352	default:
353		return -ENOTSUPP;
354	}
355
356	tegra_dpaux_writel(dpaux, value, DPAUX_HYBRID_PADCTL);
357	tegra_dpaux_pad_power_up(dpaux);
358
359	return 0;
360}
361
362#ifdef CONFIG_GENERIC_PINCONF
363static const struct pinctrl_pin_desc tegra_dpaux_pins[] = {
364	PINCTRL_PIN(0, "DP_AUX_CHx_P"),
365	PINCTRL_PIN(1, "DP_AUX_CHx_N"),
366};
367
368static const unsigned tegra_dpaux_pin_numbers[] = { 0, 1 };
369
370static const char * const tegra_dpaux_groups[] = {
371	"dpaux-io",
372};
373
374static const char * const tegra_dpaux_functions[] = {
375	"aux",
376	"i2c",
377	"off",
378};
379
380static int tegra_dpaux_get_groups_count(struct pinctrl_dev *pinctrl)
381{
382	return ARRAY_SIZE(tegra_dpaux_groups);
383}
384
385static const char *tegra_dpaux_get_group_name(struct pinctrl_dev *pinctrl,
386					      unsigned int group)
387{
388	return tegra_dpaux_groups[group];
389}
390
391static int tegra_dpaux_get_group_pins(struct pinctrl_dev *pinctrl,
392				      unsigned group, const unsigned **pins,
393				      unsigned *num_pins)
394{
395	*pins = tegra_dpaux_pin_numbers;
396	*num_pins = ARRAY_SIZE(tegra_dpaux_pin_numbers);
397
398	return 0;
399}
400
401static const struct pinctrl_ops tegra_dpaux_pinctrl_ops = {
402	.get_groups_count = tegra_dpaux_get_groups_count,
403	.get_group_name = tegra_dpaux_get_group_name,
404	.get_group_pins = tegra_dpaux_get_group_pins,
405	.dt_node_to_map = pinconf_generic_dt_node_to_map_group,
406	.dt_free_map = pinconf_generic_dt_free_map,
407};
408
409static int tegra_dpaux_get_functions_count(struct pinctrl_dev *pinctrl)
410{
411	return ARRAY_SIZE(tegra_dpaux_functions);
412}
413
414static const char *tegra_dpaux_get_function_name(struct pinctrl_dev *pinctrl,
415						 unsigned int function)
416{
417	return tegra_dpaux_functions[function];
418}
419
420static int tegra_dpaux_get_function_groups(struct pinctrl_dev *pinctrl,
421					   unsigned int function,
422					   const char * const **groups,
423					   unsigned * const num_groups)
424{
425	*num_groups = ARRAY_SIZE(tegra_dpaux_groups);
426	*groups = tegra_dpaux_groups;
427
428	return 0;
429}
430
431static int tegra_dpaux_set_mux(struct pinctrl_dev *pinctrl,
432			       unsigned int function, unsigned int group)
433{
434	struct tegra_dpaux *dpaux = pinctrl_dev_get_drvdata(pinctrl);
435
436	return tegra_dpaux_pad_config(dpaux, function);
437}
438
439static const struct pinmux_ops tegra_dpaux_pinmux_ops = {
440	.get_functions_count = tegra_dpaux_get_functions_count,
441	.get_function_name = tegra_dpaux_get_function_name,
442	.get_function_groups = tegra_dpaux_get_function_groups,
443	.set_mux = tegra_dpaux_set_mux,
444};
445#endif
446
447static int tegra_dpaux_probe(struct platform_device *pdev)
448{
449	struct tegra_dpaux *dpaux;
 
450	u32 value;
451	int err;
452
453	dpaux = devm_kzalloc(&pdev->dev, sizeof(*dpaux), GFP_KERNEL);
454	if (!dpaux)
455		return -ENOMEM;
456
457	dpaux->soc = of_device_get_match_data(&pdev->dev);
458	INIT_WORK(&dpaux->work, tegra_dpaux_hotplug);
459	init_completion(&dpaux->complete);
460	INIT_LIST_HEAD(&dpaux->list);
461	dpaux->dev = &pdev->dev;
462
463	dpaux->regs = devm_platform_ioremap_resource(pdev, 0);
 
464	if (IS_ERR(dpaux->regs))
465		return PTR_ERR(dpaux->regs);
466
467	dpaux->irq = platform_get_irq(pdev, 0);
468	if (dpaux->irq < 0)
469		return dpaux->irq;
 
 
470
471	if (!pdev->dev.pm_domain) {
472		dpaux->rst = devm_reset_control_get(&pdev->dev, "dpaux");
473		if (IS_ERR(dpaux->rst)) {
474			dev_err(&pdev->dev,
475				"failed to get reset control: %ld\n",
476				PTR_ERR(dpaux->rst));
477			return PTR_ERR(dpaux->rst);
478		}
479	}
480
481	dpaux->clk = devm_clk_get(&pdev->dev, NULL);
482	if (IS_ERR(dpaux->clk)) {
483		dev_err(&pdev->dev, "failed to get module clock: %ld\n",
484			PTR_ERR(dpaux->clk));
485		return PTR_ERR(dpaux->clk);
486	}
487
 
 
 
 
 
 
 
 
 
 
488	dpaux->clk_parent = devm_clk_get(&pdev->dev, "parent");
489	if (IS_ERR(dpaux->clk_parent)) {
490		dev_err(&pdev->dev, "failed to get parent clock: %ld\n",
491			PTR_ERR(dpaux->clk_parent));
492		return PTR_ERR(dpaux->clk_parent);
 
 
 
 
 
 
 
 
493	}
494
495	err = clk_set_rate(dpaux->clk_parent, 270000000);
496	if (err < 0) {
497		dev_err(&pdev->dev, "failed to set clock to 270 MHz: %d\n",
498			err);
499		return err;
500	}
501
502	dpaux->vdd = devm_regulator_get_optional(&pdev->dev, "vdd");
503	if (IS_ERR(dpaux->vdd)) {
504		if (PTR_ERR(dpaux->vdd) != -ENODEV) {
505			if (PTR_ERR(dpaux->vdd) != -EPROBE_DEFER)
506				dev_err(&pdev->dev,
507					"failed to get VDD supply: %ld\n",
508					PTR_ERR(dpaux->vdd));
509
510			return PTR_ERR(dpaux->vdd);
511		}
512
513		dpaux->vdd = NULL;
514	}
515
516	platform_set_drvdata(pdev, dpaux);
517	pm_runtime_enable(&pdev->dev);
518	pm_runtime_get_sync(&pdev->dev);
519
520	err = devm_request_irq(dpaux->dev, dpaux->irq, tegra_dpaux_irq, 0,
521			       dev_name(dpaux->dev), dpaux);
522	if (err < 0) {
523		dev_err(dpaux->dev, "failed to request IRQ#%u: %d\n",
524			dpaux->irq, err);
525		return err;
526	}
527
528	disable_irq(dpaux->irq);
529
530	dpaux->aux.transfer = tegra_dpaux_transfer;
531	dpaux->aux.dev = &pdev->dev;
532
533	drm_dp_aux_init(&dpaux->aux);
 
 
534
535	/*
536	 * Assume that by default the DPAUX/I2C pads will be used for HDMI,
537	 * so power them up and configure them in I2C mode.
538	 *
539	 * The DPAUX code paths reconfigure the pads in AUX mode, but there
540	 * is no possibility to perform the I2C mode configuration in the
541	 * HDMI path.
542	 */
543	err = tegra_dpaux_pad_config(dpaux, DPAUX_PADCTL_FUNC_I2C);
544	if (err < 0)
545		return err;
546
547#ifdef CONFIG_GENERIC_PINCONF
548	dpaux->desc.name = dev_name(&pdev->dev);
549	dpaux->desc.pins = tegra_dpaux_pins;
550	dpaux->desc.npins = ARRAY_SIZE(tegra_dpaux_pins);
551	dpaux->desc.pctlops = &tegra_dpaux_pinctrl_ops;
552	dpaux->desc.pmxops = &tegra_dpaux_pinmux_ops;
553	dpaux->desc.owner = THIS_MODULE;
554
555	dpaux->pinctrl = devm_pinctrl_register(&pdev->dev, &dpaux->desc, dpaux);
556	if (IS_ERR(dpaux->pinctrl)) {
557		dev_err(&pdev->dev, "failed to register pincontrol\n");
558		return PTR_ERR(dpaux->pinctrl);
559	}
560#endif
561	/* enable and clear all interrupts */
562	value = DPAUX_INTR_AUX_DONE | DPAUX_INTR_IRQ_EVENT |
563		DPAUX_INTR_UNPLUG_EVENT | DPAUX_INTR_PLUG_EVENT;
564	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_EN_AUX);
565	tegra_dpaux_writel(dpaux, value, DPAUX_INTR_AUX);
566
567	mutex_lock(&dpaux_lock);
568	list_add_tail(&dpaux->list, &dpaux_list);
569	mutex_unlock(&dpaux_lock);
570
571	err = devm_of_dp_aux_populate_ep_devices(&dpaux->aux);
572	if (err < 0) {
573		dev_err(dpaux->dev, "failed to populate AUX bus: %d\n", err);
574		return err;
575	}
576
577	return 0;
 
 
 
 
 
 
 
 
 
 
578}
579
580static void tegra_dpaux_remove(struct platform_device *pdev)
581{
582	struct tegra_dpaux *dpaux = platform_get_drvdata(pdev);
583
584	cancel_work_sync(&dpaux->work);
585
586	/* make sure pads are powered down when not in use */
587	tegra_dpaux_pad_power_down(dpaux);
588
589	pm_runtime_put_sync(&pdev->dev);
590	pm_runtime_disable(&pdev->dev);
591
592	mutex_lock(&dpaux_lock);
593	list_del(&dpaux->list);
594	mutex_unlock(&dpaux_lock);
595}
596
597static int tegra_dpaux_suspend(struct device *dev)
598{
599	struct tegra_dpaux *dpaux = dev_get_drvdata(dev);
600	int err = 0;
601
602	if (dpaux->rst) {
603		err = reset_control_assert(dpaux->rst);
604		if (err < 0) {
605			dev_err(dev, "failed to assert reset: %d\n", err);
606			return err;
607		}
608	}
609
610	usleep_range(1000, 2000);
 
611
612	clk_disable_unprepare(dpaux->clk_parent);
613	clk_disable_unprepare(dpaux->clk);
614
615	return err;
616}
617
618static int tegra_dpaux_resume(struct device *dev)
619{
620	struct tegra_dpaux *dpaux = dev_get_drvdata(dev);
621	int err;
622
623	err = clk_prepare_enable(dpaux->clk);
624	if (err < 0) {
625		dev_err(dev, "failed to enable clock: %d\n", err);
626		return err;
627	}
628
629	err = clk_prepare_enable(dpaux->clk_parent);
630	if (err < 0) {
631		dev_err(dev, "failed to enable parent clock: %d\n", err);
632		goto disable_clk;
633	}
634
635	usleep_range(1000, 2000);
636
637	if (dpaux->rst) {
638		err = reset_control_deassert(dpaux->rst);
639		if (err < 0) {
640			dev_err(dev, "failed to deassert reset: %d\n", err);
641			goto disable_parent;
642		}
643
644		usleep_range(1000, 2000);
645	}
646
647	return 0;
648
649disable_parent:
650	clk_disable_unprepare(dpaux->clk_parent);
651disable_clk:
652	clk_disable_unprepare(dpaux->clk);
653	return err;
654}
655
656static const struct dev_pm_ops tegra_dpaux_pm_ops = {
657	RUNTIME_PM_OPS(tegra_dpaux_suspend, tegra_dpaux_resume, NULL)
658};
659
660static const struct tegra_dpaux_soc tegra124_dpaux_soc = {
661	.cmh = 0x02,
662	.drvz = 0x04,
663	.drvi = 0x18,
664};
665
666static const struct tegra_dpaux_soc tegra210_dpaux_soc = {
667	.cmh = 0x02,
668	.drvz = 0x04,
669	.drvi = 0x30,
670};
671
672static const struct tegra_dpaux_soc tegra194_dpaux_soc = {
673	.cmh = 0x02,
674	.drvz = 0x04,
675	.drvi = 0x2c,
676};
677
678static const struct of_device_id tegra_dpaux_of_match[] = {
679	{ .compatible = "nvidia,tegra194-dpaux", .data = &tegra194_dpaux_soc },
680	{ .compatible = "nvidia,tegra186-dpaux", .data = &tegra210_dpaux_soc },
681	{ .compatible = "nvidia,tegra210-dpaux", .data = &tegra210_dpaux_soc },
682	{ .compatible = "nvidia,tegra124-dpaux", .data = &tegra124_dpaux_soc },
683	{ },
684};
685MODULE_DEVICE_TABLE(of, tegra_dpaux_of_match);
686
687struct platform_driver tegra_dpaux_driver = {
688	.driver = {
689		.name = "tegra-dpaux",
690		.of_match_table = tegra_dpaux_of_match,
691		.pm = pm_ptr(&tegra_dpaux_pm_ops),
692	},
693	.probe = tegra_dpaux_probe,
694	.remove_new = tegra_dpaux_remove,
695};
696
697struct drm_dp_aux *drm_dp_aux_find_by_of_node(struct device_node *np)
698{
699	struct tegra_dpaux *dpaux;
700
701	mutex_lock(&dpaux_lock);
702
703	list_for_each_entry(dpaux, &dpaux_list, list)
704		if (np == dpaux->dev->of_node) {
705			mutex_unlock(&dpaux_lock);
706			return &dpaux->aux;
707		}
708
709	mutex_unlock(&dpaux_lock);
710
711	return NULL;
712}
713
714int drm_dp_aux_attach(struct drm_dp_aux *aux, struct tegra_output *output)
715{
716	struct tegra_dpaux *dpaux = to_dpaux(aux);
717	unsigned long timeout;
718	int err;
719
720	aux->drm_dev = output->connector.dev;
721	err = drm_dp_aux_register(aux);
 
 
722	if (err < 0)
723		return err;
724
725	output->connector.polled = DRM_CONNECTOR_POLL_HPD;
726	dpaux->output = output;
727
728	if (output->panel) {
729		enum drm_connector_status status;
730
731		if (dpaux->vdd) {
732			err = regulator_enable(dpaux->vdd);
733			if (err < 0)
734				return err;
735		}
736
737		timeout = jiffies + msecs_to_jiffies(250);
738
739		while (time_before(jiffies, timeout)) {
740			status = drm_dp_aux_detect(aux);
741
742			if (status == connector_status_connected)
743				break;
744
745			usleep_range(1000, 2000);
746		}
747
748		if (status != connector_status_connected)
749			return -ETIMEDOUT;
750	}
751
752	enable_irq(dpaux->irq);
753	return 0;
754}
755
756int drm_dp_aux_detach(struct drm_dp_aux *aux)
757{
758	struct tegra_dpaux *dpaux = to_dpaux(aux);
759	unsigned long timeout;
760	int err;
761
762	drm_dp_aux_unregister(aux);
763	disable_irq(dpaux->irq);
764
765	if (dpaux->output->panel) {
766		enum drm_connector_status status;
767
768		if (dpaux->vdd) {
769			err = regulator_disable(dpaux->vdd);
770			if (err < 0)
771				return err;
772		}
773
774		timeout = jiffies + msecs_to_jiffies(250);
775
776		while (time_before(jiffies, timeout)) {
777			status = drm_dp_aux_detect(aux);
778
779			if (status == connector_status_disconnected)
780				break;
781
782			usleep_range(1000, 2000);
783		}
784
785		if (status != connector_status_disconnected)
786			return -ETIMEDOUT;
787
788		dpaux->output = NULL;
789	}
790
791	return 0;
792}
793
794enum drm_connector_status drm_dp_aux_detect(struct drm_dp_aux *aux)
795{
796	struct tegra_dpaux *dpaux = to_dpaux(aux);
797	u32 value;
798
799	value = tegra_dpaux_readl(dpaux, DPAUX_DP_AUXSTAT);
800
801	if (value & DPAUX_DP_AUXSTAT_HPD_STATUS)
802		return connector_status_connected;
803
804	return connector_status_disconnected;
805}
806
807int drm_dp_aux_enable(struct drm_dp_aux *aux)
808{
809	struct tegra_dpaux *dpaux = to_dpaux(aux);
810
811	return tegra_dpaux_pad_config(dpaux, DPAUX_PADCTL_FUNC_AUX);
812}
813
814int drm_dp_aux_disable(struct drm_dp_aux *aux)
815{
816	struct tegra_dpaux *dpaux = to_dpaux(aux);
817
818	tegra_dpaux_pad_power_down(dpaux);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
819
820	return 0;
821}