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