xref: /openbmc/linux/drivers/gpu/drm/tegra/output.c (revision be122522)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Avionic Design GmbH
4  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
5  */
6 
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_panel.h>
9 
10 #include "drm.h"
11 #include "dc.h"
12 
13 #include <media/cec-notifier.h>
14 
15 int tegra_output_connector_get_modes(struct drm_connector *connector)
16 {
17 	struct tegra_output *output = connector_to_output(connector);
18 	struct edid *edid = NULL;
19 	int err = 0;
20 
21 	/*
22 	 * If the panel provides one or more modes, use them exclusively and
23 	 * ignore any other means of obtaining a mode.
24 	 */
25 	if (output->panel) {
26 		err = output->panel->funcs->get_modes(output->panel);
27 		if (err > 0)
28 			return err;
29 	}
30 
31 	if (output->edid)
32 		edid = kmemdup(output->edid, sizeof(*edid), GFP_KERNEL);
33 	else if (output->ddc)
34 		edid = drm_get_edid(connector, output->ddc);
35 
36 	cec_notifier_set_phys_addr_from_edid(output->cec, edid);
37 	drm_connector_update_edid_property(connector, edid);
38 
39 	if (edid) {
40 		err = drm_add_edid_modes(connector, edid);
41 		kfree(edid);
42 	}
43 
44 	return err;
45 }
46 
47 enum drm_connector_status
48 tegra_output_connector_detect(struct drm_connector *connector, bool force)
49 {
50 	struct tegra_output *output = connector_to_output(connector);
51 	enum drm_connector_status status = connector_status_unknown;
52 
53 	if (output->hpd_gpio) {
54 		if (gpiod_get_value(output->hpd_gpio) == 0)
55 			status = connector_status_disconnected;
56 		else
57 			status = connector_status_connected;
58 	} else {
59 		if (!output->panel)
60 			status = connector_status_disconnected;
61 		else
62 			status = connector_status_connected;
63 	}
64 
65 	if (status != connector_status_connected)
66 		cec_notifier_phys_addr_invalidate(output->cec);
67 
68 	return status;
69 }
70 
71 void tegra_output_connector_destroy(struct drm_connector *connector)
72 {
73 	struct tegra_output *output = connector_to_output(connector);
74 
75 	if (output->cec)
76 		cec_notifier_conn_unregister(output->cec);
77 
78 	drm_connector_unregister(connector);
79 	drm_connector_cleanup(connector);
80 }
81 
82 void tegra_output_encoder_destroy(struct drm_encoder *encoder)
83 {
84 	drm_encoder_cleanup(encoder);
85 }
86 
87 static irqreturn_t hpd_irq(int irq, void *data)
88 {
89 	struct tegra_output *output = data;
90 
91 	if (output->connector.dev)
92 		drm_helper_hpd_irq_event(output->connector.dev);
93 
94 	return IRQ_HANDLED;
95 }
96 
97 int tegra_output_probe(struct tegra_output *output)
98 {
99 	struct device_node *ddc, *panel;
100 	unsigned long flags;
101 	int err, size;
102 
103 	if (!output->of_node)
104 		output->of_node = output->dev->of_node;
105 
106 	panel = of_parse_phandle(output->of_node, "nvidia,panel", 0);
107 	if (panel) {
108 		output->panel = of_drm_find_panel(panel);
109 		if (IS_ERR(output->panel))
110 			return PTR_ERR(output->panel);
111 
112 		of_node_put(panel);
113 	}
114 
115 	output->edid = of_get_property(output->of_node, "nvidia,edid", &size);
116 
117 	ddc = of_parse_phandle(output->of_node, "nvidia,ddc-i2c-bus", 0);
118 	if (ddc) {
119 		output->ddc = of_find_i2c_adapter_by_node(ddc);
120 		if (!output->ddc) {
121 			err = -EPROBE_DEFER;
122 			of_node_put(ddc);
123 			return err;
124 		}
125 
126 		of_node_put(ddc);
127 	}
128 
129 	output->hpd_gpio = devm_gpiod_get_from_of_node(output->dev,
130 						       output->of_node,
131 						       "nvidia,hpd-gpio", 0,
132 						       GPIOD_IN,
133 						       "HDMI hotplug detect");
134 	if (IS_ERR(output->hpd_gpio)) {
135 		if (PTR_ERR(output->hpd_gpio) != -ENOENT)
136 			return PTR_ERR(output->hpd_gpio);
137 
138 		output->hpd_gpio = NULL;
139 	}
140 
141 	if (output->hpd_gpio) {
142 		err = gpiod_to_irq(output->hpd_gpio);
143 		if (err < 0) {
144 			dev_err(output->dev, "gpiod_to_irq(): %d\n", err);
145 			return err;
146 		}
147 
148 		output->hpd_irq = err;
149 
150 		flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
151 			IRQF_ONESHOT;
152 
153 		err = request_threaded_irq(output->hpd_irq, NULL, hpd_irq,
154 					   flags, "hpd", output);
155 		if (err < 0) {
156 			dev_err(output->dev, "failed to request IRQ#%u: %d\n",
157 				output->hpd_irq, err);
158 			return err;
159 		}
160 
161 		output->connector.polled = DRM_CONNECTOR_POLL_HPD;
162 
163 		/*
164 		 * Disable the interrupt until the connector has been
165 		 * initialized to avoid a race in the hotplug interrupt
166 		 * handler.
167 		 */
168 		disable_irq(output->hpd_irq);
169 	}
170 
171 	return 0;
172 }
173 
174 void tegra_output_remove(struct tegra_output *output)
175 {
176 	if (output->hpd_gpio)
177 		free_irq(output->hpd_irq, output);
178 
179 	if (output->ddc)
180 		put_device(&output->ddc->dev);
181 }
182 
183 int tegra_output_init(struct drm_device *drm, struct tegra_output *output)
184 {
185 	int connector_type;
186 	int err;
187 
188 	if (output->panel) {
189 		err = drm_panel_attach(output->panel, &output->connector);
190 		if (err < 0)
191 			return err;
192 	}
193 
194 	/*
195 	 * The connector is now registered and ready to receive hotplug events
196 	 * so the hotplug interrupt can be enabled.
197 	 */
198 	if (output->hpd_gpio)
199 		enable_irq(output->hpd_irq);
200 
201 	connector_type = output->connector.connector_type;
202 	/*
203 	 * Create a CEC notifier for HDMI connector.
204 	 */
205 	if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
206 	    connector_type == DRM_MODE_CONNECTOR_HDMIB) {
207 		struct cec_connector_info conn_info;
208 
209 		cec_fill_conn_info_from_drm(&conn_info, &output->connector);
210 		output->cec = cec_notifier_conn_register(output->dev, NULL,
211 							 &conn_info);
212 		if (!output->cec)
213 			return -ENOMEM;
214 	}
215 
216 	return 0;
217 }
218 
219 void tegra_output_exit(struct tegra_output *output)
220 {
221 	/*
222 	 * The connector is going away, so the interrupt must be disabled to
223 	 * prevent the hotplug interrupt handler from potentially crashing.
224 	 */
225 	if (output->hpd_gpio)
226 		disable_irq(output->hpd_irq);
227 
228 	if (output->panel)
229 		drm_panel_detach(output->panel);
230 }
231 
232 void tegra_output_find_possible_crtcs(struct tegra_output *output,
233 				      struct drm_device *drm)
234 {
235 	struct device *dev = output->dev;
236 	struct drm_crtc *crtc;
237 	unsigned int mask = 0;
238 
239 	drm_for_each_crtc(crtc, drm) {
240 		struct tegra_dc *dc = to_tegra_dc(crtc);
241 
242 		if (tegra_dc_has_output(dc, dev))
243 			mask |= drm_crtc_mask(crtc);
244 	}
245 
246 	if (mask == 0) {
247 		dev_warn(dev, "missing output definition for heads in DT\n");
248 		mask = 0x3;
249 	}
250 
251 	output->encoder.possible_crtcs = mask;
252 }
253