]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/tegra/dp.c
c19060b8753a939a14be09abcea54b82d0ac1ed9
[linux.git] / drivers / gpu / drm / tegra / dp.c
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright (C) 2013-2019 NVIDIA Corporation
4  * Copyright (C) 2015 Rob Clark
5  */
6
7 #include <drm/drm_dp_helper.h>
8
9 #include "dp.h"
10
11 static void drm_dp_link_reset(struct drm_dp_link *link)
12 {
13         if (!link)
14                 return;
15
16         link->revision = 0;
17         link->rate = 0;
18         link->num_lanes = 0;
19         link->capabilities = 0;
20 }
21
22 /**
23  * drm_dp_link_probe() - probe a DisplayPort link for capabilities
24  * @aux: DisplayPort AUX channel
25  * @link: pointer to structure in which to return link capabilities
26  *
27  * The structure filled in by this function can usually be passed directly
28  * into drm_dp_link_power_up() and drm_dp_link_configure() to power up and
29  * configure the link based on the link's capabilities.
30  *
31  * Returns 0 on success or a negative error code on failure.
32  */
33 int drm_dp_link_probe(struct drm_dp_aux *aux, struct drm_dp_link *link)
34 {
35         u8 values[3];
36         int err;
37
38         drm_dp_link_reset(link);
39
40         err = drm_dp_dpcd_read(aux, DP_DPCD_REV, values, sizeof(values));
41         if (err < 0)
42                 return err;
43
44         link->revision = values[0];
45         link->rate = drm_dp_bw_code_to_link_rate(values[1]);
46         link->num_lanes = values[2] & DP_MAX_LANE_COUNT_MASK;
47
48         if (values[2] & DP_ENHANCED_FRAME_CAP)
49                 link->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
50
51         return 0;
52 }
53
54 /**
55  * drm_dp_link_power_up() - power up a DisplayPort link
56  * @aux: DisplayPort AUX channel
57  * @link: pointer to a structure containing the link configuration
58  *
59  * Returns 0 on success or a negative error code on failure.
60  */
61 int drm_dp_link_power_up(struct drm_dp_aux *aux, struct drm_dp_link *link)
62 {
63         u8 value;
64         int err;
65
66         /* DP_SET_POWER register is only available on DPCD v1.1 and later */
67         if (link->revision < 0x11)
68                 return 0;
69
70         err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
71         if (err < 0)
72                 return err;
73
74         value &= ~DP_SET_POWER_MASK;
75         value |= DP_SET_POWER_D0;
76
77         err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
78         if (err < 0)
79                 return err;
80
81         /*
82          * According to the DP 1.1 specification, a "Sink Device must exit the
83          * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
84          * Control Field" (register 0x600).
85          */
86         usleep_range(1000, 2000);
87
88         return 0;
89 }
90
91 /**
92  * drm_dp_link_power_down() - power down a DisplayPort link
93  * @aux: DisplayPort AUX channel
94  * @link: pointer to a structure containing the link configuration
95  *
96  * Returns 0 on success or a negative error code on failure.
97  */
98 int drm_dp_link_power_down(struct drm_dp_aux *aux, struct drm_dp_link *link)
99 {
100         u8 value;
101         int err;
102
103         /* DP_SET_POWER register is only available on DPCD v1.1 and later */
104         if (link->revision < 0x11)
105                 return 0;
106
107         err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
108         if (err < 0)
109                 return err;
110
111         value &= ~DP_SET_POWER_MASK;
112         value |= DP_SET_POWER_D3;
113
114         err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
115         if (err < 0)
116                 return err;
117
118         return 0;
119 }
120
121 /**
122  * drm_dp_link_configure() - configure a DisplayPort link
123  * @aux: DisplayPort AUX channel
124  * @link: pointer to a structure containing the link configuration
125  *
126  * Returns 0 on success or a negative error code on failure.
127  */
128 int drm_dp_link_configure(struct drm_dp_aux *aux, struct drm_dp_link *link)
129 {
130         u8 values[2];
131         int err;
132
133         values[0] = drm_dp_link_rate_to_bw_code(link->rate);
134         values[1] = link->num_lanes;
135
136         if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
137                 values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
138
139         err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
140         if (err < 0)
141                 return err;
142
143         return 0;
144 }