xref: /linux/sound/soc/fsl/imx-hdmi.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2017-2020 NXP
3 
4 #include <linux/module.h>
5 #include <linux/of_platform.h>
6 #include <sound/jack.h>
7 #include <sound/pcm_params.h>
8 #include <sound/hdmi-codec.h>
9 #include "fsl_sai.h"
10 
11 /**
12  * struct cpu_priv - CPU private data
13  * @sysclk_id: SYSCLK ids for set_sysclk()
14  * @slot_width: Slot width of each frame
15  *
16  * Note: [1] for tx and [0] for rx
17  */
18 struct cpu_priv {
19 	u32 sysclk_id[2];
20 	u32 slot_width;
21 };
22 
23 struct imx_hdmi_data {
24 	struct snd_soc_dai_link dai;
25 	struct snd_soc_card card;
26 	struct snd_soc_jack hdmi_jack;
27 	struct snd_soc_jack_pin hdmi_jack_pin;
28 	struct cpu_priv cpu_priv;
29 	u32 dai_fmt;
30 };
31 
32 static int imx_hdmi_hw_params(struct snd_pcm_substream *substream,
33 			      struct snd_pcm_hw_params *params)
34 {
35 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
36 	struct imx_hdmi_data *data = snd_soc_card_get_drvdata(rtd->card);
37 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
38 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
39 	struct snd_soc_card *card = rtd->card;
40 	struct device *dev = card->dev;
41 	u32 slot_width = data->cpu_priv.slot_width;
42 	int ret;
43 
44 	/* MCLK always is (256 or 192) * rate. */
45 	ret = snd_soc_dai_set_sysclk(cpu_dai, data->cpu_priv.sysclk_id[tx],
46 				     8 * slot_width * params_rate(params),
47 				     tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN);
48 	if (ret && ret != -ENOTSUPP) {
49 		dev_err(dev, "failed to set cpu sysclk: %d\n", ret);
50 		return ret;
51 	}
52 
53 	ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0, 0, 2, slot_width);
54 	if (ret && ret != -ENOTSUPP) {
55 		dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
56 		return ret;
57 	}
58 
59 	return 0;
60 }
61 
62 static const struct snd_soc_ops imx_hdmi_ops = {
63 	.hw_params = imx_hdmi_hw_params,
64 };
65 
66 static const struct snd_soc_dapm_widget imx_hdmi_widgets[] = {
67 	SND_SOC_DAPM_LINE("HDMI Jack", NULL),
68 };
69 
70 static int imx_hdmi_init(struct snd_soc_pcm_runtime *rtd)
71 {
72 	struct snd_soc_card *card = rtd->card;
73 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
74 	struct snd_soc_component *component = codec_dai->component;
75 	struct imx_hdmi_data *data = snd_soc_card_get_drvdata(card);
76 	int ret;
77 
78 	data->hdmi_jack_pin.pin = "HDMI Jack";
79 	data->hdmi_jack_pin.mask = SND_JACK_LINEOUT;
80 	/* enable jack detection */
81 	ret = snd_soc_card_jack_new_pins(card, "HDMI Jack", SND_JACK_LINEOUT,
82 					 &data->hdmi_jack,
83 					 &data->hdmi_jack_pin, 1);
84 	if (ret) {
85 		dev_err(card->dev, "Can't new HDMI Jack %d\n", ret);
86 		return ret;
87 	}
88 
89 	ret = snd_soc_component_set_jack(component, &data->hdmi_jack, NULL);
90 	if (ret && ret != -ENOTSUPP) {
91 		dev_err(card->dev, "Can't set HDMI Jack %d\n", ret);
92 		return ret;
93 	}
94 
95 	return 0;
96 };
97 
98 static int imx_hdmi_probe(struct platform_device *pdev)
99 {
100 	struct device_node *np = pdev->dev.of_node;
101 	bool hdmi_out = of_property_read_bool(np, "hdmi-out");
102 	bool hdmi_in = of_property_read_bool(np, "hdmi-in");
103 	struct snd_soc_dai_link_component *dlc;
104 	struct device_node *cpu_np;
105 	struct imx_hdmi_data *data;
106 	int ret;
107 
108 	dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
109 	if (!dlc)
110 		return -ENOMEM;
111 
112 	cpu_np = of_parse_phandle(np, "audio-cpu", 0);
113 	if (!cpu_np) {
114 		dev_err(&pdev->dev, "cpu dai phandle missing or invalid\n");
115 		ret = -EINVAL;
116 		goto fail;
117 	}
118 
119 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
120 	if (!data) {
121 		ret = -ENOMEM;
122 		goto fail;
123 	}
124 
125 	data->dai.cpus = &dlc[0];
126 	data->dai.num_cpus = 1;
127 	data->dai.platforms = &dlc[1];
128 	data->dai.num_platforms = 1;
129 	data->dai.codecs = &dlc[2];
130 	data->dai.num_codecs = 1;
131 
132 	data->dai.name = "i.MX HDMI";
133 	data->dai.stream_name = "i.MX HDMI";
134 	data->dai.cpus->of_node = cpu_np;
135 	data->dai.platforms->of_node = cpu_np;
136 	data->dai.ops = &imx_hdmi_ops;
137 	data->dai.playback_only = true;
138 	data->dai.capture_only = false;
139 	data->dai.init = imx_hdmi_init;
140 
141 	if (of_node_name_eq(cpu_np, "sai")) {
142 		data->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
143 		data->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
144 	}
145 
146 	if (of_device_is_compatible(np, "fsl,imx-audio-sii902x")) {
147 		data->dai_fmt = SND_SOC_DAIFMT_LEFT_J;
148 		data->cpu_priv.slot_width = 24;
149 	} else {
150 		data->dai_fmt = SND_SOC_DAIFMT_I2S;
151 		data->cpu_priv.slot_width = 32;
152 	}
153 
154 	if ((hdmi_out && hdmi_in) || (!hdmi_out && !hdmi_in)) {
155 		dev_err(&pdev->dev, "Invalid HDMI DAI link\n");
156 		ret = -EINVAL;
157 		goto fail;
158 	}
159 
160 	if (hdmi_out) {
161 		data->dai.playback_only = true;
162 		data->dai.capture_only = false;
163 		data->dai.codecs->dai_name = "i2s-hifi";
164 		data->dai.codecs->name = "hdmi-audio-codec.1";
165 		data->dai.dai_fmt = data->dai_fmt |
166 				    SND_SOC_DAIFMT_NB_NF |
167 				    SND_SOC_DAIFMT_CBC_CFC;
168 	}
169 
170 	if (hdmi_in) {
171 		data->dai.playback_only = false;
172 		data->dai.capture_only = true;
173 		data->dai.codecs->dai_name = "i2s-hifi";
174 		data->dai.codecs->name = "hdmi-audio-codec.2";
175 		data->dai.dai_fmt = data->dai_fmt |
176 				    SND_SOC_DAIFMT_NB_NF |
177 				    SND_SOC_DAIFMT_CBP_CFP;
178 	}
179 
180 	data->card.dapm_widgets = imx_hdmi_widgets;
181 	data->card.num_dapm_widgets = ARRAY_SIZE(imx_hdmi_widgets);
182 	data->card.dev = &pdev->dev;
183 	data->card.owner = THIS_MODULE;
184 	ret = snd_soc_of_parse_card_name(&data->card, "model");
185 	if (ret)
186 		goto fail;
187 
188 	data->card.num_links = 1;
189 	data->card.dai_link = &data->dai;
190 
191 	snd_soc_card_set_drvdata(&data->card, data);
192 	ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
193 	if (ret) {
194 		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
195 		goto fail;
196 	}
197 
198 fail:
199 	of_node_put(cpu_np);
200 
201 	return ret;
202 }
203 
204 static const struct of_device_id imx_hdmi_dt_ids[] = {
205 	{ .compatible = "fsl,imx-audio-hdmi", },
206 	{ .compatible = "fsl,imx-audio-sii902x", },
207 	{ /* sentinel */ }
208 };
209 MODULE_DEVICE_TABLE(of, imx_hdmi_dt_ids);
210 
211 static struct platform_driver imx_hdmi_driver = {
212 	.driver = {
213 		.name = "imx-hdmi",
214 		.pm = &snd_soc_pm_ops,
215 		.of_match_table = imx_hdmi_dt_ids,
216 	},
217 	.probe = imx_hdmi_probe,
218 };
219 module_platform_driver(imx_hdmi_driver);
220 
221 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
222 MODULE_DESCRIPTION("Freescale i.MX hdmi audio ASoC machine driver");
223 MODULE_LICENSE("GPL v2");
224 MODULE_ALIAS("platform:imx-hdmi");
225