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