1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dw-hdmi-i2s-audio.c
4  *
5  * Copyright (c) 2017 Renesas Solutions Corp.
6  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7  */
8 #include <drm/bridge/dw_hdmi.h>
9 
10 #include <sound/hdmi-codec.h>
11 
12 #include "dw-hdmi.h"
13 #include "dw-hdmi-audio.h"
14 
15 #define DRIVER_NAME "dw-hdmi-i2s-audio"
16 
17 static inline void hdmi_write(struct dw_hdmi_i2s_audio_data *audio,
18 			      u8 val, int offset)
19 {
20 	struct dw_hdmi *hdmi = audio->hdmi;
21 
22 	audio->write(hdmi, val, offset);
23 }
24 
25 static inline u8 hdmi_read(struct dw_hdmi_i2s_audio_data *audio, int offset)
26 {
27 	struct dw_hdmi *hdmi = audio->hdmi;
28 
29 	return audio->read(hdmi, offset);
30 }
31 
32 static int dw_hdmi_i2s_hw_params(struct device *dev, void *data,
33 				 struct hdmi_codec_daifmt *fmt,
34 				 struct hdmi_codec_params *hparms)
35 {
36 	struct dw_hdmi_i2s_audio_data *audio = data;
37 	struct dw_hdmi *hdmi = audio->hdmi;
38 	u8 conf0 = 0;
39 	u8 conf1 = 0;
40 	u8 inputclkfs = 0;
41 
42 	/* it cares I2S only */
43 	if ((fmt->fmt != HDMI_I2S) ||
44 	    (fmt->bit_clk_master | fmt->frame_clk_master)) {
45 		dev_err(dev, "unsupported format/settings\n");
46 		return -EINVAL;
47 	}
48 
49 	inputclkfs	= HDMI_AUD_INPUTCLKFS_64FS;
50 	conf0		= HDMI_AUD_CONF0_I2S_ALL_ENABLE;
51 
52 	switch (hparms->sample_width) {
53 	case 16:
54 		conf1 = HDMI_AUD_CONF1_WIDTH_16;
55 		break;
56 	case 24:
57 	case 32:
58 		conf1 = HDMI_AUD_CONF1_WIDTH_24;
59 		break;
60 	}
61 
62 	dw_hdmi_set_sample_rate(hdmi, hparms->sample_rate);
63 
64 	hdmi_write(audio, inputclkfs, HDMI_AUD_INPUTCLKFS);
65 	hdmi_write(audio, conf0, HDMI_AUD_CONF0);
66 	hdmi_write(audio, conf1, HDMI_AUD_CONF1);
67 
68 	dw_hdmi_audio_enable(hdmi);
69 
70 	return 0;
71 }
72 
73 static void dw_hdmi_i2s_audio_shutdown(struct device *dev, void *data)
74 {
75 	struct dw_hdmi_i2s_audio_data *audio = data;
76 	struct dw_hdmi *hdmi = audio->hdmi;
77 
78 	dw_hdmi_audio_disable(hdmi);
79 
80 	hdmi_write(audio, HDMI_AUD_CONF0_SW_RESET, HDMI_AUD_CONF0);
81 }
82 
83 static int dw_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
84 				  struct device_node *endpoint)
85 {
86 	struct of_endpoint of_ep;
87 	int ret;
88 
89 	ret = of_graph_parse_endpoint(endpoint, &of_ep);
90 	if (ret < 0)
91 		return ret;
92 
93 	/*
94 	 * HDMI sound should be located as reg = <2>
95 	 * Then, it is sound port 0
96 	 */
97 	if (of_ep.port == 2)
98 		return 0;
99 
100 	return -EINVAL;
101 }
102 
103 static struct hdmi_codec_ops dw_hdmi_i2s_ops = {
104 	.hw_params	= dw_hdmi_i2s_hw_params,
105 	.audio_shutdown	= dw_hdmi_i2s_audio_shutdown,
106 	.get_dai_id	= dw_hdmi_i2s_get_dai_id,
107 };
108 
109 static int snd_dw_hdmi_probe(struct platform_device *pdev)
110 {
111 	struct dw_hdmi_i2s_audio_data *audio = pdev->dev.platform_data;
112 	struct platform_device_info pdevinfo;
113 	struct hdmi_codec_pdata pdata;
114 	struct platform_device *platform;
115 
116 	pdata.ops		= &dw_hdmi_i2s_ops;
117 	pdata.i2s		= 1;
118 	pdata.max_i2s_channels	= 6;
119 	pdata.data		= audio;
120 
121 	memset(&pdevinfo, 0, sizeof(pdevinfo));
122 	pdevinfo.parent		= pdev->dev.parent;
123 	pdevinfo.id		= PLATFORM_DEVID_AUTO;
124 	pdevinfo.name		= HDMI_CODEC_DRV_NAME;
125 	pdevinfo.data		= &pdata;
126 	pdevinfo.size_data	= sizeof(pdata);
127 	pdevinfo.dma_mask	= DMA_BIT_MASK(32);
128 
129 	platform = platform_device_register_full(&pdevinfo);
130 	if (IS_ERR(platform))
131 		return PTR_ERR(platform);
132 
133 	dev_set_drvdata(&pdev->dev, platform);
134 
135 	return 0;
136 }
137 
138 static int snd_dw_hdmi_remove(struct platform_device *pdev)
139 {
140 	struct platform_device *platform = dev_get_drvdata(&pdev->dev);
141 
142 	platform_device_unregister(platform);
143 
144 	return 0;
145 }
146 
147 static struct platform_driver snd_dw_hdmi_driver = {
148 	.probe	= snd_dw_hdmi_probe,
149 	.remove	= snd_dw_hdmi_remove,
150 	.driver	= {
151 		.name = DRIVER_NAME,
152 	},
153 };
154 module_platform_driver(snd_dw_hdmi_driver);
155 
156 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
157 MODULE_DESCRIPTION("Synopsis Designware HDMI I2S ALSA SoC interface");
158 MODULE_LICENSE("GPL v2");
159 MODULE_ALIAS("platform:" DRIVER_NAME);
160