2 * Copyright 2014 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
29 #include <subdev/i2c.h>
31 #include <nvif/event.h>
34 nvkm_output_dp_train(struct nvkm_output *base, u32 datarate, bool wait)
36 struct nvkm_output_dp *outp = nvkm_output_dp(base);
42 /* check that the link is trained at a high enough rate */
43 ret = nvkm_rdaux(outp->aux, DPCD_LC00_LINK_BW_SET, link, 2);
46 "failed to read link config, assuming no sink");
50 linkrate = link[0] * 27000 * (link[1] & DPCD_LC01_LANE_COUNT_SET);
51 linkrate = (linkrate * 8) / 10; /* 8B/10B coding overhead */
52 datarate = (datarate + 9) / 10; /* -> decakilobits */
53 if (linkrate < datarate) {
54 OUTP_DBG(&outp->base, "link not trained at sufficient rate");
58 /* check that link is still trained */
59 ret = nvkm_rdaux(outp->aux, DPCD_LS02, stat, 3);
62 "failed to read link status, assuming no sink");
66 if (stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE) {
67 for (i = 0; i < (link[1] & DPCD_LC01_LANE_COUNT_SET); i++) {
68 u8 lane = (stat[i >> 1] >> ((i & 1) * 4)) & 0x0f;
69 if (!(lane & DPCD_LS02_LANE0_CR_DONE) ||
70 !(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
71 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED)) {
73 "lane %d not equalised", lane);
79 OUTP_DBG(&outp->base, "no inter-lane alignment");
83 if (retrain || !atomic_read(&outp->lt.done)) {
84 /* no sink, but still need to configure source */
85 if (outp->dpcd[DPCD_RC00_DPCD_REV] == 0x00) {
86 outp->dpcd[DPCD_RC01_MAX_LINK_RATE] =
87 outp->base.info.dpconf.link_bw;
88 outp->dpcd[DPCD_RC02] =
89 outp->base.info.dpconf.link_nr;
91 atomic_set(&outp->lt.done, 0);
92 schedule_work(&outp->lt.work);
94 nvkm_notify_get(&outp->irq);
98 if (!wait_event_timeout(outp->lt.wait,
99 atomic_read(&outp->lt.done),
100 msecs_to_jiffies(2000)))
108 nvkm_output_dp_enable(struct nvkm_output_dp *outp, bool enable)
110 struct nvkm_i2c_aux *aux = outp->aux;
113 if (!outp->present) {
114 OUTP_DBG(&outp->base, "aux power -> always");
115 nvkm_i2c_aux_monitor(aux, true);
116 outp->present = true;
119 if (!nvkm_rdaux(aux, DPCD_RC00_DPCD_REV, outp->dpcd,
120 sizeof(outp->dpcd))) {
121 nvkm_output_dp_train(&outp->base, 0, true);
127 OUTP_DBG(&outp->base, "aux power -> demand");
128 nvkm_i2c_aux_monitor(aux, false);
129 outp->present = false;
132 atomic_set(&outp->lt.done, 0);
136 nvkm_output_dp_hpd(struct nvkm_notify *notify)
138 const struct nvkm_i2c_ntfy_rep *line = notify->data;
139 struct nvkm_output_dp *outp = container_of(notify, typeof(*outp), hpd);
140 struct nvkm_connector *conn = outp->base.conn;
141 struct nvkm_disp *disp = outp->base.disp;
142 struct nvif_notify_conn_rep_v0 rep = {};
144 OUTP_DBG(&outp->base, "HPD: %d", line->mask);
145 nvkm_output_dp_enable(outp, true);
147 if (line->mask & NVKM_I2C_UNPLUG)
148 rep.mask |= NVIF_NOTIFY_CONN_V0_UNPLUG;
149 if (line->mask & NVKM_I2C_PLUG)
150 rep.mask |= NVIF_NOTIFY_CONN_V0_PLUG;
152 nvkm_event_send(&disp->hpd, rep.mask, conn->index, &rep, sizeof(rep));
153 return NVKM_NOTIFY_KEEP;
157 nvkm_output_dp_irq(struct nvkm_notify *notify)
159 const struct nvkm_i2c_ntfy_rep *line = notify->data;
160 struct nvkm_output_dp *outp = container_of(notify, typeof(*outp), irq);
161 struct nvkm_connector *conn = outp->base.conn;
162 struct nvkm_disp *disp = outp->base.disp;
163 struct nvif_notify_conn_rep_v0 rep = {
164 .mask = NVIF_NOTIFY_CONN_V0_IRQ,
167 OUTP_DBG(&outp->base, "IRQ: %d", line->mask);
168 nvkm_output_dp_train(&outp->base, 0, true);
170 nvkm_event_send(&disp->hpd, rep.mask, conn->index, &rep, sizeof(rep));
171 return NVKM_NOTIFY_DROP;
175 nvkm_output_dp_fini(struct nvkm_output *base)
177 struct nvkm_output_dp *outp = nvkm_output_dp(base);
178 nvkm_notify_put(&outp->hpd);
179 nvkm_notify_put(&outp->irq);
180 flush_work(&outp->lt.work);
181 nvkm_output_dp_enable(outp, false);
185 nvkm_output_dp_init(struct nvkm_output *base)
187 struct nvkm_output_dp *outp = nvkm_output_dp(base);
188 nvkm_notify_put(&outp->base.conn->hpd);
189 nvkm_output_dp_enable(outp, true);
190 nvkm_notify_get(&outp->hpd);
194 nvkm_output_dp_dtor(struct nvkm_output *base)
196 struct nvkm_output_dp *outp = nvkm_output_dp(base);
197 nvkm_notify_fini(&outp->hpd);
198 nvkm_notify_fini(&outp->irq);
202 static const struct nvkm_output_func
203 nvkm_output_dp_func = {
204 .dtor = nvkm_output_dp_dtor,
205 .init = nvkm_output_dp_init,
206 .fini = nvkm_output_dp_fini,
210 nvkm_output_dp_ctor(const struct nvkm_output_dp_func *func,
211 struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
212 struct nvkm_i2c_aux *aux, struct nvkm_output_dp *outp)
214 struct nvkm_device *device = disp->engine.subdev.device;
215 struct nvkm_bios *bios = device->bios;
216 struct nvkm_i2c *i2c = device->i2c;
221 nvkm_output_ctor(&nvkm_output_dp_func, disp, index, dcbE, &outp->base);
225 OUTP_ERR(&outp->base, "no aux");
229 /* bios data is not optional */
230 data = nvbios_dpout_match(bios, outp->base.info.hasht,
231 outp->base.info.hashm, &outp->version,
232 &hdr, &cnt, &len, &outp->info);
234 OUTP_ERR(&outp->base, "no bios dp data");
238 OUTP_DBG(&outp->base, "bios dp %02x %02x %02x %02x",
239 outp->version, hdr, cnt, len);
242 INIT_WORK(&outp->lt.work, nvkm_dp_train);
243 init_waitqueue_head(&outp->lt.wait);
244 atomic_set(&outp->lt.done, 0);
246 /* link maintenance */
247 ret = nvkm_notify_init(NULL, &i2c->event, nvkm_output_dp_irq, true,
248 &(struct nvkm_i2c_ntfy_req) {
249 .mask = NVKM_I2C_IRQ,
250 .port = outp->aux->id,
252 sizeof(struct nvkm_i2c_ntfy_req),
253 sizeof(struct nvkm_i2c_ntfy_rep),
256 OUTP_ERR(&outp->base, "error monitoring aux irq: %d", ret);
260 /* hotplug detect, replaces gpio-based mechanism with aux events */
261 ret = nvkm_notify_init(NULL, &i2c->event, nvkm_output_dp_hpd, true,
262 &(struct nvkm_i2c_ntfy_req) {
263 .mask = NVKM_I2C_PLUG | NVKM_I2C_UNPLUG,
264 .port = outp->aux->id,
266 sizeof(struct nvkm_i2c_ntfy_req),
267 sizeof(struct nvkm_i2c_ntfy_rep),
270 OUTP_ERR(&outp->base, "error monitoring aux hpd: %d", ret);
278 nvkm_output_dp_new_(const struct nvkm_output_dp_func *func,
279 struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
280 struct nvkm_output **poutp)
282 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
283 struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, dcbE->i2c_index);
284 struct nvkm_output_dp *outp;
286 if (!(outp = kzalloc(sizeof(*outp), GFP_KERNEL)))
288 *poutp = &outp->base;
290 return nvkm_output_dp_ctor(func, disp, index, dcbE, aux, outp);