1/*
2 * Copyright 2014 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Ben Skeggs
23 */
24#include "outp.h"
25#include "priv.h"
26
27#include <subdev/bios.h>
28#include <subdev/bios/conn.h>
29#include <subdev/bios/dcb.h>
30#include <subdev/i2c.h>
31
32int
33_nvkm_output_fini(struct nvkm_object *object, bool suspend)
34{
35	struct nvkm_output *outp = (void *)object;
36	nv_ofuncs(outp->conn)->fini(nv_object(outp->conn), suspend);
37	return nvkm_object_fini(&outp->base, suspend);
38}
39
40int
41_nvkm_output_init(struct nvkm_object *object)
42{
43	struct nvkm_output *outp = (void *)object;
44	int ret = nvkm_object_init(&outp->base);
45	if (ret == 0)
46		nv_ofuncs(outp->conn)->init(nv_object(outp->conn));
47	return 0;
48}
49
50void
51_nvkm_output_dtor(struct nvkm_object *object)
52{
53	struct nvkm_output *outp = (void *)object;
54	list_del(&outp->head);
55	nvkm_object_ref(NULL, (void *)&outp->conn);
56	nvkm_object_destroy(&outp->base);
57}
58
59int
60nvkm_output_create_(struct nvkm_object *parent,
61		    struct nvkm_object *engine,
62		    struct nvkm_oclass *oclass,
63		    struct dcb_output *dcbE, int index,
64		    int length, void **pobject)
65{
66	struct nvkm_disp *disp = nvkm_disp(parent);
67	struct nvkm_bios *bios = nvkm_bios(parent);
68	struct nvkm_i2c *i2c = nvkm_i2c(parent);
69	struct nvbios_connE connE;
70	struct nvkm_output *outp;
71	u8  ver, hdr;
72	u32 data;
73	int ret;
74
75	ret = nvkm_object_create_(parent, engine, oclass, 0, length, pobject);
76	outp = *pobject;
77	if (ret)
78		return ret;
79
80	outp->info = *dcbE;
81	outp->index = index;
82	outp->or = ffs(outp->info.or) - 1;
83
84	DBG("type %02x loc %d or %d link %d con %x edid %x bus %d head %x\n",
85	    dcbE->type, dcbE->location, dcbE->or, dcbE->type >= 2 ?
86	    dcbE->sorconf.link : 0, dcbE->connector, dcbE->i2c_index,
87	    dcbE->bus, dcbE->heads);
88
89	if (outp->info.type != DCB_OUTPUT_DP)
90		outp->port = i2c->find(i2c, NV_I2C_PORT(outp->info.i2c_index));
91	else
92		outp->port = i2c->find(i2c, NV_I2C_AUX(outp->info.i2c_index));
93	outp->edid = outp->port;
94
95	data = nvbios_connEp(bios, outp->info.connector, &ver, &hdr, &connE);
96	if (!data) {
97		DBG("vbios connector data not found\n");
98		memset(&connE, 0x00, sizeof(connE));
99		connE.type = DCB_CONNECTOR_NONE;
100	}
101
102	ret = nvkm_object_ctor(parent, NULL, nvkm_connector_oclass,
103			       &connE, outp->info.connector,
104			       (struct nvkm_object **)&outp->conn);
105	if (ret < 0) {
106		ERR("error %d creating connector, disabling\n", ret);
107		return ret;
108	}
109
110	list_add_tail(&outp->head, &disp->outp);
111	return 0;
112}
113
114int
115_nvkm_output_ctor(struct nvkm_object *parent,
116		  struct nvkm_object *engine,
117		  struct nvkm_oclass *oclass, void *dcbE, u32 index,
118		  struct nvkm_object **pobject)
119{
120	struct nvkm_output *outp;
121	int ret;
122
123	ret = nvkm_output_create(parent, engine, oclass, dcbE, index, &outp);
124	*pobject = nv_object(outp);
125	if (ret)
126		return ret;
127
128	return 0;
129}
130
131struct nvkm_oclass *
132nvkm_output_oclass = &(struct nvkm_output_impl) {
133	.base = {
134		.handle = 0,
135		.ofuncs = &(struct nvkm_ofuncs) {
136			.ctor = _nvkm_output_ctor,
137			.dtor = _nvkm_output_dtor,
138			.init = _nvkm_output_init,
139			.fini = _nvkm_output_fini,
140		},
141	},
142}.base;
143