Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 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 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 10 | #include <linux/of_gpio.h> |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 11 | |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 12 | #include <drm/drm_panel.h> |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 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 | |
Thierry Reding | acde541 | 2014-01-13 12:56:19 +0100 | [diff] [blame] | 21 | /* |
| 22 | * If the panel provides one or more modes, use them exclusively and |
| 23 | * ignore any other means of obtaining a mode. |
| 24 | */ |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 25 | if (output->panel) { |
| 26 | err = output->panel->funcs->get_modes(output->panel); |
| 27 | if (err > 0) |
| 28 | return err; |
| 29 | } |
| 30 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 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 | |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 80 | if (output->ops->detect) |
| 81 | return output->ops->detect(output); |
| 82 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 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 { |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 89 | if (!output->panel) |
| 90 | status = connector_status_disconnected; |
| 91 | else |
| 92 | status = connector_status_connected; |
| 93 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 94 | if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS) |
| 95 | status = connector_status_connected; |
| 96 | } |
| 97 | |
| 98 | return status; |
| 99 | } |
| 100 | |
Thierry Reding | f002abc | 2013-10-14 14:06:02 +0200 | [diff] [blame] | 101 | static void drm_connector_clear(struct drm_connector *connector) |
| 102 | { |
| 103 | memset(connector, 0, sizeof(*connector)); |
| 104 | } |
| 105 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 106 | static void tegra_connector_destroy(struct drm_connector *connector) |
| 107 | { |
| 108 | drm_sysfs_connector_remove(connector); |
| 109 | drm_connector_cleanup(connector); |
Thierry Reding | f002abc | 2013-10-14 14:06:02 +0200 | [diff] [blame] | 110 | drm_connector_clear(connector); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 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 | |
Thierry Reding | f002abc | 2013-10-14 14:06:02 +0200 | [diff] [blame] | 120 | static void drm_encoder_clear(struct drm_encoder *encoder) |
| 121 | { |
| 122 | memset(encoder, 0, sizeof(*encoder)); |
| 123 | } |
| 124 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 125 | static void tegra_encoder_destroy(struct drm_encoder *encoder) |
| 126 | { |
| 127 | drm_encoder_cleanup(encoder); |
Thierry Reding | f002abc | 2013-10-14 14:06:02 +0200 | [diff] [blame] | 128 | drm_encoder_clear(encoder); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 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 | { |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 137 | struct tegra_output *output = encoder_to_output(encoder); |
| 138 | struct drm_panel *panel = output->panel; |
| 139 | |
Thierry Reding | b519002 | 2013-10-29 16:03:03 +0100 | [diff] [blame] | 140 | if (mode != DRM_MODE_DPMS_ON) { |
| 141 | drm_panel_disable(panel); |
| 142 | tegra_output_disable(output); |
| 143 | } else { |
| 144 | tegra_output_enable(output); |
| 145 | drm_panel_enable(panel); |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 146 | } |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static bool tegra_encoder_mode_fixup(struct drm_encoder *encoder, |
| 150 | const struct drm_display_mode *mode, |
| 151 | struct drm_display_mode *adjusted) |
| 152 | { |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | static void tegra_encoder_prepare(struct drm_encoder *encoder) |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | static void tegra_encoder_commit(struct drm_encoder *encoder) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | static void tegra_encoder_mode_set(struct drm_encoder *encoder, |
| 165 | struct drm_display_mode *mode, |
| 166 | struct drm_display_mode *adjusted) |
| 167 | { |
| 168 | struct tegra_output *output = encoder_to_output(encoder); |
| 169 | int err; |
| 170 | |
| 171 | err = tegra_output_enable(output); |
| 172 | if (err < 0) |
| 173 | dev_err(encoder->dev->dev, "tegra_output_enable(): %d\n", err); |
| 174 | } |
| 175 | |
| 176 | static const struct drm_encoder_helper_funcs encoder_helper_funcs = { |
| 177 | .dpms = tegra_encoder_dpms, |
| 178 | .mode_fixup = tegra_encoder_mode_fixup, |
| 179 | .prepare = tegra_encoder_prepare, |
| 180 | .commit = tegra_encoder_commit, |
| 181 | .mode_set = tegra_encoder_mode_set, |
| 182 | }; |
| 183 | |
| 184 | static irqreturn_t hpd_irq(int irq, void *data) |
| 185 | { |
| 186 | struct tegra_output *output = data; |
| 187 | |
| 188 | drm_helper_hpd_irq_event(output->connector.dev); |
| 189 | |
| 190 | return IRQ_HANDLED; |
| 191 | } |
| 192 | |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 193 | int tegra_output_probe(struct tegra_output *output) |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 194 | { |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 195 | struct device_node *ddc, *panel; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 196 | enum of_gpio_flags flags; |
Thierry Reding | 7236aa0 | 2014-01-13 13:58:17 +0100 | [diff] [blame] | 197 | int err, size; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 198 | |
| 199 | if (!output->of_node) |
| 200 | output->of_node = output->dev->of_node; |
| 201 | |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 202 | panel = of_parse_phandle(output->of_node, "nvidia,panel", 0); |
| 203 | if (panel) { |
| 204 | output->panel = of_drm_find_panel(panel); |
| 205 | if (!output->panel) |
| 206 | return -EPROBE_DEFER; |
| 207 | |
| 208 | of_node_put(panel); |
| 209 | } |
| 210 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 211 | output->edid = of_get_property(output->of_node, "nvidia,edid", &size); |
| 212 | |
| 213 | ddc = of_parse_phandle(output->of_node, "nvidia,ddc-i2c-bus", 0); |
| 214 | if (ddc) { |
| 215 | output->ddc = of_find_i2c_adapter_by_node(ddc); |
| 216 | if (!output->ddc) { |
| 217 | err = -EPROBE_DEFER; |
| 218 | of_node_put(ddc); |
| 219 | return err; |
| 220 | } |
| 221 | |
| 222 | of_node_put(ddc); |
| 223 | } |
| 224 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 225 | output->hpd_gpio = of_get_named_gpio_flags(output->of_node, |
| 226 | "nvidia,hpd-gpio", 0, |
| 227 | &flags); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 228 | if (gpio_is_valid(output->hpd_gpio)) { |
| 229 | unsigned long flags; |
| 230 | |
| 231 | err = gpio_request_one(output->hpd_gpio, GPIOF_DIR_IN, |
| 232 | "HDMI hotplug detect"); |
| 233 | if (err < 0) { |
| 234 | dev_err(output->dev, "gpio_request_one(): %d\n", err); |
| 235 | return err; |
| 236 | } |
| 237 | |
| 238 | err = gpio_to_irq(output->hpd_gpio); |
| 239 | if (err < 0) { |
| 240 | dev_err(output->dev, "gpio_to_irq(): %d\n", err); |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 241 | gpio_free(output->hpd_gpio); |
| 242 | return err; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | output->hpd_irq = err; |
| 246 | |
| 247 | flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | |
| 248 | IRQF_ONESHOT; |
| 249 | |
| 250 | err = request_threaded_irq(output->hpd_irq, NULL, hpd_irq, |
| 251 | flags, "hpd", output); |
| 252 | if (err < 0) { |
| 253 | dev_err(output->dev, "failed to request IRQ#%u: %d\n", |
| 254 | output->hpd_irq, err); |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 255 | gpio_free(output->hpd_gpio); |
| 256 | return err; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | output->connector.polled = DRM_CONNECTOR_POLL_HPD; |
| 260 | } |
| 261 | |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | int tegra_output_remove(struct tegra_output *output) |
| 266 | { |
| 267 | if (gpio_is_valid(output->hpd_gpio)) { |
| 268 | free_irq(output->hpd_irq, output); |
| 269 | gpio_free(output->hpd_gpio); |
| 270 | } |
| 271 | |
| 272 | if (output->ddc) |
| 273 | put_device(&output->ddc->dev); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int tegra_output_init(struct drm_device *drm, struct tegra_output *output) |
| 279 | { |
| 280 | int connector, encoder; |
| 281 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 282 | switch (output->type) { |
| 283 | case TEGRA_OUTPUT_RGB: |
| 284 | connector = DRM_MODE_CONNECTOR_LVDS; |
| 285 | encoder = DRM_MODE_ENCODER_LVDS; |
| 286 | break; |
| 287 | |
Thierry Reding | edec4af | 2012-11-15 21:28:23 +0000 | [diff] [blame] | 288 | case TEGRA_OUTPUT_HDMI: |
| 289 | connector = DRM_MODE_CONNECTOR_HDMIA; |
| 290 | encoder = DRM_MODE_ENCODER_TMDS; |
| 291 | break; |
| 292 | |
Thierry Reding | dec7273 | 2013-09-03 08:45:46 +0200 | [diff] [blame] | 293 | case TEGRA_OUTPUT_DSI: |
| 294 | connector = DRM_MODE_CONNECTOR_DSI; |
| 295 | encoder = DRM_MODE_ENCODER_DSI; |
| 296 | break; |
| 297 | |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 298 | case TEGRA_OUTPUT_EDP: |
| 299 | connector = DRM_MODE_CONNECTOR_eDP; |
| 300 | encoder = DRM_MODE_ENCODER_TMDS; |
| 301 | break; |
| 302 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 303 | default: |
| 304 | connector = DRM_MODE_CONNECTOR_Unknown; |
| 305 | encoder = DRM_MODE_ENCODER_NONE; |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | drm_connector_init(drm, &output->connector, &connector_funcs, |
| 310 | connector); |
| 311 | drm_connector_helper_add(&output->connector, &connector_helper_funcs); |
Thierry Reding | f8c3325 | 2013-09-24 09:58:08 +0200 | [diff] [blame] | 312 | output->connector.dpms = DRM_MODE_DPMS_OFF; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 313 | |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 314 | if (output->panel) |
| 315 | drm_panel_attach(output->panel, &output->connector); |
| 316 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 317 | drm_encoder_init(drm, &output->encoder, &encoder_funcs, encoder); |
| 318 | drm_encoder_helper_add(&output->encoder, &encoder_helper_funcs); |
| 319 | |
| 320 | drm_mode_connector_attach_encoder(&output->connector, &output->encoder); |
| 321 | drm_sysfs_connector_add(&output->connector); |
| 322 | |
| 323 | output->encoder.possible_crtcs = 0x3; |
| 324 | |
| 325 | return 0; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | int tegra_output_exit(struct tegra_output *output) |
| 329 | { |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 330 | return 0; |
| 331 | } |