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 drm_connector_unregister(connector); 74 drm_connector_cleanup(connector); 75 } 76 77 void tegra_output_encoder_destroy(struct drm_encoder *encoder) 78 { 79 drm_encoder_cleanup(encoder); 80 } 81 82 static irqreturn_t hpd_irq(int irq, void *data) 83 { 84 struct tegra_output *output = data; 85 86 if (output->connector.dev) 87 drm_helper_hpd_irq_event(output->connector.dev); 88 89 return IRQ_HANDLED; 90 } 91 92 int tegra_output_probe(struct tegra_output *output) 93 { 94 struct device_node *ddc, *panel; 95 unsigned long flags; 96 int err, size; 97 98 if (!output->of_node) 99 output->of_node = output->dev->of_node; 100 101 panel = of_parse_phandle(output->of_node, "nvidia,panel", 0); 102 if (panel) { 103 output->panel = of_drm_find_panel(panel); 104 if (IS_ERR(output->panel)) 105 return PTR_ERR(output->panel); 106 107 of_node_put(panel); 108 } 109 110 output->edid = of_get_property(output->of_node, "nvidia,edid", &size); 111 112 ddc = of_parse_phandle(output->of_node, "nvidia,ddc-i2c-bus", 0); 113 if (ddc) { 114 output->ddc = of_find_i2c_adapter_by_node(ddc); 115 if (!output->ddc) { 116 err = -EPROBE_DEFER; 117 of_node_put(ddc); 118 return err; 119 } 120 121 of_node_put(ddc); 122 } 123 124 output->hpd_gpio = devm_gpiod_get_from_of_node(output->dev, 125 output->of_node, 126 "nvidia,hpd-gpio", 0, 127 GPIOD_IN, 128 "HDMI hotplug detect"); 129 if (IS_ERR(output->hpd_gpio)) 130 return PTR_ERR(output->hpd_gpio); 131 132 if (output->hpd_gpio) { 133 err = gpiod_to_irq(output->hpd_gpio); 134 if (err < 0) { 135 dev_err(output->dev, "gpiod_to_irq(): %d\n", err); 136 return err; 137 } 138 139 output->hpd_irq = err; 140 141 flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | 142 IRQF_ONESHOT; 143 144 err = request_threaded_irq(output->hpd_irq, NULL, hpd_irq, 145 flags, "hpd", output); 146 if (err < 0) { 147 dev_err(output->dev, "failed to request IRQ#%u: %d\n", 148 output->hpd_irq, err); 149 return err; 150 } 151 152 output->connector.polled = DRM_CONNECTOR_POLL_HPD; 153 154 /* 155 * Disable the interrupt until the connector has been 156 * initialized to avoid a race in the hotplug interrupt 157 * handler. 158 */ 159 disable_irq(output->hpd_irq); 160 } 161 162 output->cec = cec_notifier_get(output->dev); 163 if (!output->cec) 164 return -ENOMEM; 165 166 return 0; 167 } 168 169 void tegra_output_remove(struct tegra_output *output) 170 { 171 if (output->cec) 172 cec_notifier_put(output->cec); 173 174 if (output->hpd_gpio) 175 free_irq(output->hpd_irq, output); 176 177 if (output->ddc) 178 put_device(&output->ddc->dev); 179 } 180 181 int tegra_output_init(struct drm_device *drm, struct tegra_output *output) 182 { 183 int err; 184 185 if (output->panel) { 186 err = drm_panel_attach(output->panel, &output->connector); 187 if (err < 0) 188 return err; 189 } 190 191 /* 192 * The connector is now registered and ready to receive hotplug events 193 * so the hotplug interrupt can be enabled. 194 */ 195 if (output->hpd_gpio) 196 enable_irq(output->hpd_irq); 197 198 return 0; 199 } 200 201 void tegra_output_exit(struct tegra_output *output) 202 { 203 /* 204 * The connector is going away, so the interrupt must be disabled to 205 * prevent the hotplug interrupt handler from potentially crashing. 206 */ 207 if (output->hpd_gpio) 208 disable_irq(output->hpd_irq); 209 210 if (output->panel) 211 drm_panel_detach(output->panel); 212 } 213 214 void tegra_output_find_possible_crtcs(struct tegra_output *output, 215 struct drm_device *drm) 216 { 217 struct device *dev = output->dev; 218 struct drm_crtc *crtc; 219 unsigned int mask = 0; 220 221 drm_for_each_crtc(crtc, drm) { 222 struct tegra_dc *dc = to_tegra_dc(crtc); 223 224 if (tegra_dc_has_output(dc, dev)) 225 mask |= drm_crtc_mask(crtc); 226 } 227 228 if (mask == 0) { 229 dev_warn(dev, "missing output definition for heads in DT\n"); 230 mask = 0x3; 231 } 232 233 output->encoder.possible_crtcs = mask; 234 } 235