]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/msm/hdmi/hdmi_connector.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 234
[linux.git] / drivers / gpu / drm / msm / hdmi / hdmi_connector.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6
7 #include <linux/gpio.h>
8 #include <linux/pinctrl/consumer.h>
9
10 #include "msm_kms.h"
11 #include "hdmi.h"
12
13 struct hdmi_connector {
14         struct drm_connector base;
15         struct hdmi *hdmi;
16         struct work_struct hpd_work;
17 };
18 #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
19
20 static void msm_hdmi_phy_reset(struct hdmi *hdmi)
21 {
22         unsigned int val;
23
24         val = hdmi_read(hdmi, REG_HDMI_PHY_CTRL);
25
26         if (val & HDMI_PHY_CTRL_SW_RESET_LOW) {
27                 /* pull low */
28                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
29                                 val & ~HDMI_PHY_CTRL_SW_RESET);
30         } else {
31                 /* pull high */
32                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
33                                 val | HDMI_PHY_CTRL_SW_RESET);
34         }
35
36         if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW) {
37                 /* pull low */
38                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
39                                 val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
40         } else {
41                 /* pull high */
42                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
43                                 val | HDMI_PHY_CTRL_SW_RESET_PLL);
44         }
45
46         msleep(100);
47
48         if (val & HDMI_PHY_CTRL_SW_RESET_LOW) {
49                 /* pull high */
50                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
51                                 val | HDMI_PHY_CTRL_SW_RESET);
52         } else {
53                 /* pull low */
54                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
55                                 val & ~HDMI_PHY_CTRL_SW_RESET);
56         }
57
58         if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW) {
59                 /* pull high */
60                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
61                                 val | HDMI_PHY_CTRL_SW_RESET_PLL);
62         } else {
63                 /* pull low */
64                 hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
65                                 val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
66         }
67 }
68
69 static int gpio_config(struct hdmi *hdmi, bool on)
70 {
71         struct device *dev = &hdmi->pdev->dev;
72         const struct hdmi_platform_config *config = hdmi->config;
73         int ret, i;
74
75         if (on) {
76                 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
77                         struct hdmi_gpio_data gpio = config->gpios[i];
78
79                         if (gpio.num != -1) {
80                                 ret = gpio_request(gpio.num, gpio.label);
81                                 if (ret) {
82                                         DRM_DEV_ERROR(dev,
83                                                 "'%s'(%d) gpio_request failed: %d\n",
84                                                 gpio.label, gpio.num, ret);
85                                         goto err;
86                                 }
87
88                                 if (gpio.output) {
89                                         gpio_direction_output(gpio.num,
90                                                               gpio.value);
91                                 } else {
92                                         gpio_direction_input(gpio.num);
93                                         gpio_set_value_cansleep(gpio.num,
94                                                                 gpio.value);
95                                 }
96                         }
97                 }
98
99                 DBG("gpio on");
100         } else {
101                 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
102                         struct hdmi_gpio_data gpio = config->gpios[i];
103
104                         if (gpio.num == -1)
105                                 continue;
106
107                         if (gpio.output) {
108                                 int value = gpio.value ? 0 : 1;
109
110                                 gpio_set_value_cansleep(gpio.num, value);
111                         }
112
113                         gpio_free(gpio.num);
114                 };
115
116                 DBG("gpio off");
117         }
118
119         return 0;
120 err:
121         while (i--) {
122                 if (config->gpios[i].num != -1)
123                         gpio_free(config->gpios[i].num);
124         }
125
126         return ret;
127 }
128
129 static void enable_hpd_clocks(struct hdmi *hdmi, bool enable)
130 {
131         const struct hdmi_platform_config *config = hdmi->config;
132         struct device *dev = &hdmi->pdev->dev;
133         int i, ret;
134
135         if (enable) {
136                 for (i = 0; i < config->hpd_clk_cnt; i++) {
137                         if (config->hpd_freq && config->hpd_freq[i]) {
138                                 ret = clk_set_rate(hdmi->hpd_clks[i],
139                                                    config->hpd_freq[i]);
140                                 if (ret)
141                                         dev_warn(dev,
142                                                  "failed to set clk %s (%d)\n",
143                                                  config->hpd_clk_names[i], ret);
144                         }
145
146                         ret = clk_prepare_enable(hdmi->hpd_clks[i]);
147                         if (ret) {
148                                 DRM_DEV_ERROR(dev,
149                                         "failed to enable hpd clk: %s (%d)\n",
150                                         config->hpd_clk_names[i], ret);
151                         }
152                 }
153         } else {
154                 for (i = config->hpd_clk_cnt - 1; i >= 0; i--)
155                         clk_disable_unprepare(hdmi->hpd_clks[i]);
156         }
157 }
158
159 int msm_hdmi_hpd_enable(struct drm_connector *connector)
160 {
161         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
162         struct hdmi *hdmi = hdmi_connector->hdmi;
163         const struct hdmi_platform_config *config = hdmi->config;
164         struct device *dev = &hdmi->pdev->dev;
165         uint32_t hpd_ctrl;
166         int i, ret;
167         unsigned long flags;
168
169         for (i = 0; i < config->hpd_reg_cnt; i++) {
170                 ret = regulator_enable(hdmi->hpd_regs[i]);
171                 if (ret) {
172                         DRM_DEV_ERROR(dev, "failed to enable hpd regulator: %s (%d)\n",
173                                         config->hpd_reg_names[i], ret);
174                         goto fail;
175                 }
176         }
177
178         ret = pinctrl_pm_select_default_state(dev);
179         if (ret) {
180                 DRM_DEV_ERROR(dev, "pinctrl state chg failed: %d\n", ret);
181                 goto fail;
182         }
183
184         ret = gpio_config(hdmi, true);
185         if (ret) {
186                 DRM_DEV_ERROR(dev, "failed to configure GPIOs: %d\n", ret);
187                 goto fail;
188         }
189
190         pm_runtime_get_sync(dev);
191         enable_hpd_clocks(hdmi, true);
192
193         msm_hdmi_set_mode(hdmi, false);
194         msm_hdmi_phy_reset(hdmi);
195         msm_hdmi_set_mode(hdmi, true);
196
197         hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
198
199         /* enable HPD events: */
200         hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
201                         HDMI_HPD_INT_CTRL_INT_CONNECT |
202                         HDMI_HPD_INT_CTRL_INT_EN);
203
204         /* set timeout to 4.1ms (max) for hardware debounce */
205         spin_lock_irqsave(&hdmi->reg_lock, flags);
206         hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
207         hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
208
209         /* Toggle HPD circuit to trigger HPD sense */
210         hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
211                         ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
212         hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
213                         HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
214         spin_unlock_irqrestore(&hdmi->reg_lock, flags);
215
216         return 0;
217
218 fail:
219         return ret;
220 }
221
222 static void hdp_disable(struct hdmi_connector *hdmi_connector)
223 {
224         struct hdmi *hdmi = hdmi_connector->hdmi;
225         const struct hdmi_platform_config *config = hdmi->config;
226         struct device *dev = &hdmi->pdev->dev;
227         int i, ret = 0;
228
229         /* Disable HPD interrupt */
230         hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
231
232         msm_hdmi_set_mode(hdmi, false);
233
234         enable_hpd_clocks(hdmi, false);
235         pm_runtime_put_autosuspend(dev);
236
237         ret = gpio_config(hdmi, false);
238         if (ret)
239                 dev_warn(dev, "failed to unconfigure GPIOs: %d\n", ret);
240
241         ret = pinctrl_pm_select_sleep_state(dev);
242         if (ret)
243                 dev_warn(dev, "pinctrl state chg failed: %d\n", ret);
244
245         for (i = 0; i < config->hpd_reg_cnt; i++) {
246                 ret = regulator_disable(hdmi->hpd_regs[i]);
247                 if (ret)
248                         dev_warn(dev, "failed to disable hpd regulator: %s (%d)\n",
249                                         config->hpd_reg_names[i], ret);
250         }
251 }
252
253 static void
254 msm_hdmi_hotplug_work(struct work_struct *work)
255 {
256         struct hdmi_connector *hdmi_connector =
257                 container_of(work, struct hdmi_connector, hpd_work);
258         struct drm_connector *connector = &hdmi_connector->base;
259         drm_helper_hpd_irq_event(connector->dev);
260 }
261
262 void msm_hdmi_connector_irq(struct drm_connector *connector)
263 {
264         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
265         struct hdmi *hdmi = hdmi_connector->hdmi;
266         uint32_t hpd_int_status, hpd_int_ctrl;
267
268         /* Process HPD: */
269         hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
270         hpd_int_ctrl   = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
271
272         if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
273                         (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
274                 bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
275
276                 /* ack & disable (temporarily) HPD events: */
277                 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
278                         HDMI_HPD_INT_CTRL_INT_ACK);
279
280                 DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
281
282                 /* detect disconnect if we are connected or visa versa: */
283                 hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
284                 if (!detected)
285                         hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
286                 hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
287
288                 queue_work(hdmi->workq, &hdmi_connector->hpd_work);
289         }
290 }
291
292 static enum drm_connector_status detect_reg(struct hdmi *hdmi)
293 {
294         uint32_t hpd_int_status;
295
296         pm_runtime_get_sync(&hdmi->pdev->dev);
297         enable_hpd_clocks(hdmi, true);
298
299         hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
300
301         enable_hpd_clocks(hdmi, false);
302         pm_runtime_put_autosuspend(&hdmi->pdev->dev);
303
304         return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
305                         connector_status_connected : connector_status_disconnected;
306 }
307
308 #define HPD_GPIO_INDEX  2
309 static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
310 {
311         const struct hdmi_platform_config *config = hdmi->config;
312         struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
313
314         return gpio_get_value(hpd_gpio.num) ?
315                         connector_status_connected :
316                         connector_status_disconnected;
317 }
318
319 static enum drm_connector_status hdmi_connector_detect(
320                 struct drm_connector *connector, bool force)
321 {
322         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
323         struct hdmi *hdmi = hdmi_connector->hdmi;
324         const struct hdmi_platform_config *config = hdmi->config;
325         struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
326         enum drm_connector_status stat_gpio, stat_reg;
327         int retry = 20;
328
329         /*
330          * some platforms may not have hpd gpio. Rely only on the status
331          * provided by REG_HDMI_HPD_INT_STATUS in this case.
332          */
333         if (hpd_gpio.num == -1)
334                 return detect_reg(hdmi);
335
336         do {
337                 stat_gpio = detect_gpio(hdmi);
338                 stat_reg  = detect_reg(hdmi);
339
340                 if (stat_gpio == stat_reg)
341                         break;
342
343                 mdelay(10);
344         } while (--retry);
345
346         /* the status we get from reading gpio seems to be more reliable,
347          * so trust that one the most if we didn't manage to get hdmi and
348          * gpio status to agree:
349          */
350         if (stat_gpio != stat_reg) {
351                 DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
352                 DBG("hpd gpio tells us: %d", stat_gpio);
353         }
354
355         return stat_gpio;
356 }
357
358 static void hdmi_connector_destroy(struct drm_connector *connector)
359 {
360         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
361
362         hdp_disable(hdmi_connector);
363
364         drm_connector_cleanup(connector);
365
366         kfree(hdmi_connector);
367 }
368
369 static int msm_hdmi_connector_get_modes(struct drm_connector *connector)
370 {
371         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
372         struct hdmi *hdmi = hdmi_connector->hdmi;
373         struct edid *edid;
374         uint32_t hdmi_ctrl;
375         int ret = 0;
376
377         hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
378         hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
379
380         edid = drm_get_edid(connector, hdmi->i2c);
381
382         hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
383
384         hdmi->hdmi_mode = drm_detect_hdmi_monitor(edid);
385         drm_connector_update_edid_property(connector, edid);
386
387         if (edid) {
388                 ret = drm_add_edid_modes(connector, edid);
389                 kfree(edid);
390         }
391
392         return ret;
393 }
394
395 static int msm_hdmi_connector_mode_valid(struct drm_connector *connector,
396                                  struct drm_display_mode *mode)
397 {
398         struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
399         struct hdmi *hdmi = hdmi_connector->hdmi;
400         const struct hdmi_platform_config *config = hdmi->config;
401         struct msm_drm_private *priv = connector->dev->dev_private;
402         struct msm_kms *kms = priv->kms;
403         long actual, requested;
404
405         requested = 1000 * mode->clock;
406         actual = kms->funcs->round_pixclk(kms,
407                         requested, hdmi_connector->hdmi->encoder);
408
409         /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
410          * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
411          * instead):
412          */
413         if (config->pwr_clk_cnt > 0)
414                 actual = clk_round_rate(hdmi->pwr_clks[0], actual);
415
416         DBG("requested=%ld, actual=%ld", requested, actual);
417
418         if (actual != requested)
419                 return MODE_CLOCK_RANGE;
420
421         return 0;
422 }
423
424 static const struct drm_connector_funcs hdmi_connector_funcs = {
425         .detect = hdmi_connector_detect,
426         .fill_modes = drm_helper_probe_single_connector_modes,
427         .destroy = hdmi_connector_destroy,
428         .reset = drm_atomic_helper_connector_reset,
429         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
430         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
431 };
432
433 static const struct drm_connector_helper_funcs msm_hdmi_connector_helper_funcs = {
434         .get_modes = msm_hdmi_connector_get_modes,
435         .mode_valid = msm_hdmi_connector_mode_valid,
436 };
437
438 /* initialize connector */
439 struct drm_connector *msm_hdmi_connector_init(struct hdmi *hdmi)
440 {
441         struct drm_connector *connector = NULL;
442         struct hdmi_connector *hdmi_connector;
443
444         hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
445         if (!hdmi_connector)
446                 return ERR_PTR(-ENOMEM);
447
448         hdmi_connector->hdmi = hdmi;
449         INIT_WORK(&hdmi_connector->hpd_work, msm_hdmi_hotplug_work);
450
451         connector = &hdmi_connector->base;
452
453         drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
454                         DRM_MODE_CONNECTOR_HDMIA);
455         drm_connector_helper_add(connector, &msm_hdmi_connector_helper_funcs);
456
457         connector->polled = DRM_CONNECTOR_POLL_CONNECT |
458                         DRM_CONNECTOR_POLL_DISCONNECT;
459
460         connector->interlace_allowed = 0;
461         connector->doublescan_allowed = 0;
462
463         drm_connector_attach_encoder(connector, hdmi->encoder);
464
465         return connector;
466 }