2 * Copyright (C) 2013 NVIDIA Corporation
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.
10 #include <linux/delay.h>
11 #include <linux/gpio.h>
12 #include <linux/interrupt.h>
14 #include <linux/of_gpio.h>
15 #include <linux/platform_device.h>
16 #include <linux/reset.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/workqueue.h>
20 #include <drm/drm_dp_helper.h>
21 #include <drm/drm_panel.h>
26 static DEFINE_MUTEX(dpaux_lock
);
27 static LIST_HEAD(dpaux_list
);
30 struct drm_dp_aux aux
;
36 struct tegra_output
*output
;
38 struct reset_control
*rst
;
39 struct clk
*clk_parent
;
42 struct regulator
*vdd
;
44 struct completion complete
;
45 struct work_struct work
;
46 struct list_head list
;
49 static inline struct tegra_dpaux
*to_dpaux(struct drm_dp_aux
*aux
)
51 return container_of(aux
, struct tegra_dpaux
, aux
);
54 static inline struct tegra_dpaux
*work_to_dpaux(struct work_struct
*work
)
56 return container_of(work
, struct tegra_dpaux
, work
);
59 static inline u32
tegra_dpaux_readl(struct tegra_dpaux
*dpaux
,
62 return readl(dpaux
->regs
+ (offset
<< 2));
65 static inline void tegra_dpaux_writel(struct tegra_dpaux
*dpaux
,
66 u32 value
, unsigned long offset
)
68 writel(value
, dpaux
->regs
+ (offset
<< 2));
71 static void tegra_dpaux_write_fifo(struct tegra_dpaux
*dpaux
, const u8
*buffer
,
76 for (i
= 0; i
< DIV_ROUND_UP(size
, 4); i
++) {
77 size_t num
= min_t(size_t, size
- i
* 4, 4);
80 for (j
= 0; j
< num
; j
++)
81 value
|= buffer
[i
* 4 + j
] << (j
* 8);
83 tegra_dpaux_writel(dpaux
, value
, DPAUX_DP_AUXDATA_WRITE(i
));
87 static void tegra_dpaux_read_fifo(struct tegra_dpaux
*dpaux
, u8
*buffer
,
92 for (i
= 0; i
< DIV_ROUND_UP(size
, 4); i
++) {
93 size_t num
= min_t(size_t, size
- i
* 4, 4);
96 value
= tegra_dpaux_readl(dpaux
, DPAUX_DP_AUXDATA_READ(i
));
98 for (j
= 0; j
< num
; j
++)
99 buffer
[i
* 4 + j
] = value
>> (j
* 8);
103 static ssize_t
tegra_dpaux_transfer(struct drm_dp_aux
*aux
,
104 struct drm_dp_aux_msg
*msg
)
106 unsigned long timeout
= msecs_to_jiffies(250);
107 struct tegra_dpaux
*dpaux
= to_dpaux(aux
);
108 unsigned long status
;
112 /* Tegra has 4x4 byte DP AUX transmit and receive FIFOs. */
117 * Allow zero-sized messages only for I2C, in which case they specify
118 * address-only transactions.
121 switch (msg
->request
& ~DP_AUX_I2C_MOT
) {
122 case DP_AUX_I2C_WRITE_STATUS_UPDATE
:
123 case DP_AUX_I2C_WRITE
:
124 case DP_AUX_I2C_READ
:
125 value
= DPAUX_DP_AUXCTL_CMD_ADDRESS_ONLY
;
132 /* For non-zero-sized messages, set the CMDLEN field. */
133 value
= DPAUX_DP_AUXCTL_CMDLEN(msg
->size
- 1);
136 switch (msg
->request
& ~DP_AUX_I2C_MOT
) {
137 case DP_AUX_I2C_WRITE
:
138 if (msg
->request
& DP_AUX_I2C_MOT
)
139 value
|= DPAUX_DP_AUXCTL_CMD_MOT_WR
;
141 value
|= DPAUX_DP_AUXCTL_CMD_I2C_WR
;
145 case DP_AUX_I2C_READ
:
146 if (msg
->request
& DP_AUX_I2C_MOT
)
147 value
|= DPAUX_DP_AUXCTL_CMD_MOT_RD
;
149 value
|= DPAUX_DP_AUXCTL_CMD_I2C_RD
;
153 case DP_AUX_I2C_WRITE_STATUS_UPDATE
:
154 if (msg
->request
& DP_AUX_I2C_MOT
)
155 value
|= DPAUX_DP_AUXCTL_CMD_MOT_RQ
;
157 value
|= DPAUX_DP_AUXCTL_CMD_I2C_RQ
;
161 case DP_AUX_NATIVE_WRITE
:
162 value
|= DPAUX_DP_AUXCTL_CMD_AUX_WR
;
165 case DP_AUX_NATIVE_READ
:
166 value
|= DPAUX_DP_AUXCTL_CMD_AUX_RD
;
173 tegra_dpaux_writel(dpaux
, msg
->address
, DPAUX_DP_AUXADDR
);
174 tegra_dpaux_writel(dpaux
, value
, DPAUX_DP_AUXCTL
);
176 if ((msg
->request
& DP_AUX_I2C_READ
) == 0) {
177 tegra_dpaux_write_fifo(dpaux
, msg
->buffer
, msg
->size
);
181 /* start transaction */
182 value
= tegra_dpaux_readl(dpaux
, DPAUX_DP_AUXCTL
);
183 value
|= DPAUX_DP_AUXCTL_TRANSACTREQ
;
184 tegra_dpaux_writel(dpaux
, value
, DPAUX_DP_AUXCTL
);
186 status
= wait_for_completion_timeout(&dpaux
->complete
, timeout
);
190 /* read status and clear errors */
191 value
= tegra_dpaux_readl(dpaux
, DPAUX_DP_AUXSTAT
);
192 tegra_dpaux_writel(dpaux
, 0xf00, DPAUX_DP_AUXSTAT
);
194 if (value
& DPAUX_DP_AUXSTAT_TIMEOUT_ERROR
)
197 if ((value
& DPAUX_DP_AUXSTAT_RX_ERROR
) ||
198 (value
& DPAUX_DP_AUXSTAT_SINKSTAT_ERROR
) ||
199 (value
& DPAUX_DP_AUXSTAT_NO_STOP_ERROR
))
202 switch ((value
& DPAUX_DP_AUXSTAT_REPLY_TYPE_MASK
) >> 16) {
204 msg
->reply
= DP_AUX_NATIVE_REPLY_ACK
;
208 msg
->reply
= DP_AUX_NATIVE_REPLY_NACK
;
212 msg
->reply
= DP_AUX_NATIVE_REPLY_DEFER
;
216 msg
->reply
= DP_AUX_I2C_REPLY_NACK
;
220 msg
->reply
= DP_AUX_I2C_REPLY_DEFER
;
224 if ((msg
->size
> 0) && (msg
->reply
== DP_AUX_NATIVE_REPLY_ACK
)) {
225 if (msg
->request
& DP_AUX_I2C_READ
) {
226 size_t count
= value
& DPAUX_DP_AUXSTAT_REPLY_MASK
;
228 if (WARN_ON(count
!= msg
->size
))
229 count
= min_t(size_t, count
, msg
->size
);
231 tegra_dpaux_read_fifo(dpaux
, msg
->buffer
, count
);
239 static void tegra_dpaux_hotplug(struct work_struct
*work
)
241 struct tegra_dpaux
*dpaux
= work_to_dpaux(work
);
244 drm_helper_hpd_irq_event(dpaux
->output
->connector
.dev
);
247 static irqreturn_t
tegra_dpaux_irq(int irq
, void *data
)
249 struct tegra_dpaux
*dpaux
= data
;
250 irqreturn_t ret
= IRQ_HANDLED
;
253 /* clear interrupts */
254 value
= tegra_dpaux_readl(dpaux
, DPAUX_INTR_AUX
);
255 tegra_dpaux_writel(dpaux
, value
, DPAUX_INTR_AUX
);
257 if (value
& (DPAUX_INTR_PLUG_EVENT
| DPAUX_INTR_UNPLUG_EVENT
))
258 schedule_work(&dpaux
->work
);
260 if (value
& DPAUX_INTR_IRQ_EVENT
) {
261 /* TODO: handle this */
264 if (value
& DPAUX_INTR_AUX_DONE
)
265 complete(&dpaux
->complete
);
270 static int tegra_dpaux_probe(struct platform_device
*pdev
)
272 struct tegra_dpaux
*dpaux
;
273 struct resource
*regs
;
277 dpaux
= devm_kzalloc(&pdev
->dev
, sizeof(*dpaux
), GFP_KERNEL
);
281 INIT_WORK(&dpaux
->work
, tegra_dpaux_hotplug
);
282 init_completion(&dpaux
->complete
);
283 INIT_LIST_HEAD(&dpaux
->list
);
284 dpaux
->dev
= &pdev
->dev
;
286 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
287 dpaux
->regs
= devm_ioremap_resource(&pdev
->dev
, regs
);
288 if (IS_ERR(dpaux
->regs
))
289 return PTR_ERR(dpaux
->regs
);
291 dpaux
->irq
= platform_get_irq(pdev
, 0);
292 if (dpaux
->irq
< 0) {
293 dev_err(&pdev
->dev
, "failed to get IRQ\n");
297 dpaux
->rst
= devm_reset_control_get(&pdev
->dev
, "dpaux");
298 if (IS_ERR(dpaux
->rst
)) {
299 dev_err(&pdev
->dev
, "failed to get reset control: %ld\n",
300 PTR_ERR(dpaux
->rst
));
301 return PTR_ERR(dpaux
->rst
);
304 dpaux
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
305 if (IS_ERR(dpaux
->clk
)) {
306 dev_err(&pdev
->dev
, "failed to get module clock: %ld\n",
307 PTR_ERR(dpaux
->clk
));
308 return PTR_ERR(dpaux
->clk
);
311 err
= clk_prepare_enable(dpaux
->clk
);
313 dev_err(&pdev
->dev
, "failed to enable module clock: %d\n",
318 reset_control_deassert(dpaux
->rst
);
320 dpaux
->clk_parent
= devm_clk_get(&pdev
->dev
, "parent");
321 if (IS_ERR(dpaux
->clk_parent
)) {
322 dev_err(&pdev
->dev
, "failed to get parent clock: %ld\n",
323 PTR_ERR(dpaux
->clk_parent
));
324 return PTR_ERR(dpaux
->clk_parent
);
327 err
= clk_prepare_enable(dpaux
->clk_parent
);
329 dev_err(&pdev
->dev
, "failed to enable parent clock: %d\n",
334 err
= clk_set_rate(dpaux
->clk_parent
, 270000000);
336 dev_err(&pdev
->dev
, "failed to set clock to 270 MHz: %d\n",
341 dpaux
->vdd
= devm_regulator_get(&pdev
->dev
, "vdd");
342 if (IS_ERR(dpaux
->vdd
)) {
343 dev_err(&pdev
->dev
, "failed to get VDD supply: %ld\n",
344 PTR_ERR(dpaux
->vdd
));
345 return PTR_ERR(dpaux
->vdd
);
348 err
= devm_request_irq(dpaux
->dev
, dpaux
->irq
, tegra_dpaux_irq
, 0,
349 dev_name(dpaux
->dev
), dpaux
);
351 dev_err(dpaux
->dev
, "failed to request IRQ#%u: %d\n",
356 disable_irq(dpaux
->irq
);
358 dpaux
->aux
.transfer
= tegra_dpaux_transfer
;
359 dpaux
->aux
.dev
= &pdev
->dev
;
361 err
= drm_dp_aux_register(&dpaux
->aux
);
366 * Assume that by default the DPAUX/I2C pads will be used for HDMI,
367 * so power them up and configure them in I2C mode.
369 * The DPAUX code paths reconfigure the pads in AUX mode, but there
370 * is no possibility to perform the I2C mode configuration in the
373 value
= tegra_dpaux_readl(dpaux
, DPAUX_HYBRID_SPARE
);
374 value
&= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN
;
375 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_SPARE
);
377 value
= tegra_dpaux_readl(dpaux
, DPAUX_HYBRID_PADCTL
);
378 value
= DPAUX_HYBRID_PADCTL_I2C_SDA_INPUT_RCV
|
379 DPAUX_HYBRID_PADCTL_I2C_SCL_INPUT_RCV
|
380 DPAUX_HYBRID_PADCTL_MODE_I2C
;
381 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_PADCTL
);
383 /* enable and clear all interrupts */
384 value
= DPAUX_INTR_AUX_DONE
| DPAUX_INTR_IRQ_EVENT
|
385 DPAUX_INTR_UNPLUG_EVENT
| DPAUX_INTR_PLUG_EVENT
;
386 tegra_dpaux_writel(dpaux
, value
, DPAUX_INTR_EN_AUX
);
387 tegra_dpaux_writel(dpaux
, value
, DPAUX_INTR_AUX
);
389 mutex_lock(&dpaux_lock
);
390 list_add_tail(&dpaux
->list
, &dpaux_list
);
391 mutex_unlock(&dpaux_lock
);
393 platform_set_drvdata(pdev
, dpaux
);
398 static int tegra_dpaux_remove(struct platform_device
*pdev
)
400 struct tegra_dpaux
*dpaux
= platform_get_drvdata(pdev
);
403 /* make sure pads are powered down when not in use */
404 value
= tegra_dpaux_readl(dpaux
, DPAUX_HYBRID_SPARE
);
405 value
|= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN
;
406 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_SPARE
);
408 drm_dp_aux_unregister(&dpaux
->aux
);
410 mutex_lock(&dpaux_lock
);
411 list_del(&dpaux
->list
);
412 mutex_unlock(&dpaux_lock
);
414 cancel_work_sync(&dpaux
->work
);
416 clk_disable_unprepare(dpaux
->clk_parent
);
417 reset_control_assert(dpaux
->rst
);
418 clk_disable_unprepare(dpaux
->clk
);
423 static const struct of_device_id tegra_dpaux_of_match
[] = {
424 { .compatible
= "nvidia,tegra210-dpaux", },
425 { .compatible
= "nvidia,tegra124-dpaux", },
428 MODULE_DEVICE_TABLE(of
, tegra_dpaux_of_match
);
430 struct platform_driver tegra_dpaux_driver
= {
432 .name
= "tegra-dpaux",
433 .of_match_table
= tegra_dpaux_of_match
,
435 .probe
= tegra_dpaux_probe
,
436 .remove
= tegra_dpaux_remove
,
439 struct tegra_dpaux
*tegra_dpaux_find_by_of_node(struct device_node
*np
)
441 struct tegra_dpaux
*dpaux
;
443 mutex_lock(&dpaux_lock
);
445 list_for_each_entry(dpaux
, &dpaux_list
, list
)
446 if (np
== dpaux
->dev
->of_node
) {
447 mutex_unlock(&dpaux_lock
);
451 mutex_unlock(&dpaux_lock
);
456 int tegra_dpaux_attach(struct tegra_dpaux
*dpaux
, struct tegra_output
*output
)
458 unsigned long timeout
;
461 output
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
462 dpaux
->output
= output
;
464 err
= regulator_enable(dpaux
->vdd
);
468 timeout
= jiffies
+ msecs_to_jiffies(250);
470 while (time_before(jiffies
, timeout
)) {
471 enum drm_connector_status status
;
473 status
= tegra_dpaux_detect(dpaux
);
474 if (status
== connector_status_connected
) {
475 enable_irq(dpaux
->irq
);
479 usleep_range(1000, 2000);
485 int tegra_dpaux_detach(struct tegra_dpaux
*dpaux
)
487 unsigned long timeout
;
490 disable_irq(dpaux
->irq
);
492 err
= regulator_disable(dpaux
->vdd
);
496 timeout
= jiffies
+ msecs_to_jiffies(250);
498 while (time_before(jiffies
, timeout
)) {
499 enum drm_connector_status status
;
501 status
= tegra_dpaux_detect(dpaux
);
502 if (status
== connector_status_disconnected
) {
503 dpaux
->output
= NULL
;
507 usleep_range(1000, 2000);
513 enum drm_connector_status
tegra_dpaux_detect(struct tegra_dpaux
*dpaux
)
517 value
= tegra_dpaux_readl(dpaux
, DPAUX_DP_AUXSTAT
);
519 if (value
& DPAUX_DP_AUXSTAT_HPD_STATUS
)
520 return connector_status_connected
;
522 return connector_status_disconnected
;
525 int tegra_dpaux_enable(struct tegra_dpaux
*dpaux
)
529 value
= DPAUX_HYBRID_PADCTL_AUX_CMH(2) |
530 DPAUX_HYBRID_PADCTL_AUX_DRVZ(4) |
531 DPAUX_HYBRID_PADCTL_AUX_DRVI(0x18) |
532 DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV
|
533 DPAUX_HYBRID_PADCTL_MODE_AUX
;
534 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_PADCTL
);
536 value
= tegra_dpaux_readl(dpaux
, DPAUX_HYBRID_SPARE
);
537 value
&= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN
;
538 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_SPARE
);
543 int tegra_dpaux_disable(struct tegra_dpaux
*dpaux
)
547 value
= tegra_dpaux_readl(dpaux
, DPAUX_HYBRID_SPARE
);
548 value
|= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN
;
549 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_SPARE
);
554 int tegra_dpaux_prepare(struct tegra_dpaux
*dpaux
, u8 encoding
)
558 err
= drm_dp_dpcd_writeb(&dpaux
->aux
, DP_MAIN_LINK_CHANNEL_CODING_SET
,
566 int tegra_dpaux_train(struct tegra_dpaux
*dpaux
, struct drm_dp_link
*link
,
569 u8 tp
= pattern
& DP_TRAINING_PATTERN_MASK
;
570 u8 status
[DP_LINK_STATUS_SIZE
], values
[4];
574 err
= drm_dp_dpcd_writeb(&dpaux
->aux
, DP_TRAINING_PATTERN_SET
, pattern
);
578 if (tp
== DP_TRAINING_PATTERN_DISABLE
)
581 for (i
= 0; i
< link
->num_lanes
; i
++)
582 values
[i
] = DP_TRAIN_MAX_PRE_EMPHASIS_REACHED
|
583 DP_TRAIN_PRE_EMPH_LEVEL_0
|
584 DP_TRAIN_MAX_SWING_REACHED
|
585 DP_TRAIN_VOLTAGE_SWING_LEVEL_0
;
587 err
= drm_dp_dpcd_write(&dpaux
->aux
, DP_TRAINING_LANE0_SET
, values
,
592 usleep_range(500, 1000);
594 err
= drm_dp_dpcd_read_link_status(&dpaux
->aux
, status
);
599 case DP_TRAINING_PATTERN_1
:
600 if (!drm_dp_clock_recovery_ok(status
, link
->num_lanes
))
605 case DP_TRAINING_PATTERN_2
:
606 if (!drm_dp_channel_eq_ok(status
, link
->num_lanes
))
612 dev_err(dpaux
->dev
, "unsupported training pattern %u\n", tp
);
616 err
= drm_dp_dpcd_writeb(&dpaux
->aux
, DP_EDP_CONFIGURATION_SET
, 0);