OSDN Git Service

Merge tag 'topic/core-stuff-2014-06-30' of git://anongit.freedesktop.org/drm-intel...
[uclinux-h8/linux.git] / drivers / gpu / drm / msm / hdmi / hdmi_connector.c
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
20 #include "msm_kms.h"
21 #include "hdmi.h"
22
23 struct hdmi_connector {
24         struct drm_connector base;
25         struct hdmi *hdmi;
26         struct work_struct hpd_work;
27 };
28 #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
29
30 static int gpio_config(struct hdmi *hdmi, bool on)
31 {
32         struct drm_device *dev = hdmi->dev;
33         const struct hdmi_platform_config *config = hdmi->config;
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                 }
43                 gpio_set_value_cansleep(config->ddc_clk_gpio, 1);
44
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                 }
51                 gpio_set_value_cansleep(config->ddc_data_gpio, 1);
52
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                 }
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");
64                         if (ret) {
65                                 dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
66                                         "HDMI_MUX_SEL", config->mux_en_gpio, ret);
67                                 goto error4;
68                         }
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);
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
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);
95                 }
96                 DBG("gpio off");
97         }
98
99         return 0;
100
101 error5:
102         if (config->mux_en_gpio != -1)
103                 gpio_free(config->mux_en_gpio);
104 error4:
105         gpio_free(config->hpd_gpio);
106 error3:
107         gpio_free(config->ddc_data_gpio);
108 error2:
109         gpio_free(config->ddc_clk_gpio);
110 error1:
111         return ret;
112 }
113
114 static int hpd_enable(struct hdmi_connector *hdmi_connector)
115 {
116         struct hdmi *hdmi = hdmi_connector->hdmi;
117         const struct hdmi_platform_config *config = hdmi->config;
118         struct drm_device *dev = hdmi_connector->base.dev;
119         struct hdmi_phy *phy = hdmi->phy;
120         uint32_t hpd_ctrl;
121         int i, ret;
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
129         for (i = 0; i < config->hpd_clk_cnt; i++) {
130                 if (config->hpd_freq && config->hpd_freq[i]) {
131                         ret = clk_set_rate(hdmi->hpd_clks[i],
132                                         config->hpd_freq[i]);
133                         if (ret)
134                                 dev_warn(dev->dev, "failed to set clk %s (%d)\n",
135                                                 config->hpd_clk_names[i], ret);
136                 }
137
138                 ret = clk_prepare_enable(hdmi->hpd_clks[i]);
139                 if (ret) {
140                         dev_err(dev->dev, "failed to enable hpd clk: %s (%d)\n",
141                                         config->hpd_clk_names[i], ret);
142                         goto fail;
143                 }
144         }
145
146         for (i = 0; i < config->hpd_reg_cnt; i++) {
147                 ret = regulator_enable(hdmi->hpd_regs[i]);
148                 if (ret) {
149                         dev_err(dev->dev, "failed to enable hpd regulator: %s (%d)\n",
150                                         config->hpd_reg_names[i], ret);
151                         goto fail;
152                 }
153         }
154
155         hdmi_set_mode(hdmi, false);
156         phy->funcs->reset(phy);
157         hdmi_set_mode(hdmi, true);
158
159         hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
160
161         /* enable HPD events: */
162         hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
163                         HDMI_HPD_INT_CTRL_INT_CONNECT |
164                         HDMI_HPD_INT_CTRL_INT_EN);
165
166         /* set timeout to 4.1ms (max) for hardware debounce */
167         hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
168         hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
169
170         /* Toggle HPD circuit to trigger HPD sense */
171         hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
172                         ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
173         hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
174                         HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
175
176         return 0;
177
178 fail:
179         return ret;
180 }
181
182 static int hdp_disable(struct hdmi_connector *hdmi_connector)
183 {
184         struct hdmi *hdmi = hdmi_connector->hdmi;
185         const struct hdmi_platform_config *config = hdmi->config;
186         struct drm_device *dev = hdmi_connector->base.dev;
187         int i, ret = 0;
188
189         /* Disable HPD interrupt */
190         hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
191
192         hdmi_set_mode(hdmi, false);
193
194         for (i = 0; i < config->hpd_reg_cnt; i++) {
195                 ret = regulator_disable(hdmi->hpd_regs[i]);
196                 if (ret) {
197                         dev_err(dev->dev, "failed to disable hpd regulator: %s (%d)\n",
198                                         config->hpd_reg_names[i], ret);
199                         goto fail;
200                 }
201         }
202
203         for (i = 0; i < config->hpd_clk_cnt; i++)
204                 clk_disable_unprepare(hdmi->hpd_clks[i]);
205
206         ret = gpio_config(hdmi, false);
207         if (ret) {
208                 dev_err(dev->dev, "failed to unconfigure GPIOs: %d\n", ret);
209                 goto fail;
210         }
211
212         return 0;
213
214 fail:
215         return ret;
216 }
217
218 static void
219 hotplug_work(struct work_struct *work)
220 {
221         struct hdmi_connector *hdmi_connector =
222                 container_of(work, struct hdmi_connector, hpd_work);
223         struct drm_connector *connector = &hdmi_connector->base;
224         drm_helper_hpd_irq_event(connector->dev);
225 }
226
227 void hdmi_connector_irq(struct drm_connector *connector)
228 {
229         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
230         struct msm_drm_private *priv = connector->dev->dev_private;
231         struct hdmi *hdmi = hdmi_connector->hdmi;
232         uint32_t hpd_int_status, hpd_int_ctrl;
233
234         /* Process HPD: */
235         hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
236         hpd_int_ctrl   = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
237
238         if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
239                         (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
240                 bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
241
242                 DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
243
244                 /* ack the irq: */
245                 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
246                                 hpd_int_ctrl | HDMI_HPD_INT_CTRL_INT_ACK);
247
248                 /* detect disconnect if we are connected or visa versa: */
249                 hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
250                 if (!detected)
251                         hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
252                 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
253
254                 queue_work(priv->wq, &hdmi_connector->hpd_work);
255         }
256 }
257
258 static enum drm_connector_status detect_reg(struct hdmi *hdmi)
259 {
260         uint32_t hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
261         return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
262                         connector_status_connected : connector_status_disconnected;
263 }
264
265 static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
266 {
267         const struct hdmi_platform_config *config = hdmi->config;
268         return gpio_get_value(config->hpd_gpio) ?
269                         connector_status_connected :
270                         connector_status_disconnected;
271 }
272
273 static enum drm_connector_status hdmi_connector_detect(
274                 struct drm_connector *connector, bool force)
275 {
276         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
277         struct hdmi *hdmi = hdmi_connector->hdmi;
278         enum drm_connector_status stat_gpio, stat_reg;
279         int retry = 20;
280
281         do {
282                 stat_gpio = detect_gpio(hdmi);
283                 stat_reg  = detect_reg(hdmi);
284
285                 if (stat_gpio == stat_reg)
286                         break;
287
288                 mdelay(10);
289         } while (--retry);
290
291         /* the status we get from reading gpio seems to be more reliable,
292          * so trust that one the most if we didn't manage to get hdmi and
293          * gpio status to agree:
294          */
295         if (stat_gpio != stat_reg) {
296                 DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
297                 DBG("hpd gpio tells us: %d", stat_gpio);
298         }
299
300         return stat_gpio;
301 }
302
303 static void hdmi_connector_destroy(struct drm_connector *connector)
304 {
305         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
306
307         hdp_disable(hdmi_connector);
308
309         drm_connector_unregister(connector);
310         drm_connector_cleanup(connector);
311
312         hdmi_unreference(hdmi_connector->hdmi);
313
314         kfree(hdmi_connector);
315 }
316
317 static int hdmi_connector_get_modes(struct drm_connector *connector)
318 {
319         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
320         struct hdmi *hdmi = hdmi_connector->hdmi;
321         struct edid *edid;
322         uint32_t hdmi_ctrl;
323         int ret = 0;
324
325         hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
326         hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
327
328         edid = drm_get_edid(connector, hdmi->i2c);
329
330         hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
331
332         drm_mode_connector_update_edid_property(connector, edid);
333
334         if (edid) {
335                 ret = drm_add_edid_modes(connector, edid);
336                 kfree(edid);
337         }
338
339         return ret;
340 }
341
342 static int hdmi_connector_mode_valid(struct drm_connector *connector,
343                                  struct drm_display_mode *mode)
344 {
345         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
346         struct hdmi *hdmi = hdmi_connector->hdmi;
347         const struct hdmi_platform_config *config = hdmi->config;
348         struct msm_drm_private *priv = connector->dev->dev_private;
349         struct msm_kms *kms = priv->kms;
350         long actual, requested;
351
352         requested = 1000 * mode->clock;
353         actual = kms->funcs->round_pixclk(kms,
354                         requested, hdmi_connector->hdmi->encoder);
355
356         /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
357          * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
358          * instead):
359          */
360         if (config->pwr_clk_cnt > 0)
361                 actual = clk_round_rate(hdmi->pwr_clks[0], actual);
362
363         DBG("requested=%ld, actual=%ld", requested, actual);
364
365         if (actual != requested)
366                 return MODE_CLOCK_RANGE;
367
368         return 0;
369 }
370
371 static struct drm_encoder *
372 hdmi_connector_best_encoder(struct drm_connector *connector)
373 {
374         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
375         return hdmi_connector->hdmi->encoder;
376 }
377
378 static const struct drm_connector_funcs hdmi_connector_funcs = {
379         .dpms = drm_helper_connector_dpms,
380         .detect = hdmi_connector_detect,
381         .fill_modes = drm_helper_probe_single_connector_modes,
382         .destroy = hdmi_connector_destroy,
383 };
384
385 static const struct drm_connector_helper_funcs hdmi_connector_helper_funcs = {
386         .get_modes = hdmi_connector_get_modes,
387         .mode_valid = hdmi_connector_mode_valid,
388         .best_encoder = hdmi_connector_best_encoder,
389 };
390
391 /* initialize connector */
392 struct drm_connector *hdmi_connector_init(struct hdmi *hdmi)
393 {
394         struct drm_connector *connector = NULL;
395         struct hdmi_connector *hdmi_connector;
396         int ret;
397
398         hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
399         if (!hdmi_connector) {
400                 ret = -ENOMEM;
401                 goto fail;
402         }
403
404         hdmi_connector->hdmi = hdmi_reference(hdmi);
405         INIT_WORK(&hdmi_connector->hpd_work, hotplug_work);
406
407         connector = &hdmi_connector->base;
408
409         drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
410                         DRM_MODE_CONNECTOR_HDMIA);
411         drm_connector_helper_add(connector, &hdmi_connector_helper_funcs);
412
413         connector->polled = DRM_CONNECTOR_POLL_CONNECT |
414                         DRM_CONNECTOR_POLL_DISCONNECT;
415
416         connector->interlace_allowed = 1;
417         connector->doublescan_allowed = 0;
418
419         drm_connector_register(connector);
420
421         ret = hpd_enable(hdmi_connector);
422         if (ret) {
423                 dev_err(hdmi->dev->dev, "failed to enable HPD: %d\n", ret);
424                 goto fail;
425         }
426
427         drm_mode_connector_attach_encoder(connector, hdmi->encoder);
428
429         return connector;
430
431 fail:
432         if (connector)
433                 hdmi_connector_destroy(connector);
434
435         return ERR_PTR(ret);
436 }