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
:
123 case DP_AUX_I2C_READ
:
124 value
= DPAUX_DP_AUXCTL_CMD_ADDRESS_ONLY
;
131 /* For non-zero-sized messages, set the CMDLEN field. */
132 value
= DPAUX_DP_AUXCTL_CMDLEN(msg
->size
- 1);
135 switch (msg
->request
& ~DP_AUX_I2C_MOT
) {
136 case DP_AUX_I2C_WRITE
:
137 if (msg
->request
& DP_AUX_I2C_MOT
)
138 value
|= DPAUX_DP_AUXCTL_CMD_MOT_WR
;
140 value
|= DPAUX_DP_AUXCTL_CMD_I2C_WR
;
144 case DP_AUX_I2C_READ
:
145 if (msg
->request
& DP_AUX_I2C_MOT
)
146 value
|= DPAUX_DP_AUXCTL_CMD_MOT_RD
;
148 value
|= DPAUX_DP_AUXCTL_CMD_I2C_RD
;
152 case DP_AUX_I2C_STATUS
:
153 if (msg
->request
& DP_AUX_I2C_MOT
)
154 value
|= DPAUX_DP_AUXCTL_CMD_MOT_RQ
;
156 value
|= DPAUX_DP_AUXCTL_CMD_I2C_RQ
;
160 case DP_AUX_NATIVE_WRITE
:
161 value
|= DPAUX_DP_AUXCTL_CMD_AUX_WR
;
164 case DP_AUX_NATIVE_READ
:
165 value
|= DPAUX_DP_AUXCTL_CMD_AUX_RD
;
172 tegra_dpaux_writel(dpaux
, msg
->address
, DPAUX_DP_AUXADDR
);
173 tegra_dpaux_writel(dpaux
, value
, DPAUX_DP_AUXCTL
);
175 if ((msg
->request
& DP_AUX_I2C_READ
) == 0) {
176 tegra_dpaux_write_fifo(dpaux
, msg
->buffer
, msg
->size
);
180 /* start transaction */
181 value
= tegra_dpaux_readl(dpaux
, DPAUX_DP_AUXCTL
);
182 value
|= DPAUX_DP_AUXCTL_TRANSACTREQ
;
183 tegra_dpaux_writel(dpaux
, value
, DPAUX_DP_AUXCTL
);
185 status
= wait_for_completion_timeout(&dpaux
->complete
, timeout
);
189 /* read status and clear errors */
190 value
= tegra_dpaux_readl(dpaux
, DPAUX_DP_AUXSTAT
);
191 tegra_dpaux_writel(dpaux
, 0xf00, DPAUX_DP_AUXSTAT
);
193 if (value
& DPAUX_DP_AUXSTAT_TIMEOUT_ERROR
)
196 if ((value
& DPAUX_DP_AUXSTAT_RX_ERROR
) ||
197 (value
& DPAUX_DP_AUXSTAT_SINKSTAT_ERROR
) ||
198 (value
& DPAUX_DP_AUXSTAT_NO_STOP_ERROR
))
201 switch ((value
& DPAUX_DP_AUXSTAT_REPLY_TYPE_MASK
) >> 16) {
203 msg
->reply
= DP_AUX_NATIVE_REPLY_ACK
;
207 msg
->reply
= DP_AUX_NATIVE_REPLY_NACK
;
211 msg
->reply
= DP_AUX_NATIVE_REPLY_DEFER
;
215 msg
->reply
= DP_AUX_I2C_REPLY_NACK
;
219 msg
->reply
= DP_AUX_I2C_REPLY_DEFER
;
223 if ((msg
->size
> 0) && (msg
->reply
== DP_AUX_NATIVE_REPLY_ACK
)) {
224 if (msg
->request
& DP_AUX_I2C_READ
) {
225 size_t count
= value
& DPAUX_DP_AUXSTAT_REPLY_MASK
;
227 if (WARN_ON(count
!= msg
->size
))
228 count
= min_t(size_t, count
, msg
->size
);
230 tegra_dpaux_read_fifo(dpaux
, msg
->buffer
, count
);
238 static void tegra_dpaux_hotplug(struct work_struct
*work
)
240 struct tegra_dpaux
*dpaux
= work_to_dpaux(work
);
243 drm_helper_hpd_irq_event(dpaux
->output
->connector
.dev
);
246 static irqreturn_t
tegra_dpaux_irq(int irq
, void *data
)
248 struct tegra_dpaux
*dpaux
= data
;
249 irqreturn_t ret
= IRQ_HANDLED
;
252 /* clear interrupts */
253 value
= tegra_dpaux_readl(dpaux
, DPAUX_INTR_AUX
);
254 tegra_dpaux_writel(dpaux
, value
, DPAUX_INTR_AUX
);
256 if (value
& (DPAUX_INTR_PLUG_EVENT
| DPAUX_INTR_UNPLUG_EVENT
))
257 schedule_work(&dpaux
->work
);
259 if (value
& DPAUX_INTR_IRQ_EVENT
) {
260 /* TODO: handle this */
263 if (value
& DPAUX_INTR_AUX_DONE
)
264 complete(&dpaux
->complete
);
269 static int tegra_dpaux_probe(struct platform_device
*pdev
)
271 struct tegra_dpaux
*dpaux
;
272 struct resource
*regs
;
276 dpaux
= devm_kzalloc(&pdev
->dev
, sizeof(*dpaux
), GFP_KERNEL
);
280 INIT_WORK(&dpaux
->work
, tegra_dpaux_hotplug
);
281 init_completion(&dpaux
->complete
);
282 INIT_LIST_HEAD(&dpaux
->list
);
283 dpaux
->dev
= &pdev
->dev
;
285 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
286 dpaux
->regs
= devm_ioremap_resource(&pdev
->dev
, regs
);
287 if (IS_ERR(dpaux
->regs
))
288 return PTR_ERR(dpaux
->regs
);
290 dpaux
->irq
= platform_get_irq(pdev
, 0);
291 if (dpaux
->irq
< 0) {
292 dev_err(&pdev
->dev
, "failed to get IRQ\n");
296 dpaux
->rst
= devm_reset_control_get(&pdev
->dev
, "dpaux");
297 if (IS_ERR(dpaux
->rst
))
298 return PTR_ERR(dpaux
->rst
);
300 dpaux
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
301 if (IS_ERR(dpaux
->clk
))
302 return PTR_ERR(dpaux
->clk
);
304 err
= clk_prepare_enable(dpaux
->clk
);
308 reset_control_deassert(dpaux
->rst
);
310 dpaux
->clk_parent
= devm_clk_get(&pdev
->dev
, "parent");
311 if (IS_ERR(dpaux
->clk_parent
))
312 return PTR_ERR(dpaux
->clk_parent
);
314 err
= clk_prepare_enable(dpaux
->clk_parent
);
318 err
= clk_set_rate(dpaux
->clk_parent
, 270000000);
320 dev_err(&pdev
->dev
, "failed to set clock to 270 MHz: %d\n",
325 dpaux
->vdd
= devm_regulator_get(&pdev
->dev
, "vdd");
326 if (IS_ERR(dpaux
->vdd
))
327 return PTR_ERR(dpaux
->vdd
);
329 err
= devm_request_irq(dpaux
->dev
, dpaux
->irq
, tegra_dpaux_irq
, 0,
330 dev_name(dpaux
->dev
), dpaux
);
332 dev_err(dpaux
->dev
, "failed to request IRQ#%u: %d\n",
337 dpaux
->aux
.transfer
= tegra_dpaux_transfer
;
338 dpaux
->aux
.dev
= &pdev
->dev
;
340 err
= drm_dp_aux_register(&dpaux
->aux
);
344 /* enable and clear all interrupts */
345 value
= DPAUX_INTR_AUX_DONE
| DPAUX_INTR_IRQ_EVENT
|
346 DPAUX_INTR_UNPLUG_EVENT
| DPAUX_INTR_PLUG_EVENT
;
347 tegra_dpaux_writel(dpaux
, value
, DPAUX_INTR_EN_AUX
);
348 tegra_dpaux_writel(dpaux
, value
, DPAUX_INTR_AUX
);
350 mutex_lock(&dpaux_lock
);
351 list_add_tail(&dpaux
->list
, &dpaux_list
);
352 mutex_unlock(&dpaux_lock
);
354 platform_set_drvdata(pdev
, dpaux
);
359 static int tegra_dpaux_remove(struct platform_device
*pdev
)
361 struct tegra_dpaux
*dpaux
= platform_get_drvdata(pdev
);
363 drm_dp_aux_unregister(&dpaux
->aux
);
365 mutex_lock(&dpaux_lock
);
366 list_del(&dpaux
->list
);
367 mutex_unlock(&dpaux_lock
);
369 cancel_work_sync(&dpaux
->work
);
371 clk_disable_unprepare(dpaux
->clk_parent
);
372 reset_control_assert(dpaux
->rst
);
373 clk_disable_unprepare(dpaux
->clk
);
378 static const struct of_device_id tegra_dpaux_of_match
[] = {
379 { .compatible
= "nvidia,tegra124-dpaux", },
382 MODULE_DEVICE_TABLE(of
, tegra_dpaux_of_match
);
384 struct platform_driver tegra_dpaux_driver
= {
386 .name
= "tegra-dpaux",
387 .of_match_table
= tegra_dpaux_of_match
,
389 .probe
= tegra_dpaux_probe
,
390 .remove
= tegra_dpaux_remove
,
393 struct tegra_dpaux
*tegra_dpaux_find_by_of_node(struct device_node
*np
)
395 struct tegra_dpaux
*dpaux
;
397 mutex_lock(&dpaux_lock
);
399 list_for_each_entry(dpaux
, &dpaux_list
, list
)
400 if (np
== dpaux
->dev
->of_node
) {
401 mutex_unlock(&dpaux_lock
);
405 mutex_unlock(&dpaux_lock
);
410 int tegra_dpaux_attach(struct tegra_dpaux
*dpaux
, struct tegra_output
*output
)
412 unsigned long timeout
;
415 output
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
416 dpaux
->output
= output
;
418 err
= regulator_enable(dpaux
->vdd
);
422 timeout
= jiffies
+ msecs_to_jiffies(250);
424 while (time_before(jiffies
, timeout
)) {
425 enum drm_connector_status status
;
427 status
= tegra_dpaux_detect(dpaux
);
428 if (status
== connector_status_connected
)
431 usleep_range(1000, 2000);
437 int tegra_dpaux_detach(struct tegra_dpaux
*dpaux
)
439 unsigned long timeout
;
442 err
= regulator_disable(dpaux
->vdd
);
446 timeout
= jiffies
+ msecs_to_jiffies(250);
448 while (time_before(jiffies
, timeout
)) {
449 enum drm_connector_status status
;
451 status
= tegra_dpaux_detect(dpaux
);
452 if (status
== connector_status_disconnected
) {
453 dpaux
->output
= NULL
;
457 usleep_range(1000, 2000);
463 enum drm_connector_status
tegra_dpaux_detect(struct tegra_dpaux
*dpaux
)
467 value
= tegra_dpaux_readl(dpaux
, DPAUX_DP_AUXSTAT
);
469 if (value
& DPAUX_DP_AUXSTAT_HPD_STATUS
)
470 return connector_status_connected
;
472 return connector_status_disconnected
;
475 int tegra_dpaux_enable(struct tegra_dpaux
*dpaux
)
479 value
= DPAUX_HYBRID_PADCTL_AUX_CMH(2) |
480 DPAUX_HYBRID_PADCTL_AUX_DRVZ(4) |
481 DPAUX_HYBRID_PADCTL_AUX_DRVI(0x18) |
482 DPAUX_HYBRID_PADCTL_AUX_INPUT_RCV
|
483 DPAUX_HYBRID_PADCTL_MODE_AUX
;
484 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_PADCTL
);
486 value
= tegra_dpaux_readl(dpaux
, DPAUX_HYBRID_SPARE
);
487 value
&= ~DPAUX_HYBRID_SPARE_PAD_POWER_DOWN
;
488 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_SPARE
);
493 int tegra_dpaux_disable(struct tegra_dpaux
*dpaux
)
497 value
= tegra_dpaux_readl(dpaux
, DPAUX_HYBRID_SPARE
);
498 value
|= DPAUX_HYBRID_SPARE_PAD_POWER_DOWN
;
499 tegra_dpaux_writel(dpaux
, value
, DPAUX_HYBRID_SPARE
);
504 int tegra_dpaux_prepare(struct tegra_dpaux
*dpaux
, u8 encoding
)
508 err
= drm_dp_dpcd_writeb(&dpaux
->aux
, DP_MAIN_LINK_CHANNEL_CODING_SET
,
516 int tegra_dpaux_train(struct tegra_dpaux
*dpaux
, struct drm_dp_link
*link
,
519 u8 tp
= pattern
& DP_TRAINING_PATTERN_MASK
;
520 u8 status
[DP_LINK_STATUS_SIZE
], values
[4];
524 err
= drm_dp_dpcd_writeb(&dpaux
->aux
, DP_TRAINING_PATTERN_SET
, pattern
);
528 if (tp
== DP_TRAINING_PATTERN_DISABLE
)
531 for (i
= 0; i
< link
->num_lanes
; i
++)
532 values
[i
] = DP_TRAIN_MAX_PRE_EMPHASIS_REACHED
|
533 DP_TRAIN_PRE_EMPH_LEVEL_0
|
534 DP_TRAIN_MAX_SWING_REACHED
|
535 DP_TRAIN_VOLTAGE_SWING_LEVEL_0
;
537 err
= drm_dp_dpcd_write(&dpaux
->aux
, DP_TRAINING_LANE0_SET
, values
,
542 usleep_range(500, 1000);
544 err
= drm_dp_dpcd_read_link_status(&dpaux
->aux
, status
);
549 case DP_TRAINING_PATTERN_1
:
550 if (!drm_dp_clock_recovery_ok(status
, link
->num_lanes
))
555 case DP_TRAINING_PATTERN_2
:
556 if (!drm_dp_channel_eq_ok(status
, link
->num_lanes
))
562 dev_err(dpaux
->dev
, "unsupported training pattern %u\n", tp
);
566 err
= drm_dp_dpcd_writeb(&dpaux
->aux
, DP_EDP_CONFIGURATION_SET
, 0);