Merge tag 'drm-next-20221025' of git://linuxtv.org/pinchartl/media into drm-next
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / msm / hdmi / hdmi.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4  * Copyright (C) 2013 Red Hat
5  * Author: Rob Clark <robdclark@gmail.com>
6  */
7
8 #include <linux/of_irq.h>
9 #include <linux/of_gpio.h>
10
11 #include <drm/drm_bridge_connector.h>
12 #include <drm/drm_of.h>
13
14 #include <sound/hdmi-codec.h>
15 #include "hdmi.h"
16
17 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
18 {
19         uint32_t ctrl = 0;
20         unsigned long flags;
21
22         spin_lock_irqsave(&hdmi->reg_lock, flags);
23         if (power_on) {
24                 ctrl |= HDMI_CTRL_ENABLE;
25                 if (!hdmi->hdmi_mode) {
26                         ctrl |= HDMI_CTRL_HDMI;
27                         hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
28                         ctrl &= ~HDMI_CTRL_HDMI;
29                 } else {
30                         ctrl |= HDMI_CTRL_HDMI;
31                 }
32         } else {
33                 ctrl = HDMI_CTRL_HDMI;
34         }
35
36         hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
37         spin_unlock_irqrestore(&hdmi->reg_lock, flags);
38         DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
39                         power_on ? "Enable" : "Disable", ctrl);
40 }
41
42 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
43 {
44         struct hdmi *hdmi = dev_id;
45
46         /* Process HPD: */
47         msm_hdmi_hpd_irq(hdmi->bridge);
48
49         /* Process DDC: */
50         msm_hdmi_i2c_irq(hdmi->i2c);
51
52         /* Process HDCP: */
53         if (hdmi->hdcp_ctrl)
54                 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
55
56         /* TODO audio.. */
57
58         return IRQ_HANDLED;
59 }
60
61 static void msm_hdmi_destroy(struct hdmi *hdmi)
62 {
63         /*
64          * at this point, hpd has been disabled,
65          * after flush workq, it's safe to deinit hdcp
66          */
67         if (hdmi->workq)
68                 destroy_workqueue(hdmi->workq);
69         msm_hdmi_hdcp_destroy(hdmi);
70
71         if (hdmi->phy_dev) {
72                 put_device(hdmi->phy_dev);
73                 hdmi->phy = NULL;
74                 hdmi->phy_dev = NULL;
75         }
76
77         if (hdmi->i2c)
78                 msm_hdmi_i2c_destroy(hdmi->i2c);
79
80         platform_set_drvdata(hdmi->pdev, NULL);
81 }
82
83 static int msm_hdmi_get_phy(struct hdmi *hdmi)
84 {
85         struct platform_device *pdev = hdmi->pdev;
86         struct platform_device *phy_pdev;
87         struct device_node *phy_node;
88
89         phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
90         if (!phy_node) {
91                 DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
92                 return -ENXIO;
93         }
94
95         phy_pdev = of_find_device_by_node(phy_node);
96         if (phy_pdev)
97                 hdmi->phy = platform_get_drvdata(phy_pdev);
98
99         of_node_put(phy_node);
100
101         if (!phy_pdev) {
102                 DRM_DEV_ERROR(&pdev->dev, "phy driver is not ready\n");
103                 return -EPROBE_DEFER;
104         }
105         if (!hdmi->phy) {
106                 DRM_DEV_ERROR(&pdev->dev, "phy driver is not ready\n");
107                 put_device(&phy_pdev->dev);
108                 return -EPROBE_DEFER;
109         }
110
111         hdmi->phy_dev = get_device(&phy_pdev->dev);
112
113         return 0;
114 }
115
116 /* construct hdmi at bind/probe time, grab all the resources.  If
117  * we are to EPROBE_DEFER we want to do it here, rather than later
118  * at modeset_init() time
119  */
120 static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
121 {
122         struct hdmi_platform_config *config = pdev->dev.platform_data;
123         struct hdmi *hdmi = NULL;
124         struct resource *res;
125         int i, ret;
126
127         hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
128         if (!hdmi) {
129                 ret = -ENOMEM;
130                 goto fail;
131         }
132
133         hdmi->pdev = pdev;
134         hdmi->config = config;
135         spin_lock_init(&hdmi->reg_lock);
136
137         ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
138         if (ret && ret != -ENODEV)
139                 goto fail;
140
141         hdmi->mmio = msm_ioremap(pdev, config->mmio_name);
142         if (IS_ERR(hdmi->mmio)) {
143                 ret = PTR_ERR(hdmi->mmio);
144                 goto fail;
145         }
146
147         /* HDCP needs physical address of hdmi register */
148         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
149                 config->mmio_name);
150         if (!res) {
151                 ret = -EINVAL;
152                 goto fail;
153         }
154         hdmi->mmio_phy_addr = res->start;
155
156         hdmi->qfprom_mmio = msm_ioremap(pdev, config->qfprom_mmio_name);
157         if (IS_ERR(hdmi->qfprom_mmio)) {
158                 DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
159                 hdmi->qfprom_mmio = NULL;
160         }
161
162         hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
163                                       config->hpd_reg_cnt,
164                                       sizeof(hdmi->hpd_regs[0]),
165                                       GFP_KERNEL);
166         if (!hdmi->hpd_regs) {
167                 ret = -ENOMEM;
168                 goto fail;
169         }
170         for (i = 0; i < config->hpd_reg_cnt; i++)
171                 hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
172
173         ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
174         if (ret) {
175                 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd regulator: %d\n", ret);
176                 goto fail;
177         }
178
179         hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
180                                       config->pwr_reg_cnt,
181                                       sizeof(hdmi->pwr_regs[0]),
182                                       GFP_KERNEL);
183         if (!hdmi->pwr_regs) {
184                 ret = -ENOMEM;
185                 goto fail;
186         }
187
188         for (i = 0; i < config->pwr_reg_cnt; i++)
189                 hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
190
191         ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
192         if (ret) {
193                 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr regulator: %d\n", ret);
194                 goto fail;
195         }
196
197         hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
198                                       config->hpd_clk_cnt,
199                                       sizeof(hdmi->hpd_clks[0]),
200                                       GFP_KERNEL);
201         if (!hdmi->hpd_clks) {
202                 ret = -ENOMEM;
203                 goto fail;
204         }
205         for (i = 0; i < config->hpd_clk_cnt; i++) {
206                 struct clk *clk;
207
208                 clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
209                 if (IS_ERR(clk)) {
210                         ret = PTR_ERR(clk);
211                         DRM_DEV_ERROR(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
212                                         config->hpd_clk_names[i], ret);
213                         goto fail;
214                 }
215
216                 hdmi->hpd_clks[i] = clk;
217         }
218
219         hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
220                                       config->pwr_clk_cnt,
221                                       sizeof(hdmi->pwr_clks[0]),
222                                       GFP_KERNEL);
223         if (!hdmi->pwr_clks) {
224                 ret = -ENOMEM;
225                 goto fail;
226         }
227         for (i = 0; i < config->pwr_clk_cnt; i++) {
228                 struct clk *clk;
229
230                 clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
231                 if (IS_ERR(clk)) {
232                         ret = PTR_ERR(clk);
233                         DRM_DEV_ERROR(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
234                                         config->pwr_clk_names[i], ret);
235                         goto fail;
236                 }
237
238                 hdmi->pwr_clks[i] = clk;
239         }
240
241         hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
242         /* This will catch e.g. -EPROBE_DEFER */
243         if (IS_ERR(hdmi->hpd_gpiod)) {
244                 ret = PTR_ERR(hdmi->hpd_gpiod);
245                 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd gpio: (%d)\n", ret);
246                 goto fail;
247         }
248
249         if (!hdmi->hpd_gpiod)
250                 DBG("failed to get HPD gpio");
251
252         if (hdmi->hpd_gpiod)
253                 gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
254
255         pm_runtime_enable(&pdev->dev);
256
257         hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
258
259         hdmi->i2c = msm_hdmi_i2c_init(hdmi);
260         if (IS_ERR(hdmi->i2c)) {
261                 ret = PTR_ERR(hdmi->i2c);
262                 DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
263                 hdmi->i2c = NULL;
264                 goto fail;
265         }
266
267         ret = msm_hdmi_get_phy(hdmi);
268         if (ret) {
269                 DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
270                 goto fail;
271         }
272
273         hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
274         if (IS_ERR(hdmi->hdcp_ctrl)) {
275                 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
276                 hdmi->hdcp_ctrl = NULL;
277         }
278
279         return hdmi;
280
281 fail:
282         if (hdmi)
283                 msm_hdmi_destroy(hdmi);
284
285         return ERR_PTR(ret);
286 }
287
288 /* Second part of initialization, the drm/kms level modeset_init,
289  * constructs/initializes mode objects, etc, is called from master
290  * driver (not hdmi sub-device's probe/bind!)
291  *
292  * Any resource (regulator/clk/etc) which could be missing at boot
293  * should be handled in msm_hdmi_init() so that failure happens from
294  * hdmi sub-device's probe.
295  */
296 int msm_hdmi_modeset_init(struct hdmi *hdmi,
297                 struct drm_device *dev, struct drm_encoder *encoder)
298 {
299         struct msm_drm_private *priv = dev->dev_private;
300         struct platform_device *pdev = hdmi->pdev;
301         int ret;
302
303         hdmi->dev = dev;
304         hdmi->encoder = encoder;
305
306         hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
307
308         hdmi->bridge = msm_hdmi_bridge_init(hdmi);
309         if (IS_ERR(hdmi->bridge)) {
310                 ret = PTR_ERR(hdmi->bridge);
311                 DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
312                 hdmi->bridge = NULL;
313                 goto fail;
314         }
315
316         if (hdmi->next_bridge) {
317                 ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
318                                         DRM_BRIDGE_ATTACH_NO_CONNECTOR);
319                 if (ret) {
320                         DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
321                         goto fail;
322                 }
323         }
324
325         hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
326         if (IS_ERR(hdmi->connector)) {
327                 ret = PTR_ERR(hdmi->connector);
328                 DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
329                 hdmi->connector = NULL;
330                 goto fail;
331         }
332
333         drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
334
335         hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
336         if (!hdmi->irq) {
337                 ret = -EINVAL;
338                 DRM_DEV_ERROR(dev->dev, "failed to get irq\n");
339                 goto fail;
340         }
341
342         ret = devm_request_irq(&pdev->dev, hdmi->irq,
343                         msm_hdmi_irq, IRQF_TRIGGER_HIGH,
344                         "hdmi_isr", hdmi);
345         if (ret < 0) {
346                 DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
347                                 hdmi->irq, ret);
348                 goto fail;
349         }
350
351         drm_bridge_connector_enable_hpd(hdmi->connector);
352
353         ret = msm_hdmi_hpd_enable(hdmi->bridge);
354         if (ret < 0) {
355                 DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
356                 goto fail;
357         }
358
359         priv->bridges[priv->num_bridges++]       = hdmi->bridge;
360
361         platform_set_drvdata(pdev, hdmi);
362
363         return 0;
364
365 fail:
366         /* bridge is normally destroyed by drm: */
367         if (hdmi->bridge) {
368                 msm_hdmi_bridge_destroy(hdmi->bridge);
369                 hdmi->bridge = NULL;
370         }
371         if (hdmi->connector) {
372                 hdmi->connector->funcs->destroy(hdmi->connector);
373                 hdmi->connector = NULL;
374         }
375
376         return ret;
377 }
378
379 /*
380  * The hdmi device:
381  */
382
383 #define HDMI_CFG(item, entry) \
384         .item ## _names = item ##_names_ ## entry, \
385         .item ## _cnt   = ARRAY_SIZE(item ## _names_ ## entry)
386
387 static const char *hpd_reg_names_8960[] = {"core-vdda"};
388 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
389
390 static struct hdmi_platform_config hdmi_tx_8960_config = {
391                 HDMI_CFG(hpd_reg, 8960),
392                 HDMI_CFG(hpd_clk, 8960),
393 };
394
395 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
396 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
397 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
398 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
399
400 static struct hdmi_platform_config hdmi_tx_8974_config = {
401                 HDMI_CFG(pwr_reg, 8x74),
402                 HDMI_CFG(pwr_clk, 8x74),
403                 HDMI_CFG(hpd_clk, 8x74),
404                 .hpd_freq      = hpd_clk_freq_8x74,
405 };
406
407 /*
408  * HDMI audio codec callbacks
409  */
410 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
411                                     struct hdmi_codec_daifmt *daifmt,
412                                     struct hdmi_codec_params *params)
413 {
414         struct hdmi *hdmi = dev_get_drvdata(dev);
415         unsigned int chan;
416         unsigned int channel_allocation = 0;
417         unsigned int rate;
418         unsigned int level_shift  = 0; /* 0dB */
419         bool down_mix = false;
420
421         DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
422                  params->sample_width, params->cea.channels);
423
424         switch (params->cea.channels) {
425         case 2:
426                 /* FR and FL speakers */
427                 channel_allocation  = 0;
428                 chan = MSM_HDMI_AUDIO_CHANNEL_2;
429                 break;
430         case 4:
431                 /* FC, LFE, FR and FL speakers */
432                 channel_allocation  = 0x3;
433                 chan = MSM_HDMI_AUDIO_CHANNEL_4;
434                 break;
435         case 6:
436                 /* RR, RL, FC, LFE, FR and FL speakers */
437                 channel_allocation  = 0x0B;
438                 chan = MSM_HDMI_AUDIO_CHANNEL_6;
439                 break;
440         case 8:
441                 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
442                 channel_allocation  = 0x1F;
443                 chan = MSM_HDMI_AUDIO_CHANNEL_8;
444                 break;
445         default:
446                 return -EINVAL;
447         }
448
449         switch (params->sample_rate) {
450         case 32000:
451                 rate = HDMI_SAMPLE_RATE_32KHZ;
452                 break;
453         case 44100:
454                 rate = HDMI_SAMPLE_RATE_44_1KHZ;
455                 break;
456         case 48000:
457                 rate = HDMI_SAMPLE_RATE_48KHZ;
458                 break;
459         case 88200:
460                 rate = HDMI_SAMPLE_RATE_88_2KHZ;
461                 break;
462         case 96000:
463                 rate = HDMI_SAMPLE_RATE_96KHZ;
464                 break;
465         case 176400:
466                 rate = HDMI_SAMPLE_RATE_176_4KHZ;
467                 break;
468         case 192000:
469                 rate = HDMI_SAMPLE_RATE_192KHZ;
470                 break;
471         default:
472                 DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
473                         params->sample_rate);
474                 return -EINVAL;
475         }
476
477         msm_hdmi_audio_set_sample_rate(hdmi, rate);
478         msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
479                               level_shift, down_mix);
480
481         return 0;
482 }
483
484 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
485 {
486         struct hdmi *hdmi = dev_get_drvdata(dev);
487
488         msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
489 }
490
491 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
492         .hw_params = msm_hdmi_audio_hw_params,
493         .audio_shutdown = msm_hdmi_audio_shutdown,
494 };
495
496 static struct hdmi_codec_pdata codec_data = {
497         .ops = &msm_hdmi_audio_codec_ops,
498         .max_i2s_channels = 8,
499         .i2s = 1,
500 };
501
502 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
503 {
504         hdmi->audio_pdev = platform_device_register_data(dev,
505                                                          HDMI_CODEC_DRV_NAME,
506                                                          PLATFORM_DEVID_AUTO,
507                                                          &codec_data,
508                                                          sizeof(codec_data));
509         return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
510 }
511
512 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
513 {
514         struct msm_drm_private *priv = dev_get_drvdata(master);
515         struct hdmi_platform_config *hdmi_cfg;
516         struct hdmi *hdmi;
517         struct device_node *of_node = dev->of_node;
518         int err;
519
520         hdmi_cfg = (struct hdmi_platform_config *)
521                         of_device_get_match_data(dev);
522         if (!hdmi_cfg) {
523                 DRM_DEV_ERROR(dev, "unknown hdmi_cfg: %pOFn\n", of_node);
524                 return -ENXIO;
525         }
526
527         hdmi_cfg->mmio_name     = "core_physical";
528         hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
529
530         dev->platform_data = hdmi_cfg;
531
532         hdmi = msm_hdmi_init(to_platform_device(dev));
533         if (IS_ERR(hdmi))
534                 return PTR_ERR(hdmi);
535         priv->hdmi = hdmi;
536
537         err = msm_hdmi_register_audio_driver(hdmi, dev);
538         if (err) {
539                 DRM_ERROR("Failed to attach an audio codec %d\n", err);
540                 hdmi->audio_pdev = NULL;
541         }
542
543         return 0;
544 }
545
546 static void msm_hdmi_unbind(struct device *dev, struct device *master,
547                 void *data)
548 {
549         struct msm_drm_private *priv = dev_get_drvdata(master);
550
551         if (priv->hdmi) {
552                 if (priv->hdmi->audio_pdev)
553                         platform_device_unregister(priv->hdmi->audio_pdev);
554
555                 msm_hdmi_destroy(priv->hdmi);
556                 priv->hdmi = NULL;
557         }
558 }
559
560 static const struct component_ops msm_hdmi_ops = {
561                 .bind   = msm_hdmi_bind,
562                 .unbind = msm_hdmi_unbind,
563 };
564
565 static int msm_hdmi_dev_probe(struct platform_device *pdev)
566 {
567         return component_add(&pdev->dev, &msm_hdmi_ops);
568 }
569
570 static int msm_hdmi_dev_remove(struct platform_device *pdev)
571 {
572         component_del(&pdev->dev, &msm_hdmi_ops);
573         return 0;
574 }
575
576 static const struct of_device_id msm_hdmi_dt_match[] = {
577         { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
578         { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
579         { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
580         { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
581         { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
582         { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
583         {}
584 };
585
586 static struct platform_driver msm_hdmi_driver = {
587         .probe = msm_hdmi_dev_probe,
588         .remove = msm_hdmi_dev_remove,
589         .driver = {
590                 .name = "hdmi_msm",
591                 .of_match_table = msm_hdmi_dt_match,
592         },
593 };
594
595 void __init msm_hdmi_register(void)
596 {
597         msm_hdmi_phy_driver_register();
598         platform_driver_register(&msm_hdmi_driver);
599 }
600
601 void __exit msm_hdmi_unregister(void)
602 {
603         platform_driver_unregister(&msm_hdmi_driver);
604         msm_hdmi_phy_driver_unregister();
605 }