xref: /freebsd/sys/arm/nvidia/drm2/tegra_drm_subr.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 
34 #include <machine/bus.h>
35 
36 #include <dev/extres/clk/clk.h>
37 #include <dev/drm2/drmP.h>
38 #include <dev/drm2/drm_crtc.h>
39 #include <dev/drm2/drm_crtc_helper.h>
40 #include <dev/drm2/drm_edid.h>
41 #include <dev/drm2/drm_fb_helper.h>
42 #include <dev/gpio/gpiobusvar.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 #include <arm/nvidia/drm2/tegra_drm.h>
46 
47 #include <dt-bindings/gpio/gpio.h>
48 
49 int
50 tegra_drm_connector_get_modes(struct drm_connector *connector)
51 {
52 	struct tegra_drm_encoder *output;
53 	struct edid *edid = NULL;
54 	int rv;
55 
56 	output = container_of(connector, struct tegra_drm_encoder,
57 	     connector);
58 
59 	/* Panel is first */
60 	if (output->panel != NULL) {
61 		/* XXX panel parsing */
62 		return (0);
63 	}
64 
65 	/* static EDID is second*/
66 	edid = output->edid;
67 
68 	/* EDID from monitor is last */
69 	if (edid == NULL)
70 		edid = drm_get_edid(connector, output->ddc);
71 
72 	if (edid == NULL)
73 		return (0);
74 
75 	/* Process EDID */
76 	drm_mode_connector_update_edid_property(connector, edid);
77 	rv = drm_add_edid_modes(connector, edid);
78 	drm_edid_to_eld(connector, edid);
79 	return (rv);
80 }
81 
82 struct drm_encoder *
83 tegra_drm_connector_best_encoder(struct drm_connector *connector)
84 {
85 	struct tegra_drm_encoder *output;
86 
87 	output = container_of(connector, struct tegra_drm_encoder,
88 	     connector);
89 
90 	return &(output->encoder);
91 }
92 
93 enum drm_connector_status
94 tegra_drm_connector_detect(struct drm_connector *connector, bool force)
95 {
96 	struct tegra_drm_encoder *output;
97 	bool active;
98 	int rv;
99 
100 	output = container_of(connector, struct tegra_drm_encoder,
101 	     connector);
102 	if (output->gpio_hpd == NULL) {
103 		return ((output->panel != NULL) ?
104 		    connector_status_connected:
105 		    connector_status_disconnected);
106 	}
107 
108 	rv = gpio_pin_is_active(output->gpio_hpd, &active);
109 	if (rv  != 0) {
110 		device_printf(output->dev, " GPIO read failed: %d\n", rv);
111 		return (connector_status_unknown);
112 	}
113 
114 	return (active ?
115 	    connector_status_connected : connector_status_disconnected);
116 }
117 
118 int
119 tegra_drm_encoder_attach(struct tegra_drm_encoder *output, phandle_t node)
120 {
121 	int rv;
122 	phandle_t ddc;
123 
124 	/* XXX parse output panel here */
125 
126 	rv = OF_getencprop_alloc(node, "nvidia,edid",
127 	    (void **)&output->edid);
128 
129 	/* EDID exist but have invalid size */
130 	if ((rv >= 0) && (rv != sizeof(struct edid))) {
131 		device_printf(output->dev,
132 		    "Malformed \"nvidia,edid\" property\n");
133 		if (output->edid != NULL)
134 			free(output->edid, M_OFWPROP);
135 		return (ENXIO);
136 	}
137 
138 	gpio_pin_get_by_ofw_property(output->dev, node, "nvidia,hpd-gpio",
139 	    &output->gpio_hpd);
140 	ddc = 0;
141 	OF_getencprop(node, "nvidia,ddc-i2c-bus", &ddc, sizeof(ddc));
142 	if (ddc > 0)
143 		output->ddc = OF_device_from_xref(ddc);
144 	if ((output->edid == NULL) && (output->ddc == NULL))
145 		return (ENXIO);
146 
147 	if (output->gpio_hpd != NULL) {
148 		output->connector.polled =
149 //		    DRM_CONNECTOR_POLL_HPD;
150 		    DRM_CONNECTOR_POLL_DISCONNECT |
151 		    DRM_CONNECTOR_POLL_CONNECT;
152 	}
153 
154 	return (0);
155 }
156 
157 int tegra_drm_encoder_init(struct tegra_drm_encoder *output,
158     struct tegra_drm *drm)
159 {
160 
161 	if (output->panel) {
162 		/* attach panel */
163 	}
164 	return (0);
165 }
166 
167 int tegra_drm_encoder_exit(struct tegra_drm_encoder *output,
168     struct tegra_drm *drm)
169 {
170 
171 	if (output->panel) {
172 		/* detach panel */
173 	}
174 	return (0);
175 }
176