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 | 4aa3df7 | 2014-11-24 16:27:13 +0100 | [diff] [blame] | 10 | #include <drm/drm_atomic_helper.h> |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 11 | #include <drm/drm_panel.h> |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 12 | #include "drm.h" |
| 13 | |
Hans Verkuil | fb83be8 | 2017-09-11 14:29:52 +0200 | [diff] [blame] | 14 | #include <media/cec-notifier.h> |
| 15 | |
Thierry Reding | 132085d | 2014-11-28 15:38:40 +0100 | [diff] [blame] | 16 | int tegra_output_connector_get_modes(struct drm_connector *connector) |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 17 | { |
| 18 | struct tegra_output *output = connector_to_output(connector); |
| 19 | struct edid *edid = NULL; |
| 20 | int err = 0; |
| 21 | |
Thierry Reding | acde541 | 2014-01-13 12:56:19 +0100 | [diff] [blame] | 22 | /* |
| 23 | * If the panel provides one or more modes, use them exclusively and |
| 24 | * ignore any other means of obtaining a mode. |
| 25 | */ |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 26 | if (output->panel) { |
| 27 | err = output->panel->funcs->get_modes(output->panel); |
| 28 | if (err > 0) |
| 29 | return err; |
| 30 | } |
| 31 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 32 | if (output->edid) |
| 33 | edid = kmemdup(output->edid, sizeof(*edid), GFP_KERNEL); |
| 34 | else if (output->ddc) |
| 35 | edid = drm_get_edid(connector, output->ddc); |
| 36 | |
Hans Verkuil | fb83be8 | 2017-09-11 14:29:52 +0200 | [diff] [blame] | 37 | cec_notifier_set_phys_addr_from_edid(output->notifier, edid); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 38 | drm_mode_connector_update_edid_property(connector, edid); |
| 39 | |
| 40 | if (edid) { |
| 41 | err = drm_add_edid_modes(connector, edid); |
Thierry Reding | 2ccb396 | 2015-01-15 13:43:18 +0100 | [diff] [blame] | 42 | drm_edid_to_eld(connector, edid); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 43 | kfree(edid); |
| 44 | } |
| 45 | |
| 46 | return err; |
| 47 | } |
| 48 | |
Thierry Reding | 132085d | 2014-11-28 15:38:40 +0100 | [diff] [blame] | 49 | enum drm_connector_status |
| 50 | tegra_output_connector_detect(struct drm_connector *connector, bool force) |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 51 | { |
| 52 | struct tegra_output *output = connector_to_output(connector); |
| 53 | enum drm_connector_status status = connector_status_unknown; |
| 54 | |
| 55 | if (gpio_is_valid(output->hpd_gpio)) { |
Thierry Reding | fb36d0ee | 2015-04-27 15:12:39 +0200 | [diff] [blame] | 56 | if (output->hpd_gpio_flags & OF_GPIO_ACTIVE_LOW) { |
| 57 | if (gpio_get_value(output->hpd_gpio) != 0) |
| 58 | status = connector_status_disconnected; |
| 59 | else |
| 60 | status = connector_status_connected; |
| 61 | } else { |
| 62 | if (gpio_get_value(output->hpd_gpio) == 0) |
| 63 | status = connector_status_disconnected; |
| 64 | else |
| 65 | status = connector_status_connected; |
| 66 | } |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 67 | } else { |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 68 | if (!output->panel) |
| 69 | status = connector_status_disconnected; |
| 70 | else |
| 71 | status = connector_status_connected; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Hans Verkuil | fb83be8 | 2017-09-11 14:29:52 +0200 | [diff] [blame] | 74 | if (status != connector_status_connected) |
| 75 | cec_notifier_phys_addr_invalidate(output->notifier); |
| 76 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 77 | return status; |
| 78 | } |
| 79 | |
Thierry Reding | 132085d | 2014-11-28 15:38:40 +0100 | [diff] [blame] | 80 | void tegra_output_connector_destroy(struct drm_connector *connector) |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 81 | { |
Thomas Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 82 | drm_connector_unregister(connector); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 83 | drm_connector_cleanup(connector); |
| 84 | } |
| 85 | |
Thierry Reding | 132085d | 2014-11-28 15:38:40 +0100 | [diff] [blame] | 86 | void tegra_output_encoder_destroy(struct drm_encoder *encoder) |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 87 | { |
| 88 | drm_encoder_cleanup(encoder); |
| 89 | } |
| 90 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 91 | static irqreturn_t hpd_irq(int irq, void *data) |
| 92 | { |
| 93 | struct tegra_output *output = data; |
| 94 | |
Thierry Reding | 8fc8f7d | 2014-10-21 14:00:09 +0200 | [diff] [blame] | 95 | if (output->connector.dev) |
| 96 | drm_helper_hpd_irq_event(output->connector.dev); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 97 | |
| 98 | return IRQ_HANDLED; |
| 99 | } |
| 100 | |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 101 | int tegra_output_probe(struct tegra_output *output) |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 102 | { |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 103 | struct device_node *ddc, *panel; |
Thierry Reding | 7236aa0 | 2014-01-13 13:58:17 +0100 | [diff] [blame] | 104 | int err, size; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 105 | |
| 106 | if (!output->of_node) |
| 107 | output->of_node = output->dev->of_node; |
| 108 | |
Thierry Reding | 9be7d86 | 2013-08-30 15:22:36 +0200 | [diff] [blame] | 109 | panel = of_parse_phandle(output->of_node, "nvidia,panel", 0); |
| 110 | if (panel) { |
| 111 | output->panel = of_drm_find_panel(panel); |
| 112 | if (!output->panel) |
| 113 | return -EPROBE_DEFER; |
| 114 | |
| 115 | of_node_put(panel); |
| 116 | } |
| 117 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 118 | output->edid = of_get_property(output->of_node, "nvidia,edid", &size); |
| 119 | |
| 120 | ddc = of_parse_phandle(output->of_node, "nvidia,ddc-i2c-bus", 0); |
| 121 | if (ddc) { |
| 122 | output->ddc = of_find_i2c_adapter_by_node(ddc); |
| 123 | if (!output->ddc) { |
| 124 | err = -EPROBE_DEFER; |
| 125 | of_node_put(ddc); |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | of_node_put(ddc); |
| 130 | } |
| 131 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 132 | output->hpd_gpio = of_get_named_gpio_flags(output->of_node, |
| 133 | "nvidia,hpd-gpio", 0, |
Thierry Reding | fb36d0ee | 2015-04-27 15:12:39 +0200 | [diff] [blame] | 134 | &output->hpd_gpio_flags); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 135 | if (gpio_is_valid(output->hpd_gpio)) { |
| 136 | unsigned long flags; |
| 137 | |
| 138 | err = gpio_request_one(output->hpd_gpio, GPIOF_DIR_IN, |
| 139 | "HDMI hotplug detect"); |
| 140 | if (err < 0) { |
| 141 | dev_err(output->dev, "gpio_request_one(): %d\n", err); |
| 142 | return err; |
| 143 | } |
| 144 | |
| 145 | err = gpio_to_irq(output->hpd_gpio); |
| 146 | if (err < 0) { |
| 147 | dev_err(output->dev, "gpio_to_irq(): %d\n", err); |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 148 | gpio_free(output->hpd_gpio); |
| 149 | return err; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | output->hpd_irq = err; |
| 153 | |
| 154 | flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | |
| 155 | IRQF_ONESHOT; |
| 156 | |
| 157 | err = request_threaded_irq(output->hpd_irq, NULL, hpd_irq, |
| 158 | flags, "hpd", output); |
| 159 | if (err < 0) { |
| 160 | dev_err(output->dev, "failed to request IRQ#%u: %d\n", |
| 161 | output->hpd_irq, err); |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 162 | gpio_free(output->hpd_gpio); |
| 163 | return err; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | output->connector.polled = DRM_CONNECTOR_POLL_HPD; |
Thierry Reding | 8fc8f7d | 2014-10-21 14:00:09 +0200 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * Disable the interrupt until the connector has been |
| 170 | * initialized to avoid a race in the hotplug interrupt |
| 171 | * handler. |
| 172 | */ |
| 173 | disable_irq(output->hpd_irq); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
Thierry Reding | 328ec69 | 2014-12-19 15:55:08 +0100 | [diff] [blame] | 179 | void tegra_output_remove(struct tegra_output *output) |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 180 | { |
| 181 | if (gpio_is_valid(output->hpd_gpio)) { |
| 182 | free_irq(output->hpd_irq, output); |
| 183 | gpio_free(output->hpd_gpio); |
| 184 | } |
| 185 | |
| 186 | if (output->ddc) |
| 187 | put_device(&output->ddc->dev); |
Thierry Reding | 59d29c0 | 2013-10-14 14:26:42 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | int tegra_output_init(struct drm_device *drm, struct tegra_output *output) |
| 191 | { |
Thierry Reding | ea130b2 | 2014-12-19 15:51:35 +0100 | [diff] [blame] | 192 | int err; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 193 | |
Thierry Reding | ea130b2 | 2014-12-19 15:51:35 +0100 | [diff] [blame] | 194 | if (output->panel) { |
| 195 | err = drm_panel_attach(output->panel, &output->connector); |
| 196 | if (err < 0) |
| 197 | return err; |
| 198 | } |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 199 | |
Thierry Reding | 8fc8f7d | 2014-10-21 14:00:09 +0200 | [diff] [blame] | 200 | /* |
| 201 | * The connector is now registered and ready to receive hotplug events |
| 202 | * so the hotplug interrupt can be enabled. |
| 203 | */ |
| 204 | if (gpio_is_valid(output->hpd_gpio)) |
| 205 | enable_irq(output->hpd_irq); |
| 206 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 207 | return 0; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Thierry Reding | 328ec69 | 2014-12-19 15:55:08 +0100 | [diff] [blame] | 210 | void tegra_output_exit(struct tegra_output *output) |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 211 | { |
Thierry Reding | 8fc8f7d | 2014-10-21 14:00:09 +0200 | [diff] [blame] | 212 | /* |
| 213 | * The connector is going away, so the interrupt must be disabled to |
| 214 | * prevent the hotplug interrupt handler from potentially crashing. |
| 215 | */ |
| 216 | if (gpio_is_valid(output->hpd_gpio)) |
| 217 | disable_irq(output->hpd_irq); |
| 218 | |
Thierry Reding | 9aaa0ce | 2014-11-04 16:20:20 +0100 | [diff] [blame] | 219 | if (output->panel) |
| 220 | drm_panel_detach(output->panel); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 221 | } |