xref: /openbmc/linux/sound/soc/fsl/imx-rpmsg.c (revision 6c8c1406)
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 <linux/of_reserved_mem.h>
7 #include <linux/i2c.h>
8 #include <linux/of_gpio.h>
9 #include <linux/slab.h>
10 #include <linux/gpio.h>
11 #include <linux/clk.h>
12 #include <sound/soc.h>
13 #include <sound/jack.h>
14 #include <sound/control.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc-dapm.h>
17 #include "imx-pcm-rpmsg.h"
18 
19 struct imx_rpmsg {
20 	struct snd_soc_dai_link dai;
21 	struct snd_soc_card card;
22 	unsigned long sysclk;
23 };
24 
25 static const struct snd_soc_dapm_widget imx_rpmsg_dapm_widgets[] = {
26 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
27 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
28 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
29 	SND_SOC_DAPM_MIC("Main MIC", NULL),
30 };
31 
32 static int imx_rpmsg_late_probe(struct snd_soc_card *card)
33 {
34 	struct imx_rpmsg *data = snd_soc_card_get_drvdata(card);
35 	struct snd_soc_pcm_runtime *rtd = list_first_entry(&card->rtd_list,
36 							   struct snd_soc_pcm_runtime, list);
37 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
38 	struct device *dev = card->dev;
39 	int ret;
40 
41 	if (!data->sysclk)
42 		return 0;
43 
44 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, data->sysclk, SND_SOC_CLOCK_IN);
45 	if (ret && ret != -ENOTSUPP) {
46 		dev_err(dev, "failed to set sysclk in %s\n", __func__);
47 		return ret;
48 	}
49 
50 	return 0;
51 }
52 
53 static int imx_rpmsg_probe(struct platform_device *pdev)
54 {
55 	struct snd_soc_dai_link_component *dlc;
56 	struct device *dev = pdev->dev.parent;
57 	/* rpmsg_pdev is the platform device for the rpmsg node that probed us */
58 	struct platform_device *rpmsg_pdev = to_platform_device(dev);
59 	struct device_node *np = rpmsg_pdev->dev.of_node;
60 	struct of_phandle_args args;
61 	struct imx_rpmsg *data;
62 	int ret = 0;
63 
64 	dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
65 	if (!dlc)
66 		return -ENOMEM;
67 
68 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
69 	if (!data) {
70 		ret = -ENOMEM;
71 		goto fail;
72 	}
73 
74 	ret = of_reserved_mem_device_init_by_idx(&pdev->dev, np, 0);
75 	if (ret)
76 		dev_warn(&pdev->dev, "no reserved DMA memory\n");
77 
78 	data->dai.cpus = &dlc[0];
79 	data->dai.num_cpus = 1;
80 	data->dai.platforms = &dlc[1];
81 	data->dai.num_platforms = 1;
82 	data->dai.codecs = &dlc[2];
83 	data->dai.num_codecs = 1;
84 
85 	data->dai.name = "rpmsg hifi";
86 	data->dai.stream_name = "rpmsg hifi";
87 	data->dai.dai_fmt = SND_SOC_DAIFMT_I2S |
88 			    SND_SOC_DAIFMT_NB_NF |
89 			    SND_SOC_DAIFMT_CBC_CFC;
90 
91 	/* Optional codec node */
92 	ret = of_parse_phandle_with_fixed_args(np, "audio-codec", 0, 0, &args);
93 	if (ret) {
94 		data->dai.codecs->dai_name = "snd-soc-dummy-dai";
95 		data->dai.codecs->name = "snd-soc-dummy";
96 	} else {
97 		struct clk *clk;
98 
99 		data->dai.codecs->of_node = args.np;
100 		ret = snd_soc_get_dai_name(&args, &data->dai.codecs->dai_name);
101 		if (ret) {
102 			dev_err(&pdev->dev, "Unable to get codec_dai_name\n");
103 			goto fail;
104 		}
105 
106 		clk = devm_get_clk_from_child(&pdev->dev, args.np, NULL);
107 		if (!IS_ERR(clk))
108 			data->sysclk = clk_get_rate(clk);
109 	}
110 
111 	data->dai.cpus->dai_name = dev_name(&rpmsg_pdev->dev);
112 	data->dai.platforms->name = IMX_PCM_DRV_NAME;
113 	data->dai.playback_only = true;
114 	data->dai.capture_only = true;
115 	data->card.num_links = 1;
116 	data->card.dai_link = &data->dai;
117 
118 	if (of_property_read_bool(np, "fsl,rpmsg-out"))
119 		data->dai.capture_only = false;
120 
121 	if (of_property_read_bool(np, "fsl,rpmsg-in"))
122 		data->dai.playback_only = false;
123 
124 	if (data->dai.playback_only && data->dai.capture_only) {
125 		dev_err(&pdev->dev, "no enabled rpmsg DAI link\n");
126 		ret = -EINVAL;
127 		goto fail;
128 	}
129 
130 	data->card.dev = &pdev->dev;
131 	data->card.owner = THIS_MODULE;
132 	data->card.dapm_widgets = imx_rpmsg_dapm_widgets;
133 	data->card.num_dapm_widgets = ARRAY_SIZE(imx_rpmsg_dapm_widgets);
134 	data->card.late_probe = imx_rpmsg_late_probe;
135 	/*
136 	 * Inoder to use common api to get card name and audio routing.
137 	 * Use parent of_node for this device, revert it after finishing using
138 	 */
139 	data->card.dev->of_node = np;
140 
141 	ret = snd_soc_of_parse_card_name(&data->card, "model");
142 	if (ret)
143 		goto fail;
144 
145 	if (of_property_read_bool(np, "audio-routing")) {
146 		ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
147 		if (ret) {
148 			dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
149 			goto fail;
150 		}
151 	}
152 
153 	platform_set_drvdata(pdev, &data->card);
154 	snd_soc_card_set_drvdata(&data->card, data);
155 	ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
156 	if (ret) {
157 		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
158 		goto fail;
159 	}
160 
161 fail:
162 	pdev->dev.of_node = NULL;
163 	return ret;
164 }
165 
166 static struct platform_driver imx_rpmsg_driver = {
167 	.driver = {
168 		.name = "imx-audio-rpmsg",
169 		.pm = &snd_soc_pm_ops,
170 	},
171 	.probe = imx_rpmsg_probe,
172 };
173 module_platform_driver(imx_rpmsg_driver);
174 
175 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG Machine Driver");
176 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
177 MODULE_ALIAS("platform:imx-audio-rpmsg");
178 MODULE_LICENSE("GPL v2");
179