xref: /freebsd/sys/arm/nvidia/drm2/tegra_drm_subr.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * Copyright (c) 2015 Michal Meloun
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 
36 #include <machine/bus.h>
37 
38 #include <dev/extres/clk/clk.h>
39 #include <dev/drm2/drmP.h>
40 #include <dev/drm2/drm_crtc.h>
41 #include <dev/drm2/drm_crtc_helper.h>
42 #include <dev/drm2/drm_edid.h>
43 #include <dev/drm2/drm_fb_helper.h>
44 #include <dev/gpio/gpiobusvar.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #include <arm/nvidia/drm2/tegra_drm.h>
48 
49 #include <dt-bindings/gpio/gpio.h>
50 
51 int
52 tegra_drm_connector_get_modes(struct drm_connector *connector)
53 {
54 	struct tegra_drm_encoder *output;
55 	struct edid *edid = NULL;
56 	int rv;
57 
58 	output = container_of(connector, struct tegra_drm_encoder,
59 	     connector);
60 
61 	/* Panel is first */
62 	if (output->panel != NULL) {
63 		/* XXX panel parsing */
64 		return (0);
65 	}
66 
67 	/* static EDID is second*/
68 	edid = output->edid;
69 
70 	/* EDID from monitor is last */
71 	if (edid == NULL)
72 		edid = drm_get_edid(connector, output->ddc);
73 
74 	if (edid == NULL)
75 		return (0);
76 
77 	/* Process EDID */
78 	drm_mode_connector_update_edid_property(connector, edid);
79 	rv = drm_add_edid_modes(connector, edid);
80 	drm_edid_to_eld(connector, edid);
81 	return (rv);
82 }
83 
84 struct drm_encoder *
85 tegra_drm_connector_best_encoder(struct drm_connector *connector)
86 {
87 	struct tegra_drm_encoder *output;
88 
89 	output = container_of(connector, struct tegra_drm_encoder,
90 	     connector);
91 
92 	return &(output->encoder);
93 }
94 
95 enum drm_connector_status
96 tegra_drm_connector_detect(struct drm_connector *connector, bool force)
97 {
98 	struct tegra_drm_encoder *output;
99 	bool active;
100 	int rv;
101 
102 	output = container_of(connector, struct tegra_drm_encoder,
103 	     connector);
104 	if (output->gpio_hpd == NULL) {
105 		return ((output->panel != NULL) ?
106 		    connector_status_connected:
107 		    connector_status_disconnected);
108 	}
109 
110 	rv = gpio_pin_is_active(output->gpio_hpd, &active);
111 	if (rv  != 0) {
112 		device_printf(output->dev, " GPIO read failed: %d\n", rv);
113 		return (connector_status_unknown);
114 	}
115 
116 	return (active ?
117 	    connector_status_connected : connector_status_disconnected);
118 }
119 
120 int
121 tegra_drm_encoder_attach(struct tegra_drm_encoder *output, phandle_t node)
122 {
123 	int rv;
124 	phandle_t ddc;
125 
126 	/* XXX parse output panel here */
127 
128 	rv = OF_getencprop_alloc(node, "nvidia,edid",
129 	    (void **)&output->edid);
130 
131 	/* EDID exist but have invalid size */
132 	if ((rv >= 0) && (rv != sizeof(struct edid))) {
133 		device_printf(output->dev,
134 		    "Malformed \"nvidia,edid\" property\n");
135 		if (output->edid != NULL)
136 			free(output->edid, M_OFWPROP);
137 		return (ENXIO);
138 	}
139 
140 	gpio_pin_get_by_ofw_property(output->dev, node, "nvidia,hpd-gpio",
141 	    &output->gpio_hpd);
142 	ddc = 0;
143 	OF_getencprop(node, "nvidia,ddc-i2c-bus", &ddc, sizeof(ddc));
144 	if (ddc > 0)
145 		output->ddc = OF_device_from_xref(ddc);
146 	if ((output->edid == NULL) && (output->ddc == NULL))
147 		return (ENXIO);
148 
149 	if (output->gpio_hpd != NULL) {
150 		output->connector.polled =
151 //		    DRM_CONNECTOR_POLL_HPD;
152 		    DRM_CONNECTOR_POLL_DISCONNECT |
153 		    DRM_CONNECTOR_POLL_CONNECT;
154 	}
155 
156 	return (0);
157 }
158 
159 int tegra_drm_encoder_init(struct tegra_drm_encoder *output,
160     struct tegra_drm *drm)
161 {
162 
163 	if (output->panel) {
164 		/* attach panel */
165 	}
166 	return (0);
167 }
168 
169 int tegra_drm_encoder_exit(struct tegra_drm_encoder *output,
170     struct tegra_drm *drm)
171 {
172 
173 	if (output->panel) {
174 		/* detach panel */
175 	}
176 	return (0);
177 }
178