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