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#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	struct msm_drm_private *priv = dev->dev_private;
164	int ret;
165
166	if (priv->num_bridges == ARRAY_SIZE(priv->bridges)) {
167		DRM_DEV_ERROR(dev->dev, "too many bridges\n");
168		return -ENOSPC;
169	}
170
171	hdmi->dev = dev;
172	hdmi->encoder = encoder;
173
174	hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
175
176	hdmi->bridge = msm_hdmi_bridge_init(hdmi);
177	if (IS_ERR(hdmi->bridge)) {
178		ret = PTR_ERR(hdmi->bridge);
179		DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
180		hdmi->bridge = NULL;
181		goto fail;
182	}
183
184	if (hdmi->next_bridge) {
185		ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
186					DRM_BRIDGE_ATTACH_NO_CONNECTOR);
187		if (ret) {
188			DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
189			goto fail;
190		}
191	}
192
193	hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
194	if (IS_ERR(hdmi->connector)) {
195		ret = PTR_ERR(hdmi->connector);
196		DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
197		hdmi->connector = NULL;
198		goto fail;
199	}
200
201	drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
202
203	ret = devm_request_irq(dev->dev, hdmi->irq,
204			msm_hdmi_irq, IRQF_TRIGGER_HIGH,
205			"hdmi_isr", hdmi);
206	if (ret < 0) {
207		DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
208				hdmi->irq, ret);
209		goto fail;
210	}
211
212	ret = msm_hdmi_hpd_enable(hdmi->bridge);
213	if (ret < 0) {
214		DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
215		goto fail;
216	}
217
218	priv->bridges[priv->num_bridges++]       = hdmi->bridge;
219
220	return 0;
221
222fail:
223	/* bridge is normally destroyed by drm: */
224	if (hdmi->bridge) {
225		msm_hdmi_bridge_destroy(hdmi->bridge);
226		hdmi->bridge = NULL;
227	}
228	if (hdmi->connector) {
229		hdmi->connector->funcs->destroy(hdmi->connector);
230		hdmi->connector = NULL;
231	}
232
233	return ret;
234}
235
236/*
237 * The hdmi device:
238 */
239
240#define HDMI_CFG(item, entry) \
241	.item ## _names = item ##_names_ ## entry, \
242	.item ## _cnt   = ARRAY_SIZE(item ## _names_ ## entry)
243
244static const char *hpd_reg_names_8960[] = {"core-vdda"};
245static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
246
247static const struct hdmi_platform_config hdmi_tx_8960_config = {
248		HDMI_CFG(hpd_reg, 8960),
249		HDMI_CFG(hpd_clk, 8960),
250};
251
252static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
253static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
254static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
255static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
256
257static const struct hdmi_platform_config hdmi_tx_8974_config = {
258		HDMI_CFG(pwr_reg, 8x74),
259		HDMI_CFG(pwr_clk, 8x74),
260		HDMI_CFG(hpd_clk, 8x74),
261		.hpd_freq      = hpd_clk_freq_8x74,
262};
263
264/*
265 * HDMI audio codec callbacks
266 */
267static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
268				    struct hdmi_codec_daifmt *daifmt,
269				    struct hdmi_codec_params *params)
270{
271	struct hdmi *hdmi = dev_get_drvdata(dev);
272	unsigned int chan;
273	unsigned int channel_allocation = 0;
274	unsigned int rate;
275	unsigned int level_shift  = 0; /* 0dB */
276	bool down_mix = false;
277
278	DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
279		 params->sample_width, params->cea.channels);
280
281	switch (params->cea.channels) {
282	case 2:
283		/* FR and FL speakers */
284		channel_allocation  = 0;
285		chan = MSM_HDMI_AUDIO_CHANNEL_2;
286		break;
287	case 4:
288		/* FC, LFE, FR and FL speakers */
289		channel_allocation  = 0x3;
290		chan = MSM_HDMI_AUDIO_CHANNEL_4;
291		break;
292	case 6:
293		/* RR, RL, FC, LFE, FR and FL speakers */
294		channel_allocation  = 0x0B;
295		chan = MSM_HDMI_AUDIO_CHANNEL_6;
296		break;
297	case 8:
298		/* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
299		channel_allocation  = 0x1F;
300		chan = MSM_HDMI_AUDIO_CHANNEL_8;
301		break;
302	default:
303		return -EINVAL;
304	}
305
306	switch (params->sample_rate) {
307	case 32000:
308		rate = HDMI_SAMPLE_RATE_32KHZ;
309		break;
310	case 44100:
311		rate = HDMI_SAMPLE_RATE_44_1KHZ;
312		break;
313	case 48000:
314		rate = HDMI_SAMPLE_RATE_48KHZ;
315		break;
316	case 88200:
317		rate = HDMI_SAMPLE_RATE_88_2KHZ;
318		break;
319	case 96000:
320		rate = HDMI_SAMPLE_RATE_96KHZ;
321		break;
322	case 176400:
323		rate = HDMI_SAMPLE_RATE_176_4KHZ;
324		break;
325	case 192000:
326		rate = HDMI_SAMPLE_RATE_192KHZ;
327		break;
328	default:
329		DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
330			params->sample_rate);
331		return -EINVAL;
332	}
333
334	msm_hdmi_audio_set_sample_rate(hdmi, rate);
335	msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
336			      level_shift, down_mix);
337
338	return 0;
339}
340
341static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
342{
343	struct hdmi *hdmi = dev_get_drvdata(dev);
344
345	msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
346}
347
348static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
349	.hw_params = msm_hdmi_audio_hw_params,
350	.audio_shutdown = msm_hdmi_audio_shutdown,
351};
352
353static struct hdmi_codec_pdata codec_data = {
354	.ops = &msm_hdmi_audio_codec_ops,
355	.max_i2s_channels = 8,
356	.i2s = 1,
357};
358
359static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
360{
361	hdmi->audio_pdev = platform_device_register_data(dev,
362							 HDMI_CODEC_DRV_NAME,
363							 PLATFORM_DEVID_AUTO,
364							 &codec_data,
365							 sizeof(codec_data));
366	return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
367}
368
369static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
370{
371	struct msm_drm_private *priv = dev_get_drvdata(master);
372	struct hdmi *hdmi = dev_get_drvdata(dev);
373	int err;
374
375	err = msm_hdmi_init(hdmi);
376	if (err)
377		return err;
378	priv->hdmi = hdmi;
379
380	err = msm_hdmi_register_audio_driver(hdmi, dev);
381	if (err) {
382		DRM_ERROR("Failed to attach an audio codec %d\n", err);
383		hdmi->audio_pdev = NULL;
384	}
385
386	return 0;
387}
388
389static void msm_hdmi_unbind(struct device *dev, struct device *master,
390		void *data)
391{
392	struct msm_drm_private *priv = dev_get_drvdata(master);
393
394	if (priv->hdmi) {
395		if (priv->hdmi->audio_pdev)
396			platform_device_unregister(priv->hdmi->audio_pdev);
397
398		msm_hdmi_destroy(priv->hdmi);
399		priv->hdmi = NULL;
400	}
401}
402
403static const struct component_ops msm_hdmi_ops = {
404		.bind   = msm_hdmi_bind,
405		.unbind = msm_hdmi_unbind,
406};
407
408static int msm_hdmi_dev_probe(struct platform_device *pdev)
409{
410	const struct hdmi_platform_config *config;
411	struct device *dev = &pdev->dev;
412	struct hdmi *hdmi;
413	struct resource *res;
414	int i, ret;
415
416	config = of_device_get_match_data(dev);
417	if (!config)
418		return -EINVAL;
419
420	hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
421	if (!hdmi)
422		return -ENOMEM;
423
424	hdmi->pdev = pdev;
425	hdmi->config = config;
426	spin_lock_init(&hdmi->reg_lock);
427
428	ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
429	if (ret && ret != -ENODEV)
430		return ret;
431
432	hdmi->mmio = msm_ioremap(pdev, "core_physical");
433	if (IS_ERR(hdmi->mmio))
434		return PTR_ERR(hdmi->mmio);
435
436	/* HDCP needs physical address of hdmi register */
437	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
438		"core_physical");
439	if (!res)
440		return -EINVAL;
441	hdmi->mmio_phy_addr = res->start;
442
443	hdmi->qfprom_mmio = msm_ioremap(pdev, "qfprom_physical");
444	if (IS_ERR(hdmi->qfprom_mmio)) {
445		DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
446		hdmi->qfprom_mmio = NULL;
447	}
448
449	hdmi->irq = platform_get_irq(pdev, 0);
450	if (hdmi->irq < 0)
451		return hdmi->irq;
452
453	hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
454				      config->hpd_reg_cnt,
455				      sizeof(hdmi->hpd_regs[0]),
456				      GFP_KERNEL);
457	if (!hdmi->hpd_regs)
458		return -ENOMEM;
459
460	for (i = 0; i < config->hpd_reg_cnt; i++)
461		hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
462
463	ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
464	if (ret)
465		return dev_err_probe(dev, ret, "failed to get hpd regulators\n");
466
467	hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
468				      config->pwr_reg_cnt,
469				      sizeof(hdmi->pwr_regs[0]),
470				      GFP_KERNEL);
471	if (!hdmi->pwr_regs)
472		return -ENOMEM;
473
474	for (i = 0; i < config->pwr_reg_cnt; i++)
475		hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
476
477	ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
478	if (ret)
479		return dev_err_probe(dev, ret, "failed to get pwr regulators\n");
480
481	hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
482				      config->hpd_clk_cnt,
483				      sizeof(hdmi->hpd_clks[0]),
484				      GFP_KERNEL);
485	if (!hdmi->hpd_clks)
486		return -ENOMEM;
487
488	for (i = 0; i < config->hpd_clk_cnt; i++) {
489		struct clk *clk;
490
491		clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
492		if (IS_ERR(clk))
493			return dev_err_probe(dev, PTR_ERR(clk),
494					     "failed to get hpd clk: %s\n",
495					     config->hpd_clk_names[i]);
496
497		hdmi->hpd_clks[i] = clk;
498	}
499
500	hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
501				      config->pwr_clk_cnt,
502				      sizeof(hdmi->pwr_clks[0]),
503				      GFP_KERNEL);
504	if (!hdmi->pwr_clks)
505		return -ENOMEM;
506
507	for (i = 0; i < config->pwr_clk_cnt; i++) {
508		struct clk *clk;
509
510		clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
511		if (IS_ERR(clk))
512			return dev_err_probe(dev, PTR_ERR(clk),
513					     "failed to get pwr clk: %s\n",
514					     config->pwr_clk_names[i]);
515
516		hdmi->pwr_clks[i] = clk;
517	}
518
519	hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
520	/* This will catch e.g. -EPROBE_DEFER */
521	if (IS_ERR(hdmi->hpd_gpiod))
522		return dev_err_probe(dev, PTR_ERR(hdmi->hpd_gpiod),
523				     "failed to get hpd gpio\n");
524
525	if (!hdmi->hpd_gpiod)
526		DBG("failed to get HPD gpio");
527
528	if (hdmi->hpd_gpiod)
529		gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
530
531	ret = msm_hdmi_get_phy(hdmi);
532	if (ret) {
533		DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
534		return ret;
535	}
536
537	ret = devm_pm_runtime_enable(&pdev->dev);
538	if (ret)
539		goto err_put_phy;
540
541	platform_set_drvdata(pdev, hdmi);
542
543	ret = component_add(&pdev->dev, &msm_hdmi_ops);
544	if (ret)
545		goto err_put_phy;
546
547	return 0;
548
549err_put_phy:
550	msm_hdmi_put_phy(hdmi);
551	return ret;
552}
553
554static int msm_hdmi_dev_remove(struct platform_device *pdev)
555{
556	struct hdmi *hdmi = dev_get_drvdata(&pdev->dev);
557
558	component_del(&pdev->dev, &msm_hdmi_ops);
559
560	msm_hdmi_put_phy(hdmi);
561
562	return 0;
563}
564
565static const struct of_device_id msm_hdmi_dt_match[] = {
566	{ .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
567	{ .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
568	{ .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
569	{ .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
570	{ .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
571	{ .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
572	{}
573};
574
575static struct platform_driver msm_hdmi_driver = {
576	.probe = msm_hdmi_dev_probe,
577	.remove = msm_hdmi_dev_remove,
578	.driver = {
579		.name = "hdmi_msm",
580		.of_match_table = msm_hdmi_dt_match,
581	},
582};
583
584void __init msm_hdmi_register(void)
585{
586	msm_hdmi_phy_driver_register();
587	platform_driver_register(&msm_hdmi_driver);
588}
589
590void __exit msm_hdmi_unregister(void)
591{
592	platform_driver_unregister(&msm_hdmi_driver);
593	msm_hdmi_phy_driver_unregister();
594}
595