drm/msm: add hdmi support for apq8x74/mdp5
[deliverable/linux.git] / drivers / gpu / drm / msm / hdmi / hdmi_connector.c
CommitLineData
c8afe684
RC
1/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/gpio.h>
19
dd2da6e3 20#include "msm_kms.h"
c8afe684
RC
21#include "hdmi.h"
22
23struct hdmi_connector {
a3376e3e
RC
24 struct drm_connector base;
25 struct hdmi *hdmi;
dada25bd 26 struct work_struct hpd_work;
c8afe684
RC
27};
28#define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
29
30static int gpio_config(struct hdmi *hdmi, bool on)
31{
32 struct drm_device *dev = hdmi->dev;
dada25bd 33 const struct hdmi_platform_config *config = hdmi->config;
c8afe684
RC
34 int ret;
35
36 if (on) {
37 ret = gpio_request(config->ddc_clk_gpio, "HDMI_DDC_CLK");
38 if (ret) {
39 dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
40 "HDMI_DDC_CLK", config->ddc_clk_gpio, ret);
41 goto error1;
42 }
dada25bd
RC
43 gpio_set_value_cansleep(config->ddc_clk_gpio, 1);
44
c8afe684
RC
45 ret = gpio_request(config->ddc_data_gpio, "HDMI_DDC_DATA");
46 if (ret) {
47 dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
48 "HDMI_DDC_DATA", config->ddc_data_gpio, ret);
49 goto error2;
50 }
dada25bd
RC
51 gpio_set_value_cansleep(config->ddc_data_gpio, 1);
52
c8afe684
RC
53 ret = gpio_request(config->hpd_gpio, "HDMI_HPD");
54 if (ret) {
55 dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
56 "HDMI_HPD", config->hpd_gpio, ret);
57 goto error3;
58 }
dada25bd
RC
59 gpio_direction_input(config->hpd_gpio);
60 gpio_set_value_cansleep(config->hpd_gpio, 1);
61
62 if (config->mux_en_gpio != -1) {
63 ret = gpio_request(config->mux_en_gpio, "HDMI_MUX_EN");
c8afe684
RC
64 if (ret) {
65 dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
dada25bd 66 "HDMI_MUX_SEL", config->mux_en_gpio, ret);
c8afe684
RC
67 goto error4;
68 }
dada25bd
RC
69 gpio_set_value_cansleep(config->mux_en_gpio, 1);
70 }
71
72 if (config->mux_sel_gpio != -1) {
73 ret = gpio_request(config->mux_sel_gpio, "HDMI_MUX_SEL");
74 if (ret) {
75 dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
76 "HDMI_MUX_SEL", config->mux_sel_gpio, ret);
77 goto error5;
78 }
79 gpio_set_value_cansleep(config->mux_sel_gpio, 0);
c8afe684
RC
80 }
81 DBG("gpio on");
82 } else {
83 gpio_free(config->ddc_clk_gpio);
84 gpio_free(config->ddc_data_gpio);
85 gpio_free(config->hpd_gpio);
86
dada25bd
RC
87 if (config->mux_en_gpio != -1) {
88 gpio_set_value_cansleep(config->mux_en_gpio, 0);
89 gpio_free(config->mux_en_gpio);
90 }
91
92 if (config->mux_sel_gpio != -1) {
93 gpio_set_value_cansleep(config->mux_sel_gpio, 1);
94 gpio_free(config->mux_sel_gpio);
c8afe684
RC
95 }
96 DBG("gpio off");
97 }
98
99 return 0;
100
dada25bd
RC
101error5:
102 if (config->mux_en_gpio != -1)
103 gpio_free(config->mux_en_gpio);
c8afe684
RC
104error4:
105 gpio_free(config->hpd_gpio);
106error3:
107 gpio_free(config->ddc_data_gpio);
108error2:
109 gpio_free(config->ddc_clk_gpio);
110error1:
111 return ret;
112}
113
114static int hpd_enable(struct hdmi_connector *hdmi_connector)
115{
a3376e3e 116 struct hdmi *hdmi = hdmi_connector->hdmi;
dada25bd 117 const struct hdmi_platform_config *config = hdmi->config;
a3376e3e 118 struct drm_device *dev = hdmi_connector->base.dev;
c8afe684
RC
119 struct hdmi_phy *phy = hdmi->phy;
120 uint32_t hpd_ctrl;
dada25bd 121 int i, ret;
c8afe684
RC
122
123 ret = gpio_config(hdmi, true);
124 if (ret) {
125 dev_err(dev->dev, "failed to configure GPIOs: %d\n", ret);
126 goto fail;
127 }
128
dada25bd
RC
129 for (i = 0; i < config->hpd_clk_cnt; i++) {
130 ret = clk_prepare_enable(hdmi->hpd_clks[i]);
131 if (ret) {
132 dev_err(dev->dev, "failed to enable hpd clk: %s (%d)\n",
133 config->hpd_clk_names[i], ret);
134 goto fail;
135 }
c8afe684
RC
136 }
137
dada25bd
RC
138 for (i = 0; i < config->hpd_reg_cnt; i++) {
139 ret = regulator_enable(hdmi->hpd_regs[i]);
140 if (ret) {
141 dev_err(dev->dev, "failed to enable hpd regulator: %s (%d)\n",
142 config->hpd_reg_names[i], ret);
143 goto fail;
144 }
c8afe684
RC
145 }
146
147 hdmi_set_mode(hdmi, false);
148 phy->funcs->reset(phy);
149 hdmi_set_mode(hdmi, true);
150
151 hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
152
153 /* enable HPD events: */
154 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
155 HDMI_HPD_INT_CTRL_INT_CONNECT |
156 HDMI_HPD_INT_CTRL_INT_EN);
157
158 /* set timeout to 4.1ms (max) for hardware debounce */
159 hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
160 hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
161
162 /* Toggle HPD circuit to trigger HPD sense */
163 hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
164 ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
165 hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
166 HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
167
168 return 0;
169
170fail:
171 return ret;
172}
173
174static int hdp_disable(struct hdmi_connector *hdmi_connector)
175{
a3376e3e 176 struct hdmi *hdmi = hdmi_connector->hdmi;
dada25bd 177 const struct hdmi_platform_config *config = hdmi->config;
a3376e3e 178 struct drm_device *dev = hdmi_connector->base.dev;
dada25bd 179 int i, ret = 0;
c8afe684
RC
180
181 /* Disable HPD interrupt */
182 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
183
184 hdmi_set_mode(hdmi, false);
185
dada25bd
RC
186 for (i = 0; i < config->hpd_reg_cnt; i++) {
187 ret = regulator_disable(hdmi->hpd_regs[i]);
188 if (ret) {
189 dev_err(dev->dev, "failed to disable hpd regulator: %s (%d)\n",
190 config->hpd_reg_names[i], ret);
191 goto fail;
192 }
c8afe684
RC
193 }
194
dada25bd
RC
195 for (i = 0; i < config->hpd_clk_cnt; i++)
196 clk_disable_unprepare(hdmi->hpd_clks[i]);
c8afe684
RC
197
198 ret = gpio_config(hdmi, false);
199 if (ret) {
200 dev_err(dev->dev, "failed to unconfigure GPIOs: %d\n", ret);
201 goto fail;
202 }
203
204 return 0;
205
206fail:
207 return ret;
208}
209
dada25bd
RC
210static void
211hotplug_work(struct work_struct *work)
212{
213 struct hdmi_connector *hdmi_connector =
214 container_of(work, struct hdmi_connector, hpd_work);
215 struct drm_connector *connector = &hdmi_connector->base;
216 drm_helper_hpd_irq_event(connector->dev);
217}
218
c8afe684
RC
219void hdmi_connector_irq(struct drm_connector *connector)
220{
a3376e3e 221 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
dada25bd 222 struct msm_drm_private *priv = connector->dev->dev_private;
a3376e3e 223 struct hdmi *hdmi = hdmi_connector->hdmi;
c8afe684
RC
224 uint32_t hpd_int_status, hpd_int_ctrl;
225
226 /* Process HPD: */
227 hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
228 hpd_int_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
229
230 if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
231 (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
232 bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
233
234 DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
235
236 /* ack the irq: */
237 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
238 hpd_int_ctrl | HDMI_HPD_INT_CTRL_INT_ACK);
239
c8afe684
RC
240 /* detect disconnect if we are connected or visa versa: */
241 hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
242 if (!detected)
243 hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
244 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
dada25bd
RC
245
246 queue_work(priv->wq, &hdmi_connector->hpd_work);
c8afe684
RC
247 }
248}
249
250static enum drm_connector_status hdmi_connector_detect(
251 struct drm_connector *connector, bool force)
252{
a3376e3e
RC
253 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
254 struct hdmi *hdmi = hdmi_connector->hdmi;
dada25bd 255 const struct hdmi_platform_config *config = hdmi->config;
c8afe684
RC
256 uint32_t hpd_int_status;
257 int retry = 20;
258
259 hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
260
261 /* sense seems to in some cases be momentarily de-asserted, don't
262 * let that trick us into thinking the monitor is gone:
263 */
264 while (retry-- && !(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED)) {
dada25bd
RC
265 /* hdmi debounce logic seems to get stuck sometimes,
266 * read directly the gpio to get a second opinion:
267 */
268 if (gpio_get_value(config->hpd_gpio)) {
269 DBG("gpio tells us we are connected!");
270 hpd_int_status |= HDMI_HPD_INT_STATUS_CABLE_DETECTED;
271 break;
272 }
c8afe684
RC
273 mdelay(10);
274 hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
275 DBG("status=%08x", hpd_int_status);
276 }
277
278 return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
279 connector_status_connected : connector_status_disconnected;
280}
281
282static void hdmi_connector_destroy(struct drm_connector *connector)
283{
a3376e3e 284 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
c8afe684
RC
285
286 hdp_disable(hdmi_connector);
287
288 drm_sysfs_connector_remove(connector);
289 drm_connector_cleanup(connector);
290
a3376e3e 291 hdmi_unreference(hdmi_connector->hdmi);
c8afe684
RC
292
293 kfree(hdmi_connector);
294}
295
296static int hdmi_connector_get_modes(struct drm_connector *connector)
297{
a3376e3e
RC
298 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
299 struct hdmi *hdmi = hdmi_connector->hdmi;
c8afe684
RC
300 struct edid *edid;
301 uint32_t hdmi_ctrl;
302 int ret = 0;
303
304 hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
305 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
306
307 edid = drm_get_edid(connector, hdmi->i2c);
308
309 hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
310
311 drm_mode_connector_update_edid_property(connector, edid);
312
313 if (edid) {
314 ret = drm_add_edid_modes(connector, edid);
315 kfree(edid);
316 }
317
318 return ret;
319}
320
321static int hdmi_connector_mode_valid(struct drm_connector *connector,
322 struct drm_display_mode *mode)
323{
a3376e3e 324 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
dada25bd
RC
325 struct hdmi *hdmi = hdmi_connector->hdmi;
326 const struct hdmi_platform_config *config = hdmi->config;
c8afe684
RC
327 struct msm_drm_private *priv = connector->dev->dev_private;
328 struct msm_kms *kms = priv->kms;
329 long actual, requested;
330
331 requested = 1000 * mode->clock;
332 actual = kms->funcs->round_pixclk(kms,
a3376e3e 333 requested, hdmi_connector->hdmi->encoder);
c8afe684 334
dada25bd
RC
335 /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
336 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
337 * instead):
338 */
339 if (config->pwr_clk_cnt > 0)
340 actual = clk_round_rate(hdmi->pwr_clks[0], actual);
341
c8afe684
RC
342 DBG("requested=%ld, actual=%ld", requested, actual);
343
344 if (actual != requested)
345 return MODE_CLOCK_RANGE;
346
347 return 0;
348}
349
a3376e3e
RC
350static struct drm_encoder *
351hdmi_connector_best_encoder(struct drm_connector *connector)
352{
353 struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
354 return hdmi_connector->hdmi->encoder;
355}
356
c8afe684
RC
357static const struct drm_connector_funcs hdmi_connector_funcs = {
358 .dpms = drm_helper_connector_dpms,
359 .detect = hdmi_connector_detect,
360 .fill_modes = drm_helper_probe_single_connector_modes,
361 .destroy = hdmi_connector_destroy,
362};
363
364static const struct drm_connector_helper_funcs hdmi_connector_helper_funcs = {
365 .get_modes = hdmi_connector_get_modes,
366 .mode_valid = hdmi_connector_mode_valid,
a3376e3e 367 .best_encoder = hdmi_connector_best_encoder,
c8afe684
RC
368};
369
370/* initialize connector */
a3376e3e 371struct drm_connector *hdmi_connector_init(struct hdmi *hdmi)
c8afe684
RC
372{
373 struct drm_connector *connector = NULL;
374 struct hdmi_connector *hdmi_connector;
375 int ret;
376
377 hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
378 if (!hdmi_connector) {
379 ret = -ENOMEM;
380 goto fail;
381 }
382
a3376e3e 383 hdmi_connector->hdmi = hdmi_reference(hdmi);
dada25bd 384 INIT_WORK(&hdmi_connector->hpd_work, hotplug_work);
a3376e3e
RC
385
386 connector = &hdmi_connector->base;
c8afe684 387
a3376e3e 388 drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
c8afe684
RC
389 DRM_MODE_CONNECTOR_HDMIA);
390 drm_connector_helper_add(connector, &hdmi_connector_helper_funcs);
391
392 connector->polled = DRM_CONNECTOR_POLL_HPD;
393
394 connector->interlace_allowed = 1;
395 connector->doublescan_allowed = 0;
396
397 drm_sysfs_connector_add(connector);
398
c8afe684
RC
399 ret = hpd_enable(hdmi_connector);
400 if (ret) {
a3376e3e 401 dev_err(hdmi->dev->dev, "failed to enable HPD: %d\n", ret);
c8afe684
RC
402 goto fail;
403 }
404
a3376e3e 405 drm_mode_connector_attach_encoder(connector, hdmi->encoder);
c8afe684
RC
406
407 return connector;
408
409fail:
410 if (connector)
411 hdmi_connector_destroy(connector);
412
413 return ERR_PTR(ret);
414}
This page took 0.104473 seconds and 5 git commands to generate.