1 /* 2 * Copyright (C) 2012 Avionic Design GmbH 3 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 */ 9 10 #include <linux/of_gpio.h> 11 12 #include <drm/drm_panel.h> 13 #include "drm.h" 14 15 static int tegra_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 drm_mode_connector_update_edid_property(connector, edid); 37 38 if (edid) { 39 err = drm_add_edid_modes(connector, edid); 40 kfree(edid); 41 } 42 43 return err; 44 } 45 46 static int tegra_connector_mode_valid(struct drm_connector *connector, 47 struct drm_display_mode *mode) 48 { 49 struct tegra_output *output = connector_to_output(connector); 50 enum drm_mode_status status = MODE_OK; 51 int err; 52 53 err = tegra_output_check_mode(output, mode, &status); 54 if (err < 0) 55 return MODE_ERROR; 56 57 return status; 58 } 59 60 static struct drm_encoder * 61 tegra_connector_best_encoder(struct drm_connector *connector) 62 { 63 struct tegra_output *output = connector_to_output(connector); 64 65 return &output->encoder; 66 } 67 68 static const struct drm_connector_helper_funcs connector_helper_funcs = { 69 .get_modes = tegra_connector_get_modes, 70 .mode_valid = tegra_connector_mode_valid, 71 .best_encoder = tegra_connector_best_encoder, 72 }; 73 74 static enum drm_connector_status 75 tegra_connector_detect(struct drm_connector *connector, bool force) 76 { 77 struct tegra_output *output = connector_to_output(connector); 78 enum drm_connector_status status = connector_status_unknown; 79 80 if (output->ops->detect) 81 return output->ops->detect(output); 82 83 if (gpio_is_valid(output->hpd_gpio)) { 84 if (gpio_get_value(output->hpd_gpio) == 0) 85 status = connector_status_disconnected; 86 else 87 status = connector_status_connected; 88 } else { 89 if (!output->panel) 90 status = connector_status_disconnected; 91 else 92 status = connector_status_connected; 93 94 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS) 95 status = connector_status_connected; 96 } 97 98 return status; 99 } 100 101 static void drm_connector_clear(struct drm_connector *connector) 102 { 103 memset(connector, 0, sizeof(*connector)); 104 } 105 106 static void tegra_connector_destroy(struct drm_connector *connector) 107 { 108 drm_connector_unregister(connector); 109 drm_connector_cleanup(connector); 110 drm_connector_clear(connector); 111 } 112 113 static const struct drm_connector_funcs connector_funcs = { 114 .dpms = drm_helper_connector_dpms, 115 .detect = tegra_connector_detect, 116 .fill_modes = drm_helper_probe_single_connector_modes, 117 .destroy = tegra_connector_destroy, 118 }; 119 120 static void drm_encoder_clear(struct drm_encoder *encoder) 121 { 122 memset(encoder, 0, sizeof(*encoder)); 123 } 124 125 static void tegra_encoder_destroy(struct drm_encoder *encoder) 126 { 127 drm_encoder_cleanup(encoder); 128 drm_encoder_clear(encoder); 129 } 130 131 static const struct drm_encoder_funcs encoder_funcs = { 132 .destroy = tegra_encoder_destroy, 133 }; 134 135 static void tegra_encoder_dpms(struct drm_encoder *encoder, int mode) 136 { 137 struct tegra_output *output = encoder_to_output(encoder); 138 struct drm_panel *panel = output->panel; 139 140 if (mode != DRM_MODE_DPMS_ON) { 141 drm_panel_disable(panel); 142 tegra_output_disable(output); 143 drm_panel_unprepare(panel); 144 } else { 145 drm_panel_prepare(panel); 146 tegra_output_enable(output); 147 drm_panel_enable(panel); 148 } 149 } 150 151 static bool tegra_encoder_mode_fixup(struct drm_encoder *encoder, 152 const struct drm_display_mode *mode, 153 struct drm_display_mode *adjusted) 154 { 155 return true; 156 } 157 158 static void tegra_encoder_prepare(struct drm_encoder *encoder) 159 { 160 tegra_encoder_dpms(encoder, DRM_MODE_DPMS_OFF); 161 } 162 163 static void tegra_encoder_commit(struct drm_encoder *encoder) 164 { 165 tegra_encoder_dpms(encoder, DRM_MODE_DPMS_ON); 166 } 167 168 static void tegra_encoder_mode_set(struct drm_encoder *encoder, 169 struct drm_display_mode *mode, 170 struct drm_display_mode *adjusted) 171 { 172 } 173 174 static const struct drm_encoder_helper_funcs encoder_helper_funcs = { 175 .dpms = tegra_encoder_dpms, 176 .mode_fixup = tegra_encoder_mode_fixup, 177 .prepare = tegra_encoder_prepare, 178 .commit = tegra_encoder_commit, 179 .mode_set = tegra_encoder_mode_set, 180 }; 181 182 static irqreturn_t hpd_irq(int irq, void *data) 183 { 184 struct tegra_output *output = data; 185 186 if (output->connector.dev) 187 drm_helper_hpd_irq_event(output->connector.dev); 188 189 return IRQ_HANDLED; 190 } 191 192 int tegra_output_probe(struct tegra_output *output) 193 { 194 struct device_node *ddc, *panel; 195 enum of_gpio_flags flags; 196 int err, size; 197 198 if (!output->of_node) 199 output->of_node = output->dev->of_node; 200 201 panel = of_parse_phandle(output->of_node, "nvidia,panel", 0); 202 if (panel) { 203 output->panel = of_drm_find_panel(panel); 204 if (!output->panel) 205 return -EPROBE_DEFER; 206 207 of_node_put(panel); 208 } 209 210 output->edid = of_get_property(output->of_node, "nvidia,edid", &size); 211 212 ddc = of_parse_phandle(output->of_node, "nvidia,ddc-i2c-bus", 0); 213 if (ddc) { 214 output->ddc = of_find_i2c_adapter_by_node(ddc); 215 if (!output->ddc) { 216 err = -EPROBE_DEFER; 217 of_node_put(ddc); 218 return err; 219 } 220 221 of_node_put(ddc); 222 } 223 224 output->hpd_gpio = of_get_named_gpio_flags(output->of_node, 225 "nvidia,hpd-gpio", 0, 226 &flags); 227 if (gpio_is_valid(output->hpd_gpio)) { 228 unsigned long flags; 229 230 err = gpio_request_one(output->hpd_gpio, GPIOF_DIR_IN, 231 "HDMI hotplug detect"); 232 if (err < 0) { 233 dev_err(output->dev, "gpio_request_one(): %d\n", err); 234 return err; 235 } 236 237 err = gpio_to_irq(output->hpd_gpio); 238 if (err < 0) { 239 dev_err(output->dev, "gpio_to_irq(): %d\n", err); 240 gpio_free(output->hpd_gpio); 241 return err; 242 } 243 244 output->hpd_irq = err; 245 246 flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | 247 IRQF_ONESHOT; 248 249 err = request_threaded_irq(output->hpd_irq, NULL, hpd_irq, 250 flags, "hpd", output); 251 if (err < 0) { 252 dev_err(output->dev, "failed to request IRQ#%u: %d\n", 253 output->hpd_irq, err); 254 gpio_free(output->hpd_gpio); 255 return err; 256 } 257 258 output->connector.polled = DRM_CONNECTOR_POLL_HPD; 259 260 /* 261 * Disable the interrupt until the connector has been 262 * initialized to avoid a race in the hotplug interrupt 263 * handler. 264 */ 265 disable_irq(output->hpd_irq); 266 } 267 268 return 0; 269 } 270 271 int tegra_output_remove(struct tegra_output *output) 272 { 273 if (gpio_is_valid(output->hpd_gpio)) { 274 free_irq(output->hpd_irq, output); 275 gpio_free(output->hpd_gpio); 276 } 277 278 if (output->ddc) 279 put_device(&output->ddc->dev); 280 281 return 0; 282 } 283 284 int tegra_output_init(struct drm_device *drm, struct tegra_output *output) 285 { 286 int connector, encoder; 287 288 switch (output->type) { 289 case TEGRA_OUTPUT_RGB: 290 connector = DRM_MODE_CONNECTOR_LVDS; 291 encoder = DRM_MODE_ENCODER_LVDS; 292 break; 293 294 case TEGRA_OUTPUT_HDMI: 295 connector = DRM_MODE_CONNECTOR_HDMIA; 296 encoder = DRM_MODE_ENCODER_TMDS; 297 break; 298 299 case TEGRA_OUTPUT_DSI: 300 connector = DRM_MODE_CONNECTOR_DSI; 301 encoder = DRM_MODE_ENCODER_DSI; 302 break; 303 304 case TEGRA_OUTPUT_EDP: 305 connector = DRM_MODE_CONNECTOR_eDP; 306 encoder = DRM_MODE_ENCODER_TMDS; 307 break; 308 309 default: 310 connector = DRM_MODE_CONNECTOR_Unknown; 311 encoder = DRM_MODE_ENCODER_NONE; 312 break; 313 } 314 315 drm_connector_init(drm, &output->connector, &connector_funcs, 316 connector); 317 drm_connector_helper_add(&output->connector, &connector_helper_funcs); 318 output->connector.dpms = DRM_MODE_DPMS_OFF; 319 320 if (output->panel) 321 drm_panel_attach(output->panel, &output->connector); 322 323 drm_encoder_init(drm, &output->encoder, &encoder_funcs, encoder); 324 drm_encoder_helper_add(&output->encoder, &encoder_helper_funcs); 325 326 drm_mode_connector_attach_encoder(&output->connector, &output->encoder); 327 drm_connector_register(&output->connector); 328 329 output->encoder.possible_crtcs = 0x3; 330 331 /* 332 * The connector is now registered and ready to receive hotplug events 333 * so the hotplug interrupt can be enabled. 334 */ 335 if (gpio_is_valid(output->hpd_gpio)) 336 enable_irq(output->hpd_irq); 337 338 return 0; 339 } 340 341 int tegra_output_exit(struct tegra_output *output) 342 { 343 /* 344 * The connector is going away, so the interrupt must be disabled to 345 * prevent the hotplug interrupt handler from potentially crashing. 346 */ 347 if (gpio_is_valid(output->hpd_gpio)) 348 disable_irq(output->hpd_irq); 349 350 if (output->panel) 351 drm_panel_detach(output->panel); 352 353 return 0; 354 } 355