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