blob: 46664b6222707e3d4e51e4629305268711503c33 [file] [log] [blame]
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001/*
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 Reding4aa3df72014-11-24 16:27:13 +010010#include <drm/drm_atomic_helper.h>
Thierry Reding9be7d862013-08-30 15:22:36 +020011#include <drm/drm_panel.h>
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000012#include "drm.h"
13
Thierry Reding132085d2014-11-28 15:38:40 +010014int tegra_output_connector_get_modes(struct drm_connector *connector)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000015{
16 struct tegra_output *output = connector_to_output(connector);
17 struct edid *edid = NULL;
18 int err = 0;
19
Thierry Redingacde5412014-01-13 12:56:19 +010020 /*
21 * If the panel provides one or more modes, use them exclusively and
22 * ignore any other means of obtaining a mode.
23 */
Thierry Reding9be7d862013-08-30 15:22:36 +020024 if (output->panel) {
25 err = output->panel->funcs->get_modes(output->panel);
26 if (err > 0)
27 return err;
28 }
29
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000030 if (output->edid)
31 edid = kmemdup(output->edid, sizeof(*edid), GFP_KERNEL);
32 else if (output->ddc)
33 edid = drm_get_edid(connector, output->ddc);
34
35 drm_mode_connector_update_edid_property(connector, edid);
36
37 if (edid) {
38 err = drm_add_edid_modes(connector, edid);
39 kfree(edid);
40 }
41
42 return err;
43}
44
Thierry Reding132085d2014-11-28 15:38:40 +010045struct drm_encoder *
46tegra_output_connector_best_encoder(struct drm_connector *connector)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000047{
48 struct tegra_output *output = connector_to_output(connector);
49
50 return &output->encoder;
51}
52
Thierry Reding132085d2014-11-28 15:38:40 +010053enum drm_connector_status
54tegra_output_connector_detect(struct drm_connector *connector, bool force)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000055{
56 struct tegra_output *output = connector_to_output(connector);
57 enum drm_connector_status status = connector_status_unknown;
58
59 if (gpio_is_valid(output->hpd_gpio)) {
Thierry Redingfb36d0ee2015-04-27 15:12:39 +020060 if (output->hpd_gpio_flags & OF_GPIO_ACTIVE_LOW) {
61 if (gpio_get_value(output->hpd_gpio) != 0)
62 status = connector_status_disconnected;
63 else
64 status = connector_status_connected;
65 } else {
66 if (gpio_get_value(output->hpd_gpio) == 0)
67 status = connector_status_disconnected;
68 else
69 status = connector_status_connected;
70 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000071 } else {
Thierry Reding9be7d862013-08-30 15:22:36 +020072 if (!output->panel)
73 status = connector_status_disconnected;
74 else
75 status = connector_status_connected;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000076 }
77
78 return status;
79}
80
Thierry Reding132085d2014-11-28 15:38:40 +010081void tegra_output_connector_destroy(struct drm_connector *connector)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000082{
Thomas Wood34ea3d32014-05-29 16:57:41 +010083 drm_connector_unregister(connector);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000084 drm_connector_cleanup(connector);
85}
86
Thierry Reding132085d2014-11-28 15:38:40 +010087void tegra_output_encoder_destroy(struct drm_encoder *encoder)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000088{
89 drm_encoder_cleanup(encoder);
90}
91
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000092static irqreturn_t hpd_irq(int irq, void *data)
93{
94 struct tegra_output *output = data;
95
Thierry Reding8fc8f7d2014-10-21 14:00:09 +020096 if (output->connector.dev)
97 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000098
99 return IRQ_HANDLED;
100}
101
Thierry Reding59d29c02013-10-14 14:26:42 +0200102int tegra_output_probe(struct tegra_output *output)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000103{
Thierry Reding9be7d862013-08-30 15:22:36 +0200104 struct device_node *ddc, *panel;
Thierry Reding7236aa02014-01-13 13:58:17 +0100105 int err, size;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000106
107 if (!output->of_node)
108 output->of_node = output->dev->of_node;
109
Thierry Reding9be7d862013-08-30 15:22:36 +0200110 panel = of_parse_phandle(output->of_node, "nvidia,panel", 0);
111 if (panel) {
112 output->panel = of_drm_find_panel(panel);
113 if (!output->panel)
114 return -EPROBE_DEFER;
115
116 of_node_put(panel);
117 }
118
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000119 output->edid = of_get_property(output->of_node, "nvidia,edid", &size);
120
121 ddc = of_parse_phandle(output->of_node, "nvidia,ddc-i2c-bus", 0);
122 if (ddc) {
123 output->ddc = of_find_i2c_adapter_by_node(ddc);
124 if (!output->ddc) {
125 err = -EPROBE_DEFER;
126 of_node_put(ddc);
127 return err;
128 }
129
130 of_node_put(ddc);
131 }
132
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000133 output->hpd_gpio = of_get_named_gpio_flags(output->of_node,
134 "nvidia,hpd-gpio", 0,
Thierry Redingfb36d0ee2015-04-27 15:12:39 +0200135 &output->hpd_gpio_flags);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000136 if (gpio_is_valid(output->hpd_gpio)) {
137 unsigned long flags;
138
139 err = gpio_request_one(output->hpd_gpio, GPIOF_DIR_IN,
140 "HDMI hotplug detect");
141 if (err < 0) {
142 dev_err(output->dev, "gpio_request_one(): %d\n", err);
143 return err;
144 }
145
146 err = gpio_to_irq(output->hpd_gpio);
147 if (err < 0) {
148 dev_err(output->dev, "gpio_to_irq(): %d\n", err);
Thierry Reding59d29c02013-10-14 14:26:42 +0200149 gpio_free(output->hpd_gpio);
150 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000151 }
152
153 output->hpd_irq = err;
154
155 flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
156 IRQF_ONESHOT;
157
158 err = request_threaded_irq(output->hpd_irq, NULL, hpd_irq,
159 flags, "hpd", output);
160 if (err < 0) {
161 dev_err(output->dev, "failed to request IRQ#%u: %d\n",
162 output->hpd_irq, err);
Thierry Reding59d29c02013-10-14 14:26:42 +0200163 gpio_free(output->hpd_gpio);
164 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000165 }
166
167 output->connector.polled = DRM_CONNECTOR_POLL_HPD;
Thierry Reding8fc8f7d2014-10-21 14:00:09 +0200168
169 /*
170 * Disable the interrupt until the connector has been
171 * initialized to avoid a race in the hotplug interrupt
172 * handler.
173 */
174 disable_irq(output->hpd_irq);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000175 }
176
Thierry Reding59d29c02013-10-14 14:26:42 +0200177 return 0;
178}
179
Thierry Reding328ec692014-12-19 15:55:08 +0100180void tegra_output_remove(struct tegra_output *output)
Thierry Reding59d29c02013-10-14 14:26:42 +0200181{
182 if (gpio_is_valid(output->hpd_gpio)) {
183 free_irq(output->hpd_irq, output);
184 gpio_free(output->hpd_gpio);
185 }
186
187 if (output->ddc)
188 put_device(&output->ddc->dev);
Thierry Reding59d29c02013-10-14 14:26:42 +0200189}
190
191int tegra_output_init(struct drm_device *drm, struct tegra_output *output)
192{
Thierry Redingea130b22014-12-19 15:51:35 +0100193 int err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000194
Thierry Redingea130b22014-12-19 15:51:35 +0100195 if (output->panel) {
196 err = drm_panel_attach(output->panel, &output->connector);
197 if (err < 0)
198 return err;
199 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000200
Thierry Reding8fc8f7d2014-10-21 14:00:09 +0200201 /*
202 * The connector is now registered and ready to receive hotplug events
203 * so the hotplug interrupt can be enabled.
204 */
205 if (gpio_is_valid(output->hpd_gpio))
206 enable_irq(output->hpd_irq);
207
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000208 return 0;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000209}
210
Thierry Reding328ec692014-12-19 15:55:08 +0100211void tegra_output_exit(struct tegra_output *output)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000212{
Thierry Reding8fc8f7d2014-10-21 14:00:09 +0200213 /*
214 * The connector is going away, so the interrupt must be disabled to
215 * prevent the hotplug interrupt handler from potentially crashing.
216 */
217 if (gpio_is_valid(output->hpd_gpio))
218 disable_irq(output->hpd_irq);
219
Thierry Reding9aaa0ce2014-11-04 16:20:20 +0100220 if (output->panel)
221 drm_panel_detach(output->panel);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000222}